merge from trunk
[emacs.git] / src / xdisp.c
blob67bf15bddd194920f31dd0ab9eb476eddf9d9a7e
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"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
306 #include "font.h"
307 #ifdef HAVE_XWIDGETS
308 #include "xwidget.h"
309 #endif
310 #ifndef FRAME_X_OUTPUT
311 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
312 #endif
314 #define INFINITY 10000000
316 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
317 Lisp_Object Qwindow_scroll_functions;
318 static Lisp_Object Qwindow_text_change_functions;
319 static Lisp_Object Qredisplay_end_trigger_functions;
320 Lisp_Object Qinhibit_point_motion_hooks;
321 static Lisp_Object QCeval, QCpropertize;
322 Lisp_Object QCfile, QCdata;
323 static Lisp_Object Qfontified;
324 static Lisp_Object Qgrow_only;
325 static Lisp_Object Qinhibit_eval_during_redisplay;
326 static Lisp_Object Qbuffer_position, Qposition, Qobject;
327 static Lisp_Object Qright_to_left, Qleft_to_right;
329 /* Cursor shapes. */
330 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
332 /* Pointer shapes. */
333 static Lisp_Object Qarrow, Qhand;
334 Lisp_Object Qtext;
336 /* Holds the list (error). */
337 static Lisp_Object list_of_error;
339 static Lisp_Object Qfontification_functions;
341 static Lisp_Object Qwrap_prefix;
342 static Lisp_Object Qline_prefix;
343 static Lisp_Object Qredisplay_internal;
345 /* Non-nil means don't actually do any redisplay. */
347 Lisp_Object Qinhibit_redisplay;
349 /* Names of text properties relevant for redisplay. */
351 Lisp_Object Qdisplay;
353 Lisp_Object Qspace, QCalign_to;
354 static Lisp_Object QCrelative_width, QCrelative_height;
355 Lisp_Object Qleft_margin, Qright_margin;
356 static Lisp_Object Qspace_width, Qraise;
357 static Lisp_Object Qslice;
358 Lisp_Object Qcenter;
359 static Lisp_Object Qmargin, Qpointer;
360 static Lisp_Object Qline_height;
362 #ifdef HAVE_WINDOW_SYSTEM
364 /* Test if overflow newline into fringe. Called with iterator IT
365 at or past right window margin, and with IT->current_x set. */
367 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
368 (!NILP (Voverflow_newline_into_fringe) \
369 && FRAME_WINDOW_P ((IT)->f) \
370 && ((IT)->bidi_it.paragraph_dir == R2L \
371 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
372 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
373 && (IT)->current_x == (IT)->last_visible_x)
375 #else /* !HAVE_WINDOW_SYSTEM */
376 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
377 #endif /* HAVE_WINDOW_SYSTEM */
379 /* Test if the display element loaded in IT, or the underlying buffer
380 or string character, is a space or a TAB character. This is used
381 to determine where word wrapping can occur. */
383 #define IT_DISPLAYING_WHITESPACE(it) \
384 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
385 || ((STRINGP (it->string) \
386 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
387 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
388 || (it->s \
389 && (it->s[IT_BYTEPOS (*it)] == ' ' \
390 || it->s[IT_BYTEPOS (*it)] == '\t')) \
391 || (IT_BYTEPOS (*it) < ZV_BYTE \
392 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
393 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
395 /* Name of the face used to highlight trailing whitespace. */
397 static Lisp_Object Qtrailing_whitespace;
399 /* Name and number of the face used to highlight escape glyphs. */
401 static Lisp_Object Qescape_glyph;
403 /* Name and number of the face used to highlight non-breaking spaces. */
405 static Lisp_Object Qnobreak_space;
407 /* The symbol `image' which is the car of the lists used to represent
408 images in Lisp. Also a tool bar style. */
410 Lisp_Object Qimage;
412 /* The image map types. */
413 Lisp_Object QCmap;
414 static Lisp_Object QCpointer;
415 static Lisp_Object Qrect, Qcircle, Qpoly;
417 /* Tool bar styles */
418 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
420 /* Non-zero means print newline to stdout before next mini-buffer
421 message. */
423 int noninteractive_need_newline;
425 /* Non-zero means print newline to message log before next message. */
427 static int message_log_need_newline;
429 /* Three markers that message_dolog uses.
430 It could allocate them itself, but that causes trouble
431 in handling memory-full errors. */
432 static Lisp_Object message_dolog_marker1;
433 static Lisp_Object message_dolog_marker2;
434 static Lisp_Object message_dolog_marker3;
436 /* The buffer position of the first character appearing entirely or
437 partially on the line of the selected window which contains the
438 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
439 redisplay optimization in redisplay_internal. */
441 static struct text_pos this_line_start_pos;
443 /* Number of characters past the end of the line above, including the
444 terminating newline. */
446 static struct text_pos this_line_end_pos;
448 /* The vertical positions and the height of this line. */
450 static int this_line_vpos;
451 static int this_line_y;
452 static int this_line_pixel_height;
454 /* X position at which this display line starts. Usually zero;
455 negative if first character is partially visible. */
457 static int this_line_start_x;
459 /* The smallest character position seen by move_it_* functions as they
460 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
461 hscrolled lines, see display_line. */
463 static struct text_pos this_line_min_pos;
465 /* Buffer that this_line_.* variables are referring to. */
467 static struct buffer *this_line_buffer;
470 /* Values of those variables at last redisplay are stored as
471 properties on `overlay-arrow-position' symbol. However, if
472 Voverlay_arrow_position is a marker, last-arrow-position is its
473 numerical position. */
475 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
477 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
478 properties on a symbol in overlay-arrow-variable-list. */
480 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
482 Lisp_Object Qmenu_bar_update_hook;
484 /* Nonzero if an overlay arrow has been displayed in this window. */
486 static int overlay_arrow_seen;
488 /* Vector containing glyphs for an ellipsis `...'. */
490 static Lisp_Object default_invis_vector[3];
492 /* This is the window where the echo area message was displayed. It
493 is always a mini-buffer window, but it may not be the same window
494 currently active as a mini-buffer. */
496 Lisp_Object echo_area_window;
498 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
499 pushes the current message and the value of
500 message_enable_multibyte on the stack, the function restore_message
501 pops the stack and displays MESSAGE again. */
503 static Lisp_Object Vmessage_stack;
505 /* Nonzero means multibyte characters were enabled when the echo area
506 message was specified. */
508 static int message_enable_multibyte;
510 /* Nonzero if we should redraw the mode lines on the next redisplay. */
512 int update_mode_lines;
514 /* Nonzero if window sizes or contents have changed since last
515 redisplay that finished. */
517 int windows_or_buffers_changed;
519 /* Nonzero after display_mode_line if %l was used and it displayed a
520 line number. */
522 static int line_number_displayed;
524 /* The name of the *Messages* buffer, a string. */
526 static Lisp_Object Vmessages_buffer_name;
528 /* Current, index 0, and last displayed echo area message. Either
529 buffers from echo_buffers, or nil to indicate no message. */
531 Lisp_Object echo_area_buffer[2];
533 /* The buffers referenced from echo_area_buffer. */
535 static Lisp_Object echo_buffer[2];
537 /* A vector saved used in with_area_buffer to reduce consing. */
539 static Lisp_Object Vwith_echo_area_save_vector;
541 /* Non-zero means display_echo_area should display the last echo area
542 message again. Set by redisplay_preserve_echo_area. */
544 static int display_last_displayed_message_p;
546 /* Nonzero if echo area is being used by print; zero if being used by
547 message. */
549 static int message_buf_print;
551 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
553 static Lisp_Object Qinhibit_menubar_update;
554 static Lisp_Object Qmessage_truncate_lines;
556 /* Set to 1 in clear_message to make redisplay_internal aware
557 of an emptied echo area. */
559 static int message_cleared_p;
561 /* A scratch glyph row with contents used for generating truncation
562 glyphs. Also used in direct_output_for_insert. */
564 #define MAX_SCRATCH_GLYPHS 100
565 static struct glyph_row scratch_glyph_row;
566 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
568 /* Ascent and height of the last line processed by move_it_to. */
570 static int last_height;
572 /* Non-zero if there's a help-echo in the echo area. */
574 int help_echo_showing_p;
576 /* The maximum distance to look ahead for text properties. Values
577 that are too small let us call compute_char_face and similar
578 functions too often which is expensive. Values that are too large
579 let us call compute_char_face and alike too often because we
580 might not be interested in text properties that far away. */
582 #define TEXT_PROP_DISTANCE_LIMIT 100
584 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
585 iterator state and later restore it. This is needed because the
586 bidi iterator on bidi.c keeps a stacked cache of its states, which
587 is really a singleton. When we use scratch iterator objects to
588 move around the buffer, we can cause the bidi cache to be pushed or
589 popped, and therefore we need to restore the cache state when we
590 return to the original iterator. */
591 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
592 do { \
593 if (CACHE) \
594 bidi_unshelve_cache (CACHE, 1); \
595 ITCOPY = ITORIG; \
596 CACHE = bidi_shelve_cache (); \
597 } while (0)
599 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
600 do { \
601 if (pITORIG != pITCOPY) \
602 *(pITORIG) = *(pITCOPY); \
603 bidi_unshelve_cache (CACHE, 0); \
604 CACHE = NULL; \
605 } while (0)
607 #ifdef GLYPH_DEBUG
609 /* Non-zero means print traces of redisplay if compiled with
610 GLYPH_DEBUG defined. */
612 int trace_redisplay_p;
614 #endif /* GLYPH_DEBUG */
616 #ifdef DEBUG_TRACE_MOVE
617 /* Non-zero means trace with TRACE_MOVE to stderr. */
618 int trace_move;
620 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
621 #else
622 #define TRACE_MOVE(x) (void) 0
623 #endif
625 static Lisp_Object Qauto_hscroll_mode;
627 /* Buffer being redisplayed -- for redisplay_window_error. */
629 static struct buffer *displayed_buffer;
631 /* Value returned from text property handlers (see below). */
633 enum prop_handled
635 HANDLED_NORMALLY,
636 HANDLED_RECOMPUTE_PROPS,
637 HANDLED_OVERLAY_STRING_CONSUMED,
638 HANDLED_RETURN
641 /* A description of text properties that redisplay is interested
642 in. */
644 struct props
646 /* The name of the property. */
647 Lisp_Object *name;
649 /* A unique index for the property. */
650 enum prop_idx idx;
652 /* A handler function called to set up iterator IT from the property
653 at IT's current position. Value is used to steer handle_stop. */
654 enum prop_handled (*handler) (struct it *it);
657 static enum prop_handled handle_face_prop (struct it *);
658 static enum prop_handled handle_invisible_prop (struct it *);
659 static enum prop_handled handle_display_prop (struct it *);
660 static enum prop_handled handle_composition_prop (struct it *);
661 static enum prop_handled handle_overlay_change (struct it *);
662 static enum prop_handled handle_fontified_prop (struct it *);
664 /* Properties handled by iterators. */
666 static struct props it_props[] =
668 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
669 /* Handle `face' before `display' because some sub-properties of
670 `display' need to know the face. */
671 {&Qface, FACE_PROP_IDX, handle_face_prop},
672 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
673 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
674 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
675 {NULL, 0, NULL}
678 /* Value is the position described by X. If X is a marker, value is
679 the marker_position of X. Otherwise, value is X. */
681 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
683 /* Enumeration returned by some move_it_.* functions internally. */
685 enum move_it_result
687 /* Not used. Undefined value. */
688 MOVE_UNDEFINED,
690 /* Move ended at the requested buffer position or ZV. */
691 MOVE_POS_MATCH_OR_ZV,
693 /* Move ended at the requested X pixel position. */
694 MOVE_X_REACHED,
696 /* Move within a line ended at the end of a line that must be
697 continued. */
698 MOVE_LINE_CONTINUED,
700 /* Move within a line ended at the end of a line that would
701 be displayed truncated. */
702 MOVE_LINE_TRUNCATED,
704 /* Move within a line ended at a line end. */
705 MOVE_NEWLINE_OR_CR
708 /* This counter is used to clear the face cache every once in a while
709 in redisplay_internal. It is incremented for each redisplay.
710 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
711 cleared. */
713 #define CLEAR_FACE_CACHE_COUNT 500
714 static int clear_face_cache_count;
716 /* Similarly for the image cache. */
718 #ifdef HAVE_WINDOW_SYSTEM
719 #define CLEAR_IMAGE_CACHE_COUNT 101
720 static int clear_image_cache_count;
722 /* Null glyph slice */
723 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
724 #endif
726 /* True while redisplay_internal is in progress. */
728 bool redisplaying_p;
730 static Lisp_Object Qinhibit_free_realized_faces;
731 static Lisp_Object Qmode_line_default_help_echo;
733 /* If a string, XTread_socket generates an event to display that string.
734 (The display is done in read_char.) */
736 Lisp_Object help_echo_string;
737 Lisp_Object help_echo_window;
738 Lisp_Object help_echo_object;
739 ptrdiff_t help_echo_pos;
741 /* Temporary variable for XTread_socket. */
743 Lisp_Object previous_help_echo_string;
745 /* Platform-independent portion of hourglass implementation. */
747 #ifdef HAVE_WINDOW_SYSTEM
749 /* Non-zero means an hourglass cursor is currently shown. */
750 int hourglass_shown_p;
752 /* If non-null, an asynchronous timer that, when it expires, displays
753 an hourglass cursor on all frames. */
754 struct atimer *hourglass_atimer;
756 #endif /* HAVE_WINDOW_SYSTEM */
758 /* Name of the face used to display glyphless characters. */
759 Lisp_Object Qglyphless_char;
761 /* Symbol for the purpose of Vglyphless_char_display. */
762 static Lisp_Object Qglyphless_char_display;
764 /* Method symbols for Vglyphless_char_display. */
765 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
767 /* Default number of seconds to wait before displaying an hourglass
768 cursor. */
769 #define DEFAULT_HOURGLASS_DELAY 1
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* Default pixel width of `thin-space' display method. */
774 #define THIN_SPACE_WIDTH 1
776 #endif /* HAVE_WINDOW_SYSTEM */
778 /* Function prototypes. */
780 static void setup_for_ellipsis (struct it *, int);
781 static void set_iterator_to_next (struct it *, int);
782 static void mark_window_display_accurate_1 (struct window *, int);
783 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
784 static int display_prop_string_p (Lisp_Object, Lisp_Object);
785 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
786 static int cursor_row_p (struct glyph_row *);
787 static int redisplay_mode_lines (Lisp_Object, int);
788 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
790 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
792 static void handle_line_prefix (struct it *);
794 static void pint2str (char *, int, ptrdiff_t);
795 static void pint2hrstr (char *, int, ptrdiff_t);
796 static struct text_pos run_window_scroll_functions (Lisp_Object,
797 struct text_pos);
798 static int text_outside_line_unchanged_p (struct window *,
799 ptrdiff_t, ptrdiff_t);
800 static void store_mode_line_noprop_char (char);
801 static int store_mode_line_noprop (const char *, int, int);
802 static void handle_stop (struct it *);
803 static void handle_stop_backwards (struct it *, ptrdiff_t);
804 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
805 static void ensure_echo_area_buffers (void);
806 static void unwind_with_echo_area_buffer (Lisp_Object);
807 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
808 static int with_echo_area_buffer (struct window *, int,
809 int (*) (ptrdiff_t, Lisp_Object),
810 ptrdiff_t, Lisp_Object);
811 static void clear_garbaged_frames (void);
812 static int current_message_1 (ptrdiff_t, Lisp_Object);
813 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
814 static void set_message (Lisp_Object);
815 static int set_message_1 (ptrdiff_t, Lisp_Object);
816 static int display_echo_area (struct window *);
817 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
818 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
819 static void unwind_redisplay (void);
820 static int string_char_and_length (const unsigned char *, int *);
821 static struct text_pos display_prop_end (struct it *, Lisp_Object,
822 struct text_pos);
823 static int compute_window_start_on_continuation_line (struct window *);
824 static void insert_left_trunc_glyphs (struct it *);
825 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
826 Lisp_Object);
827 static void extend_face_to_end_of_line (struct it *);
828 static int append_space_for_newline (struct it *, int);
829 static int cursor_row_fully_visible_p (struct window *, int, int);
830 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
831 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
832 static int trailing_whitespace_p (ptrdiff_t);
833 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
834 static void push_it (struct it *, struct text_pos *);
835 static void iterate_out_of_display_property (struct it *);
836 static void pop_it (struct it *);
837 static void sync_frame_with_window_matrix_rows (struct window *);
838 static void redisplay_internal (void);
839 static int echo_area_display (int);
840 static void redisplay_windows (Lisp_Object);
841 static void redisplay_window (Lisp_Object, int);
842 static Lisp_Object redisplay_window_error (Lisp_Object);
843 static Lisp_Object redisplay_window_0 (Lisp_Object);
844 static Lisp_Object redisplay_window_1 (Lisp_Object);
845 static int set_cursor_from_row (struct window *, struct glyph_row *,
846 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
847 int, int);
848 static int update_menu_bar (struct frame *, int, int);
849 static int try_window_reusing_current_matrix (struct window *);
850 static int try_window_id (struct window *);
851 static int display_line (struct it *);
852 static int display_mode_lines (struct window *);
853 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
854 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
855 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
856 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
857 static void display_menu_bar (struct window *);
858 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
859 ptrdiff_t *);
860 static int display_string (const char *, Lisp_Object, Lisp_Object,
861 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
862 static void compute_line_metrics (struct it *);
863 static void run_redisplay_end_trigger_hook (struct it *);
864 static int get_overlay_strings (struct it *, ptrdiff_t);
865 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
866 static void next_overlay_string (struct it *);
867 static void reseat (struct it *, struct text_pos, int);
868 static void reseat_1 (struct it *, struct text_pos, int);
869 static void back_to_previous_visible_line_start (struct it *);
870 static void reseat_at_next_visible_line_start (struct it *, int);
871 static int next_element_from_ellipsis (struct it *);
872 static int next_element_from_display_vector (struct it *);
873 static int next_element_from_string (struct it *);
874 static int next_element_from_c_string (struct it *);
875 static int next_element_from_buffer (struct it *);
876 static int next_element_from_composition (struct it *);
877 static int next_element_from_image (struct it *);
878 #ifdef HAVE_XWIDGETS
879 static int next_element_from_xwidget(struct it *);
880 #endif
881 static int next_element_from_stretch (struct it *);
882 static void load_overlay_strings (struct it *, ptrdiff_t);
883 static int init_from_display_pos (struct it *, struct window *,
884 struct display_pos *);
885 static void reseat_to_string (struct it *, const char *,
886 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
887 static int get_next_display_element (struct it *);
888 static enum move_it_result
889 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
890 enum move_operation_enum);
891 static void get_visually_first_element (struct it *);
892 static void init_to_row_start (struct it *, struct window *,
893 struct glyph_row *);
894 static int init_to_row_end (struct it *, struct window *,
895 struct glyph_row *);
896 static void back_to_previous_line_start (struct it *);
897 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
898 static struct text_pos string_pos_nchars_ahead (struct text_pos,
899 Lisp_Object, ptrdiff_t);
900 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
901 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
902 static ptrdiff_t number_of_chars (const char *, bool);
903 static void compute_stop_pos (struct it *);
904 static void compute_string_pos (struct text_pos *, struct text_pos,
905 Lisp_Object);
906 static int face_before_or_after_it_pos (struct it *, int);
907 static ptrdiff_t next_overlay_change (ptrdiff_t);
908 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
909 Lisp_Object, struct text_pos *, ptrdiff_t, int);
910 static int handle_single_display_spec (struct it *, Lisp_Object,
911 Lisp_Object, Lisp_Object,
912 struct text_pos *, ptrdiff_t, int, int);
913 static int underlying_face_id (struct it *);
914 static int in_ellipses_for_invisible_text_p (struct display_pos *,
915 struct window *);
917 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
918 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
920 #ifdef HAVE_WINDOW_SYSTEM
922 static void x_consider_frame_title (Lisp_Object);
923 static void update_tool_bar (struct frame *, int);
924 static int redisplay_tool_bar (struct frame *);
925 static void notice_overwritten_cursor (struct window *,
926 enum glyph_row_area,
927 int, int, int, int);
928 static void append_stretch_glyph (struct it *, Lisp_Object,
929 int, int, int);
932 #endif /* HAVE_WINDOW_SYSTEM */
934 static void produce_special_glyphs (struct it *, enum display_element_type);
935 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
936 static int coords_in_mouse_face_p (struct window *, int, int);
940 /***********************************************************************
941 Window display dimensions
942 ***********************************************************************/
944 /* Return the bottom boundary y-position for text lines in window W.
945 This is the first y position at which a line cannot start.
946 It is relative to the top of the window.
948 This is the height of W minus the height of a mode line, if any. */
951 window_text_bottom_y (struct window *w)
953 int height = WINDOW_TOTAL_HEIGHT (w);
955 if (WINDOW_WANTS_MODELINE_P (w))
956 height -= CURRENT_MODE_LINE_HEIGHT (w);
957 return height;
960 /* Return the pixel width of display area AREA of window W.
961 ANY_AREA means return the total width of W, not including
962 fringes to the left and right of the window. */
965 window_box_width (struct window *w, enum glyph_row_area area)
967 int cols = w->total_cols;
968 int pixels = 0;
970 if (!w->pseudo_window_p)
972 cols -= WINDOW_SCROLL_BAR_COLS (w);
974 if (area == TEXT_AREA)
976 cols -= max (0, w->left_margin_cols);
977 cols -= max (0, w->right_margin_cols);
978 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
980 else if (area == LEFT_MARGIN_AREA)
982 cols = max (0, w->left_margin_cols);
983 pixels = 0;
985 else if (area == RIGHT_MARGIN_AREA)
987 cols = max (0, w->right_margin_cols);
988 pixels = 0;
992 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
996 /* Return the pixel height of the display area of window W, not
997 including mode lines of W, if any. */
1000 window_box_height (struct window *w)
1002 struct frame *f = XFRAME (w->frame);
1003 int height = WINDOW_TOTAL_HEIGHT (w);
1005 eassert (height >= 0);
1007 /* Note: the code below that determines the mode-line/header-line
1008 height is essentially the same as that contained in the macro
1009 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1010 the appropriate glyph row has its `mode_line_p' flag set,
1011 and if it doesn't, uses estimate_mode_line_height instead. */
1013 if (WINDOW_WANTS_MODELINE_P (w))
1015 struct glyph_row *ml_row
1016 = (w->current_matrix && w->current_matrix->rows
1017 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1018 : 0);
1019 if (ml_row && ml_row->mode_line_p)
1020 height -= ml_row->height;
1021 else
1022 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1025 if (WINDOW_WANTS_HEADER_LINE_P (w))
1027 struct glyph_row *hl_row
1028 = (w->current_matrix && w->current_matrix->rows
1029 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1030 : 0);
1031 if (hl_row && hl_row->mode_line_p)
1032 height -= hl_row->height;
1033 else
1034 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1037 /* With a very small font and a mode-line that's taller than
1038 default, we might end up with a negative height. */
1039 return max (0, height);
1042 /* Return the window-relative coordinate of the left edge of display
1043 area AREA of window W. ANY_AREA means return the left edge of the
1044 whole window, to the right of the left fringe of W. */
1047 window_box_left_offset (struct window *w, enum glyph_row_area area)
1049 int x;
1051 if (w->pseudo_window_p)
1052 return 0;
1054 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1056 if (area == TEXT_AREA)
1057 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1058 + window_box_width (w, LEFT_MARGIN_AREA));
1059 else if (area == RIGHT_MARGIN_AREA)
1060 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1061 + window_box_width (w, LEFT_MARGIN_AREA)
1062 + window_box_width (w, TEXT_AREA)
1063 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1065 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1066 else if (area == LEFT_MARGIN_AREA
1067 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1068 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1070 return x;
1074 /* Return the window-relative coordinate of the right edge of display
1075 area AREA of window W. ANY_AREA means return the right edge of the
1076 whole window, to the left of the right fringe of W. */
1079 window_box_right_offset (struct window *w, enum glyph_row_area area)
1081 return window_box_left_offset (w, area) + window_box_width (w, area);
1084 /* Return the frame-relative coordinate of the left edge of display
1085 area AREA of window W. ANY_AREA means return the left edge of the
1086 whole window, to the right of the left fringe of W. */
1089 window_box_left (struct window *w, enum glyph_row_area area)
1091 struct frame *f = XFRAME (w->frame);
1092 int x;
1094 if (w->pseudo_window_p)
1095 return FRAME_INTERNAL_BORDER_WIDTH (f);
1097 x = (WINDOW_LEFT_EDGE_X (w)
1098 + window_box_left_offset (w, area));
1100 return x;
1104 /* Return the frame-relative coordinate of the right edge of display
1105 area AREA of window W. ANY_AREA means return the right edge of the
1106 whole window, to the left of the right fringe of W. */
1109 window_box_right (struct window *w, enum glyph_row_area area)
1111 return window_box_left (w, area) + window_box_width (w, area);
1114 /* Get the bounding box of the display area AREA of window W, without
1115 mode lines, in frame-relative coordinates. ANY_AREA means the
1116 whole window, not including the left and right fringes of
1117 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1118 coordinates of the upper-left corner of the box. Return in
1119 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1121 void
1122 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1123 int *box_y, int *box_width, int *box_height)
1125 if (box_width)
1126 *box_width = window_box_width (w, area);
1127 if (box_height)
1128 *box_height = window_box_height (w);
1129 if (box_x)
1130 *box_x = window_box_left (w, area);
1131 if (box_y)
1133 *box_y = WINDOW_TOP_EDGE_Y (w);
1134 if (WINDOW_WANTS_HEADER_LINE_P (w))
1135 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1139 #ifdef HAVE_WINDOW_SYSTEM
1141 /* Get the bounding box of the display area AREA of window W, without
1142 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1143 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1144 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1145 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1146 box. */
1148 static void
1149 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1150 int *bottom_right_x, int *bottom_right_y)
1152 window_box (w, ANY_AREA, top_left_x, top_left_y,
1153 bottom_right_x, bottom_right_y);
1154 *bottom_right_x += *top_left_x;
1155 *bottom_right_y += *top_left_y;
1158 #endif /* HAVE_WINDOW_SYSTEM */
1160 /***********************************************************************
1161 Utilities
1162 ***********************************************************************/
1164 /* Return the bottom y-position of the line the iterator IT is in.
1165 This can modify IT's settings. */
1168 line_bottom_y (struct it *it)
1170 int line_height = it->max_ascent + it->max_descent;
1171 int line_top_y = it->current_y;
1173 if (line_height == 0)
1175 if (last_height)
1176 line_height = last_height;
1177 else if (IT_CHARPOS (*it) < ZV)
1179 move_it_by_lines (it, 1);
1180 line_height = (it->max_ascent || it->max_descent
1181 ? it->max_ascent + it->max_descent
1182 : last_height);
1184 else
1186 struct glyph_row *row = it->glyph_row;
1188 /* Use the default character height. */
1189 it->glyph_row = NULL;
1190 it->what = IT_CHARACTER;
1191 it->c = ' ';
1192 it->len = 1;
1193 PRODUCE_GLYPHS (it);
1194 line_height = it->ascent + it->descent;
1195 it->glyph_row = row;
1199 return line_top_y + line_height;
1202 DEFUN ("line-pixel-height", Fline_pixel_height,
1203 Sline_pixel_height, 0, 0, 0,
1204 doc: /* Return height in pixels of text line in the selected window.
1206 Value is the height in pixels of the line at point. */)
1207 (void)
1209 struct it it;
1210 struct text_pos pt;
1211 struct window *w = XWINDOW (selected_window);
1213 SET_TEXT_POS (pt, PT, PT_BYTE);
1214 start_display (&it, w, pt);
1215 it.vpos = it.current_y = 0;
1216 last_height = 0;
1217 return make_number (line_bottom_y (&it));
1220 /* Return the default pixel height of text lines in window W. The
1221 value is the canonical height of the W frame's default font, plus
1222 any extra space required by the line-spacing variable or frame
1223 parameter.
1225 Implementation note: this ignores any line-spacing text properties
1226 put on the newline characters. This is because those properties
1227 only affect the _screen_ line ending in the newline (i.e., in a
1228 continued line, only the last screen line will be affected), which
1229 means only a small number of lines in a buffer can ever use this
1230 feature. Since this function is used to compute the default pixel
1231 equivalent of text lines in a window, we can safely ignore those
1232 few lines. For the same reasons, we ignore the line-height
1233 properties. */
1235 default_line_pixel_height (struct window *w)
1237 struct frame *f = WINDOW_XFRAME (w);
1238 int height = FRAME_LINE_HEIGHT (f);
1240 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1242 struct buffer *b = XBUFFER (w->contents);
1243 Lisp_Object val = BVAR (b, extra_line_spacing);
1245 if (NILP (val))
1246 val = BVAR (&buffer_defaults, extra_line_spacing);
1247 if (!NILP (val))
1249 if (RANGED_INTEGERP (0, val, INT_MAX))
1250 height += XFASTINT (val);
1251 else if (FLOATP (val))
1253 int addon = XFLOAT_DATA (val) * height + 0.5;
1255 if (addon >= 0)
1256 height += addon;
1259 else
1260 height += f->extra_line_spacing;
1263 return height;
1266 /* Subroutine of pos_visible_p below. Extracts a display string, if
1267 any, from the display spec given as its argument. */
1268 static Lisp_Object
1269 string_from_display_spec (Lisp_Object spec)
1271 if (CONSP (spec))
1273 while (CONSP (spec))
1275 if (STRINGP (XCAR (spec)))
1276 return XCAR (spec);
1277 spec = XCDR (spec);
1280 else if (VECTORP (spec))
1282 ptrdiff_t i;
1284 for (i = 0; i < ASIZE (spec); i++)
1286 if (STRINGP (AREF (spec, i)))
1287 return AREF (spec, i);
1289 return Qnil;
1292 return spec;
1296 /* Limit insanely large values of W->hscroll on frame F to the largest
1297 value that will still prevent first_visible_x and last_visible_x of
1298 'struct it' from overflowing an int. */
1299 static int
1300 window_hscroll_limited (struct window *w, struct frame *f)
1302 ptrdiff_t window_hscroll = w->hscroll;
1303 int window_text_width = window_box_width (w, TEXT_AREA);
1304 int colwidth = FRAME_COLUMN_WIDTH (f);
1306 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1307 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1309 return window_hscroll;
1312 /* Return 1 if position CHARPOS is visible in window W.
1313 CHARPOS < 0 means return info about WINDOW_END position.
1314 If visible, set *X and *Y to pixel coordinates of top left corner.
1315 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1316 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1319 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1320 int *rtop, int *rbot, int *rowh, int *vpos)
1322 struct it it;
1323 void *itdata = bidi_shelve_cache ();
1324 struct text_pos top;
1325 int visible_p = 0;
1326 struct buffer *old_buffer = NULL;
1328 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1329 return visible_p;
1331 if (XBUFFER (w->contents) != current_buffer)
1333 old_buffer = current_buffer;
1334 set_buffer_internal_1 (XBUFFER (w->contents));
1337 SET_TEXT_POS_FROM_MARKER (top, w->start);
1338 /* Scrolling a minibuffer window via scroll bar when the echo area
1339 shows long text sometimes resets the minibuffer contents behind
1340 our backs. */
1341 if (CHARPOS (top) > ZV)
1342 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1344 /* Compute exact mode line heights. */
1345 if (WINDOW_WANTS_MODELINE_P (w))
1346 w->mode_line_height
1347 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1348 BVAR (current_buffer, mode_line_format));
1350 if (WINDOW_WANTS_HEADER_LINE_P (w))
1351 w->header_line_height
1352 = display_mode_line (w, HEADER_LINE_FACE_ID,
1353 BVAR (current_buffer, header_line_format));
1355 start_display (&it, w, top);
1356 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1357 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1359 if (charpos >= 0
1360 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1361 && IT_CHARPOS (it) >= charpos)
1362 /* When scanning backwards under bidi iteration, move_it_to
1363 stops at or _before_ CHARPOS, because it stops at or to
1364 the _right_ of the character at CHARPOS. */
1365 || (it.bidi_p && it.bidi_it.scan_dir == -1
1366 && IT_CHARPOS (it) <= charpos)))
1368 /* We have reached CHARPOS, or passed it. How the call to
1369 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1370 or covered by a display property, move_it_to stops at the end
1371 of the invisible text, to the right of CHARPOS. (ii) If
1372 CHARPOS is in a display vector, move_it_to stops on its last
1373 glyph. */
1374 int top_x = it.current_x;
1375 int top_y = it.current_y;
1376 /* Calling line_bottom_y may change it.method, it.position, etc. */
1377 enum it_method it_method = it.method;
1378 int bottom_y = (last_height = 0, line_bottom_y (&it));
1379 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1381 if (top_y < window_top_y)
1382 visible_p = bottom_y > window_top_y;
1383 else if (top_y < it.last_visible_y)
1384 visible_p = 1;
1385 if (bottom_y >= it.last_visible_y
1386 && it.bidi_p && it.bidi_it.scan_dir == -1
1387 && IT_CHARPOS (it) < charpos)
1389 /* When the last line of the window is scanned backwards
1390 under bidi iteration, we could be duped into thinking
1391 that we have passed CHARPOS, when in fact move_it_to
1392 simply stopped short of CHARPOS because it reached
1393 last_visible_y. To see if that's what happened, we call
1394 move_it_to again with a slightly larger vertical limit,
1395 and see if it actually moved vertically; if it did, we
1396 didn't really reach CHARPOS, which is beyond window end. */
1397 struct it save_it = it;
1398 /* Why 10? because we don't know how many canonical lines
1399 will the height of the next line(s) be. So we guess. */
1400 int ten_more_lines = 10 * default_line_pixel_height (w);
1402 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1403 MOVE_TO_POS | MOVE_TO_Y);
1404 if (it.current_y > top_y)
1405 visible_p = 0;
1407 it = save_it;
1409 if (visible_p)
1411 if (it_method == GET_FROM_DISPLAY_VECTOR)
1413 /* We stopped on the last glyph of a display vector.
1414 Try and recompute. Hack alert! */
1415 if (charpos < 2 || top.charpos >= charpos)
1416 top_x = it.glyph_row->x;
1417 else
1419 struct it it2, it2_prev;
1420 /* The idea is to get to the previous buffer
1421 position, consume the character there, and use
1422 the pixel coordinates we get after that. But if
1423 the previous buffer position is also displayed
1424 from a display vector, we need to consume all of
1425 the glyphs from that display vector. */
1426 start_display (&it2, w, top);
1427 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1428 /* If we didn't get to CHARPOS - 1, there's some
1429 replacing display property at that position, and
1430 we stopped after it. That is exactly the place
1431 whose coordinates we want. */
1432 if (IT_CHARPOS (it2) != charpos - 1)
1433 it2_prev = it2;
1434 else
1436 /* Iterate until we get out of the display
1437 vector that displays the character at
1438 CHARPOS - 1. */
1439 do {
1440 get_next_display_element (&it2);
1441 PRODUCE_GLYPHS (&it2);
1442 it2_prev = it2;
1443 set_iterator_to_next (&it2, 1);
1444 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1445 && IT_CHARPOS (it2) < charpos);
1447 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1448 || it2_prev.current_x > it2_prev.last_visible_x)
1449 top_x = it.glyph_row->x;
1450 else
1452 top_x = it2_prev.current_x;
1453 top_y = it2_prev.current_y;
1457 else if (IT_CHARPOS (it) != charpos)
1459 Lisp_Object cpos = make_number (charpos);
1460 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1461 Lisp_Object string = string_from_display_spec (spec);
1462 struct text_pos tpos;
1463 int replacing_spec_p;
1464 bool newline_in_string
1465 = (STRINGP (string)
1466 && memchr (SDATA (string), '\n', SBYTES (string)));
1468 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1469 replacing_spec_p
1470 = (!NILP (spec)
1471 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1472 charpos, FRAME_WINDOW_P (it.f)));
1473 /* The tricky code below is needed because there's a
1474 discrepancy between move_it_to and how we set cursor
1475 when PT is at the beginning of a portion of text
1476 covered by a display property or an overlay with a
1477 display property, or the display line ends in a
1478 newline from a display string. move_it_to will stop
1479 _after_ such display strings, whereas
1480 set_cursor_from_row conspires with cursor_row_p to
1481 place the cursor on the first glyph produced from the
1482 display string. */
1484 /* We have overshoot PT because it is covered by a
1485 display property that replaces the text it covers.
1486 If the string includes embedded newlines, we are also
1487 in the wrong display line. Backtrack to the correct
1488 line, where the display property begins. */
1489 if (replacing_spec_p)
1491 Lisp_Object startpos, endpos;
1492 EMACS_INT start, end;
1493 struct it it3;
1494 int it3_moved;
1496 /* Find the first and the last buffer positions
1497 covered by the display string. */
1498 endpos =
1499 Fnext_single_char_property_change (cpos, Qdisplay,
1500 Qnil, Qnil);
1501 startpos =
1502 Fprevious_single_char_property_change (endpos, Qdisplay,
1503 Qnil, Qnil);
1504 start = XFASTINT (startpos);
1505 end = XFASTINT (endpos);
1506 /* Move to the last buffer position before the
1507 display property. */
1508 start_display (&it3, w, top);
1509 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1510 /* Move forward one more line if the position before
1511 the display string is a newline or if it is the
1512 rightmost character on a line that is
1513 continued or word-wrapped. */
1514 if (it3.method == GET_FROM_BUFFER
1515 && (it3.c == '\n'
1516 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1517 move_it_by_lines (&it3, 1);
1518 else if (move_it_in_display_line_to (&it3, -1,
1519 it3.current_x
1520 + it3.pixel_width,
1521 MOVE_TO_X)
1522 == MOVE_LINE_CONTINUED)
1524 move_it_by_lines (&it3, 1);
1525 /* When we are under word-wrap, the #$@%!
1526 move_it_by_lines moves 2 lines, so we need to
1527 fix that up. */
1528 if (it3.line_wrap == WORD_WRAP)
1529 move_it_by_lines (&it3, -1);
1532 /* Record the vertical coordinate of the display
1533 line where we wound up. */
1534 top_y = it3.current_y;
1535 if (it3.bidi_p)
1537 /* When characters are reordered for display,
1538 the character displayed to the left of the
1539 display string could be _after_ the display
1540 property in the logical order. Use the
1541 smallest vertical position of these two. */
1542 start_display (&it3, w, top);
1543 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1544 if (it3.current_y < top_y)
1545 top_y = it3.current_y;
1547 /* Move from the top of the window to the beginning
1548 of the display line where the display string
1549 begins. */
1550 start_display (&it3, w, top);
1551 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1552 /* If it3_moved stays zero after the 'while' loop
1553 below, that means we already were at a newline
1554 before the loop (e.g., the display string begins
1555 with a newline), so we don't need to (and cannot)
1556 inspect the glyphs of it3.glyph_row, because
1557 PRODUCE_GLYPHS will not produce anything for a
1558 newline, and thus it3.glyph_row stays at its
1559 stale content it got at top of the window. */
1560 it3_moved = 0;
1561 /* Finally, advance the iterator until we hit the
1562 first display element whose character position is
1563 CHARPOS, or until the first newline from the
1564 display string, which signals the end of the
1565 display line. */
1566 while (get_next_display_element (&it3))
1568 PRODUCE_GLYPHS (&it3);
1569 if (IT_CHARPOS (it3) == charpos
1570 || ITERATOR_AT_END_OF_LINE_P (&it3))
1571 break;
1572 it3_moved = 1;
1573 set_iterator_to_next (&it3, 0);
1575 top_x = it3.current_x - it3.pixel_width;
1576 /* Normally, we would exit the above loop because we
1577 found the display element whose character
1578 position is CHARPOS. For the contingency that we
1579 didn't, and stopped at the first newline from the
1580 display string, move back over the glyphs
1581 produced from the string, until we find the
1582 rightmost glyph not from the string. */
1583 if (it3_moved
1584 && newline_in_string
1585 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1587 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1588 + it3.glyph_row->used[TEXT_AREA];
1590 while (EQ ((g - 1)->object, string))
1592 --g;
1593 top_x -= g->pixel_width;
1595 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1596 + it3.glyph_row->used[TEXT_AREA]);
1601 *x = top_x;
1602 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1603 *rtop = max (0, window_top_y - top_y);
1604 *rbot = max (0, bottom_y - it.last_visible_y);
1605 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1606 - max (top_y, window_top_y)));
1607 *vpos = it.vpos;
1610 else
1612 /* We were asked to provide info about WINDOW_END. */
1613 struct it it2;
1614 void *it2data = NULL;
1616 SAVE_IT (it2, it, it2data);
1617 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1618 move_it_by_lines (&it, 1);
1619 if (charpos < IT_CHARPOS (it)
1620 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1622 visible_p = 1;
1623 RESTORE_IT (&it2, &it2, it2data);
1624 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1625 *x = it2.current_x;
1626 *y = it2.current_y + it2.max_ascent - it2.ascent;
1627 *rtop = max (0, -it2.current_y);
1628 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1629 - it.last_visible_y));
1630 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1631 it.last_visible_y)
1632 - max (it2.current_y,
1633 WINDOW_HEADER_LINE_HEIGHT (w))));
1634 *vpos = it2.vpos;
1636 else
1637 bidi_unshelve_cache (it2data, 1);
1639 bidi_unshelve_cache (itdata, 0);
1641 if (old_buffer)
1642 set_buffer_internal_1 (old_buffer);
1644 if (visible_p && w->hscroll > 0)
1645 *x -=
1646 window_hscroll_limited (w, WINDOW_XFRAME (w))
1647 * WINDOW_FRAME_COLUMN_WIDTH (w);
1649 #if 0
1650 /* Debugging code. */
1651 if (visible_p)
1652 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1653 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1654 else
1655 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1656 #endif
1658 return visible_p;
1662 /* Return the next character from STR. Return in *LEN the length of
1663 the character. This is like STRING_CHAR_AND_LENGTH but never
1664 returns an invalid character. If we find one, we return a `?', but
1665 with the length of the invalid character. */
1667 static int
1668 string_char_and_length (const unsigned char *str, int *len)
1670 int c;
1672 c = STRING_CHAR_AND_LENGTH (str, *len);
1673 if (!CHAR_VALID_P (c))
1674 /* We may not change the length here because other places in Emacs
1675 don't use this function, i.e. they silently accept invalid
1676 characters. */
1677 c = '?';
1679 return c;
1684 /* Given a position POS containing a valid character and byte position
1685 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1687 static struct text_pos
1688 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1690 eassert (STRINGP (string) && nchars >= 0);
1692 if (STRING_MULTIBYTE (string))
1694 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1695 int len;
1697 while (nchars--)
1699 string_char_and_length (p, &len);
1700 p += len;
1701 CHARPOS (pos) += 1;
1702 BYTEPOS (pos) += len;
1705 else
1706 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1708 return pos;
1712 /* Value is the text position, i.e. character and byte position,
1713 for character position CHARPOS in STRING. */
1715 static struct text_pos
1716 string_pos (ptrdiff_t charpos, Lisp_Object string)
1718 struct text_pos pos;
1719 eassert (STRINGP (string));
1720 eassert (charpos >= 0);
1721 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1722 return pos;
1726 /* Value is a text position, i.e. character and byte position, for
1727 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1728 means recognize multibyte characters. */
1730 static struct text_pos
1731 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1733 struct text_pos pos;
1735 eassert (s != NULL);
1736 eassert (charpos >= 0);
1738 if (multibyte_p)
1740 int len;
1742 SET_TEXT_POS (pos, 0, 0);
1743 while (charpos--)
1745 string_char_and_length ((const unsigned char *) s, &len);
1746 s += len;
1747 CHARPOS (pos) += 1;
1748 BYTEPOS (pos) += len;
1751 else
1752 SET_TEXT_POS (pos, charpos, charpos);
1754 return pos;
1758 /* Value is the number of characters in C string S. MULTIBYTE_P
1759 non-zero means recognize multibyte characters. */
1761 static ptrdiff_t
1762 number_of_chars (const char *s, bool multibyte_p)
1764 ptrdiff_t nchars;
1766 if (multibyte_p)
1768 ptrdiff_t rest = strlen (s);
1769 int len;
1770 const unsigned char *p = (const unsigned char *) s;
1772 for (nchars = 0; rest > 0; ++nchars)
1774 string_char_and_length (p, &len);
1775 rest -= len, p += len;
1778 else
1779 nchars = strlen (s);
1781 return nchars;
1785 /* Compute byte position NEWPOS->bytepos corresponding to
1786 NEWPOS->charpos. POS is a known position in string STRING.
1787 NEWPOS->charpos must be >= POS.charpos. */
1789 static void
1790 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1792 eassert (STRINGP (string));
1793 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1795 if (STRING_MULTIBYTE (string))
1796 *newpos = string_pos_nchars_ahead (pos, string,
1797 CHARPOS (*newpos) - CHARPOS (pos));
1798 else
1799 BYTEPOS (*newpos) = CHARPOS (*newpos);
1802 /* EXPORT:
1803 Return an estimation of the pixel height of mode or header lines on
1804 frame F. FACE_ID specifies what line's height to estimate. */
1807 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1809 #ifdef HAVE_WINDOW_SYSTEM
1810 if (FRAME_WINDOW_P (f))
1812 int height = FONT_HEIGHT (FRAME_FONT (f));
1814 /* This function is called so early when Emacs starts that the face
1815 cache and mode line face are not yet initialized. */
1816 if (FRAME_FACE_CACHE (f))
1818 struct face *face = FACE_FROM_ID (f, face_id);
1819 if (face)
1821 if (face->font)
1822 height = FONT_HEIGHT (face->font);
1823 if (face->box_line_width > 0)
1824 height += 2 * face->box_line_width;
1828 return height;
1830 #endif
1832 return 1;
1835 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1836 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1837 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1838 not force the value into range. */
1840 void
1841 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1842 int *x, int *y, NativeRectangle *bounds, int noclip)
1845 #ifdef HAVE_WINDOW_SYSTEM
1846 if (FRAME_WINDOW_P (f))
1848 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1849 even for negative values. */
1850 if (pix_x < 0)
1851 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1852 if (pix_y < 0)
1853 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1855 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1856 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1858 if (bounds)
1859 STORE_NATIVE_RECT (*bounds,
1860 FRAME_COL_TO_PIXEL_X (f, pix_x),
1861 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1862 FRAME_COLUMN_WIDTH (f) - 1,
1863 FRAME_LINE_HEIGHT (f) - 1);
1865 if (!noclip)
1867 if (pix_x < 0)
1868 pix_x = 0;
1869 else if (pix_x > FRAME_TOTAL_COLS (f))
1870 pix_x = FRAME_TOTAL_COLS (f);
1872 if (pix_y < 0)
1873 pix_y = 0;
1874 else if (pix_y > FRAME_LINES (f))
1875 pix_y = FRAME_LINES (f);
1878 #endif
1880 *x = pix_x;
1881 *y = pix_y;
1885 /* Find the glyph under window-relative coordinates X/Y in window W.
1886 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1887 strings. Return in *HPOS and *VPOS the row and column number of
1888 the glyph found. Return in *AREA the glyph area containing X.
1889 Value is a pointer to the glyph found or null if X/Y is not on
1890 text, or we can't tell because W's current matrix is not up to
1891 date. */
1893 static
1894 struct glyph *
1895 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1896 int *dx, int *dy, int *area)
1898 struct glyph *glyph, *end;
1899 struct glyph_row *row = NULL;
1900 int x0, i;
1902 /* Find row containing Y. Give up if some row is not enabled. */
1903 for (i = 0; i < w->current_matrix->nrows; ++i)
1905 row = MATRIX_ROW (w->current_matrix, i);
1906 if (!row->enabled_p)
1907 return NULL;
1908 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1909 break;
1912 *vpos = i;
1913 *hpos = 0;
1915 /* Give up if Y is not in the window. */
1916 if (i == w->current_matrix->nrows)
1917 return NULL;
1919 /* Get the glyph area containing X. */
1920 if (w->pseudo_window_p)
1922 *area = TEXT_AREA;
1923 x0 = 0;
1925 else
1927 if (x < window_box_left_offset (w, TEXT_AREA))
1929 *area = LEFT_MARGIN_AREA;
1930 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1932 else if (x < window_box_right_offset (w, TEXT_AREA))
1934 *area = TEXT_AREA;
1935 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1937 else
1939 *area = RIGHT_MARGIN_AREA;
1940 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1944 /* Find glyph containing X. */
1945 glyph = row->glyphs[*area];
1946 end = glyph + row->used[*area];
1947 x -= x0;
1948 while (glyph < end && x >= glyph->pixel_width)
1950 x -= glyph->pixel_width;
1951 ++glyph;
1954 if (glyph == end)
1955 return NULL;
1957 if (dx)
1959 *dx = x;
1960 *dy = y - (row->y + row->ascent - glyph->ascent);
1963 *hpos = glyph - row->glyphs[*area];
1964 return glyph;
1967 /* Convert frame-relative x/y to coordinates relative to window W.
1968 Takes pseudo-windows into account. */
1970 static void
1971 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1973 if (w->pseudo_window_p)
1975 /* A pseudo-window is always full-width, and starts at the
1976 left edge of the frame, plus a frame border. */
1977 struct frame *f = XFRAME (w->frame);
1978 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1979 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1981 else
1983 *x -= WINDOW_LEFT_EDGE_X (w);
1984 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1988 #ifdef HAVE_WINDOW_SYSTEM
1990 /* EXPORT:
1991 Return in RECTS[] at most N clipping rectangles for glyph string S.
1992 Return the number of stored rectangles. */
1995 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1997 XRectangle r;
1999 if (n <= 0)
2000 return 0;
2002 if (s->row->full_width_p)
2004 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2005 r.x = WINDOW_LEFT_EDGE_X (s->w);
2006 r.width = WINDOW_TOTAL_WIDTH (s->w);
2008 /* Unless displaying a mode or menu bar line, which are always
2009 fully visible, clip to the visible part of the row. */
2010 if (s->w->pseudo_window_p)
2011 r.height = s->row->visible_height;
2012 else
2013 r.height = s->height;
2015 else
2017 /* This is a text line that may be partially visible. */
2018 r.x = window_box_left (s->w, s->area);
2019 r.width = window_box_width (s->w, s->area);
2020 r.height = s->row->visible_height;
2023 if (s->clip_head)
2024 if (r.x < s->clip_head->x)
2026 if (r.width >= s->clip_head->x - r.x)
2027 r.width -= s->clip_head->x - r.x;
2028 else
2029 r.width = 0;
2030 r.x = s->clip_head->x;
2032 if (s->clip_tail)
2033 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2035 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2036 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2037 else
2038 r.width = 0;
2041 /* If S draws overlapping rows, it's sufficient to use the top and
2042 bottom of the window for clipping because this glyph string
2043 intentionally draws over other lines. */
2044 if (s->for_overlaps)
2046 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2047 r.height = window_text_bottom_y (s->w) - r.y;
2049 /* Alas, the above simple strategy does not work for the
2050 environments with anti-aliased text: if the same text is
2051 drawn onto the same place multiple times, it gets thicker.
2052 If the overlap we are processing is for the erased cursor, we
2053 take the intersection with the rectangle of the cursor. */
2054 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2056 XRectangle rc, r_save = r;
2058 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2059 rc.y = s->w->phys_cursor.y;
2060 rc.width = s->w->phys_cursor_width;
2061 rc.height = s->w->phys_cursor_height;
2063 x_intersect_rectangles (&r_save, &rc, &r);
2066 else
2068 /* Don't use S->y for clipping because it doesn't take partially
2069 visible lines into account. For example, it can be negative for
2070 partially visible lines at the top of a window. */
2071 if (!s->row->full_width_p
2072 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2073 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2074 else
2075 r.y = max (0, s->row->y);
2078 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2080 /* If drawing the cursor, don't let glyph draw outside its
2081 advertised boundaries. Cleartype does this under some circumstances. */
2082 if (s->hl == DRAW_CURSOR)
2084 struct glyph *glyph = s->first_glyph;
2085 int height, max_y;
2087 if (s->x > r.x)
2089 r.width -= s->x - r.x;
2090 r.x = s->x;
2092 r.width = min (r.width, glyph->pixel_width);
2094 /* If r.y is below window bottom, ensure that we still see a cursor. */
2095 height = min (glyph->ascent + glyph->descent,
2096 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2097 max_y = window_text_bottom_y (s->w) - height;
2098 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2099 if (s->ybase - glyph->ascent > max_y)
2101 r.y = max_y;
2102 r.height = height;
2104 else
2106 /* Don't draw cursor glyph taller than our actual glyph. */
2107 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2108 if (height < r.height)
2110 max_y = r.y + r.height;
2111 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2112 r.height = min (max_y - r.y, height);
2117 if (s->row->clip)
2119 XRectangle r_save = r;
2121 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2122 r.width = 0;
2125 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2126 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2128 #ifdef CONVERT_FROM_XRECT
2129 CONVERT_FROM_XRECT (r, *rects);
2130 #else
2131 *rects = r;
2132 #endif
2133 return 1;
2135 else
2137 /* If we are processing overlapping and allowed to return
2138 multiple clipping rectangles, we exclude the row of the glyph
2139 string from the clipping rectangle. This is to avoid drawing
2140 the same text on the environment with anti-aliasing. */
2141 #ifdef CONVERT_FROM_XRECT
2142 XRectangle rs[2];
2143 #else
2144 XRectangle *rs = rects;
2145 #endif
2146 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2148 if (s->for_overlaps & OVERLAPS_PRED)
2150 rs[i] = r;
2151 if (r.y + r.height > row_y)
2153 if (r.y < row_y)
2154 rs[i].height = row_y - r.y;
2155 else
2156 rs[i].height = 0;
2158 i++;
2160 if (s->for_overlaps & OVERLAPS_SUCC)
2162 rs[i] = r;
2163 if (r.y < row_y + s->row->visible_height)
2165 if (r.y + r.height > row_y + s->row->visible_height)
2167 rs[i].y = row_y + s->row->visible_height;
2168 rs[i].height = r.y + r.height - rs[i].y;
2170 else
2171 rs[i].height = 0;
2173 i++;
2176 n = i;
2177 #ifdef CONVERT_FROM_XRECT
2178 for (i = 0; i < n; i++)
2179 CONVERT_FROM_XRECT (rs[i], rects[i]);
2180 #endif
2181 return n;
2185 /* EXPORT:
2186 Return in *NR the clipping rectangle for glyph string S. */
2188 void
2189 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2191 get_glyph_string_clip_rects (s, nr, 1);
2195 /* EXPORT:
2196 Return the position and height of the phys cursor in window W.
2197 Set w->phys_cursor_width to width of phys cursor.
2200 void
2201 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2202 struct glyph *glyph, int *xp, int *yp, int *heightp)
2204 struct frame *f = XFRAME (WINDOW_FRAME (w));
2205 int x, y, wd, h, h0, y0;
2207 /* Compute the width of the rectangle to draw. If on a stretch
2208 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2209 rectangle as wide as the glyph, but use a canonical character
2210 width instead. */
2211 wd = glyph->pixel_width - 1;
2212 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2213 wd++; /* Why? */
2214 #endif
2216 x = w->phys_cursor.x;
2217 if (x < 0)
2219 wd += x;
2220 x = 0;
2223 if (glyph->type == STRETCH_GLYPH
2224 && !x_stretch_cursor_p)
2225 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2226 w->phys_cursor_width = wd;
2228 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2230 /* If y is below window bottom, ensure that we still see a cursor. */
2231 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2233 h = max (h0, glyph->ascent + glyph->descent);
2234 h0 = min (h0, glyph->ascent + glyph->descent);
2236 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2237 if (y < y0)
2239 h = max (h - (y0 - y) + 1, h0);
2240 y = y0 - 1;
2242 else
2244 y0 = window_text_bottom_y (w) - h0;
2245 if (y > y0)
2247 h += y - y0;
2248 y = y0;
2252 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2253 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2254 *heightp = h;
2258 * Remember which glyph the mouse is over.
2261 void
2262 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2264 Lisp_Object window;
2265 struct window *w;
2266 struct glyph_row *r, *gr, *end_row;
2267 enum window_part part;
2268 enum glyph_row_area area;
2269 int x, y, width, height;
2271 /* Try to determine frame pixel position and size of the glyph under
2272 frame pixel coordinates X/Y on frame F. */
2274 if (!f->glyphs_initialized_p
2275 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2276 NILP (window)))
2278 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2279 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2280 goto virtual_glyph;
2283 w = XWINDOW (window);
2284 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2285 height = WINDOW_FRAME_LINE_HEIGHT (w);
2287 x = window_relative_x_coord (w, part, gx);
2288 y = gy - WINDOW_TOP_EDGE_Y (w);
2290 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2291 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2293 if (w->pseudo_window_p)
2295 area = TEXT_AREA;
2296 part = ON_MODE_LINE; /* Don't adjust margin. */
2297 goto text_glyph;
2300 switch (part)
2302 case ON_LEFT_MARGIN:
2303 area = LEFT_MARGIN_AREA;
2304 goto text_glyph;
2306 case ON_RIGHT_MARGIN:
2307 area = RIGHT_MARGIN_AREA;
2308 goto text_glyph;
2310 case ON_HEADER_LINE:
2311 case ON_MODE_LINE:
2312 gr = (part == ON_HEADER_LINE
2313 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2314 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2315 gy = gr->y;
2316 area = TEXT_AREA;
2317 goto text_glyph_row_found;
2319 case ON_TEXT:
2320 area = TEXT_AREA;
2322 text_glyph:
2323 gr = 0; gy = 0;
2324 for (; r <= end_row && r->enabled_p; ++r)
2325 if (r->y + r->height > y)
2327 gr = r; gy = r->y;
2328 break;
2331 text_glyph_row_found:
2332 if (gr && gy <= y)
2334 struct glyph *g = gr->glyphs[area];
2335 struct glyph *end = g + gr->used[area];
2337 height = gr->height;
2338 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2339 if (gx + g->pixel_width > x)
2340 break;
2342 if (g < end)
2344 if (g->type == IMAGE_GLYPH)
2346 /* Don't remember when mouse is over image, as
2347 image may have hot-spots. */
2348 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2349 return;
2351 width = g->pixel_width;
2353 else
2355 /* Use nominal char spacing at end of line. */
2356 x -= gx;
2357 gx += (x / width) * width;
2360 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2361 gx += window_box_left_offset (w, area);
2363 else
2365 /* Use nominal line height at end of window. */
2366 gx = (x / width) * width;
2367 y -= gy;
2368 gy += (y / height) * height;
2370 break;
2372 case ON_LEFT_FRINGE:
2373 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2374 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2375 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2376 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2377 goto row_glyph;
2379 case ON_RIGHT_FRINGE:
2380 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2381 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2382 : window_box_right_offset (w, TEXT_AREA));
2383 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2384 goto row_glyph;
2386 case ON_SCROLL_BAR:
2387 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2389 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2390 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2391 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2392 : 0)));
2393 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2395 row_glyph:
2396 gr = 0, gy = 0;
2397 for (; r <= end_row && r->enabled_p; ++r)
2398 if (r->y + r->height > y)
2400 gr = r; gy = r->y;
2401 break;
2404 if (gr && gy <= y)
2405 height = gr->height;
2406 else
2408 /* Use nominal line height at end of window. */
2409 y -= gy;
2410 gy += (y / height) * height;
2412 break;
2414 default:
2416 virtual_glyph:
2417 /* If there is no glyph under the mouse, then we divide the screen
2418 into a grid of the smallest glyph in the frame, and use that
2419 as our "glyph". */
2421 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2422 round down even for negative values. */
2423 if (gx < 0)
2424 gx -= width - 1;
2425 if (gy < 0)
2426 gy -= height - 1;
2428 gx = (gx / width) * width;
2429 gy = (gy / height) * height;
2431 goto store_rect;
2434 gx += WINDOW_LEFT_EDGE_X (w);
2435 gy += WINDOW_TOP_EDGE_Y (w);
2437 store_rect:
2438 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2440 /* Visible feedback for debugging. */
2441 #if 0
2442 #if HAVE_X_WINDOWS
2443 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2444 f->output_data.x->normal_gc,
2445 gx, gy, width, height);
2446 #endif
2447 #endif
2451 #endif /* HAVE_WINDOW_SYSTEM */
2453 static void
2454 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2456 eassert (w);
2457 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2458 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2459 w->window_end_vpos
2460 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2463 /***********************************************************************
2464 Lisp form evaluation
2465 ***********************************************************************/
2467 /* Error handler for safe_eval and safe_call. */
2469 static Lisp_Object
2470 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2472 add_to_log ("Error during redisplay: %S signaled %S",
2473 Flist (nargs, args), arg);
2474 return Qnil;
2477 /* Call function FUNC with the rest of NARGS - 1 arguments
2478 following. Return the result, or nil if something went
2479 wrong. Prevent redisplay during the evaluation. */
2481 Lisp_Object
2482 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2484 Lisp_Object val;
2486 if (inhibit_eval_during_redisplay)
2487 val = Qnil;
2488 else
2490 va_list ap;
2491 ptrdiff_t i;
2492 ptrdiff_t count = SPECPDL_INDEX ();
2493 struct gcpro gcpro1;
2494 Lisp_Object *args = alloca (nargs * word_size);
2496 args[0] = func;
2497 va_start (ap, func);
2498 for (i = 1; i < nargs; i++)
2499 args[i] = va_arg (ap, Lisp_Object);
2500 va_end (ap);
2502 GCPRO1 (args[0]);
2503 gcpro1.nvars = nargs;
2504 specbind (Qinhibit_redisplay, Qt);
2505 /* Use Qt to ensure debugger does not run,
2506 so there is no possibility of wanting to redisplay. */
2507 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2508 safe_eval_handler);
2509 UNGCPRO;
2510 val = unbind_to (count, val);
2513 return val;
2517 /* Call function FN with one argument ARG.
2518 Return the result, or nil if something went wrong. */
2520 Lisp_Object
2521 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2523 return safe_call (2, fn, arg);
2526 static Lisp_Object Qeval;
2528 Lisp_Object
2529 safe_eval (Lisp_Object sexpr)
2531 return safe_call1 (Qeval, sexpr);
2534 /* Call function FN with two arguments ARG1 and ARG2.
2535 Return the result, or nil if something went wrong. */
2537 Lisp_Object
2538 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2540 return safe_call (3, fn, arg1, arg2);
2545 /***********************************************************************
2546 Debugging
2547 ***********************************************************************/
2549 #if 0
2551 /* Define CHECK_IT to perform sanity checks on iterators.
2552 This is for debugging. It is too slow to do unconditionally. */
2554 static void
2555 check_it (struct it *it)
2557 if (it->method == GET_FROM_STRING)
2559 eassert (STRINGP (it->string));
2560 eassert (IT_STRING_CHARPOS (*it) >= 0);
2562 else
2564 eassert (IT_STRING_CHARPOS (*it) < 0);
2565 if (it->method == GET_FROM_BUFFER)
2567 /* Check that character and byte positions agree. */
2568 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2572 if (it->dpvec)
2573 eassert (it->current.dpvec_index >= 0);
2574 else
2575 eassert (it->current.dpvec_index < 0);
2578 #define CHECK_IT(IT) check_it ((IT))
2580 #else /* not 0 */
2582 #define CHECK_IT(IT) (void) 0
2584 #endif /* not 0 */
2587 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2589 /* Check that the window end of window W is what we expect it
2590 to be---the last row in the current matrix displaying text. */
2592 static void
2593 check_window_end (struct window *w)
2595 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2597 struct glyph_row *row;
2598 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2599 !row->enabled_p
2600 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2601 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2605 #define CHECK_WINDOW_END(W) check_window_end ((W))
2607 #else
2609 #define CHECK_WINDOW_END(W) (void) 0
2611 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2613 /* Return mark position if current buffer has the region of non-zero length,
2614 or -1 otherwise. */
2616 static ptrdiff_t
2617 markpos_of_region (void)
2619 if (!NILP (Vtransient_mark_mode)
2620 && !NILP (BVAR (current_buffer, mark_active))
2621 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2623 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2625 if (markpos != PT)
2626 return markpos;
2628 return -1;
2631 /***********************************************************************
2632 Iterator initialization
2633 ***********************************************************************/
2635 /* Initialize IT for displaying current_buffer in window W, starting
2636 at character position CHARPOS. CHARPOS < 0 means that no buffer
2637 position is specified which is useful when the iterator is assigned
2638 a position later. BYTEPOS is the byte position corresponding to
2639 CHARPOS.
2641 If ROW is not null, calls to produce_glyphs with IT as parameter
2642 will produce glyphs in that row.
2644 BASE_FACE_ID is the id of a base face to use. It must be one of
2645 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2646 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2647 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2649 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2650 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2651 will be initialized to use the corresponding mode line glyph row of
2652 the desired matrix of W. */
2654 void
2655 init_iterator (struct it *it, struct window *w,
2656 ptrdiff_t charpos, ptrdiff_t bytepos,
2657 struct glyph_row *row, enum face_id base_face_id)
2659 ptrdiff_t markpos;
2660 enum face_id remapped_base_face_id = base_face_id;
2662 /* Some precondition checks. */
2663 eassert (w != NULL && it != NULL);
2664 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2665 && charpos <= ZV));
2667 /* If face attributes have been changed since the last redisplay,
2668 free realized faces now because they depend on face definitions
2669 that might have changed. Don't free faces while there might be
2670 desired matrices pending which reference these faces. */
2671 if (face_change_count && !inhibit_free_realized_faces)
2673 face_change_count = 0;
2674 free_all_realized_faces (Qnil);
2677 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2678 if (! NILP (Vface_remapping_alist))
2679 remapped_base_face_id
2680 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2682 /* Use one of the mode line rows of W's desired matrix if
2683 appropriate. */
2684 if (row == NULL)
2686 if (base_face_id == MODE_LINE_FACE_ID
2687 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2688 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2689 else if (base_face_id == HEADER_LINE_FACE_ID)
2690 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2693 /* Clear IT. */
2694 memset (it, 0, sizeof *it);
2695 it->current.overlay_string_index = -1;
2696 it->current.dpvec_index = -1;
2697 it->base_face_id = remapped_base_face_id;
2698 it->string = Qnil;
2699 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2700 it->paragraph_embedding = L2R;
2701 it->bidi_it.string.lstring = Qnil;
2702 it->bidi_it.string.s = NULL;
2703 it->bidi_it.string.bufpos = 0;
2704 it->bidi_it.w = w;
2706 /* The window in which we iterate over current_buffer: */
2707 XSETWINDOW (it->window, w);
2708 it->w = w;
2709 it->f = XFRAME (w->frame);
2711 it->cmp_it.id = -1;
2713 /* Extra space between lines (on window systems only). */
2714 if (base_face_id == DEFAULT_FACE_ID
2715 && FRAME_WINDOW_P (it->f))
2717 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2718 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2719 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2720 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2721 * FRAME_LINE_HEIGHT (it->f));
2722 else if (it->f->extra_line_spacing > 0)
2723 it->extra_line_spacing = it->f->extra_line_spacing;
2724 it->max_extra_line_spacing = 0;
2727 /* If realized faces have been removed, e.g. because of face
2728 attribute changes of named faces, recompute them. When running
2729 in batch mode, the face cache of the initial frame is null. If
2730 we happen to get called, make a dummy face cache. */
2731 if (FRAME_FACE_CACHE (it->f) == NULL)
2732 init_frame_faces (it->f);
2733 if (FRAME_FACE_CACHE (it->f)->used == 0)
2734 recompute_basic_faces (it->f);
2736 /* Current value of the `slice', `space-width', and 'height' properties. */
2737 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2738 it->space_width = Qnil;
2739 it->font_height = Qnil;
2740 it->override_ascent = -1;
2742 /* Are control characters displayed as `^C'? */
2743 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2745 /* -1 means everything between a CR and the following line end
2746 is invisible. >0 means lines indented more than this value are
2747 invisible. */
2748 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2749 ? (clip_to_bounds
2750 (-1, XINT (BVAR (current_buffer, selective_display)),
2751 PTRDIFF_MAX))
2752 : (!NILP (BVAR (current_buffer, selective_display))
2753 ? -1 : 0));
2754 it->selective_display_ellipsis_p
2755 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2757 /* Display table to use. */
2758 it->dp = window_display_table (w);
2760 /* Are multibyte characters enabled in current_buffer? */
2761 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2763 /* If visible region is of non-zero length, set IT->region_beg_charpos
2764 and IT->region_end_charpos to the start and end of a visible region
2765 in window IT->w. Set both to -1 to indicate no region. */
2766 markpos = markpos_of_region ();
2767 if (markpos >= 0
2768 /* Maybe highlight only in selected window. */
2769 && (/* Either show region everywhere. */
2770 highlight_nonselected_windows
2771 /* Or show region in the selected window. */
2772 || w == XWINDOW (selected_window)
2773 /* Or show the region if we are in the mini-buffer and W is
2774 the window the mini-buffer refers to. */
2775 || (MINI_WINDOW_P (XWINDOW (selected_window))
2776 && WINDOWP (minibuf_selected_window)
2777 && w == XWINDOW (minibuf_selected_window))))
2779 it->region_beg_charpos = min (PT, markpos);
2780 it->region_end_charpos = max (PT, markpos);
2782 else
2783 it->region_beg_charpos = it->region_end_charpos = -1;
2785 /* Get the position at which the redisplay_end_trigger hook should
2786 be run, if it is to be run at all. */
2787 if (MARKERP (w->redisplay_end_trigger)
2788 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2789 it->redisplay_end_trigger_charpos
2790 = marker_position (w->redisplay_end_trigger);
2791 else if (INTEGERP (w->redisplay_end_trigger))
2792 it->redisplay_end_trigger_charpos =
2793 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2795 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2797 /* Are lines in the display truncated? */
2798 if (base_face_id != DEFAULT_FACE_ID
2799 || it->w->hscroll
2800 || (! WINDOW_FULL_WIDTH_P (it->w)
2801 && ((!NILP (Vtruncate_partial_width_windows)
2802 && !INTEGERP (Vtruncate_partial_width_windows))
2803 || (INTEGERP (Vtruncate_partial_width_windows)
2804 && (WINDOW_TOTAL_COLS (it->w)
2805 < XINT (Vtruncate_partial_width_windows))))))
2806 it->line_wrap = TRUNCATE;
2807 else if (NILP (BVAR (current_buffer, truncate_lines)))
2808 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2809 ? WINDOW_WRAP : WORD_WRAP;
2810 else
2811 it->line_wrap = TRUNCATE;
2813 /* Get dimensions of truncation and continuation glyphs. These are
2814 displayed as fringe bitmaps under X, but we need them for such
2815 frames when the fringes are turned off. But leave the dimensions
2816 zero for tooltip frames, as these glyphs look ugly there and also
2817 sabotage calculations of tooltip dimensions in x-show-tip. */
2818 #ifdef HAVE_WINDOW_SYSTEM
2819 if (!(FRAME_WINDOW_P (it->f)
2820 && FRAMEP (tip_frame)
2821 && it->f == XFRAME (tip_frame)))
2822 #endif
2824 if (it->line_wrap == TRUNCATE)
2826 /* We will need the truncation glyph. */
2827 eassert (it->glyph_row == NULL);
2828 produce_special_glyphs (it, IT_TRUNCATION);
2829 it->truncation_pixel_width = it->pixel_width;
2831 else
2833 /* We will need the continuation glyph. */
2834 eassert (it->glyph_row == NULL);
2835 produce_special_glyphs (it, IT_CONTINUATION);
2836 it->continuation_pixel_width = it->pixel_width;
2840 /* Reset these values to zero because the produce_special_glyphs
2841 above has changed them. */
2842 it->pixel_width = it->ascent = it->descent = 0;
2843 it->phys_ascent = it->phys_descent = 0;
2845 /* Set this after getting the dimensions of truncation and
2846 continuation glyphs, so that we don't produce glyphs when calling
2847 produce_special_glyphs, above. */
2848 it->glyph_row = row;
2849 it->area = TEXT_AREA;
2851 /* Forget any previous info about this row being reversed. */
2852 if (it->glyph_row)
2853 it->glyph_row->reversed_p = 0;
2855 /* Get the dimensions of the display area. The display area
2856 consists of the visible window area plus a horizontally scrolled
2857 part to the left of the window. All x-values are relative to the
2858 start of this total display area. */
2859 if (base_face_id != DEFAULT_FACE_ID)
2861 /* Mode lines, menu bar in terminal frames. */
2862 it->first_visible_x = 0;
2863 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2865 else
2867 it->first_visible_x =
2868 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2869 it->last_visible_x = (it->first_visible_x
2870 + window_box_width (w, TEXT_AREA));
2872 /* If we truncate lines, leave room for the truncation glyph(s) at
2873 the right margin. Otherwise, leave room for the continuation
2874 glyph(s). Done only if the window has no fringes. Since we
2875 don't know at this point whether there will be any R2L lines in
2876 the window, we reserve space for truncation/continuation glyphs
2877 even if only one of the fringes is absent. */
2878 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2879 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2881 if (it->line_wrap == TRUNCATE)
2882 it->last_visible_x -= it->truncation_pixel_width;
2883 else
2884 it->last_visible_x -= it->continuation_pixel_width;
2887 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2888 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2891 /* Leave room for a border glyph. */
2892 if (!FRAME_WINDOW_P (it->f)
2893 && !WINDOW_RIGHTMOST_P (it->w))
2894 it->last_visible_x -= 1;
2896 it->last_visible_y = window_text_bottom_y (w);
2898 /* For mode lines and alike, arrange for the first glyph having a
2899 left box line if the face specifies a box. */
2900 if (base_face_id != DEFAULT_FACE_ID)
2902 struct face *face;
2904 it->face_id = remapped_base_face_id;
2906 /* If we have a boxed mode line, make the first character appear
2907 with a left box line. */
2908 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2909 if (face->box != FACE_NO_BOX)
2910 it->start_of_box_run_p = 1;
2913 /* If a buffer position was specified, set the iterator there,
2914 getting overlays and face properties from that position. */
2915 if (charpos >= BUF_BEG (current_buffer))
2917 it->end_charpos = ZV;
2918 eassert (charpos == BYTE_TO_CHAR (bytepos));
2919 IT_CHARPOS (*it) = charpos;
2920 IT_BYTEPOS (*it) = bytepos;
2922 /* We will rely on `reseat' to set this up properly, via
2923 handle_face_prop. */
2924 it->face_id = it->base_face_id;
2926 it->start = it->current;
2927 /* Do we need to reorder bidirectional text? Not if this is a
2928 unibyte buffer: by definition, none of the single-byte
2929 characters are strong R2L, so no reordering is needed. And
2930 bidi.c doesn't support unibyte buffers anyway. Also, don't
2931 reorder while we are loading loadup.el, since the tables of
2932 character properties needed for reordering are not yet
2933 available. */
2934 it->bidi_p =
2935 NILP (Vpurify_flag)
2936 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2937 && it->multibyte_p;
2939 /* If we are to reorder bidirectional text, init the bidi
2940 iterator. */
2941 if (it->bidi_p)
2943 /* Note the paragraph direction that this buffer wants to
2944 use. */
2945 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2946 Qleft_to_right))
2947 it->paragraph_embedding = L2R;
2948 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2949 Qright_to_left))
2950 it->paragraph_embedding = R2L;
2951 else
2952 it->paragraph_embedding = NEUTRAL_DIR;
2953 bidi_unshelve_cache (NULL, 0);
2954 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2955 &it->bidi_it);
2958 /* Compute faces etc. */
2959 reseat (it, it->current.pos, 1);
2962 CHECK_IT (it);
2966 /* Initialize IT for the display of window W with window start POS. */
2968 void
2969 start_display (struct it *it, struct window *w, struct text_pos pos)
2971 struct glyph_row *row;
2972 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2974 row = w->desired_matrix->rows + first_vpos;
2975 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2976 it->first_vpos = first_vpos;
2978 /* Don't reseat to previous visible line start if current start
2979 position is in a string or image. */
2980 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2982 int start_at_line_beg_p;
2983 int first_y = it->current_y;
2985 /* If window start is not at a line start, skip forward to POS to
2986 get the correct continuation lines width. */
2987 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2988 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2989 if (!start_at_line_beg_p)
2991 int new_x;
2993 reseat_at_previous_visible_line_start (it);
2994 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2996 new_x = it->current_x + it->pixel_width;
2998 /* If lines are continued, this line may end in the middle
2999 of a multi-glyph character (e.g. a control character
3000 displayed as \003, or in the middle of an overlay
3001 string). In this case move_it_to above will not have
3002 taken us to the start of the continuation line but to the
3003 end of the continued line. */
3004 if (it->current_x > 0
3005 && it->line_wrap != TRUNCATE /* Lines are continued. */
3006 && (/* And glyph doesn't fit on the line. */
3007 new_x > it->last_visible_x
3008 /* Or it fits exactly and we're on a window
3009 system frame. */
3010 || (new_x == it->last_visible_x
3011 && FRAME_WINDOW_P (it->f)
3012 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3013 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3014 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3016 if ((it->current.dpvec_index >= 0
3017 || it->current.overlay_string_index >= 0)
3018 /* If we are on a newline from a display vector or
3019 overlay string, then we are already at the end of
3020 a screen line; no need to go to the next line in
3021 that case, as this line is not really continued.
3022 (If we do go to the next line, C-e will not DTRT.) */
3023 && it->c != '\n')
3025 set_iterator_to_next (it, 1);
3026 move_it_in_display_line_to (it, -1, -1, 0);
3029 it->continuation_lines_width += it->current_x;
3031 /* If the character at POS is displayed via a display
3032 vector, move_it_to above stops at the final glyph of
3033 IT->dpvec. To make the caller redisplay that character
3034 again (a.k.a. start at POS), we need to reset the
3035 dpvec_index to the beginning of IT->dpvec. */
3036 else if (it->current.dpvec_index >= 0)
3037 it->current.dpvec_index = 0;
3039 /* We're starting a new display line, not affected by the
3040 height of the continued line, so clear the appropriate
3041 fields in the iterator structure. */
3042 it->max_ascent = it->max_descent = 0;
3043 it->max_phys_ascent = it->max_phys_descent = 0;
3045 it->current_y = first_y;
3046 it->vpos = 0;
3047 it->current_x = it->hpos = 0;
3053 /* Return 1 if POS is a position in ellipses displayed for invisible
3054 text. W is the window we display, for text property lookup. */
3056 static int
3057 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3059 Lisp_Object prop, window;
3060 int ellipses_p = 0;
3061 ptrdiff_t charpos = CHARPOS (pos->pos);
3063 /* If POS specifies a position in a display vector, this might
3064 be for an ellipsis displayed for invisible text. We won't
3065 get the iterator set up for delivering that ellipsis unless
3066 we make sure that it gets aware of the invisible text. */
3067 if (pos->dpvec_index >= 0
3068 && pos->overlay_string_index < 0
3069 && CHARPOS (pos->string_pos) < 0
3070 && charpos > BEGV
3071 && (XSETWINDOW (window, w),
3072 prop = Fget_char_property (make_number (charpos),
3073 Qinvisible, window),
3074 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3076 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3077 window);
3078 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3081 return ellipses_p;
3085 /* Initialize IT for stepping through current_buffer in window W,
3086 starting at position POS that includes overlay string and display
3087 vector/ control character translation position information. Value
3088 is zero if there are overlay strings with newlines at POS. */
3090 static int
3091 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3093 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3094 int i, overlay_strings_with_newlines = 0;
3096 /* If POS specifies a position in a display vector, this might
3097 be for an ellipsis displayed for invisible text. We won't
3098 get the iterator set up for delivering that ellipsis unless
3099 we make sure that it gets aware of the invisible text. */
3100 if (in_ellipses_for_invisible_text_p (pos, w))
3102 --charpos;
3103 bytepos = 0;
3106 /* Keep in mind: the call to reseat in init_iterator skips invisible
3107 text, so we might end up at a position different from POS. This
3108 is only a problem when POS is a row start after a newline and an
3109 overlay starts there with an after-string, and the overlay has an
3110 invisible property. Since we don't skip invisible text in
3111 display_line and elsewhere immediately after consuming the
3112 newline before the row start, such a POS will not be in a string,
3113 but the call to init_iterator below will move us to the
3114 after-string. */
3115 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3117 /* This only scans the current chunk -- it should scan all chunks.
3118 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3119 to 16 in 22.1 to make this a lesser problem. */
3120 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3122 const char *s = SSDATA (it->overlay_strings[i]);
3123 const char *e = s + SBYTES (it->overlay_strings[i]);
3125 while (s < e && *s != '\n')
3126 ++s;
3128 if (s < e)
3130 overlay_strings_with_newlines = 1;
3131 break;
3135 /* If position is within an overlay string, set up IT to the right
3136 overlay string. */
3137 if (pos->overlay_string_index >= 0)
3139 int relative_index;
3141 /* If the first overlay string happens to have a `display'
3142 property for an image, the iterator will be set up for that
3143 image, and we have to undo that setup first before we can
3144 correct the overlay string index. */
3145 if (it->method == GET_FROM_IMAGE)
3146 pop_it (it);
3148 /* We already have the first chunk of overlay strings in
3149 IT->overlay_strings. Load more until the one for
3150 pos->overlay_string_index is in IT->overlay_strings. */
3151 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3153 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3154 it->current.overlay_string_index = 0;
3155 while (n--)
3157 load_overlay_strings (it, 0);
3158 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3162 it->current.overlay_string_index = pos->overlay_string_index;
3163 relative_index = (it->current.overlay_string_index
3164 % OVERLAY_STRING_CHUNK_SIZE);
3165 it->string = it->overlay_strings[relative_index];
3166 eassert (STRINGP (it->string));
3167 it->current.string_pos = pos->string_pos;
3168 it->method = GET_FROM_STRING;
3169 it->end_charpos = SCHARS (it->string);
3170 /* Set up the bidi iterator for this overlay string. */
3171 if (it->bidi_p)
3173 it->bidi_it.string.lstring = it->string;
3174 it->bidi_it.string.s = NULL;
3175 it->bidi_it.string.schars = SCHARS (it->string);
3176 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3177 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3178 it->bidi_it.string.unibyte = !it->multibyte_p;
3179 it->bidi_it.w = it->w;
3180 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3181 FRAME_WINDOW_P (it->f), &it->bidi_it);
3183 /* Synchronize the state of the bidi iterator with
3184 pos->string_pos. For any string position other than
3185 zero, this will be done automagically when we resume
3186 iteration over the string and get_visually_first_element
3187 is called. But if string_pos is zero, and the string is
3188 to be reordered for display, we need to resync manually,
3189 since it could be that the iteration state recorded in
3190 pos ended at string_pos of 0 moving backwards in string. */
3191 if (CHARPOS (pos->string_pos) == 0)
3193 get_visually_first_element (it);
3194 if (IT_STRING_CHARPOS (*it) != 0)
3195 do {
3196 /* Paranoia. */
3197 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3198 bidi_move_to_visually_next (&it->bidi_it);
3199 } while (it->bidi_it.charpos != 0);
3201 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3202 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3206 if (CHARPOS (pos->string_pos) >= 0)
3208 /* Recorded position is not in an overlay string, but in another
3209 string. This can only be a string from a `display' property.
3210 IT should already be filled with that string. */
3211 it->current.string_pos = pos->string_pos;
3212 eassert (STRINGP (it->string));
3213 if (it->bidi_p)
3214 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3215 FRAME_WINDOW_P (it->f), &it->bidi_it);
3218 /* Restore position in display vector translations, control
3219 character translations or ellipses. */
3220 if (pos->dpvec_index >= 0)
3222 if (it->dpvec == NULL)
3223 get_next_display_element (it);
3224 eassert (it->dpvec && it->current.dpvec_index == 0);
3225 it->current.dpvec_index = pos->dpvec_index;
3228 CHECK_IT (it);
3229 return !overlay_strings_with_newlines;
3233 /* Initialize IT for stepping through current_buffer in window W
3234 starting at ROW->start. */
3236 static void
3237 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3239 init_from_display_pos (it, w, &row->start);
3240 it->start = row->start;
3241 it->continuation_lines_width = row->continuation_lines_width;
3242 CHECK_IT (it);
3246 /* Initialize IT for stepping through current_buffer in window W
3247 starting in the line following ROW, i.e. starting at ROW->end.
3248 Value is zero if there are overlay strings with newlines at ROW's
3249 end position. */
3251 static int
3252 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3254 int success = 0;
3256 if (init_from_display_pos (it, w, &row->end))
3258 if (row->continued_p)
3259 it->continuation_lines_width
3260 = row->continuation_lines_width + row->pixel_width;
3261 CHECK_IT (it);
3262 success = 1;
3265 return success;
3271 /***********************************************************************
3272 Text properties
3273 ***********************************************************************/
3275 /* Called when IT reaches IT->stop_charpos. Handle text property and
3276 overlay changes. Set IT->stop_charpos to the next position where
3277 to stop. */
3279 static void
3280 handle_stop (struct it *it)
3282 enum prop_handled handled;
3283 int handle_overlay_change_p;
3284 struct props *p;
3286 it->dpvec = NULL;
3287 it->current.dpvec_index = -1;
3288 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3289 it->ignore_overlay_strings_at_pos_p = 0;
3290 it->ellipsis_p = 0;
3292 /* Use face of preceding text for ellipsis (if invisible) */
3293 if (it->selective_display_ellipsis_p)
3294 it->saved_face_id = it->face_id;
3298 handled = HANDLED_NORMALLY;
3300 /* Call text property handlers. */
3301 for (p = it_props; p->handler; ++p)
3303 handled = p->handler (it);
3305 if (handled == HANDLED_RECOMPUTE_PROPS)
3306 break;
3307 else if (handled == HANDLED_RETURN)
3309 /* We still want to show before and after strings from
3310 overlays even if the actual buffer text is replaced. */
3311 if (!handle_overlay_change_p
3312 || it->sp > 1
3313 /* Don't call get_overlay_strings_1 if we already
3314 have overlay strings loaded, because doing so
3315 will load them again and push the iterator state
3316 onto the stack one more time, which is not
3317 expected by the rest of the code that processes
3318 overlay strings. */
3319 || (it->current.overlay_string_index < 0
3320 ? !get_overlay_strings_1 (it, 0, 0)
3321 : 0))
3323 if (it->ellipsis_p)
3324 setup_for_ellipsis (it, 0);
3325 /* When handling a display spec, we might load an
3326 empty string. In that case, discard it here. We
3327 used to discard it in handle_single_display_spec,
3328 but that causes get_overlay_strings_1, above, to
3329 ignore overlay strings that we must check. */
3330 if (STRINGP (it->string) && !SCHARS (it->string))
3331 pop_it (it);
3332 return;
3334 else if (STRINGP (it->string) && !SCHARS (it->string))
3335 pop_it (it);
3336 else
3338 it->ignore_overlay_strings_at_pos_p = 1;
3339 it->string_from_display_prop_p = 0;
3340 it->from_disp_prop_p = 0;
3341 handle_overlay_change_p = 0;
3343 handled = HANDLED_RECOMPUTE_PROPS;
3344 break;
3346 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3347 handle_overlay_change_p = 0;
3350 if (handled != HANDLED_RECOMPUTE_PROPS)
3352 /* Don't check for overlay strings below when set to deliver
3353 characters from a display vector. */
3354 if (it->method == GET_FROM_DISPLAY_VECTOR)
3355 handle_overlay_change_p = 0;
3357 /* Handle overlay changes.
3358 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3359 if it finds overlays. */
3360 if (handle_overlay_change_p)
3361 handled = handle_overlay_change (it);
3364 if (it->ellipsis_p)
3366 setup_for_ellipsis (it, 0);
3367 break;
3370 while (handled == HANDLED_RECOMPUTE_PROPS);
3372 /* Determine where to stop next. */
3373 if (handled == HANDLED_NORMALLY)
3374 compute_stop_pos (it);
3378 /* Compute IT->stop_charpos from text property and overlay change
3379 information for IT's current position. */
3381 static void
3382 compute_stop_pos (struct it *it)
3384 register INTERVAL iv, next_iv;
3385 Lisp_Object object, limit, position;
3386 ptrdiff_t charpos, bytepos;
3388 if (STRINGP (it->string))
3390 /* Strings are usually short, so don't limit the search for
3391 properties. */
3392 it->stop_charpos = it->end_charpos;
3393 object = it->string;
3394 limit = Qnil;
3395 charpos = IT_STRING_CHARPOS (*it);
3396 bytepos = IT_STRING_BYTEPOS (*it);
3398 else
3400 ptrdiff_t pos;
3402 /* If end_charpos is out of range for some reason, such as a
3403 misbehaving display function, rationalize it (Bug#5984). */
3404 if (it->end_charpos > ZV)
3405 it->end_charpos = ZV;
3406 it->stop_charpos = it->end_charpos;
3408 /* If next overlay change is in front of the current stop pos
3409 (which is IT->end_charpos), stop there. Note: value of
3410 next_overlay_change is point-max if no overlay change
3411 follows. */
3412 charpos = IT_CHARPOS (*it);
3413 bytepos = IT_BYTEPOS (*it);
3414 pos = next_overlay_change (charpos);
3415 if (pos < it->stop_charpos)
3416 it->stop_charpos = pos;
3418 /* If showing the region, we have to stop at the region
3419 start or end because the face might change there. */
3420 if (it->region_beg_charpos > 0)
3422 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3423 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3424 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3425 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3428 /* Set up variables for computing the stop position from text
3429 property changes. */
3430 XSETBUFFER (object, current_buffer);
3431 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3434 /* Get the interval containing IT's position. Value is a null
3435 interval if there isn't such an interval. */
3436 position = make_number (charpos);
3437 iv = validate_interval_range (object, &position, &position, 0);
3438 if (iv)
3440 Lisp_Object values_here[LAST_PROP_IDX];
3441 struct props *p;
3443 /* Get properties here. */
3444 for (p = it_props; p->handler; ++p)
3445 values_here[p->idx] = textget (iv->plist, *p->name);
3447 /* Look for an interval following iv that has different
3448 properties. */
3449 for (next_iv = next_interval (iv);
3450 (next_iv
3451 && (NILP (limit)
3452 || XFASTINT (limit) > next_iv->position));
3453 next_iv = next_interval (next_iv))
3455 for (p = it_props; p->handler; ++p)
3457 Lisp_Object new_value;
3459 new_value = textget (next_iv->plist, *p->name);
3460 if (!EQ (values_here[p->idx], new_value))
3461 break;
3464 if (p->handler)
3465 break;
3468 if (next_iv)
3470 if (INTEGERP (limit)
3471 && next_iv->position >= XFASTINT (limit))
3472 /* No text property change up to limit. */
3473 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3474 else
3475 /* Text properties change in next_iv. */
3476 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3480 if (it->cmp_it.id < 0)
3482 ptrdiff_t stoppos = it->end_charpos;
3484 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3485 stoppos = -1;
3486 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3487 stoppos, it->string);
3490 eassert (STRINGP (it->string)
3491 || (it->stop_charpos >= BEGV
3492 && it->stop_charpos >= IT_CHARPOS (*it)));
3496 /* Return the position of the next overlay change after POS in
3497 current_buffer. Value is point-max if no overlay change
3498 follows. This is like `next-overlay-change' but doesn't use
3499 xmalloc. */
3501 static ptrdiff_t
3502 next_overlay_change (ptrdiff_t pos)
3504 ptrdiff_t i, noverlays;
3505 ptrdiff_t endpos;
3506 Lisp_Object *overlays;
3508 /* Get all overlays at the given position. */
3509 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3511 /* If any of these overlays ends before endpos,
3512 use its ending point instead. */
3513 for (i = 0; i < noverlays; ++i)
3515 Lisp_Object oend;
3516 ptrdiff_t oendpos;
3518 oend = OVERLAY_END (overlays[i]);
3519 oendpos = OVERLAY_POSITION (oend);
3520 endpos = min (endpos, oendpos);
3523 return endpos;
3526 /* How many characters forward to search for a display property or
3527 display string. Searching too far forward makes the bidi display
3528 sluggish, especially in small windows. */
3529 #define MAX_DISP_SCAN 250
3531 /* Return the character position of a display string at or after
3532 position specified by POSITION. If no display string exists at or
3533 after POSITION, return ZV. A display string is either an overlay
3534 with `display' property whose value is a string, or a `display'
3535 text property whose value is a string. STRING is data about the
3536 string to iterate; if STRING->lstring is nil, we are iterating a
3537 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3538 on a GUI frame. DISP_PROP is set to zero if we searched
3539 MAX_DISP_SCAN characters forward without finding any display
3540 strings, non-zero otherwise. It is set to 2 if the display string
3541 uses any kind of `(space ...)' spec that will produce a stretch of
3542 white space in the text area. */
3543 ptrdiff_t
3544 compute_display_string_pos (struct text_pos *position,
3545 struct bidi_string_data *string,
3546 struct window *w,
3547 int frame_window_p, int *disp_prop)
3549 /* OBJECT = nil means current buffer. */
3550 Lisp_Object object, object1;
3551 Lisp_Object pos, spec, limpos;
3552 int string_p = (string && (STRINGP (string->lstring) || string->s));
3553 ptrdiff_t eob = string_p ? string->schars : ZV;
3554 ptrdiff_t begb = string_p ? 0 : BEGV;
3555 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3556 ptrdiff_t lim =
3557 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3558 struct text_pos tpos;
3559 int rv = 0;
3561 if (string && STRINGP (string->lstring))
3562 object1 = object = string->lstring;
3563 else if (w && !string_p)
3565 XSETWINDOW (object, w);
3566 object1 = Qnil;
3568 else
3569 object1 = object = Qnil;
3571 *disp_prop = 1;
3573 if (charpos >= eob
3574 /* We don't support display properties whose values are strings
3575 that have display string properties. */
3576 || string->from_disp_str
3577 /* C strings cannot have display properties. */
3578 || (string->s && !STRINGP (object)))
3580 *disp_prop = 0;
3581 return eob;
3584 /* If the character at CHARPOS is where the display string begins,
3585 return CHARPOS. */
3586 pos = make_number (charpos);
3587 if (STRINGP (object))
3588 bufpos = string->bufpos;
3589 else
3590 bufpos = charpos;
3591 tpos = *position;
3592 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3593 && (charpos <= begb
3594 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3595 object),
3596 spec))
3597 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3598 frame_window_p)))
3600 if (rv == 2)
3601 *disp_prop = 2;
3602 return charpos;
3605 /* Look forward for the first character with a `display' property
3606 that will replace the underlying text when displayed. */
3607 limpos = make_number (lim);
3608 do {
3609 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3610 CHARPOS (tpos) = XFASTINT (pos);
3611 if (CHARPOS (tpos) >= lim)
3613 *disp_prop = 0;
3614 break;
3616 if (STRINGP (object))
3617 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3618 else
3619 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3620 spec = Fget_char_property (pos, Qdisplay, object);
3621 if (!STRINGP (object))
3622 bufpos = CHARPOS (tpos);
3623 } while (NILP (spec)
3624 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3625 bufpos, frame_window_p)));
3626 if (rv == 2)
3627 *disp_prop = 2;
3629 return CHARPOS (tpos);
3632 /* Return the character position of the end of the display string that
3633 started at CHARPOS. If there's no display string at CHARPOS,
3634 return -1. A display string is either an overlay with `display'
3635 property whose value is a string or a `display' text property whose
3636 value is a string. */
3637 ptrdiff_t
3638 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3640 /* OBJECT = nil means current buffer. */
3641 Lisp_Object object =
3642 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3643 Lisp_Object pos = make_number (charpos);
3644 ptrdiff_t eob =
3645 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3647 if (charpos >= eob || (string->s && !STRINGP (object)))
3648 return eob;
3650 /* It could happen that the display property or overlay was removed
3651 since we found it in compute_display_string_pos above. One way
3652 this can happen is if JIT font-lock was called (through
3653 handle_fontified_prop), and jit-lock-functions remove text
3654 properties or overlays from the portion of buffer that includes
3655 CHARPOS. Muse mode is known to do that, for example. In this
3656 case, we return -1 to the caller, to signal that no display
3657 string is actually present at CHARPOS. See bidi_fetch_char for
3658 how this is handled.
3660 An alternative would be to never look for display properties past
3661 it->stop_charpos. But neither compute_display_string_pos nor
3662 bidi_fetch_char that calls it know or care where the next
3663 stop_charpos is. */
3664 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3665 return -1;
3667 /* Look forward for the first character where the `display' property
3668 changes. */
3669 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3671 return XFASTINT (pos);
3676 /***********************************************************************
3677 Fontification
3678 ***********************************************************************/
3680 /* Handle changes in the `fontified' property of the current buffer by
3681 calling hook functions from Qfontification_functions to fontify
3682 regions of text. */
3684 static enum prop_handled
3685 handle_fontified_prop (struct it *it)
3687 Lisp_Object prop, pos;
3688 enum prop_handled handled = HANDLED_NORMALLY;
3690 if (!NILP (Vmemory_full))
3691 return handled;
3693 /* Get the value of the `fontified' property at IT's current buffer
3694 position. (The `fontified' property doesn't have a special
3695 meaning in strings.) If the value is nil, call functions from
3696 Qfontification_functions. */
3697 if (!STRINGP (it->string)
3698 && it->s == NULL
3699 && !NILP (Vfontification_functions)
3700 && !NILP (Vrun_hooks)
3701 && (pos = make_number (IT_CHARPOS (*it)),
3702 prop = Fget_char_property (pos, Qfontified, Qnil),
3703 /* Ignore the special cased nil value always present at EOB since
3704 no amount of fontifying will be able to change it. */
3705 NILP (prop) && IT_CHARPOS (*it) < Z))
3707 ptrdiff_t count = SPECPDL_INDEX ();
3708 Lisp_Object val;
3709 struct buffer *obuf = current_buffer;
3710 int begv = BEGV, zv = ZV;
3711 int old_clip_changed = current_buffer->clip_changed;
3713 val = Vfontification_functions;
3714 specbind (Qfontification_functions, Qnil);
3716 eassert (it->end_charpos == ZV);
3718 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3719 safe_call1 (val, pos);
3720 else
3722 Lisp_Object fns, fn;
3723 struct gcpro gcpro1, gcpro2;
3725 fns = Qnil;
3726 GCPRO2 (val, fns);
3728 for (; CONSP (val); val = XCDR (val))
3730 fn = XCAR (val);
3732 if (EQ (fn, Qt))
3734 /* A value of t indicates this hook has a local
3735 binding; it means to run the global binding too.
3736 In a global value, t should not occur. If it
3737 does, we must ignore it to avoid an endless
3738 loop. */
3739 for (fns = Fdefault_value (Qfontification_functions);
3740 CONSP (fns);
3741 fns = XCDR (fns))
3743 fn = XCAR (fns);
3744 if (!EQ (fn, Qt))
3745 safe_call1 (fn, pos);
3748 else
3749 safe_call1 (fn, pos);
3752 UNGCPRO;
3755 unbind_to (count, Qnil);
3757 /* Fontification functions routinely call `save-restriction'.
3758 Normally, this tags clip_changed, which can confuse redisplay
3759 (see discussion in Bug#6671). Since we don't perform any
3760 special handling of fontification changes in the case where
3761 `save-restriction' isn't called, there's no point doing so in
3762 this case either. So, if the buffer's restrictions are
3763 actually left unchanged, reset clip_changed. */
3764 if (obuf == current_buffer)
3766 if (begv == BEGV && zv == ZV)
3767 current_buffer->clip_changed = old_clip_changed;
3769 /* There isn't much we can reasonably do to protect against
3770 misbehaving fontification, but here's a fig leaf. */
3771 else if (BUFFER_LIVE_P (obuf))
3772 set_buffer_internal_1 (obuf);
3774 /* The fontification code may have added/removed text.
3775 It could do even a lot worse, but let's at least protect against
3776 the most obvious case where only the text past `pos' gets changed',
3777 as is/was done in grep.el where some escapes sequences are turned
3778 into face properties (bug#7876). */
3779 it->end_charpos = ZV;
3781 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3782 something. This avoids an endless loop if they failed to
3783 fontify the text for which reason ever. */
3784 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3785 handled = HANDLED_RECOMPUTE_PROPS;
3788 return handled;
3793 /***********************************************************************
3794 Faces
3795 ***********************************************************************/
3797 /* Set up iterator IT from face properties at its current position.
3798 Called from handle_stop. */
3800 static enum prop_handled
3801 handle_face_prop (struct it *it)
3803 int new_face_id;
3804 ptrdiff_t next_stop;
3806 if (!STRINGP (it->string))
3808 new_face_id
3809 = face_at_buffer_position (it->w,
3810 IT_CHARPOS (*it),
3811 it->region_beg_charpos,
3812 it->region_end_charpos,
3813 &next_stop,
3814 (IT_CHARPOS (*it)
3815 + TEXT_PROP_DISTANCE_LIMIT),
3816 0, it->base_face_id);
3818 /* Is this a start of a run of characters with box face?
3819 Caveat: this can be called for a freshly initialized
3820 iterator; face_id is -1 in this case. We know that the new
3821 face will not change until limit, i.e. if the new face has a
3822 box, all characters up to limit will have one. But, as
3823 usual, we don't know whether limit is really the end. */
3824 if (new_face_id != it->face_id)
3826 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3827 /* If it->face_id is -1, old_face below will be NULL, see
3828 the definition of FACE_FROM_ID. This will happen if this
3829 is the initial call that gets the face. */
3830 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3832 /* If the value of face_id of the iterator is -1, we have to
3833 look in front of IT's position and see whether there is a
3834 face there that's different from new_face_id. */
3835 if (!old_face && IT_CHARPOS (*it) > BEG)
3837 int prev_face_id = face_before_it_pos (it);
3839 old_face = FACE_FROM_ID (it->f, prev_face_id);
3842 /* If the new face has a box, but the old face does not,
3843 this is the start of a run of characters with box face,
3844 i.e. this character has a shadow on the left side. */
3845 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3846 && (old_face == NULL || !old_face->box));
3847 it->face_box_p = new_face->box != FACE_NO_BOX;
3850 else
3852 int base_face_id;
3853 ptrdiff_t bufpos;
3854 int i;
3855 Lisp_Object from_overlay
3856 = (it->current.overlay_string_index >= 0
3857 ? it->string_overlays[it->current.overlay_string_index
3858 % OVERLAY_STRING_CHUNK_SIZE]
3859 : Qnil);
3861 /* See if we got to this string directly or indirectly from
3862 an overlay property. That includes the before-string or
3863 after-string of an overlay, strings in display properties
3864 provided by an overlay, their text properties, etc.
3866 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3867 if (! NILP (from_overlay))
3868 for (i = it->sp - 1; i >= 0; i--)
3870 if (it->stack[i].current.overlay_string_index >= 0)
3871 from_overlay
3872 = it->string_overlays[it->stack[i].current.overlay_string_index
3873 % OVERLAY_STRING_CHUNK_SIZE];
3874 else if (! NILP (it->stack[i].from_overlay))
3875 from_overlay = it->stack[i].from_overlay;
3877 if (!NILP (from_overlay))
3878 break;
3881 if (! NILP (from_overlay))
3883 bufpos = IT_CHARPOS (*it);
3884 /* For a string from an overlay, the base face depends
3885 only on text properties and ignores overlays. */
3886 base_face_id
3887 = face_for_overlay_string (it->w,
3888 IT_CHARPOS (*it),
3889 it->region_beg_charpos,
3890 it->region_end_charpos,
3891 &next_stop,
3892 (IT_CHARPOS (*it)
3893 + TEXT_PROP_DISTANCE_LIMIT),
3895 from_overlay);
3897 else
3899 bufpos = 0;
3901 /* For strings from a `display' property, use the face at
3902 IT's current buffer position as the base face to merge
3903 with, so that overlay strings appear in the same face as
3904 surrounding text, unless they specify their own faces.
3905 For strings from wrap-prefix and line-prefix properties,
3906 use the default face, possibly remapped via
3907 Vface_remapping_alist. */
3908 base_face_id = it->string_from_prefix_prop_p
3909 ? (!NILP (Vface_remapping_alist)
3910 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3911 : DEFAULT_FACE_ID)
3912 : underlying_face_id (it);
3915 new_face_id = face_at_string_position (it->w,
3916 it->string,
3917 IT_STRING_CHARPOS (*it),
3918 bufpos,
3919 it->region_beg_charpos,
3920 it->region_end_charpos,
3921 &next_stop,
3922 base_face_id, 0);
3924 /* Is this a start of a run of characters with box? Caveat:
3925 this can be called for a freshly allocated iterator; face_id
3926 is -1 is this case. We know that the new face will not
3927 change until the next check pos, i.e. if the new face has a
3928 box, all characters up to that position will have a
3929 box. But, as usual, we don't know whether that position
3930 is really the end. */
3931 if (new_face_id != it->face_id)
3933 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3934 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3936 /* If new face has a box but old face hasn't, this is the
3937 start of a run of characters with box, i.e. it has a
3938 shadow on the left side. */
3939 it->start_of_box_run_p
3940 = new_face->box && (old_face == NULL || !old_face->box);
3941 it->face_box_p = new_face->box != FACE_NO_BOX;
3945 it->face_id = new_face_id;
3946 return HANDLED_NORMALLY;
3950 /* Return the ID of the face ``underlying'' IT's current position,
3951 which is in a string. If the iterator is associated with a
3952 buffer, return the face at IT's current buffer position.
3953 Otherwise, use the iterator's base_face_id. */
3955 static int
3956 underlying_face_id (struct it *it)
3958 int face_id = it->base_face_id, i;
3960 eassert (STRINGP (it->string));
3962 for (i = it->sp - 1; i >= 0; --i)
3963 if (NILP (it->stack[i].string))
3964 face_id = it->stack[i].face_id;
3966 return face_id;
3970 /* Compute the face one character before or after the current position
3971 of IT, in the visual order. BEFORE_P non-zero means get the face
3972 in front (to the left in L2R paragraphs, to the right in R2L
3973 paragraphs) of IT's screen position. Value is the ID of the face. */
3975 static int
3976 face_before_or_after_it_pos (struct it *it, int before_p)
3978 int face_id, limit;
3979 ptrdiff_t next_check_charpos;
3980 struct it it_copy;
3981 void *it_copy_data = NULL;
3983 eassert (it->s == NULL);
3985 if (STRINGP (it->string))
3987 ptrdiff_t bufpos, charpos;
3988 int base_face_id;
3990 /* No face change past the end of the string (for the case
3991 we are padding with spaces). No face change before the
3992 string start. */
3993 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3994 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3995 return it->face_id;
3997 if (!it->bidi_p)
3999 /* Set charpos to the position before or after IT's current
4000 position, in the logical order, which in the non-bidi
4001 case is the same as the visual order. */
4002 if (before_p)
4003 charpos = IT_STRING_CHARPOS (*it) - 1;
4004 else if (it->what == IT_COMPOSITION)
4005 /* For composition, we must check the character after the
4006 composition. */
4007 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4008 else
4009 charpos = IT_STRING_CHARPOS (*it) + 1;
4011 else
4013 if (before_p)
4015 /* With bidi iteration, the character before the current
4016 in the visual order cannot be found by simple
4017 iteration, because "reverse" reordering is not
4018 supported. Instead, we need to use the move_it_*
4019 family of functions. */
4020 /* Ignore face changes before the first visible
4021 character on this display line. */
4022 if (it->current_x <= it->first_visible_x)
4023 return it->face_id;
4024 SAVE_IT (it_copy, *it, it_copy_data);
4025 /* Implementation note: Since move_it_in_display_line
4026 works in the iterator geometry, and thinks the first
4027 character is always the leftmost, even in R2L lines,
4028 we don't need to distinguish between the R2L and L2R
4029 cases here. */
4030 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4031 it_copy.current_x - 1, MOVE_TO_X);
4032 charpos = IT_STRING_CHARPOS (it_copy);
4033 RESTORE_IT (it, it, it_copy_data);
4035 else
4037 /* Set charpos to the string position of the character
4038 that comes after IT's current position in the visual
4039 order. */
4040 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4042 it_copy = *it;
4043 while (n--)
4044 bidi_move_to_visually_next (&it_copy.bidi_it);
4046 charpos = it_copy.bidi_it.charpos;
4049 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4051 if (it->current.overlay_string_index >= 0)
4052 bufpos = IT_CHARPOS (*it);
4053 else
4054 bufpos = 0;
4056 base_face_id = underlying_face_id (it);
4058 /* Get the face for ASCII, or unibyte. */
4059 face_id = face_at_string_position (it->w,
4060 it->string,
4061 charpos,
4062 bufpos,
4063 it->region_beg_charpos,
4064 it->region_end_charpos,
4065 &next_check_charpos,
4066 base_face_id, 0);
4068 /* Correct the face for charsets different from ASCII. Do it
4069 for the multibyte case only. The face returned above is
4070 suitable for unibyte text if IT->string is unibyte. */
4071 if (STRING_MULTIBYTE (it->string))
4073 struct text_pos pos1 = string_pos (charpos, it->string);
4074 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4075 int c, len;
4076 struct face *face = FACE_FROM_ID (it->f, face_id);
4078 c = string_char_and_length (p, &len);
4079 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4082 else
4084 struct text_pos pos;
4086 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4087 || (IT_CHARPOS (*it) <= BEGV && before_p))
4088 return it->face_id;
4090 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4091 pos = it->current.pos;
4093 if (!it->bidi_p)
4095 if (before_p)
4096 DEC_TEXT_POS (pos, it->multibyte_p);
4097 else
4099 if (it->what == IT_COMPOSITION)
4101 /* For composition, we must check the position after
4102 the composition. */
4103 pos.charpos += it->cmp_it.nchars;
4104 pos.bytepos += it->len;
4106 else
4107 INC_TEXT_POS (pos, it->multibyte_p);
4110 else
4112 if (before_p)
4114 /* With bidi iteration, the character before the current
4115 in the visual order cannot be found by simple
4116 iteration, because "reverse" reordering is not
4117 supported. Instead, we need to use the move_it_*
4118 family of functions. */
4119 /* Ignore face changes before the first visible
4120 character on this display line. */
4121 if (it->current_x <= it->first_visible_x)
4122 return it->face_id;
4123 SAVE_IT (it_copy, *it, it_copy_data);
4124 /* Implementation note: Since move_it_in_display_line
4125 works in the iterator geometry, and thinks the first
4126 character is always the leftmost, even in R2L lines,
4127 we don't need to distinguish between the R2L and L2R
4128 cases here. */
4129 move_it_in_display_line (&it_copy, ZV,
4130 it_copy.current_x - 1, MOVE_TO_X);
4131 pos = it_copy.current.pos;
4132 RESTORE_IT (it, it, it_copy_data);
4134 else
4136 /* Set charpos to the buffer position of the character
4137 that comes after IT's current position in the visual
4138 order. */
4139 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4141 it_copy = *it;
4142 while (n--)
4143 bidi_move_to_visually_next (&it_copy.bidi_it);
4145 SET_TEXT_POS (pos,
4146 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4149 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4151 /* Determine face for CHARSET_ASCII, or unibyte. */
4152 face_id = face_at_buffer_position (it->w,
4153 CHARPOS (pos),
4154 it->region_beg_charpos,
4155 it->region_end_charpos,
4156 &next_check_charpos,
4157 limit, 0, -1);
4159 /* Correct the face for charsets different from ASCII. Do it
4160 for the multibyte case only. The face returned above is
4161 suitable for unibyte text if current_buffer is unibyte. */
4162 if (it->multibyte_p)
4164 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4165 struct face *face = FACE_FROM_ID (it->f, face_id);
4166 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4170 return face_id;
4175 /***********************************************************************
4176 Invisible text
4177 ***********************************************************************/
4179 /* Set up iterator IT from invisible properties at its current
4180 position. Called from handle_stop. */
4182 static enum prop_handled
4183 handle_invisible_prop (struct it *it)
4185 enum prop_handled handled = HANDLED_NORMALLY;
4186 int invis_p;
4187 Lisp_Object prop;
4189 if (STRINGP (it->string))
4191 Lisp_Object end_charpos, limit, charpos;
4193 /* Get the value of the invisible text property at the
4194 current position. Value will be nil if there is no such
4195 property. */
4196 charpos = make_number (IT_STRING_CHARPOS (*it));
4197 prop = Fget_text_property (charpos, Qinvisible, it->string);
4198 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4200 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4202 /* Record whether we have to display an ellipsis for the
4203 invisible text. */
4204 int display_ellipsis_p = (invis_p == 2);
4205 ptrdiff_t len, endpos;
4207 handled = HANDLED_RECOMPUTE_PROPS;
4209 /* Get the position at which the next visible text can be
4210 found in IT->string, if any. */
4211 endpos = len = SCHARS (it->string);
4212 XSETINT (limit, len);
4215 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4216 it->string, limit);
4217 if (INTEGERP (end_charpos))
4219 endpos = XFASTINT (end_charpos);
4220 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4221 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4222 if (invis_p == 2)
4223 display_ellipsis_p = 1;
4226 while (invis_p && endpos < len);
4228 if (display_ellipsis_p)
4229 it->ellipsis_p = 1;
4231 if (endpos < len)
4233 /* Text at END_CHARPOS is visible. Move IT there. */
4234 struct text_pos old;
4235 ptrdiff_t oldpos;
4237 old = it->current.string_pos;
4238 oldpos = CHARPOS (old);
4239 if (it->bidi_p)
4241 if (it->bidi_it.first_elt
4242 && it->bidi_it.charpos < SCHARS (it->string))
4243 bidi_paragraph_init (it->paragraph_embedding,
4244 &it->bidi_it, 1);
4245 /* Bidi-iterate out of the invisible text. */
4248 bidi_move_to_visually_next (&it->bidi_it);
4250 while (oldpos <= it->bidi_it.charpos
4251 && it->bidi_it.charpos < endpos);
4253 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4254 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4255 if (IT_CHARPOS (*it) >= endpos)
4256 it->prev_stop = endpos;
4258 else
4260 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4261 compute_string_pos (&it->current.string_pos, old, it->string);
4264 else
4266 /* The rest of the string is invisible. If this is an
4267 overlay string, proceed with the next overlay string
4268 or whatever comes and return a character from there. */
4269 if (it->current.overlay_string_index >= 0
4270 && !display_ellipsis_p)
4272 next_overlay_string (it);
4273 /* Don't check for overlay strings when we just
4274 finished processing them. */
4275 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4277 else
4279 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4280 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4285 else
4287 ptrdiff_t newpos, next_stop, start_charpos, tem;
4288 Lisp_Object pos, overlay;
4290 /* First of all, is there invisible text at this position? */
4291 tem = start_charpos = IT_CHARPOS (*it);
4292 pos = make_number (tem);
4293 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4294 &overlay);
4295 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4297 /* If we are on invisible text, skip over it. */
4298 if (invis_p && start_charpos < it->end_charpos)
4300 /* Record whether we have to display an ellipsis for the
4301 invisible text. */
4302 int display_ellipsis_p = invis_p == 2;
4304 handled = HANDLED_RECOMPUTE_PROPS;
4306 /* Loop skipping over invisible text. The loop is left at
4307 ZV or with IT on the first char being visible again. */
4310 /* Try to skip some invisible text. Return value is the
4311 position reached which can be equal to where we start
4312 if there is nothing invisible there. This skips both
4313 over invisible text properties and overlays with
4314 invisible property. */
4315 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4317 /* If we skipped nothing at all we weren't at invisible
4318 text in the first place. If everything to the end of
4319 the buffer was skipped, end the loop. */
4320 if (newpos == tem || newpos >= ZV)
4321 invis_p = 0;
4322 else
4324 /* We skipped some characters but not necessarily
4325 all there are. Check if we ended up on visible
4326 text. Fget_char_property returns the property of
4327 the char before the given position, i.e. if we
4328 get invis_p = 0, this means that the char at
4329 newpos is visible. */
4330 pos = make_number (newpos);
4331 prop = Fget_char_property (pos, Qinvisible, it->window);
4332 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4335 /* If we ended up on invisible text, proceed to
4336 skip starting with next_stop. */
4337 if (invis_p)
4338 tem = next_stop;
4340 /* If there are adjacent invisible texts, don't lose the
4341 second one's ellipsis. */
4342 if (invis_p == 2)
4343 display_ellipsis_p = 1;
4345 while (invis_p);
4347 /* The position newpos is now either ZV or on visible text. */
4348 if (it->bidi_p)
4350 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4351 int on_newline =
4352 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4353 int after_newline =
4354 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4356 /* If the invisible text ends on a newline or on a
4357 character after a newline, we can avoid the costly,
4358 character by character, bidi iteration to NEWPOS, and
4359 instead simply reseat the iterator there. That's
4360 because all bidi reordering information is tossed at
4361 the newline. This is a big win for modes that hide
4362 complete lines, like Outline, Org, etc. */
4363 if (on_newline || after_newline)
4365 struct text_pos tpos;
4366 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4368 SET_TEXT_POS (tpos, newpos, bpos);
4369 reseat_1 (it, tpos, 0);
4370 /* If we reseat on a newline/ZV, we need to prep the
4371 bidi iterator for advancing to the next character
4372 after the newline/EOB, keeping the current paragraph
4373 direction (so that PRODUCE_GLYPHS does TRT wrt
4374 prepending/appending glyphs to a glyph row). */
4375 if (on_newline)
4377 it->bidi_it.first_elt = 0;
4378 it->bidi_it.paragraph_dir = pdir;
4379 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4380 it->bidi_it.nchars = 1;
4381 it->bidi_it.ch_len = 1;
4384 else /* Must use the slow method. */
4386 /* With bidi iteration, the region of invisible text
4387 could start and/or end in the middle of a
4388 non-base embedding level. Therefore, we need to
4389 skip invisible text using the bidi iterator,
4390 starting at IT's current position, until we find
4391 ourselves outside of the invisible text.
4392 Skipping invisible text _after_ bidi iteration
4393 avoids affecting the visual order of the
4394 displayed text when invisible properties are
4395 added or removed. */
4396 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4398 /* If we were `reseat'ed to a new paragraph,
4399 determine the paragraph base direction. We
4400 need to do it now because
4401 next_element_from_buffer may not have a
4402 chance to do it, if we are going to skip any
4403 text at the beginning, which resets the
4404 FIRST_ELT flag. */
4405 bidi_paragraph_init (it->paragraph_embedding,
4406 &it->bidi_it, 1);
4410 bidi_move_to_visually_next (&it->bidi_it);
4412 while (it->stop_charpos <= it->bidi_it.charpos
4413 && it->bidi_it.charpos < newpos);
4414 IT_CHARPOS (*it) = it->bidi_it.charpos;
4415 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4416 /* If we overstepped NEWPOS, record its position in
4417 the iterator, so that we skip invisible text if
4418 later the bidi iteration lands us in the
4419 invisible region again. */
4420 if (IT_CHARPOS (*it) >= newpos)
4421 it->prev_stop = newpos;
4424 else
4426 IT_CHARPOS (*it) = newpos;
4427 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4430 /* If there are before-strings at the start of invisible
4431 text, and the text is invisible because of a text
4432 property, arrange to show before-strings because 20.x did
4433 it that way. (If the text is invisible because of an
4434 overlay property instead of a text property, this is
4435 already handled in the overlay code.) */
4436 if (NILP (overlay)
4437 && get_overlay_strings (it, it->stop_charpos))
4439 handled = HANDLED_RECOMPUTE_PROPS;
4440 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4442 else if (display_ellipsis_p)
4444 /* Make sure that the glyphs of the ellipsis will get
4445 correct `charpos' values. If we would not update
4446 it->position here, the glyphs would belong to the
4447 last visible character _before_ the invisible
4448 text, which confuses `set_cursor_from_row'.
4450 We use the last invisible position instead of the
4451 first because this way the cursor is always drawn on
4452 the first "." of the ellipsis, whenever PT is inside
4453 the invisible text. Otherwise the cursor would be
4454 placed _after_ the ellipsis when the point is after the
4455 first invisible character. */
4456 if (!STRINGP (it->object))
4458 it->position.charpos = newpos - 1;
4459 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4461 it->ellipsis_p = 1;
4462 /* Let the ellipsis display before
4463 considering any properties of the following char.
4464 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4465 handled = HANDLED_RETURN;
4470 return handled;
4474 /* Make iterator IT return `...' next.
4475 Replaces LEN characters from buffer. */
4477 static void
4478 setup_for_ellipsis (struct it *it, int len)
4480 /* Use the display table definition for `...'. Invalid glyphs
4481 will be handled by the method returning elements from dpvec. */
4482 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4484 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4485 it->dpvec = v->contents;
4486 it->dpend = v->contents + v->header.size;
4488 else
4490 /* Default `...'. */
4491 it->dpvec = default_invis_vector;
4492 it->dpend = default_invis_vector + 3;
4495 it->dpvec_char_len = len;
4496 it->current.dpvec_index = 0;
4497 it->dpvec_face_id = -1;
4499 /* Remember the current face id in case glyphs specify faces.
4500 IT's face is restored in set_iterator_to_next.
4501 saved_face_id was set to preceding char's face in handle_stop. */
4502 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4503 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4505 it->method = GET_FROM_DISPLAY_VECTOR;
4506 it->ellipsis_p = 1;
4511 /***********************************************************************
4512 'display' property
4513 ***********************************************************************/
4515 /* Set up iterator IT from `display' property at its current position.
4516 Called from handle_stop.
4517 We return HANDLED_RETURN if some part of the display property
4518 overrides the display of the buffer text itself.
4519 Otherwise we return HANDLED_NORMALLY. */
4521 static enum prop_handled
4522 handle_display_prop (struct it *it)
4524 Lisp_Object propval, object, overlay;
4525 struct text_pos *position;
4526 ptrdiff_t bufpos;
4527 /* Nonzero if some property replaces the display of the text itself. */
4528 int display_replaced_p = 0;
4530 if (STRINGP (it->string))
4532 object = it->string;
4533 position = &it->current.string_pos;
4534 bufpos = CHARPOS (it->current.pos);
4536 else
4538 XSETWINDOW (object, it->w);
4539 position = &it->current.pos;
4540 bufpos = CHARPOS (*position);
4543 /* Reset those iterator values set from display property values. */
4544 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4545 it->space_width = Qnil;
4546 it->font_height = Qnil;
4547 it->voffset = 0;
4549 /* We don't support recursive `display' properties, i.e. string
4550 values that have a string `display' property, that have a string
4551 `display' property etc. */
4552 if (!it->string_from_display_prop_p)
4553 it->area = TEXT_AREA;
4555 propval = get_char_property_and_overlay (make_number (position->charpos),
4556 Qdisplay, object, &overlay);
4557 if (NILP (propval))
4558 return HANDLED_NORMALLY;
4559 /* Now OVERLAY is the overlay that gave us this property, or nil
4560 if it was a text property. */
4562 if (!STRINGP (it->string))
4563 object = it->w->contents;
4565 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4566 position, bufpos,
4567 FRAME_WINDOW_P (it->f));
4569 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4572 /* Subroutine of handle_display_prop. Returns non-zero if the display
4573 specification in SPEC is a replacing specification, i.e. it would
4574 replace the text covered by `display' property with something else,
4575 such as an image or a display string. If SPEC includes any kind or
4576 `(space ...) specification, the value is 2; this is used by
4577 compute_display_string_pos, which see.
4579 See handle_single_display_spec for documentation of arguments.
4580 frame_window_p is non-zero if the window being redisplayed is on a
4581 GUI frame; this argument is used only if IT is NULL, see below.
4583 IT can be NULL, if this is called by the bidi reordering code
4584 through compute_display_string_pos, which see. In that case, this
4585 function only examines SPEC, but does not otherwise "handle" it, in
4586 the sense that it doesn't set up members of IT from the display
4587 spec. */
4588 static int
4589 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4590 Lisp_Object overlay, struct text_pos *position,
4591 ptrdiff_t bufpos, int frame_window_p)
4593 int replacing_p = 0;
4594 int rv;
4596 if (CONSP (spec)
4597 /* Simple specifications. */
4598 && !EQ (XCAR (spec), Qimage)
4599 #ifdef HAVE_XWIDGETS
4600 && !EQ (XCAR (spec), Qxwidget)
4601 #endif
4602 && !EQ (XCAR (spec), Qspace)
4603 && !EQ (XCAR (spec), Qwhen)
4604 && !EQ (XCAR (spec), Qslice)
4605 && !EQ (XCAR (spec), Qspace_width)
4606 && !EQ (XCAR (spec), Qheight)
4607 && !EQ (XCAR (spec), Qraise)
4608 /* Marginal area specifications. */
4609 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4610 && !EQ (XCAR (spec), Qleft_fringe)
4611 && !EQ (XCAR (spec), Qright_fringe)
4612 && !NILP (XCAR (spec)))
4614 for (; CONSP (spec); spec = XCDR (spec))
4616 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4617 overlay, position, bufpos,
4618 replacing_p, frame_window_p)))
4620 replacing_p = rv;
4621 /* If some text in a string is replaced, `position' no
4622 longer points to the position of `object'. */
4623 if (!it || STRINGP (object))
4624 break;
4628 else if (VECTORP (spec))
4630 ptrdiff_t i;
4631 for (i = 0; i < ASIZE (spec); ++i)
4632 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4633 overlay, position, bufpos,
4634 replacing_p, frame_window_p)))
4636 replacing_p = rv;
4637 /* If some text in a string is replaced, `position' no
4638 longer points to the position of `object'. */
4639 if (!it || STRINGP (object))
4640 break;
4643 else
4645 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4646 position, bufpos, 0,
4647 frame_window_p)))
4648 replacing_p = rv;
4651 return replacing_p;
4654 /* Value is the position of the end of the `display' property starting
4655 at START_POS in OBJECT. */
4657 static struct text_pos
4658 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4660 Lisp_Object end;
4661 struct text_pos end_pos;
4663 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4664 Qdisplay, object, Qnil);
4665 CHARPOS (end_pos) = XFASTINT (end);
4666 if (STRINGP (object))
4667 compute_string_pos (&end_pos, start_pos, it->string);
4668 else
4669 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4671 return end_pos;
4675 /* Set up IT from a single `display' property specification SPEC. OBJECT
4676 is the object in which the `display' property was found. *POSITION
4677 is the position in OBJECT at which the `display' property was found.
4678 BUFPOS is the buffer position of OBJECT (different from POSITION if
4679 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4680 previously saw a display specification which already replaced text
4681 display with something else, for example an image; we ignore such
4682 properties after the first one has been processed.
4684 OVERLAY is the overlay this `display' property came from,
4685 or nil if it was a text property.
4687 If SPEC is a `space' or `image' specification, and in some other
4688 cases too, set *POSITION to the position where the `display'
4689 property ends.
4691 If IT is NULL, only examine the property specification in SPEC, but
4692 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4693 is intended to be displayed in a window on a GUI frame.
4695 Value is non-zero if something was found which replaces the display
4696 of buffer or string text. */
4698 static int
4699 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4700 Lisp_Object overlay, struct text_pos *position,
4701 ptrdiff_t bufpos, int display_replaced_p,
4702 int frame_window_p)
4704 Lisp_Object form;
4705 Lisp_Object location, value;
4706 struct text_pos start_pos = *position;
4707 int valid_p;
4709 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4710 If the result is non-nil, use VALUE instead of SPEC. */
4711 form = Qt;
4712 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4714 spec = XCDR (spec);
4715 if (!CONSP (spec))
4716 return 0;
4717 form = XCAR (spec);
4718 spec = XCDR (spec);
4721 if (!NILP (form) && !EQ (form, Qt))
4723 ptrdiff_t count = SPECPDL_INDEX ();
4724 struct gcpro gcpro1;
4726 /* Bind `object' to the object having the `display' property, a
4727 buffer or string. Bind `position' to the position in the
4728 object where the property was found, and `buffer-position'
4729 to the current position in the buffer. */
4731 if (NILP (object))
4732 XSETBUFFER (object, current_buffer);
4733 specbind (Qobject, object);
4734 specbind (Qposition, make_number (CHARPOS (*position)));
4735 specbind (Qbuffer_position, make_number (bufpos));
4736 GCPRO1 (form);
4737 form = safe_eval (form);
4738 UNGCPRO;
4739 unbind_to (count, Qnil);
4742 if (NILP (form))
4743 return 0;
4745 /* Handle `(height HEIGHT)' specifications. */
4746 if (CONSP (spec)
4747 && EQ (XCAR (spec), Qheight)
4748 && CONSP (XCDR (spec)))
4750 if (it)
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4755 it->font_height = XCAR (XCDR (spec));
4756 if (!NILP (it->font_height))
4758 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4759 int new_height = -1;
4761 if (CONSP (it->font_height)
4762 && (EQ (XCAR (it->font_height), Qplus)
4763 || EQ (XCAR (it->font_height), Qminus))
4764 && CONSP (XCDR (it->font_height))
4765 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4767 /* `(+ N)' or `(- N)' where N is an integer. */
4768 int steps = XINT (XCAR (XCDR (it->font_height)));
4769 if (EQ (XCAR (it->font_height), Qplus))
4770 steps = - steps;
4771 it->face_id = smaller_face (it->f, it->face_id, steps);
4773 else if (FUNCTIONP (it->font_height))
4775 /* Call function with current height as argument.
4776 Value is the new height. */
4777 Lisp_Object height;
4778 height = safe_call1 (it->font_height,
4779 face->lface[LFACE_HEIGHT_INDEX]);
4780 if (NUMBERP (height))
4781 new_height = XFLOATINT (height);
4783 else if (NUMBERP (it->font_height))
4785 /* Value is a multiple of the canonical char height. */
4786 struct face *f;
4788 f = FACE_FROM_ID (it->f,
4789 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4790 new_height = (XFLOATINT (it->font_height)
4791 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4793 else
4795 /* Evaluate IT->font_height with `height' bound to the
4796 current specified height to get the new height. */
4797 ptrdiff_t count = SPECPDL_INDEX ();
4799 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4800 value = safe_eval (it->font_height);
4801 unbind_to (count, Qnil);
4803 if (NUMBERP (value))
4804 new_height = XFLOATINT (value);
4807 if (new_height > 0)
4808 it->face_id = face_with_height (it->f, it->face_id, new_height);
4812 return 0;
4815 /* Handle `(space-width WIDTH)'. */
4816 if (CONSP (spec)
4817 && EQ (XCAR (spec), Qspace_width)
4818 && CONSP (XCDR (spec)))
4820 if (it)
4822 if (!FRAME_WINDOW_P (it->f))
4823 return 0;
4825 value = XCAR (XCDR (spec));
4826 if (NUMBERP (value) && XFLOATINT (value) > 0)
4827 it->space_width = value;
4830 return 0;
4833 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4834 if (CONSP (spec)
4835 && EQ (XCAR (spec), Qslice))
4837 Lisp_Object tem;
4839 if (it)
4841 if (!FRAME_WINDOW_P (it->f))
4842 return 0;
4844 if (tem = XCDR (spec), CONSP (tem))
4846 it->slice.x = XCAR (tem);
4847 if (tem = XCDR (tem), CONSP (tem))
4849 it->slice.y = XCAR (tem);
4850 if (tem = XCDR (tem), CONSP (tem))
4852 it->slice.width = XCAR (tem);
4853 if (tem = XCDR (tem), CONSP (tem))
4854 it->slice.height = XCAR (tem);
4860 return 0;
4863 /* Handle `(raise FACTOR)'. */
4864 if (CONSP (spec)
4865 && EQ (XCAR (spec), Qraise)
4866 && CONSP (XCDR (spec)))
4868 if (it)
4870 if (!FRAME_WINDOW_P (it->f))
4871 return 0;
4873 #ifdef HAVE_WINDOW_SYSTEM
4874 value = XCAR (XCDR (spec));
4875 if (NUMBERP (value))
4877 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4878 it->voffset = - (XFLOATINT (value)
4879 * (FONT_HEIGHT (face->font)));
4881 #endif /* HAVE_WINDOW_SYSTEM */
4884 return 0;
4887 /* Don't handle the other kinds of display specifications
4888 inside a string that we got from a `display' property. */
4889 if (it && it->string_from_display_prop_p)
4890 return 0;
4892 /* Characters having this form of property are not displayed, so
4893 we have to find the end of the property. */
4894 if (it)
4896 start_pos = *position;
4897 *position = display_prop_end (it, object, start_pos);
4899 value = Qnil;
4901 /* Stop the scan at that end position--we assume that all
4902 text properties change there. */
4903 if (it)
4904 it->stop_charpos = position->charpos;
4906 /* Handle `(left-fringe BITMAP [FACE])'
4907 and `(right-fringe BITMAP [FACE])'. */
4908 if (CONSP (spec)
4909 && (EQ (XCAR (spec), Qleft_fringe)
4910 || EQ (XCAR (spec), Qright_fringe))
4911 && CONSP (XCDR (spec)))
4913 int fringe_bitmap;
4915 if (it)
4917 if (!FRAME_WINDOW_P (it->f))
4918 /* If we return here, POSITION has been advanced
4919 across the text with this property. */
4921 /* Synchronize the bidi iterator with POSITION. This is
4922 needed because we are not going to push the iterator
4923 on behalf of this display property, so there will be
4924 no pop_it call to do this synchronization for us. */
4925 if (it->bidi_p)
4927 it->position = *position;
4928 iterate_out_of_display_property (it);
4929 *position = it->position;
4931 return 1;
4934 else if (!frame_window_p)
4935 return 1;
4937 #ifdef HAVE_WINDOW_SYSTEM
4938 value = XCAR (XCDR (spec));
4939 if (!SYMBOLP (value)
4940 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4941 /* If we return here, POSITION has been advanced
4942 across the text with this property. */
4944 if (it && it->bidi_p)
4946 it->position = *position;
4947 iterate_out_of_display_property (it);
4948 *position = it->position;
4950 return 1;
4953 if (it)
4955 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4957 if (CONSP (XCDR (XCDR (spec))))
4959 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4960 int face_id2 = lookup_derived_face (it->f, face_name,
4961 FRINGE_FACE_ID, 0);
4962 if (face_id2 >= 0)
4963 face_id = face_id2;
4966 /* Save current settings of IT so that we can restore them
4967 when we are finished with the glyph property value. */
4968 push_it (it, position);
4970 it->area = TEXT_AREA;
4971 it->what = IT_IMAGE;
4972 it->image_id = -1; /* no image */
4973 it->position = start_pos;
4974 it->object = NILP (object) ? it->w->contents : object;
4975 it->method = GET_FROM_IMAGE;
4976 it->from_overlay = Qnil;
4977 it->face_id = face_id;
4978 it->from_disp_prop_p = 1;
4980 /* Say that we haven't consumed the characters with
4981 `display' property yet. The call to pop_it in
4982 set_iterator_to_next will clean this up. */
4983 *position = start_pos;
4985 if (EQ (XCAR (spec), Qleft_fringe))
4987 it->left_user_fringe_bitmap = fringe_bitmap;
4988 it->left_user_fringe_face_id = face_id;
4990 else
4992 it->right_user_fringe_bitmap = fringe_bitmap;
4993 it->right_user_fringe_face_id = face_id;
4996 #endif /* HAVE_WINDOW_SYSTEM */
4997 return 1;
5000 /* Prepare to handle `((margin left-margin) ...)',
5001 `((margin right-margin) ...)' and `((margin nil) ...)'
5002 prefixes for display specifications. */
5003 location = Qunbound;
5004 if (CONSP (spec) && CONSP (XCAR (spec)))
5006 Lisp_Object tem;
5008 value = XCDR (spec);
5009 if (CONSP (value))
5010 value = XCAR (value);
5012 tem = XCAR (spec);
5013 if (EQ (XCAR (tem), Qmargin)
5014 && (tem = XCDR (tem),
5015 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5016 (NILP (tem)
5017 || EQ (tem, Qleft_margin)
5018 || EQ (tem, Qright_margin))))
5019 location = tem;
5022 if (EQ (location, Qunbound))
5024 location = Qnil;
5025 value = spec;
5028 /* After this point, VALUE is the property after any
5029 margin prefix has been stripped. It must be a string,
5030 an image specification, or `(space ...)'.
5032 LOCATION specifies where to display: `left-margin',
5033 `right-margin' or nil. */
5035 valid_p = (STRINGP (value)
5036 #ifdef HAVE_WINDOW_SYSTEM
5037 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5038 && valid_image_p (value))
5039 #endif /* not HAVE_WINDOW_SYSTEM */
5040 || (CONSP (value) && EQ (XCAR (value), Qspace))
5041 #ifdef HAVE_XWIDGETS
5042 || valid_xwidget_spec_p(value)
5043 #endif
5046 if (valid_p && !display_replaced_p)
5048 int retval = 1;
5050 if (!it)
5052 /* Callers need to know whether the display spec is any kind
5053 of `(space ...)' spec that is about to affect text-area
5054 display. */
5055 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5056 retval = 2;
5057 return retval;
5060 /* Save current settings of IT so that we can restore them
5061 when we are finished with the glyph property value. */
5062 push_it (it, position);
5063 it->from_overlay = overlay;
5064 it->from_disp_prop_p = 1;
5066 if (NILP (location))
5067 it->area = TEXT_AREA;
5068 else if (EQ (location, Qleft_margin))
5069 it->area = LEFT_MARGIN_AREA;
5070 else
5071 it->area = RIGHT_MARGIN_AREA;
5073 if (STRINGP (value))
5075 it->string = value;
5076 it->multibyte_p = STRING_MULTIBYTE (it->string);
5077 it->current.overlay_string_index = -1;
5078 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5079 it->end_charpos = it->string_nchars = SCHARS (it->string);
5080 it->method = GET_FROM_STRING;
5081 it->stop_charpos = 0;
5082 it->prev_stop = 0;
5083 it->base_level_stop = 0;
5084 it->string_from_display_prop_p = 1;
5085 /* Say that we haven't consumed the characters with
5086 `display' property yet. The call to pop_it in
5087 set_iterator_to_next will clean this up. */
5088 if (BUFFERP (object))
5089 *position = start_pos;
5091 /* Force paragraph direction to be that of the parent
5092 object. If the parent object's paragraph direction is
5093 not yet determined, default to L2R. */
5094 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5095 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5096 else
5097 it->paragraph_embedding = L2R;
5099 /* Set up the bidi iterator for this display string. */
5100 if (it->bidi_p)
5102 it->bidi_it.string.lstring = it->string;
5103 it->bidi_it.string.s = NULL;
5104 it->bidi_it.string.schars = it->end_charpos;
5105 it->bidi_it.string.bufpos = bufpos;
5106 it->bidi_it.string.from_disp_str = 1;
5107 it->bidi_it.string.unibyte = !it->multibyte_p;
5108 it->bidi_it.w = it->w;
5109 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5112 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5114 it->method = GET_FROM_STRETCH;
5115 it->object = value;
5116 *position = it->position = start_pos;
5117 retval = 1 + (it->area == TEXT_AREA);
5119 #ifdef HAVE_XWIDGETS
5120 else if (valid_xwidget_spec_p(value))
5122 //printf("handle_single_display_spec: im an xwidget!!\n");
5123 it->what = IT_XWIDGET;
5124 it->method = GET_FROM_XWIDGET;
5125 it->position = start_pos;
5126 it->object = NILP (object) ? it->w->contents : object;
5127 *position = start_pos;
5129 it->xwidget = lookup_xwidget(value);
5131 #endif
5132 #ifdef HAVE_WINDOW_SYSTEM
5133 else
5135 it->what = IT_IMAGE;
5136 it->image_id = lookup_image (it->f, value);
5137 it->position = start_pos;
5138 it->object = NILP (object) ? it->w->contents : object;
5139 it->method = GET_FROM_IMAGE;
5141 /* Say that we haven't consumed the characters with
5142 `display' property yet. The call to pop_it in
5143 set_iterator_to_next will clean this up. */
5144 *position = start_pos;
5146 #endif /* HAVE_WINDOW_SYSTEM */
5148 return retval;
5151 /* Invalid property or property not supported. Restore
5152 POSITION to what it was before. */
5153 *position = start_pos;
5154 return 0;
5157 /* Check if PROP is a display property value whose text should be
5158 treated as intangible. OVERLAY is the overlay from which PROP
5159 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5160 specify the buffer position covered by PROP. */
5163 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5164 ptrdiff_t charpos, ptrdiff_t bytepos)
5166 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5167 struct text_pos position;
5169 SET_TEXT_POS (position, charpos, bytepos);
5170 return handle_display_spec (NULL, prop, Qnil, overlay,
5171 &position, charpos, frame_window_p);
5175 /* Return 1 if PROP is a display sub-property value containing STRING.
5177 Implementation note: this and the following function are really
5178 special cases of handle_display_spec and
5179 handle_single_display_spec, and should ideally use the same code.
5180 Until they do, these two pairs must be consistent and must be
5181 modified in sync. */
5183 static int
5184 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5186 if (EQ (string, prop))
5187 return 1;
5189 /* Skip over `when FORM'. */
5190 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5192 prop = XCDR (prop);
5193 if (!CONSP (prop))
5194 return 0;
5195 /* Actually, the condition following `when' should be eval'ed,
5196 like handle_single_display_spec does, and we should return
5197 zero if it evaluates to nil. However, this function is
5198 called only when the buffer was already displayed and some
5199 glyph in the glyph matrix was found to come from a display
5200 string. Therefore, the condition was already evaluated, and
5201 the result was non-nil, otherwise the display string wouldn't
5202 have been displayed and we would have never been called for
5203 this property. Thus, we can skip the evaluation and assume
5204 its result is non-nil. */
5205 prop = XCDR (prop);
5208 if (CONSP (prop))
5209 /* Skip over `margin LOCATION'. */
5210 if (EQ (XCAR (prop), Qmargin))
5212 prop = XCDR (prop);
5213 if (!CONSP (prop))
5214 return 0;
5216 prop = XCDR (prop);
5217 if (!CONSP (prop))
5218 return 0;
5221 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5225 /* Return 1 if STRING appears in the `display' property PROP. */
5227 static int
5228 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5230 if (CONSP (prop)
5231 && !EQ (XCAR (prop), Qwhen)
5232 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5234 /* A list of sub-properties. */
5235 while (CONSP (prop))
5237 if (single_display_spec_string_p (XCAR (prop), string))
5238 return 1;
5239 prop = XCDR (prop);
5242 else if (VECTORP (prop))
5244 /* A vector of sub-properties. */
5245 ptrdiff_t i;
5246 for (i = 0; i < ASIZE (prop); ++i)
5247 if (single_display_spec_string_p (AREF (prop, i), string))
5248 return 1;
5250 else
5251 return single_display_spec_string_p (prop, string);
5253 return 0;
5256 /* Look for STRING in overlays and text properties in the current
5257 buffer, between character positions FROM and TO (excluding TO).
5258 BACK_P non-zero means look back (in this case, TO is supposed to be
5259 less than FROM).
5260 Value is the first character position where STRING was found, or
5261 zero if it wasn't found before hitting TO.
5263 This function may only use code that doesn't eval because it is
5264 called asynchronously from note_mouse_highlight. */
5266 static ptrdiff_t
5267 string_buffer_position_lim (Lisp_Object string,
5268 ptrdiff_t from, ptrdiff_t to, int back_p)
5270 Lisp_Object limit, prop, pos;
5271 int found = 0;
5273 pos = make_number (max (from, BEGV));
5275 if (!back_p) /* looking forward */
5277 limit = make_number (min (to, ZV));
5278 while (!found && !EQ (pos, limit))
5280 prop = Fget_char_property (pos, Qdisplay, Qnil);
5281 if (!NILP (prop) && display_prop_string_p (prop, string))
5282 found = 1;
5283 else
5284 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5285 limit);
5288 else /* looking back */
5290 limit = make_number (max (to, BEGV));
5291 while (!found && !EQ (pos, limit))
5293 prop = Fget_char_property (pos, Qdisplay, Qnil);
5294 if (!NILP (prop) && display_prop_string_p (prop, string))
5295 found = 1;
5296 else
5297 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5298 limit);
5302 return found ? XINT (pos) : 0;
5305 /* Determine which buffer position in current buffer STRING comes from.
5306 AROUND_CHARPOS is an approximate position where it could come from.
5307 Value is the buffer position or 0 if it couldn't be determined.
5309 This function is necessary because we don't record buffer positions
5310 in glyphs generated from strings (to keep struct glyph small).
5311 This function may only use code that doesn't eval because it is
5312 called asynchronously from note_mouse_highlight. */
5314 static ptrdiff_t
5315 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5317 const int MAX_DISTANCE = 1000;
5318 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5319 around_charpos + MAX_DISTANCE,
5322 if (!found)
5323 found = string_buffer_position_lim (string, around_charpos,
5324 around_charpos - MAX_DISTANCE, 1);
5325 return found;
5330 /***********************************************************************
5331 `composition' property
5332 ***********************************************************************/
5334 /* Set up iterator IT from `composition' property at its current
5335 position. Called from handle_stop. */
5337 static enum prop_handled
5338 handle_composition_prop (struct it *it)
5340 Lisp_Object prop, string;
5341 ptrdiff_t pos, pos_byte, start, end;
5343 if (STRINGP (it->string))
5345 unsigned char *s;
5347 pos = IT_STRING_CHARPOS (*it);
5348 pos_byte = IT_STRING_BYTEPOS (*it);
5349 string = it->string;
5350 s = SDATA (string) + pos_byte;
5351 it->c = STRING_CHAR (s);
5353 else
5355 pos = IT_CHARPOS (*it);
5356 pos_byte = IT_BYTEPOS (*it);
5357 string = Qnil;
5358 it->c = FETCH_CHAR (pos_byte);
5361 /* If there's a valid composition and point is not inside of the
5362 composition (in the case that the composition is from the current
5363 buffer), draw a glyph composed from the composition components. */
5364 if (find_composition (pos, -1, &start, &end, &prop, string)
5365 && composition_valid_p (start, end, prop)
5366 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5368 if (start < pos)
5369 /* As we can't handle this situation (perhaps font-lock added
5370 a new composition), we just return here hoping that next
5371 redisplay will detect this composition much earlier. */
5372 return HANDLED_NORMALLY;
5373 if (start != pos)
5375 if (STRINGP (it->string))
5376 pos_byte = string_char_to_byte (it->string, start);
5377 else
5378 pos_byte = CHAR_TO_BYTE (start);
5380 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5381 prop, string);
5383 if (it->cmp_it.id >= 0)
5385 it->cmp_it.ch = -1;
5386 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5387 it->cmp_it.nglyphs = -1;
5391 return HANDLED_NORMALLY;
5396 /***********************************************************************
5397 Overlay strings
5398 ***********************************************************************/
5400 /* The following structure is used to record overlay strings for
5401 later sorting in load_overlay_strings. */
5403 struct overlay_entry
5405 Lisp_Object overlay;
5406 Lisp_Object string;
5407 EMACS_INT priority;
5408 int after_string_p;
5412 /* Set up iterator IT from overlay strings at its current position.
5413 Called from handle_stop. */
5415 static enum prop_handled
5416 handle_overlay_change (struct it *it)
5418 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5419 return HANDLED_RECOMPUTE_PROPS;
5420 else
5421 return HANDLED_NORMALLY;
5425 /* Set up the next overlay string for delivery by IT, if there is an
5426 overlay string to deliver. Called by set_iterator_to_next when the
5427 end of the current overlay string is reached. If there are more
5428 overlay strings to display, IT->string and
5429 IT->current.overlay_string_index are set appropriately here.
5430 Otherwise IT->string is set to nil. */
5432 static void
5433 next_overlay_string (struct it *it)
5435 ++it->current.overlay_string_index;
5436 if (it->current.overlay_string_index == it->n_overlay_strings)
5438 /* No more overlay strings. Restore IT's settings to what
5439 they were before overlay strings were processed, and
5440 continue to deliver from current_buffer. */
5442 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5443 pop_it (it);
5444 eassert (it->sp > 0
5445 || (NILP (it->string)
5446 && it->method == GET_FROM_BUFFER
5447 && it->stop_charpos >= BEGV
5448 && it->stop_charpos <= it->end_charpos));
5449 it->current.overlay_string_index = -1;
5450 it->n_overlay_strings = 0;
5451 it->overlay_strings_charpos = -1;
5452 /* If there's an empty display string on the stack, pop the
5453 stack, to resync the bidi iterator with IT's position. Such
5454 empty strings are pushed onto the stack in
5455 get_overlay_strings_1. */
5456 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5457 pop_it (it);
5459 /* If we're at the end of the buffer, record that we have
5460 processed the overlay strings there already, so that
5461 next_element_from_buffer doesn't try it again. */
5462 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5463 it->overlay_strings_at_end_processed_p = 1;
5465 else
5467 /* There are more overlay strings to process. If
5468 IT->current.overlay_string_index has advanced to a position
5469 where we must load IT->overlay_strings with more strings, do
5470 it. We must load at the IT->overlay_strings_charpos where
5471 IT->n_overlay_strings was originally computed; when invisible
5472 text is present, this might not be IT_CHARPOS (Bug#7016). */
5473 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5475 if (it->current.overlay_string_index && i == 0)
5476 load_overlay_strings (it, it->overlay_strings_charpos);
5478 /* Initialize IT to deliver display elements from the overlay
5479 string. */
5480 it->string = it->overlay_strings[i];
5481 it->multibyte_p = STRING_MULTIBYTE (it->string);
5482 SET_TEXT_POS (it->current.string_pos, 0, 0);
5483 it->method = GET_FROM_STRING;
5484 it->stop_charpos = 0;
5485 it->end_charpos = SCHARS (it->string);
5486 if (it->cmp_it.stop_pos >= 0)
5487 it->cmp_it.stop_pos = 0;
5488 it->prev_stop = 0;
5489 it->base_level_stop = 0;
5491 /* Set up the bidi iterator for this overlay string. */
5492 if (it->bidi_p)
5494 it->bidi_it.string.lstring = it->string;
5495 it->bidi_it.string.s = NULL;
5496 it->bidi_it.string.schars = SCHARS (it->string);
5497 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5498 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5499 it->bidi_it.string.unibyte = !it->multibyte_p;
5500 it->bidi_it.w = it->w;
5501 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5505 CHECK_IT (it);
5509 /* Compare two overlay_entry structures E1 and E2. Used as a
5510 comparison function for qsort in load_overlay_strings. Overlay
5511 strings for the same position are sorted so that
5513 1. All after-strings come in front of before-strings, except
5514 when they come from the same overlay.
5516 2. Within after-strings, strings are sorted so that overlay strings
5517 from overlays with higher priorities come first.
5519 2. Within before-strings, strings are sorted so that overlay
5520 strings from overlays with higher priorities come last.
5522 Value is analogous to strcmp. */
5525 static int
5526 compare_overlay_entries (const void *e1, const void *e2)
5528 struct overlay_entry const *entry1 = e1;
5529 struct overlay_entry const *entry2 = e2;
5530 int result;
5532 if (entry1->after_string_p != entry2->after_string_p)
5534 /* Let after-strings appear in front of before-strings if
5535 they come from different overlays. */
5536 if (EQ (entry1->overlay, entry2->overlay))
5537 result = entry1->after_string_p ? 1 : -1;
5538 else
5539 result = entry1->after_string_p ? -1 : 1;
5541 else if (entry1->priority != entry2->priority)
5543 if (entry1->after_string_p)
5544 /* After-strings sorted in order of decreasing priority. */
5545 result = entry2->priority < entry1->priority ? -1 : 1;
5546 else
5547 /* Before-strings sorted in order of increasing priority. */
5548 result = entry1->priority < entry2->priority ? -1 : 1;
5550 else
5551 result = 0;
5553 return result;
5557 /* Load the vector IT->overlay_strings with overlay strings from IT's
5558 current buffer position, or from CHARPOS if that is > 0. Set
5559 IT->n_overlays to the total number of overlay strings found.
5561 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5562 a time. On entry into load_overlay_strings,
5563 IT->current.overlay_string_index gives the number of overlay
5564 strings that have already been loaded by previous calls to this
5565 function.
5567 IT->add_overlay_start contains an additional overlay start
5568 position to consider for taking overlay strings from, if non-zero.
5569 This position comes into play when the overlay has an `invisible'
5570 property, and both before and after-strings. When we've skipped to
5571 the end of the overlay, because of its `invisible' property, we
5572 nevertheless want its before-string to appear.
5573 IT->add_overlay_start will contain the overlay start position
5574 in this case.
5576 Overlay strings are sorted so that after-string strings come in
5577 front of before-string strings. Within before and after-strings,
5578 strings are sorted by overlay priority. See also function
5579 compare_overlay_entries. */
5581 static void
5582 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5584 Lisp_Object overlay, window, str, invisible;
5585 struct Lisp_Overlay *ov;
5586 ptrdiff_t start, end;
5587 ptrdiff_t size = 20;
5588 ptrdiff_t n = 0, i, j;
5589 int invis_p;
5590 struct overlay_entry *entries = alloca (size * sizeof *entries);
5591 USE_SAFE_ALLOCA;
5593 if (charpos <= 0)
5594 charpos = IT_CHARPOS (*it);
5596 /* Append the overlay string STRING of overlay OVERLAY to vector
5597 `entries' which has size `size' and currently contains `n'
5598 elements. AFTER_P non-zero means STRING is an after-string of
5599 OVERLAY. */
5600 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5601 do \
5603 Lisp_Object priority; \
5605 if (n == size) \
5607 struct overlay_entry *old = entries; \
5608 SAFE_NALLOCA (entries, 2, size); \
5609 memcpy (entries, old, size * sizeof *entries); \
5610 size *= 2; \
5613 entries[n].string = (STRING); \
5614 entries[n].overlay = (OVERLAY); \
5615 priority = Foverlay_get ((OVERLAY), Qpriority); \
5616 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5617 entries[n].after_string_p = (AFTER_P); \
5618 ++n; \
5620 while (0)
5622 /* Process overlay before the overlay center. */
5623 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5625 XSETMISC (overlay, ov);
5626 eassert (OVERLAYP (overlay));
5627 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5628 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5630 if (end < charpos)
5631 break;
5633 /* Skip this overlay if it doesn't start or end at IT's current
5634 position. */
5635 if (end != charpos && start != charpos)
5636 continue;
5638 /* Skip this overlay if it doesn't apply to IT->w. */
5639 window = Foverlay_get (overlay, Qwindow);
5640 if (WINDOWP (window) && XWINDOW (window) != it->w)
5641 continue;
5643 /* If the text ``under'' the overlay is invisible, both before-
5644 and after-strings from this overlay are visible; start and
5645 end position are indistinguishable. */
5646 invisible = Foverlay_get (overlay, Qinvisible);
5647 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5649 /* If overlay has a non-empty before-string, record it. */
5650 if ((start == charpos || (end == charpos && invis_p))
5651 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5652 && SCHARS (str))
5653 RECORD_OVERLAY_STRING (overlay, str, 0);
5655 /* If overlay has a non-empty after-string, record it. */
5656 if ((end == charpos || (start == charpos && invis_p))
5657 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5658 && SCHARS (str))
5659 RECORD_OVERLAY_STRING (overlay, str, 1);
5662 /* Process overlays after the overlay center. */
5663 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5665 XSETMISC (overlay, ov);
5666 eassert (OVERLAYP (overlay));
5667 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5668 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5670 if (start > charpos)
5671 break;
5673 /* Skip this overlay if it doesn't start or end at IT's current
5674 position. */
5675 if (end != charpos && start != charpos)
5676 continue;
5678 /* Skip this overlay if it doesn't apply to IT->w. */
5679 window = Foverlay_get (overlay, Qwindow);
5680 if (WINDOWP (window) && XWINDOW (window) != it->w)
5681 continue;
5683 /* If the text ``under'' the overlay is invisible, it has a zero
5684 dimension, and both before- and after-strings apply. */
5685 invisible = Foverlay_get (overlay, Qinvisible);
5686 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5688 /* If overlay has a non-empty before-string, record it. */
5689 if ((start == charpos || (end == charpos && invis_p))
5690 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5691 && SCHARS (str))
5692 RECORD_OVERLAY_STRING (overlay, str, 0);
5694 /* If overlay has a non-empty after-string, record it. */
5695 if ((end == charpos || (start == charpos && invis_p))
5696 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5697 && SCHARS (str))
5698 RECORD_OVERLAY_STRING (overlay, str, 1);
5701 #undef RECORD_OVERLAY_STRING
5703 /* Sort entries. */
5704 if (n > 1)
5705 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5707 /* Record number of overlay strings, and where we computed it. */
5708 it->n_overlay_strings = n;
5709 it->overlay_strings_charpos = charpos;
5711 /* IT->current.overlay_string_index is the number of overlay strings
5712 that have already been consumed by IT. Copy some of the
5713 remaining overlay strings to IT->overlay_strings. */
5714 i = 0;
5715 j = it->current.overlay_string_index;
5716 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5718 it->overlay_strings[i] = entries[j].string;
5719 it->string_overlays[i++] = entries[j++].overlay;
5722 CHECK_IT (it);
5723 SAFE_FREE ();
5727 /* Get the first chunk of overlay strings at IT's current buffer
5728 position, or at CHARPOS if that is > 0. Value is non-zero if at
5729 least one overlay string was found. */
5731 static int
5732 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5734 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5735 process. This fills IT->overlay_strings with strings, and sets
5736 IT->n_overlay_strings to the total number of strings to process.
5737 IT->pos.overlay_string_index has to be set temporarily to zero
5738 because load_overlay_strings needs this; it must be set to -1
5739 when no overlay strings are found because a zero value would
5740 indicate a position in the first overlay string. */
5741 it->current.overlay_string_index = 0;
5742 load_overlay_strings (it, charpos);
5744 /* If we found overlay strings, set up IT to deliver display
5745 elements from the first one. Otherwise set up IT to deliver
5746 from current_buffer. */
5747 if (it->n_overlay_strings)
5749 /* Make sure we know settings in current_buffer, so that we can
5750 restore meaningful values when we're done with the overlay
5751 strings. */
5752 if (compute_stop_p)
5753 compute_stop_pos (it);
5754 eassert (it->face_id >= 0);
5756 /* Save IT's settings. They are restored after all overlay
5757 strings have been processed. */
5758 eassert (!compute_stop_p || it->sp == 0);
5760 /* When called from handle_stop, there might be an empty display
5761 string loaded. In that case, don't bother saving it. But
5762 don't use this optimization with the bidi iterator, since we
5763 need the corresponding pop_it call to resync the bidi
5764 iterator's position with IT's position, after we are done
5765 with the overlay strings. (The corresponding call to pop_it
5766 in case of an empty display string is in
5767 next_overlay_string.) */
5768 if (!(!it->bidi_p
5769 && STRINGP (it->string) && !SCHARS (it->string)))
5770 push_it (it, NULL);
5772 /* Set up IT to deliver display elements from the first overlay
5773 string. */
5774 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5775 it->string = it->overlay_strings[0];
5776 it->from_overlay = Qnil;
5777 it->stop_charpos = 0;
5778 eassert (STRINGP (it->string));
5779 it->end_charpos = SCHARS (it->string);
5780 it->prev_stop = 0;
5781 it->base_level_stop = 0;
5782 it->multibyte_p = STRING_MULTIBYTE (it->string);
5783 it->method = GET_FROM_STRING;
5784 it->from_disp_prop_p = 0;
5786 /* Force paragraph direction to be that of the parent
5787 buffer. */
5788 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5789 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5790 else
5791 it->paragraph_embedding = L2R;
5793 /* Set up the bidi iterator for this overlay string. */
5794 if (it->bidi_p)
5796 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5798 it->bidi_it.string.lstring = it->string;
5799 it->bidi_it.string.s = NULL;
5800 it->bidi_it.string.schars = SCHARS (it->string);
5801 it->bidi_it.string.bufpos = pos;
5802 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5803 it->bidi_it.string.unibyte = !it->multibyte_p;
5804 it->bidi_it.w = it->w;
5805 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5807 return 1;
5810 it->current.overlay_string_index = -1;
5811 return 0;
5814 static int
5815 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5817 it->string = Qnil;
5818 it->method = GET_FROM_BUFFER;
5820 (void) get_overlay_strings_1 (it, charpos, 1);
5822 CHECK_IT (it);
5824 /* Value is non-zero if we found at least one overlay string. */
5825 return STRINGP (it->string);
5830 /***********************************************************************
5831 Saving and restoring state
5832 ***********************************************************************/
5834 /* Save current settings of IT on IT->stack. Called, for example,
5835 before setting up IT for an overlay string, to be able to restore
5836 IT's settings to what they were after the overlay string has been
5837 processed. If POSITION is non-NULL, it is the position to save on
5838 the stack instead of IT->position. */
5840 static void
5841 push_it (struct it *it, struct text_pos *position)
5843 struct iterator_stack_entry *p;
5845 eassert (it->sp < IT_STACK_SIZE);
5846 p = it->stack + it->sp;
5848 p->stop_charpos = it->stop_charpos;
5849 p->prev_stop = it->prev_stop;
5850 p->base_level_stop = it->base_level_stop;
5851 p->cmp_it = it->cmp_it;
5852 eassert (it->face_id >= 0);
5853 p->face_id = it->face_id;
5854 p->string = it->string;
5855 p->method = it->method;
5856 p->from_overlay = it->from_overlay;
5857 switch (p->method)
5859 case GET_FROM_IMAGE:
5860 p->u.image.object = it->object;
5861 p->u.image.image_id = it->image_id;
5862 p->u.image.slice = it->slice;
5863 break;
5864 case GET_FROM_STRETCH:
5865 p->u.stretch.object = it->object;
5866 break;
5867 #ifdef HAVE_XWIDGETS
5868 case GET_FROM_XWIDGET:
5869 p->u.xwidget.object = it->object;
5870 break;
5871 #endif
5873 p->position = position ? *position : it->position;
5874 p->current = it->current;
5875 p->end_charpos = it->end_charpos;
5876 p->string_nchars = it->string_nchars;
5877 p->area = it->area;
5878 p->multibyte_p = it->multibyte_p;
5879 p->avoid_cursor_p = it->avoid_cursor_p;
5880 p->space_width = it->space_width;
5881 p->font_height = it->font_height;
5882 p->voffset = it->voffset;
5883 p->string_from_display_prop_p = it->string_from_display_prop_p;
5884 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5885 p->display_ellipsis_p = 0;
5886 p->line_wrap = it->line_wrap;
5887 p->bidi_p = it->bidi_p;
5888 p->paragraph_embedding = it->paragraph_embedding;
5889 p->from_disp_prop_p = it->from_disp_prop_p;
5890 ++it->sp;
5892 /* Save the state of the bidi iterator as well. */
5893 if (it->bidi_p)
5894 bidi_push_it (&it->bidi_it);
5897 static void
5898 iterate_out_of_display_property (struct it *it)
5900 int buffer_p = !STRINGP (it->string);
5901 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5902 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5904 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5906 /* Maybe initialize paragraph direction. If we are at the beginning
5907 of a new paragraph, next_element_from_buffer may not have a
5908 chance to do that. */
5909 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5910 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5911 /* prev_stop can be zero, so check against BEGV as well. */
5912 while (it->bidi_it.charpos >= bob
5913 && it->prev_stop <= it->bidi_it.charpos
5914 && it->bidi_it.charpos < CHARPOS (it->position)
5915 && it->bidi_it.charpos < eob)
5916 bidi_move_to_visually_next (&it->bidi_it);
5917 /* Record the stop_pos we just crossed, for when we cross it
5918 back, maybe. */
5919 if (it->bidi_it.charpos > CHARPOS (it->position))
5920 it->prev_stop = CHARPOS (it->position);
5921 /* If we ended up not where pop_it put us, resync IT's
5922 positional members with the bidi iterator. */
5923 if (it->bidi_it.charpos != CHARPOS (it->position))
5924 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5925 if (buffer_p)
5926 it->current.pos = it->position;
5927 else
5928 it->current.string_pos = it->position;
5931 /* Restore IT's settings from IT->stack. Called, for example, when no
5932 more overlay strings must be processed, and we return to delivering
5933 display elements from a buffer, or when the end of a string from a
5934 `display' property is reached and we return to delivering display
5935 elements from an overlay string, or from a buffer. */
5937 static void
5938 pop_it (struct it *it)
5940 struct iterator_stack_entry *p;
5941 int from_display_prop = it->from_disp_prop_p;
5943 eassert (it->sp > 0);
5944 --it->sp;
5945 p = it->stack + it->sp;
5946 it->stop_charpos = p->stop_charpos;
5947 it->prev_stop = p->prev_stop;
5948 it->base_level_stop = p->base_level_stop;
5949 it->cmp_it = p->cmp_it;
5950 it->face_id = p->face_id;
5951 it->current = p->current;
5952 it->position = p->position;
5953 it->string = p->string;
5954 it->from_overlay = p->from_overlay;
5955 if (NILP (it->string))
5956 SET_TEXT_POS (it->current.string_pos, -1, -1);
5957 it->method = p->method;
5958 switch (it->method)
5960 case GET_FROM_IMAGE:
5961 it->image_id = p->u.image.image_id;
5962 it->object = p->u.image.object;
5963 it->slice = p->u.image.slice;
5964 break;
5965 #ifdef HAVE_XWIDGETS
5966 case GET_FROM_XWIDGET:
5967 it->object = p->u.xwidget.object;
5968 break;
5969 #endif
5970 case GET_FROM_STRETCH:
5971 it->object = p->u.stretch.object;
5972 break;
5973 case GET_FROM_BUFFER:
5974 it->object = it->w->contents;
5975 break;
5976 case GET_FROM_STRING:
5977 it->object = it->string;
5978 break;
5979 case GET_FROM_DISPLAY_VECTOR:
5980 if (it->s)
5981 it->method = GET_FROM_C_STRING;
5982 else if (STRINGP (it->string))
5983 it->method = GET_FROM_STRING;
5984 else
5986 it->method = GET_FROM_BUFFER;
5987 it->object = it->w->contents;
5990 it->end_charpos = p->end_charpos;
5991 it->string_nchars = p->string_nchars;
5992 it->area = p->area;
5993 it->multibyte_p = p->multibyte_p;
5994 it->avoid_cursor_p = p->avoid_cursor_p;
5995 it->space_width = p->space_width;
5996 it->font_height = p->font_height;
5997 it->voffset = p->voffset;
5998 it->string_from_display_prop_p = p->string_from_display_prop_p;
5999 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6000 it->line_wrap = p->line_wrap;
6001 it->bidi_p = p->bidi_p;
6002 it->paragraph_embedding = p->paragraph_embedding;
6003 it->from_disp_prop_p = p->from_disp_prop_p;
6004 if (it->bidi_p)
6006 bidi_pop_it (&it->bidi_it);
6007 /* Bidi-iterate until we get out of the portion of text, if any,
6008 covered by a `display' text property or by an overlay with
6009 `display' property. (We cannot just jump there, because the
6010 internal coherency of the bidi iterator state can not be
6011 preserved across such jumps.) We also must determine the
6012 paragraph base direction if the overlay we just processed is
6013 at the beginning of a new paragraph. */
6014 if (from_display_prop
6015 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6016 iterate_out_of_display_property (it);
6018 eassert ((BUFFERP (it->object)
6019 && IT_CHARPOS (*it) == it->bidi_it.charpos
6020 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6021 || (STRINGP (it->object)
6022 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6023 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6024 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6030 /***********************************************************************
6031 Moving over lines
6032 ***********************************************************************/
6034 /* Set IT's current position to the previous line start. */
6036 static void
6037 back_to_previous_line_start (struct it *it)
6039 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6041 DEC_BOTH (cp, bp);
6042 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6046 /* Move IT to the next line start.
6048 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6049 we skipped over part of the text (as opposed to moving the iterator
6050 continuously over the text). Otherwise, don't change the value
6051 of *SKIPPED_P.
6053 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6054 iterator on the newline, if it was found.
6056 Newlines may come from buffer text, overlay strings, or strings
6057 displayed via the `display' property. That's the reason we can't
6058 simply use find_newline_no_quit.
6060 Note that this function may not skip over invisible text that is so
6061 because of text properties and immediately follows a newline. If
6062 it would, function reseat_at_next_visible_line_start, when called
6063 from set_iterator_to_next, would effectively make invisible
6064 characters following a newline part of the wrong glyph row, which
6065 leads to wrong cursor motion. */
6067 static int
6068 forward_to_next_line_start (struct it *it, int *skipped_p,
6069 struct bidi_it *bidi_it_prev)
6071 ptrdiff_t old_selective;
6072 int newline_found_p, n;
6073 const int MAX_NEWLINE_DISTANCE = 500;
6075 /* If already on a newline, just consume it to avoid unintended
6076 skipping over invisible text below. */
6077 if (it->what == IT_CHARACTER
6078 && it->c == '\n'
6079 && CHARPOS (it->position) == IT_CHARPOS (*it))
6081 if (it->bidi_p && bidi_it_prev)
6082 *bidi_it_prev = it->bidi_it;
6083 set_iterator_to_next (it, 0);
6084 it->c = 0;
6085 return 1;
6088 /* Don't handle selective display in the following. It's (a)
6089 unnecessary because it's done by the caller, and (b) leads to an
6090 infinite recursion because next_element_from_ellipsis indirectly
6091 calls this function. */
6092 old_selective = it->selective;
6093 it->selective = 0;
6095 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6096 from buffer text. */
6097 for (n = newline_found_p = 0;
6098 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6099 n += STRINGP (it->string) ? 0 : 1)
6101 if (!get_next_display_element (it))
6102 return 0;
6103 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6104 if (newline_found_p && it->bidi_p && bidi_it_prev)
6105 *bidi_it_prev = it->bidi_it;
6106 set_iterator_to_next (it, 0);
6109 /* If we didn't find a newline near enough, see if we can use a
6110 short-cut. */
6111 if (!newline_found_p)
6113 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6114 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6115 1, &bytepos);
6116 Lisp_Object pos;
6118 eassert (!STRINGP (it->string));
6120 /* If there isn't any `display' property in sight, and no
6121 overlays, we can just use the position of the newline in
6122 buffer text. */
6123 if (it->stop_charpos >= limit
6124 || ((pos = Fnext_single_property_change (make_number (start),
6125 Qdisplay, Qnil,
6126 make_number (limit)),
6127 NILP (pos))
6128 && next_overlay_change (start) == ZV))
6130 if (!it->bidi_p)
6132 IT_CHARPOS (*it) = limit;
6133 IT_BYTEPOS (*it) = bytepos;
6135 else
6137 struct bidi_it bprev;
6139 /* Help bidi.c avoid expensive searches for display
6140 properties and overlays, by telling it that there are
6141 none up to `limit'. */
6142 if (it->bidi_it.disp_pos < limit)
6144 it->bidi_it.disp_pos = limit;
6145 it->bidi_it.disp_prop = 0;
6147 do {
6148 bprev = it->bidi_it;
6149 bidi_move_to_visually_next (&it->bidi_it);
6150 } while (it->bidi_it.charpos != limit);
6151 IT_CHARPOS (*it) = limit;
6152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6153 if (bidi_it_prev)
6154 *bidi_it_prev = bprev;
6156 *skipped_p = newline_found_p = 1;
6158 else
6160 while (get_next_display_element (it)
6161 && !newline_found_p)
6163 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6164 if (newline_found_p && it->bidi_p && bidi_it_prev)
6165 *bidi_it_prev = it->bidi_it;
6166 set_iterator_to_next (it, 0);
6171 it->selective = old_selective;
6172 return newline_found_p;
6176 /* Set IT's current position to the previous visible line start. Skip
6177 invisible text that is so either due to text properties or due to
6178 selective display. Caution: this does not change IT->current_x and
6179 IT->hpos. */
6181 static void
6182 back_to_previous_visible_line_start (struct it *it)
6184 while (IT_CHARPOS (*it) > BEGV)
6186 back_to_previous_line_start (it);
6188 if (IT_CHARPOS (*it) <= BEGV)
6189 break;
6191 /* If selective > 0, then lines indented more than its value are
6192 invisible. */
6193 if (it->selective > 0
6194 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6195 it->selective))
6196 continue;
6198 /* Check the newline before point for invisibility. */
6200 Lisp_Object prop;
6201 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6202 Qinvisible, it->window);
6203 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6204 continue;
6207 if (IT_CHARPOS (*it) <= BEGV)
6208 break;
6211 struct it it2;
6212 void *it2data = NULL;
6213 ptrdiff_t pos;
6214 ptrdiff_t beg, end;
6215 Lisp_Object val, overlay;
6217 SAVE_IT (it2, *it, it2data);
6219 /* If newline is part of a composition, continue from start of composition */
6220 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6221 && beg < IT_CHARPOS (*it))
6222 goto replaced;
6224 /* If newline is replaced by a display property, find start of overlay
6225 or interval and continue search from that point. */
6226 pos = --IT_CHARPOS (it2);
6227 --IT_BYTEPOS (it2);
6228 it2.sp = 0;
6229 bidi_unshelve_cache (NULL, 0);
6230 it2.string_from_display_prop_p = 0;
6231 it2.from_disp_prop_p = 0;
6232 if (handle_display_prop (&it2) == HANDLED_RETURN
6233 && !NILP (val = get_char_property_and_overlay
6234 (make_number (pos), Qdisplay, Qnil, &overlay))
6235 && (OVERLAYP (overlay)
6236 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6237 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6239 RESTORE_IT (it, it, it2data);
6240 goto replaced;
6243 /* Newline is not replaced by anything -- so we are done. */
6244 RESTORE_IT (it, it, it2data);
6245 break;
6247 replaced:
6248 if (beg < BEGV)
6249 beg = BEGV;
6250 IT_CHARPOS (*it) = beg;
6251 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6255 it->continuation_lines_width = 0;
6257 eassert (IT_CHARPOS (*it) >= BEGV);
6258 eassert (IT_CHARPOS (*it) == BEGV
6259 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6260 CHECK_IT (it);
6264 /* Reseat iterator IT at the previous visible line start. Skip
6265 invisible text that is so either due to text properties or due to
6266 selective display. At the end, update IT's overlay information,
6267 face information etc. */
6269 void
6270 reseat_at_previous_visible_line_start (struct it *it)
6272 back_to_previous_visible_line_start (it);
6273 reseat (it, it->current.pos, 1);
6274 CHECK_IT (it);
6278 /* Reseat iterator IT on the next visible line start in the current
6279 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6280 preceding the line start. Skip over invisible text that is so
6281 because of selective display. Compute faces, overlays etc at the
6282 new position. Note that this function does not skip over text that
6283 is invisible because of text properties. */
6285 static void
6286 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6288 int newline_found_p, skipped_p = 0;
6289 struct bidi_it bidi_it_prev;
6291 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6293 /* Skip over lines that are invisible because they are indented
6294 more than the value of IT->selective. */
6295 if (it->selective > 0)
6296 while (IT_CHARPOS (*it) < ZV
6297 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6298 it->selective))
6300 eassert (IT_BYTEPOS (*it) == BEGV
6301 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6302 newline_found_p =
6303 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6306 /* Position on the newline if that's what's requested. */
6307 if (on_newline_p && newline_found_p)
6309 if (STRINGP (it->string))
6311 if (IT_STRING_CHARPOS (*it) > 0)
6313 if (!it->bidi_p)
6315 --IT_STRING_CHARPOS (*it);
6316 --IT_STRING_BYTEPOS (*it);
6318 else
6320 /* We need to restore the bidi iterator to the state
6321 it had on the newline, and resync the IT's
6322 position with that. */
6323 it->bidi_it = bidi_it_prev;
6324 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6325 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6329 else if (IT_CHARPOS (*it) > BEGV)
6331 if (!it->bidi_p)
6333 --IT_CHARPOS (*it);
6334 --IT_BYTEPOS (*it);
6336 else
6338 /* We need to restore the bidi iterator to the state it
6339 had on the newline and resync IT with that. */
6340 it->bidi_it = bidi_it_prev;
6341 IT_CHARPOS (*it) = it->bidi_it.charpos;
6342 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6344 reseat (it, it->current.pos, 0);
6347 else if (skipped_p)
6348 reseat (it, it->current.pos, 0);
6350 CHECK_IT (it);
6355 /***********************************************************************
6356 Changing an iterator's position
6357 ***********************************************************************/
6359 /* Change IT's current position to POS in current_buffer. If FORCE_P
6360 is non-zero, always check for text properties at the new position.
6361 Otherwise, text properties are only looked up if POS >=
6362 IT->check_charpos of a property. */
6364 static void
6365 reseat (struct it *it, struct text_pos pos, int force_p)
6367 ptrdiff_t original_pos = IT_CHARPOS (*it);
6369 reseat_1 (it, pos, 0);
6371 /* Determine where to check text properties. Avoid doing it
6372 where possible because text property lookup is very expensive. */
6373 if (force_p
6374 || CHARPOS (pos) > it->stop_charpos
6375 || CHARPOS (pos) < original_pos)
6377 if (it->bidi_p)
6379 /* For bidi iteration, we need to prime prev_stop and
6380 base_level_stop with our best estimations. */
6381 /* Implementation note: Of course, POS is not necessarily a
6382 stop position, so assigning prev_pos to it is a lie; we
6383 should have called compute_stop_backwards. However, if
6384 the current buffer does not include any R2L characters,
6385 that call would be a waste of cycles, because the
6386 iterator will never move back, and thus never cross this
6387 "fake" stop position. So we delay that backward search
6388 until the time we really need it, in next_element_from_buffer. */
6389 if (CHARPOS (pos) != it->prev_stop)
6390 it->prev_stop = CHARPOS (pos);
6391 if (CHARPOS (pos) < it->base_level_stop)
6392 it->base_level_stop = 0; /* meaning it's unknown */
6393 handle_stop (it);
6395 else
6397 handle_stop (it);
6398 it->prev_stop = it->base_level_stop = 0;
6403 CHECK_IT (it);
6407 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6408 IT->stop_pos to POS, also. */
6410 static void
6411 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6413 /* Don't call this function when scanning a C string. */
6414 eassert (it->s == NULL);
6416 /* POS must be a reasonable value. */
6417 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6419 it->current.pos = it->position = pos;
6420 it->end_charpos = ZV;
6421 it->dpvec = NULL;
6422 it->current.dpvec_index = -1;
6423 it->current.overlay_string_index = -1;
6424 IT_STRING_CHARPOS (*it) = -1;
6425 IT_STRING_BYTEPOS (*it) = -1;
6426 it->string = Qnil;
6427 it->method = GET_FROM_BUFFER;
6428 it->object = it->w->contents;
6429 it->area = TEXT_AREA;
6430 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6431 it->sp = 0;
6432 it->string_from_display_prop_p = 0;
6433 it->string_from_prefix_prop_p = 0;
6435 it->from_disp_prop_p = 0;
6436 it->face_before_selective_p = 0;
6437 if (it->bidi_p)
6439 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6440 &it->bidi_it);
6441 bidi_unshelve_cache (NULL, 0);
6442 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6443 it->bidi_it.string.s = NULL;
6444 it->bidi_it.string.lstring = Qnil;
6445 it->bidi_it.string.bufpos = 0;
6446 it->bidi_it.string.unibyte = 0;
6447 it->bidi_it.w = it->w;
6450 if (set_stop_p)
6452 it->stop_charpos = CHARPOS (pos);
6453 it->base_level_stop = CHARPOS (pos);
6455 /* This make the information stored in it->cmp_it invalidate. */
6456 it->cmp_it.id = -1;
6460 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6461 If S is non-null, it is a C string to iterate over. Otherwise,
6462 STRING gives a Lisp string to iterate over.
6464 If PRECISION > 0, don't return more then PRECISION number of
6465 characters from the string.
6467 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6468 characters have been returned. FIELD_WIDTH < 0 means an infinite
6469 field width.
6471 MULTIBYTE = 0 means disable processing of multibyte characters,
6472 MULTIBYTE > 0 means enable it,
6473 MULTIBYTE < 0 means use IT->multibyte_p.
6475 IT must be initialized via a prior call to init_iterator before
6476 calling this function. */
6478 static void
6479 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6480 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6481 int multibyte)
6483 /* No region in strings. */
6484 it->region_beg_charpos = it->region_end_charpos = -1;
6486 /* No text property checks performed by default, but see below. */
6487 it->stop_charpos = -1;
6489 /* Set iterator position and end position. */
6490 memset (&it->current, 0, sizeof it->current);
6491 it->current.overlay_string_index = -1;
6492 it->current.dpvec_index = -1;
6493 eassert (charpos >= 0);
6495 /* If STRING is specified, use its multibyteness, otherwise use the
6496 setting of MULTIBYTE, if specified. */
6497 if (multibyte >= 0)
6498 it->multibyte_p = multibyte > 0;
6500 /* Bidirectional reordering of strings is controlled by the default
6501 value of bidi-display-reordering. Don't try to reorder while
6502 loading loadup.el, as the necessary character property tables are
6503 not yet available. */
6504 it->bidi_p =
6505 NILP (Vpurify_flag)
6506 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6508 if (s == NULL)
6510 eassert (STRINGP (string));
6511 it->string = string;
6512 it->s = NULL;
6513 it->end_charpos = it->string_nchars = SCHARS (string);
6514 it->method = GET_FROM_STRING;
6515 it->current.string_pos = string_pos (charpos, string);
6517 if (it->bidi_p)
6519 it->bidi_it.string.lstring = string;
6520 it->bidi_it.string.s = NULL;
6521 it->bidi_it.string.schars = it->end_charpos;
6522 it->bidi_it.string.bufpos = 0;
6523 it->bidi_it.string.from_disp_str = 0;
6524 it->bidi_it.string.unibyte = !it->multibyte_p;
6525 it->bidi_it.w = it->w;
6526 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6527 FRAME_WINDOW_P (it->f), &it->bidi_it);
6530 else
6532 it->s = (const unsigned char *) s;
6533 it->string = Qnil;
6535 /* Note that we use IT->current.pos, not it->current.string_pos,
6536 for displaying C strings. */
6537 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6538 if (it->multibyte_p)
6540 it->current.pos = c_string_pos (charpos, s, 1);
6541 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6543 else
6545 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6546 it->end_charpos = it->string_nchars = strlen (s);
6549 if (it->bidi_p)
6551 it->bidi_it.string.lstring = Qnil;
6552 it->bidi_it.string.s = (const unsigned char *) s;
6553 it->bidi_it.string.schars = it->end_charpos;
6554 it->bidi_it.string.bufpos = 0;
6555 it->bidi_it.string.from_disp_str = 0;
6556 it->bidi_it.string.unibyte = !it->multibyte_p;
6557 it->bidi_it.w = it->w;
6558 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6559 &it->bidi_it);
6561 it->method = GET_FROM_C_STRING;
6564 /* PRECISION > 0 means don't return more than PRECISION characters
6565 from the string. */
6566 if (precision > 0 && it->end_charpos - charpos > precision)
6568 it->end_charpos = it->string_nchars = charpos + precision;
6569 if (it->bidi_p)
6570 it->bidi_it.string.schars = it->end_charpos;
6573 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6574 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6575 FIELD_WIDTH < 0 means infinite field width. This is useful for
6576 padding with `-' at the end of a mode line. */
6577 if (field_width < 0)
6578 field_width = INFINITY;
6579 /* Implementation note: We deliberately don't enlarge
6580 it->bidi_it.string.schars here to fit it->end_charpos, because
6581 the bidi iterator cannot produce characters out of thin air. */
6582 if (field_width > it->end_charpos - charpos)
6583 it->end_charpos = charpos + field_width;
6585 /* Use the standard display table for displaying strings. */
6586 if (DISP_TABLE_P (Vstandard_display_table))
6587 it->dp = XCHAR_TABLE (Vstandard_display_table);
6589 it->stop_charpos = charpos;
6590 it->prev_stop = charpos;
6591 it->base_level_stop = 0;
6592 if (it->bidi_p)
6594 it->bidi_it.first_elt = 1;
6595 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6596 it->bidi_it.disp_pos = -1;
6598 if (s == NULL && it->multibyte_p)
6600 ptrdiff_t endpos = SCHARS (it->string);
6601 if (endpos > it->end_charpos)
6602 endpos = it->end_charpos;
6603 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6604 it->string);
6606 CHECK_IT (it);
6611 /***********************************************************************
6612 Iteration
6613 ***********************************************************************/
6615 /* Map enum it_method value to corresponding next_element_from_* function. */
6617 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6619 next_element_from_buffer,
6620 next_element_from_display_vector,
6621 next_element_from_string,
6622 next_element_from_c_string,
6623 next_element_from_image,
6624 next_element_from_stretch
6625 #ifdef HAVE_XWIDGETS
6626 ,next_element_from_xwidget
6627 #endif
6630 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6633 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6634 (possibly with the following characters). */
6636 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6637 ((IT)->cmp_it.id >= 0 \
6638 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6639 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6640 END_CHARPOS, (IT)->w, \
6641 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6642 (IT)->string)))
6645 /* Lookup the char-table Vglyphless_char_display for character C (-1
6646 if we want information for no-font case), and return the display
6647 method symbol. By side-effect, update it->what and
6648 it->glyphless_method. This function is called from
6649 get_next_display_element for each character element, and from
6650 x_produce_glyphs when no suitable font was found. */
6652 Lisp_Object
6653 lookup_glyphless_char_display (int c, struct it *it)
6655 Lisp_Object glyphless_method = Qnil;
6657 if (CHAR_TABLE_P (Vglyphless_char_display)
6658 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6660 if (c >= 0)
6662 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6663 if (CONSP (glyphless_method))
6664 glyphless_method = FRAME_WINDOW_P (it->f)
6665 ? XCAR (glyphless_method)
6666 : XCDR (glyphless_method);
6668 else
6669 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6672 retry:
6673 if (NILP (glyphless_method))
6675 if (c >= 0)
6676 /* The default is to display the character by a proper font. */
6677 return Qnil;
6678 /* The default for the no-font case is to display an empty box. */
6679 glyphless_method = Qempty_box;
6681 if (EQ (glyphless_method, Qzero_width))
6683 if (c >= 0)
6684 return glyphless_method;
6685 /* This method can't be used for the no-font case. */
6686 glyphless_method = Qempty_box;
6688 if (EQ (glyphless_method, Qthin_space))
6689 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6690 else if (EQ (glyphless_method, Qempty_box))
6691 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6692 else if (EQ (glyphless_method, Qhex_code))
6693 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6694 else if (STRINGP (glyphless_method))
6695 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6696 else
6698 /* Invalid value. We use the default method. */
6699 glyphless_method = Qnil;
6700 goto retry;
6702 it->what = IT_GLYPHLESS;
6703 return glyphless_method;
6706 /* Merge escape glyph face and cache the result. */
6708 static struct frame *last_escape_glyph_frame = NULL;
6709 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6710 static int last_escape_glyph_merged_face_id = 0;
6712 static int
6713 merge_escape_glyph_face (struct it *it)
6715 int face_id;
6717 if (it->f == last_escape_glyph_frame
6718 && it->face_id == last_escape_glyph_face_id)
6719 face_id = last_escape_glyph_merged_face_id;
6720 else
6722 /* Merge the `escape-glyph' face into the current face. */
6723 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6724 last_escape_glyph_frame = it->f;
6725 last_escape_glyph_face_id = it->face_id;
6726 last_escape_glyph_merged_face_id = face_id;
6728 return face_id;
6731 /* Likewise for glyphless glyph face. */
6733 static struct frame *last_glyphless_glyph_frame = NULL;
6734 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6735 static int last_glyphless_glyph_merged_face_id = 0;
6738 merge_glyphless_glyph_face (struct it *it)
6740 int face_id;
6742 if (it->f == last_glyphless_glyph_frame
6743 && it->face_id == last_glyphless_glyph_face_id)
6744 face_id = last_glyphless_glyph_merged_face_id;
6745 else
6747 /* Merge the `glyphless-char' face into the current face. */
6748 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6749 last_glyphless_glyph_frame = it->f;
6750 last_glyphless_glyph_face_id = it->face_id;
6751 last_glyphless_glyph_merged_face_id = face_id;
6753 return face_id;
6756 /* Load IT's display element fields with information about the next
6757 display element from the current position of IT. Value is zero if
6758 end of buffer (or C string) is reached. */
6760 static int
6761 get_next_display_element (struct it *it)
6763 /* Non-zero means that we found a display element. Zero means that
6764 we hit the end of what we iterate over. Performance note: the
6765 function pointer `method' used here turns out to be faster than
6766 using a sequence of if-statements. */
6767 int success_p;
6769 get_next:
6770 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6772 if (it->what == IT_CHARACTER)
6774 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6775 and only if (a) the resolved directionality of that character
6776 is R..." */
6777 /* FIXME: Do we need an exception for characters from display
6778 tables? */
6779 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6780 it->c = bidi_mirror_char (it->c);
6781 /* Map via display table or translate control characters.
6782 IT->c, IT->len etc. have been set to the next character by
6783 the function call above. If we have a display table, and it
6784 contains an entry for IT->c, translate it. Don't do this if
6785 IT->c itself comes from a display table, otherwise we could
6786 end up in an infinite recursion. (An alternative could be to
6787 count the recursion depth of this function and signal an
6788 error when a certain maximum depth is reached.) Is it worth
6789 it? */
6790 if (success_p && it->dpvec == NULL)
6792 Lisp_Object dv;
6793 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6794 int nonascii_space_p = 0;
6795 int nonascii_hyphen_p = 0;
6796 int c = it->c; /* This is the character to display. */
6798 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6800 eassert (SINGLE_BYTE_CHAR_P (c));
6801 if (unibyte_display_via_language_environment)
6803 c = DECODE_CHAR (unibyte, c);
6804 if (c < 0)
6805 c = BYTE8_TO_CHAR (it->c);
6807 else
6808 c = BYTE8_TO_CHAR (it->c);
6811 if (it->dp
6812 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6813 VECTORP (dv)))
6815 struct Lisp_Vector *v = XVECTOR (dv);
6817 /* Return the first character from the display table
6818 entry, if not empty. If empty, don't display the
6819 current character. */
6820 if (v->header.size)
6822 it->dpvec_char_len = it->len;
6823 it->dpvec = v->contents;
6824 it->dpend = v->contents + v->header.size;
6825 it->current.dpvec_index = 0;
6826 it->dpvec_face_id = -1;
6827 it->saved_face_id = it->face_id;
6828 it->method = GET_FROM_DISPLAY_VECTOR;
6829 it->ellipsis_p = 0;
6831 else
6833 set_iterator_to_next (it, 0);
6835 goto get_next;
6838 if (! NILP (lookup_glyphless_char_display (c, it)))
6840 if (it->what == IT_GLYPHLESS)
6841 goto done;
6842 /* Don't display this character. */
6843 set_iterator_to_next (it, 0);
6844 goto get_next;
6847 /* If `nobreak-char-display' is non-nil, we display
6848 non-ASCII spaces and hyphens specially. */
6849 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6851 if (c == 0xA0)
6852 nonascii_space_p = 1;
6853 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6854 nonascii_hyphen_p = 1;
6857 /* Translate control characters into `\003' or `^C' form.
6858 Control characters coming from a display table entry are
6859 currently not translated because we use IT->dpvec to hold
6860 the translation. This could easily be changed but I
6861 don't believe that it is worth doing.
6863 The characters handled by `nobreak-char-display' must be
6864 translated too.
6866 Non-printable characters and raw-byte characters are also
6867 translated to octal form. */
6868 if (((c < ' ' || c == 127) /* ASCII control chars */
6869 ? (it->area != TEXT_AREA
6870 /* In mode line, treat \n, \t like other crl chars. */
6871 || (c != '\t'
6872 && it->glyph_row
6873 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6874 || (c != '\n' && c != '\t'))
6875 : (nonascii_space_p
6876 || nonascii_hyphen_p
6877 || CHAR_BYTE8_P (c)
6878 || ! CHAR_PRINTABLE_P (c))))
6880 /* C is a control character, non-ASCII space/hyphen,
6881 raw-byte, or a non-printable character which must be
6882 displayed either as '\003' or as `^C' where the '\\'
6883 and '^' can be defined in the display table. Fill
6884 IT->ctl_chars with glyphs for what we have to
6885 display. Then, set IT->dpvec to these glyphs. */
6886 Lisp_Object gc;
6887 int ctl_len;
6888 int face_id;
6889 int lface_id = 0;
6890 int escape_glyph;
6892 /* Handle control characters with ^. */
6894 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6896 int g;
6898 g = '^'; /* default glyph for Control */
6899 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6900 if (it->dp
6901 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6903 g = GLYPH_CODE_CHAR (gc);
6904 lface_id = GLYPH_CODE_FACE (gc);
6907 face_id = (lface_id
6908 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6909 : merge_escape_glyph_face (it));
6911 XSETINT (it->ctl_chars[0], g);
6912 XSETINT (it->ctl_chars[1], c ^ 0100);
6913 ctl_len = 2;
6914 goto display_control;
6917 /* Handle non-ascii space in the mode where it only gets
6918 highlighting. */
6920 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6922 /* Merge `nobreak-space' into the current face. */
6923 face_id = merge_faces (it->f, Qnobreak_space, 0,
6924 it->face_id);
6925 XSETINT (it->ctl_chars[0], ' ');
6926 ctl_len = 1;
6927 goto display_control;
6930 /* Handle sequences that start with the "escape glyph". */
6932 /* the default escape glyph is \. */
6933 escape_glyph = '\\';
6935 if (it->dp
6936 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6938 escape_glyph = GLYPH_CODE_CHAR (gc);
6939 lface_id = GLYPH_CODE_FACE (gc);
6942 face_id = (lface_id
6943 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6944 : merge_escape_glyph_face (it));
6946 /* Draw non-ASCII hyphen with just highlighting: */
6948 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6950 XSETINT (it->ctl_chars[0], '-');
6951 ctl_len = 1;
6952 goto display_control;
6955 /* Draw non-ASCII space/hyphen with escape glyph: */
6957 if (nonascii_space_p || nonascii_hyphen_p)
6959 XSETINT (it->ctl_chars[0], escape_glyph);
6960 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6961 ctl_len = 2;
6962 goto display_control;
6966 char str[10];
6967 int len, i;
6969 if (CHAR_BYTE8_P (c))
6970 /* Display \200 instead of \17777600. */
6971 c = CHAR_TO_BYTE8 (c);
6972 len = sprintf (str, "%03o", c);
6974 XSETINT (it->ctl_chars[0], escape_glyph);
6975 for (i = 0; i < len; i++)
6976 XSETINT (it->ctl_chars[i + 1], str[i]);
6977 ctl_len = len + 1;
6980 display_control:
6981 /* Set up IT->dpvec and return first character from it. */
6982 it->dpvec_char_len = it->len;
6983 it->dpvec = it->ctl_chars;
6984 it->dpend = it->dpvec + ctl_len;
6985 it->current.dpvec_index = 0;
6986 it->dpvec_face_id = face_id;
6987 it->saved_face_id = it->face_id;
6988 it->method = GET_FROM_DISPLAY_VECTOR;
6989 it->ellipsis_p = 0;
6990 goto get_next;
6992 it->char_to_display = c;
6994 else if (success_p)
6996 it->char_to_display = it->c;
7000 /* Adjust face id for a multibyte character. There are no multibyte
7001 character in unibyte text. */
7002 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7003 && it->multibyte_p
7004 && success_p
7005 && FRAME_WINDOW_P (it->f))
7007 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7009 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7011 /* Automatic composition with glyph-string. */
7012 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7014 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7016 else
7018 ptrdiff_t pos = (it->s ? -1
7019 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7020 : IT_CHARPOS (*it));
7021 int c;
7023 if (it->what == IT_CHARACTER)
7024 c = it->char_to_display;
7025 else
7027 struct composition *cmp = composition_table[it->cmp_it.id];
7028 int i;
7030 c = ' ';
7031 for (i = 0; i < cmp->glyph_len; i++)
7032 /* TAB in a composition means display glyphs with
7033 padding space on the left or right. */
7034 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7035 break;
7037 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7041 done:
7042 /* Is this character the last one of a run of characters with
7043 box? If yes, set IT->end_of_box_run_p to 1. */
7044 if (it->face_box_p
7045 && it->s == NULL)
7047 if (it->method == GET_FROM_STRING && it->sp)
7049 int face_id = underlying_face_id (it);
7050 struct face *face = FACE_FROM_ID (it->f, face_id);
7052 if (face)
7054 if (face->box == FACE_NO_BOX)
7056 /* If the box comes from face properties in a
7057 display string, check faces in that string. */
7058 int string_face_id = face_after_it_pos (it);
7059 it->end_of_box_run_p
7060 = (FACE_FROM_ID (it->f, string_face_id)->box
7061 == FACE_NO_BOX);
7063 /* Otherwise, the box comes from the underlying face.
7064 If this is the last string character displayed, check
7065 the next buffer location. */
7066 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7067 && (it->current.overlay_string_index
7068 == it->n_overlay_strings - 1))
7070 ptrdiff_t ignore;
7071 int next_face_id;
7072 struct text_pos pos = it->current.pos;
7073 INC_TEXT_POS (pos, it->multibyte_p);
7075 next_face_id = face_at_buffer_position
7076 (it->w, CHARPOS (pos), it->region_beg_charpos,
7077 it->region_end_charpos, &ignore,
7078 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7079 -1);
7080 it->end_of_box_run_p
7081 = (FACE_FROM_ID (it->f, next_face_id)->box
7082 == FACE_NO_BOX);
7086 /* next_element_from_display_vector sets this flag according to
7087 faces of the display vector glyphs, see there. */
7088 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7090 int face_id = face_after_it_pos (it);
7091 it->end_of_box_run_p
7092 = (face_id != it->face_id
7093 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7096 /* If we reached the end of the object we've been iterating (e.g., a
7097 display string or an overlay string), and there's something on
7098 IT->stack, proceed with what's on the stack. It doesn't make
7099 sense to return zero if there's unprocessed stuff on the stack,
7100 because otherwise that stuff will never be displayed. */
7101 if (!success_p && it->sp > 0)
7103 set_iterator_to_next (it, 0);
7104 success_p = get_next_display_element (it);
7107 /* Value is 0 if end of buffer or string reached. */
7108 return success_p;
7112 /* Move IT to the next display element.
7114 RESEAT_P non-zero means if called on a newline in buffer text,
7115 skip to the next visible line start.
7117 Functions get_next_display_element and set_iterator_to_next are
7118 separate because I find this arrangement easier to handle than a
7119 get_next_display_element function that also increments IT's
7120 position. The way it is we can first look at an iterator's current
7121 display element, decide whether it fits on a line, and if it does,
7122 increment the iterator position. The other way around we probably
7123 would either need a flag indicating whether the iterator has to be
7124 incremented the next time, or we would have to implement a
7125 decrement position function which would not be easy to write. */
7127 void
7128 set_iterator_to_next (struct it *it, int reseat_p)
7130 /* Reset flags indicating start and end of a sequence of characters
7131 with box. Reset them at the start of this function because
7132 moving the iterator to a new position might set them. */
7133 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7135 switch (it->method)
7137 case GET_FROM_BUFFER:
7138 /* The current display element of IT is a character from
7139 current_buffer. Advance in the buffer, and maybe skip over
7140 invisible lines that are so because of selective display. */
7141 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7142 reseat_at_next_visible_line_start (it, 0);
7143 else if (it->cmp_it.id >= 0)
7145 /* We are currently getting glyphs from a composition. */
7146 int i;
7148 if (! it->bidi_p)
7150 IT_CHARPOS (*it) += it->cmp_it.nchars;
7151 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7152 if (it->cmp_it.to < it->cmp_it.nglyphs)
7154 it->cmp_it.from = it->cmp_it.to;
7156 else
7158 it->cmp_it.id = -1;
7159 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7160 IT_BYTEPOS (*it),
7161 it->end_charpos, Qnil);
7164 else if (! it->cmp_it.reversed_p)
7166 /* Composition created while scanning forward. */
7167 /* Update IT's char/byte positions to point to the first
7168 character of the next grapheme cluster, or to the
7169 character visually after the current composition. */
7170 for (i = 0; i < it->cmp_it.nchars; i++)
7171 bidi_move_to_visually_next (&it->bidi_it);
7172 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7173 IT_CHARPOS (*it) = it->bidi_it.charpos;
7175 if (it->cmp_it.to < it->cmp_it.nglyphs)
7177 /* Proceed to the next grapheme cluster. */
7178 it->cmp_it.from = it->cmp_it.to;
7180 else
7182 /* No more grapheme clusters in this composition.
7183 Find the next stop position. */
7184 ptrdiff_t stop = it->end_charpos;
7185 if (it->bidi_it.scan_dir < 0)
7186 /* Now we are scanning backward and don't know
7187 where to stop. */
7188 stop = -1;
7189 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7190 IT_BYTEPOS (*it), stop, Qnil);
7193 else
7195 /* Composition created while scanning backward. */
7196 /* Update IT's char/byte positions to point to the last
7197 character of the previous grapheme cluster, or the
7198 character visually after the current composition. */
7199 for (i = 0; i < it->cmp_it.nchars; i++)
7200 bidi_move_to_visually_next (&it->bidi_it);
7201 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7202 IT_CHARPOS (*it) = it->bidi_it.charpos;
7203 if (it->cmp_it.from > 0)
7205 /* Proceed to the previous grapheme cluster. */
7206 it->cmp_it.to = it->cmp_it.from;
7208 else
7210 /* No more grapheme clusters in this composition.
7211 Find the next stop position. */
7212 ptrdiff_t stop = it->end_charpos;
7213 if (it->bidi_it.scan_dir < 0)
7214 /* Now we are scanning backward and don't know
7215 where to stop. */
7216 stop = -1;
7217 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7218 IT_BYTEPOS (*it), stop, Qnil);
7222 else
7224 eassert (it->len != 0);
7226 if (!it->bidi_p)
7228 IT_BYTEPOS (*it) += it->len;
7229 IT_CHARPOS (*it) += 1;
7231 else
7233 int prev_scan_dir = it->bidi_it.scan_dir;
7234 /* If this is a new paragraph, determine its base
7235 direction (a.k.a. its base embedding level). */
7236 if (it->bidi_it.new_paragraph)
7237 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7240 IT_CHARPOS (*it) = it->bidi_it.charpos;
7241 if (prev_scan_dir != it->bidi_it.scan_dir)
7243 /* As the scan direction was changed, we must
7244 re-compute the stop position for composition. */
7245 ptrdiff_t stop = it->end_charpos;
7246 if (it->bidi_it.scan_dir < 0)
7247 stop = -1;
7248 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7249 IT_BYTEPOS (*it), stop, Qnil);
7252 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7254 break;
7256 case GET_FROM_C_STRING:
7257 /* Current display element of IT is from a C string. */
7258 if (!it->bidi_p
7259 /* If the string position is beyond string's end, it means
7260 next_element_from_c_string is padding the string with
7261 blanks, in which case we bypass the bidi iterator,
7262 because it cannot deal with such virtual characters. */
7263 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7265 IT_BYTEPOS (*it) += it->len;
7266 IT_CHARPOS (*it) += 1;
7268 else
7270 bidi_move_to_visually_next (&it->bidi_it);
7271 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7272 IT_CHARPOS (*it) = it->bidi_it.charpos;
7274 break;
7276 case GET_FROM_DISPLAY_VECTOR:
7277 /* Current display element of IT is from a display table entry.
7278 Advance in the display table definition. Reset it to null if
7279 end reached, and continue with characters from buffers/
7280 strings. */
7281 ++it->current.dpvec_index;
7283 /* Restore face of the iterator to what they were before the
7284 display vector entry (these entries may contain faces). */
7285 it->face_id = it->saved_face_id;
7287 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7289 int recheck_faces = it->ellipsis_p;
7291 if (it->s)
7292 it->method = GET_FROM_C_STRING;
7293 else if (STRINGP (it->string))
7294 it->method = GET_FROM_STRING;
7295 else
7297 it->method = GET_FROM_BUFFER;
7298 it->object = it->w->contents;
7301 it->dpvec = NULL;
7302 it->current.dpvec_index = -1;
7304 /* Skip over characters which were displayed via IT->dpvec. */
7305 if (it->dpvec_char_len < 0)
7306 reseat_at_next_visible_line_start (it, 1);
7307 else if (it->dpvec_char_len > 0)
7309 if (it->method == GET_FROM_STRING
7310 && it->current.overlay_string_index >= 0
7311 && it->n_overlay_strings > 0)
7312 it->ignore_overlay_strings_at_pos_p = 1;
7313 it->len = it->dpvec_char_len;
7314 set_iterator_to_next (it, reseat_p);
7317 /* Maybe recheck faces after display vector */
7318 if (recheck_faces)
7319 it->stop_charpos = IT_CHARPOS (*it);
7321 break;
7323 case GET_FROM_STRING:
7324 /* Current display element is a character from a Lisp string. */
7325 eassert (it->s == NULL && STRINGP (it->string));
7326 /* Don't advance past string end. These conditions are true
7327 when set_iterator_to_next is called at the end of
7328 get_next_display_element, in which case the Lisp string is
7329 already exhausted, and all we want is pop the iterator
7330 stack. */
7331 if (it->current.overlay_string_index >= 0)
7333 /* This is an overlay string, so there's no padding with
7334 spaces, and the number of characters in the string is
7335 where the string ends. */
7336 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7337 goto consider_string_end;
7339 else
7341 /* Not an overlay string. There could be padding, so test
7342 against it->end_charpos . */
7343 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7344 goto consider_string_end;
7346 if (it->cmp_it.id >= 0)
7348 int i;
7350 if (! it->bidi_p)
7352 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7353 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7354 if (it->cmp_it.to < it->cmp_it.nglyphs)
7355 it->cmp_it.from = it->cmp_it.to;
7356 else
7358 it->cmp_it.id = -1;
7359 composition_compute_stop_pos (&it->cmp_it,
7360 IT_STRING_CHARPOS (*it),
7361 IT_STRING_BYTEPOS (*it),
7362 it->end_charpos, it->string);
7365 else if (! it->cmp_it.reversed_p)
7367 for (i = 0; i < it->cmp_it.nchars; i++)
7368 bidi_move_to_visually_next (&it->bidi_it);
7369 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7370 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7372 if (it->cmp_it.to < it->cmp_it.nglyphs)
7373 it->cmp_it.from = it->cmp_it.to;
7374 else
7376 ptrdiff_t stop = it->end_charpos;
7377 if (it->bidi_it.scan_dir < 0)
7378 stop = -1;
7379 composition_compute_stop_pos (&it->cmp_it,
7380 IT_STRING_CHARPOS (*it),
7381 IT_STRING_BYTEPOS (*it), stop,
7382 it->string);
7385 else
7387 for (i = 0; i < it->cmp_it.nchars; i++)
7388 bidi_move_to_visually_next (&it->bidi_it);
7389 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7390 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7391 if (it->cmp_it.from > 0)
7392 it->cmp_it.to = it->cmp_it.from;
7393 else
7395 ptrdiff_t stop = it->end_charpos;
7396 if (it->bidi_it.scan_dir < 0)
7397 stop = -1;
7398 composition_compute_stop_pos (&it->cmp_it,
7399 IT_STRING_CHARPOS (*it),
7400 IT_STRING_BYTEPOS (*it), stop,
7401 it->string);
7405 else
7407 if (!it->bidi_p
7408 /* If the string position is beyond string's end, it
7409 means next_element_from_string is padding the string
7410 with blanks, in which case we bypass the bidi
7411 iterator, because it cannot deal with such virtual
7412 characters. */
7413 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7415 IT_STRING_BYTEPOS (*it) += it->len;
7416 IT_STRING_CHARPOS (*it) += 1;
7418 else
7420 int prev_scan_dir = it->bidi_it.scan_dir;
7422 bidi_move_to_visually_next (&it->bidi_it);
7423 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7424 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7425 if (prev_scan_dir != it->bidi_it.scan_dir)
7427 ptrdiff_t stop = it->end_charpos;
7429 if (it->bidi_it.scan_dir < 0)
7430 stop = -1;
7431 composition_compute_stop_pos (&it->cmp_it,
7432 IT_STRING_CHARPOS (*it),
7433 IT_STRING_BYTEPOS (*it), stop,
7434 it->string);
7439 consider_string_end:
7441 if (it->current.overlay_string_index >= 0)
7443 /* IT->string is an overlay string. Advance to the
7444 next, if there is one. */
7445 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7447 it->ellipsis_p = 0;
7448 next_overlay_string (it);
7449 if (it->ellipsis_p)
7450 setup_for_ellipsis (it, 0);
7453 else
7455 /* IT->string is not an overlay string. If we reached
7456 its end, and there is something on IT->stack, proceed
7457 with what is on the stack. This can be either another
7458 string, this time an overlay string, or a buffer. */
7459 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7460 && it->sp > 0)
7462 pop_it (it);
7463 if (it->method == GET_FROM_STRING)
7464 goto consider_string_end;
7467 break;
7469 case GET_FROM_IMAGE:
7470 case GET_FROM_STRETCH:
7471 #ifdef HAVE_XWIDGETS
7472 case GET_FROM_XWIDGET:
7474 /* The position etc with which we have to proceed are on
7475 the stack. The position may be at the end of a string,
7476 if the `display' property takes up the whole string. */
7477 eassert (it->sp > 0);
7478 pop_it (it);
7479 if (it->method == GET_FROM_STRING)
7480 goto consider_string_end;
7481 break;
7482 #endif
7483 default:
7484 /* There are no other methods defined, so this should be a bug. */
7485 emacs_abort ();
7488 eassert (it->method != GET_FROM_STRING
7489 || (STRINGP (it->string)
7490 && IT_STRING_CHARPOS (*it) >= 0));
7493 /* Load IT's display element fields with information about the next
7494 display element which comes from a display table entry or from the
7495 result of translating a control character to one of the forms `^C'
7496 or `\003'.
7498 IT->dpvec holds the glyphs to return as characters.
7499 IT->saved_face_id holds the face id before the display vector--it
7500 is restored into IT->face_id in set_iterator_to_next. */
7502 static int
7503 next_element_from_display_vector (struct it *it)
7505 Lisp_Object gc;
7506 int prev_face_id = it->face_id;
7507 int next_face_id;
7509 /* Precondition. */
7510 eassert (it->dpvec && it->current.dpvec_index >= 0);
7512 it->face_id = it->saved_face_id;
7514 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7515 That seemed totally bogus - so I changed it... */
7516 gc = it->dpvec[it->current.dpvec_index];
7518 if (GLYPH_CODE_P (gc))
7520 struct face *this_face, *prev_face, *next_face;
7522 it->c = GLYPH_CODE_CHAR (gc);
7523 it->len = CHAR_BYTES (it->c);
7525 /* The entry may contain a face id to use. Such a face id is
7526 the id of a Lisp face, not a realized face. A face id of
7527 zero means no face is specified. */
7528 if (it->dpvec_face_id >= 0)
7529 it->face_id = it->dpvec_face_id;
7530 else
7532 int lface_id = GLYPH_CODE_FACE (gc);
7533 if (lface_id > 0)
7534 it->face_id = merge_faces (it->f, Qt, lface_id,
7535 it->saved_face_id);
7538 /* Glyphs in the display vector could have the box face, so we
7539 need to set the related flags in the iterator, as
7540 appropriate. */
7541 this_face = FACE_FROM_ID (it->f, it->face_id);
7542 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7544 /* Is this character the first character of a box-face run? */
7545 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7546 && (!prev_face
7547 || prev_face->box == FACE_NO_BOX));
7549 /* For the last character of the box-face run, we need to look
7550 either at the next glyph from the display vector, or at the
7551 face we saw before the display vector. */
7552 next_face_id = it->saved_face_id;
7553 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7555 if (it->dpvec_face_id >= 0)
7556 next_face_id = it->dpvec_face_id;
7557 else
7559 int lface_id =
7560 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7562 if (lface_id > 0)
7563 next_face_id = merge_faces (it->f, Qt, lface_id,
7564 it->saved_face_id);
7567 next_face = FACE_FROM_ID (it->f, next_face_id);
7568 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7569 && (!next_face
7570 || next_face->box == FACE_NO_BOX));
7571 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7573 else
7574 /* Display table entry is invalid. Return a space. */
7575 it->c = ' ', it->len = 1;
7577 /* Don't change position and object of the iterator here. They are
7578 still the values of the character that had this display table
7579 entry or was translated, and that's what we want. */
7580 it->what = IT_CHARACTER;
7581 return 1;
7584 /* Get the first element of string/buffer in the visual order, after
7585 being reseated to a new position in a string or a buffer. */
7586 static void
7587 get_visually_first_element (struct it *it)
7589 int string_p = STRINGP (it->string) || it->s;
7590 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7591 ptrdiff_t bob = (string_p ? 0 : BEGV);
7593 if (STRINGP (it->string))
7595 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7596 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7598 else
7600 it->bidi_it.charpos = IT_CHARPOS (*it);
7601 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7604 if (it->bidi_it.charpos == eob)
7606 /* Nothing to do, but reset the FIRST_ELT flag, like
7607 bidi_paragraph_init does, because we are not going to
7608 call it. */
7609 it->bidi_it.first_elt = 0;
7611 else if (it->bidi_it.charpos == bob
7612 || (!string_p
7613 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7614 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7616 /* If we are at the beginning of a line/string, we can produce
7617 the next element right away. */
7618 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7619 bidi_move_to_visually_next (&it->bidi_it);
7621 else
7623 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7625 /* We need to prime the bidi iterator starting at the line's or
7626 string's beginning, before we will be able to produce the
7627 next element. */
7628 if (string_p)
7629 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7630 else
7631 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7632 IT_BYTEPOS (*it), -1,
7633 &it->bidi_it.bytepos);
7634 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7637 /* Now return to buffer/string position where we were asked
7638 to get the next display element, and produce that. */
7639 bidi_move_to_visually_next (&it->bidi_it);
7641 while (it->bidi_it.bytepos != orig_bytepos
7642 && it->bidi_it.charpos < eob);
7645 /* Adjust IT's position information to where we ended up. */
7646 if (STRINGP (it->string))
7648 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7649 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7651 else
7653 IT_CHARPOS (*it) = it->bidi_it.charpos;
7654 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7657 if (STRINGP (it->string) || !it->s)
7659 ptrdiff_t stop, charpos, bytepos;
7661 if (STRINGP (it->string))
7663 eassert (!it->s);
7664 stop = SCHARS (it->string);
7665 if (stop > it->end_charpos)
7666 stop = it->end_charpos;
7667 charpos = IT_STRING_CHARPOS (*it);
7668 bytepos = IT_STRING_BYTEPOS (*it);
7670 else
7672 stop = it->end_charpos;
7673 charpos = IT_CHARPOS (*it);
7674 bytepos = IT_BYTEPOS (*it);
7676 if (it->bidi_it.scan_dir < 0)
7677 stop = -1;
7678 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7679 it->string);
7683 /* Load IT with the next display element from Lisp string IT->string.
7684 IT->current.string_pos is the current position within the string.
7685 If IT->current.overlay_string_index >= 0, the Lisp string is an
7686 overlay string. */
7688 static int
7689 next_element_from_string (struct it *it)
7691 struct text_pos position;
7693 eassert (STRINGP (it->string));
7694 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7695 eassert (IT_STRING_CHARPOS (*it) >= 0);
7696 position = it->current.string_pos;
7698 /* With bidi reordering, the character to display might not be the
7699 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7700 that we were reseat()ed to a new string, whose paragraph
7701 direction is not known. */
7702 if (it->bidi_p && it->bidi_it.first_elt)
7704 get_visually_first_element (it);
7705 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7708 /* Time to check for invisible text? */
7709 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7711 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7713 if (!(!it->bidi_p
7714 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7715 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7717 /* With bidi non-linear iteration, we could find
7718 ourselves far beyond the last computed stop_charpos,
7719 with several other stop positions in between that we
7720 missed. Scan them all now, in buffer's logical
7721 order, until we find and handle the last stop_charpos
7722 that precedes our current position. */
7723 handle_stop_backwards (it, it->stop_charpos);
7724 return GET_NEXT_DISPLAY_ELEMENT (it);
7726 else
7728 if (it->bidi_p)
7730 /* Take note of the stop position we just moved
7731 across, for when we will move back across it. */
7732 it->prev_stop = it->stop_charpos;
7733 /* If we are at base paragraph embedding level, take
7734 note of the last stop position seen at this
7735 level. */
7736 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7737 it->base_level_stop = it->stop_charpos;
7739 handle_stop (it);
7741 /* Since a handler may have changed IT->method, we must
7742 recurse here. */
7743 return GET_NEXT_DISPLAY_ELEMENT (it);
7746 else if (it->bidi_p
7747 /* If we are before prev_stop, we may have overstepped
7748 on our way backwards a stop_pos, and if so, we need
7749 to handle that stop_pos. */
7750 && IT_STRING_CHARPOS (*it) < it->prev_stop
7751 /* We can sometimes back up for reasons that have nothing
7752 to do with bidi reordering. E.g., compositions. The
7753 code below is only needed when we are above the base
7754 embedding level, so test for that explicitly. */
7755 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7757 /* If we lost track of base_level_stop, we have no better
7758 place for handle_stop_backwards to start from than string
7759 beginning. This happens, e.g., when we were reseated to
7760 the previous screenful of text by vertical-motion. */
7761 if (it->base_level_stop <= 0
7762 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7763 it->base_level_stop = 0;
7764 handle_stop_backwards (it, it->base_level_stop);
7765 return GET_NEXT_DISPLAY_ELEMENT (it);
7769 if (it->current.overlay_string_index >= 0)
7771 /* Get the next character from an overlay string. In overlay
7772 strings, there is no field width or padding with spaces to
7773 do. */
7774 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7776 it->what = IT_EOB;
7777 return 0;
7779 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7780 IT_STRING_BYTEPOS (*it),
7781 it->bidi_it.scan_dir < 0
7782 ? -1
7783 : SCHARS (it->string))
7784 && next_element_from_composition (it))
7786 return 1;
7788 else if (STRING_MULTIBYTE (it->string))
7790 const unsigned char *s = (SDATA (it->string)
7791 + IT_STRING_BYTEPOS (*it));
7792 it->c = string_char_and_length (s, &it->len);
7794 else
7796 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7797 it->len = 1;
7800 else
7802 /* Get the next character from a Lisp string that is not an
7803 overlay string. Such strings come from the mode line, for
7804 example. We may have to pad with spaces, or truncate the
7805 string. See also next_element_from_c_string. */
7806 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7808 it->what = IT_EOB;
7809 return 0;
7811 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7813 /* Pad with spaces. */
7814 it->c = ' ', it->len = 1;
7815 CHARPOS (position) = BYTEPOS (position) = -1;
7817 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7818 IT_STRING_BYTEPOS (*it),
7819 it->bidi_it.scan_dir < 0
7820 ? -1
7821 : it->string_nchars)
7822 && next_element_from_composition (it))
7824 return 1;
7826 else if (STRING_MULTIBYTE (it->string))
7828 const unsigned char *s = (SDATA (it->string)
7829 + IT_STRING_BYTEPOS (*it));
7830 it->c = string_char_and_length (s, &it->len);
7832 else
7834 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7835 it->len = 1;
7839 /* Record what we have and where it came from. */
7840 it->what = IT_CHARACTER;
7841 it->object = it->string;
7842 it->position = position;
7843 return 1;
7847 /* Load IT with next display element from C string IT->s.
7848 IT->string_nchars is the maximum number of characters to return
7849 from the string. IT->end_charpos may be greater than
7850 IT->string_nchars when this function is called, in which case we
7851 may have to return padding spaces. Value is zero if end of string
7852 reached, including padding spaces. */
7854 static int
7855 next_element_from_c_string (struct it *it)
7857 int success_p = 1;
7859 eassert (it->s);
7860 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7861 it->what = IT_CHARACTER;
7862 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7863 it->object = Qnil;
7865 /* With bidi reordering, the character to display might not be the
7866 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7867 we were reseated to a new string, whose paragraph direction is
7868 not known. */
7869 if (it->bidi_p && it->bidi_it.first_elt)
7870 get_visually_first_element (it);
7872 /* IT's position can be greater than IT->string_nchars in case a
7873 field width or precision has been specified when the iterator was
7874 initialized. */
7875 if (IT_CHARPOS (*it) >= it->end_charpos)
7877 /* End of the game. */
7878 it->what = IT_EOB;
7879 success_p = 0;
7881 else if (IT_CHARPOS (*it) >= it->string_nchars)
7883 /* Pad with spaces. */
7884 it->c = ' ', it->len = 1;
7885 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7887 else if (it->multibyte_p)
7888 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7889 else
7890 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7892 return success_p;
7896 /* Set up IT to return characters from an ellipsis, if appropriate.
7897 The definition of the ellipsis glyphs may come from a display table
7898 entry. This function fills IT with the first glyph from the
7899 ellipsis if an ellipsis is to be displayed. */
7901 static int
7902 next_element_from_ellipsis (struct it *it)
7904 if (it->selective_display_ellipsis_p)
7905 setup_for_ellipsis (it, it->len);
7906 else
7908 /* The face at the current position may be different from the
7909 face we find after the invisible text. Remember what it
7910 was in IT->saved_face_id, and signal that it's there by
7911 setting face_before_selective_p. */
7912 it->saved_face_id = it->face_id;
7913 it->method = GET_FROM_BUFFER;
7914 it->object = it->w->contents;
7915 reseat_at_next_visible_line_start (it, 1);
7916 it->face_before_selective_p = 1;
7919 return GET_NEXT_DISPLAY_ELEMENT (it);
7923 /* Deliver an image display element. The iterator IT is already
7924 filled with image information (done in handle_display_prop). Value
7925 is always 1. */
7928 static int
7929 next_element_from_image (struct it *it)
7931 it->what = IT_IMAGE;
7932 it->ignore_overlay_strings_at_pos_p = 0;
7933 return 1;
7936 #ifdef HAVE_XWIDGETS
7937 /* im not sure about this FIXME JAVE*/
7938 static int
7939 next_element_from_xwidget (struct it *it)
7941 it->what = IT_XWIDGET;
7942 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7943 //this is shaky because why do we set "what" if we dont set the other parts??
7944 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7945 return 1;
7947 #endif
7950 /* Fill iterator IT with next display element from a stretch glyph
7951 property. IT->object is the value of the text property. Value is
7952 always 1. */
7954 static int
7955 next_element_from_stretch (struct it *it)
7957 it->what = IT_STRETCH;
7958 return 1;
7961 /* Scan backwards from IT's current position until we find a stop
7962 position, or until BEGV. This is called when we find ourself
7963 before both the last known prev_stop and base_level_stop while
7964 reordering bidirectional text. */
7966 static void
7967 compute_stop_pos_backwards (struct it *it)
7969 const int SCAN_BACK_LIMIT = 1000;
7970 struct text_pos pos;
7971 struct display_pos save_current = it->current;
7972 struct text_pos save_position = it->position;
7973 ptrdiff_t charpos = IT_CHARPOS (*it);
7974 ptrdiff_t where_we_are = charpos;
7975 ptrdiff_t save_stop_pos = it->stop_charpos;
7976 ptrdiff_t save_end_pos = it->end_charpos;
7978 eassert (NILP (it->string) && !it->s);
7979 eassert (it->bidi_p);
7980 it->bidi_p = 0;
7983 it->end_charpos = min (charpos + 1, ZV);
7984 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7985 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7986 reseat_1 (it, pos, 0);
7987 compute_stop_pos (it);
7988 /* We must advance forward, right? */
7989 if (it->stop_charpos <= charpos)
7990 emacs_abort ();
7992 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7994 if (it->stop_charpos <= where_we_are)
7995 it->prev_stop = it->stop_charpos;
7996 else
7997 it->prev_stop = BEGV;
7998 it->bidi_p = 1;
7999 it->current = save_current;
8000 it->position = save_position;
8001 it->stop_charpos = save_stop_pos;
8002 it->end_charpos = save_end_pos;
8005 /* Scan forward from CHARPOS in the current buffer/string, until we
8006 find a stop position > current IT's position. Then handle the stop
8007 position before that. This is called when we bump into a stop
8008 position while reordering bidirectional text. CHARPOS should be
8009 the last previously processed stop_pos (or BEGV/0, if none were
8010 processed yet) whose position is less that IT's current
8011 position. */
8013 static void
8014 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8016 int bufp = !STRINGP (it->string);
8017 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8018 struct display_pos save_current = it->current;
8019 struct text_pos save_position = it->position;
8020 struct text_pos pos1;
8021 ptrdiff_t next_stop;
8023 /* Scan in strict logical order. */
8024 eassert (it->bidi_p);
8025 it->bidi_p = 0;
8028 it->prev_stop = charpos;
8029 if (bufp)
8031 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8032 reseat_1 (it, pos1, 0);
8034 else
8035 it->current.string_pos = string_pos (charpos, it->string);
8036 compute_stop_pos (it);
8037 /* We must advance forward, right? */
8038 if (it->stop_charpos <= it->prev_stop)
8039 emacs_abort ();
8040 charpos = it->stop_charpos;
8042 while (charpos <= where_we_are);
8044 it->bidi_p = 1;
8045 it->current = save_current;
8046 it->position = save_position;
8047 next_stop = it->stop_charpos;
8048 it->stop_charpos = it->prev_stop;
8049 handle_stop (it);
8050 it->stop_charpos = next_stop;
8053 /* Load IT with the next display element from current_buffer. Value
8054 is zero if end of buffer reached. IT->stop_charpos is the next
8055 position at which to stop and check for text properties or buffer
8056 end. */
8058 static int
8059 next_element_from_buffer (struct it *it)
8061 int success_p = 1;
8063 eassert (IT_CHARPOS (*it) >= BEGV);
8064 eassert (NILP (it->string) && !it->s);
8065 eassert (!it->bidi_p
8066 || (EQ (it->bidi_it.string.lstring, Qnil)
8067 && it->bidi_it.string.s == NULL));
8069 /* With bidi reordering, the character to display might not be the
8070 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8071 we were reseat()ed to a new buffer position, which is potentially
8072 a different paragraph. */
8073 if (it->bidi_p && it->bidi_it.first_elt)
8075 get_visually_first_element (it);
8076 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8079 if (IT_CHARPOS (*it) >= it->stop_charpos)
8081 if (IT_CHARPOS (*it) >= it->end_charpos)
8083 int overlay_strings_follow_p;
8085 /* End of the game, except when overlay strings follow that
8086 haven't been returned yet. */
8087 if (it->overlay_strings_at_end_processed_p)
8088 overlay_strings_follow_p = 0;
8089 else
8091 it->overlay_strings_at_end_processed_p = 1;
8092 overlay_strings_follow_p = get_overlay_strings (it, 0);
8095 if (overlay_strings_follow_p)
8096 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8097 else
8099 it->what = IT_EOB;
8100 it->position = it->current.pos;
8101 success_p = 0;
8104 else if (!(!it->bidi_p
8105 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8106 || IT_CHARPOS (*it) == it->stop_charpos))
8108 /* With bidi non-linear iteration, we could find ourselves
8109 far beyond the last computed stop_charpos, with several
8110 other stop positions in between that we missed. Scan
8111 them all now, in buffer's logical order, until we find
8112 and handle the last stop_charpos that precedes our
8113 current position. */
8114 handle_stop_backwards (it, it->stop_charpos);
8115 return GET_NEXT_DISPLAY_ELEMENT (it);
8117 else
8119 if (it->bidi_p)
8121 /* Take note of the stop position we just moved across,
8122 for when we will move back across it. */
8123 it->prev_stop = it->stop_charpos;
8124 /* If we are at base paragraph embedding level, take
8125 note of the last stop position seen at this
8126 level. */
8127 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8128 it->base_level_stop = it->stop_charpos;
8130 handle_stop (it);
8131 return GET_NEXT_DISPLAY_ELEMENT (it);
8134 else if (it->bidi_p
8135 /* If we are before prev_stop, we may have overstepped on
8136 our way backwards a stop_pos, and if so, we need to
8137 handle that stop_pos. */
8138 && IT_CHARPOS (*it) < it->prev_stop
8139 /* We can sometimes back up for reasons that have nothing
8140 to do with bidi reordering. E.g., compositions. The
8141 code below is only needed when we are above the base
8142 embedding level, so test for that explicitly. */
8143 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8145 if (it->base_level_stop <= 0
8146 || IT_CHARPOS (*it) < it->base_level_stop)
8148 /* If we lost track of base_level_stop, we need to find
8149 prev_stop by looking backwards. This happens, e.g., when
8150 we were reseated to the previous screenful of text by
8151 vertical-motion. */
8152 it->base_level_stop = BEGV;
8153 compute_stop_pos_backwards (it);
8154 handle_stop_backwards (it, it->prev_stop);
8156 else
8157 handle_stop_backwards (it, it->base_level_stop);
8158 return GET_NEXT_DISPLAY_ELEMENT (it);
8160 else
8162 /* No face changes, overlays etc. in sight, so just return a
8163 character from current_buffer. */
8164 unsigned char *p;
8165 ptrdiff_t stop;
8167 /* Maybe run the redisplay end trigger hook. Performance note:
8168 This doesn't seem to cost measurable time. */
8169 if (it->redisplay_end_trigger_charpos
8170 && it->glyph_row
8171 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8172 run_redisplay_end_trigger_hook (it);
8174 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8175 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8176 stop)
8177 && next_element_from_composition (it))
8179 return 1;
8182 /* Get the next character, maybe multibyte. */
8183 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8184 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8185 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8186 else
8187 it->c = *p, it->len = 1;
8189 /* Record what we have and where it came from. */
8190 it->what = IT_CHARACTER;
8191 it->object = it->w->contents;
8192 it->position = it->current.pos;
8194 /* Normally we return the character found above, except when we
8195 really want to return an ellipsis for selective display. */
8196 if (it->selective)
8198 if (it->c == '\n')
8200 /* A value of selective > 0 means hide lines indented more
8201 than that number of columns. */
8202 if (it->selective > 0
8203 && IT_CHARPOS (*it) + 1 < ZV
8204 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8205 IT_BYTEPOS (*it) + 1,
8206 it->selective))
8208 success_p = next_element_from_ellipsis (it);
8209 it->dpvec_char_len = -1;
8212 else if (it->c == '\r' && it->selective == -1)
8214 /* A value of selective == -1 means that everything from the
8215 CR to the end of the line is invisible, with maybe an
8216 ellipsis displayed for it. */
8217 success_p = next_element_from_ellipsis (it);
8218 it->dpvec_char_len = -1;
8223 /* Value is zero if end of buffer reached. */
8224 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8225 return success_p;
8229 /* Run the redisplay end trigger hook for IT. */
8231 static void
8232 run_redisplay_end_trigger_hook (struct it *it)
8234 Lisp_Object args[3];
8236 /* IT->glyph_row should be non-null, i.e. we should be actually
8237 displaying something, or otherwise we should not run the hook. */
8238 eassert (it->glyph_row);
8240 /* Set up hook arguments. */
8241 args[0] = Qredisplay_end_trigger_functions;
8242 args[1] = it->window;
8243 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8244 it->redisplay_end_trigger_charpos = 0;
8246 /* Since we are *trying* to run these functions, don't try to run
8247 them again, even if they get an error. */
8248 wset_redisplay_end_trigger (it->w, Qnil);
8249 Frun_hook_with_args (3, args);
8251 /* Notice if it changed the face of the character we are on. */
8252 handle_face_prop (it);
8256 /* Deliver a composition display element. Unlike the other
8257 next_element_from_XXX, this function is not registered in the array
8258 get_next_element[]. It is called from next_element_from_buffer and
8259 next_element_from_string when necessary. */
8261 static int
8262 next_element_from_composition (struct it *it)
8264 it->what = IT_COMPOSITION;
8265 it->len = it->cmp_it.nbytes;
8266 if (STRINGP (it->string))
8268 if (it->c < 0)
8270 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8271 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8272 return 0;
8274 it->position = it->current.string_pos;
8275 it->object = it->string;
8276 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8277 IT_STRING_BYTEPOS (*it), it->string);
8279 else
8281 if (it->c < 0)
8283 IT_CHARPOS (*it) += it->cmp_it.nchars;
8284 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8285 if (it->bidi_p)
8287 if (it->bidi_it.new_paragraph)
8288 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8289 /* Resync the bidi iterator with IT's new position.
8290 FIXME: this doesn't support bidirectional text. */
8291 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8292 bidi_move_to_visually_next (&it->bidi_it);
8294 return 0;
8296 it->position = it->current.pos;
8297 it->object = it->w->contents;
8298 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8299 IT_BYTEPOS (*it), Qnil);
8301 return 1;
8306 /***********************************************************************
8307 Moving an iterator without producing glyphs
8308 ***********************************************************************/
8310 /* Check if iterator is at a position corresponding to a valid buffer
8311 position after some move_it_ call. */
8313 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8314 ((it)->method == GET_FROM_STRING \
8315 ? IT_STRING_CHARPOS (*it) == 0 \
8316 : 1)
8319 /* Move iterator IT to a specified buffer or X position within one
8320 line on the display without producing glyphs.
8322 OP should be a bit mask including some or all of these bits:
8323 MOVE_TO_X: Stop upon reaching x-position TO_X.
8324 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8325 Regardless of OP's value, stop upon reaching the end of the display line.
8327 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8328 This means, in particular, that TO_X includes window's horizontal
8329 scroll amount.
8331 The return value has several possible values that
8332 say what condition caused the scan to stop:
8334 MOVE_POS_MATCH_OR_ZV
8335 - when TO_POS or ZV was reached.
8337 MOVE_X_REACHED
8338 -when TO_X was reached before TO_POS or ZV were reached.
8340 MOVE_LINE_CONTINUED
8341 - when we reached the end of the display area and the line must
8342 be continued.
8344 MOVE_LINE_TRUNCATED
8345 - when we reached the end of the display area and the line is
8346 truncated.
8348 MOVE_NEWLINE_OR_CR
8349 - when we stopped at a line end, i.e. a newline or a CR and selective
8350 display is on. */
8352 static enum move_it_result
8353 move_it_in_display_line_to (struct it *it,
8354 ptrdiff_t to_charpos, int to_x,
8355 enum move_operation_enum op)
8357 enum move_it_result result = MOVE_UNDEFINED;
8358 struct glyph_row *saved_glyph_row;
8359 struct it wrap_it, atpos_it, atx_it, ppos_it;
8360 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8361 void *ppos_data = NULL;
8362 int may_wrap = 0;
8363 enum it_method prev_method = it->method;
8364 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8365 int saw_smaller_pos = prev_pos < to_charpos;
8367 /* Don't produce glyphs in produce_glyphs. */
8368 saved_glyph_row = it->glyph_row;
8369 it->glyph_row = NULL;
8371 /* Use wrap_it to save a copy of IT wherever a word wrap could
8372 occur. Use atpos_it to save a copy of IT at the desired buffer
8373 position, if found, so that we can scan ahead and check if the
8374 word later overshoots the window edge. Use atx_it similarly, for
8375 pixel positions. */
8376 wrap_it.sp = -1;
8377 atpos_it.sp = -1;
8378 atx_it.sp = -1;
8380 /* Use ppos_it under bidi reordering to save a copy of IT for the
8381 position > CHARPOS that is the closest to CHARPOS. We restore
8382 that position in IT when we have scanned the entire display line
8383 without finding a match for CHARPOS and all the character
8384 positions are greater than CHARPOS. */
8385 if (it->bidi_p)
8387 SAVE_IT (ppos_it, *it, ppos_data);
8388 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8389 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8390 SAVE_IT (ppos_it, *it, ppos_data);
8393 #define BUFFER_POS_REACHED_P() \
8394 ((op & MOVE_TO_POS) != 0 \
8395 && BUFFERP (it->object) \
8396 && (IT_CHARPOS (*it) == to_charpos \
8397 || ((!it->bidi_p \
8398 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8399 && IT_CHARPOS (*it) > to_charpos) \
8400 || (it->what == IT_COMPOSITION \
8401 && ((IT_CHARPOS (*it) > to_charpos \
8402 && to_charpos >= it->cmp_it.charpos) \
8403 || (IT_CHARPOS (*it) < to_charpos \
8404 && to_charpos <= it->cmp_it.charpos)))) \
8405 && (it->method == GET_FROM_BUFFER \
8406 || (it->method == GET_FROM_DISPLAY_VECTOR \
8407 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8409 /* If there's a line-/wrap-prefix, handle it. */
8410 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8411 && it->current_y < it->last_visible_y)
8412 handle_line_prefix (it);
8414 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8415 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8417 while (1)
8419 int x, i, ascent = 0, descent = 0;
8421 /* Utility macro to reset an iterator with x, ascent, and descent. */
8422 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8423 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8424 (IT)->max_descent = descent)
8426 /* Stop if we move beyond TO_CHARPOS (after an image or a
8427 display string or stretch glyph). */
8428 if ((op & MOVE_TO_POS) != 0
8429 && BUFFERP (it->object)
8430 && it->method == GET_FROM_BUFFER
8431 && (((!it->bidi_p
8432 /* When the iterator is at base embedding level, we
8433 are guaranteed that characters are delivered for
8434 display in strictly increasing order of their
8435 buffer positions. */
8436 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8437 && IT_CHARPOS (*it) > to_charpos)
8438 || (it->bidi_p
8439 && (prev_method == GET_FROM_IMAGE
8440 || prev_method == GET_FROM_STRETCH
8441 || prev_method == GET_FROM_STRING)
8442 /* Passed TO_CHARPOS from left to right. */
8443 && ((prev_pos < to_charpos
8444 && IT_CHARPOS (*it) > to_charpos)
8445 /* Passed TO_CHARPOS from right to left. */
8446 || (prev_pos > to_charpos
8447 && IT_CHARPOS (*it) < to_charpos)))))
8449 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8451 result = MOVE_POS_MATCH_OR_ZV;
8452 break;
8454 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8455 /* If wrap_it is valid, the current position might be in a
8456 word that is wrapped. So, save the iterator in
8457 atpos_it and continue to see if wrapping happens. */
8458 SAVE_IT (atpos_it, *it, atpos_data);
8461 /* Stop when ZV reached.
8462 We used to stop here when TO_CHARPOS reached as well, but that is
8463 too soon if this glyph does not fit on this line. So we handle it
8464 explicitly below. */
8465 if (!get_next_display_element (it))
8467 result = MOVE_POS_MATCH_OR_ZV;
8468 break;
8471 if (it->line_wrap == TRUNCATE)
8473 if (BUFFER_POS_REACHED_P ())
8475 result = MOVE_POS_MATCH_OR_ZV;
8476 break;
8479 else
8481 if (it->line_wrap == WORD_WRAP)
8483 if (IT_DISPLAYING_WHITESPACE (it))
8484 may_wrap = 1;
8485 else if (may_wrap)
8487 /* We have reached a glyph that follows one or more
8488 whitespace characters. If the position is
8489 already found, we are done. */
8490 if (atpos_it.sp >= 0)
8492 RESTORE_IT (it, &atpos_it, atpos_data);
8493 result = MOVE_POS_MATCH_OR_ZV;
8494 goto done;
8496 if (atx_it.sp >= 0)
8498 RESTORE_IT (it, &atx_it, atx_data);
8499 result = MOVE_X_REACHED;
8500 goto done;
8502 /* Otherwise, we can wrap here. */
8503 SAVE_IT (wrap_it, *it, wrap_data);
8504 may_wrap = 0;
8509 /* Remember the line height for the current line, in case
8510 the next element doesn't fit on the line. */
8511 ascent = it->max_ascent;
8512 descent = it->max_descent;
8514 /* The call to produce_glyphs will get the metrics of the
8515 display element IT is loaded with. Record the x-position
8516 before this display element, in case it doesn't fit on the
8517 line. */
8518 x = it->current_x;
8520 PRODUCE_GLYPHS (it);
8522 if (it->area != TEXT_AREA)
8524 prev_method = it->method;
8525 if (it->method == GET_FROM_BUFFER)
8526 prev_pos = IT_CHARPOS (*it);
8527 set_iterator_to_next (it, 1);
8528 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8529 SET_TEXT_POS (this_line_min_pos,
8530 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8531 if (it->bidi_p
8532 && (op & MOVE_TO_POS)
8533 && IT_CHARPOS (*it) > to_charpos
8534 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8535 SAVE_IT (ppos_it, *it, ppos_data);
8536 continue;
8539 /* The number of glyphs we get back in IT->nglyphs will normally
8540 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8541 character on a terminal frame, or (iii) a line end. For the
8542 second case, IT->nglyphs - 1 padding glyphs will be present.
8543 (On X frames, there is only one glyph produced for a
8544 composite character.)
8546 The behavior implemented below means, for continuation lines,
8547 that as many spaces of a TAB as fit on the current line are
8548 displayed there. For terminal frames, as many glyphs of a
8549 multi-glyph character are displayed in the current line, too.
8550 This is what the old redisplay code did, and we keep it that
8551 way. Under X, the whole shape of a complex character must
8552 fit on the line or it will be completely displayed in the
8553 next line.
8555 Note that both for tabs and padding glyphs, all glyphs have
8556 the same width. */
8557 if (it->nglyphs)
8559 /* More than one glyph or glyph doesn't fit on line. All
8560 glyphs have the same width. */
8561 int single_glyph_width = it->pixel_width / it->nglyphs;
8562 int new_x;
8563 int x_before_this_char = x;
8564 int hpos_before_this_char = it->hpos;
8566 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8568 new_x = x + single_glyph_width;
8570 /* We want to leave anything reaching TO_X to the caller. */
8571 if ((op & MOVE_TO_X) && new_x > to_x)
8573 if (BUFFER_POS_REACHED_P ())
8575 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8576 goto buffer_pos_reached;
8577 if (atpos_it.sp < 0)
8579 SAVE_IT (atpos_it, *it, atpos_data);
8580 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8583 else
8585 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8587 it->current_x = x;
8588 result = MOVE_X_REACHED;
8589 break;
8591 if (atx_it.sp < 0)
8593 SAVE_IT (atx_it, *it, atx_data);
8594 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8599 if (/* Lines are continued. */
8600 it->line_wrap != TRUNCATE
8601 && (/* And glyph doesn't fit on the line. */
8602 new_x > it->last_visible_x
8603 /* Or it fits exactly and we're on a window
8604 system frame. */
8605 || (new_x == it->last_visible_x
8606 && FRAME_WINDOW_P (it->f)
8607 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8608 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8609 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8611 if (/* IT->hpos == 0 means the very first glyph
8612 doesn't fit on the line, e.g. a wide image. */
8613 it->hpos == 0
8614 || (new_x == it->last_visible_x
8615 && FRAME_WINDOW_P (it->f)))
8617 ++it->hpos;
8618 it->current_x = new_x;
8620 /* The character's last glyph just barely fits
8621 in this row. */
8622 if (i == it->nglyphs - 1)
8624 /* If this is the destination position,
8625 return a position *before* it in this row,
8626 now that we know it fits in this row. */
8627 if (BUFFER_POS_REACHED_P ())
8629 if (it->line_wrap != WORD_WRAP
8630 || wrap_it.sp < 0)
8632 it->hpos = hpos_before_this_char;
8633 it->current_x = x_before_this_char;
8634 result = MOVE_POS_MATCH_OR_ZV;
8635 break;
8637 if (it->line_wrap == WORD_WRAP
8638 && atpos_it.sp < 0)
8640 SAVE_IT (atpos_it, *it, atpos_data);
8641 atpos_it.current_x = x_before_this_char;
8642 atpos_it.hpos = hpos_before_this_char;
8646 prev_method = it->method;
8647 if (it->method == GET_FROM_BUFFER)
8648 prev_pos = IT_CHARPOS (*it);
8649 set_iterator_to_next (it, 1);
8650 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8651 SET_TEXT_POS (this_line_min_pos,
8652 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8653 /* On graphical terminals, newlines may
8654 "overflow" into the fringe if
8655 overflow-newline-into-fringe is non-nil.
8656 On text terminals, and on graphical
8657 terminals with no right margin, newlines
8658 may overflow into the last glyph on the
8659 display line.*/
8660 if (!FRAME_WINDOW_P (it->f)
8661 || ((it->bidi_p
8662 && it->bidi_it.paragraph_dir == R2L)
8663 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8664 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8665 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8667 if (!get_next_display_element (it))
8669 result = MOVE_POS_MATCH_OR_ZV;
8670 break;
8672 if (BUFFER_POS_REACHED_P ())
8674 if (ITERATOR_AT_END_OF_LINE_P (it))
8675 result = MOVE_POS_MATCH_OR_ZV;
8676 else
8677 result = MOVE_LINE_CONTINUED;
8678 break;
8680 if (ITERATOR_AT_END_OF_LINE_P (it)
8681 && (it->line_wrap != WORD_WRAP
8682 || wrap_it.sp < 0))
8684 result = MOVE_NEWLINE_OR_CR;
8685 break;
8690 else
8691 IT_RESET_X_ASCENT_DESCENT (it);
8693 if (wrap_it.sp >= 0)
8695 RESTORE_IT (it, &wrap_it, wrap_data);
8696 atpos_it.sp = -1;
8697 atx_it.sp = -1;
8700 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8701 IT_CHARPOS (*it)));
8702 result = MOVE_LINE_CONTINUED;
8703 break;
8706 if (BUFFER_POS_REACHED_P ())
8708 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8709 goto buffer_pos_reached;
8710 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8712 SAVE_IT (atpos_it, *it, atpos_data);
8713 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8717 if (new_x > it->first_visible_x)
8719 /* Glyph is visible. Increment number of glyphs that
8720 would be displayed. */
8721 ++it->hpos;
8725 if (result != MOVE_UNDEFINED)
8726 break;
8728 else if (BUFFER_POS_REACHED_P ())
8730 buffer_pos_reached:
8731 IT_RESET_X_ASCENT_DESCENT (it);
8732 result = MOVE_POS_MATCH_OR_ZV;
8733 break;
8735 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8737 /* Stop when TO_X specified and reached. This check is
8738 necessary here because of lines consisting of a line end,
8739 only. The line end will not produce any glyphs and we
8740 would never get MOVE_X_REACHED. */
8741 eassert (it->nglyphs == 0);
8742 result = MOVE_X_REACHED;
8743 break;
8746 /* Is this a line end? If yes, we're done. */
8747 if (ITERATOR_AT_END_OF_LINE_P (it))
8749 /* If we are past TO_CHARPOS, but never saw any character
8750 positions smaller than TO_CHARPOS, return
8751 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8752 did. */
8753 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8755 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8757 if (IT_CHARPOS (ppos_it) < ZV)
8759 RESTORE_IT (it, &ppos_it, ppos_data);
8760 result = MOVE_POS_MATCH_OR_ZV;
8762 else
8763 goto buffer_pos_reached;
8765 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8766 && IT_CHARPOS (*it) > to_charpos)
8767 goto buffer_pos_reached;
8768 else
8769 result = MOVE_NEWLINE_OR_CR;
8771 else
8772 result = MOVE_NEWLINE_OR_CR;
8773 break;
8776 prev_method = it->method;
8777 if (it->method == GET_FROM_BUFFER)
8778 prev_pos = IT_CHARPOS (*it);
8779 /* The current display element has been consumed. Advance
8780 to the next. */
8781 set_iterator_to_next (it, 1);
8782 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8783 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8784 if (IT_CHARPOS (*it) < to_charpos)
8785 saw_smaller_pos = 1;
8786 if (it->bidi_p
8787 && (op & MOVE_TO_POS)
8788 && IT_CHARPOS (*it) >= to_charpos
8789 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8790 SAVE_IT (ppos_it, *it, ppos_data);
8792 /* Stop if lines are truncated and IT's current x-position is
8793 past the right edge of the window now. */
8794 if (it->line_wrap == TRUNCATE
8795 && it->current_x >= it->last_visible_x)
8797 if (!FRAME_WINDOW_P (it->f)
8798 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8799 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8800 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8801 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8803 int at_eob_p = 0;
8805 if ((at_eob_p = !get_next_display_element (it))
8806 || BUFFER_POS_REACHED_P ()
8807 /* If we are past TO_CHARPOS, but never saw any
8808 character positions smaller than TO_CHARPOS,
8809 return MOVE_POS_MATCH_OR_ZV, like the
8810 unidirectional display did. */
8811 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8812 && !saw_smaller_pos
8813 && IT_CHARPOS (*it) > to_charpos))
8815 if (it->bidi_p
8816 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8817 RESTORE_IT (it, &ppos_it, ppos_data);
8818 result = MOVE_POS_MATCH_OR_ZV;
8819 break;
8821 if (ITERATOR_AT_END_OF_LINE_P (it))
8823 result = MOVE_NEWLINE_OR_CR;
8824 break;
8827 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8828 && !saw_smaller_pos
8829 && IT_CHARPOS (*it) > to_charpos)
8831 if (IT_CHARPOS (ppos_it) < ZV)
8832 RESTORE_IT (it, &ppos_it, ppos_data);
8833 result = MOVE_POS_MATCH_OR_ZV;
8834 break;
8836 result = MOVE_LINE_TRUNCATED;
8837 break;
8839 #undef IT_RESET_X_ASCENT_DESCENT
8842 #undef BUFFER_POS_REACHED_P
8844 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8845 restore the saved iterator. */
8846 if (atpos_it.sp >= 0)
8847 RESTORE_IT (it, &atpos_it, atpos_data);
8848 else if (atx_it.sp >= 0)
8849 RESTORE_IT (it, &atx_it, atx_data);
8851 done:
8853 if (atpos_data)
8854 bidi_unshelve_cache (atpos_data, 1);
8855 if (atx_data)
8856 bidi_unshelve_cache (atx_data, 1);
8857 if (wrap_data)
8858 bidi_unshelve_cache (wrap_data, 1);
8859 if (ppos_data)
8860 bidi_unshelve_cache (ppos_data, 1);
8862 /* Restore the iterator settings altered at the beginning of this
8863 function. */
8864 it->glyph_row = saved_glyph_row;
8865 return result;
8868 /* For external use. */
8869 void
8870 move_it_in_display_line (struct it *it,
8871 ptrdiff_t to_charpos, int to_x,
8872 enum move_operation_enum op)
8874 if (it->line_wrap == WORD_WRAP
8875 && (op & MOVE_TO_X))
8877 struct it save_it;
8878 void *save_data = NULL;
8879 int skip;
8881 SAVE_IT (save_it, *it, save_data);
8882 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8883 /* When word-wrap is on, TO_X may lie past the end
8884 of a wrapped line. Then it->current is the
8885 character on the next line, so backtrack to the
8886 space before the wrap point. */
8887 if (skip == MOVE_LINE_CONTINUED)
8889 int prev_x = max (it->current_x - 1, 0);
8890 RESTORE_IT (it, &save_it, save_data);
8891 move_it_in_display_line_to
8892 (it, -1, prev_x, MOVE_TO_X);
8894 else
8895 bidi_unshelve_cache (save_data, 1);
8897 else
8898 move_it_in_display_line_to (it, to_charpos, to_x, op);
8902 /* Move IT forward until it satisfies one or more of the criteria in
8903 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8905 OP is a bit-mask that specifies where to stop, and in particular,
8906 which of those four position arguments makes a difference. See the
8907 description of enum move_operation_enum.
8909 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8910 screen line, this function will set IT to the next position that is
8911 displayed to the right of TO_CHARPOS on the screen. */
8913 void
8914 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8916 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8917 int line_height, line_start_x = 0, reached = 0;
8918 void *backup_data = NULL;
8920 for (;;)
8922 if (op & MOVE_TO_VPOS)
8924 /* If no TO_CHARPOS and no TO_X specified, stop at the
8925 start of the line TO_VPOS. */
8926 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8928 if (it->vpos == to_vpos)
8930 reached = 1;
8931 break;
8933 else
8934 skip = move_it_in_display_line_to (it, -1, -1, 0);
8936 else
8938 /* TO_VPOS >= 0 means stop at TO_X in the line at
8939 TO_VPOS, or at TO_POS, whichever comes first. */
8940 if (it->vpos == to_vpos)
8942 reached = 2;
8943 break;
8946 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8948 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8950 reached = 3;
8951 break;
8953 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8955 /* We have reached TO_X but not in the line we want. */
8956 skip = move_it_in_display_line_to (it, to_charpos,
8957 -1, MOVE_TO_POS);
8958 if (skip == MOVE_POS_MATCH_OR_ZV)
8960 reached = 4;
8961 break;
8966 else if (op & MOVE_TO_Y)
8968 struct it it_backup;
8970 if (it->line_wrap == WORD_WRAP)
8971 SAVE_IT (it_backup, *it, backup_data);
8973 /* TO_Y specified means stop at TO_X in the line containing
8974 TO_Y---or at TO_CHARPOS if this is reached first. The
8975 problem is that we can't really tell whether the line
8976 contains TO_Y before we have completely scanned it, and
8977 this may skip past TO_X. What we do is to first scan to
8978 TO_X.
8980 If TO_X is not specified, use a TO_X of zero. The reason
8981 is to make the outcome of this function more predictable.
8982 If we didn't use TO_X == 0, we would stop at the end of
8983 the line which is probably not what a caller would expect
8984 to happen. */
8985 skip = move_it_in_display_line_to
8986 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8987 (MOVE_TO_X | (op & MOVE_TO_POS)));
8989 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8990 if (skip == MOVE_POS_MATCH_OR_ZV)
8991 reached = 5;
8992 else if (skip == MOVE_X_REACHED)
8994 /* If TO_X was reached, we want to know whether TO_Y is
8995 in the line. We know this is the case if the already
8996 scanned glyphs make the line tall enough. Otherwise,
8997 we must check by scanning the rest of the line. */
8998 line_height = it->max_ascent + it->max_descent;
8999 if (to_y >= it->current_y
9000 && to_y < it->current_y + line_height)
9002 reached = 6;
9003 break;
9005 SAVE_IT (it_backup, *it, backup_data);
9006 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9007 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9008 op & MOVE_TO_POS);
9009 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9010 line_height = it->max_ascent + it->max_descent;
9011 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9013 if (to_y >= it->current_y
9014 && to_y < it->current_y + line_height)
9016 /* If TO_Y is in this line and TO_X was reached
9017 above, we scanned too far. We have to restore
9018 IT's settings to the ones before skipping. But
9019 keep the more accurate values of max_ascent and
9020 max_descent we've found while skipping the rest
9021 of the line, for the sake of callers, such as
9022 pos_visible_p, that need to know the line
9023 height. */
9024 int max_ascent = it->max_ascent;
9025 int max_descent = it->max_descent;
9027 RESTORE_IT (it, &it_backup, backup_data);
9028 it->max_ascent = max_ascent;
9029 it->max_descent = max_descent;
9030 reached = 6;
9032 else
9034 skip = skip2;
9035 if (skip == MOVE_POS_MATCH_OR_ZV)
9036 reached = 7;
9039 else
9041 /* Check whether TO_Y is in this line. */
9042 line_height = it->max_ascent + it->max_descent;
9043 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9045 if (to_y >= it->current_y
9046 && to_y < it->current_y + line_height)
9048 /* When word-wrap is on, TO_X may lie past the end
9049 of a wrapped line. Then it->current is the
9050 character on the next line, so backtrack to the
9051 space before the wrap point. */
9052 if (skip == MOVE_LINE_CONTINUED
9053 && it->line_wrap == WORD_WRAP)
9055 int prev_x = max (it->current_x - 1, 0);
9056 RESTORE_IT (it, &it_backup, backup_data);
9057 skip = move_it_in_display_line_to
9058 (it, -1, prev_x, MOVE_TO_X);
9060 reached = 6;
9064 if (reached)
9065 break;
9067 else if (BUFFERP (it->object)
9068 && (it->method == GET_FROM_BUFFER
9069 || it->method == GET_FROM_STRETCH)
9070 && IT_CHARPOS (*it) >= to_charpos
9071 /* Under bidi iteration, a call to set_iterator_to_next
9072 can scan far beyond to_charpos if the initial
9073 portion of the next line needs to be reordered. In
9074 that case, give move_it_in_display_line_to another
9075 chance below. */
9076 && !(it->bidi_p
9077 && it->bidi_it.scan_dir == -1))
9078 skip = MOVE_POS_MATCH_OR_ZV;
9079 else
9080 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9082 switch (skip)
9084 case MOVE_POS_MATCH_OR_ZV:
9085 reached = 8;
9086 goto out;
9088 case MOVE_NEWLINE_OR_CR:
9089 set_iterator_to_next (it, 1);
9090 it->continuation_lines_width = 0;
9091 break;
9093 case MOVE_LINE_TRUNCATED:
9094 it->continuation_lines_width = 0;
9095 reseat_at_next_visible_line_start (it, 0);
9096 if ((op & MOVE_TO_POS) != 0
9097 && IT_CHARPOS (*it) > to_charpos)
9099 reached = 9;
9100 goto out;
9102 break;
9104 case MOVE_LINE_CONTINUED:
9105 /* For continued lines ending in a tab, some of the glyphs
9106 associated with the tab are displayed on the current
9107 line. Since it->current_x does not include these glyphs,
9108 we use it->last_visible_x instead. */
9109 if (it->c == '\t')
9111 it->continuation_lines_width += it->last_visible_x;
9112 /* When moving by vpos, ensure that the iterator really
9113 advances to the next line (bug#847, bug#969). Fixme:
9114 do we need to do this in other circumstances? */
9115 if (it->current_x != it->last_visible_x
9116 && (op & MOVE_TO_VPOS)
9117 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9119 line_start_x = it->current_x + it->pixel_width
9120 - it->last_visible_x;
9121 set_iterator_to_next (it, 0);
9124 else
9125 it->continuation_lines_width += it->current_x;
9126 break;
9128 default:
9129 emacs_abort ();
9132 /* Reset/increment for the next run. */
9133 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9134 it->current_x = line_start_x;
9135 line_start_x = 0;
9136 it->hpos = 0;
9137 it->current_y += it->max_ascent + it->max_descent;
9138 ++it->vpos;
9139 last_height = it->max_ascent + it->max_descent;
9140 it->max_ascent = it->max_descent = 0;
9143 out:
9145 /* On text terminals, we may stop at the end of a line in the middle
9146 of a multi-character glyph. If the glyph itself is continued,
9147 i.e. it is actually displayed on the next line, don't treat this
9148 stopping point as valid; move to the next line instead (unless
9149 that brings us offscreen). */
9150 if (!FRAME_WINDOW_P (it->f)
9151 && op & MOVE_TO_POS
9152 && IT_CHARPOS (*it) == to_charpos
9153 && it->what == IT_CHARACTER
9154 && it->nglyphs > 1
9155 && it->line_wrap == WINDOW_WRAP
9156 && it->current_x == it->last_visible_x - 1
9157 && it->c != '\n'
9158 && it->c != '\t'
9159 && it->vpos < it->w->window_end_vpos)
9161 it->continuation_lines_width += it->current_x;
9162 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9163 it->current_y += it->max_ascent + it->max_descent;
9164 ++it->vpos;
9165 last_height = it->max_ascent + it->max_descent;
9168 if (backup_data)
9169 bidi_unshelve_cache (backup_data, 1);
9171 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9175 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9177 If DY > 0, move IT backward at least that many pixels. DY = 0
9178 means move IT backward to the preceding line start or BEGV. This
9179 function may move over more than DY pixels if IT->current_y - DY
9180 ends up in the middle of a line; in this case IT->current_y will be
9181 set to the top of the line moved to. */
9183 void
9184 move_it_vertically_backward (struct it *it, int dy)
9186 int nlines, h;
9187 struct it it2, it3;
9188 void *it2data = NULL, *it3data = NULL;
9189 ptrdiff_t start_pos;
9190 int nchars_per_row
9191 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9192 ptrdiff_t pos_limit;
9194 move_further_back:
9195 eassert (dy >= 0);
9197 start_pos = IT_CHARPOS (*it);
9199 /* Estimate how many newlines we must move back. */
9200 nlines = max (1, dy / default_line_pixel_height (it->w));
9201 if (it->line_wrap == TRUNCATE)
9202 pos_limit = BEGV;
9203 else
9204 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9206 /* Set the iterator's position that many lines back. But don't go
9207 back more than NLINES full screen lines -- this wins a day with
9208 buffers which have very long lines. */
9209 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9210 back_to_previous_visible_line_start (it);
9212 /* Reseat the iterator here. When moving backward, we don't want
9213 reseat to skip forward over invisible text, set up the iterator
9214 to deliver from overlay strings at the new position etc. So,
9215 use reseat_1 here. */
9216 reseat_1 (it, it->current.pos, 1);
9218 /* We are now surely at a line start. */
9219 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9220 reordering is in effect. */
9221 it->continuation_lines_width = 0;
9223 /* Move forward and see what y-distance we moved. First move to the
9224 start of the next line so that we get its height. We need this
9225 height to be able to tell whether we reached the specified
9226 y-distance. */
9227 SAVE_IT (it2, *it, it2data);
9228 it2.max_ascent = it2.max_descent = 0;
9231 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9232 MOVE_TO_POS | MOVE_TO_VPOS);
9234 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9235 /* If we are in a display string which starts at START_POS,
9236 and that display string includes a newline, and we are
9237 right after that newline (i.e. at the beginning of a
9238 display line), exit the loop, because otherwise we will
9239 infloop, since move_it_to will see that it is already at
9240 START_POS and will not move. */
9241 || (it2.method == GET_FROM_STRING
9242 && IT_CHARPOS (it2) == start_pos
9243 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9244 eassert (IT_CHARPOS (*it) >= BEGV);
9245 SAVE_IT (it3, it2, it3data);
9247 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9248 eassert (IT_CHARPOS (*it) >= BEGV);
9249 /* H is the actual vertical distance from the position in *IT
9250 and the starting position. */
9251 h = it2.current_y - it->current_y;
9252 /* NLINES is the distance in number of lines. */
9253 nlines = it2.vpos - it->vpos;
9255 /* Correct IT's y and vpos position
9256 so that they are relative to the starting point. */
9257 it->vpos -= nlines;
9258 it->current_y -= h;
9260 if (dy == 0)
9262 /* DY == 0 means move to the start of the screen line. The
9263 value of nlines is > 0 if continuation lines were involved,
9264 or if the original IT position was at start of a line. */
9265 RESTORE_IT (it, it, it2data);
9266 if (nlines > 0)
9267 move_it_by_lines (it, nlines);
9268 /* The above code moves us to some position NLINES down,
9269 usually to its first glyph (leftmost in an L2R line), but
9270 that's not necessarily the start of the line, under bidi
9271 reordering. We want to get to the character position
9272 that is immediately after the newline of the previous
9273 line. */
9274 if (it->bidi_p
9275 && !it->continuation_lines_width
9276 && !STRINGP (it->string)
9277 && IT_CHARPOS (*it) > BEGV
9278 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9280 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9282 DEC_BOTH (cp, bp);
9283 cp = find_newline_no_quit (cp, bp, -1, NULL);
9284 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9286 bidi_unshelve_cache (it3data, 1);
9288 else
9290 /* The y-position we try to reach, relative to *IT.
9291 Note that H has been subtracted in front of the if-statement. */
9292 int target_y = it->current_y + h - dy;
9293 int y0 = it3.current_y;
9294 int y1;
9295 int line_height;
9297 RESTORE_IT (&it3, &it3, it3data);
9298 y1 = line_bottom_y (&it3);
9299 line_height = y1 - y0;
9300 RESTORE_IT (it, it, it2data);
9301 /* If we did not reach target_y, try to move further backward if
9302 we can. If we moved too far backward, try to move forward. */
9303 if (target_y < it->current_y
9304 /* This is heuristic. In a window that's 3 lines high, with
9305 a line height of 13 pixels each, recentering with point
9306 on the bottom line will try to move -39/2 = 19 pixels
9307 backward. Try to avoid moving into the first line. */
9308 && (it->current_y - target_y
9309 > min (window_box_height (it->w), line_height * 2 / 3))
9310 && IT_CHARPOS (*it) > BEGV)
9312 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9313 target_y - it->current_y));
9314 dy = it->current_y - target_y;
9315 goto move_further_back;
9317 else if (target_y >= it->current_y + line_height
9318 && IT_CHARPOS (*it) < ZV)
9320 /* Should move forward by at least one line, maybe more.
9322 Note: Calling move_it_by_lines can be expensive on
9323 terminal frames, where compute_motion is used (via
9324 vmotion) to do the job, when there are very long lines
9325 and truncate-lines is nil. That's the reason for
9326 treating terminal frames specially here. */
9328 if (!FRAME_WINDOW_P (it->f))
9329 move_it_vertically (it, target_y - (it->current_y + line_height));
9330 else
9334 move_it_by_lines (it, 1);
9336 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9343 /* Move IT by a specified amount of pixel lines DY. DY negative means
9344 move backwards. DY = 0 means move to start of screen line. At the
9345 end, IT will be on the start of a screen line. */
9347 void
9348 move_it_vertically (struct it *it, int dy)
9350 if (dy <= 0)
9351 move_it_vertically_backward (it, -dy);
9352 else
9354 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9355 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9356 MOVE_TO_POS | MOVE_TO_Y);
9357 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9359 /* If buffer ends in ZV without a newline, move to the start of
9360 the line to satisfy the post-condition. */
9361 if (IT_CHARPOS (*it) == ZV
9362 && ZV > BEGV
9363 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9364 move_it_by_lines (it, 0);
9369 /* Move iterator IT past the end of the text line it is in. */
9371 void
9372 move_it_past_eol (struct it *it)
9374 enum move_it_result rc;
9376 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9377 if (rc == MOVE_NEWLINE_OR_CR)
9378 set_iterator_to_next (it, 0);
9382 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9383 negative means move up. DVPOS == 0 means move to the start of the
9384 screen line.
9386 Optimization idea: If we would know that IT->f doesn't use
9387 a face with proportional font, we could be faster for
9388 truncate-lines nil. */
9390 void
9391 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9394 /* The commented-out optimization uses vmotion on terminals. This
9395 gives bad results, because elements like it->what, on which
9396 callers such as pos_visible_p rely, aren't updated. */
9397 /* struct position pos;
9398 if (!FRAME_WINDOW_P (it->f))
9400 struct text_pos textpos;
9402 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9403 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9404 reseat (it, textpos, 1);
9405 it->vpos += pos.vpos;
9406 it->current_y += pos.vpos;
9408 else */
9410 if (dvpos == 0)
9412 /* DVPOS == 0 means move to the start of the screen line. */
9413 move_it_vertically_backward (it, 0);
9414 /* Let next call to line_bottom_y calculate real line height */
9415 last_height = 0;
9417 else if (dvpos > 0)
9419 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9420 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9422 /* Only move to the next buffer position if we ended up in a
9423 string from display property, not in an overlay string
9424 (before-string or after-string). That is because the
9425 latter don't conceal the underlying buffer position, so
9426 we can ask to move the iterator to the exact position we
9427 are interested in. Note that, even if we are already at
9428 IT_CHARPOS (*it), the call below is not a no-op, as it
9429 will detect that we are at the end of the string, pop the
9430 iterator, and compute it->current_x and it->hpos
9431 correctly. */
9432 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9433 -1, -1, -1, MOVE_TO_POS);
9436 else
9438 struct it it2;
9439 void *it2data = NULL;
9440 ptrdiff_t start_charpos, i;
9441 int nchars_per_row
9442 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9443 ptrdiff_t pos_limit;
9445 /* Start at the beginning of the screen line containing IT's
9446 position. This may actually move vertically backwards,
9447 in case of overlays, so adjust dvpos accordingly. */
9448 dvpos += it->vpos;
9449 move_it_vertically_backward (it, 0);
9450 dvpos -= it->vpos;
9452 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9453 screen lines, and reseat the iterator there. */
9454 start_charpos = IT_CHARPOS (*it);
9455 if (it->line_wrap == TRUNCATE)
9456 pos_limit = BEGV;
9457 else
9458 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9459 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9460 back_to_previous_visible_line_start (it);
9461 reseat (it, it->current.pos, 1);
9463 /* Move further back if we end up in a string or an image. */
9464 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9466 /* First try to move to start of display line. */
9467 dvpos += it->vpos;
9468 move_it_vertically_backward (it, 0);
9469 dvpos -= it->vpos;
9470 if (IT_POS_VALID_AFTER_MOVE_P (it))
9471 break;
9472 /* If start of line is still in string or image,
9473 move further back. */
9474 back_to_previous_visible_line_start (it);
9475 reseat (it, it->current.pos, 1);
9476 dvpos--;
9479 it->current_x = it->hpos = 0;
9481 /* Above call may have moved too far if continuation lines
9482 are involved. Scan forward and see if it did. */
9483 SAVE_IT (it2, *it, it2data);
9484 it2.vpos = it2.current_y = 0;
9485 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9486 it->vpos -= it2.vpos;
9487 it->current_y -= it2.current_y;
9488 it->current_x = it->hpos = 0;
9490 /* If we moved too far back, move IT some lines forward. */
9491 if (it2.vpos > -dvpos)
9493 int delta = it2.vpos + dvpos;
9495 RESTORE_IT (&it2, &it2, it2data);
9496 SAVE_IT (it2, *it, it2data);
9497 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9498 /* Move back again if we got too far ahead. */
9499 if (IT_CHARPOS (*it) >= start_charpos)
9500 RESTORE_IT (it, &it2, it2data);
9501 else
9502 bidi_unshelve_cache (it2data, 1);
9504 else
9505 RESTORE_IT (it, it, it2data);
9509 /* Return 1 if IT points into the middle of a display vector. */
9512 in_display_vector_p (struct it *it)
9514 return (it->method == GET_FROM_DISPLAY_VECTOR
9515 && it->current.dpvec_index > 0
9516 && it->dpvec + it->current.dpvec_index != it->dpend);
9520 /***********************************************************************
9521 Messages
9522 ***********************************************************************/
9525 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9526 to *Messages*. */
9528 void
9529 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9531 Lisp_Object args[3];
9532 Lisp_Object msg, fmt;
9533 char *buffer;
9534 ptrdiff_t len;
9535 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9536 USE_SAFE_ALLOCA;
9538 fmt = msg = Qnil;
9539 GCPRO4 (fmt, msg, arg1, arg2);
9541 args[0] = fmt = build_string (format);
9542 args[1] = arg1;
9543 args[2] = arg2;
9544 msg = Fformat (3, args);
9546 len = SBYTES (msg) + 1;
9547 buffer = SAFE_ALLOCA (len);
9548 memcpy (buffer, SDATA (msg), len);
9550 message_dolog (buffer, len - 1, 1, 0);
9551 SAFE_FREE ();
9553 UNGCPRO;
9557 /* Output a newline in the *Messages* buffer if "needs" one. */
9559 void
9560 message_log_maybe_newline (void)
9562 if (message_log_need_newline)
9563 message_dolog ("", 0, 1, 0);
9567 /* Add a string M of length NBYTES to the message log, optionally
9568 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9569 true, means interpret the contents of M as multibyte. This
9570 function calls low-level routines in order to bypass text property
9571 hooks, etc. which might not be safe to run.
9573 This may GC (insert may run before/after change hooks),
9574 so the buffer M must NOT point to a Lisp string. */
9576 void
9577 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9579 const unsigned char *msg = (const unsigned char *) m;
9581 if (!NILP (Vmemory_full))
9582 return;
9584 if (!NILP (Vmessage_log_max))
9586 struct buffer *oldbuf;
9587 Lisp_Object oldpoint, oldbegv, oldzv;
9588 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9589 ptrdiff_t point_at_end = 0;
9590 ptrdiff_t zv_at_end = 0;
9591 Lisp_Object old_deactivate_mark;
9592 bool shown;
9593 struct gcpro gcpro1;
9595 old_deactivate_mark = Vdeactivate_mark;
9596 oldbuf = current_buffer;
9597 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9598 bset_undo_list (current_buffer, Qt);
9600 oldpoint = message_dolog_marker1;
9601 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9602 oldbegv = message_dolog_marker2;
9603 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9604 oldzv = message_dolog_marker3;
9605 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9606 GCPRO1 (old_deactivate_mark);
9608 if (PT == Z)
9609 point_at_end = 1;
9610 if (ZV == Z)
9611 zv_at_end = 1;
9613 BEGV = BEG;
9614 BEGV_BYTE = BEG_BYTE;
9615 ZV = Z;
9616 ZV_BYTE = Z_BYTE;
9617 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9619 /* Insert the string--maybe converting multibyte to single byte
9620 or vice versa, so that all the text fits the buffer. */
9621 if (multibyte
9622 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9624 ptrdiff_t i;
9625 int c, char_bytes;
9626 char work[1];
9628 /* Convert a multibyte string to single-byte
9629 for the *Message* buffer. */
9630 for (i = 0; i < nbytes; i += char_bytes)
9632 c = string_char_and_length (msg + i, &char_bytes);
9633 work[0] = (ASCII_CHAR_P (c)
9635 : multibyte_char_to_unibyte (c));
9636 insert_1_both (work, 1, 1, 1, 0, 0);
9639 else if (! multibyte
9640 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9642 ptrdiff_t i;
9643 int c, char_bytes;
9644 unsigned char str[MAX_MULTIBYTE_LENGTH];
9645 /* Convert a single-byte string to multibyte
9646 for the *Message* buffer. */
9647 for (i = 0; i < nbytes; i++)
9649 c = msg[i];
9650 MAKE_CHAR_MULTIBYTE (c);
9651 char_bytes = CHAR_STRING (c, str);
9652 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9655 else if (nbytes)
9656 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9658 if (nlflag)
9660 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9661 printmax_t dups;
9663 insert_1_both ("\n", 1, 1, 1, 0, 0);
9665 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9666 this_bol = PT;
9667 this_bol_byte = PT_BYTE;
9669 /* See if this line duplicates the previous one.
9670 If so, combine duplicates. */
9671 if (this_bol > BEG)
9673 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9674 prev_bol = PT;
9675 prev_bol_byte = PT_BYTE;
9677 dups = message_log_check_duplicate (prev_bol_byte,
9678 this_bol_byte);
9679 if (dups)
9681 del_range_both (prev_bol, prev_bol_byte,
9682 this_bol, this_bol_byte, 0);
9683 if (dups > 1)
9685 char dupstr[sizeof " [ times]"
9686 + INT_STRLEN_BOUND (printmax_t)];
9688 /* If you change this format, don't forget to also
9689 change message_log_check_duplicate. */
9690 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9691 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9692 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9697 /* If we have more than the desired maximum number of lines
9698 in the *Messages* buffer now, delete the oldest ones.
9699 This is safe because we don't have undo in this buffer. */
9701 if (NATNUMP (Vmessage_log_max))
9703 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9704 -XFASTINT (Vmessage_log_max) - 1, 0);
9705 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9708 BEGV = marker_position (oldbegv);
9709 BEGV_BYTE = marker_byte_position (oldbegv);
9711 if (zv_at_end)
9713 ZV = Z;
9714 ZV_BYTE = Z_BYTE;
9716 else
9718 ZV = marker_position (oldzv);
9719 ZV_BYTE = marker_byte_position (oldzv);
9722 if (point_at_end)
9723 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9724 else
9725 /* We can't do Fgoto_char (oldpoint) because it will run some
9726 Lisp code. */
9727 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9728 marker_byte_position (oldpoint));
9730 UNGCPRO;
9731 unchain_marker (XMARKER (oldpoint));
9732 unchain_marker (XMARKER (oldbegv));
9733 unchain_marker (XMARKER (oldzv));
9735 shown = buffer_window_count (current_buffer) > 0;
9736 set_buffer_internal (oldbuf);
9737 /* We called insert_1_both above with its 5th argument (PREPARE)
9738 zero, which prevents insert_1_both from calling
9739 prepare_to_modify_buffer, which in turns prevents us from
9740 incrementing windows_or_buffers_changed even if *Messages* is
9741 shown in some window. So we must manually incrementing
9742 windows_or_buffers_changed here to make up for that. */
9743 if (shown)
9744 windows_or_buffers_changed++;
9745 else
9746 windows_or_buffers_changed = old_windows_or_buffers_changed;
9747 message_log_need_newline = !nlflag;
9748 Vdeactivate_mark = old_deactivate_mark;
9753 /* We are at the end of the buffer after just having inserted a newline.
9754 (Note: We depend on the fact we won't be crossing the gap.)
9755 Check to see if the most recent message looks a lot like the previous one.
9756 Return 0 if different, 1 if the new one should just replace it, or a
9757 value N > 1 if we should also append " [N times]". */
9759 static intmax_t
9760 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9762 ptrdiff_t i;
9763 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9764 int seen_dots = 0;
9765 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9766 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9768 for (i = 0; i < len; i++)
9770 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9771 seen_dots = 1;
9772 if (p1[i] != p2[i])
9773 return seen_dots;
9775 p1 += len;
9776 if (*p1 == '\n')
9777 return 2;
9778 if (*p1++ == ' ' && *p1++ == '[')
9780 char *pend;
9781 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9782 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9783 return n + 1;
9785 return 0;
9789 /* Display an echo area message M with a specified length of NBYTES
9790 bytes. The string may include null characters. If M is not a
9791 string, clear out any existing message, and let the mini-buffer
9792 text show through.
9794 This function cancels echoing. */
9796 void
9797 message3 (Lisp_Object m)
9799 struct gcpro gcpro1;
9801 GCPRO1 (m);
9802 clear_message (1,1);
9803 cancel_echoing ();
9805 /* First flush out any partial line written with print. */
9806 message_log_maybe_newline ();
9807 if (STRINGP (m))
9809 ptrdiff_t nbytes = SBYTES (m);
9810 bool multibyte = STRING_MULTIBYTE (m);
9811 USE_SAFE_ALLOCA;
9812 char *buffer = SAFE_ALLOCA (nbytes);
9813 memcpy (buffer, SDATA (m), nbytes);
9814 message_dolog (buffer, nbytes, 1, multibyte);
9815 SAFE_FREE ();
9817 message3_nolog (m);
9819 UNGCPRO;
9823 /* The non-logging version of message3.
9824 This does not cancel echoing, because it is used for echoing.
9825 Perhaps we need to make a separate function for echoing
9826 and make this cancel echoing. */
9828 void
9829 message3_nolog (Lisp_Object m)
9831 struct frame *sf = SELECTED_FRAME ();
9833 if (FRAME_INITIAL_P (sf))
9835 if (noninteractive_need_newline)
9836 putc ('\n', stderr);
9837 noninteractive_need_newline = 0;
9838 if (STRINGP (m))
9839 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9840 if (cursor_in_echo_area == 0)
9841 fprintf (stderr, "\n");
9842 fflush (stderr);
9844 /* Error messages get reported properly by cmd_error, so this must be just an
9845 informative message; if the frame hasn't really been initialized yet, just
9846 toss it. */
9847 else if (INTERACTIVE && sf->glyphs_initialized_p)
9849 /* Get the frame containing the mini-buffer
9850 that the selected frame is using. */
9851 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9852 Lisp_Object frame = XWINDOW (mini_window)->frame;
9853 struct frame *f = XFRAME (frame);
9855 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9856 Fmake_frame_visible (frame);
9858 if (STRINGP (m) && SCHARS (m) > 0)
9860 set_message (m);
9861 if (minibuffer_auto_raise)
9862 Fraise_frame (frame);
9863 /* Assume we are not echoing.
9864 (If we are, echo_now will override this.) */
9865 echo_message_buffer = Qnil;
9867 else
9868 clear_message (1, 1);
9870 do_pending_window_change (0);
9871 echo_area_display (1);
9872 do_pending_window_change (0);
9873 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9874 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9879 /* Display a null-terminated echo area message M. If M is 0, clear
9880 out any existing message, and let the mini-buffer text show through.
9882 The buffer M must continue to exist until after the echo area gets
9883 cleared or some other message gets displayed there. Do not pass
9884 text that is stored in a Lisp string. Do not pass text in a buffer
9885 that was alloca'd. */
9887 void
9888 message1 (const char *m)
9890 message3 (m ? build_unibyte_string (m) : Qnil);
9894 /* The non-logging counterpart of message1. */
9896 void
9897 message1_nolog (const char *m)
9899 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9902 /* Display a message M which contains a single %s
9903 which gets replaced with STRING. */
9905 void
9906 message_with_string (const char *m, Lisp_Object string, int log)
9908 CHECK_STRING (string);
9910 if (noninteractive)
9912 if (m)
9914 if (noninteractive_need_newline)
9915 putc ('\n', stderr);
9916 noninteractive_need_newline = 0;
9917 fprintf (stderr, m, SDATA (string));
9918 if (!cursor_in_echo_area)
9919 fprintf (stderr, "\n");
9920 fflush (stderr);
9923 else if (INTERACTIVE)
9925 /* The frame whose minibuffer we're going to display the message on.
9926 It may be larger than the selected frame, so we need
9927 to use its buffer, not the selected frame's buffer. */
9928 Lisp_Object mini_window;
9929 struct frame *f, *sf = SELECTED_FRAME ();
9931 /* Get the frame containing the minibuffer
9932 that the selected frame is using. */
9933 mini_window = FRAME_MINIBUF_WINDOW (sf);
9934 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9936 /* Error messages get reported properly by cmd_error, so this must be
9937 just an informative message; if the frame hasn't really been
9938 initialized yet, just toss it. */
9939 if (f->glyphs_initialized_p)
9941 Lisp_Object args[2], msg;
9942 struct gcpro gcpro1, gcpro2;
9944 args[0] = build_string (m);
9945 args[1] = msg = string;
9946 GCPRO2 (args[0], msg);
9947 gcpro1.nvars = 2;
9949 msg = Fformat (2, args);
9951 if (log)
9952 message3 (msg);
9953 else
9954 message3_nolog (msg);
9956 UNGCPRO;
9958 /* Print should start at the beginning of the message
9959 buffer next time. */
9960 message_buf_print = 0;
9966 /* Dump an informative message to the minibuf. If M is 0, clear out
9967 any existing message, and let the mini-buffer text show through. */
9969 static void
9970 vmessage (const char *m, va_list ap)
9972 if (noninteractive)
9974 if (m)
9976 if (noninteractive_need_newline)
9977 putc ('\n', stderr);
9978 noninteractive_need_newline = 0;
9979 vfprintf (stderr, m, ap);
9980 if (cursor_in_echo_area == 0)
9981 fprintf (stderr, "\n");
9982 fflush (stderr);
9985 else if (INTERACTIVE)
9987 /* The frame whose mini-buffer we're going to display the message
9988 on. It may be larger than the selected frame, so we need to
9989 use its buffer, not the selected frame's buffer. */
9990 Lisp_Object mini_window;
9991 struct frame *f, *sf = SELECTED_FRAME ();
9993 /* Get the frame containing the mini-buffer
9994 that the selected frame is using. */
9995 mini_window = FRAME_MINIBUF_WINDOW (sf);
9996 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9998 /* Error messages get reported properly by cmd_error, so this must be
9999 just an informative message; if the frame hasn't really been
10000 initialized yet, just toss it. */
10001 if (f->glyphs_initialized_p)
10003 if (m)
10005 ptrdiff_t len;
10006 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10007 char *message_buf = alloca (maxsize + 1);
10009 len = doprnt (message_buf, maxsize, m, 0, ap);
10011 message3 (make_string (message_buf, len));
10013 else
10014 message1 (0);
10016 /* Print should start at the beginning of the message
10017 buffer next time. */
10018 message_buf_print = 0;
10023 void
10024 message (const char *m, ...)
10026 va_list ap;
10027 va_start (ap, m);
10028 vmessage (m, ap);
10029 va_end (ap);
10033 #if 0
10034 /* The non-logging version of message. */
10036 void
10037 message_nolog (const char *m, ...)
10039 Lisp_Object old_log_max;
10040 va_list ap;
10041 va_start (ap, m);
10042 old_log_max = Vmessage_log_max;
10043 Vmessage_log_max = Qnil;
10044 vmessage (m, ap);
10045 Vmessage_log_max = old_log_max;
10046 va_end (ap);
10048 #endif
10051 /* Display the current message in the current mini-buffer. This is
10052 only called from error handlers in process.c, and is not time
10053 critical. */
10055 void
10056 update_echo_area (void)
10058 if (!NILP (echo_area_buffer[0]))
10060 Lisp_Object string;
10061 string = Fcurrent_message ();
10062 message3 (string);
10067 /* Make sure echo area buffers in `echo_buffers' are live.
10068 If they aren't, make new ones. */
10070 static void
10071 ensure_echo_area_buffers (void)
10073 int i;
10075 for (i = 0; i < 2; ++i)
10076 if (!BUFFERP (echo_buffer[i])
10077 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10079 char name[30];
10080 Lisp_Object old_buffer;
10081 int j;
10083 old_buffer = echo_buffer[i];
10084 echo_buffer[i] = Fget_buffer_create
10085 (make_formatted_string (name, " *Echo Area %d*", i));
10086 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10087 /* to force word wrap in echo area -
10088 it was decided to postpone this*/
10089 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10091 for (j = 0; j < 2; ++j)
10092 if (EQ (old_buffer, echo_area_buffer[j]))
10093 echo_area_buffer[j] = echo_buffer[i];
10098 /* Call FN with args A1..A2 with either the current or last displayed
10099 echo_area_buffer as current buffer.
10101 WHICH zero means use the current message buffer
10102 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10103 from echo_buffer[] and clear it.
10105 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10106 suitable buffer from echo_buffer[] and clear it.
10108 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10109 that the current message becomes the last displayed one, make
10110 choose a suitable buffer for echo_area_buffer[0], and clear it.
10112 Value is what FN returns. */
10114 static int
10115 with_echo_area_buffer (struct window *w, int which,
10116 int (*fn) (ptrdiff_t, Lisp_Object),
10117 ptrdiff_t a1, Lisp_Object a2)
10119 Lisp_Object buffer;
10120 int this_one, the_other, clear_buffer_p, rc;
10121 ptrdiff_t count = SPECPDL_INDEX ();
10123 /* If buffers aren't live, make new ones. */
10124 ensure_echo_area_buffers ();
10126 clear_buffer_p = 0;
10128 if (which == 0)
10129 this_one = 0, the_other = 1;
10130 else if (which > 0)
10131 this_one = 1, the_other = 0;
10132 else
10134 this_one = 0, the_other = 1;
10135 clear_buffer_p = 1;
10137 /* We need a fresh one in case the current echo buffer equals
10138 the one containing the last displayed echo area message. */
10139 if (!NILP (echo_area_buffer[this_one])
10140 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10141 echo_area_buffer[this_one] = Qnil;
10144 /* Choose a suitable buffer from echo_buffer[] is we don't
10145 have one. */
10146 if (NILP (echo_area_buffer[this_one]))
10148 echo_area_buffer[this_one]
10149 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10150 ? echo_buffer[the_other]
10151 : echo_buffer[this_one]);
10152 clear_buffer_p = 1;
10155 buffer = echo_area_buffer[this_one];
10157 /* Don't get confused by reusing the buffer used for echoing
10158 for a different purpose. */
10159 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10160 cancel_echoing ();
10162 record_unwind_protect (unwind_with_echo_area_buffer,
10163 with_echo_area_buffer_unwind_data (w));
10165 /* Make the echo area buffer current. Note that for display
10166 purposes, it is not necessary that the displayed window's buffer
10167 == current_buffer, except for text property lookup. So, let's
10168 only set that buffer temporarily here without doing a full
10169 Fset_window_buffer. We must also change w->pointm, though,
10170 because otherwise an assertions in unshow_buffer fails, and Emacs
10171 aborts. */
10172 set_buffer_internal_1 (XBUFFER (buffer));
10173 if (w)
10175 wset_buffer (w, buffer);
10176 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10179 bset_undo_list (current_buffer, Qt);
10180 bset_read_only (current_buffer, Qnil);
10181 specbind (Qinhibit_read_only, Qt);
10182 specbind (Qinhibit_modification_hooks, Qt);
10184 if (clear_buffer_p && Z > BEG)
10185 del_range (BEG, Z);
10187 eassert (BEGV >= BEG);
10188 eassert (ZV <= Z && ZV >= BEGV);
10190 rc = fn (a1, a2);
10192 eassert (BEGV >= BEG);
10193 eassert (ZV <= Z && ZV >= BEGV);
10195 unbind_to (count, Qnil);
10196 return rc;
10200 /* Save state that should be preserved around the call to the function
10201 FN called in with_echo_area_buffer. */
10203 static Lisp_Object
10204 with_echo_area_buffer_unwind_data (struct window *w)
10206 int i = 0;
10207 Lisp_Object vector, tmp;
10209 /* Reduce consing by keeping one vector in
10210 Vwith_echo_area_save_vector. */
10211 vector = Vwith_echo_area_save_vector;
10212 Vwith_echo_area_save_vector = Qnil;
10214 if (NILP (vector))
10215 vector = Fmake_vector (make_number (9), Qnil);
10217 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10218 ASET (vector, i, Vdeactivate_mark); ++i;
10219 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10221 if (w)
10223 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10224 ASET (vector, i, w->contents); ++i;
10225 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10226 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10227 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10228 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10230 else
10232 int end = i + 6;
10233 for (; i < end; ++i)
10234 ASET (vector, i, Qnil);
10237 eassert (i == ASIZE (vector));
10238 return vector;
10242 /* Restore global state from VECTOR which was created by
10243 with_echo_area_buffer_unwind_data. */
10245 static void
10246 unwind_with_echo_area_buffer (Lisp_Object vector)
10248 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10249 Vdeactivate_mark = AREF (vector, 1);
10250 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10252 if (WINDOWP (AREF (vector, 3)))
10254 struct window *w;
10255 Lisp_Object buffer;
10257 w = XWINDOW (AREF (vector, 3));
10258 buffer = AREF (vector, 4);
10260 wset_buffer (w, buffer);
10261 set_marker_both (w->pointm, buffer,
10262 XFASTINT (AREF (vector, 5)),
10263 XFASTINT (AREF (vector, 6)));
10264 set_marker_both (w->start, buffer,
10265 XFASTINT (AREF (vector, 7)),
10266 XFASTINT (AREF (vector, 8)));
10269 Vwith_echo_area_save_vector = vector;
10273 /* Set up the echo area for use by print functions. MULTIBYTE_P
10274 non-zero means we will print multibyte. */
10276 void
10277 setup_echo_area_for_printing (int multibyte_p)
10279 /* If we can't find an echo area any more, exit. */
10280 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10281 Fkill_emacs (Qnil);
10283 ensure_echo_area_buffers ();
10285 if (!message_buf_print)
10287 /* A message has been output since the last time we printed.
10288 Choose a fresh echo area buffer. */
10289 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10290 echo_area_buffer[0] = echo_buffer[1];
10291 else
10292 echo_area_buffer[0] = echo_buffer[0];
10294 /* Switch to that buffer and clear it. */
10295 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10296 bset_truncate_lines (current_buffer, Qnil);
10298 if (Z > BEG)
10300 ptrdiff_t count = SPECPDL_INDEX ();
10301 specbind (Qinhibit_read_only, Qt);
10302 /* Note that undo recording is always disabled. */
10303 del_range (BEG, Z);
10304 unbind_to (count, Qnil);
10306 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10308 /* Set up the buffer for the multibyteness we need. */
10309 if (multibyte_p
10310 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10311 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10313 /* Raise the frame containing the echo area. */
10314 if (minibuffer_auto_raise)
10316 struct frame *sf = SELECTED_FRAME ();
10317 Lisp_Object mini_window;
10318 mini_window = FRAME_MINIBUF_WINDOW (sf);
10319 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10322 message_log_maybe_newline ();
10323 message_buf_print = 1;
10325 else
10327 if (NILP (echo_area_buffer[0]))
10329 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10330 echo_area_buffer[0] = echo_buffer[1];
10331 else
10332 echo_area_buffer[0] = echo_buffer[0];
10335 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10337 /* Someone switched buffers between print requests. */
10338 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10339 bset_truncate_lines (current_buffer, Qnil);
10345 /* Display an echo area message in window W. Value is non-zero if W's
10346 height is changed. If display_last_displayed_message_p is
10347 non-zero, display the message that was last displayed, otherwise
10348 display the current message. */
10350 static int
10351 display_echo_area (struct window *w)
10353 int i, no_message_p, window_height_changed_p;
10355 /* Temporarily disable garbage collections while displaying the echo
10356 area. This is done because a GC can print a message itself.
10357 That message would modify the echo area buffer's contents while a
10358 redisplay of the buffer is going on, and seriously confuse
10359 redisplay. */
10360 ptrdiff_t count = inhibit_garbage_collection ();
10362 /* If there is no message, we must call display_echo_area_1
10363 nevertheless because it resizes the window. But we will have to
10364 reset the echo_area_buffer in question to nil at the end because
10365 with_echo_area_buffer will sets it to an empty buffer. */
10366 i = display_last_displayed_message_p ? 1 : 0;
10367 no_message_p = NILP (echo_area_buffer[i]);
10369 window_height_changed_p
10370 = with_echo_area_buffer (w, display_last_displayed_message_p,
10371 display_echo_area_1,
10372 (intptr_t) w, Qnil);
10374 if (no_message_p)
10375 echo_area_buffer[i] = Qnil;
10377 unbind_to (count, Qnil);
10378 return window_height_changed_p;
10382 /* Helper for display_echo_area. Display the current buffer which
10383 contains the current echo area message in window W, a mini-window,
10384 a pointer to which is passed in A1. A2..A4 are currently not used.
10385 Change the height of W so that all of the message is displayed.
10386 Value is non-zero if height of W was changed. */
10388 static int
10389 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10391 intptr_t i1 = a1;
10392 struct window *w = (struct window *) i1;
10393 Lisp_Object window;
10394 struct text_pos start;
10395 int window_height_changed_p = 0;
10397 /* Do this before displaying, so that we have a large enough glyph
10398 matrix for the display. If we can't get enough space for the
10399 whole text, display the last N lines. That works by setting w->start. */
10400 window_height_changed_p = resize_mini_window (w, 0);
10402 /* Use the starting position chosen by resize_mini_window. */
10403 SET_TEXT_POS_FROM_MARKER (start, w->start);
10405 /* Display. */
10406 clear_glyph_matrix (w->desired_matrix);
10407 XSETWINDOW (window, w);
10408 try_window (window, start, 0);
10410 return window_height_changed_p;
10414 /* Resize the echo area window to exactly the size needed for the
10415 currently displayed message, if there is one. If a mini-buffer
10416 is active, don't shrink it. */
10418 void
10419 resize_echo_area_exactly (void)
10421 if (BUFFERP (echo_area_buffer[0])
10422 && WINDOWP (echo_area_window))
10424 struct window *w = XWINDOW (echo_area_window);
10425 int resized_p;
10426 Lisp_Object resize_exactly;
10428 if (minibuf_level == 0)
10429 resize_exactly = Qt;
10430 else
10431 resize_exactly = Qnil;
10433 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10434 (intptr_t) w, resize_exactly);
10435 if (resized_p)
10437 ++windows_or_buffers_changed;
10438 ++update_mode_lines;
10439 redisplay_internal ();
10445 /* Callback function for with_echo_area_buffer, when used from
10446 resize_echo_area_exactly. A1 contains a pointer to the window to
10447 resize, EXACTLY non-nil means resize the mini-window exactly to the
10448 size of the text displayed. A3 and A4 are not used. Value is what
10449 resize_mini_window returns. */
10451 static int
10452 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10454 intptr_t i1 = a1;
10455 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10459 /* Resize mini-window W to fit the size of its contents. EXACT_P
10460 means size the window exactly to the size needed. Otherwise, it's
10461 only enlarged until W's buffer is empty.
10463 Set W->start to the right place to begin display. If the whole
10464 contents fit, start at the beginning. Otherwise, start so as
10465 to make the end of the contents appear. This is particularly
10466 important for y-or-n-p, but seems desirable generally.
10468 Value is non-zero if the window height has been changed. */
10471 resize_mini_window (struct window *w, int exact_p)
10473 struct frame *f = XFRAME (w->frame);
10474 int window_height_changed_p = 0;
10476 eassert (MINI_WINDOW_P (w));
10478 /* By default, start display at the beginning. */
10479 set_marker_both (w->start, w->contents,
10480 BUF_BEGV (XBUFFER (w->contents)),
10481 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10483 /* Don't resize windows while redisplaying a window; it would
10484 confuse redisplay functions when the size of the window they are
10485 displaying changes from under them. Such a resizing can happen,
10486 for instance, when which-func prints a long message while
10487 we are running fontification-functions. We're running these
10488 functions with safe_call which binds inhibit-redisplay to t. */
10489 if (!NILP (Vinhibit_redisplay))
10490 return 0;
10492 /* Nil means don't try to resize. */
10493 if (NILP (Vresize_mini_windows)
10494 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10495 return 0;
10497 if (!FRAME_MINIBUF_ONLY_P (f))
10499 struct it it;
10500 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10501 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10502 int height;
10503 EMACS_INT max_height;
10504 int unit = FRAME_LINE_HEIGHT (f);
10505 struct text_pos start;
10506 struct buffer *old_current_buffer = NULL;
10508 if (current_buffer != XBUFFER (w->contents))
10510 old_current_buffer = current_buffer;
10511 set_buffer_internal (XBUFFER (w->contents));
10514 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10516 /* Compute the max. number of lines specified by the user. */
10517 if (FLOATP (Vmax_mini_window_height))
10518 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10519 else if (INTEGERP (Vmax_mini_window_height))
10520 max_height = XINT (Vmax_mini_window_height);
10521 else
10522 max_height = total_height / 4;
10524 /* Correct that max. height if it's bogus. */
10525 max_height = clip_to_bounds (1, max_height, total_height);
10527 /* Find out the height of the text in the window. */
10528 if (it.line_wrap == TRUNCATE)
10529 height = 1;
10530 else
10532 last_height = 0;
10533 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10534 if (it.max_ascent == 0 && it.max_descent == 0)
10535 height = it.current_y + last_height;
10536 else
10537 height = it.current_y + it.max_ascent + it.max_descent;
10538 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10539 height = (height + unit - 1) / unit;
10542 /* Compute a suitable window start. */
10543 if (height > max_height)
10545 height = max_height;
10546 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10547 move_it_vertically_backward (&it, (height - 1) * unit);
10548 start = it.current.pos;
10550 else
10551 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10552 SET_MARKER_FROM_TEXT_POS (w->start, start);
10554 if (EQ (Vresize_mini_windows, Qgrow_only))
10556 /* Let it grow only, until we display an empty message, in which
10557 case the window shrinks again. */
10558 if (height > WINDOW_TOTAL_LINES (w))
10560 int old_height = WINDOW_TOTAL_LINES (w);
10562 FRAME_WINDOWS_FROZEN (f) = 1;
10563 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10564 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10566 else if (height < WINDOW_TOTAL_LINES (w)
10567 && (exact_p || BEGV == ZV))
10569 int old_height = WINDOW_TOTAL_LINES (w);
10571 FRAME_WINDOWS_FROZEN (f) = 0;
10572 shrink_mini_window (w);
10573 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10576 else
10578 /* Always resize to exact size needed. */
10579 if (height > WINDOW_TOTAL_LINES (w))
10581 int old_height = WINDOW_TOTAL_LINES (w);
10583 FRAME_WINDOWS_FROZEN (f) = 1;
10584 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10585 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10587 else if (height < WINDOW_TOTAL_LINES (w))
10589 int old_height = WINDOW_TOTAL_LINES (w);
10591 FRAME_WINDOWS_FROZEN (f) = 0;
10592 shrink_mini_window (w);
10594 if (height)
10596 FRAME_WINDOWS_FROZEN (f) = 1;
10597 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10600 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10604 if (old_current_buffer)
10605 set_buffer_internal (old_current_buffer);
10608 return window_height_changed_p;
10612 /* Value is the current message, a string, or nil if there is no
10613 current message. */
10615 Lisp_Object
10616 current_message (void)
10618 Lisp_Object msg;
10620 if (!BUFFERP (echo_area_buffer[0]))
10621 msg = Qnil;
10622 else
10624 with_echo_area_buffer (0, 0, current_message_1,
10625 (intptr_t) &msg, Qnil);
10626 if (NILP (msg))
10627 echo_area_buffer[0] = Qnil;
10630 return msg;
10634 static int
10635 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10637 intptr_t i1 = a1;
10638 Lisp_Object *msg = (Lisp_Object *) i1;
10640 if (Z > BEG)
10641 *msg = make_buffer_string (BEG, Z, 1);
10642 else
10643 *msg = Qnil;
10644 return 0;
10648 /* Push the current message on Vmessage_stack for later restoration
10649 by restore_message. Value is non-zero if the current message isn't
10650 empty. This is a relatively infrequent operation, so it's not
10651 worth optimizing. */
10653 bool
10654 push_message (void)
10656 Lisp_Object msg = current_message ();
10657 Vmessage_stack = Fcons (msg, Vmessage_stack);
10658 return STRINGP (msg);
10662 /* Restore message display from the top of Vmessage_stack. */
10664 void
10665 restore_message (void)
10667 eassert (CONSP (Vmessage_stack));
10668 message3_nolog (XCAR (Vmessage_stack));
10672 /* Handler for unwind-protect calling pop_message. */
10674 void
10675 pop_message_unwind (void)
10677 /* Pop the top-most entry off Vmessage_stack. */
10678 eassert (CONSP (Vmessage_stack));
10679 Vmessage_stack = XCDR (Vmessage_stack);
10683 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10684 exits. If the stack is not empty, we have a missing pop_message
10685 somewhere. */
10687 void
10688 check_message_stack (void)
10690 if (!NILP (Vmessage_stack))
10691 emacs_abort ();
10695 /* Truncate to NCHARS what will be displayed in the echo area the next
10696 time we display it---but don't redisplay it now. */
10698 void
10699 truncate_echo_area (ptrdiff_t nchars)
10701 if (nchars == 0)
10702 echo_area_buffer[0] = Qnil;
10703 else if (!noninteractive
10704 && INTERACTIVE
10705 && !NILP (echo_area_buffer[0]))
10707 struct frame *sf = SELECTED_FRAME ();
10708 /* Error messages get reported properly by cmd_error, so this must be
10709 just an informative message; if the frame hasn't really been
10710 initialized yet, just toss it. */
10711 if (sf->glyphs_initialized_p)
10712 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10717 /* Helper function for truncate_echo_area. Truncate the current
10718 message to at most NCHARS characters. */
10720 static int
10721 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10723 if (BEG + nchars < Z)
10724 del_range (BEG + nchars, Z);
10725 if (Z == BEG)
10726 echo_area_buffer[0] = Qnil;
10727 return 0;
10730 /* Set the current message to STRING. */
10732 static void
10733 set_message (Lisp_Object string)
10735 eassert (STRINGP (string));
10737 message_enable_multibyte = STRING_MULTIBYTE (string);
10739 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10740 message_buf_print = 0;
10741 help_echo_showing_p = 0;
10743 if (STRINGP (Vdebug_on_message)
10744 && STRINGP (string)
10745 && fast_string_match (Vdebug_on_message, string) >= 0)
10746 call_debugger (list2 (Qerror, string));
10750 /* Helper function for set_message. First argument is ignored and second
10751 argument has the same meaning as for set_message.
10752 This function is called with the echo area buffer being current. */
10754 static int
10755 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10757 eassert (STRINGP (string));
10759 /* Change multibyteness of the echo buffer appropriately. */
10760 if (message_enable_multibyte
10761 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10762 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10764 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10765 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10766 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10768 /* Insert new message at BEG. */
10769 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10771 /* This function takes care of single/multibyte conversion.
10772 We just have to ensure that the echo area buffer has the right
10773 setting of enable_multibyte_characters. */
10774 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10776 return 0;
10780 /* Clear messages. CURRENT_P non-zero means clear the current
10781 message. LAST_DISPLAYED_P non-zero means clear the message
10782 last displayed. */
10784 void
10785 clear_message (int current_p, int last_displayed_p)
10787 if (current_p)
10789 echo_area_buffer[0] = Qnil;
10790 message_cleared_p = 1;
10793 if (last_displayed_p)
10794 echo_area_buffer[1] = Qnil;
10796 message_buf_print = 0;
10799 /* Clear garbaged frames.
10801 This function is used where the old redisplay called
10802 redraw_garbaged_frames which in turn called redraw_frame which in
10803 turn called clear_frame. The call to clear_frame was a source of
10804 flickering. I believe a clear_frame is not necessary. It should
10805 suffice in the new redisplay to invalidate all current matrices,
10806 and ensure a complete redisplay of all windows. */
10808 static void
10809 clear_garbaged_frames (void)
10811 if (frame_garbaged)
10813 Lisp_Object tail, frame;
10814 int changed_count = 0;
10816 FOR_EACH_FRAME (tail, frame)
10818 struct frame *f = XFRAME (frame);
10820 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10822 if (f->resized_p)
10823 redraw_frame (f);
10824 else
10825 clear_current_matrices (f);
10826 changed_count++;
10827 f->garbaged = 0;
10828 f->resized_p = 0;
10832 frame_garbaged = 0;
10833 if (changed_count)
10834 ++windows_or_buffers_changed;
10839 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10840 is non-zero update selected_frame. Value is non-zero if the
10841 mini-windows height has been changed. */
10843 static int
10844 echo_area_display (int update_frame_p)
10846 Lisp_Object mini_window;
10847 struct window *w;
10848 struct frame *f;
10849 int window_height_changed_p = 0;
10850 struct frame *sf = SELECTED_FRAME ();
10852 mini_window = FRAME_MINIBUF_WINDOW (sf);
10853 w = XWINDOW (mini_window);
10854 f = XFRAME (WINDOW_FRAME (w));
10856 /* Don't display if frame is invisible or not yet initialized. */
10857 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10858 return 0;
10860 #ifdef HAVE_WINDOW_SYSTEM
10861 /* When Emacs starts, selected_frame may be the initial terminal
10862 frame. If we let this through, a message would be displayed on
10863 the terminal. */
10864 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10865 return 0;
10866 #endif /* HAVE_WINDOW_SYSTEM */
10868 /* Redraw garbaged frames. */
10869 clear_garbaged_frames ();
10871 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10873 echo_area_window = mini_window;
10874 window_height_changed_p = display_echo_area (w);
10875 w->must_be_updated_p = 1;
10877 /* Update the display, unless called from redisplay_internal.
10878 Also don't update the screen during redisplay itself. The
10879 update will happen at the end of redisplay, and an update
10880 here could cause confusion. */
10881 if (update_frame_p && !redisplaying_p)
10883 int n = 0;
10885 /* If the display update has been interrupted by pending
10886 input, update mode lines in the frame. Due to the
10887 pending input, it might have been that redisplay hasn't
10888 been called, so that mode lines above the echo area are
10889 garbaged. This looks odd, so we prevent it here. */
10890 if (!display_completed)
10891 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10893 if (window_height_changed_p
10894 /* Don't do this if Emacs is shutting down. Redisplay
10895 needs to run hooks. */
10896 && !NILP (Vrun_hooks))
10898 /* Must update other windows. Likewise as in other
10899 cases, don't let this update be interrupted by
10900 pending input. */
10901 ptrdiff_t count = SPECPDL_INDEX ();
10902 specbind (Qredisplay_dont_pause, Qt);
10903 windows_or_buffers_changed = 1;
10904 redisplay_internal ();
10905 unbind_to (count, Qnil);
10907 else if (FRAME_WINDOW_P (f) && n == 0)
10909 /* Window configuration is the same as before.
10910 Can do with a display update of the echo area,
10911 unless we displayed some mode lines. */
10912 update_single_window (w, 1);
10913 flush_frame (f);
10915 else
10916 update_frame (f, 1, 1);
10918 /* If cursor is in the echo area, make sure that the next
10919 redisplay displays the minibuffer, so that the cursor will
10920 be replaced with what the minibuffer wants. */
10921 if (cursor_in_echo_area)
10922 ++windows_or_buffers_changed;
10925 else if (!EQ (mini_window, selected_window))
10926 windows_or_buffers_changed++;
10928 /* Last displayed message is now the current message. */
10929 echo_area_buffer[1] = echo_area_buffer[0];
10930 /* Inform read_char that we're not echoing. */
10931 echo_message_buffer = Qnil;
10933 /* Prevent redisplay optimization in redisplay_internal by resetting
10934 this_line_start_pos. This is done because the mini-buffer now
10935 displays the message instead of its buffer text. */
10936 if (EQ (mini_window, selected_window))
10937 CHARPOS (this_line_start_pos) = 0;
10939 return window_height_changed_p;
10942 /* Nonzero if the current window's buffer is shown in more than one
10943 window and was modified since last redisplay. */
10945 static int
10946 buffer_shared_and_changed (void)
10948 return (buffer_window_count (current_buffer) > 1
10949 && UNCHANGED_MODIFIED < MODIFF);
10952 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10953 is enabled and mark of W's buffer was changed since last W's update. */
10955 static int
10956 window_buffer_changed (struct window *w)
10958 struct buffer *b = XBUFFER (w->contents);
10960 eassert (BUFFER_LIVE_P (b));
10962 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10963 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10964 != (w->region_showing != 0)));
10967 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10969 static int
10970 mode_line_update_needed (struct window *w)
10972 return (w->column_number_displayed != -1
10973 && !(PT == w->last_point && !window_outdated (w))
10974 && (w->column_number_displayed != current_column ()));
10977 /* Nonzero if window start of W is frozen and may not be changed during
10978 redisplay. */
10980 static bool
10981 window_frozen_p (struct window *w)
10983 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10985 Lisp_Object window;
10987 XSETWINDOW (window, w);
10988 if (MINI_WINDOW_P (w))
10989 return 0;
10990 else if (EQ (window, selected_window))
10991 return 0;
10992 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10993 && EQ (window, Vminibuf_scroll_window))
10994 /* This special window can't be frozen too. */
10995 return 0;
10996 else
10997 return 1;
10999 return 0;
11002 /***********************************************************************
11003 Mode Lines and Frame Titles
11004 ***********************************************************************/
11006 /* A buffer for constructing non-propertized mode-line strings and
11007 frame titles in it; allocated from the heap in init_xdisp and
11008 resized as needed in store_mode_line_noprop_char. */
11010 static char *mode_line_noprop_buf;
11012 /* The buffer's end, and a current output position in it. */
11014 static char *mode_line_noprop_buf_end;
11015 static char *mode_line_noprop_ptr;
11017 #define MODE_LINE_NOPROP_LEN(start) \
11018 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11020 static enum {
11021 MODE_LINE_DISPLAY = 0,
11022 MODE_LINE_TITLE,
11023 MODE_LINE_NOPROP,
11024 MODE_LINE_STRING
11025 } mode_line_target;
11027 /* Alist that caches the results of :propertize.
11028 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11029 static Lisp_Object mode_line_proptrans_alist;
11031 /* List of strings making up the mode-line. */
11032 static Lisp_Object mode_line_string_list;
11034 /* Base face property when building propertized mode line string. */
11035 static Lisp_Object mode_line_string_face;
11036 static Lisp_Object mode_line_string_face_prop;
11039 /* Unwind data for mode line strings */
11041 static Lisp_Object Vmode_line_unwind_vector;
11043 static Lisp_Object
11044 format_mode_line_unwind_data (struct frame *target_frame,
11045 struct buffer *obuf,
11046 Lisp_Object owin,
11047 int save_proptrans)
11049 Lisp_Object vector, tmp;
11051 /* Reduce consing by keeping one vector in
11052 Vwith_echo_area_save_vector. */
11053 vector = Vmode_line_unwind_vector;
11054 Vmode_line_unwind_vector = Qnil;
11056 if (NILP (vector))
11057 vector = Fmake_vector (make_number (10), Qnil);
11059 ASET (vector, 0, make_number (mode_line_target));
11060 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11061 ASET (vector, 2, mode_line_string_list);
11062 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11063 ASET (vector, 4, mode_line_string_face);
11064 ASET (vector, 5, mode_line_string_face_prop);
11066 if (obuf)
11067 XSETBUFFER (tmp, obuf);
11068 else
11069 tmp = Qnil;
11070 ASET (vector, 6, tmp);
11071 ASET (vector, 7, owin);
11072 if (target_frame)
11074 /* Similarly to `with-selected-window', if the operation selects
11075 a window on another frame, we must restore that frame's
11076 selected window, and (for a tty) the top-frame. */
11077 ASET (vector, 8, target_frame->selected_window);
11078 if (FRAME_TERMCAP_P (target_frame))
11079 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11082 return vector;
11085 static void
11086 unwind_format_mode_line (Lisp_Object vector)
11088 Lisp_Object old_window = AREF (vector, 7);
11089 Lisp_Object target_frame_window = AREF (vector, 8);
11090 Lisp_Object old_top_frame = AREF (vector, 9);
11092 mode_line_target = XINT (AREF (vector, 0));
11093 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11094 mode_line_string_list = AREF (vector, 2);
11095 if (! EQ (AREF (vector, 3), Qt))
11096 mode_line_proptrans_alist = AREF (vector, 3);
11097 mode_line_string_face = AREF (vector, 4);
11098 mode_line_string_face_prop = AREF (vector, 5);
11100 /* Select window before buffer, since it may change the buffer. */
11101 if (!NILP (old_window))
11103 /* If the operation that we are unwinding had selected a window
11104 on a different frame, reset its frame-selected-window. For a
11105 text terminal, reset its top-frame if necessary. */
11106 if (!NILP (target_frame_window))
11108 Lisp_Object frame
11109 = WINDOW_FRAME (XWINDOW (target_frame_window));
11111 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11112 Fselect_window (target_frame_window, Qt);
11114 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11115 Fselect_frame (old_top_frame, Qt);
11118 Fselect_window (old_window, Qt);
11121 if (!NILP (AREF (vector, 6)))
11123 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11124 ASET (vector, 6, Qnil);
11127 Vmode_line_unwind_vector = vector;
11131 /* Store a single character C for the frame title in mode_line_noprop_buf.
11132 Re-allocate mode_line_noprop_buf if necessary. */
11134 static void
11135 store_mode_line_noprop_char (char c)
11137 /* If output position has reached the end of the allocated buffer,
11138 increase the buffer's size. */
11139 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11141 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11142 ptrdiff_t size = len;
11143 mode_line_noprop_buf =
11144 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11145 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11146 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11149 *mode_line_noprop_ptr++ = c;
11153 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11154 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11155 characters that yield more columns than PRECISION; PRECISION <= 0
11156 means copy the whole string. Pad with spaces until FIELD_WIDTH
11157 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11158 pad. Called from display_mode_element when it is used to build a
11159 frame title. */
11161 static int
11162 store_mode_line_noprop (const char *string, int field_width, int precision)
11164 const unsigned char *str = (const unsigned char *) string;
11165 int n = 0;
11166 ptrdiff_t dummy, nbytes;
11168 /* Copy at most PRECISION chars from STR. */
11169 nbytes = strlen (string);
11170 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11171 while (nbytes--)
11172 store_mode_line_noprop_char (*str++);
11174 /* Fill up with spaces until FIELD_WIDTH reached. */
11175 while (field_width > 0
11176 && n < field_width)
11178 store_mode_line_noprop_char (' ');
11179 ++n;
11182 return n;
11185 /***********************************************************************
11186 Frame Titles
11187 ***********************************************************************/
11189 #ifdef HAVE_WINDOW_SYSTEM
11191 /* Set the title of FRAME, if it has changed. The title format is
11192 Vicon_title_format if FRAME is iconified, otherwise it is
11193 frame_title_format. */
11195 static void
11196 x_consider_frame_title (Lisp_Object frame)
11198 struct frame *f = XFRAME (frame);
11200 if (FRAME_WINDOW_P (f)
11201 || FRAME_MINIBUF_ONLY_P (f)
11202 || f->explicit_name)
11204 /* Do we have more than one visible frame on this X display? */
11205 Lisp_Object tail, other_frame, fmt;
11206 ptrdiff_t title_start;
11207 char *title;
11208 ptrdiff_t len;
11209 struct it it;
11210 ptrdiff_t count = SPECPDL_INDEX ();
11212 FOR_EACH_FRAME (tail, other_frame)
11214 struct frame *tf = XFRAME (other_frame);
11216 if (tf != f
11217 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11218 && !FRAME_MINIBUF_ONLY_P (tf)
11219 && !EQ (other_frame, tip_frame)
11220 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11221 break;
11224 /* Set global variable indicating that multiple frames exist. */
11225 multiple_frames = CONSP (tail);
11227 /* Switch to the buffer of selected window of the frame. Set up
11228 mode_line_target so that display_mode_element will output into
11229 mode_line_noprop_buf; then display the title. */
11230 record_unwind_protect (unwind_format_mode_line,
11231 format_mode_line_unwind_data
11232 (f, current_buffer, selected_window, 0));
11234 Fselect_window (f->selected_window, Qt);
11235 set_buffer_internal_1
11236 (XBUFFER (XWINDOW (f->selected_window)->contents));
11237 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11239 mode_line_target = MODE_LINE_TITLE;
11240 title_start = MODE_LINE_NOPROP_LEN (0);
11241 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11242 NULL, DEFAULT_FACE_ID);
11243 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11244 len = MODE_LINE_NOPROP_LEN (title_start);
11245 title = mode_line_noprop_buf + title_start;
11246 unbind_to (count, Qnil);
11248 /* Set the title only if it's changed. This avoids consing in
11249 the common case where it hasn't. (If it turns out that we've
11250 already wasted too much time by walking through the list with
11251 display_mode_element, then we might need to optimize at a
11252 higher level than this.) */
11253 if (! STRINGP (f->name)
11254 || SBYTES (f->name) != len
11255 || memcmp (title, SDATA (f->name), len) != 0)
11256 x_implicitly_set_name (f, make_string (title, len), Qnil);
11260 #endif /* not HAVE_WINDOW_SYSTEM */
11263 /***********************************************************************
11264 Menu Bars
11265 ***********************************************************************/
11268 /* Prepare for redisplay by updating menu-bar item lists when
11269 appropriate. This can call eval. */
11271 void
11272 prepare_menu_bars (void)
11274 int all_windows;
11275 struct gcpro gcpro1, gcpro2;
11276 struct frame *f;
11277 Lisp_Object tooltip_frame;
11279 #ifdef HAVE_WINDOW_SYSTEM
11280 tooltip_frame = tip_frame;
11281 #else
11282 tooltip_frame = Qnil;
11283 #endif
11285 /* Update all frame titles based on their buffer names, etc. We do
11286 this before the menu bars so that the buffer-menu will show the
11287 up-to-date frame titles. */
11288 #ifdef HAVE_WINDOW_SYSTEM
11289 if (windows_or_buffers_changed || update_mode_lines)
11291 Lisp_Object tail, frame;
11293 FOR_EACH_FRAME (tail, frame)
11295 f = XFRAME (frame);
11296 if (!EQ (frame, tooltip_frame)
11297 && (FRAME_ICONIFIED_P (f)
11298 || FRAME_VISIBLE_P (f) == 1
11299 /* Exclude TTY frames that are obscured because they
11300 are not the top frame on their console. This is
11301 because x_consider_frame_title actually switches
11302 to the frame, which for TTY frames means it is
11303 marked as garbaged, and will be completely
11304 redrawn on the next redisplay cycle. This causes
11305 TTY frames to be completely redrawn, when there
11306 are more than one of them, even though nothing
11307 should be changed on display. */
11308 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11309 x_consider_frame_title (frame);
11312 #endif /* HAVE_WINDOW_SYSTEM */
11314 /* Update the menu bar item lists, if appropriate. This has to be
11315 done before any actual redisplay or generation of display lines. */
11316 all_windows = (update_mode_lines
11317 || buffer_shared_and_changed ()
11318 || windows_or_buffers_changed);
11319 if (all_windows)
11321 Lisp_Object tail, frame;
11322 ptrdiff_t count = SPECPDL_INDEX ();
11323 /* 1 means that update_menu_bar has run its hooks
11324 so any further calls to update_menu_bar shouldn't do so again. */
11325 int menu_bar_hooks_run = 0;
11327 record_unwind_save_match_data ();
11329 FOR_EACH_FRAME (tail, frame)
11331 f = XFRAME (frame);
11333 /* Ignore tooltip frame. */
11334 if (EQ (frame, tooltip_frame))
11335 continue;
11337 /* If a window on this frame changed size, report that to
11338 the user and clear the size-change flag. */
11339 if (FRAME_WINDOW_SIZES_CHANGED (f))
11341 Lisp_Object functions;
11343 /* Clear flag first in case we get an error below. */
11344 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11345 functions = Vwindow_size_change_functions;
11346 GCPRO2 (tail, functions);
11348 while (CONSP (functions))
11350 if (!EQ (XCAR (functions), Qt))
11351 call1 (XCAR (functions), frame);
11352 functions = XCDR (functions);
11354 UNGCPRO;
11357 GCPRO1 (tail);
11358 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11359 #ifdef HAVE_WINDOW_SYSTEM
11360 update_tool_bar (f, 0);
11361 #endif
11362 #ifdef HAVE_NS
11363 if (windows_or_buffers_changed
11364 && FRAME_NS_P (f))
11365 ns_set_doc_edited
11366 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11367 #endif
11368 UNGCPRO;
11371 unbind_to (count, Qnil);
11373 else
11375 struct frame *sf = SELECTED_FRAME ();
11376 update_menu_bar (sf, 1, 0);
11377 #ifdef HAVE_WINDOW_SYSTEM
11378 update_tool_bar (sf, 1);
11379 #endif
11384 /* Update the menu bar item list for frame F. This has to be done
11385 before we start to fill in any display lines, because it can call
11386 eval.
11388 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11390 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11391 already ran the menu bar hooks for this redisplay, so there
11392 is no need to run them again. The return value is the
11393 updated value of this flag, to pass to the next call. */
11395 static int
11396 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11398 Lisp_Object window;
11399 register struct window *w;
11401 /* If called recursively during a menu update, do nothing. This can
11402 happen when, for instance, an activate-menubar-hook causes a
11403 redisplay. */
11404 if (inhibit_menubar_update)
11405 return hooks_run;
11407 window = FRAME_SELECTED_WINDOW (f);
11408 w = XWINDOW (window);
11410 if (FRAME_WINDOW_P (f)
11412 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11413 || defined (HAVE_NS) || defined (USE_GTK)
11414 FRAME_EXTERNAL_MENU_BAR (f)
11415 #else
11416 FRAME_MENU_BAR_LINES (f) > 0
11417 #endif
11418 : FRAME_MENU_BAR_LINES (f) > 0)
11420 /* If the user has switched buffers or windows, we need to
11421 recompute to reflect the new bindings. But we'll
11422 recompute when update_mode_lines is set too; that means
11423 that people can use force-mode-line-update to request
11424 that the menu bar be recomputed. The adverse effect on
11425 the rest of the redisplay algorithm is about the same as
11426 windows_or_buffers_changed anyway. */
11427 if (windows_or_buffers_changed
11428 /* This used to test w->update_mode_line, but we believe
11429 there is no need to recompute the menu in that case. */
11430 || update_mode_lines
11431 || window_buffer_changed (w))
11433 struct buffer *prev = current_buffer;
11434 ptrdiff_t count = SPECPDL_INDEX ();
11436 specbind (Qinhibit_menubar_update, Qt);
11438 set_buffer_internal_1 (XBUFFER (w->contents));
11439 if (save_match_data)
11440 record_unwind_save_match_data ();
11441 if (NILP (Voverriding_local_map_menu_flag))
11443 specbind (Qoverriding_terminal_local_map, Qnil);
11444 specbind (Qoverriding_local_map, Qnil);
11447 if (!hooks_run)
11449 /* Run the Lucid hook. */
11450 safe_run_hooks (Qactivate_menubar_hook);
11452 /* If it has changed current-menubar from previous value,
11453 really recompute the menu-bar from the value. */
11454 if (! NILP (Vlucid_menu_bar_dirty_flag))
11455 call0 (Qrecompute_lucid_menubar);
11457 safe_run_hooks (Qmenu_bar_update_hook);
11459 hooks_run = 1;
11462 XSETFRAME (Vmenu_updating_frame, f);
11463 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11465 /* Redisplay the menu bar in case we changed it. */
11466 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11467 || defined (HAVE_NS) || defined (USE_GTK)
11468 if (FRAME_WINDOW_P (f))
11470 #if defined (HAVE_NS)
11471 /* All frames on Mac OS share the same menubar. So only
11472 the selected frame should be allowed to set it. */
11473 if (f == SELECTED_FRAME ())
11474 #endif
11475 set_frame_menubar (f, 0, 0);
11477 else
11478 /* On a terminal screen, the menu bar is an ordinary screen
11479 line, and this makes it get updated. */
11480 w->update_mode_line = 1;
11481 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11482 /* In the non-toolkit version, the menu bar is an ordinary screen
11483 line, and this makes it get updated. */
11484 w->update_mode_line = 1;
11485 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11487 unbind_to (count, Qnil);
11488 set_buffer_internal_1 (prev);
11492 return hooks_run;
11495 /***********************************************************************
11496 Tool-bars
11497 ***********************************************************************/
11499 #ifdef HAVE_WINDOW_SYSTEM
11501 /* Where the mouse was last time we reported a mouse event. */
11503 struct frame *last_mouse_frame;
11505 /* Tool-bar item index of the item on which a mouse button was pressed
11506 or -1. */
11508 int last_tool_bar_item;
11510 /* Select `frame' temporarily without running all the code in
11511 do_switch_frame.
11512 FIXME: Maybe do_switch_frame should be trimmed down similarly
11513 when `norecord' is set. */
11514 static void
11515 fast_set_selected_frame (Lisp_Object frame)
11517 if (!EQ (selected_frame, frame))
11519 selected_frame = frame;
11520 selected_window = XFRAME (frame)->selected_window;
11524 /* Update the tool-bar item list for frame F. This has to be done
11525 before we start to fill in any display lines. Called from
11526 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11527 and restore it here. */
11529 static void
11530 update_tool_bar (struct frame *f, int save_match_data)
11532 #if defined (USE_GTK) || defined (HAVE_NS)
11533 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11534 #else
11535 int do_update = WINDOWP (f->tool_bar_window)
11536 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11537 #endif
11539 if (do_update)
11541 Lisp_Object window;
11542 struct window *w;
11544 window = FRAME_SELECTED_WINDOW (f);
11545 w = XWINDOW (window);
11547 /* If the user has switched buffers or windows, we need to
11548 recompute to reflect the new bindings. But we'll
11549 recompute when update_mode_lines is set too; that means
11550 that people can use force-mode-line-update to request
11551 that the menu bar be recomputed. The adverse effect on
11552 the rest of the redisplay algorithm is about the same as
11553 windows_or_buffers_changed anyway. */
11554 if (windows_or_buffers_changed
11555 || w->update_mode_line
11556 || update_mode_lines
11557 || window_buffer_changed (w))
11559 struct buffer *prev = current_buffer;
11560 ptrdiff_t count = SPECPDL_INDEX ();
11561 Lisp_Object frame, new_tool_bar;
11562 int new_n_tool_bar;
11563 struct gcpro gcpro1;
11565 /* Set current_buffer to the buffer of the selected
11566 window of the frame, so that we get the right local
11567 keymaps. */
11568 set_buffer_internal_1 (XBUFFER (w->contents));
11570 /* Save match data, if we must. */
11571 if (save_match_data)
11572 record_unwind_save_match_data ();
11574 /* Make sure that we don't accidentally use bogus keymaps. */
11575 if (NILP (Voverriding_local_map_menu_flag))
11577 specbind (Qoverriding_terminal_local_map, Qnil);
11578 specbind (Qoverriding_local_map, Qnil);
11581 GCPRO1 (new_tool_bar);
11583 /* We must temporarily set the selected frame to this frame
11584 before calling tool_bar_items, because the calculation of
11585 the tool-bar keymap uses the selected frame (see
11586 `tool-bar-make-keymap' in tool-bar.el). */
11587 eassert (EQ (selected_window,
11588 /* Since we only explicitly preserve selected_frame,
11589 check that selected_window would be redundant. */
11590 XFRAME (selected_frame)->selected_window));
11591 record_unwind_protect (fast_set_selected_frame, selected_frame);
11592 XSETFRAME (frame, f);
11593 fast_set_selected_frame (frame);
11595 /* Build desired tool-bar items from keymaps. */
11596 new_tool_bar
11597 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11598 &new_n_tool_bar);
11600 /* Redisplay the tool-bar if we changed it. */
11601 if (new_n_tool_bar != f->n_tool_bar_items
11602 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11604 /* Redisplay that happens asynchronously due to an expose event
11605 may access f->tool_bar_items. Make sure we update both
11606 variables within BLOCK_INPUT so no such event interrupts. */
11607 block_input ();
11608 fset_tool_bar_items (f, new_tool_bar);
11609 f->n_tool_bar_items = new_n_tool_bar;
11610 w->update_mode_line = 1;
11611 unblock_input ();
11614 UNGCPRO;
11616 unbind_to (count, Qnil);
11617 set_buffer_internal_1 (prev);
11622 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11624 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11625 F's desired tool-bar contents. F->tool_bar_items must have
11626 been set up previously by calling prepare_menu_bars. */
11628 static void
11629 build_desired_tool_bar_string (struct frame *f)
11631 int i, size, size_needed;
11632 struct gcpro gcpro1, gcpro2, gcpro3;
11633 Lisp_Object image, plist, props;
11635 image = plist = props = Qnil;
11636 GCPRO3 (image, plist, props);
11638 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11639 Otherwise, make a new string. */
11641 /* The size of the string we might be able to reuse. */
11642 size = (STRINGP (f->desired_tool_bar_string)
11643 ? SCHARS (f->desired_tool_bar_string)
11644 : 0);
11646 /* We need one space in the string for each image. */
11647 size_needed = f->n_tool_bar_items;
11649 /* Reuse f->desired_tool_bar_string, if possible. */
11650 if (size < size_needed || NILP (f->desired_tool_bar_string))
11651 fset_desired_tool_bar_string
11652 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11653 else
11655 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11656 Fremove_text_properties (make_number (0), make_number (size),
11657 props, f->desired_tool_bar_string);
11660 /* Put a `display' property on the string for the images to display,
11661 put a `menu_item' property on tool-bar items with a value that
11662 is the index of the item in F's tool-bar item vector. */
11663 for (i = 0; i < f->n_tool_bar_items; ++i)
11665 #define PROP(IDX) \
11666 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11668 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11669 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11670 int hmargin, vmargin, relief, idx, end;
11672 /* If image is a vector, choose the image according to the
11673 button state. */
11674 image = PROP (TOOL_BAR_ITEM_IMAGES);
11675 if (VECTORP (image))
11677 if (enabled_p)
11678 idx = (selected_p
11679 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11680 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11681 else
11682 idx = (selected_p
11683 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11684 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11686 eassert (ASIZE (image) >= idx);
11687 image = AREF (image, idx);
11689 else
11690 idx = -1;
11692 /* Ignore invalid image specifications. */
11693 if (!valid_image_p (image))
11694 continue;
11696 /* Display the tool-bar button pressed, or depressed. */
11697 plist = Fcopy_sequence (XCDR (image));
11699 /* Compute margin and relief to draw. */
11700 relief = (tool_bar_button_relief >= 0
11701 ? tool_bar_button_relief
11702 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11703 hmargin = vmargin = relief;
11705 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11706 INT_MAX - max (hmargin, vmargin)))
11708 hmargin += XFASTINT (Vtool_bar_button_margin);
11709 vmargin += XFASTINT (Vtool_bar_button_margin);
11711 else if (CONSP (Vtool_bar_button_margin))
11713 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11714 INT_MAX - hmargin))
11715 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11717 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11718 INT_MAX - vmargin))
11719 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11722 if (auto_raise_tool_bar_buttons_p)
11724 /* Add a `:relief' property to the image spec if the item is
11725 selected. */
11726 if (selected_p)
11728 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11729 hmargin -= relief;
11730 vmargin -= relief;
11733 else
11735 /* If image is selected, display it pressed, i.e. with a
11736 negative relief. If it's not selected, display it with a
11737 raised relief. */
11738 plist = Fplist_put (plist, QCrelief,
11739 (selected_p
11740 ? make_number (-relief)
11741 : make_number (relief)));
11742 hmargin -= relief;
11743 vmargin -= relief;
11746 /* Put a margin around the image. */
11747 if (hmargin || vmargin)
11749 if (hmargin == vmargin)
11750 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11751 else
11752 plist = Fplist_put (plist, QCmargin,
11753 Fcons (make_number (hmargin),
11754 make_number (vmargin)));
11757 /* If button is not enabled, and we don't have special images
11758 for the disabled state, make the image appear disabled by
11759 applying an appropriate algorithm to it. */
11760 if (!enabled_p && idx < 0)
11761 plist = Fplist_put (plist, QCconversion, Qdisabled);
11763 /* Put a `display' text property on the string for the image to
11764 display. Put a `menu-item' property on the string that gives
11765 the start of this item's properties in the tool-bar items
11766 vector. */
11767 image = Fcons (Qimage, plist);
11768 props = list4 (Qdisplay, image,
11769 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11771 /* Let the last image hide all remaining spaces in the tool bar
11772 string. The string can be longer than needed when we reuse a
11773 previous string. */
11774 if (i + 1 == f->n_tool_bar_items)
11775 end = SCHARS (f->desired_tool_bar_string);
11776 else
11777 end = i + 1;
11778 Fadd_text_properties (make_number (i), make_number (end),
11779 props, f->desired_tool_bar_string);
11780 #undef PROP
11783 UNGCPRO;
11787 /* Display one line of the tool-bar of frame IT->f.
11789 HEIGHT specifies the desired height of the tool-bar line.
11790 If the actual height of the glyph row is less than HEIGHT, the
11791 row's height is increased to HEIGHT, and the icons are centered
11792 vertically in the new height.
11794 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11795 count a final empty row in case the tool-bar width exactly matches
11796 the window width.
11799 static void
11800 display_tool_bar_line (struct it *it, int height)
11802 struct glyph_row *row = it->glyph_row;
11803 int max_x = it->last_visible_x;
11804 struct glyph *last;
11806 prepare_desired_row (row);
11807 row->y = it->current_y;
11809 /* Note that this isn't made use of if the face hasn't a box,
11810 so there's no need to check the face here. */
11811 it->start_of_box_run_p = 1;
11813 while (it->current_x < max_x)
11815 int x, n_glyphs_before, i, nglyphs;
11816 struct it it_before;
11818 /* Get the next display element. */
11819 if (!get_next_display_element (it))
11821 /* Don't count empty row if we are counting needed tool-bar lines. */
11822 if (height < 0 && !it->hpos)
11823 return;
11824 break;
11827 /* Produce glyphs. */
11828 n_glyphs_before = row->used[TEXT_AREA];
11829 it_before = *it;
11831 PRODUCE_GLYPHS (it);
11833 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11834 i = 0;
11835 x = it_before.current_x;
11836 while (i < nglyphs)
11838 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11840 if (x + glyph->pixel_width > max_x)
11842 /* Glyph doesn't fit on line. Backtrack. */
11843 row->used[TEXT_AREA] = n_glyphs_before;
11844 *it = it_before;
11845 /* If this is the only glyph on this line, it will never fit on the
11846 tool-bar, so skip it. But ensure there is at least one glyph,
11847 so we don't accidentally disable the tool-bar. */
11848 if (n_glyphs_before == 0
11849 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11850 break;
11851 goto out;
11854 ++it->hpos;
11855 x += glyph->pixel_width;
11856 ++i;
11859 /* Stop at line end. */
11860 if (ITERATOR_AT_END_OF_LINE_P (it))
11861 break;
11863 set_iterator_to_next (it, 1);
11866 out:;
11868 row->displays_text_p = row->used[TEXT_AREA] != 0;
11870 /* Use default face for the border below the tool bar.
11872 FIXME: When auto-resize-tool-bars is grow-only, there is
11873 no additional border below the possibly empty tool-bar lines.
11874 So to make the extra empty lines look "normal", we have to
11875 use the tool-bar face for the border too. */
11876 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11877 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11878 it->face_id = DEFAULT_FACE_ID;
11880 extend_face_to_end_of_line (it);
11881 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11882 last->right_box_line_p = 1;
11883 if (last == row->glyphs[TEXT_AREA])
11884 last->left_box_line_p = 1;
11886 /* Make line the desired height and center it vertically. */
11887 if ((height -= it->max_ascent + it->max_descent) > 0)
11889 /* Don't add more than one line height. */
11890 height %= FRAME_LINE_HEIGHT (it->f);
11891 it->max_ascent += height / 2;
11892 it->max_descent += (height + 1) / 2;
11895 compute_line_metrics (it);
11897 /* If line is empty, make it occupy the rest of the tool-bar. */
11898 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11900 row->height = row->phys_height = it->last_visible_y - row->y;
11901 row->visible_height = row->height;
11902 row->ascent = row->phys_ascent = 0;
11903 row->extra_line_spacing = 0;
11906 row->full_width_p = 1;
11907 row->continued_p = 0;
11908 row->truncated_on_left_p = 0;
11909 row->truncated_on_right_p = 0;
11911 it->current_x = it->hpos = 0;
11912 it->current_y += row->height;
11913 ++it->vpos;
11914 ++it->glyph_row;
11918 /* Max tool-bar height. */
11920 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11921 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11923 /* Value is the number of screen lines needed to make all tool-bar
11924 items of frame F visible. The number of actual rows needed is
11925 returned in *N_ROWS if non-NULL. */
11927 static int
11928 tool_bar_lines_needed (struct frame *f, int *n_rows)
11930 struct window *w = XWINDOW (f->tool_bar_window);
11931 struct it it;
11932 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11933 the desired matrix, so use (unused) mode-line row as temporary row to
11934 avoid destroying the first tool-bar row. */
11935 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11937 /* Initialize an iterator for iteration over
11938 F->desired_tool_bar_string in the tool-bar window of frame F. */
11939 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11940 it.first_visible_x = 0;
11941 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11942 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11943 it.paragraph_embedding = L2R;
11945 while (!ITERATOR_AT_END_P (&it))
11947 clear_glyph_row (temp_row);
11948 it.glyph_row = temp_row;
11949 display_tool_bar_line (&it, -1);
11951 clear_glyph_row (temp_row);
11953 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11954 if (n_rows)
11955 *n_rows = it.vpos > 0 ? it.vpos : -1;
11957 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11960 #endif /* !USE_GTK && !HAVE_NS */
11962 #if defined USE_GTK || defined HAVE_NS
11963 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11964 #endif
11966 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11967 0, 1, 0,
11968 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11969 If FRAME is nil or omitted, use the selected frame. */)
11970 (Lisp_Object frame)
11972 int nlines = 0;
11973 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11974 struct frame *f = decode_any_frame (frame);
11975 struct window *w;
11977 if (WINDOWP (f->tool_bar_window)
11978 && (w = XWINDOW (f->tool_bar_window),
11979 WINDOW_TOTAL_LINES (w) > 0))
11981 update_tool_bar (f, 1);
11982 if (f->n_tool_bar_items)
11984 build_desired_tool_bar_string (f);
11985 nlines = tool_bar_lines_needed (f, NULL);
11988 #endif
11989 return make_number (nlines);
11993 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11994 height should be changed. */
11996 static int
11997 redisplay_tool_bar (struct frame *f)
11999 #if defined (USE_GTK) || defined (HAVE_NS)
12001 if (FRAME_EXTERNAL_TOOL_BAR (f))
12002 update_frame_tool_bar (f);
12003 return 0;
12005 #else /* !USE_GTK && !HAVE_NS */
12007 struct window *w;
12008 struct it it;
12009 struct glyph_row *row;
12011 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12012 do anything. This means you must start with tool-bar-lines
12013 non-zero to get the auto-sizing effect. Or in other words, you
12014 can turn off tool-bars by specifying tool-bar-lines zero. */
12015 if (!WINDOWP (f->tool_bar_window)
12016 || (w = XWINDOW (f->tool_bar_window),
12017 WINDOW_TOTAL_LINES (w) == 0))
12018 return 0;
12020 /* Set up an iterator for the tool-bar window. */
12021 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12022 it.first_visible_x = 0;
12023 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12024 row = it.glyph_row;
12026 /* Build a string that represents the contents of the tool-bar. */
12027 build_desired_tool_bar_string (f);
12028 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12029 /* FIXME: This should be controlled by a user option. But it
12030 doesn't make sense to have an R2L tool bar if the menu bar cannot
12031 be drawn also R2L, and making the menu bar R2L is tricky due
12032 toolkit-specific code that implements it. If an R2L tool bar is
12033 ever supported, display_tool_bar_line should also be augmented to
12034 call unproduce_glyphs like display_line and display_string
12035 do. */
12036 it.paragraph_embedding = L2R;
12038 if (f->n_tool_bar_rows == 0)
12040 int nlines;
12042 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12043 nlines != WINDOW_TOTAL_LINES (w)))
12045 Lisp_Object frame;
12046 int old_height = WINDOW_TOTAL_LINES (w);
12048 XSETFRAME (frame, f);
12049 Fmodify_frame_parameters (frame,
12050 list1 (Fcons (Qtool_bar_lines,
12051 make_number (nlines))));
12052 if (WINDOW_TOTAL_LINES (w) != old_height)
12054 clear_glyph_matrix (w->desired_matrix);
12055 f->fonts_changed = 1;
12056 return 1;
12061 /* Display as many lines as needed to display all tool-bar items. */
12063 if (f->n_tool_bar_rows > 0)
12065 int border, rows, height, extra;
12067 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12068 border = XINT (Vtool_bar_border);
12069 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12070 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12071 else if (EQ (Vtool_bar_border, Qborder_width))
12072 border = f->border_width;
12073 else
12074 border = 0;
12075 if (border < 0)
12076 border = 0;
12078 rows = f->n_tool_bar_rows;
12079 height = max (1, (it.last_visible_y - border) / rows);
12080 extra = it.last_visible_y - border - height * rows;
12082 while (it.current_y < it.last_visible_y)
12084 int h = 0;
12085 if (extra > 0 && rows-- > 0)
12087 h = (extra + rows - 1) / rows;
12088 extra -= h;
12090 display_tool_bar_line (&it, height + h);
12093 else
12095 while (it.current_y < it.last_visible_y)
12096 display_tool_bar_line (&it, 0);
12099 /* It doesn't make much sense to try scrolling in the tool-bar
12100 window, so don't do it. */
12101 w->desired_matrix->no_scrolling_p = 1;
12102 w->must_be_updated_p = 1;
12104 if (!NILP (Vauto_resize_tool_bars))
12106 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12107 int change_height_p = 0;
12109 /* If we couldn't display everything, change the tool-bar's
12110 height if there is room for more. */
12111 if (IT_STRING_CHARPOS (it) < it.end_charpos
12112 && it.current_y < max_tool_bar_height)
12113 change_height_p = 1;
12115 row = it.glyph_row - 1;
12117 /* If there are blank lines at the end, except for a partially
12118 visible blank line at the end that is smaller than
12119 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12120 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12121 && row->height >= FRAME_LINE_HEIGHT (f))
12122 change_height_p = 1;
12124 /* If row displays tool-bar items, but is partially visible,
12125 change the tool-bar's height. */
12126 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12127 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12128 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12129 change_height_p = 1;
12131 /* Resize windows as needed by changing the `tool-bar-lines'
12132 frame parameter. */
12133 if (change_height_p)
12135 Lisp_Object frame;
12136 int old_height = WINDOW_TOTAL_LINES (w);
12137 int nrows;
12138 int nlines = tool_bar_lines_needed (f, &nrows);
12140 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12141 && !f->minimize_tool_bar_window_p)
12142 ? (nlines > old_height)
12143 : (nlines != old_height));
12144 f->minimize_tool_bar_window_p = 0;
12146 if (change_height_p)
12148 XSETFRAME (frame, f);
12149 Fmodify_frame_parameters (frame,
12150 list1 (Fcons (Qtool_bar_lines,
12151 make_number (nlines))));
12152 if (WINDOW_TOTAL_LINES (w) != old_height)
12154 clear_glyph_matrix (w->desired_matrix);
12155 f->n_tool_bar_rows = nrows;
12156 f->fonts_changed = 1;
12157 return 1;
12163 f->minimize_tool_bar_window_p = 0;
12164 return 0;
12166 #endif /* USE_GTK || HAVE_NS */
12169 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12171 /* Get information about the tool-bar item which is displayed in GLYPH
12172 on frame F. Return in *PROP_IDX the index where tool-bar item
12173 properties start in F->tool_bar_items. Value is zero if
12174 GLYPH doesn't display a tool-bar item. */
12176 static int
12177 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12179 Lisp_Object prop;
12180 int success_p;
12181 int charpos;
12183 /* This function can be called asynchronously, which means we must
12184 exclude any possibility that Fget_text_property signals an
12185 error. */
12186 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12187 charpos = max (0, charpos);
12189 /* Get the text property `menu-item' at pos. The value of that
12190 property is the start index of this item's properties in
12191 F->tool_bar_items. */
12192 prop = Fget_text_property (make_number (charpos),
12193 Qmenu_item, f->current_tool_bar_string);
12194 if (INTEGERP (prop))
12196 *prop_idx = XINT (prop);
12197 success_p = 1;
12199 else
12200 success_p = 0;
12202 return success_p;
12206 /* Get information about the tool-bar item at position X/Y on frame F.
12207 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12208 the current matrix of the tool-bar window of F, or NULL if not
12209 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12210 item in F->tool_bar_items. Value is
12212 -1 if X/Y is not on a tool-bar item
12213 0 if X/Y is on the same item that was highlighted before.
12214 1 otherwise. */
12216 static int
12217 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12218 int *hpos, int *vpos, int *prop_idx)
12220 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12221 struct window *w = XWINDOW (f->tool_bar_window);
12222 int area;
12224 /* Find the glyph under X/Y. */
12225 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12226 if (*glyph == NULL)
12227 return -1;
12229 /* Get the start of this tool-bar item's properties in
12230 f->tool_bar_items. */
12231 if (!tool_bar_item_info (f, *glyph, prop_idx))
12232 return -1;
12234 /* Is mouse on the highlighted item? */
12235 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12236 && *vpos >= hlinfo->mouse_face_beg_row
12237 && *vpos <= hlinfo->mouse_face_end_row
12238 && (*vpos > hlinfo->mouse_face_beg_row
12239 || *hpos >= hlinfo->mouse_face_beg_col)
12240 && (*vpos < hlinfo->mouse_face_end_row
12241 || *hpos < hlinfo->mouse_face_end_col
12242 || hlinfo->mouse_face_past_end))
12243 return 0;
12245 return 1;
12249 /* EXPORT:
12250 Handle mouse button event on the tool-bar of frame F, at
12251 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12252 0 for button release. MODIFIERS is event modifiers for button
12253 release. */
12255 void
12256 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12257 int modifiers)
12259 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12260 struct window *w = XWINDOW (f->tool_bar_window);
12261 int hpos, vpos, prop_idx;
12262 struct glyph *glyph;
12263 Lisp_Object enabled_p;
12264 int ts;
12266 /* If not on the highlighted tool-bar item, and mouse-highlight is
12267 non-nil, return. This is so we generate the tool-bar button
12268 click only when the mouse button is released on the same item as
12269 where it was pressed. However, when mouse-highlight is disabled,
12270 generate the click when the button is released regardless of the
12271 highlight, since tool-bar items are not highlighted in that
12272 case. */
12273 frame_to_window_pixel_xy (w, &x, &y);
12274 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12275 if (ts == -1
12276 || (ts != 0 && !NILP (Vmouse_highlight)))
12277 return;
12279 /* When mouse-highlight is off, generate the click for the item
12280 where the button was pressed, disregarding where it was
12281 released. */
12282 if (NILP (Vmouse_highlight) && !down_p)
12283 prop_idx = last_tool_bar_item;
12285 /* If item is disabled, do nothing. */
12286 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12287 if (NILP (enabled_p))
12288 return;
12290 if (down_p)
12292 /* Show item in pressed state. */
12293 if (!NILP (Vmouse_highlight))
12294 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12295 last_tool_bar_item = prop_idx;
12297 else
12299 Lisp_Object key, frame;
12300 struct input_event event;
12301 EVENT_INIT (event);
12303 /* Show item in released state. */
12304 if (!NILP (Vmouse_highlight))
12305 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12307 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12309 XSETFRAME (frame, f);
12310 event.kind = TOOL_BAR_EVENT;
12311 event.frame_or_window = frame;
12312 event.arg = frame;
12313 kbd_buffer_store_event (&event);
12315 event.kind = TOOL_BAR_EVENT;
12316 event.frame_or_window = frame;
12317 event.arg = key;
12318 event.modifiers = modifiers;
12319 kbd_buffer_store_event (&event);
12320 last_tool_bar_item = -1;
12325 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12326 tool-bar window-relative coordinates X/Y. Called from
12327 note_mouse_highlight. */
12329 static void
12330 note_tool_bar_highlight (struct frame *f, int x, int y)
12332 Lisp_Object window = f->tool_bar_window;
12333 struct window *w = XWINDOW (window);
12334 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12335 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12336 int hpos, vpos;
12337 struct glyph *glyph;
12338 struct glyph_row *row;
12339 int i;
12340 Lisp_Object enabled_p;
12341 int prop_idx;
12342 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12343 int mouse_down_p, rc;
12345 /* Function note_mouse_highlight is called with negative X/Y
12346 values when mouse moves outside of the frame. */
12347 if (x <= 0 || y <= 0)
12349 clear_mouse_face (hlinfo);
12350 return;
12353 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12354 if (rc < 0)
12356 /* Not on tool-bar item. */
12357 clear_mouse_face (hlinfo);
12358 return;
12360 else if (rc == 0)
12361 /* On same tool-bar item as before. */
12362 goto set_help_echo;
12364 clear_mouse_face (hlinfo);
12366 /* Mouse is down, but on different tool-bar item? */
12367 mouse_down_p = (dpyinfo->grabbed
12368 && f == last_mouse_frame
12369 && FRAME_LIVE_P (f));
12370 if (mouse_down_p
12371 && last_tool_bar_item != prop_idx)
12372 return;
12374 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12376 /* If tool-bar item is not enabled, don't highlight it. */
12377 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12378 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12380 /* Compute the x-position of the glyph. In front and past the
12381 image is a space. We include this in the highlighted area. */
12382 row = MATRIX_ROW (w->current_matrix, vpos);
12383 for (i = x = 0; i < hpos; ++i)
12384 x += row->glyphs[TEXT_AREA][i].pixel_width;
12386 /* Record this as the current active region. */
12387 hlinfo->mouse_face_beg_col = hpos;
12388 hlinfo->mouse_face_beg_row = vpos;
12389 hlinfo->mouse_face_beg_x = x;
12390 hlinfo->mouse_face_past_end = 0;
12392 hlinfo->mouse_face_end_col = hpos + 1;
12393 hlinfo->mouse_face_end_row = vpos;
12394 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12395 hlinfo->mouse_face_window = window;
12396 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12398 /* Display it as active. */
12399 show_mouse_face (hlinfo, draw);
12402 set_help_echo:
12404 /* Set help_echo_string to a help string to display for this tool-bar item.
12405 XTread_socket does the rest. */
12406 help_echo_object = help_echo_window = Qnil;
12407 help_echo_pos = -1;
12408 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12409 if (NILP (help_echo_string))
12410 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12413 #endif /* !USE_GTK && !HAVE_NS */
12415 #endif /* HAVE_WINDOW_SYSTEM */
12419 /************************************************************************
12420 Horizontal scrolling
12421 ************************************************************************/
12423 static int hscroll_window_tree (Lisp_Object);
12424 static int hscroll_windows (Lisp_Object);
12426 /* For all leaf windows in the window tree rooted at WINDOW, set their
12427 hscroll value so that PT is (i) visible in the window, and (ii) so
12428 that it is not within a certain margin at the window's left and
12429 right border. Value is non-zero if any window's hscroll has been
12430 changed. */
12432 static int
12433 hscroll_window_tree (Lisp_Object window)
12435 int hscrolled_p = 0;
12436 int hscroll_relative_p = FLOATP (Vhscroll_step);
12437 int hscroll_step_abs = 0;
12438 double hscroll_step_rel = 0;
12440 if (hscroll_relative_p)
12442 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12443 if (hscroll_step_rel < 0)
12445 hscroll_relative_p = 0;
12446 hscroll_step_abs = 0;
12449 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12451 hscroll_step_abs = XINT (Vhscroll_step);
12452 if (hscroll_step_abs < 0)
12453 hscroll_step_abs = 0;
12455 else
12456 hscroll_step_abs = 0;
12458 while (WINDOWP (window))
12460 struct window *w = XWINDOW (window);
12462 if (WINDOWP (w->contents))
12463 hscrolled_p |= hscroll_window_tree (w->contents);
12464 else if (w->cursor.vpos >= 0)
12466 int h_margin;
12467 int text_area_width;
12468 struct glyph_row *current_cursor_row
12469 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12470 struct glyph_row *desired_cursor_row
12471 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12472 struct glyph_row *cursor_row
12473 = (desired_cursor_row->enabled_p
12474 ? desired_cursor_row
12475 : current_cursor_row);
12476 int row_r2l_p = cursor_row->reversed_p;
12478 text_area_width = window_box_width (w, TEXT_AREA);
12480 /* Scroll when cursor is inside this scroll margin. */
12481 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12483 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12484 /* For left-to-right rows, hscroll when cursor is either
12485 (i) inside the right hscroll margin, or (ii) if it is
12486 inside the left margin and the window is already
12487 hscrolled. */
12488 && ((!row_r2l_p
12489 && ((w->hscroll
12490 && w->cursor.x <= h_margin)
12491 || (cursor_row->enabled_p
12492 && cursor_row->truncated_on_right_p
12493 && (w->cursor.x >= text_area_width - h_margin))))
12494 /* For right-to-left rows, the logic is similar,
12495 except that rules for scrolling to left and right
12496 are reversed. E.g., if cursor.x <= h_margin, we
12497 need to hscroll "to the right" unconditionally,
12498 and that will scroll the screen to the left so as
12499 to reveal the next portion of the row. */
12500 || (row_r2l_p
12501 && ((cursor_row->enabled_p
12502 /* FIXME: It is confusing to set the
12503 truncated_on_right_p flag when R2L rows
12504 are actually truncated on the left. */
12505 && cursor_row->truncated_on_right_p
12506 && w->cursor.x <= h_margin)
12507 || (w->hscroll
12508 && (w->cursor.x >= text_area_width - h_margin))))))
12510 struct it it;
12511 ptrdiff_t hscroll;
12512 struct buffer *saved_current_buffer;
12513 ptrdiff_t pt;
12514 int wanted_x;
12516 /* Find point in a display of infinite width. */
12517 saved_current_buffer = current_buffer;
12518 current_buffer = XBUFFER (w->contents);
12520 if (w == XWINDOW (selected_window))
12521 pt = PT;
12522 else
12523 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12525 /* Move iterator to pt starting at cursor_row->start in
12526 a line with infinite width. */
12527 init_to_row_start (&it, w, cursor_row);
12528 it.last_visible_x = INFINITY;
12529 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12530 current_buffer = saved_current_buffer;
12532 /* Position cursor in window. */
12533 if (!hscroll_relative_p && hscroll_step_abs == 0)
12534 hscroll = max (0, (it.current_x
12535 - (ITERATOR_AT_END_OF_LINE_P (&it)
12536 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12537 : (text_area_width / 2))))
12538 / FRAME_COLUMN_WIDTH (it.f);
12539 else if ((!row_r2l_p
12540 && w->cursor.x >= text_area_width - h_margin)
12541 || (row_r2l_p && w->cursor.x <= h_margin))
12543 if (hscroll_relative_p)
12544 wanted_x = text_area_width * (1 - hscroll_step_rel)
12545 - h_margin;
12546 else
12547 wanted_x = text_area_width
12548 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12549 - h_margin;
12550 hscroll
12551 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12553 else
12555 if (hscroll_relative_p)
12556 wanted_x = text_area_width * hscroll_step_rel
12557 + h_margin;
12558 else
12559 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12560 + h_margin;
12561 hscroll
12562 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12564 hscroll = max (hscroll, w->min_hscroll);
12566 /* Don't prevent redisplay optimizations if hscroll
12567 hasn't changed, as it will unnecessarily slow down
12568 redisplay. */
12569 if (w->hscroll != hscroll)
12571 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12572 w->hscroll = hscroll;
12573 hscrolled_p = 1;
12578 window = w->next;
12581 /* Value is non-zero if hscroll of any leaf window has been changed. */
12582 return hscrolled_p;
12586 /* Set hscroll so that cursor is visible and not inside horizontal
12587 scroll margins for all windows in the tree rooted at WINDOW. See
12588 also hscroll_window_tree above. Value is non-zero if any window's
12589 hscroll has been changed. If it has, desired matrices on the frame
12590 of WINDOW are cleared. */
12592 static int
12593 hscroll_windows (Lisp_Object window)
12595 int hscrolled_p = hscroll_window_tree (window);
12596 if (hscrolled_p)
12597 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12598 return hscrolled_p;
12603 /************************************************************************
12604 Redisplay
12605 ************************************************************************/
12607 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12608 to a non-zero value. This is sometimes handy to have in a debugger
12609 session. */
12611 #ifdef GLYPH_DEBUG
12613 /* First and last unchanged row for try_window_id. */
12615 static int debug_first_unchanged_at_end_vpos;
12616 static int debug_last_unchanged_at_beg_vpos;
12618 /* Delta vpos and y. */
12620 static int debug_dvpos, debug_dy;
12622 /* Delta in characters and bytes for try_window_id. */
12624 static ptrdiff_t debug_delta, debug_delta_bytes;
12626 /* Values of window_end_pos and window_end_vpos at the end of
12627 try_window_id. */
12629 static ptrdiff_t debug_end_vpos;
12631 /* Append a string to W->desired_matrix->method. FMT is a printf
12632 format string. If trace_redisplay_p is non-zero also printf the
12633 resulting string to stderr. */
12635 static void debug_method_add (struct window *, char const *, ...)
12636 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12638 static void
12639 debug_method_add (struct window *w, char const *fmt, ...)
12641 void *ptr = w;
12642 char *method = w->desired_matrix->method;
12643 int len = strlen (method);
12644 int size = sizeof w->desired_matrix->method;
12645 int remaining = size - len - 1;
12646 va_list ap;
12648 if (len && remaining)
12650 method[len] = '|';
12651 --remaining, ++len;
12654 va_start (ap, fmt);
12655 vsnprintf (method + len, remaining + 1, fmt, ap);
12656 va_end (ap);
12658 if (trace_redisplay_p)
12659 fprintf (stderr, "%p (%s): %s\n",
12660 ptr,
12661 ((BUFFERP (w->contents)
12662 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12663 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12664 : "no buffer"),
12665 method + len);
12668 #endif /* GLYPH_DEBUG */
12671 /* Value is non-zero if all changes in window W, which displays
12672 current_buffer, are in the text between START and END. START is a
12673 buffer position, END is given as a distance from Z. Used in
12674 redisplay_internal for display optimization. */
12676 static int
12677 text_outside_line_unchanged_p (struct window *w,
12678 ptrdiff_t start, ptrdiff_t end)
12680 int unchanged_p = 1;
12682 /* If text or overlays have changed, see where. */
12683 if (window_outdated (w))
12685 /* Gap in the line? */
12686 if (GPT < start || Z - GPT < end)
12687 unchanged_p = 0;
12689 /* Changes start in front of the line, or end after it? */
12690 if (unchanged_p
12691 && (BEG_UNCHANGED < start - 1
12692 || END_UNCHANGED < end))
12693 unchanged_p = 0;
12695 /* If selective display, can't optimize if changes start at the
12696 beginning of the line. */
12697 if (unchanged_p
12698 && INTEGERP (BVAR (current_buffer, selective_display))
12699 && XINT (BVAR (current_buffer, selective_display)) > 0
12700 && (BEG_UNCHANGED < start || GPT <= start))
12701 unchanged_p = 0;
12703 /* If there are overlays at the start or end of the line, these
12704 may have overlay strings with newlines in them. A change at
12705 START, for instance, may actually concern the display of such
12706 overlay strings as well, and they are displayed on different
12707 lines. So, quickly rule out this case. (For the future, it
12708 might be desirable to implement something more telling than
12709 just BEG/END_UNCHANGED.) */
12710 if (unchanged_p)
12712 if (BEG + BEG_UNCHANGED == start
12713 && overlay_touches_p (start))
12714 unchanged_p = 0;
12715 if (END_UNCHANGED == end
12716 && overlay_touches_p (Z - end))
12717 unchanged_p = 0;
12720 /* Under bidi reordering, adding or deleting a character in the
12721 beginning of a paragraph, before the first strong directional
12722 character, can change the base direction of the paragraph (unless
12723 the buffer specifies a fixed paragraph direction), which will
12724 require to redisplay the whole paragraph. It might be worthwhile
12725 to find the paragraph limits and widen the range of redisplayed
12726 lines to that, but for now just give up this optimization. */
12727 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12728 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12729 unchanged_p = 0;
12732 return unchanged_p;
12736 /* Do a frame update, taking possible shortcuts into account. This is
12737 the main external entry point for redisplay.
12739 If the last redisplay displayed an echo area message and that message
12740 is no longer requested, we clear the echo area or bring back the
12741 mini-buffer if that is in use. */
12743 void
12744 redisplay (void)
12746 redisplay_internal ();
12750 static Lisp_Object
12751 overlay_arrow_string_or_property (Lisp_Object var)
12753 Lisp_Object val;
12755 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12756 return val;
12758 return Voverlay_arrow_string;
12761 /* Return 1 if there are any overlay-arrows in current_buffer. */
12762 static int
12763 overlay_arrow_in_current_buffer_p (void)
12765 Lisp_Object vlist;
12767 for (vlist = Voverlay_arrow_variable_list;
12768 CONSP (vlist);
12769 vlist = XCDR (vlist))
12771 Lisp_Object var = XCAR (vlist);
12772 Lisp_Object val;
12774 if (!SYMBOLP (var))
12775 continue;
12776 val = find_symbol_value (var);
12777 if (MARKERP (val)
12778 && current_buffer == XMARKER (val)->buffer)
12779 return 1;
12781 return 0;
12785 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12786 has changed. */
12788 static int
12789 overlay_arrows_changed_p (void)
12791 Lisp_Object vlist;
12793 for (vlist = Voverlay_arrow_variable_list;
12794 CONSP (vlist);
12795 vlist = XCDR (vlist))
12797 Lisp_Object var = XCAR (vlist);
12798 Lisp_Object val, pstr;
12800 if (!SYMBOLP (var))
12801 continue;
12802 val = find_symbol_value (var);
12803 if (!MARKERP (val))
12804 continue;
12805 if (! EQ (COERCE_MARKER (val),
12806 Fget (var, Qlast_arrow_position))
12807 || ! (pstr = overlay_arrow_string_or_property (var),
12808 EQ (pstr, Fget (var, Qlast_arrow_string))))
12809 return 1;
12811 return 0;
12814 /* Mark overlay arrows to be updated on next redisplay. */
12816 static void
12817 update_overlay_arrows (int up_to_date)
12819 Lisp_Object vlist;
12821 for (vlist = Voverlay_arrow_variable_list;
12822 CONSP (vlist);
12823 vlist = XCDR (vlist))
12825 Lisp_Object var = XCAR (vlist);
12827 if (!SYMBOLP (var))
12828 continue;
12830 if (up_to_date > 0)
12832 Lisp_Object val = find_symbol_value (var);
12833 Fput (var, Qlast_arrow_position,
12834 COERCE_MARKER (val));
12835 Fput (var, Qlast_arrow_string,
12836 overlay_arrow_string_or_property (var));
12838 else if (up_to_date < 0
12839 || !NILP (Fget (var, Qlast_arrow_position)))
12841 Fput (var, Qlast_arrow_position, Qt);
12842 Fput (var, Qlast_arrow_string, Qt);
12848 /* Return overlay arrow string to display at row.
12849 Return integer (bitmap number) for arrow bitmap in left fringe.
12850 Return nil if no overlay arrow. */
12852 static Lisp_Object
12853 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12855 Lisp_Object vlist;
12857 for (vlist = Voverlay_arrow_variable_list;
12858 CONSP (vlist);
12859 vlist = XCDR (vlist))
12861 Lisp_Object var = XCAR (vlist);
12862 Lisp_Object val;
12864 if (!SYMBOLP (var))
12865 continue;
12867 val = find_symbol_value (var);
12869 if (MARKERP (val)
12870 && current_buffer == XMARKER (val)->buffer
12871 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12873 if (FRAME_WINDOW_P (it->f)
12874 /* FIXME: if ROW->reversed_p is set, this should test
12875 the right fringe, not the left one. */
12876 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12878 #ifdef HAVE_WINDOW_SYSTEM
12879 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12881 int fringe_bitmap;
12882 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12883 return make_number (fringe_bitmap);
12885 #endif
12886 return make_number (-1); /* Use default arrow bitmap. */
12888 return overlay_arrow_string_or_property (var);
12892 return Qnil;
12895 /* Return 1 if point moved out of or into a composition. Otherwise
12896 return 0. PREV_BUF and PREV_PT are the last point buffer and
12897 position. BUF and PT are the current point buffer and position. */
12899 static int
12900 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12901 struct buffer *buf, ptrdiff_t pt)
12903 ptrdiff_t start, end;
12904 Lisp_Object prop;
12905 Lisp_Object buffer;
12907 XSETBUFFER (buffer, buf);
12908 /* Check a composition at the last point if point moved within the
12909 same buffer. */
12910 if (prev_buf == buf)
12912 if (prev_pt == pt)
12913 /* Point didn't move. */
12914 return 0;
12916 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12917 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12918 && composition_valid_p (start, end, prop)
12919 && start < prev_pt && end > prev_pt)
12920 /* The last point was within the composition. Return 1 iff
12921 point moved out of the composition. */
12922 return (pt <= start || pt >= end);
12925 /* Check a composition at the current point. */
12926 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12927 && find_composition (pt, -1, &start, &end, &prop, buffer)
12928 && composition_valid_p (start, end, prop)
12929 && start < pt && end > pt);
12932 /* Reconsider the clip changes of buffer which is displayed in W. */
12934 static void
12935 reconsider_clip_changes (struct window *w)
12937 struct buffer *b = XBUFFER (w->contents);
12939 if (b->clip_changed
12940 && w->window_end_valid
12941 && w->current_matrix->buffer == b
12942 && w->current_matrix->zv == BUF_ZV (b)
12943 && w->current_matrix->begv == BUF_BEGV (b))
12944 b->clip_changed = 0;
12946 /* If display wasn't paused, and W is not a tool bar window, see if
12947 point has been moved into or out of a composition. In that case,
12948 we set b->clip_changed to 1 to force updating the screen. If
12949 b->clip_changed has already been set to 1, we can skip this
12950 check. */
12951 if (!b->clip_changed && w->window_end_valid)
12953 ptrdiff_t pt = (w == XWINDOW (selected_window)
12954 ? PT : marker_position (w->pointm));
12956 if ((w->current_matrix->buffer != b || pt != w->last_point)
12957 && check_point_in_composition (w->current_matrix->buffer,
12958 w->last_point, b, pt))
12959 b->clip_changed = 1;
12963 #define STOP_POLLING \
12964 do { if (! polling_stopped_here) stop_polling (); \
12965 polling_stopped_here = 1; } while (0)
12967 #define RESUME_POLLING \
12968 do { if (polling_stopped_here) start_polling (); \
12969 polling_stopped_here = 0; } while (0)
12972 /* Perhaps in the future avoid recentering windows if it
12973 is not necessary; currently that causes some problems. */
12975 static void
12976 redisplay_internal (void)
12978 struct window *w = XWINDOW (selected_window);
12979 struct window *sw;
12980 struct frame *fr;
12981 int pending;
12982 bool must_finish = 0, match_p;
12983 struct text_pos tlbufpos, tlendpos;
12984 int number_of_visible_frames;
12985 ptrdiff_t count;
12986 struct frame *sf;
12987 int polling_stopped_here = 0;
12988 Lisp_Object tail, frame;
12990 /* Non-zero means redisplay has to consider all windows on all
12991 frames. Zero means, only selected_window is considered. */
12992 int consider_all_windows_p;
12994 /* Non-zero means redisplay has to redisplay the miniwindow. */
12995 int update_miniwindow_p = 0;
12997 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12999 /* No redisplay if running in batch mode or frame is not yet fully
13000 initialized, or redisplay is explicitly turned off by setting
13001 Vinhibit_redisplay. */
13002 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13003 || !NILP (Vinhibit_redisplay))
13004 return;
13006 /* Don't examine these until after testing Vinhibit_redisplay.
13007 When Emacs is shutting down, perhaps because its connection to
13008 X has dropped, we should not look at them at all. */
13009 fr = XFRAME (w->frame);
13010 sf = SELECTED_FRAME ();
13012 if (!fr->glyphs_initialized_p)
13013 return;
13015 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13016 if (popup_activated ())
13017 return;
13018 #endif
13020 /* I don't think this happens but let's be paranoid. */
13021 if (redisplaying_p)
13022 return;
13024 /* Record a function that clears redisplaying_p
13025 when we leave this function. */
13026 count = SPECPDL_INDEX ();
13027 record_unwind_protect_void (unwind_redisplay);
13028 redisplaying_p = 1;
13029 specbind (Qinhibit_free_realized_faces, Qnil);
13031 /* Record this function, so it appears on the profiler's backtraces. */
13032 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13034 FOR_EACH_FRAME (tail, frame)
13035 XFRAME (frame)->already_hscrolled_p = 0;
13037 retry:
13038 /* Remember the currently selected window. */
13039 sw = w;
13041 pending = 0;
13042 last_escape_glyph_frame = NULL;
13043 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13044 last_glyphless_glyph_frame = NULL;
13045 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13047 /* If face_change_count is non-zero, init_iterator will free all
13048 realized faces, which includes the faces referenced from current
13049 matrices. So, we can't reuse current matrices in this case. */
13050 if (face_change_count)
13051 ++windows_or_buffers_changed;
13053 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13054 && FRAME_TTY (sf)->previous_frame != sf)
13056 /* Since frames on a single ASCII terminal share the same
13057 display area, displaying a different frame means redisplay
13058 the whole thing. */
13059 windows_or_buffers_changed++;
13060 SET_FRAME_GARBAGED (sf);
13061 #ifndef DOS_NT
13062 set_tty_color_mode (FRAME_TTY (sf), sf);
13063 #endif
13064 FRAME_TTY (sf)->previous_frame = sf;
13067 /* Set the visible flags for all frames. Do this before checking for
13068 resized or garbaged frames; they want to know if their frames are
13069 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13070 number_of_visible_frames = 0;
13072 FOR_EACH_FRAME (tail, frame)
13074 struct frame *f = XFRAME (frame);
13076 if (FRAME_VISIBLE_P (f))
13078 ++number_of_visible_frames;
13079 /* Adjust matrices for visible frames only. */
13080 if (f->fonts_changed)
13082 adjust_frame_glyphs (f);
13083 f->fonts_changed = 0;
13085 /* If cursor type has been changed on the frame
13086 other than selected, consider all frames. */
13087 if (f != sf && f->cursor_type_changed)
13088 update_mode_lines++;
13090 clear_desired_matrices (f);
13093 /* Notice any pending interrupt request to change frame size. */
13094 do_pending_window_change (1);
13096 /* do_pending_window_change could change the selected_window due to
13097 frame resizing which makes the selected window too small. */
13098 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13099 sw = w;
13101 /* Clear frames marked as garbaged. */
13102 clear_garbaged_frames ();
13104 /* Build menubar and tool-bar items. */
13105 if (NILP (Vmemory_full))
13106 prepare_menu_bars ();
13108 if (windows_or_buffers_changed)
13109 update_mode_lines++;
13111 reconsider_clip_changes (w);
13113 /* In most cases selected window displays current buffer. */
13114 match_p = XBUFFER (w->contents) == current_buffer;
13115 if (match_p)
13117 ptrdiff_t count1;
13119 /* Detect case that we need to write or remove a star in the mode line. */
13120 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13122 w->update_mode_line = 1;
13123 if (buffer_shared_and_changed ())
13124 update_mode_lines++;
13127 /* Avoid invocation of point motion hooks by `current_column' below. */
13128 count1 = SPECPDL_INDEX ();
13129 specbind (Qinhibit_point_motion_hooks, Qt);
13131 if (mode_line_update_needed (w))
13132 w->update_mode_line = 1;
13134 unbind_to (count1, Qnil);
13137 consider_all_windows_p = (update_mode_lines
13138 || buffer_shared_and_changed ());
13140 /* If specs for an arrow have changed, do thorough redisplay
13141 to ensure we remove any arrow that should no longer exist. */
13142 if (overlay_arrows_changed_p ())
13143 consider_all_windows_p = windows_or_buffers_changed = 1;
13145 /* Normally the message* functions will have already displayed and
13146 updated the echo area, but the frame may have been trashed, or
13147 the update may have been preempted, so display the echo area
13148 again here. Checking message_cleared_p captures the case that
13149 the echo area should be cleared. */
13150 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13151 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13152 || (message_cleared_p
13153 && minibuf_level == 0
13154 /* If the mini-window is currently selected, this means the
13155 echo-area doesn't show through. */
13156 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13158 int window_height_changed_p = echo_area_display (0);
13160 if (message_cleared_p)
13161 update_miniwindow_p = 1;
13163 must_finish = 1;
13165 /* If we don't display the current message, don't clear the
13166 message_cleared_p flag, because, if we did, we wouldn't clear
13167 the echo area in the next redisplay which doesn't preserve
13168 the echo area. */
13169 if (!display_last_displayed_message_p)
13170 message_cleared_p = 0;
13172 if (window_height_changed_p)
13174 consider_all_windows_p = 1;
13175 ++update_mode_lines;
13176 ++windows_or_buffers_changed;
13178 /* If window configuration was changed, frames may have been
13179 marked garbaged. Clear them or we will experience
13180 surprises wrt scrolling. */
13181 clear_garbaged_frames ();
13184 else if (EQ (selected_window, minibuf_window)
13185 && (current_buffer->clip_changed || window_outdated (w))
13186 && resize_mini_window (w, 0))
13188 /* Resized active mini-window to fit the size of what it is
13189 showing if its contents might have changed. */
13190 must_finish = 1;
13191 /* FIXME: this causes all frames to be updated, which seems unnecessary
13192 since only the current frame needs to be considered. This function
13193 needs to be rewritten with two variables, consider_all_windows and
13194 consider_all_frames. */
13195 consider_all_windows_p = 1;
13196 ++windows_or_buffers_changed;
13197 ++update_mode_lines;
13199 /* If window configuration was changed, frames may have been
13200 marked garbaged. Clear them or we will experience
13201 surprises wrt scrolling. */
13202 clear_garbaged_frames ();
13205 /* If showing the region, and mark has changed, we must redisplay
13206 the whole window. The assignment to this_line_start_pos prevents
13207 the optimization directly below this if-statement. */
13208 if (((!NILP (Vtransient_mark_mode)
13209 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13210 != (w->region_showing > 0))
13211 || (w->region_showing
13212 && w->region_showing
13213 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13214 CHARPOS (this_line_start_pos) = 0;
13216 /* Optimize the case that only the line containing the cursor in the
13217 selected window has changed. Variables starting with this_ are
13218 set in display_line and record information about the line
13219 containing the cursor. */
13220 tlbufpos = this_line_start_pos;
13221 tlendpos = this_line_end_pos;
13222 if (!consider_all_windows_p
13223 && CHARPOS (tlbufpos) > 0
13224 && !w->update_mode_line
13225 && !current_buffer->clip_changed
13226 && !current_buffer->prevent_redisplay_optimizations_p
13227 && FRAME_VISIBLE_P (XFRAME (w->frame))
13228 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13229 && !XFRAME (w->frame)->cursor_type_changed
13230 /* Make sure recorded data applies to current buffer, etc. */
13231 && this_line_buffer == current_buffer
13232 && match_p
13233 && !w->force_start
13234 && !w->optional_new_start
13235 /* Point must be on the line that we have info recorded about. */
13236 && PT >= CHARPOS (tlbufpos)
13237 && PT <= Z - CHARPOS (tlendpos)
13238 /* All text outside that line, including its final newline,
13239 must be unchanged. */
13240 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13241 CHARPOS (tlendpos)))
13243 if (CHARPOS (tlbufpos) > BEGV
13244 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13245 && (CHARPOS (tlbufpos) == ZV
13246 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13247 /* Former continuation line has disappeared by becoming empty. */
13248 goto cancel;
13249 else if (window_outdated (w) || MINI_WINDOW_P (w))
13251 /* We have to handle the case of continuation around a
13252 wide-column character (see the comment in indent.c around
13253 line 1340).
13255 For instance, in the following case:
13257 -------- Insert --------
13258 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13259 J_I_ ==> J_I_ `^^' are cursors.
13260 ^^ ^^
13261 -------- --------
13263 As we have to redraw the line above, we cannot use this
13264 optimization. */
13266 struct it it;
13267 int line_height_before = this_line_pixel_height;
13269 /* Note that start_display will handle the case that the
13270 line starting at tlbufpos is a continuation line. */
13271 start_display (&it, w, tlbufpos);
13273 /* Implementation note: It this still necessary? */
13274 if (it.current_x != this_line_start_x)
13275 goto cancel;
13277 TRACE ((stderr, "trying display optimization 1\n"));
13278 w->cursor.vpos = -1;
13279 overlay_arrow_seen = 0;
13280 it.vpos = this_line_vpos;
13281 it.current_y = this_line_y;
13282 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13283 display_line (&it);
13285 /* If line contains point, is not continued,
13286 and ends at same distance from eob as before, we win. */
13287 if (w->cursor.vpos >= 0
13288 /* Line is not continued, otherwise this_line_start_pos
13289 would have been set to 0 in display_line. */
13290 && CHARPOS (this_line_start_pos)
13291 /* Line ends as before. */
13292 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13293 /* Line has same height as before. Otherwise other lines
13294 would have to be shifted up or down. */
13295 && this_line_pixel_height == line_height_before)
13297 /* If this is not the window's last line, we must adjust
13298 the charstarts of the lines below. */
13299 if (it.current_y < it.last_visible_y)
13301 struct glyph_row *row
13302 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13303 ptrdiff_t delta, delta_bytes;
13305 /* We used to distinguish between two cases here,
13306 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13307 when the line ends in a newline or the end of the
13308 buffer's accessible portion. But both cases did
13309 the same, so they were collapsed. */
13310 delta = (Z
13311 - CHARPOS (tlendpos)
13312 - MATRIX_ROW_START_CHARPOS (row));
13313 delta_bytes = (Z_BYTE
13314 - BYTEPOS (tlendpos)
13315 - MATRIX_ROW_START_BYTEPOS (row));
13317 increment_matrix_positions (w->current_matrix,
13318 this_line_vpos + 1,
13319 w->current_matrix->nrows,
13320 delta, delta_bytes);
13323 /* If this row displays text now but previously didn't,
13324 or vice versa, w->window_end_vpos may have to be
13325 adjusted. */
13326 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13328 if (w->window_end_vpos < this_line_vpos)
13329 w->window_end_vpos = this_line_vpos;
13331 else if (w->window_end_vpos == this_line_vpos
13332 && this_line_vpos > 0)
13333 w->window_end_vpos = this_line_vpos - 1;
13334 w->window_end_valid = 0;
13336 /* Update hint: No need to try to scroll in update_window. */
13337 w->desired_matrix->no_scrolling_p = 1;
13339 #ifdef GLYPH_DEBUG
13340 *w->desired_matrix->method = 0;
13341 debug_method_add (w, "optimization 1");
13342 #endif
13343 #if HAVE_XWIDGETS
13344 //debug optimization movement issue
13345 //w->desired_matrix->no_scrolling_p = 1;
13346 //*w->desired_matrix->method = 0;
13347 //debug_method_add (w, "optimization 1");
13348 #endif
13350 #ifdef HAVE_WINDOW_SYSTEM
13351 update_window_fringes (w, 0);
13352 #endif
13353 goto update;
13355 else
13356 goto cancel;
13358 else if (/* Cursor position hasn't changed. */
13359 PT == w->last_point
13360 /* Make sure the cursor was last displayed
13361 in this window. Otherwise we have to reposition it. */
13362 && 0 <= w->cursor.vpos
13363 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13365 if (!must_finish)
13367 do_pending_window_change (1);
13368 /* If selected_window changed, redisplay again. */
13369 if (WINDOWP (selected_window)
13370 && (w = XWINDOW (selected_window)) != sw)
13371 goto retry;
13373 /* We used to always goto end_of_redisplay here, but this
13374 isn't enough if we have a blinking cursor. */
13375 if (w->cursor_off_p == w->last_cursor_off_p)
13376 goto end_of_redisplay;
13378 goto update;
13380 /* If highlighting the region, or if the cursor is in the echo area,
13381 then we can't just move the cursor. */
13382 else if (! (!NILP (Vtransient_mark_mode)
13383 && !NILP (BVAR (current_buffer, mark_active)))
13384 && (EQ (selected_window,
13385 BVAR (current_buffer, last_selected_window))
13386 || highlight_nonselected_windows)
13387 && !w->region_showing
13388 && NILP (Vshow_trailing_whitespace)
13389 && !cursor_in_echo_area)
13391 struct it it;
13392 struct glyph_row *row;
13394 /* Skip from tlbufpos to PT and see where it is. Note that
13395 PT may be in invisible text. If so, we will end at the
13396 next visible position. */
13397 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13398 NULL, DEFAULT_FACE_ID);
13399 it.current_x = this_line_start_x;
13400 it.current_y = this_line_y;
13401 it.vpos = this_line_vpos;
13403 /* The call to move_it_to stops in front of PT, but
13404 moves over before-strings. */
13405 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13407 if (it.vpos == this_line_vpos
13408 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13409 row->enabled_p))
13411 eassert (this_line_vpos == it.vpos);
13412 eassert (this_line_y == it.current_y);
13413 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13414 #ifdef GLYPH_DEBUG
13415 *w->desired_matrix->method = 0;
13416 debug_method_add (w, "optimization 3");
13417 #endif
13418 goto update;
13420 else
13421 goto cancel;
13424 cancel:
13425 /* Text changed drastically or point moved off of line. */
13426 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13429 CHARPOS (this_line_start_pos) = 0;
13430 consider_all_windows_p |= buffer_shared_and_changed ();
13431 ++clear_face_cache_count;
13432 #ifdef HAVE_WINDOW_SYSTEM
13433 ++clear_image_cache_count;
13434 #endif
13436 /* Build desired matrices, and update the display. If
13437 consider_all_windows_p is non-zero, do it for all windows on all
13438 frames. Otherwise do it for selected_window, only. */
13440 if (consider_all_windows_p)
13442 FOR_EACH_FRAME (tail, frame)
13443 XFRAME (frame)->updated_p = 0;
13445 FOR_EACH_FRAME (tail, frame)
13447 struct frame *f = XFRAME (frame);
13449 /* We don't have to do anything for unselected terminal
13450 frames. */
13451 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13452 && !EQ (FRAME_TTY (f)->top_frame, frame))
13453 continue;
13455 retry_frame:
13457 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13459 /* Mark all the scroll bars to be removed; we'll redeem
13460 the ones we want when we redisplay their windows. */
13461 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13462 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13464 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13465 redisplay_windows (FRAME_ROOT_WINDOW (f));
13467 /* The X error handler may have deleted that frame. */
13468 if (!FRAME_LIVE_P (f))
13469 continue;
13471 /* Any scroll bars which redisplay_windows should have
13472 nuked should now go away. */
13473 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13474 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13476 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13478 /* If fonts changed on visible frame, display again. */
13479 if (f->fonts_changed)
13481 adjust_frame_glyphs (f);
13482 f->fonts_changed = 0;
13483 goto retry_frame;
13486 /* See if we have to hscroll. */
13487 if (!f->already_hscrolled_p)
13489 f->already_hscrolled_p = 1;
13490 if (hscroll_windows (f->root_window))
13491 goto retry_frame;
13494 /* Prevent various kinds of signals during display
13495 update. stdio is not robust about handling
13496 signals, which can cause an apparent I/O
13497 error. */
13498 if (interrupt_input)
13499 unrequest_sigio ();
13500 STOP_POLLING;
13502 /* Update the display. */
13503 set_window_update_flags (XWINDOW (f->root_window), 1);
13504 pending |= update_frame (f, 0, 0);
13505 f->cursor_type_changed = 0;
13506 f->updated_p = 1;
13511 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13513 if (!pending)
13515 /* Do the mark_window_display_accurate after all windows have
13516 been redisplayed because this call resets flags in buffers
13517 which are needed for proper redisplay. */
13518 FOR_EACH_FRAME (tail, frame)
13520 struct frame *f = XFRAME (frame);
13521 if (f->updated_p)
13523 mark_window_display_accurate (f->root_window, 1);
13524 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13525 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13530 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13532 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13533 struct frame *mini_frame;
13535 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13536 /* Use list_of_error, not Qerror, so that
13537 we catch only errors and don't run the debugger. */
13538 internal_condition_case_1 (redisplay_window_1, selected_window,
13539 list_of_error,
13540 redisplay_window_error);
13541 if (update_miniwindow_p)
13542 internal_condition_case_1 (redisplay_window_1, mini_window,
13543 list_of_error,
13544 redisplay_window_error);
13546 /* Compare desired and current matrices, perform output. */
13548 update:
13549 /* If fonts changed, display again. */
13550 if (sf->fonts_changed)
13551 goto retry;
13553 /* Prevent various kinds of signals during display update.
13554 stdio is not robust about handling signals,
13555 which can cause an apparent I/O error. */
13556 if (interrupt_input)
13557 unrequest_sigio ();
13558 STOP_POLLING;
13560 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13562 if (hscroll_windows (selected_window))
13563 goto retry;
13565 XWINDOW (selected_window)->must_be_updated_p = 1;
13566 pending = update_frame (sf, 0, 0);
13567 sf->cursor_type_changed = 0;
13570 /* We may have called echo_area_display at the top of this
13571 function. If the echo area is on another frame, that may
13572 have put text on a frame other than the selected one, so the
13573 above call to update_frame would not have caught it. Catch
13574 it here. */
13575 mini_window = FRAME_MINIBUF_WINDOW (sf);
13576 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13578 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13580 XWINDOW (mini_window)->must_be_updated_p = 1;
13581 pending |= update_frame (mini_frame, 0, 0);
13582 mini_frame->cursor_type_changed = 0;
13583 if (!pending && hscroll_windows (mini_window))
13584 goto retry;
13588 /* If display was paused because of pending input, make sure we do a
13589 thorough update the next time. */
13590 if (pending)
13592 /* Prevent the optimization at the beginning of
13593 redisplay_internal that tries a single-line update of the
13594 line containing the cursor in the selected window. */
13595 CHARPOS (this_line_start_pos) = 0;
13597 /* Let the overlay arrow be updated the next time. */
13598 update_overlay_arrows (0);
13600 /* If we pause after scrolling, some rows in the current
13601 matrices of some windows are not valid. */
13602 if (!WINDOW_FULL_WIDTH_P (w)
13603 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13604 update_mode_lines = 1;
13606 else
13608 if (!consider_all_windows_p)
13610 /* This has already been done above if
13611 consider_all_windows_p is set. */
13612 mark_window_display_accurate_1 (w, 1);
13614 /* Say overlay arrows are up to date. */
13615 update_overlay_arrows (1);
13617 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13618 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13621 update_mode_lines = 0;
13622 windows_or_buffers_changed = 0;
13625 /* Start SIGIO interrupts coming again. Having them off during the
13626 code above makes it less likely one will discard output, but not
13627 impossible, since there might be stuff in the system buffer here.
13628 But it is much hairier to try to do anything about that. */
13629 if (interrupt_input)
13630 request_sigio ();
13631 RESUME_POLLING;
13633 /* If a frame has become visible which was not before, redisplay
13634 again, so that we display it. Expose events for such a frame
13635 (which it gets when becoming visible) don't call the parts of
13636 redisplay constructing glyphs, so simply exposing a frame won't
13637 display anything in this case. So, we have to display these
13638 frames here explicitly. */
13639 if (!pending)
13641 int new_count = 0;
13643 FOR_EACH_FRAME (tail, frame)
13645 int this_is_visible = 0;
13647 if (XFRAME (frame)->visible)
13648 this_is_visible = 1;
13650 if (this_is_visible)
13651 new_count++;
13654 if (new_count != number_of_visible_frames)
13655 windows_or_buffers_changed++;
13658 /* Change frame size now if a change is pending. */
13659 do_pending_window_change (1);
13661 /* If we just did a pending size change, or have additional
13662 visible frames, or selected_window changed, redisplay again. */
13663 if ((windows_or_buffers_changed && !pending)
13664 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13665 goto retry;
13667 /* Clear the face and image caches.
13669 We used to do this only if consider_all_windows_p. But the cache
13670 needs to be cleared if a timer creates images in the current
13671 buffer (e.g. the test case in Bug#6230). */
13673 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13675 clear_face_cache (0);
13676 clear_face_cache_count = 0;
13679 #ifdef HAVE_WINDOW_SYSTEM
13680 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13682 clear_image_caches (Qnil);
13683 clear_image_cache_count = 0;
13685 #endif /* HAVE_WINDOW_SYSTEM */
13687 end_of_redisplay:
13688 unbind_to (count, Qnil);
13689 RESUME_POLLING;
13693 /* Redisplay, but leave alone any recent echo area message unless
13694 another message has been requested in its place.
13696 This is useful in situations where you need to redisplay but no
13697 user action has occurred, making it inappropriate for the message
13698 area to be cleared. See tracking_off and
13699 wait_reading_process_output for examples of these situations.
13701 FROM_WHERE is an integer saying from where this function was
13702 called. This is useful for debugging. */
13704 void
13705 redisplay_preserve_echo_area (int from_where)
13707 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13709 if (!NILP (echo_area_buffer[1]))
13711 /* We have a previously displayed message, but no current
13712 message. Redisplay the previous message. */
13713 display_last_displayed_message_p = 1;
13714 redisplay_internal ();
13715 display_last_displayed_message_p = 0;
13717 else
13718 redisplay_internal ();
13720 flush_frame (SELECTED_FRAME ());
13724 /* Function registered with record_unwind_protect in redisplay_internal. */
13726 static void
13727 unwind_redisplay (void)
13729 redisplaying_p = 0;
13733 /* Mark the display of leaf window W as accurate or inaccurate.
13734 If ACCURATE_P is non-zero mark display of W as accurate. If
13735 ACCURATE_P is zero, arrange for W to be redisplayed the next
13736 time redisplay_internal is called. */
13738 static void
13739 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13741 struct buffer *b = XBUFFER (w->contents);
13743 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13744 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13745 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13747 if (accurate_p)
13749 b->clip_changed = 0;
13750 b->prevent_redisplay_optimizations_p = 0;
13752 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13753 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13754 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13755 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13757 w->current_matrix->buffer = b;
13758 w->current_matrix->begv = BUF_BEGV (b);
13759 w->current_matrix->zv = BUF_ZV (b);
13761 w->last_cursor_vpos = w->cursor.vpos;
13762 w->last_cursor_off_p = w->cursor_off_p;
13764 if (w == XWINDOW (selected_window))
13765 w->last_point = BUF_PT (b);
13766 else
13767 w->last_point = marker_position (w->pointm);
13769 w->window_end_valid = 1;
13770 w->update_mode_line = 0;
13775 /* Mark the display of windows in the window tree rooted at WINDOW as
13776 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13777 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13778 be redisplayed the next time redisplay_internal is called. */
13780 void
13781 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13783 struct window *w;
13785 for (; !NILP (window); window = w->next)
13787 w = XWINDOW (window);
13788 if (WINDOWP (w->contents))
13789 mark_window_display_accurate (w->contents, accurate_p);
13790 else
13791 mark_window_display_accurate_1 (w, accurate_p);
13794 if (accurate_p)
13795 update_overlay_arrows (1);
13796 else
13797 /* Force a thorough redisplay the next time by setting
13798 last_arrow_position and last_arrow_string to t, which is
13799 unequal to any useful value of Voverlay_arrow_... */
13800 update_overlay_arrows (-1);
13804 /* Return value in display table DP (Lisp_Char_Table *) for character
13805 C. Since a display table doesn't have any parent, we don't have to
13806 follow parent. Do not call this function directly but use the
13807 macro DISP_CHAR_VECTOR. */
13809 Lisp_Object
13810 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13812 Lisp_Object val;
13814 if (ASCII_CHAR_P (c))
13816 val = dp->ascii;
13817 if (SUB_CHAR_TABLE_P (val))
13818 val = XSUB_CHAR_TABLE (val)->contents[c];
13820 else
13822 Lisp_Object table;
13824 XSETCHAR_TABLE (table, dp);
13825 val = char_table_ref (table, c);
13827 if (NILP (val))
13828 val = dp->defalt;
13829 return val;
13834 /***********************************************************************
13835 Window Redisplay
13836 ***********************************************************************/
13838 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13840 static void
13841 redisplay_windows (Lisp_Object window)
13843 while (!NILP (window))
13845 struct window *w = XWINDOW (window);
13847 if (WINDOWP (w->contents))
13848 redisplay_windows (w->contents);
13849 else if (BUFFERP (w->contents))
13851 displayed_buffer = XBUFFER (w->contents);
13852 /* Use list_of_error, not Qerror, so that
13853 we catch only errors and don't run the debugger. */
13854 internal_condition_case_1 (redisplay_window_0, window,
13855 list_of_error,
13856 redisplay_window_error);
13859 window = w->next;
13863 static Lisp_Object
13864 redisplay_window_error (Lisp_Object ignore)
13866 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13867 return Qnil;
13870 static Lisp_Object
13871 redisplay_window_0 (Lisp_Object window)
13873 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13874 redisplay_window (window, 0);
13875 return Qnil;
13878 static Lisp_Object
13879 redisplay_window_1 (Lisp_Object window)
13881 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13882 redisplay_window (window, 1);
13883 return Qnil;
13887 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13888 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13889 which positions recorded in ROW differ from current buffer
13890 positions.
13892 Return 0 if cursor is not on this row, 1 otherwise. */
13894 static int
13895 set_cursor_from_row (struct window *w, struct glyph_row *row,
13896 struct glyph_matrix *matrix,
13897 ptrdiff_t delta, ptrdiff_t delta_bytes,
13898 int dy, int dvpos)
13900 struct glyph *glyph = row->glyphs[TEXT_AREA];
13901 struct glyph *end = glyph + row->used[TEXT_AREA];
13902 struct glyph *cursor = NULL;
13903 /* The last known character position in row. */
13904 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13905 int x = row->x;
13906 ptrdiff_t pt_old = PT - delta;
13907 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13908 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13909 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13910 /* A glyph beyond the edge of TEXT_AREA which we should never
13911 touch. */
13912 struct glyph *glyphs_end = end;
13913 /* Non-zero means we've found a match for cursor position, but that
13914 glyph has the avoid_cursor_p flag set. */
13915 int match_with_avoid_cursor = 0;
13916 /* Non-zero means we've seen at least one glyph that came from a
13917 display string. */
13918 int string_seen = 0;
13919 /* Largest and smallest buffer positions seen so far during scan of
13920 glyph row. */
13921 ptrdiff_t bpos_max = pos_before;
13922 ptrdiff_t bpos_min = pos_after;
13923 /* Last buffer position covered by an overlay string with an integer
13924 `cursor' property. */
13925 ptrdiff_t bpos_covered = 0;
13926 /* Non-zero means the display string on which to display the cursor
13927 comes from a text property, not from an overlay. */
13928 int string_from_text_prop = 0;
13930 /* Don't even try doing anything if called for a mode-line or
13931 header-line row, since the rest of the code isn't prepared to
13932 deal with such calamities. */
13933 eassert (!row->mode_line_p);
13934 if (row->mode_line_p)
13935 return 0;
13937 /* Skip over glyphs not having an object at the start and the end of
13938 the row. These are special glyphs like truncation marks on
13939 terminal frames. */
13940 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13942 if (!row->reversed_p)
13944 while (glyph < end
13945 && INTEGERP (glyph->object)
13946 && glyph->charpos < 0)
13948 x += glyph->pixel_width;
13949 ++glyph;
13951 while (end > glyph
13952 && INTEGERP ((end - 1)->object)
13953 /* CHARPOS is zero for blanks and stretch glyphs
13954 inserted by extend_face_to_end_of_line. */
13955 && (end - 1)->charpos <= 0)
13956 --end;
13957 glyph_before = glyph - 1;
13958 glyph_after = end;
13960 else
13962 struct glyph *g;
13964 /* If the glyph row is reversed, we need to process it from back
13965 to front, so swap the edge pointers. */
13966 glyphs_end = end = glyph - 1;
13967 glyph += row->used[TEXT_AREA] - 1;
13969 while (glyph > end + 1
13970 && INTEGERP (glyph->object)
13971 && glyph->charpos < 0)
13973 --glyph;
13974 x -= glyph->pixel_width;
13976 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13977 --glyph;
13978 /* By default, in reversed rows we put the cursor on the
13979 rightmost (first in the reading order) glyph. */
13980 for (g = end + 1; g < glyph; g++)
13981 x += g->pixel_width;
13982 while (end < glyph
13983 && INTEGERP ((end + 1)->object)
13984 && (end + 1)->charpos <= 0)
13985 ++end;
13986 glyph_before = glyph + 1;
13987 glyph_after = end;
13990 else if (row->reversed_p)
13992 /* In R2L rows that don't display text, put the cursor on the
13993 rightmost glyph. Case in point: an empty last line that is
13994 part of an R2L paragraph. */
13995 cursor = end - 1;
13996 /* Avoid placing the cursor on the last glyph of the row, where
13997 on terminal frames we hold the vertical border between
13998 adjacent windows. */
13999 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14000 && !WINDOW_RIGHTMOST_P (w)
14001 && cursor == row->glyphs[LAST_AREA] - 1)
14002 cursor--;
14003 x = -1; /* will be computed below, at label compute_x */
14006 /* Step 1: Try to find the glyph whose character position
14007 corresponds to point. If that's not possible, find 2 glyphs
14008 whose character positions are the closest to point, one before
14009 point, the other after it. */
14010 if (!row->reversed_p)
14011 while (/* not marched to end of glyph row */
14012 glyph < end
14013 /* glyph was not inserted by redisplay for internal purposes */
14014 && !INTEGERP (glyph->object))
14016 if (BUFFERP (glyph->object))
14018 ptrdiff_t dpos = glyph->charpos - pt_old;
14020 if (glyph->charpos > bpos_max)
14021 bpos_max = glyph->charpos;
14022 if (glyph->charpos < bpos_min)
14023 bpos_min = glyph->charpos;
14024 if (!glyph->avoid_cursor_p)
14026 /* If we hit point, we've found the glyph on which to
14027 display the cursor. */
14028 if (dpos == 0)
14030 match_with_avoid_cursor = 0;
14031 break;
14033 /* See if we've found a better approximation to
14034 POS_BEFORE or to POS_AFTER. */
14035 if (0 > dpos && dpos > pos_before - pt_old)
14037 pos_before = glyph->charpos;
14038 glyph_before = glyph;
14040 else if (0 < dpos && dpos < pos_after - pt_old)
14042 pos_after = glyph->charpos;
14043 glyph_after = glyph;
14046 else if (dpos == 0)
14047 match_with_avoid_cursor = 1;
14049 else if (STRINGP (glyph->object))
14051 Lisp_Object chprop;
14052 ptrdiff_t glyph_pos = glyph->charpos;
14054 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14055 glyph->object);
14056 if (!NILP (chprop))
14058 /* If the string came from a `display' text property,
14059 look up the buffer position of that property and
14060 use that position to update bpos_max, as if we
14061 actually saw such a position in one of the row's
14062 glyphs. This helps with supporting integer values
14063 of `cursor' property on the display string in
14064 situations where most or all of the row's buffer
14065 text is completely covered by display properties,
14066 so that no glyph with valid buffer positions is
14067 ever seen in the row. */
14068 ptrdiff_t prop_pos =
14069 string_buffer_position_lim (glyph->object, pos_before,
14070 pos_after, 0);
14072 if (prop_pos >= pos_before)
14073 bpos_max = prop_pos - 1;
14075 if (INTEGERP (chprop))
14077 bpos_covered = bpos_max + XINT (chprop);
14078 /* If the `cursor' property covers buffer positions up
14079 to and including point, we should display cursor on
14080 this glyph. Note that, if a `cursor' property on one
14081 of the string's characters has an integer value, we
14082 will break out of the loop below _before_ we get to
14083 the position match above. IOW, integer values of
14084 the `cursor' property override the "exact match for
14085 point" strategy of positioning the cursor. */
14086 /* Implementation note: bpos_max == pt_old when, e.g.,
14087 we are in an empty line, where bpos_max is set to
14088 MATRIX_ROW_START_CHARPOS, see above. */
14089 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14091 cursor = glyph;
14092 break;
14096 string_seen = 1;
14098 x += glyph->pixel_width;
14099 ++glyph;
14101 else if (glyph > end) /* row is reversed */
14102 while (!INTEGERP (glyph->object))
14104 if (BUFFERP (glyph->object))
14106 ptrdiff_t dpos = glyph->charpos - pt_old;
14108 if (glyph->charpos > bpos_max)
14109 bpos_max = glyph->charpos;
14110 if (glyph->charpos < bpos_min)
14111 bpos_min = glyph->charpos;
14112 if (!glyph->avoid_cursor_p)
14114 if (dpos == 0)
14116 match_with_avoid_cursor = 0;
14117 break;
14119 if (0 > dpos && dpos > pos_before - pt_old)
14121 pos_before = glyph->charpos;
14122 glyph_before = glyph;
14124 else if (0 < dpos && dpos < pos_after - pt_old)
14126 pos_after = glyph->charpos;
14127 glyph_after = glyph;
14130 else if (dpos == 0)
14131 match_with_avoid_cursor = 1;
14133 else if (STRINGP (glyph->object))
14135 Lisp_Object chprop;
14136 ptrdiff_t glyph_pos = glyph->charpos;
14138 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14139 glyph->object);
14140 if (!NILP (chprop))
14142 ptrdiff_t prop_pos =
14143 string_buffer_position_lim (glyph->object, pos_before,
14144 pos_after, 0);
14146 if (prop_pos >= pos_before)
14147 bpos_max = prop_pos - 1;
14149 if (INTEGERP (chprop))
14151 bpos_covered = bpos_max + XINT (chprop);
14152 /* If the `cursor' property covers buffer positions up
14153 to and including point, we should display cursor on
14154 this glyph. */
14155 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14157 cursor = glyph;
14158 break;
14161 string_seen = 1;
14163 --glyph;
14164 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14166 x--; /* can't use any pixel_width */
14167 break;
14169 x -= glyph->pixel_width;
14172 /* Step 2: If we didn't find an exact match for point, we need to
14173 look for a proper place to put the cursor among glyphs between
14174 GLYPH_BEFORE and GLYPH_AFTER. */
14175 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14176 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14177 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14179 /* An empty line has a single glyph whose OBJECT is zero and
14180 whose CHARPOS is the position of a newline on that line.
14181 Note that on a TTY, there are more glyphs after that, which
14182 were produced by extend_face_to_end_of_line, but their
14183 CHARPOS is zero or negative. */
14184 int empty_line_p =
14185 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14186 && INTEGERP (glyph->object) && glyph->charpos > 0
14187 /* On a TTY, continued and truncated rows also have a glyph at
14188 their end whose OBJECT is zero and whose CHARPOS is
14189 positive (the continuation and truncation glyphs), but such
14190 rows are obviously not "empty". */
14191 && !(row->continued_p || row->truncated_on_right_p);
14193 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14195 ptrdiff_t ellipsis_pos;
14197 /* Scan back over the ellipsis glyphs. */
14198 if (!row->reversed_p)
14200 ellipsis_pos = (glyph - 1)->charpos;
14201 while (glyph > row->glyphs[TEXT_AREA]
14202 && (glyph - 1)->charpos == ellipsis_pos)
14203 glyph--, x -= glyph->pixel_width;
14204 /* That loop always goes one position too far, including
14205 the glyph before the ellipsis. So scan forward over
14206 that one. */
14207 x += glyph->pixel_width;
14208 glyph++;
14210 else /* row is reversed */
14212 ellipsis_pos = (glyph + 1)->charpos;
14213 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14214 && (glyph + 1)->charpos == ellipsis_pos)
14215 glyph++, x += glyph->pixel_width;
14216 x -= glyph->pixel_width;
14217 glyph--;
14220 else if (match_with_avoid_cursor)
14222 cursor = glyph_after;
14223 x = -1;
14225 else if (string_seen)
14227 int incr = row->reversed_p ? -1 : +1;
14229 /* Need to find the glyph that came out of a string which is
14230 present at point. That glyph is somewhere between
14231 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14232 positioned between POS_BEFORE and POS_AFTER in the
14233 buffer. */
14234 struct glyph *start, *stop;
14235 ptrdiff_t pos = pos_before;
14237 x = -1;
14239 /* If the row ends in a newline from a display string,
14240 reordering could have moved the glyphs belonging to the
14241 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14242 in this case we extend the search to the last glyph in
14243 the row that was not inserted by redisplay. */
14244 if (row->ends_in_newline_from_string_p)
14246 glyph_after = end;
14247 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14250 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14251 correspond to POS_BEFORE and POS_AFTER, respectively. We
14252 need START and STOP in the order that corresponds to the
14253 row's direction as given by its reversed_p flag. If the
14254 directionality of characters between POS_BEFORE and
14255 POS_AFTER is the opposite of the row's base direction,
14256 these characters will have been reordered for display,
14257 and we need to reverse START and STOP. */
14258 if (!row->reversed_p)
14260 start = min (glyph_before, glyph_after);
14261 stop = max (glyph_before, glyph_after);
14263 else
14265 start = max (glyph_before, glyph_after);
14266 stop = min (glyph_before, glyph_after);
14268 for (glyph = start + incr;
14269 row->reversed_p ? glyph > stop : glyph < stop; )
14272 /* Any glyphs that come from the buffer are here because
14273 of bidi reordering. Skip them, and only pay
14274 attention to glyphs that came from some string. */
14275 if (STRINGP (glyph->object))
14277 Lisp_Object str;
14278 ptrdiff_t tem;
14279 /* If the display property covers the newline, we
14280 need to search for it one position farther. */
14281 ptrdiff_t lim = pos_after
14282 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14284 string_from_text_prop = 0;
14285 str = glyph->object;
14286 tem = string_buffer_position_lim (str, pos, lim, 0);
14287 if (tem == 0 /* from overlay */
14288 || pos <= tem)
14290 /* If the string from which this glyph came is
14291 found in the buffer at point, or at position
14292 that is closer to point than pos_after, then
14293 we've found the glyph we've been looking for.
14294 If it comes from an overlay (tem == 0), and
14295 it has the `cursor' property on one of its
14296 glyphs, record that glyph as a candidate for
14297 displaying the cursor. (As in the
14298 unidirectional version, we will display the
14299 cursor on the last candidate we find.) */
14300 if (tem == 0
14301 || tem == pt_old
14302 || (tem - pt_old > 0 && tem < pos_after))
14304 /* The glyphs from this string could have
14305 been reordered. Find the one with the
14306 smallest string position. Or there could
14307 be a character in the string with the
14308 `cursor' property, which means display
14309 cursor on that character's glyph. */
14310 ptrdiff_t strpos = glyph->charpos;
14312 if (tem)
14314 cursor = glyph;
14315 string_from_text_prop = 1;
14317 for ( ;
14318 (row->reversed_p ? glyph > stop : glyph < stop)
14319 && EQ (glyph->object, str);
14320 glyph += incr)
14322 Lisp_Object cprop;
14323 ptrdiff_t gpos = glyph->charpos;
14325 cprop = Fget_char_property (make_number (gpos),
14326 Qcursor,
14327 glyph->object);
14328 if (!NILP (cprop))
14330 cursor = glyph;
14331 break;
14333 if (tem && glyph->charpos < strpos)
14335 strpos = glyph->charpos;
14336 cursor = glyph;
14340 if (tem == pt_old
14341 || (tem - pt_old > 0 && tem < pos_after))
14342 goto compute_x;
14344 if (tem)
14345 pos = tem + 1; /* don't find previous instances */
14347 /* This string is not what we want; skip all of the
14348 glyphs that came from it. */
14349 while ((row->reversed_p ? glyph > stop : glyph < stop)
14350 && EQ (glyph->object, str))
14351 glyph += incr;
14353 else
14354 glyph += incr;
14357 /* If we reached the end of the line, and END was from a string,
14358 the cursor is not on this line. */
14359 if (cursor == NULL
14360 && (row->reversed_p ? glyph <= end : glyph >= end)
14361 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14362 && STRINGP (end->object)
14363 && row->continued_p)
14364 return 0;
14366 /* A truncated row may not include PT among its character positions.
14367 Setting the cursor inside the scroll margin will trigger
14368 recalculation of hscroll in hscroll_window_tree. But if a
14369 display string covers point, defer to the string-handling
14370 code below to figure this out. */
14371 else if (row->truncated_on_left_p && pt_old < bpos_min)
14373 cursor = glyph_before;
14374 x = -1;
14376 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14377 /* Zero-width characters produce no glyphs. */
14378 || (!empty_line_p
14379 && (row->reversed_p
14380 ? glyph_after > glyphs_end
14381 : glyph_after < glyphs_end)))
14383 cursor = glyph_after;
14384 x = -1;
14388 compute_x:
14389 if (cursor != NULL)
14390 glyph = cursor;
14391 else if (glyph == glyphs_end
14392 && pos_before == pos_after
14393 && STRINGP ((row->reversed_p
14394 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14395 : row->glyphs[TEXT_AREA])->object))
14397 /* If all the glyphs of this row came from strings, put the
14398 cursor on the first glyph of the row. This avoids having the
14399 cursor outside of the text area in this very rare and hard
14400 use case. */
14401 glyph =
14402 row->reversed_p
14403 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14404 : row->glyphs[TEXT_AREA];
14406 if (x < 0)
14408 struct glyph *g;
14410 /* Need to compute x that corresponds to GLYPH. */
14411 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14413 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14414 emacs_abort ();
14415 x += g->pixel_width;
14419 /* ROW could be part of a continued line, which, under bidi
14420 reordering, might have other rows whose start and end charpos
14421 occlude point. Only set w->cursor if we found a better
14422 approximation to the cursor position than we have from previously
14423 examined candidate rows belonging to the same continued line. */
14424 if (/* we already have a candidate row */
14425 w->cursor.vpos >= 0
14426 /* that candidate is not the row we are processing */
14427 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14428 /* Make sure cursor.vpos specifies a row whose start and end
14429 charpos occlude point, and it is valid candidate for being a
14430 cursor-row. This is because some callers of this function
14431 leave cursor.vpos at the row where the cursor was displayed
14432 during the last redisplay cycle. */
14433 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14434 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14435 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14437 struct glyph *g1 =
14438 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14440 /* Don't consider glyphs that are outside TEXT_AREA. */
14441 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14442 return 0;
14443 /* Keep the candidate whose buffer position is the closest to
14444 point or has the `cursor' property. */
14445 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14446 w->cursor.hpos >= 0
14447 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14448 && ((BUFFERP (g1->object)
14449 && (g1->charpos == pt_old /* an exact match always wins */
14450 || (BUFFERP (glyph->object)
14451 && eabs (g1->charpos - pt_old)
14452 < eabs (glyph->charpos - pt_old))))
14453 /* previous candidate is a glyph from a string that has
14454 a non-nil `cursor' property */
14455 || (STRINGP (g1->object)
14456 && (!NILP (Fget_char_property (make_number (g1->charpos),
14457 Qcursor, g1->object))
14458 /* previous candidate is from the same display
14459 string as this one, and the display string
14460 came from a text property */
14461 || (EQ (g1->object, glyph->object)
14462 && string_from_text_prop)
14463 /* this candidate is from newline and its
14464 position is not an exact match */
14465 || (INTEGERP (glyph->object)
14466 && glyph->charpos != pt_old)))))
14467 return 0;
14468 /* If this candidate gives an exact match, use that. */
14469 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14470 /* If this candidate is a glyph created for the
14471 terminating newline of a line, and point is on that
14472 newline, it wins because it's an exact match. */
14473 || (!row->continued_p
14474 && INTEGERP (glyph->object)
14475 && glyph->charpos == 0
14476 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14477 /* Otherwise, keep the candidate that comes from a row
14478 spanning less buffer positions. This may win when one or
14479 both candidate positions are on glyphs that came from
14480 display strings, for which we cannot compare buffer
14481 positions. */
14482 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14483 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14484 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14485 return 0;
14487 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14488 w->cursor.x = x;
14489 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14490 w->cursor.y = row->y + dy;
14492 if (w == XWINDOW (selected_window))
14494 if (!row->continued_p
14495 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14496 && row->x == 0)
14498 this_line_buffer = XBUFFER (w->contents);
14500 CHARPOS (this_line_start_pos)
14501 = MATRIX_ROW_START_CHARPOS (row) + delta;
14502 BYTEPOS (this_line_start_pos)
14503 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14505 CHARPOS (this_line_end_pos)
14506 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14507 BYTEPOS (this_line_end_pos)
14508 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14510 this_line_y = w->cursor.y;
14511 this_line_pixel_height = row->height;
14512 this_line_vpos = w->cursor.vpos;
14513 this_line_start_x = row->x;
14515 else
14516 CHARPOS (this_line_start_pos) = 0;
14519 return 1;
14523 /* Run window scroll functions, if any, for WINDOW with new window
14524 start STARTP. Sets the window start of WINDOW to that position.
14526 We assume that the window's buffer is really current. */
14528 static struct text_pos
14529 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14531 struct window *w = XWINDOW (window);
14532 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14534 eassert (current_buffer == XBUFFER (w->contents));
14536 if (!NILP (Vwindow_scroll_functions))
14538 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14539 make_number (CHARPOS (startp)));
14540 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14541 /* In case the hook functions switch buffers. */
14542 set_buffer_internal (XBUFFER (w->contents));
14545 return startp;
14549 /* Make sure the line containing the cursor is fully visible.
14550 A value of 1 means there is nothing to be done.
14551 (Either the line is fully visible, or it cannot be made so,
14552 or we cannot tell.)
14554 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14555 is higher than window.
14557 A value of 0 means the caller should do scrolling
14558 as if point had gone off the screen. */
14560 static int
14561 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14563 struct glyph_matrix *matrix;
14564 struct glyph_row *row;
14565 int window_height;
14567 if (!make_cursor_line_fully_visible_p)
14568 return 1;
14570 /* It's not always possible to find the cursor, e.g, when a window
14571 is full of overlay strings. Don't do anything in that case. */
14572 if (w->cursor.vpos < 0)
14573 return 1;
14575 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14576 row = MATRIX_ROW (matrix, w->cursor.vpos);
14578 /* If the cursor row is not partially visible, there's nothing to do. */
14579 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14580 return 1;
14582 /* If the row the cursor is in is taller than the window's height,
14583 it's not clear what to do, so do nothing. */
14584 window_height = window_box_height (w);
14585 if (row->height >= window_height)
14587 if (!force_p || MINI_WINDOW_P (w)
14588 || w->vscroll || w->cursor.vpos == 0)
14589 return 1;
14591 return 0;
14595 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14596 non-zero means only WINDOW is redisplayed in redisplay_internal.
14597 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14598 in redisplay_window to bring a partially visible line into view in
14599 the case that only the cursor has moved.
14601 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14602 last screen line's vertical height extends past the end of the screen.
14604 Value is
14606 1 if scrolling succeeded
14608 0 if scrolling didn't find point.
14610 -1 if new fonts have been loaded so that we must interrupt
14611 redisplay, adjust glyph matrices, and try again. */
14613 enum
14615 SCROLLING_SUCCESS,
14616 SCROLLING_FAILED,
14617 SCROLLING_NEED_LARGER_MATRICES
14620 /* If scroll-conservatively is more than this, never recenter.
14622 If you change this, don't forget to update the doc string of
14623 `scroll-conservatively' and the Emacs manual. */
14624 #define SCROLL_LIMIT 100
14626 static int
14627 try_scrolling (Lisp_Object window, int just_this_one_p,
14628 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14629 int temp_scroll_step, int last_line_misfit)
14631 struct window *w = XWINDOW (window);
14632 struct frame *f = XFRAME (w->frame);
14633 struct text_pos pos, startp;
14634 struct it it;
14635 int this_scroll_margin, scroll_max, rc, height;
14636 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14637 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14638 Lisp_Object aggressive;
14639 /* We will never try scrolling more than this number of lines. */
14640 int scroll_limit = SCROLL_LIMIT;
14641 int frame_line_height = default_line_pixel_height (w);
14642 int window_total_lines
14643 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14645 #ifdef GLYPH_DEBUG
14646 debug_method_add (w, "try_scrolling");
14647 #endif
14649 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14651 /* Compute scroll margin height in pixels. We scroll when point is
14652 within this distance from the top or bottom of the window. */
14653 if (scroll_margin > 0)
14654 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14655 * frame_line_height;
14656 else
14657 this_scroll_margin = 0;
14659 /* Force arg_scroll_conservatively to have a reasonable value, to
14660 avoid scrolling too far away with slow move_it_* functions. Note
14661 that the user can supply scroll-conservatively equal to
14662 `most-positive-fixnum', which can be larger than INT_MAX. */
14663 if (arg_scroll_conservatively > scroll_limit)
14665 arg_scroll_conservatively = scroll_limit + 1;
14666 scroll_max = scroll_limit * frame_line_height;
14668 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14669 /* Compute how much we should try to scroll maximally to bring
14670 point into view. */
14671 scroll_max = (max (scroll_step,
14672 max (arg_scroll_conservatively, temp_scroll_step))
14673 * frame_line_height);
14674 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14675 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14676 /* We're trying to scroll because of aggressive scrolling but no
14677 scroll_step is set. Choose an arbitrary one. */
14678 scroll_max = 10 * frame_line_height;
14679 else
14680 scroll_max = 0;
14682 too_near_end:
14684 /* Decide whether to scroll down. */
14685 if (PT > CHARPOS (startp))
14687 int scroll_margin_y;
14689 /* Compute the pixel ypos of the scroll margin, then move IT to
14690 either that ypos or PT, whichever comes first. */
14691 start_display (&it, w, startp);
14692 scroll_margin_y = it.last_visible_y - this_scroll_margin
14693 - frame_line_height * extra_scroll_margin_lines;
14694 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14695 (MOVE_TO_POS | MOVE_TO_Y));
14697 if (PT > CHARPOS (it.current.pos))
14699 int y0 = line_bottom_y (&it);
14700 /* Compute how many pixels below window bottom to stop searching
14701 for PT. This avoids costly search for PT that is far away if
14702 the user limited scrolling by a small number of lines, but
14703 always finds PT if scroll_conservatively is set to a large
14704 number, such as most-positive-fixnum. */
14705 int slack = max (scroll_max, 10 * frame_line_height);
14706 int y_to_move = it.last_visible_y + slack;
14708 /* Compute the distance from the scroll margin to PT or to
14709 the scroll limit, whichever comes first. This should
14710 include the height of the cursor line, to make that line
14711 fully visible. */
14712 move_it_to (&it, PT, -1, y_to_move,
14713 -1, MOVE_TO_POS | MOVE_TO_Y);
14714 dy = line_bottom_y (&it) - y0;
14716 if (dy > scroll_max)
14717 return SCROLLING_FAILED;
14719 if (dy > 0)
14720 scroll_down_p = 1;
14724 if (scroll_down_p)
14726 /* Point is in or below the bottom scroll margin, so move the
14727 window start down. If scrolling conservatively, move it just
14728 enough down to make point visible. If scroll_step is set,
14729 move it down by scroll_step. */
14730 if (arg_scroll_conservatively)
14731 amount_to_scroll
14732 = min (max (dy, frame_line_height),
14733 frame_line_height * arg_scroll_conservatively);
14734 else if (scroll_step || temp_scroll_step)
14735 amount_to_scroll = scroll_max;
14736 else
14738 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14739 height = WINDOW_BOX_TEXT_HEIGHT (w);
14740 if (NUMBERP (aggressive))
14742 double float_amount = XFLOATINT (aggressive) * height;
14743 int aggressive_scroll = float_amount;
14744 if (aggressive_scroll == 0 && float_amount > 0)
14745 aggressive_scroll = 1;
14746 /* Don't let point enter the scroll margin near top of
14747 the window. This could happen if the value of
14748 scroll_up_aggressively is too large and there are
14749 non-zero margins, because scroll_up_aggressively
14750 means put point that fraction of window height
14751 _from_the_bottom_margin_. */
14752 if (aggressive_scroll + 2*this_scroll_margin > height)
14753 aggressive_scroll = height - 2*this_scroll_margin;
14754 amount_to_scroll = dy + aggressive_scroll;
14758 if (amount_to_scroll <= 0)
14759 return SCROLLING_FAILED;
14761 start_display (&it, w, startp);
14762 if (arg_scroll_conservatively <= scroll_limit)
14763 move_it_vertically (&it, amount_to_scroll);
14764 else
14766 /* Extra precision for users who set scroll-conservatively
14767 to a large number: make sure the amount we scroll
14768 the window start is never less than amount_to_scroll,
14769 which was computed as distance from window bottom to
14770 point. This matters when lines at window top and lines
14771 below window bottom have different height. */
14772 struct it it1;
14773 void *it1data = NULL;
14774 /* We use a temporary it1 because line_bottom_y can modify
14775 its argument, if it moves one line down; see there. */
14776 int start_y;
14778 SAVE_IT (it1, it, it1data);
14779 start_y = line_bottom_y (&it1);
14780 do {
14781 RESTORE_IT (&it, &it, it1data);
14782 move_it_by_lines (&it, 1);
14783 SAVE_IT (it1, it, it1data);
14784 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14787 /* If STARTP is unchanged, move it down another screen line. */
14788 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14789 move_it_by_lines (&it, 1);
14790 startp = it.current.pos;
14792 else
14794 struct text_pos scroll_margin_pos = startp;
14795 int y_offset = 0;
14797 /* See if point is inside the scroll margin at the top of the
14798 window. */
14799 if (this_scroll_margin)
14801 int y_start;
14803 start_display (&it, w, startp);
14804 y_start = it.current_y;
14805 move_it_vertically (&it, this_scroll_margin);
14806 scroll_margin_pos = it.current.pos;
14807 /* If we didn't move enough before hitting ZV, request
14808 additional amount of scroll, to move point out of the
14809 scroll margin. */
14810 if (IT_CHARPOS (it) == ZV
14811 && it.current_y - y_start < this_scroll_margin)
14812 y_offset = this_scroll_margin - (it.current_y - y_start);
14815 if (PT < CHARPOS (scroll_margin_pos))
14817 /* Point is in the scroll margin at the top of the window or
14818 above what is displayed in the window. */
14819 int y0, y_to_move;
14821 /* Compute the vertical distance from PT to the scroll
14822 margin position. Move as far as scroll_max allows, or
14823 one screenful, or 10 screen lines, whichever is largest.
14824 Give up if distance is greater than scroll_max or if we
14825 didn't reach the scroll margin position. */
14826 SET_TEXT_POS (pos, PT, PT_BYTE);
14827 start_display (&it, w, pos);
14828 y0 = it.current_y;
14829 y_to_move = max (it.last_visible_y,
14830 max (scroll_max, 10 * frame_line_height));
14831 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14832 y_to_move, -1,
14833 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14834 dy = it.current_y - y0;
14835 if (dy > scroll_max
14836 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14837 return SCROLLING_FAILED;
14839 /* Additional scroll for when ZV was too close to point. */
14840 dy += y_offset;
14842 /* Compute new window start. */
14843 start_display (&it, w, startp);
14845 if (arg_scroll_conservatively)
14846 amount_to_scroll = max (dy, frame_line_height *
14847 max (scroll_step, temp_scroll_step));
14848 else if (scroll_step || temp_scroll_step)
14849 amount_to_scroll = scroll_max;
14850 else
14852 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14853 height = WINDOW_BOX_TEXT_HEIGHT (w);
14854 if (NUMBERP (aggressive))
14856 double float_amount = XFLOATINT (aggressive) * height;
14857 int aggressive_scroll = float_amount;
14858 if (aggressive_scroll == 0 && float_amount > 0)
14859 aggressive_scroll = 1;
14860 /* Don't let point enter the scroll margin near
14861 bottom of the window, if the value of
14862 scroll_down_aggressively happens to be too
14863 large. */
14864 if (aggressive_scroll + 2*this_scroll_margin > height)
14865 aggressive_scroll = height - 2*this_scroll_margin;
14866 amount_to_scroll = dy + aggressive_scroll;
14870 if (amount_to_scroll <= 0)
14871 return SCROLLING_FAILED;
14873 move_it_vertically_backward (&it, amount_to_scroll);
14874 startp = it.current.pos;
14878 /* Run window scroll functions. */
14879 startp = run_window_scroll_functions (window, startp);
14881 /* Display the window. Give up if new fonts are loaded, or if point
14882 doesn't appear. */
14883 if (!try_window (window, startp, 0))
14884 rc = SCROLLING_NEED_LARGER_MATRICES;
14885 else if (w->cursor.vpos < 0)
14887 clear_glyph_matrix (w->desired_matrix);
14888 rc = SCROLLING_FAILED;
14890 else
14892 /* Maybe forget recorded base line for line number display. */
14893 if (!just_this_one_p
14894 || current_buffer->clip_changed
14895 || BEG_UNCHANGED < CHARPOS (startp))
14896 w->base_line_number = 0;
14898 /* If cursor ends up on a partially visible line,
14899 treat that as being off the bottom of the screen. */
14900 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14901 /* It's possible that the cursor is on the first line of the
14902 buffer, which is partially obscured due to a vscroll
14903 (Bug#7537). In that case, avoid looping forever . */
14904 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14906 clear_glyph_matrix (w->desired_matrix);
14907 ++extra_scroll_margin_lines;
14908 goto too_near_end;
14910 rc = SCROLLING_SUCCESS;
14913 return rc;
14917 /* Compute a suitable window start for window W if display of W starts
14918 on a continuation line. Value is non-zero if a new window start
14919 was computed.
14921 The new window start will be computed, based on W's width, starting
14922 from the start of the continued line. It is the start of the
14923 screen line with the minimum distance from the old start W->start. */
14925 static int
14926 compute_window_start_on_continuation_line (struct window *w)
14928 struct text_pos pos, start_pos;
14929 int window_start_changed_p = 0;
14931 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14933 /* If window start is on a continuation line... Window start may be
14934 < BEGV in case there's invisible text at the start of the
14935 buffer (M-x rmail, for example). */
14936 if (CHARPOS (start_pos) > BEGV
14937 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14939 struct it it;
14940 struct glyph_row *row;
14942 /* Handle the case that the window start is out of range. */
14943 if (CHARPOS (start_pos) < BEGV)
14944 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14945 else if (CHARPOS (start_pos) > ZV)
14946 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14948 /* Find the start of the continued line. This should be fast
14949 because find_newline is fast (newline cache). */
14950 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14951 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14952 row, DEFAULT_FACE_ID);
14953 reseat_at_previous_visible_line_start (&it);
14955 /* If the line start is "too far" away from the window start,
14956 say it takes too much time to compute a new window start. */
14957 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14958 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14960 int min_distance, distance;
14962 /* Move forward by display lines to find the new window
14963 start. If window width was enlarged, the new start can
14964 be expected to be > the old start. If window width was
14965 decreased, the new window start will be < the old start.
14966 So, we're looking for the display line start with the
14967 minimum distance from the old window start. */
14968 pos = it.current.pos;
14969 min_distance = INFINITY;
14970 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14971 distance < min_distance)
14973 min_distance = distance;
14974 pos = it.current.pos;
14975 if (it.line_wrap == WORD_WRAP)
14977 /* Under WORD_WRAP, move_it_by_lines is likely to
14978 overshoot and stop not at the first, but the
14979 second character from the left margin. So in
14980 that case, we need a more tight control on the X
14981 coordinate of the iterator than move_it_by_lines
14982 promises in its contract. The method is to first
14983 go to the last (rightmost) visible character of a
14984 line, then move to the leftmost character on the
14985 next line in a separate call. */
14986 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14987 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14988 move_it_to (&it, ZV, 0,
14989 it.current_y + it.max_ascent + it.max_descent, -1,
14990 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14992 else
14993 move_it_by_lines (&it, 1);
14996 /* Set the window start there. */
14997 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14998 window_start_changed_p = 1;
15002 return window_start_changed_p;
15006 /* Try cursor movement in case text has not changed in window WINDOW,
15007 with window start STARTP. Value is
15009 CURSOR_MOVEMENT_SUCCESS if successful
15011 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15013 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15014 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15015 we want to scroll as if scroll-step were set to 1. See the code.
15017 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15018 which case we have to abort this redisplay, and adjust matrices
15019 first. */
15021 enum
15023 CURSOR_MOVEMENT_SUCCESS,
15024 CURSOR_MOVEMENT_CANNOT_BE_USED,
15025 CURSOR_MOVEMENT_MUST_SCROLL,
15026 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15029 static int
15030 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15032 struct window *w = XWINDOW (window);
15033 struct frame *f = XFRAME (w->frame);
15034 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15036 #ifdef GLYPH_DEBUG
15037 if (inhibit_try_cursor_movement)
15038 return rc;
15039 #endif
15041 /* Previously, there was a check for Lisp integer in the
15042 if-statement below. Now, this field is converted to
15043 ptrdiff_t, thus zero means invalid position in a buffer. */
15044 eassert (w->last_point > 0);
15045 /* Likewise there was a check whether window_end_vpos is nil or larger
15046 than the window. Now window_end_vpos is int and so never nil, but
15047 let's leave eassert to check whether it fits in the window. */
15048 eassert (w->window_end_vpos < w->current_matrix->nrows);
15050 /* Handle case where text has not changed, only point, and it has
15051 not moved off the frame. */
15052 if (/* Point may be in this window. */
15053 PT >= CHARPOS (startp)
15054 /* Selective display hasn't changed. */
15055 && !current_buffer->clip_changed
15056 /* Function force-mode-line-update is used to force a thorough
15057 redisplay. It sets either windows_or_buffers_changed or
15058 update_mode_lines. So don't take a shortcut here for these
15059 cases. */
15060 && !update_mode_lines
15061 && !windows_or_buffers_changed
15062 && !f->cursor_type_changed
15063 /* Can't use this case if highlighting a region. When a
15064 region exists, cursor movement has to do more than just
15065 set the cursor. */
15066 && markpos_of_region () < 0
15067 && !w->region_showing
15068 && NILP (Vshow_trailing_whitespace)
15069 /* This code is not used for mini-buffer for the sake of the case
15070 of redisplaying to replace an echo area message; since in
15071 that case the mini-buffer contents per se are usually
15072 unchanged. This code is of no real use in the mini-buffer
15073 since the handling of this_line_start_pos, etc., in redisplay
15074 handles the same cases. */
15075 && !EQ (window, minibuf_window)
15076 && (FRAME_WINDOW_P (f)
15077 || !overlay_arrow_in_current_buffer_p ()))
15079 int this_scroll_margin, top_scroll_margin;
15080 struct glyph_row *row = NULL;
15081 int frame_line_height = default_line_pixel_height (w);
15082 int window_total_lines
15083 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15085 #ifdef GLYPH_DEBUG
15086 debug_method_add (w, "cursor movement");
15087 #endif
15089 /* Scroll if point within this distance from the top or bottom
15090 of the window. This is a pixel value. */
15091 if (scroll_margin > 0)
15093 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15094 this_scroll_margin *= frame_line_height;
15096 else
15097 this_scroll_margin = 0;
15099 top_scroll_margin = this_scroll_margin;
15100 if (WINDOW_WANTS_HEADER_LINE_P (w))
15101 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15103 /* Start with the row the cursor was displayed during the last
15104 not paused redisplay. Give up if that row is not valid. */
15105 if (w->last_cursor_vpos < 0
15106 || w->last_cursor_vpos >= w->current_matrix->nrows)
15107 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15108 else
15110 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15111 if (row->mode_line_p)
15112 ++row;
15113 if (!row->enabled_p)
15114 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15117 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15119 int scroll_p = 0, must_scroll = 0;
15120 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15122 if (PT > w->last_point)
15124 /* Point has moved forward. */
15125 while (MATRIX_ROW_END_CHARPOS (row) < PT
15126 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15128 eassert (row->enabled_p);
15129 ++row;
15132 /* If the end position of a row equals the start
15133 position of the next row, and PT is at that position,
15134 we would rather display cursor in the next line. */
15135 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15136 && MATRIX_ROW_END_CHARPOS (row) == PT
15137 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15138 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15139 && !cursor_row_p (row))
15140 ++row;
15142 /* If within the scroll margin, scroll. Note that
15143 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15144 the next line would be drawn, and that
15145 this_scroll_margin can be zero. */
15146 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15147 || PT > MATRIX_ROW_END_CHARPOS (row)
15148 /* Line is completely visible last line in window
15149 and PT is to be set in the next line. */
15150 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15151 && PT == MATRIX_ROW_END_CHARPOS (row)
15152 && !row->ends_at_zv_p
15153 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15154 scroll_p = 1;
15156 else if (PT < w->last_point)
15158 /* Cursor has to be moved backward. Note that PT >=
15159 CHARPOS (startp) because of the outer if-statement. */
15160 while (!row->mode_line_p
15161 && (MATRIX_ROW_START_CHARPOS (row) > PT
15162 || (MATRIX_ROW_START_CHARPOS (row) == PT
15163 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15164 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15165 row > w->current_matrix->rows
15166 && (row-1)->ends_in_newline_from_string_p))))
15167 && (row->y > top_scroll_margin
15168 || CHARPOS (startp) == BEGV))
15170 eassert (row->enabled_p);
15171 --row;
15174 /* Consider the following case: Window starts at BEGV,
15175 there is invisible, intangible text at BEGV, so that
15176 display starts at some point START > BEGV. It can
15177 happen that we are called with PT somewhere between
15178 BEGV and START. Try to handle that case. */
15179 if (row < w->current_matrix->rows
15180 || row->mode_line_p)
15182 row = w->current_matrix->rows;
15183 if (row->mode_line_p)
15184 ++row;
15187 /* Due to newlines in overlay strings, we may have to
15188 skip forward over overlay strings. */
15189 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15190 && MATRIX_ROW_END_CHARPOS (row) == PT
15191 && !cursor_row_p (row))
15192 ++row;
15194 /* If within the scroll margin, scroll. */
15195 if (row->y < top_scroll_margin
15196 && CHARPOS (startp) != BEGV)
15197 scroll_p = 1;
15199 else
15201 /* Cursor did not move. So don't scroll even if cursor line
15202 is partially visible, as it was so before. */
15203 rc = CURSOR_MOVEMENT_SUCCESS;
15206 if (PT < MATRIX_ROW_START_CHARPOS (row)
15207 || PT > MATRIX_ROW_END_CHARPOS (row))
15209 /* if PT is not in the glyph row, give up. */
15210 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15211 must_scroll = 1;
15213 else if (rc != CURSOR_MOVEMENT_SUCCESS
15214 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15216 struct glyph_row *row1;
15218 /* If rows are bidi-reordered and point moved, back up
15219 until we find a row that does not belong to a
15220 continuation line. This is because we must consider
15221 all rows of a continued line as candidates for the
15222 new cursor positioning, since row start and end
15223 positions change non-linearly with vertical position
15224 in such rows. */
15225 /* FIXME: Revisit this when glyph ``spilling'' in
15226 continuation lines' rows is implemented for
15227 bidi-reordered rows. */
15228 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15229 MATRIX_ROW_CONTINUATION_LINE_P (row);
15230 --row)
15232 /* If we hit the beginning of the displayed portion
15233 without finding the first row of a continued
15234 line, give up. */
15235 if (row <= row1)
15237 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15238 break;
15240 eassert (row->enabled_p);
15243 if (must_scroll)
15245 else if (rc != CURSOR_MOVEMENT_SUCCESS
15246 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15247 /* Make sure this isn't a header line by any chance, since
15248 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15249 && !row->mode_line_p
15250 && make_cursor_line_fully_visible_p)
15252 if (PT == MATRIX_ROW_END_CHARPOS (row)
15253 && !row->ends_at_zv_p
15254 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15255 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15256 else if (row->height > window_box_height (w))
15258 /* If we end up in a partially visible line, let's
15259 make it fully visible, except when it's taller
15260 than the window, in which case we can't do much
15261 about it. */
15262 *scroll_step = 1;
15263 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15265 else
15267 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15268 if (!cursor_row_fully_visible_p (w, 0, 1))
15269 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15270 else
15271 rc = CURSOR_MOVEMENT_SUCCESS;
15274 else if (scroll_p)
15275 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15276 else if (rc != CURSOR_MOVEMENT_SUCCESS
15277 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15279 /* With bidi-reordered rows, there could be more than
15280 one candidate row whose start and end positions
15281 occlude point. We need to let set_cursor_from_row
15282 find the best candidate. */
15283 /* FIXME: Revisit this when glyph ``spilling'' in
15284 continuation lines' rows is implemented for
15285 bidi-reordered rows. */
15286 int rv = 0;
15290 int at_zv_p = 0, exact_match_p = 0;
15292 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15293 && PT <= MATRIX_ROW_END_CHARPOS (row)
15294 && cursor_row_p (row))
15295 rv |= set_cursor_from_row (w, row, w->current_matrix,
15296 0, 0, 0, 0);
15297 /* As soon as we've found the exact match for point,
15298 or the first suitable row whose ends_at_zv_p flag
15299 is set, we are done. */
15300 at_zv_p =
15301 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15302 if (rv && !at_zv_p
15303 && w->cursor.hpos >= 0
15304 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15305 w->cursor.vpos))
15307 struct glyph_row *candidate =
15308 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15309 struct glyph *g =
15310 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15311 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15313 exact_match_p =
15314 (BUFFERP (g->object) && g->charpos == PT)
15315 || (INTEGERP (g->object)
15316 && (g->charpos == PT
15317 || (g->charpos == 0 && endpos - 1 == PT)));
15319 if (rv && (at_zv_p || exact_match_p))
15321 rc = CURSOR_MOVEMENT_SUCCESS;
15322 break;
15324 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15325 break;
15326 ++row;
15328 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15329 || row->continued_p)
15330 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15331 || (MATRIX_ROW_START_CHARPOS (row) == PT
15332 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15333 /* If we didn't find any candidate rows, or exited the
15334 loop before all the candidates were examined, signal
15335 to the caller that this method failed. */
15336 if (rc != CURSOR_MOVEMENT_SUCCESS
15337 && !(rv
15338 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15339 && !row->continued_p))
15340 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15341 else if (rv)
15342 rc = CURSOR_MOVEMENT_SUCCESS;
15344 else
15348 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15350 rc = CURSOR_MOVEMENT_SUCCESS;
15351 break;
15353 ++row;
15355 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15356 && MATRIX_ROW_START_CHARPOS (row) == PT
15357 && cursor_row_p (row));
15362 return rc;
15365 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15366 static
15367 #endif
15368 void
15369 set_vertical_scroll_bar (struct window *w)
15371 ptrdiff_t start, end, whole;
15373 /* Calculate the start and end positions for the current window.
15374 At some point, it would be nice to choose between scrollbars
15375 which reflect the whole buffer size, with special markers
15376 indicating narrowing, and scrollbars which reflect only the
15377 visible region.
15379 Note that mini-buffers sometimes aren't displaying any text. */
15380 if (!MINI_WINDOW_P (w)
15381 || (w == XWINDOW (minibuf_window)
15382 && NILP (echo_area_buffer[0])))
15384 struct buffer *buf = XBUFFER (w->contents);
15385 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15386 start = marker_position (w->start) - BUF_BEGV (buf);
15387 /* I don't think this is guaranteed to be right. For the
15388 moment, we'll pretend it is. */
15389 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15391 if (end < start)
15392 end = start;
15393 if (whole < (end - start))
15394 whole = end - start;
15396 else
15397 start = end = whole = 0;
15399 /* Indicate what this scroll bar ought to be displaying now. */
15400 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15401 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15402 (w, end - start, whole, start);
15406 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15407 selected_window is redisplayed.
15409 We can return without actually redisplaying the window if fonts has been
15410 changed on window's frame. In that case, redisplay_internal will retry. */
15412 static void
15413 redisplay_window (Lisp_Object window, int just_this_one_p)
15415 struct window *w = XWINDOW (window);
15416 struct frame *f = XFRAME (w->frame);
15417 struct buffer *buffer = XBUFFER (w->contents);
15418 struct buffer *old = current_buffer;
15419 struct text_pos lpoint, opoint, startp;
15420 int update_mode_line;
15421 int tem;
15422 struct it it;
15423 /* Record it now because it's overwritten. */
15424 int current_matrix_up_to_date_p = 0;
15425 int used_current_matrix_p = 0;
15426 /* This is less strict than current_matrix_up_to_date_p.
15427 It indicates that the buffer contents and narrowing are unchanged. */
15428 int buffer_unchanged_p = 0;
15429 int temp_scroll_step = 0;
15430 ptrdiff_t count = SPECPDL_INDEX ();
15431 int rc;
15432 int centering_position = -1;
15433 int last_line_misfit = 0;
15434 ptrdiff_t beg_unchanged, end_unchanged;
15435 int frame_line_height;
15437 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15438 opoint = lpoint;
15440 #ifdef GLYPH_DEBUG
15441 *w->desired_matrix->method = 0;
15442 #endif
15444 /* Make sure that both W's markers are valid. */
15445 eassert (XMARKER (w->start)->buffer == buffer);
15446 eassert (XMARKER (w->pointm)->buffer == buffer);
15448 restart:
15449 reconsider_clip_changes (w);
15450 frame_line_height = default_line_pixel_height (w);
15452 /* Has the mode line to be updated? */
15453 update_mode_line = (w->update_mode_line
15454 || update_mode_lines
15455 || buffer->clip_changed
15456 || buffer->prevent_redisplay_optimizations_p);
15458 if (MINI_WINDOW_P (w))
15460 if (w == XWINDOW (echo_area_window)
15461 && !NILP (echo_area_buffer[0]))
15463 if (update_mode_line)
15464 /* We may have to update a tty frame's menu bar or a
15465 tool-bar. Example `M-x C-h C-h C-g'. */
15466 goto finish_menu_bars;
15467 else
15468 /* We've already displayed the echo area glyphs in this window. */
15469 goto finish_scroll_bars;
15471 else if ((w != XWINDOW (minibuf_window)
15472 || minibuf_level == 0)
15473 /* When buffer is nonempty, redisplay window normally. */
15474 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15475 /* Quail displays non-mini buffers in minibuffer window.
15476 In that case, redisplay the window normally. */
15477 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15479 /* W is a mini-buffer window, but it's not active, so clear
15480 it. */
15481 int yb = window_text_bottom_y (w);
15482 struct glyph_row *row;
15483 int y;
15485 for (y = 0, row = w->desired_matrix->rows;
15486 y < yb;
15487 y += row->height, ++row)
15488 blank_row (w, row, y);
15489 goto finish_scroll_bars;
15492 clear_glyph_matrix (w->desired_matrix);
15495 /* Otherwise set up data on this window; select its buffer and point
15496 value. */
15497 /* Really select the buffer, for the sake of buffer-local
15498 variables. */
15499 set_buffer_internal_1 (XBUFFER (w->contents));
15501 current_matrix_up_to_date_p
15502 = (w->window_end_valid
15503 && !current_buffer->clip_changed
15504 && !current_buffer->prevent_redisplay_optimizations_p
15505 && !window_outdated (w));
15507 /* Run the window-bottom-change-functions
15508 if it is possible that the text on the screen has changed
15509 (either due to modification of the text, or any other reason). */
15510 if (!current_matrix_up_to_date_p
15511 && !NILP (Vwindow_text_change_functions))
15513 safe_run_hooks (Qwindow_text_change_functions);
15514 goto restart;
15517 beg_unchanged = BEG_UNCHANGED;
15518 end_unchanged = END_UNCHANGED;
15520 SET_TEXT_POS (opoint, PT, PT_BYTE);
15522 specbind (Qinhibit_point_motion_hooks, Qt);
15524 buffer_unchanged_p
15525 = (w->window_end_valid
15526 && !current_buffer->clip_changed
15527 && !window_outdated (w));
15529 /* When windows_or_buffers_changed is non-zero, we can't rely
15530 on the window end being valid, so set it to zero there. */
15531 if (windows_or_buffers_changed)
15533 /* If window starts on a continuation line, maybe adjust the
15534 window start in case the window's width changed. */
15535 if (XMARKER (w->start)->buffer == current_buffer)
15536 compute_window_start_on_continuation_line (w);
15538 w->window_end_valid = 0;
15539 /* If so, we also can't rely on current matrix
15540 and should not fool try_cursor_movement below. */
15541 current_matrix_up_to_date_p = 0;
15544 /* Some sanity checks. */
15545 CHECK_WINDOW_END (w);
15546 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15547 emacs_abort ();
15548 if (BYTEPOS (opoint) < CHARPOS (opoint))
15549 emacs_abort ();
15551 if (mode_line_update_needed (w))
15552 update_mode_line = 1;
15554 /* Point refers normally to the selected window. For any other
15555 window, set up appropriate value. */
15556 if (!EQ (window, selected_window))
15558 ptrdiff_t new_pt = marker_position (w->pointm);
15559 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15560 if (new_pt < BEGV)
15562 new_pt = BEGV;
15563 new_pt_byte = BEGV_BYTE;
15564 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15566 else if (new_pt > (ZV - 1))
15568 new_pt = ZV;
15569 new_pt_byte = ZV_BYTE;
15570 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15573 /* We don't use SET_PT so that the point-motion hooks don't run. */
15574 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15577 /* If any of the character widths specified in the display table
15578 have changed, invalidate the width run cache. It's true that
15579 this may be a bit late to catch such changes, but the rest of
15580 redisplay goes (non-fatally) haywire when the display table is
15581 changed, so why should we worry about doing any better? */
15582 if (current_buffer->width_run_cache)
15584 struct Lisp_Char_Table *disptab = buffer_display_table ();
15586 if (! disptab_matches_widthtab
15587 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15589 invalidate_region_cache (current_buffer,
15590 current_buffer->width_run_cache,
15591 BEG, Z);
15592 recompute_width_table (current_buffer, disptab);
15596 /* If window-start is screwed up, choose a new one. */
15597 if (XMARKER (w->start)->buffer != current_buffer)
15598 goto recenter;
15600 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15602 /* If someone specified a new starting point but did not insist,
15603 check whether it can be used. */
15604 if (w->optional_new_start
15605 && CHARPOS (startp) >= BEGV
15606 && CHARPOS (startp) <= ZV)
15608 w->optional_new_start = 0;
15609 start_display (&it, w, startp);
15610 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15611 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15612 if (IT_CHARPOS (it) == PT)
15613 w->force_start = 1;
15614 /* IT may overshoot PT if text at PT is invisible. */
15615 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15616 w->force_start = 1;
15619 force_start:
15621 /* Handle case where place to start displaying has been specified,
15622 unless the specified location is outside the accessible range. */
15623 if (w->force_start || window_frozen_p (w))
15625 /* We set this later on if we have to adjust point. */
15626 int new_vpos = -1;
15628 w->force_start = 0;
15629 w->vscroll = 0;
15630 w->window_end_valid = 0;
15632 /* Forget any recorded base line for line number display. */
15633 if (!buffer_unchanged_p)
15634 w->base_line_number = 0;
15636 /* Redisplay the mode line. Select the buffer properly for that.
15637 Also, run the hook window-scroll-functions
15638 because we have scrolled. */
15639 /* Note, we do this after clearing force_start because
15640 if there's an error, it is better to forget about force_start
15641 than to get into an infinite loop calling the hook functions
15642 and having them get more errors. */
15643 if (!update_mode_line
15644 || ! NILP (Vwindow_scroll_functions))
15646 update_mode_line = 1;
15647 w->update_mode_line = 1;
15648 startp = run_window_scroll_functions (window, startp);
15651 if (CHARPOS (startp) < BEGV)
15652 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15653 else if (CHARPOS (startp) > ZV)
15654 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15656 /* Redisplay, then check if cursor has been set during the
15657 redisplay. Give up if new fonts were loaded. */
15658 /* We used to issue a CHECK_MARGINS argument to try_window here,
15659 but this causes scrolling to fail when point begins inside
15660 the scroll margin (bug#148) -- cyd */
15661 if (!try_window (window, startp, 0))
15663 w->force_start = 1;
15664 clear_glyph_matrix (w->desired_matrix);
15665 goto need_larger_matrices;
15668 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15670 /* If point does not appear, try to move point so it does
15671 appear. The desired matrix has been built above, so we
15672 can use it here. */
15673 new_vpos = window_box_height (w) / 2;
15676 if (!cursor_row_fully_visible_p (w, 0, 0))
15678 /* Point does appear, but on a line partly visible at end of window.
15679 Move it back to a fully-visible line. */
15680 new_vpos = window_box_height (w);
15682 else if (w->cursor.vpos >=0)
15684 /* Some people insist on not letting point enter the scroll
15685 margin, even though this part handles windows that didn't
15686 scroll at all. */
15687 int window_total_lines
15688 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15689 int margin = min (scroll_margin, window_total_lines / 4);
15690 int pixel_margin = margin * frame_line_height;
15691 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15693 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15694 below, which finds the row to move point to, advances by
15695 the Y coordinate of the _next_ row, see the definition of
15696 MATRIX_ROW_BOTTOM_Y. */
15697 if (w->cursor.vpos < margin + header_line)
15699 w->cursor.vpos = -1;
15700 clear_glyph_matrix (w->desired_matrix);
15701 goto try_to_scroll;
15703 else
15705 int window_height = window_box_height (w);
15707 if (header_line)
15708 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15709 if (w->cursor.y >= window_height - pixel_margin)
15711 w->cursor.vpos = -1;
15712 clear_glyph_matrix (w->desired_matrix);
15713 goto try_to_scroll;
15718 /* If we need to move point for either of the above reasons,
15719 now actually do it. */
15720 if (new_vpos >= 0)
15722 struct glyph_row *row;
15724 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15725 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15726 ++row;
15728 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15729 MATRIX_ROW_START_BYTEPOS (row));
15731 if (w != XWINDOW (selected_window))
15732 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15733 else if (current_buffer == old)
15734 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15736 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15738 /* If we are highlighting the region, then we just changed
15739 the region, so redisplay to show it. */
15740 if (markpos_of_region () >= 0)
15742 clear_glyph_matrix (w->desired_matrix);
15743 if (!try_window (window, startp, 0))
15744 goto need_larger_matrices;
15748 #ifdef GLYPH_DEBUG
15749 debug_method_add (w, "forced window start");
15750 #endif
15751 goto done;
15754 /* Handle case where text has not changed, only point, and it has
15755 not moved off the frame, and we are not retrying after hscroll.
15756 (current_matrix_up_to_date_p is nonzero when retrying.) */
15757 if (current_matrix_up_to_date_p
15758 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15759 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15761 switch (rc)
15763 case CURSOR_MOVEMENT_SUCCESS:
15764 used_current_matrix_p = 1;
15765 goto done;
15767 case CURSOR_MOVEMENT_MUST_SCROLL:
15768 goto try_to_scroll;
15770 default:
15771 emacs_abort ();
15774 /* If current starting point was originally the beginning of a line
15775 but no longer is, find a new starting point. */
15776 else if (w->start_at_line_beg
15777 && !(CHARPOS (startp) <= BEGV
15778 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15780 #ifdef GLYPH_DEBUG
15781 debug_method_add (w, "recenter 1");
15782 #endif
15783 goto recenter;
15786 /* Try scrolling with try_window_id. Value is > 0 if update has
15787 been done, it is -1 if we know that the same window start will
15788 not work. It is 0 if unsuccessful for some other reason. */
15789 else if ((tem = try_window_id (w)) != 0)
15791 #ifdef GLYPH_DEBUG
15792 debug_method_add (w, "try_window_id %d", tem);
15793 #endif
15795 if (f->fonts_changed)
15796 goto need_larger_matrices;
15797 if (tem > 0)
15798 goto done;
15800 /* Otherwise try_window_id has returned -1 which means that we
15801 don't want the alternative below this comment to execute. */
15803 else if (CHARPOS (startp) >= BEGV
15804 && CHARPOS (startp) <= ZV
15805 && PT >= CHARPOS (startp)
15806 && (CHARPOS (startp) < ZV
15807 /* Avoid starting at end of buffer. */
15808 || CHARPOS (startp) == BEGV
15809 || !window_outdated (w)))
15811 int d1, d2, d3, d4, d5, d6;
15813 /* If first window line is a continuation line, and window start
15814 is inside the modified region, but the first change is before
15815 current window start, we must select a new window start.
15817 However, if this is the result of a down-mouse event (e.g. by
15818 extending the mouse-drag-overlay), we don't want to select a
15819 new window start, since that would change the position under
15820 the mouse, resulting in an unwanted mouse-movement rather
15821 than a simple mouse-click. */
15822 if (!w->start_at_line_beg
15823 && NILP (do_mouse_tracking)
15824 && CHARPOS (startp) > BEGV
15825 && CHARPOS (startp) > BEG + beg_unchanged
15826 && CHARPOS (startp) <= Z - end_unchanged
15827 /* Even if w->start_at_line_beg is nil, a new window may
15828 start at a line_beg, since that's how set_buffer_window
15829 sets it. So, we need to check the return value of
15830 compute_window_start_on_continuation_line. (See also
15831 bug#197). */
15832 && XMARKER (w->start)->buffer == current_buffer
15833 && compute_window_start_on_continuation_line (w)
15834 /* It doesn't make sense to force the window start like we
15835 do at label force_start if it is already known that point
15836 will not be visible in the resulting window, because
15837 doing so will move point from its correct position
15838 instead of scrolling the window to bring point into view.
15839 See bug#9324. */
15840 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15842 w->force_start = 1;
15843 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15844 goto force_start;
15847 #ifdef GLYPH_DEBUG
15848 debug_method_add (w, "same window start");
15849 #endif
15851 /* Try to redisplay starting at same place as before.
15852 If point has not moved off frame, accept the results. */
15853 if (!current_matrix_up_to_date_p
15854 /* Don't use try_window_reusing_current_matrix in this case
15855 because a window scroll function can have changed the
15856 buffer. */
15857 || !NILP (Vwindow_scroll_functions)
15858 || MINI_WINDOW_P (w)
15859 || !(used_current_matrix_p
15860 = try_window_reusing_current_matrix (w)))
15862 IF_DEBUG (debug_method_add (w, "1"));
15863 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15864 /* -1 means we need to scroll.
15865 0 means we need new matrices, but fonts_changed
15866 is set in that case, so we will detect it below. */
15867 goto try_to_scroll;
15870 if (f->fonts_changed)
15871 goto need_larger_matrices;
15873 if (w->cursor.vpos >= 0)
15875 if (!just_this_one_p
15876 || current_buffer->clip_changed
15877 || BEG_UNCHANGED < CHARPOS (startp))
15878 /* Forget any recorded base line for line number display. */
15879 w->base_line_number = 0;
15881 if (!cursor_row_fully_visible_p (w, 1, 0))
15883 clear_glyph_matrix (w->desired_matrix);
15884 last_line_misfit = 1;
15886 /* Drop through and scroll. */
15887 else
15888 goto done;
15890 else
15891 clear_glyph_matrix (w->desired_matrix);
15894 try_to_scroll:
15896 /* Redisplay the mode line. Select the buffer properly for that. */
15897 if (!update_mode_line)
15899 update_mode_line = 1;
15900 w->update_mode_line = 1;
15903 /* Try to scroll by specified few lines. */
15904 if ((scroll_conservatively
15905 || emacs_scroll_step
15906 || temp_scroll_step
15907 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15908 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15909 && CHARPOS (startp) >= BEGV
15910 && CHARPOS (startp) <= ZV)
15912 /* The function returns -1 if new fonts were loaded, 1 if
15913 successful, 0 if not successful. */
15914 int ss = try_scrolling (window, just_this_one_p,
15915 scroll_conservatively,
15916 emacs_scroll_step,
15917 temp_scroll_step, last_line_misfit);
15918 switch (ss)
15920 case SCROLLING_SUCCESS:
15921 goto done;
15923 case SCROLLING_NEED_LARGER_MATRICES:
15924 goto need_larger_matrices;
15926 case SCROLLING_FAILED:
15927 break;
15929 default:
15930 emacs_abort ();
15934 /* Finally, just choose a place to start which positions point
15935 according to user preferences. */
15937 recenter:
15939 #ifdef GLYPH_DEBUG
15940 debug_method_add (w, "recenter");
15941 #endif
15943 /* Forget any previously recorded base line for line number display. */
15944 if (!buffer_unchanged_p)
15945 w->base_line_number = 0;
15947 /* Determine the window start relative to point. */
15948 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15949 it.current_y = it.last_visible_y;
15950 if (centering_position < 0)
15952 int window_total_lines
15953 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15954 int margin =
15955 scroll_margin > 0
15956 ? min (scroll_margin, window_total_lines / 4)
15957 : 0;
15958 ptrdiff_t margin_pos = CHARPOS (startp);
15959 Lisp_Object aggressive;
15960 int scrolling_up;
15962 /* If there is a scroll margin at the top of the window, find
15963 its character position. */
15964 if (margin
15965 /* Cannot call start_display if startp is not in the
15966 accessible region of the buffer. This can happen when we
15967 have just switched to a different buffer and/or changed
15968 its restriction. In that case, startp is initialized to
15969 the character position 1 (BEGV) because we did not yet
15970 have chance to display the buffer even once. */
15971 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15973 struct it it1;
15974 void *it1data = NULL;
15976 SAVE_IT (it1, it, it1data);
15977 start_display (&it1, w, startp);
15978 move_it_vertically (&it1, margin * frame_line_height);
15979 margin_pos = IT_CHARPOS (it1);
15980 RESTORE_IT (&it, &it, it1data);
15982 scrolling_up = PT > margin_pos;
15983 aggressive =
15984 scrolling_up
15985 ? BVAR (current_buffer, scroll_up_aggressively)
15986 : BVAR (current_buffer, scroll_down_aggressively);
15988 if (!MINI_WINDOW_P (w)
15989 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15991 int pt_offset = 0;
15993 /* Setting scroll-conservatively overrides
15994 scroll-*-aggressively. */
15995 if (!scroll_conservatively && NUMBERP (aggressive))
15997 double float_amount = XFLOATINT (aggressive);
15999 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16000 if (pt_offset == 0 && float_amount > 0)
16001 pt_offset = 1;
16002 if (pt_offset && margin > 0)
16003 margin -= 1;
16005 /* Compute how much to move the window start backward from
16006 point so that point will be displayed where the user
16007 wants it. */
16008 if (scrolling_up)
16010 centering_position = it.last_visible_y;
16011 if (pt_offset)
16012 centering_position -= pt_offset;
16013 centering_position -=
16014 frame_line_height * (1 + margin + (last_line_misfit != 0))
16015 + WINDOW_HEADER_LINE_HEIGHT (w);
16016 /* Don't let point enter the scroll margin near top of
16017 the window. */
16018 if (centering_position < margin * frame_line_height)
16019 centering_position = margin * frame_line_height;
16021 else
16022 centering_position = margin * frame_line_height + pt_offset;
16024 else
16025 /* Set the window start half the height of the window backward
16026 from point. */
16027 centering_position = window_box_height (w) / 2;
16029 move_it_vertically_backward (&it, centering_position);
16031 eassert (IT_CHARPOS (it) >= BEGV);
16033 /* The function move_it_vertically_backward may move over more
16034 than the specified y-distance. If it->w is small, e.g. a
16035 mini-buffer window, we may end up in front of the window's
16036 display area. Start displaying at the start of the line
16037 containing PT in this case. */
16038 if (it.current_y <= 0)
16040 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16041 move_it_vertically_backward (&it, 0);
16042 it.current_y = 0;
16045 it.current_x = it.hpos = 0;
16047 /* Set the window start position here explicitly, to avoid an
16048 infinite loop in case the functions in window-scroll-functions
16049 get errors. */
16050 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16052 /* Run scroll hooks. */
16053 startp = run_window_scroll_functions (window, it.current.pos);
16055 /* Redisplay the window. */
16056 if (!current_matrix_up_to_date_p
16057 || windows_or_buffers_changed
16058 || f->cursor_type_changed
16059 /* Don't use try_window_reusing_current_matrix in this case
16060 because it can have changed the buffer. */
16061 || !NILP (Vwindow_scroll_functions)
16062 || !just_this_one_p
16063 || MINI_WINDOW_P (w)
16064 || !(used_current_matrix_p
16065 = try_window_reusing_current_matrix (w)))
16066 try_window (window, startp, 0);
16068 /* If new fonts have been loaded (due to fontsets), give up. We
16069 have to start a new redisplay since we need to re-adjust glyph
16070 matrices. */
16071 if (f->fonts_changed)
16072 goto need_larger_matrices;
16074 /* If cursor did not appear assume that the middle of the window is
16075 in the first line of the window. Do it again with the next line.
16076 (Imagine a window of height 100, displaying two lines of height
16077 60. Moving back 50 from it->last_visible_y will end in the first
16078 line.) */
16079 if (w->cursor.vpos < 0)
16081 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16083 clear_glyph_matrix (w->desired_matrix);
16084 move_it_by_lines (&it, 1);
16085 try_window (window, it.current.pos, 0);
16087 else if (PT < IT_CHARPOS (it))
16089 clear_glyph_matrix (w->desired_matrix);
16090 move_it_by_lines (&it, -1);
16091 try_window (window, it.current.pos, 0);
16093 else
16095 /* Not much we can do about it. */
16099 /* Consider the following case: Window starts at BEGV, there is
16100 invisible, intangible text at BEGV, so that display starts at
16101 some point START > BEGV. It can happen that we are called with
16102 PT somewhere between BEGV and START. Try to handle that case. */
16103 if (w->cursor.vpos < 0)
16105 struct glyph_row *row = w->current_matrix->rows;
16106 if (row->mode_line_p)
16107 ++row;
16108 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16111 if (!cursor_row_fully_visible_p (w, 0, 0))
16113 /* If vscroll is enabled, disable it and try again. */
16114 if (w->vscroll)
16116 w->vscroll = 0;
16117 clear_glyph_matrix (w->desired_matrix);
16118 goto recenter;
16121 /* Users who set scroll-conservatively to a large number want
16122 point just above/below the scroll margin. If we ended up
16123 with point's row partially visible, move the window start to
16124 make that row fully visible and out of the margin. */
16125 if (scroll_conservatively > SCROLL_LIMIT)
16127 int window_total_lines
16128 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16129 int margin =
16130 scroll_margin > 0
16131 ? min (scroll_margin, window_total_lines / 4)
16132 : 0;
16133 int move_down = w->cursor.vpos >= window_total_lines / 2;
16135 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16136 clear_glyph_matrix (w->desired_matrix);
16137 if (1 == try_window (window, it.current.pos,
16138 TRY_WINDOW_CHECK_MARGINS))
16139 goto done;
16142 /* If centering point failed to make the whole line visible,
16143 put point at the top instead. That has to make the whole line
16144 visible, if it can be done. */
16145 if (centering_position == 0)
16146 goto done;
16148 clear_glyph_matrix (w->desired_matrix);
16149 centering_position = 0;
16150 goto recenter;
16153 done:
16155 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16156 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16157 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16159 /* Display the mode line, if we must. */
16160 if ((update_mode_line
16161 /* If window not full width, must redo its mode line
16162 if (a) the window to its side is being redone and
16163 (b) we do a frame-based redisplay. This is a consequence
16164 of how inverted lines are drawn in frame-based redisplay. */
16165 || (!just_this_one_p
16166 && !FRAME_WINDOW_P (f)
16167 && !WINDOW_FULL_WIDTH_P (w))
16168 /* Line number to display. */
16169 || w->base_line_pos > 0
16170 /* Column number is displayed and different from the one displayed. */
16171 || (w->column_number_displayed != -1
16172 && (w->column_number_displayed != current_column ())))
16173 /* This means that the window has a mode line. */
16174 && (WINDOW_WANTS_MODELINE_P (w)
16175 || WINDOW_WANTS_HEADER_LINE_P (w)))
16177 display_mode_lines (w);
16179 /* If mode line height has changed, arrange for a thorough
16180 immediate redisplay using the correct mode line height. */
16181 if (WINDOW_WANTS_MODELINE_P (w)
16182 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16184 f->fonts_changed = 1;
16185 w->mode_line_height = -1;
16186 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16187 = DESIRED_MODE_LINE_HEIGHT (w);
16190 /* If header line height has changed, arrange for a thorough
16191 immediate redisplay using the correct header line height. */
16192 if (WINDOW_WANTS_HEADER_LINE_P (w)
16193 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16195 f->fonts_changed = 1;
16196 w->header_line_height = -1;
16197 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16198 = DESIRED_HEADER_LINE_HEIGHT (w);
16201 if (f->fonts_changed)
16202 goto need_larger_matrices;
16205 if (!line_number_displayed && w->base_line_pos != -1)
16207 w->base_line_pos = 0;
16208 w->base_line_number = 0;
16211 finish_menu_bars:
16213 /* When we reach a frame's selected window, redo the frame's menu bar. */
16214 if (update_mode_line
16215 && EQ (FRAME_SELECTED_WINDOW (f), window))
16217 int redisplay_menu_p = 0;
16219 if (FRAME_WINDOW_P (f))
16221 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16222 || defined (HAVE_NS) || defined (USE_GTK)
16223 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16224 #else
16225 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16226 #endif
16228 else
16229 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16231 if (redisplay_menu_p)
16232 display_menu_bar (w);
16234 #ifdef HAVE_WINDOW_SYSTEM
16235 if (FRAME_WINDOW_P (f))
16237 #if defined (USE_GTK) || defined (HAVE_NS)
16238 if (FRAME_EXTERNAL_TOOL_BAR (f))
16239 redisplay_tool_bar (f);
16240 #else
16241 if (WINDOWP (f->tool_bar_window)
16242 && (FRAME_TOOL_BAR_LINES (f) > 0
16243 || !NILP (Vauto_resize_tool_bars))
16244 && redisplay_tool_bar (f))
16245 ignore_mouse_drag_p = 1;
16246 #endif
16248 #endif
16251 #ifdef HAVE_WINDOW_SYSTEM
16252 if (FRAME_WINDOW_P (f)
16253 && update_window_fringes (w, (just_this_one_p
16254 || (!used_current_matrix_p && !overlay_arrow_seen)
16255 || w->pseudo_window_p)))
16257 update_begin (f);
16258 block_input ();
16259 if (draw_window_fringes (w, 1))
16260 x_draw_vertical_border (w);
16261 unblock_input ();
16262 update_end (f);
16264 #endif /* HAVE_WINDOW_SYSTEM */
16266 /* We go to this label, with fonts_changed set, if it is
16267 necessary to try again using larger glyph matrices.
16268 We have to redeem the scroll bar even in this case,
16269 because the loop in redisplay_internal expects that. */
16270 need_larger_matrices:
16272 finish_scroll_bars:
16274 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16276 /* Set the thumb's position and size. */
16277 set_vertical_scroll_bar (w);
16279 /* Note that we actually used the scroll bar attached to this
16280 window, so it shouldn't be deleted at the end of redisplay. */
16281 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16282 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16285 /* Restore current_buffer and value of point in it. The window
16286 update may have changed the buffer, so first make sure `opoint'
16287 is still valid (Bug#6177). */
16288 if (CHARPOS (opoint) < BEGV)
16289 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16290 else if (CHARPOS (opoint) > ZV)
16291 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16292 else
16293 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16295 set_buffer_internal_1 (old);
16296 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16297 shorter. This can be caused by log truncation in *Messages*. */
16298 if (CHARPOS (lpoint) <= ZV)
16299 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16301 unbind_to (count, Qnil);
16305 /* Build the complete desired matrix of WINDOW with a window start
16306 buffer position POS.
16308 Value is 1 if successful. It is zero if fonts were loaded during
16309 redisplay which makes re-adjusting glyph matrices necessary, and -1
16310 if point would appear in the scroll margins.
16311 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16312 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16313 set in FLAGS.) */
16316 try_window (Lisp_Object window, struct text_pos pos, int flags)
16318 struct window *w = XWINDOW (window);
16319 struct it it;
16320 struct glyph_row *last_text_row = NULL;
16321 struct frame *f = XFRAME (w->frame);
16322 int frame_line_height = default_line_pixel_height (w);
16324 /* Make POS the new window start. */
16325 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16327 /* Mark cursor position as unknown. No overlay arrow seen. */
16328 w->cursor.vpos = -1;
16329 overlay_arrow_seen = 0;
16331 /* Initialize iterator and info to start at POS. */
16332 start_display (&it, w, pos);
16336 /* Display all lines of W. */
16337 while (it.current_y < it.last_visible_y)
16339 if (display_line (&it))
16340 last_text_row = it.glyph_row - 1;
16341 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16342 return 0;
16344 #ifdef HAVE_XWIDGETS_xxx
16345 //currently this is needed to detect xwidget movement reliably. or probably not.
16346 printf("try_window\n");
16347 return 0;
16348 #endif
16350 /* Don't let the cursor end in the scroll margins. */
16351 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16352 && !MINI_WINDOW_P (w))
16354 int this_scroll_margin;
16355 int window_total_lines
16356 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16358 if (scroll_margin > 0)
16360 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16361 this_scroll_margin *= frame_line_height;
16363 else
16364 this_scroll_margin = 0;
16366 if ((w->cursor.y >= 0 /* not vscrolled */
16367 && w->cursor.y < this_scroll_margin
16368 && CHARPOS (pos) > BEGV
16369 && IT_CHARPOS (it) < ZV)
16370 /* rms: considering make_cursor_line_fully_visible_p here
16371 seems to give wrong results. We don't want to recenter
16372 when the last line is partly visible, we want to allow
16373 that case to be handled in the usual way. */
16374 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16376 w->cursor.vpos = -1;
16377 clear_glyph_matrix (w->desired_matrix);
16378 return -1;
16382 /* If bottom moved off end of frame, change mode line percentage. */
16383 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16384 w->update_mode_line = 1;
16386 /* Set window_end_pos to the offset of the last character displayed
16387 on the window from the end of current_buffer. Set
16388 window_end_vpos to its row number. */
16389 if (last_text_row)
16391 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16392 adjust_window_ends (w, last_text_row, 0);
16393 eassert
16394 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16395 w->window_end_vpos)));
16397 else
16399 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16400 w->window_end_pos = Z - ZV;
16401 w->window_end_vpos = 0;
16404 /* But that is not valid info until redisplay finishes. */
16405 w->window_end_valid = 0;
16406 return 1;
16411 /************************************************************************
16412 Window redisplay reusing current matrix when buffer has not changed
16413 ************************************************************************/
16415 /* Try redisplay of window W showing an unchanged buffer with a
16416 different window start than the last time it was displayed by
16417 reusing its current matrix. Value is non-zero if successful.
16418 W->start is the new window start. */
16420 static int
16421 try_window_reusing_current_matrix (struct window *w)
16423 struct frame *f = XFRAME (w->frame);
16424 struct glyph_row *bottom_row;
16425 struct it it;
16426 struct run run;
16427 struct text_pos start, new_start;
16428 int nrows_scrolled, i;
16429 struct glyph_row *last_text_row;
16430 struct glyph_row *last_reused_text_row;
16431 struct glyph_row *start_row;
16432 int start_vpos, min_y, max_y;
16434 #ifdef GLYPH_DEBUG
16435 if (inhibit_try_window_reusing)
16436 return 0;
16437 #endif
16439 #ifdef HAVE_XWIDGETS_xxx
16440 //currently this is needed to detect xwidget movement reliably. or probably not.
16441 printf("try_window_reusing_current_matrix\n");
16442 return 0;
16443 #endif
16446 if (/* This function doesn't handle terminal frames. */
16447 !FRAME_WINDOW_P (f)
16448 /* Don't try to reuse the display if windows have been split
16449 or such. */
16450 || windows_or_buffers_changed
16451 || f->cursor_type_changed)
16452 return 0;
16454 /* Can't do this if region may have changed. */
16455 if (markpos_of_region () >= 0
16456 || w->region_showing
16457 || !NILP (Vshow_trailing_whitespace))
16458 return 0;
16460 /* If top-line visibility has changed, give up. */
16461 if (WINDOW_WANTS_HEADER_LINE_P (w)
16462 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16463 return 0;
16465 /* Give up if old or new display is scrolled vertically. We could
16466 make this function handle this, but right now it doesn't. */
16467 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16468 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16469 return 0;
16471 /* The variable new_start now holds the new window start. The old
16472 start `start' can be determined from the current matrix. */
16473 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16474 start = start_row->minpos;
16475 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16477 /* Clear the desired matrix for the display below. */
16478 clear_glyph_matrix (w->desired_matrix);
16480 if (CHARPOS (new_start) <= CHARPOS (start))
16482 /* Don't use this method if the display starts with an ellipsis
16483 displayed for invisible text. It's not easy to handle that case
16484 below, and it's certainly not worth the effort since this is
16485 not a frequent case. */
16486 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16487 return 0;
16489 IF_DEBUG (debug_method_add (w, "twu1"));
16491 /* Display up to a row that can be reused. The variable
16492 last_text_row is set to the last row displayed that displays
16493 text. Note that it.vpos == 0 if or if not there is a
16494 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16495 start_display (&it, w, new_start);
16496 w->cursor.vpos = -1;
16497 last_text_row = last_reused_text_row = NULL;
16499 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16501 /* If we have reached into the characters in the START row,
16502 that means the line boundaries have changed. So we
16503 can't start copying with the row START. Maybe it will
16504 work to start copying with the following row. */
16505 while (IT_CHARPOS (it) > CHARPOS (start))
16507 /* Advance to the next row as the "start". */
16508 start_row++;
16509 start = start_row->minpos;
16510 /* If there are no more rows to try, or just one, give up. */
16511 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16512 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16513 || CHARPOS (start) == ZV)
16515 clear_glyph_matrix (w->desired_matrix);
16516 return 0;
16519 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16521 /* If we have reached alignment, we can copy the rest of the
16522 rows. */
16523 if (IT_CHARPOS (it) == CHARPOS (start)
16524 /* Don't accept "alignment" inside a display vector,
16525 since start_row could have started in the middle of
16526 that same display vector (thus their character
16527 positions match), and we have no way of telling if
16528 that is the case. */
16529 && it.current.dpvec_index < 0)
16530 break;
16532 if (display_line (&it))
16533 last_text_row = it.glyph_row - 1;
16537 /* A value of current_y < last_visible_y means that we stopped
16538 at the previous window start, which in turn means that we
16539 have at least one reusable row. */
16540 if (it.current_y < it.last_visible_y)
16542 struct glyph_row *row;
16544 /* IT.vpos always starts from 0; it counts text lines. */
16545 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16547 /* Find PT if not already found in the lines displayed. */
16548 if (w->cursor.vpos < 0)
16550 int dy = it.current_y - start_row->y;
16552 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16553 row = row_containing_pos (w, PT, row, NULL, dy);
16554 if (row)
16555 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16556 dy, nrows_scrolled);
16557 else
16559 clear_glyph_matrix (w->desired_matrix);
16560 return 0;
16564 /* Scroll the display. Do it before the current matrix is
16565 changed. The problem here is that update has not yet
16566 run, i.e. part of the current matrix is not up to date.
16567 scroll_run_hook will clear the cursor, and use the
16568 current matrix to get the height of the row the cursor is
16569 in. */
16570 run.current_y = start_row->y;
16571 run.desired_y = it.current_y;
16572 run.height = it.last_visible_y - it.current_y;
16574 if (run.height > 0 && run.current_y != run.desired_y)
16576 update_begin (f);
16577 FRAME_RIF (f)->update_window_begin_hook (w);
16578 FRAME_RIF (f)->clear_window_mouse_face (w);
16579 FRAME_RIF (f)->scroll_run_hook (w, &run);
16580 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16581 update_end (f);
16584 /* Shift current matrix down by nrows_scrolled lines. */
16585 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16586 rotate_matrix (w->current_matrix,
16587 start_vpos,
16588 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16589 nrows_scrolled);
16591 /* Disable lines that must be updated. */
16592 for (i = 0; i < nrows_scrolled; ++i)
16593 (start_row + i)->enabled_p = 0;
16595 /* Re-compute Y positions. */
16596 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16597 max_y = it.last_visible_y;
16598 for (row = start_row + nrows_scrolled;
16599 row < bottom_row;
16600 ++row)
16602 row->y = it.current_y;
16603 row->visible_height = row->height;
16605 if (row->y < min_y)
16606 row->visible_height -= min_y - row->y;
16607 if (row->y + row->height > max_y)
16608 row->visible_height -= row->y + row->height - max_y;
16609 if (row->fringe_bitmap_periodic_p)
16610 row->redraw_fringe_bitmaps_p = 1;
16612 it.current_y += row->height;
16614 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16615 last_reused_text_row = row;
16616 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16617 break;
16620 /* Disable lines in the current matrix which are now
16621 below the window. */
16622 for (++row; row < bottom_row; ++row)
16623 row->enabled_p = row->mode_line_p = 0;
16626 /* Update window_end_pos etc.; last_reused_text_row is the last
16627 reused row from the current matrix containing text, if any.
16628 The value of last_text_row is the last displayed line
16629 containing text. */
16630 if (last_reused_text_row)
16631 adjust_window_ends (w, last_reused_text_row, 1);
16632 else if (last_text_row)
16633 adjust_window_ends (w, last_text_row, 0);
16634 else
16636 /* This window must be completely empty. */
16637 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16638 w->window_end_pos = Z - ZV;
16639 w->window_end_vpos = 0;
16641 w->window_end_valid = 0;
16643 /* Update hint: don't try scrolling again in update_window. */
16644 w->desired_matrix->no_scrolling_p = 1;
16646 #ifdef GLYPH_DEBUG
16647 debug_method_add (w, "try_window_reusing_current_matrix 1");
16648 #endif
16649 return 1;
16651 else if (CHARPOS (new_start) > CHARPOS (start))
16653 struct glyph_row *pt_row, *row;
16654 struct glyph_row *first_reusable_row;
16655 struct glyph_row *first_row_to_display;
16656 int dy;
16657 int yb = window_text_bottom_y (w);
16659 /* Find the row starting at new_start, if there is one. Don't
16660 reuse a partially visible line at the end. */
16661 first_reusable_row = start_row;
16662 while (first_reusable_row->enabled_p
16663 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16664 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16665 < CHARPOS (new_start)))
16666 ++first_reusable_row;
16668 /* Give up if there is no row to reuse. */
16669 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16670 || !first_reusable_row->enabled_p
16671 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16672 != CHARPOS (new_start)))
16673 return 0;
16675 /* We can reuse fully visible rows beginning with
16676 first_reusable_row to the end of the window. Set
16677 first_row_to_display to the first row that cannot be reused.
16678 Set pt_row to the row containing point, if there is any. */
16679 pt_row = NULL;
16680 for (first_row_to_display = first_reusable_row;
16681 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16682 ++first_row_to_display)
16684 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16685 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16686 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16687 && first_row_to_display->ends_at_zv_p
16688 && pt_row == NULL)))
16689 pt_row = first_row_to_display;
16692 /* Start displaying at the start of first_row_to_display. */
16693 eassert (first_row_to_display->y < yb);
16694 init_to_row_start (&it, w, first_row_to_display);
16696 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16697 - start_vpos);
16698 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16699 - nrows_scrolled);
16700 it.current_y = (first_row_to_display->y - first_reusable_row->y
16701 + WINDOW_HEADER_LINE_HEIGHT (w));
16703 /* Display lines beginning with first_row_to_display in the
16704 desired matrix. Set last_text_row to the last row displayed
16705 that displays text. */
16706 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16707 if (pt_row == NULL)
16708 w->cursor.vpos = -1;
16709 last_text_row = NULL;
16710 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16711 if (display_line (&it))
16712 last_text_row = it.glyph_row - 1;
16714 /* If point is in a reused row, adjust y and vpos of the cursor
16715 position. */
16716 if (pt_row)
16718 w->cursor.vpos -= nrows_scrolled;
16719 w->cursor.y -= first_reusable_row->y - start_row->y;
16722 /* Give up if point isn't in a row displayed or reused. (This
16723 also handles the case where w->cursor.vpos < nrows_scrolled
16724 after the calls to display_line, which can happen with scroll
16725 margins. See bug#1295.) */
16726 if (w->cursor.vpos < 0)
16728 clear_glyph_matrix (w->desired_matrix);
16729 return 0;
16732 /* Scroll the display. */
16733 run.current_y = first_reusable_row->y;
16734 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16735 run.height = it.last_visible_y - run.current_y;
16736 dy = run.current_y - run.desired_y;
16738 if (run.height)
16740 update_begin (f);
16741 FRAME_RIF (f)->update_window_begin_hook (w);
16742 FRAME_RIF (f)->clear_window_mouse_face (w);
16743 FRAME_RIF (f)->scroll_run_hook (w, &run);
16744 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16745 update_end (f);
16748 /* Adjust Y positions of reused rows. */
16749 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16750 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16751 max_y = it.last_visible_y;
16752 for (row = first_reusable_row; row < first_row_to_display; ++row)
16754 row->y -= dy;
16755 row->visible_height = row->height;
16756 if (row->y < min_y)
16757 row->visible_height -= min_y - row->y;
16758 if (row->y + row->height > max_y)
16759 row->visible_height -= row->y + row->height - max_y;
16760 if (row->fringe_bitmap_periodic_p)
16761 row->redraw_fringe_bitmaps_p = 1;
16764 /* Scroll the current matrix. */
16765 eassert (nrows_scrolled > 0);
16766 rotate_matrix (w->current_matrix,
16767 start_vpos,
16768 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16769 -nrows_scrolled);
16771 /* Disable rows not reused. */
16772 for (row -= nrows_scrolled; row < bottom_row; ++row)
16773 row->enabled_p = 0;
16775 /* Point may have moved to a different line, so we cannot assume that
16776 the previous cursor position is valid; locate the correct row. */
16777 if (pt_row)
16779 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16780 row < bottom_row
16781 && PT >= MATRIX_ROW_END_CHARPOS (row)
16782 && !row->ends_at_zv_p;
16783 row++)
16785 w->cursor.vpos++;
16786 w->cursor.y = row->y;
16788 if (row < bottom_row)
16790 /* Can't simply scan the row for point with
16791 bidi-reordered glyph rows. Let set_cursor_from_row
16792 figure out where to put the cursor, and if it fails,
16793 give up. */
16794 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16796 if (!set_cursor_from_row (w, row, w->current_matrix,
16797 0, 0, 0, 0))
16799 clear_glyph_matrix (w->desired_matrix);
16800 return 0;
16803 else
16805 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16806 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16808 for (; glyph < end
16809 && (!BUFFERP (glyph->object)
16810 || glyph->charpos < PT);
16811 glyph++)
16813 w->cursor.hpos++;
16814 w->cursor.x += glyph->pixel_width;
16820 /* Adjust window end. A null value of last_text_row means that
16821 the window end is in reused rows which in turn means that
16822 only its vpos can have changed. */
16823 if (last_text_row)
16824 adjust_window_ends (w, last_text_row, 0);
16825 else
16826 w->window_end_vpos -= nrows_scrolled;
16828 w->window_end_valid = 0;
16829 w->desired_matrix->no_scrolling_p = 1;
16831 #ifdef GLYPH_DEBUG
16832 debug_method_add (w, "try_window_reusing_current_matrix 2");
16833 #endif
16834 return 1;
16837 return 0;
16842 /************************************************************************
16843 Window redisplay reusing current matrix when buffer has changed
16844 ************************************************************************/
16846 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16847 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16848 ptrdiff_t *, ptrdiff_t *);
16849 static struct glyph_row *
16850 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16851 struct glyph_row *);
16854 /* Return the last row in MATRIX displaying text. If row START is
16855 non-null, start searching with that row. IT gives the dimensions
16856 of the display. Value is null if matrix is empty; otherwise it is
16857 a pointer to the row found. */
16859 static struct glyph_row *
16860 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16861 struct glyph_row *start)
16863 struct glyph_row *row, *row_found;
16865 /* Set row_found to the last row in IT->w's current matrix
16866 displaying text. The loop looks funny but think of partially
16867 visible lines. */
16868 row_found = NULL;
16869 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16870 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16872 eassert (row->enabled_p);
16873 row_found = row;
16874 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16875 break;
16876 ++row;
16879 return row_found;
16883 /* Return the last row in the current matrix of W that is not affected
16884 by changes at the start of current_buffer that occurred since W's
16885 current matrix was built. Value is null if no such row exists.
16887 BEG_UNCHANGED us the number of characters unchanged at the start of
16888 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16889 first changed character in current_buffer. Characters at positions <
16890 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16891 when the current matrix was built. */
16893 static struct glyph_row *
16894 find_last_unchanged_at_beg_row (struct window *w)
16896 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16897 struct glyph_row *row;
16898 struct glyph_row *row_found = NULL;
16899 int yb = window_text_bottom_y (w);
16901 /* Find the last row displaying unchanged text. */
16902 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16903 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16904 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16905 ++row)
16907 if (/* If row ends before first_changed_pos, it is unchanged,
16908 except in some case. */
16909 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16910 /* When row ends in ZV and we write at ZV it is not
16911 unchanged. */
16912 && !row->ends_at_zv_p
16913 /* When first_changed_pos is the end of a continued line,
16914 row is not unchanged because it may be no longer
16915 continued. */
16916 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16917 && (row->continued_p
16918 || row->exact_window_width_line_p))
16919 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16920 needs to be recomputed, so don't consider this row as
16921 unchanged. This happens when the last line was
16922 bidi-reordered and was killed immediately before this
16923 redisplay cycle. In that case, ROW->end stores the
16924 buffer position of the first visual-order character of
16925 the killed text, which is now beyond ZV. */
16926 && CHARPOS (row->end.pos) <= ZV)
16927 row_found = row;
16929 /* Stop if last visible row. */
16930 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16931 break;
16934 return row_found;
16938 /* Find the first glyph row in the current matrix of W that is not
16939 affected by changes at the end of current_buffer since the
16940 time W's current matrix was built.
16942 Return in *DELTA the number of chars by which buffer positions in
16943 unchanged text at the end of current_buffer must be adjusted.
16945 Return in *DELTA_BYTES the corresponding number of bytes.
16947 Value is null if no such row exists, i.e. all rows are affected by
16948 changes. */
16950 static struct glyph_row *
16951 find_first_unchanged_at_end_row (struct window *w,
16952 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16954 struct glyph_row *row;
16955 struct glyph_row *row_found = NULL;
16957 *delta = *delta_bytes = 0;
16959 /* Display must not have been paused, otherwise the current matrix
16960 is not up to date. */
16961 eassert (w->window_end_valid);
16963 /* A value of window_end_pos >= END_UNCHANGED means that the window
16964 end is in the range of changed text. If so, there is no
16965 unchanged row at the end of W's current matrix. */
16966 if (w->window_end_pos >= END_UNCHANGED)
16967 return NULL;
16969 /* Set row to the last row in W's current matrix displaying text. */
16970 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16972 /* If matrix is entirely empty, no unchanged row exists. */
16973 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16975 /* The value of row is the last glyph row in the matrix having a
16976 meaningful buffer position in it. The end position of row
16977 corresponds to window_end_pos. This allows us to translate
16978 buffer positions in the current matrix to current buffer
16979 positions for characters not in changed text. */
16980 ptrdiff_t Z_old =
16981 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16982 ptrdiff_t Z_BYTE_old =
16983 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16984 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16985 struct glyph_row *first_text_row
16986 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16988 *delta = Z - Z_old;
16989 *delta_bytes = Z_BYTE - Z_BYTE_old;
16991 /* Set last_unchanged_pos to the buffer position of the last
16992 character in the buffer that has not been changed. Z is the
16993 index + 1 of the last character in current_buffer, i.e. by
16994 subtracting END_UNCHANGED we get the index of the last
16995 unchanged character, and we have to add BEG to get its buffer
16996 position. */
16997 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16998 last_unchanged_pos_old = last_unchanged_pos - *delta;
17000 /* Search backward from ROW for a row displaying a line that
17001 starts at a minimum position >= last_unchanged_pos_old. */
17002 for (; row > first_text_row; --row)
17004 /* This used to abort, but it can happen.
17005 It is ok to just stop the search instead here. KFS. */
17006 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17007 break;
17009 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17010 row_found = row;
17014 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17016 return row_found;
17020 /* Make sure that glyph rows in the current matrix of window W
17021 reference the same glyph memory as corresponding rows in the
17022 frame's frame matrix. This function is called after scrolling W's
17023 current matrix on a terminal frame in try_window_id and
17024 try_window_reusing_current_matrix. */
17026 static void
17027 sync_frame_with_window_matrix_rows (struct window *w)
17029 struct frame *f = XFRAME (w->frame);
17030 struct glyph_row *window_row, *window_row_end, *frame_row;
17032 /* Preconditions: W must be a leaf window and full-width. Its frame
17033 must have a frame matrix. */
17034 eassert (BUFFERP (w->contents));
17035 eassert (WINDOW_FULL_WIDTH_P (w));
17036 eassert (!FRAME_WINDOW_P (f));
17038 /* If W is a full-width window, glyph pointers in W's current matrix
17039 have, by definition, to be the same as glyph pointers in the
17040 corresponding frame matrix. Note that frame matrices have no
17041 marginal areas (see build_frame_matrix). */
17042 window_row = w->current_matrix->rows;
17043 window_row_end = window_row + w->current_matrix->nrows;
17044 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17045 while (window_row < window_row_end)
17047 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17048 struct glyph *end = window_row->glyphs[LAST_AREA];
17050 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17051 frame_row->glyphs[TEXT_AREA] = start;
17052 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17053 frame_row->glyphs[LAST_AREA] = end;
17055 /* Disable frame rows whose corresponding window rows have
17056 been disabled in try_window_id. */
17057 if (!window_row->enabled_p)
17058 frame_row->enabled_p = 0;
17060 ++window_row, ++frame_row;
17065 /* Find the glyph row in window W containing CHARPOS. Consider all
17066 rows between START and END (not inclusive). END null means search
17067 all rows to the end of the display area of W. Value is the row
17068 containing CHARPOS or null. */
17070 struct glyph_row *
17071 row_containing_pos (struct window *w, ptrdiff_t charpos,
17072 struct glyph_row *start, struct glyph_row *end, int dy)
17074 struct glyph_row *row = start;
17075 struct glyph_row *best_row = NULL;
17076 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17077 int last_y;
17079 /* If we happen to start on a header-line, skip that. */
17080 if (row->mode_line_p)
17081 ++row;
17083 if ((end && row >= end) || !row->enabled_p)
17084 return NULL;
17086 last_y = window_text_bottom_y (w) - dy;
17088 while (1)
17090 /* Give up if we have gone too far. */
17091 if (end && row >= end)
17092 return NULL;
17093 /* This formerly returned if they were equal.
17094 I think that both quantities are of a "last plus one" type;
17095 if so, when they are equal, the row is within the screen. -- rms. */
17096 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17097 return NULL;
17099 /* If it is in this row, return this row. */
17100 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17101 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17102 /* The end position of a row equals the start
17103 position of the next row. If CHARPOS is there, we
17104 would rather consider it displayed in the next
17105 line, except when this line ends in ZV. */
17106 && !row_for_charpos_p (row, charpos)))
17107 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17109 struct glyph *g;
17111 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17112 || (!best_row && !row->continued_p))
17113 return row;
17114 /* In bidi-reordered rows, there could be several rows whose
17115 edges surround CHARPOS, all of these rows belonging to
17116 the same continued line. We need to find the row which
17117 fits CHARPOS the best. */
17118 for (g = row->glyphs[TEXT_AREA];
17119 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17120 g++)
17122 if (!STRINGP (g->object))
17124 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17126 mindif = eabs (g->charpos - charpos);
17127 best_row = row;
17128 /* Exact match always wins. */
17129 if (mindif == 0)
17130 return best_row;
17135 else if (best_row && !row->continued_p)
17136 return best_row;
17137 ++row;
17142 /* Try to redisplay window W by reusing its existing display. W's
17143 current matrix must be up to date when this function is called,
17144 i.e. window_end_valid must be nonzero.
17146 Value is
17148 1 if display has been updated
17149 0 if otherwise unsuccessful
17150 -1 if redisplay with same window start is known not to succeed
17152 The following steps are performed:
17154 1. Find the last row in the current matrix of W that is not
17155 affected by changes at the start of current_buffer. If no such row
17156 is found, give up.
17158 2. Find the first row in W's current matrix that is not affected by
17159 changes at the end of current_buffer. Maybe there is no such row.
17161 3. Display lines beginning with the row + 1 found in step 1 to the
17162 row found in step 2 or, if step 2 didn't find a row, to the end of
17163 the window.
17165 4. If cursor is not known to appear on the window, give up.
17167 5. If display stopped at the row found in step 2, scroll the
17168 display and current matrix as needed.
17170 6. Maybe display some lines at the end of W, if we must. This can
17171 happen under various circumstances, like a partially visible line
17172 becoming fully visible, or because newly displayed lines are displayed
17173 in smaller font sizes.
17175 7. Update W's window end information. */
17177 static int
17178 try_window_id (struct window *w)
17180 struct frame *f = XFRAME (w->frame);
17181 struct glyph_matrix *current_matrix = w->current_matrix;
17182 struct glyph_matrix *desired_matrix = w->desired_matrix;
17183 struct glyph_row *last_unchanged_at_beg_row;
17184 struct glyph_row *first_unchanged_at_end_row;
17185 struct glyph_row *row;
17186 struct glyph_row *bottom_row;
17187 int bottom_vpos;
17188 struct it it;
17189 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17190 int dvpos, dy;
17191 struct text_pos start_pos;
17192 struct run run;
17193 int first_unchanged_at_end_vpos = 0;
17194 struct glyph_row *last_text_row, *last_text_row_at_end;
17195 struct text_pos start;
17196 ptrdiff_t first_changed_charpos, last_changed_charpos;
17198 #ifdef GLYPH_DEBUG
17199 if (inhibit_try_window_id)
17200 return 0;
17201 #endif
17203 #ifdef HAVE_XWIDGETS_xxx
17204 //maybe needed for proper xwidget movement
17205 printf("try_window_id\n");
17206 return -1;
17207 #endif
17210 /* This is handy for debugging. */
17211 #if 0
17212 #define GIVE_UP(X) \
17213 do { \
17214 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17215 return 0; \
17216 } while (0)
17217 #else
17218 #define GIVE_UP(X) return 0
17219 #endif
17221 SET_TEXT_POS_FROM_MARKER (start, w->start);
17223 /* Don't use this for mini-windows because these can show
17224 messages and mini-buffers, and we don't handle that here. */
17225 if (MINI_WINDOW_P (w))
17226 GIVE_UP (1);
17228 /* This flag is used to prevent redisplay optimizations. */
17229 if (windows_or_buffers_changed || f->cursor_type_changed)
17230 GIVE_UP (2);
17232 /* Verify that narrowing has not changed.
17233 Also verify that we were not told to prevent redisplay optimizations.
17234 It would be nice to further
17235 reduce the number of cases where this prevents try_window_id. */
17236 if (current_buffer->clip_changed
17237 || current_buffer->prevent_redisplay_optimizations_p)
17238 GIVE_UP (3);
17240 /* Window must either use window-based redisplay or be full width. */
17241 if (!FRAME_WINDOW_P (f)
17242 && (!FRAME_LINE_INS_DEL_OK (f)
17243 || !WINDOW_FULL_WIDTH_P (w)))
17244 GIVE_UP (4);
17246 /* Give up if point is known NOT to appear in W. */
17247 if (PT < CHARPOS (start))
17248 GIVE_UP (5);
17250 /* Another way to prevent redisplay optimizations. */
17251 if (w->last_modified == 0)
17252 GIVE_UP (6);
17254 /* Verify that window is not hscrolled. */
17255 if (w->hscroll != 0)
17256 GIVE_UP (7);
17258 /* Verify that display wasn't paused. */
17259 if (!w->window_end_valid)
17260 GIVE_UP (8);
17262 /* Can't use this if highlighting a region because a cursor movement
17263 will do more than just set the cursor. */
17264 if (markpos_of_region () >= 0)
17265 GIVE_UP (9);
17267 /* Likewise if highlighting trailing whitespace. */
17268 if (!NILP (Vshow_trailing_whitespace))
17269 GIVE_UP (11);
17271 /* Likewise if showing a region. */
17272 if (w->region_showing)
17273 GIVE_UP (10);
17275 /* Can't use this if overlay arrow position and/or string have
17276 changed. */
17277 if (overlay_arrows_changed_p ())
17278 GIVE_UP (12);
17280 /* When word-wrap is on, adding a space to the first word of a
17281 wrapped line can change the wrap position, altering the line
17282 above it. It might be worthwhile to handle this more
17283 intelligently, but for now just redisplay from scratch. */
17284 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17285 GIVE_UP (21);
17287 /* Under bidi reordering, adding or deleting a character in the
17288 beginning of a paragraph, before the first strong directional
17289 character, can change the base direction of the paragraph (unless
17290 the buffer specifies a fixed paragraph direction), which will
17291 require to redisplay the whole paragraph. It might be worthwhile
17292 to find the paragraph limits and widen the range of redisplayed
17293 lines to that, but for now just give up this optimization and
17294 redisplay from scratch. */
17295 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17296 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17297 GIVE_UP (22);
17299 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17300 only if buffer has really changed. The reason is that the gap is
17301 initially at Z for freshly visited files. The code below would
17302 set end_unchanged to 0 in that case. */
17303 if (MODIFF > SAVE_MODIFF
17304 /* This seems to happen sometimes after saving a buffer. */
17305 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17307 if (GPT - BEG < BEG_UNCHANGED)
17308 BEG_UNCHANGED = GPT - BEG;
17309 if (Z - GPT < END_UNCHANGED)
17310 END_UNCHANGED = Z - GPT;
17313 /* The position of the first and last character that has been changed. */
17314 first_changed_charpos = BEG + BEG_UNCHANGED;
17315 last_changed_charpos = Z - END_UNCHANGED;
17317 /* If window starts after a line end, and the last change is in
17318 front of that newline, then changes don't affect the display.
17319 This case happens with stealth-fontification. Note that although
17320 the display is unchanged, glyph positions in the matrix have to
17321 be adjusted, of course. */
17322 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17323 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17324 && ((last_changed_charpos < CHARPOS (start)
17325 && CHARPOS (start) == BEGV)
17326 || (last_changed_charpos < CHARPOS (start) - 1
17327 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17329 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17330 struct glyph_row *r0;
17332 /* Compute how many chars/bytes have been added to or removed
17333 from the buffer. */
17334 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17335 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17336 Z_delta = Z - Z_old;
17337 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17339 /* Give up if PT is not in the window. Note that it already has
17340 been checked at the start of try_window_id that PT is not in
17341 front of the window start. */
17342 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17343 GIVE_UP (13);
17345 /* If window start is unchanged, we can reuse the whole matrix
17346 as is, after adjusting glyph positions. No need to compute
17347 the window end again, since its offset from Z hasn't changed. */
17348 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17349 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17350 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17351 /* PT must not be in a partially visible line. */
17352 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17353 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17355 /* Adjust positions in the glyph matrix. */
17356 if (Z_delta || Z_delta_bytes)
17358 struct glyph_row *r1
17359 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17360 increment_matrix_positions (w->current_matrix,
17361 MATRIX_ROW_VPOS (r0, current_matrix),
17362 MATRIX_ROW_VPOS (r1, current_matrix),
17363 Z_delta, Z_delta_bytes);
17366 /* Set the cursor. */
17367 row = row_containing_pos (w, PT, r0, NULL, 0);
17368 if (row)
17369 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17370 else
17371 emacs_abort ();
17372 return 1;
17376 /* Handle the case that changes are all below what is displayed in
17377 the window, and that PT is in the window. This shortcut cannot
17378 be taken if ZV is visible in the window, and text has been added
17379 there that is visible in the window. */
17380 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17381 /* ZV is not visible in the window, or there are no
17382 changes at ZV, actually. */
17383 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17384 || first_changed_charpos == last_changed_charpos))
17386 struct glyph_row *r0;
17388 /* Give up if PT is not in the window. Note that it already has
17389 been checked at the start of try_window_id that PT is not in
17390 front of the window start. */
17391 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17392 GIVE_UP (14);
17394 /* If window start is unchanged, we can reuse the whole matrix
17395 as is, without changing glyph positions since no text has
17396 been added/removed in front of the window end. */
17397 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17398 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17399 /* PT must not be in a partially visible line. */
17400 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17401 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17403 /* We have to compute the window end anew since text
17404 could have been added/removed after it. */
17405 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17406 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17408 /* Set the cursor. */
17409 row = row_containing_pos (w, PT, r0, NULL, 0);
17410 if (row)
17411 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17412 else
17413 emacs_abort ();
17414 return 2;
17418 /* Give up if window start is in the changed area.
17420 The condition used to read
17422 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17424 but why that was tested escapes me at the moment. */
17425 if (CHARPOS (start) >= first_changed_charpos
17426 && CHARPOS (start) <= last_changed_charpos)
17427 GIVE_UP (15);
17429 /* Check that window start agrees with the start of the first glyph
17430 row in its current matrix. Check this after we know the window
17431 start is not in changed text, otherwise positions would not be
17432 comparable. */
17433 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17434 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17435 GIVE_UP (16);
17437 /* Give up if the window ends in strings. Overlay strings
17438 at the end are difficult to handle, so don't try. */
17439 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17440 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17441 GIVE_UP (20);
17443 /* Compute the position at which we have to start displaying new
17444 lines. Some of the lines at the top of the window might be
17445 reusable because they are not displaying changed text. Find the
17446 last row in W's current matrix not affected by changes at the
17447 start of current_buffer. Value is null if changes start in the
17448 first line of window. */
17449 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17450 if (last_unchanged_at_beg_row)
17452 /* Avoid starting to display in the middle of a character, a TAB
17453 for instance. This is easier than to set up the iterator
17454 exactly, and it's not a frequent case, so the additional
17455 effort wouldn't really pay off. */
17456 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17457 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17458 && last_unchanged_at_beg_row > w->current_matrix->rows)
17459 --last_unchanged_at_beg_row;
17461 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17462 GIVE_UP (17);
17464 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17465 GIVE_UP (18);
17466 start_pos = it.current.pos;
17468 /* Start displaying new lines in the desired matrix at the same
17469 vpos we would use in the current matrix, i.e. below
17470 last_unchanged_at_beg_row. */
17471 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17472 current_matrix);
17473 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17474 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17476 eassert (it.hpos == 0 && it.current_x == 0);
17478 else
17480 /* There are no reusable lines at the start of the window.
17481 Start displaying in the first text line. */
17482 start_display (&it, w, start);
17483 it.vpos = it.first_vpos;
17484 start_pos = it.current.pos;
17487 /* Find the first row that is not affected by changes at the end of
17488 the buffer. Value will be null if there is no unchanged row, in
17489 which case we must redisplay to the end of the window. delta
17490 will be set to the value by which buffer positions beginning with
17491 first_unchanged_at_end_row have to be adjusted due to text
17492 changes. */
17493 first_unchanged_at_end_row
17494 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17495 IF_DEBUG (debug_delta = delta);
17496 IF_DEBUG (debug_delta_bytes = delta_bytes);
17498 /* Set stop_pos to the buffer position up to which we will have to
17499 display new lines. If first_unchanged_at_end_row != NULL, this
17500 is the buffer position of the start of the line displayed in that
17501 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17502 that we don't stop at a buffer position. */
17503 stop_pos = 0;
17504 if (first_unchanged_at_end_row)
17506 eassert (last_unchanged_at_beg_row == NULL
17507 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17509 /* If this is a continuation line, move forward to the next one
17510 that isn't. Changes in lines above affect this line.
17511 Caution: this may move first_unchanged_at_end_row to a row
17512 not displaying text. */
17513 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17514 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17515 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17516 < it.last_visible_y))
17517 ++first_unchanged_at_end_row;
17519 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17520 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17521 >= it.last_visible_y))
17522 first_unchanged_at_end_row = NULL;
17523 else
17525 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17526 + delta);
17527 first_unchanged_at_end_vpos
17528 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17529 eassert (stop_pos >= Z - END_UNCHANGED);
17532 else if (last_unchanged_at_beg_row == NULL)
17533 GIVE_UP (19);
17536 #ifdef GLYPH_DEBUG
17538 /* Either there is no unchanged row at the end, or the one we have
17539 now displays text. This is a necessary condition for the window
17540 end pos calculation at the end of this function. */
17541 eassert (first_unchanged_at_end_row == NULL
17542 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17544 debug_last_unchanged_at_beg_vpos
17545 = (last_unchanged_at_beg_row
17546 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17547 : -1);
17548 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17550 #endif /* GLYPH_DEBUG */
17553 /* Display new lines. Set last_text_row to the last new line
17554 displayed which has text on it, i.e. might end up as being the
17555 line where the window_end_vpos is. */
17556 w->cursor.vpos = -1;
17557 last_text_row = NULL;
17558 overlay_arrow_seen = 0;
17559 while (it.current_y < it.last_visible_y
17560 && !f->fonts_changed
17561 && (first_unchanged_at_end_row == NULL
17562 || IT_CHARPOS (it) < stop_pos))
17564 if (display_line (&it))
17565 last_text_row = it.glyph_row - 1;
17568 if (f->fonts_changed)
17569 return -1;
17572 /* Compute differences in buffer positions, y-positions etc. for
17573 lines reused at the bottom of the window. Compute what we can
17574 scroll. */
17575 if (first_unchanged_at_end_row
17576 /* No lines reused because we displayed everything up to the
17577 bottom of the window. */
17578 && it.current_y < it.last_visible_y)
17580 dvpos = (it.vpos
17581 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17582 current_matrix));
17583 dy = it.current_y - first_unchanged_at_end_row->y;
17584 run.current_y = first_unchanged_at_end_row->y;
17585 run.desired_y = run.current_y + dy;
17586 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17588 else
17590 delta = delta_bytes = dvpos = dy
17591 = run.current_y = run.desired_y = run.height = 0;
17592 first_unchanged_at_end_row = NULL;
17594 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17597 /* Find the cursor if not already found. We have to decide whether
17598 PT will appear on this window (it sometimes doesn't, but this is
17599 not a very frequent case.) This decision has to be made before
17600 the current matrix is altered. A value of cursor.vpos < 0 means
17601 that PT is either in one of the lines beginning at
17602 first_unchanged_at_end_row or below the window. Don't care for
17603 lines that might be displayed later at the window end; as
17604 mentioned, this is not a frequent case. */
17605 if (w->cursor.vpos < 0)
17607 /* Cursor in unchanged rows at the top? */
17608 if (PT < CHARPOS (start_pos)
17609 && last_unchanged_at_beg_row)
17611 row = row_containing_pos (w, PT,
17612 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17613 last_unchanged_at_beg_row + 1, 0);
17614 if (row)
17615 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17618 /* Start from first_unchanged_at_end_row looking for PT. */
17619 else if (first_unchanged_at_end_row)
17621 row = row_containing_pos (w, PT - delta,
17622 first_unchanged_at_end_row, NULL, 0);
17623 if (row)
17624 set_cursor_from_row (w, row, w->current_matrix, delta,
17625 delta_bytes, dy, dvpos);
17628 /* Give up if cursor was not found. */
17629 if (w->cursor.vpos < 0)
17631 clear_glyph_matrix (w->desired_matrix);
17632 return -1;
17636 /* Don't let the cursor end in the scroll margins. */
17638 int this_scroll_margin, cursor_height;
17639 int frame_line_height = default_line_pixel_height (w);
17640 int window_total_lines
17641 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17643 this_scroll_margin =
17644 max (0, min (scroll_margin, window_total_lines / 4));
17645 this_scroll_margin *= frame_line_height;
17646 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17648 if ((w->cursor.y < this_scroll_margin
17649 && CHARPOS (start) > BEGV)
17650 /* Old redisplay didn't take scroll margin into account at the bottom,
17651 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17652 || (w->cursor.y + (make_cursor_line_fully_visible_p
17653 ? cursor_height + this_scroll_margin
17654 : 1)) > it.last_visible_y)
17656 w->cursor.vpos = -1;
17657 clear_glyph_matrix (w->desired_matrix);
17658 return -1;
17662 /* Scroll the display. Do it before changing the current matrix so
17663 that xterm.c doesn't get confused about where the cursor glyph is
17664 found. */
17665 if (dy && run.height)
17667 update_begin (f);
17669 if (FRAME_WINDOW_P (f))
17671 FRAME_RIF (f)->update_window_begin_hook (w);
17672 FRAME_RIF (f)->clear_window_mouse_face (w);
17673 FRAME_RIF (f)->scroll_run_hook (w, &run);
17674 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17676 else
17678 /* Terminal frame. In this case, dvpos gives the number of
17679 lines to scroll by; dvpos < 0 means scroll up. */
17680 int from_vpos
17681 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17682 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17683 int end = (WINDOW_TOP_EDGE_LINE (w)
17684 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17685 + window_internal_height (w));
17687 #if defined (HAVE_GPM) || defined (MSDOS)
17688 x_clear_window_mouse_face (w);
17689 #endif
17690 /* Perform the operation on the screen. */
17691 if (dvpos > 0)
17693 /* Scroll last_unchanged_at_beg_row to the end of the
17694 window down dvpos lines. */
17695 set_terminal_window (f, end);
17697 /* On dumb terminals delete dvpos lines at the end
17698 before inserting dvpos empty lines. */
17699 if (!FRAME_SCROLL_REGION_OK (f))
17700 ins_del_lines (f, end - dvpos, -dvpos);
17702 /* Insert dvpos empty lines in front of
17703 last_unchanged_at_beg_row. */
17704 ins_del_lines (f, from, dvpos);
17706 else if (dvpos < 0)
17708 /* Scroll up last_unchanged_at_beg_vpos to the end of
17709 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17710 set_terminal_window (f, end);
17712 /* Delete dvpos lines in front of
17713 last_unchanged_at_beg_vpos. ins_del_lines will set
17714 the cursor to the given vpos and emit |dvpos| delete
17715 line sequences. */
17716 ins_del_lines (f, from + dvpos, dvpos);
17718 /* On a dumb terminal insert dvpos empty lines at the
17719 end. */
17720 if (!FRAME_SCROLL_REGION_OK (f))
17721 ins_del_lines (f, end + dvpos, -dvpos);
17724 set_terminal_window (f, 0);
17727 update_end (f);
17730 /* Shift reused rows of the current matrix to the right position.
17731 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17732 text. */
17733 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17734 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17735 if (dvpos < 0)
17737 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17738 bottom_vpos, dvpos);
17739 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17740 bottom_vpos);
17742 else if (dvpos > 0)
17744 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17745 bottom_vpos, dvpos);
17746 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17747 first_unchanged_at_end_vpos + dvpos);
17750 /* For frame-based redisplay, make sure that current frame and window
17751 matrix are in sync with respect to glyph memory. */
17752 if (!FRAME_WINDOW_P (f))
17753 sync_frame_with_window_matrix_rows (w);
17755 /* Adjust buffer positions in reused rows. */
17756 if (delta || delta_bytes)
17757 increment_matrix_positions (current_matrix,
17758 first_unchanged_at_end_vpos + dvpos,
17759 bottom_vpos, delta, delta_bytes);
17761 /* Adjust Y positions. */
17762 if (dy)
17763 shift_glyph_matrix (w, current_matrix,
17764 first_unchanged_at_end_vpos + dvpos,
17765 bottom_vpos, dy);
17767 if (first_unchanged_at_end_row)
17769 first_unchanged_at_end_row += dvpos;
17770 if (first_unchanged_at_end_row->y >= it.last_visible_y
17771 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17772 first_unchanged_at_end_row = NULL;
17775 /* If scrolling up, there may be some lines to display at the end of
17776 the window. */
17777 last_text_row_at_end = NULL;
17778 if (dy < 0)
17780 /* Scrolling up can leave for example a partially visible line
17781 at the end of the window to be redisplayed. */
17782 /* Set last_row to the glyph row in the current matrix where the
17783 window end line is found. It has been moved up or down in
17784 the matrix by dvpos. */
17785 int last_vpos = w->window_end_vpos + dvpos;
17786 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17788 /* If last_row is the window end line, it should display text. */
17789 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17791 /* If window end line was partially visible before, begin
17792 displaying at that line. Otherwise begin displaying with the
17793 line following it. */
17794 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17796 init_to_row_start (&it, w, last_row);
17797 it.vpos = last_vpos;
17798 it.current_y = last_row->y;
17800 else
17802 init_to_row_end (&it, w, last_row);
17803 it.vpos = 1 + last_vpos;
17804 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17805 ++last_row;
17808 /* We may start in a continuation line. If so, we have to
17809 get the right continuation_lines_width and current_x. */
17810 it.continuation_lines_width = last_row->continuation_lines_width;
17811 it.hpos = it.current_x = 0;
17813 /* Display the rest of the lines at the window end. */
17814 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17815 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17817 /* Is it always sure that the display agrees with lines in
17818 the current matrix? I don't think so, so we mark rows
17819 displayed invalid in the current matrix by setting their
17820 enabled_p flag to zero. */
17821 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17822 if (display_line (&it))
17823 last_text_row_at_end = it.glyph_row - 1;
17827 /* Update window_end_pos and window_end_vpos. */
17828 if (first_unchanged_at_end_row && !last_text_row_at_end)
17830 /* Window end line if one of the preserved rows from the current
17831 matrix. Set row to the last row displaying text in current
17832 matrix starting at first_unchanged_at_end_row, after
17833 scrolling. */
17834 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17835 row = find_last_row_displaying_text (w->current_matrix, &it,
17836 first_unchanged_at_end_row);
17837 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17838 adjust_window_ends (w, row, 1);
17839 eassert (w->window_end_bytepos >= 0);
17840 IF_DEBUG (debug_method_add (w, "A"));
17842 else if (last_text_row_at_end)
17844 adjust_window_ends (w, last_text_row_at_end, 0);
17845 eassert (w->window_end_bytepos >= 0);
17846 IF_DEBUG (debug_method_add (w, "B"));
17848 else if (last_text_row)
17850 /* We have displayed either to the end of the window or at the
17851 end of the window, i.e. the last row with text is to be found
17852 in the desired matrix. */
17853 adjust_window_ends (w, last_text_row, 0);
17854 eassert (w->window_end_bytepos >= 0);
17856 else if (first_unchanged_at_end_row == NULL
17857 && last_text_row == NULL
17858 && last_text_row_at_end == NULL)
17860 /* Displayed to end of window, but no line containing text was
17861 displayed. Lines were deleted at the end of the window. */
17862 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17863 int vpos = w->window_end_vpos;
17864 struct glyph_row *current_row = current_matrix->rows + vpos;
17865 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17867 for (row = NULL;
17868 row == NULL && vpos >= first_vpos;
17869 --vpos, --current_row, --desired_row)
17871 if (desired_row->enabled_p)
17873 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17874 row = desired_row;
17876 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17877 row = current_row;
17880 eassert (row != NULL);
17881 w->window_end_vpos = vpos + 1;
17882 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17883 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17884 eassert (w->window_end_bytepos >= 0);
17885 IF_DEBUG (debug_method_add (w, "C"));
17887 else
17888 emacs_abort ();
17890 IF_DEBUG (debug_end_pos = w->window_end_pos;
17891 debug_end_vpos = w->window_end_vpos);
17893 /* Record that display has not been completed. */
17894 w->window_end_valid = 0;
17895 w->desired_matrix->no_scrolling_p = 1;
17896 return 3;
17898 #undef GIVE_UP
17903 /***********************************************************************
17904 More debugging support
17905 ***********************************************************************/
17907 #ifdef GLYPH_DEBUG
17909 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17910 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17911 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17914 /* Dump the contents of glyph matrix MATRIX on stderr.
17916 GLYPHS 0 means don't show glyph contents.
17917 GLYPHS 1 means show glyphs in short form
17918 GLYPHS > 1 means show glyphs in long form. */
17920 void
17921 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17923 int i;
17924 for (i = 0; i < matrix->nrows; ++i)
17925 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17929 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17930 the glyph row and area where the glyph comes from. */
17932 void
17933 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17935 if (glyph->type == CHAR_GLYPH
17936 || glyph->type == GLYPHLESS_GLYPH)
17938 fprintf (stderr,
17939 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17940 glyph - row->glyphs[TEXT_AREA],
17941 (glyph->type == CHAR_GLYPH
17942 ? 'C'
17943 : 'G'),
17944 glyph->charpos,
17945 (BUFFERP (glyph->object)
17946 ? 'B'
17947 : (STRINGP (glyph->object)
17948 ? 'S'
17949 : (INTEGERP (glyph->object)
17950 ? '0'
17951 : '-'))),
17952 glyph->pixel_width,
17953 glyph->u.ch,
17954 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17955 ? glyph->u.ch
17956 : '.'),
17957 glyph->face_id,
17958 glyph->left_box_line_p,
17959 glyph->right_box_line_p);
17961 else if (glyph->type == STRETCH_GLYPH)
17963 fprintf (stderr,
17964 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17965 glyph - row->glyphs[TEXT_AREA],
17966 'S',
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,
17977 ' ',
17978 glyph->face_id,
17979 glyph->left_box_line_p,
17980 glyph->right_box_line_p);
17982 else if (glyph->type == IMAGE_GLYPH)
17984 fprintf (stderr,
17985 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17986 glyph - row->glyphs[TEXT_AREA],
17987 'I',
17988 glyph->charpos,
17989 (BUFFERP (glyph->object)
17990 ? 'B'
17991 : (STRINGP (glyph->object)
17992 ? 'S'
17993 : (INTEGERP (glyph->object)
17994 ? '0'
17995 : '-'))),
17996 glyph->pixel_width,
17997 glyph->u.img_id,
17998 '.',
17999 glyph->face_id,
18000 glyph->left_box_line_p,
18001 glyph->right_box_line_p);
18003 else if (glyph->type == COMPOSITE_GLYPH)
18005 fprintf (stderr,
18006 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18007 glyph - row->glyphs[TEXT_AREA],
18008 '+',
18009 glyph->charpos,
18010 (BUFFERP (glyph->object)
18011 ? 'B'
18012 : (STRINGP (glyph->object)
18013 ? 'S'
18014 : (INTEGERP (glyph->object)
18015 ? '0'
18016 : '-'))),
18017 glyph->pixel_width,
18018 glyph->u.cmp.id);
18019 if (glyph->u.cmp.automatic)
18020 fprintf (stderr,
18021 "[%d-%d]",
18022 glyph->slice.cmp.from, glyph->slice.cmp.to);
18023 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18024 glyph->face_id,
18025 glyph->left_box_line_p,
18026 glyph->right_box_line_p);
18028 #ifdef HAVE_XWIDGETS
18029 else if (glyph->type == XWIDGET_GLYPH)
18031 fprintf (stderr,
18032 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18033 glyph - row->glyphs[TEXT_AREA],
18034 'X',
18035 glyph->charpos,
18036 (BUFFERP (glyph->object)
18037 ? 'B'
18038 : (STRINGP (glyph->object)
18039 ? 'S'
18040 : '-')),
18041 glyph->pixel_width,
18042 glyph->u.xwidget,
18043 '.',
18044 glyph->face_id,
18045 glyph->left_box_line_p,
18046 glyph->right_box_line_p);
18048 // printf("dump xwidget glyph\n");
18050 #endif
18054 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18055 GLYPHS 0 means don't show glyph contents.
18056 GLYPHS 1 means show glyphs in short form
18057 GLYPHS > 1 means show glyphs in long form. */
18059 void
18060 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18062 if (glyphs != 1)
18064 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18065 fprintf (stderr, "==============================================================================\n");
18067 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18068 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18069 vpos,
18070 MATRIX_ROW_START_CHARPOS (row),
18071 MATRIX_ROW_END_CHARPOS (row),
18072 row->used[TEXT_AREA],
18073 row->contains_overlapping_glyphs_p,
18074 row->enabled_p,
18075 row->truncated_on_left_p,
18076 row->truncated_on_right_p,
18077 row->continued_p,
18078 MATRIX_ROW_CONTINUATION_LINE_P (row),
18079 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18080 row->ends_at_zv_p,
18081 row->fill_line_p,
18082 row->ends_in_middle_of_char_p,
18083 row->starts_in_middle_of_char_p,
18084 row->mouse_face_p,
18085 row->x,
18086 row->y,
18087 row->pixel_width,
18088 row->height,
18089 row->visible_height,
18090 row->ascent,
18091 row->phys_ascent);
18092 /* The next 3 lines should align to "Start" in the header. */
18093 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18094 row->end.overlay_string_index,
18095 row->continuation_lines_width);
18096 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18097 CHARPOS (row->start.string_pos),
18098 CHARPOS (row->end.string_pos));
18099 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18100 row->end.dpvec_index);
18103 if (glyphs > 1)
18105 int area;
18107 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18109 struct glyph *glyph = row->glyphs[area];
18110 struct glyph *glyph_end = glyph + row->used[area];
18112 /* Glyph for a line end in text. */
18113 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18114 ++glyph_end;
18116 if (glyph < glyph_end)
18117 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18119 for (; glyph < glyph_end; ++glyph)
18120 dump_glyph (row, glyph, area);
18123 else if (glyphs == 1)
18125 int area;
18127 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18129 char *s = alloca (row->used[area] + 4);
18130 int i;
18132 for (i = 0; i < row->used[area]; ++i)
18134 struct glyph *glyph = row->glyphs[area] + i;
18135 if (i == row->used[area] - 1
18136 && area == TEXT_AREA
18137 && INTEGERP (glyph->object)
18138 && glyph->type == CHAR_GLYPH
18139 && glyph->u.ch == ' ')
18141 strcpy (&s[i], "[\\n]");
18142 i += 4;
18144 else if (glyph->type == CHAR_GLYPH
18145 && glyph->u.ch < 0x80
18146 && glyph->u.ch >= ' ')
18147 s[i] = glyph->u.ch;
18148 else
18149 s[i] = '.';
18152 s[i] = '\0';
18153 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18159 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18160 Sdump_glyph_matrix, 0, 1, "p",
18161 doc: /* Dump the current matrix of the selected window to stderr.
18162 Shows contents of glyph row structures. With non-nil
18163 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18164 glyphs in short form, otherwise show glyphs in long form. */)
18165 (Lisp_Object glyphs)
18167 struct window *w = XWINDOW (selected_window);
18168 struct buffer *buffer = XBUFFER (w->contents);
18170 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18171 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18172 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18173 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18174 fprintf (stderr, "=============================================\n");
18175 dump_glyph_matrix (w->current_matrix,
18176 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18177 return Qnil;
18181 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18182 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18183 (void)
18185 struct frame *f = XFRAME (selected_frame);
18186 dump_glyph_matrix (f->current_matrix, 1);
18187 return Qnil;
18191 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18192 doc: /* Dump glyph row ROW to stderr.
18193 GLYPH 0 means don't dump glyphs.
18194 GLYPH 1 means dump glyphs in short form.
18195 GLYPH > 1 or omitted means dump glyphs in long form. */)
18196 (Lisp_Object row, Lisp_Object glyphs)
18198 struct glyph_matrix *matrix;
18199 EMACS_INT vpos;
18201 CHECK_NUMBER (row);
18202 matrix = XWINDOW (selected_window)->current_matrix;
18203 vpos = XINT (row);
18204 if (vpos >= 0 && vpos < matrix->nrows)
18205 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18206 vpos,
18207 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18208 return Qnil;
18212 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18213 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18214 GLYPH 0 means don't dump glyphs.
18215 GLYPH 1 means dump glyphs in short form.
18216 GLYPH > 1 or omitted means dump glyphs in long form. */)
18217 (Lisp_Object row, Lisp_Object glyphs)
18219 struct frame *sf = SELECTED_FRAME ();
18220 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18221 EMACS_INT vpos;
18223 CHECK_NUMBER (row);
18224 vpos = XINT (row);
18225 if (vpos >= 0 && vpos < m->nrows)
18226 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18227 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18228 return Qnil;
18232 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18233 doc: /* Toggle tracing of redisplay.
18234 With ARG, turn tracing on if and only if ARG is positive. */)
18235 (Lisp_Object arg)
18237 if (NILP (arg))
18238 trace_redisplay_p = !trace_redisplay_p;
18239 else
18241 arg = Fprefix_numeric_value (arg);
18242 trace_redisplay_p = XINT (arg) > 0;
18245 return Qnil;
18249 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18250 doc: /* Like `format', but print result to stderr.
18251 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18252 (ptrdiff_t nargs, Lisp_Object *args)
18254 Lisp_Object s = Fformat (nargs, args);
18255 fprintf (stderr, "%s", SDATA (s));
18256 return Qnil;
18259 #endif /* GLYPH_DEBUG */
18263 /***********************************************************************
18264 Building Desired Matrix Rows
18265 ***********************************************************************/
18267 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18268 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18270 static struct glyph_row *
18271 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18273 struct frame *f = XFRAME (WINDOW_FRAME (w));
18274 struct buffer *buffer = XBUFFER (w->contents);
18275 struct buffer *old = current_buffer;
18276 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18277 int arrow_len = SCHARS (overlay_arrow_string);
18278 const unsigned char *arrow_end = arrow_string + arrow_len;
18279 const unsigned char *p;
18280 struct it it;
18281 bool multibyte_p;
18282 int n_glyphs_before;
18284 set_buffer_temp (buffer);
18285 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18286 it.glyph_row->used[TEXT_AREA] = 0;
18287 SET_TEXT_POS (it.position, 0, 0);
18289 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18290 p = arrow_string;
18291 while (p < arrow_end)
18293 Lisp_Object face, ilisp;
18295 /* Get the next character. */
18296 if (multibyte_p)
18297 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18298 else
18300 it.c = it.char_to_display = *p, it.len = 1;
18301 if (! ASCII_CHAR_P (it.c))
18302 it.char_to_display = BYTE8_TO_CHAR (it.c);
18304 p += it.len;
18306 /* Get its face. */
18307 ilisp = make_number (p - arrow_string);
18308 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18309 it.face_id = compute_char_face (f, it.char_to_display, face);
18311 /* Compute its width, get its glyphs. */
18312 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18313 SET_TEXT_POS (it.position, -1, -1);
18314 PRODUCE_GLYPHS (&it);
18316 /* If this character doesn't fit any more in the line, we have
18317 to remove some glyphs. */
18318 if (it.current_x > it.last_visible_x)
18320 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18321 break;
18325 set_buffer_temp (old);
18326 return it.glyph_row;
18330 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18331 glyphs to insert is determined by produce_special_glyphs. */
18333 static void
18334 insert_left_trunc_glyphs (struct it *it)
18336 struct it truncate_it;
18337 struct glyph *from, *end, *to, *toend;
18339 eassert (!FRAME_WINDOW_P (it->f)
18340 || (!it->glyph_row->reversed_p
18341 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18342 || (it->glyph_row->reversed_p
18343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18345 /* Get the truncation glyphs. */
18346 truncate_it = *it;
18347 truncate_it.current_x = 0;
18348 truncate_it.face_id = DEFAULT_FACE_ID;
18349 truncate_it.glyph_row = &scratch_glyph_row;
18350 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18351 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18352 truncate_it.object = make_number (0);
18353 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18355 /* Overwrite glyphs from IT with truncation glyphs. */
18356 if (!it->glyph_row->reversed_p)
18358 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18360 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18361 end = from + tused;
18362 to = it->glyph_row->glyphs[TEXT_AREA];
18363 toend = to + it->glyph_row->used[TEXT_AREA];
18364 if (FRAME_WINDOW_P (it->f))
18366 /* On GUI frames, when variable-size fonts are displayed,
18367 the truncation glyphs may need more pixels than the row's
18368 glyphs they overwrite. We overwrite more glyphs to free
18369 enough screen real estate, and enlarge the stretch glyph
18370 on the right (see display_line), if there is one, to
18371 preserve the screen position of the truncation glyphs on
18372 the right. */
18373 int w = 0;
18374 struct glyph *g = to;
18375 short used;
18377 /* The first glyph could be partially visible, in which case
18378 it->glyph_row->x will be negative. But we want the left
18379 truncation glyphs to be aligned at the left margin of the
18380 window, so we override the x coordinate at which the row
18381 will begin. */
18382 it->glyph_row->x = 0;
18383 while (g < toend && w < it->truncation_pixel_width)
18385 w += g->pixel_width;
18386 ++g;
18388 if (g - to - tused > 0)
18390 memmove (to + tused, g, (toend - g) * sizeof(*g));
18391 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18393 used = it->glyph_row->used[TEXT_AREA];
18394 if (it->glyph_row->truncated_on_right_p
18395 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18396 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18397 == STRETCH_GLYPH)
18399 int extra = w - it->truncation_pixel_width;
18401 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18405 while (from < end)
18406 *to++ = *from++;
18408 /* There may be padding glyphs left over. Overwrite them too. */
18409 if (!FRAME_WINDOW_P (it->f))
18411 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18413 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18414 while (from < end)
18415 *to++ = *from++;
18419 if (to > toend)
18420 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18422 else
18424 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18426 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18427 that back to front. */
18428 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18429 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18430 toend = it->glyph_row->glyphs[TEXT_AREA];
18431 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18432 if (FRAME_WINDOW_P (it->f))
18434 int w = 0;
18435 struct glyph *g = to;
18437 while (g >= toend && w < it->truncation_pixel_width)
18439 w += g->pixel_width;
18440 --g;
18442 if (to - g - tused > 0)
18443 to = g + tused;
18444 if (it->glyph_row->truncated_on_right_p
18445 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18446 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18448 int extra = w - it->truncation_pixel_width;
18450 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18454 while (from >= end && to >= toend)
18455 *to-- = *from--;
18456 if (!FRAME_WINDOW_P (it->f))
18458 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18460 from =
18461 truncate_it.glyph_row->glyphs[TEXT_AREA]
18462 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18463 while (from >= end && to >= toend)
18464 *to-- = *from--;
18467 if (from >= end)
18469 /* Need to free some room before prepending additional
18470 glyphs. */
18471 int move_by = from - end + 1;
18472 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18473 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18475 for ( ; g >= g0; g--)
18476 g[move_by] = *g;
18477 while (from >= end)
18478 *to-- = *from--;
18479 it->glyph_row->used[TEXT_AREA] += move_by;
18484 /* Compute the hash code for ROW. */
18485 unsigned
18486 row_hash (struct glyph_row *row)
18488 int area, k;
18489 unsigned hashval = 0;
18491 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18492 for (k = 0; k < row->used[area]; ++k)
18493 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18494 + row->glyphs[area][k].u.val
18495 + row->glyphs[area][k].face_id
18496 + row->glyphs[area][k].padding_p
18497 + (row->glyphs[area][k].type << 2));
18499 return hashval;
18502 /* Compute the pixel height and width of IT->glyph_row.
18504 Most of the time, ascent and height of a display line will be equal
18505 to the max_ascent and max_height values of the display iterator
18506 structure. This is not the case if
18508 1. We hit ZV without displaying anything. In this case, max_ascent
18509 and max_height will be zero.
18511 2. We have some glyphs that don't contribute to the line height.
18512 (The glyph row flag contributes_to_line_height_p is for future
18513 pixmap extensions).
18515 The first case is easily covered by using default values because in
18516 these cases, the line height does not really matter, except that it
18517 must not be zero. */
18519 static void
18520 compute_line_metrics (struct it *it)
18522 struct glyph_row *row = it->glyph_row;
18524 if (FRAME_WINDOW_P (it->f))
18526 int i, min_y, max_y;
18528 /* The line may consist of one space only, that was added to
18529 place the cursor on it. If so, the row's height hasn't been
18530 computed yet. */
18531 if (row->height == 0)
18533 if (it->max_ascent + it->max_descent == 0)
18534 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18535 row->ascent = it->max_ascent;
18536 row->height = it->max_ascent + it->max_descent;
18537 row->phys_ascent = it->max_phys_ascent;
18538 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18539 row->extra_line_spacing = it->max_extra_line_spacing;
18542 /* Compute the width of this line. */
18543 row->pixel_width = row->x;
18544 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18545 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18547 eassert (row->pixel_width >= 0);
18548 eassert (row->ascent >= 0 && row->height > 0);
18550 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18551 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18553 /* If first line's physical ascent is larger than its logical
18554 ascent, use the physical ascent, and make the row taller.
18555 This makes accented characters fully visible. */
18556 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18557 && row->phys_ascent > row->ascent)
18559 row->height += row->phys_ascent - row->ascent;
18560 row->ascent = row->phys_ascent;
18563 /* Compute how much of the line is visible. */
18564 row->visible_height = row->height;
18566 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18567 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18569 if (row->y < min_y)
18570 row->visible_height -= min_y - row->y;
18571 if (row->y + row->height > max_y)
18572 row->visible_height -= row->y + row->height - max_y;
18574 else
18576 row->pixel_width = row->used[TEXT_AREA];
18577 if (row->continued_p)
18578 row->pixel_width -= it->continuation_pixel_width;
18579 else if (row->truncated_on_right_p)
18580 row->pixel_width -= it->truncation_pixel_width;
18581 row->ascent = row->phys_ascent = 0;
18582 row->height = row->phys_height = row->visible_height = 1;
18583 row->extra_line_spacing = 0;
18586 /* Compute a hash code for this row. */
18587 row->hash = row_hash (row);
18589 it->max_ascent = it->max_descent = 0;
18590 it->max_phys_ascent = it->max_phys_descent = 0;
18594 /* Append one space to the glyph row of iterator IT if doing a
18595 window-based redisplay. The space has the same face as
18596 IT->face_id. Value is non-zero if a space was added.
18598 This function is called to make sure that there is always one glyph
18599 at the end of a glyph row that the cursor can be set on under
18600 window-systems. (If there weren't such a glyph we would not know
18601 how wide and tall a box cursor should be displayed).
18603 At the same time this space let's a nicely handle clearing to the
18604 end of the line if the row ends in italic text. */
18606 static int
18607 append_space_for_newline (struct it *it, int default_face_p)
18609 if (FRAME_WINDOW_P (it->f))
18611 int n = it->glyph_row->used[TEXT_AREA];
18613 if (it->glyph_row->glyphs[TEXT_AREA] + n
18614 < it->glyph_row->glyphs[1 + TEXT_AREA])
18616 /* Save some values that must not be changed.
18617 Must save IT->c and IT->len because otherwise
18618 ITERATOR_AT_END_P wouldn't work anymore after
18619 append_space_for_newline has been called. */
18620 enum display_element_type saved_what = it->what;
18621 int saved_c = it->c, saved_len = it->len;
18622 int saved_char_to_display = it->char_to_display;
18623 int saved_x = it->current_x;
18624 int saved_face_id = it->face_id;
18625 int saved_box_end = it->end_of_box_run_p;
18626 struct text_pos saved_pos;
18627 Lisp_Object saved_object;
18628 struct face *face;
18630 saved_object = it->object;
18631 saved_pos = it->position;
18633 it->what = IT_CHARACTER;
18634 memset (&it->position, 0, sizeof it->position);
18635 it->object = make_number (0);
18636 it->c = it->char_to_display = ' ';
18637 it->len = 1;
18639 /* If the default face was remapped, be sure to use the
18640 remapped face for the appended newline. */
18641 if (default_face_p)
18642 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18643 else if (it->face_before_selective_p)
18644 it->face_id = it->saved_face_id;
18645 face = FACE_FROM_ID (it->f, it->face_id);
18646 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18647 /* In R2L rows, we will prepend a stretch glyph that will
18648 have the end_of_box_run_p flag set for it, so there's no
18649 need for the appended newline glyph to have that flag
18650 set. */
18651 if (it->glyph_row->reversed_p
18652 /* But if the appended newline glyph goes all the way to
18653 the end of the row, there will be no stretch glyph,
18654 so leave the box flag set. */
18655 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18656 it->end_of_box_run_p = 0;
18658 PRODUCE_GLYPHS (it);
18660 it->override_ascent = -1;
18661 it->constrain_row_ascent_descent_p = 0;
18662 it->current_x = saved_x;
18663 it->object = saved_object;
18664 it->position = saved_pos;
18665 it->what = saved_what;
18666 it->face_id = saved_face_id;
18667 it->len = saved_len;
18668 it->c = saved_c;
18669 it->char_to_display = saved_char_to_display;
18670 it->end_of_box_run_p = saved_box_end;
18671 return 1;
18675 return 0;
18679 /* Extend the face of the last glyph in the text area of IT->glyph_row
18680 to the end of the display line. Called from display_line. If the
18681 glyph row is empty, add a space glyph to it so that we know the
18682 face to draw. Set the glyph row flag fill_line_p. If the glyph
18683 row is R2L, prepend a stretch glyph to cover the empty space to the
18684 left of the leftmost glyph. */
18686 static void
18687 extend_face_to_end_of_line (struct it *it)
18689 struct face *face, *default_face;
18690 struct frame *f = it->f;
18692 /* If line is already filled, do nothing. Non window-system frames
18693 get a grace of one more ``pixel'' because their characters are
18694 1-``pixel'' wide, so they hit the equality too early. This grace
18695 is needed only for R2L rows that are not continued, to produce
18696 one extra blank where we could display the cursor. */
18697 if (it->current_x >= it->last_visible_x
18698 + (!FRAME_WINDOW_P (f)
18699 && it->glyph_row->reversed_p
18700 && !it->glyph_row->continued_p))
18701 return;
18703 /* The default face, possibly remapped. */
18704 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18706 /* Face extension extends the background and box of IT->face_id
18707 to the end of the line. If the background equals the background
18708 of the frame, we don't have to do anything. */
18709 if (it->face_before_selective_p)
18710 face = FACE_FROM_ID (f, it->saved_face_id);
18711 else
18712 face = FACE_FROM_ID (f, it->face_id);
18714 if (FRAME_WINDOW_P (f)
18715 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18716 && face->box == FACE_NO_BOX
18717 && face->background == FRAME_BACKGROUND_PIXEL (f)
18718 && !face->stipple
18719 && !it->glyph_row->reversed_p)
18720 return;
18722 /* Set the glyph row flag indicating that the face of the last glyph
18723 in the text area has to be drawn to the end of the text area. */
18724 it->glyph_row->fill_line_p = 1;
18726 /* If current character of IT is not ASCII, make sure we have the
18727 ASCII face. This will be automatically undone the next time
18728 get_next_display_element returns a multibyte character. Note
18729 that the character will always be single byte in unibyte
18730 text. */
18731 if (!ASCII_CHAR_P (it->c))
18733 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18736 if (FRAME_WINDOW_P (f))
18738 /* If the row is empty, add a space with the current face of IT,
18739 so that we know which face to draw. */
18740 if (it->glyph_row->used[TEXT_AREA] == 0)
18742 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18743 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18744 it->glyph_row->used[TEXT_AREA] = 1;
18746 #ifdef HAVE_WINDOW_SYSTEM
18747 if (it->glyph_row->reversed_p)
18749 /* Prepend a stretch glyph to the row, such that the
18750 rightmost glyph will be drawn flushed all the way to the
18751 right margin of the window. The stretch glyph that will
18752 occupy the empty space, if any, to the left of the
18753 glyphs. */
18754 struct font *font = face->font ? face->font : FRAME_FONT (f);
18755 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18756 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18757 struct glyph *g;
18758 int row_width, stretch_ascent, stretch_width;
18759 struct text_pos saved_pos;
18760 int saved_face_id, saved_avoid_cursor, saved_box_start;
18762 for (row_width = 0, g = row_start; g < row_end; g++)
18763 row_width += g->pixel_width;
18764 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18765 if (stretch_width > 0)
18767 stretch_ascent =
18768 (((it->ascent + it->descent)
18769 * FONT_BASE (font)) / FONT_HEIGHT (font));
18770 saved_pos = it->position;
18771 memset (&it->position, 0, sizeof it->position);
18772 saved_avoid_cursor = it->avoid_cursor_p;
18773 it->avoid_cursor_p = 1;
18774 saved_face_id = it->face_id;
18775 saved_box_start = it->start_of_box_run_p;
18776 /* The last row's stretch glyph should get the default
18777 face, to avoid painting the rest of the window with
18778 the region face, if the region ends at ZV. */
18779 if (it->glyph_row->ends_at_zv_p)
18780 it->face_id = default_face->id;
18781 else
18782 it->face_id = face->id;
18783 it->start_of_box_run_p = 0;
18784 append_stretch_glyph (it, make_number (0), stretch_width,
18785 it->ascent + it->descent, stretch_ascent);
18786 it->position = saved_pos;
18787 it->avoid_cursor_p = saved_avoid_cursor;
18788 it->face_id = saved_face_id;
18789 it->start_of_box_run_p = saved_box_start;
18792 #endif /* HAVE_WINDOW_SYSTEM */
18794 else
18796 /* Save some values that must not be changed. */
18797 int saved_x = it->current_x;
18798 struct text_pos saved_pos;
18799 Lisp_Object saved_object;
18800 enum display_element_type saved_what = it->what;
18801 int saved_face_id = it->face_id;
18803 saved_object = it->object;
18804 saved_pos = it->position;
18806 it->what = IT_CHARACTER;
18807 memset (&it->position, 0, sizeof it->position);
18808 it->object = make_number (0);
18809 it->c = it->char_to_display = ' ';
18810 it->len = 1;
18811 /* The last row's blank glyphs should get the default face, to
18812 avoid painting the rest of the window with the region face,
18813 if the region ends at ZV. */
18814 if (it->glyph_row->ends_at_zv_p)
18815 it->face_id = default_face->id;
18816 else
18817 it->face_id = face->id;
18819 PRODUCE_GLYPHS (it);
18821 while (it->current_x <= it->last_visible_x)
18822 PRODUCE_GLYPHS (it);
18824 /* Don't count these blanks really. It would let us insert a left
18825 truncation glyph below and make us set the cursor on them, maybe. */
18826 it->current_x = saved_x;
18827 it->object = saved_object;
18828 it->position = saved_pos;
18829 it->what = saved_what;
18830 it->face_id = saved_face_id;
18835 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18836 trailing whitespace. */
18838 static int
18839 trailing_whitespace_p (ptrdiff_t charpos)
18841 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18842 int c = 0;
18844 while (bytepos < ZV_BYTE
18845 && (c = FETCH_CHAR (bytepos),
18846 c == ' ' || c == '\t'))
18847 ++bytepos;
18849 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18851 if (bytepos != PT_BYTE)
18852 return 1;
18854 return 0;
18858 /* Highlight trailing whitespace, if any, in ROW. */
18860 static void
18861 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18863 int used = row->used[TEXT_AREA];
18865 if (used)
18867 struct glyph *start = row->glyphs[TEXT_AREA];
18868 struct glyph *glyph = start + used - 1;
18870 if (row->reversed_p)
18872 /* Right-to-left rows need to be processed in the opposite
18873 direction, so swap the edge pointers. */
18874 glyph = start;
18875 start = row->glyphs[TEXT_AREA] + used - 1;
18878 /* Skip over glyphs inserted to display the cursor at the
18879 end of a line, for extending the face of the last glyph
18880 to the end of the line on terminals, and for truncation
18881 and continuation glyphs. */
18882 if (!row->reversed_p)
18884 while (glyph >= start
18885 && glyph->type == CHAR_GLYPH
18886 && INTEGERP (glyph->object))
18887 --glyph;
18889 else
18891 while (glyph <= start
18892 && glyph->type == CHAR_GLYPH
18893 && INTEGERP (glyph->object))
18894 ++glyph;
18897 /* If last glyph is a space or stretch, and it's trailing
18898 whitespace, set the face of all trailing whitespace glyphs in
18899 IT->glyph_row to `trailing-whitespace'. */
18900 if ((row->reversed_p ? glyph <= start : glyph >= start)
18901 && BUFFERP (glyph->object)
18902 && (glyph->type == STRETCH_GLYPH
18903 || (glyph->type == CHAR_GLYPH
18904 && glyph->u.ch == ' '))
18905 && trailing_whitespace_p (glyph->charpos))
18907 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18908 if (face_id < 0)
18909 return;
18911 if (!row->reversed_p)
18913 while (glyph >= start
18914 && BUFFERP (glyph->object)
18915 && (glyph->type == STRETCH_GLYPH
18916 || (glyph->type == CHAR_GLYPH
18917 && glyph->u.ch == ' ')))
18918 (glyph--)->face_id = face_id;
18920 else
18922 while (glyph <= start
18923 && BUFFERP (glyph->object)
18924 && (glyph->type == STRETCH_GLYPH
18925 || (glyph->type == CHAR_GLYPH
18926 && glyph->u.ch == ' ')))
18927 (glyph++)->face_id = face_id;
18934 /* Value is non-zero if glyph row ROW should be
18935 considered to hold the buffer position CHARPOS. */
18937 static int
18938 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18940 int result = 1;
18942 if (charpos == CHARPOS (row->end.pos)
18943 || charpos == MATRIX_ROW_END_CHARPOS (row))
18945 /* Suppose the row ends on a string.
18946 Unless the row is continued, that means it ends on a newline
18947 in the string. If it's anything other than a display string
18948 (e.g., a before-string from an overlay), we don't want the
18949 cursor there. (This heuristic seems to give the optimal
18950 behavior for the various types of multi-line strings.)
18951 One exception: if the string has `cursor' property on one of
18952 its characters, we _do_ want the cursor there. */
18953 if (CHARPOS (row->end.string_pos) >= 0)
18955 if (row->continued_p)
18956 result = 1;
18957 else
18959 /* Check for `display' property. */
18960 struct glyph *beg = row->glyphs[TEXT_AREA];
18961 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18962 struct glyph *glyph;
18964 result = 0;
18965 for (glyph = end; glyph >= beg; --glyph)
18966 if (STRINGP (glyph->object))
18968 Lisp_Object prop
18969 = Fget_char_property (make_number (charpos),
18970 Qdisplay, Qnil);
18971 result =
18972 (!NILP (prop)
18973 && display_prop_string_p (prop, glyph->object));
18974 /* If there's a `cursor' property on one of the
18975 string's characters, this row is a cursor row,
18976 even though this is not a display string. */
18977 if (!result)
18979 Lisp_Object s = glyph->object;
18981 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18983 ptrdiff_t gpos = glyph->charpos;
18985 if (!NILP (Fget_char_property (make_number (gpos),
18986 Qcursor, s)))
18988 result = 1;
18989 break;
18993 break;
18997 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18999 /* If the row ends in middle of a real character,
19000 and the line is continued, we want the cursor here.
19001 That's because CHARPOS (ROW->end.pos) would equal
19002 PT if PT is before the character. */
19003 if (!row->ends_in_ellipsis_p)
19004 result = row->continued_p;
19005 else
19006 /* If the row ends in an ellipsis, then
19007 CHARPOS (ROW->end.pos) will equal point after the
19008 invisible text. We want that position to be displayed
19009 after the ellipsis. */
19010 result = 0;
19012 /* If the row ends at ZV, display the cursor at the end of that
19013 row instead of at the start of the row below. */
19014 else if (row->ends_at_zv_p)
19015 result = 1;
19016 else
19017 result = 0;
19020 return result;
19023 /* Value is non-zero if glyph row ROW should be
19024 used to hold the cursor. */
19026 static int
19027 cursor_row_p (struct glyph_row *row)
19029 return row_for_charpos_p (row, PT);
19034 /* Push the property PROP so that it will be rendered at the current
19035 position in IT. Return 1 if PROP was successfully pushed, 0
19036 otherwise. Called from handle_line_prefix to handle the
19037 `line-prefix' and `wrap-prefix' properties. */
19039 static int
19040 push_prefix_prop (struct it *it, Lisp_Object prop)
19042 struct text_pos pos =
19043 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19045 eassert (it->method == GET_FROM_BUFFER
19046 || it->method == GET_FROM_DISPLAY_VECTOR
19047 || it->method == GET_FROM_STRING);
19049 /* We need to save the current buffer/string position, so it will be
19050 restored by pop_it, because iterate_out_of_display_property
19051 depends on that being set correctly, but some situations leave
19052 it->position not yet set when this function is called. */
19053 push_it (it, &pos);
19055 if (STRINGP (prop))
19057 if (SCHARS (prop) == 0)
19059 pop_it (it);
19060 return 0;
19063 it->string = prop;
19064 it->string_from_prefix_prop_p = 1;
19065 it->multibyte_p = STRING_MULTIBYTE (it->string);
19066 it->current.overlay_string_index = -1;
19067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19068 it->end_charpos = it->string_nchars = SCHARS (it->string);
19069 it->method = GET_FROM_STRING;
19070 it->stop_charpos = 0;
19071 it->prev_stop = 0;
19072 it->base_level_stop = 0;
19074 /* Force paragraph direction to be that of the parent
19075 buffer/string. */
19076 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19077 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19078 else
19079 it->paragraph_embedding = L2R;
19081 /* Set up the bidi iterator for this display string. */
19082 if (it->bidi_p)
19084 it->bidi_it.string.lstring = it->string;
19085 it->bidi_it.string.s = NULL;
19086 it->bidi_it.string.schars = it->end_charpos;
19087 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19088 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19089 it->bidi_it.string.unibyte = !it->multibyte_p;
19090 it->bidi_it.w = it->w;
19091 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19094 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19096 it->method = GET_FROM_STRETCH;
19097 it->object = prop;
19099 #ifdef HAVE_WINDOW_SYSTEM
19100 else if (IMAGEP (prop))
19102 it->what = IT_IMAGE;
19103 it->image_id = lookup_image (it->f, prop);
19104 it->method = GET_FROM_IMAGE;
19106 #endif /* HAVE_WINDOW_SYSTEM */
19107 else
19109 pop_it (it); /* bogus display property, give up */
19110 return 0;
19113 return 1;
19116 /* Return the character-property PROP at the current position in IT. */
19118 static Lisp_Object
19119 get_it_property (struct it *it, Lisp_Object prop)
19121 Lisp_Object position, object = it->object;
19123 if (STRINGP (object))
19124 position = make_number (IT_STRING_CHARPOS (*it));
19125 else if (BUFFERP (object))
19127 position = make_number (IT_CHARPOS (*it));
19128 object = it->window;
19130 else
19131 return Qnil;
19133 return Fget_char_property (position, prop, object);
19136 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19138 static void
19139 handle_line_prefix (struct it *it)
19141 Lisp_Object prefix;
19143 if (it->continuation_lines_width > 0)
19145 prefix = get_it_property (it, Qwrap_prefix);
19146 if (NILP (prefix))
19147 prefix = Vwrap_prefix;
19149 else
19151 prefix = get_it_property (it, Qline_prefix);
19152 if (NILP (prefix))
19153 prefix = Vline_prefix;
19155 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19157 /* If the prefix is wider than the window, and we try to wrap
19158 it, it would acquire its own wrap prefix, and so on till the
19159 iterator stack overflows. So, don't wrap the prefix. */
19160 it->line_wrap = TRUNCATE;
19161 it->avoid_cursor_p = 1;
19167 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19168 only for R2L lines from display_line and display_string, when they
19169 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19170 the line/string needs to be continued on the next glyph row. */
19171 static void
19172 unproduce_glyphs (struct it *it, int n)
19174 struct glyph *glyph, *end;
19176 eassert (it->glyph_row);
19177 eassert (it->glyph_row->reversed_p);
19178 eassert (it->area == TEXT_AREA);
19179 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19181 if (n > it->glyph_row->used[TEXT_AREA])
19182 n = it->glyph_row->used[TEXT_AREA];
19183 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19184 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19185 for ( ; glyph < end; glyph++)
19186 glyph[-n] = *glyph;
19189 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19190 and ROW->maxpos. */
19191 static void
19192 find_row_edges (struct it *it, struct glyph_row *row,
19193 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19194 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19196 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19197 lines' rows is implemented for bidi-reordered rows. */
19199 /* ROW->minpos is the value of min_pos, the minimal buffer position
19200 we have in ROW, or ROW->start.pos if that is smaller. */
19201 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19202 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19203 else
19204 /* We didn't find buffer positions smaller than ROW->start, or
19205 didn't find _any_ valid buffer positions in any of the glyphs,
19206 so we must trust the iterator's computed positions. */
19207 row->minpos = row->start.pos;
19208 if (max_pos <= 0)
19210 max_pos = CHARPOS (it->current.pos);
19211 max_bpos = BYTEPOS (it->current.pos);
19214 /* Here are the various use-cases for ending the row, and the
19215 corresponding values for ROW->maxpos:
19217 Line ends in a newline from buffer eol_pos + 1
19218 Line is continued from buffer max_pos + 1
19219 Line is truncated on right it->current.pos
19220 Line ends in a newline from string max_pos + 1(*)
19221 (*) + 1 only when line ends in a forward scan
19222 Line is continued from string max_pos
19223 Line is continued from display vector max_pos
19224 Line is entirely from a string min_pos == max_pos
19225 Line is entirely from a display vector min_pos == max_pos
19226 Line that ends at ZV ZV
19228 If you discover other use-cases, please add them here as
19229 appropriate. */
19230 if (row->ends_at_zv_p)
19231 row->maxpos = it->current.pos;
19232 else if (row->used[TEXT_AREA])
19234 int seen_this_string = 0;
19235 struct glyph_row *r1 = row - 1;
19237 /* Did we see the same display string on the previous row? */
19238 if (STRINGP (it->object)
19239 /* this is not the first row */
19240 && row > it->w->desired_matrix->rows
19241 /* previous row is not the header line */
19242 && !r1->mode_line_p
19243 /* previous row also ends in a newline from a string */
19244 && r1->ends_in_newline_from_string_p)
19246 struct glyph *start, *end;
19248 /* Search for the last glyph of the previous row that came
19249 from buffer or string. Depending on whether the row is
19250 L2R or R2L, we need to process it front to back or the
19251 other way round. */
19252 if (!r1->reversed_p)
19254 start = r1->glyphs[TEXT_AREA];
19255 end = start + r1->used[TEXT_AREA];
19256 /* Glyphs inserted by redisplay have an integer (zero)
19257 as their object. */
19258 while (end > start
19259 && INTEGERP ((end - 1)->object)
19260 && (end - 1)->charpos <= 0)
19261 --end;
19262 if (end > start)
19264 if (EQ ((end - 1)->object, it->object))
19265 seen_this_string = 1;
19267 else
19268 /* If all the glyphs of the previous row were inserted
19269 by redisplay, it means the previous row was
19270 produced from a single newline, which is only
19271 possible if that newline came from the same string
19272 as the one which produced this ROW. */
19273 seen_this_string = 1;
19275 else
19277 end = r1->glyphs[TEXT_AREA] - 1;
19278 start = end + r1->used[TEXT_AREA];
19279 while (end < start
19280 && INTEGERP ((end + 1)->object)
19281 && (end + 1)->charpos <= 0)
19282 ++end;
19283 if (end < start)
19285 if (EQ ((end + 1)->object, it->object))
19286 seen_this_string = 1;
19288 else
19289 seen_this_string = 1;
19292 /* Take note of each display string that covers a newline only
19293 once, the first time we see it. This is for when a display
19294 string includes more than one newline in it. */
19295 if (row->ends_in_newline_from_string_p && !seen_this_string)
19297 /* If we were scanning the buffer forward when we displayed
19298 the string, we want to account for at least one buffer
19299 position that belongs to this row (position covered by
19300 the display string), so that cursor positioning will
19301 consider this row as a candidate when point is at the end
19302 of the visual line represented by this row. This is not
19303 required when scanning back, because max_pos will already
19304 have a much larger value. */
19305 if (CHARPOS (row->end.pos) > max_pos)
19306 INC_BOTH (max_pos, max_bpos);
19307 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19309 else if (CHARPOS (it->eol_pos) > 0)
19310 SET_TEXT_POS (row->maxpos,
19311 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19312 else if (row->continued_p)
19314 /* If max_pos is different from IT's current position, it
19315 means IT->method does not belong to the display element
19316 at max_pos. However, it also means that the display
19317 element at max_pos was displayed in its entirety on this
19318 line, which is equivalent to saying that the next line
19319 starts at the next buffer position. */
19320 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19321 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19322 else
19324 INC_BOTH (max_pos, max_bpos);
19325 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19328 else if (row->truncated_on_right_p)
19329 /* display_line already called reseat_at_next_visible_line_start,
19330 which puts the iterator at the beginning of the next line, in
19331 the logical order. */
19332 row->maxpos = it->current.pos;
19333 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19334 /* A line that is entirely from a string/image/stretch... */
19335 row->maxpos = row->minpos;
19336 else
19337 emacs_abort ();
19339 else
19340 row->maxpos = it->current.pos;
19343 /* Construct the glyph row IT->glyph_row in the desired matrix of
19344 IT->w from text at the current position of IT. See dispextern.h
19345 for an overview of struct it. Value is non-zero if
19346 IT->glyph_row displays text, as opposed to a line displaying ZV
19347 only. */
19349 static int
19350 display_line (struct it *it)
19352 struct glyph_row *row = it->glyph_row;
19353 Lisp_Object overlay_arrow_string;
19354 struct it wrap_it;
19355 void *wrap_data = NULL;
19356 int may_wrap = 0, wrap_x IF_LINT (= 0);
19357 int wrap_row_used = -1;
19358 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19359 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19360 int wrap_row_extra_line_spacing IF_LINT (= 0);
19361 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19362 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19363 int cvpos;
19364 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19365 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19367 /* We always start displaying at hpos zero even if hscrolled. */
19368 eassert (it->hpos == 0 && it->current_x == 0);
19370 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19371 >= it->w->desired_matrix->nrows)
19373 it->w->nrows_scale_factor++;
19374 it->f->fonts_changed = 1;
19375 return 0;
19378 /* Is IT->w showing the region? */
19379 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19381 /* Clear the result glyph row and enable it. */
19382 prepare_desired_row (row);
19384 row->y = it->current_y;
19385 row->start = it->start;
19386 row->continuation_lines_width = it->continuation_lines_width;
19387 row->displays_text_p = 1;
19388 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19389 it->starts_in_middle_of_char_p = 0;
19391 /* Arrange the overlays nicely for our purposes. Usually, we call
19392 display_line on only one line at a time, in which case this
19393 can't really hurt too much, or we call it on lines which appear
19394 one after another in the buffer, in which case all calls to
19395 recenter_overlay_lists but the first will be pretty cheap. */
19396 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19398 /* Move over display elements that are not visible because we are
19399 hscrolled. This may stop at an x-position < IT->first_visible_x
19400 if the first glyph is partially visible or if we hit a line end. */
19401 if (it->current_x < it->first_visible_x)
19403 enum move_it_result move_result;
19405 this_line_min_pos = row->start.pos;
19406 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19407 MOVE_TO_POS | MOVE_TO_X);
19408 /* If we are under a large hscroll, move_it_in_display_line_to
19409 could hit the end of the line without reaching
19410 it->first_visible_x. Pretend that we did reach it. This is
19411 especially important on a TTY, where we will call
19412 extend_face_to_end_of_line, which needs to know how many
19413 blank glyphs to produce. */
19414 if (it->current_x < it->first_visible_x
19415 && (move_result == MOVE_NEWLINE_OR_CR
19416 || move_result == MOVE_POS_MATCH_OR_ZV))
19417 it->current_x = it->first_visible_x;
19419 /* Record the smallest positions seen while we moved over
19420 display elements that are not visible. This is needed by
19421 redisplay_internal for optimizing the case where the cursor
19422 stays inside the same line. The rest of this function only
19423 considers positions that are actually displayed, so
19424 RECORD_MAX_MIN_POS will not otherwise record positions that
19425 are hscrolled to the left of the left edge of the window. */
19426 min_pos = CHARPOS (this_line_min_pos);
19427 min_bpos = BYTEPOS (this_line_min_pos);
19429 else
19431 /* We only do this when not calling `move_it_in_display_line_to'
19432 above, because move_it_in_display_line_to calls
19433 handle_line_prefix itself. */
19434 handle_line_prefix (it);
19437 /* Get the initial row height. This is either the height of the
19438 text hscrolled, if there is any, or zero. */
19439 row->ascent = it->max_ascent;
19440 row->height = it->max_ascent + it->max_descent;
19441 row->phys_ascent = it->max_phys_ascent;
19442 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19443 row->extra_line_spacing = it->max_extra_line_spacing;
19445 /* Utility macro to record max and min buffer positions seen until now. */
19446 #define RECORD_MAX_MIN_POS(IT) \
19447 do \
19449 int composition_p = !STRINGP ((IT)->string) \
19450 && ((IT)->what == IT_COMPOSITION); \
19451 ptrdiff_t current_pos = \
19452 composition_p ? (IT)->cmp_it.charpos \
19453 : IT_CHARPOS (*(IT)); \
19454 ptrdiff_t current_bpos = \
19455 composition_p ? CHAR_TO_BYTE (current_pos) \
19456 : IT_BYTEPOS (*(IT)); \
19457 if (current_pos < min_pos) \
19459 min_pos = current_pos; \
19460 min_bpos = current_bpos; \
19462 if (IT_CHARPOS (*it) > max_pos) \
19464 max_pos = IT_CHARPOS (*it); \
19465 max_bpos = IT_BYTEPOS (*it); \
19468 while (0)
19470 /* Loop generating characters. The loop is left with IT on the next
19471 character to display. */
19472 while (1)
19474 int n_glyphs_before, hpos_before, x_before;
19475 int x, nglyphs;
19476 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19478 /* Retrieve the next thing to display. Value is zero if end of
19479 buffer reached. */
19480 if (!get_next_display_element (it))
19482 /* Maybe add a space at the end of this line that is used to
19483 display the cursor there under X. Set the charpos of the
19484 first glyph of blank lines not corresponding to any text
19485 to -1. */
19486 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19487 row->exact_window_width_line_p = 1;
19488 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19489 || row->used[TEXT_AREA] == 0)
19491 row->glyphs[TEXT_AREA]->charpos = -1;
19492 row->displays_text_p = 0;
19494 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19495 && (!MINI_WINDOW_P (it->w)
19496 || (minibuf_level && EQ (it->window, minibuf_window))))
19497 row->indicate_empty_line_p = 1;
19500 it->continuation_lines_width = 0;
19501 row->ends_at_zv_p = 1;
19502 /* A row that displays right-to-left text must always have
19503 its last face extended all the way to the end of line,
19504 even if this row ends in ZV, because we still write to
19505 the screen left to right. We also need to extend the
19506 last face if the default face is remapped to some
19507 different face, otherwise the functions that clear
19508 portions of the screen will clear with the default face's
19509 background color. */
19510 if (row->reversed_p
19511 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19512 extend_face_to_end_of_line (it);
19513 break;
19516 /* Now, get the metrics of what we want to display. This also
19517 generates glyphs in `row' (which is IT->glyph_row). */
19518 n_glyphs_before = row->used[TEXT_AREA];
19519 x = it->current_x;
19521 /* Remember the line height so far in case the next element doesn't
19522 fit on the line. */
19523 if (it->line_wrap != TRUNCATE)
19525 ascent = it->max_ascent;
19526 descent = it->max_descent;
19527 phys_ascent = it->max_phys_ascent;
19528 phys_descent = it->max_phys_descent;
19530 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19532 if (IT_DISPLAYING_WHITESPACE (it))
19533 may_wrap = 1;
19534 else if (may_wrap)
19536 SAVE_IT (wrap_it, *it, wrap_data);
19537 wrap_x = x;
19538 wrap_row_used = row->used[TEXT_AREA];
19539 wrap_row_ascent = row->ascent;
19540 wrap_row_height = row->height;
19541 wrap_row_phys_ascent = row->phys_ascent;
19542 wrap_row_phys_height = row->phys_height;
19543 wrap_row_extra_line_spacing = row->extra_line_spacing;
19544 wrap_row_min_pos = min_pos;
19545 wrap_row_min_bpos = min_bpos;
19546 wrap_row_max_pos = max_pos;
19547 wrap_row_max_bpos = max_bpos;
19548 may_wrap = 0;
19553 PRODUCE_GLYPHS (it);
19555 /* If this display element was in marginal areas, continue with
19556 the next one. */
19557 if (it->area != TEXT_AREA)
19559 row->ascent = max (row->ascent, it->max_ascent);
19560 row->height = max (row->height, it->max_ascent + it->max_descent);
19561 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19562 row->phys_height = max (row->phys_height,
19563 it->max_phys_ascent + it->max_phys_descent);
19564 row->extra_line_spacing = max (row->extra_line_spacing,
19565 it->max_extra_line_spacing);
19566 set_iterator_to_next (it, 1);
19567 continue;
19570 /* Does the display element fit on the line? If we truncate
19571 lines, we should draw past the right edge of the window. If
19572 we don't truncate, we want to stop so that we can display the
19573 continuation glyph before the right margin. If lines are
19574 continued, there are two possible strategies for characters
19575 resulting in more than 1 glyph (e.g. tabs): Display as many
19576 glyphs as possible in this line and leave the rest for the
19577 continuation line, or display the whole element in the next
19578 line. Original redisplay did the former, so we do it also. */
19579 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19580 hpos_before = it->hpos;
19581 x_before = x;
19583 if (/* Not a newline. */
19584 nglyphs > 0
19585 /* Glyphs produced fit entirely in the line. */
19586 && it->current_x < it->last_visible_x)
19588 it->hpos += nglyphs;
19589 row->ascent = max (row->ascent, it->max_ascent);
19590 row->height = max (row->height, it->max_ascent + it->max_descent);
19591 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19592 row->phys_height = max (row->phys_height,
19593 it->max_phys_ascent + it->max_phys_descent);
19594 row->extra_line_spacing = max (row->extra_line_spacing,
19595 it->max_extra_line_spacing);
19596 if (it->current_x - it->pixel_width < it->first_visible_x)
19597 row->x = x - it->first_visible_x;
19598 /* Record the maximum and minimum buffer positions seen so
19599 far in glyphs that will be displayed by this row. */
19600 if (it->bidi_p)
19601 RECORD_MAX_MIN_POS (it);
19603 else
19605 int i, new_x;
19606 struct glyph *glyph;
19608 for (i = 0; i < nglyphs; ++i, x = new_x)
19610 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19611 new_x = x + glyph->pixel_width;
19613 if (/* Lines are continued. */
19614 it->line_wrap != TRUNCATE
19615 && (/* Glyph doesn't fit on the line. */
19616 new_x > it->last_visible_x
19617 /* Or it fits exactly on a window system frame. */
19618 || (new_x == it->last_visible_x
19619 && FRAME_WINDOW_P (it->f)
19620 && (row->reversed_p
19621 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19622 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19624 /* End of a continued line. */
19626 if (it->hpos == 0
19627 || (new_x == it->last_visible_x
19628 && FRAME_WINDOW_P (it->f)
19629 && (row->reversed_p
19630 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19631 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19633 /* Current glyph is the only one on the line or
19634 fits exactly on the line. We must continue
19635 the line because we can't draw the cursor
19636 after the glyph. */
19637 row->continued_p = 1;
19638 it->current_x = new_x;
19639 it->continuation_lines_width += new_x;
19640 ++it->hpos;
19641 if (i == nglyphs - 1)
19643 /* If line-wrap is on, check if a previous
19644 wrap point was found. */
19645 if (wrap_row_used > 0
19646 /* Even if there is a previous wrap
19647 point, continue the line here as
19648 usual, if (i) the previous character
19649 was a space or tab AND (ii) the
19650 current character is not. */
19651 && (!may_wrap
19652 || IT_DISPLAYING_WHITESPACE (it)))
19653 goto back_to_wrap;
19655 /* Record the maximum and minimum buffer
19656 positions seen so far in glyphs that will be
19657 displayed by this row. */
19658 if (it->bidi_p)
19659 RECORD_MAX_MIN_POS (it);
19660 set_iterator_to_next (it, 1);
19661 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19663 if (!get_next_display_element (it))
19665 row->exact_window_width_line_p = 1;
19666 it->continuation_lines_width = 0;
19667 row->continued_p = 0;
19668 row->ends_at_zv_p = 1;
19670 else if (ITERATOR_AT_END_OF_LINE_P (it))
19672 row->continued_p = 0;
19673 row->exact_window_width_line_p = 1;
19677 else if (it->bidi_p)
19678 RECORD_MAX_MIN_POS (it);
19680 else if (CHAR_GLYPH_PADDING_P (*glyph)
19681 && !FRAME_WINDOW_P (it->f))
19683 /* A padding glyph that doesn't fit on this line.
19684 This means the whole character doesn't fit
19685 on the line. */
19686 if (row->reversed_p)
19687 unproduce_glyphs (it, row->used[TEXT_AREA]
19688 - n_glyphs_before);
19689 row->used[TEXT_AREA] = n_glyphs_before;
19691 /* Fill the rest of the row with continuation
19692 glyphs like in 20.x. */
19693 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19694 < row->glyphs[1 + TEXT_AREA])
19695 produce_special_glyphs (it, IT_CONTINUATION);
19697 row->continued_p = 1;
19698 it->current_x = x_before;
19699 it->continuation_lines_width += x_before;
19701 /* Restore the height to what it was before the
19702 element not fitting on the line. */
19703 it->max_ascent = ascent;
19704 it->max_descent = descent;
19705 it->max_phys_ascent = phys_ascent;
19706 it->max_phys_descent = phys_descent;
19708 else if (wrap_row_used > 0)
19710 back_to_wrap:
19711 if (row->reversed_p)
19712 unproduce_glyphs (it,
19713 row->used[TEXT_AREA] - wrap_row_used);
19714 RESTORE_IT (it, &wrap_it, wrap_data);
19715 it->continuation_lines_width += wrap_x;
19716 row->used[TEXT_AREA] = wrap_row_used;
19717 row->ascent = wrap_row_ascent;
19718 row->height = wrap_row_height;
19719 row->phys_ascent = wrap_row_phys_ascent;
19720 row->phys_height = wrap_row_phys_height;
19721 row->extra_line_spacing = wrap_row_extra_line_spacing;
19722 min_pos = wrap_row_min_pos;
19723 min_bpos = wrap_row_min_bpos;
19724 max_pos = wrap_row_max_pos;
19725 max_bpos = wrap_row_max_bpos;
19726 row->continued_p = 1;
19727 row->ends_at_zv_p = 0;
19728 row->exact_window_width_line_p = 0;
19729 it->continuation_lines_width += x;
19731 /* Make sure that a non-default face is extended
19732 up to the right margin of the window. */
19733 extend_face_to_end_of_line (it);
19735 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19737 /* A TAB that extends past the right edge of the
19738 window. This produces a single glyph on
19739 window system frames. We leave the glyph in
19740 this row and let it fill the row, but don't
19741 consume the TAB. */
19742 if ((row->reversed_p
19743 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19744 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19745 produce_special_glyphs (it, IT_CONTINUATION);
19746 it->continuation_lines_width += it->last_visible_x;
19747 row->ends_in_middle_of_char_p = 1;
19748 row->continued_p = 1;
19749 glyph->pixel_width = it->last_visible_x - x;
19750 it->starts_in_middle_of_char_p = 1;
19752 else
19754 /* Something other than a TAB that draws past
19755 the right edge of the window. Restore
19756 positions to values before the element. */
19757 if (row->reversed_p)
19758 unproduce_glyphs (it, row->used[TEXT_AREA]
19759 - (n_glyphs_before + i));
19760 row->used[TEXT_AREA] = n_glyphs_before + i;
19762 /* Display continuation glyphs. */
19763 it->current_x = x_before;
19764 it->continuation_lines_width += x;
19765 if (!FRAME_WINDOW_P (it->f)
19766 || (row->reversed_p
19767 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19768 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19769 produce_special_glyphs (it, IT_CONTINUATION);
19770 row->continued_p = 1;
19772 extend_face_to_end_of_line (it);
19774 if (nglyphs > 1 && i > 0)
19776 row->ends_in_middle_of_char_p = 1;
19777 it->starts_in_middle_of_char_p = 1;
19780 /* Restore the height to what it was before the
19781 element not fitting on the line. */
19782 it->max_ascent = ascent;
19783 it->max_descent = descent;
19784 it->max_phys_ascent = phys_ascent;
19785 it->max_phys_descent = phys_descent;
19788 break;
19790 else if (new_x > it->first_visible_x)
19792 /* Increment number of glyphs actually displayed. */
19793 ++it->hpos;
19795 /* Record the maximum and minimum buffer positions
19796 seen so far in glyphs that will be displayed by
19797 this row. */
19798 if (it->bidi_p)
19799 RECORD_MAX_MIN_POS (it);
19801 if (x < it->first_visible_x)
19802 /* Glyph is partially visible, i.e. row starts at
19803 negative X position. */
19804 row->x = x - it->first_visible_x;
19806 else
19808 /* Glyph is completely off the left margin of the
19809 window. This should not happen because of the
19810 move_it_in_display_line at the start of this
19811 function, unless the text display area of the
19812 window is empty. */
19813 eassert (it->first_visible_x <= it->last_visible_x);
19816 /* Even if this display element produced no glyphs at all,
19817 we want to record its position. */
19818 if (it->bidi_p && nglyphs == 0)
19819 RECORD_MAX_MIN_POS (it);
19821 row->ascent = max (row->ascent, it->max_ascent);
19822 row->height = max (row->height, it->max_ascent + it->max_descent);
19823 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19824 row->phys_height = max (row->phys_height,
19825 it->max_phys_ascent + it->max_phys_descent);
19826 row->extra_line_spacing = max (row->extra_line_spacing,
19827 it->max_extra_line_spacing);
19829 /* End of this display line if row is continued. */
19830 if (row->continued_p || row->ends_at_zv_p)
19831 break;
19834 at_end_of_line:
19835 /* Is this a line end? If yes, we're also done, after making
19836 sure that a non-default face is extended up to the right
19837 margin of the window. */
19838 if (ITERATOR_AT_END_OF_LINE_P (it))
19840 int used_before = row->used[TEXT_AREA];
19842 row->ends_in_newline_from_string_p = STRINGP (it->object);
19844 /* Add a space at the end of the line that is used to
19845 display the cursor there. */
19846 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19847 append_space_for_newline (it, 0);
19849 /* Extend the face to the end of the line. */
19850 extend_face_to_end_of_line (it);
19852 /* Make sure we have the position. */
19853 if (used_before == 0)
19854 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19856 /* Record the position of the newline, for use in
19857 find_row_edges. */
19858 it->eol_pos = it->current.pos;
19860 /* Consume the line end. This skips over invisible lines. */
19861 set_iterator_to_next (it, 1);
19862 it->continuation_lines_width = 0;
19863 break;
19866 /* Proceed with next display element. Note that this skips
19867 over lines invisible because of selective display. */
19868 set_iterator_to_next (it, 1);
19870 /* If we truncate lines, we are done when the last displayed
19871 glyphs reach past the right margin of the window. */
19872 if (it->line_wrap == TRUNCATE
19873 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19874 ? (it->current_x >= it->last_visible_x)
19875 : (it->current_x > it->last_visible_x)))
19877 /* Maybe add truncation glyphs. */
19878 if (!FRAME_WINDOW_P (it->f)
19879 || (row->reversed_p
19880 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19881 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19883 int i, n;
19885 if (!row->reversed_p)
19887 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19888 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19889 break;
19891 else
19893 for (i = 0; i < row->used[TEXT_AREA]; i++)
19894 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19895 break;
19896 /* Remove any padding glyphs at the front of ROW, to
19897 make room for the truncation glyphs we will be
19898 adding below. The loop below always inserts at
19899 least one truncation glyph, so also remove the
19900 last glyph added to ROW. */
19901 unproduce_glyphs (it, i + 1);
19902 /* Adjust i for the loop below. */
19903 i = row->used[TEXT_AREA] - (i + 1);
19906 it->current_x = x_before;
19907 if (!FRAME_WINDOW_P (it->f))
19909 for (n = row->used[TEXT_AREA]; i < n; ++i)
19911 row->used[TEXT_AREA] = i;
19912 produce_special_glyphs (it, IT_TRUNCATION);
19915 else
19917 row->used[TEXT_AREA] = i;
19918 produce_special_glyphs (it, IT_TRUNCATION);
19921 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19923 /* Don't truncate if we can overflow newline into fringe. */
19924 if (!get_next_display_element (it))
19926 it->continuation_lines_width = 0;
19927 row->ends_at_zv_p = 1;
19928 row->exact_window_width_line_p = 1;
19929 break;
19931 if (ITERATOR_AT_END_OF_LINE_P (it))
19933 row->exact_window_width_line_p = 1;
19934 goto at_end_of_line;
19936 it->current_x = x_before;
19939 row->truncated_on_right_p = 1;
19940 it->continuation_lines_width = 0;
19941 reseat_at_next_visible_line_start (it, 0);
19942 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19943 it->hpos = hpos_before;
19944 break;
19948 if (wrap_data)
19949 bidi_unshelve_cache (wrap_data, 1);
19951 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19952 at the left window margin. */
19953 if (it->first_visible_x
19954 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19956 if (!FRAME_WINDOW_P (it->f)
19957 || (row->reversed_p
19958 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19959 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19960 insert_left_trunc_glyphs (it);
19961 row->truncated_on_left_p = 1;
19964 /* Remember the position at which this line ends.
19966 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19967 cannot be before the call to find_row_edges below, since that is
19968 where these positions are determined. */
19969 row->end = it->current;
19970 if (!it->bidi_p)
19972 row->minpos = row->start.pos;
19973 row->maxpos = row->end.pos;
19975 else
19977 /* ROW->minpos and ROW->maxpos must be the smallest and
19978 `1 + the largest' buffer positions in ROW. But if ROW was
19979 bidi-reordered, these two positions can be anywhere in the
19980 row, so we must determine them now. */
19981 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19984 /* If the start of this line is the overlay arrow-position, then
19985 mark this glyph row as the one containing the overlay arrow.
19986 This is clearly a mess with variable size fonts. It would be
19987 better to let it be displayed like cursors under X. */
19988 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19989 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19990 !NILP (overlay_arrow_string)))
19992 /* Overlay arrow in window redisplay is a fringe bitmap. */
19993 if (STRINGP (overlay_arrow_string))
19995 struct glyph_row *arrow_row
19996 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19997 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19998 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19999 struct glyph *p = row->glyphs[TEXT_AREA];
20000 struct glyph *p2, *end;
20002 /* Copy the arrow glyphs. */
20003 while (glyph < arrow_end)
20004 *p++ = *glyph++;
20006 /* Throw away padding glyphs. */
20007 p2 = p;
20008 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20009 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20010 ++p2;
20011 if (p2 > p)
20013 while (p2 < end)
20014 *p++ = *p2++;
20015 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20018 else
20020 eassert (INTEGERP (overlay_arrow_string));
20021 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20023 overlay_arrow_seen = 1;
20026 /* Highlight trailing whitespace. */
20027 if (!NILP (Vshow_trailing_whitespace))
20028 highlight_trailing_whitespace (it->f, it->glyph_row);
20030 /* Compute pixel dimensions of this line. */
20031 compute_line_metrics (it);
20033 /* Implementation note: No changes in the glyphs of ROW or in their
20034 faces can be done past this point, because compute_line_metrics
20035 computes ROW's hash value and stores it within the glyph_row
20036 structure. */
20038 /* Record whether this row ends inside an ellipsis. */
20039 row->ends_in_ellipsis_p
20040 = (it->method == GET_FROM_DISPLAY_VECTOR
20041 && it->ellipsis_p);
20043 /* Save fringe bitmaps in this row. */
20044 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20045 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20046 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20047 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20049 it->left_user_fringe_bitmap = 0;
20050 it->left_user_fringe_face_id = 0;
20051 it->right_user_fringe_bitmap = 0;
20052 it->right_user_fringe_face_id = 0;
20054 /* Maybe set the cursor. */
20055 cvpos = it->w->cursor.vpos;
20056 if ((cvpos < 0
20057 /* In bidi-reordered rows, keep checking for proper cursor
20058 position even if one has been found already, because buffer
20059 positions in such rows change non-linearly with ROW->VPOS,
20060 when a line is continued. One exception: when we are at ZV,
20061 display cursor on the first suitable glyph row, since all
20062 the empty rows after that also have their position set to ZV. */
20063 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20064 lines' rows is implemented for bidi-reordered rows. */
20065 || (it->bidi_p
20066 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20067 && PT >= MATRIX_ROW_START_CHARPOS (row)
20068 && PT <= MATRIX_ROW_END_CHARPOS (row)
20069 && cursor_row_p (row))
20070 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20072 /* Prepare for the next line. This line starts horizontally at (X
20073 HPOS) = (0 0). Vertical positions are incremented. As a
20074 convenience for the caller, IT->glyph_row is set to the next
20075 row to be used. */
20076 it->current_x = it->hpos = 0;
20077 it->current_y += row->height;
20078 SET_TEXT_POS (it->eol_pos, 0, 0);
20079 ++it->vpos;
20080 ++it->glyph_row;
20081 /* The next row should by default use the same value of the
20082 reversed_p flag as this one. set_iterator_to_next decides when
20083 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20084 the flag accordingly. */
20085 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20086 it->glyph_row->reversed_p = row->reversed_p;
20087 it->start = row->end;
20088 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20090 #undef RECORD_MAX_MIN_POS
20093 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20094 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20095 doc: /* Return paragraph direction at point in BUFFER.
20096 Value is either `left-to-right' or `right-to-left'.
20097 If BUFFER is omitted or nil, it defaults to the current buffer.
20099 Paragraph direction determines how the text in the paragraph is displayed.
20100 In left-to-right paragraphs, text begins at the left margin of the window
20101 and the reading direction is generally left to right. In right-to-left
20102 paragraphs, text begins at the right margin and is read from right to left.
20104 See also `bidi-paragraph-direction'. */)
20105 (Lisp_Object buffer)
20107 struct buffer *buf = current_buffer;
20108 struct buffer *old = buf;
20110 if (! NILP (buffer))
20112 CHECK_BUFFER (buffer);
20113 buf = XBUFFER (buffer);
20116 if (NILP (BVAR (buf, bidi_display_reordering))
20117 || NILP (BVAR (buf, enable_multibyte_characters))
20118 /* When we are loading loadup.el, the character property tables
20119 needed for bidi iteration are not yet available. */
20120 || !NILP (Vpurify_flag))
20121 return Qleft_to_right;
20122 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20123 return BVAR (buf, bidi_paragraph_direction);
20124 else
20126 /* Determine the direction from buffer text. We could try to
20127 use current_matrix if it is up to date, but this seems fast
20128 enough as it is. */
20129 struct bidi_it itb;
20130 ptrdiff_t pos = BUF_PT (buf);
20131 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20132 int c;
20133 void *itb_data = bidi_shelve_cache ();
20135 set_buffer_temp (buf);
20136 /* bidi_paragraph_init finds the base direction of the paragraph
20137 by searching forward from paragraph start. We need the base
20138 direction of the current or _previous_ paragraph, so we need
20139 to make sure we are within that paragraph. To that end, find
20140 the previous non-empty line. */
20141 if (pos >= ZV && pos > BEGV)
20142 DEC_BOTH (pos, bytepos);
20143 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20144 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20146 while ((c = FETCH_BYTE (bytepos)) == '\n'
20147 || c == ' ' || c == '\t' || c == '\f')
20149 if (bytepos <= BEGV_BYTE)
20150 break;
20151 bytepos--;
20152 pos--;
20154 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20155 bytepos--;
20157 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20158 itb.paragraph_dir = NEUTRAL_DIR;
20159 itb.string.s = NULL;
20160 itb.string.lstring = Qnil;
20161 itb.string.bufpos = 0;
20162 itb.string.unibyte = 0;
20163 /* We have no window to use here for ignoring window-specific
20164 overlays. Using NULL for window pointer will cause
20165 compute_display_string_pos to use the current buffer. */
20166 itb.w = NULL;
20167 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20168 bidi_unshelve_cache (itb_data, 0);
20169 set_buffer_temp (old);
20170 switch (itb.paragraph_dir)
20172 case L2R:
20173 return Qleft_to_right;
20174 break;
20175 case R2L:
20176 return Qright_to_left;
20177 break;
20178 default:
20179 emacs_abort ();
20184 DEFUN ("move-point-visually", Fmove_point_visually,
20185 Smove_point_visually, 1, 1, 0,
20186 doc: /* Move point in the visual order in the specified DIRECTION.
20187 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20188 left.
20190 Value is the new character position of point. */)
20191 (Lisp_Object direction)
20193 struct window *w = XWINDOW (selected_window);
20194 struct buffer *b = XBUFFER (w->contents);
20195 struct glyph_row *row;
20196 int dir;
20197 Lisp_Object paragraph_dir;
20199 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20200 (!(ROW)->continued_p \
20201 && INTEGERP ((GLYPH)->object) \
20202 && (GLYPH)->type == CHAR_GLYPH \
20203 && (GLYPH)->u.ch == ' ' \
20204 && (GLYPH)->charpos >= 0 \
20205 && !(GLYPH)->avoid_cursor_p)
20207 CHECK_NUMBER (direction);
20208 dir = XINT (direction);
20209 if (dir > 0)
20210 dir = 1;
20211 else
20212 dir = -1;
20214 /* If current matrix is up-to-date, we can use the information
20215 recorded in the glyphs, at least as long as the goal is on the
20216 screen. */
20217 if (w->window_end_valid
20218 && !windows_or_buffers_changed
20219 && b
20220 && !b->clip_changed
20221 && !b->prevent_redisplay_optimizations_p
20222 && !window_outdated (w)
20223 && w->cursor.vpos >= 0
20224 && w->cursor.vpos < w->current_matrix->nrows
20225 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20227 struct glyph *g = row->glyphs[TEXT_AREA];
20228 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20229 struct glyph *gpt = g + w->cursor.hpos;
20231 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20233 if (BUFFERP (g->object) && g->charpos != PT)
20235 SET_PT (g->charpos);
20236 w->cursor.vpos = -1;
20237 return make_number (PT);
20239 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20241 ptrdiff_t new_pos;
20243 if (BUFFERP (gpt->object))
20245 new_pos = PT;
20246 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20247 new_pos += (row->reversed_p ? -dir : dir);
20248 else
20249 new_pos -= (row->reversed_p ? -dir : dir);;
20251 else if (BUFFERP (g->object))
20252 new_pos = g->charpos;
20253 else
20254 break;
20255 SET_PT (new_pos);
20256 w->cursor.vpos = -1;
20257 return make_number (PT);
20259 else if (ROW_GLYPH_NEWLINE_P (row, g))
20261 /* Glyphs inserted at the end of a non-empty line for
20262 positioning the cursor have zero charpos, so we must
20263 deduce the value of point by other means. */
20264 if (g->charpos > 0)
20265 SET_PT (g->charpos);
20266 else if (row->ends_at_zv_p && PT != ZV)
20267 SET_PT (ZV);
20268 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20269 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20270 else
20271 break;
20272 w->cursor.vpos = -1;
20273 return make_number (PT);
20276 if (g == e || INTEGERP (g->object))
20278 if (row->truncated_on_left_p || row->truncated_on_right_p)
20279 goto simulate_display;
20280 if (!row->reversed_p)
20281 row += dir;
20282 else
20283 row -= dir;
20284 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20285 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20286 goto simulate_display;
20288 if (dir > 0)
20290 if (row->reversed_p && !row->continued_p)
20292 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20293 w->cursor.vpos = -1;
20294 return make_number (PT);
20296 g = row->glyphs[TEXT_AREA];
20297 e = g + row->used[TEXT_AREA];
20298 for ( ; g < e; g++)
20300 if (BUFFERP (g->object)
20301 /* Empty lines have only one glyph, which stands
20302 for the newline, and whose charpos is the
20303 buffer position of the newline. */
20304 || ROW_GLYPH_NEWLINE_P (row, g)
20305 /* When the buffer ends in a newline, the line at
20306 EOB also has one glyph, but its charpos is -1. */
20307 || (row->ends_at_zv_p
20308 && !row->reversed_p
20309 && INTEGERP (g->object)
20310 && g->type == CHAR_GLYPH
20311 && g->u.ch == ' '))
20313 if (g->charpos > 0)
20314 SET_PT (g->charpos);
20315 else if (!row->reversed_p
20316 && row->ends_at_zv_p
20317 && PT != ZV)
20318 SET_PT (ZV);
20319 else
20320 continue;
20321 w->cursor.vpos = -1;
20322 return make_number (PT);
20326 else
20328 if (!row->reversed_p && !row->continued_p)
20330 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20331 w->cursor.vpos = -1;
20332 return make_number (PT);
20334 e = row->glyphs[TEXT_AREA];
20335 g = e + row->used[TEXT_AREA] - 1;
20336 for ( ; g >= e; g--)
20338 if (BUFFERP (g->object)
20339 || (ROW_GLYPH_NEWLINE_P (row, g)
20340 && g->charpos > 0)
20341 /* Empty R2L lines on GUI frames have the buffer
20342 position of the newline stored in the stretch
20343 glyph. */
20344 || g->type == STRETCH_GLYPH
20345 || (row->ends_at_zv_p
20346 && row->reversed_p
20347 && INTEGERP (g->object)
20348 && g->type == CHAR_GLYPH
20349 && g->u.ch == ' '))
20351 if (g->charpos > 0)
20352 SET_PT (g->charpos);
20353 else if (row->reversed_p
20354 && row->ends_at_zv_p
20355 && PT != ZV)
20356 SET_PT (ZV);
20357 else
20358 continue;
20359 w->cursor.vpos = -1;
20360 return make_number (PT);
20367 simulate_display:
20369 /* If we wind up here, we failed to move by using the glyphs, so we
20370 need to simulate display instead. */
20372 if (b)
20373 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20374 else
20375 paragraph_dir = Qleft_to_right;
20376 if (EQ (paragraph_dir, Qright_to_left))
20377 dir = -dir;
20378 if (PT <= BEGV && dir < 0)
20379 xsignal0 (Qbeginning_of_buffer);
20380 else if (PT >= ZV && dir > 0)
20381 xsignal0 (Qend_of_buffer);
20382 else
20384 struct text_pos pt;
20385 struct it it;
20386 int pt_x, target_x, pixel_width, pt_vpos;
20387 bool at_eol_p;
20388 bool overshoot_expected = false;
20389 bool target_is_eol_p = false;
20391 /* Setup the arena. */
20392 SET_TEXT_POS (pt, PT, PT_BYTE);
20393 start_display (&it, w, pt);
20395 if (it.cmp_it.id < 0
20396 && it.method == GET_FROM_STRING
20397 && it.area == TEXT_AREA
20398 && it.string_from_display_prop_p
20399 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20400 overshoot_expected = true;
20402 /* Find the X coordinate of point. We start from the beginning
20403 of this or previous line to make sure we are before point in
20404 the logical order (since the move_it_* functions can only
20405 move forward). */
20406 reseat_at_previous_visible_line_start (&it);
20407 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20408 if (IT_CHARPOS (it) != PT)
20409 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20410 -1, -1, -1, MOVE_TO_POS);
20411 pt_x = it.current_x;
20412 pt_vpos = it.vpos;
20413 if (dir > 0 || overshoot_expected)
20415 struct glyph_row *row = it.glyph_row;
20417 /* When point is at beginning of line, we don't have
20418 information about the glyph there loaded into struct
20419 it. Calling get_next_display_element fixes that. */
20420 if (pt_x == 0)
20421 get_next_display_element (&it);
20422 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20423 it.glyph_row = NULL;
20424 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20425 it.glyph_row = row;
20426 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20427 it, lest it will become out of sync with it's buffer
20428 position. */
20429 it.current_x = pt_x;
20431 else
20432 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20433 pixel_width = it.pixel_width;
20434 if (overshoot_expected && at_eol_p)
20435 pixel_width = 0;
20436 else if (pixel_width <= 0)
20437 pixel_width = 1;
20439 /* If there's a display string at point, we are actually at the
20440 glyph to the left of point, so we need to correct the X
20441 coordinate. */
20442 if (overshoot_expected)
20443 pt_x += pixel_width;
20445 /* Compute target X coordinate, either to the left or to the
20446 right of point. On TTY frames, all characters have the same
20447 pixel width of 1, so we can use that. On GUI frames we don't
20448 have an easy way of getting at the pixel width of the
20449 character to the left of point, so we use a different method
20450 of getting to that place. */
20451 if (dir > 0)
20452 target_x = pt_x + pixel_width;
20453 else
20454 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20456 /* Target X coordinate could be one line above or below the line
20457 of point, in which case we need to adjust the target X
20458 coordinate. Also, if moving to the left, we need to begin at
20459 the left edge of the point's screen line. */
20460 if (dir < 0)
20462 if (pt_x > 0)
20464 start_display (&it, w, pt);
20465 reseat_at_previous_visible_line_start (&it);
20466 it.current_x = it.current_y = it.hpos = 0;
20467 if (pt_vpos != 0)
20468 move_it_by_lines (&it, pt_vpos);
20470 else
20472 move_it_by_lines (&it, -1);
20473 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20474 target_is_eol_p = true;
20477 else
20479 if (at_eol_p
20480 || (target_x >= it.last_visible_x
20481 && it.line_wrap != TRUNCATE))
20483 if (pt_x > 0)
20484 move_it_by_lines (&it, 0);
20485 move_it_by_lines (&it, 1);
20486 target_x = 0;
20490 /* Move to the target X coordinate. */
20491 #ifdef HAVE_WINDOW_SYSTEM
20492 /* On GUI frames, as we don't know the X coordinate of the
20493 character to the left of point, moving point to the left
20494 requires walking, one grapheme cluster at a time, until we
20495 find ourself at a place immediately to the left of the
20496 character at point. */
20497 if (FRAME_WINDOW_P (it.f) && dir < 0)
20499 struct text_pos new_pos = it.current.pos;
20500 enum move_it_result rc = MOVE_X_REACHED;
20502 while (it.current_x + it.pixel_width <= target_x
20503 && rc == MOVE_X_REACHED)
20505 int new_x = it.current_x + it.pixel_width;
20507 new_pos = it.current.pos;
20508 if (new_x == it.current_x)
20509 new_x++;
20510 rc = move_it_in_display_line_to (&it, ZV, new_x,
20511 MOVE_TO_POS | MOVE_TO_X);
20512 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20513 break;
20515 /* If we ended up on a composed character inside
20516 bidi-reordered text (e.g., Hebrew text with diacritics),
20517 the iterator gives us the buffer position of the last (in
20518 logical order) character of the composed grapheme cluster,
20519 which is not what we want. So we cheat: we compute the
20520 character position of the character that follows (in the
20521 logical order) the one where the above loop stopped. That
20522 character will appear on display to the left of point. */
20523 if (it.bidi_p
20524 && it.bidi_it.scan_dir == -1
20525 && new_pos.charpos - IT_CHARPOS (it) > 1)
20527 new_pos.charpos = IT_CHARPOS (it) + 1;
20528 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20530 it.current.pos = new_pos;
20532 else
20533 #endif
20534 if (it.current_x != target_x)
20535 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20537 /* When lines are truncated, the above loop will stop at the
20538 window edge. But we want to get to the end of line, even if
20539 it is beyond the window edge; automatic hscroll will then
20540 scroll the window to show point as appropriate. */
20541 if (target_is_eol_p && it.line_wrap == TRUNCATE
20542 && get_next_display_element (&it))
20544 struct text_pos new_pos = it.current.pos;
20546 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20548 set_iterator_to_next (&it, 0);
20549 if (it.method == GET_FROM_BUFFER)
20550 new_pos = it.current.pos;
20551 if (!get_next_display_element (&it))
20552 break;
20555 it.current.pos = new_pos;
20558 /* If we ended up in a display string that covers point, move to
20559 buffer position to the right in the visual order. */
20560 if (dir > 0)
20562 while (IT_CHARPOS (it) == PT)
20564 set_iterator_to_next (&it, 0);
20565 if (!get_next_display_element (&it))
20566 break;
20570 /* Move point to that position. */
20571 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20574 return make_number (PT);
20576 #undef ROW_GLYPH_NEWLINE_P
20580 /***********************************************************************
20581 Menu Bar
20582 ***********************************************************************/
20584 /* Redisplay the menu bar in the frame for window W.
20586 The menu bar of X frames that don't have X toolkit support is
20587 displayed in a special window W->frame->menu_bar_window.
20589 The menu bar of terminal frames is treated specially as far as
20590 glyph matrices are concerned. Menu bar lines are not part of
20591 windows, so the update is done directly on the frame matrix rows
20592 for the menu bar. */
20594 static void
20595 display_menu_bar (struct window *w)
20597 struct frame *f = XFRAME (WINDOW_FRAME (w));
20598 struct it it;
20599 Lisp_Object items;
20600 int i;
20602 /* Don't do all this for graphical frames. */
20603 #ifdef HAVE_NTGUI
20604 if (FRAME_W32_P (f))
20605 return;
20606 #endif
20607 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20608 if (FRAME_X_P (f))
20609 return;
20610 #endif
20612 #ifdef HAVE_NS
20613 if (FRAME_NS_P (f))
20614 return;
20615 #endif /* HAVE_NS */
20617 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20618 eassert (!FRAME_WINDOW_P (f));
20619 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20620 it.first_visible_x = 0;
20621 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20622 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20623 if (FRAME_WINDOW_P (f))
20625 /* Menu bar lines are displayed in the desired matrix of the
20626 dummy window menu_bar_window. */
20627 struct window *menu_w;
20628 menu_w = XWINDOW (f->menu_bar_window);
20629 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20630 MENU_FACE_ID);
20631 it.first_visible_x = 0;
20632 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20634 else
20635 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20637 /* This is a TTY frame, i.e. character hpos/vpos are used as
20638 pixel x/y. */
20639 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20640 MENU_FACE_ID);
20641 it.first_visible_x = 0;
20642 it.last_visible_x = FRAME_COLS (f);
20645 /* FIXME: This should be controlled by a user option. See the
20646 comments in redisplay_tool_bar and display_mode_line about
20647 this. */
20648 it.paragraph_embedding = L2R;
20650 /* Clear all rows of the menu bar. */
20651 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20653 struct glyph_row *row = it.glyph_row + i;
20654 clear_glyph_row (row);
20655 row->enabled_p = 1;
20656 row->full_width_p = 1;
20659 /* Display all items of the menu bar. */
20660 items = FRAME_MENU_BAR_ITEMS (it.f);
20661 for (i = 0; i < ASIZE (items); i += 4)
20663 Lisp_Object string;
20665 /* Stop at nil string. */
20666 string = AREF (items, i + 1);
20667 if (NILP (string))
20668 break;
20670 /* Remember where item was displayed. */
20671 ASET (items, i + 3, make_number (it.hpos));
20673 /* Display the item, pad with one space. */
20674 if (it.current_x < it.last_visible_x)
20675 display_string (NULL, string, Qnil, 0, 0, &it,
20676 SCHARS (string) + 1, 0, 0, -1);
20679 /* Fill out the line with spaces. */
20680 if (it.current_x < it.last_visible_x)
20681 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20683 /* Compute the total height of the lines. */
20684 compute_line_metrics (&it);
20689 /***********************************************************************
20690 Mode Line
20691 ***********************************************************************/
20693 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20694 FORCE is non-zero, redisplay mode lines unconditionally.
20695 Otherwise, redisplay only mode lines that are garbaged. Value is
20696 the number of windows whose mode lines were redisplayed. */
20698 static int
20699 redisplay_mode_lines (Lisp_Object window, int force)
20701 int nwindows = 0;
20703 while (!NILP (window))
20705 struct window *w = XWINDOW (window);
20707 if (WINDOWP (w->contents))
20708 nwindows += redisplay_mode_lines (w->contents, force);
20709 else if (force
20710 || FRAME_GARBAGED_P (XFRAME (w->frame))
20711 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20713 struct text_pos lpoint;
20714 struct buffer *old = current_buffer;
20716 /* Set the window's buffer for the mode line display. */
20717 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20718 set_buffer_internal_1 (XBUFFER (w->contents));
20720 /* Point refers normally to the selected window. For any
20721 other window, set up appropriate value. */
20722 if (!EQ (window, selected_window))
20724 struct text_pos pt;
20726 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20727 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20730 /* Display mode lines. */
20731 clear_glyph_matrix (w->desired_matrix);
20732 if (display_mode_lines (w))
20734 ++nwindows;
20735 w->must_be_updated_p = 1;
20738 /* Restore old settings. */
20739 set_buffer_internal_1 (old);
20740 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20743 window = w->next;
20746 return nwindows;
20750 /* Display the mode and/or header line of window W. Value is the
20751 sum number of mode lines and header lines displayed. */
20753 static int
20754 display_mode_lines (struct window *w)
20756 Lisp_Object old_selected_window = selected_window;
20757 Lisp_Object old_selected_frame = selected_frame;
20758 Lisp_Object new_frame = w->frame;
20759 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20760 int n = 0;
20762 selected_frame = new_frame;
20763 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20764 or window's point, then we'd need select_window_1 here as well. */
20765 XSETWINDOW (selected_window, w);
20766 XFRAME (new_frame)->selected_window = selected_window;
20768 /* These will be set while the mode line specs are processed. */
20769 line_number_displayed = 0;
20770 w->column_number_displayed = -1;
20772 if (WINDOW_WANTS_MODELINE_P (w))
20774 struct window *sel_w = XWINDOW (old_selected_window);
20776 /* Select mode line face based on the real selected window. */
20777 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20778 BVAR (current_buffer, mode_line_format));
20779 ++n;
20782 if (WINDOW_WANTS_HEADER_LINE_P (w))
20784 display_mode_line (w, HEADER_LINE_FACE_ID,
20785 BVAR (current_buffer, header_line_format));
20786 ++n;
20789 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20790 selected_frame = old_selected_frame;
20791 selected_window = old_selected_window;
20792 return n;
20796 /* Display mode or header line of window W. FACE_ID specifies which
20797 line to display; it is either MODE_LINE_FACE_ID or
20798 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20799 display. Value is the pixel height of the mode/header line
20800 displayed. */
20802 static int
20803 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20805 struct it it;
20806 struct face *face;
20807 ptrdiff_t count = SPECPDL_INDEX ();
20809 init_iterator (&it, w, -1, -1, NULL, face_id);
20810 /* Don't extend on a previously drawn mode-line.
20811 This may happen if called from pos_visible_p. */
20812 it.glyph_row->enabled_p = 0;
20813 prepare_desired_row (it.glyph_row);
20815 it.glyph_row->mode_line_p = 1;
20817 /* FIXME: This should be controlled by a user option. But
20818 supporting such an option is not trivial, since the mode line is
20819 made up of many separate strings. */
20820 it.paragraph_embedding = L2R;
20822 record_unwind_protect (unwind_format_mode_line,
20823 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20825 mode_line_target = MODE_LINE_DISPLAY;
20827 /* Temporarily make frame's keyboard the current kboard so that
20828 kboard-local variables in the mode_line_format will get the right
20829 values. */
20830 push_kboard (FRAME_KBOARD (it.f));
20831 record_unwind_save_match_data ();
20832 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20833 pop_kboard ();
20835 unbind_to (count, Qnil);
20837 /* Fill up with spaces. */
20838 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20840 compute_line_metrics (&it);
20841 it.glyph_row->full_width_p = 1;
20842 it.glyph_row->continued_p = 0;
20843 it.glyph_row->truncated_on_left_p = 0;
20844 it.glyph_row->truncated_on_right_p = 0;
20846 /* Make a 3D mode-line have a shadow at its right end. */
20847 face = FACE_FROM_ID (it.f, face_id);
20848 extend_face_to_end_of_line (&it);
20849 if (face->box != FACE_NO_BOX)
20851 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20852 + it.glyph_row->used[TEXT_AREA] - 1);
20853 last->right_box_line_p = 1;
20856 return it.glyph_row->height;
20859 /* Move element ELT in LIST to the front of LIST.
20860 Return the updated list. */
20862 static Lisp_Object
20863 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20865 register Lisp_Object tail, prev;
20866 register Lisp_Object tem;
20868 tail = list;
20869 prev = Qnil;
20870 while (CONSP (tail))
20872 tem = XCAR (tail);
20874 if (EQ (elt, tem))
20876 /* Splice out the link TAIL. */
20877 if (NILP (prev))
20878 list = XCDR (tail);
20879 else
20880 Fsetcdr (prev, XCDR (tail));
20882 /* Now make it the first. */
20883 Fsetcdr (tail, list);
20884 return tail;
20886 else
20887 prev = tail;
20888 tail = XCDR (tail);
20889 QUIT;
20892 /* Not found--return unchanged LIST. */
20893 return list;
20896 /* Contribute ELT to the mode line for window IT->w. How it
20897 translates into text depends on its data type.
20899 IT describes the display environment in which we display, as usual.
20901 DEPTH is the depth in recursion. It is used to prevent
20902 infinite recursion here.
20904 FIELD_WIDTH is the number of characters the display of ELT should
20905 occupy in the mode line, and PRECISION is the maximum number of
20906 characters to display from ELT's representation. See
20907 display_string for details.
20909 Returns the hpos of the end of the text generated by ELT.
20911 PROPS is a property list to add to any string we encounter.
20913 If RISKY is nonzero, remove (disregard) any properties in any string
20914 we encounter, and ignore :eval and :propertize.
20916 The global variable `mode_line_target' determines whether the
20917 output is passed to `store_mode_line_noprop',
20918 `store_mode_line_string', or `display_string'. */
20920 static int
20921 display_mode_element (struct it *it, int depth, int field_width, int precision,
20922 Lisp_Object elt, Lisp_Object props, int risky)
20924 int n = 0, field, prec;
20925 int literal = 0;
20927 tail_recurse:
20928 if (depth > 100)
20929 elt = build_string ("*too-deep*");
20931 depth++;
20933 switch (XTYPE (elt))
20935 case Lisp_String:
20937 /* A string: output it and check for %-constructs within it. */
20938 unsigned char c;
20939 ptrdiff_t offset = 0;
20941 if (SCHARS (elt) > 0
20942 && (!NILP (props) || risky))
20944 Lisp_Object oprops, aelt;
20945 oprops = Ftext_properties_at (make_number (0), elt);
20947 /* If the starting string's properties are not what
20948 we want, translate the string. Also, if the string
20949 is risky, do that anyway. */
20951 if (NILP (Fequal (props, oprops)) || risky)
20953 /* If the starting string has properties,
20954 merge the specified ones onto the existing ones. */
20955 if (! NILP (oprops) && !risky)
20957 Lisp_Object tem;
20959 oprops = Fcopy_sequence (oprops);
20960 tem = props;
20961 while (CONSP (tem))
20963 oprops = Fplist_put (oprops, XCAR (tem),
20964 XCAR (XCDR (tem)));
20965 tem = XCDR (XCDR (tem));
20967 props = oprops;
20970 aelt = Fassoc (elt, mode_line_proptrans_alist);
20971 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20973 /* AELT is what we want. Move it to the front
20974 without consing. */
20975 elt = XCAR (aelt);
20976 mode_line_proptrans_alist
20977 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20979 else
20981 Lisp_Object tem;
20983 /* If AELT has the wrong props, it is useless.
20984 so get rid of it. */
20985 if (! NILP (aelt))
20986 mode_line_proptrans_alist
20987 = Fdelq (aelt, mode_line_proptrans_alist);
20989 elt = Fcopy_sequence (elt);
20990 Fset_text_properties (make_number (0), Flength (elt),
20991 props, elt);
20992 /* Add this item to mode_line_proptrans_alist. */
20993 mode_line_proptrans_alist
20994 = Fcons (Fcons (elt, props),
20995 mode_line_proptrans_alist);
20996 /* Truncate mode_line_proptrans_alist
20997 to at most 50 elements. */
20998 tem = Fnthcdr (make_number (50),
20999 mode_line_proptrans_alist);
21000 if (! NILP (tem))
21001 XSETCDR (tem, Qnil);
21006 offset = 0;
21008 if (literal)
21010 prec = precision - n;
21011 switch (mode_line_target)
21013 case MODE_LINE_NOPROP:
21014 case MODE_LINE_TITLE:
21015 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21016 break;
21017 case MODE_LINE_STRING:
21018 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21019 break;
21020 case MODE_LINE_DISPLAY:
21021 n += display_string (NULL, elt, Qnil, 0, 0, it,
21022 0, prec, 0, STRING_MULTIBYTE (elt));
21023 break;
21026 break;
21029 /* Handle the non-literal case. */
21031 while ((precision <= 0 || n < precision)
21032 && SREF (elt, offset) != 0
21033 && (mode_line_target != MODE_LINE_DISPLAY
21034 || it->current_x < it->last_visible_x))
21036 ptrdiff_t last_offset = offset;
21038 /* Advance to end of string or next format specifier. */
21039 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21042 if (offset - 1 != last_offset)
21044 ptrdiff_t nchars, nbytes;
21046 /* Output to end of string or up to '%'. Field width
21047 is length of string. Don't output more than
21048 PRECISION allows us. */
21049 offset--;
21051 prec = c_string_width (SDATA (elt) + last_offset,
21052 offset - last_offset, precision - n,
21053 &nchars, &nbytes);
21055 switch (mode_line_target)
21057 case MODE_LINE_NOPROP:
21058 case MODE_LINE_TITLE:
21059 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21060 break;
21061 case MODE_LINE_STRING:
21063 ptrdiff_t bytepos = last_offset;
21064 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21065 ptrdiff_t endpos = (precision <= 0
21066 ? string_byte_to_char (elt, offset)
21067 : charpos + nchars);
21069 n += store_mode_line_string (NULL,
21070 Fsubstring (elt, make_number (charpos),
21071 make_number (endpos)),
21072 0, 0, 0, Qnil);
21074 break;
21075 case MODE_LINE_DISPLAY:
21077 ptrdiff_t bytepos = last_offset;
21078 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21080 if (precision <= 0)
21081 nchars = string_byte_to_char (elt, offset) - charpos;
21082 n += display_string (NULL, elt, Qnil, 0, charpos,
21083 it, 0, nchars, 0,
21084 STRING_MULTIBYTE (elt));
21086 break;
21089 else /* c == '%' */
21091 ptrdiff_t percent_position = offset;
21093 /* Get the specified minimum width. Zero means
21094 don't pad. */
21095 field = 0;
21096 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21097 field = field * 10 + c - '0';
21099 /* Don't pad beyond the total padding allowed. */
21100 if (field_width - n > 0 && field > field_width - n)
21101 field = field_width - n;
21103 /* Note that either PRECISION <= 0 or N < PRECISION. */
21104 prec = precision - n;
21106 if (c == 'M')
21107 n += display_mode_element (it, depth, field, prec,
21108 Vglobal_mode_string, props,
21109 risky);
21110 else if (c != 0)
21112 bool multibyte;
21113 ptrdiff_t bytepos, charpos;
21114 const char *spec;
21115 Lisp_Object string;
21117 bytepos = percent_position;
21118 charpos = (STRING_MULTIBYTE (elt)
21119 ? string_byte_to_char (elt, bytepos)
21120 : bytepos);
21121 spec = decode_mode_spec (it->w, c, field, &string);
21122 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21124 switch (mode_line_target)
21126 case MODE_LINE_NOPROP:
21127 case MODE_LINE_TITLE:
21128 n += store_mode_line_noprop (spec, field, prec);
21129 break;
21130 case MODE_LINE_STRING:
21132 Lisp_Object tem = build_string (spec);
21133 props = Ftext_properties_at (make_number (charpos), elt);
21134 /* Should only keep face property in props */
21135 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21137 break;
21138 case MODE_LINE_DISPLAY:
21140 int nglyphs_before, nwritten;
21142 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21143 nwritten = display_string (spec, string, elt,
21144 charpos, 0, it,
21145 field, prec, 0,
21146 multibyte);
21148 /* Assign to the glyphs written above the
21149 string where the `%x' came from, position
21150 of the `%'. */
21151 if (nwritten > 0)
21153 struct glyph *glyph
21154 = (it->glyph_row->glyphs[TEXT_AREA]
21155 + nglyphs_before);
21156 int i;
21158 for (i = 0; i < nwritten; ++i)
21160 glyph[i].object = elt;
21161 glyph[i].charpos = charpos;
21164 n += nwritten;
21167 break;
21170 else /* c == 0 */
21171 break;
21175 break;
21177 case Lisp_Symbol:
21178 /* A symbol: process the value of the symbol recursively
21179 as if it appeared here directly. Avoid error if symbol void.
21180 Special case: if value of symbol is a string, output the string
21181 literally. */
21183 register Lisp_Object tem;
21185 /* If the variable is not marked as risky to set
21186 then its contents are risky to use. */
21187 if (NILP (Fget (elt, Qrisky_local_variable)))
21188 risky = 1;
21190 tem = Fboundp (elt);
21191 if (!NILP (tem))
21193 tem = Fsymbol_value (elt);
21194 /* If value is a string, output that string literally:
21195 don't check for % within it. */
21196 if (STRINGP (tem))
21197 literal = 1;
21199 if (!EQ (tem, elt))
21201 /* Give up right away for nil or t. */
21202 elt = tem;
21203 goto tail_recurse;
21207 break;
21209 case Lisp_Cons:
21211 register Lisp_Object car, tem;
21213 /* A cons cell: five distinct cases.
21214 If first element is :eval or :propertize, do something special.
21215 If first element is a string or a cons, process all the elements
21216 and effectively concatenate them.
21217 If first element is a negative number, truncate displaying cdr to
21218 at most that many characters. If positive, pad (with spaces)
21219 to at least that many characters.
21220 If first element is a symbol, process the cadr or caddr recursively
21221 according to whether the symbol's value is non-nil or nil. */
21222 car = XCAR (elt);
21223 if (EQ (car, QCeval))
21225 /* An element of the form (:eval FORM) means evaluate FORM
21226 and use the result as mode line elements. */
21228 if (risky)
21229 break;
21231 if (CONSP (XCDR (elt)))
21233 Lisp_Object spec;
21234 spec = safe_eval (XCAR (XCDR (elt)));
21235 n += display_mode_element (it, depth, field_width - n,
21236 precision - n, spec, props,
21237 risky);
21240 else if (EQ (car, QCpropertize))
21242 /* An element of the form (:propertize ELT PROPS...)
21243 means display ELT but applying properties PROPS. */
21245 if (risky)
21246 break;
21248 if (CONSP (XCDR (elt)))
21249 n += display_mode_element (it, depth, field_width - n,
21250 precision - n, XCAR (XCDR (elt)),
21251 XCDR (XCDR (elt)), risky);
21253 else if (SYMBOLP (car))
21255 tem = Fboundp (car);
21256 elt = XCDR (elt);
21257 if (!CONSP (elt))
21258 goto invalid;
21259 /* elt is now the cdr, and we know it is a cons cell.
21260 Use its car if CAR has a non-nil value. */
21261 if (!NILP (tem))
21263 tem = Fsymbol_value (car);
21264 if (!NILP (tem))
21266 elt = XCAR (elt);
21267 goto tail_recurse;
21270 /* Symbol's value is nil (or symbol is unbound)
21271 Get the cddr of the original list
21272 and if possible find the caddr and use that. */
21273 elt = XCDR (elt);
21274 if (NILP (elt))
21275 break;
21276 else if (!CONSP (elt))
21277 goto invalid;
21278 elt = XCAR (elt);
21279 goto tail_recurse;
21281 else if (INTEGERP (car))
21283 register int lim = XINT (car);
21284 elt = XCDR (elt);
21285 if (lim < 0)
21287 /* Negative int means reduce maximum width. */
21288 if (precision <= 0)
21289 precision = -lim;
21290 else
21291 precision = min (precision, -lim);
21293 else if (lim > 0)
21295 /* Padding specified. Don't let it be more than
21296 current maximum. */
21297 if (precision > 0)
21298 lim = min (precision, lim);
21300 /* If that's more padding than already wanted, queue it.
21301 But don't reduce padding already specified even if
21302 that is beyond the current truncation point. */
21303 field_width = max (lim, field_width);
21305 goto tail_recurse;
21307 else if (STRINGP (car) || CONSP (car))
21309 Lisp_Object halftail = elt;
21310 int len = 0;
21312 while (CONSP (elt)
21313 && (precision <= 0 || n < precision))
21315 n += display_mode_element (it, depth,
21316 /* Do padding only after the last
21317 element in the list. */
21318 (! CONSP (XCDR (elt))
21319 ? field_width - n
21320 : 0),
21321 precision - n, XCAR (elt),
21322 props, risky);
21323 elt = XCDR (elt);
21324 len++;
21325 if ((len & 1) == 0)
21326 halftail = XCDR (halftail);
21327 /* Check for cycle. */
21328 if (EQ (halftail, elt))
21329 break;
21333 break;
21335 default:
21336 invalid:
21337 elt = build_string ("*invalid*");
21338 goto tail_recurse;
21341 /* Pad to FIELD_WIDTH. */
21342 if (field_width > 0 && n < field_width)
21344 switch (mode_line_target)
21346 case MODE_LINE_NOPROP:
21347 case MODE_LINE_TITLE:
21348 n += store_mode_line_noprop ("", field_width - n, 0);
21349 break;
21350 case MODE_LINE_STRING:
21351 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21352 break;
21353 case MODE_LINE_DISPLAY:
21354 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21355 0, 0, 0);
21356 break;
21360 return n;
21363 /* Store a mode-line string element in mode_line_string_list.
21365 If STRING is non-null, display that C string. Otherwise, the Lisp
21366 string LISP_STRING is displayed.
21368 FIELD_WIDTH is the minimum number of output glyphs to produce.
21369 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21370 with spaces. FIELD_WIDTH <= 0 means don't pad.
21372 PRECISION is the maximum number of characters to output from
21373 STRING. PRECISION <= 0 means don't truncate the string.
21375 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21376 properties to the string.
21378 PROPS are the properties to add to the string.
21379 The mode_line_string_face face property is always added to the string.
21382 static int
21383 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21384 int field_width, int precision, Lisp_Object props)
21386 ptrdiff_t len;
21387 int n = 0;
21389 if (string != NULL)
21391 len = strlen (string);
21392 if (precision > 0 && len > precision)
21393 len = precision;
21394 lisp_string = make_string (string, len);
21395 if (NILP (props))
21396 props = mode_line_string_face_prop;
21397 else if (!NILP (mode_line_string_face))
21399 Lisp_Object face = Fplist_get (props, Qface);
21400 props = Fcopy_sequence (props);
21401 if (NILP (face))
21402 face = mode_line_string_face;
21403 else
21404 face = list2 (face, mode_line_string_face);
21405 props = Fplist_put (props, Qface, face);
21407 Fadd_text_properties (make_number (0), make_number (len),
21408 props, lisp_string);
21410 else
21412 len = XFASTINT (Flength (lisp_string));
21413 if (precision > 0 && len > precision)
21415 len = precision;
21416 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21417 precision = -1;
21419 if (!NILP (mode_line_string_face))
21421 Lisp_Object face;
21422 if (NILP (props))
21423 props = Ftext_properties_at (make_number (0), lisp_string);
21424 face = Fplist_get (props, Qface);
21425 if (NILP (face))
21426 face = mode_line_string_face;
21427 else
21428 face = list2 (face, mode_line_string_face);
21429 props = list2 (Qface, face);
21430 if (copy_string)
21431 lisp_string = Fcopy_sequence (lisp_string);
21433 if (!NILP (props))
21434 Fadd_text_properties (make_number (0), make_number (len),
21435 props, lisp_string);
21438 if (len > 0)
21440 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21441 n += len;
21444 if (field_width > len)
21446 field_width -= len;
21447 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21448 if (!NILP (props))
21449 Fadd_text_properties (make_number (0), make_number (field_width),
21450 props, lisp_string);
21451 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21452 n += field_width;
21455 return n;
21459 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21460 1, 4, 0,
21461 doc: /* Format a string out of a mode line format specification.
21462 First arg FORMAT specifies the mode line format (see `mode-line-format'
21463 for details) to use.
21465 By default, the format is evaluated for the currently selected window.
21467 Optional second arg FACE specifies the face property to put on all
21468 characters for which no face is specified. The value nil means the
21469 default face. The value t means whatever face the window's mode line
21470 currently uses (either `mode-line' or `mode-line-inactive',
21471 depending on whether the window is the selected window or not).
21472 An integer value means the value string has no text
21473 properties.
21475 Optional third and fourth args WINDOW and BUFFER specify the window
21476 and buffer to use as the context for the formatting (defaults
21477 are the selected window and the WINDOW's buffer). */)
21478 (Lisp_Object format, Lisp_Object face,
21479 Lisp_Object window, Lisp_Object buffer)
21481 struct it it;
21482 int len;
21483 struct window *w;
21484 struct buffer *old_buffer = NULL;
21485 int face_id;
21486 int no_props = INTEGERP (face);
21487 ptrdiff_t count = SPECPDL_INDEX ();
21488 Lisp_Object str;
21489 int string_start = 0;
21491 w = decode_any_window (window);
21492 XSETWINDOW (window, w);
21494 if (NILP (buffer))
21495 buffer = w->contents;
21496 CHECK_BUFFER (buffer);
21498 /* Make formatting the modeline a non-op when noninteractive, otherwise
21499 there will be problems later caused by a partially initialized frame. */
21500 if (NILP (format) || noninteractive)
21501 return empty_unibyte_string;
21503 if (no_props)
21504 face = Qnil;
21506 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21507 : EQ (face, Qt) ? (EQ (window, selected_window)
21508 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21509 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21510 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21511 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21512 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21513 : DEFAULT_FACE_ID;
21515 old_buffer = current_buffer;
21517 /* Save things including mode_line_proptrans_alist,
21518 and set that to nil so that we don't alter the outer value. */
21519 record_unwind_protect (unwind_format_mode_line,
21520 format_mode_line_unwind_data
21521 (XFRAME (WINDOW_FRAME (w)),
21522 old_buffer, selected_window, 1));
21523 mode_line_proptrans_alist = Qnil;
21525 Fselect_window (window, Qt);
21526 set_buffer_internal_1 (XBUFFER (buffer));
21528 init_iterator (&it, w, -1, -1, NULL, face_id);
21530 if (no_props)
21532 mode_line_target = MODE_LINE_NOPROP;
21533 mode_line_string_face_prop = Qnil;
21534 mode_line_string_list = Qnil;
21535 string_start = MODE_LINE_NOPROP_LEN (0);
21537 else
21539 mode_line_target = MODE_LINE_STRING;
21540 mode_line_string_list = Qnil;
21541 mode_line_string_face = face;
21542 mode_line_string_face_prop
21543 = NILP (face) ? Qnil : list2 (Qface, face);
21546 push_kboard (FRAME_KBOARD (it.f));
21547 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21548 pop_kboard ();
21550 if (no_props)
21552 len = MODE_LINE_NOPROP_LEN (string_start);
21553 str = make_string (mode_line_noprop_buf + string_start, len);
21555 else
21557 mode_line_string_list = Fnreverse (mode_line_string_list);
21558 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21559 empty_unibyte_string);
21562 unbind_to (count, Qnil);
21563 return str;
21566 /* Write a null-terminated, right justified decimal representation of
21567 the positive integer D to BUF using a minimal field width WIDTH. */
21569 static void
21570 pint2str (register char *buf, register int width, register ptrdiff_t d)
21572 register char *p = buf;
21574 if (d <= 0)
21575 *p++ = '0';
21576 else
21578 while (d > 0)
21580 *p++ = d % 10 + '0';
21581 d /= 10;
21585 for (width -= (int) (p - buf); width > 0; --width)
21586 *p++ = ' ';
21587 *p-- = '\0';
21588 while (p > buf)
21590 d = *buf;
21591 *buf++ = *p;
21592 *p-- = d;
21596 /* Write a null-terminated, right justified decimal and "human
21597 readable" representation of the nonnegative integer D to BUF using
21598 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21600 static const char power_letter[] =
21602 0, /* no letter */
21603 'k', /* kilo */
21604 'M', /* mega */
21605 'G', /* giga */
21606 'T', /* tera */
21607 'P', /* peta */
21608 'E', /* exa */
21609 'Z', /* zetta */
21610 'Y' /* yotta */
21613 static void
21614 pint2hrstr (char *buf, int width, ptrdiff_t d)
21616 /* We aim to represent the nonnegative integer D as
21617 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21618 ptrdiff_t quotient = d;
21619 int remainder = 0;
21620 /* -1 means: do not use TENTHS. */
21621 int tenths = -1;
21622 int exponent = 0;
21624 /* Length of QUOTIENT.TENTHS as a string. */
21625 int length;
21627 char * psuffix;
21628 char * p;
21630 if (quotient >= 1000)
21632 /* Scale to the appropriate EXPONENT. */
21635 remainder = quotient % 1000;
21636 quotient /= 1000;
21637 exponent++;
21639 while (quotient >= 1000);
21641 /* Round to nearest and decide whether to use TENTHS or not. */
21642 if (quotient <= 9)
21644 tenths = remainder / 100;
21645 if (remainder % 100 >= 50)
21647 if (tenths < 9)
21648 tenths++;
21649 else
21651 quotient++;
21652 if (quotient == 10)
21653 tenths = -1;
21654 else
21655 tenths = 0;
21659 else
21660 if (remainder >= 500)
21662 if (quotient < 999)
21663 quotient++;
21664 else
21666 quotient = 1;
21667 exponent++;
21668 tenths = 0;
21673 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21674 if (tenths == -1 && quotient <= 99)
21675 if (quotient <= 9)
21676 length = 1;
21677 else
21678 length = 2;
21679 else
21680 length = 3;
21681 p = psuffix = buf + max (width, length);
21683 /* Print EXPONENT. */
21684 *psuffix++ = power_letter[exponent];
21685 *psuffix = '\0';
21687 /* Print TENTHS. */
21688 if (tenths >= 0)
21690 *--p = '0' + tenths;
21691 *--p = '.';
21694 /* Print QUOTIENT. */
21697 int digit = quotient % 10;
21698 *--p = '0' + digit;
21700 while ((quotient /= 10) != 0);
21702 /* Print leading spaces. */
21703 while (buf < p)
21704 *--p = ' ';
21707 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21708 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21709 type of CODING_SYSTEM. Return updated pointer into BUF. */
21711 static unsigned char invalid_eol_type[] = "(*invalid*)";
21713 static char *
21714 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21716 Lisp_Object val;
21717 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21718 const unsigned char *eol_str;
21719 int eol_str_len;
21720 /* The EOL conversion we are using. */
21721 Lisp_Object eoltype;
21723 val = CODING_SYSTEM_SPEC (coding_system);
21724 eoltype = Qnil;
21726 if (!VECTORP (val)) /* Not yet decided. */
21728 *buf++ = multibyte ? '-' : ' ';
21729 if (eol_flag)
21730 eoltype = eol_mnemonic_undecided;
21731 /* Don't mention EOL conversion if it isn't decided. */
21733 else
21735 Lisp_Object attrs;
21736 Lisp_Object eolvalue;
21738 attrs = AREF (val, 0);
21739 eolvalue = AREF (val, 2);
21741 *buf++ = multibyte
21742 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21743 : ' ';
21745 if (eol_flag)
21747 /* The EOL conversion that is normal on this system. */
21749 if (NILP (eolvalue)) /* Not yet decided. */
21750 eoltype = eol_mnemonic_undecided;
21751 else if (VECTORP (eolvalue)) /* Not yet decided. */
21752 eoltype = eol_mnemonic_undecided;
21753 else /* eolvalue is Qunix, Qdos, or Qmac. */
21754 eoltype = (EQ (eolvalue, Qunix)
21755 ? eol_mnemonic_unix
21756 : (EQ (eolvalue, Qdos) == 1
21757 ? eol_mnemonic_dos : eol_mnemonic_mac));
21761 if (eol_flag)
21763 /* Mention the EOL conversion if it is not the usual one. */
21764 if (STRINGP (eoltype))
21766 eol_str = SDATA (eoltype);
21767 eol_str_len = SBYTES (eoltype);
21769 else if (CHARACTERP (eoltype))
21771 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21772 int c = XFASTINT (eoltype);
21773 eol_str_len = CHAR_STRING (c, tmp);
21774 eol_str = tmp;
21776 else
21778 eol_str = invalid_eol_type;
21779 eol_str_len = sizeof (invalid_eol_type) - 1;
21781 memcpy (buf, eol_str, eol_str_len);
21782 buf += eol_str_len;
21785 return buf;
21788 /* Return a string for the output of a mode line %-spec for window W,
21789 generated by character C. FIELD_WIDTH > 0 means pad the string
21790 returned with spaces to that value. Return a Lisp string in
21791 *STRING if the resulting string is taken from that Lisp string.
21793 Note we operate on the current buffer for most purposes. */
21795 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21797 static const char *
21798 decode_mode_spec (struct window *w, register int c, int field_width,
21799 Lisp_Object *string)
21801 Lisp_Object obj;
21802 struct frame *f = XFRAME (WINDOW_FRAME (w));
21803 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21804 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21805 produce strings from numerical values, so limit preposterously
21806 large values of FIELD_WIDTH to avoid overrunning the buffer's
21807 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21808 bytes plus the terminating null. */
21809 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21810 struct buffer *b = current_buffer;
21812 obj = Qnil;
21813 *string = Qnil;
21815 switch (c)
21817 case '*':
21818 if (!NILP (BVAR (b, read_only)))
21819 return "%";
21820 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21821 return "*";
21822 return "-";
21824 case '+':
21825 /* This differs from %* only for a modified read-only buffer. */
21826 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21827 return "*";
21828 if (!NILP (BVAR (b, read_only)))
21829 return "%";
21830 return "-";
21832 case '&':
21833 /* This differs from %* in ignoring read-only-ness. */
21834 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21835 return "*";
21836 return "-";
21838 case '%':
21839 return "%";
21841 case '[':
21843 int i;
21844 char *p;
21846 if (command_loop_level > 5)
21847 return "[[[... ";
21848 p = decode_mode_spec_buf;
21849 for (i = 0; i < command_loop_level; i++)
21850 *p++ = '[';
21851 *p = 0;
21852 return decode_mode_spec_buf;
21855 case ']':
21857 int i;
21858 char *p;
21860 if (command_loop_level > 5)
21861 return " ...]]]";
21862 p = decode_mode_spec_buf;
21863 for (i = 0; i < command_loop_level; i++)
21864 *p++ = ']';
21865 *p = 0;
21866 return decode_mode_spec_buf;
21869 case '-':
21871 register int i;
21873 /* Let lots_of_dashes be a string of infinite length. */
21874 if (mode_line_target == MODE_LINE_NOPROP
21875 || mode_line_target == MODE_LINE_STRING)
21876 return "--";
21877 if (field_width <= 0
21878 || field_width > sizeof (lots_of_dashes))
21880 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21881 decode_mode_spec_buf[i] = '-';
21882 decode_mode_spec_buf[i] = '\0';
21883 return decode_mode_spec_buf;
21885 else
21886 return lots_of_dashes;
21889 case 'b':
21890 obj = BVAR (b, name);
21891 break;
21893 case 'c':
21894 /* %c and %l are ignored in `frame-title-format'.
21895 (In redisplay_internal, the frame title is drawn _before_ the
21896 windows are updated, so the stuff which depends on actual
21897 window contents (such as %l) may fail to render properly, or
21898 even crash emacs.) */
21899 if (mode_line_target == MODE_LINE_TITLE)
21900 return "";
21901 else
21903 ptrdiff_t col = current_column ();
21904 w->column_number_displayed = col;
21905 pint2str (decode_mode_spec_buf, width, col);
21906 return decode_mode_spec_buf;
21909 case 'e':
21910 #ifndef SYSTEM_MALLOC
21912 if (NILP (Vmemory_full))
21913 return "";
21914 else
21915 return "!MEM FULL! ";
21917 #else
21918 return "";
21919 #endif
21921 case 'F':
21922 /* %F displays the frame name. */
21923 if (!NILP (f->title))
21924 return SSDATA (f->title);
21925 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21926 return SSDATA (f->name);
21927 return "Emacs";
21929 case 'f':
21930 obj = BVAR (b, filename);
21931 break;
21933 case 'i':
21935 ptrdiff_t size = ZV - BEGV;
21936 pint2str (decode_mode_spec_buf, width, size);
21937 return decode_mode_spec_buf;
21940 case 'I':
21942 ptrdiff_t size = ZV - BEGV;
21943 pint2hrstr (decode_mode_spec_buf, width, size);
21944 return decode_mode_spec_buf;
21947 case 'l':
21949 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21950 ptrdiff_t topline, nlines, height;
21951 ptrdiff_t junk;
21953 /* %c and %l are ignored in `frame-title-format'. */
21954 if (mode_line_target == MODE_LINE_TITLE)
21955 return "";
21957 startpos = marker_position (w->start);
21958 startpos_byte = marker_byte_position (w->start);
21959 height = WINDOW_TOTAL_LINES (w);
21961 /* If we decided that this buffer isn't suitable for line numbers,
21962 don't forget that too fast. */
21963 if (w->base_line_pos == -1)
21964 goto no_value;
21966 /* If the buffer is very big, don't waste time. */
21967 if (INTEGERP (Vline_number_display_limit)
21968 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21970 w->base_line_pos = 0;
21971 w->base_line_number = 0;
21972 goto no_value;
21975 if (w->base_line_number > 0
21976 && w->base_line_pos > 0
21977 && w->base_line_pos <= startpos)
21979 line = w->base_line_number;
21980 linepos = w->base_line_pos;
21981 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21983 else
21985 line = 1;
21986 linepos = BUF_BEGV (b);
21987 linepos_byte = BUF_BEGV_BYTE (b);
21990 /* Count lines from base line to window start position. */
21991 nlines = display_count_lines (linepos_byte,
21992 startpos_byte,
21993 startpos, &junk);
21995 topline = nlines + line;
21997 /* Determine a new base line, if the old one is too close
21998 or too far away, or if we did not have one.
21999 "Too close" means it's plausible a scroll-down would
22000 go back past it. */
22001 if (startpos == BUF_BEGV (b))
22003 w->base_line_number = topline;
22004 w->base_line_pos = BUF_BEGV (b);
22006 else if (nlines < height + 25 || nlines > height * 3 + 50
22007 || linepos == BUF_BEGV (b))
22009 ptrdiff_t limit = BUF_BEGV (b);
22010 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22011 ptrdiff_t position;
22012 ptrdiff_t distance =
22013 (height * 2 + 30) * line_number_display_limit_width;
22015 if (startpos - distance > limit)
22017 limit = startpos - distance;
22018 limit_byte = CHAR_TO_BYTE (limit);
22021 nlines = display_count_lines (startpos_byte,
22022 limit_byte,
22023 - (height * 2 + 30),
22024 &position);
22025 /* If we couldn't find the lines we wanted within
22026 line_number_display_limit_width chars per line,
22027 give up on line numbers for this window. */
22028 if (position == limit_byte && limit == startpos - distance)
22030 w->base_line_pos = -1;
22031 w->base_line_number = 0;
22032 goto no_value;
22035 w->base_line_number = topline - nlines;
22036 w->base_line_pos = BYTE_TO_CHAR (position);
22039 /* Now count lines from the start pos to point. */
22040 nlines = display_count_lines (startpos_byte,
22041 PT_BYTE, PT, &junk);
22043 /* Record that we did display the line number. */
22044 line_number_displayed = 1;
22046 /* Make the string to show. */
22047 pint2str (decode_mode_spec_buf, width, topline + nlines);
22048 return decode_mode_spec_buf;
22049 no_value:
22051 char* p = decode_mode_spec_buf;
22052 int pad = width - 2;
22053 while (pad-- > 0)
22054 *p++ = ' ';
22055 *p++ = '?';
22056 *p++ = '?';
22057 *p = '\0';
22058 return decode_mode_spec_buf;
22061 break;
22063 case 'm':
22064 obj = BVAR (b, mode_name);
22065 break;
22067 case 'n':
22068 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22069 return " Narrow";
22070 break;
22072 case 'p':
22074 ptrdiff_t pos = marker_position (w->start);
22075 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22077 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22079 if (pos <= BUF_BEGV (b))
22080 return "All";
22081 else
22082 return "Bottom";
22084 else if (pos <= BUF_BEGV (b))
22085 return "Top";
22086 else
22088 if (total > 1000000)
22089 /* Do it differently for a large value, to avoid overflow. */
22090 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22091 else
22092 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22093 /* We can't normally display a 3-digit number,
22094 so get us a 2-digit number that is close. */
22095 if (total == 100)
22096 total = 99;
22097 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22098 return decode_mode_spec_buf;
22102 /* Display percentage of size above the bottom of the screen. */
22103 case 'P':
22105 ptrdiff_t toppos = marker_position (w->start);
22106 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22107 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22109 if (botpos >= BUF_ZV (b))
22111 if (toppos <= BUF_BEGV (b))
22112 return "All";
22113 else
22114 return "Bottom";
22116 else
22118 if (total > 1000000)
22119 /* Do it differently for a large value, to avoid overflow. */
22120 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22121 else
22122 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22123 /* We can't normally display a 3-digit number,
22124 so get us a 2-digit number that is close. */
22125 if (total == 100)
22126 total = 99;
22127 if (toppos <= BUF_BEGV (b))
22128 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22129 else
22130 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22131 return decode_mode_spec_buf;
22135 case 's':
22136 /* status of process */
22137 obj = Fget_buffer_process (Fcurrent_buffer ());
22138 if (NILP (obj))
22139 return "no process";
22140 #ifndef MSDOS
22141 obj = Fsymbol_name (Fprocess_status (obj));
22142 #endif
22143 break;
22145 case '@':
22147 ptrdiff_t count = inhibit_garbage_collection ();
22148 Lisp_Object val = call1 (intern ("file-remote-p"),
22149 BVAR (current_buffer, directory));
22150 unbind_to (count, Qnil);
22152 if (NILP (val))
22153 return "-";
22154 else
22155 return "@";
22158 case 'z':
22159 /* coding-system (not including end-of-line format) */
22160 case 'Z':
22161 /* coding-system (including end-of-line type) */
22163 int eol_flag = (c == 'Z');
22164 char *p = decode_mode_spec_buf;
22166 if (! FRAME_WINDOW_P (f))
22168 /* No need to mention EOL here--the terminal never needs
22169 to do EOL conversion. */
22170 p = decode_mode_spec_coding (CODING_ID_NAME
22171 (FRAME_KEYBOARD_CODING (f)->id),
22172 p, 0);
22173 p = decode_mode_spec_coding (CODING_ID_NAME
22174 (FRAME_TERMINAL_CODING (f)->id),
22175 p, 0);
22177 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22178 p, eol_flag);
22180 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22181 #ifdef subprocesses
22182 obj = Fget_buffer_process (Fcurrent_buffer ());
22183 if (PROCESSP (obj))
22185 p = decode_mode_spec_coding
22186 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22187 p = decode_mode_spec_coding
22188 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22190 #endif /* subprocesses */
22191 #endif /* 0 */
22192 *p = 0;
22193 return decode_mode_spec_buf;
22197 if (STRINGP (obj))
22199 *string = obj;
22200 return SSDATA (obj);
22202 else
22203 return "";
22207 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22208 means count lines back from START_BYTE. But don't go beyond
22209 LIMIT_BYTE. Return the number of lines thus found (always
22210 nonnegative).
22212 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22213 either the position COUNT lines after/before START_BYTE, if we
22214 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22215 COUNT lines. */
22217 static ptrdiff_t
22218 display_count_lines (ptrdiff_t start_byte,
22219 ptrdiff_t limit_byte, ptrdiff_t count,
22220 ptrdiff_t *byte_pos_ptr)
22222 register unsigned char *cursor;
22223 unsigned char *base;
22225 register ptrdiff_t ceiling;
22226 register unsigned char *ceiling_addr;
22227 ptrdiff_t orig_count = count;
22229 /* If we are not in selective display mode,
22230 check only for newlines. */
22231 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22232 && !INTEGERP (BVAR (current_buffer, selective_display)));
22234 if (count > 0)
22236 while (start_byte < limit_byte)
22238 ceiling = BUFFER_CEILING_OF (start_byte);
22239 ceiling = min (limit_byte - 1, ceiling);
22240 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22241 base = (cursor = BYTE_POS_ADDR (start_byte));
22245 if (selective_display)
22247 while (*cursor != '\n' && *cursor != 015
22248 && ++cursor != ceiling_addr)
22249 continue;
22250 if (cursor == ceiling_addr)
22251 break;
22253 else
22255 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22256 if (! cursor)
22257 break;
22260 cursor++;
22262 if (--count == 0)
22264 start_byte += cursor - base;
22265 *byte_pos_ptr = start_byte;
22266 return orig_count;
22269 while (cursor < ceiling_addr);
22271 start_byte += ceiling_addr - base;
22274 else
22276 while (start_byte > limit_byte)
22278 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22279 ceiling = max (limit_byte, ceiling);
22280 ceiling_addr = BYTE_POS_ADDR (ceiling);
22281 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22282 while (1)
22284 if (selective_display)
22286 while (--cursor >= ceiling_addr
22287 && *cursor != '\n' && *cursor != 015)
22288 continue;
22289 if (cursor < ceiling_addr)
22290 break;
22292 else
22294 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22295 if (! cursor)
22296 break;
22299 if (++count == 0)
22301 start_byte += cursor - base + 1;
22302 *byte_pos_ptr = start_byte;
22303 /* When scanning backwards, we should
22304 not count the newline posterior to which we stop. */
22305 return - orig_count - 1;
22308 start_byte += ceiling_addr - base;
22312 *byte_pos_ptr = limit_byte;
22314 if (count < 0)
22315 return - orig_count + count;
22316 return orig_count - count;
22322 /***********************************************************************
22323 Displaying strings
22324 ***********************************************************************/
22326 /* Display a NUL-terminated string, starting with index START.
22328 If STRING is non-null, display that C string. Otherwise, the Lisp
22329 string LISP_STRING is displayed. There's a case that STRING is
22330 non-null and LISP_STRING is not nil. It means STRING is a string
22331 data of LISP_STRING. In that case, we display LISP_STRING while
22332 ignoring its text properties.
22334 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22335 FACE_STRING. Display STRING or LISP_STRING with the face at
22336 FACE_STRING_POS in FACE_STRING:
22338 Display the string in the environment given by IT, but use the
22339 standard display table, temporarily.
22341 FIELD_WIDTH is the minimum number of output glyphs to produce.
22342 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22343 with spaces. If STRING has more characters, more than FIELD_WIDTH
22344 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22346 PRECISION is the maximum number of characters to output from
22347 STRING. PRECISION < 0 means don't truncate the string.
22349 This is roughly equivalent to printf format specifiers:
22351 FIELD_WIDTH PRECISION PRINTF
22352 ----------------------------------------
22353 -1 -1 %s
22354 -1 10 %.10s
22355 10 -1 %10s
22356 20 10 %20.10s
22358 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22359 display them, and < 0 means obey the current buffer's value of
22360 enable_multibyte_characters.
22362 Value is the number of columns displayed. */
22364 static int
22365 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22366 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22367 int field_width, int precision, int max_x, int multibyte)
22369 int hpos_at_start = it->hpos;
22370 int saved_face_id = it->face_id;
22371 struct glyph_row *row = it->glyph_row;
22372 ptrdiff_t it_charpos;
22374 /* Initialize the iterator IT for iteration over STRING beginning
22375 with index START. */
22376 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22377 precision, field_width, multibyte);
22378 if (string && STRINGP (lisp_string))
22379 /* LISP_STRING is the one returned by decode_mode_spec. We should
22380 ignore its text properties. */
22381 it->stop_charpos = it->end_charpos;
22383 /* If displaying STRING, set up the face of the iterator from
22384 FACE_STRING, if that's given. */
22385 if (STRINGP (face_string))
22387 ptrdiff_t endptr;
22388 struct face *face;
22390 it->face_id
22391 = face_at_string_position (it->w, face_string, face_string_pos,
22392 0, it->region_beg_charpos,
22393 it->region_end_charpos,
22394 &endptr, it->base_face_id, 0);
22395 face = FACE_FROM_ID (it->f, it->face_id);
22396 it->face_box_p = face->box != FACE_NO_BOX;
22399 /* Set max_x to the maximum allowed X position. Don't let it go
22400 beyond the right edge of the window. */
22401 if (max_x <= 0)
22402 max_x = it->last_visible_x;
22403 else
22404 max_x = min (max_x, it->last_visible_x);
22406 /* Skip over display elements that are not visible. because IT->w is
22407 hscrolled. */
22408 if (it->current_x < it->first_visible_x)
22409 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22410 MOVE_TO_POS | MOVE_TO_X);
22412 row->ascent = it->max_ascent;
22413 row->height = it->max_ascent + it->max_descent;
22414 row->phys_ascent = it->max_phys_ascent;
22415 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22416 row->extra_line_spacing = it->max_extra_line_spacing;
22418 if (STRINGP (it->string))
22419 it_charpos = IT_STRING_CHARPOS (*it);
22420 else
22421 it_charpos = IT_CHARPOS (*it);
22423 /* This condition is for the case that we are called with current_x
22424 past last_visible_x. */
22425 while (it->current_x < max_x)
22427 int x_before, x, n_glyphs_before, i, nglyphs;
22429 /* Get the next display element. */
22430 if (!get_next_display_element (it))
22431 break;
22433 /* Produce glyphs. */
22434 x_before = it->current_x;
22435 n_glyphs_before = row->used[TEXT_AREA];
22436 PRODUCE_GLYPHS (it);
22438 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22439 i = 0;
22440 x = x_before;
22441 while (i < nglyphs)
22443 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22445 if (it->line_wrap != TRUNCATE
22446 && x + glyph->pixel_width > max_x)
22448 /* End of continued line or max_x reached. */
22449 if (CHAR_GLYPH_PADDING_P (*glyph))
22451 /* A wide character is unbreakable. */
22452 if (row->reversed_p)
22453 unproduce_glyphs (it, row->used[TEXT_AREA]
22454 - n_glyphs_before);
22455 row->used[TEXT_AREA] = n_glyphs_before;
22456 it->current_x = x_before;
22458 else
22460 if (row->reversed_p)
22461 unproduce_glyphs (it, row->used[TEXT_AREA]
22462 - (n_glyphs_before + i));
22463 row->used[TEXT_AREA] = n_glyphs_before + i;
22464 it->current_x = x;
22466 break;
22468 else if (x + glyph->pixel_width >= it->first_visible_x)
22470 /* Glyph is at least partially visible. */
22471 ++it->hpos;
22472 if (x < it->first_visible_x)
22473 row->x = x - it->first_visible_x;
22475 else
22477 /* Glyph is off the left margin of the display area.
22478 Should not happen. */
22479 emacs_abort ();
22482 row->ascent = max (row->ascent, it->max_ascent);
22483 row->height = max (row->height, it->max_ascent + it->max_descent);
22484 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22485 row->phys_height = max (row->phys_height,
22486 it->max_phys_ascent + it->max_phys_descent);
22487 row->extra_line_spacing = max (row->extra_line_spacing,
22488 it->max_extra_line_spacing);
22489 x += glyph->pixel_width;
22490 ++i;
22493 /* Stop if max_x reached. */
22494 if (i < nglyphs)
22495 break;
22497 /* Stop at line ends. */
22498 if (ITERATOR_AT_END_OF_LINE_P (it))
22500 it->continuation_lines_width = 0;
22501 break;
22504 set_iterator_to_next (it, 1);
22505 if (STRINGP (it->string))
22506 it_charpos = IT_STRING_CHARPOS (*it);
22507 else
22508 it_charpos = IT_CHARPOS (*it);
22510 /* Stop if truncating at the right edge. */
22511 if (it->line_wrap == TRUNCATE
22512 && it->current_x >= it->last_visible_x)
22514 /* Add truncation mark, but don't do it if the line is
22515 truncated at a padding space. */
22516 if (it_charpos < it->string_nchars)
22518 if (!FRAME_WINDOW_P (it->f))
22520 int ii, n;
22522 if (it->current_x > it->last_visible_x)
22524 if (!row->reversed_p)
22526 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22527 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22528 break;
22530 else
22532 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22533 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22534 break;
22535 unproduce_glyphs (it, ii + 1);
22536 ii = row->used[TEXT_AREA] - (ii + 1);
22538 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22540 row->used[TEXT_AREA] = ii;
22541 produce_special_glyphs (it, IT_TRUNCATION);
22544 produce_special_glyphs (it, IT_TRUNCATION);
22546 row->truncated_on_right_p = 1;
22548 break;
22552 /* Maybe insert a truncation at the left. */
22553 if (it->first_visible_x
22554 && it_charpos > 0)
22556 if (!FRAME_WINDOW_P (it->f)
22557 || (row->reversed_p
22558 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22559 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22560 insert_left_trunc_glyphs (it);
22561 row->truncated_on_left_p = 1;
22564 it->face_id = saved_face_id;
22566 /* Value is number of columns displayed. */
22567 return it->hpos - hpos_at_start;
22572 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22573 appears as an element of LIST or as the car of an element of LIST.
22574 If PROPVAL is a list, compare each element against LIST in that
22575 way, and return 1/2 if any element of PROPVAL is found in LIST.
22576 Otherwise return 0. This function cannot quit.
22577 The return value is 2 if the text is invisible but with an ellipsis
22578 and 1 if it's invisible and without an ellipsis. */
22581 invisible_p (register Lisp_Object propval, Lisp_Object list)
22583 register Lisp_Object tail, proptail;
22585 for (tail = list; CONSP (tail); tail = XCDR (tail))
22587 register Lisp_Object tem;
22588 tem = XCAR (tail);
22589 if (EQ (propval, tem))
22590 return 1;
22591 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22592 return NILP (XCDR (tem)) ? 1 : 2;
22595 if (CONSP (propval))
22597 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22599 Lisp_Object propelt;
22600 propelt = XCAR (proptail);
22601 for (tail = list; CONSP (tail); tail = XCDR (tail))
22603 register Lisp_Object tem;
22604 tem = XCAR (tail);
22605 if (EQ (propelt, tem))
22606 return 1;
22607 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22608 return NILP (XCDR (tem)) ? 1 : 2;
22613 return 0;
22616 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22617 doc: /* Non-nil if the property makes the text invisible.
22618 POS-OR-PROP can be a marker or number, in which case it is taken to be
22619 a position in the current buffer and the value of the `invisible' property
22620 is checked; or it can be some other value, which is then presumed to be the
22621 value of the `invisible' property of the text of interest.
22622 The non-nil value returned can be t for truly invisible text or something
22623 else if the text is replaced by an ellipsis. */)
22624 (Lisp_Object pos_or_prop)
22626 Lisp_Object prop
22627 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22628 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22629 : pos_or_prop);
22630 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22631 return (invis == 0 ? Qnil
22632 : invis == 1 ? Qt
22633 : make_number (invis));
22636 /* Calculate a width or height in pixels from a specification using
22637 the following elements:
22639 SPEC ::=
22640 NUM - a (fractional) multiple of the default font width/height
22641 (NUM) - specifies exactly NUM pixels
22642 UNIT - a fixed number of pixels, see below.
22643 ELEMENT - size of a display element in pixels, see below.
22644 (NUM . SPEC) - equals NUM * SPEC
22645 (+ SPEC SPEC ...) - add pixel values
22646 (- SPEC SPEC ...) - subtract pixel values
22647 (- SPEC) - negate pixel value
22649 NUM ::=
22650 INT or FLOAT - a number constant
22651 SYMBOL - use symbol's (buffer local) variable binding.
22653 UNIT ::=
22654 in - pixels per inch *)
22655 mm - pixels per 1/1000 meter *)
22656 cm - pixels per 1/100 meter *)
22657 width - width of current font in pixels.
22658 height - height of current font in pixels.
22660 *) using the ratio(s) defined in display-pixels-per-inch.
22662 ELEMENT ::=
22664 left-fringe - left fringe width in pixels
22665 right-fringe - right fringe width in pixels
22667 left-margin - left margin width in pixels
22668 right-margin - right margin width in pixels
22670 scroll-bar - scroll-bar area width in pixels
22672 Examples:
22674 Pixels corresponding to 5 inches:
22675 (5 . in)
22677 Total width of non-text areas on left side of window (if scroll-bar is on left):
22678 '(space :width (+ left-fringe left-margin scroll-bar))
22680 Align to first text column (in header line):
22681 '(space :align-to 0)
22683 Align to middle of text area minus half the width of variable `my-image'
22684 containing a loaded image:
22685 '(space :align-to (0.5 . (- text my-image)))
22687 Width of left margin minus width of 1 character in the default font:
22688 '(space :width (- left-margin 1))
22690 Width of left margin minus width of 2 characters in the current font:
22691 '(space :width (- left-margin (2 . width)))
22693 Center 1 character over left-margin (in header line):
22694 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22696 Different ways to express width of left fringe plus left margin minus one pixel:
22697 '(space :width (- (+ left-fringe left-margin) (1)))
22698 '(space :width (+ left-fringe left-margin (- (1))))
22699 '(space :width (+ left-fringe left-margin (-1)))
22703 static int
22704 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22705 struct font *font, int width_p, int *align_to)
22707 double pixels;
22709 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22710 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22712 if (NILP (prop))
22713 return OK_PIXELS (0);
22715 eassert (FRAME_LIVE_P (it->f));
22717 if (SYMBOLP (prop))
22719 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22721 char *unit = SSDATA (SYMBOL_NAME (prop));
22723 if (unit[0] == 'i' && unit[1] == 'n')
22724 pixels = 1.0;
22725 else if (unit[0] == 'm' && unit[1] == 'm')
22726 pixels = 25.4;
22727 else if (unit[0] == 'c' && unit[1] == 'm')
22728 pixels = 2.54;
22729 else
22730 pixels = 0;
22731 if (pixels > 0)
22733 double ppi = (width_p ? FRAME_RES_X (it->f)
22734 : FRAME_RES_Y (it->f));
22736 if (ppi > 0)
22737 return OK_PIXELS (ppi / pixels);
22738 return 0;
22742 #ifdef HAVE_WINDOW_SYSTEM
22743 if (EQ (prop, Qheight))
22744 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22745 if (EQ (prop, Qwidth))
22746 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22747 #else
22748 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22749 return OK_PIXELS (1);
22750 #endif
22752 if (EQ (prop, Qtext))
22753 return OK_PIXELS (width_p
22754 ? window_box_width (it->w, TEXT_AREA)
22755 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22757 if (align_to && *align_to < 0)
22759 *res = 0;
22760 if (EQ (prop, Qleft))
22761 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22762 if (EQ (prop, Qright))
22763 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22764 if (EQ (prop, Qcenter))
22765 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22766 + window_box_width (it->w, TEXT_AREA) / 2);
22767 if (EQ (prop, Qleft_fringe))
22768 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22769 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22770 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22771 if (EQ (prop, Qright_fringe))
22772 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22773 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22774 : window_box_right_offset (it->w, TEXT_AREA));
22775 if (EQ (prop, Qleft_margin))
22776 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22777 if (EQ (prop, Qright_margin))
22778 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22779 if (EQ (prop, Qscroll_bar))
22780 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22782 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22783 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22784 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22785 : 0)));
22787 else
22789 if (EQ (prop, Qleft_fringe))
22790 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22791 if (EQ (prop, Qright_fringe))
22792 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22793 if (EQ (prop, Qleft_margin))
22794 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22795 if (EQ (prop, Qright_margin))
22796 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22797 if (EQ (prop, Qscroll_bar))
22798 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22801 prop = buffer_local_value_1 (prop, it->w->contents);
22802 if (EQ (prop, Qunbound))
22803 prop = Qnil;
22806 if (INTEGERP (prop) || FLOATP (prop))
22808 int base_unit = (width_p
22809 ? FRAME_COLUMN_WIDTH (it->f)
22810 : FRAME_LINE_HEIGHT (it->f));
22811 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22814 if (CONSP (prop))
22816 Lisp_Object car = XCAR (prop);
22817 Lisp_Object cdr = XCDR (prop);
22819 if (SYMBOLP (car))
22821 #ifdef HAVE_WINDOW_SYSTEM
22822 if (FRAME_WINDOW_P (it->f)
22823 && valid_image_p (prop))
22825 ptrdiff_t id = lookup_image (it->f, prop);
22826 struct image *img = IMAGE_FROM_ID (it->f, id);
22828 return OK_PIXELS (width_p ? img->width : img->height);
22830 #ifdef HAVE_XWIDGETS
22831 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
22833 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
22834 return OK_PIXELS (width_p ? 100 : 100);
22836 #endif
22837 #endif
22838 if (EQ (car, Qplus) || EQ (car, Qminus))
22840 int first = 1;
22841 double px;
22843 pixels = 0;
22844 while (CONSP (cdr))
22846 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22847 font, width_p, align_to))
22848 return 0;
22849 if (first)
22850 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22851 else
22852 pixels += px;
22853 cdr = XCDR (cdr);
22855 if (EQ (car, Qminus))
22856 pixels = -pixels;
22857 return OK_PIXELS (pixels);
22860 car = buffer_local_value_1 (car, it->w->contents);
22861 if (EQ (car, Qunbound))
22862 car = Qnil;
22865 if (INTEGERP (car) || FLOATP (car))
22867 double fact;
22868 pixels = XFLOATINT (car);
22869 if (NILP (cdr))
22870 return OK_PIXELS (pixels);
22871 if (calc_pixel_width_or_height (&fact, it, cdr,
22872 font, width_p, align_to))
22873 return OK_PIXELS (pixels * fact);
22874 return 0;
22877 return 0;
22880 return 0;
22884 /***********************************************************************
22885 Glyph Display
22886 ***********************************************************************/
22888 #ifdef HAVE_WINDOW_SYSTEM
22890 #ifdef GLYPH_DEBUG
22892 void
22893 dump_glyph_string (struct glyph_string *s)
22895 fprintf (stderr, "glyph string\n");
22896 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22897 s->x, s->y, s->width, s->height);
22898 fprintf (stderr, " ybase = %d\n", s->ybase);
22899 fprintf (stderr, " hl = %d\n", s->hl);
22900 fprintf (stderr, " left overhang = %d, right = %d\n",
22901 s->left_overhang, s->right_overhang);
22902 fprintf (stderr, " nchars = %d\n", s->nchars);
22903 fprintf (stderr, " extends to end of line = %d\n",
22904 s->extends_to_end_of_line_p);
22905 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22906 fprintf (stderr, " bg width = %d\n", s->background_width);
22909 #endif /* GLYPH_DEBUG */
22911 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22912 of XChar2b structures for S; it can't be allocated in
22913 init_glyph_string because it must be allocated via `alloca'. W
22914 is the window on which S is drawn. ROW and AREA are the glyph row
22915 and area within the row from which S is constructed. START is the
22916 index of the first glyph structure covered by S. HL is a
22917 face-override for drawing S. */
22919 #ifdef HAVE_NTGUI
22920 #define OPTIONAL_HDC(hdc) HDC hdc,
22921 #define DECLARE_HDC(hdc) HDC hdc;
22922 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22923 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22924 #endif
22926 #ifndef OPTIONAL_HDC
22927 #define OPTIONAL_HDC(hdc)
22928 #define DECLARE_HDC(hdc)
22929 #define ALLOCATE_HDC(hdc, f)
22930 #define RELEASE_HDC(hdc, f)
22931 #endif
22933 static void
22934 init_glyph_string (struct glyph_string *s,
22935 OPTIONAL_HDC (hdc)
22936 XChar2b *char2b, struct window *w, struct glyph_row *row,
22937 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22939 memset (s, 0, sizeof *s);
22940 s->w = w;
22941 s->f = XFRAME (w->frame);
22942 #ifdef HAVE_NTGUI
22943 s->hdc = hdc;
22944 #endif
22945 s->display = FRAME_X_DISPLAY (s->f);
22946 s->window = FRAME_X_WINDOW (s->f);
22947 s->char2b = char2b;
22948 s->hl = hl;
22949 s->row = row;
22950 s->area = area;
22951 s->first_glyph = row->glyphs[area] + start;
22952 s->height = row->height;
22953 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22954 s->ybase = s->y + row->ascent;
22958 /* Append the list of glyph strings with head H and tail T to the list
22959 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22961 static void
22962 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22963 struct glyph_string *h, struct glyph_string *t)
22965 if (h)
22967 if (*head)
22968 (*tail)->next = h;
22969 else
22970 *head = h;
22971 h->prev = *tail;
22972 *tail = t;
22977 /* Prepend the list of glyph strings with head H and tail T to the
22978 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22979 result. */
22981 static void
22982 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22983 struct glyph_string *h, struct glyph_string *t)
22985 if (h)
22987 if (*head)
22988 (*head)->prev = t;
22989 else
22990 *tail = t;
22991 t->next = *head;
22992 *head = h;
22997 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22998 Set *HEAD and *TAIL to the resulting list. */
23000 static void
23001 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23002 struct glyph_string *s)
23004 s->next = s->prev = NULL;
23005 append_glyph_string_lists (head, tail, s, s);
23009 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23010 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23011 make sure that X resources for the face returned are allocated.
23012 Value is a pointer to a realized face that is ready for display if
23013 DISPLAY_P is non-zero. */
23015 static struct face *
23016 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23017 XChar2b *char2b, int display_p)
23019 struct face *face = FACE_FROM_ID (f, face_id);
23020 unsigned code = 0;
23022 if (face->font)
23024 code = face->font->driver->encode_char (face->font, c);
23026 if (code == FONT_INVALID_CODE)
23027 code = 0;
23029 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23031 /* Make sure X resources of the face are allocated. */
23032 #ifdef HAVE_X_WINDOWS
23033 if (display_p)
23034 #endif
23036 eassert (face != NULL);
23037 PREPARE_FACE_FOR_DISPLAY (f, face);
23040 return face;
23044 /* Get face and two-byte form of character glyph GLYPH on frame F.
23045 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23046 a pointer to a realized face that is ready for display. */
23048 static struct face *
23049 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23050 XChar2b *char2b, int *two_byte_p)
23052 struct face *face;
23053 unsigned code = 0;
23055 eassert (glyph->type == CHAR_GLYPH);
23056 face = FACE_FROM_ID (f, glyph->face_id);
23058 /* Make sure X resources of the face are allocated. */
23059 eassert (face != NULL);
23060 PREPARE_FACE_FOR_DISPLAY (f, face);
23062 if (two_byte_p)
23063 *two_byte_p = 0;
23065 if (face->font)
23067 if (CHAR_BYTE8_P (glyph->u.ch))
23068 code = CHAR_TO_BYTE8 (glyph->u.ch);
23069 else
23070 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23072 if (code == FONT_INVALID_CODE)
23073 code = 0;
23076 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23077 return face;
23081 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23082 Return 1 if FONT has a glyph for C, otherwise return 0. */
23084 static int
23085 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23087 unsigned code;
23089 if (CHAR_BYTE8_P (c))
23090 code = CHAR_TO_BYTE8 (c);
23091 else
23092 code = font->driver->encode_char (font, c);
23094 if (code == FONT_INVALID_CODE)
23095 return 0;
23096 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23097 return 1;
23101 /* Fill glyph string S with composition components specified by S->cmp.
23103 BASE_FACE is the base face of the composition.
23104 S->cmp_from is the index of the first component for S.
23106 OVERLAPS non-zero means S should draw the foreground only, and use
23107 its physical height for clipping. See also draw_glyphs.
23109 Value is the index of a component not in S. */
23111 static int
23112 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23113 int overlaps)
23115 int i;
23116 /* For all glyphs of this composition, starting at the offset
23117 S->cmp_from, until we reach the end of the definition or encounter a
23118 glyph that requires the different face, add it to S. */
23119 struct face *face;
23121 eassert (s);
23123 s->for_overlaps = overlaps;
23124 s->face = NULL;
23125 s->font = NULL;
23126 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23128 int c = COMPOSITION_GLYPH (s->cmp, i);
23130 /* TAB in a composition means display glyphs with padding space
23131 on the left or right. */
23132 if (c != '\t')
23134 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23135 -1, Qnil);
23137 face = get_char_face_and_encoding (s->f, c, face_id,
23138 s->char2b + i, 1);
23139 if (face)
23141 if (! s->face)
23143 s->face = face;
23144 s->font = s->face->font;
23146 else if (s->face != face)
23147 break;
23150 ++s->nchars;
23152 s->cmp_to = i;
23154 if (s->face == NULL)
23156 s->face = base_face->ascii_face;
23157 s->font = s->face->font;
23160 /* All glyph strings for the same composition has the same width,
23161 i.e. the width set for the first component of the composition. */
23162 s->width = s->first_glyph->pixel_width;
23164 /* If the specified font could not be loaded, use the frame's
23165 default font, but record the fact that we couldn't load it in
23166 the glyph string so that we can draw rectangles for the
23167 characters of the glyph string. */
23168 if (s->font == NULL)
23170 s->font_not_found_p = 1;
23171 s->font = FRAME_FONT (s->f);
23174 /* Adjust base line for subscript/superscript text. */
23175 s->ybase += s->first_glyph->voffset;
23177 /* This glyph string must always be drawn with 16-bit functions. */
23178 s->two_byte_p = 1;
23180 return s->cmp_to;
23183 static int
23184 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23185 int start, int end, int overlaps)
23187 struct glyph *glyph, *last;
23188 Lisp_Object lgstring;
23189 int i;
23191 s->for_overlaps = overlaps;
23192 glyph = s->row->glyphs[s->area] + start;
23193 last = s->row->glyphs[s->area] + end;
23194 s->cmp_id = glyph->u.cmp.id;
23195 s->cmp_from = glyph->slice.cmp.from;
23196 s->cmp_to = glyph->slice.cmp.to + 1;
23197 s->face = FACE_FROM_ID (s->f, face_id);
23198 lgstring = composition_gstring_from_id (s->cmp_id);
23199 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23200 glyph++;
23201 while (glyph < last
23202 && glyph->u.cmp.automatic
23203 && glyph->u.cmp.id == s->cmp_id
23204 && s->cmp_to == glyph->slice.cmp.from)
23205 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23207 for (i = s->cmp_from; i < s->cmp_to; i++)
23209 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23210 unsigned code = LGLYPH_CODE (lglyph);
23212 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23214 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23215 return glyph - s->row->glyphs[s->area];
23219 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23220 See the comment of fill_glyph_string for arguments.
23221 Value is the index of the first glyph not in S. */
23224 static int
23225 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23226 int start, int end, int overlaps)
23228 struct glyph *glyph, *last;
23229 int voffset;
23231 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23232 s->for_overlaps = overlaps;
23233 glyph = s->row->glyphs[s->area] + start;
23234 last = s->row->glyphs[s->area] + end;
23235 voffset = glyph->voffset;
23236 s->face = FACE_FROM_ID (s->f, face_id);
23237 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23238 s->nchars = 1;
23239 s->width = glyph->pixel_width;
23240 glyph++;
23241 while (glyph < last
23242 && glyph->type == GLYPHLESS_GLYPH
23243 && glyph->voffset == voffset
23244 && glyph->face_id == face_id)
23246 s->nchars++;
23247 s->width += glyph->pixel_width;
23248 glyph++;
23250 s->ybase += voffset;
23251 return glyph - s->row->glyphs[s->area];
23255 /* Fill glyph string S from a sequence of character glyphs.
23257 FACE_ID is the face id of the string. START is the index of the
23258 first glyph to consider, END is the index of the last + 1.
23259 OVERLAPS non-zero means S should draw the foreground only, and use
23260 its physical height for clipping. See also draw_glyphs.
23262 Value is the index of the first glyph not in S. */
23264 static int
23265 fill_glyph_string (struct glyph_string *s, int face_id,
23266 int start, int end, int overlaps)
23268 struct glyph *glyph, *last;
23269 int voffset;
23270 int glyph_not_available_p;
23272 eassert (s->f == XFRAME (s->w->frame));
23273 eassert (s->nchars == 0);
23274 eassert (start >= 0 && end > start);
23276 s->for_overlaps = overlaps;
23277 glyph = s->row->glyphs[s->area] + start;
23278 last = s->row->glyphs[s->area] + end;
23279 voffset = glyph->voffset;
23280 s->padding_p = glyph->padding_p;
23281 glyph_not_available_p = glyph->glyph_not_available_p;
23283 while (glyph < last
23284 && glyph->type == CHAR_GLYPH
23285 && glyph->voffset == voffset
23286 /* Same face id implies same font, nowadays. */
23287 && glyph->face_id == face_id
23288 && glyph->glyph_not_available_p == glyph_not_available_p)
23290 int two_byte_p;
23292 s->face = get_glyph_face_and_encoding (s->f, glyph,
23293 s->char2b + s->nchars,
23294 &two_byte_p);
23295 s->two_byte_p = two_byte_p;
23296 ++s->nchars;
23297 eassert (s->nchars <= end - start);
23298 s->width += glyph->pixel_width;
23299 if (glyph++->padding_p != s->padding_p)
23300 break;
23303 s->font = s->face->font;
23305 /* If the specified font could not be loaded, use the frame's font,
23306 but record the fact that we couldn't load it in
23307 S->font_not_found_p so that we can draw rectangles for the
23308 characters of the glyph string. */
23309 if (s->font == NULL || glyph_not_available_p)
23311 s->font_not_found_p = 1;
23312 s->font = FRAME_FONT (s->f);
23315 /* Adjust base line for subscript/superscript text. */
23316 s->ybase += voffset;
23318 eassert (s->face && s->face->gc);
23319 return glyph - s->row->glyphs[s->area];
23323 /* Fill glyph string S from image glyph S->first_glyph. */
23325 static void
23326 fill_image_glyph_string (struct glyph_string *s)
23328 eassert (s->first_glyph->type == IMAGE_GLYPH);
23329 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23330 eassert (s->img);
23331 s->slice = s->first_glyph->slice.img;
23332 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23333 s->font = s->face->font;
23334 s->width = s->first_glyph->pixel_width;
23336 /* Adjust base line for subscript/superscript text. */
23337 s->ybase += s->first_glyph->voffset;
23340 #ifdef HAVE_XWIDGETS
23341 static void
23342 fill_xwidget_glyph_string (struct glyph_string *s)
23344 eassert (s->first_glyph->type == XWIDGET_GLYPH);
23345 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
23346 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23347 s->font = s->face->font;
23348 s->width = s->first_glyph->pixel_width;
23349 s->ybase += s->first_glyph->voffset;
23350 s->xwidget = s->first_glyph->u.xwidget;
23351 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
23353 #endif
23354 /* Fill glyph string S from a sequence of stretch glyphs.
23356 START is the index of the first glyph to consider,
23357 END is the index of the last + 1.
23359 Value is the index of the first glyph not in S. */
23361 static int
23362 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23364 struct glyph *glyph, *last;
23365 int voffset, face_id;
23367 eassert (s->first_glyph->type == STRETCH_GLYPH);
23369 glyph = s->row->glyphs[s->area] + start;
23370 last = s->row->glyphs[s->area] + end;
23371 face_id = glyph->face_id;
23372 s->face = FACE_FROM_ID (s->f, face_id);
23373 s->font = s->face->font;
23374 s->width = glyph->pixel_width;
23375 s->nchars = 1;
23376 voffset = glyph->voffset;
23378 for (++glyph;
23379 (glyph < last
23380 && glyph->type == STRETCH_GLYPH
23381 && glyph->voffset == voffset
23382 && glyph->face_id == face_id);
23383 ++glyph)
23384 s->width += glyph->pixel_width;
23386 /* Adjust base line for subscript/superscript text. */
23387 s->ybase += voffset;
23389 /* The case that face->gc == 0 is handled when drawing the glyph
23390 string by calling PREPARE_FACE_FOR_DISPLAY. */
23391 eassert (s->face);
23392 return glyph - s->row->glyphs[s->area];
23395 static struct font_metrics *
23396 get_per_char_metric (struct font *font, XChar2b *char2b)
23398 static struct font_metrics metrics;
23399 unsigned code;
23401 if (! font)
23402 return NULL;
23403 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23404 if (code == FONT_INVALID_CODE)
23405 return NULL;
23406 font->driver->text_extents (font, &code, 1, &metrics);
23407 return &metrics;
23410 /* EXPORT for RIF:
23411 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23412 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23413 assumed to be zero. */
23415 void
23416 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23418 *left = *right = 0;
23420 if (glyph->type == CHAR_GLYPH)
23422 struct face *face;
23423 XChar2b char2b;
23424 struct font_metrics *pcm;
23426 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23427 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23429 if (pcm->rbearing > pcm->width)
23430 *right = pcm->rbearing - pcm->width;
23431 if (pcm->lbearing < 0)
23432 *left = -pcm->lbearing;
23435 else if (glyph->type == COMPOSITE_GLYPH)
23437 if (! glyph->u.cmp.automatic)
23439 struct composition *cmp = composition_table[glyph->u.cmp.id];
23441 if (cmp->rbearing > cmp->pixel_width)
23442 *right = cmp->rbearing - cmp->pixel_width;
23443 if (cmp->lbearing < 0)
23444 *left = - cmp->lbearing;
23446 else
23448 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23449 struct font_metrics metrics;
23451 composition_gstring_width (gstring, glyph->slice.cmp.from,
23452 glyph->slice.cmp.to + 1, &metrics);
23453 if (metrics.rbearing > metrics.width)
23454 *right = metrics.rbearing - metrics.width;
23455 if (metrics.lbearing < 0)
23456 *left = - metrics.lbearing;
23462 /* Return the index of the first glyph preceding glyph string S that
23463 is overwritten by S because of S's left overhang. Value is -1
23464 if no glyphs are overwritten. */
23466 static int
23467 left_overwritten (struct glyph_string *s)
23469 int k;
23471 if (s->left_overhang)
23473 int x = 0, i;
23474 struct glyph *glyphs = s->row->glyphs[s->area];
23475 int first = s->first_glyph - glyphs;
23477 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23478 x -= glyphs[i].pixel_width;
23480 k = i + 1;
23482 else
23483 k = -1;
23485 return k;
23489 /* Return the index of the first glyph preceding glyph string S that
23490 is overwriting S because of its right overhang. Value is -1 if no
23491 glyph in front of S overwrites S. */
23493 static int
23494 left_overwriting (struct glyph_string *s)
23496 int i, k, x;
23497 struct glyph *glyphs = s->row->glyphs[s->area];
23498 int first = s->first_glyph - glyphs;
23500 k = -1;
23501 x = 0;
23502 for (i = first - 1; i >= 0; --i)
23504 int left, right;
23505 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23506 if (x + right > 0)
23507 k = i;
23508 x -= glyphs[i].pixel_width;
23511 return k;
23515 /* Return the index of the last glyph following glyph string S that is
23516 overwritten by S because of S's right overhang. Value is -1 if
23517 no such glyph is found. */
23519 static int
23520 right_overwritten (struct glyph_string *s)
23522 int k = -1;
23524 if (s->right_overhang)
23526 int x = 0, i;
23527 struct glyph *glyphs = s->row->glyphs[s->area];
23528 int first = (s->first_glyph - glyphs
23529 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23530 int end = s->row->used[s->area];
23532 for (i = first; i < end && s->right_overhang > x; ++i)
23533 x += glyphs[i].pixel_width;
23535 k = i;
23538 return k;
23542 /* Return the index of the last glyph following glyph string S that
23543 overwrites S because of its left overhang. Value is negative
23544 if no such glyph is found. */
23546 static int
23547 right_overwriting (struct glyph_string *s)
23549 int i, k, x;
23550 int end = s->row->used[s->area];
23551 struct glyph *glyphs = s->row->glyphs[s->area];
23552 int first = (s->first_glyph - glyphs
23553 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23555 k = -1;
23556 x = 0;
23557 for (i = first; i < end; ++i)
23559 int left, right;
23560 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23561 if (x - left < 0)
23562 k = i;
23563 x += glyphs[i].pixel_width;
23566 return k;
23570 /* Set background width of glyph string S. START is the index of the
23571 first glyph following S. LAST_X is the right-most x-position + 1
23572 in the drawing area. */
23574 static void
23575 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23577 /* If the face of this glyph string has to be drawn to the end of
23578 the drawing area, set S->extends_to_end_of_line_p. */
23580 if (start == s->row->used[s->area]
23581 && s->area == TEXT_AREA
23582 && ((s->row->fill_line_p
23583 && (s->hl == DRAW_NORMAL_TEXT
23584 || s->hl == DRAW_IMAGE_RAISED
23585 || s->hl == DRAW_IMAGE_SUNKEN))
23586 || s->hl == DRAW_MOUSE_FACE))
23587 s->extends_to_end_of_line_p = 1;
23589 /* If S extends its face to the end of the line, set its
23590 background_width to the distance to the right edge of the drawing
23591 area. */
23592 if (s->extends_to_end_of_line_p)
23593 s->background_width = last_x - s->x + 1;
23594 else
23595 s->background_width = s->width;
23599 /* Compute overhangs and x-positions for glyph string S and its
23600 predecessors, or successors. X is the starting x-position for S.
23601 BACKWARD_P non-zero means process predecessors. */
23603 static void
23604 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23606 if (backward_p)
23608 while (s)
23610 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23611 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23612 x -= s->width;
23613 s->x = x;
23614 s = s->prev;
23617 else
23619 while (s)
23621 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23622 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23623 s->x = x;
23624 x += s->width;
23625 s = s->next;
23632 /* The following macros are only called from draw_glyphs below.
23633 They reference the following parameters of that function directly:
23634 `w', `row', `area', and `overlap_p'
23635 as well as the following local variables:
23636 `s', `f', and `hdc' (in W32) */
23638 #ifdef HAVE_NTGUI
23639 /* On W32, silently add local `hdc' variable to argument list of
23640 init_glyph_string. */
23641 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23642 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23643 #else
23644 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23645 init_glyph_string (s, char2b, w, row, area, start, hl)
23646 #endif
23648 /* Add a glyph string for a stretch glyph to the list of strings
23649 between HEAD and TAIL. START is the index of the stretch glyph in
23650 row area AREA of glyph row ROW. END is the index of the last glyph
23651 in that glyph row area. X is the current output position assigned
23652 to the new glyph string constructed. HL overrides that face of the
23653 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23654 is the right-most x-position of the drawing area. */
23656 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23657 and below -- keep them on one line. */
23658 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23659 do \
23661 s = alloca (sizeof *s); \
23662 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23663 START = fill_stretch_glyph_string (s, START, END); \
23664 append_glyph_string (&HEAD, &TAIL, s); \
23665 s->x = (X); \
23667 while (0)
23670 /* Add a glyph string for an image glyph to the list of strings
23671 between HEAD and TAIL. START is the index of the image glyph in
23672 row area AREA of glyph row ROW. END is the index of the last glyph
23673 in that glyph row area. X is the current output position assigned
23674 to the new glyph string constructed. HL overrides that face of the
23675 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23676 is the right-most x-position of the drawing area. */
23678 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23679 do \
23681 s = alloca (sizeof *s); \
23682 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23683 fill_image_glyph_string (s); \
23684 append_glyph_string (&HEAD, &TAIL, s); \
23685 ++START; \
23686 s->x = (X); \
23688 while (0)
23690 #ifdef HAVE_XWIDGETS
23691 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23692 do \
23694 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
23695 s = (struct glyph_string *) alloca (sizeof *s); \
23696 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23697 fill_xwidget_glyph_string (s); \
23698 append_glyph_string (&HEAD, &TAIL, s); \
23699 ++START; \
23700 s->x = (X); \
23702 while (0)
23703 #endif
23706 /* Add a glyph string for a sequence of character glyphs to the list
23707 of strings between HEAD and TAIL. START is the index of the first
23708 glyph in row area AREA of glyph row ROW that is part of the new
23709 glyph string. END is the index of the last glyph in that glyph row
23710 area. X is the current output position assigned to the new glyph
23711 string constructed. HL overrides that face of the glyph; e.g. it
23712 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23713 right-most x-position of the drawing area. */
23715 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23716 do \
23718 int face_id; \
23719 XChar2b *char2b; \
23721 face_id = (row)->glyphs[area][START].face_id; \
23723 s = alloca (sizeof *s); \
23724 char2b = alloca ((END - START) * sizeof *char2b); \
23725 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23726 append_glyph_string (&HEAD, &TAIL, s); \
23727 s->x = (X); \
23728 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23730 while (0)
23733 /* Add a glyph string for a composite sequence to the list of strings
23734 between HEAD and TAIL. START is the index of the first glyph in
23735 row area AREA of glyph row ROW that is part of the new glyph
23736 string. END is the index of the last glyph in that glyph row area.
23737 X is the current output position assigned to the new glyph string
23738 constructed. HL overrides that face of the glyph; e.g. it is
23739 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23740 x-position of the drawing area. */
23742 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23743 do { \
23744 int face_id = (row)->glyphs[area][START].face_id; \
23745 struct face *base_face = FACE_FROM_ID (f, face_id); \
23746 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23747 struct composition *cmp = composition_table[cmp_id]; \
23748 XChar2b *char2b; \
23749 struct glyph_string *first_s = NULL; \
23750 int n; \
23752 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23754 /* Make glyph_strings for each glyph sequence that is drawable by \
23755 the same face, and append them to HEAD/TAIL. */ \
23756 for (n = 0; n < cmp->glyph_len;) \
23758 s = alloca (sizeof *s); \
23759 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23760 append_glyph_string (&(HEAD), &(TAIL), s); \
23761 s->cmp = cmp; \
23762 s->cmp_from = n; \
23763 s->x = (X); \
23764 if (n == 0) \
23765 first_s = s; \
23766 n = fill_composite_glyph_string (s, base_face, overlaps); \
23769 ++START; \
23770 s = first_s; \
23771 } while (0)
23774 /* Add a glyph string for a glyph-string sequence to the list of strings
23775 between HEAD and TAIL. */
23777 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23778 do { \
23779 int face_id; \
23780 XChar2b *char2b; \
23781 Lisp_Object gstring; \
23783 face_id = (row)->glyphs[area][START].face_id; \
23784 gstring = (composition_gstring_from_id \
23785 ((row)->glyphs[area][START].u.cmp.id)); \
23786 s = alloca (sizeof *s); \
23787 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23788 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23789 append_glyph_string (&(HEAD), &(TAIL), s); \
23790 s->x = (X); \
23791 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23792 } while (0)
23795 /* Add a glyph string for a sequence of glyphless character's glyphs
23796 to the list of strings between HEAD and TAIL. The meanings of
23797 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23799 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23800 do \
23802 int face_id; \
23804 face_id = (row)->glyphs[area][START].face_id; \
23806 s = alloca (sizeof *s); \
23807 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23808 append_glyph_string (&HEAD, &TAIL, s); \
23809 s->x = (X); \
23810 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23811 overlaps); \
23813 while (0)
23816 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23817 of AREA of glyph row ROW on window W between indices START and END.
23818 HL overrides the face for drawing glyph strings, e.g. it is
23819 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23820 x-positions of the drawing area.
23822 This is an ugly monster macro construct because we must use alloca
23823 to allocate glyph strings (because draw_glyphs can be called
23824 asynchronously). */
23826 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23827 do \
23829 HEAD = TAIL = NULL; \
23830 while (START < END) \
23832 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23833 switch (first_glyph->type) \
23835 case CHAR_GLYPH: \
23836 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23837 HL, X, LAST_X); \
23838 break; \
23840 case COMPOSITE_GLYPH: \
23841 if (first_glyph->u.cmp.automatic) \
23842 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23843 HL, X, LAST_X); \
23844 else \
23845 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23846 HL, X, LAST_X); \
23847 break; \
23849 case STRETCH_GLYPH: \
23850 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23851 HL, X, LAST_X); \
23852 break; \
23854 case IMAGE_GLYPH: \
23855 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23856 HL, X, LAST_X); \
23857 break;
23859 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23860 case XWIDGET_GLYPH: \
23861 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
23862 HL, X, LAST_X); \
23863 break;
23865 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
23866 case GLYPHLESS_GLYPH: \
23867 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23868 HL, X, LAST_X); \
23869 break; \
23871 default: \
23872 emacs_abort (); \
23875 if (s) \
23877 set_glyph_string_background_width (s, START, LAST_X); \
23878 (X) += s->width; \
23881 } while (0)
23884 #ifdef HAVE_XWIDGETS
23885 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23886 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23887 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23888 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23889 #else
23890 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23891 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23892 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23893 #endif
23896 /* Draw glyphs between START and END in AREA of ROW on window W,
23897 starting at x-position X. X is relative to AREA in W. HL is a
23898 face-override with the following meaning:
23900 DRAW_NORMAL_TEXT draw normally
23901 DRAW_CURSOR draw in cursor face
23902 DRAW_MOUSE_FACE draw in mouse face.
23903 DRAW_INVERSE_VIDEO draw in mode line face
23904 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23905 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23907 If OVERLAPS is non-zero, draw only the foreground of characters and
23908 clip to the physical height of ROW. Non-zero value also defines
23909 the overlapping part to be drawn:
23911 OVERLAPS_PRED overlap with preceding rows
23912 OVERLAPS_SUCC overlap with succeeding rows
23913 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23914 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23916 Value is the x-position reached, relative to AREA of W. */
23918 static int
23919 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23920 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23921 enum draw_glyphs_face hl, int overlaps)
23923 struct glyph_string *head, *tail;
23924 struct glyph_string *s;
23925 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23926 int i, j, x_reached, last_x, area_left = 0;
23927 struct frame *f = XFRAME (WINDOW_FRAME (w));
23928 DECLARE_HDC (hdc);
23930 ALLOCATE_HDC (hdc, f);
23932 /* Let's rather be paranoid than getting a SEGV. */
23933 end = min (end, row->used[area]);
23934 start = clip_to_bounds (0, start, end);
23936 /* Translate X to frame coordinates. Set last_x to the right
23937 end of the drawing area. */
23938 if (row->full_width_p)
23940 /* X is relative to the left edge of W, without scroll bars
23941 or fringes. */
23942 area_left = WINDOW_LEFT_EDGE_X (w);
23943 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23945 else
23947 area_left = window_box_left (w, area);
23948 last_x = area_left + window_box_width (w, area);
23950 x += area_left;
23952 /* Build a doubly-linked list of glyph_string structures between
23953 head and tail from what we have to draw. Note that the macro
23954 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23955 the reason we use a separate variable `i'. */
23956 i = start;
23957 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23958 if (tail)
23959 x_reached = tail->x + tail->background_width;
23960 else
23961 x_reached = x;
23963 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23964 the row, redraw some glyphs in front or following the glyph
23965 strings built above. */
23966 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23968 struct glyph_string *h, *t;
23969 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23970 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23971 int check_mouse_face = 0;
23972 int dummy_x = 0;
23974 /* If mouse highlighting is on, we may need to draw adjacent
23975 glyphs using mouse-face highlighting. */
23976 if (area == TEXT_AREA && row->mouse_face_p
23977 && hlinfo->mouse_face_beg_row >= 0
23978 && hlinfo->mouse_face_end_row >= 0)
23980 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23982 if (row_vpos >= hlinfo->mouse_face_beg_row
23983 && row_vpos <= hlinfo->mouse_face_end_row)
23985 check_mouse_face = 1;
23986 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23987 ? hlinfo->mouse_face_beg_col : 0;
23988 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23989 ? hlinfo->mouse_face_end_col
23990 : row->used[TEXT_AREA];
23994 /* Compute overhangs for all glyph strings. */
23995 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23996 for (s = head; s; s = s->next)
23997 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23999 /* Prepend glyph strings for glyphs in front of the first glyph
24000 string that are overwritten because of the first glyph
24001 string's left overhang. The background of all strings
24002 prepended must be drawn because the first glyph string
24003 draws over it. */
24004 i = left_overwritten (head);
24005 if (i >= 0)
24007 enum draw_glyphs_face overlap_hl;
24009 /* If this row contains mouse highlighting, attempt to draw
24010 the overlapped glyphs with the correct highlight. This
24011 code fails if the overlap encompasses more than one glyph
24012 and mouse-highlight spans only some of these glyphs.
24013 However, making it work perfectly involves a lot more
24014 code, and I don't know if the pathological case occurs in
24015 practice, so we'll stick to this for now. --- cyd */
24016 if (check_mouse_face
24017 && mouse_beg_col < start && mouse_end_col > i)
24018 overlap_hl = DRAW_MOUSE_FACE;
24019 else
24020 overlap_hl = DRAW_NORMAL_TEXT;
24022 j = i;
24023 BUILD_GLYPH_STRINGS (j, start, h, t,
24024 overlap_hl, dummy_x, last_x);
24025 start = i;
24026 compute_overhangs_and_x (t, head->x, 1);
24027 prepend_glyph_string_lists (&head, &tail, h, t);
24028 clip_head = head;
24031 /* Prepend glyph strings for glyphs in front of the first glyph
24032 string that overwrite that glyph string because of their
24033 right overhang. For these strings, only the foreground must
24034 be drawn, because it draws over the glyph string at `head'.
24035 The background must not be drawn because this would overwrite
24036 right overhangs of preceding glyphs for which no glyph
24037 strings exist. */
24038 i = left_overwriting (head);
24039 if (i >= 0)
24041 enum draw_glyphs_face overlap_hl;
24043 if (check_mouse_face
24044 && mouse_beg_col < start && mouse_end_col > i)
24045 overlap_hl = DRAW_MOUSE_FACE;
24046 else
24047 overlap_hl = DRAW_NORMAL_TEXT;
24049 clip_head = head;
24050 BUILD_GLYPH_STRINGS (i, start, h, t,
24051 overlap_hl, dummy_x, last_x);
24052 for (s = h; s; s = s->next)
24053 s->background_filled_p = 1;
24054 compute_overhangs_and_x (t, head->x, 1);
24055 prepend_glyph_string_lists (&head, &tail, h, t);
24058 /* Append glyphs strings for glyphs following the last glyph
24059 string tail that are overwritten by tail. The background of
24060 these strings has to be drawn because tail's foreground draws
24061 over it. */
24062 i = right_overwritten (tail);
24063 if (i >= 0)
24065 enum draw_glyphs_face overlap_hl;
24067 if (check_mouse_face
24068 && mouse_beg_col < i && mouse_end_col > end)
24069 overlap_hl = DRAW_MOUSE_FACE;
24070 else
24071 overlap_hl = DRAW_NORMAL_TEXT;
24073 BUILD_GLYPH_STRINGS (end, i, h, t,
24074 overlap_hl, x, last_x);
24075 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24076 we don't have `end = i;' here. */
24077 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24078 append_glyph_string_lists (&head, &tail, h, t);
24079 clip_tail = tail;
24082 /* Append glyph strings for glyphs following the last glyph
24083 string tail that overwrite tail. The foreground of such
24084 glyphs has to be drawn because it writes into the background
24085 of tail. The background must not be drawn because it could
24086 paint over the foreground of following glyphs. */
24087 i = right_overwriting (tail);
24088 if (i >= 0)
24090 enum draw_glyphs_face overlap_hl;
24091 if (check_mouse_face
24092 && mouse_beg_col < i && mouse_end_col > end)
24093 overlap_hl = DRAW_MOUSE_FACE;
24094 else
24095 overlap_hl = DRAW_NORMAL_TEXT;
24097 clip_tail = tail;
24098 i++; /* We must include the Ith glyph. */
24099 BUILD_GLYPH_STRINGS (end, i, h, t,
24100 overlap_hl, x, last_x);
24101 for (s = h; s; s = s->next)
24102 s->background_filled_p = 1;
24103 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24104 append_glyph_string_lists (&head, &tail, h, t);
24106 if (clip_head || clip_tail)
24107 for (s = head; s; s = s->next)
24109 s->clip_head = clip_head;
24110 s->clip_tail = clip_tail;
24114 /* Draw all strings. */
24115 for (s = head; s; s = s->next)
24116 FRAME_RIF (f)->draw_glyph_string (s);
24118 #ifndef HAVE_NS
24119 /* When focus a sole frame and move horizontally, this sets on_p to 0
24120 causing a failure to erase prev cursor position. */
24121 if (area == TEXT_AREA
24122 && !row->full_width_p
24123 /* When drawing overlapping rows, only the glyph strings'
24124 foreground is drawn, which doesn't erase a cursor
24125 completely. */
24126 && !overlaps)
24128 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24129 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24130 : (tail ? tail->x + tail->background_width : x));
24131 x0 -= area_left;
24132 x1 -= area_left;
24134 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24135 row->y, MATRIX_ROW_BOTTOM_Y (row));
24137 #endif
24139 /* Value is the x-position up to which drawn, relative to AREA of W.
24140 This doesn't include parts drawn because of overhangs. */
24141 if (row->full_width_p)
24142 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24143 else
24144 x_reached -= area_left;
24146 RELEASE_HDC (hdc, f);
24148 return x_reached;
24151 /* Expand row matrix if too narrow. Don't expand if area
24152 is not present. */
24154 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24156 if (!it->f->fonts_changed \
24157 && (it->glyph_row->glyphs[area] \
24158 < it->glyph_row->glyphs[area + 1])) \
24160 it->w->ncols_scale_factor++; \
24161 it->f->fonts_changed = 1; \
24165 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24166 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24168 static void
24169 append_glyph (struct it *it)
24171 struct glyph *glyph;
24172 enum glyph_row_area area = it->area;
24174 eassert (it->glyph_row);
24175 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24177 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24178 if (glyph < it->glyph_row->glyphs[area + 1])
24180 /* If the glyph row is reversed, we need to prepend the glyph
24181 rather than append it. */
24182 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24184 struct glyph *g;
24186 /* Make room for the additional glyph. */
24187 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24188 g[1] = *g;
24189 glyph = it->glyph_row->glyphs[area];
24191 glyph->charpos = CHARPOS (it->position);
24192 glyph->object = it->object;
24193 if (it->pixel_width > 0)
24195 glyph->pixel_width = it->pixel_width;
24196 glyph->padding_p = 0;
24198 else
24200 /* Assure at least 1-pixel width. Otherwise, cursor can't
24201 be displayed correctly. */
24202 glyph->pixel_width = 1;
24203 glyph->padding_p = 1;
24205 glyph->ascent = it->ascent;
24206 glyph->descent = it->descent;
24207 glyph->voffset = it->voffset;
24208 glyph->type = CHAR_GLYPH;
24209 glyph->avoid_cursor_p = it->avoid_cursor_p;
24210 glyph->multibyte_p = it->multibyte_p;
24211 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24213 /* In R2L rows, the left and the right box edges need to be
24214 drawn in reverse direction. */
24215 glyph->right_box_line_p = it->start_of_box_run_p;
24216 glyph->left_box_line_p = it->end_of_box_run_p;
24218 else
24220 glyph->left_box_line_p = it->start_of_box_run_p;
24221 glyph->right_box_line_p = it->end_of_box_run_p;
24223 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24224 || it->phys_descent > it->descent);
24225 glyph->glyph_not_available_p = it->glyph_not_available_p;
24226 glyph->face_id = it->face_id;
24227 glyph->u.ch = it->char_to_display;
24228 glyph->slice.img = null_glyph_slice;
24229 glyph->font_type = FONT_TYPE_UNKNOWN;
24230 if (it->bidi_p)
24232 glyph->resolved_level = it->bidi_it.resolved_level;
24233 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24234 emacs_abort ();
24235 glyph->bidi_type = it->bidi_it.type;
24237 else
24239 glyph->resolved_level = 0;
24240 glyph->bidi_type = UNKNOWN_BT;
24242 ++it->glyph_row->used[area];
24244 else
24245 IT_EXPAND_MATRIX_WIDTH (it, area);
24248 /* Store one glyph for the composition IT->cmp_it.id in
24249 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24250 non-null. */
24252 static void
24253 append_composite_glyph (struct it *it)
24255 struct glyph *glyph;
24256 enum glyph_row_area area = it->area;
24258 eassert (it->glyph_row);
24260 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24261 if (glyph < it->glyph_row->glyphs[area + 1])
24263 /* If the glyph row is reversed, we need to prepend the glyph
24264 rather than append it. */
24265 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24267 struct glyph *g;
24269 /* Make room for the new glyph. */
24270 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24271 g[1] = *g;
24272 glyph = it->glyph_row->glyphs[it->area];
24274 glyph->charpos = it->cmp_it.charpos;
24275 glyph->object = it->object;
24276 glyph->pixel_width = it->pixel_width;
24277 glyph->ascent = it->ascent;
24278 glyph->descent = it->descent;
24279 glyph->voffset = it->voffset;
24280 glyph->type = COMPOSITE_GLYPH;
24281 if (it->cmp_it.ch < 0)
24283 glyph->u.cmp.automatic = 0;
24284 glyph->u.cmp.id = it->cmp_it.id;
24285 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24287 else
24289 glyph->u.cmp.automatic = 1;
24290 glyph->u.cmp.id = it->cmp_it.id;
24291 glyph->slice.cmp.from = it->cmp_it.from;
24292 glyph->slice.cmp.to = it->cmp_it.to - 1;
24294 glyph->avoid_cursor_p = it->avoid_cursor_p;
24295 glyph->multibyte_p = it->multibyte_p;
24296 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24298 /* In R2L rows, the left and the right box edges need to be
24299 drawn in reverse direction. */
24300 glyph->right_box_line_p = it->start_of_box_run_p;
24301 glyph->left_box_line_p = it->end_of_box_run_p;
24303 else
24305 glyph->left_box_line_p = it->start_of_box_run_p;
24306 glyph->right_box_line_p = it->end_of_box_run_p;
24308 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24309 || it->phys_descent > it->descent);
24310 glyph->padding_p = 0;
24311 glyph->glyph_not_available_p = 0;
24312 glyph->face_id = it->face_id;
24313 glyph->font_type = FONT_TYPE_UNKNOWN;
24314 if (it->bidi_p)
24316 glyph->resolved_level = it->bidi_it.resolved_level;
24317 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24318 emacs_abort ();
24319 glyph->bidi_type = it->bidi_it.type;
24321 ++it->glyph_row->used[area];
24323 else
24324 IT_EXPAND_MATRIX_WIDTH (it, area);
24328 /* Change IT->ascent and IT->height according to the setting of
24329 IT->voffset. */
24331 static void
24332 take_vertical_position_into_account (struct it *it)
24334 if (it->voffset)
24336 if (it->voffset < 0)
24337 /* Increase the ascent so that we can display the text higher
24338 in the line. */
24339 it->ascent -= it->voffset;
24340 else
24341 /* Increase the descent so that we can display the text lower
24342 in the line. */
24343 it->descent += it->voffset;
24348 /* Produce glyphs/get display metrics for the image IT is loaded with.
24349 See the description of struct display_iterator in dispextern.h for
24350 an overview of struct display_iterator. */
24352 static void
24353 produce_image_glyph (struct it *it)
24355 struct image *img;
24356 struct face *face;
24357 int glyph_ascent, crop;
24358 struct glyph_slice slice;
24360 eassert (it->what == IT_IMAGE);
24362 face = FACE_FROM_ID (it->f, it->face_id);
24363 eassert (face);
24364 /* Make sure X resources of the face is loaded. */
24365 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24367 if (it->image_id < 0)
24369 /* Fringe bitmap. */
24370 it->ascent = it->phys_ascent = 0;
24371 it->descent = it->phys_descent = 0;
24372 it->pixel_width = 0;
24373 it->nglyphs = 0;
24374 return;
24377 img = IMAGE_FROM_ID (it->f, it->image_id);
24378 eassert (img);
24379 /* Make sure X resources of the image is loaded. */
24380 prepare_image_for_display (it->f, img);
24382 slice.x = slice.y = 0;
24383 slice.width = img->width;
24384 slice.height = img->height;
24386 if (INTEGERP (it->slice.x))
24387 slice.x = XINT (it->slice.x);
24388 else if (FLOATP (it->slice.x))
24389 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24391 if (INTEGERP (it->slice.y))
24392 slice.y = XINT (it->slice.y);
24393 else if (FLOATP (it->slice.y))
24394 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24396 if (INTEGERP (it->slice.width))
24397 slice.width = XINT (it->slice.width);
24398 else if (FLOATP (it->slice.width))
24399 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24401 if (INTEGERP (it->slice.height))
24402 slice.height = XINT (it->slice.height);
24403 else if (FLOATP (it->slice.height))
24404 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24406 if (slice.x >= img->width)
24407 slice.x = img->width;
24408 if (slice.y >= img->height)
24409 slice.y = img->height;
24410 if (slice.x + slice.width >= img->width)
24411 slice.width = img->width - slice.x;
24412 if (slice.y + slice.height > img->height)
24413 slice.height = img->height - slice.y;
24415 if (slice.width == 0 || slice.height == 0)
24416 return;
24418 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24420 it->descent = slice.height - glyph_ascent;
24421 if (slice.y == 0)
24422 it->descent += img->vmargin;
24423 if (slice.y + slice.height == img->height)
24424 it->descent += img->vmargin;
24425 it->phys_descent = it->descent;
24427 it->pixel_width = slice.width;
24428 if (slice.x == 0)
24429 it->pixel_width += img->hmargin;
24430 if (slice.x + slice.width == img->width)
24431 it->pixel_width += img->hmargin;
24433 /* It's quite possible for images to have an ascent greater than
24434 their height, so don't get confused in that case. */
24435 if (it->descent < 0)
24436 it->descent = 0;
24438 it->nglyphs = 1;
24440 if (face->box != FACE_NO_BOX)
24442 if (face->box_line_width > 0)
24444 if (slice.y == 0)
24445 it->ascent += face->box_line_width;
24446 if (slice.y + slice.height == img->height)
24447 it->descent += face->box_line_width;
24450 if (it->start_of_box_run_p && slice.x == 0)
24451 it->pixel_width += eabs (face->box_line_width);
24452 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24453 it->pixel_width += eabs (face->box_line_width);
24456 take_vertical_position_into_account (it);
24458 /* Automatically crop wide image glyphs at right edge so we can
24459 draw the cursor on same display row. */
24460 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24461 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24463 it->pixel_width -= crop;
24464 slice.width -= crop;
24467 if (it->glyph_row)
24469 struct glyph *glyph;
24470 enum glyph_row_area area = it->area;
24472 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24473 if (glyph < it->glyph_row->glyphs[area + 1])
24475 glyph->charpos = CHARPOS (it->position);
24476 glyph->object = it->object;
24477 glyph->pixel_width = it->pixel_width;
24478 glyph->ascent = glyph_ascent;
24479 glyph->descent = it->descent;
24480 glyph->voffset = it->voffset;
24481 glyph->type = IMAGE_GLYPH;
24482 glyph->avoid_cursor_p = it->avoid_cursor_p;
24483 glyph->multibyte_p = it->multibyte_p;
24484 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24486 /* In R2L rows, the left and the right box edges need to be
24487 drawn in reverse direction. */
24488 glyph->right_box_line_p = it->start_of_box_run_p;
24489 glyph->left_box_line_p = it->end_of_box_run_p;
24491 else
24493 glyph->left_box_line_p = it->start_of_box_run_p;
24494 glyph->right_box_line_p = it->end_of_box_run_p;
24496 glyph->overlaps_vertically_p = 0;
24497 glyph->padding_p = 0;
24498 glyph->glyph_not_available_p = 0;
24499 glyph->face_id = it->face_id;
24500 glyph->u.img_id = img->id;
24501 glyph->slice.img = slice;
24502 glyph->font_type = FONT_TYPE_UNKNOWN;
24503 if (it->bidi_p)
24505 glyph->resolved_level = it->bidi_it.resolved_level;
24506 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24507 emacs_abort ();
24508 glyph->bidi_type = it->bidi_it.type;
24510 ++it->glyph_row->used[area];
24512 else
24513 IT_EXPAND_MATRIX_WIDTH (it, area);
24517 #ifdef HAVE_XWIDGETS
24518 static void
24519 produce_xwidget_glyph (struct it *it)
24521 struct xwidget* xw;
24522 struct face *face;
24523 int glyph_ascent, crop;
24524 printf("produce_xwidget_glyph:\n");
24525 eassert (it->what == IT_XWIDGET);
24527 face = FACE_FROM_ID (it->f, it->face_id);
24528 eassert (face);
24529 /* Make sure X resources of the face is loaded. */
24530 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24532 xw = it->xwidget;
24533 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
24534 it->descent = xw->height/2;
24535 it->phys_descent = it->descent;
24536 it->pixel_width = xw->width;
24537 /* It's quite possible for images to have an ascent greater than
24538 their height, so don't get confused in that case. */
24539 if (it->descent < 0)
24540 it->descent = 0;
24542 it->nglyphs = 1;
24544 if (face->box != FACE_NO_BOX)
24546 if (face->box_line_width > 0)
24548 it->ascent += face->box_line_width;
24549 it->descent += face->box_line_width;
24552 if (it->start_of_box_run_p)
24553 it->pixel_width += eabs (face->box_line_width);
24554 it->pixel_width += eabs (face->box_line_width);
24557 take_vertical_position_into_account (it);
24559 /* Automatically crop wide image glyphs at right edge so we can
24560 draw the cursor on same display row. */
24561 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24562 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24564 it->pixel_width -= crop;
24567 if (it->glyph_row)
24569 struct glyph *glyph;
24570 enum glyph_row_area area = it->area;
24572 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24573 if (glyph < it->glyph_row->glyphs[area + 1])
24575 glyph->charpos = CHARPOS (it->position);
24576 glyph->object = it->object;
24577 glyph->pixel_width = it->pixel_width;
24578 glyph->ascent = glyph_ascent;
24579 glyph->descent = it->descent;
24580 glyph->voffset = it->voffset;
24581 glyph->type = XWIDGET_GLYPH;
24583 glyph->multibyte_p = it->multibyte_p;
24584 glyph->left_box_line_p = it->start_of_box_run_p;
24585 glyph->right_box_line_p = it->end_of_box_run_p;
24586 glyph->overlaps_vertically_p = 0;
24587 glyph->padding_p = 0;
24588 glyph->glyph_not_available_p = 0;
24589 glyph->face_id = it->face_id;
24590 glyph->u.xwidget = it->xwidget;
24591 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
24592 glyph->font_type = FONT_TYPE_UNKNOWN;
24593 ++it->glyph_row->used[area];
24595 else
24596 IT_EXPAND_MATRIX_WIDTH (it, area);
24599 #endif
24601 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24602 of the glyph, WIDTH and HEIGHT are the width and height of the
24603 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24605 static void
24606 append_stretch_glyph (struct it *it, Lisp_Object object,
24607 int width, int height, int ascent)
24609 struct glyph *glyph;
24610 enum glyph_row_area area = it->area;
24612 eassert (ascent >= 0 && ascent <= height);
24614 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24615 if (glyph < it->glyph_row->glyphs[area + 1])
24617 /* If the glyph row is reversed, we need to prepend the glyph
24618 rather than append it. */
24619 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24621 struct glyph *g;
24623 /* Make room for the additional glyph. */
24624 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24625 g[1] = *g;
24626 glyph = it->glyph_row->glyphs[area];
24628 glyph->charpos = CHARPOS (it->position);
24629 glyph->object = object;
24630 glyph->pixel_width = width;
24631 glyph->ascent = ascent;
24632 glyph->descent = height - ascent;
24633 glyph->voffset = it->voffset;
24634 glyph->type = STRETCH_GLYPH;
24635 glyph->avoid_cursor_p = it->avoid_cursor_p;
24636 glyph->multibyte_p = it->multibyte_p;
24637 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24639 /* In R2L rows, the left and the right box edges need to be
24640 drawn in reverse direction. */
24641 glyph->right_box_line_p = it->start_of_box_run_p;
24642 glyph->left_box_line_p = it->end_of_box_run_p;
24644 else
24646 glyph->left_box_line_p = it->start_of_box_run_p;
24647 glyph->right_box_line_p = it->end_of_box_run_p;
24649 glyph->overlaps_vertically_p = 0;
24650 glyph->padding_p = 0;
24651 glyph->glyph_not_available_p = 0;
24652 glyph->face_id = it->face_id;
24653 glyph->u.stretch.ascent = ascent;
24654 glyph->u.stretch.height = height;
24655 glyph->slice.img = null_glyph_slice;
24656 glyph->font_type = FONT_TYPE_UNKNOWN;
24657 if (it->bidi_p)
24659 glyph->resolved_level = it->bidi_it.resolved_level;
24660 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24661 emacs_abort ();
24662 glyph->bidi_type = it->bidi_it.type;
24664 else
24666 glyph->resolved_level = 0;
24667 glyph->bidi_type = UNKNOWN_BT;
24669 ++it->glyph_row->used[area];
24671 else
24672 IT_EXPAND_MATRIX_WIDTH (it, area);
24675 #endif /* HAVE_WINDOW_SYSTEM */
24677 /* Produce a stretch glyph for iterator IT. IT->object is the value
24678 of the glyph property displayed. The value must be a list
24679 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24680 being recognized:
24682 1. `:width WIDTH' specifies that the space should be WIDTH *
24683 canonical char width wide. WIDTH may be an integer or floating
24684 point number.
24686 2. `:relative-width FACTOR' specifies that the width of the stretch
24687 should be computed from the width of the first character having the
24688 `glyph' property, and should be FACTOR times that width.
24690 3. `:align-to HPOS' specifies that the space should be wide enough
24691 to reach HPOS, a value in canonical character units.
24693 Exactly one of the above pairs must be present.
24695 4. `:height HEIGHT' specifies that the height of the stretch produced
24696 should be HEIGHT, measured in canonical character units.
24698 5. `:relative-height FACTOR' specifies that the height of the
24699 stretch should be FACTOR times the height of the characters having
24700 the glyph property.
24702 Either none or exactly one of 4 or 5 must be present.
24704 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24705 of the stretch should be used for the ascent of the stretch.
24706 ASCENT must be in the range 0 <= ASCENT <= 100. */
24708 void
24709 produce_stretch_glyph (struct it *it)
24711 /* (space :width WIDTH :height HEIGHT ...) */
24712 Lisp_Object prop, plist;
24713 int width = 0, height = 0, align_to = -1;
24714 int zero_width_ok_p = 0;
24715 double tem;
24716 struct font *font = NULL;
24718 #ifdef HAVE_WINDOW_SYSTEM
24719 int ascent = 0;
24720 int zero_height_ok_p = 0;
24722 if (FRAME_WINDOW_P (it->f))
24724 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24725 font = face->font ? face->font : FRAME_FONT (it->f);
24726 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24728 #endif
24730 /* List should start with `space'. */
24731 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24732 plist = XCDR (it->object);
24734 /* Compute the width of the stretch. */
24735 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24736 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24738 /* Absolute width `:width WIDTH' specified and valid. */
24739 zero_width_ok_p = 1;
24740 width = (int)tem;
24742 #ifdef HAVE_WINDOW_SYSTEM
24743 else if (FRAME_WINDOW_P (it->f)
24744 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24746 /* Relative width `:relative-width FACTOR' specified and valid.
24747 Compute the width of the characters having the `glyph'
24748 property. */
24749 struct it it2;
24750 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24752 it2 = *it;
24753 if (it->multibyte_p)
24754 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24755 else
24757 it2.c = it2.char_to_display = *p, it2.len = 1;
24758 if (! ASCII_CHAR_P (it2.c))
24759 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24762 it2.glyph_row = NULL;
24763 it2.what = IT_CHARACTER;
24764 x_produce_glyphs (&it2);
24765 width = NUMVAL (prop) * it2.pixel_width;
24767 #endif /* HAVE_WINDOW_SYSTEM */
24768 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24769 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24771 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24772 align_to = (align_to < 0
24774 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24775 else if (align_to < 0)
24776 align_to = window_box_left_offset (it->w, TEXT_AREA);
24777 width = max (0, (int)tem + align_to - it->current_x);
24778 zero_width_ok_p = 1;
24780 else
24781 /* Nothing specified -> width defaults to canonical char width. */
24782 width = FRAME_COLUMN_WIDTH (it->f);
24784 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24785 width = 1;
24787 #ifdef HAVE_WINDOW_SYSTEM
24788 /* Compute height. */
24789 if (FRAME_WINDOW_P (it->f))
24791 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24792 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24794 height = (int)tem;
24795 zero_height_ok_p = 1;
24797 else if (prop = Fplist_get (plist, QCrelative_height),
24798 NUMVAL (prop) > 0)
24799 height = FONT_HEIGHT (font) * NUMVAL (prop);
24800 else
24801 height = FONT_HEIGHT (font);
24803 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24804 height = 1;
24806 /* Compute percentage of height used for ascent. If
24807 `:ascent ASCENT' is present and valid, use that. Otherwise,
24808 derive the ascent from the font in use. */
24809 if (prop = Fplist_get (plist, QCascent),
24810 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24811 ascent = height * NUMVAL (prop) / 100.0;
24812 else if (!NILP (prop)
24813 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24814 ascent = min (max (0, (int)tem), height);
24815 else
24816 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24818 else
24819 #endif /* HAVE_WINDOW_SYSTEM */
24820 height = 1;
24822 if (width > 0 && it->line_wrap != TRUNCATE
24823 && it->current_x + width > it->last_visible_x)
24825 width = it->last_visible_x - it->current_x;
24826 #ifdef HAVE_WINDOW_SYSTEM
24827 /* Subtract one more pixel from the stretch width, but only on
24828 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24829 width -= FRAME_WINDOW_P (it->f);
24830 #endif
24833 if (width > 0 && height > 0 && it->glyph_row)
24835 Lisp_Object o_object = it->object;
24836 Lisp_Object object = it->stack[it->sp - 1].string;
24837 int n = width;
24839 if (!STRINGP (object))
24840 object = it->w->contents;
24841 #ifdef HAVE_WINDOW_SYSTEM
24842 if (FRAME_WINDOW_P (it->f))
24843 append_stretch_glyph (it, object, width, height, ascent);
24844 else
24845 #endif
24847 it->object = object;
24848 it->char_to_display = ' ';
24849 it->pixel_width = it->len = 1;
24850 while (n--)
24851 tty_append_glyph (it);
24852 it->object = o_object;
24856 it->pixel_width = width;
24857 #ifdef HAVE_WINDOW_SYSTEM
24858 if (FRAME_WINDOW_P (it->f))
24860 it->ascent = it->phys_ascent = ascent;
24861 it->descent = it->phys_descent = height - it->ascent;
24862 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24863 take_vertical_position_into_account (it);
24865 else
24866 #endif
24867 it->nglyphs = width;
24870 /* Get information about special display element WHAT in an
24871 environment described by IT. WHAT is one of IT_TRUNCATION or
24872 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24873 non-null glyph_row member. This function ensures that fields like
24874 face_id, c, len of IT are left untouched. */
24876 static void
24877 produce_special_glyphs (struct it *it, enum display_element_type what)
24879 struct it temp_it;
24880 Lisp_Object gc;
24881 GLYPH glyph;
24883 temp_it = *it;
24884 temp_it.object = make_number (0);
24885 memset (&temp_it.current, 0, sizeof temp_it.current);
24887 if (what == IT_CONTINUATION)
24889 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24890 if (it->bidi_it.paragraph_dir == R2L)
24891 SET_GLYPH_FROM_CHAR (glyph, '/');
24892 else
24893 SET_GLYPH_FROM_CHAR (glyph, '\\');
24894 if (it->dp
24895 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24897 /* FIXME: Should we mirror GC for R2L lines? */
24898 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24899 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24902 else if (what == IT_TRUNCATION)
24904 /* Truncation glyph. */
24905 SET_GLYPH_FROM_CHAR (glyph, '$');
24906 if (it->dp
24907 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24909 /* FIXME: Should we mirror GC for R2L lines? */
24910 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24911 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24914 else
24915 emacs_abort ();
24917 #ifdef HAVE_WINDOW_SYSTEM
24918 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24919 is turned off, we precede the truncation/continuation glyphs by a
24920 stretch glyph whose width is computed such that these special
24921 glyphs are aligned at the window margin, even when very different
24922 fonts are used in different glyph rows. */
24923 if (FRAME_WINDOW_P (temp_it.f)
24924 /* init_iterator calls this with it->glyph_row == NULL, and it
24925 wants only the pixel width of the truncation/continuation
24926 glyphs. */
24927 && temp_it.glyph_row
24928 /* insert_left_trunc_glyphs calls us at the beginning of the
24929 row, and it has its own calculation of the stretch glyph
24930 width. */
24931 && temp_it.glyph_row->used[TEXT_AREA] > 0
24932 && (temp_it.glyph_row->reversed_p
24933 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24934 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24936 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24938 if (stretch_width > 0)
24940 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24941 struct font *font =
24942 face->font ? face->font : FRAME_FONT (temp_it.f);
24943 int stretch_ascent =
24944 (((temp_it.ascent + temp_it.descent)
24945 * FONT_BASE (font)) / FONT_HEIGHT (font));
24947 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24948 temp_it.ascent + temp_it.descent,
24949 stretch_ascent);
24952 #endif
24954 temp_it.dp = NULL;
24955 temp_it.what = IT_CHARACTER;
24956 temp_it.len = 1;
24957 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24958 temp_it.face_id = GLYPH_FACE (glyph);
24959 temp_it.len = CHAR_BYTES (temp_it.c);
24961 PRODUCE_GLYPHS (&temp_it);
24962 it->pixel_width = temp_it.pixel_width;
24963 it->nglyphs = temp_it.pixel_width;
24966 #ifdef HAVE_WINDOW_SYSTEM
24968 /* Calculate line-height and line-spacing properties.
24969 An integer value specifies explicit pixel value.
24970 A float value specifies relative value to current face height.
24971 A cons (float . face-name) specifies relative value to
24972 height of specified face font.
24974 Returns height in pixels, or nil. */
24977 static Lisp_Object
24978 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24979 int boff, int override)
24981 Lisp_Object face_name = Qnil;
24982 int ascent, descent, height;
24984 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24985 return val;
24987 if (CONSP (val))
24989 face_name = XCAR (val);
24990 val = XCDR (val);
24991 if (!NUMBERP (val))
24992 val = make_number (1);
24993 if (NILP (face_name))
24995 height = it->ascent + it->descent;
24996 goto scale;
25000 if (NILP (face_name))
25002 font = FRAME_FONT (it->f);
25003 boff = FRAME_BASELINE_OFFSET (it->f);
25005 else if (EQ (face_name, Qt))
25007 override = 0;
25009 else
25011 int face_id;
25012 struct face *face;
25014 face_id = lookup_named_face (it->f, face_name, 0);
25015 if (face_id < 0)
25016 return make_number (-1);
25018 face = FACE_FROM_ID (it->f, face_id);
25019 font = face->font;
25020 if (font == NULL)
25021 return make_number (-1);
25022 boff = font->baseline_offset;
25023 if (font->vertical_centering)
25024 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25027 ascent = FONT_BASE (font) + boff;
25028 descent = FONT_DESCENT (font) - boff;
25030 if (override)
25032 it->override_ascent = ascent;
25033 it->override_descent = descent;
25034 it->override_boff = boff;
25037 height = ascent + descent;
25039 scale:
25040 if (FLOATP (val))
25041 height = (int)(XFLOAT_DATA (val) * height);
25042 else if (INTEGERP (val))
25043 height *= XINT (val);
25045 return make_number (height);
25049 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25050 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25051 and only if this is for a character for which no font was found.
25053 If the display method (it->glyphless_method) is
25054 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25055 length of the acronym or the hexadecimal string, UPPER_XOFF and
25056 UPPER_YOFF are pixel offsets for the upper part of the string,
25057 LOWER_XOFF and LOWER_YOFF are for the lower part.
25059 For the other display methods, LEN through LOWER_YOFF are zero. */
25061 static void
25062 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25063 short upper_xoff, short upper_yoff,
25064 short lower_xoff, short lower_yoff)
25066 struct glyph *glyph;
25067 enum glyph_row_area area = it->area;
25069 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25070 if (glyph < it->glyph_row->glyphs[area + 1])
25072 /* If the glyph row is reversed, we need to prepend the glyph
25073 rather than append it. */
25074 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25076 struct glyph *g;
25078 /* Make room for the additional glyph. */
25079 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25080 g[1] = *g;
25081 glyph = it->glyph_row->glyphs[area];
25083 glyph->charpos = CHARPOS (it->position);
25084 glyph->object = it->object;
25085 glyph->pixel_width = it->pixel_width;
25086 glyph->ascent = it->ascent;
25087 glyph->descent = it->descent;
25088 glyph->voffset = it->voffset;
25089 glyph->type = GLYPHLESS_GLYPH;
25090 glyph->u.glyphless.method = it->glyphless_method;
25091 glyph->u.glyphless.for_no_font = for_no_font;
25092 glyph->u.glyphless.len = len;
25093 glyph->u.glyphless.ch = it->c;
25094 glyph->slice.glyphless.upper_xoff = upper_xoff;
25095 glyph->slice.glyphless.upper_yoff = upper_yoff;
25096 glyph->slice.glyphless.lower_xoff = lower_xoff;
25097 glyph->slice.glyphless.lower_yoff = lower_yoff;
25098 glyph->avoid_cursor_p = it->avoid_cursor_p;
25099 glyph->multibyte_p = it->multibyte_p;
25100 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25102 /* In R2L rows, the left and the right box edges need to be
25103 drawn in reverse direction. */
25104 glyph->right_box_line_p = it->start_of_box_run_p;
25105 glyph->left_box_line_p = it->end_of_box_run_p;
25107 else
25109 glyph->left_box_line_p = it->start_of_box_run_p;
25110 glyph->right_box_line_p = it->end_of_box_run_p;
25112 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25113 || it->phys_descent > it->descent);
25114 glyph->padding_p = 0;
25115 glyph->glyph_not_available_p = 0;
25116 glyph->face_id = face_id;
25117 glyph->font_type = FONT_TYPE_UNKNOWN;
25118 if (it->bidi_p)
25120 glyph->resolved_level = it->bidi_it.resolved_level;
25121 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25122 emacs_abort ();
25123 glyph->bidi_type = it->bidi_it.type;
25125 ++it->glyph_row->used[area];
25127 else
25128 IT_EXPAND_MATRIX_WIDTH (it, area);
25132 /* Produce a glyph for a glyphless character for iterator IT.
25133 IT->glyphless_method specifies which method to use for displaying
25134 the character. See the description of enum
25135 glyphless_display_method in dispextern.h for the detail.
25137 FOR_NO_FONT is nonzero if and only if this is for a character for
25138 which no font was found. ACRONYM, if non-nil, is an acronym string
25139 for the character. */
25141 static void
25142 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25144 int face_id;
25145 struct face *face;
25146 struct font *font;
25147 int base_width, base_height, width, height;
25148 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25149 int len;
25151 /* Get the metrics of the base font. We always refer to the current
25152 ASCII face. */
25153 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25154 font = face->font ? face->font : FRAME_FONT (it->f);
25155 it->ascent = FONT_BASE (font) + font->baseline_offset;
25156 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25157 base_height = it->ascent + it->descent;
25158 base_width = font->average_width;
25160 face_id = merge_glyphless_glyph_face (it);
25162 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25164 it->pixel_width = THIN_SPACE_WIDTH;
25165 len = 0;
25166 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25168 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25170 width = CHAR_WIDTH (it->c);
25171 if (width == 0)
25172 width = 1;
25173 else if (width > 4)
25174 width = 4;
25175 it->pixel_width = base_width * width;
25176 len = 0;
25177 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25179 else
25181 char buf[7];
25182 const char *str;
25183 unsigned int code[6];
25184 int upper_len;
25185 int ascent, descent;
25186 struct font_metrics metrics_upper, metrics_lower;
25188 face = FACE_FROM_ID (it->f, face_id);
25189 font = face->font ? face->font : FRAME_FONT (it->f);
25190 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25192 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25194 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25195 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25196 if (CONSP (acronym))
25197 acronym = XCAR (acronym);
25198 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25200 else
25202 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25203 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25204 str = buf;
25206 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25207 code[len] = font->driver->encode_char (font, str[len]);
25208 upper_len = (len + 1) / 2;
25209 font->driver->text_extents (font, code, upper_len,
25210 &metrics_upper);
25211 font->driver->text_extents (font, code + upper_len, len - upper_len,
25212 &metrics_lower);
25216 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25217 width = max (metrics_upper.width, metrics_lower.width) + 4;
25218 upper_xoff = upper_yoff = 2; /* the typical case */
25219 if (base_width >= width)
25221 /* Align the upper to the left, the lower to the right. */
25222 it->pixel_width = base_width;
25223 lower_xoff = base_width - 2 - metrics_lower.width;
25225 else
25227 /* Center the shorter one. */
25228 it->pixel_width = width;
25229 if (metrics_upper.width >= metrics_lower.width)
25230 lower_xoff = (width - metrics_lower.width) / 2;
25231 else
25233 /* FIXME: This code doesn't look right. It formerly was
25234 missing the "lower_xoff = 0;", which couldn't have
25235 been right since it left lower_xoff uninitialized. */
25236 lower_xoff = 0;
25237 upper_xoff = (width - metrics_upper.width) / 2;
25241 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25242 top, bottom, and between upper and lower strings. */
25243 height = (metrics_upper.ascent + metrics_upper.descent
25244 + metrics_lower.ascent + metrics_lower.descent) + 5;
25245 /* Center vertically.
25246 H:base_height, D:base_descent
25247 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25249 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25250 descent = D - H/2 + h/2;
25251 lower_yoff = descent - 2 - ld;
25252 upper_yoff = lower_yoff - la - 1 - ud; */
25253 ascent = - (it->descent - (base_height + height + 1) / 2);
25254 descent = it->descent - (base_height - height) / 2;
25255 lower_yoff = descent - 2 - metrics_lower.descent;
25256 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25257 - metrics_upper.descent);
25258 /* Don't make the height shorter than the base height. */
25259 if (height > base_height)
25261 it->ascent = ascent;
25262 it->descent = descent;
25266 it->phys_ascent = it->ascent;
25267 it->phys_descent = it->descent;
25268 if (it->glyph_row)
25269 append_glyphless_glyph (it, face_id, for_no_font, len,
25270 upper_xoff, upper_yoff,
25271 lower_xoff, lower_yoff);
25272 it->nglyphs = 1;
25273 take_vertical_position_into_account (it);
25277 /* RIF:
25278 Produce glyphs/get display metrics for the display element IT is
25279 loaded with. See the description of struct it in dispextern.h
25280 for an overview of struct it. */
25282 void
25283 x_produce_glyphs (struct it *it)
25285 int extra_line_spacing = it->extra_line_spacing;
25287 it->glyph_not_available_p = 0;
25289 if (it->what == IT_CHARACTER)
25291 XChar2b char2b;
25292 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25293 struct font *font = face->font;
25294 struct font_metrics *pcm = NULL;
25295 int boff; /* baseline offset */
25297 if (font == NULL)
25299 /* When no suitable font is found, display this character by
25300 the method specified in the first extra slot of
25301 Vglyphless_char_display. */
25302 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25304 eassert (it->what == IT_GLYPHLESS);
25305 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25306 goto done;
25309 boff = font->baseline_offset;
25310 if (font->vertical_centering)
25311 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25313 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25315 int stretched_p;
25317 it->nglyphs = 1;
25319 if (it->override_ascent >= 0)
25321 it->ascent = it->override_ascent;
25322 it->descent = it->override_descent;
25323 boff = it->override_boff;
25325 else
25327 it->ascent = FONT_BASE (font) + boff;
25328 it->descent = FONT_DESCENT (font) - boff;
25331 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25333 pcm = get_per_char_metric (font, &char2b);
25334 if (pcm->width == 0
25335 && pcm->rbearing == 0 && pcm->lbearing == 0)
25336 pcm = NULL;
25339 if (pcm)
25341 it->phys_ascent = pcm->ascent + boff;
25342 it->phys_descent = pcm->descent - boff;
25343 it->pixel_width = pcm->width;
25345 else
25347 it->glyph_not_available_p = 1;
25348 it->phys_ascent = it->ascent;
25349 it->phys_descent = it->descent;
25350 it->pixel_width = font->space_width;
25353 if (it->constrain_row_ascent_descent_p)
25355 if (it->descent > it->max_descent)
25357 it->ascent += it->descent - it->max_descent;
25358 it->descent = it->max_descent;
25360 if (it->ascent > it->max_ascent)
25362 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25363 it->ascent = it->max_ascent;
25365 it->phys_ascent = min (it->phys_ascent, it->ascent);
25366 it->phys_descent = min (it->phys_descent, it->descent);
25367 extra_line_spacing = 0;
25370 /* If this is a space inside a region of text with
25371 `space-width' property, change its width. */
25372 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25373 if (stretched_p)
25374 it->pixel_width *= XFLOATINT (it->space_width);
25376 /* If face has a box, add the box thickness to the character
25377 height. If character has a box line to the left and/or
25378 right, add the box line width to the character's width. */
25379 if (face->box != FACE_NO_BOX)
25381 int thick = face->box_line_width;
25383 if (thick > 0)
25385 it->ascent += thick;
25386 it->descent += thick;
25388 else
25389 thick = -thick;
25391 if (it->start_of_box_run_p)
25392 it->pixel_width += thick;
25393 if (it->end_of_box_run_p)
25394 it->pixel_width += thick;
25397 /* If face has an overline, add the height of the overline
25398 (1 pixel) and a 1 pixel margin to the character height. */
25399 if (face->overline_p)
25400 it->ascent += overline_margin;
25402 if (it->constrain_row_ascent_descent_p)
25404 if (it->ascent > it->max_ascent)
25405 it->ascent = it->max_ascent;
25406 if (it->descent > it->max_descent)
25407 it->descent = it->max_descent;
25410 take_vertical_position_into_account (it);
25412 /* If we have to actually produce glyphs, do it. */
25413 if (it->glyph_row)
25415 if (stretched_p)
25417 /* Translate a space with a `space-width' property
25418 into a stretch glyph. */
25419 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25420 / FONT_HEIGHT (font));
25421 append_stretch_glyph (it, it->object, it->pixel_width,
25422 it->ascent + it->descent, ascent);
25424 else
25425 append_glyph (it);
25427 /* If characters with lbearing or rbearing are displayed
25428 in this line, record that fact in a flag of the
25429 glyph row. This is used to optimize X output code. */
25430 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25431 it->glyph_row->contains_overlapping_glyphs_p = 1;
25433 if (! stretched_p && it->pixel_width == 0)
25434 /* We assure that all visible glyphs have at least 1-pixel
25435 width. */
25436 it->pixel_width = 1;
25438 else if (it->char_to_display == '\n')
25440 /* A newline has no width, but we need the height of the
25441 line. But if previous part of the line sets a height,
25442 don't increase that height */
25444 Lisp_Object height;
25445 Lisp_Object total_height = Qnil;
25447 it->override_ascent = -1;
25448 it->pixel_width = 0;
25449 it->nglyphs = 0;
25451 height = get_it_property (it, Qline_height);
25452 /* Split (line-height total-height) list */
25453 if (CONSP (height)
25454 && CONSP (XCDR (height))
25455 && NILP (XCDR (XCDR (height))))
25457 total_height = XCAR (XCDR (height));
25458 height = XCAR (height);
25460 height = calc_line_height_property (it, height, font, boff, 1);
25462 if (it->override_ascent >= 0)
25464 it->ascent = it->override_ascent;
25465 it->descent = it->override_descent;
25466 boff = it->override_boff;
25468 else
25470 it->ascent = FONT_BASE (font) + boff;
25471 it->descent = FONT_DESCENT (font) - boff;
25474 if (EQ (height, Qt))
25476 if (it->descent > it->max_descent)
25478 it->ascent += it->descent - it->max_descent;
25479 it->descent = it->max_descent;
25481 if (it->ascent > it->max_ascent)
25483 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25484 it->ascent = it->max_ascent;
25486 it->phys_ascent = min (it->phys_ascent, it->ascent);
25487 it->phys_descent = min (it->phys_descent, it->descent);
25488 it->constrain_row_ascent_descent_p = 1;
25489 extra_line_spacing = 0;
25491 else
25493 Lisp_Object spacing;
25495 it->phys_ascent = it->ascent;
25496 it->phys_descent = it->descent;
25498 if ((it->max_ascent > 0 || it->max_descent > 0)
25499 && face->box != FACE_NO_BOX
25500 && face->box_line_width > 0)
25502 it->ascent += face->box_line_width;
25503 it->descent += face->box_line_width;
25505 if (!NILP (height)
25506 && XINT (height) > it->ascent + it->descent)
25507 it->ascent = XINT (height) - it->descent;
25509 if (!NILP (total_height))
25510 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25511 else
25513 spacing = get_it_property (it, Qline_spacing);
25514 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25516 if (INTEGERP (spacing))
25518 extra_line_spacing = XINT (spacing);
25519 if (!NILP (total_height))
25520 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25524 else /* i.e. (it->char_to_display == '\t') */
25526 if (font->space_width > 0)
25528 int tab_width = it->tab_width * font->space_width;
25529 int x = it->current_x + it->continuation_lines_width;
25530 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25532 /* If the distance from the current position to the next tab
25533 stop is less than a space character width, use the
25534 tab stop after that. */
25535 if (next_tab_x - x < font->space_width)
25536 next_tab_x += tab_width;
25538 it->pixel_width = next_tab_x - x;
25539 it->nglyphs = 1;
25540 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25541 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25543 if (it->glyph_row)
25545 append_stretch_glyph (it, it->object, it->pixel_width,
25546 it->ascent + it->descent, it->ascent);
25549 else
25551 it->pixel_width = 0;
25552 it->nglyphs = 1;
25556 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25558 /* A static composition.
25560 Note: A composition is represented as one glyph in the
25561 glyph matrix. There are no padding glyphs.
25563 Important note: pixel_width, ascent, and descent are the
25564 values of what is drawn by draw_glyphs (i.e. the values of
25565 the overall glyphs composed). */
25566 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25567 int boff; /* baseline offset */
25568 struct composition *cmp = composition_table[it->cmp_it.id];
25569 int glyph_len = cmp->glyph_len;
25570 struct font *font = face->font;
25572 it->nglyphs = 1;
25574 /* If we have not yet calculated pixel size data of glyphs of
25575 the composition for the current face font, calculate them
25576 now. Theoretically, we have to check all fonts for the
25577 glyphs, but that requires much time and memory space. So,
25578 here we check only the font of the first glyph. This may
25579 lead to incorrect display, but it's very rare, and C-l
25580 (recenter-top-bottom) can correct the display anyway. */
25581 if (! cmp->font || cmp->font != font)
25583 /* Ascent and descent of the font of the first character
25584 of this composition (adjusted by baseline offset).
25585 Ascent and descent of overall glyphs should not be less
25586 than these, respectively. */
25587 int font_ascent, font_descent, font_height;
25588 /* Bounding box of the overall glyphs. */
25589 int leftmost, rightmost, lowest, highest;
25590 int lbearing, rbearing;
25591 int i, width, ascent, descent;
25592 int left_padded = 0, right_padded = 0;
25593 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25594 XChar2b char2b;
25595 struct font_metrics *pcm;
25596 int font_not_found_p;
25597 ptrdiff_t pos;
25599 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25600 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25601 break;
25602 if (glyph_len < cmp->glyph_len)
25603 right_padded = 1;
25604 for (i = 0; i < glyph_len; i++)
25606 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25607 break;
25608 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25610 if (i > 0)
25611 left_padded = 1;
25613 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25614 : IT_CHARPOS (*it));
25615 /* If no suitable font is found, use the default font. */
25616 font_not_found_p = font == NULL;
25617 if (font_not_found_p)
25619 face = face->ascii_face;
25620 font = face->font;
25622 boff = font->baseline_offset;
25623 if (font->vertical_centering)
25624 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25625 font_ascent = FONT_BASE (font) + boff;
25626 font_descent = FONT_DESCENT (font) - boff;
25627 font_height = FONT_HEIGHT (font);
25629 cmp->font = font;
25631 pcm = NULL;
25632 if (! font_not_found_p)
25634 get_char_face_and_encoding (it->f, c, it->face_id,
25635 &char2b, 0);
25636 pcm = get_per_char_metric (font, &char2b);
25639 /* Initialize the bounding box. */
25640 if (pcm)
25642 width = cmp->glyph_len > 0 ? pcm->width : 0;
25643 ascent = pcm->ascent;
25644 descent = pcm->descent;
25645 lbearing = pcm->lbearing;
25646 rbearing = pcm->rbearing;
25648 else
25650 width = cmp->glyph_len > 0 ? font->space_width : 0;
25651 ascent = FONT_BASE (font);
25652 descent = FONT_DESCENT (font);
25653 lbearing = 0;
25654 rbearing = width;
25657 rightmost = width;
25658 leftmost = 0;
25659 lowest = - descent + boff;
25660 highest = ascent + boff;
25662 if (! font_not_found_p
25663 && font->default_ascent
25664 && CHAR_TABLE_P (Vuse_default_ascent)
25665 && !NILP (Faref (Vuse_default_ascent,
25666 make_number (it->char_to_display))))
25667 highest = font->default_ascent + boff;
25669 /* Draw the first glyph at the normal position. It may be
25670 shifted to right later if some other glyphs are drawn
25671 at the left. */
25672 cmp->offsets[i * 2] = 0;
25673 cmp->offsets[i * 2 + 1] = boff;
25674 cmp->lbearing = lbearing;
25675 cmp->rbearing = rbearing;
25677 /* Set cmp->offsets for the remaining glyphs. */
25678 for (i++; i < glyph_len; i++)
25680 int left, right, btm, top;
25681 int ch = COMPOSITION_GLYPH (cmp, i);
25682 int face_id;
25683 struct face *this_face;
25685 if (ch == '\t')
25686 ch = ' ';
25687 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25688 this_face = FACE_FROM_ID (it->f, face_id);
25689 font = this_face->font;
25691 if (font == NULL)
25692 pcm = NULL;
25693 else
25695 get_char_face_and_encoding (it->f, ch, face_id,
25696 &char2b, 0);
25697 pcm = get_per_char_metric (font, &char2b);
25699 if (! pcm)
25700 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25701 else
25703 width = pcm->width;
25704 ascent = pcm->ascent;
25705 descent = pcm->descent;
25706 lbearing = pcm->lbearing;
25707 rbearing = pcm->rbearing;
25708 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25710 /* Relative composition with or without
25711 alternate chars. */
25712 left = (leftmost + rightmost - width) / 2;
25713 btm = - descent + boff;
25714 if (font->relative_compose
25715 && (! CHAR_TABLE_P (Vignore_relative_composition)
25716 || NILP (Faref (Vignore_relative_composition,
25717 make_number (ch)))))
25720 if (- descent >= font->relative_compose)
25721 /* One extra pixel between two glyphs. */
25722 btm = highest + 1;
25723 else if (ascent <= 0)
25724 /* One extra pixel between two glyphs. */
25725 btm = lowest - 1 - ascent - descent;
25728 else
25730 /* A composition rule is specified by an integer
25731 value that encodes global and new reference
25732 points (GREF and NREF). GREF and NREF are
25733 specified by numbers as below:
25735 0---1---2 -- ascent
25739 9--10--11 -- center
25741 ---3---4---5--- baseline
25743 6---7---8 -- descent
25745 int rule = COMPOSITION_RULE (cmp, i);
25746 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25748 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25749 grefx = gref % 3, nrefx = nref % 3;
25750 grefy = gref / 3, nrefy = nref / 3;
25751 if (xoff)
25752 xoff = font_height * (xoff - 128) / 256;
25753 if (yoff)
25754 yoff = font_height * (yoff - 128) / 256;
25756 left = (leftmost
25757 + grefx * (rightmost - leftmost) / 2
25758 - nrefx * width / 2
25759 + xoff);
25761 btm = ((grefy == 0 ? highest
25762 : grefy == 1 ? 0
25763 : grefy == 2 ? lowest
25764 : (highest + lowest) / 2)
25765 - (nrefy == 0 ? ascent + descent
25766 : nrefy == 1 ? descent - boff
25767 : nrefy == 2 ? 0
25768 : (ascent + descent) / 2)
25769 + yoff);
25772 cmp->offsets[i * 2] = left;
25773 cmp->offsets[i * 2 + 1] = btm + descent;
25775 /* Update the bounding box of the overall glyphs. */
25776 if (width > 0)
25778 right = left + width;
25779 if (left < leftmost)
25780 leftmost = left;
25781 if (right > rightmost)
25782 rightmost = right;
25784 top = btm + descent + ascent;
25785 if (top > highest)
25786 highest = top;
25787 if (btm < lowest)
25788 lowest = btm;
25790 if (cmp->lbearing > left + lbearing)
25791 cmp->lbearing = left + lbearing;
25792 if (cmp->rbearing < left + rbearing)
25793 cmp->rbearing = left + rbearing;
25797 /* If there are glyphs whose x-offsets are negative,
25798 shift all glyphs to the right and make all x-offsets
25799 non-negative. */
25800 if (leftmost < 0)
25802 for (i = 0; i < cmp->glyph_len; i++)
25803 cmp->offsets[i * 2] -= leftmost;
25804 rightmost -= leftmost;
25805 cmp->lbearing -= leftmost;
25806 cmp->rbearing -= leftmost;
25809 if (left_padded && cmp->lbearing < 0)
25811 for (i = 0; i < cmp->glyph_len; i++)
25812 cmp->offsets[i * 2] -= cmp->lbearing;
25813 rightmost -= cmp->lbearing;
25814 cmp->rbearing -= cmp->lbearing;
25815 cmp->lbearing = 0;
25817 if (right_padded && rightmost < cmp->rbearing)
25819 rightmost = cmp->rbearing;
25822 cmp->pixel_width = rightmost;
25823 cmp->ascent = highest;
25824 cmp->descent = - lowest;
25825 if (cmp->ascent < font_ascent)
25826 cmp->ascent = font_ascent;
25827 if (cmp->descent < font_descent)
25828 cmp->descent = font_descent;
25831 if (it->glyph_row
25832 && (cmp->lbearing < 0
25833 || cmp->rbearing > cmp->pixel_width))
25834 it->glyph_row->contains_overlapping_glyphs_p = 1;
25836 it->pixel_width = cmp->pixel_width;
25837 it->ascent = it->phys_ascent = cmp->ascent;
25838 it->descent = it->phys_descent = cmp->descent;
25839 if (face->box != FACE_NO_BOX)
25841 int thick = face->box_line_width;
25843 if (thick > 0)
25845 it->ascent += thick;
25846 it->descent += thick;
25848 else
25849 thick = - thick;
25851 if (it->start_of_box_run_p)
25852 it->pixel_width += thick;
25853 if (it->end_of_box_run_p)
25854 it->pixel_width += thick;
25857 /* If face has an overline, add the height of the overline
25858 (1 pixel) and a 1 pixel margin to the character height. */
25859 if (face->overline_p)
25860 it->ascent += overline_margin;
25862 take_vertical_position_into_account (it);
25863 if (it->ascent < 0)
25864 it->ascent = 0;
25865 if (it->descent < 0)
25866 it->descent = 0;
25868 if (it->glyph_row && cmp->glyph_len > 0)
25869 append_composite_glyph (it);
25871 else if (it->what == IT_COMPOSITION)
25873 /* A dynamic (automatic) composition. */
25874 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25875 Lisp_Object gstring;
25876 struct font_metrics metrics;
25878 it->nglyphs = 1;
25880 gstring = composition_gstring_from_id (it->cmp_it.id);
25881 it->pixel_width
25882 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25883 &metrics);
25884 if (it->glyph_row
25885 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25886 it->glyph_row->contains_overlapping_glyphs_p = 1;
25887 it->ascent = it->phys_ascent = metrics.ascent;
25888 it->descent = it->phys_descent = metrics.descent;
25889 if (face->box != FACE_NO_BOX)
25891 int thick = face->box_line_width;
25893 if (thick > 0)
25895 it->ascent += thick;
25896 it->descent += thick;
25898 else
25899 thick = - thick;
25901 if (it->start_of_box_run_p)
25902 it->pixel_width += thick;
25903 if (it->end_of_box_run_p)
25904 it->pixel_width += thick;
25906 /* If face has an overline, add the height of the overline
25907 (1 pixel) and a 1 pixel margin to the character height. */
25908 if (face->overline_p)
25909 it->ascent += overline_margin;
25910 take_vertical_position_into_account (it);
25911 if (it->ascent < 0)
25912 it->ascent = 0;
25913 if (it->descent < 0)
25914 it->descent = 0;
25916 if (it->glyph_row)
25917 append_composite_glyph (it);
25919 else if (it->what == IT_GLYPHLESS)
25920 produce_glyphless_glyph (it, 0, Qnil);
25921 else if (it->what == IT_IMAGE)
25922 produce_image_glyph (it);
25923 else if (it->what == IT_STRETCH)
25924 produce_stretch_glyph (it);
25925 #ifdef HAVE_XWIDGETS
25926 else if (it->what == IT_XWIDGET)
25927 produce_xwidget_glyph (it);
25928 #endif
25929 done:
25930 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25931 because this isn't true for images with `:ascent 100'. */
25932 eassert (it->ascent >= 0 && it->descent >= 0);
25933 if (it->area == TEXT_AREA)
25934 it->current_x += it->pixel_width;
25936 if (extra_line_spacing > 0)
25938 it->descent += extra_line_spacing;
25939 if (extra_line_spacing > it->max_extra_line_spacing)
25940 it->max_extra_line_spacing = extra_line_spacing;
25943 it->max_ascent = max (it->max_ascent, it->ascent);
25944 it->max_descent = max (it->max_descent, it->descent);
25945 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25946 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25949 /* EXPORT for RIF:
25950 Output LEN glyphs starting at START at the nominal cursor position.
25951 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25952 being updated, and UPDATED_AREA is the area of that row being updated. */
25954 void
25955 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25956 struct glyph *start, enum glyph_row_area updated_area, int len)
25958 int x, hpos, chpos = w->phys_cursor.hpos;
25960 eassert (updated_row);
25961 /* When the window is hscrolled, cursor hpos can legitimately be out
25962 of bounds, but we draw the cursor at the corresponding window
25963 margin in that case. */
25964 if (!updated_row->reversed_p && chpos < 0)
25965 chpos = 0;
25966 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25967 chpos = updated_row->used[TEXT_AREA] - 1;
25969 block_input ();
25971 /* Write glyphs. */
25973 hpos = start - updated_row->glyphs[updated_area];
25974 x = draw_glyphs (w, w->output_cursor.x,
25975 updated_row, updated_area,
25976 hpos, hpos + len,
25977 DRAW_NORMAL_TEXT, 0);
25979 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25980 if (updated_area == TEXT_AREA
25981 && w->phys_cursor_on_p
25982 && w->phys_cursor.vpos == w->output_cursor.vpos
25983 && chpos >= hpos
25984 && chpos < hpos + len)
25985 w->phys_cursor_on_p = 0;
25987 unblock_input ();
25989 /* Advance the output cursor. */
25990 w->output_cursor.hpos += len;
25991 w->output_cursor.x = x;
25995 /* EXPORT for RIF:
25996 Insert LEN glyphs from START at the nominal cursor position. */
25998 void
25999 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26000 struct glyph *start, enum glyph_row_area updated_area, int len)
26002 struct frame *f;
26003 int line_height, shift_by_width, shifted_region_width;
26004 struct glyph_row *row;
26005 struct glyph *glyph;
26006 int frame_x, frame_y;
26007 ptrdiff_t hpos;
26009 eassert (updated_row);
26010 block_input ();
26011 f = XFRAME (WINDOW_FRAME (w));
26013 /* Get the height of the line we are in. */
26014 row = updated_row;
26015 line_height = row->height;
26017 /* Get the width of the glyphs to insert. */
26018 shift_by_width = 0;
26019 for (glyph = start; glyph < start + len; ++glyph)
26020 shift_by_width += glyph->pixel_width;
26022 /* Get the width of the region to shift right. */
26023 shifted_region_width = (window_box_width (w, updated_area)
26024 - w->output_cursor.x
26025 - shift_by_width);
26027 /* Shift right. */
26028 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26029 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26031 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26032 line_height, shift_by_width);
26034 /* Write the glyphs. */
26035 hpos = start - row->glyphs[updated_area];
26036 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26037 hpos, hpos + len,
26038 DRAW_NORMAL_TEXT, 0);
26040 /* Advance the output cursor. */
26041 w->output_cursor.hpos += len;
26042 w->output_cursor.x += shift_by_width;
26043 unblock_input ();
26047 /* EXPORT for RIF:
26048 Erase the current text line from the nominal cursor position
26049 (inclusive) to pixel column TO_X (exclusive). The idea is that
26050 everything from TO_X onward is already erased.
26052 TO_X is a pixel position relative to UPDATED_AREA of currently
26053 updated window W. TO_X == -1 means clear to the end of this area. */
26055 void
26056 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26057 enum glyph_row_area updated_area, int to_x)
26059 struct frame *f;
26060 int max_x, min_y, max_y;
26061 int from_x, from_y, to_y;
26063 eassert (updated_row);
26064 f = XFRAME (w->frame);
26066 if (updated_row->full_width_p)
26067 max_x = WINDOW_TOTAL_WIDTH (w);
26068 else
26069 max_x = window_box_width (w, updated_area);
26070 max_y = window_text_bottom_y (w);
26072 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26073 of window. For TO_X > 0, truncate to end of drawing area. */
26074 if (to_x == 0)
26075 return;
26076 else if (to_x < 0)
26077 to_x = max_x;
26078 else
26079 to_x = min (to_x, max_x);
26081 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26083 /* Notice if the cursor will be cleared by this operation. */
26084 if (!updated_row->full_width_p)
26085 notice_overwritten_cursor (w, updated_area,
26086 w->output_cursor.x, -1,
26087 updated_row->y,
26088 MATRIX_ROW_BOTTOM_Y (updated_row));
26090 from_x = w->output_cursor.x;
26092 /* Translate to frame coordinates. */
26093 if (updated_row->full_width_p)
26095 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26096 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26098 else
26100 int area_left = window_box_left (w, updated_area);
26101 from_x += area_left;
26102 to_x += area_left;
26105 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26106 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26107 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26109 /* Prevent inadvertently clearing to end of the X window. */
26110 if (to_x > from_x && to_y > from_y)
26112 block_input ();
26113 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26114 to_x - from_x, to_y - from_y);
26115 unblock_input ();
26119 #endif /* HAVE_WINDOW_SYSTEM */
26123 /***********************************************************************
26124 Cursor types
26125 ***********************************************************************/
26127 /* Value is the internal representation of the specified cursor type
26128 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26129 of the bar cursor. */
26131 static enum text_cursor_kinds
26132 get_specified_cursor_type (Lisp_Object arg, int *width)
26134 enum text_cursor_kinds type;
26136 if (NILP (arg))
26137 return NO_CURSOR;
26139 if (EQ (arg, Qbox))
26140 return FILLED_BOX_CURSOR;
26142 if (EQ (arg, Qhollow))
26143 return HOLLOW_BOX_CURSOR;
26145 if (EQ (arg, Qbar))
26147 *width = 2;
26148 return BAR_CURSOR;
26151 if (CONSP (arg)
26152 && EQ (XCAR (arg), Qbar)
26153 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26155 *width = XINT (XCDR (arg));
26156 return BAR_CURSOR;
26159 if (EQ (arg, Qhbar))
26161 *width = 2;
26162 return HBAR_CURSOR;
26165 if (CONSP (arg)
26166 && EQ (XCAR (arg), Qhbar)
26167 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26169 *width = XINT (XCDR (arg));
26170 return HBAR_CURSOR;
26173 /* Treat anything unknown as "hollow box cursor".
26174 It was bad to signal an error; people have trouble fixing
26175 .Xdefaults with Emacs, when it has something bad in it. */
26176 type = HOLLOW_BOX_CURSOR;
26178 return type;
26181 /* Set the default cursor types for specified frame. */
26182 void
26183 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26185 int width = 1;
26186 Lisp_Object tem;
26188 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26189 FRAME_CURSOR_WIDTH (f) = width;
26191 /* By default, set up the blink-off state depending on the on-state. */
26193 tem = Fassoc (arg, Vblink_cursor_alist);
26194 if (!NILP (tem))
26196 FRAME_BLINK_OFF_CURSOR (f)
26197 = get_specified_cursor_type (XCDR (tem), &width);
26198 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26200 else
26201 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26203 /* Make sure the cursor gets redrawn. */
26204 f->cursor_type_changed = 1;
26208 #ifdef HAVE_WINDOW_SYSTEM
26210 /* Return the cursor we want to be displayed in window W. Return
26211 width of bar/hbar cursor through WIDTH arg. Return with
26212 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26213 (i.e. if the `system caret' should track this cursor).
26215 In a mini-buffer window, we want the cursor only to appear if we
26216 are reading input from this window. For the selected window, we
26217 want the cursor type given by the frame parameter or buffer local
26218 setting of cursor-type. If explicitly marked off, draw no cursor.
26219 In all other cases, we want a hollow box cursor. */
26221 static enum text_cursor_kinds
26222 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26223 int *active_cursor)
26225 struct frame *f = XFRAME (w->frame);
26226 struct buffer *b = XBUFFER (w->contents);
26227 int cursor_type = DEFAULT_CURSOR;
26228 Lisp_Object alt_cursor;
26229 int non_selected = 0;
26231 *active_cursor = 1;
26233 /* Echo area */
26234 if (cursor_in_echo_area
26235 && FRAME_HAS_MINIBUF_P (f)
26236 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26238 if (w == XWINDOW (echo_area_window))
26240 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26242 *width = FRAME_CURSOR_WIDTH (f);
26243 return FRAME_DESIRED_CURSOR (f);
26245 else
26246 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26249 *active_cursor = 0;
26250 non_selected = 1;
26253 /* Detect a nonselected window or nonselected frame. */
26254 else if (w != XWINDOW (f->selected_window)
26255 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26257 *active_cursor = 0;
26259 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26260 return NO_CURSOR;
26262 non_selected = 1;
26265 /* Never display a cursor in a window in which cursor-type is nil. */
26266 if (NILP (BVAR (b, cursor_type)))
26267 return NO_CURSOR;
26269 /* Get the normal cursor type for this window. */
26270 if (EQ (BVAR (b, cursor_type), Qt))
26272 cursor_type = FRAME_DESIRED_CURSOR (f);
26273 *width = FRAME_CURSOR_WIDTH (f);
26275 else
26276 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26278 /* Use cursor-in-non-selected-windows instead
26279 for non-selected window or frame. */
26280 if (non_selected)
26282 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26283 if (!EQ (Qt, alt_cursor))
26284 return get_specified_cursor_type (alt_cursor, width);
26285 /* t means modify the normal cursor type. */
26286 if (cursor_type == FILLED_BOX_CURSOR)
26287 cursor_type = HOLLOW_BOX_CURSOR;
26288 else if (cursor_type == BAR_CURSOR && *width > 1)
26289 --*width;
26290 return cursor_type;
26293 /* Use normal cursor if not blinked off. */
26294 if (!w->cursor_off_p)
26297 #ifdef HAVE_XWIDGETS
26298 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
26299 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
26300 return NO_CURSOR;
26302 #endif
26303 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26305 if (cursor_type == FILLED_BOX_CURSOR)
26307 /* Using a block cursor on large images can be very annoying.
26308 So use a hollow cursor for "large" images.
26309 If image is not transparent (no mask), also use hollow cursor. */
26310 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26311 if (img != NULL && IMAGEP (img->spec))
26313 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26314 where N = size of default frame font size.
26315 This should cover most of the "tiny" icons people may use. */
26316 if (!img->mask
26317 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26318 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26319 cursor_type = HOLLOW_BOX_CURSOR;
26322 else if (cursor_type != NO_CURSOR)
26324 /* Display current only supports BOX and HOLLOW cursors for images.
26325 So for now, unconditionally use a HOLLOW cursor when cursor is
26326 not a solid box cursor. */
26327 cursor_type = HOLLOW_BOX_CURSOR;
26330 return cursor_type;
26333 /* Cursor is blinked off, so determine how to "toggle" it. */
26335 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26336 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26337 return get_specified_cursor_type (XCDR (alt_cursor), width);
26339 /* Then see if frame has specified a specific blink off cursor type. */
26340 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26342 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26343 return FRAME_BLINK_OFF_CURSOR (f);
26346 #if 0
26347 /* Some people liked having a permanently visible blinking cursor,
26348 while others had very strong opinions against it. So it was
26349 decided to remove it. KFS 2003-09-03 */
26351 /* Finally perform built-in cursor blinking:
26352 filled box <-> hollow box
26353 wide [h]bar <-> narrow [h]bar
26354 narrow [h]bar <-> no cursor
26355 other type <-> no cursor */
26357 if (cursor_type == FILLED_BOX_CURSOR)
26358 return HOLLOW_BOX_CURSOR;
26360 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26362 *width = 1;
26363 return cursor_type;
26365 #endif
26367 return NO_CURSOR;
26371 /* Notice when the text cursor of window W has been completely
26372 overwritten by a drawing operation that outputs glyphs in AREA
26373 starting at X0 and ending at X1 in the line starting at Y0 and
26374 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26375 the rest of the line after X0 has been written. Y coordinates
26376 are window-relative. */
26378 static void
26379 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26380 int x0, int x1, int y0, int y1)
26382 int cx0, cx1, cy0, cy1;
26383 struct glyph_row *row;
26385 if (!w->phys_cursor_on_p)
26386 return;
26387 if (area != TEXT_AREA)
26388 return;
26390 if (w->phys_cursor.vpos < 0
26391 || w->phys_cursor.vpos >= w->current_matrix->nrows
26392 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26393 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26394 return;
26396 if (row->cursor_in_fringe_p)
26398 row->cursor_in_fringe_p = 0;
26399 draw_fringe_bitmap (w, row, row->reversed_p);
26400 w->phys_cursor_on_p = 0;
26401 return;
26404 cx0 = w->phys_cursor.x;
26405 cx1 = cx0 + w->phys_cursor_width;
26406 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26407 return;
26409 /* The cursor image will be completely removed from the
26410 screen if the output area intersects the cursor area in
26411 y-direction. When we draw in [y0 y1[, and some part of
26412 the cursor is at y < y0, that part must have been drawn
26413 before. When scrolling, the cursor is erased before
26414 actually scrolling, so we don't come here. When not
26415 scrolling, the rows above the old cursor row must have
26416 changed, and in this case these rows must have written
26417 over the cursor image.
26419 Likewise if part of the cursor is below y1, with the
26420 exception of the cursor being in the first blank row at
26421 the buffer and window end because update_text_area
26422 doesn't draw that row. (Except when it does, but
26423 that's handled in update_text_area.) */
26425 cy0 = w->phys_cursor.y;
26426 cy1 = cy0 + w->phys_cursor_height;
26427 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26428 return;
26430 w->phys_cursor_on_p = 0;
26433 #endif /* HAVE_WINDOW_SYSTEM */
26436 /************************************************************************
26437 Mouse Face
26438 ************************************************************************/
26440 #ifdef HAVE_WINDOW_SYSTEM
26442 /* EXPORT for RIF:
26443 Fix the display of area AREA of overlapping row ROW in window W
26444 with respect to the overlapping part OVERLAPS. */
26446 void
26447 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26448 enum glyph_row_area area, int overlaps)
26450 int i, x;
26452 block_input ();
26454 x = 0;
26455 for (i = 0; i < row->used[area];)
26457 if (row->glyphs[area][i].overlaps_vertically_p)
26459 int start = i, start_x = x;
26463 x += row->glyphs[area][i].pixel_width;
26464 ++i;
26466 while (i < row->used[area]
26467 && row->glyphs[area][i].overlaps_vertically_p);
26469 draw_glyphs (w, start_x, row, area,
26470 start, i,
26471 DRAW_NORMAL_TEXT, overlaps);
26473 else
26475 x += row->glyphs[area][i].pixel_width;
26476 ++i;
26480 unblock_input ();
26484 /* EXPORT:
26485 Draw the cursor glyph of window W in glyph row ROW. See the
26486 comment of draw_glyphs for the meaning of HL. */
26488 void
26489 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26490 enum draw_glyphs_face hl)
26492 /* If cursor hpos is out of bounds, don't draw garbage. This can
26493 happen in mini-buffer windows when switching between echo area
26494 glyphs and mini-buffer. */
26495 if ((row->reversed_p
26496 ? (w->phys_cursor.hpos >= 0)
26497 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26499 int on_p = w->phys_cursor_on_p;
26500 int x1;
26501 int hpos = w->phys_cursor.hpos;
26503 /* When the window is hscrolled, cursor hpos can legitimately be
26504 out of bounds, but we draw the cursor at the corresponding
26505 window margin in that case. */
26506 if (!row->reversed_p && hpos < 0)
26507 hpos = 0;
26508 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26509 hpos = row->used[TEXT_AREA] - 1;
26511 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26512 hl, 0);
26513 w->phys_cursor_on_p = on_p;
26515 if (hl == DRAW_CURSOR)
26516 w->phys_cursor_width = x1 - w->phys_cursor.x;
26517 /* When we erase the cursor, and ROW is overlapped by other
26518 rows, make sure that these overlapping parts of other rows
26519 are redrawn. */
26520 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26522 w->phys_cursor_width = x1 - w->phys_cursor.x;
26524 if (row > w->current_matrix->rows
26525 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26526 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26527 OVERLAPS_ERASED_CURSOR);
26529 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26530 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26531 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26532 OVERLAPS_ERASED_CURSOR);
26538 /* EXPORT:
26539 Erase the image of a cursor of window W from the screen. */
26541 void
26542 erase_phys_cursor (struct window *w)
26544 struct frame *f = XFRAME (w->frame);
26545 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26546 int hpos = w->phys_cursor.hpos;
26547 int vpos = w->phys_cursor.vpos;
26548 int mouse_face_here_p = 0;
26549 struct glyph_matrix *active_glyphs = w->current_matrix;
26550 struct glyph_row *cursor_row;
26551 struct glyph *cursor_glyph;
26552 enum draw_glyphs_face hl;
26554 /* No cursor displayed or row invalidated => nothing to do on the
26555 screen. */
26556 if (w->phys_cursor_type == NO_CURSOR)
26557 goto mark_cursor_off;
26559 /* VPOS >= active_glyphs->nrows means that window has been resized.
26560 Don't bother to erase the cursor. */
26561 if (vpos >= active_glyphs->nrows)
26562 goto mark_cursor_off;
26564 /* If row containing cursor is marked invalid, there is nothing we
26565 can do. */
26566 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26567 if (!cursor_row->enabled_p)
26568 goto mark_cursor_off;
26570 /* If line spacing is > 0, old cursor may only be partially visible in
26571 window after split-window. So adjust visible height. */
26572 cursor_row->visible_height = min (cursor_row->visible_height,
26573 window_text_bottom_y (w) - cursor_row->y);
26575 /* If row is completely invisible, don't attempt to delete a cursor which
26576 isn't there. This can happen if cursor is at top of a window, and
26577 we switch to a buffer with a header line in that window. */
26578 if (cursor_row->visible_height <= 0)
26579 goto mark_cursor_off;
26581 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26582 if (cursor_row->cursor_in_fringe_p)
26584 cursor_row->cursor_in_fringe_p = 0;
26585 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26586 goto mark_cursor_off;
26589 /* This can happen when the new row is shorter than the old one.
26590 In this case, either draw_glyphs or clear_end_of_line
26591 should have cleared the cursor. Note that we wouldn't be
26592 able to erase the cursor in this case because we don't have a
26593 cursor glyph at hand. */
26594 if ((cursor_row->reversed_p
26595 ? (w->phys_cursor.hpos < 0)
26596 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26597 goto mark_cursor_off;
26599 /* When the window is hscrolled, cursor hpos can legitimately be out
26600 of bounds, but we draw the cursor at the corresponding window
26601 margin in that case. */
26602 if (!cursor_row->reversed_p && hpos < 0)
26603 hpos = 0;
26604 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26605 hpos = cursor_row->used[TEXT_AREA] - 1;
26607 /* If the cursor is in the mouse face area, redisplay that when
26608 we clear the cursor. */
26609 if (! NILP (hlinfo->mouse_face_window)
26610 && coords_in_mouse_face_p (w, hpos, vpos)
26611 /* Don't redraw the cursor's spot in mouse face if it is at the
26612 end of a line (on a newline). The cursor appears there, but
26613 mouse highlighting does not. */
26614 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26615 mouse_face_here_p = 1;
26617 /* Maybe clear the display under the cursor. */
26618 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26620 int x, y, left_x;
26621 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26622 int width;
26624 cursor_glyph = get_phys_cursor_glyph (w);
26625 if (cursor_glyph == NULL)
26626 goto mark_cursor_off;
26628 width = cursor_glyph->pixel_width;
26629 left_x = window_box_left_offset (w, TEXT_AREA);
26630 x = w->phys_cursor.x;
26631 if (x < left_x)
26632 width -= left_x - x;
26633 width = min (width, window_box_width (w, TEXT_AREA) - x);
26634 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26635 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26637 if (width > 0)
26638 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26641 /* Erase the cursor by redrawing the character underneath it. */
26642 if (mouse_face_here_p)
26643 hl = DRAW_MOUSE_FACE;
26644 else
26645 hl = DRAW_NORMAL_TEXT;
26646 draw_phys_cursor_glyph (w, cursor_row, hl);
26648 mark_cursor_off:
26649 w->phys_cursor_on_p = 0;
26650 w->phys_cursor_type = NO_CURSOR;
26654 /* EXPORT:
26655 Display or clear cursor of window W. If ON is zero, clear the
26656 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26657 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26659 void
26660 display_and_set_cursor (struct window *w, bool on,
26661 int hpos, int vpos, int x, int y)
26663 struct frame *f = XFRAME (w->frame);
26664 int new_cursor_type;
26665 int new_cursor_width;
26666 int active_cursor;
26667 struct glyph_row *glyph_row;
26668 struct glyph *glyph;
26670 /* This is pointless on invisible frames, and dangerous on garbaged
26671 windows and frames; in the latter case, the frame or window may
26672 be in the midst of changing its size, and x and y may be off the
26673 window. */
26674 if (! FRAME_VISIBLE_P (f)
26675 || FRAME_GARBAGED_P (f)
26676 || vpos >= w->current_matrix->nrows
26677 || hpos >= w->current_matrix->matrix_w)
26678 return;
26680 /* If cursor is off and we want it off, return quickly. */
26681 if (!on && !w->phys_cursor_on_p)
26682 return;
26684 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26685 /* If cursor row is not enabled, we don't really know where to
26686 display the cursor. */
26687 if (!glyph_row->enabled_p)
26689 w->phys_cursor_on_p = 0;
26690 return;
26693 glyph = NULL;
26694 if (!glyph_row->exact_window_width_line_p
26695 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26696 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26698 eassert (input_blocked_p ());
26700 /* Set new_cursor_type to the cursor we want to be displayed. */
26701 new_cursor_type = get_window_cursor_type (w, glyph,
26702 &new_cursor_width, &active_cursor);
26704 /* If cursor is currently being shown and we don't want it to be or
26705 it is in the wrong place, or the cursor type is not what we want,
26706 erase it. */
26707 if (w->phys_cursor_on_p
26708 && (!on
26709 || w->phys_cursor.x != x
26710 || w->phys_cursor.y != y
26711 || new_cursor_type != w->phys_cursor_type
26712 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26713 && new_cursor_width != w->phys_cursor_width)))
26714 erase_phys_cursor (w);
26716 /* Don't check phys_cursor_on_p here because that flag is only set
26717 to zero in some cases where we know that the cursor has been
26718 completely erased, to avoid the extra work of erasing the cursor
26719 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26720 still not be visible, or it has only been partly erased. */
26721 if (on)
26723 w->phys_cursor_ascent = glyph_row->ascent;
26724 w->phys_cursor_height = glyph_row->height;
26726 /* Set phys_cursor_.* before x_draw_.* is called because some
26727 of them may need the information. */
26728 w->phys_cursor.x = x;
26729 w->phys_cursor.y = glyph_row->y;
26730 w->phys_cursor.hpos = hpos;
26731 w->phys_cursor.vpos = vpos;
26734 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26735 new_cursor_type, new_cursor_width,
26736 on, active_cursor);
26740 /* Switch the display of W's cursor on or off, according to the value
26741 of ON. */
26743 static void
26744 update_window_cursor (struct window *w, bool on)
26746 /* Don't update cursor in windows whose frame is in the process
26747 of being deleted. */
26748 if (w->current_matrix)
26750 int hpos = w->phys_cursor.hpos;
26751 int vpos = w->phys_cursor.vpos;
26752 struct glyph_row *row;
26754 if (vpos >= w->current_matrix->nrows
26755 || hpos >= w->current_matrix->matrix_w)
26756 return;
26758 row = MATRIX_ROW (w->current_matrix, vpos);
26760 /* When the window is hscrolled, cursor hpos can legitimately be
26761 out of bounds, but we draw the cursor at the corresponding
26762 window margin in that case. */
26763 if (!row->reversed_p && hpos < 0)
26764 hpos = 0;
26765 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26766 hpos = row->used[TEXT_AREA] - 1;
26768 block_input ();
26769 display_and_set_cursor (w, on, hpos, vpos,
26770 w->phys_cursor.x, w->phys_cursor.y);
26771 unblock_input ();
26776 /* Call update_window_cursor with parameter ON_P on all leaf windows
26777 in the window tree rooted at W. */
26779 static void
26780 update_cursor_in_window_tree (struct window *w, bool on_p)
26782 while (w)
26784 if (WINDOWP (w->contents))
26785 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26786 else
26787 update_window_cursor (w, on_p);
26789 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26794 /* EXPORT:
26795 Display the cursor on window W, or clear it, according to ON_P.
26796 Don't change the cursor's position. */
26798 void
26799 x_update_cursor (struct frame *f, bool on_p)
26801 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26805 /* EXPORT:
26806 Clear the cursor of window W to background color, and mark the
26807 cursor as not shown. This is used when the text where the cursor
26808 is about to be rewritten. */
26810 void
26811 x_clear_cursor (struct window *w)
26813 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26814 update_window_cursor (w, 0);
26817 #endif /* HAVE_WINDOW_SYSTEM */
26819 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26820 and MSDOS. */
26821 static void
26822 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26823 int start_hpos, int end_hpos,
26824 enum draw_glyphs_face draw)
26826 #ifdef HAVE_WINDOW_SYSTEM
26827 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26829 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26830 return;
26832 #endif
26833 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26834 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26835 #endif
26838 /* Display the active region described by mouse_face_* according to DRAW. */
26840 static void
26841 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26843 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26844 struct frame *f = XFRAME (WINDOW_FRAME (w));
26846 if (/* If window is in the process of being destroyed, don't bother
26847 to do anything. */
26848 w->current_matrix != NULL
26849 /* Don't update mouse highlight if hidden */
26850 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26851 /* Recognize when we are called to operate on rows that don't exist
26852 anymore. This can happen when a window is split. */
26853 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26855 int phys_cursor_on_p = w->phys_cursor_on_p;
26856 struct glyph_row *row, *first, *last;
26858 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26859 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26861 for (row = first; row <= last && row->enabled_p; ++row)
26863 int start_hpos, end_hpos, start_x;
26865 /* For all but the first row, the highlight starts at column 0. */
26866 if (row == first)
26868 /* R2L rows have BEG and END in reversed order, but the
26869 screen drawing geometry is always left to right. So
26870 we need to mirror the beginning and end of the
26871 highlighted area in R2L rows. */
26872 if (!row->reversed_p)
26874 start_hpos = hlinfo->mouse_face_beg_col;
26875 start_x = hlinfo->mouse_face_beg_x;
26877 else if (row == last)
26879 start_hpos = hlinfo->mouse_face_end_col;
26880 start_x = hlinfo->mouse_face_end_x;
26882 else
26884 start_hpos = 0;
26885 start_x = 0;
26888 else if (row->reversed_p && row == last)
26890 start_hpos = hlinfo->mouse_face_end_col;
26891 start_x = hlinfo->mouse_face_end_x;
26893 else
26895 start_hpos = 0;
26896 start_x = 0;
26899 if (row == last)
26901 if (!row->reversed_p)
26902 end_hpos = hlinfo->mouse_face_end_col;
26903 else if (row == first)
26904 end_hpos = hlinfo->mouse_face_beg_col;
26905 else
26907 end_hpos = row->used[TEXT_AREA];
26908 if (draw == DRAW_NORMAL_TEXT)
26909 row->fill_line_p = 1; /* Clear to end of line */
26912 else if (row->reversed_p && row == first)
26913 end_hpos = hlinfo->mouse_face_beg_col;
26914 else
26916 end_hpos = row->used[TEXT_AREA];
26917 if (draw == DRAW_NORMAL_TEXT)
26918 row->fill_line_p = 1; /* Clear to end of line */
26921 if (end_hpos > start_hpos)
26923 draw_row_with_mouse_face (w, start_x, row,
26924 start_hpos, end_hpos, draw);
26926 row->mouse_face_p
26927 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26931 #ifdef HAVE_WINDOW_SYSTEM
26932 /* When we've written over the cursor, arrange for it to
26933 be displayed again. */
26934 if (FRAME_WINDOW_P (f)
26935 && phys_cursor_on_p && !w->phys_cursor_on_p)
26937 int hpos = w->phys_cursor.hpos;
26939 /* When the window is hscrolled, cursor hpos can legitimately be
26940 out of bounds, but we draw the cursor at the corresponding
26941 window margin in that case. */
26942 if (!row->reversed_p && hpos < 0)
26943 hpos = 0;
26944 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26945 hpos = row->used[TEXT_AREA] - 1;
26947 block_input ();
26948 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26949 w->phys_cursor.x, w->phys_cursor.y);
26950 unblock_input ();
26952 #endif /* HAVE_WINDOW_SYSTEM */
26955 #ifdef HAVE_WINDOW_SYSTEM
26956 /* Change the mouse cursor. */
26957 if (FRAME_WINDOW_P (f))
26959 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26960 if (draw == DRAW_NORMAL_TEXT
26961 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26962 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26963 else
26964 #endif
26965 if (draw == DRAW_MOUSE_FACE)
26966 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26967 else
26968 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26970 #endif /* HAVE_WINDOW_SYSTEM */
26973 /* EXPORT:
26974 Clear out the mouse-highlighted active region.
26975 Redraw it un-highlighted first. Value is non-zero if mouse
26976 face was actually drawn unhighlighted. */
26979 clear_mouse_face (Mouse_HLInfo *hlinfo)
26981 int cleared = 0;
26983 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26985 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26986 cleared = 1;
26989 reset_mouse_highlight (hlinfo);
26990 return cleared;
26993 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26994 within the mouse face on that window. */
26995 static int
26996 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26998 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27000 /* Quickly resolve the easy cases. */
27001 if (!(WINDOWP (hlinfo->mouse_face_window)
27002 && XWINDOW (hlinfo->mouse_face_window) == w))
27003 return 0;
27004 if (vpos < hlinfo->mouse_face_beg_row
27005 || vpos > hlinfo->mouse_face_end_row)
27006 return 0;
27007 if (vpos > hlinfo->mouse_face_beg_row
27008 && vpos < hlinfo->mouse_face_end_row)
27009 return 1;
27011 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27013 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27015 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27016 return 1;
27018 else if ((vpos == hlinfo->mouse_face_beg_row
27019 && hpos >= hlinfo->mouse_face_beg_col)
27020 || (vpos == hlinfo->mouse_face_end_row
27021 && hpos < hlinfo->mouse_face_end_col))
27022 return 1;
27024 else
27026 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27028 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27029 return 1;
27031 else if ((vpos == hlinfo->mouse_face_beg_row
27032 && hpos <= hlinfo->mouse_face_beg_col)
27033 || (vpos == hlinfo->mouse_face_end_row
27034 && hpos > hlinfo->mouse_face_end_col))
27035 return 1;
27037 return 0;
27041 /* EXPORT:
27042 Non-zero if physical cursor of window W is within mouse face. */
27045 cursor_in_mouse_face_p (struct window *w)
27047 int hpos = w->phys_cursor.hpos;
27048 int vpos = w->phys_cursor.vpos;
27049 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27051 /* When the window is hscrolled, cursor hpos can legitimately be out
27052 of bounds, but we draw the cursor at the corresponding window
27053 margin in that case. */
27054 if (!row->reversed_p && hpos < 0)
27055 hpos = 0;
27056 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27057 hpos = row->used[TEXT_AREA] - 1;
27059 return coords_in_mouse_face_p (w, hpos, vpos);
27064 /* Find the glyph rows START_ROW and END_ROW of window W that display
27065 characters between buffer positions START_CHARPOS and END_CHARPOS
27066 (excluding END_CHARPOS). DISP_STRING is a display string that
27067 covers these buffer positions. This is similar to
27068 row_containing_pos, but is more accurate when bidi reordering makes
27069 buffer positions change non-linearly with glyph rows. */
27070 static void
27071 rows_from_pos_range (struct window *w,
27072 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27073 Lisp_Object disp_string,
27074 struct glyph_row **start, struct glyph_row **end)
27076 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27077 int last_y = window_text_bottom_y (w);
27078 struct glyph_row *row;
27080 *start = NULL;
27081 *end = NULL;
27083 while (!first->enabled_p
27084 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27085 first++;
27087 /* Find the START row. */
27088 for (row = first;
27089 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27090 row++)
27092 /* A row can potentially be the START row if the range of the
27093 characters it displays intersects the range
27094 [START_CHARPOS..END_CHARPOS). */
27095 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27096 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27097 /* See the commentary in row_containing_pos, for the
27098 explanation of the complicated way to check whether
27099 some position is beyond the end of the characters
27100 displayed by a row. */
27101 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27102 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27103 && !row->ends_at_zv_p
27104 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27105 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27106 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27107 && !row->ends_at_zv_p
27108 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27110 /* Found a candidate row. Now make sure at least one of the
27111 glyphs it displays has a charpos from the range
27112 [START_CHARPOS..END_CHARPOS).
27114 This is not obvious because bidi reordering could make
27115 buffer positions of a row be 1,2,3,102,101,100, and if we
27116 want to highlight characters in [50..60), we don't want
27117 this row, even though [50..60) does intersect [1..103),
27118 the range of character positions given by the row's start
27119 and end positions. */
27120 struct glyph *g = row->glyphs[TEXT_AREA];
27121 struct glyph *e = g + row->used[TEXT_AREA];
27123 while (g < e)
27125 if (((BUFFERP (g->object) || INTEGERP (g->object))
27126 && start_charpos <= g->charpos && g->charpos < end_charpos)
27127 /* A glyph that comes from DISP_STRING is by
27128 definition to be highlighted. */
27129 || EQ (g->object, disp_string))
27130 *start = row;
27131 g++;
27133 if (*start)
27134 break;
27138 /* Find the END row. */
27139 if (!*start
27140 /* If the last row is partially visible, start looking for END
27141 from that row, instead of starting from FIRST. */
27142 && !(row->enabled_p
27143 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27144 row = first;
27145 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27147 struct glyph_row *next = row + 1;
27148 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27150 if (!next->enabled_p
27151 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27152 /* The first row >= START whose range of displayed characters
27153 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27154 is the row END + 1. */
27155 || (start_charpos < next_start
27156 && end_charpos < next_start)
27157 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27158 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27159 && !next->ends_at_zv_p
27160 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27161 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27162 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27163 && !next->ends_at_zv_p
27164 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27166 *end = row;
27167 break;
27169 else
27171 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27172 but none of the characters it displays are in the range, it is
27173 also END + 1. */
27174 struct glyph *g = next->glyphs[TEXT_AREA];
27175 struct glyph *s = g;
27176 struct glyph *e = g + next->used[TEXT_AREA];
27178 while (g < e)
27180 if (((BUFFERP (g->object) || INTEGERP (g->object))
27181 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27182 /* If the buffer position of the first glyph in
27183 the row is equal to END_CHARPOS, it means
27184 the last character to be highlighted is the
27185 newline of ROW, and we must consider NEXT as
27186 END, not END+1. */
27187 || (((!next->reversed_p && g == s)
27188 || (next->reversed_p && g == e - 1))
27189 && (g->charpos == end_charpos
27190 /* Special case for when NEXT is an
27191 empty line at ZV. */
27192 || (g->charpos == -1
27193 && !row->ends_at_zv_p
27194 && next_start == end_charpos)))))
27195 /* A glyph that comes from DISP_STRING is by
27196 definition to be highlighted. */
27197 || EQ (g->object, disp_string))
27198 break;
27199 g++;
27201 if (g == e)
27203 *end = row;
27204 break;
27206 /* The first row that ends at ZV must be the last to be
27207 highlighted. */
27208 else if (next->ends_at_zv_p)
27210 *end = next;
27211 break;
27217 /* This function sets the mouse_face_* elements of HLINFO, assuming
27218 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27219 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27220 for the overlay or run of text properties specifying the mouse
27221 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27222 before-string and after-string that must also be highlighted.
27223 DISP_STRING, if non-nil, is a display string that may cover some
27224 or all of the highlighted text. */
27226 static void
27227 mouse_face_from_buffer_pos (Lisp_Object window,
27228 Mouse_HLInfo *hlinfo,
27229 ptrdiff_t mouse_charpos,
27230 ptrdiff_t start_charpos,
27231 ptrdiff_t end_charpos,
27232 Lisp_Object before_string,
27233 Lisp_Object after_string,
27234 Lisp_Object disp_string)
27236 struct window *w = XWINDOW (window);
27237 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27238 struct glyph_row *r1, *r2;
27239 struct glyph *glyph, *end;
27240 ptrdiff_t ignore, pos;
27241 int x;
27243 eassert (NILP (disp_string) || STRINGP (disp_string));
27244 eassert (NILP (before_string) || STRINGP (before_string));
27245 eassert (NILP (after_string) || STRINGP (after_string));
27247 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27248 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27249 if (r1 == NULL)
27250 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27251 /* If the before-string or display-string contains newlines,
27252 rows_from_pos_range skips to its last row. Move back. */
27253 if (!NILP (before_string) || !NILP (disp_string))
27255 struct glyph_row *prev;
27256 while ((prev = r1 - 1, prev >= first)
27257 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27258 && prev->used[TEXT_AREA] > 0)
27260 struct glyph *beg = prev->glyphs[TEXT_AREA];
27261 glyph = beg + prev->used[TEXT_AREA];
27262 while (--glyph >= beg && INTEGERP (glyph->object));
27263 if (glyph < beg
27264 || !(EQ (glyph->object, before_string)
27265 || EQ (glyph->object, disp_string)))
27266 break;
27267 r1 = prev;
27270 if (r2 == NULL)
27272 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27273 hlinfo->mouse_face_past_end = 1;
27275 else if (!NILP (after_string))
27277 /* If the after-string has newlines, advance to its last row. */
27278 struct glyph_row *next;
27279 struct glyph_row *last
27280 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27282 for (next = r2 + 1;
27283 next <= last
27284 && next->used[TEXT_AREA] > 0
27285 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27286 ++next)
27287 r2 = next;
27289 /* The rest of the display engine assumes that mouse_face_beg_row is
27290 either above mouse_face_end_row or identical to it. But with
27291 bidi-reordered continued lines, the row for START_CHARPOS could
27292 be below the row for END_CHARPOS. If so, swap the rows and store
27293 them in correct order. */
27294 if (r1->y > r2->y)
27296 struct glyph_row *tem = r2;
27298 r2 = r1;
27299 r1 = tem;
27302 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27303 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27305 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27306 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27307 could be anywhere in the row and in any order. The strategy
27308 below is to find the leftmost and the rightmost glyph that
27309 belongs to either of these 3 strings, or whose position is
27310 between START_CHARPOS and END_CHARPOS, and highlight all the
27311 glyphs between those two. This may cover more than just the text
27312 between START_CHARPOS and END_CHARPOS if the range of characters
27313 strides the bidi level boundary, e.g. if the beginning is in R2L
27314 text while the end is in L2R text or vice versa. */
27315 if (!r1->reversed_p)
27317 /* This row is in a left to right paragraph. Scan it left to
27318 right. */
27319 glyph = r1->glyphs[TEXT_AREA];
27320 end = glyph + r1->used[TEXT_AREA];
27321 x = r1->x;
27323 /* Skip truncation glyphs at the start of the glyph row. */
27324 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27325 for (; glyph < end
27326 && INTEGERP (glyph->object)
27327 && glyph->charpos < 0;
27328 ++glyph)
27329 x += glyph->pixel_width;
27331 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27332 or DISP_STRING, and the first glyph from buffer whose
27333 position is between START_CHARPOS and END_CHARPOS. */
27334 for (; glyph < end
27335 && !INTEGERP (glyph->object)
27336 && !EQ (glyph->object, disp_string)
27337 && !(BUFFERP (glyph->object)
27338 && (glyph->charpos >= start_charpos
27339 && glyph->charpos < end_charpos));
27340 ++glyph)
27342 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27343 are present at buffer positions between START_CHARPOS and
27344 END_CHARPOS, or if they come from an overlay. */
27345 if (EQ (glyph->object, before_string))
27347 pos = string_buffer_position (before_string,
27348 start_charpos);
27349 /* If pos == 0, it means before_string came from an
27350 overlay, not from a buffer position. */
27351 if (!pos || (pos >= start_charpos && pos < end_charpos))
27352 break;
27354 else if (EQ (glyph->object, after_string))
27356 pos = string_buffer_position (after_string, end_charpos);
27357 if (!pos || (pos >= start_charpos && pos < end_charpos))
27358 break;
27360 x += glyph->pixel_width;
27362 hlinfo->mouse_face_beg_x = x;
27363 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27365 else
27367 /* This row is in a right to left paragraph. Scan it right to
27368 left. */
27369 struct glyph *g;
27371 end = r1->glyphs[TEXT_AREA] - 1;
27372 glyph = end + r1->used[TEXT_AREA];
27374 /* Skip truncation glyphs at the start of the glyph row. */
27375 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27376 for (; glyph > end
27377 && INTEGERP (glyph->object)
27378 && glyph->charpos < 0;
27379 --glyph)
27382 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27383 or DISP_STRING, and the first glyph from buffer whose
27384 position is between START_CHARPOS and END_CHARPOS. */
27385 for (; glyph > end
27386 && !INTEGERP (glyph->object)
27387 && !EQ (glyph->object, disp_string)
27388 && !(BUFFERP (glyph->object)
27389 && (glyph->charpos >= start_charpos
27390 && glyph->charpos < end_charpos));
27391 --glyph)
27393 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27394 are present at buffer positions between START_CHARPOS and
27395 END_CHARPOS, or if they come from an overlay. */
27396 if (EQ (glyph->object, before_string))
27398 pos = string_buffer_position (before_string, start_charpos);
27399 /* If pos == 0, it means before_string came from an
27400 overlay, not from a buffer position. */
27401 if (!pos || (pos >= start_charpos && pos < end_charpos))
27402 break;
27404 else if (EQ (glyph->object, after_string))
27406 pos = string_buffer_position (after_string, end_charpos);
27407 if (!pos || (pos >= start_charpos && pos < end_charpos))
27408 break;
27412 glyph++; /* first glyph to the right of the highlighted area */
27413 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27414 x += g->pixel_width;
27415 hlinfo->mouse_face_beg_x = x;
27416 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27419 /* If the highlight ends in a different row, compute GLYPH and END
27420 for the end row. Otherwise, reuse the values computed above for
27421 the row where the highlight begins. */
27422 if (r2 != r1)
27424 if (!r2->reversed_p)
27426 glyph = r2->glyphs[TEXT_AREA];
27427 end = glyph + r2->used[TEXT_AREA];
27428 x = r2->x;
27430 else
27432 end = r2->glyphs[TEXT_AREA] - 1;
27433 glyph = end + r2->used[TEXT_AREA];
27437 if (!r2->reversed_p)
27439 /* Skip truncation and continuation glyphs near the end of the
27440 row, and also blanks and stretch glyphs inserted by
27441 extend_face_to_end_of_line. */
27442 while (end > glyph
27443 && INTEGERP ((end - 1)->object))
27444 --end;
27445 /* Scan the rest of the glyph row from the end, looking for the
27446 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27447 DISP_STRING, or whose position is between START_CHARPOS
27448 and END_CHARPOS */
27449 for (--end;
27450 end > glyph
27451 && !INTEGERP (end->object)
27452 && !EQ (end->object, disp_string)
27453 && !(BUFFERP (end->object)
27454 && (end->charpos >= start_charpos
27455 && end->charpos < end_charpos));
27456 --end)
27458 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27459 are present at buffer positions between START_CHARPOS and
27460 END_CHARPOS, or if they come from an overlay. */
27461 if (EQ (end->object, before_string))
27463 pos = string_buffer_position (before_string, start_charpos);
27464 if (!pos || (pos >= start_charpos && pos < end_charpos))
27465 break;
27467 else if (EQ (end->object, after_string))
27469 pos = string_buffer_position (after_string, end_charpos);
27470 if (!pos || (pos >= start_charpos && pos < end_charpos))
27471 break;
27474 /* Find the X coordinate of the last glyph to be highlighted. */
27475 for (; glyph <= end; ++glyph)
27476 x += glyph->pixel_width;
27478 hlinfo->mouse_face_end_x = x;
27479 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27481 else
27483 /* Skip truncation and continuation glyphs near the end of the
27484 row, and also blanks and stretch glyphs inserted by
27485 extend_face_to_end_of_line. */
27486 x = r2->x;
27487 end++;
27488 while (end < glyph
27489 && INTEGERP (end->object))
27491 x += end->pixel_width;
27492 ++end;
27494 /* Scan the rest of the glyph row from the end, looking for the
27495 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27496 DISP_STRING, or whose position is between START_CHARPOS
27497 and END_CHARPOS */
27498 for ( ;
27499 end < glyph
27500 && !INTEGERP (end->object)
27501 && !EQ (end->object, disp_string)
27502 && !(BUFFERP (end->object)
27503 && (end->charpos >= start_charpos
27504 && end->charpos < end_charpos));
27505 ++end)
27507 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27508 are present at buffer positions between START_CHARPOS and
27509 END_CHARPOS, or if they come from an overlay. */
27510 if (EQ (end->object, before_string))
27512 pos = string_buffer_position (before_string, start_charpos);
27513 if (!pos || (pos >= start_charpos && pos < end_charpos))
27514 break;
27516 else if (EQ (end->object, after_string))
27518 pos = string_buffer_position (after_string, end_charpos);
27519 if (!pos || (pos >= start_charpos && pos < end_charpos))
27520 break;
27522 x += end->pixel_width;
27524 /* If we exited the above loop because we arrived at the last
27525 glyph of the row, and its buffer position is still not in
27526 range, it means the last character in range is the preceding
27527 newline. Bump the end column and x values to get past the
27528 last glyph. */
27529 if (end == glyph
27530 && BUFFERP (end->object)
27531 && (end->charpos < start_charpos
27532 || end->charpos >= end_charpos))
27534 x += end->pixel_width;
27535 ++end;
27537 hlinfo->mouse_face_end_x = x;
27538 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27541 hlinfo->mouse_face_window = window;
27542 hlinfo->mouse_face_face_id
27543 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27544 mouse_charpos + 1,
27545 !hlinfo->mouse_face_hidden, -1);
27546 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27549 /* The following function is not used anymore (replaced with
27550 mouse_face_from_string_pos), but I leave it here for the time
27551 being, in case someone would. */
27553 #if 0 /* not used */
27555 /* Find the position of the glyph for position POS in OBJECT in
27556 window W's current matrix, and return in *X, *Y the pixel
27557 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27559 RIGHT_P non-zero means return the position of the right edge of the
27560 glyph, RIGHT_P zero means return the left edge position.
27562 If no glyph for POS exists in the matrix, return the position of
27563 the glyph with the next smaller position that is in the matrix, if
27564 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27565 exists in the matrix, return the position of the glyph with the
27566 next larger position in OBJECT.
27568 Value is non-zero if a glyph was found. */
27570 static int
27571 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27572 int *hpos, int *vpos, int *x, int *y, int right_p)
27574 int yb = window_text_bottom_y (w);
27575 struct glyph_row *r;
27576 struct glyph *best_glyph = NULL;
27577 struct glyph_row *best_row = NULL;
27578 int best_x = 0;
27580 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27581 r->enabled_p && r->y < yb;
27582 ++r)
27584 struct glyph *g = r->glyphs[TEXT_AREA];
27585 struct glyph *e = g + r->used[TEXT_AREA];
27586 int gx;
27588 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27589 if (EQ (g->object, object))
27591 if (g->charpos == pos)
27593 best_glyph = g;
27594 best_x = gx;
27595 best_row = r;
27596 goto found;
27598 else if (best_glyph == NULL
27599 || ((eabs (g->charpos - pos)
27600 < eabs (best_glyph->charpos - pos))
27601 && (right_p
27602 ? g->charpos < pos
27603 : g->charpos > pos)))
27605 best_glyph = g;
27606 best_x = gx;
27607 best_row = r;
27612 found:
27614 if (best_glyph)
27616 *x = best_x;
27617 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27619 if (right_p)
27621 *x += best_glyph->pixel_width;
27622 ++*hpos;
27625 *y = best_row->y;
27626 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27629 return best_glyph != NULL;
27631 #endif /* not used */
27633 /* Find the positions of the first and the last glyphs in window W's
27634 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27635 (assumed to be a string), and return in HLINFO's mouse_face_*
27636 members the pixel and column/row coordinates of those glyphs. */
27638 static void
27639 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27640 Lisp_Object object,
27641 ptrdiff_t startpos, ptrdiff_t endpos)
27643 int yb = window_text_bottom_y (w);
27644 struct glyph_row *r;
27645 struct glyph *g, *e;
27646 int gx;
27647 int found = 0;
27649 /* Find the glyph row with at least one position in the range
27650 [STARTPOS..ENDPOS], and the first glyph in that row whose
27651 position belongs to that range. */
27652 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27653 r->enabled_p && r->y < yb;
27654 ++r)
27656 if (!r->reversed_p)
27658 g = r->glyphs[TEXT_AREA];
27659 e = g + r->used[TEXT_AREA];
27660 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27661 if (EQ (g->object, object)
27662 && startpos <= g->charpos && g->charpos <= endpos)
27664 hlinfo->mouse_face_beg_row
27665 = MATRIX_ROW_VPOS (r, w->current_matrix);
27666 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27667 hlinfo->mouse_face_beg_x = gx;
27668 found = 1;
27669 break;
27672 else
27674 struct glyph *g1;
27676 e = r->glyphs[TEXT_AREA];
27677 g = e + r->used[TEXT_AREA];
27678 for ( ; g > e; --g)
27679 if (EQ ((g-1)->object, object)
27680 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27682 hlinfo->mouse_face_beg_row
27683 = MATRIX_ROW_VPOS (r, w->current_matrix);
27684 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27685 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27686 gx += g1->pixel_width;
27687 hlinfo->mouse_face_beg_x = gx;
27688 found = 1;
27689 break;
27692 if (found)
27693 break;
27696 if (!found)
27697 return;
27699 /* Starting with the next row, look for the first row which does NOT
27700 include any glyphs whose positions are in the range. */
27701 for (++r; r->enabled_p && r->y < yb; ++r)
27703 g = r->glyphs[TEXT_AREA];
27704 e = g + r->used[TEXT_AREA];
27705 found = 0;
27706 for ( ; g < e; ++g)
27707 if (EQ (g->object, object)
27708 && startpos <= g->charpos && g->charpos <= endpos)
27710 found = 1;
27711 break;
27713 if (!found)
27714 break;
27717 /* The highlighted region ends on the previous row. */
27718 r--;
27720 /* Set the end row. */
27721 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27723 /* Compute and set the end column and the end column's horizontal
27724 pixel coordinate. */
27725 if (!r->reversed_p)
27727 g = r->glyphs[TEXT_AREA];
27728 e = g + r->used[TEXT_AREA];
27729 for ( ; e > g; --e)
27730 if (EQ ((e-1)->object, object)
27731 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27732 break;
27733 hlinfo->mouse_face_end_col = e - g;
27735 for (gx = r->x; g < e; ++g)
27736 gx += g->pixel_width;
27737 hlinfo->mouse_face_end_x = gx;
27739 else
27741 e = r->glyphs[TEXT_AREA];
27742 g = e + r->used[TEXT_AREA];
27743 for (gx = r->x ; e < g; ++e)
27745 if (EQ (e->object, object)
27746 && startpos <= e->charpos && e->charpos <= endpos)
27747 break;
27748 gx += e->pixel_width;
27750 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27751 hlinfo->mouse_face_end_x = gx;
27755 #ifdef HAVE_WINDOW_SYSTEM
27757 /* See if position X, Y is within a hot-spot of an image. */
27759 static int
27760 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27762 if (!CONSP (hot_spot))
27763 return 0;
27765 if (EQ (XCAR (hot_spot), Qrect))
27767 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27768 Lisp_Object rect = XCDR (hot_spot);
27769 Lisp_Object tem;
27770 if (!CONSP (rect))
27771 return 0;
27772 if (!CONSP (XCAR (rect)))
27773 return 0;
27774 if (!CONSP (XCDR (rect)))
27775 return 0;
27776 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27777 return 0;
27778 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27779 return 0;
27780 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27781 return 0;
27782 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27783 return 0;
27784 return 1;
27786 else if (EQ (XCAR (hot_spot), Qcircle))
27788 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27789 Lisp_Object circ = XCDR (hot_spot);
27790 Lisp_Object lr, lx0, ly0;
27791 if (CONSP (circ)
27792 && CONSP (XCAR (circ))
27793 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27794 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27795 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27797 double r = XFLOATINT (lr);
27798 double dx = XINT (lx0) - x;
27799 double dy = XINT (ly0) - y;
27800 return (dx * dx + dy * dy <= r * r);
27803 else if (EQ (XCAR (hot_spot), Qpoly))
27805 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27806 if (VECTORP (XCDR (hot_spot)))
27808 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27809 Lisp_Object *poly = v->contents;
27810 ptrdiff_t n = v->header.size;
27811 ptrdiff_t i;
27812 int inside = 0;
27813 Lisp_Object lx, ly;
27814 int x0, y0;
27816 /* Need an even number of coordinates, and at least 3 edges. */
27817 if (n < 6 || n & 1)
27818 return 0;
27820 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27821 If count is odd, we are inside polygon. Pixels on edges
27822 may or may not be included depending on actual geometry of the
27823 polygon. */
27824 if ((lx = poly[n-2], !INTEGERP (lx))
27825 || (ly = poly[n-1], !INTEGERP (lx)))
27826 return 0;
27827 x0 = XINT (lx), y0 = XINT (ly);
27828 for (i = 0; i < n; i += 2)
27830 int x1 = x0, y1 = y0;
27831 if ((lx = poly[i], !INTEGERP (lx))
27832 || (ly = poly[i+1], !INTEGERP (ly)))
27833 return 0;
27834 x0 = XINT (lx), y0 = XINT (ly);
27836 /* Does this segment cross the X line? */
27837 if (x0 >= x)
27839 if (x1 >= x)
27840 continue;
27842 else if (x1 < x)
27843 continue;
27844 if (y > y0 && y > y1)
27845 continue;
27846 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27847 inside = !inside;
27849 return inside;
27852 return 0;
27855 Lisp_Object
27856 find_hot_spot (Lisp_Object map, int x, int y)
27858 while (CONSP (map))
27860 if (CONSP (XCAR (map))
27861 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27862 return XCAR (map);
27863 map = XCDR (map);
27866 return Qnil;
27869 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27870 3, 3, 0,
27871 doc: /* Lookup in image map MAP coordinates X and Y.
27872 An image map is an alist where each element has the format (AREA ID PLIST).
27873 An AREA is specified as either a rectangle, a circle, or a polygon:
27874 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27875 pixel coordinates of the upper left and bottom right corners.
27876 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27877 and the radius of the circle; r may be a float or integer.
27878 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27879 vector describes one corner in the polygon.
27880 Returns the alist element for the first matching AREA in MAP. */)
27881 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27883 if (NILP (map))
27884 return Qnil;
27886 CHECK_NUMBER (x);
27887 CHECK_NUMBER (y);
27889 return find_hot_spot (map,
27890 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27891 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27895 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27896 static void
27897 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27899 /* Do not change cursor shape while dragging mouse. */
27900 if (!NILP (do_mouse_tracking))
27901 return;
27903 if (!NILP (pointer))
27905 if (EQ (pointer, Qarrow))
27906 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27907 else if (EQ (pointer, Qhand))
27908 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27909 else if (EQ (pointer, Qtext))
27910 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27911 else if (EQ (pointer, intern ("hdrag")))
27912 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27913 #ifdef HAVE_X_WINDOWS
27914 else if (EQ (pointer, intern ("vdrag")))
27915 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27916 #endif
27917 else if (EQ (pointer, intern ("hourglass")))
27918 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27919 else if (EQ (pointer, Qmodeline))
27920 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27921 else
27922 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27925 if (cursor != No_Cursor)
27926 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27929 #endif /* HAVE_WINDOW_SYSTEM */
27931 /* Take proper action when mouse has moved to the mode or header line
27932 or marginal area AREA of window W, x-position X and y-position Y.
27933 X is relative to the start of the text display area of W, so the
27934 width of bitmap areas and scroll bars must be subtracted to get a
27935 position relative to the start of the mode line. */
27937 static void
27938 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27939 enum window_part area)
27941 struct window *w = XWINDOW (window);
27942 struct frame *f = XFRAME (w->frame);
27943 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27944 #ifdef HAVE_WINDOW_SYSTEM
27945 Display_Info *dpyinfo;
27946 #endif
27947 Cursor cursor = No_Cursor;
27948 Lisp_Object pointer = Qnil;
27949 int dx, dy, width, height;
27950 ptrdiff_t charpos;
27951 Lisp_Object string, object = Qnil;
27952 Lisp_Object pos IF_LINT (= Qnil), help;
27954 Lisp_Object mouse_face;
27955 int original_x_pixel = x;
27956 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27957 struct glyph_row *row IF_LINT (= 0);
27959 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27961 int x0;
27962 struct glyph *end;
27964 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27965 returns them in row/column units! */
27966 string = mode_line_string (w, area, &x, &y, &charpos,
27967 &object, &dx, &dy, &width, &height);
27969 row = (area == ON_MODE_LINE
27970 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27971 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27973 /* Find the glyph under the mouse pointer. */
27974 if (row->mode_line_p && row->enabled_p)
27976 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27977 end = glyph + row->used[TEXT_AREA];
27979 for (x0 = original_x_pixel;
27980 glyph < end && x0 >= glyph->pixel_width;
27981 ++glyph)
27982 x0 -= glyph->pixel_width;
27984 if (glyph >= end)
27985 glyph = NULL;
27988 else
27990 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27991 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27992 returns them in row/column units! */
27993 string = marginal_area_string (w, area, &x, &y, &charpos,
27994 &object, &dx, &dy, &width, &height);
27997 help = Qnil;
27999 #ifdef HAVE_WINDOW_SYSTEM
28000 if (IMAGEP (object))
28002 Lisp_Object image_map, hotspot;
28003 if ((image_map = Fplist_get (XCDR (object), QCmap),
28004 !NILP (image_map))
28005 && (hotspot = find_hot_spot (image_map, dx, dy),
28006 CONSP (hotspot))
28007 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28009 Lisp_Object plist;
28011 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28012 If so, we could look for mouse-enter, mouse-leave
28013 properties in PLIST (and do something...). */
28014 hotspot = XCDR (hotspot);
28015 if (CONSP (hotspot)
28016 && (plist = XCAR (hotspot), CONSP (plist)))
28018 pointer = Fplist_get (plist, Qpointer);
28019 if (NILP (pointer))
28020 pointer = Qhand;
28021 help = Fplist_get (plist, Qhelp_echo);
28022 if (!NILP (help))
28024 help_echo_string = help;
28025 XSETWINDOW (help_echo_window, w);
28026 help_echo_object = w->contents;
28027 help_echo_pos = charpos;
28031 if (NILP (pointer))
28032 pointer = Fplist_get (XCDR (object), QCpointer);
28034 #endif /* HAVE_WINDOW_SYSTEM */
28036 if (STRINGP (string))
28037 pos = make_number (charpos);
28039 /* Set the help text and mouse pointer. If the mouse is on a part
28040 of the mode line without any text (e.g. past the right edge of
28041 the mode line text), use the default help text and pointer. */
28042 if (STRINGP (string) || area == ON_MODE_LINE)
28044 /* Arrange to display the help by setting the global variables
28045 help_echo_string, help_echo_object, and help_echo_pos. */
28046 if (NILP (help))
28048 if (STRINGP (string))
28049 help = Fget_text_property (pos, Qhelp_echo, string);
28051 if (!NILP (help))
28053 help_echo_string = help;
28054 XSETWINDOW (help_echo_window, w);
28055 help_echo_object = string;
28056 help_echo_pos = charpos;
28058 else if (area == ON_MODE_LINE)
28060 Lisp_Object default_help
28061 = buffer_local_value_1 (Qmode_line_default_help_echo,
28062 w->contents);
28064 if (STRINGP (default_help))
28066 help_echo_string = default_help;
28067 XSETWINDOW (help_echo_window, w);
28068 help_echo_object = Qnil;
28069 help_echo_pos = -1;
28074 #ifdef HAVE_WINDOW_SYSTEM
28075 /* Change the mouse pointer according to what is under it. */
28076 if (FRAME_WINDOW_P (f))
28078 dpyinfo = FRAME_X_DISPLAY_INFO (f);
28079 if (STRINGP (string))
28081 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28083 if (NILP (pointer))
28084 pointer = Fget_text_property (pos, Qpointer, string);
28086 /* Change the mouse pointer according to what is under X/Y. */
28087 if (NILP (pointer)
28088 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28090 Lisp_Object map;
28091 map = Fget_text_property (pos, Qlocal_map, string);
28092 if (!KEYMAPP (map))
28093 map = Fget_text_property (pos, Qkeymap, string);
28094 if (!KEYMAPP (map))
28095 cursor = dpyinfo->vertical_scroll_bar_cursor;
28098 else
28099 /* Default mode-line pointer. */
28100 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28102 #endif
28105 /* Change the mouse face according to what is under X/Y. */
28106 if (STRINGP (string))
28108 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28109 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28110 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28111 && glyph)
28113 Lisp_Object b, e;
28115 struct glyph * tmp_glyph;
28117 int gpos;
28118 int gseq_length;
28119 int total_pixel_width;
28120 ptrdiff_t begpos, endpos, ignore;
28122 int vpos, hpos;
28124 b = Fprevious_single_property_change (make_number (charpos + 1),
28125 Qmouse_face, string, Qnil);
28126 if (NILP (b))
28127 begpos = 0;
28128 else
28129 begpos = XINT (b);
28131 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28132 if (NILP (e))
28133 endpos = SCHARS (string);
28134 else
28135 endpos = XINT (e);
28137 /* Calculate the glyph position GPOS of GLYPH in the
28138 displayed string, relative to the beginning of the
28139 highlighted part of the string.
28141 Note: GPOS is different from CHARPOS. CHARPOS is the
28142 position of GLYPH in the internal string object. A mode
28143 line string format has structures which are converted to
28144 a flattened string by the Emacs Lisp interpreter. The
28145 internal string is an element of those structures. The
28146 displayed string is the flattened string. */
28147 tmp_glyph = row_start_glyph;
28148 while (tmp_glyph < glyph
28149 && (!(EQ (tmp_glyph->object, glyph->object)
28150 && begpos <= tmp_glyph->charpos
28151 && tmp_glyph->charpos < endpos)))
28152 tmp_glyph++;
28153 gpos = glyph - tmp_glyph;
28155 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28156 the highlighted part of the displayed string to which
28157 GLYPH belongs. Note: GSEQ_LENGTH is different from
28158 SCHARS (STRING), because the latter returns the length of
28159 the internal string. */
28160 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28161 tmp_glyph > glyph
28162 && (!(EQ (tmp_glyph->object, glyph->object)
28163 && begpos <= tmp_glyph->charpos
28164 && tmp_glyph->charpos < endpos));
28165 tmp_glyph--)
28167 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28169 /* Calculate the total pixel width of all the glyphs between
28170 the beginning of the highlighted area and GLYPH. */
28171 total_pixel_width = 0;
28172 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28173 total_pixel_width += tmp_glyph->pixel_width;
28175 /* Pre calculation of re-rendering position. Note: X is in
28176 column units here, after the call to mode_line_string or
28177 marginal_area_string. */
28178 hpos = x - gpos;
28179 vpos = (area == ON_MODE_LINE
28180 ? (w->current_matrix)->nrows - 1
28181 : 0);
28183 /* If GLYPH's position is included in the region that is
28184 already drawn in mouse face, we have nothing to do. */
28185 if ( EQ (window, hlinfo->mouse_face_window)
28186 && (!row->reversed_p
28187 ? (hlinfo->mouse_face_beg_col <= hpos
28188 && hpos < hlinfo->mouse_face_end_col)
28189 /* In R2L rows we swap BEG and END, see below. */
28190 : (hlinfo->mouse_face_end_col <= hpos
28191 && hpos < hlinfo->mouse_face_beg_col))
28192 && hlinfo->mouse_face_beg_row == vpos )
28193 return;
28195 if (clear_mouse_face (hlinfo))
28196 cursor = No_Cursor;
28198 if (!row->reversed_p)
28200 hlinfo->mouse_face_beg_col = hpos;
28201 hlinfo->mouse_face_beg_x = original_x_pixel
28202 - (total_pixel_width + dx);
28203 hlinfo->mouse_face_end_col = hpos + gseq_length;
28204 hlinfo->mouse_face_end_x = 0;
28206 else
28208 /* In R2L rows, show_mouse_face expects BEG and END
28209 coordinates to be swapped. */
28210 hlinfo->mouse_face_end_col = hpos;
28211 hlinfo->mouse_face_end_x = original_x_pixel
28212 - (total_pixel_width + dx);
28213 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28214 hlinfo->mouse_face_beg_x = 0;
28217 hlinfo->mouse_face_beg_row = vpos;
28218 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28219 hlinfo->mouse_face_past_end = 0;
28220 hlinfo->mouse_face_window = window;
28222 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28223 charpos,
28224 0, 0, 0,
28225 &ignore,
28226 glyph->face_id,
28228 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28230 if (NILP (pointer))
28231 pointer = Qhand;
28233 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28234 clear_mouse_face (hlinfo);
28236 #ifdef HAVE_WINDOW_SYSTEM
28237 if (FRAME_WINDOW_P (f))
28238 define_frame_cursor1 (f, cursor, pointer);
28239 #endif
28243 /* EXPORT:
28244 Take proper action when the mouse has moved to position X, Y on
28245 frame F with regards to highlighting portions of display that have
28246 mouse-face properties. Also de-highlight portions of display where
28247 the mouse was before, set the mouse pointer shape as appropriate
28248 for the mouse coordinates, and activate help echo (tooltips).
28249 X and Y can be negative or out of range. */
28251 void
28252 note_mouse_highlight (struct frame *f, int x, int y)
28254 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28255 enum window_part part = ON_NOTHING;
28256 Lisp_Object window;
28257 struct window *w;
28258 Cursor cursor = No_Cursor;
28259 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28260 struct buffer *b;
28262 /* When a menu is active, don't highlight because this looks odd. */
28263 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28264 if (popup_activated ())
28265 return;
28266 #endif
28268 if (!f->glyphs_initialized_p
28269 || f->pointer_invisible)
28270 return;
28272 hlinfo->mouse_face_mouse_x = x;
28273 hlinfo->mouse_face_mouse_y = y;
28274 hlinfo->mouse_face_mouse_frame = f;
28276 if (hlinfo->mouse_face_defer)
28277 return;
28279 /* Which window is that in? */
28280 window = window_from_coordinates (f, x, y, &part, 1);
28282 /* If displaying active text in another window, clear that. */
28283 if (! EQ (window, hlinfo->mouse_face_window)
28284 /* Also clear if we move out of text area in same window. */
28285 || (!NILP (hlinfo->mouse_face_window)
28286 && !NILP (window)
28287 && part != ON_TEXT
28288 && part != ON_MODE_LINE
28289 && part != ON_HEADER_LINE))
28290 clear_mouse_face (hlinfo);
28292 /* Not on a window -> return. */
28293 if (!WINDOWP (window))
28294 return;
28296 /* Reset help_echo_string. It will get recomputed below. */
28297 help_echo_string = Qnil;
28299 /* Convert to window-relative pixel coordinates. */
28300 w = XWINDOW (window);
28301 frame_to_window_pixel_xy (w, &x, &y);
28303 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28304 /* Handle tool-bar window differently since it doesn't display a
28305 buffer. */
28306 if (EQ (window, f->tool_bar_window))
28308 note_tool_bar_highlight (f, x, y);
28309 return;
28311 #endif
28313 /* Mouse is on the mode, header line or margin? */
28314 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28315 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28317 note_mode_line_or_margin_highlight (window, x, y, part);
28318 return;
28321 #ifdef HAVE_WINDOW_SYSTEM
28322 if (part == ON_VERTICAL_BORDER)
28324 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28325 help_echo_string = build_string ("drag-mouse-1: resize");
28327 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28328 || part == ON_SCROLL_BAR)
28329 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28330 else
28331 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28332 #endif
28334 /* Are we in a window whose display is up to date?
28335 And verify the buffer's text has not changed. */
28336 b = XBUFFER (w->contents);
28337 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28339 int hpos, vpos, dx, dy, area = LAST_AREA;
28340 ptrdiff_t pos;
28341 struct glyph *glyph;
28342 Lisp_Object object;
28343 Lisp_Object mouse_face = Qnil, position;
28344 Lisp_Object *overlay_vec = NULL;
28345 ptrdiff_t i, noverlays;
28346 struct buffer *obuf;
28347 ptrdiff_t obegv, ozv;
28348 int same_region;
28350 /* Find the glyph under X/Y. */
28351 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28353 #ifdef HAVE_WINDOW_SYSTEM
28354 /* Look for :pointer property on image. */
28355 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28357 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28358 if (img != NULL && IMAGEP (img->spec))
28360 Lisp_Object image_map, hotspot;
28361 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28362 !NILP (image_map))
28363 && (hotspot = find_hot_spot (image_map,
28364 glyph->slice.img.x + dx,
28365 glyph->slice.img.y + dy),
28366 CONSP (hotspot))
28367 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28369 Lisp_Object plist;
28371 /* Could check XCAR (hotspot) to see if we enter/leave
28372 this hot-spot.
28373 If so, we could look for mouse-enter, mouse-leave
28374 properties in PLIST (and do something...). */
28375 hotspot = XCDR (hotspot);
28376 if (CONSP (hotspot)
28377 && (plist = XCAR (hotspot), CONSP (plist)))
28379 pointer = Fplist_get (plist, Qpointer);
28380 if (NILP (pointer))
28381 pointer = Qhand;
28382 help_echo_string = Fplist_get (plist, Qhelp_echo);
28383 if (!NILP (help_echo_string))
28385 help_echo_window = window;
28386 help_echo_object = glyph->object;
28387 help_echo_pos = glyph->charpos;
28391 if (NILP (pointer))
28392 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28395 #endif /* HAVE_WINDOW_SYSTEM */
28397 /* Clear mouse face if X/Y not over text. */
28398 if (glyph == NULL
28399 || area != TEXT_AREA
28400 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28401 /* Glyph's OBJECT is an integer for glyphs inserted by the
28402 display engine for its internal purposes, like truncation
28403 and continuation glyphs and blanks beyond the end of
28404 line's text on text terminals. If we are over such a
28405 glyph, we are not over any text. */
28406 || INTEGERP (glyph->object)
28407 /* R2L rows have a stretch glyph at their front, which
28408 stands for no text, whereas L2R rows have no glyphs at
28409 all beyond the end of text. Treat such stretch glyphs
28410 like we do with NULL glyphs in L2R rows. */
28411 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28412 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28413 && glyph->type == STRETCH_GLYPH
28414 && glyph->avoid_cursor_p))
28416 if (clear_mouse_face (hlinfo))
28417 cursor = No_Cursor;
28418 #ifdef HAVE_WINDOW_SYSTEM
28419 if (FRAME_WINDOW_P (f) && NILP (pointer))
28421 if (area != TEXT_AREA)
28422 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28423 else
28424 pointer = Vvoid_text_area_pointer;
28426 #endif
28427 goto set_cursor;
28430 pos = glyph->charpos;
28431 object = glyph->object;
28432 if (!STRINGP (object) && !BUFFERP (object))
28433 goto set_cursor;
28435 /* If we get an out-of-range value, return now; avoid an error. */
28436 if (BUFFERP (object) && pos > BUF_Z (b))
28437 goto set_cursor;
28439 /* Make the window's buffer temporarily current for
28440 overlays_at and compute_char_face. */
28441 obuf = current_buffer;
28442 current_buffer = b;
28443 obegv = BEGV;
28444 ozv = ZV;
28445 BEGV = BEG;
28446 ZV = Z;
28448 /* Is this char mouse-active or does it have help-echo? */
28449 position = make_number (pos);
28451 if (BUFFERP (object))
28453 /* Put all the overlays we want in a vector in overlay_vec. */
28454 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28455 /* Sort overlays into increasing priority order. */
28456 noverlays = sort_overlays (overlay_vec, noverlays, w);
28458 else
28459 noverlays = 0;
28461 if (NILP (Vmouse_highlight))
28463 clear_mouse_face (hlinfo);
28464 goto check_help_echo;
28467 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28469 if (same_region)
28470 cursor = No_Cursor;
28472 /* Check mouse-face highlighting. */
28473 if (! same_region
28474 /* If there exists an overlay with mouse-face overlapping
28475 the one we are currently highlighting, we have to
28476 check if we enter the overlapping overlay, and then
28477 highlight only that. */
28478 || (OVERLAYP (hlinfo->mouse_face_overlay)
28479 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28481 /* Find the highest priority overlay with a mouse-face. */
28482 Lisp_Object overlay = Qnil;
28483 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28485 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28486 if (!NILP (mouse_face))
28487 overlay = overlay_vec[i];
28490 /* If we're highlighting the same overlay as before, there's
28491 no need to do that again. */
28492 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28493 goto check_help_echo;
28494 hlinfo->mouse_face_overlay = overlay;
28496 /* Clear the display of the old active region, if any. */
28497 if (clear_mouse_face (hlinfo))
28498 cursor = No_Cursor;
28500 /* If no overlay applies, get a text property. */
28501 if (NILP (overlay))
28502 mouse_face = Fget_text_property (position, Qmouse_face, object);
28504 /* Next, compute the bounds of the mouse highlighting and
28505 display it. */
28506 if (!NILP (mouse_face) && STRINGP (object))
28508 /* The mouse-highlighting comes from a display string
28509 with a mouse-face. */
28510 Lisp_Object s, e;
28511 ptrdiff_t ignore;
28513 s = Fprevious_single_property_change
28514 (make_number (pos + 1), Qmouse_face, object, Qnil);
28515 e = Fnext_single_property_change
28516 (position, Qmouse_face, object, Qnil);
28517 if (NILP (s))
28518 s = make_number (0);
28519 if (NILP (e))
28520 e = make_number (SCHARS (object) - 1);
28521 mouse_face_from_string_pos (w, hlinfo, object,
28522 XINT (s), XINT (e));
28523 hlinfo->mouse_face_past_end = 0;
28524 hlinfo->mouse_face_window = window;
28525 hlinfo->mouse_face_face_id
28526 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28527 glyph->face_id, 1);
28528 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28529 cursor = No_Cursor;
28531 else
28533 /* The mouse-highlighting, if any, comes from an overlay
28534 or text property in the buffer. */
28535 Lisp_Object buffer IF_LINT (= Qnil);
28536 Lisp_Object disp_string IF_LINT (= Qnil);
28538 if (STRINGP (object))
28540 /* If we are on a display string with no mouse-face,
28541 check if the text under it has one. */
28542 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28543 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28544 pos = string_buffer_position (object, start);
28545 if (pos > 0)
28547 mouse_face = get_char_property_and_overlay
28548 (make_number (pos), Qmouse_face, w->contents, &overlay);
28549 buffer = w->contents;
28550 disp_string = object;
28553 else
28555 buffer = object;
28556 disp_string = Qnil;
28559 if (!NILP (mouse_face))
28561 Lisp_Object before, after;
28562 Lisp_Object before_string, after_string;
28563 /* To correctly find the limits of mouse highlight
28564 in a bidi-reordered buffer, we must not use the
28565 optimization of limiting the search in
28566 previous-single-property-change and
28567 next-single-property-change, because
28568 rows_from_pos_range needs the real start and end
28569 positions to DTRT in this case. That's because
28570 the first row visible in a window does not
28571 necessarily display the character whose position
28572 is the smallest. */
28573 Lisp_Object lim1 =
28574 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28575 ? Fmarker_position (w->start)
28576 : Qnil;
28577 Lisp_Object lim2 =
28578 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28579 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28580 : Qnil;
28582 if (NILP (overlay))
28584 /* Handle the text property case. */
28585 before = Fprevious_single_property_change
28586 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28587 after = Fnext_single_property_change
28588 (make_number (pos), Qmouse_face, buffer, lim2);
28589 before_string = after_string = Qnil;
28591 else
28593 /* Handle the overlay case. */
28594 before = Foverlay_start (overlay);
28595 after = Foverlay_end (overlay);
28596 before_string = Foverlay_get (overlay, Qbefore_string);
28597 after_string = Foverlay_get (overlay, Qafter_string);
28599 if (!STRINGP (before_string)) before_string = Qnil;
28600 if (!STRINGP (after_string)) after_string = Qnil;
28603 mouse_face_from_buffer_pos (window, hlinfo, pos,
28604 NILP (before)
28606 : XFASTINT (before),
28607 NILP (after)
28608 ? BUF_Z (XBUFFER (buffer))
28609 : XFASTINT (after),
28610 before_string, after_string,
28611 disp_string);
28612 cursor = No_Cursor;
28617 check_help_echo:
28619 /* Look for a `help-echo' property. */
28620 if (NILP (help_echo_string)) {
28621 Lisp_Object help, overlay;
28623 /* Check overlays first. */
28624 help = overlay = Qnil;
28625 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28627 overlay = overlay_vec[i];
28628 help = Foverlay_get (overlay, Qhelp_echo);
28631 if (!NILP (help))
28633 help_echo_string = help;
28634 help_echo_window = window;
28635 help_echo_object = overlay;
28636 help_echo_pos = pos;
28638 else
28640 Lisp_Object obj = glyph->object;
28641 ptrdiff_t charpos = glyph->charpos;
28643 /* Try text properties. */
28644 if (STRINGP (obj)
28645 && charpos >= 0
28646 && charpos < SCHARS (obj))
28648 help = Fget_text_property (make_number (charpos),
28649 Qhelp_echo, obj);
28650 if (NILP (help))
28652 /* If the string itself doesn't specify a help-echo,
28653 see if the buffer text ``under'' it does. */
28654 struct glyph_row *r
28655 = MATRIX_ROW (w->current_matrix, vpos);
28656 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28657 ptrdiff_t p = string_buffer_position (obj, start);
28658 if (p > 0)
28660 help = Fget_char_property (make_number (p),
28661 Qhelp_echo, w->contents);
28662 if (!NILP (help))
28664 charpos = p;
28665 obj = w->contents;
28670 else if (BUFFERP (obj)
28671 && charpos >= BEGV
28672 && charpos < ZV)
28673 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28674 obj);
28676 if (!NILP (help))
28678 help_echo_string = help;
28679 help_echo_window = window;
28680 help_echo_object = obj;
28681 help_echo_pos = charpos;
28686 #ifdef HAVE_WINDOW_SYSTEM
28687 /* Look for a `pointer' property. */
28688 if (FRAME_WINDOW_P (f) && NILP (pointer))
28690 /* Check overlays first. */
28691 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28692 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28694 if (NILP (pointer))
28696 Lisp_Object obj = glyph->object;
28697 ptrdiff_t charpos = glyph->charpos;
28699 /* Try text properties. */
28700 if (STRINGP (obj)
28701 && charpos >= 0
28702 && charpos < SCHARS (obj))
28704 pointer = Fget_text_property (make_number (charpos),
28705 Qpointer, obj);
28706 if (NILP (pointer))
28708 /* If the string itself doesn't specify a pointer,
28709 see if the buffer text ``under'' it does. */
28710 struct glyph_row *r
28711 = MATRIX_ROW (w->current_matrix, vpos);
28712 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28713 ptrdiff_t p = string_buffer_position (obj, start);
28714 if (p > 0)
28715 pointer = Fget_char_property (make_number (p),
28716 Qpointer, w->contents);
28719 else if (BUFFERP (obj)
28720 && charpos >= BEGV
28721 && charpos < ZV)
28722 pointer = Fget_text_property (make_number (charpos),
28723 Qpointer, obj);
28726 #endif /* HAVE_WINDOW_SYSTEM */
28728 BEGV = obegv;
28729 ZV = ozv;
28730 current_buffer = obuf;
28733 set_cursor:
28735 #ifdef HAVE_WINDOW_SYSTEM
28736 if (FRAME_WINDOW_P (f))
28737 define_frame_cursor1 (f, cursor, pointer);
28738 #else
28739 /* This is here to prevent a compiler error, about "label at end of
28740 compound statement". */
28741 return;
28742 #endif
28746 /* EXPORT for RIF:
28747 Clear any mouse-face on window W. This function is part of the
28748 redisplay interface, and is called from try_window_id and similar
28749 functions to ensure the mouse-highlight is off. */
28751 void
28752 x_clear_window_mouse_face (struct window *w)
28754 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28755 Lisp_Object window;
28757 block_input ();
28758 XSETWINDOW (window, w);
28759 if (EQ (window, hlinfo->mouse_face_window))
28760 clear_mouse_face (hlinfo);
28761 unblock_input ();
28765 /* EXPORT:
28766 Just discard the mouse face information for frame F, if any.
28767 This is used when the size of F is changed. */
28769 void
28770 cancel_mouse_face (struct frame *f)
28772 Lisp_Object window;
28773 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28775 window = hlinfo->mouse_face_window;
28776 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28777 reset_mouse_highlight (hlinfo);
28782 /***********************************************************************
28783 Exposure Events
28784 ***********************************************************************/
28786 #ifdef HAVE_WINDOW_SYSTEM
28788 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28789 which intersects rectangle R. R is in window-relative coordinates. */
28791 static void
28792 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28793 enum glyph_row_area area)
28795 struct glyph *first = row->glyphs[area];
28796 struct glyph *end = row->glyphs[area] + row->used[area];
28797 struct glyph *last;
28798 int first_x, start_x, x;
28800 if (area == TEXT_AREA && row->fill_line_p)
28801 /* If row extends face to end of line write the whole line. */
28802 draw_glyphs (w, 0, row, area,
28803 0, row->used[area],
28804 DRAW_NORMAL_TEXT, 0);
28805 else
28807 /* Set START_X to the window-relative start position for drawing glyphs of
28808 AREA. The first glyph of the text area can be partially visible.
28809 The first glyphs of other areas cannot. */
28810 start_x = window_box_left_offset (w, area);
28811 x = start_x;
28812 if (area == TEXT_AREA)
28813 x += row->x;
28815 /* Find the first glyph that must be redrawn. */
28816 while (first < end
28817 && x + first->pixel_width < r->x)
28819 x += first->pixel_width;
28820 ++first;
28823 /* Find the last one. */
28824 last = first;
28825 first_x = x;
28826 while (last < end
28827 && x < r->x + r->width)
28829 x += last->pixel_width;
28830 ++last;
28833 /* Repaint. */
28834 if (last > first)
28835 draw_glyphs (w, first_x - start_x, row, area,
28836 first - row->glyphs[area], last - row->glyphs[area],
28837 DRAW_NORMAL_TEXT, 0);
28842 /* Redraw the parts of the glyph row ROW on window W intersecting
28843 rectangle R. R is in window-relative coordinates. Value is
28844 non-zero if mouse-face was overwritten. */
28846 static int
28847 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28849 eassert (row->enabled_p);
28851 if (row->mode_line_p || w->pseudo_window_p)
28852 draw_glyphs (w, 0, row, TEXT_AREA,
28853 0, row->used[TEXT_AREA],
28854 DRAW_NORMAL_TEXT, 0);
28855 else
28857 if (row->used[LEFT_MARGIN_AREA])
28858 expose_area (w, row, r, LEFT_MARGIN_AREA);
28859 if (row->used[TEXT_AREA])
28860 expose_area (w, row, r, TEXT_AREA);
28861 if (row->used[RIGHT_MARGIN_AREA])
28862 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28863 draw_row_fringe_bitmaps (w, row);
28866 return row->mouse_face_p;
28870 /* Redraw those parts of glyphs rows during expose event handling that
28871 overlap other rows. Redrawing of an exposed line writes over parts
28872 of lines overlapping that exposed line; this function fixes that.
28874 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28875 row in W's current matrix that is exposed and overlaps other rows.
28876 LAST_OVERLAPPING_ROW is the last such row. */
28878 static void
28879 expose_overlaps (struct window *w,
28880 struct glyph_row *first_overlapping_row,
28881 struct glyph_row *last_overlapping_row,
28882 XRectangle *r)
28884 struct glyph_row *row;
28886 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28887 if (row->overlapping_p)
28889 eassert (row->enabled_p && !row->mode_line_p);
28891 row->clip = r;
28892 if (row->used[LEFT_MARGIN_AREA])
28893 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28895 if (row->used[TEXT_AREA])
28896 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28898 if (row->used[RIGHT_MARGIN_AREA])
28899 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28900 row->clip = NULL;
28905 /* Return non-zero if W's cursor intersects rectangle R. */
28907 static int
28908 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28910 XRectangle cr, result;
28911 struct glyph *cursor_glyph;
28912 struct glyph_row *row;
28914 if (w->phys_cursor.vpos >= 0
28915 && w->phys_cursor.vpos < w->current_matrix->nrows
28916 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28917 row->enabled_p)
28918 && row->cursor_in_fringe_p)
28920 /* Cursor is in the fringe. */
28921 cr.x = window_box_right_offset (w,
28922 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28923 ? RIGHT_MARGIN_AREA
28924 : TEXT_AREA));
28925 cr.y = row->y;
28926 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28927 cr.height = row->height;
28928 return x_intersect_rectangles (&cr, r, &result);
28931 cursor_glyph = get_phys_cursor_glyph (w);
28932 if (cursor_glyph)
28934 /* r is relative to W's box, but w->phys_cursor.x is relative
28935 to left edge of W's TEXT area. Adjust it. */
28936 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28937 cr.y = w->phys_cursor.y;
28938 cr.width = cursor_glyph->pixel_width;
28939 cr.height = w->phys_cursor_height;
28940 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28941 I assume the effect is the same -- and this is portable. */
28942 return x_intersect_rectangles (&cr, r, &result);
28944 /* If we don't understand the format, pretend we're not in the hot-spot. */
28945 return 0;
28949 /* EXPORT:
28950 Draw a vertical window border to the right of window W if W doesn't
28951 have vertical scroll bars. */
28953 void
28954 x_draw_vertical_border (struct window *w)
28956 struct frame *f = XFRAME (WINDOW_FRAME (w));
28958 /* We could do better, if we knew what type of scroll-bar the adjacent
28959 windows (on either side) have... But we don't :-(
28960 However, I think this works ok. ++KFS 2003-04-25 */
28962 /* Redraw borders between horizontally adjacent windows. Don't
28963 do it for frames with vertical scroll bars because either the
28964 right scroll bar of a window, or the left scroll bar of its
28965 neighbor will suffice as a border. */
28966 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28967 return;
28969 /* Note: It is necessary to redraw both the left and the right
28970 borders, for when only this single window W is being
28971 redisplayed. */
28972 if (!WINDOW_RIGHTMOST_P (w)
28973 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28975 int x0, x1, y0, y1;
28977 window_box_edges (w, &x0, &y0, &x1, &y1);
28978 y1 -= 1;
28980 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28981 x1 -= 1;
28983 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28985 if (!WINDOW_LEFTMOST_P (w)
28986 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28988 int x0, x1, y0, y1;
28990 window_box_edges (w, &x0, &y0, &x1, &y1);
28991 y1 -= 1;
28993 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28994 x0 -= 1;
28996 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29001 /* Redraw the part of window W intersection rectangle FR. Pixel
29002 coordinates in FR are frame-relative. Call this function with
29003 input blocked. Value is non-zero if the exposure overwrites
29004 mouse-face. */
29006 static int
29007 expose_window (struct window *w, XRectangle *fr)
29009 struct frame *f = XFRAME (w->frame);
29010 XRectangle wr, r;
29011 int mouse_face_overwritten_p = 0;
29013 /* If window is not yet fully initialized, do nothing. This can
29014 happen when toolkit scroll bars are used and a window is split.
29015 Reconfiguring the scroll bar will generate an expose for a newly
29016 created window. */
29017 if (w->current_matrix == NULL)
29018 return 0;
29020 /* When we're currently updating the window, display and current
29021 matrix usually don't agree. Arrange for a thorough display
29022 later. */
29023 if (w->must_be_updated_p)
29025 SET_FRAME_GARBAGED (f);
29026 return 0;
29029 /* Frame-relative pixel rectangle of W. */
29030 wr.x = WINDOW_LEFT_EDGE_X (w);
29031 wr.y = WINDOW_TOP_EDGE_Y (w);
29032 wr.width = WINDOW_TOTAL_WIDTH (w);
29033 wr.height = WINDOW_TOTAL_HEIGHT (w);
29035 if (x_intersect_rectangles (fr, &wr, &r))
29037 int yb = window_text_bottom_y (w);
29038 struct glyph_row *row;
29039 int cursor_cleared_p, phys_cursor_on_p;
29040 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29042 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29043 r.x, r.y, r.width, r.height));
29045 /* Convert to window coordinates. */
29046 r.x -= WINDOW_LEFT_EDGE_X (w);
29047 r.y -= WINDOW_TOP_EDGE_Y (w);
29049 /* Turn off the cursor. */
29050 if (!w->pseudo_window_p
29051 && phys_cursor_in_rect_p (w, &r))
29053 x_clear_cursor (w);
29054 cursor_cleared_p = 1;
29056 else
29057 cursor_cleared_p = 0;
29059 /* If the row containing the cursor extends face to end of line,
29060 then expose_area might overwrite the cursor outside the
29061 rectangle and thus notice_overwritten_cursor might clear
29062 w->phys_cursor_on_p. We remember the original value and
29063 check later if it is changed. */
29064 phys_cursor_on_p = w->phys_cursor_on_p;
29066 /* Update lines intersecting rectangle R. */
29067 first_overlapping_row = last_overlapping_row = NULL;
29068 for (row = w->current_matrix->rows;
29069 row->enabled_p;
29070 ++row)
29072 int y0 = row->y;
29073 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29075 if ((y0 >= r.y && y0 < r.y + r.height)
29076 || (y1 > r.y && y1 < r.y + r.height)
29077 || (r.y >= y0 && r.y < y1)
29078 || (r.y + r.height > y0 && r.y + r.height < y1))
29080 /* A header line may be overlapping, but there is no need
29081 to fix overlapping areas for them. KFS 2005-02-12 */
29082 if (row->overlapping_p && !row->mode_line_p)
29084 if (first_overlapping_row == NULL)
29085 first_overlapping_row = row;
29086 last_overlapping_row = row;
29089 row->clip = fr;
29090 if (expose_line (w, row, &r))
29091 mouse_face_overwritten_p = 1;
29092 row->clip = NULL;
29094 else if (row->overlapping_p)
29096 /* We must redraw a row overlapping the exposed area. */
29097 if (y0 < r.y
29098 ? y0 + row->phys_height > r.y
29099 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29101 if (first_overlapping_row == NULL)
29102 first_overlapping_row = row;
29103 last_overlapping_row = row;
29107 if (y1 >= yb)
29108 break;
29111 /* Display the mode line if there is one. */
29112 if (WINDOW_WANTS_MODELINE_P (w)
29113 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29114 row->enabled_p)
29115 && row->y < r.y + r.height)
29117 if (expose_line (w, row, &r))
29118 mouse_face_overwritten_p = 1;
29121 if (!w->pseudo_window_p)
29123 /* Fix the display of overlapping rows. */
29124 if (first_overlapping_row)
29125 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29126 fr);
29128 /* Draw border between windows. */
29129 x_draw_vertical_border (w);
29131 /* Turn the cursor on again. */
29132 if (cursor_cleared_p
29133 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29134 update_window_cursor (w, 1);
29138 return mouse_face_overwritten_p;
29143 /* Redraw (parts) of all windows in the window tree rooted at W that
29144 intersect R. R contains frame pixel coordinates. Value is
29145 non-zero if the exposure overwrites mouse-face. */
29147 static int
29148 expose_window_tree (struct window *w, XRectangle *r)
29150 struct frame *f = XFRAME (w->frame);
29151 int mouse_face_overwritten_p = 0;
29153 while (w && !FRAME_GARBAGED_P (f))
29155 if (WINDOWP (w->contents))
29156 mouse_face_overwritten_p
29157 |= expose_window_tree (XWINDOW (w->contents), r);
29158 else
29159 mouse_face_overwritten_p |= expose_window (w, r);
29161 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29164 return mouse_face_overwritten_p;
29168 /* EXPORT:
29169 Redisplay an exposed area of frame F. X and Y are the upper-left
29170 corner of the exposed rectangle. W and H are width and height of
29171 the exposed area. All are pixel values. W or H zero means redraw
29172 the entire frame. */
29174 void
29175 expose_frame (struct frame *f, int x, int y, int w, int h)
29177 XRectangle r;
29178 int mouse_face_overwritten_p = 0;
29180 TRACE ((stderr, "expose_frame "));
29182 /* No need to redraw if frame will be redrawn soon. */
29183 if (FRAME_GARBAGED_P (f))
29185 TRACE ((stderr, " garbaged\n"));
29186 return;
29189 /* If basic faces haven't been realized yet, there is no point in
29190 trying to redraw anything. This can happen when we get an expose
29191 event while Emacs is starting, e.g. by moving another window. */
29192 if (FRAME_FACE_CACHE (f) == NULL
29193 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29195 TRACE ((stderr, " no faces\n"));
29196 return;
29199 if (w == 0 || h == 0)
29201 r.x = r.y = 0;
29202 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29203 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29205 else
29207 r.x = x;
29208 r.y = y;
29209 r.width = w;
29210 r.height = h;
29213 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29214 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29216 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29217 if (WINDOWP (f->tool_bar_window))
29218 mouse_face_overwritten_p
29219 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29220 #endif
29222 #ifdef HAVE_X_WINDOWS
29223 #ifndef MSDOS
29224 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29225 if (WINDOWP (f->menu_bar_window))
29226 mouse_face_overwritten_p
29227 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29228 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29229 #endif
29230 #endif
29232 /* Some window managers support a focus-follows-mouse style with
29233 delayed raising of frames. Imagine a partially obscured frame,
29234 and moving the mouse into partially obscured mouse-face on that
29235 frame. The visible part of the mouse-face will be highlighted,
29236 then the WM raises the obscured frame. With at least one WM, KDE
29237 2.1, Emacs is not getting any event for the raising of the frame
29238 (even tried with SubstructureRedirectMask), only Expose events.
29239 These expose events will draw text normally, i.e. not
29240 highlighted. Which means we must redo the highlight here.
29241 Subsume it under ``we love X''. --gerd 2001-08-15 */
29242 /* Included in Windows version because Windows most likely does not
29243 do the right thing if any third party tool offers
29244 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29245 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29247 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29248 if (f == hlinfo->mouse_face_mouse_frame)
29250 int mouse_x = hlinfo->mouse_face_mouse_x;
29251 int mouse_y = hlinfo->mouse_face_mouse_y;
29252 clear_mouse_face (hlinfo);
29253 note_mouse_highlight (f, mouse_x, mouse_y);
29259 /* EXPORT:
29260 Determine the intersection of two rectangles R1 and R2. Return
29261 the intersection in *RESULT. Value is non-zero if RESULT is not
29262 empty. */
29265 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29267 XRectangle *left, *right;
29268 XRectangle *upper, *lower;
29269 int intersection_p = 0;
29271 /* Rearrange so that R1 is the left-most rectangle. */
29272 if (r1->x < r2->x)
29273 left = r1, right = r2;
29274 else
29275 left = r2, right = r1;
29277 /* X0 of the intersection is right.x0, if this is inside R1,
29278 otherwise there is no intersection. */
29279 if (right->x <= left->x + left->width)
29281 result->x = right->x;
29283 /* The right end of the intersection is the minimum of
29284 the right ends of left and right. */
29285 result->width = (min (left->x + left->width, right->x + right->width)
29286 - result->x);
29288 /* Same game for Y. */
29289 if (r1->y < r2->y)
29290 upper = r1, lower = r2;
29291 else
29292 upper = r2, lower = r1;
29294 /* The upper end of the intersection is lower.y0, if this is inside
29295 of upper. Otherwise, there is no intersection. */
29296 if (lower->y <= upper->y + upper->height)
29298 result->y = lower->y;
29300 /* The lower end of the intersection is the minimum of the lower
29301 ends of upper and lower. */
29302 result->height = (min (lower->y + lower->height,
29303 upper->y + upper->height)
29304 - result->y);
29305 intersection_p = 1;
29309 return intersection_p;
29312 #endif /* HAVE_WINDOW_SYSTEM */
29315 /***********************************************************************
29316 Initialization
29317 ***********************************************************************/
29319 void
29320 syms_of_xdisp (void)
29322 Vwith_echo_area_save_vector = Qnil;
29323 staticpro (&Vwith_echo_area_save_vector);
29325 Vmessage_stack = Qnil;
29326 staticpro (&Vmessage_stack);
29328 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29329 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29331 message_dolog_marker1 = Fmake_marker ();
29332 staticpro (&message_dolog_marker1);
29333 message_dolog_marker2 = Fmake_marker ();
29334 staticpro (&message_dolog_marker2);
29335 message_dolog_marker3 = Fmake_marker ();
29336 staticpro (&message_dolog_marker3);
29338 #ifdef GLYPH_DEBUG
29339 defsubr (&Sdump_frame_glyph_matrix);
29340 defsubr (&Sdump_glyph_matrix);
29341 defsubr (&Sdump_glyph_row);
29342 defsubr (&Sdump_tool_bar_row);
29343 defsubr (&Strace_redisplay);
29344 defsubr (&Strace_to_stderr);
29345 #endif
29346 #ifdef HAVE_WINDOW_SYSTEM
29347 defsubr (&Stool_bar_lines_needed);
29348 defsubr (&Slookup_image_map);
29349 #endif
29350 defsubr (&Sline_pixel_height);
29351 defsubr (&Sformat_mode_line);
29352 defsubr (&Sinvisible_p);
29353 defsubr (&Scurrent_bidi_paragraph_direction);
29354 defsubr (&Smove_point_visually);
29356 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29357 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29358 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29359 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29360 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29361 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29362 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29363 DEFSYM (Qeval, "eval");
29364 DEFSYM (QCdata, ":data");
29365 DEFSYM (Qdisplay, "display");
29366 DEFSYM (Qspace_width, "space-width");
29367 DEFSYM (Qraise, "raise");
29368 DEFSYM (Qslice, "slice");
29369 DEFSYM (Qspace, "space");
29370 DEFSYM (Qmargin, "margin");
29371 DEFSYM (Qpointer, "pointer");
29372 DEFSYM (Qleft_margin, "left-margin");
29373 DEFSYM (Qright_margin, "right-margin");
29374 DEFSYM (Qcenter, "center");
29375 DEFSYM (Qline_height, "line-height");
29376 DEFSYM (QCalign_to, ":align-to");
29377 DEFSYM (QCrelative_width, ":relative-width");
29378 DEFSYM (QCrelative_height, ":relative-height");
29379 DEFSYM (QCeval, ":eval");
29380 DEFSYM (QCpropertize, ":propertize");
29381 DEFSYM (QCfile, ":file");
29382 DEFSYM (Qfontified, "fontified");
29383 DEFSYM (Qfontification_functions, "fontification-functions");
29384 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29385 DEFSYM (Qescape_glyph, "escape-glyph");
29386 DEFSYM (Qnobreak_space, "nobreak-space");
29387 DEFSYM (Qimage, "image");
29388 DEFSYM (Qtext, "text");
29389 DEFSYM (Qboth, "both");
29390 DEFSYM (Qboth_horiz, "both-horiz");
29391 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29392 DEFSYM (QCmap, ":map");
29393 DEFSYM (QCpointer, ":pointer");
29394 DEFSYM (Qrect, "rect");
29395 DEFSYM (Qcircle, "circle");
29396 DEFSYM (Qpoly, "poly");
29397 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29398 DEFSYM (Qgrow_only, "grow-only");
29399 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29400 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29401 DEFSYM (Qposition, "position");
29402 DEFSYM (Qbuffer_position, "buffer-position");
29403 DEFSYM (Qobject, "object");
29404 DEFSYM (Qbar, "bar");
29405 DEFSYM (Qhbar, "hbar");
29406 DEFSYM (Qbox, "box");
29407 DEFSYM (Qhollow, "hollow");
29408 DEFSYM (Qhand, "hand");
29409 DEFSYM (Qarrow, "arrow");
29410 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29412 list_of_error = list1 (list2 (intern_c_string ("error"),
29413 intern_c_string ("void-variable")));
29414 staticpro (&list_of_error);
29416 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29417 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29418 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29419 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29421 echo_buffer[0] = echo_buffer[1] = Qnil;
29422 staticpro (&echo_buffer[0]);
29423 staticpro (&echo_buffer[1]);
29425 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29426 staticpro (&echo_area_buffer[0]);
29427 staticpro (&echo_area_buffer[1]);
29429 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29430 staticpro (&Vmessages_buffer_name);
29432 mode_line_proptrans_alist = Qnil;
29433 staticpro (&mode_line_proptrans_alist);
29434 mode_line_string_list = Qnil;
29435 staticpro (&mode_line_string_list);
29436 mode_line_string_face = Qnil;
29437 staticpro (&mode_line_string_face);
29438 mode_line_string_face_prop = Qnil;
29439 staticpro (&mode_line_string_face_prop);
29440 Vmode_line_unwind_vector = Qnil;
29441 staticpro (&Vmode_line_unwind_vector);
29443 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29445 help_echo_string = Qnil;
29446 staticpro (&help_echo_string);
29447 help_echo_object = Qnil;
29448 staticpro (&help_echo_object);
29449 help_echo_window = Qnil;
29450 staticpro (&help_echo_window);
29451 previous_help_echo_string = Qnil;
29452 staticpro (&previous_help_echo_string);
29453 help_echo_pos = -1;
29455 DEFSYM (Qright_to_left, "right-to-left");
29456 DEFSYM (Qleft_to_right, "left-to-right");
29458 #ifdef HAVE_WINDOW_SYSTEM
29459 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29460 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29461 For example, if a block cursor is over a tab, it will be drawn as
29462 wide as that tab on the display. */);
29463 x_stretch_cursor_p = 0;
29464 #endif
29466 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29467 doc: /* Non-nil means highlight trailing whitespace.
29468 The face used for trailing whitespace is `trailing-whitespace'. */);
29469 Vshow_trailing_whitespace = Qnil;
29471 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29472 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29473 If the value is t, Emacs highlights non-ASCII chars which have the
29474 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29475 or `escape-glyph' face respectively.
29477 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29478 U+2011 (non-breaking hyphen) are affected.
29480 Any other non-nil value means to display these characters as a escape
29481 glyph followed by an ordinary space or hyphen.
29483 A value of nil means no special handling of these characters. */);
29484 Vnobreak_char_display = Qt;
29486 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29487 doc: /* The pointer shape to show in void text areas.
29488 A value of nil means to show the text pointer. Other options are `arrow',
29489 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29490 Vvoid_text_area_pointer = Qarrow;
29492 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29493 doc: /* Non-nil means don't actually do any redisplay.
29494 This is used for internal purposes. */);
29495 Vinhibit_redisplay = Qnil;
29497 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29498 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29499 Vglobal_mode_string = Qnil;
29501 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29502 doc: /* Marker for where to display an arrow on top of the buffer text.
29503 This must be the beginning of a line in order to work.
29504 See also `overlay-arrow-string'. */);
29505 Voverlay_arrow_position = Qnil;
29507 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29508 doc: /* String to display as an arrow in non-window frames.
29509 See also `overlay-arrow-position'. */);
29510 Voverlay_arrow_string = build_pure_c_string ("=>");
29512 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29513 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29514 The symbols on this list are examined during redisplay to determine
29515 where to display overlay arrows. */);
29516 Voverlay_arrow_variable_list
29517 = list1 (intern_c_string ("overlay-arrow-position"));
29519 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29520 doc: /* The number of lines to try scrolling a window by when point moves out.
29521 If that fails to bring point back on frame, point is centered instead.
29522 If this is zero, point is always centered after it moves off frame.
29523 If you want scrolling to always be a line at a time, you should set
29524 `scroll-conservatively' to a large value rather than set this to 1. */);
29526 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29527 doc: /* Scroll up to this many lines, to bring point back on screen.
29528 If point moves off-screen, redisplay will scroll by up to
29529 `scroll-conservatively' lines in order to bring point just barely
29530 onto the screen again. If that cannot be done, then redisplay
29531 recenters point as usual.
29533 If the value is greater than 100, redisplay will never recenter point,
29534 but will always scroll just enough text to bring point into view, even
29535 if you move far away.
29537 A value of zero means always recenter point if it moves off screen. */);
29538 scroll_conservatively = 0;
29540 DEFVAR_INT ("scroll-margin", scroll_margin,
29541 doc: /* Number of lines of margin at the top and bottom of a window.
29542 Recenter the window whenever point gets within this many lines
29543 of the top or bottom of the window. */);
29544 scroll_margin = 0;
29546 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29547 doc: /* Pixels per inch value for non-window system displays.
29548 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29549 Vdisplay_pixels_per_inch = make_float (72.0);
29551 #ifdef GLYPH_DEBUG
29552 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29553 #endif
29555 DEFVAR_LISP ("truncate-partial-width-windows",
29556 Vtruncate_partial_width_windows,
29557 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29558 For an integer value, truncate lines in each window narrower than the
29559 full frame width, provided the window width is less than that integer;
29560 otherwise, respect the value of `truncate-lines'.
29562 For any other non-nil value, truncate lines in all windows that do
29563 not span the full frame width.
29565 A value of nil means to respect the value of `truncate-lines'.
29567 If `word-wrap' is enabled, you might want to reduce this. */);
29568 Vtruncate_partial_width_windows = make_number (50);
29570 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29571 doc: /* Maximum buffer size for which line number should be displayed.
29572 If the buffer is bigger than this, the line number does not appear
29573 in the mode line. A value of nil means no limit. */);
29574 Vline_number_display_limit = Qnil;
29576 DEFVAR_INT ("line-number-display-limit-width",
29577 line_number_display_limit_width,
29578 doc: /* Maximum line width (in characters) for line number display.
29579 If the average length of the lines near point is bigger than this, then the
29580 line number may be omitted from the mode line. */);
29581 line_number_display_limit_width = 200;
29583 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29584 doc: /* Non-nil means highlight region even in nonselected windows. */);
29585 highlight_nonselected_windows = 0;
29587 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29588 doc: /* Non-nil if more than one frame is visible on this display.
29589 Minibuffer-only frames don't count, but iconified frames do.
29590 This variable is not guaranteed to be accurate except while processing
29591 `frame-title-format' and `icon-title-format'. */);
29593 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29594 doc: /* Template for displaying the title bar of visible frames.
29595 \(Assuming the window manager supports this feature.)
29597 This variable has the same structure as `mode-line-format', except that
29598 the %c and %l constructs are ignored. It is used only on frames for
29599 which no explicit name has been set \(see `modify-frame-parameters'). */);
29601 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29602 doc: /* Template for displaying the title bar of an iconified frame.
29603 \(Assuming the window manager supports this feature.)
29604 This variable has the same structure as `mode-line-format' (which see),
29605 and is used only on frames for which no explicit name has been set
29606 \(see `modify-frame-parameters'). */);
29607 Vicon_title_format
29608 = Vframe_title_format
29609 = listn (CONSTYPE_PURE, 3,
29610 intern_c_string ("multiple-frames"),
29611 build_pure_c_string ("%b"),
29612 listn (CONSTYPE_PURE, 4,
29613 empty_unibyte_string,
29614 intern_c_string ("invocation-name"),
29615 build_pure_c_string ("@"),
29616 intern_c_string ("system-name")));
29618 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29619 doc: /* Maximum number of lines to keep in the message log buffer.
29620 If nil, disable message logging. If t, log messages but don't truncate
29621 the buffer when it becomes large. */);
29622 Vmessage_log_max = make_number (1000);
29624 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29625 doc: /* Functions called before redisplay, if window sizes have changed.
29626 The value should be a list of functions that take one argument.
29627 Just before redisplay, for each frame, if any of its windows have changed
29628 size since the last redisplay, or have been split or deleted,
29629 all the functions in the list are called, with the frame as argument. */);
29630 Vwindow_size_change_functions = Qnil;
29632 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29633 doc: /* List of functions to call before redisplaying a window with scrolling.
29634 Each function is called with two arguments, the window and its new
29635 display-start position. Note that these functions are also called by
29636 `set-window-buffer'. Also note that the value of `window-end' is not
29637 valid when these functions are called.
29639 Warning: Do not use this feature to alter the way the window
29640 is scrolled. It is not designed for that, and such use probably won't
29641 work. */);
29642 Vwindow_scroll_functions = Qnil;
29644 DEFVAR_LISP ("window-text-change-functions",
29645 Vwindow_text_change_functions,
29646 doc: /* Functions to call in redisplay when text in the window might change. */);
29647 Vwindow_text_change_functions = Qnil;
29649 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29650 doc: /* Functions called when redisplay of a window reaches the end trigger.
29651 Each function is called with two arguments, the window and the end trigger value.
29652 See `set-window-redisplay-end-trigger'. */);
29653 Vredisplay_end_trigger_functions = Qnil;
29655 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29656 doc: /* Non-nil means autoselect window with mouse pointer.
29657 If nil, do not autoselect windows.
29658 A positive number means delay autoselection by that many seconds: a
29659 window is autoselected only after the mouse has remained in that
29660 window for the duration of the delay.
29661 A negative number has a similar effect, but causes windows to be
29662 autoselected only after the mouse has stopped moving. \(Because of
29663 the way Emacs compares mouse events, you will occasionally wait twice
29664 that time before the window gets selected.\)
29665 Any other value means to autoselect window instantaneously when the
29666 mouse pointer enters it.
29668 Autoselection selects the minibuffer only if it is active, and never
29669 unselects the minibuffer if it is active.
29671 When customizing this variable make sure that the actual value of
29672 `focus-follows-mouse' matches the behavior of your window manager. */);
29673 Vmouse_autoselect_window = Qnil;
29675 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29676 doc: /* Non-nil means automatically resize tool-bars.
29677 This dynamically changes the tool-bar's height to the minimum height
29678 that is needed to make all tool-bar items visible.
29679 If value is `grow-only', the tool-bar's height is only increased
29680 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29681 Vauto_resize_tool_bars = Qt;
29683 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29684 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29685 auto_raise_tool_bar_buttons_p = 1;
29687 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29688 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29689 make_cursor_line_fully_visible_p = 1;
29691 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29692 doc: /* Border below tool-bar in pixels.
29693 If an integer, use it as the height of the border.
29694 If it is one of `internal-border-width' or `border-width', use the
29695 value of the corresponding frame parameter.
29696 Otherwise, no border is added below the tool-bar. */);
29697 Vtool_bar_border = Qinternal_border_width;
29699 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29700 doc: /* Margin around tool-bar buttons in pixels.
29701 If an integer, use that for both horizontal and vertical margins.
29702 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29703 HORZ specifying the horizontal margin, and VERT specifying the
29704 vertical margin. */);
29705 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29707 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29708 doc: /* Relief thickness of tool-bar buttons. */);
29709 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29711 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29712 doc: /* Tool bar style to use.
29713 It can be one of
29714 image - show images only
29715 text - show text only
29716 both - show both, text below image
29717 both-horiz - show text to the right of the image
29718 text-image-horiz - show text to the left of the image
29719 any other - use system default or image if no system default.
29721 This variable only affects the GTK+ toolkit version of Emacs. */);
29722 Vtool_bar_style = Qnil;
29724 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29725 doc: /* Maximum number of characters a label can have to be shown.
29726 The tool bar style must also show labels for this to have any effect, see
29727 `tool-bar-style'. */);
29728 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29730 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29731 doc: /* List of functions to call to fontify regions of text.
29732 Each function is called with one argument POS. Functions must
29733 fontify a region starting at POS in the current buffer, and give
29734 fontified regions the property `fontified'. */);
29735 Vfontification_functions = Qnil;
29736 Fmake_variable_buffer_local (Qfontification_functions);
29738 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29739 unibyte_display_via_language_environment,
29740 doc: /* Non-nil means display unibyte text according to language environment.
29741 Specifically, this means that raw bytes in the range 160-255 decimal
29742 are displayed by converting them to the equivalent multibyte characters
29743 according to the current language environment. As a result, they are
29744 displayed according to the current fontset.
29746 Note that this variable affects only how these bytes are displayed,
29747 but does not change the fact they are interpreted as raw bytes. */);
29748 unibyte_display_via_language_environment = 0;
29750 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29751 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29752 If a float, it specifies a fraction of the mini-window frame's height.
29753 If an integer, it specifies a number of lines. */);
29754 Vmax_mini_window_height = make_float (0.25);
29756 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29757 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29758 A value of nil means don't automatically resize mini-windows.
29759 A value of t means resize them to fit the text displayed in them.
29760 A value of `grow-only', the default, means let mini-windows grow only;
29761 they return to their normal size when the minibuffer is closed, or the
29762 echo area becomes empty. */);
29763 Vresize_mini_windows = Qgrow_only;
29765 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29766 doc: /* Alist specifying how to blink the cursor off.
29767 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29768 `cursor-type' frame-parameter or variable equals ON-STATE,
29769 comparing using `equal', Emacs uses OFF-STATE to specify
29770 how to blink it off. ON-STATE and OFF-STATE are values for
29771 the `cursor-type' frame parameter.
29773 If a frame's ON-STATE has no entry in this list,
29774 the frame's other specifications determine how to blink the cursor off. */);
29775 Vblink_cursor_alist = Qnil;
29777 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29778 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29779 If non-nil, windows are automatically scrolled horizontally to make
29780 point visible. */);
29781 automatic_hscrolling_p = 1;
29782 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29784 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29785 doc: /* How many columns away from the window edge point is allowed to get
29786 before automatic hscrolling will horizontally scroll the window. */);
29787 hscroll_margin = 5;
29789 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29790 doc: /* How many columns to scroll the window when point gets too close to the edge.
29791 When point is less than `hscroll-margin' columns from the window
29792 edge, automatic hscrolling will scroll the window by the amount of columns
29793 determined by this variable. If its value is a positive integer, scroll that
29794 many columns. If it's a positive floating-point number, it specifies the
29795 fraction of the window's width to scroll. If it's nil or zero, point will be
29796 centered horizontally after the scroll. Any other value, including negative
29797 numbers, are treated as if the value were zero.
29799 Automatic hscrolling always moves point outside the scroll margin, so if
29800 point was more than scroll step columns inside the margin, the window will
29801 scroll more than the value given by the scroll step.
29803 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29804 and `scroll-right' overrides this variable's effect. */);
29805 Vhscroll_step = make_number (0);
29807 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29808 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29809 Bind this around calls to `message' to let it take effect. */);
29810 message_truncate_lines = 0;
29812 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29813 doc: /* Normal hook run to update the menu bar definitions.
29814 Redisplay runs this hook before it redisplays the menu bar.
29815 This is used to update submenus such as Buffers,
29816 whose contents depend on various data. */);
29817 Vmenu_bar_update_hook = Qnil;
29819 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29820 doc: /* Frame for which we are updating a menu.
29821 The enable predicate for a menu binding should check this variable. */);
29822 Vmenu_updating_frame = Qnil;
29824 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29825 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29826 inhibit_menubar_update = 0;
29828 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29829 doc: /* Prefix prepended to all continuation lines at display time.
29830 The value may be a string, an image, or a stretch-glyph; it is
29831 interpreted in the same way as the value of a `display' text property.
29833 This variable is overridden by any `wrap-prefix' text or overlay
29834 property.
29836 To add a prefix to non-continuation lines, use `line-prefix'. */);
29837 Vwrap_prefix = Qnil;
29838 DEFSYM (Qwrap_prefix, "wrap-prefix");
29839 Fmake_variable_buffer_local (Qwrap_prefix);
29841 DEFVAR_LISP ("line-prefix", Vline_prefix,
29842 doc: /* Prefix prepended to all non-continuation lines at display time.
29843 The value may be a string, an image, or a stretch-glyph; it is
29844 interpreted in the same way as the value of a `display' text property.
29846 This variable is overridden by any `line-prefix' text or overlay
29847 property.
29849 To add a prefix to continuation lines, use `wrap-prefix'. */);
29850 Vline_prefix = Qnil;
29851 DEFSYM (Qline_prefix, "line-prefix");
29852 Fmake_variable_buffer_local (Qline_prefix);
29854 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29855 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29856 inhibit_eval_during_redisplay = 0;
29858 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29859 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29860 inhibit_free_realized_faces = 0;
29862 #ifdef GLYPH_DEBUG
29863 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29864 doc: /* Inhibit try_window_id display optimization. */);
29865 inhibit_try_window_id = 0;
29867 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29868 doc: /* Inhibit try_window_reusing display optimization. */);
29869 inhibit_try_window_reusing = 0;
29871 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29872 doc: /* Inhibit try_cursor_movement display optimization. */);
29873 inhibit_try_cursor_movement = 0;
29874 #endif /* GLYPH_DEBUG */
29876 DEFVAR_INT ("overline-margin", overline_margin,
29877 doc: /* Space between overline and text, in pixels.
29878 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29879 margin to the character height. */);
29880 overline_margin = 2;
29882 DEFVAR_INT ("underline-minimum-offset",
29883 underline_minimum_offset,
29884 doc: /* Minimum distance between baseline and underline.
29885 This can improve legibility of underlined text at small font sizes,
29886 particularly when using variable `x-use-underline-position-properties'
29887 with fonts that specify an UNDERLINE_POSITION relatively close to the
29888 baseline. The default value is 1. */);
29889 underline_minimum_offset = 1;
29891 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29892 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29893 This feature only works when on a window system that can change
29894 cursor shapes. */);
29895 display_hourglass_p = 1;
29897 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29898 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29899 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29901 #ifdef HAVE_WINDOW_SYSTEM
29902 hourglass_atimer = NULL;
29903 hourglass_shown_p = 0;
29904 #endif /* HAVE_WINDOW_SYSTEM */
29906 DEFSYM (Qglyphless_char, "glyphless-char");
29907 DEFSYM (Qhex_code, "hex-code");
29908 DEFSYM (Qempty_box, "empty-box");
29909 DEFSYM (Qthin_space, "thin-space");
29910 DEFSYM (Qzero_width, "zero-width");
29912 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29913 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29915 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29916 doc: /* Char-table defining glyphless characters.
29917 Each element, if non-nil, should be one of the following:
29918 an ASCII acronym string: display this string in a box
29919 `hex-code': display the hexadecimal code of a character in a box
29920 `empty-box': display as an empty box
29921 `thin-space': display as 1-pixel width space
29922 `zero-width': don't display
29923 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29924 display method for graphical terminals and text terminals respectively.
29925 GRAPHICAL and TEXT should each have one of the values listed above.
29927 The char-table has one extra slot to control the display of a character for
29928 which no font is found. This slot only takes effect on graphical terminals.
29929 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29930 `thin-space'. The default is `empty-box'. */);
29931 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29932 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29933 Qempty_box);
29935 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29936 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29937 Vdebug_on_message = Qnil;
29941 /* Initialize this module when Emacs starts. */
29943 void
29944 init_xdisp (void)
29946 CHARPOS (this_line_start_pos) = 0;
29948 if (!noninteractive)
29950 struct window *m = XWINDOW (minibuf_window);
29951 Lisp_Object frame = m->frame;
29952 struct frame *f = XFRAME (frame);
29953 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29954 struct window *r = XWINDOW (root);
29955 int i;
29957 echo_area_window = minibuf_window;
29959 r->top_line = FRAME_TOP_MARGIN (f);
29960 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29961 r->total_cols = FRAME_COLS (f);
29963 m->top_line = FRAME_LINES (f) - 1;
29964 m->total_lines = 1;
29965 m->total_cols = FRAME_COLS (f);
29967 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29968 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29969 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29971 /* The default ellipsis glyphs `...'. */
29972 for (i = 0; i < 3; ++i)
29973 default_invis_vector[i] = make_number ('.');
29977 /* Allocate the buffer for frame titles.
29978 Also used for `format-mode-line'. */
29979 int size = 100;
29980 mode_line_noprop_buf = xmalloc (size);
29981 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29982 mode_line_noprop_ptr = mode_line_noprop_buf;
29983 mode_line_target = MODE_LINE_DISPLAY;
29986 help_echo_showing_p = 0;
29989 #ifdef HAVE_WINDOW_SYSTEM
29991 /* Platform-independent portion of hourglass implementation. */
29993 /* Cancel a currently active hourglass timer, and start a new one. */
29994 void
29995 start_hourglass (void)
29997 struct timespec delay;
29999 cancel_hourglass ();
30001 if (INTEGERP (Vhourglass_delay)
30002 && XINT (Vhourglass_delay) > 0)
30003 delay = make_timespec (min (XINT (Vhourglass_delay),
30004 TYPE_MAXIMUM (time_t)),
30006 else if (FLOATP (Vhourglass_delay)
30007 && XFLOAT_DATA (Vhourglass_delay) > 0)
30008 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30009 else
30010 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30012 #ifdef HAVE_NTGUI
30014 extern void w32_note_current_window (void);
30015 w32_note_current_window ();
30017 #endif /* HAVE_NTGUI */
30019 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30020 show_hourglass, NULL);
30024 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30025 shown. */
30026 void
30027 cancel_hourglass (void)
30029 if (hourglass_atimer)
30031 cancel_atimer (hourglass_atimer);
30032 hourglass_atimer = NULL;
30035 if (hourglass_shown_p)
30036 hide_hourglass ();
30039 #endif /* HAVE_WINDOW_SYSTEM */