Auto-commit of generated files.
[emacs.git] / src / xdisp.c
blob6762bf85eb459f47c1856c89df82f8858e2897ef
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
315 #include "font.h"
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
321 #define INFINITY 10000000
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
346 static Lisp_Object Qfontification_functions;
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
392 /* Name of the face used to highlight trailing whitespace. */
394 static Lisp_Object Qtrailing_whitespace;
396 /* Name and number of the face used to highlight escape glyphs. */
398 static Lisp_Object Qescape_glyph;
400 /* Name and number of the face used to highlight non-breaking spaces. */
402 static Lisp_Object Qnobreak_space;
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
407 Lisp_Object Qimage;
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
420 int noninteractive_need_newline;
422 /* Non-zero means print newline to message log before next message. */
424 static int message_log_need_newline;
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
438 static struct text_pos this_line_start_pos;
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
443 static struct text_pos this_line_end_pos;
445 /* The vertical positions and the height of this line. */
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
454 static int this_line_start_x;
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
460 static struct text_pos this_line_min_pos;
462 /* Buffer that this_line_.* variables are referring to. */
464 static struct buffer *this_line_buffer;
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
479 Lisp_Object Qmenu_bar_update_hook;
481 /* Nonzero if an overlay arrow has been displayed in this window. */
483 static int overlay_arrow_seen;
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
489 int buffer_shared;
491 /* Vector containing glyphs for an ellipsis `...'. */
493 static Lisp_Object default_invis_vector[3];
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
499 Lisp_Object echo_area_window;
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
506 static Lisp_Object Vmessage_stack;
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
511 static int message_enable_multibyte;
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
515 int update_mode_lines;
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
520 int windows_or_buffers_changed;
522 /* Nonzero means a frame's cursor type has been changed. */
524 int cursor_type_changed;
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
529 static int line_number_displayed;
531 /* The name of the *Messages* buffer, a string. */
533 static Lisp_Object Vmessages_buffer_name;
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
538 Lisp_Object echo_area_buffer[2];
540 /* The buffers referenced from echo_area_buffer. */
542 static Lisp_Object echo_buffer[2];
544 /* A vector saved used in with_area_buffer to reduce consing. */
546 static Lisp_Object Vwith_echo_area_save_vector;
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
551 static int display_last_displayed_message_p;
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
556 static int message_buf_print;
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
566 static int message_cleared_p;
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
575 /* Ascent and height of the last line processed by move_it_to. */
577 static int last_max_ascent, last_height;
579 /* Non-zero if there's a help-echo in the echo area. */
581 int help_echo_showing_p;
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
587 int current_mode_line_height, current_header_line_height;
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
595 #define TEXT_PROP_DISTANCE_LIMIT 100
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache (); \
610 } while (0)
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
620 #if GLYPH_DEBUG
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
625 int trace_redisplay_p;
627 #endif /* GLYPH_DEBUG */
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
638 static Lisp_Object Qauto_hscroll_mode;
640 /* Buffer being redisplayed -- for redisplay_window_error. */
642 static struct buffer *displayed_buffer;
644 /* Value returned from text property handlers (see below). */
646 enum prop_handled
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
654 /* A description of text properties that redisplay is interested
655 in. */
657 struct props
659 /* The name of the property. */
660 Lisp_Object *name;
662 /* A unique index for the property. */
663 enum prop_idx idx;
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
677 /* Properties handled by iterators. */
679 static struct props it_props[] =
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696 /* Enumeration returned by some move_it_.* functions internally. */
698 enum move_it_result
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
729 /* Similarly for the image cache. */
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
739 /* Non-zero while redisplay_internal is in progress. */
741 int redisplaying_p;
743 static Lisp_Object Qinhibit_free_realized_faces;
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 EMACS_INT help_echo_pos;
753 /* Temporary variable for XTread_socket. */
755 Lisp_Object previous_help_echo_string;
757 /* Platform-independent portion of hourglass implementation. */
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
783 /* Function prototypes. */
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
796 static void handle_line_prefix (struct it *);
798 static void pint2str (char *, int, EMACS_INT);
799 static void pint2hrstr (char *, int, EMACS_INT);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 EMACS_INT, EMACS_INT);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, EMACS_INT);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
815 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void pop_message (void);
819 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
821 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (EMACS_INT);
840 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
841 static void push_it (struct it *, struct text_pos *);
842 static void iterate_out_of_display_property (struct it *);
843 static void pop_it (struct it *);
844 static void sync_frame_with_window_matrix_rows (struct window *);
845 static void select_frame_for_redisplay (Lisp_Object);
846 static void redisplay_internal (void);
847 static int echo_area_display (int);
848 static void redisplay_windows (Lisp_Object);
849 static void redisplay_window (Lisp_Object, int);
850 static Lisp_Object redisplay_window_error (Lisp_Object);
851 static Lisp_Object redisplay_window_0 (Lisp_Object);
852 static Lisp_Object redisplay_window_1 (Lisp_Object);
853 static int set_cursor_from_row (struct window *, struct glyph_row *,
854 struct glyph_matrix *, EMACS_INT, EMACS_INT,
855 int, int);
856 static int update_menu_bar (struct frame *, int, int);
857 static int try_window_reusing_current_matrix (struct window *);
858 static int try_window_id (struct window *);
859 static int display_line (struct it *);
860 static int display_mode_lines (struct window *);
861 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
862 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
863 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
864 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
865 static void display_menu_bar (struct window *);
866 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
867 EMACS_INT *);
868 static int display_string (const char *, Lisp_Object, Lisp_Object,
869 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
870 static void compute_line_metrics (struct it *);
871 static void run_redisplay_end_trigger_hook (struct it *);
872 static int get_overlay_strings (struct it *, EMACS_INT);
873 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
874 static void next_overlay_string (struct it *);
875 static void reseat (struct it *, struct text_pos, int);
876 static void reseat_1 (struct it *, struct text_pos, int);
877 static void back_to_previous_visible_line_start (struct it *);
878 void reseat_at_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, EMACS_INT);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, EMACS_INT, int,
896 enum move_operation_enum);
897 void move_it_vertically_backward (struct it *, int);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, EMACS_INT);
906 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
907 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
908 static EMACS_INT number_of_chars (const char *, int);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static EMACS_INT next_overlay_change (EMACS_INT);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, EMACS_INT, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, EMACS_INT, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
926 #ifdef HAVE_WINDOW_SYSTEM
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
941 #endif /* HAVE_WINDOW_SYSTEM */
943 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
944 static int coords_in_mouse_face_p (struct window *, int, int);
948 /***********************************************************************
949 Window display dimensions
950 ***********************************************************************/
952 /* Return the bottom boundary y-position for text lines in window W.
953 This is the first y position at which a line cannot start.
954 It is relative to the top of the window.
956 This is the height of W minus the height of a mode line, if any. */
959 window_text_bottom_y (struct window *w)
961 int height = WINDOW_TOTAL_HEIGHT (w);
963 if (WINDOW_WANTS_MODELINE_P (w))
964 height -= CURRENT_MODE_LINE_HEIGHT (w);
965 return height;
968 /* Return the pixel width of display area AREA of window W. AREA < 0
969 means return the total width of W, not including fringes to
970 the left and right of the window. */
973 window_box_width (struct window *w, int area)
975 int cols = XFASTINT (w->total_cols);
976 int pixels = 0;
978 if (!w->pseudo_window_p)
980 cols -= WINDOW_SCROLL_BAR_COLS (w);
982 if (area == TEXT_AREA)
984 if (INTEGERP (w->left_margin_cols))
985 cols -= XFASTINT (w->left_margin_cols);
986 if (INTEGERP (w->right_margin_cols))
987 cols -= XFASTINT (w->right_margin_cols);
988 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
990 else if (area == LEFT_MARGIN_AREA)
992 cols = (INTEGERP (w->left_margin_cols)
993 ? XFASTINT (w->left_margin_cols) : 0);
994 pixels = 0;
996 else if (area == RIGHT_MARGIN_AREA)
998 cols = (INTEGERP (w->right_margin_cols)
999 ? XFASTINT (w->right_margin_cols) : 0);
1000 pixels = 0;
1004 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1008 /* Return the pixel height of the display area of window W, not
1009 including mode lines of W, if any. */
1012 window_box_height (struct window *w)
1014 struct frame *f = XFRAME (w->frame);
1015 int height = WINDOW_TOTAL_HEIGHT (w);
1017 xassert (height >= 0);
1019 /* Note: the code below that determines the mode-line/header-line
1020 height is essentially the same as that contained in the macro
1021 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1022 the appropriate glyph row has its `mode_line_p' flag set,
1023 and if it doesn't, uses estimate_mode_line_height instead. */
1025 if (WINDOW_WANTS_MODELINE_P (w))
1027 struct glyph_row *ml_row
1028 = (w->current_matrix && w->current_matrix->rows
1029 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1030 : 0);
1031 if (ml_row && ml_row->mode_line_p)
1032 height -= ml_row->height;
1033 else
1034 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1037 if (WINDOW_WANTS_HEADER_LINE_P (w))
1039 struct glyph_row *hl_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (hl_row && hl_row->mode_line_p)
1044 height -= hl_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1049 /* With a very small font and a mode-line that's taller than
1050 default, we might end up with a negative height. */
1051 return max (0, height);
1054 /* Return the window-relative coordinate of the left edge of display
1055 area AREA of window W. AREA < 0 means return the left edge of the
1056 whole window, to the right of the left fringe of W. */
1059 window_box_left_offset (struct window *w, int area)
1061 int x;
1063 if (w->pseudo_window_p)
1064 return 0;
1066 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1068 if (area == TEXT_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA));
1071 else if (area == RIGHT_MARGIN_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA)
1074 + window_box_width (w, TEXT_AREA)
1075 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1077 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1078 else if (area == LEFT_MARGIN_AREA
1079 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1080 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1082 return x;
1086 /* Return the window-relative coordinate of the right edge of display
1087 area AREA of window W. AREA < 0 means return the right edge of the
1088 whole window, to the left of the right fringe of W. */
1091 window_box_right_offset (struct window *w, int area)
1093 return window_box_left_offset (w, area) + window_box_width (w, area);
1096 /* Return the frame-relative coordinate of the left edge of display
1097 area AREA of window W. AREA < 0 means return the left edge of the
1098 whole window, to the right of the left fringe of W. */
1101 window_box_left (struct window *w, int area)
1103 struct frame *f = XFRAME (w->frame);
1104 int x;
1106 if (w->pseudo_window_p)
1107 return FRAME_INTERNAL_BORDER_WIDTH (f);
1109 x = (WINDOW_LEFT_EDGE_X (w)
1110 + window_box_left_offset (w, area));
1112 return x;
1116 /* Return the frame-relative coordinate of the right edge of display
1117 area AREA of window W. AREA < 0 means return the right edge of the
1118 whole window, to the left of the right fringe of W. */
1121 window_box_right (struct window *w, int area)
1123 return window_box_left (w, area) + window_box_width (w, area);
1126 /* Get the bounding box of the display area AREA of window W, without
1127 mode lines, in frame-relative coordinates. AREA < 0 means the
1128 whole window, not including the left and right fringes of
1129 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1130 coordinates of the upper-left corner of the box. Return in
1131 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1133 void
1134 window_box (struct window *w, int area, int *box_x, int *box_y,
1135 int *box_width, int *box_height)
1137 if (box_width)
1138 *box_width = window_box_width (w, area);
1139 if (box_height)
1140 *box_height = window_box_height (w);
1141 if (box_x)
1142 *box_x = window_box_left (w, area);
1143 if (box_y)
1145 *box_y = WINDOW_TOP_EDGE_Y (w);
1146 if (WINDOW_WANTS_HEADER_LINE_P (w))
1147 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1152 /* Get the bounding box of the display area AREA of window W, without
1153 mode lines. AREA < 0 means the whole window, not including the
1154 left and right fringe of the window. Return in *TOP_LEFT_X
1155 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1156 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1157 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1158 box. */
1160 static inline void
1161 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1162 int *bottom_right_x, int *bottom_right_y)
1164 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1165 bottom_right_y);
1166 *bottom_right_x += *top_left_x;
1167 *bottom_right_y += *top_left_y;
1172 /***********************************************************************
1173 Utilities
1174 ***********************************************************************/
1176 /* Return the bottom y-position of the line the iterator IT is in.
1177 This can modify IT's settings. */
1180 line_bottom_y (struct it *it)
1182 int line_height = it->max_ascent + it->max_descent;
1183 int line_top_y = it->current_y;
1185 if (line_height == 0)
1187 if (last_height)
1188 line_height = last_height;
1189 else if (IT_CHARPOS (*it) < ZV)
1191 move_it_by_lines (it, 1);
1192 line_height = (it->max_ascent || it->max_descent
1193 ? it->max_ascent + it->max_descent
1194 : last_height);
1196 else
1198 struct glyph_row *row = it->glyph_row;
1200 /* Use the default character height. */
1201 it->glyph_row = NULL;
1202 it->what = IT_CHARACTER;
1203 it->c = ' ';
1204 it->len = 1;
1205 PRODUCE_GLYPHS (it);
1206 line_height = it->ascent + it->descent;
1207 it->glyph_row = row;
1211 return line_top_y + line_height;
1214 /* Subroutine of pos_visible_p below. Extracts a display string, if
1215 any, from the display spec given as its argument. */
1216 static Lisp_Object
1217 string_from_display_spec (Lisp_Object spec)
1219 if (CONSP (spec))
1221 while (CONSP (spec))
1223 if (STRINGP (XCAR (spec)))
1224 return XCAR (spec);
1225 spec = XCDR (spec);
1228 else if (VECTORP (spec))
1230 ptrdiff_t i;
1232 for (i = 0; i < ASIZE (spec); i++)
1234 if (STRINGP (AREF (spec, i)))
1235 return AREF (spec, i);
1237 return Qnil;
1240 return spec;
1243 /* Return 1 if position CHARPOS is visible in window W.
1244 CHARPOS < 0 means return info about WINDOW_END position.
1245 If visible, set *X and *Y to pixel coordinates of top left corner.
1246 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1247 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1250 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1251 int *rtop, int *rbot, int *rowh, int *vpos)
1253 struct it it;
1254 void *itdata = bidi_shelve_cache ();
1255 struct text_pos top;
1256 int visible_p = 0;
1257 struct buffer *old_buffer = NULL;
1259 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1260 return visible_p;
1262 if (XBUFFER (w->buffer) != current_buffer)
1264 old_buffer = current_buffer;
1265 set_buffer_internal_1 (XBUFFER (w->buffer));
1268 SET_TEXT_POS_FROM_MARKER (top, w->start);
1269 /* Scrolling a minibuffer window via scroll bar when the echo area
1270 shows long text sometimes resets the minibuffer contents behind
1271 our backs. */
1272 if (CHARPOS (top) > ZV)
1273 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1275 /* Compute exact mode line heights. */
1276 if (WINDOW_WANTS_MODELINE_P (w))
1277 current_mode_line_height
1278 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1279 BVAR (current_buffer, mode_line_format));
1281 if (WINDOW_WANTS_HEADER_LINE_P (w))
1282 current_header_line_height
1283 = display_mode_line (w, HEADER_LINE_FACE_ID,
1284 BVAR (current_buffer, header_line_format));
1286 start_display (&it, w, top);
1287 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1288 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1290 if (charpos >= 0
1291 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1292 && IT_CHARPOS (it) >= charpos)
1293 /* When scanning backwards under bidi iteration, move_it_to
1294 stops at or _before_ CHARPOS, because it stops at or to
1295 the _right_ of the character at CHARPOS. */
1296 || (it.bidi_p && it.bidi_it.scan_dir == -1
1297 && IT_CHARPOS (it) <= charpos)))
1299 /* We have reached CHARPOS, or passed it. How the call to
1300 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1301 or covered by a display property, move_it_to stops at the end
1302 of the invisible text, to the right of CHARPOS. (ii) If
1303 CHARPOS is in a display vector, move_it_to stops on its last
1304 glyph. */
1305 int top_x = it.current_x;
1306 int top_y = it.current_y;
1307 /* Calling line_bottom_y may change it.method, it.position, etc. */
1308 enum it_method it_method = it.method;
1309 int bottom_y = (last_height = 0, line_bottom_y (&it));
1310 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1312 if (top_y < window_top_y)
1313 visible_p = bottom_y > window_top_y;
1314 else if (top_y < it.last_visible_y)
1315 visible_p = 1;
1316 if (bottom_y >= it.last_visible_y
1317 && it.bidi_p && it.bidi_it.scan_dir == -1
1318 && IT_CHARPOS (it) < charpos)
1320 /* When the last line of the window is scanned backwards
1321 under bidi iteration, we could be duped into thinking
1322 that we have passed CHARPOS, when in fact move_it_to
1323 simply stopped short of CHARPOS because it reached
1324 last_visible_y. To see if that's what happened, we call
1325 move_it_to again with a slightly larger vertical limit,
1326 and see if it actually moved vertically; if it did, we
1327 didn't really reach CHARPOS, which is beyond window end. */
1328 struct it save_it = it;
1329 /* Why 10? because we don't know how many canonical lines
1330 will the height of the next line(s) be. So we guess. */
1331 int ten_more_lines =
1332 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1334 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1335 MOVE_TO_POS | MOVE_TO_Y);
1336 if (it.current_y > top_y)
1337 visible_p = 0;
1339 it = save_it;
1341 if (visible_p)
1343 if (it_method == GET_FROM_DISPLAY_VECTOR)
1345 /* We stopped on the last glyph of a display vector.
1346 Try and recompute. Hack alert! */
1347 if (charpos < 2 || top.charpos >= charpos)
1348 top_x = it.glyph_row->x;
1349 else
1351 struct it it2;
1352 start_display (&it2, w, top);
1353 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1354 get_next_display_element (&it2);
1355 PRODUCE_GLYPHS (&it2);
1356 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1357 || it2.current_x > it2.last_visible_x)
1358 top_x = it.glyph_row->x;
1359 else
1361 top_x = it2.current_x;
1362 top_y = it2.current_y;
1366 else if (IT_CHARPOS (it) != charpos)
1368 Lisp_Object cpos = make_number (charpos);
1369 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1370 Lisp_Object string = string_from_display_spec (spec);
1371 int newline_in_string = 0;
1373 if (STRINGP (string))
1375 const char *s = SSDATA (string);
1376 const char *e = s + SBYTES (string);
1377 while (s < e)
1379 if (*s++ == '\n')
1381 newline_in_string = 1;
1382 break;
1386 /* The tricky code below is needed because there's a
1387 discrepancy between move_it_to and how we set cursor
1388 when the display line ends in a newline from a
1389 display string. move_it_to will stop _after_ such
1390 display strings, whereas set_cursor_from_row
1391 conspires with cursor_row_p to place the cursor on
1392 the first glyph produced from the display string. */
1394 /* We have overshoot PT because it is covered by a
1395 display property whose value is a string. If the
1396 string includes embedded newlines, we are also in the
1397 wrong display line. Backtrack to the correct line,
1398 where the display string begins. */
1399 if (newline_in_string)
1401 Lisp_Object startpos, endpos;
1402 EMACS_INT start, end;
1403 struct it it3;
1404 int it3_moved;
1406 /* Find the first and the last buffer positions
1407 covered by the display string. */
1408 endpos =
1409 Fnext_single_char_property_change (cpos, Qdisplay,
1410 Qnil, Qnil);
1411 startpos =
1412 Fprevious_single_char_property_change (endpos, Qdisplay,
1413 Qnil, Qnil);
1414 start = XFASTINT (startpos);
1415 end = XFASTINT (endpos);
1416 /* Move to the last buffer position before the
1417 display property. */
1418 start_display (&it3, w, top);
1419 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1420 /* Move forward one more line if the position before
1421 the display string is a newline or if it is the
1422 rightmost character on a line that is
1423 continued or word-wrapped. */
1424 if (it3.method == GET_FROM_BUFFER
1425 && it3.c == '\n')
1426 move_it_by_lines (&it3, 1);
1427 else if (move_it_in_display_line_to (&it3, -1,
1428 it3.current_x
1429 + it3.pixel_width,
1430 MOVE_TO_X)
1431 == MOVE_LINE_CONTINUED)
1433 move_it_by_lines (&it3, 1);
1434 /* When we are under word-wrap, the #$@%!
1435 move_it_by_lines moves 2 lines, so we need to
1436 fix that up. */
1437 if (it3.line_wrap == WORD_WRAP)
1438 move_it_by_lines (&it3, -1);
1441 /* Record the vertical coordinate of the display
1442 line where we wound up. */
1443 top_y = it3.current_y;
1444 if (it3.bidi_p)
1446 /* When characters are reordered for display,
1447 the character displayed to the left of the
1448 display string could be _after_ the display
1449 property in the logical order. Use the
1450 smallest vertical position of these two. */
1451 start_display (&it3, w, top);
1452 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1453 if (it3.current_y < top_y)
1454 top_y = it3.current_y;
1456 /* Move from the top of the window to the beginning
1457 of the display line where the display string
1458 begins. */
1459 start_display (&it3, w, top);
1460 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1461 /* If it3_moved stays zero after the 'while' loop
1462 below, that means we already were at a newline
1463 before the loop (e.g., the display string begins
1464 with a newline), so we don't need to (and cannot)
1465 inspect the glyphs of it3.glyph_row, because
1466 PRODUCE_GLYPHS will not produce anything for a
1467 newline, and thus it3.glyph_row stays at its
1468 stale content it got at top of the window. */
1469 it3_moved = 0;
1470 /* Finally, advance the iterator until we hit the
1471 first display element whose character position is
1472 CHARPOS, or until the first newline from the
1473 display string, which signals the end of the
1474 display line. */
1475 while (get_next_display_element (&it3))
1477 PRODUCE_GLYPHS (&it3);
1478 if (IT_CHARPOS (it3) == charpos
1479 || ITERATOR_AT_END_OF_LINE_P (&it3))
1480 break;
1481 it3_moved = 1;
1482 set_iterator_to_next (&it3, 0);
1484 top_x = it3.current_x - it3.pixel_width;
1485 /* Normally, we would exit the above loop because we
1486 found the display element whose character
1487 position is CHARPOS. For the contingency that we
1488 didn't, and stopped at the first newline from the
1489 display string, move back over the glyphs
1490 produced from the string, until we find the
1491 rightmost glyph not from the string. */
1492 if (it3_moved
1493 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1495 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1496 + it3.glyph_row->used[TEXT_AREA];
1498 while (EQ ((g - 1)->object, string))
1500 --g;
1501 top_x -= g->pixel_width;
1503 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1504 + it3.glyph_row->used[TEXT_AREA]);
1509 *x = top_x;
1510 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1511 *rtop = max (0, window_top_y - top_y);
1512 *rbot = max (0, bottom_y - it.last_visible_y);
1513 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1514 - max (top_y, window_top_y)));
1515 *vpos = it.vpos;
1518 else
1520 /* We were asked to provide info about WINDOW_END. */
1521 struct it it2;
1522 void *it2data = NULL;
1524 SAVE_IT (it2, it, it2data);
1525 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1526 move_it_by_lines (&it, 1);
1527 if (charpos < IT_CHARPOS (it)
1528 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1530 visible_p = 1;
1531 RESTORE_IT (&it2, &it2, it2data);
1532 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1533 *x = it2.current_x;
1534 *y = it2.current_y + it2.max_ascent - it2.ascent;
1535 *rtop = max (0, -it2.current_y);
1536 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1537 - it.last_visible_y));
1538 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1539 it.last_visible_y)
1540 - max (it2.current_y,
1541 WINDOW_HEADER_LINE_HEIGHT (w))));
1542 *vpos = it2.vpos;
1544 else
1545 bidi_unshelve_cache (it2data, 1);
1547 bidi_unshelve_cache (itdata, 0);
1549 if (old_buffer)
1550 set_buffer_internal_1 (old_buffer);
1552 current_header_line_height = current_mode_line_height = -1;
1554 if (visible_p && XFASTINT (w->hscroll) > 0)
1555 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1557 #if 0
1558 /* Debugging code. */
1559 if (visible_p)
1560 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1561 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1562 else
1563 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1564 #endif
1566 return visible_p;
1570 /* Return the next character from STR. Return in *LEN the length of
1571 the character. This is like STRING_CHAR_AND_LENGTH but never
1572 returns an invalid character. If we find one, we return a `?', but
1573 with the length of the invalid character. */
1575 static inline int
1576 string_char_and_length (const unsigned char *str, int *len)
1578 int c;
1580 c = STRING_CHAR_AND_LENGTH (str, *len);
1581 if (!CHAR_VALID_P (c))
1582 /* We may not change the length here because other places in Emacs
1583 don't use this function, i.e. they silently accept invalid
1584 characters. */
1585 c = '?';
1587 return c;
1592 /* Given a position POS containing a valid character and byte position
1593 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1595 static struct text_pos
1596 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1598 xassert (STRINGP (string) && nchars >= 0);
1600 if (STRING_MULTIBYTE (string))
1602 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1603 int len;
1605 while (nchars--)
1607 string_char_and_length (p, &len);
1608 p += len;
1609 CHARPOS (pos) += 1;
1610 BYTEPOS (pos) += len;
1613 else
1614 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1616 return pos;
1620 /* Value is the text position, i.e. character and byte position,
1621 for character position CHARPOS in STRING. */
1623 static inline struct text_pos
1624 string_pos (EMACS_INT charpos, Lisp_Object string)
1626 struct text_pos pos;
1627 xassert (STRINGP (string));
1628 xassert (charpos >= 0);
1629 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1630 return pos;
1634 /* Value is a text position, i.e. character and byte position, for
1635 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1636 means recognize multibyte characters. */
1638 static struct text_pos
1639 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1641 struct text_pos pos;
1643 xassert (s != NULL);
1644 xassert (charpos >= 0);
1646 if (multibyte_p)
1648 int len;
1650 SET_TEXT_POS (pos, 0, 0);
1651 while (charpos--)
1653 string_char_and_length ((const unsigned char *) s, &len);
1654 s += len;
1655 CHARPOS (pos) += 1;
1656 BYTEPOS (pos) += len;
1659 else
1660 SET_TEXT_POS (pos, charpos, charpos);
1662 return pos;
1666 /* Value is the number of characters in C string S. MULTIBYTE_P
1667 non-zero means recognize multibyte characters. */
1669 static EMACS_INT
1670 number_of_chars (const char *s, int multibyte_p)
1672 EMACS_INT nchars;
1674 if (multibyte_p)
1676 EMACS_INT rest = strlen (s);
1677 int len;
1678 const unsigned char *p = (const unsigned char *) s;
1680 for (nchars = 0; rest > 0; ++nchars)
1682 string_char_and_length (p, &len);
1683 rest -= len, p += len;
1686 else
1687 nchars = strlen (s);
1689 return nchars;
1693 /* Compute byte position NEWPOS->bytepos corresponding to
1694 NEWPOS->charpos. POS is a known position in string STRING.
1695 NEWPOS->charpos must be >= POS.charpos. */
1697 static void
1698 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1700 xassert (STRINGP (string));
1701 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1703 if (STRING_MULTIBYTE (string))
1704 *newpos = string_pos_nchars_ahead (pos, string,
1705 CHARPOS (*newpos) - CHARPOS (pos));
1706 else
1707 BYTEPOS (*newpos) = CHARPOS (*newpos);
1710 /* EXPORT:
1711 Return an estimation of the pixel height of mode or header lines on
1712 frame F. FACE_ID specifies what line's height to estimate. */
1715 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1717 #ifdef HAVE_WINDOW_SYSTEM
1718 if (FRAME_WINDOW_P (f))
1720 int height = FONT_HEIGHT (FRAME_FONT (f));
1722 /* This function is called so early when Emacs starts that the face
1723 cache and mode line face are not yet initialized. */
1724 if (FRAME_FACE_CACHE (f))
1726 struct face *face = FACE_FROM_ID (f, face_id);
1727 if (face)
1729 if (face->font)
1730 height = FONT_HEIGHT (face->font);
1731 if (face->box_line_width > 0)
1732 height += 2 * face->box_line_width;
1736 return height;
1738 #endif
1740 return 1;
1743 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1744 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1745 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1746 not force the value into range. */
1748 void
1749 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1750 int *x, int *y, NativeRectangle *bounds, int noclip)
1753 #ifdef HAVE_WINDOW_SYSTEM
1754 if (FRAME_WINDOW_P (f))
1756 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1757 even for negative values. */
1758 if (pix_x < 0)
1759 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1760 if (pix_y < 0)
1761 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1763 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1764 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1766 if (bounds)
1767 STORE_NATIVE_RECT (*bounds,
1768 FRAME_COL_TO_PIXEL_X (f, pix_x),
1769 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1770 FRAME_COLUMN_WIDTH (f) - 1,
1771 FRAME_LINE_HEIGHT (f) - 1);
1773 if (!noclip)
1775 if (pix_x < 0)
1776 pix_x = 0;
1777 else if (pix_x > FRAME_TOTAL_COLS (f))
1778 pix_x = FRAME_TOTAL_COLS (f);
1780 if (pix_y < 0)
1781 pix_y = 0;
1782 else if (pix_y > FRAME_LINES (f))
1783 pix_y = FRAME_LINES (f);
1786 #endif
1788 *x = pix_x;
1789 *y = pix_y;
1793 /* Find the glyph under window-relative coordinates X/Y in window W.
1794 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1795 strings. Return in *HPOS and *VPOS the row and column number of
1796 the glyph found. Return in *AREA the glyph area containing X.
1797 Value is a pointer to the glyph found or null if X/Y is not on
1798 text, or we can't tell because W's current matrix is not up to
1799 date. */
1801 static
1802 struct glyph *
1803 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1804 int *dx, int *dy, int *area)
1806 struct glyph *glyph, *end;
1807 struct glyph_row *row = NULL;
1808 int x0, i;
1810 /* Find row containing Y. Give up if some row is not enabled. */
1811 for (i = 0; i < w->current_matrix->nrows; ++i)
1813 row = MATRIX_ROW (w->current_matrix, i);
1814 if (!row->enabled_p)
1815 return NULL;
1816 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1817 break;
1820 *vpos = i;
1821 *hpos = 0;
1823 /* Give up if Y is not in the window. */
1824 if (i == w->current_matrix->nrows)
1825 return NULL;
1827 /* Get the glyph area containing X. */
1828 if (w->pseudo_window_p)
1830 *area = TEXT_AREA;
1831 x0 = 0;
1833 else
1835 if (x < window_box_left_offset (w, TEXT_AREA))
1837 *area = LEFT_MARGIN_AREA;
1838 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1840 else if (x < window_box_right_offset (w, TEXT_AREA))
1842 *area = TEXT_AREA;
1843 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1845 else
1847 *area = RIGHT_MARGIN_AREA;
1848 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1852 /* Find glyph containing X. */
1853 glyph = row->glyphs[*area];
1854 end = glyph + row->used[*area];
1855 x -= x0;
1856 while (glyph < end && x >= glyph->pixel_width)
1858 x -= glyph->pixel_width;
1859 ++glyph;
1862 if (glyph == end)
1863 return NULL;
1865 if (dx)
1867 *dx = x;
1868 *dy = y - (row->y + row->ascent - glyph->ascent);
1871 *hpos = glyph - row->glyphs[*area];
1872 return glyph;
1875 /* Convert frame-relative x/y to coordinates relative to window W.
1876 Takes pseudo-windows into account. */
1878 static void
1879 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1881 if (w->pseudo_window_p)
1883 /* A pseudo-window is always full-width, and starts at the
1884 left edge of the frame, plus a frame border. */
1885 struct frame *f = XFRAME (w->frame);
1886 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1889 else
1891 *x -= WINDOW_LEFT_EDGE_X (w);
1892 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1896 #ifdef HAVE_WINDOW_SYSTEM
1898 /* EXPORT:
1899 Return in RECTS[] at most N clipping rectangles for glyph string S.
1900 Return the number of stored rectangles. */
1903 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1905 XRectangle r;
1907 if (n <= 0)
1908 return 0;
1910 if (s->row->full_width_p)
1912 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1913 r.x = WINDOW_LEFT_EDGE_X (s->w);
1914 r.width = WINDOW_TOTAL_WIDTH (s->w);
1916 /* Unless displaying a mode or menu bar line, which are always
1917 fully visible, clip to the visible part of the row. */
1918 if (s->w->pseudo_window_p)
1919 r.height = s->row->visible_height;
1920 else
1921 r.height = s->height;
1923 else
1925 /* This is a text line that may be partially visible. */
1926 r.x = window_box_left (s->w, s->area);
1927 r.width = window_box_width (s->w, s->area);
1928 r.height = s->row->visible_height;
1931 if (s->clip_head)
1932 if (r.x < s->clip_head->x)
1934 if (r.width >= s->clip_head->x - r.x)
1935 r.width -= s->clip_head->x - r.x;
1936 else
1937 r.width = 0;
1938 r.x = s->clip_head->x;
1940 if (s->clip_tail)
1941 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1943 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1944 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1945 else
1946 r.width = 0;
1949 /* If S draws overlapping rows, it's sufficient to use the top and
1950 bottom of the window for clipping because this glyph string
1951 intentionally draws over other lines. */
1952 if (s->for_overlaps)
1954 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1955 r.height = window_text_bottom_y (s->w) - r.y;
1957 /* Alas, the above simple strategy does not work for the
1958 environments with anti-aliased text: if the same text is
1959 drawn onto the same place multiple times, it gets thicker.
1960 If the overlap we are processing is for the erased cursor, we
1961 take the intersection with the rectangle of the cursor. */
1962 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1964 XRectangle rc, r_save = r;
1966 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1967 rc.y = s->w->phys_cursor.y;
1968 rc.width = s->w->phys_cursor_width;
1969 rc.height = s->w->phys_cursor_height;
1971 x_intersect_rectangles (&r_save, &rc, &r);
1974 else
1976 /* Don't use S->y for clipping because it doesn't take partially
1977 visible lines into account. For example, it can be negative for
1978 partially visible lines at the top of a window. */
1979 if (!s->row->full_width_p
1980 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1981 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1982 else
1983 r.y = max (0, s->row->y);
1986 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1988 /* If drawing the cursor, don't let glyph draw outside its
1989 advertised boundaries. Cleartype does this under some circumstances. */
1990 if (s->hl == DRAW_CURSOR)
1992 struct glyph *glyph = s->first_glyph;
1993 int height, max_y;
1995 if (s->x > r.x)
1997 r.width -= s->x - r.x;
1998 r.x = s->x;
2000 r.width = min (r.width, glyph->pixel_width);
2002 /* If r.y is below window bottom, ensure that we still see a cursor. */
2003 height = min (glyph->ascent + glyph->descent,
2004 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2005 max_y = window_text_bottom_y (s->w) - height;
2006 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2007 if (s->ybase - glyph->ascent > max_y)
2009 r.y = max_y;
2010 r.height = height;
2012 else
2014 /* Don't draw cursor glyph taller than our actual glyph. */
2015 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2016 if (height < r.height)
2018 max_y = r.y + r.height;
2019 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2020 r.height = min (max_y - r.y, height);
2025 if (s->row->clip)
2027 XRectangle r_save = r;
2029 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2030 r.width = 0;
2033 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2034 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2036 #ifdef CONVERT_FROM_XRECT
2037 CONVERT_FROM_XRECT (r, *rects);
2038 #else
2039 *rects = r;
2040 #endif
2041 return 1;
2043 else
2045 /* If we are processing overlapping and allowed to return
2046 multiple clipping rectangles, we exclude the row of the glyph
2047 string from the clipping rectangle. This is to avoid drawing
2048 the same text on the environment with anti-aliasing. */
2049 #ifdef CONVERT_FROM_XRECT
2050 XRectangle rs[2];
2051 #else
2052 XRectangle *rs = rects;
2053 #endif
2054 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2056 if (s->for_overlaps & OVERLAPS_PRED)
2058 rs[i] = r;
2059 if (r.y + r.height > row_y)
2061 if (r.y < row_y)
2062 rs[i].height = row_y - r.y;
2063 else
2064 rs[i].height = 0;
2066 i++;
2068 if (s->for_overlaps & OVERLAPS_SUCC)
2070 rs[i] = r;
2071 if (r.y < row_y + s->row->visible_height)
2073 if (r.y + r.height > row_y + s->row->visible_height)
2075 rs[i].y = row_y + s->row->visible_height;
2076 rs[i].height = r.y + r.height - rs[i].y;
2078 else
2079 rs[i].height = 0;
2081 i++;
2084 n = i;
2085 #ifdef CONVERT_FROM_XRECT
2086 for (i = 0; i < n; i++)
2087 CONVERT_FROM_XRECT (rs[i], rects[i]);
2088 #endif
2089 return n;
2093 /* EXPORT:
2094 Return in *NR the clipping rectangle for glyph string S. */
2096 void
2097 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2099 get_glyph_string_clip_rects (s, nr, 1);
2103 /* EXPORT:
2104 Return the position and height of the phys cursor in window W.
2105 Set w->phys_cursor_width to width of phys cursor.
2108 void
2109 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2110 struct glyph *glyph, int *xp, int *yp, int *heightp)
2112 struct frame *f = XFRAME (WINDOW_FRAME (w));
2113 int x, y, wd, h, h0, y0;
2115 /* Compute the width of the rectangle to draw. If on a stretch
2116 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2117 rectangle as wide as the glyph, but use a canonical character
2118 width instead. */
2119 wd = glyph->pixel_width - 1;
2120 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2121 wd++; /* Why? */
2122 #endif
2124 x = w->phys_cursor.x;
2125 if (x < 0)
2127 wd += x;
2128 x = 0;
2131 if (glyph->type == STRETCH_GLYPH
2132 && !x_stretch_cursor_p)
2133 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2134 w->phys_cursor_width = wd;
2136 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2138 /* If y is below window bottom, ensure that we still see a cursor. */
2139 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2141 h = max (h0, glyph->ascent + glyph->descent);
2142 h0 = min (h0, glyph->ascent + glyph->descent);
2144 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2145 if (y < y0)
2147 h = max (h - (y0 - y) + 1, h0);
2148 y = y0 - 1;
2150 else
2152 y0 = window_text_bottom_y (w) - h0;
2153 if (y > y0)
2155 h += y - y0;
2156 y = y0;
2160 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2161 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2162 *heightp = h;
2166 * Remember which glyph the mouse is over.
2169 void
2170 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2172 Lisp_Object window;
2173 struct window *w;
2174 struct glyph_row *r, *gr, *end_row;
2175 enum window_part part;
2176 enum glyph_row_area area;
2177 int x, y, width, height;
2179 /* Try to determine frame pixel position and size of the glyph under
2180 frame pixel coordinates X/Y on frame F. */
2182 if (!f->glyphs_initialized_p
2183 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2184 NILP (window)))
2186 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2187 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2188 goto virtual_glyph;
2191 w = XWINDOW (window);
2192 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2193 height = WINDOW_FRAME_LINE_HEIGHT (w);
2195 x = window_relative_x_coord (w, part, gx);
2196 y = gy - WINDOW_TOP_EDGE_Y (w);
2198 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2199 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2201 if (w->pseudo_window_p)
2203 area = TEXT_AREA;
2204 part = ON_MODE_LINE; /* Don't adjust margin. */
2205 goto text_glyph;
2208 switch (part)
2210 case ON_LEFT_MARGIN:
2211 area = LEFT_MARGIN_AREA;
2212 goto text_glyph;
2214 case ON_RIGHT_MARGIN:
2215 area = RIGHT_MARGIN_AREA;
2216 goto text_glyph;
2218 case ON_HEADER_LINE:
2219 case ON_MODE_LINE:
2220 gr = (part == ON_HEADER_LINE
2221 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2222 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2223 gy = gr->y;
2224 area = TEXT_AREA;
2225 goto text_glyph_row_found;
2227 case ON_TEXT:
2228 area = TEXT_AREA;
2230 text_glyph:
2231 gr = 0; gy = 0;
2232 for (; r <= end_row && r->enabled_p; ++r)
2233 if (r->y + r->height > y)
2235 gr = r; gy = r->y;
2236 break;
2239 text_glyph_row_found:
2240 if (gr && gy <= y)
2242 struct glyph *g = gr->glyphs[area];
2243 struct glyph *end = g + gr->used[area];
2245 height = gr->height;
2246 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2247 if (gx + g->pixel_width > x)
2248 break;
2250 if (g < end)
2252 if (g->type == IMAGE_GLYPH)
2254 /* Don't remember when mouse is over image, as
2255 image may have hot-spots. */
2256 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2257 return;
2259 width = g->pixel_width;
2261 else
2263 /* Use nominal char spacing at end of line. */
2264 x -= gx;
2265 gx += (x / width) * width;
2268 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2269 gx += window_box_left_offset (w, area);
2271 else
2273 /* Use nominal line height at end of window. */
2274 gx = (x / width) * width;
2275 y -= gy;
2276 gy += (y / height) * height;
2278 break;
2280 case ON_LEFT_FRINGE:
2281 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2282 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2283 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2284 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2285 goto row_glyph;
2287 case ON_RIGHT_FRINGE:
2288 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2289 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2290 : window_box_right_offset (w, TEXT_AREA));
2291 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2292 goto row_glyph;
2294 case ON_SCROLL_BAR:
2295 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2297 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2298 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2300 : 0)));
2301 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2303 row_glyph:
2304 gr = 0, gy = 0;
2305 for (; r <= end_row && r->enabled_p; ++r)
2306 if (r->y + r->height > y)
2308 gr = r; gy = r->y;
2309 break;
2312 if (gr && gy <= y)
2313 height = gr->height;
2314 else
2316 /* Use nominal line height at end of window. */
2317 y -= gy;
2318 gy += (y / height) * height;
2320 break;
2322 default:
2324 virtual_glyph:
2325 /* If there is no glyph under the mouse, then we divide the screen
2326 into a grid of the smallest glyph in the frame, and use that
2327 as our "glyph". */
2329 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2330 round down even for negative values. */
2331 if (gx < 0)
2332 gx -= width - 1;
2333 if (gy < 0)
2334 gy -= height - 1;
2336 gx = (gx / width) * width;
2337 gy = (gy / height) * height;
2339 goto store_rect;
2342 gx += WINDOW_LEFT_EDGE_X (w);
2343 gy += WINDOW_TOP_EDGE_Y (w);
2345 store_rect:
2346 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2348 /* Visible feedback for debugging. */
2349 #if 0
2350 #if HAVE_X_WINDOWS
2351 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2352 f->output_data.x->normal_gc,
2353 gx, gy, width, height);
2354 #endif
2355 #endif
2359 #endif /* HAVE_WINDOW_SYSTEM */
2362 /***********************************************************************
2363 Lisp form evaluation
2364 ***********************************************************************/
2366 /* Error handler for safe_eval and safe_call. */
2368 static Lisp_Object
2369 safe_eval_handler (Lisp_Object arg)
2371 add_to_log ("Error during redisplay: %S", arg, Qnil);
2372 return Qnil;
2376 /* Evaluate SEXPR and return the result, or nil if something went
2377 wrong. Prevent redisplay during the evaluation. */
2379 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2380 Return the result, or nil if something went wrong. Prevent
2381 redisplay during the evaluation. */
2383 Lisp_Object
2384 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2386 Lisp_Object val;
2388 if (inhibit_eval_during_redisplay)
2389 val = Qnil;
2390 else
2392 int count = SPECPDL_INDEX ();
2393 struct gcpro gcpro1;
2395 GCPRO1 (args[0]);
2396 gcpro1.nvars = nargs;
2397 specbind (Qinhibit_redisplay, Qt);
2398 /* Use Qt to ensure debugger does not run,
2399 so there is no possibility of wanting to redisplay. */
2400 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2401 safe_eval_handler);
2402 UNGCPRO;
2403 val = unbind_to (count, val);
2406 return val;
2410 /* Call function FN with one argument ARG.
2411 Return the result, or nil if something went wrong. */
2413 Lisp_Object
2414 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2416 Lisp_Object args[2];
2417 args[0] = fn;
2418 args[1] = arg;
2419 return safe_call (2, args);
2422 static Lisp_Object Qeval;
2424 Lisp_Object
2425 safe_eval (Lisp_Object sexpr)
2427 return safe_call1 (Qeval, sexpr);
2430 /* Call function FN with one argument ARG.
2431 Return the result, or nil if something went wrong. */
2433 Lisp_Object
2434 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2436 Lisp_Object args[3];
2437 args[0] = fn;
2438 args[1] = arg1;
2439 args[2] = arg2;
2440 return safe_call (3, args);
2445 /***********************************************************************
2446 Debugging
2447 ***********************************************************************/
2449 #if 0
2451 /* Define CHECK_IT to perform sanity checks on iterators.
2452 This is for debugging. It is too slow to do unconditionally. */
2454 static void
2455 check_it (struct it *it)
2457 if (it->method == GET_FROM_STRING)
2459 xassert (STRINGP (it->string));
2460 xassert (IT_STRING_CHARPOS (*it) >= 0);
2462 else
2464 xassert (IT_STRING_CHARPOS (*it) < 0);
2465 if (it->method == GET_FROM_BUFFER)
2467 /* Check that character and byte positions agree. */
2468 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2472 if (it->dpvec)
2473 xassert (it->current.dpvec_index >= 0);
2474 else
2475 xassert (it->current.dpvec_index < 0);
2478 #define CHECK_IT(IT) check_it ((IT))
2480 #else /* not 0 */
2482 #define CHECK_IT(IT) (void) 0
2484 #endif /* not 0 */
2487 #if GLYPH_DEBUG && XASSERTS
2489 /* Check that the window end of window W is what we expect it
2490 to be---the last row in the current matrix displaying text. */
2492 static void
2493 check_window_end (struct window *w)
2495 if (!MINI_WINDOW_P (w)
2496 && !NILP (w->window_end_valid))
2498 struct glyph_row *row;
2499 xassert ((row = MATRIX_ROW (w->current_matrix,
2500 XFASTINT (w->window_end_vpos)),
2501 !row->enabled_p
2502 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2503 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2507 #define CHECK_WINDOW_END(W) check_window_end ((W))
2509 #else
2511 #define CHECK_WINDOW_END(W) (void) 0
2513 #endif
2517 /***********************************************************************
2518 Iterator initialization
2519 ***********************************************************************/
2521 /* Initialize IT for displaying current_buffer in window W, starting
2522 at character position CHARPOS. CHARPOS < 0 means that no buffer
2523 position is specified which is useful when the iterator is assigned
2524 a position later. BYTEPOS is the byte position corresponding to
2525 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2527 If ROW is not null, calls to produce_glyphs with IT as parameter
2528 will produce glyphs in that row.
2530 BASE_FACE_ID is the id of a base face to use. It must be one of
2531 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2532 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2533 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2535 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2536 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2537 will be initialized to use the corresponding mode line glyph row of
2538 the desired matrix of W. */
2540 void
2541 init_iterator (struct it *it, struct window *w,
2542 EMACS_INT charpos, EMACS_INT bytepos,
2543 struct glyph_row *row, enum face_id base_face_id)
2545 int highlight_region_p;
2546 enum face_id remapped_base_face_id = base_face_id;
2548 /* Some precondition checks. */
2549 xassert (w != NULL && it != NULL);
2550 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2551 && charpos <= ZV));
2553 /* If face attributes have been changed since the last redisplay,
2554 free realized faces now because they depend on face definitions
2555 that might have changed. Don't free faces while there might be
2556 desired matrices pending which reference these faces. */
2557 if (face_change_count && !inhibit_free_realized_faces)
2559 face_change_count = 0;
2560 free_all_realized_faces (Qnil);
2563 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2564 if (! NILP (Vface_remapping_alist))
2565 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2567 /* Use one of the mode line rows of W's desired matrix if
2568 appropriate. */
2569 if (row == NULL)
2571 if (base_face_id == MODE_LINE_FACE_ID
2572 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2573 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2574 else if (base_face_id == HEADER_LINE_FACE_ID)
2575 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2578 /* Clear IT. */
2579 memset (it, 0, sizeof *it);
2580 it->current.overlay_string_index = -1;
2581 it->current.dpvec_index = -1;
2582 it->base_face_id = remapped_base_face_id;
2583 it->string = Qnil;
2584 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2585 it->paragraph_embedding = L2R;
2586 it->bidi_it.string.lstring = Qnil;
2587 it->bidi_it.string.s = NULL;
2588 it->bidi_it.string.bufpos = 0;
2590 /* The window in which we iterate over current_buffer: */
2591 XSETWINDOW (it->window, w);
2592 it->w = w;
2593 it->f = XFRAME (w->frame);
2595 it->cmp_it.id = -1;
2597 /* Extra space between lines (on window systems only). */
2598 if (base_face_id == DEFAULT_FACE_ID
2599 && FRAME_WINDOW_P (it->f))
2601 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2602 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2603 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2604 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2605 * FRAME_LINE_HEIGHT (it->f));
2606 else if (it->f->extra_line_spacing > 0)
2607 it->extra_line_spacing = it->f->extra_line_spacing;
2608 it->max_extra_line_spacing = 0;
2611 /* If realized faces have been removed, e.g. because of face
2612 attribute changes of named faces, recompute them. When running
2613 in batch mode, the face cache of the initial frame is null. If
2614 we happen to get called, make a dummy face cache. */
2615 if (FRAME_FACE_CACHE (it->f) == NULL)
2616 init_frame_faces (it->f);
2617 if (FRAME_FACE_CACHE (it->f)->used == 0)
2618 recompute_basic_faces (it->f);
2620 /* Current value of the `slice', `space-width', and 'height' properties. */
2621 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2622 it->space_width = Qnil;
2623 it->font_height = Qnil;
2624 it->override_ascent = -1;
2626 /* Are control characters displayed as `^C'? */
2627 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2629 /* -1 means everything between a CR and the following line end
2630 is invisible. >0 means lines indented more than this value are
2631 invisible. */
2632 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2633 ? XINT (BVAR (current_buffer, selective_display))
2634 : (!NILP (BVAR (current_buffer, selective_display))
2635 ? -1 : 0));
2636 it->selective_display_ellipsis_p
2637 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2639 /* Display table to use. */
2640 it->dp = window_display_table (w);
2642 /* Are multibyte characters enabled in current_buffer? */
2643 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2645 /* Non-zero if we should highlight the region. */
2646 highlight_region_p
2647 = (!NILP (Vtransient_mark_mode)
2648 && !NILP (BVAR (current_buffer, mark_active))
2649 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2651 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2652 start and end of a visible region in window IT->w. Set both to
2653 -1 to indicate no region. */
2654 if (highlight_region_p
2655 /* Maybe highlight only in selected window. */
2656 && (/* Either show region everywhere. */
2657 highlight_nonselected_windows
2658 /* Or show region in the selected window. */
2659 || w == XWINDOW (selected_window)
2660 /* Or show the region if we are in the mini-buffer and W is
2661 the window the mini-buffer refers to. */
2662 || (MINI_WINDOW_P (XWINDOW (selected_window))
2663 && WINDOWP (minibuf_selected_window)
2664 && w == XWINDOW (minibuf_selected_window))))
2666 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2667 it->region_beg_charpos = min (PT, markpos);
2668 it->region_end_charpos = max (PT, markpos);
2670 else
2671 it->region_beg_charpos = it->region_end_charpos = -1;
2673 /* Get the position at which the redisplay_end_trigger hook should
2674 be run, if it is to be run at all. */
2675 if (MARKERP (w->redisplay_end_trigger)
2676 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2677 it->redisplay_end_trigger_charpos
2678 = marker_position (w->redisplay_end_trigger);
2679 else if (INTEGERP (w->redisplay_end_trigger))
2680 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2682 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2684 /* Are lines in the display truncated? */
2685 if (base_face_id != DEFAULT_FACE_ID
2686 || XINT (it->w->hscroll)
2687 || (! WINDOW_FULL_WIDTH_P (it->w)
2688 && ((!NILP (Vtruncate_partial_width_windows)
2689 && !INTEGERP (Vtruncate_partial_width_windows))
2690 || (INTEGERP (Vtruncate_partial_width_windows)
2691 && (WINDOW_TOTAL_COLS (it->w)
2692 < XINT (Vtruncate_partial_width_windows))))))
2693 it->line_wrap = TRUNCATE;
2694 else if (NILP (BVAR (current_buffer, truncate_lines)))
2695 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2696 ? WINDOW_WRAP : WORD_WRAP;
2697 else
2698 it->line_wrap = TRUNCATE;
2700 /* Get dimensions of truncation and continuation glyphs. These are
2701 displayed as fringe bitmaps under X, so we don't need them for such
2702 frames. */
2703 if (!FRAME_WINDOW_P (it->f))
2705 if (it->line_wrap == TRUNCATE)
2707 /* We will need the truncation glyph. */
2708 xassert (it->glyph_row == NULL);
2709 produce_special_glyphs (it, IT_TRUNCATION);
2710 it->truncation_pixel_width = it->pixel_width;
2712 else
2714 /* We will need the continuation glyph. */
2715 xassert (it->glyph_row == NULL);
2716 produce_special_glyphs (it, IT_CONTINUATION);
2717 it->continuation_pixel_width = it->pixel_width;
2720 /* Reset these values to zero because the produce_special_glyphs
2721 above has changed them. */
2722 it->pixel_width = it->ascent = it->descent = 0;
2723 it->phys_ascent = it->phys_descent = 0;
2726 /* Set this after getting the dimensions of truncation and
2727 continuation glyphs, so that we don't produce glyphs when calling
2728 produce_special_glyphs, above. */
2729 it->glyph_row = row;
2730 it->area = TEXT_AREA;
2732 /* Forget any previous info about this row being reversed. */
2733 if (it->glyph_row)
2734 it->glyph_row->reversed_p = 0;
2736 /* Get the dimensions of the display area. The display area
2737 consists of the visible window area plus a horizontally scrolled
2738 part to the left of the window. All x-values are relative to the
2739 start of this total display area. */
2740 if (base_face_id != DEFAULT_FACE_ID)
2742 /* Mode lines, menu bar in terminal frames. */
2743 it->first_visible_x = 0;
2744 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2746 else
2748 it->first_visible_x
2749 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2750 it->last_visible_x = (it->first_visible_x
2751 + window_box_width (w, TEXT_AREA));
2753 /* If we truncate lines, leave room for the truncator glyph(s) at
2754 the right margin. Otherwise, leave room for the continuation
2755 glyph(s). Truncation and continuation glyphs are not inserted
2756 for window-based redisplay. */
2757 if (!FRAME_WINDOW_P (it->f))
2759 if (it->line_wrap == TRUNCATE)
2760 it->last_visible_x -= it->truncation_pixel_width;
2761 else
2762 it->last_visible_x -= it->continuation_pixel_width;
2765 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2766 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2769 /* Leave room for a border glyph. */
2770 if (!FRAME_WINDOW_P (it->f)
2771 && !WINDOW_RIGHTMOST_P (it->w))
2772 it->last_visible_x -= 1;
2774 it->last_visible_y = window_text_bottom_y (w);
2776 /* For mode lines and alike, arrange for the first glyph having a
2777 left box line if the face specifies a box. */
2778 if (base_face_id != DEFAULT_FACE_ID)
2780 struct face *face;
2782 it->face_id = remapped_base_face_id;
2784 /* If we have a boxed mode line, make the first character appear
2785 with a left box line. */
2786 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2787 if (face->box != FACE_NO_BOX)
2788 it->start_of_box_run_p = 1;
2791 /* If a buffer position was specified, set the iterator there,
2792 getting overlays and face properties from that position. */
2793 if (charpos >= BUF_BEG (current_buffer))
2795 it->end_charpos = ZV;
2796 IT_CHARPOS (*it) = charpos;
2798 /* We will rely on `reseat' to set this up properly, via
2799 handle_face_prop. */
2800 it->face_id = it->base_face_id;
2802 /* Compute byte position if not specified. */
2803 if (bytepos < charpos)
2804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2805 else
2806 IT_BYTEPOS (*it) = bytepos;
2808 it->start = it->current;
2809 /* Do we need to reorder bidirectional text? Not if this is a
2810 unibyte buffer: by definition, none of the single-byte
2811 characters are strong R2L, so no reordering is needed. And
2812 bidi.c doesn't support unibyte buffers anyway. Also, don't
2813 reorder while we are loading loadup.el, since the tables of
2814 character properties needed for reordering are not yet
2815 available. */
2816 it->bidi_p =
2817 NILP (Vpurify_flag)
2818 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2819 && it->multibyte_p;
2821 /* If we are to reorder bidirectional text, init the bidi
2822 iterator. */
2823 if (it->bidi_p)
2825 /* Note the paragraph direction that this buffer wants to
2826 use. */
2827 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2828 Qleft_to_right))
2829 it->paragraph_embedding = L2R;
2830 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2831 Qright_to_left))
2832 it->paragraph_embedding = R2L;
2833 else
2834 it->paragraph_embedding = NEUTRAL_DIR;
2835 bidi_unshelve_cache (NULL, 0);
2836 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2837 &it->bidi_it);
2840 /* Compute faces etc. */
2841 reseat (it, it->current.pos, 1);
2844 CHECK_IT (it);
2848 /* Initialize IT for the display of window W with window start POS. */
2850 void
2851 start_display (struct it *it, struct window *w, struct text_pos pos)
2853 struct glyph_row *row;
2854 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2856 row = w->desired_matrix->rows + first_vpos;
2857 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2858 it->first_vpos = first_vpos;
2860 /* Don't reseat to previous visible line start if current start
2861 position is in a string or image. */
2862 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2864 int start_at_line_beg_p;
2865 int first_y = it->current_y;
2867 /* If window start is not at a line start, skip forward to POS to
2868 get the correct continuation lines width. */
2869 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2870 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2871 if (!start_at_line_beg_p)
2873 int new_x;
2875 reseat_at_previous_visible_line_start (it);
2876 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2878 new_x = it->current_x + it->pixel_width;
2880 /* If lines are continued, this line may end in the middle
2881 of a multi-glyph character (e.g. a control character
2882 displayed as \003, or in the middle of an overlay
2883 string). In this case move_it_to above will not have
2884 taken us to the start of the continuation line but to the
2885 end of the continued line. */
2886 if (it->current_x > 0
2887 && it->line_wrap != TRUNCATE /* Lines are continued. */
2888 && (/* And glyph doesn't fit on the line. */
2889 new_x > it->last_visible_x
2890 /* Or it fits exactly and we're on a window
2891 system frame. */
2892 || (new_x == it->last_visible_x
2893 && FRAME_WINDOW_P (it->f))))
2895 if ((it->current.dpvec_index >= 0
2896 || it->current.overlay_string_index >= 0)
2897 /* If we are on a newline from a display vector or
2898 overlay string, then we are already at the end of
2899 a screen line; no need to go to the next line in
2900 that case, as this line is not really continued.
2901 (If we do go to the next line, C-e will not DTRT.) */
2902 && it->c != '\n')
2904 set_iterator_to_next (it, 1);
2905 move_it_in_display_line_to (it, -1, -1, 0);
2908 it->continuation_lines_width += it->current_x;
2910 /* If the character at POS is displayed via a display
2911 vector, move_it_to above stops at the final glyph of
2912 IT->dpvec. To make the caller redisplay that character
2913 again (a.k.a. start at POS), we need to reset the
2914 dpvec_index to the beginning of IT->dpvec. */
2915 else if (it->current.dpvec_index >= 0)
2916 it->current.dpvec_index = 0;
2918 /* We're starting a new display line, not affected by the
2919 height of the continued line, so clear the appropriate
2920 fields in the iterator structure. */
2921 it->max_ascent = it->max_descent = 0;
2922 it->max_phys_ascent = it->max_phys_descent = 0;
2924 it->current_y = first_y;
2925 it->vpos = 0;
2926 it->current_x = it->hpos = 0;
2932 /* Return 1 if POS is a position in ellipses displayed for invisible
2933 text. W is the window we display, for text property lookup. */
2935 static int
2936 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2938 Lisp_Object prop, window;
2939 int ellipses_p = 0;
2940 EMACS_INT charpos = CHARPOS (pos->pos);
2942 /* If POS specifies a position in a display vector, this might
2943 be for an ellipsis displayed for invisible text. We won't
2944 get the iterator set up for delivering that ellipsis unless
2945 we make sure that it gets aware of the invisible text. */
2946 if (pos->dpvec_index >= 0
2947 && pos->overlay_string_index < 0
2948 && CHARPOS (pos->string_pos) < 0
2949 && charpos > BEGV
2950 && (XSETWINDOW (window, w),
2951 prop = Fget_char_property (make_number (charpos),
2952 Qinvisible, window),
2953 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2955 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2956 window);
2957 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2960 return ellipses_p;
2964 /* Initialize IT for stepping through current_buffer in window W,
2965 starting at position POS that includes overlay string and display
2966 vector/ control character translation position information. Value
2967 is zero if there are overlay strings with newlines at POS. */
2969 static int
2970 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2972 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2973 int i, overlay_strings_with_newlines = 0;
2975 /* If POS specifies a position in a display vector, this might
2976 be for an ellipsis displayed for invisible text. We won't
2977 get the iterator set up for delivering that ellipsis unless
2978 we make sure that it gets aware of the invisible text. */
2979 if (in_ellipses_for_invisible_text_p (pos, w))
2981 --charpos;
2982 bytepos = 0;
2985 /* Keep in mind: the call to reseat in init_iterator skips invisible
2986 text, so we might end up at a position different from POS. This
2987 is only a problem when POS is a row start after a newline and an
2988 overlay starts there with an after-string, and the overlay has an
2989 invisible property. Since we don't skip invisible text in
2990 display_line and elsewhere immediately after consuming the
2991 newline before the row start, such a POS will not be in a string,
2992 but the call to init_iterator below will move us to the
2993 after-string. */
2994 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2996 /* This only scans the current chunk -- it should scan all chunks.
2997 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2998 to 16 in 22.1 to make this a lesser problem. */
2999 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3001 const char *s = SSDATA (it->overlay_strings[i]);
3002 const char *e = s + SBYTES (it->overlay_strings[i]);
3004 while (s < e && *s != '\n')
3005 ++s;
3007 if (s < e)
3009 overlay_strings_with_newlines = 1;
3010 break;
3014 /* If position is within an overlay string, set up IT to the right
3015 overlay string. */
3016 if (pos->overlay_string_index >= 0)
3018 int relative_index;
3020 /* If the first overlay string happens to have a `display'
3021 property for an image, the iterator will be set up for that
3022 image, and we have to undo that setup first before we can
3023 correct the overlay string index. */
3024 if (it->method == GET_FROM_IMAGE)
3025 pop_it (it);
3027 /* We already have the first chunk of overlay strings in
3028 IT->overlay_strings. Load more until the one for
3029 pos->overlay_string_index is in IT->overlay_strings. */
3030 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3032 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3033 it->current.overlay_string_index = 0;
3034 while (n--)
3036 load_overlay_strings (it, 0);
3037 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3041 it->current.overlay_string_index = pos->overlay_string_index;
3042 relative_index = (it->current.overlay_string_index
3043 % OVERLAY_STRING_CHUNK_SIZE);
3044 it->string = it->overlay_strings[relative_index];
3045 xassert (STRINGP (it->string));
3046 it->current.string_pos = pos->string_pos;
3047 it->method = GET_FROM_STRING;
3050 if (CHARPOS (pos->string_pos) >= 0)
3052 /* Recorded position is not in an overlay string, but in another
3053 string. This can only be a string from a `display' property.
3054 IT should already be filled with that string. */
3055 it->current.string_pos = pos->string_pos;
3056 xassert (STRINGP (it->string));
3059 /* Restore position in display vector translations, control
3060 character translations or ellipses. */
3061 if (pos->dpvec_index >= 0)
3063 if (it->dpvec == NULL)
3064 get_next_display_element (it);
3065 xassert (it->dpvec && it->current.dpvec_index == 0);
3066 it->current.dpvec_index = pos->dpvec_index;
3069 CHECK_IT (it);
3070 return !overlay_strings_with_newlines;
3074 /* Initialize IT for stepping through current_buffer in window W
3075 starting at ROW->start. */
3077 static void
3078 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3080 init_from_display_pos (it, w, &row->start);
3081 it->start = row->start;
3082 it->continuation_lines_width = row->continuation_lines_width;
3083 CHECK_IT (it);
3087 /* Initialize IT for stepping through current_buffer in window W
3088 starting in the line following ROW, i.e. starting at ROW->end.
3089 Value is zero if there are overlay strings with newlines at ROW's
3090 end position. */
3092 static int
3093 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3095 int success = 0;
3097 if (init_from_display_pos (it, w, &row->end))
3099 if (row->continued_p)
3100 it->continuation_lines_width
3101 = row->continuation_lines_width + row->pixel_width;
3102 CHECK_IT (it);
3103 success = 1;
3106 return success;
3112 /***********************************************************************
3113 Text properties
3114 ***********************************************************************/
3116 /* Called when IT reaches IT->stop_charpos. Handle text property and
3117 overlay changes. Set IT->stop_charpos to the next position where
3118 to stop. */
3120 static void
3121 handle_stop (struct it *it)
3123 enum prop_handled handled;
3124 int handle_overlay_change_p;
3125 struct props *p;
3127 it->dpvec = NULL;
3128 it->current.dpvec_index = -1;
3129 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3130 it->ignore_overlay_strings_at_pos_p = 0;
3131 it->ellipsis_p = 0;
3133 /* Use face of preceding text for ellipsis (if invisible) */
3134 if (it->selective_display_ellipsis_p)
3135 it->saved_face_id = it->face_id;
3139 handled = HANDLED_NORMALLY;
3141 /* Call text property handlers. */
3142 for (p = it_props; p->handler; ++p)
3144 handled = p->handler (it);
3146 if (handled == HANDLED_RECOMPUTE_PROPS)
3147 break;
3148 else if (handled == HANDLED_RETURN)
3150 /* We still want to show before and after strings from
3151 overlays even if the actual buffer text is replaced. */
3152 if (!handle_overlay_change_p
3153 || it->sp > 1
3154 /* Don't call get_overlay_strings_1 if we already
3155 have overlay strings loaded, because doing so
3156 will load them again and push the iterator state
3157 onto the stack one more time, which is not
3158 expected by the rest of the code that processes
3159 overlay strings. */
3160 || (it->current.overlay_string_index < 0
3161 ? !get_overlay_strings_1 (it, 0, 0)
3162 : 0))
3164 if (it->ellipsis_p)
3165 setup_for_ellipsis (it, 0);
3166 /* When handling a display spec, we might load an
3167 empty string. In that case, discard it here. We
3168 used to discard it in handle_single_display_spec,
3169 but that causes get_overlay_strings_1, above, to
3170 ignore overlay strings that we must check. */
3171 if (STRINGP (it->string) && !SCHARS (it->string))
3172 pop_it (it);
3173 return;
3175 else if (STRINGP (it->string) && !SCHARS (it->string))
3176 pop_it (it);
3177 else
3179 it->ignore_overlay_strings_at_pos_p = 1;
3180 it->string_from_display_prop_p = 0;
3181 it->from_disp_prop_p = 0;
3182 handle_overlay_change_p = 0;
3184 handled = HANDLED_RECOMPUTE_PROPS;
3185 break;
3187 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3188 handle_overlay_change_p = 0;
3191 if (handled != HANDLED_RECOMPUTE_PROPS)
3193 /* Don't check for overlay strings below when set to deliver
3194 characters from a display vector. */
3195 if (it->method == GET_FROM_DISPLAY_VECTOR)
3196 handle_overlay_change_p = 0;
3198 /* Handle overlay changes.
3199 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3200 if it finds overlays. */
3201 if (handle_overlay_change_p)
3202 handled = handle_overlay_change (it);
3205 if (it->ellipsis_p)
3207 setup_for_ellipsis (it, 0);
3208 break;
3211 while (handled == HANDLED_RECOMPUTE_PROPS);
3213 /* Determine where to stop next. */
3214 if (handled == HANDLED_NORMALLY)
3215 compute_stop_pos (it);
3219 /* Compute IT->stop_charpos from text property and overlay change
3220 information for IT's current position. */
3222 static void
3223 compute_stop_pos (struct it *it)
3225 register INTERVAL iv, next_iv;
3226 Lisp_Object object, limit, position;
3227 EMACS_INT charpos, bytepos;
3229 if (STRINGP (it->string))
3231 /* Strings are usually short, so don't limit the search for
3232 properties. */
3233 it->stop_charpos = it->end_charpos;
3234 object = it->string;
3235 limit = Qnil;
3236 charpos = IT_STRING_CHARPOS (*it);
3237 bytepos = IT_STRING_BYTEPOS (*it);
3239 else
3241 EMACS_INT pos;
3243 /* If end_charpos is out of range for some reason, such as a
3244 misbehaving display function, rationalize it (Bug#5984). */
3245 if (it->end_charpos > ZV)
3246 it->end_charpos = ZV;
3247 it->stop_charpos = it->end_charpos;
3249 /* If next overlay change is in front of the current stop pos
3250 (which is IT->end_charpos), stop there. Note: value of
3251 next_overlay_change is point-max if no overlay change
3252 follows. */
3253 charpos = IT_CHARPOS (*it);
3254 bytepos = IT_BYTEPOS (*it);
3255 pos = next_overlay_change (charpos);
3256 if (pos < it->stop_charpos)
3257 it->stop_charpos = pos;
3259 /* If showing the region, we have to stop at the region
3260 start or end because the face might change there. */
3261 if (it->region_beg_charpos > 0)
3263 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3264 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3265 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3266 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3269 /* Set up variables for computing the stop position from text
3270 property changes. */
3271 XSETBUFFER (object, current_buffer);
3272 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3275 /* Get the interval containing IT's position. Value is a null
3276 interval if there isn't such an interval. */
3277 position = make_number (charpos);
3278 iv = validate_interval_range (object, &position, &position, 0);
3279 if (!NULL_INTERVAL_P (iv))
3281 Lisp_Object values_here[LAST_PROP_IDX];
3282 struct props *p;
3284 /* Get properties here. */
3285 for (p = it_props; p->handler; ++p)
3286 values_here[p->idx] = textget (iv->plist, *p->name);
3288 /* Look for an interval following iv that has different
3289 properties. */
3290 for (next_iv = next_interval (iv);
3291 (!NULL_INTERVAL_P (next_iv)
3292 && (NILP (limit)
3293 || XFASTINT (limit) > next_iv->position));
3294 next_iv = next_interval (next_iv))
3296 for (p = it_props; p->handler; ++p)
3298 Lisp_Object new_value;
3300 new_value = textget (next_iv->plist, *p->name);
3301 if (!EQ (values_here[p->idx], new_value))
3302 break;
3305 if (p->handler)
3306 break;
3309 if (!NULL_INTERVAL_P (next_iv))
3311 if (INTEGERP (limit)
3312 && next_iv->position >= XFASTINT (limit))
3313 /* No text property change up to limit. */
3314 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3315 else
3316 /* Text properties change in next_iv. */
3317 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3321 if (it->cmp_it.id < 0)
3323 EMACS_INT stoppos = it->end_charpos;
3325 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3326 stoppos = -1;
3327 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3328 stoppos, it->string);
3331 xassert (STRINGP (it->string)
3332 || (it->stop_charpos >= BEGV
3333 && it->stop_charpos >= IT_CHARPOS (*it)));
3337 /* Return the position of the next overlay change after POS in
3338 current_buffer. Value is point-max if no overlay change
3339 follows. This is like `next-overlay-change' but doesn't use
3340 xmalloc. */
3342 static EMACS_INT
3343 next_overlay_change (EMACS_INT pos)
3345 ptrdiff_t i, noverlays;
3346 EMACS_INT endpos;
3347 Lisp_Object *overlays;
3349 /* Get all overlays at the given position. */
3350 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3352 /* If any of these overlays ends before endpos,
3353 use its ending point instead. */
3354 for (i = 0; i < noverlays; ++i)
3356 Lisp_Object oend;
3357 EMACS_INT oendpos;
3359 oend = OVERLAY_END (overlays[i]);
3360 oendpos = OVERLAY_POSITION (oend);
3361 endpos = min (endpos, oendpos);
3364 return endpos;
3367 /* How many characters forward to search for a display property or
3368 display string. Searching too far forward makes the bidi display
3369 sluggish, especially in small windows. */
3370 #define MAX_DISP_SCAN 250
3372 /* Return the character position of a display string at or after
3373 position specified by POSITION. If no display string exists at or
3374 after POSITION, return ZV. A display string is either an overlay
3375 with `display' property whose value is a string, or a `display'
3376 text property whose value is a string. STRING is data about the
3377 string to iterate; if STRING->lstring is nil, we are iterating a
3378 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3379 on a GUI frame. DISP_PROP is set to zero if we searched
3380 MAX_DISP_SCAN characters forward without finding any display
3381 strings, non-zero otherwise. It is set to 2 if the display string
3382 uses any kind of `(space ...)' spec that will produce a stretch of
3383 white space in the text area. */
3384 EMACS_INT
3385 compute_display_string_pos (struct text_pos *position,
3386 struct bidi_string_data *string,
3387 int frame_window_p, int *disp_prop)
3389 /* OBJECT = nil means current buffer. */
3390 Lisp_Object object =
3391 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3392 Lisp_Object pos, spec, limpos;
3393 int string_p = (string && (STRINGP (string->lstring) || string->s));
3394 EMACS_INT eob = string_p ? string->schars : ZV;
3395 EMACS_INT begb = string_p ? 0 : BEGV;
3396 EMACS_INT bufpos, charpos = CHARPOS (*position);
3397 EMACS_INT lim =
3398 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3399 struct text_pos tpos;
3400 int rv = 0;
3402 *disp_prop = 1;
3404 if (charpos >= eob
3405 /* We don't support display properties whose values are strings
3406 that have display string properties. */
3407 || string->from_disp_str
3408 /* C strings cannot have display properties. */
3409 || (string->s && !STRINGP (object)))
3411 *disp_prop = 0;
3412 return eob;
3415 /* If the character at CHARPOS is where the display string begins,
3416 return CHARPOS. */
3417 pos = make_number (charpos);
3418 if (STRINGP (object))
3419 bufpos = string->bufpos;
3420 else
3421 bufpos = charpos;
3422 tpos = *position;
3423 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3424 && (charpos <= begb
3425 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3426 object),
3427 spec))
3428 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3429 frame_window_p)))
3431 if (rv == 2)
3432 *disp_prop = 2;
3433 return charpos;
3436 /* Look forward for the first character with a `display' property
3437 that will replace the underlying text when displayed. */
3438 limpos = make_number (lim);
3439 do {
3440 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3441 CHARPOS (tpos) = XFASTINT (pos);
3442 if (CHARPOS (tpos) >= lim)
3444 *disp_prop = 0;
3445 break;
3447 if (STRINGP (object))
3448 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3449 else
3450 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3451 spec = Fget_char_property (pos, Qdisplay, object);
3452 if (!STRINGP (object))
3453 bufpos = CHARPOS (tpos);
3454 } while (NILP (spec)
3455 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3456 bufpos, frame_window_p)));
3457 if (rv == 2)
3458 *disp_prop = 2;
3460 return CHARPOS (tpos);
3463 /* Return the character position of the end of the display string that
3464 started at CHARPOS. If there's no display string at CHARPOS,
3465 return -1. A display string is either an overlay with `display'
3466 property whose value is a string or a `display' text property whose
3467 value is a string. */
3468 EMACS_INT
3469 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3471 /* OBJECT = nil means current buffer. */
3472 Lisp_Object object =
3473 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3474 Lisp_Object pos = make_number (charpos);
3475 EMACS_INT eob =
3476 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3478 if (charpos >= eob || (string->s && !STRINGP (object)))
3479 return eob;
3481 /* It could happen that the display property or overlay was removed
3482 since we found it in compute_display_string_pos above. One way
3483 this can happen is if JIT font-lock was called (through
3484 handle_fontified_prop), and jit-lock-functions remove text
3485 properties or overlays from the portion of buffer that includes
3486 CHARPOS. Muse mode is known to do that, for example. In this
3487 case, we return -1 to the caller, to signal that no display
3488 string is actually present at CHARPOS. See bidi_fetch_char for
3489 how this is handled.
3491 An alternative would be to never look for display properties past
3492 it->stop_charpos. But neither compute_display_string_pos nor
3493 bidi_fetch_char that calls it know or care where the next
3494 stop_charpos is. */
3495 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3496 return -1;
3498 /* Look forward for the first character where the `display' property
3499 changes. */
3500 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3502 return XFASTINT (pos);
3507 /***********************************************************************
3508 Fontification
3509 ***********************************************************************/
3511 /* Handle changes in the `fontified' property of the current buffer by
3512 calling hook functions from Qfontification_functions to fontify
3513 regions of text. */
3515 static enum prop_handled
3516 handle_fontified_prop (struct it *it)
3518 Lisp_Object prop, pos;
3519 enum prop_handled handled = HANDLED_NORMALLY;
3521 if (!NILP (Vmemory_full))
3522 return handled;
3524 /* Get the value of the `fontified' property at IT's current buffer
3525 position. (The `fontified' property doesn't have a special
3526 meaning in strings.) If the value is nil, call functions from
3527 Qfontification_functions. */
3528 if (!STRINGP (it->string)
3529 && it->s == NULL
3530 && !NILP (Vfontification_functions)
3531 && !NILP (Vrun_hooks)
3532 && (pos = make_number (IT_CHARPOS (*it)),
3533 prop = Fget_char_property (pos, Qfontified, Qnil),
3534 /* Ignore the special cased nil value always present at EOB since
3535 no amount of fontifying will be able to change it. */
3536 NILP (prop) && IT_CHARPOS (*it) < Z))
3538 int count = SPECPDL_INDEX ();
3539 Lisp_Object val;
3540 struct buffer *obuf = current_buffer;
3541 int begv = BEGV, zv = ZV;
3542 int old_clip_changed = current_buffer->clip_changed;
3544 val = Vfontification_functions;
3545 specbind (Qfontification_functions, Qnil);
3547 xassert (it->end_charpos == ZV);
3549 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3550 safe_call1 (val, pos);
3551 else
3553 Lisp_Object fns, fn;
3554 struct gcpro gcpro1, gcpro2;
3556 fns = Qnil;
3557 GCPRO2 (val, fns);
3559 for (; CONSP (val); val = XCDR (val))
3561 fn = XCAR (val);
3563 if (EQ (fn, Qt))
3565 /* A value of t indicates this hook has a local
3566 binding; it means to run the global binding too.
3567 In a global value, t should not occur. If it
3568 does, we must ignore it to avoid an endless
3569 loop. */
3570 for (fns = Fdefault_value (Qfontification_functions);
3571 CONSP (fns);
3572 fns = XCDR (fns))
3574 fn = XCAR (fns);
3575 if (!EQ (fn, Qt))
3576 safe_call1 (fn, pos);
3579 else
3580 safe_call1 (fn, pos);
3583 UNGCPRO;
3586 unbind_to (count, Qnil);
3588 /* Fontification functions routinely call `save-restriction'.
3589 Normally, this tags clip_changed, which can confuse redisplay
3590 (see discussion in Bug#6671). Since we don't perform any
3591 special handling of fontification changes in the case where
3592 `save-restriction' isn't called, there's no point doing so in
3593 this case either. So, if the buffer's restrictions are
3594 actually left unchanged, reset clip_changed. */
3595 if (obuf == current_buffer)
3597 if (begv == BEGV && zv == ZV)
3598 current_buffer->clip_changed = old_clip_changed;
3600 /* There isn't much we can reasonably do to protect against
3601 misbehaving fontification, but here's a fig leaf. */
3602 else if (!NILP (BVAR (obuf, name)))
3603 set_buffer_internal_1 (obuf);
3605 /* The fontification code may have added/removed text.
3606 It could do even a lot worse, but let's at least protect against
3607 the most obvious case where only the text past `pos' gets changed',
3608 as is/was done in grep.el where some escapes sequences are turned
3609 into face properties (bug#7876). */
3610 it->end_charpos = ZV;
3612 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3613 something. This avoids an endless loop if they failed to
3614 fontify the text for which reason ever. */
3615 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3616 handled = HANDLED_RECOMPUTE_PROPS;
3619 return handled;
3624 /***********************************************************************
3625 Faces
3626 ***********************************************************************/
3628 /* Set up iterator IT from face properties at its current position.
3629 Called from handle_stop. */
3631 static enum prop_handled
3632 handle_face_prop (struct it *it)
3634 int new_face_id;
3635 EMACS_INT next_stop;
3637 if (!STRINGP (it->string))
3639 new_face_id
3640 = face_at_buffer_position (it->w,
3641 IT_CHARPOS (*it),
3642 it->region_beg_charpos,
3643 it->region_end_charpos,
3644 &next_stop,
3645 (IT_CHARPOS (*it)
3646 + TEXT_PROP_DISTANCE_LIMIT),
3647 0, it->base_face_id);
3649 /* Is this a start of a run of characters with box face?
3650 Caveat: this can be called for a freshly initialized
3651 iterator; face_id is -1 in this case. We know that the new
3652 face will not change until limit, i.e. if the new face has a
3653 box, all characters up to limit will have one. But, as
3654 usual, we don't know whether limit is really the end. */
3655 if (new_face_id != it->face_id)
3657 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3659 /* If new face has a box but old face has not, this is
3660 the start of a run of characters with box, i.e. it has
3661 a shadow on the left side. The value of face_id of the
3662 iterator will be -1 if this is the initial call that gets
3663 the face. In this case, we have to look in front of IT's
3664 position and see whether there is a face != new_face_id. */
3665 it->start_of_box_run_p
3666 = (new_face->box != FACE_NO_BOX
3667 && (it->face_id >= 0
3668 || IT_CHARPOS (*it) == BEG
3669 || new_face_id != face_before_it_pos (it)));
3670 it->face_box_p = new_face->box != FACE_NO_BOX;
3673 else
3675 int base_face_id;
3676 EMACS_INT bufpos;
3677 int i;
3678 Lisp_Object from_overlay
3679 = (it->current.overlay_string_index >= 0
3680 ? it->string_overlays[it->current.overlay_string_index]
3681 : Qnil);
3683 /* See if we got to this string directly or indirectly from
3684 an overlay property. That includes the before-string or
3685 after-string of an overlay, strings in display properties
3686 provided by an overlay, their text properties, etc.
3688 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3689 if (! NILP (from_overlay))
3690 for (i = it->sp - 1; i >= 0; i--)
3692 if (it->stack[i].current.overlay_string_index >= 0)
3693 from_overlay
3694 = it->string_overlays[it->stack[i].current.overlay_string_index];
3695 else if (! NILP (it->stack[i].from_overlay))
3696 from_overlay = it->stack[i].from_overlay;
3698 if (!NILP (from_overlay))
3699 break;
3702 if (! NILP (from_overlay))
3704 bufpos = IT_CHARPOS (*it);
3705 /* For a string from an overlay, the base face depends
3706 only on text properties and ignores overlays. */
3707 base_face_id
3708 = face_for_overlay_string (it->w,
3709 IT_CHARPOS (*it),
3710 it->region_beg_charpos,
3711 it->region_end_charpos,
3712 &next_stop,
3713 (IT_CHARPOS (*it)
3714 + TEXT_PROP_DISTANCE_LIMIT),
3716 from_overlay);
3718 else
3720 bufpos = 0;
3722 /* For strings from a `display' property, use the face at
3723 IT's current buffer position as the base face to merge
3724 with, so that overlay strings appear in the same face as
3725 surrounding text, unless they specify their own
3726 faces. */
3727 base_face_id = it->string_from_prefix_prop_p
3728 ? DEFAULT_FACE_ID
3729 : underlying_face_id (it);
3732 new_face_id = face_at_string_position (it->w,
3733 it->string,
3734 IT_STRING_CHARPOS (*it),
3735 bufpos,
3736 it->region_beg_charpos,
3737 it->region_end_charpos,
3738 &next_stop,
3739 base_face_id, 0);
3741 /* Is this a start of a run of characters with box? Caveat:
3742 this can be called for a freshly allocated iterator; face_id
3743 is -1 is this case. We know that the new face will not
3744 change until the next check pos, i.e. if the new face has a
3745 box, all characters up to that position will have a
3746 box. But, as usual, we don't know whether that position
3747 is really the end. */
3748 if (new_face_id != it->face_id)
3750 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3751 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3753 /* If new face has a box but old face hasn't, this is the
3754 start of a run of characters with box, i.e. it has a
3755 shadow on the left side. */
3756 it->start_of_box_run_p
3757 = new_face->box && (old_face == NULL || !old_face->box);
3758 it->face_box_p = new_face->box != FACE_NO_BOX;
3762 it->face_id = new_face_id;
3763 return HANDLED_NORMALLY;
3767 /* Return the ID of the face ``underlying'' IT's current position,
3768 which is in a string. If the iterator is associated with a
3769 buffer, return the face at IT's current buffer position.
3770 Otherwise, use the iterator's base_face_id. */
3772 static int
3773 underlying_face_id (struct it *it)
3775 int face_id = it->base_face_id, i;
3777 xassert (STRINGP (it->string));
3779 for (i = it->sp - 1; i >= 0; --i)
3780 if (NILP (it->stack[i].string))
3781 face_id = it->stack[i].face_id;
3783 return face_id;
3787 /* Compute the face one character before or after the current position
3788 of IT, in the visual order. BEFORE_P non-zero means get the face
3789 in front (to the left in L2R paragraphs, to the right in R2L
3790 paragraphs) of IT's screen position. Value is the ID of the face. */
3792 static int
3793 face_before_or_after_it_pos (struct it *it, int before_p)
3795 int face_id, limit;
3796 EMACS_INT next_check_charpos;
3797 struct it it_copy;
3798 void *it_copy_data = NULL;
3800 xassert (it->s == NULL);
3802 if (STRINGP (it->string))
3804 EMACS_INT bufpos, charpos;
3805 int base_face_id;
3807 /* No face change past the end of the string (for the case
3808 we are padding with spaces). No face change before the
3809 string start. */
3810 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3811 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3812 return it->face_id;
3814 if (!it->bidi_p)
3816 /* Set charpos to the position before or after IT's current
3817 position, in the logical order, which in the non-bidi
3818 case is the same as the visual order. */
3819 if (before_p)
3820 charpos = IT_STRING_CHARPOS (*it) - 1;
3821 else if (it->what == IT_COMPOSITION)
3822 /* For composition, we must check the character after the
3823 composition. */
3824 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3825 else
3826 charpos = IT_STRING_CHARPOS (*it) + 1;
3828 else
3830 if (before_p)
3832 /* With bidi iteration, the character before the current
3833 in the visual order cannot be found by simple
3834 iteration, because "reverse" reordering is not
3835 supported. Instead, we need to use the move_it_*
3836 family of functions. */
3837 /* Ignore face changes before the first visible
3838 character on this display line. */
3839 if (it->current_x <= it->first_visible_x)
3840 return it->face_id;
3841 SAVE_IT (it_copy, *it, it_copy_data);
3842 /* Implementation note: Since move_it_in_display_line
3843 works in the iterator geometry, and thinks the first
3844 character is always the leftmost, even in R2L lines,
3845 we don't need to distinguish between the R2L and L2R
3846 cases here. */
3847 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3848 it_copy.current_x - 1, MOVE_TO_X);
3849 charpos = IT_STRING_CHARPOS (it_copy);
3850 RESTORE_IT (it, it, it_copy_data);
3852 else
3854 /* Set charpos to the string position of the character
3855 that comes after IT's current position in the visual
3856 order. */
3857 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3859 it_copy = *it;
3860 while (n--)
3861 bidi_move_to_visually_next (&it_copy.bidi_it);
3863 charpos = it_copy.bidi_it.charpos;
3866 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3868 if (it->current.overlay_string_index >= 0)
3869 bufpos = IT_CHARPOS (*it);
3870 else
3871 bufpos = 0;
3873 base_face_id = underlying_face_id (it);
3875 /* Get the face for ASCII, or unibyte. */
3876 face_id = face_at_string_position (it->w,
3877 it->string,
3878 charpos,
3879 bufpos,
3880 it->region_beg_charpos,
3881 it->region_end_charpos,
3882 &next_check_charpos,
3883 base_face_id, 0);
3885 /* Correct the face for charsets different from ASCII. Do it
3886 for the multibyte case only. The face returned above is
3887 suitable for unibyte text if IT->string is unibyte. */
3888 if (STRING_MULTIBYTE (it->string))
3890 struct text_pos pos1 = string_pos (charpos, it->string);
3891 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3892 int c, len;
3893 struct face *face = FACE_FROM_ID (it->f, face_id);
3895 c = string_char_and_length (p, &len);
3896 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3899 else
3901 struct text_pos pos;
3903 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3904 || (IT_CHARPOS (*it) <= BEGV && before_p))
3905 return it->face_id;
3907 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3908 pos = it->current.pos;
3910 if (!it->bidi_p)
3912 if (before_p)
3913 DEC_TEXT_POS (pos, it->multibyte_p);
3914 else
3916 if (it->what == IT_COMPOSITION)
3918 /* For composition, we must check the position after
3919 the composition. */
3920 pos.charpos += it->cmp_it.nchars;
3921 pos.bytepos += it->len;
3923 else
3924 INC_TEXT_POS (pos, it->multibyte_p);
3927 else
3929 if (before_p)
3931 /* With bidi iteration, the character before the current
3932 in the visual order cannot be found by simple
3933 iteration, because "reverse" reordering is not
3934 supported. Instead, we need to use the move_it_*
3935 family of functions. */
3936 /* Ignore face changes before the first visible
3937 character on this display line. */
3938 if (it->current_x <= it->first_visible_x)
3939 return it->face_id;
3940 SAVE_IT (it_copy, *it, it_copy_data);
3941 /* Implementation note: Since move_it_in_display_line
3942 works in the iterator geometry, and thinks the first
3943 character is always the leftmost, even in R2L lines,
3944 we don't need to distinguish between the R2L and L2R
3945 cases here. */
3946 move_it_in_display_line (&it_copy, ZV,
3947 it_copy.current_x - 1, MOVE_TO_X);
3948 pos = it_copy.current.pos;
3949 RESTORE_IT (it, it, it_copy_data);
3951 else
3953 /* Set charpos to the buffer position of the character
3954 that comes after IT's current position in the visual
3955 order. */
3956 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3958 it_copy = *it;
3959 while (n--)
3960 bidi_move_to_visually_next (&it_copy.bidi_it);
3962 SET_TEXT_POS (pos,
3963 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3966 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3968 /* Determine face for CHARSET_ASCII, or unibyte. */
3969 face_id = face_at_buffer_position (it->w,
3970 CHARPOS (pos),
3971 it->region_beg_charpos,
3972 it->region_end_charpos,
3973 &next_check_charpos,
3974 limit, 0, -1);
3976 /* Correct the face for charsets different from ASCII. Do it
3977 for the multibyte case only. The face returned above is
3978 suitable for unibyte text if current_buffer is unibyte. */
3979 if (it->multibyte_p)
3981 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3982 struct face *face = FACE_FROM_ID (it->f, face_id);
3983 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3987 return face_id;
3992 /***********************************************************************
3993 Invisible text
3994 ***********************************************************************/
3996 /* Set up iterator IT from invisible properties at its current
3997 position. Called from handle_stop. */
3999 static enum prop_handled
4000 handle_invisible_prop (struct it *it)
4002 enum prop_handled handled = HANDLED_NORMALLY;
4004 if (STRINGP (it->string))
4006 Lisp_Object prop, end_charpos, limit, charpos;
4008 /* Get the value of the invisible text property at the
4009 current position. Value will be nil if there is no such
4010 property. */
4011 charpos = make_number (IT_STRING_CHARPOS (*it));
4012 prop = Fget_text_property (charpos, Qinvisible, it->string);
4014 if (!NILP (prop)
4015 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4017 EMACS_INT endpos;
4019 handled = HANDLED_RECOMPUTE_PROPS;
4021 /* Get the position at which the next change of the
4022 invisible text property can be found in IT->string.
4023 Value will be nil if the property value is the same for
4024 all the rest of IT->string. */
4025 XSETINT (limit, SCHARS (it->string));
4026 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4027 it->string, limit);
4029 /* Text at current position is invisible. The next
4030 change in the property is at position end_charpos.
4031 Move IT's current position to that position. */
4032 if (INTEGERP (end_charpos)
4033 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4035 struct text_pos old;
4036 EMACS_INT oldpos;
4038 old = it->current.string_pos;
4039 oldpos = CHARPOS (old);
4040 if (it->bidi_p)
4042 if (it->bidi_it.first_elt
4043 && it->bidi_it.charpos < SCHARS (it->string))
4044 bidi_paragraph_init (it->paragraph_embedding,
4045 &it->bidi_it, 1);
4046 /* Bidi-iterate out of the invisible text. */
4049 bidi_move_to_visually_next (&it->bidi_it);
4051 while (oldpos <= it->bidi_it.charpos
4052 && it->bidi_it.charpos < endpos);
4054 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4055 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4056 if (IT_CHARPOS (*it) >= endpos)
4057 it->prev_stop = endpos;
4059 else
4061 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4062 compute_string_pos (&it->current.string_pos, old, it->string);
4065 else
4067 /* The rest of the string is invisible. If this is an
4068 overlay string, proceed with the next overlay string
4069 or whatever comes and return a character from there. */
4070 if (it->current.overlay_string_index >= 0)
4072 next_overlay_string (it);
4073 /* Don't check for overlay strings when we just
4074 finished processing them. */
4075 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4077 else
4079 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4080 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4085 else
4087 int invis_p;
4088 EMACS_INT newpos, next_stop, start_charpos, tem;
4089 Lisp_Object pos, prop, overlay;
4091 /* First of all, is there invisible text at this position? */
4092 tem = start_charpos = IT_CHARPOS (*it);
4093 pos = make_number (tem);
4094 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4095 &overlay);
4096 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4098 /* If we are on invisible text, skip over it. */
4099 if (invis_p && start_charpos < it->end_charpos)
4101 /* Record whether we have to display an ellipsis for the
4102 invisible text. */
4103 int display_ellipsis_p = invis_p == 2;
4105 handled = HANDLED_RECOMPUTE_PROPS;
4107 /* Loop skipping over invisible text. The loop is left at
4108 ZV or with IT on the first char being visible again. */
4111 /* Try to skip some invisible text. Return value is the
4112 position reached which can be equal to where we start
4113 if there is nothing invisible there. This skips both
4114 over invisible text properties and overlays with
4115 invisible property. */
4116 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4118 /* If we skipped nothing at all we weren't at invisible
4119 text in the first place. If everything to the end of
4120 the buffer was skipped, end the loop. */
4121 if (newpos == tem || newpos >= ZV)
4122 invis_p = 0;
4123 else
4125 /* We skipped some characters but not necessarily
4126 all there are. Check if we ended up on visible
4127 text. Fget_char_property returns the property of
4128 the char before the given position, i.e. if we
4129 get invis_p = 0, this means that the char at
4130 newpos is visible. */
4131 pos = make_number (newpos);
4132 prop = Fget_char_property (pos, Qinvisible, it->window);
4133 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 /* If we ended up on invisible text, proceed to
4137 skip starting with next_stop. */
4138 if (invis_p)
4139 tem = next_stop;
4141 /* If there are adjacent invisible texts, don't lose the
4142 second one's ellipsis. */
4143 if (invis_p == 2)
4144 display_ellipsis_p = 1;
4146 while (invis_p);
4148 /* The position newpos is now either ZV or on visible text. */
4149 if (it->bidi_p)
4151 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4152 int on_newline =
4153 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4154 int after_newline =
4155 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4157 /* If the invisible text ends on a newline or on a
4158 character after a newline, we can avoid the costly,
4159 character by character, bidi iteration to NEWPOS, and
4160 instead simply reseat the iterator there. That's
4161 because all bidi reordering information is tossed at
4162 the newline. This is a big win for modes that hide
4163 complete lines, like Outline, Org, etc. */
4164 if (on_newline || after_newline)
4166 struct text_pos tpos;
4167 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4169 SET_TEXT_POS (tpos, newpos, bpos);
4170 reseat_1 (it, tpos, 0);
4171 /* If we reseat on a newline/ZV, we need to prep the
4172 bidi iterator for advancing to the next character
4173 after the newline/EOB, keeping the current paragraph
4174 direction (so that PRODUCE_GLYPHS does TRT wrt
4175 prepending/appending glyphs to a glyph row). */
4176 if (on_newline)
4178 it->bidi_it.first_elt = 0;
4179 it->bidi_it.paragraph_dir = pdir;
4180 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4181 it->bidi_it.nchars = 1;
4182 it->bidi_it.ch_len = 1;
4185 else /* Must use the slow method. */
4187 /* With bidi iteration, the region of invisible text
4188 could start and/or end in the middle of a
4189 non-base embedding level. Therefore, we need to
4190 skip invisible text using the bidi iterator,
4191 starting at IT's current position, until we find
4192 ourselves outside of the invisible text.
4193 Skipping invisible text _after_ bidi iteration
4194 avoids affecting the visual order of the
4195 displayed text when invisible properties are
4196 added or removed. */
4197 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4199 /* If we were `reseat'ed to a new paragraph,
4200 determine the paragraph base direction. We
4201 need to do it now because
4202 next_element_from_buffer may not have a
4203 chance to do it, if we are going to skip any
4204 text at the beginning, which resets the
4205 FIRST_ELT flag. */
4206 bidi_paragraph_init (it->paragraph_embedding,
4207 &it->bidi_it, 1);
4211 bidi_move_to_visually_next (&it->bidi_it);
4213 while (it->stop_charpos <= it->bidi_it.charpos
4214 && it->bidi_it.charpos < newpos);
4215 IT_CHARPOS (*it) = it->bidi_it.charpos;
4216 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4217 /* If we overstepped NEWPOS, record its position in
4218 the iterator, so that we skip invisible text if
4219 later the bidi iteration lands us in the
4220 invisible region again. */
4221 if (IT_CHARPOS (*it) >= newpos)
4222 it->prev_stop = newpos;
4225 else
4227 IT_CHARPOS (*it) = newpos;
4228 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4231 /* If there are before-strings at the start of invisible
4232 text, and the text is invisible because of a text
4233 property, arrange to show before-strings because 20.x did
4234 it that way. (If the text is invisible because of an
4235 overlay property instead of a text property, this is
4236 already handled in the overlay code.) */
4237 if (NILP (overlay)
4238 && get_overlay_strings (it, it->stop_charpos))
4240 handled = HANDLED_RECOMPUTE_PROPS;
4241 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4243 else if (display_ellipsis_p)
4245 /* Make sure that the glyphs of the ellipsis will get
4246 correct `charpos' values. If we would not update
4247 it->position here, the glyphs would belong to the
4248 last visible character _before_ the invisible
4249 text, which confuses `set_cursor_from_row'.
4251 We use the last invisible position instead of the
4252 first because this way the cursor is always drawn on
4253 the first "." of the ellipsis, whenever PT is inside
4254 the invisible text. Otherwise the cursor would be
4255 placed _after_ the ellipsis when the point is after the
4256 first invisible character. */
4257 if (!STRINGP (it->object))
4259 it->position.charpos = newpos - 1;
4260 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4262 it->ellipsis_p = 1;
4263 /* Let the ellipsis display before
4264 considering any properties of the following char.
4265 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4266 handled = HANDLED_RETURN;
4271 return handled;
4275 /* Make iterator IT return `...' next.
4276 Replaces LEN characters from buffer. */
4278 static void
4279 setup_for_ellipsis (struct it *it, int len)
4281 /* Use the display table definition for `...'. Invalid glyphs
4282 will be handled by the method returning elements from dpvec. */
4283 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4285 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4286 it->dpvec = v->contents;
4287 it->dpend = v->contents + v->header.size;
4289 else
4291 /* Default `...'. */
4292 it->dpvec = default_invis_vector;
4293 it->dpend = default_invis_vector + 3;
4296 it->dpvec_char_len = len;
4297 it->current.dpvec_index = 0;
4298 it->dpvec_face_id = -1;
4300 /* Remember the current face id in case glyphs specify faces.
4301 IT's face is restored in set_iterator_to_next.
4302 saved_face_id was set to preceding char's face in handle_stop. */
4303 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4304 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4306 it->method = GET_FROM_DISPLAY_VECTOR;
4307 it->ellipsis_p = 1;
4312 /***********************************************************************
4313 'display' property
4314 ***********************************************************************/
4316 /* Set up iterator IT from `display' property at its current position.
4317 Called from handle_stop.
4318 We return HANDLED_RETURN if some part of the display property
4319 overrides the display of the buffer text itself.
4320 Otherwise we return HANDLED_NORMALLY. */
4322 static enum prop_handled
4323 handle_display_prop (struct it *it)
4325 Lisp_Object propval, object, overlay;
4326 struct text_pos *position;
4327 EMACS_INT bufpos;
4328 /* Nonzero if some property replaces the display of the text itself. */
4329 int display_replaced_p = 0;
4331 if (STRINGP (it->string))
4333 object = it->string;
4334 position = &it->current.string_pos;
4335 bufpos = CHARPOS (it->current.pos);
4337 else
4339 XSETWINDOW (object, it->w);
4340 position = &it->current.pos;
4341 bufpos = CHARPOS (*position);
4344 /* Reset those iterator values set from display property values. */
4345 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4346 it->space_width = Qnil;
4347 it->font_height = Qnil;
4348 it->voffset = 0;
4350 /* We don't support recursive `display' properties, i.e. string
4351 values that have a string `display' property, that have a string
4352 `display' property etc. */
4353 if (!it->string_from_display_prop_p)
4354 it->area = TEXT_AREA;
4356 propval = get_char_property_and_overlay (make_number (position->charpos),
4357 Qdisplay, object, &overlay);
4358 if (NILP (propval))
4359 return HANDLED_NORMALLY;
4360 /* Now OVERLAY is the overlay that gave us this property, or nil
4361 if it was a text property. */
4363 if (!STRINGP (it->string))
4364 object = it->w->buffer;
4366 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4367 position, bufpos,
4368 FRAME_WINDOW_P (it->f));
4370 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4373 /* Subroutine of handle_display_prop. Returns non-zero if the display
4374 specification in SPEC is a replacing specification, i.e. it would
4375 replace the text covered by `display' property with something else,
4376 such as an image or a display string. If SPEC includes any kind or
4377 `(space ...) specification, the value is 2; this is used by
4378 compute_display_string_pos, which see.
4380 See handle_single_display_spec for documentation of arguments.
4381 frame_window_p is non-zero if the window being redisplayed is on a
4382 GUI frame; this argument is used only if IT is NULL, see below.
4384 IT can be NULL, if this is called by the bidi reordering code
4385 through compute_display_string_pos, which see. In that case, this
4386 function only examines SPEC, but does not otherwise "handle" it, in
4387 the sense that it doesn't set up members of IT from the display
4388 spec. */
4389 static int
4390 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4391 Lisp_Object overlay, struct text_pos *position,
4392 EMACS_INT bufpos, int frame_window_p)
4394 int replacing_p = 0;
4395 int rv;
4397 if (CONSP (spec)
4398 /* Simple specifications. */
4399 && !EQ (XCAR (spec), Qimage)
4400 && !EQ (XCAR (spec), Qspace)
4401 && !EQ (XCAR (spec), Qwhen)
4402 && !EQ (XCAR (spec), Qslice)
4403 && !EQ (XCAR (spec), Qspace_width)
4404 && !EQ (XCAR (spec), Qheight)
4405 && !EQ (XCAR (spec), Qraise)
4406 /* Marginal area specifications. */
4407 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4408 && !EQ (XCAR (spec), Qleft_fringe)
4409 && !EQ (XCAR (spec), Qright_fringe)
4410 && !NILP (XCAR (spec)))
4412 for (; CONSP (spec); spec = XCDR (spec))
4414 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4415 overlay, position, bufpos,
4416 replacing_p, frame_window_p)))
4418 replacing_p = rv;
4419 /* If some text in a string is replaced, `position' no
4420 longer points to the position of `object'. */
4421 if (!it || STRINGP (object))
4422 break;
4426 else if (VECTORP (spec))
4428 int i;
4429 for (i = 0; i < ASIZE (spec); ++i)
4430 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4431 overlay, position, bufpos,
4432 replacing_p, frame_window_p)))
4434 replacing_p = rv;
4435 /* If some text in a string is replaced, `position' no
4436 longer points to the position of `object'. */
4437 if (!it || STRINGP (object))
4438 break;
4441 else
4443 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4444 position, bufpos, 0,
4445 frame_window_p)))
4446 replacing_p = rv;
4449 return replacing_p;
4452 /* Value is the position of the end of the `display' property starting
4453 at START_POS in OBJECT. */
4455 static struct text_pos
4456 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4458 Lisp_Object end;
4459 struct text_pos end_pos;
4461 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4462 Qdisplay, object, Qnil);
4463 CHARPOS (end_pos) = XFASTINT (end);
4464 if (STRINGP (object))
4465 compute_string_pos (&end_pos, start_pos, it->string);
4466 else
4467 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4469 return end_pos;
4473 /* Set up IT from a single `display' property specification SPEC. OBJECT
4474 is the object in which the `display' property was found. *POSITION
4475 is the position in OBJECT at which the `display' property was found.
4476 BUFPOS is the buffer position of OBJECT (different from POSITION if
4477 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4478 previously saw a display specification which already replaced text
4479 display with something else, for example an image; we ignore such
4480 properties after the first one has been processed.
4482 OVERLAY is the overlay this `display' property came from,
4483 or nil if it was a text property.
4485 If SPEC is a `space' or `image' specification, and in some other
4486 cases too, set *POSITION to the position where the `display'
4487 property ends.
4489 If IT is NULL, only examine the property specification in SPEC, but
4490 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4491 is intended to be displayed in a window on a GUI frame.
4493 Value is non-zero if something was found which replaces the display
4494 of buffer or string text. */
4496 static int
4497 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4498 Lisp_Object overlay, struct text_pos *position,
4499 EMACS_INT bufpos, int display_replaced_p,
4500 int frame_window_p)
4502 Lisp_Object form;
4503 Lisp_Object location, value;
4504 struct text_pos start_pos = *position;
4505 int valid_p;
4507 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4508 If the result is non-nil, use VALUE instead of SPEC. */
4509 form = Qt;
4510 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4512 spec = XCDR (spec);
4513 if (!CONSP (spec))
4514 return 0;
4515 form = XCAR (spec);
4516 spec = XCDR (spec);
4519 if (!NILP (form) && !EQ (form, Qt))
4521 int count = SPECPDL_INDEX ();
4522 struct gcpro gcpro1;
4524 /* Bind `object' to the object having the `display' property, a
4525 buffer or string. Bind `position' to the position in the
4526 object where the property was found, and `buffer-position'
4527 to the current position in the buffer. */
4529 if (NILP (object))
4530 XSETBUFFER (object, current_buffer);
4531 specbind (Qobject, object);
4532 specbind (Qposition, make_number (CHARPOS (*position)));
4533 specbind (Qbuffer_position, make_number (bufpos));
4534 GCPRO1 (form);
4535 form = safe_eval (form);
4536 UNGCPRO;
4537 unbind_to (count, Qnil);
4540 if (NILP (form))
4541 return 0;
4543 /* Handle `(height HEIGHT)' specifications. */
4544 if (CONSP (spec)
4545 && EQ (XCAR (spec), Qheight)
4546 && CONSP (XCDR (spec)))
4548 if (it)
4550 if (!FRAME_WINDOW_P (it->f))
4551 return 0;
4553 it->font_height = XCAR (XCDR (spec));
4554 if (!NILP (it->font_height))
4556 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4557 int new_height = -1;
4559 if (CONSP (it->font_height)
4560 && (EQ (XCAR (it->font_height), Qplus)
4561 || EQ (XCAR (it->font_height), Qminus))
4562 && CONSP (XCDR (it->font_height))
4563 && INTEGERP (XCAR (XCDR (it->font_height))))
4565 /* `(+ N)' or `(- N)' where N is an integer. */
4566 int steps = XINT (XCAR (XCDR (it->font_height)));
4567 if (EQ (XCAR (it->font_height), Qplus))
4568 steps = - steps;
4569 it->face_id = smaller_face (it->f, it->face_id, steps);
4571 else if (FUNCTIONP (it->font_height))
4573 /* Call function with current height as argument.
4574 Value is the new height. */
4575 Lisp_Object height;
4576 height = safe_call1 (it->font_height,
4577 face->lface[LFACE_HEIGHT_INDEX]);
4578 if (NUMBERP (height))
4579 new_height = XFLOATINT (height);
4581 else if (NUMBERP (it->font_height))
4583 /* Value is a multiple of the canonical char height. */
4584 struct face *f;
4586 f = FACE_FROM_ID (it->f,
4587 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4588 new_height = (XFLOATINT (it->font_height)
4589 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4591 else
4593 /* Evaluate IT->font_height with `height' bound to the
4594 current specified height to get the new height. */
4595 int count = SPECPDL_INDEX ();
4597 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4598 value = safe_eval (it->font_height);
4599 unbind_to (count, Qnil);
4601 if (NUMBERP (value))
4602 new_height = XFLOATINT (value);
4605 if (new_height > 0)
4606 it->face_id = face_with_height (it->f, it->face_id, new_height);
4610 return 0;
4613 /* Handle `(space-width WIDTH)'. */
4614 if (CONSP (spec)
4615 && EQ (XCAR (spec), Qspace_width)
4616 && CONSP (XCDR (spec)))
4618 if (it)
4620 if (!FRAME_WINDOW_P (it->f))
4621 return 0;
4623 value = XCAR (XCDR (spec));
4624 if (NUMBERP (value) && XFLOATINT (value) > 0)
4625 it->space_width = value;
4628 return 0;
4631 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4632 if (CONSP (spec)
4633 && EQ (XCAR (spec), Qslice))
4635 Lisp_Object tem;
4637 if (it)
4639 if (!FRAME_WINDOW_P (it->f))
4640 return 0;
4642 if (tem = XCDR (spec), CONSP (tem))
4644 it->slice.x = XCAR (tem);
4645 if (tem = XCDR (tem), CONSP (tem))
4647 it->slice.y = XCAR (tem);
4648 if (tem = XCDR (tem), CONSP (tem))
4650 it->slice.width = XCAR (tem);
4651 if (tem = XCDR (tem), CONSP (tem))
4652 it->slice.height = XCAR (tem);
4658 return 0;
4661 /* Handle `(raise FACTOR)'. */
4662 if (CONSP (spec)
4663 && EQ (XCAR (spec), Qraise)
4664 && CONSP (XCDR (spec)))
4666 if (it)
4668 if (!FRAME_WINDOW_P (it->f))
4669 return 0;
4671 #ifdef HAVE_WINDOW_SYSTEM
4672 value = XCAR (XCDR (spec));
4673 if (NUMBERP (value))
4675 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4676 it->voffset = - (XFLOATINT (value)
4677 * (FONT_HEIGHT (face->font)));
4679 #endif /* HAVE_WINDOW_SYSTEM */
4682 return 0;
4685 /* Don't handle the other kinds of display specifications
4686 inside a string that we got from a `display' property. */
4687 if (it && it->string_from_display_prop_p)
4688 return 0;
4690 /* Characters having this form of property are not displayed, so
4691 we have to find the end of the property. */
4692 if (it)
4694 start_pos = *position;
4695 *position = display_prop_end (it, object, start_pos);
4697 value = Qnil;
4699 /* Stop the scan at that end position--we assume that all
4700 text properties change there. */
4701 if (it)
4702 it->stop_charpos = position->charpos;
4704 /* Handle `(left-fringe BITMAP [FACE])'
4705 and `(right-fringe BITMAP [FACE])'. */
4706 if (CONSP (spec)
4707 && (EQ (XCAR (spec), Qleft_fringe)
4708 || EQ (XCAR (spec), Qright_fringe))
4709 && CONSP (XCDR (spec)))
4711 int fringe_bitmap;
4713 if (it)
4715 if (!FRAME_WINDOW_P (it->f))
4716 /* If we return here, POSITION has been advanced
4717 across the text with this property. */
4719 /* Synchronize the bidi iterator with POSITION. This is
4720 needed because we are not going to push the iterator
4721 on behalf of this display property, so there will be
4722 no pop_it call to do this synchronization for us. */
4723 if (it->bidi_p)
4725 it->position = *position;
4726 iterate_out_of_display_property (it);
4727 *position = it->position;
4729 return 1;
4732 else if (!frame_window_p)
4733 return 1;
4735 #ifdef HAVE_WINDOW_SYSTEM
4736 value = XCAR (XCDR (spec));
4737 if (!SYMBOLP (value)
4738 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4739 /* If we return here, POSITION has been advanced
4740 across the text with this property. */
4742 if (it && it->bidi_p)
4744 it->position = *position;
4745 iterate_out_of_display_property (it);
4746 *position = it->position;
4748 return 1;
4751 if (it)
4753 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4755 if (CONSP (XCDR (XCDR (spec))))
4757 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4758 int face_id2 = lookup_derived_face (it->f, face_name,
4759 FRINGE_FACE_ID, 0);
4760 if (face_id2 >= 0)
4761 face_id = face_id2;
4764 /* Save current settings of IT so that we can restore them
4765 when we are finished with the glyph property value. */
4766 push_it (it, position);
4768 it->area = TEXT_AREA;
4769 it->what = IT_IMAGE;
4770 it->image_id = -1; /* no image */
4771 it->position = start_pos;
4772 it->object = NILP (object) ? it->w->buffer : object;
4773 it->method = GET_FROM_IMAGE;
4774 it->from_overlay = Qnil;
4775 it->face_id = face_id;
4776 it->from_disp_prop_p = 1;
4778 /* Say that we haven't consumed the characters with
4779 `display' property yet. The call to pop_it in
4780 set_iterator_to_next will clean this up. */
4781 *position = start_pos;
4783 if (EQ (XCAR (spec), Qleft_fringe))
4785 it->left_user_fringe_bitmap = fringe_bitmap;
4786 it->left_user_fringe_face_id = face_id;
4788 else
4790 it->right_user_fringe_bitmap = fringe_bitmap;
4791 it->right_user_fringe_face_id = face_id;
4794 #endif /* HAVE_WINDOW_SYSTEM */
4795 return 1;
4798 /* Prepare to handle `((margin left-margin) ...)',
4799 `((margin right-margin) ...)' and `((margin nil) ...)'
4800 prefixes for display specifications. */
4801 location = Qunbound;
4802 if (CONSP (spec) && CONSP (XCAR (spec)))
4804 Lisp_Object tem;
4806 value = XCDR (spec);
4807 if (CONSP (value))
4808 value = XCAR (value);
4810 tem = XCAR (spec);
4811 if (EQ (XCAR (tem), Qmargin)
4812 && (tem = XCDR (tem),
4813 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4814 (NILP (tem)
4815 || EQ (tem, Qleft_margin)
4816 || EQ (tem, Qright_margin))))
4817 location = tem;
4820 if (EQ (location, Qunbound))
4822 location = Qnil;
4823 value = spec;
4826 /* After this point, VALUE is the property after any
4827 margin prefix has been stripped. It must be a string,
4828 an image specification, or `(space ...)'.
4830 LOCATION specifies where to display: `left-margin',
4831 `right-margin' or nil. */
4833 valid_p = (STRINGP (value)
4834 #ifdef HAVE_WINDOW_SYSTEM
4835 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4836 && valid_image_p (value))
4837 #endif /* not HAVE_WINDOW_SYSTEM */
4838 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4840 if (valid_p && !display_replaced_p)
4842 int retval = 1;
4844 if (!it)
4846 /* Callers need to know whether the display spec is any kind
4847 of `(space ...)' spec that is about to affect text-area
4848 display. */
4849 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4850 retval = 2;
4851 return retval;
4854 /* Save current settings of IT so that we can restore them
4855 when we are finished with the glyph property value. */
4856 push_it (it, position);
4857 it->from_overlay = overlay;
4858 it->from_disp_prop_p = 1;
4860 if (NILP (location))
4861 it->area = TEXT_AREA;
4862 else if (EQ (location, Qleft_margin))
4863 it->area = LEFT_MARGIN_AREA;
4864 else
4865 it->area = RIGHT_MARGIN_AREA;
4867 if (STRINGP (value))
4869 it->string = value;
4870 it->multibyte_p = STRING_MULTIBYTE (it->string);
4871 it->current.overlay_string_index = -1;
4872 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4873 it->end_charpos = it->string_nchars = SCHARS (it->string);
4874 it->method = GET_FROM_STRING;
4875 it->stop_charpos = 0;
4876 it->prev_stop = 0;
4877 it->base_level_stop = 0;
4878 it->string_from_display_prop_p = 1;
4879 /* Say that we haven't consumed the characters with
4880 `display' property yet. The call to pop_it in
4881 set_iterator_to_next will clean this up. */
4882 if (BUFFERP (object))
4883 *position = start_pos;
4885 /* Force paragraph direction to be that of the parent
4886 object. If the parent object's paragraph direction is
4887 not yet determined, default to L2R. */
4888 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4889 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4890 else
4891 it->paragraph_embedding = L2R;
4893 /* Set up the bidi iterator for this display string. */
4894 if (it->bidi_p)
4896 it->bidi_it.string.lstring = it->string;
4897 it->bidi_it.string.s = NULL;
4898 it->bidi_it.string.schars = it->end_charpos;
4899 it->bidi_it.string.bufpos = bufpos;
4900 it->bidi_it.string.from_disp_str = 1;
4901 it->bidi_it.string.unibyte = !it->multibyte_p;
4902 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4905 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4907 it->method = GET_FROM_STRETCH;
4908 it->object = value;
4909 *position = it->position = start_pos;
4910 retval = 1 + (it->area == TEXT_AREA);
4912 #ifdef HAVE_WINDOW_SYSTEM
4913 else
4915 it->what = IT_IMAGE;
4916 it->image_id = lookup_image (it->f, value);
4917 it->position = start_pos;
4918 it->object = NILP (object) ? it->w->buffer : object;
4919 it->method = GET_FROM_IMAGE;
4921 /* Say that we haven't consumed the characters with
4922 `display' property yet. The call to pop_it in
4923 set_iterator_to_next will clean this up. */
4924 *position = start_pos;
4926 #endif /* HAVE_WINDOW_SYSTEM */
4928 return retval;
4931 /* Invalid property or property not supported. Restore
4932 POSITION to what it was before. */
4933 *position = start_pos;
4934 return 0;
4937 /* Check if PROP is a display property value whose text should be
4938 treated as intangible. OVERLAY is the overlay from which PROP
4939 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4940 specify the buffer position covered by PROP. */
4943 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4944 EMACS_INT charpos, EMACS_INT bytepos)
4946 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4947 struct text_pos position;
4949 SET_TEXT_POS (position, charpos, bytepos);
4950 return handle_display_spec (NULL, prop, Qnil, overlay,
4951 &position, charpos, frame_window_p);
4955 /* Return 1 if PROP is a display sub-property value containing STRING.
4957 Implementation note: this and the following function are really
4958 special cases of handle_display_spec and
4959 handle_single_display_spec, and should ideally use the same code.
4960 Until they do, these two pairs must be consistent and must be
4961 modified in sync. */
4963 static int
4964 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4966 if (EQ (string, prop))
4967 return 1;
4969 /* Skip over `when FORM'. */
4970 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4972 prop = XCDR (prop);
4973 if (!CONSP (prop))
4974 return 0;
4975 /* Actually, the condition following `when' should be eval'ed,
4976 like handle_single_display_spec does, and we should return
4977 zero if it evaluates to nil. However, this function is
4978 called only when the buffer was already displayed and some
4979 glyph in the glyph matrix was found to come from a display
4980 string. Therefore, the condition was already evaluated, and
4981 the result was non-nil, otherwise the display string wouldn't
4982 have been displayed and we would have never been called for
4983 this property. Thus, we can skip the evaluation and assume
4984 its result is non-nil. */
4985 prop = XCDR (prop);
4988 if (CONSP (prop))
4989 /* Skip over `margin LOCATION'. */
4990 if (EQ (XCAR (prop), Qmargin))
4992 prop = XCDR (prop);
4993 if (!CONSP (prop))
4994 return 0;
4996 prop = XCDR (prop);
4997 if (!CONSP (prop))
4998 return 0;
5001 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5005 /* Return 1 if STRING appears in the `display' property PROP. */
5007 static int
5008 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5010 if (CONSP (prop)
5011 && !EQ (XCAR (prop), Qwhen)
5012 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5014 /* A list of sub-properties. */
5015 while (CONSP (prop))
5017 if (single_display_spec_string_p (XCAR (prop), string))
5018 return 1;
5019 prop = XCDR (prop);
5022 else if (VECTORP (prop))
5024 /* A vector of sub-properties. */
5025 int i;
5026 for (i = 0; i < ASIZE (prop); ++i)
5027 if (single_display_spec_string_p (AREF (prop, i), string))
5028 return 1;
5030 else
5031 return single_display_spec_string_p (prop, string);
5033 return 0;
5036 /* Look for STRING in overlays and text properties in the current
5037 buffer, between character positions FROM and TO (excluding TO).
5038 BACK_P non-zero means look back (in this case, TO is supposed to be
5039 less than FROM).
5040 Value is the first character position where STRING was found, or
5041 zero if it wasn't found before hitting TO.
5043 This function may only use code that doesn't eval because it is
5044 called asynchronously from note_mouse_highlight. */
5046 static EMACS_INT
5047 string_buffer_position_lim (Lisp_Object string,
5048 EMACS_INT from, EMACS_INT to, int back_p)
5050 Lisp_Object limit, prop, pos;
5051 int found = 0;
5053 pos = make_number (max (from, BEGV));
5055 if (!back_p) /* looking forward */
5057 limit = make_number (min (to, ZV));
5058 while (!found && !EQ (pos, limit))
5060 prop = Fget_char_property (pos, Qdisplay, Qnil);
5061 if (!NILP (prop) && display_prop_string_p (prop, string))
5062 found = 1;
5063 else
5064 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5065 limit);
5068 else /* looking back */
5070 limit = make_number (max (to, BEGV));
5071 while (!found && !EQ (pos, limit))
5073 prop = Fget_char_property (pos, Qdisplay, Qnil);
5074 if (!NILP (prop) && display_prop_string_p (prop, string))
5075 found = 1;
5076 else
5077 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5078 limit);
5082 return found ? XINT (pos) : 0;
5085 /* Determine which buffer position in current buffer STRING comes from.
5086 AROUND_CHARPOS is an approximate position where it could come from.
5087 Value is the buffer position or 0 if it couldn't be determined.
5089 This function is necessary because we don't record buffer positions
5090 in glyphs generated from strings (to keep struct glyph small).
5091 This function may only use code that doesn't eval because it is
5092 called asynchronously from note_mouse_highlight. */
5094 static EMACS_INT
5095 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5097 const int MAX_DISTANCE = 1000;
5098 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5099 around_charpos + MAX_DISTANCE,
5102 if (!found)
5103 found = string_buffer_position_lim (string, around_charpos,
5104 around_charpos - MAX_DISTANCE, 1);
5105 return found;
5110 /***********************************************************************
5111 `composition' property
5112 ***********************************************************************/
5114 /* Set up iterator IT from `composition' property at its current
5115 position. Called from handle_stop. */
5117 static enum prop_handled
5118 handle_composition_prop (struct it *it)
5120 Lisp_Object prop, string;
5121 EMACS_INT pos, pos_byte, start, end;
5123 if (STRINGP (it->string))
5125 unsigned char *s;
5127 pos = IT_STRING_CHARPOS (*it);
5128 pos_byte = IT_STRING_BYTEPOS (*it);
5129 string = it->string;
5130 s = SDATA (string) + pos_byte;
5131 it->c = STRING_CHAR (s);
5133 else
5135 pos = IT_CHARPOS (*it);
5136 pos_byte = IT_BYTEPOS (*it);
5137 string = Qnil;
5138 it->c = FETCH_CHAR (pos_byte);
5141 /* If there's a valid composition and point is not inside of the
5142 composition (in the case that the composition is from the current
5143 buffer), draw a glyph composed from the composition components. */
5144 if (find_composition (pos, -1, &start, &end, &prop, string)
5145 && COMPOSITION_VALID_P (start, end, prop)
5146 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5148 if (start < pos)
5149 /* As we can't handle this situation (perhaps font-lock added
5150 a new composition), we just return here hoping that next
5151 redisplay will detect this composition much earlier. */
5152 return HANDLED_NORMALLY;
5153 if (start != pos)
5155 if (STRINGP (it->string))
5156 pos_byte = string_char_to_byte (it->string, start);
5157 else
5158 pos_byte = CHAR_TO_BYTE (start);
5160 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5161 prop, string);
5163 if (it->cmp_it.id >= 0)
5165 it->cmp_it.ch = -1;
5166 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5167 it->cmp_it.nglyphs = -1;
5171 return HANDLED_NORMALLY;
5176 /***********************************************************************
5177 Overlay strings
5178 ***********************************************************************/
5180 /* The following structure is used to record overlay strings for
5181 later sorting in load_overlay_strings. */
5183 struct overlay_entry
5185 Lisp_Object overlay;
5186 Lisp_Object string;
5187 int priority;
5188 int after_string_p;
5192 /* Set up iterator IT from overlay strings at its current position.
5193 Called from handle_stop. */
5195 static enum prop_handled
5196 handle_overlay_change (struct it *it)
5198 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5199 return HANDLED_RECOMPUTE_PROPS;
5200 else
5201 return HANDLED_NORMALLY;
5205 /* Set up the next overlay string for delivery by IT, if there is an
5206 overlay string to deliver. Called by set_iterator_to_next when the
5207 end of the current overlay string is reached. If there are more
5208 overlay strings to display, IT->string and
5209 IT->current.overlay_string_index are set appropriately here.
5210 Otherwise IT->string is set to nil. */
5212 static void
5213 next_overlay_string (struct it *it)
5215 ++it->current.overlay_string_index;
5216 if (it->current.overlay_string_index == it->n_overlay_strings)
5218 /* No more overlay strings. Restore IT's settings to what
5219 they were before overlay strings were processed, and
5220 continue to deliver from current_buffer. */
5222 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5223 pop_it (it);
5224 xassert (it->sp > 0
5225 || (NILP (it->string)
5226 && it->method == GET_FROM_BUFFER
5227 && it->stop_charpos >= BEGV
5228 && it->stop_charpos <= it->end_charpos));
5229 it->current.overlay_string_index = -1;
5230 it->n_overlay_strings = 0;
5231 it->overlay_strings_charpos = -1;
5232 /* If there's an empty display string on the stack, pop the
5233 stack, to resync the bidi iterator with IT's position. Such
5234 empty strings are pushed onto the stack in
5235 get_overlay_strings_1. */
5236 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5237 pop_it (it);
5239 /* If we're at the end of the buffer, record that we have
5240 processed the overlay strings there already, so that
5241 next_element_from_buffer doesn't try it again. */
5242 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5243 it->overlay_strings_at_end_processed_p = 1;
5245 else
5247 /* There are more overlay strings to process. If
5248 IT->current.overlay_string_index has advanced to a position
5249 where we must load IT->overlay_strings with more strings, do
5250 it. We must load at the IT->overlay_strings_charpos where
5251 IT->n_overlay_strings was originally computed; when invisible
5252 text is present, this might not be IT_CHARPOS (Bug#7016). */
5253 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5255 if (it->current.overlay_string_index && i == 0)
5256 load_overlay_strings (it, it->overlay_strings_charpos);
5258 /* Initialize IT to deliver display elements from the overlay
5259 string. */
5260 it->string = it->overlay_strings[i];
5261 it->multibyte_p = STRING_MULTIBYTE (it->string);
5262 SET_TEXT_POS (it->current.string_pos, 0, 0);
5263 it->method = GET_FROM_STRING;
5264 it->stop_charpos = 0;
5265 if (it->cmp_it.stop_pos >= 0)
5266 it->cmp_it.stop_pos = 0;
5267 it->prev_stop = 0;
5268 it->base_level_stop = 0;
5270 /* Set up the bidi iterator for this overlay string. */
5271 if (it->bidi_p)
5273 it->bidi_it.string.lstring = it->string;
5274 it->bidi_it.string.s = NULL;
5275 it->bidi_it.string.schars = SCHARS (it->string);
5276 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5277 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5278 it->bidi_it.string.unibyte = !it->multibyte_p;
5279 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5283 CHECK_IT (it);
5287 /* Compare two overlay_entry structures E1 and E2. Used as a
5288 comparison function for qsort in load_overlay_strings. Overlay
5289 strings for the same position are sorted so that
5291 1. All after-strings come in front of before-strings, except
5292 when they come from the same overlay.
5294 2. Within after-strings, strings are sorted so that overlay strings
5295 from overlays with higher priorities come first.
5297 2. Within before-strings, strings are sorted so that overlay
5298 strings from overlays with higher priorities come last.
5300 Value is analogous to strcmp. */
5303 static int
5304 compare_overlay_entries (const void *e1, const void *e2)
5306 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5307 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5308 int result;
5310 if (entry1->after_string_p != entry2->after_string_p)
5312 /* Let after-strings appear in front of before-strings if
5313 they come from different overlays. */
5314 if (EQ (entry1->overlay, entry2->overlay))
5315 result = entry1->after_string_p ? 1 : -1;
5316 else
5317 result = entry1->after_string_p ? -1 : 1;
5319 else if (entry1->after_string_p)
5320 /* After-strings sorted in order of decreasing priority. */
5321 result = entry2->priority - entry1->priority;
5322 else
5323 /* Before-strings sorted in order of increasing priority. */
5324 result = entry1->priority - entry2->priority;
5326 return result;
5330 /* Load the vector IT->overlay_strings with overlay strings from IT's
5331 current buffer position, or from CHARPOS if that is > 0. Set
5332 IT->n_overlays to the total number of overlay strings found.
5334 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5335 a time. On entry into load_overlay_strings,
5336 IT->current.overlay_string_index gives the number of overlay
5337 strings that have already been loaded by previous calls to this
5338 function.
5340 IT->add_overlay_start contains an additional overlay start
5341 position to consider for taking overlay strings from, if non-zero.
5342 This position comes into play when the overlay has an `invisible'
5343 property, and both before and after-strings. When we've skipped to
5344 the end of the overlay, because of its `invisible' property, we
5345 nevertheless want its before-string to appear.
5346 IT->add_overlay_start will contain the overlay start position
5347 in this case.
5349 Overlay strings are sorted so that after-string strings come in
5350 front of before-string strings. Within before and after-strings,
5351 strings are sorted by overlay priority. See also function
5352 compare_overlay_entries. */
5354 static void
5355 load_overlay_strings (struct it *it, EMACS_INT charpos)
5357 Lisp_Object overlay, window, str, invisible;
5358 struct Lisp_Overlay *ov;
5359 EMACS_INT start, end;
5360 int size = 20;
5361 int n = 0, i, j, invis_p;
5362 struct overlay_entry *entries
5363 = (struct overlay_entry *) alloca (size * sizeof *entries);
5365 if (charpos <= 0)
5366 charpos = IT_CHARPOS (*it);
5368 /* Append the overlay string STRING of overlay OVERLAY to vector
5369 `entries' which has size `size' and currently contains `n'
5370 elements. AFTER_P non-zero means STRING is an after-string of
5371 OVERLAY. */
5372 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5373 do \
5375 Lisp_Object priority; \
5377 if (n == size) \
5379 int new_size = 2 * size; \
5380 struct overlay_entry *old = entries; \
5381 entries = \
5382 (struct overlay_entry *) alloca (new_size \
5383 * sizeof *entries); \
5384 memcpy (entries, old, size * sizeof *entries); \
5385 size = new_size; \
5388 entries[n].string = (STRING); \
5389 entries[n].overlay = (OVERLAY); \
5390 priority = Foverlay_get ((OVERLAY), Qpriority); \
5391 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5392 entries[n].after_string_p = (AFTER_P); \
5393 ++n; \
5395 while (0)
5397 /* Process overlay before the overlay center. */
5398 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5400 XSETMISC (overlay, ov);
5401 xassert (OVERLAYP (overlay));
5402 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5403 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5405 if (end < charpos)
5406 break;
5408 /* Skip this overlay if it doesn't start or end at IT's current
5409 position. */
5410 if (end != charpos && start != charpos)
5411 continue;
5413 /* Skip this overlay if it doesn't apply to IT->w. */
5414 window = Foverlay_get (overlay, Qwindow);
5415 if (WINDOWP (window) && XWINDOW (window) != it->w)
5416 continue;
5418 /* If the text ``under'' the overlay is invisible, both before-
5419 and after-strings from this overlay are visible; start and
5420 end position are indistinguishable. */
5421 invisible = Foverlay_get (overlay, Qinvisible);
5422 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5424 /* If overlay has a non-empty before-string, record it. */
5425 if ((start == charpos || (end == charpos && invis_p))
5426 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5427 && SCHARS (str))
5428 RECORD_OVERLAY_STRING (overlay, str, 0);
5430 /* If overlay has a non-empty after-string, record it. */
5431 if ((end == charpos || (start == charpos && invis_p))
5432 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5433 && SCHARS (str))
5434 RECORD_OVERLAY_STRING (overlay, str, 1);
5437 /* Process overlays after the overlay center. */
5438 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5440 XSETMISC (overlay, ov);
5441 xassert (OVERLAYP (overlay));
5442 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5443 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5445 if (start > charpos)
5446 break;
5448 /* Skip this overlay if it doesn't start or end at IT's current
5449 position. */
5450 if (end != charpos && start != charpos)
5451 continue;
5453 /* Skip this overlay if it doesn't apply to IT->w. */
5454 window = Foverlay_get (overlay, Qwindow);
5455 if (WINDOWP (window) && XWINDOW (window) != it->w)
5456 continue;
5458 /* If the text ``under'' the overlay is invisible, it has a zero
5459 dimension, and both before- and after-strings apply. */
5460 invisible = Foverlay_get (overlay, Qinvisible);
5461 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5463 /* If overlay has a non-empty before-string, record it. */
5464 if ((start == charpos || (end == charpos && invis_p))
5465 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5466 && SCHARS (str))
5467 RECORD_OVERLAY_STRING (overlay, str, 0);
5469 /* If overlay has a non-empty after-string, record it. */
5470 if ((end == charpos || (start == charpos && invis_p))
5471 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5472 && SCHARS (str))
5473 RECORD_OVERLAY_STRING (overlay, str, 1);
5476 #undef RECORD_OVERLAY_STRING
5478 /* Sort entries. */
5479 if (n > 1)
5480 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5482 /* Record number of overlay strings, and where we computed it. */
5483 it->n_overlay_strings = n;
5484 it->overlay_strings_charpos = charpos;
5486 /* IT->current.overlay_string_index is the number of overlay strings
5487 that have already been consumed by IT. Copy some of the
5488 remaining overlay strings to IT->overlay_strings. */
5489 i = 0;
5490 j = it->current.overlay_string_index;
5491 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5493 it->overlay_strings[i] = entries[j].string;
5494 it->string_overlays[i++] = entries[j++].overlay;
5497 CHECK_IT (it);
5501 /* Get the first chunk of overlay strings at IT's current buffer
5502 position, or at CHARPOS if that is > 0. Value is non-zero if at
5503 least one overlay string was found. */
5505 static int
5506 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5508 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5509 process. This fills IT->overlay_strings with strings, and sets
5510 IT->n_overlay_strings to the total number of strings to process.
5511 IT->pos.overlay_string_index has to be set temporarily to zero
5512 because load_overlay_strings needs this; it must be set to -1
5513 when no overlay strings are found because a zero value would
5514 indicate a position in the first overlay string. */
5515 it->current.overlay_string_index = 0;
5516 load_overlay_strings (it, charpos);
5518 /* If we found overlay strings, set up IT to deliver display
5519 elements from the first one. Otherwise set up IT to deliver
5520 from current_buffer. */
5521 if (it->n_overlay_strings)
5523 /* Make sure we know settings in current_buffer, so that we can
5524 restore meaningful values when we're done with the overlay
5525 strings. */
5526 if (compute_stop_p)
5527 compute_stop_pos (it);
5528 xassert (it->face_id >= 0);
5530 /* Save IT's settings. They are restored after all overlay
5531 strings have been processed. */
5532 xassert (!compute_stop_p || it->sp == 0);
5534 /* When called from handle_stop, there might be an empty display
5535 string loaded. In that case, don't bother saving it. But
5536 don't use this optimization with the bidi iterator, since we
5537 need the corresponding pop_it call to resync the bidi
5538 iterator's position with IT's position, after we are done
5539 with the overlay strings. (The corresponding call to pop_it
5540 in case of an empty display string is in
5541 next_overlay_string.) */
5542 if (!(!it->bidi_p
5543 && STRINGP (it->string) && !SCHARS (it->string)))
5544 push_it (it, NULL);
5546 /* Set up IT to deliver display elements from the first overlay
5547 string. */
5548 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5549 it->string = it->overlay_strings[0];
5550 it->from_overlay = Qnil;
5551 it->stop_charpos = 0;
5552 xassert (STRINGP (it->string));
5553 it->end_charpos = SCHARS (it->string);
5554 it->prev_stop = 0;
5555 it->base_level_stop = 0;
5556 it->multibyte_p = STRING_MULTIBYTE (it->string);
5557 it->method = GET_FROM_STRING;
5558 it->from_disp_prop_p = 0;
5560 /* Force paragraph direction to be that of the parent
5561 buffer. */
5562 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5563 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5564 else
5565 it->paragraph_embedding = L2R;
5567 /* Set up the bidi iterator for this overlay string. */
5568 if (it->bidi_p)
5570 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5572 it->bidi_it.string.lstring = it->string;
5573 it->bidi_it.string.s = NULL;
5574 it->bidi_it.string.schars = SCHARS (it->string);
5575 it->bidi_it.string.bufpos = pos;
5576 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5577 it->bidi_it.string.unibyte = !it->multibyte_p;
5578 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5580 return 1;
5583 it->current.overlay_string_index = -1;
5584 return 0;
5587 static int
5588 get_overlay_strings (struct it *it, EMACS_INT charpos)
5590 it->string = Qnil;
5591 it->method = GET_FROM_BUFFER;
5593 (void) get_overlay_strings_1 (it, charpos, 1);
5595 CHECK_IT (it);
5597 /* Value is non-zero if we found at least one overlay string. */
5598 return STRINGP (it->string);
5603 /***********************************************************************
5604 Saving and restoring state
5605 ***********************************************************************/
5607 /* Save current settings of IT on IT->stack. Called, for example,
5608 before setting up IT for an overlay string, to be able to restore
5609 IT's settings to what they were after the overlay string has been
5610 processed. If POSITION is non-NULL, it is the position to save on
5611 the stack instead of IT->position. */
5613 static void
5614 push_it (struct it *it, struct text_pos *position)
5616 struct iterator_stack_entry *p;
5618 xassert (it->sp < IT_STACK_SIZE);
5619 p = it->stack + it->sp;
5621 p->stop_charpos = it->stop_charpos;
5622 p->prev_stop = it->prev_stop;
5623 p->base_level_stop = it->base_level_stop;
5624 p->cmp_it = it->cmp_it;
5625 xassert (it->face_id >= 0);
5626 p->face_id = it->face_id;
5627 p->string = it->string;
5628 p->method = it->method;
5629 p->from_overlay = it->from_overlay;
5630 switch (p->method)
5632 case GET_FROM_IMAGE:
5633 p->u.image.object = it->object;
5634 p->u.image.image_id = it->image_id;
5635 p->u.image.slice = it->slice;
5636 break;
5637 case GET_FROM_STRETCH:
5638 p->u.stretch.object = it->object;
5639 break;
5641 p->position = position ? *position : it->position;
5642 p->current = it->current;
5643 p->end_charpos = it->end_charpos;
5644 p->string_nchars = it->string_nchars;
5645 p->area = it->area;
5646 p->multibyte_p = it->multibyte_p;
5647 p->avoid_cursor_p = it->avoid_cursor_p;
5648 p->space_width = it->space_width;
5649 p->font_height = it->font_height;
5650 p->voffset = it->voffset;
5651 p->string_from_display_prop_p = it->string_from_display_prop_p;
5652 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5653 p->display_ellipsis_p = 0;
5654 p->line_wrap = it->line_wrap;
5655 p->bidi_p = it->bidi_p;
5656 p->paragraph_embedding = it->paragraph_embedding;
5657 p->from_disp_prop_p = it->from_disp_prop_p;
5658 ++it->sp;
5660 /* Save the state of the bidi iterator as well. */
5661 if (it->bidi_p)
5662 bidi_push_it (&it->bidi_it);
5665 static void
5666 iterate_out_of_display_property (struct it *it)
5668 int buffer_p = !STRINGP (it->string);
5669 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5670 EMACS_INT bob = (buffer_p ? BEGV : 0);
5672 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5674 /* Maybe initialize paragraph direction. If we are at the beginning
5675 of a new paragraph, next_element_from_buffer may not have a
5676 chance to do that. */
5677 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5678 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5679 /* prev_stop can be zero, so check against BEGV as well. */
5680 while (it->bidi_it.charpos >= bob
5681 && it->prev_stop <= it->bidi_it.charpos
5682 && it->bidi_it.charpos < CHARPOS (it->position)
5683 && it->bidi_it.charpos < eob)
5684 bidi_move_to_visually_next (&it->bidi_it);
5685 /* Record the stop_pos we just crossed, for when we cross it
5686 back, maybe. */
5687 if (it->bidi_it.charpos > CHARPOS (it->position))
5688 it->prev_stop = CHARPOS (it->position);
5689 /* If we ended up not where pop_it put us, resync IT's
5690 positional members with the bidi iterator. */
5691 if (it->bidi_it.charpos != CHARPOS (it->position))
5692 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5693 if (buffer_p)
5694 it->current.pos = it->position;
5695 else
5696 it->current.string_pos = it->position;
5699 /* Restore IT's settings from IT->stack. Called, for example, when no
5700 more overlay strings must be processed, and we return to delivering
5701 display elements from a buffer, or when the end of a string from a
5702 `display' property is reached and we return to delivering display
5703 elements from an overlay string, or from a buffer. */
5705 static void
5706 pop_it (struct it *it)
5708 struct iterator_stack_entry *p;
5709 int from_display_prop = it->from_disp_prop_p;
5711 xassert (it->sp > 0);
5712 --it->sp;
5713 p = it->stack + it->sp;
5714 it->stop_charpos = p->stop_charpos;
5715 it->prev_stop = p->prev_stop;
5716 it->base_level_stop = p->base_level_stop;
5717 it->cmp_it = p->cmp_it;
5718 it->face_id = p->face_id;
5719 it->current = p->current;
5720 it->position = p->position;
5721 it->string = p->string;
5722 it->from_overlay = p->from_overlay;
5723 if (NILP (it->string))
5724 SET_TEXT_POS (it->current.string_pos, -1, -1);
5725 it->method = p->method;
5726 switch (it->method)
5728 case GET_FROM_IMAGE:
5729 it->image_id = p->u.image.image_id;
5730 it->object = p->u.image.object;
5731 it->slice = p->u.image.slice;
5732 break;
5733 case GET_FROM_STRETCH:
5734 it->object = p->u.stretch.object;
5735 break;
5736 case GET_FROM_BUFFER:
5737 it->object = it->w->buffer;
5738 break;
5739 case GET_FROM_STRING:
5740 it->object = it->string;
5741 break;
5742 case GET_FROM_DISPLAY_VECTOR:
5743 if (it->s)
5744 it->method = GET_FROM_C_STRING;
5745 else if (STRINGP (it->string))
5746 it->method = GET_FROM_STRING;
5747 else
5749 it->method = GET_FROM_BUFFER;
5750 it->object = it->w->buffer;
5753 it->end_charpos = p->end_charpos;
5754 it->string_nchars = p->string_nchars;
5755 it->area = p->area;
5756 it->multibyte_p = p->multibyte_p;
5757 it->avoid_cursor_p = p->avoid_cursor_p;
5758 it->space_width = p->space_width;
5759 it->font_height = p->font_height;
5760 it->voffset = p->voffset;
5761 it->string_from_display_prop_p = p->string_from_display_prop_p;
5762 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5763 it->line_wrap = p->line_wrap;
5764 it->bidi_p = p->bidi_p;
5765 it->paragraph_embedding = p->paragraph_embedding;
5766 it->from_disp_prop_p = p->from_disp_prop_p;
5767 if (it->bidi_p)
5769 bidi_pop_it (&it->bidi_it);
5770 /* Bidi-iterate until we get out of the portion of text, if any,
5771 covered by a `display' text property or by an overlay with
5772 `display' property. (We cannot just jump there, because the
5773 internal coherency of the bidi iterator state can not be
5774 preserved across such jumps.) We also must determine the
5775 paragraph base direction if the overlay we just processed is
5776 at the beginning of a new paragraph. */
5777 if (from_display_prop
5778 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5779 iterate_out_of_display_property (it);
5781 xassert ((BUFFERP (it->object)
5782 && IT_CHARPOS (*it) == it->bidi_it.charpos
5783 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5784 || (STRINGP (it->object)
5785 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5786 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5787 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5793 /***********************************************************************
5794 Moving over lines
5795 ***********************************************************************/
5797 /* Set IT's current position to the previous line start. */
5799 static void
5800 back_to_previous_line_start (struct it *it)
5802 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5803 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5807 /* Move IT to the next line start.
5809 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5810 we skipped over part of the text (as opposed to moving the iterator
5811 continuously over the text). Otherwise, don't change the value
5812 of *SKIPPED_P.
5814 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5815 iterator on the newline, if it was found.
5817 Newlines may come from buffer text, overlay strings, or strings
5818 displayed via the `display' property. That's the reason we can't
5819 simply use find_next_newline_no_quit.
5821 Note that this function may not skip over invisible text that is so
5822 because of text properties and immediately follows a newline. If
5823 it would, function reseat_at_next_visible_line_start, when called
5824 from set_iterator_to_next, would effectively make invisible
5825 characters following a newline part of the wrong glyph row, which
5826 leads to wrong cursor motion. */
5828 static int
5829 forward_to_next_line_start (struct it *it, int *skipped_p,
5830 struct bidi_it *bidi_it_prev)
5832 EMACS_INT old_selective;
5833 int newline_found_p, n;
5834 const int MAX_NEWLINE_DISTANCE = 500;
5836 /* If already on a newline, just consume it to avoid unintended
5837 skipping over invisible text below. */
5838 if (it->what == IT_CHARACTER
5839 && it->c == '\n'
5840 && CHARPOS (it->position) == IT_CHARPOS (*it))
5842 if (it->bidi_p && bidi_it_prev)
5843 *bidi_it_prev = it->bidi_it;
5844 set_iterator_to_next (it, 0);
5845 it->c = 0;
5846 return 1;
5849 /* Don't handle selective display in the following. It's (a)
5850 unnecessary because it's done by the caller, and (b) leads to an
5851 infinite recursion because next_element_from_ellipsis indirectly
5852 calls this function. */
5853 old_selective = it->selective;
5854 it->selective = 0;
5856 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5857 from buffer text. */
5858 for (n = newline_found_p = 0;
5859 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5860 n += STRINGP (it->string) ? 0 : 1)
5862 if (!get_next_display_element (it))
5863 return 0;
5864 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5865 if (newline_found_p && it->bidi_p && bidi_it_prev)
5866 *bidi_it_prev = it->bidi_it;
5867 set_iterator_to_next (it, 0);
5870 /* If we didn't find a newline near enough, see if we can use a
5871 short-cut. */
5872 if (!newline_found_p)
5874 EMACS_INT start = IT_CHARPOS (*it);
5875 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5876 Lisp_Object pos;
5878 xassert (!STRINGP (it->string));
5880 /* If there isn't any `display' property in sight, and no
5881 overlays, we can just use the position of the newline in
5882 buffer text. */
5883 if (it->stop_charpos >= limit
5884 || ((pos = Fnext_single_property_change (make_number (start),
5885 Qdisplay, Qnil,
5886 make_number (limit)),
5887 NILP (pos))
5888 && next_overlay_change (start) == ZV))
5890 if (!it->bidi_p)
5892 IT_CHARPOS (*it) = limit;
5893 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5895 else
5897 struct bidi_it bprev;
5899 /* Help bidi.c avoid expensive searches for display
5900 properties and overlays, by telling it that there are
5901 none up to `limit'. */
5902 if (it->bidi_it.disp_pos < limit)
5904 it->bidi_it.disp_pos = limit;
5905 it->bidi_it.disp_prop = 0;
5907 do {
5908 bprev = it->bidi_it;
5909 bidi_move_to_visually_next (&it->bidi_it);
5910 } while (it->bidi_it.charpos != limit);
5911 IT_CHARPOS (*it) = limit;
5912 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5913 if (bidi_it_prev)
5914 *bidi_it_prev = bprev;
5916 *skipped_p = newline_found_p = 1;
5918 else
5920 while (get_next_display_element (it)
5921 && !newline_found_p)
5923 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5924 if (newline_found_p && it->bidi_p && bidi_it_prev)
5925 *bidi_it_prev = it->bidi_it;
5926 set_iterator_to_next (it, 0);
5931 it->selective = old_selective;
5932 return newline_found_p;
5936 /* Set IT's current position to the previous visible line start. Skip
5937 invisible text that is so either due to text properties or due to
5938 selective display. Caution: this does not change IT->current_x and
5939 IT->hpos. */
5941 static void
5942 back_to_previous_visible_line_start (struct it *it)
5944 while (IT_CHARPOS (*it) > BEGV)
5946 back_to_previous_line_start (it);
5948 if (IT_CHARPOS (*it) <= BEGV)
5949 break;
5951 /* If selective > 0, then lines indented more than its value are
5952 invisible. */
5953 if (it->selective > 0
5954 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5955 it->selective))
5956 continue;
5958 /* Check the newline before point for invisibility. */
5960 Lisp_Object prop;
5961 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5962 Qinvisible, it->window);
5963 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5964 continue;
5967 if (IT_CHARPOS (*it) <= BEGV)
5968 break;
5971 struct it it2;
5972 void *it2data = NULL;
5973 EMACS_INT pos;
5974 EMACS_INT beg, end;
5975 Lisp_Object val, overlay;
5977 SAVE_IT (it2, *it, it2data);
5979 /* If newline is part of a composition, continue from start of composition */
5980 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5981 && beg < IT_CHARPOS (*it))
5982 goto replaced;
5984 /* If newline is replaced by a display property, find start of overlay
5985 or interval and continue search from that point. */
5986 pos = --IT_CHARPOS (it2);
5987 --IT_BYTEPOS (it2);
5988 it2.sp = 0;
5989 bidi_unshelve_cache (NULL, 0);
5990 it2.string_from_display_prop_p = 0;
5991 it2.from_disp_prop_p = 0;
5992 if (handle_display_prop (&it2) == HANDLED_RETURN
5993 && !NILP (val = get_char_property_and_overlay
5994 (make_number (pos), Qdisplay, Qnil, &overlay))
5995 && (OVERLAYP (overlay)
5996 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5997 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5999 RESTORE_IT (it, it, it2data);
6000 goto replaced;
6003 /* Newline is not replaced by anything -- so we are done. */
6004 RESTORE_IT (it, it, it2data);
6005 break;
6007 replaced:
6008 if (beg < BEGV)
6009 beg = BEGV;
6010 IT_CHARPOS (*it) = beg;
6011 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6015 it->continuation_lines_width = 0;
6017 xassert (IT_CHARPOS (*it) >= BEGV);
6018 xassert (IT_CHARPOS (*it) == BEGV
6019 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6020 CHECK_IT (it);
6024 /* Reseat iterator IT at the previous visible line start. Skip
6025 invisible text that is so either due to text properties or due to
6026 selective display. At the end, update IT's overlay information,
6027 face information etc. */
6029 void
6030 reseat_at_previous_visible_line_start (struct it *it)
6032 back_to_previous_visible_line_start (it);
6033 reseat (it, it->current.pos, 1);
6034 CHECK_IT (it);
6038 /* Reseat iterator IT on the next visible line start in the current
6039 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6040 preceding the line start. Skip over invisible text that is so
6041 because of selective display. Compute faces, overlays etc at the
6042 new position. Note that this function does not skip over text that
6043 is invisible because of text properties. */
6045 static void
6046 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6048 int newline_found_p, skipped_p = 0;
6049 struct bidi_it bidi_it_prev;
6051 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6053 /* Skip over lines that are invisible because they are indented
6054 more than the value of IT->selective. */
6055 if (it->selective > 0)
6056 while (IT_CHARPOS (*it) < ZV
6057 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6058 it->selective))
6060 xassert (IT_BYTEPOS (*it) == BEGV
6061 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6062 newline_found_p =
6063 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6066 /* Position on the newline if that's what's requested. */
6067 if (on_newline_p && newline_found_p)
6069 if (STRINGP (it->string))
6071 if (IT_STRING_CHARPOS (*it) > 0)
6073 if (!it->bidi_p)
6075 --IT_STRING_CHARPOS (*it);
6076 --IT_STRING_BYTEPOS (*it);
6078 else
6080 /* We need to restore the bidi iterator to the state
6081 it had on the newline, and resync the IT's
6082 position with that. */
6083 it->bidi_it = bidi_it_prev;
6084 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6085 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6089 else if (IT_CHARPOS (*it) > BEGV)
6091 if (!it->bidi_p)
6093 --IT_CHARPOS (*it);
6094 --IT_BYTEPOS (*it);
6096 else
6098 /* We need to restore the bidi iterator to the state it
6099 had on the newline and resync IT with that. */
6100 it->bidi_it = bidi_it_prev;
6101 IT_CHARPOS (*it) = it->bidi_it.charpos;
6102 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6104 reseat (it, it->current.pos, 0);
6107 else if (skipped_p)
6108 reseat (it, it->current.pos, 0);
6110 CHECK_IT (it);
6115 /***********************************************************************
6116 Changing an iterator's position
6117 ***********************************************************************/
6119 /* Change IT's current position to POS in current_buffer. If FORCE_P
6120 is non-zero, always check for text properties at the new position.
6121 Otherwise, text properties are only looked up if POS >=
6122 IT->check_charpos of a property. */
6124 static void
6125 reseat (struct it *it, struct text_pos pos, int force_p)
6127 EMACS_INT original_pos = IT_CHARPOS (*it);
6129 reseat_1 (it, pos, 0);
6131 /* Determine where to check text properties. Avoid doing it
6132 where possible because text property lookup is very expensive. */
6133 if (force_p
6134 || CHARPOS (pos) > it->stop_charpos
6135 || CHARPOS (pos) < original_pos)
6137 if (it->bidi_p)
6139 /* For bidi iteration, we need to prime prev_stop and
6140 base_level_stop with our best estimations. */
6141 /* Implementation note: Of course, POS is not necessarily a
6142 stop position, so assigning prev_pos to it is a lie; we
6143 should have called compute_stop_backwards. However, if
6144 the current buffer does not include any R2L characters,
6145 that call would be a waste of cycles, because the
6146 iterator will never move back, and thus never cross this
6147 "fake" stop position. So we delay that backward search
6148 until the time we really need it, in next_element_from_buffer. */
6149 if (CHARPOS (pos) != it->prev_stop)
6150 it->prev_stop = CHARPOS (pos);
6151 if (CHARPOS (pos) < it->base_level_stop)
6152 it->base_level_stop = 0; /* meaning it's unknown */
6153 handle_stop (it);
6155 else
6157 handle_stop (it);
6158 it->prev_stop = it->base_level_stop = 0;
6163 CHECK_IT (it);
6167 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6168 IT->stop_pos to POS, also. */
6170 static void
6171 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6173 /* Don't call this function when scanning a C string. */
6174 xassert (it->s == NULL);
6176 /* POS must be a reasonable value. */
6177 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6179 it->current.pos = it->position = pos;
6180 it->end_charpos = ZV;
6181 it->dpvec = NULL;
6182 it->current.dpvec_index = -1;
6183 it->current.overlay_string_index = -1;
6184 IT_STRING_CHARPOS (*it) = -1;
6185 IT_STRING_BYTEPOS (*it) = -1;
6186 it->string = Qnil;
6187 it->method = GET_FROM_BUFFER;
6188 it->object = it->w->buffer;
6189 it->area = TEXT_AREA;
6190 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6191 it->sp = 0;
6192 it->string_from_display_prop_p = 0;
6193 it->string_from_prefix_prop_p = 0;
6195 it->from_disp_prop_p = 0;
6196 it->face_before_selective_p = 0;
6197 if (it->bidi_p)
6199 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6200 &it->bidi_it);
6201 bidi_unshelve_cache (NULL, 0);
6202 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6203 it->bidi_it.string.s = NULL;
6204 it->bidi_it.string.lstring = Qnil;
6205 it->bidi_it.string.bufpos = 0;
6206 it->bidi_it.string.unibyte = 0;
6209 if (set_stop_p)
6211 it->stop_charpos = CHARPOS (pos);
6212 it->base_level_stop = CHARPOS (pos);
6217 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6218 If S is non-null, it is a C string to iterate over. Otherwise,
6219 STRING gives a Lisp string to iterate over.
6221 If PRECISION > 0, don't return more then PRECISION number of
6222 characters from the string.
6224 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6225 characters have been returned. FIELD_WIDTH < 0 means an infinite
6226 field width.
6228 MULTIBYTE = 0 means disable processing of multibyte characters,
6229 MULTIBYTE > 0 means enable it,
6230 MULTIBYTE < 0 means use IT->multibyte_p.
6232 IT must be initialized via a prior call to init_iterator before
6233 calling this function. */
6235 static void
6236 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6237 EMACS_INT charpos, EMACS_INT precision, int field_width,
6238 int multibyte)
6240 /* No region in strings. */
6241 it->region_beg_charpos = it->region_end_charpos = -1;
6243 /* No text property checks performed by default, but see below. */
6244 it->stop_charpos = -1;
6246 /* Set iterator position and end position. */
6247 memset (&it->current, 0, sizeof it->current);
6248 it->current.overlay_string_index = -1;
6249 it->current.dpvec_index = -1;
6250 xassert (charpos >= 0);
6252 /* If STRING is specified, use its multibyteness, otherwise use the
6253 setting of MULTIBYTE, if specified. */
6254 if (multibyte >= 0)
6255 it->multibyte_p = multibyte > 0;
6257 /* Bidirectional reordering of strings is controlled by the default
6258 value of bidi-display-reordering. Don't try to reorder while
6259 loading loadup.el, as the necessary character property tables are
6260 not yet available. */
6261 it->bidi_p =
6262 NILP (Vpurify_flag)
6263 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6265 if (s == NULL)
6267 xassert (STRINGP (string));
6268 it->string = string;
6269 it->s = NULL;
6270 it->end_charpos = it->string_nchars = SCHARS (string);
6271 it->method = GET_FROM_STRING;
6272 it->current.string_pos = string_pos (charpos, string);
6274 if (it->bidi_p)
6276 it->bidi_it.string.lstring = string;
6277 it->bidi_it.string.s = NULL;
6278 it->bidi_it.string.schars = it->end_charpos;
6279 it->bidi_it.string.bufpos = 0;
6280 it->bidi_it.string.from_disp_str = 0;
6281 it->bidi_it.string.unibyte = !it->multibyte_p;
6282 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6283 FRAME_WINDOW_P (it->f), &it->bidi_it);
6286 else
6288 it->s = (const unsigned char *) s;
6289 it->string = Qnil;
6291 /* Note that we use IT->current.pos, not it->current.string_pos,
6292 for displaying C strings. */
6293 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6294 if (it->multibyte_p)
6296 it->current.pos = c_string_pos (charpos, s, 1);
6297 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6299 else
6301 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6302 it->end_charpos = it->string_nchars = strlen (s);
6305 if (it->bidi_p)
6307 it->bidi_it.string.lstring = Qnil;
6308 it->bidi_it.string.s = (const unsigned char *) s;
6309 it->bidi_it.string.schars = it->end_charpos;
6310 it->bidi_it.string.bufpos = 0;
6311 it->bidi_it.string.from_disp_str = 0;
6312 it->bidi_it.string.unibyte = !it->multibyte_p;
6313 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6314 &it->bidi_it);
6316 it->method = GET_FROM_C_STRING;
6319 /* PRECISION > 0 means don't return more than PRECISION characters
6320 from the string. */
6321 if (precision > 0 && it->end_charpos - charpos > precision)
6323 it->end_charpos = it->string_nchars = charpos + precision;
6324 if (it->bidi_p)
6325 it->bidi_it.string.schars = it->end_charpos;
6328 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6329 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6330 FIELD_WIDTH < 0 means infinite field width. This is useful for
6331 padding with `-' at the end of a mode line. */
6332 if (field_width < 0)
6333 field_width = INFINITY;
6334 /* Implementation note: We deliberately don't enlarge
6335 it->bidi_it.string.schars here to fit it->end_charpos, because
6336 the bidi iterator cannot produce characters out of thin air. */
6337 if (field_width > it->end_charpos - charpos)
6338 it->end_charpos = charpos + field_width;
6340 /* Use the standard display table for displaying strings. */
6341 if (DISP_TABLE_P (Vstandard_display_table))
6342 it->dp = XCHAR_TABLE (Vstandard_display_table);
6344 it->stop_charpos = charpos;
6345 it->prev_stop = charpos;
6346 it->base_level_stop = 0;
6347 if (it->bidi_p)
6349 it->bidi_it.first_elt = 1;
6350 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6351 it->bidi_it.disp_pos = -1;
6353 if (s == NULL && it->multibyte_p)
6355 EMACS_INT endpos = SCHARS (it->string);
6356 if (endpos > it->end_charpos)
6357 endpos = it->end_charpos;
6358 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6359 it->string);
6361 CHECK_IT (it);
6366 /***********************************************************************
6367 Iteration
6368 ***********************************************************************/
6370 /* Map enum it_method value to corresponding next_element_from_* function. */
6372 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6374 next_element_from_buffer,
6375 next_element_from_display_vector,
6376 next_element_from_string,
6377 next_element_from_c_string,
6378 next_element_from_image,
6379 next_element_from_stretch
6382 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6385 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6386 (possibly with the following characters). */
6388 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6389 ((IT)->cmp_it.id >= 0 \
6390 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6391 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6392 END_CHARPOS, (IT)->w, \
6393 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6394 (IT)->string)))
6397 /* Lookup the char-table Vglyphless_char_display for character C (-1
6398 if we want information for no-font case), and return the display
6399 method symbol. By side-effect, update it->what and
6400 it->glyphless_method. This function is called from
6401 get_next_display_element for each character element, and from
6402 x_produce_glyphs when no suitable font was found. */
6404 Lisp_Object
6405 lookup_glyphless_char_display (int c, struct it *it)
6407 Lisp_Object glyphless_method = Qnil;
6409 if (CHAR_TABLE_P (Vglyphless_char_display)
6410 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6412 if (c >= 0)
6414 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6415 if (CONSP (glyphless_method))
6416 glyphless_method = FRAME_WINDOW_P (it->f)
6417 ? XCAR (glyphless_method)
6418 : XCDR (glyphless_method);
6420 else
6421 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6424 retry:
6425 if (NILP (glyphless_method))
6427 if (c >= 0)
6428 /* The default is to display the character by a proper font. */
6429 return Qnil;
6430 /* The default for the no-font case is to display an empty box. */
6431 glyphless_method = Qempty_box;
6433 if (EQ (glyphless_method, Qzero_width))
6435 if (c >= 0)
6436 return glyphless_method;
6437 /* This method can't be used for the no-font case. */
6438 glyphless_method = Qempty_box;
6440 if (EQ (glyphless_method, Qthin_space))
6441 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6442 else if (EQ (glyphless_method, Qempty_box))
6443 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6444 else if (EQ (glyphless_method, Qhex_code))
6445 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6446 else if (STRINGP (glyphless_method))
6447 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6448 else
6450 /* Invalid value. We use the default method. */
6451 glyphless_method = Qnil;
6452 goto retry;
6454 it->what = IT_GLYPHLESS;
6455 return glyphless_method;
6458 /* Load IT's display element fields with information about the next
6459 display element from the current position of IT. Value is zero if
6460 end of buffer (or C string) is reached. */
6462 static struct frame *last_escape_glyph_frame = NULL;
6463 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6464 static int last_escape_glyph_merged_face_id = 0;
6466 struct frame *last_glyphless_glyph_frame = NULL;
6467 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6468 int last_glyphless_glyph_merged_face_id = 0;
6470 static int
6471 get_next_display_element (struct it *it)
6473 /* Non-zero means that we found a display element. Zero means that
6474 we hit the end of what we iterate over. Performance note: the
6475 function pointer `method' used here turns out to be faster than
6476 using a sequence of if-statements. */
6477 int success_p;
6479 get_next:
6480 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6482 if (it->what == IT_CHARACTER)
6484 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6485 and only if (a) the resolved directionality of that character
6486 is R..." */
6487 /* FIXME: Do we need an exception for characters from display
6488 tables? */
6489 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6490 it->c = bidi_mirror_char (it->c);
6491 /* Map via display table or translate control characters.
6492 IT->c, IT->len etc. have been set to the next character by
6493 the function call above. If we have a display table, and it
6494 contains an entry for IT->c, translate it. Don't do this if
6495 IT->c itself comes from a display table, otherwise we could
6496 end up in an infinite recursion. (An alternative could be to
6497 count the recursion depth of this function and signal an
6498 error when a certain maximum depth is reached.) Is it worth
6499 it? */
6500 if (success_p && it->dpvec == NULL)
6502 Lisp_Object dv;
6503 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6504 int nonascii_space_p = 0;
6505 int nonascii_hyphen_p = 0;
6506 int c = it->c; /* This is the character to display. */
6508 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6510 xassert (SINGLE_BYTE_CHAR_P (c));
6511 if (unibyte_display_via_language_environment)
6513 c = DECODE_CHAR (unibyte, c);
6514 if (c < 0)
6515 c = BYTE8_TO_CHAR (it->c);
6517 else
6518 c = BYTE8_TO_CHAR (it->c);
6521 if (it->dp
6522 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6523 VECTORP (dv)))
6525 struct Lisp_Vector *v = XVECTOR (dv);
6527 /* Return the first character from the display table
6528 entry, if not empty. If empty, don't display the
6529 current character. */
6530 if (v->header.size)
6532 it->dpvec_char_len = it->len;
6533 it->dpvec = v->contents;
6534 it->dpend = v->contents + v->header.size;
6535 it->current.dpvec_index = 0;
6536 it->dpvec_face_id = -1;
6537 it->saved_face_id = it->face_id;
6538 it->method = GET_FROM_DISPLAY_VECTOR;
6539 it->ellipsis_p = 0;
6541 else
6543 set_iterator_to_next (it, 0);
6545 goto get_next;
6548 if (! NILP (lookup_glyphless_char_display (c, it)))
6550 if (it->what == IT_GLYPHLESS)
6551 goto done;
6552 /* Don't display this character. */
6553 set_iterator_to_next (it, 0);
6554 goto get_next;
6557 /* If `nobreak-char-display' is non-nil, we display
6558 non-ASCII spaces and hyphens specially. */
6559 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6561 if (c == 0xA0)
6562 nonascii_space_p = 1;
6563 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6564 nonascii_hyphen_p = 1;
6567 /* Translate control characters into `\003' or `^C' form.
6568 Control characters coming from a display table entry are
6569 currently not translated because we use IT->dpvec to hold
6570 the translation. This could easily be changed but I
6571 don't believe that it is worth doing.
6573 The characters handled by `nobreak-char-display' must be
6574 translated too.
6576 Non-printable characters and raw-byte characters are also
6577 translated to octal form. */
6578 if (((c < ' ' || c == 127) /* ASCII control chars */
6579 ? (it->area != TEXT_AREA
6580 /* In mode line, treat \n, \t like other crl chars. */
6581 || (c != '\t'
6582 && it->glyph_row
6583 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6584 || (c != '\n' && c != '\t'))
6585 : (nonascii_space_p
6586 || nonascii_hyphen_p
6587 || CHAR_BYTE8_P (c)
6588 || ! CHAR_PRINTABLE_P (c))))
6590 /* C is a control character, non-ASCII space/hyphen,
6591 raw-byte, or a non-printable character which must be
6592 displayed either as '\003' or as `^C' where the '\\'
6593 and '^' can be defined in the display table. Fill
6594 IT->ctl_chars with glyphs for what we have to
6595 display. Then, set IT->dpvec to these glyphs. */
6596 Lisp_Object gc;
6597 int ctl_len;
6598 int face_id;
6599 EMACS_INT lface_id = 0;
6600 int escape_glyph;
6602 /* Handle control characters with ^. */
6604 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6606 int g;
6608 g = '^'; /* default glyph for Control */
6609 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6610 if (it->dp
6611 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6612 && GLYPH_CODE_CHAR_VALID_P (gc))
6614 g = GLYPH_CODE_CHAR (gc);
6615 lface_id = GLYPH_CODE_FACE (gc);
6617 if (lface_id)
6619 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6621 else if (it->f == last_escape_glyph_frame
6622 && it->face_id == last_escape_glyph_face_id)
6624 face_id = last_escape_glyph_merged_face_id;
6626 else
6628 /* Merge the escape-glyph face into the current face. */
6629 face_id = merge_faces (it->f, Qescape_glyph, 0,
6630 it->face_id);
6631 last_escape_glyph_frame = it->f;
6632 last_escape_glyph_face_id = it->face_id;
6633 last_escape_glyph_merged_face_id = face_id;
6636 XSETINT (it->ctl_chars[0], g);
6637 XSETINT (it->ctl_chars[1], c ^ 0100);
6638 ctl_len = 2;
6639 goto display_control;
6642 /* Handle non-ascii space in the mode where it only gets
6643 highlighting. */
6645 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6647 /* Merge `nobreak-space' into the current face. */
6648 face_id = merge_faces (it->f, Qnobreak_space, 0,
6649 it->face_id);
6650 XSETINT (it->ctl_chars[0], ' ');
6651 ctl_len = 1;
6652 goto display_control;
6655 /* Handle sequences that start with the "escape glyph". */
6657 /* the default escape glyph is \. */
6658 escape_glyph = '\\';
6660 if (it->dp
6661 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6662 && GLYPH_CODE_CHAR_VALID_P (gc))
6664 escape_glyph = GLYPH_CODE_CHAR (gc);
6665 lface_id = GLYPH_CODE_FACE (gc);
6667 if (lface_id)
6669 /* The display table specified a face.
6670 Merge it into face_id and also into escape_glyph. */
6671 face_id = merge_faces (it->f, Qt, lface_id,
6672 it->face_id);
6674 else if (it->f == last_escape_glyph_frame
6675 && it->face_id == last_escape_glyph_face_id)
6677 face_id = last_escape_glyph_merged_face_id;
6679 else
6681 /* Merge the escape-glyph face into the current face. */
6682 face_id = merge_faces (it->f, Qescape_glyph, 0,
6683 it->face_id);
6684 last_escape_glyph_frame = it->f;
6685 last_escape_glyph_face_id = it->face_id;
6686 last_escape_glyph_merged_face_id = face_id;
6689 /* Draw non-ASCII hyphen with just highlighting: */
6691 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6693 XSETINT (it->ctl_chars[0], '-');
6694 ctl_len = 1;
6695 goto display_control;
6698 /* Draw non-ASCII space/hyphen with escape glyph: */
6700 if (nonascii_space_p || nonascii_hyphen_p)
6702 XSETINT (it->ctl_chars[0], escape_glyph);
6703 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6704 ctl_len = 2;
6705 goto display_control;
6709 char str[10];
6710 int len, i;
6712 if (CHAR_BYTE8_P (c))
6713 /* Display \200 instead of \17777600. */
6714 c = CHAR_TO_BYTE8 (c);
6715 len = sprintf (str, "%03o", c);
6717 XSETINT (it->ctl_chars[0], escape_glyph);
6718 for (i = 0; i < len; i++)
6719 XSETINT (it->ctl_chars[i + 1], str[i]);
6720 ctl_len = len + 1;
6723 display_control:
6724 /* Set up IT->dpvec and return first character from it. */
6725 it->dpvec_char_len = it->len;
6726 it->dpvec = it->ctl_chars;
6727 it->dpend = it->dpvec + ctl_len;
6728 it->current.dpvec_index = 0;
6729 it->dpvec_face_id = face_id;
6730 it->saved_face_id = it->face_id;
6731 it->method = GET_FROM_DISPLAY_VECTOR;
6732 it->ellipsis_p = 0;
6733 goto get_next;
6735 it->char_to_display = c;
6737 else if (success_p)
6739 it->char_to_display = it->c;
6743 /* Adjust face id for a multibyte character. There are no multibyte
6744 character in unibyte text. */
6745 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6746 && it->multibyte_p
6747 && success_p
6748 && FRAME_WINDOW_P (it->f))
6750 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6752 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6754 /* Automatic composition with glyph-string. */
6755 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6757 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6759 else
6761 EMACS_INT pos = (it->s ? -1
6762 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6763 : IT_CHARPOS (*it));
6764 int c;
6766 if (it->what == IT_CHARACTER)
6767 c = it->char_to_display;
6768 else
6770 struct composition *cmp = composition_table[it->cmp_it.id];
6771 int i;
6773 c = ' ';
6774 for (i = 0; i < cmp->glyph_len; i++)
6775 /* TAB in a composition means display glyphs with
6776 padding space on the left or right. */
6777 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6778 break;
6780 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6784 done:
6785 /* Is this character the last one of a run of characters with
6786 box? If yes, set IT->end_of_box_run_p to 1. */
6787 if (it->face_box_p
6788 && it->s == NULL)
6790 if (it->method == GET_FROM_STRING && it->sp)
6792 int face_id = underlying_face_id (it);
6793 struct face *face = FACE_FROM_ID (it->f, face_id);
6795 if (face)
6797 if (face->box == FACE_NO_BOX)
6799 /* If the box comes from face properties in a
6800 display string, check faces in that string. */
6801 int string_face_id = face_after_it_pos (it);
6802 it->end_of_box_run_p
6803 = (FACE_FROM_ID (it->f, string_face_id)->box
6804 == FACE_NO_BOX);
6806 /* Otherwise, the box comes from the underlying face.
6807 If this is the last string character displayed, check
6808 the next buffer location. */
6809 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6810 && (it->current.overlay_string_index
6811 == it->n_overlay_strings - 1))
6813 EMACS_INT ignore;
6814 int next_face_id;
6815 struct text_pos pos = it->current.pos;
6816 INC_TEXT_POS (pos, it->multibyte_p);
6818 next_face_id = face_at_buffer_position
6819 (it->w, CHARPOS (pos), it->region_beg_charpos,
6820 it->region_end_charpos, &ignore,
6821 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6822 -1);
6823 it->end_of_box_run_p
6824 = (FACE_FROM_ID (it->f, next_face_id)->box
6825 == FACE_NO_BOX);
6829 else
6831 int face_id = face_after_it_pos (it);
6832 it->end_of_box_run_p
6833 = (face_id != it->face_id
6834 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6837 /* If we reached the end of the object we've been iterating (e.g., a
6838 display string or an overlay string), and there's something on
6839 IT->stack, proceed with what's on the stack. It doesn't make
6840 sense to return zero if there's unprocessed stuff on the stack,
6841 because otherwise that stuff will never be displayed. */
6842 if (!success_p && it->sp > 0)
6844 set_iterator_to_next (it, 0);
6845 success_p = get_next_display_element (it);
6848 /* Value is 0 if end of buffer or string reached. */
6849 return success_p;
6853 /* Move IT to the next display element.
6855 RESEAT_P non-zero means if called on a newline in buffer text,
6856 skip to the next visible line start.
6858 Functions get_next_display_element and set_iterator_to_next are
6859 separate because I find this arrangement easier to handle than a
6860 get_next_display_element function that also increments IT's
6861 position. The way it is we can first look at an iterator's current
6862 display element, decide whether it fits on a line, and if it does,
6863 increment the iterator position. The other way around we probably
6864 would either need a flag indicating whether the iterator has to be
6865 incremented the next time, or we would have to implement a
6866 decrement position function which would not be easy to write. */
6868 void
6869 set_iterator_to_next (struct it *it, int reseat_p)
6871 /* Reset flags indicating start and end of a sequence of characters
6872 with box. Reset them at the start of this function because
6873 moving the iterator to a new position might set them. */
6874 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6876 switch (it->method)
6878 case GET_FROM_BUFFER:
6879 /* The current display element of IT is a character from
6880 current_buffer. Advance in the buffer, and maybe skip over
6881 invisible lines that are so because of selective display. */
6882 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6883 reseat_at_next_visible_line_start (it, 0);
6884 else if (it->cmp_it.id >= 0)
6886 /* We are currently getting glyphs from a composition. */
6887 int i;
6889 if (! it->bidi_p)
6891 IT_CHARPOS (*it) += it->cmp_it.nchars;
6892 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6893 if (it->cmp_it.to < it->cmp_it.nglyphs)
6895 it->cmp_it.from = it->cmp_it.to;
6897 else
6899 it->cmp_it.id = -1;
6900 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6901 IT_BYTEPOS (*it),
6902 it->end_charpos, Qnil);
6905 else if (! it->cmp_it.reversed_p)
6907 /* Composition created while scanning forward. */
6908 /* Update IT's char/byte positions to point to the first
6909 character of the next grapheme cluster, or to the
6910 character visually after the current composition. */
6911 for (i = 0; i < it->cmp_it.nchars; i++)
6912 bidi_move_to_visually_next (&it->bidi_it);
6913 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6914 IT_CHARPOS (*it) = it->bidi_it.charpos;
6916 if (it->cmp_it.to < it->cmp_it.nglyphs)
6918 /* Proceed to the next grapheme cluster. */
6919 it->cmp_it.from = it->cmp_it.to;
6921 else
6923 /* No more grapheme clusters in this composition.
6924 Find the next stop position. */
6925 EMACS_INT stop = it->end_charpos;
6926 if (it->bidi_it.scan_dir < 0)
6927 /* Now we are scanning backward and don't know
6928 where to stop. */
6929 stop = -1;
6930 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6931 IT_BYTEPOS (*it), stop, Qnil);
6934 else
6936 /* Composition created while scanning backward. */
6937 /* Update IT's char/byte positions to point to the last
6938 character of the previous grapheme cluster, or the
6939 character visually after the current composition. */
6940 for (i = 0; i < it->cmp_it.nchars; i++)
6941 bidi_move_to_visually_next (&it->bidi_it);
6942 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6943 IT_CHARPOS (*it) = it->bidi_it.charpos;
6944 if (it->cmp_it.from > 0)
6946 /* Proceed to the previous grapheme cluster. */
6947 it->cmp_it.to = it->cmp_it.from;
6949 else
6951 /* No more grapheme clusters in this composition.
6952 Find the next stop position. */
6953 EMACS_INT stop = it->end_charpos;
6954 if (it->bidi_it.scan_dir < 0)
6955 /* Now we are scanning backward and don't know
6956 where to stop. */
6957 stop = -1;
6958 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6959 IT_BYTEPOS (*it), stop, Qnil);
6963 else
6965 xassert (it->len != 0);
6967 if (!it->bidi_p)
6969 IT_BYTEPOS (*it) += it->len;
6970 IT_CHARPOS (*it) += 1;
6972 else
6974 int prev_scan_dir = it->bidi_it.scan_dir;
6975 /* If this is a new paragraph, determine its base
6976 direction (a.k.a. its base embedding level). */
6977 if (it->bidi_it.new_paragraph)
6978 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6979 bidi_move_to_visually_next (&it->bidi_it);
6980 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6981 IT_CHARPOS (*it) = it->bidi_it.charpos;
6982 if (prev_scan_dir != it->bidi_it.scan_dir)
6984 /* As the scan direction was changed, we must
6985 re-compute the stop position for composition. */
6986 EMACS_INT stop = it->end_charpos;
6987 if (it->bidi_it.scan_dir < 0)
6988 stop = -1;
6989 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6990 IT_BYTEPOS (*it), stop, Qnil);
6993 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6995 break;
6997 case GET_FROM_C_STRING:
6998 /* Current display element of IT is from a C string. */
6999 if (!it->bidi_p
7000 /* If the string position is beyond string's end, it means
7001 next_element_from_c_string is padding the string with
7002 blanks, in which case we bypass the bidi iterator,
7003 because it cannot deal with such virtual characters. */
7004 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7006 IT_BYTEPOS (*it) += it->len;
7007 IT_CHARPOS (*it) += 1;
7009 else
7011 bidi_move_to_visually_next (&it->bidi_it);
7012 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7013 IT_CHARPOS (*it) = it->bidi_it.charpos;
7015 break;
7017 case GET_FROM_DISPLAY_VECTOR:
7018 /* Current display element of IT is from a display table entry.
7019 Advance in the display table definition. Reset it to null if
7020 end reached, and continue with characters from buffers/
7021 strings. */
7022 ++it->current.dpvec_index;
7024 /* Restore face of the iterator to what they were before the
7025 display vector entry (these entries may contain faces). */
7026 it->face_id = it->saved_face_id;
7028 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7030 int recheck_faces = it->ellipsis_p;
7032 if (it->s)
7033 it->method = GET_FROM_C_STRING;
7034 else if (STRINGP (it->string))
7035 it->method = GET_FROM_STRING;
7036 else
7038 it->method = GET_FROM_BUFFER;
7039 it->object = it->w->buffer;
7042 it->dpvec = NULL;
7043 it->current.dpvec_index = -1;
7045 /* Skip over characters which were displayed via IT->dpvec. */
7046 if (it->dpvec_char_len < 0)
7047 reseat_at_next_visible_line_start (it, 1);
7048 else if (it->dpvec_char_len > 0)
7050 if (it->method == GET_FROM_STRING
7051 && it->n_overlay_strings > 0)
7052 it->ignore_overlay_strings_at_pos_p = 1;
7053 it->len = it->dpvec_char_len;
7054 set_iterator_to_next (it, reseat_p);
7057 /* Maybe recheck faces after display vector */
7058 if (recheck_faces)
7059 it->stop_charpos = IT_CHARPOS (*it);
7061 break;
7063 case GET_FROM_STRING:
7064 /* Current display element is a character from a Lisp string. */
7065 xassert (it->s == NULL && STRINGP (it->string));
7066 /* Don't advance past string end. These conditions are true
7067 when set_iterator_to_next is called at the end of
7068 get_next_display_element, in which case the Lisp string is
7069 already exhausted, and all we want is pop the iterator
7070 stack. */
7071 if (it->current.overlay_string_index >= 0)
7073 /* This is an overlay string, so there's no padding with
7074 spaces, and the number of characters in the string is
7075 where the string ends. */
7076 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7077 goto consider_string_end;
7079 else
7081 /* Not an overlay string. There could be padding, so test
7082 against it->end_charpos . */
7083 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7084 goto consider_string_end;
7086 if (it->cmp_it.id >= 0)
7088 int i;
7090 if (! it->bidi_p)
7092 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7093 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7094 if (it->cmp_it.to < it->cmp_it.nglyphs)
7095 it->cmp_it.from = it->cmp_it.to;
7096 else
7098 it->cmp_it.id = -1;
7099 composition_compute_stop_pos (&it->cmp_it,
7100 IT_STRING_CHARPOS (*it),
7101 IT_STRING_BYTEPOS (*it),
7102 it->end_charpos, it->string);
7105 else if (! it->cmp_it.reversed_p)
7107 for (i = 0; i < it->cmp_it.nchars; i++)
7108 bidi_move_to_visually_next (&it->bidi_it);
7109 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7110 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7112 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 it->cmp_it.from = it->cmp_it.to;
7114 else
7116 EMACS_INT stop = it->end_charpos;
7117 if (it->bidi_it.scan_dir < 0)
7118 stop = -1;
7119 composition_compute_stop_pos (&it->cmp_it,
7120 IT_STRING_CHARPOS (*it),
7121 IT_STRING_BYTEPOS (*it), stop,
7122 it->string);
7125 else
7127 for (i = 0; i < it->cmp_it.nchars; i++)
7128 bidi_move_to_visually_next (&it->bidi_it);
7129 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7130 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7131 if (it->cmp_it.from > 0)
7132 it->cmp_it.to = it->cmp_it.from;
7133 else
7135 EMACS_INT stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 stop = -1;
7138 composition_compute_stop_pos (&it->cmp_it,
7139 IT_STRING_CHARPOS (*it),
7140 IT_STRING_BYTEPOS (*it), stop,
7141 it->string);
7145 else
7147 if (!it->bidi_p
7148 /* If the string position is beyond string's end, it
7149 means next_element_from_string is padding the string
7150 with blanks, in which case we bypass the bidi
7151 iterator, because it cannot deal with such virtual
7152 characters. */
7153 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7155 IT_STRING_BYTEPOS (*it) += it->len;
7156 IT_STRING_CHARPOS (*it) += 1;
7158 else
7160 int prev_scan_dir = it->bidi_it.scan_dir;
7162 bidi_move_to_visually_next (&it->bidi_it);
7163 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7164 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7165 if (prev_scan_dir != it->bidi_it.scan_dir)
7167 EMACS_INT stop = it->end_charpos;
7169 if (it->bidi_it.scan_dir < 0)
7170 stop = -1;
7171 composition_compute_stop_pos (&it->cmp_it,
7172 IT_STRING_CHARPOS (*it),
7173 IT_STRING_BYTEPOS (*it), stop,
7174 it->string);
7179 consider_string_end:
7181 if (it->current.overlay_string_index >= 0)
7183 /* IT->string is an overlay string. Advance to the
7184 next, if there is one. */
7185 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7187 it->ellipsis_p = 0;
7188 next_overlay_string (it);
7189 if (it->ellipsis_p)
7190 setup_for_ellipsis (it, 0);
7193 else
7195 /* IT->string is not an overlay string. If we reached
7196 its end, and there is something on IT->stack, proceed
7197 with what is on the stack. This can be either another
7198 string, this time an overlay string, or a buffer. */
7199 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7200 && it->sp > 0)
7202 pop_it (it);
7203 if (it->method == GET_FROM_STRING)
7204 goto consider_string_end;
7207 break;
7209 case GET_FROM_IMAGE:
7210 case GET_FROM_STRETCH:
7211 /* The position etc with which we have to proceed are on
7212 the stack. The position may be at the end of a string,
7213 if the `display' property takes up the whole string. */
7214 xassert (it->sp > 0);
7215 pop_it (it);
7216 if (it->method == GET_FROM_STRING)
7217 goto consider_string_end;
7218 break;
7220 default:
7221 /* There are no other methods defined, so this should be a bug. */
7222 abort ();
7225 xassert (it->method != GET_FROM_STRING
7226 || (STRINGP (it->string)
7227 && IT_STRING_CHARPOS (*it) >= 0));
7230 /* Load IT's display element fields with information about the next
7231 display element which comes from a display table entry or from the
7232 result of translating a control character to one of the forms `^C'
7233 or `\003'.
7235 IT->dpvec holds the glyphs to return as characters.
7236 IT->saved_face_id holds the face id before the display vector--it
7237 is restored into IT->face_id in set_iterator_to_next. */
7239 static int
7240 next_element_from_display_vector (struct it *it)
7242 Lisp_Object gc;
7244 /* Precondition. */
7245 xassert (it->dpvec && it->current.dpvec_index >= 0);
7247 it->face_id = it->saved_face_id;
7249 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7250 That seemed totally bogus - so I changed it... */
7251 gc = it->dpvec[it->current.dpvec_index];
7253 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7255 it->c = GLYPH_CODE_CHAR (gc);
7256 it->len = CHAR_BYTES (it->c);
7258 /* The entry may contain a face id to use. Such a face id is
7259 the id of a Lisp face, not a realized face. A face id of
7260 zero means no face is specified. */
7261 if (it->dpvec_face_id >= 0)
7262 it->face_id = it->dpvec_face_id;
7263 else
7265 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7266 if (lface_id > 0)
7267 it->face_id = merge_faces (it->f, Qt, lface_id,
7268 it->saved_face_id);
7271 else
7272 /* Display table entry is invalid. Return a space. */
7273 it->c = ' ', it->len = 1;
7275 /* Don't change position and object of the iterator here. They are
7276 still the values of the character that had this display table
7277 entry or was translated, and that's what we want. */
7278 it->what = IT_CHARACTER;
7279 return 1;
7282 /* Get the first element of string/buffer in the visual order, after
7283 being reseated to a new position in a string or a buffer. */
7284 static void
7285 get_visually_first_element (struct it *it)
7287 int string_p = STRINGP (it->string) || it->s;
7288 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7289 EMACS_INT bob = (string_p ? 0 : BEGV);
7291 if (STRINGP (it->string))
7293 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7294 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7296 else
7298 it->bidi_it.charpos = IT_CHARPOS (*it);
7299 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7302 if (it->bidi_it.charpos == eob)
7304 /* Nothing to do, but reset the FIRST_ELT flag, like
7305 bidi_paragraph_init does, because we are not going to
7306 call it. */
7307 it->bidi_it.first_elt = 0;
7309 else if (it->bidi_it.charpos == bob
7310 || (!string_p
7311 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7312 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7314 /* If we are at the beginning of a line/string, we can produce
7315 the next element right away. */
7316 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7317 bidi_move_to_visually_next (&it->bidi_it);
7319 else
7321 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7323 /* We need to prime the bidi iterator starting at the line's or
7324 string's beginning, before we will be able to produce the
7325 next element. */
7326 if (string_p)
7327 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7328 else
7330 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7331 -1);
7332 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7334 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7337 /* Now return to buffer/string position where we were asked
7338 to get the next display element, and produce that. */
7339 bidi_move_to_visually_next (&it->bidi_it);
7341 while (it->bidi_it.bytepos != orig_bytepos
7342 && it->bidi_it.charpos < eob);
7345 /* Adjust IT's position information to where we ended up. */
7346 if (STRINGP (it->string))
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7349 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7351 else
7353 IT_CHARPOS (*it) = it->bidi_it.charpos;
7354 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7357 if (STRINGP (it->string) || !it->s)
7359 EMACS_INT stop, charpos, bytepos;
7361 if (STRINGP (it->string))
7363 xassert (!it->s);
7364 stop = SCHARS (it->string);
7365 if (stop > it->end_charpos)
7366 stop = it->end_charpos;
7367 charpos = IT_STRING_CHARPOS (*it);
7368 bytepos = IT_STRING_BYTEPOS (*it);
7370 else
7372 stop = it->end_charpos;
7373 charpos = IT_CHARPOS (*it);
7374 bytepos = IT_BYTEPOS (*it);
7376 if (it->bidi_it.scan_dir < 0)
7377 stop = -1;
7378 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7379 it->string);
7383 /* Load IT with the next display element from Lisp string IT->string.
7384 IT->current.string_pos is the current position within the string.
7385 If IT->current.overlay_string_index >= 0, the Lisp string is an
7386 overlay string. */
7388 static int
7389 next_element_from_string (struct it *it)
7391 struct text_pos position;
7393 xassert (STRINGP (it->string));
7394 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7395 xassert (IT_STRING_CHARPOS (*it) >= 0);
7396 position = it->current.string_pos;
7398 /* With bidi reordering, the character to display might not be the
7399 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7400 that we were reseat()ed to a new string, whose paragraph
7401 direction is not known. */
7402 if (it->bidi_p && it->bidi_it.first_elt)
7404 get_visually_first_element (it);
7405 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7408 /* Time to check for invisible text? */
7409 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7411 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7413 if (!(!it->bidi_p
7414 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7415 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7417 /* With bidi non-linear iteration, we could find
7418 ourselves far beyond the last computed stop_charpos,
7419 with several other stop positions in between that we
7420 missed. Scan them all now, in buffer's logical
7421 order, until we find and handle the last stop_charpos
7422 that precedes our current position. */
7423 handle_stop_backwards (it, it->stop_charpos);
7424 return GET_NEXT_DISPLAY_ELEMENT (it);
7426 else
7428 if (it->bidi_p)
7430 /* Take note of the stop position we just moved
7431 across, for when we will move back across it. */
7432 it->prev_stop = it->stop_charpos;
7433 /* If we are at base paragraph embedding level, take
7434 note of the last stop position seen at this
7435 level. */
7436 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7437 it->base_level_stop = it->stop_charpos;
7439 handle_stop (it);
7441 /* Since a handler may have changed IT->method, we must
7442 recurse here. */
7443 return GET_NEXT_DISPLAY_ELEMENT (it);
7446 else if (it->bidi_p
7447 /* If we are before prev_stop, we may have overstepped
7448 on our way backwards a stop_pos, and if so, we need
7449 to handle that stop_pos. */
7450 && IT_STRING_CHARPOS (*it) < it->prev_stop
7451 /* We can sometimes back up for reasons that have nothing
7452 to do with bidi reordering. E.g., compositions. The
7453 code below is only needed when we are above the base
7454 embedding level, so test for that explicitly. */
7455 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7457 /* If we lost track of base_level_stop, we have no better
7458 place for handle_stop_backwards to start from than string
7459 beginning. This happens, e.g., when we were reseated to
7460 the previous screenful of text by vertical-motion. */
7461 if (it->base_level_stop <= 0
7462 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7463 it->base_level_stop = 0;
7464 handle_stop_backwards (it, it->base_level_stop);
7465 return GET_NEXT_DISPLAY_ELEMENT (it);
7469 if (it->current.overlay_string_index >= 0)
7471 /* Get the next character from an overlay string. In overlay
7472 strings, there is no field width or padding with spaces to
7473 do. */
7474 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7476 it->what = IT_EOB;
7477 return 0;
7479 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7480 IT_STRING_BYTEPOS (*it),
7481 it->bidi_it.scan_dir < 0
7482 ? -1
7483 : SCHARS (it->string))
7484 && next_element_from_composition (it))
7486 return 1;
7488 else if (STRING_MULTIBYTE (it->string))
7490 const unsigned char *s = (SDATA (it->string)
7491 + IT_STRING_BYTEPOS (*it));
7492 it->c = string_char_and_length (s, &it->len);
7494 else
7496 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7497 it->len = 1;
7500 else
7502 /* Get the next character from a Lisp string that is not an
7503 overlay string. Such strings come from the mode line, for
7504 example. We may have to pad with spaces, or truncate the
7505 string. See also next_element_from_c_string. */
7506 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7508 it->what = IT_EOB;
7509 return 0;
7511 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7513 /* Pad with spaces. */
7514 it->c = ' ', it->len = 1;
7515 CHARPOS (position) = BYTEPOS (position) = -1;
7517 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7518 IT_STRING_BYTEPOS (*it),
7519 it->bidi_it.scan_dir < 0
7520 ? -1
7521 : it->string_nchars)
7522 && next_element_from_composition (it))
7524 return 1;
7526 else if (STRING_MULTIBYTE (it->string))
7528 const unsigned char *s = (SDATA (it->string)
7529 + IT_STRING_BYTEPOS (*it));
7530 it->c = string_char_and_length (s, &it->len);
7532 else
7534 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7535 it->len = 1;
7539 /* Record what we have and where it came from. */
7540 it->what = IT_CHARACTER;
7541 it->object = it->string;
7542 it->position = position;
7543 return 1;
7547 /* Load IT with next display element from C string IT->s.
7548 IT->string_nchars is the maximum number of characters to return
7549 from the string. IT->end_charpos may be greater than
7550 IT->string_nchars when this function is called, in which case we
7551 may have to return padding spaces. Value is zero if end of string
7552 reached, including padding spaces. */
7554 static int
7555 next_element_from_c_string (struct it *it)
7557 int success_p = 1;
7559 xassert (it->s);
7560 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7561 it->what = IT_CHARACTER;
7562 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7563 it->object = Qnil;
7565 /* With bidi reordering, the character to display might not be the
7566 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7567 we were reseated to a new string, whose paragraph direction is
7568 not known. */
7569 if (it->bidi_p && it->bidi_it.first_elt)
7570 get_visually_first_element (it);
7572 /* IT's position can be greater than IT->string_nchars in case a
7573 field width or precision has been specified when the iterator was
7574 initialized. */
7575 if (IT_CHARPOS (*it) >= it->end_charpos)
7577 /* End of the game. */
7578 it->what = IT_EOB;
7579 success_p = 0;
7581 else if (IT_CHARPOS (*it) >= it->string_nchars)
7583 /* Pad with spaces. */
7584 it->c = ' ', it->len = 1;
7585 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7587 else if (it->multibyte_p)
7588 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7589 else
7590 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7592 return success_p;
7596 /* Set up IT to return characters from an ellipsis, if appropriate.
7597 The definition of the ellipsis glyphs may come from a display table
7598 entry. This function fills IT with the first glyph from the
7599 ellipsis if an ellipsis is to be displayed. */
7601 static int
7602 next_element_from_ellipsis (struct it *it)
7604 if (it->selective_display_ellipsis_p)
7605 setup_for_ellipsis (it, it->len);
7606 else
7608 /* The face at the current position may be different from the
7609 face we find after the invisible text. Remember what it
7610 was in IT->saved_face_id, and signal that it's there by
7611 setting face_before_selective_p. */
7612 it->saved_face_id = it->face_id;
7613 it->method = GET_FROM_BUFFER;
7614 it->object = it->w->buffer;
7615 reseat_at_next_visible_line_start (it, 1);
7616 it->face_before_selective_p = 1;
7619 return GET_NEXT_DISPLAY_ELEMENT (it);
7623 /* Deliver an image display element. The iterator IT is already
7624 filled with image information (done in handle_display_prop). Value
7625 is always 1. */
7628 static int
7629 next_element_from_image (struct it *it)
7631 it->what = IT_IMAGE;
7632 it->ignore_overlay_strings_at_pos_p = 0;
7633 return 1;
7637 /* Fill iterator IT with next display element from a stretch glyph
7638 property. IT->object is the value of the text property. Value is
7639 always 1. */
7641 static int
7642 next_element_from_stretch (struct it *it)
7644 it->what = IT_STRETCH;
7645 return 1;
7648 /* Scan backwards from IT's current position until we find a stop
7649 position, or until BEGV. This is called when we find ourself
7650 before both the last known prev_stop and base_level_stop while
7651 reordering bidirectional text. */
7653 static void
7654 compute_stop_pos_backwards (struct it *it)
7656 const int SCAN_BACK_LIMIT = 1000;
7657 struct text_pos pos;
7658 struct display_pos save_current = it->current;
7659 struct text_pos save_position = it->position;
7660 EMACS_INT charpos = IT_CHARPOS (*it);
7661 EMACS_INT where_we_are = charpos;
7662 EMACS_INT save_stop_pos = it->stop_charpos;
7663 EMACS_INT save_end_pos = it->end_charpos;
7665 xassert (NILP (it->string) && !it->s);
7666 xassert (it->bidi_p);
7667 it->bidi_p = 0;
7670 it->end_charpos = min (charpos + 1, ZV);
7671 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7672 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7673 reseat_1 (it, pos, 0);
7674 compute_stop_pos (it);
7675 /* We must advance forward, right? */
7676 if (it->stop_charpos <= charpos)
7677 abort ();
7679 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7681 if (it->stop_charpos <= where_we_are)
7682 it->prev_stop = it->stop_charpos;
7683 else
7684 it->prev_stop = BEGV;
7685 it->bidi_p = 1;
7686 it->current = save_current;
7687 it->position = save_position;
7688 it->stop_charpos = save_stop_pos;
7689 it->end_charpos = save_end_pos;
7692 /* Scan forward from CHARPOS in the current buffer/string, until we
7693 find a stop position > current IT's position. Then handle the stop
7694 position before that. This is called when we bump into a stop
7695 position while reordering bidirectional text. CHARPOS should be
7696 the last previously processed stop_pos (or BEGV/0, if none were
7697 processed yet) whose position is less that IT's current
7698 position. */
7700 static void
7701 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7703 int bufp = !STRINGP (it->string);
7704 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7705 struct display_pos save_current = it->current;
7706 struct text_pos save_position = it->position;
7707 struct text_pos pos1;
7708 EMACS_INT next_stop;
7710 /* Scan in strict logical order. */
7711 xassert (it->bidi_p);
7712 it->bidi_p = 0;
7715 it->prev_stop = charpos;
7716 if (bufp)
7718 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7719 reseat_1 (it, pos1, 0);
7721 else
7722 it->current.string_pos = string_pos (charpos, it->string);
7723 compute_stop_pos (it);
7724 /* We must advance forward, right? */
7725 if (it->stop_charpos <= it->prev_stop)
7726 abort ();
7727 charpos = it->stop_charpos;
7729 while (charpos <= where_we_are);
7731 it->bidi_p = 1;
7732 it->current = save_current;
7733 it->position = save_position;
7734 next_stop = it->stop_charpos;
7735 it->stop_charpos = it->prev_stop;
7736 handle_stop (it);
7737 it->stop_charpos = next_stop;
7740 /* Load IT with the next display element from current_buffer. Value
7741 is zero if end of buffer reached. IT->stop_charpos is the next
7742 position at which to stop and check for text properties or buffer
7743 end. */
7745 static int
7746 next_element_from_buffer (struct it *it)
7748 int success_p = 1;
7750 xassert (IT_CHARPOS (*it) >= BEGV);
7751 xassert (NILP (it->string) && !it->s);
7752 xassert (!it->bidi_p
7753 || (EQ (it->bidi_it.string.lstring, Qnil)
7754 && it->bidi_it.string.s == NULL));
7756 /* With bidi reordering, the character to display might not be the
7757 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7758 we were reseat()ed to a new buffer position, which is potentially
7759 a different paragraph. */
7760 if (it->bidi_p && it->bidi_it.first_elt)
7762 get_visually_first_element (it);
7763 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7766 if (IT_CHARPOS (*it) >= it->stop_charpos)
7768 if (IT_CHARPOS (*it) >= it->end_charpos)
7770 int overlay_strings_follow_p;
7772 /* End of the game, except when overlay strings follow that
7773 haven't been returned yet. */
7774 if (it->overlay_strings_at_end_processed_p)
7775 overlay_strings_follow_p = 0;
7776 else
7778 it->overlay_strings_at_end_processed_p = 1;
7779 overlay_strings_follow_p = get_overlay_strings (it, 0);
7782 if (overlay_strings_follow_p)
7783 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7784 else
7786 it->what = IT_EOB;
7787 it->position = it->current.pos;
7788 success_p = 0;
7791 else if (!(!it->bidi_p
7792 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7793 || IT_CHARPOS (*it) == it->stop_charpos))
7795 /* With bidi non-linear iteration, we could find ourselves
7796 far beyond the last computed stop_charpos, with several
7797 other stop positions in between that we missed. Scan
7798 them all now, in buffer's logical order, until we find
7799 and handle the last stop_charpos that precedes our
7800 current position. */
7801 handle_stop_backwards (it, it->stop_charpos);
7802 return GET_NEXT_DISPLAY_ELEMENT (it);
7804 else
7806 if (it->bidi_p)
7808 /* Take note of the stop position we just moved across,
7809 for when we will move back across it. */
7810 it->prev_stop = it->stop_charpos;
7811 /* If we are at base paragraph embedding level, take
7812 note of the last stop position seen at this
7813 level. */
7814 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7815 it->base_level_stop = it->stop_charpos;
7817 handle_stop (it);
7818 return GET_NEXT_DISPLAY_ELEMENT (it);
7821 else if (it->bidi_p
7822 /* If we are before prev_stop, we may have overstepped on
7823 our way backwards a stop_pos, and if so, we need to
7824 handle that stop_pos. */
7825 && IT_CHARPOS (*it) < it->prev_stop
7826 /* We can sometimes back up for reasons that have nothing
7827 to do with bidi reordering. E.g., compositions. The
7828 code below is only needed when we are above the base
7829 embedding level, so test for that explicitly. */
7830 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7832 if (it->base_level_stop <= 0
7833 || IT_CHARPOS (*it) < it->base_level_stop)
7835 /* If we lost track of base_level_stop, we need to find
7836 prev_stop by looking backwards. This happens, e.g., when
7837 we were reseated to the previous screenful of text by
7838 vertical-motion. */
7839 it->base_level_stop = BEGV;
7840 compute_stop_pos_backwards (it);
7841 handle_stop_backwards (it, it->prev_stop);
7843 else
7844 handle_stop_backwards (it, it->base_level_stop);
7845 return GET_NEXT_DISPLAY_ELEMENT (it);
7847 else
7849 /* No face changes, overlays etc. in sight, so just return a
7850 character from current_buffer. */
7851 unsigned char *p;
7852 EMACS_INT stop;
7854 /* Maybe run the redisplay end trigger hook. Performance note:
7855 This doesn't seem to cost measurable time. */
7856 if (it->redisplay_end_trigger_charpos
7857 && it->glyph_row
7858 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7859 run_redisplay_end_trigger_hook (it);
7861 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7862 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7863 stop)
7864 && next_element_from_composition (it))
7866 return 1;
7869 /* Get the next character, maybe multibyte. */
7870 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7871 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7872 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7873 else
7874 it->c = *p, it->len = 1;
7876 /* Record what we have and where it came from. */
7877 it->what = IT_CHARACTER;
7878 it->object = it->w->buffer;
7879 it->position = it->current.pos;
7881 /* Normally we return the character found above, except when we
7882 really want to return an ellipsis for selective display. */
7883 if (it->selective)
7885 if (it->c == '\n')
7887 /* A value of selective > 0 means hide lines indented more
7888 than that number of columns. */
7889 if (it->selective > 0
7890 && IT_CHARPOS (*it) + 1 < ZV
7891 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7892 IT_BYTEPOS (*it) + 1,
7893 it->selective))
7895 success_p = next_element_from_ellipsis (it);
7896 it->dpvec_char_len = -1;
7899 else if (it->c == '\r' && it->selective == -1)
7901 /* A value of selective == -1 means that everything from the
7902 CR to the end of the line is invisible, with maybe an
7903 ellipsis displayed for it. */
7904 success_p = next_element_from_ellipsis (it);
7905 it->dpvec_char_len = -1;
7910 /* Value is zero if end of buffer reached. */
7911 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7912 return success_p;
7916 /* Run the redisplay end trigger hook for IT. */
7918 static void
7919 run_redisplay_end_trigger_hook (struct it *it)
7921 Lisp_Object args[3];
7923 /* IT->glyph_row should be non-null, i.e. we should be actually
7924 displaying something, or otherwise we should not run the hook. */
7925 xassert (it->glyph_row);
7927 /* Set up hook arguments. */
7928 args[0] = Qredisplay_end_trigger_functions;
7929 args[1] = it->window;
7930 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7931 it->redisplay_end_trigger_charpos = 0;
7933 /* Since we are *trying* to run these functions, don't try to run
7934 them again, even if they get an error. */
7935 it->w->redisplay_end_trigger = Qnil;
7936 Frun_hook_with_args (3, args);
7938 /* Notice if it changed the face of the character we are on. */
7939 handle_face_prop (it);
7943 /* Deliver a composition display element. Unlike the other
7944 next_element_from_XXX, this function is not registered in the array
7945 get_next_element[]. It is called from next_element_from_buffer and
7946 next_element_from_string when necessary. */
7948 static int
7949 next_element_from_composition (struct it *it)
7951 it->what = IT_COMPOSITION;
7952 it->len = it->cmp_it.nbytes;
7953 if (STRINGP (it->string))
7955 if (it->c < 0)
7957 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7958 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7959 return 0;
7961 it->position = it->current.string_pos;
7962 it->object = it->string;
7963 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7964 IT_STRING_BYTEPOS (*it), it->string);
7966 else
7968 if (it->c < 0)
7970 IT_CHARPOS (*it) += it->cmp_it.nchars;
7971 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7972 if (it->bidi_p)
7974 if (it->bidi_it.new_paragraph)
7975 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7976 /* Resync the bidi iterator with IT's new position.
7977 FIXME: this doesn't support bidirectional text. */
7978 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7979 bidi_move_to_visually_next (&it->bidi_it);
7981 return 0;
7983 it->position = it->current.pos;
7984 it->object = it->w->buffer;
7985 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7986 IT_BYTEPOS (*it), Qnil);
7988 return 1;
7993 /***********************************************************************
7994 Moving an iterator without producing glyphs
7995 ***********************************************************************/
7997 /* Check if iterator is at a position corresponding to a valid buffer
7998 position after some move_it_ call. */
8000 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8001 ((it)->method == GET_FROM_STRING \
8002 ? IT_STRING_CHARPOS (*it) == 0 \
8003 : 1)
8006 /* Move iterator IT to a specified buffer or X position within one
8007 line on the display without producing glyphs.
8009 OP should be a bit mask including some or all of these bits:
8010 MOVE_TO_X: Stop upon reaching x-position TO_X.
8011 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8012 Regardless of OP's value, stop upon reaching the end of the display line.
8014 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8015 This means, in particular, that TO_X includes window's horizontal
8016 scroll amount.
8018 The return value has several possible values that
8019 say what condition caused the scan to stop:
8021 MOVE_POS_MATCH_OR_ZV
8022 - when TO_POS or ZV was reached.
8024 MOVE_X_REACHED
8025 -when TO_X was reached before TO_POS or ZV were reached.
8027 MOVE_LINE_CONTINUED
8028 - when we reached the end of the display area and the line must
8029 be continued.
8031 MOVE_LINE_TRUNCATED
8032 - when we reached the end of the display area and the line is
8033 truncated.
8035 MOVE_NEWLINE_OR_CR
8036 - when we stopped at a line end, i.e. a newline or a CR and selective
8037 display is on. */
8039 static enum move_it_result
8040 move_it_in_display_line_to (struct it *it,
8041 EMACS_INT to_charpos, int to_x,
8042 enum move_operation_enum op)
8044 enum move_it_result result = MOVE_UNDEFINED;
8045 struct glyph_row *saved_glyph_row;
8046 struct it wrap_it, atpos_it, atx_it, ppos_it;
8047 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8048 void *ppos_data = NULL;
8049 int may_wrap = 0;
8050 enum it_method prev_method = it->method;
8051 EMACS_INT prev_pos = IT_CHARPOS (*it);
8052 int saw_smaller_pos = prev_pos < to_charpos;
8054 /* Don't produce glyphs in produce_glyphs. */
8055 saved_glyph_row = it->glyph_row;
8056 it->glyph_row = NULL;
8058 /* Use wrap_it to save a copy of IT wherever a word wrap could
8059 occur. Use atpos_it to save a copy of IT at the desired buffer
8060 position, if found, so that we can scan ahead and check if the
8061 word later overshoots the window edge. Use atx_it similarly, for
8062 pixel positions. */
8063 wrap_it.sp = -1;
8064 atpos_it.sp = -1;
8065 atx_it.sp = -1;
8067 /* Use ppos_it under bidi reordering to save a copy of IT for the
8068 position > CHARPOS that is the closest to CHARPOS. We restore
8069 that position in IT when we have scanned the entire display line
8070 without finding a match for CHARPOS and all the character
8071 positions are greater than CHARPOS. */
8072 if (it->bidi_p)
8074 SAVE_IT (ppos_it, *it, ppos_data);
8075 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8076 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8077 SAVE_IT (ppos_it, *it, ppos_data);
8080 #define BUFFER_POS_REACHED_P() \
8081 ((op & MOVE_TO_POS) != 0 \
8082 && BUFFERP (it->object) \
8083 && (IT_CHARPOS (*it) == to_charpos \
8084 || ((!it->bidi_p \
8085 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8086 && IT_CHARPOS (*it) > to_charpos) \
8087 || (it->what == IT_COMPOSITION \
8088 && ((IT_CHARPOS (*it) > to_charpos \
8089 && to_charpos >= it->cmp_it.charpos) \
8090 || (IT_CHARPOS (*it) < to_charpos \
8091 && to_charpos <= it->cmp_it.charpos)))) \
8092 && (it->method == GET_FROM_BUFFER \
8093 || (it->method == GET_FROM_DISPLAY_VECTOR \
8094 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8096 /* If there's a line-/wrap-prefix, handle it. */
8097 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8098 && it->current_y < it->last_visible_y)
8099 handle_line_prefix (it);
8101 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8102 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8104 while (1)
8106 int x, i, ascent = 0, descent = 0;
8108 /* Utility macro to reset an iterator with x, ascent, and descent. */
8109 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8110 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8111 (IT)->max_descent = descent)
8113 /* Stop if we move beyond TO_CHARPOS (after an image or a
8114 display string or stretch glyph). */
8115 if ((op & MOVE_TO_POS) != 0
8116 && BUFFERP (it->object)
8117 && it->method == GET_FROM_BUFFER
8118 && (((!it->bidi_p
8119 /* When the iterator is at base embedding level, we
8120 are guaranteed that characters are delivered for
8121 display in strictly increasing order of their
8122 buffer positions. */
8123 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8124 && IT_CHARPOS (*it) > to_charpos)
8125 || (it->bidi_p
8126 && (prev_method == GET_FROM_IMAGE
8127 || prev_method == GET_FROM_STRETCH
8128 || prev_method == GET_FROM_STRING)
8129 /* Passed TO_CHARPOS from left to right. */
8130 && ((prev_pos < to_charpos
8131 && IT_CHARPOS (*it) > to_charpos)
8132 /* Passed TO_CHARPOS from right to left. */
8133 || (prev_pos > to_charpos
8134 && IT_CHARPOS (*it) < to_charpos)))))
8136 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8138 result = MOVE_POS_MATCH_OR_ZV;
8139 break;
8141 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8142 /* If wrap_it is valid, the current position might be in a
8143 word that is wrapped. So, save the iterator in
8144 atpos_it and continue to see if wrapping happens. */
8145 SAVE_IT (atpos_it, *it, atpos_data);
8148 /* Stop when ZV reached.
8149 We used to stop here when TO_CHARPOS reached as well, but that is
8150 too soon if this glyph does not fit on this line. So we handle it
8151 explicitly below. */
8152 if (!get_next_display_element (it))
8154 result = MOVE_POS_MATCH_OR_ZV;
8155 break;
8158 if (it->line_wrap == TRUNCATE)
8160 if (BUFFER_POS_REACHED_P ())
8162 result = MOVE_POS_MATCH_OR_ZV;
8163 break;
8166 else
8168 if (it->line_wrap == WORD_WRAP)
8170 if (IT_DISPLAYING_WHITESPACE (it))
8171 may_wrap = 1;
8172 else if (may_wrap)
8174 /* We have reached a glyph that follows one or more
8175 whitespace characters. If the position is
8176 already found, we are done. */
8177 if (atpos_it.sp >= 0)
8179 RESTORE_IT (it, &atpos_it, atpos_data);
8180 result = MOVE_POS_MATCH_OR_ZV;
8181 goto done;
8183 if (atx_it.sp >= 0)
8185 RESTORE_IT (it, &atx_it, atx_data);
8186 result = MOVE_X_REACHED;
8187 goto done;
8189 /* Otherwise, we can wrap here. */
8190 SAVE_IT (wrap_it, *it, wrap_data);
8191 may_wrap = 0;
8196 /* Remember the line height for the current line, in case
8197 the next element doesn't fit on the line. */
8198 ascent = it->max_ascent;
8199 descent = it->max_descent;
8201 /* The call to produce_glyphs will get the metrics of the
8202 display element IT is loaded with. Record the x-position
8203 before this display element, in case it doesn't fit on the
8204 line. */
8205 x = it->current_x;
8207 PRODUCE_GLYPHS (it);
8209 if (it->area != TEXT_AREA)
8211 prev_method = it->method;
8212 if (it->method == GET_FROM_BUFFER)
8213 prev_pos = IT_CHARPOS (*it);
8214 set_iterator_to_next (it, 1);
8215 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8216 SET_TEXT_POS (this_line_min_pos,
8217 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8218 if (it->bidi_p
8219 && (op & MOVE_TO_POS)
8220 && IT_CHARPOS (*it) > to_charpos
8221 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8222 SAVE_IT (ppos_it, *it, ppos_data);
8223 continue;
8226 /* The number of glyphs we get back in IT->nglyphs will normally
8227 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8228 character on a terminal frame, or (iii) a line end. For the
8229 second case, IT->nglyphs - 1 padding glyphs will be present.
8230 (On X frames, there is only one glyph produced for a
8231 composite character.)
8233 The behavior implemented below means, for continuation lines,
8234 that as many spaces of a TAB as fit on the current line are
8235 displayed there. For terminal frames, as many glyphs of a
8236 multi-glyph character are displayed in the current line, too.
8237 This is what the old redisplay code did, and we keep it that
8238 way. Under X, the whole shape of a complex character must
8239 fit on the line or it will be completely displayed in the
8240 next line.
8242 Note that both for tabs and padding glyphs, all glyphs have
8243 the same width. */
8244 if (it->nglyphs)
8246 /* More than one glyph or glyph doesn't fit on line. All
8247 glyphs have the same width. */
8248 int single_glyph_width = it->pixel_width / it->nglyphs;
8249 int new_x;
8250 int x_before_this_char = x;
8251 int hpos_before_this_char = it->hpos;
8253 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8255 new_x = x + single_glyph_width;
8257 /* We want to leave anything reaching TO_X to the caller. */
8258 if ((op & MOVE_TO_X) && new_x > to_x)
8260 if (BUFFER_POS_REACHED_P ())
8262 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8263 goto buffer_pos_reached;
8264 if (atpos_it.sp < 0)
8266 SAVE_IT (atpos_it, *it, atpos_data);
8267 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8270 else
8272 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8274 it->current_x = x;
8275 result = MOVE_X_REACHED;
8276 break;
8278 if (atx_it.sp < 0)
8280 SAVE_IT (atx_it, *it, atx_data);
8281 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8286 if (/* Lines are continued. */
8287 it->line_wrap != TRUNCATE
8288 && (/* And glyph doesn't fit on the line. */
8289 new_x > it->last_visible_x
8290 /* Or it fits exactly and we're on a window
8291 system frame. */
8292 || (new_x == it->last_visible_x
8293 && FRAME_WINDOW_P (it->f))))
8295 if (/* IT->hpos == 0 means the very first glyph
8296 doesn't fit on the line, e.g. a wide image. */
8297 it->hpos == 0
8298 || (new_x == it->last_visible_x
8299 && FRAME_WINDOW_P (it->f)))
8301 ++it->hpos;
8302 it->current_x = new_x;
8304 /* The character's last glyph just barely fits
8305 in this row. */
8306 if (i == it->nglyphs - 1)
8308 /* If this is the destination position,
8309 return a position *before* it in this row,
8310 now that we know it fits in this row. */
8311 if (BUFFER_POS_REACHED_P ())
8313 if (it->line_wrap != WORD_WRAP
8314 || wrap_it.sp < 0)
8316 it->hpos = hpos_before_this_char;
8317 it->current_x = x_before_this_char;
8318 result = MOVE_POS_MATCH_OR_ZV;
8319 break;
8321 if (it->line_wrap == WORD_WRAP
8322 && atpos_it.sp < 0)
8324 SAVE_IT (atpos_it, *it, atpos_data);
8325 atpos_it.current_x = x_before_this_char;
8326 atpos_it.hpos = hpos_before_this_char;
8330 prev_method = it->method;
8331 if (it->method == GET_FROM_BUFFER)
8332 prev_pos = IT_CHARPOS (*it);
8333 set_iterator_to_next (it, 1);
8334 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8335 SET_TEXT_POS (this_line_min_pos,
8336 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8337 /* On graphical terminals, newlines may
8338 "overflow" into the fringe if
8339 overflow-newline-into-fringe is non-nil.
8340 On text-only terminals, newlines may
8341 overflow into the last glyph on the
8342 display line.*/
8343 if (!FRAME_WINDOW_P (it->f)
8344 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8346 if (!get_next_display_element (it))
8348 result = MOVE_POS_MATCH_OR_ZV;
8349 break;
8351 if (BUFFER_POS_REACHED_P ())
8353 if (ITERATOR_AT_END_OF_LINE_P (it))
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 else
8356 result = MOVE_LINE_CONTINUED;
8357 break;
8359 if (ITERATOR_AT_END_OF_LINE_P (it))
8361 result = MOVE_NEWLINE_OR_CR;
8362 break;
8367 else
8368 IT_RESET_X_ASCENT_DESCENT (it);
8370 if (wrap_it.sp >= 0)
8372 RESTORE_IT (it, &wrap_it, wrap_data);
8373 atpos_it.sp = -1;
8374 atx_it.sp = -1;
8377 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8378 IT_CHARPOS (*it)));
8379 result = MOVE_LINE_CONTINUED;
8380 break;
8383 if (BUFFER_POS_REACHED_P ())
8385 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8386 goto buffer_pos_reached;
8387 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8389 SAVE_IT (atpos_it, *it, atpos_data);
8390 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8394 if (new_x > it->first_visible_x)
8396 /* Glyph is visible. Increment number of glyphs that
8397 would be displayed. */
8398 ++it->hpos;
8402 if (result != MOVE_UNDEFINED)
8403 break;
8405 else if (BUFFER_POS_REACHED_P ())
8407 buffer_pos_reached:
8408 IT_RESET_X_ASCENT_DESCENT (it);
8409 result = MOVE_POS_MATCH_OR_ZV;
8410 break;
8412 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8414 /* Stop when TO_X specified and reached. This check is
8415 necessary here because of lines consisting of a line end,
8416 only. The line end will not produce any glyphs and we
8417 would never get MOVE_X_REACHED. */
8418 xassert (it->nglyphs == 0);
8419 result = MOVE_X_REACHED;
8420 break;
8423 /* Is this a line end? If yes, we're done. */
8424 if (ITERATOR_AT_END_OF_LINE_P (it))
8426 /* If we are past TO_CHARPOS, but never saw any character
8427 positions smaller than TO_CHARPOS, return
8428 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8429 did. */
8430 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8432 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8434 if (IT_CHARPOS (ppos_it) < ZV)
8436 RESTORE_IT (it, &ppos_it, ppos_data);
8437 result = MOVE_POS_MATCH_OR_ZV;
8439 else
8440 goto buffer_pos_reached;
8442 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8443 && IT_CHARPOS (*it) > to_charpos)
8444 goto buffer_pos_reached;
8445 else
8446 result = MOVE_NEWLINE_OR_CR;
8448 else
8449 result = MOVE_NEWLINE_OR_CR;
8450 break;
8453 prev_method = it->method;
8454 if (it->method == GET_FROM_BUFFER)
8455 prev_pos = IT_CHARPOS (*it);
8456 /* The current display element has been consumed. Advance
8457 to the next. */
8458 set_iterator_to_next (it, 1);
8459 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8460 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8461 if (IT_CHARPOS (*it) < to_charpos)
8462 saw_smaller_pos = 1;
8463 if (it->bidi_p
8464 && (op & MOVE_TO_POS)
8465 && IT_CHARPOS (*it) >= to_charpos
8466 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8467 SAVE_IT (ppos_it, *it, ppos_data);
8469 /* Stop if lines are truncated and IT's current x-position is
8470 past the right edge of the window now. */
8471 if (it->line_wrap == TRUNCATE
8472 && it->current_x >= it->last_visible_x)
8474 if (!FRAME_WINDOW_P (it->f)
8475 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8477 int at_eob_p = 0;
8479 if ((at_eob_p = !get_next_display_element (it))
8480 || BUFFER_POS_REACHED_P ()
8481 /* If we are past TO_CHARPOS, but never saw any
8482 character positions smaller than TO_CHARPOS,
8483 return MOVE_POS_MATCH_OR_ZV, like the
8484 unidirectional display did. */
8485 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8486 && !saw_smaller_pos
8487 && IT_CHARPOS (*it) > to_charpos))
8489 if (it->bidi_p
8490 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8491 RESTORE_IT (it, &ppos_it, ppos_data);
8492 result = MOVE_POS_MATCH_OR_ZV;
8493 break;
8495 if (ITERATOR_AT_END_OF_LINE_P (it))
8497 result = MOVE_NEWLINE_OR_CR;
8498 break;
8501 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8502 && !saw_smaller_pos
8503 && IT_CHARPOS (*it) > to_charpos)
8505 if (IT_CHARPOS (ppos_it) < ZV)
8506 RESTORE_IT (it, &ppos_it, ppos_data);
8507 result = MOVE_POS_MATCH_OR_ZV;
8508 break;
8510 result = MOVE_LINE_TRUNCATED;
8511 break;
8513 #undef IT_RESET_X_ASCENT_DESCENT
8516 #undef BUFFER_POS_REACHED_P
8518 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8519 restore the saved iterator. */
8520 if (atpos_it.sp >= 0)
8521 RESTORE_IT (it, &atpos_it, atpos_data);
8522 else if (atx_it.sp >= 0)
8523 RESTORE_IT (it, &atx_it, atx_data);
8525 done:
8527 if (atpos_data)
8528 bidi_unshelve_cache (atpos_data, 1);
8529 if (atx_data)
8530 bidi_unshelve_cache (atx_data, 1);
8531 if (wrap_data)
8532 bidi_unshelve_cache (wrap_data, 1);
8533 if (ppos_data)
8534 bidi_unshelve_cache (ppos_data, 1);
8536 /* Restore the iterator settings altered at the beginning of this
8537 function. */
8538 it->glyph_row = saved_glyph_row;
8539 return result;
8542 /* For external use. */
8543 void
8544 move_it_in_display_line (struct it *it,
8545 EMACS_INT to_charpos, int to_x,
8546 enum move_operation_enum op)
8548 if (it->line_wrap == WORD_WRAP
8549 && (op & MOVE_TO_X))
8551 struct it save_it;
8552 void *save_data = NULL;
8553 int skip;
8555 SAVE_IT (save_it, *it, save_data);
8556 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8557 /* When word-wrap is on, TO_X may lie past the end
8558 of a wrapped line. Then it->current is the
8559 character on the next line, so backtrack to the
8560 space before the wrap point. */
8561 if (skip == MOVE_LINE_CONTINUED)
8563 int prev_x = max (it->current_x - 1, 0);
8564 RESTORE_IT (it, &save_it, save_data);
8565 move_it_in_display_line_to
8566 (it, -1, prev_x, MOVE_TO_X);
8568 else
8569 bidi_unshelve_cache (save_data, 1);
8571 else
8572 move_it_in_display_line_to (it, to_charpos, to_x, op);
8576 /* Move IT forward until it satisfies one or more of the criteria in
8577 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8579 OP is a bit-mask that specifies where to stop, and in particular,
8580 which of those four position arguments makes a difference. See the
8581 description of enum move_operation_enum.
8583 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8584 screen line, this function will set IT to the next position that is
8585 displayed to the right of TO_CHARPOS on the screen. */
8587 void
8588 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8590 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8591 int line_height, line_start_x = 0, reached = 0;
8592 void *backup_data = NULL;
8594 for (;;)
8596 if (op & MOVE_TO_VPOS)
8598 /* If no TO_CHARPOS and no TO_X specified, stop at the
8599 start of the line TO_VPOS. */
8600 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8602 if (it->vpos == to_vpos)
8604 reached = 1;
8605 break;
8607 else
8608 skip = move_it_in_display_line_to (it, -1, -1, 0);
8610 else
8612 /* TO_VPOS >= 0 means stop at TO_X in the line at
8613 TO_VPOS, or at TO_POS, whichever comes first. */
8614 if (it->vpos == to_vpos)
8616 reached = 2;
8617 break;
8620 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8622 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8624 reached = 3;
8625 break;
8627 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8629 /* We have reached TO_X but not in the line we want. */
8630 skip = move_it_in_display_line_to (it, to_charpos,
8631 -1, MOVE_TO_POS);
8632 if (skip == MOVE_POS_MATCH_OR_ZV)
8634 reached = 4;
8635 break;
8640 else if (op & MOVE_TO_Y)
8642 struct it it_backup;
8644 if (it->line_wrap == WORD_WRAP)
8645 SAVE_IT (it_backup, *it, backup_data);
8647 /* TO_Y specified means stop at TO_X in the line containing
8648 TO_Y---or at TO_CHARPOS if this is reached first. The
8649 problem is that we can't really tell whether the line
8650 contains TO_Y before we have completely scanned it, and
8651 this may skip past TO_X. What we do is to first scan to
8652 TO_X.
8654 If TO_X is not specified, use a TO_X of zero. The reason
8655 is to make the outcome of this function more predictable.
8656 If we didn't use TO_X == 0, we would stop at the end of
8657 the line which is probably not what a caller would expect
8658 to happen. */
8659 skip = move_it_in_display_line_to
8660 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8661 (MOVE_TO_X | (op & MOVE_TO_POS)));
8663 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8664 if (skip == MOVE_POS_MATCH_OR_ZV)
8665 reached = 5;
8666 else if (skip == MOVE_X_REACHED)
8668 /* If TO_X was reached, we want to know whether TO_Y is
8669 in the line. We know this is the case if the already
8670 scanned glyphs make the line tall enough. Otherwise,
8671 we must check by scanning the rest of the line. */
8672 line_height = it->max_ascent + it->max_descent;
8673 if (to_y >= it->current_y
8674 && to_y < it->current_y + line_height)
8676 reached = 6;
8677 break;
8679 SAVE_IT (it_backup, *it, backup_data);
8680 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8681 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8682 op & MOVE_TO_POS);
8683 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8684 line_height = it->max_ascent + it->max_descent;
8685 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8687 if (to_y >= it->current_y
8688 && to_y < it->current_y + line_height)
8690 /* If TO_Y is in this line and TO_X was reached
8691 above, we scanned too far. We have to restore
8692 IT's settings to the ones before skipping. But
8693 keep the more accurate values of max_ascent and
8694 max_descent we've found while skipping the rest
8695 of the line, for the sake of callers, such as
8696 pos_visible_p, that need to know the line
8697 height. */
8698 int max_ascent = it->max_ascent;
8699 int max_descent = it->max_descent;
8701 RESTORE_IT (it, &it_backup, backup_data);
8702 it->max_ascent = max_ascent;
8703 it->max_descent = max_descent;
8704 reached = 6;
8706 else
8708 skip = skip2;
8709 if (skip == MOVE_POS_MATCH_OR_ZV)
8710 reached = 7;
8713 else
8715 /* Check whether TO_Y is in this line. */
8716 line_height = it->max_ascent + it->max_descent;
8717 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8719 if (to_y >= it->current_y
8720 && to_y < it->current_y + line_height)
8722 /* When word-wrap is on, TO_X may lie past the end
8723 of a wrapped line. Then it->current is the
8724 character on the next line, so backtrack to the
8725 space before the wrap point. */
8726 if (skip == MOVE_LINE_CONTINUED
8727 && it->line_wrap == WORD_WRAP)
8729 int prev_x = max (it->current_x - 1, 0);
8730 RESTORE_IT (it, &it_backup, backup_data);
8731 skip = move_it_in_display_line_to
8732 (it, -1, prev_x, MOVE_TO_X);
8734 reached = 6;
8738 if (reached)
8739 break;
8741 else if (BUFFERP (it->object)
8742 && (it->method == GET_FROM_BUFFER
8743 || it->method == GET_FROM_STRETCH)
8744 && IT_CHARPOS (*it) >= to_charpos
8745 /* Under bidi iteration, a call to set_iterator_to_next
8746 can scan far beyond to_charpos if the initial
8747 portion of the next line needs to be reordered. In
8748 that case, give move_it_in_display_line_to another
8749 chance below. */
8750 && !(it->bidi_p
8751 && it->bidi_it.scan_dir == -1))
8752 skip = MOVE_POS_MATCH_OR_ZV;
8753 else
8754 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8756 switch (skip)
8758 case MOVE_POS_MATCH_OR_ZV:
8759 reached = 8;
8760 goto out;
8762 case MOVE_NEWLINE_OR_CR:
8763 set_iterator_to_next (it, 1);
8764 it->continuation_lines_width = 0;
8765 break;
8767 case MOVE_LINE_TRUNCATED:
8768 it->continuation_lines_width = 0;
8769 reseat_at_next_visible_line_start (it, 0);
8770 if ((op & MOVE_TO_POS) != 0
8771 && IT_CHARPOS (*it) > to_charpos)
8773 reached = 9;
8774 goto out;
8776 break;
8778 case MOVE_LINE_CONTINUED:
8779 /* For continued lines ending in a tab, some of the glyphs
8780 associated with the tab are displayed on the current
8781 line. Since it->current_x does not include these glyphs,
8782 we use it->last_visible_x instead. */
8783 if (it->c == '\t')
8785 it->continuation_lines_width += it->last_visible_x;
8786 /* When moving by vpos, ensure that the iterator really
8787 advances to the next line (bug#847, bug#969). Fixme:
8788 do we need to do this in other circumstances? */
8789 if (it->current_x != it->last_visible_x
8790 && (op & MOVE_TO_VPOS)
8791 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8793 line_start_x = it->current_x + it->pixel_width
8794 - it->last_visible_x;
8795 set_iterator_to_next (it, 0);
8798 else
8799 it->continuation_lines_width += it->current_x;
8800 break;
8802 default:
8803 abort ();
8806 /* Reset/increment for the next run. */
8807 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8808 it->current_x = line_start_x;
8809 line_start_x = 0;
8810 it->hpos = 0;
8811 it->current_y += it->max_ascent + it->max_descent;
8812 ++it->vpos;
8813 last_height = it->max_ascent + it->max_descent;
8814 last_max_ascent = it->max_ascent;
8815 it->max_ascent = it->max_descent = 0;
8818 out:
8820 /* On text terminals, we may stop at the end of a line in the middle
8821 of a multi-character glyph. If the glyph itself is continued,
8822 i.e. it is actually displayed on the next line, don't treat this
8823 stopping point as valid; move to the next line instead (unless
8824 that brings us offscreen). */
8825 if (!FRAME_WINDOW_P (it->f)
8826 && op & MOVE_TO_POS
8827 && IT_CHARPOS (*it) == to_charpos
8828 && it->what == IT_CHARACTER
8829 && it->nglyphs > 1
8830 && it->line_wrap == WINDOW_WRAP
8831 && it->current_x == it->last_visible_x - 1
8832 && it->c != '\n'
8833 && it->c != '\t'
8834 && it->vpos < XFASTINT (it->w->window_end_vpos))
8836 it->continuation_lines_width += it->current_x;
8837 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8838 it->current_y += it->max_ascent + it->max_descent;
8839 ++it->vpos;
8840 last_height = it->max_ascent + it->max_descent;
8841 last_max_ascent = it->max_ascent;
8844 if (backup_data)
8845 bidi_unshelve_cache (backup_data, 1);
8847 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8851 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8853 If DY > 0, move IT backward at least that many pixels. DY = 0
8854 means move IT backward to the preceding line start or BEGV. This
8855 function may move over more than DY pixels if IT->current_y - DY
8856 ends up in the middle of a line; in this case IT->current_y will be
8857 set to the top of the line moved to. */
8859 void
8860 move_it_vertically_backward (struct it *it, int dy)
8862 int nlines, h;
8863 struct it it2, it3;
8864 void *it2data = NULL, *it3data = NULL;
8865 EMACS_INT start_pos;
8867 move_further_back:
8868 xassert (dy >= 0);
8870 start_pos = IT_CHARPOS (*it);
8872 /* Estimate how many newlines we must move back. */
8873 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8875 /* Set the iterator's position that many lines back. */
8876 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8877 back_to_previous_visible_line_start (it);
8879 /* Reseat the iterator here. When moving backward, we don't want
8880 reseat to skip forward over invisible text, set up the iterator
8881 to deliver from overlay strings at the new position etc. So,
8882 use reseat_1 here. */
8883 reseat_1 (it, it->current.pos, 1);
8885 /* We are now surely at a line start. */
8886 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8887 reordering is in effect. */
8888 it->continuation_lines_width = 0;
8890 /* Move forward and see what y-distance we moved. First move to the
8891 start of the next line so that we get its height. We need this
8892 height to be able to tell whether we reached the specified
8893 y-distance. */
8894 SAVE_IT (it2, *it, it2data);
8895 it2.max_ascent = it2.max_descent = 0;
8898 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8899 MOVE_TO_POS | MOVE_TO_VPOS);
8901 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8902 /* If we are in a display string which starts at START_POS,
8903 and that display string includes a newline, and we are
8904 right after that newline (i.e. at the beginning of a
8905 display line), exit the loop, because otherwise we will
8906 infloop, since move_it_to will see that it is already at
8907 START_POS and will not move. */
8908 || (it2.method == GET_FROM_STRING
8909 && IT_CHARPOS (it2) == start_pos
8910 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8911 xassert (IT_CHARPOS (*it) >= BEGV);
8912 SAVE_IT (it3, it2, it3data);
8914 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8915 xassert (IT_CHARPOS (*it) >= BEGV);
8916 /* H is the actual vertical distance from the position in *IT
8917 and the starting position. */
8918 h = it2.current_y - it->current_y;
8919 /* NLINES is the distance in number of lines. */
8920 nlines = it2.vpos - it->vpos;
8922 /* Correct IT's y and vpos position
8923 so that they are relative to the starting point. */
8924 it->vpos -= nlines;
8925 it->current_y -= h;
8927 if (dy == 0)
8929 /* DY == 0 means move to the start of the screen line. The
8930 value of nlines is > 0 if continuation lines were involved,
8931 or if the original IT position was at start of a line. */
8932 RESTORE_IT (it, it, it2data);
8933 if (nlines > 0)
8934 move_it_by_lines (it, nlines);
8935 /* The above code moves us to some position NLINES down,
8936 usually to its first glyph (leftmost in an L2R line), but
8937 that's not necessarily the start of the line, under bidi
8938 reordering. We want to get to the character position
8939 that is immediately after the newline of the previous
8940 line. */
8941 if (it->bidi_p
8942 && !it->continuation_lines_width
8943 && !STRINGP (it->string)
8944 && IT_CHARPOS (*it) > BEGV
8945 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8947 EMACS_INT nl_pos =
8948 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8950 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8952 bidi_unshelve_cache (it3data, 1);
8954 else
8956 /* The y-position we try to reach, relative to *IT.
8957 Note that H has been subtracted in front of the if-statement. */
8958 int target_y = it->current_y + h - dy;
8959 int y0 = it3.current_y;
8960 int y1;
8961 int line_height;
8963 RESTORE_IT (&it3, &it3, it3data);
8964 y1 = line_bottom_y (&it3);
8965 line_height = y1 - y0;
8966 RESTORE_IT (it, it, it2data);
8967 /* If we did not reach target_y, try to move further backward if
8968 we can. If we moved too far backward, try to move forward. */
8969 if (target_y < it->current_y
8970 /* This is heuristic. In a window that's 3 lines high, with
8971 a line height of 13 pixels each, recentering with point
8972 on the bottom line will try to move -39/2 = 19 pixels
8973 backward. Try to avoid moving into the first line. */
8974 && (it->current_y - target_y
8975 > min (window_box_height (it->w), line_height * 2 / 3))
8976 && IT_CHARPOS (*it) > BEGV)
8978 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8979 target_y - it->current_y));
8980 dy = it->current_y - target_y;
8981 goto move_further_back;
8983 else if (target_y >= it->current_y + line_height
8984 && IT_CHARPOS (*it) < ZV)
8986 /* Should move forward by at least one line, maybe more.
8988 Note: Calling move_it_by_lines can be expensive on
8989 terminal frames, where compute_motion is used (via
8990 vmotion) to do the job, when there are very long lines
8991 and truncate-lines is nil. That's the reason for
8992 treating terminal frames specially here. */
8994 if (!FRAME_WINDOW_P (it->f))
8995 move_it_vertically (it, target_y - (it->current_y + line_height));
8996 else
9000 move_it_by_lines (it, 1);
9002 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9009 /* Move IT by a specified amount of pixel lines DY. DY negative means
9010 move backwards. DY = 0 means move to start of screen line. At the
9011 end, IT will be on the start of a screen line. */
9013 void
9014 move_it_vertically (struct it *it, int dy)
9016 if (dy <= 0)
9017 move_it_vertically_backward (it, -dy);
9018 else
9020 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9021 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9022 MOVE_TO_POS | MOVE_TO_Y);
9023 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9025 /* If buffer ends in ZV without a newline, move to the start of
9026 the line to satisfy the post-condition. */
9027 if (IT_CHARPOS (*it) == ZV
9028 && ZV > BEGV
9029 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9030 move_it_by_lines (it, 0);
9035 /* Move iterator IT past the end of the text line it is in. */
9037 void
9038 move_it_past_eol (struct it *it)
9040 enum move_it_result rc;
9042 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9043 if (rc == MOVE_NEWLINE_OR_CR)
9044 set_iterator_to_next (it, 0);
9048 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9049 negative means move up. DVPOS == 0 means move to the start of the
9050 screen line.
9052 Optimization idea: If we would know that IT->f doesn't use
9053 a face with proportional font, we could be faster for
9054 truncate-lines nil. */
9056 void
9057 move_it_by_lines (struct it *it, int dvpos)
9060 /* The commented-out optimization uses vmotion on terminals. This
9061 gives bad results, because elements like it->what, on which
9062 callers such as pos_visible_p rely, aren't updated. */
9063 /* struct position pos;
9064 if (!FRAME_WINDOW_P (it->f))
9066 struct text_pos textpos;
9068 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9069 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9070 reseat (it, textpos, 1);
9071 it->vpos += pos.vpos;
9072 it->current_y += pos.vpos;
9074 else */
9076 if (dvpos == 0)
9078 /* DVPOS == 0 means move to the start of the screen line. */
9079 move_it_vertically_backward (it, 0);
9080 /* Let next call to line_bottom_y calculate real line height */
9081 last_height = 0;
9083 else if (dvpos > 0)
9085 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9086 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9088 /* Only move to the next buffer position if we ended up in a
9089 string from display property, not in an overlay string
9090 (before-string or after-string). That is because the
9091 latter don't conceal the underlying buffer position, so
9092 we can ask to move the iterator to the exact position we
9093 are interested in. Note that, even if we are already at
9094 IT_CHARPOS (*it), the call below is not a no-op, as it
9095 will detect that we are at the end of the string, pop the
9096 iterator, and compute it->current_x and it->hpos
9097 correctly. */
9098 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9099 -1, -1, -1, MOVE_TO_POS);
9102 else
9104 struct it it2;
9105 void *it2data = NULL;
9106 EMACS_INT start_charpos, i;
9108 /* Start at the beginning of the screen line containing IT's
9109 position. This may actually move vertically backwards,
9110 in case of overlays, so adjust dvpos accordingly. */
9111 dvpos += it->vpos;
9112 move_it_vertically_backward (it, 0);
9113 dvpos -= it->vpos;
9115 /* Go back -DVPOS visible lines and reseat the iterator there. */
9116 start_charpos = IT_CHARPOS (*it);
9117 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9118 back_to_previous_visible_line_start (it);
9119 reseat (it, it->current.pos, 1);
9121 /* Move further back if we end up in a string or an image. */
9122 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9124 /* First try to move to start of display line. */
9125 dvpos += it->vpos;
9126 move_it_vertically_backward (it, 0);
9127 dvpos -= it->vpos;
9128 if (IT_POS_VALID_AFTER_MOVE_P (it))
9129 break;
9130 /* If start of line is still in string or image,
9131 move further back. */
9132 back_to_previous_visible_line_start (it);
9133 reseat (it, it->current.pos, 1);
9134 dvpos--;
9137 it->current_x = it->hpos = 0;
9139 /* Above call may have moved too far if continuation lines
9140 are involved. Scan forward and see if it did. */
9141 SAVE_IT (it2, *it, it2data);
9142 it2.vpos = it2.current_y = 0;
9143 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9144 it->vpos -= it2.vpos;
9145 it->current_y -= it2.current_y;
9146 it->current_x = it->hpos = 0;
9148 /* If we moved too far back, move IT some lines forward. */
9149 if (it2.vpos > -dvpos)
9151 int delta = it2.vpos + dvpos;
9153 RESTORE_IT (&it2, &it2, it2data);
9154 SAVE_IT (it2, *it, it2data);
9155 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9156 /* Move back again if we got too far ahead. */
9157 if (IT_CHARPOS (*it) >= start_charpos)
9158 RESTORE_IT (it, &it2, it2data);
9159 else
9160 bidi_unshelve_cache (it2data, 1);
9162 else
9163 RESTORE_IT (it, it, it2data);
9167 /* Return 1 if IT points into the middle of a display vector. */
9170 in_display_vector_p (struct it *it)
9172 return (it->method == GET_FROM_DISPLAY_VECTOR
9173 && it->current.dpvec_index > 0
9174 && it->dpvec + it->current.dpvec_index != it->dpend);
9178 /***********************************************************************
9179 Messages
9180 ***********************************************************************/
9183 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9184 to *Messages*. */
9186 void
9187 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9189 Lisp_Object args[3];
9190 Lisp_Object msg, fmt;
9191 char *buffer;
9192 EMACS_INT len;
9193 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9194 USE_SAFE_ALLOCA;
9196 /* Do nothing if called asynchronously. Inserting text into
9197 a buffer may call after-change-functions and alike and
9198 that would means running Lisp asynchronously. */
9199 if (handling_signal)
9200 return;
9202 fmt = msg = Qnil;
9203 GCPRO4 (fmt, msg, arg1, arg2);
9205 args[0] = fmt = build_string (format);
9206 args[1] = arg1;
9207 args[2] = arg2;
9208 msg = Fformat (3, args);
9210 len = SBYTES (msg) + 1;
9211 SAFE_ALLOCA (buffer, char *, len);
9212 memcpy (buffer, SDATA (msg), len);
9214 message_dolog (buffer, len - 1, 1, 0);
9215 SAFE_FREE ();
9217 UNGCPRO;
9221 /* Output a newline in the *Messages* buffer if "needs" one. */
9223 void
9224 message_log_maybe_newline (void)
9226 if (message_log_need_newline)
9227 message_dolog ("", 0, 1, 0);
9231 /* Add a string M of length NBYTES to the message log, optionally
9232 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9233 nonzero, means interpret the contents of M as multibyte. This
9234 function calls low-level routines in order to bypass text property
9235 hooks, etc. which might not be safe to run.
9237 This may GC (insert may run before/after change hooks),
9238 so the buffer M must NOT point to a Lisp string. */
9240 void
9241 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9243 const unsigned char *msg = (const unsigned char *) m;
9245 if (!NILP (Vmemory_full))
9246 return;
9248 if (!NILP (Vmessage_log_max))
9250 struct buffer *oldbuf;
9251 Lisp_Object oldpoint, oldbegv, oldzv;
9252 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9253 EMACS_INT point_at_end = 0;
9254 EMACS_INT zv_at_end = 0;
9255 Lisp_Object old_deactivate_mark, tem;
9256 struct gcpro gcpro1;
9258 old_deactivate_mark = Vdeactivate_mark;
9259 oldbuf = current_buffer;
9260 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9261 BVAR (current_buffer, undo_list) = Qt;
9263 oldpoint = message_dolog_marker1;
9264 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9265 oldbegv = message_dolog_marker2;
9266 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9267 oldzv = message_dolog_marker3;
9268 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9269 GCPRO1 (old_deactivate_mark);
9271 if (PT == Z)
9272 point_at_end = 1;
9273 if (ZV == Z)
9274 zv_at_end = 1;
9276 BEGV = BEG;
9277 BEGV_BYTE = BEG_BYTE;
9278 ZV = Z;
9279 ZV_BYTE = Z_BYTE;
9280 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9282 /* Insert the string--maybe converting multibyte to single byte
9283 or vice versa, so that all the text fits the buffer. */
9284 if (multibyte
9285 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9287 EMACS_INT i;
9288 int c, char_bytes;
9289 char work[1];
9291 /* Convert a multibyte string to single-byte
9292 for the *Message* buffer. */
9293 for (i = 0; i < nbytes; i += char_bytes)
9295 c = string_char_and_length (msg + i, &char_bytes);
9296 work[0] = (ASCII_CHAR_P (c)
9298 : multibyte_char_to_unibyte (c));
9299 insert_1_both (work, 1, 1, 1, 0, 0);
9302 else if (! multibyte
9303 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9305 EMACS_INT i;
9306 int c, char_bytes;
9307 unsigned char str[MAX_MULTIBYTE_LENGTH];
9308 /* Convert a single-byte string to multibyte
9309 for the *Message* buffer. */
9310 for (i = 0; i < nbytes; i++)
9312 c = msg[i];
9313 MAKE_CHAR_MULTIBYTE (c);
9314 char_bytes = CHAR_STRING (c, str);
9315 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9318 else if (nbytes)
9319 insert_1 (m, nbytes, 1, 0, 0);
9321 if (nlflag)
9323 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9324 printmax_t dups;
9325 insert_1 ("\n", 1, 1, 0, 0);
9327 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9328 this_bol = PT;
9329 this_bol_byte = PT_BYTE;
9331 /* See if this line duplicates the previous one.
9332 If so, combine duplicates. */
9333 if (this_bol > BEG)
9335 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9336 prev_bol = PT;
9337 prev_bol_byte = PT_BYTE;
9339 dups = message_log_check_duplicate (prev_bol_byte,
9340 this_bol_byte);
9341 if (dups)
9343 del_range_both (prev_bol, prev_bol_byte,
9344 this_bol, this_bol_byte, 0);
9345 if (dups > 1)
9347 char dupstr[sizeof " [ times]"
9348 + INT_STRLEN_BOUND (printmax_t)];
9349 int duplen;
9351 /* If you change this format, don't forget to also
9352 change message_log_check_duplicate. */
9353 sprintf (dupstr, " [%"pMd" times]", dups);
9354 duplen = strlen (dupstr);
9355 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9356 insert_1 (dupstr, duplen, 1, 0, 1);
9361 /* If we have more than the desired maximum number of lines
9362 in the *Messages* buffer now, delete the oldest ones.
9363 This is safe because we don't have undo in this buffer. */
9365 if (NATNUMP (Vmessage_log_max))
9367 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9368 -XFASTINT (Vmessage_log_max) - 1, 0);
9369 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9372 BEGV = XMARKER (oldbegv)->charpos;
9373 BEGV_BYTE = marker_byte_position (oldbegv);
9375 if (zv_at_end)
9377 ZV = Z;
9378 ZV_BYTE = Z_BYTE;
9380 else
9382 ZV = XMARKER (oldzv)->charpos;
9383 ZV_BYTE = marker_byte_position (oldzv);
9386 if (point_at_end)
9387 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9388 else
9389 /* We can't do Fgoto_char (oldpoint) because it will run some
9390 Lisp code. */
9391 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9392 XMARKER (oldpoint)->bytepos);
9394 UNGCPRO;
9395 unchain_marker (XMARKER (oldpoint));
9396 unchain_marker (XMARKER (oldbegv));
9397 unchain_marker (XMARKER (oldzv));
9399 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9400 set_buffer_internal (oldbuf);
9401 if (NILP (tem))
9402 windows_or_buffers_changed = old_windows_or_buffers_changed;
9403 message_log_need_newline = !nlflag;
9404 Vdeactivate_mark = old_deactivate_mark;
9409 /* We are at the end of the buffer after just having inserted a newline.
9410 (Note: We depend on the fact we won't be crossing the gap.)
9411 Check to see if the most recent message looks a lot like the previous one.
9412 Return 0 if different, 1 if the new one should just replace it, or a
9413 value N > 1 if we should also append " [N times]". */
9415 static intmax_t
9416 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9418 EMACS_INT i;
9419 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9420 int seen_dots = 0;
9421 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9422 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9424 for (i = 0; i < len; i++)
9426 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9427 seen_dots = 1;
9428 if (p1[i] != p2[i])
9429 return seen_dots;
9431 p1 += len;
9432 if (*p1 == '\n')
9433 return 2;
9434 if (*p1++ == ' ' && *p1++ == '[')
9436 char *pend;
9437 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9438 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9439 return n+1;
9441 return 0;
9445 /* Display an echo area message M with a specified length of NBYTES
9446 bytes. The string may include null characters. If M is 0, clear
9447 out any existing message, and let the mini-buffer text show
9448 through.
9450 This may GC, so the buffer M must NOT point to a Lisp string. */
9452 void
9453 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9455 /* First flush out any partial line written with print. */
9456 message_log_maybe_newline ();
9457 if (m)
9458 message_dolog (m, nbytes, 1, multibyte);
9459 message2_nolog (m, nbytes, multibyte);
9463 /* The non-logging counterpart of message2. */
9465 void
9466 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9468 struct frame *sf = SELECTED_FRAME ();
9469 message_enable_multibyte = multibyte;
9471 if (FRAME_INITIAL_P (sf))
9473 if (noninteractive_need_newline)
9474 putc ('\n', stderr);
9475 noninteractive_need_newline = 0;
9476 if (m)
9477 fwrite (m, nbytes, 1, stderr);
9478 if (cursor_in_echo_area == 0)
9479 fprintf (stderr, "\n");
9480 fflush (stderr);
9482 /* A null message buffer means that the frame hasn't really been
9483 initialized yet. Error messages get reported properly by
9484 cmd_error, so this must be just an informative message; toss it. */
9485 else if (INTERACTIVE
9486 && sf->glyphs_initialized_p
9487 && FRAME_MESSAGE_BUF (sf))
9489 Lisp_Object mini_window;
9490 struct frame *f;
9492 /* Get the frame containing the mini-buffer
9493 that the selected frame is using. */
9494 mini_window = FRAME_MINIBUF_WINDOW (sf);
9495 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9497 FRAME_SAMPLE_VISIBILITY (f);
9498 if (FRAME_VISIBLE_P (sf)
9499 && ! FRAME_VISIBLE_P (f))
9500 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9502 if (m)
9504 set_message (m, Qnil, nbytes, multibyte);
9505 if (minibuffer_auto_raise)
9506 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9508 else
9509 clear_message (1, 1);
9511 do_pending_window_change (0);
9512 echo_area_display (1);
9513 do_pending_window_change (0);
9514 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9515 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9520 /* Display an echo area message M with a specified length of NBYTES
9521 bytes. The string may include null characters. If M is not a
9522 string, clear out any existing message, and let the mini-buffer
9523 text show through.
9525 This function cancels echoing. */
9527 void
9528 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9530 struct gcpro gcpro1;
9532 GCPRO1 (m);
9533 clear_message (1,1);
9534 cancel_echoing ();
9536 /* First flush out any partial line written with print. */
9537 message_log_maybe_newline ();
9538 if (STRINGP (m))
9540 char *buffer;
9541 USE_SAFE_ALLOCA;
9543 SAFE_ALLOCA (buffer, char *, nbytes);
9544 memcpy (buffer, SDATA (m), nbytes);
9545 message_dolog (buffer, nbytes, 1, multibyte);
9546 SAFE_FREE ();
9548 message3_nolog (m, nbytes, multibyte);
9550 UNGCPRO;
9554 /* The non-logging version of message3.
9555 This does not cancel echoing, because it is used for echoing.
9556 Perhaps we need to make a separate function for echoing
9557 and make this cancel echoing. */
9559 void
9560 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9562 struct frame *sf = SELECTED_FRAME ();
9563 message_enable_multibyte = multibyte;
9565 if (FRAME_INITIAL_P (sf))
9567 if (noninteractive_need_newline)
9568 putc ('\n', stderr);
9569 noninteractive_need_newline = 0;
9570 if (STRINGP (m))
9571 fwrite (SDATA (m), nbytes, 1, stderr);
9572 if (cursor_in_echo_area == 0)
9573 fprintf (stderr, "\n");
9574 fflush (stderr);
9576 /* A null message buffer means that the frame hasn't really been
9577 initialized yet. Error messages get reported properly by
9578 cmd_error, so this must be just an informative message; toss it. */
9579 else if (INTERACTIVE
9580 && sf->glyphs_initialized_p
9581 && FRAME_MESSAGE_BUF (sf))
9583 Lisp_Object mini_window;
9584 Lisp_Object frame;
9585 struct frame *f;
9587 /* Get the frame containing the mini-buffer
9588 that the selected frame is using. */
9589 mini_window = FRAME_MINIBUF_WINDOW (sf);
9590 frame = XWINDOW (mini_window)->frame;
9591 f = XFRAME (frame);
9593 FRAME_SAMPLE_VISIBILITY (f);
9594 if (FRAME_VISIBLE_P (sf)
9595 && !FRAME_VISIBLE_P (f))
9596 Fmake_frame_visible (frame);
9598 if (STRINGP (m) && SCHARS (m) > 0)
9600 set_message (NULL, m, nbytes, multibyte);
9601 if (minibuffer_auto_raise)
9602 Fraise_frame (frame);
9603 /* Assume we are not echoing.
9604 (If we are, echo_now will override this.) */
9605 echo_message_buffer = Qnil;
9607 else
9608 clear_message (1, 1);
9610 do_pending_window_change (0);
9611 echo_area_display (1);
9612 do_pending_window_change (0);
9613 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9614 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9619 /* Display a null-terminated echo area message M. If M is 0, clear
9620 out any existing message, and let the mini-buffer text show through.
9622 The buffer M must continue to exist until after the echo area gets
9623 cleared or some other message gets displayed there. Do not pass
9624 text that is stored in a Lisp string. Do not pass text in a buffer
9625 that was alloca'd. */
9627 void
9628 message1 (const char *m)
9630 message2 (m, (m ? strlen (m) : 0), 0);
9634 /* The non-logging counterpart of message1. */
9636 void
9637 message1_nolog (const char *m)
9639 message2_nolog (m, (m ? strlen (m) : 0), 0);
9642 /* Display a message M which contains a single %s
9643 which gets replaced with STRING. */
9645 void
9646 message_with_string (const char *m, Lisp_Object string, int log)
9648 CHECK_STRING (string);
9650 if (noninteractive)
9652 if (m)
9654 if (noninteractive_need_newline)
9655 putc ('\n', stderr);
9656 noninteractive_need_newline = 0;
9657 fprintf (stderr, m, SDATA (string));
9658 if (!cursor_in_echo_area)
9659 fprintf (stderr, "\n");
9660 fflush (stderr);
9663 else if (INTERACTIVE)
9665 /* The frame whose minibuffer we're going to display the message on.
9666 It may be larger than the selected frame, so we need
9667 to use its buffer, not the selected frame's buffer. */
9668 Lisp_Object mini_window;
9669 struct frame *f, *sf = SELECTED_FRAME ();
9671 /* Get the frame containing the minibuffer
9672 that the selected frame is using. */
9673 mini_window = FRAME_MINIBUF_WINDOW (sf);
9674 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9676 /* A null message buffer means that the frame hasn't really been
9677 initialized yet. Error messages get reported properly by
9678 cmd_error, so this must be just an informative message; toss it. */
9679 if (FRAME_MESSAGE_BUF (f))
9681 Lisp_Object args[2], msg;
9682 struct gcpro gcpro1, gcpro2;
9684 args[0] = build_string (m);
9685 args[1] = msg = string;
9686 GCPRO2 (args[0], msg);
9687 gcpro1.nvars = 2;
9689 msg = Fformat (2, args);
9691 if (log)
9692 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9693 else
9694 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9696 UNGCPRO;
9698 /* Print should start at the beginning of the message
9699 buffer next time. */
9700 message_buf_print = 0;
9706 /* Dump an informative message to the minibuf. If M is 0, clear out
9707 any existing message, and let the mini-buffer text show through. */
9709 static void
9710 vmessage (const char *m, va_list ap)
9712 if (noninteractive)
9714 if (m)
9716 if (noninteractive_need_newline)
9717 putc ('\n', stderr);
9718 noninteractive_need_newline = 0;
9719 vfprintf (stderr, m, ap);
9720 if (cursor_in_echo_area == 0)
9721 fprintf (stderr, "\n");
9722 fflush (stderr);
9725 else if (INTERACTIVE)
9727 /* The frame whose mini-buffer we're going to display the message
9728 on. It may be larger than the selected frame, so we need to
9729 use its buffer, not the selected frame's buffer. */
9730 Lisp_Object mini_window;
9731 struct frame *f, *sf = SELECTED_FRAME ();
9733 /* Get the frame containing the mini-buffer
9734 that the selected frame is using. */
9735 mini_window = FRAME_MINIBUF_WINDOW (sf);
9736 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9738 /* A null message buffer means that the frame hasn't really been
9739 initialized yet. Error messages get reported properly by
9740 cmd_error, so this must be just an informative message; toss
9741 it. */
9742 if (FRAME_MESSAGE_BUF (f))
9744 if (m)
9746 ptrdiff_t len;
9748 len = doprnt (FRAME_MESSAGE_BUF (f),
9749 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9751 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9753 else
9754 message1 (0);
9756 /* Print should start at the beginning of the message
9757 buffer next time. */
9758 message_buf_print = 0;
9763 void
9764 message (const char *m, ...)
9766 va_list ap;
9767 va_start (ap, m);
9768 vmessage (m, ap);
9769 va_end (ap);
9773 #if 0
9774 /* The non-logging version of message. */
9776 void
9777 message_nolog (const char *m, ...)
9779 Lisp_Object old_log_max;
9780 va_list ap;
9781 va_start (ap, m);
9782 old_log_max = Vmessage_log_max;
9783 Vmessage_log_max = Qnil;
9784 vmessage (m, ap);
9785 Vmessage_log_max = old_log_max;
9786 va_end (ap);
9788 #endif
9791 /* Display the current message in the current mini-buffer. This is
9792 only called from error handlers in process.c, and is not time
9793 critical. */
9795 void
9796 update_echo_area (void)
9798 if (!NILP (echo_area_buffer[0]))
9800 Lisp_Object string;
9801 string = Fcurrent_message ();
9802 message3 (string, SBYTES (string),
9803 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9808 /* Make sure echo area buffers in `echo_buffers' are live.
9809 If they aren't, make new ones. */
9811 static void
9812 ensure_echo_area_buffers (void)
9814 int i;
9816 for (i = 0; i < 2; ++i)
9817 if (!BUFFERP (echo_buffer[i])
9818 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9820 char name[30];
9821 Lisp_Object old_buffer;
9822 int j;
9824 old_buffer = echo_buffer[i];
9825 sprintf (name, " *Echo Area %d*", i);
9826 echo_buffer[i] = Fget_buffer_create (build_string (name));
9827 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9828 /* to force word wrap in echo area -
9829 it was decided to postpone this*/
9830 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9832 for (j = 0; j < 2; ++j)
9833 if (EQ (old_buffer, echo_area_buffer[j]))
9834 echo_area_buffer[j] = echo_buffer[i];
9839 /* Call FN with args A1..A4 with either the current or last displayed
9840 echo_area_buffer as current buffer.
9842 WHICH zero means use the current message buffer
9843 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9844 from echo_buffer[] and clear it.
9846 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9847 suitable buffer from echo_buffer[] and clear it.
9849 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9850 that the current message becomes the last displayed one, make
9851 choose a suitable buffer for echo_area_buffer[0], and clear it.
9853 Value is what FN returns. */
9855 static int
9856 with_echo_area_buffer (struct window *w, int which,
9857 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9858 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9860 Lisp_Object buffer;
9861 int this_one, the_other, clear_buffer_p, rc;
9862 int count = SPECPDL_INDEX ();
9864 /* If buffers aren't live, make new ones. */
9865 ensure_echo_area_buffers ();
9867 clear_buffer_p = 0;
9869 if (which == 0)
9870 this_one = 0, the_other = 1;
9871 else if (which > 0)
9872 this_one = 1, the_other = 0;
9873 else
9875 this_one = 0, the_other = 1;
9876 clear_buffer_p = 1;
9878 /* We need a fresh one in case the current echo buffer equals
9879 the one containing the last displayed echo area message. */
9880 if (!NILP (echo_area_buffer[this_one])
9881 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9882 echo_area_buffer[this_one] = Qnil;
9885 /* Choose a suitable buffer from echo_buffer[] is we don't
9886 have one. */
9887 if (NILP (echo_area_buffer[this_one]))
9889 echo_area_buffer[this_one]
9890 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9891 ? echo_buffer[the_other]
9892 : echo_buffer[this_one]);
9893 clear_buffer_p = 1;
9896 buffer = echo_area_buffer[this_one];
9898 /* Don't get confused by reusing the buffer used for echoing
9899 for a different purpose. */
9900 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9901 cancel_echoing ();
9903 record_unwind_protect (unwind_with_echo_area_buffer,
9904 with_echo_area_buffer_unwind_data (w));
9906 /* Make the echo area buffer current. Note that for display
9907 purposes, it is not necessary that the displayed window's buffer
9908 == current_buffer, except for text property lookup. So, let's
9909 only set that buffer temporarily here without doing a full
9910 Fset_window_buffer. We must also change w->pointm, though,
9911 because otherwise an assertions in unshow_buffer fails, and Emacs
9912 aborts. */
9913 set_buffer_internal_1 (XBUFFER (buffer));
9914 if (w)
9916 w->buffer = buffer;
9917 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9920 BVAR (current_buffer, undo_list) = Qt;
9921 BVAR (current_buffer, read_only) = Qnil;
9922 specbind (Qinhibit_read_only, Qt);
9923 specbind (Qinhibit_modification_hooks, Qt);
9925 if (clear_buffer_p && Z > BEG)
9926 del_range (BEG, Z);
9928 xassert (BEGV >= BEG);
9929 xassert (ZV <= Z && ZV >= BEGV);
9931 rc = fn (a1, a2, a3, a4);
9933 xassert (BEGV >= BEG);
9934 xassert (ZV <= Z && ZV >= BEGV);
9936 unbind_to (count, Qnil);
9937 return rc;
9941 /* Save state that should be preserved around the call to the function
9942 FN called in with_echo_area_buffer. */
9944 static Lisp_Object
9945 with_echo_area_buffer_unwind_data (struct window *w)
9947 int i = 0;
9948 Lisp_Object vector, tmp;
9950 /* Reduce consing by keeping one vector in
9951 Vwith_echo_area_save_vector. */
9952 vector = Vwith_echo_area_save_vector;
9953 Vwith_echo_area_save_vector = Qnil;
9955 if (NILP (vector))
9956 vector = Fmake_vector (make_number (7), Qnil);
9958 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9959 ASET (vector, i, Vdeactivate_mark); ++i;
9960 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9962 if (w)
9964 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9965 ASET (vector, i, w->buffer); ++i;
9966 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9967 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9969 else
9971 int end = i + 4;
9972 for (; i < end; ++i)
9973 ASET (vector, i, Qnil);
9976 xassert (i == ASIZE (vector));
9977 return vector;
9981 /* Restore global state from VECTOR which was created by
9982 with_echo_area_buffer_unwind_data. */
9984 static Lisp_Object
9985 unwind_with_echo_area_buffer (Lisp_Object vector)
9987 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9988 Vdeactivate_mark = AREF (vector, 1);
9989 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9991 if (WINDOWP (AREF (vector, 3)))
9993 struct window *w;
9994 Lisp_Object buffer, charpos, bytepos;
9996 w = XWINDOW (AREF (vector, 3));
9997 buffer = AREF (vector, 4);
9998 charpos = AREF (vector, 5);
9999 bytepos = AREF (vector, 6);
10001 w->buffer = buffer;
10002 set_marker_both (w->pointm, buffer,
10003 XFASTINT (charpos), XFASTINT (bytepos));
10006 Vwith_echo_area_save_vector = vector;
10007 return Qnil;
10011 /* Set up the echo area for use by print functions. MULTIBYTE_P
10012 non-zero means we will print multibyte. */
10014 void
10015 setup_echo_area_for_printing (int multibyte_p)
10017 /* If we can't find an echo area any more, exit. */
10018 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10019 Fkill_emacs (Qnil);
10021 ensure_echo_area_buffers ();
10023 if (!message_buf_print)
10025 /* A message has been output since the last time we printed.
10026 Choose a fresh echo area buffer. */
10027 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10028 echo_area_buffer[0] = echo_buffer[1];
10029 else
10030 echo_area_buffer[0] = echo_buffer[0];
10032 /* Switch to that buffer and clear it. */
10033 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10034 BVAR (current_buffer, truncate_lines) = Qnil;
10036 if (Z > BEG)
10038 int count = SPECPDL_INDEX ();
10039 specbind (Qinhibit_read_only, Qt);
10040 /* Note that undo recording is always disabled. */
10041 del_range (BEG, Z);
10042 unbind_to (count, Qnil);
10044 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10046 /* Set up the buffer for the multibyteness we need. */
10047 if (multibyte_p
10048 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10049 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10051 /* Raise the frame containing the echo area. */
10052 if (minibuffer_auto_raise)
10054 struct frame *sf = SELECTED_FRAME ();
10055 Lisp_Object mini_window;
10056 mini_window = FRAME_MINIBUF_WINDOW (sf);
10057 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10060 message_log_maybe_newline ();
10061 message_buf_print = 1;
10063 else
10065 if (NILP (echo_area_buffer[0]))
10067 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10068 echo_area_buffer[0] = echo_buffer[1];
10069 else
10070 echo_area_buffer[0] = echo_buffer[0];
10073 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10075 /* Someone switched buffers between print requests. */
10076 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10077 BVAR (current_buffer, truncate_lines) = Qnil;
10083 /* Display an echo area message in window W. Value is non-zero if W's
10084 height is changed. If display_last_displayed_message_p is
10085 non-zero, display the message that was last displayed, otherwise
10086 display the current message. */
10088 static int
10089 display_echo_area (struct window *w)
10091 int i, no_message_p, window_height_changed_p, count;
10093 /* Temporarily disable garbage collections while displaying the echo
10094 area. This is done because a GC can print a message itself.
10095 That message would modify the echo area buffer's contents while a
10096 redisplay of the buffer is going on, and seriously confuse
10097 redisplay. */
10098 count = inhibit_garbage_collection ();
10100 /* If there is no message, we must call display_echo_area_1
10101 nevertheless because it resizes the window. But we will have to
10102 reset the echo_area_buffer in question to nil at the end because
10103 with_echo_area_buffer will sets it to an empty buffer. */
10104 i = display_last_displayed_message_p ? 1 : 0;
10105 no_message_p = NILP (echo_area_buffer[i]);
10107 window_height_changed_p
10108 = with_echo_area_buffer (w, display_last_displayed_message_p,
10109 display_echo_area_1,
10110 (intptr_t) w, Qnil, 0, 0);
10112 if (no_message_p)
10113 echo_area_buffer[i] = Qnil;
10115 unbind_to (count, Qnil);
10116 return window_height_changed_p;
10120 /* Helper for display_echo_area. Display the current buffer which
10121 contains the current echo area message in window W, a mini-window,
10122 a pointer to which is passed in A1. A2..A4 are currently not used.
10123 Change the height of W so that all of the message is displayed.
10124 Value is non-zero if height of W was changed. */
10126 static int
10127 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10129 intptr_t i1 = a1;
10130 struct window *w = (struct window *) i1;
10131 Lisp_Object window;
10132 struct text_pos start;
10133 int window_height_changed_p = 0;
10135 /* Do this before displaying, so that we have a large enough glyph
10136 matrix for the display. If we can't get enough space for the
10137 whole text, display the last N lines. That works by setting w->start. */
10138 window_height_changed_p = resize_mini_window (w, 0);
10140 /* Use the starting position chosen by resize_mini_window. */
10141 SET_TEXT_POS_FROM_MARKER (start, w->start);
10143 /* Display. */
10144 clear_glyph_matrix (w->desired_matrix);
10145 XSETWINDOW (window, w);
10146 try_window (window, start, 0);
10148 return window_height_changed_p;
10152 /* Resize the echo area window to exactly the size needed for the
10153 currently displayed message, if there is one. If a mini-buffer
10154 is active, don't shrink it. */
10156 void
10157 resize_echo_area_exactly (void)
10159 if (BUFFERP (echo_area_buffer[0])
10160 && WINDOWP (echo_area_window))
10162 struct window *w = XWINDOW (echo_area_window);
10163 int resized_p;
10164 Lisp_Object resize_exactly;
10166 if (minibuf_level == 0)
10167 resize_exactly = Qt;
10168 else
10169 resize_exactly = Qnil;
10171 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10172 (intptr_t) w, resize_exactly,
10173 0, 0);
10174 if (resized_p)
10176 ++windows_or_buffers_changed;
10177 ++update_mode_lines;
10178 redisplay_internal ();
10184 /* Callback function for with_echo_area_buffer, when used from
10185 resize_echo_area_exactly. A1 contains a pointer to the window to
10186 resize, EXACTLY non-nil means resize the mini-window exactly to the
10187 size of the text displayed. A3 and A4 are not used. Value is what
10188 resize_mini_window returns. */
10190 static int
10191 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10193 intptr_t i1 = a1;
10194 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10198 /* Resize mini-window W to fit the size of its contents. EXACT_P
10199 means size the window exactly to the size needed. Otherwise, it's
10200 only enlarged until W's buffer is empty.
10202 Set W->start to the right place to begin display. If the whole
10203 contents fit, start at the beginning. Otherwise, start so as
10204 to make the end of the contents appear. This is particularly
10205 important for y-or-n-p, but seems desirable generally.
10207 Value is non-zero if the window height has been changed. */
10210 resize_mini_window (struct window *w, int exact_p)
10212 struct frame *f = XFRAME (w->frame);
10213 int window_height_changed_p = 0;
10215 xassert (MINI_WINDOW_P (w));
10217 /* By default, start display at the beginning. */
10218 set_marker_both (w->start, w->buffer,
10219 BUF_BEGV (XBUFFER (w->buffer)),
10220 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10222 /* Don't resize windows while redisplaying a window; it would
10223 confuse redisplay functions when the size of the window they are
10224 displaying changes from under them. Such a resizing can happen,
10225 for instance, when which-func prints a long message while
10226 we are running fontification-functions. We're running these
10227 functions with safe_call which binds inhibit-redisplay to t. */
10228 if (!NILP (Vinhibit_redisplay))
10229 return 0;
10231 /* Nil means don't try to resize. */
10232 if (NILP (Vresize_mini_windows)
10233 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10234 return 0;
10236 if (!FRAME_MINIBUF_ONLY_P (f))
10238 struct it it;
10239 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10240 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10241 int height, max_height;
10242 int unit = FRAME_LINE_HEIGHT (f);
10243 struct text_pos start;
10244 struct buffer *old_current_buffer = NULL;
10246 if (current_buffer != XBUFFER (w->buffer))
10248 old_current_buffer = current_buffer;
10249 set_buffer_internal (XBUFFER (w->buffer));
10252 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10254 /* Compute the max. number of lines specified by the user. */
10255 if (FLOATP (Vmax_mini_window_height))
10256 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10257 else if (INTEGERP (Vmax_mini_window_height))
10258 max_height = XINT (Vmax_mini_window_height);
10259 else
10260 max_height = total_height / 4;
10262 /* Correct that max. height if it's bogus. */
10263 max_height = max (1, max_height);
10264 max_height = min (total_height, max_height);
10266 /* Find out the height of the text in the window. */
10267 if (it.line_wrap == TRUNCATE)
10268 height = 1;
10269 else
10271 last_height = 0;
10272 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10273 if (it.max_ascent == 0 && it.max_descent == 0)
10274 height = it.current_y + last_height;
10275 else
10276 height = it.current_y + it.max_ascent + it.max_descent;
10277 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10278 height = (height + unit - 1) / unit;
10281 /* Compute a suitable window start. */
10282 if (height > max_height)
10284 height = max_height;
10285 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10286 move_it_vertically_backward (&it, (height - 1) * unit);
10287 start = it.current.pos;
10289 else
10290 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10291 SET_MARKER_FROM_TEXT_POS (w->start, start);
10293 if (EQ (Vresize_mini_windows, Qgrow_only))
10295 /* Let it grow only, until we display an empty message, in which
10296 case the window shrinks again. */
10297 if (height > WINDOW_TOTAL_LINES (w))
10299 int old_height = WINDOW_TOTAL_LINES (w);
10300 freeze_window_starts (f, 1);
10301 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10302 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10304 else if (height < WINDOW_TOTAL_LINES (w)
10305 && (exact_p || BEGV == ZV))
10307 int old_height = WINDOW_TOTAL_LINES (w);
10308 freeze_window_starts (f, 0);
10309 shrink_mini_window (w);
10310 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10313 else
10315 /* Always resize to exact size needed. */
10316 if (height > WINDOW_TOTAL_LINES (w))
10318 int old_height = WINDOW_TOTAL_LINES (w);
10319 freeze_window_starts (f, 1);
10320 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10321 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10323 else if (height < WINDOW_TOTAL_LINES (w))
10325 int old_height = WINDOW_TOTAL_LINES (w);
10326 freeze_window_starts (f, 0);
10327 shrink_mini_window (w);
10329 if (height)
10331 freeze_window_starts (f, 1);
10332 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10335 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10339 if (old_current_buffer)
10340 set_buffer_internal (old_current_buffer);
10343 return window_height_changed_p;
10347 /* Value is the current message, a string, or nil if there is no
10348 current message. */
10350 Lisp_Object
10351 current_message (void)
10353 Lisp_Object msg;
10355 if (!BUFFERP (echo_area_buffer[0]))
10356 msg = Qnil;
10357 else
10359 with_echo_area_buffer (0, 0, current_message_1,
10360 (intptr_t) &msg, Qnil, 0, 0);
10361 if (NILP (msg))
10362 echo_area_buffer[0] = Qnil;
10365 return msg;
10369 static int
10370 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10372 intptr_t i1 = a1;
10373 Lisp_Object *msg = (Lisp_Object *) i1;
10375 if (Z > BEG)
10376 *msg = make_buffer_string (BEG, Z, 1);
10377 else
10378 *msg = Qnil;
10379 return 0;
10383 /* Push the current message on Vmessage_stack for later restoration
10384 by restore_message. Value is non-zero if the current message isn't
10385 empty. This is a relatively infrequent operation, so it's not
10386 worth optimizing. */
10389 push_message (void)
10391 Lisp_Object msg;
10392 msg = current_message ();
10393 Vmessage_stack = Fcons (msg, Vmessage_stack);
10394 return STRINGP (msg);
10398 /* Restore message display from the top of Vmessage_stack. */
10400 void
10401 restore_message (void)
10403 Lisp_Object msg;
10405 xassert (CONSP (Vmessage_stack));
10406 msg = XCAR (Vmessage_stack);
10407 if (STRINGP (msg))
10408 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10409 else
10410 message3_nolog (msg, 0, 0);
10414 /* Handler for record_unwind_protect calling pop_message. */
10416 Lisp_Object
10417 pop_message_unwind (Lisp_Object dummy)
10419 pop_message ();
10420 return Qnil;
10423 /* Pop the top-most entry off Vmessage_stack. */
10425 static void
10426 pop_message (void)
10428 xassert (CONSP (Vmessage_stack));
10429 Vmessage_stack = XCDR (Vmessage_stack);
10433 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10434 exits. If the stack is not empty, we have a missing pop_message
10435 somewhere. */
10437 void
10438 check_message_stack (void)
10440 if (!NILP (Vmessage_stack))
10441 abort ();
10445 /* Truncate to NCHARS what will be displayed in the echo area the next
10446 time we display it---but don't redisplay it now. */
10448 void
10449 truncate_echo_area (EMACS_INT nchars)
10451 if (nchars == 0)
10452 echo_area_buffer[0] = Qnil;
10453 /* A null message buffer means that the frame hasn't really been
10454 initialized yet. Error messages get reported properly by
10455 cmd_error, so this must be just an informative message; toss it. */
10456 else if (!noninteractive
10457 && INTERACTIVE
10458 && !NILP (echo_area_buffer[0]))
10460 struct frame *sf = SELECTED_FRAME ();
10461 if (FRAME_MESSAGE_BUF (sf))
10462 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10467 /* Helper function for truncate_echo_area. Truncate the current
10468 message to at most NCHARS characters. */
10470 static int
10471 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10473 if (BEG + nchars < Z)
10474 del_range (BEG + nchars, Z);
10475 if (Z == BEG)
10476 echo_area_buffer[0] = Qnil;
10477 return 0;
10481 /* Set the current message to a substring of S or STRING.
10483 If STRING is a Lisp string, set the message to the first NBYTES
10484 bytes from STRING. NBYTES zero means use the whole string. If
10485 STRING is multibyte, the message will be displayed multibyte.
10487 If S is not null, set the message to the first LEN bytes of S. LEN
10488 zero means use the whole string. MULTIBYTE_P non-zero means S is
10489 multibyte. Display the message multibyte in that case.
10491 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10492 to t before calling set_message_1 (which calls insert).
10495 static void
10496 set_message (const char *s, Lisp_Object string,
10497 EMACS_INT nbytes, int multibyte_p)
10499 message_enable_multibyte
10500 = ((s && multibyte_p)
10501 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10503 with_echo_area_buffer (0, -1, set_message_1,
10504 (intptr_t) s, string, nbytes, multibyte_p);
10505 message_buf_print = 0;
10506 help_echo_showing_p = 0;
10510 /* Helper function for set_message. Arguments have the same meaning
10511 as there, with A1 corresponding to S and A2 corresponding to STRING
10512 This function is called with the echo area buffer being
10513 current. */
10515 static int
10516 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10518 intptr_t i1 = a1;
10519 const char *s = (const char *) i1;
10520 const unsigned char *msg = (const unsigned char *) s;
10521 Lisp_Object string = a2;
10523 /* Change multibyteness of the echo buffer appropriately. */
10524 if (message_enable_multibyte
10525 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10526 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10528 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10529 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10530 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10532 /* Insert new message at BEG. */
10533 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10535 if (STRINGP (string))
10537 EMACS_INT nchars;
10539 if (nbytes == 0)
10540 nbytes = SBYTES (string);
10541 nchars = string_byte_to_char (string, nbytes);
10543 /* This function takes care of single/multibyte conversion. We
10544 just have to ensure that the echo area buffer has the right
10545 setting of enable_multibyte_characters. */
10546 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10548 else if (s)
10550 if (nbytes == 0)
10551 nbytes = strlen (s);
10553 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10555 /* Convert from multi-byte to single-byte. */
10556 EMACS_INT i;
10557 int c, n;
10558 char work[1];
10560 /* Convert a multibyte string to single-byte. */
10561 for (i = 0; i < nbytes; i += n)
10563 c = string_char_and_length (msg + i, &n);
10564 work[0] = (ASCII_CHAR_P (c)
10566 : multibyte_char_to_unibyte (c));
10567 insert_1_both (work, 1, 1, 1, 0, 0);
10570 else if (!multibyte_p
10571 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10573 /* Convert from single-byte to multi-byte. */
10574 EMACS_INT i;
10575 int c, n;
10576 unsigned char str[MAX_MULTIBYTE_LENGTH];
10578 /* Convert a single-byte string to multibyte. */
10579 for (i = 0; i < nbytes; i++)
10581 c = msg[i];
10582 MAKE_CHAR_MULTIBYTE (c);
10583 n = CHAR_STRING (c, str);
10584 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10587 else
10588 insert_1 (s, nbytes, 1, 0, 0);
10591 return 0;
10595 /* Clear messages. CURRENT_P non-zero means clear the current
10596 message. LAST_DISPLAYED_P non-zero means clear the message
10597 last displayed. */
10599 void
10600 clear_message (int current_p, int last_displayed_p)
10602 if (current_p)
10604 echo_area_buffer[0] = Qnil;
10605 message_cleared_p = 1;
10608 if (last_displayed_p)
10609 echo_area_buffer[1] = Qnil;
10611 message_buf_print = 0;
10614 /* Clear garbaged frames.
10616 This function is used where the old redisplay called
10617 redraw_garbaged_frames which in turn called redraw_frame which in
10618 turn called clear_frame. The call to clear_frame was a source of
10619 flickering. I believe a clear_frame is not necessary. It should
10620 suffice in the new redisplay to invalidate all current matrices,
10621 and ensure a complete redisplay of all windows. */
10623 static void
10624 clear_garbaged_frames (void)
10626 if (frame_garbaged)
10628 Lisp_Object tail, frame;
10629 int changed_count = 0;
10631 FOR_EACH_FRAME (tail, frame)
10633 struct frame *f = XFRAME (frame);
10635 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10637 if (f->resized_p)
10639 Fredraw_frame (frame);
10640 f->force_flush_display_p = 1;
10642 clear_current_matrices (f);
10643 changed_count++;
10644 f->garbaged = 0;
10645 f->resized_p = 0;
10649 frame_garbaged = 0;
10650 if (changed_count)
10651 ++windows_or_buffers_changed;
10656 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10657 is non-zero update selected_frame. Value is non-zero if the
10658 mini-windows height has been changed. */
10660 static int
10661 echo_area_display (int update_frame_p)
10663 Lisp_Object mini_window;
10664 struct window *w;
10665 struct frame *f;
10666 int window_height_changed_p = 0;
10667 struct frame *sf = SELECTED_FRAME ();
10669 mini_window = FRAME_MINIBUF_WINDOW (sf);
10670 w = XWINDOW (mini_window);
10671 f = XFRAME (WINDOW_FRAME (w));
10673 /* Don't display if frame is invisible or not yet initialized. */
10674 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10675 return 0;
10677 #ifdef HAVE_WINDOW_SYSTEM
10678 /* When Emacs starts, selected_frame may be the initial terminal
10679 frame. If we let this through, a message would be displayed on
10680 the terminal. */
10681 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10682 return 0;
10683 #endif /* HAVE_WINDOW_SYSTEM */
10685 /* Redraw garbaged frames. */
10686 if (frame_garbaged)
10687 clear_garbaged_frames ();
10689 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10691 echo_area_window = mini_window;
10692 window_height_changed_p = display_echo_area (w);
10693 w->must_be_updated_p = 1;
10695 /* Update the display, unless called from redisplay_internal.
10696 Also don't update the screen during redisplay itself. The
10697 update will happen at the end of redisplay, and an update
10698 here could cause confusion. */
10699 if (update_frame_p && !redisplaying_p)
10701 int n = 0;
10703 /* If the display update has been interrupted by pending
10704 input, update mode lines in the frame. Due to the
10705 pending input, it might have been that redisplay hasn't
10706 been called, so that mode lines above the echo area are
10707 garbaged. This looks odd, so we prevent it here. */
10708 if (!display_completed)
10709 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10711 if (window_height_changed_p
10712 /* Don't do this if Emacs is shutting down. Redisplay
10713 needs to run hooks. */
10714 && !NILP (Vrun_hooks))
10716 /* Must update other windows. Likewise as in other
10717 cases, don't let this update be interrupted by
10718 pending input. */
10719 int count = SPECPDL_INDEX ();
10720 specbind (Qredisplay_dont_pause, Qt);
10721 windows_or_buffers_changed = 1;
10722 redisplay_internal ();
10723 unbind_to (count, Qnil);
10725 else if (FRAME_WINDOW_P (f) && n == 0)
10727 /* Window configuration is the same as before.
10728 Can do with a display update of the echo area,
10729 unless we displayed some mode lines. */
10730 update_single_window (w, 1);
10731 FRAME_RIF (f)->flush_display (f);
10733 else
10734 update_frame (f, 1, 1);
10736 /* If cursor is in the echo area, make sure that the next
10737 redisplay displays the minibuffer, so that the cursor will
10738 be replaced with what the minibuffer wants. */
10739 if (cursor_in_echo_area)
10740 ++windows_or_buffers_changed;
10743 else if (!EQ (mini_window, selected_window))
10744 windows_or_buffers_changed++;
10746 /* Last displayed message is now the current message. */
10747 echo_area_buffer[1] = echo_area_buffer[0];
10748 /* Inform read_char that we're not echoing. */
10749 echo_message_buffer = Qnil;
10751 /* Prevent redisplay optimization in redisplay_internal by resetting
10752 this_line_start_pos. This is done because the mini-buffer now
10753 displays the message instead of its buffer text. */
10754 if (EQ (mini_window, selected_window))
10755 CHARPOS (this_line_start_pos) = 0;
10757 return window_height_changed_p;
10762 /***********************************************************************
10763 Mode Lines and Frame Titles
10764 ***********************************************************************/
10766 /* A buffer for constructing non-propertized mode-line strings and
10767 frame titles in it; allocated from the heap in init_xdisp and
10768 resized as needed in store_mode_line_noprop_char. */
10770 static char *mode_line_noprop_buf;
10772 /* The buffer's end, and a current output position in it. */
10774 static char *mode_line_noprop_buf_end;
10775 static char *mode_line_noprop_ptr;
10777 #define MODE_LINE_NOPROP_LEN(start) \
10778 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10780 static enum {
10781 MODE_LINE_DISPLAY = 0,
10782 MODE_LINE_TITLE,
10783 MODE_LINE_NOPROP,
10784 MODE_LINE_STRING
10785 } mode_line_target;
10787 /* Alist that caches the results of :propertize.
10788 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10789 static Lisp_Object mode_line_proptrans_alist;
10791 /* List of strings making up the mode-line. */
10792 static Lisp_Object mode_line_string_list;
10794 /* Base face property when building propertized mode line string. */
10795 static Lisp_Object mode_line_string_face;
10796 static Lisp_Object mode_line_string_face_prop;
10799 /* Unwind data for mode line strings */
10801 static Lisp_Object Vmode_line_unwind_vector;
10803 static Lisp_Object
10804 format_mode_line_unwind_data (struct buffer *obuf,
10805 Lisp_Object owin,
10806 int save_proptrans)
10808 Lisp_Object vector, tmp;
10810 /* Reduce consing by keeping one vector in
10811 Vwith_echo_area_save_vector. */
10812 vector = Vmode_line_unwind_vector;
10813 Vmode_line_unwind_vector = Qnil;
10815 if (NILP (vector))
10816 vector = Fmake_vector (make_number (8), Qnil);
10818 ASET (vector, 0, make_number (mode_line_target));
10819 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10820 ASET (vector, 2, mode_line_string_list);
10821 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10822 ASET (vector, 4, mode_line_string_face);
10823 ASET (vector, 5, mode_line_string_face_prop);
10825 if (obuf)
10826 XSETBUFFER (tmp, obuf);
10827 else
10828 tmp = Qnil;
10829 ASET (vector, 6, tmp);
10830 ASET (vector, 7, owin);
10832 return vector;
10835 static Lisp_Object
10836 unwind_format_mode_line (Lisp_Object vector)
10838 mode_line_target = XINT (AREF (vector, 0));
10839 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10840 mode_line_string_list = AREF (vector, 2);
10841 if (! EQ (AREF (vector, 3), Qt))
10842 mode_line_proptrans_alist = AREF (vector, 3);
10843 mode_line_string_face = AREF (vector, 4);
10844 mode_line_string_face_prop = AREF (vector, 5);
10846 if (!NILP (AREF (vector, 7)))
10847 /* Select window before buffer, since it may change the buffer. */
10848 Fselect_window (AREF (vector, 7), Qt);
10850 if (!NILP (AREF (vector, 6)))
10852 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10853 ASET (vector, 6, Qnil);
10856 Vmode_line_unwind_vector = vector;
10857 return Qnil;
10861 /* Store a single character C for the frame title in mode_line_noprop_buf.
10862 Re-allocate mode_line_noprop_buf if necessary. */
10864 static void
10865 store_mode_line_noprop_char (char c)
10867 /* If output position has reached the end of the allocated buffer,
10868 increase the buffer's size. */
10869 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10871 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10872 ptrdiff_t size = len;
10873 mode_line_noprop_buf =
10874 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10875 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10876 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10879 *mode_line_noprop_ptr++ = c;
10883 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10884 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10885 characters that yield more columns than PRECISION; PRECISION <= 0
10886 means copy the whole string. Pad with spaces until FIELD_WIDTH
10887 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10888 pad. Called from display_mode_element when it is used to build a
10889 frame title. */
10891 static int
10892 store_mode_line_noprop (const char *string, int field_width, int precision)
10894 const unsigned char *str = (const unsigned char *) string;
10895 int n = 0;
10896 EMACS_INT dummy, nbytes;
10898 /* Copy at most PRECISION chars from STR. */
10899 nbytes = strlen (string);
10900 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10901 while (nbytes--)
10902 store_mode_line_noprop_char (*str++);
10904 /* Fill up with spaces until FIELD_WIDTH reached. */
10905 while (field_width > 0
10906 && n < field_width)
10908 store_mode_line_noprop_char (' ');
10909 ++n;
10912 return n;
10915 /***********************************************************************
10916 Frame Titles
10917 ***********************************************************************/
10919 #ifdef HAVE_WINDOW_SYSTEM
10921 /* Set the title of FRAME, if it has changed. The title format is
10922 Vicon_title_format if FRAME is iconified, otherwise it is
10923 frame_title_format. */
10925 static void
10926 x_consider_frame_title (Lisp_Object frame)
10928 struct frame *f = XFRAME (frame);
10930 if (FRAME_WINDOW_P (f)
10931 || FRAME_MINIBUF_ONLY_P (f)
10932 || f->explicit_name)
10934 /* Do we have more than one visible frame on this X display? */
10935 Lisp_Object tail;
10936 Lisp_Object fmt;
10937 ptrdiff_t title_start;
10938 char *title;
10939 ptrdiff_t len;
10940 struct it it;
10941 int count = SPECPDL_INDEX ();
10943 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10945 Lisp_Object other_frame = XCAR (tail);
10946 struct frame *tf = XFRAME (other_frame);
10948 if (tf != f
10949 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10950 && !FRAME_MINIBUF_ONLY_P (tf)
10951 && !EQ (other_frame, tip_frame)
10952 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10953 break;
10956 /* Set global variable indicating that multiple frames exist. */
10957 multiple_frames = CONSP (tail);
10959 /* Switch to the buffer of selected window of the frame. Set up
10960 mode_line_target so that display_mode_element will output into
10961 mode_line_noprop_buf; then display the title. */
10962 record_unwind_protect (unwind_format_mode_line,
10963 format_mode_line_unwind_data
10964 (current_buffer, selected_window, 0));
10966 Fselect_window (f->selected_window, Qt);
10967 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10968 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10970 mode_line_target = MODE_LINE_TITLE;
10971 title_start = MODE_LINE_NOPROP_LEN (0);
10972 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10973 NULL, DEFAULT_FACE_ID);
10974 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10975 len = MODE_LINE_NOPROP_LEN (title_start);
10976 title = mode_line_noprop_buf + title_start;
10977 unbind_to (count, Qnil);
10979 /* Set the title only if it's changed. This avoids consing in
10980 the common case where it hasn't. (If it turns out that we've
10981 already wasted too much time by walking through the list with
10982 display_mode_element, then we might need to optimize at a
10983 higher level than this.) */
10984 if (! STRINGP (f->name)
10985 || SBYTES (f->name) != len
10986 || memcmp (title, SDATA (f->name), len) != 0)
10987 x_implicitly_set_name (f, make_string (title, len), Qnil);
10991 #endif /* not HAVE_WINDOW_SYSTEM */
10996 /***********************************************************************
10997 Menu Bars
10998 ***********************************************************************/
11001 /* Prepare for redisplay by updating menu-bar item lists when
11002 appropriate. This can call eval. */
11004 void
11005 prepare_menu_bars (void)
11007 int all_windows;
11008 struct gcpro gcpro1, gcpro2;
11009 struct frame *f;
11010 Lisp_Object tooltip_frame;
11012 #ifdef HAVE_WINDOW_SYSTEM
11013 tooltip_frame = tip_frame;
11014 #else
11015 tooltip_frame = Qnil;
11016 #endif
11018 /* Update all frame titles based on their buffer names, etc. We do
11019 this before the menu bars so that the buffer-menu will show the
11020 up-to-date frame titles. */
11021 #ifdef HAVE_WINDOW_SYSTEM
11022 if (windows_or_buffers_changed || update_mode_lines)
11024 Lisp_Object tail, frame;
11026 FOR_EACH_FRAME (tail, frame)
11028 f = XFRAME (frame);
11029 if (!EQ (frame, tooltip_frame)
11030 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11031 x_consider_frame_title (frame);
11034 #endif /* HAVE_WINDOW_SYSTEM */
11036 /* Update the menu bar item lists, if appropriate. This has to be
11037 done before any actual redisplay or generation of display lines. */
11038 all_windows = (update_mode_lines
11039 || buffer_shared > 1
11040 || windows_or_buffers_changed);
11041 if (all_windows)
11043 Lisp_Object tail, frame;
11044 int count = SPECPDL_INDEX ();
11045 /* 1 means that update_menu_bar has run its hooks
11046 so any further calls to update_menu_bar shouldn't do so again. */
11047 int menu_bar_hooks_run = 0;
11049 record_unwind_save_match_data ();
11051 FOR_EACH_FRAME (tail, frame)
11053 f = XFRAME (frame);
11055 /* Ignore tooltip frame. */
11056 if (EQ (frame, tooltip_frame))
11057 continue;
11059 /* If a window on this frame changed size, report that to
11060 the user and clear the size-change flag. */
11061 if (FRAME_WINDOW_SIZES_CHANGED (f))
11063 Lisp_Object functions;
11065 /* Clear flag first in case we get an error below. */
11066 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11067 functions = Vwindow_size_change_functions;
11068 GCPRO2 (tail, functions);
11070 while (CONSP (functions))
11072 if (!EQ (XCAR (functions), Qt))
11073 call1 (XCAR (functions), frame);
11074 functions = XCDR (functions);
11076 UNGCPRO;
11079 GCPRO1 (tail);
11080 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11081 #ifdef HAVE_WINDOW_SYSTEM
11082 update_tool_bar (f, 0);
11083 #endif
11084 #ifdef HAVE_NS
11085 if (windows_or_buffers_changed
11086 && FRAME_NS_P (f))
11087 ns_set_doc_edited (f, Fbuffer_modified_p
11088 (XWINDOW (f->selected_window)->buffer));
11089 #endif
11090 UNGCPRO;
11093 unbind_to (count, Qnil);
11095 else
11097 struct frame *sf = SELECTED_FRAME ();
11098 update_menu_bar (sf, 1, 0);
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 update_tool_bar (sf, 1);
11101 #endif
11106 /* Update the menu bar item list for frame F. This has to be done
11107 before we start to fill in any display lines, because it can call
11108 eval.
11110 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11112 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11113 already ran the menu bar hooks for this redisplay, so there
11114 is no need to run them again. The return value is the
11115 updated value of this flag, to pass to the next call. */
11117 static int
11118 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11120 Lisp_Object window;
11121 register struct window *w;
11123 /* If called recursively during a menu update, do nothing. This can
11124 happen when, for instance, an activate-menubar-hook causes a
11125 redisplay. */
11126 if (inhibit_menubar_update)
11127 return hooks_run;
11129 window = FRAME_SELECTED_WINDOW (f);
11130 w = XWINDOW (window);
11132 if (FRAME_WINDOW_P (f)
11134 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11135 || defined (HAVE_NS) || defined (USE_GTK)
11136 FRAME_EXTERNAL_MENU_BAR (f)
11137 #else
11138 FRAME_MENU_BAR_LINES (f) > 0
11139 #endif
11140 : FRAME_MENU_BAR_LINES (f) > 0)
11142 /* If the user has switched buffers or windows, we need to
11143 recompute to reflect the new bindings. But we'll
11144 recompute when update_mode_lines is set too; that means
11145 that people can use force-mode-line-update to request
11146 that the menu bar be recomputed. The adverse effect on
11147 the rest of the redisplay algorithm is about the same as
11148 windows_or_buffers_changed anyway. */
11149 if (windows_or_buffers_changed
11150 /* This used to test w->update_mode_line, but we believe
11151 there is no need to recompute the menu in that case. */
11152 || update_mode_lines
11153 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11154 < BUF_MODIFF (XBUFFER (w->buffer)))
11155 != !NILP (w->last_had_star))
11156 || ((!NILP (Vtransient_mark_mode)
11157 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11158 != !NILP (w->region_showing)))
11160 struct buffer *prev = current_buffer;
11161 int count = SPECPDL_INDEX ();
11163 specbind (Qinhibit_menubar_update, Qt);
11165 set_buffer_internal_1 (XBUFFER (w->buffer));
11166 if (save_match_data)
11167 record_unwind_save_match_data ();
11168 if (NILP (Voverriding_local_map_menu_flag))
11170 specbind (Qoverriding_terminal_local_map, Qnil);
11171 specbind (Qoverriding_local_map, Qnil);
11174 if (!hooks_run)
11176 /* Run the Lucid hook. */
11177 safe_run_hooks (Qactivate_menubar_hook);
11179 /* If it has changed current-menubar from previous value,
11180 really recompute the menu-bar from the value. */
11181 if (! NILP (Vlucid_menu_bar_dirty_flag))
11182 call0 (Qrecompute_lucid_menubar);
11184 safe_run_hooks (Qmenu_bar_update_hook);
11186 hooks_run = 1;
11189 XSETFRAME (Vmenu_updating_frame, f);
11190 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11192 /* Redisplay the menu bar in case we changed it. */
11193 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11194 || defined (HAVE_NS) || defined (USE_GTK)
11195 if (FRAME_WINDOW_P (f))
11197 #if defined (HAVE_NS)
11198 /* All frames on Mac OS share the same menubar. So only
11199 the selected frame should be allowed to set it. */
11200 if (f == SELECTED_FRAME ())
11201 #endif
11202 set_frame_menubar (f, 0, 0);
11204 else
11205 /* On a terminal screen, the menu bar is an ordinary screen
11206 line, and this makes it get updated. */
11207 w->update_mode_line = Qt;
11208 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11209 /* In the non-toolkit version, the menu bar is an ordinary screen
11210 line, and this makes it get updated. */
11211 w->update_mode_line = Qt;
11212 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11214 unbind_to (count, Qnil);
11215 set_buffer_internal_1 (prev);
11219 return hooks_run;
11224 /***********************************************************************
11225 Output Cursor
11226 ***********************************************************************/
11228 #ifdef HAVE_WINDOW_SYSTEM
11230 /* EXPORT:
11231 Nominal cursor position -- where to draw output.
11232 HPOS and VPOS are window relative glyph matrix coordinates.
11233 X and Y are window relative pixel coordinates. */
11235 struct cursor_pos output_cursor;
11238 /* EXPORT:
11239 Set the global variable output_cursor to CURSOR. All cursor
11240 positions are relative to updated_window. */
11242 void
11243 set_output_cursor (struct cursor_pos *cursor)
11245 output_cursor.hpos = cursor->hpos;
11246 output_cursor.vpos = cursor->vpos;
11247 output_cursor.x = cursor->x;
11248 output_cursor.y = cursor->y;
11252 /* EXPORT for RIF:
11253 Set a nominal cursor position.
11255 HPOS and VPOS are column/row positions in a window glyph matrix. X
11256 and Y are window text area relative pixel positions.
11258 If this is done during an update, updated_window will contain the
11259 window that is being updated and the position is the future output
11260 cursor position for that window. If updated_window is null, use
11261 selected_window and display the cursor at the given position. */
11263 void
11264 x_cursor_to (int vpos, int hpos, int y, int x)
11266 struct window *w;
11268 /* If updated_window is not set, work on selected_window. */
11269 if (updated_window)
11270 w = updated_window;
11271 else
11272 w = XWINDOW (selected_window);
11274 /* Set the output cursor. */
11275 output_cursor.hpos = hpos;
11276 output_cursor.vpos = vpos;
11277 output_cursor.x = x;
11278 output_cursor.y = y;
11280 /* If not called as part of an update, really display the cursor.
11281 This will also set the cursor position of W. */
11282 if (updated_window == NULL)
11284 BLOCK_INPUT;
11285 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11286 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11287 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11288 UNBLOCK_INPUT;
11292 #endif /* HAVE_WINDOW_SYSTEM */
11295 /***********************************************************************
11296 Tool-bars
11297 ***********************************************************************/
11299 #ifdef HAVE_WINDOW_SYSTEM
11301 /* Where the mouse was last time we reported a mouse event. */
11303 FRAME_PTR last_mouse_frame;
11305 /* Tool-bar item index of the item on which a mouse button was pressed
11306 or -1. */
11308 int last_tool_bar_item;
11311 static Lisp_Object
11312 update_tool_bar_unwind (Lisp_Object frame)
11314 selected_frame = frame;
11315 return Qnil;
11318 /* Update the tool-bar item list for frame F. This has to be done
11319 before we start to fill in any display lines. Called from
11320 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11321 and restore it here. */
11323 static void
11324 update_tool_bar (struct frame *f, int save_match_data)
11326 #if defined (USE_GTK) || defined (HAVE_NS)
11327 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11328 #else
11329 int do_update = WINDOWP (f->tool_bar_window)
11330 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11331 #endif
11333 if (do_update)
11335 Lisp_Object window;
11336 struct window *w;
11338 window = FRAME_SELECTED_WINDOW (f);
11339 w = XWINDOW (window);
11341 /* If the user has switched buffers or windows, we need to
11342 recompute to reflect the new bindings. But we'll
11343 recompute when update_mode_lines is set too; that means
11344 that people can use force-mode-line-update to request
11345 that the menu bar be recomputed. The adverse effect on
11346 the rest of the redisplay algorithm is about the same as
11347 windows_or_buffers_changed anyway. */
11348 if (windows_or_buffers_changed
11349 || !NILP (w->update_mode_line)
11350 || update_mode_lines
11351 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11352 < BUF_MODIFF (XBUFFER (w->buffer)))
11353 != !NILP (w->last_had_star))
11354 || ((!NILP (Vtransient_mark_mode)
11355 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11356 != !NILP (w->region_showing)))
11358 struct buffer *prev = current_buffer;
11359 int count = SPECPDL_INDEX ();
11360 Lisp_Object frame, new_tool_bar;
11361 int new_n_tool_bar;
11362 struct gcpro gcpro1;
11364 /* Set current_buffer to the buffer of the selected
11365 window of the frame, so that we get the right local
11366 keymaps. */
11367 set_buffer_internal_1 (XBUFFER (w->buffer));
11369 /* Save match data, if we must. */
11370 if (save_match_data)
11371 record_unwind_save_match_data ();
11373 /* Make sure that we don't accidentally use bogus keymaps. */
11374 if (NILP (Voverriding_local_map_menu_flag))
11376 specbind (Qoverriding_terminal_local_map, Qnil);
11377 specbind (Qoverriding_local_map, Qnil);
11380 GCPRO1 (new_tool_bar);
11382 /* We must temporarily set the selected frame to this frame
11383 before calling tool_bar_items, because the calculation of
11384 the tool-bar keymap uses the selected frame (see
11385 `tool-bar-make-keymap' in tool-bar.el). */
11386 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11387 XSETFRAME (frame, f);
11388 selected_frame = frame;
11390 /* Build desired tool-bar items from keymaps. */
11391 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11392 &new_n_tool_bar);
11394 /* Redisplay the tool-bar if we changed it. */
11395 if (new_n_tool_bar != f->n_tool_bar_items
11396 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11398 /* Redisplay that happens asynchronously due to an expose event
11399 may access f->tool_bar_items. Make sure we update both
11400 variables within BLOCK_INPUT so no such event interrupts. */
11401 BLOCK_INPUT;
11402 f->tool_bar_items = new_tool_bar;
11403 f->n_tool_bar_items = new_n_tool_bar;
11404 w->update_mode_line = Qt;
11405 UNBLOCK_INPUT;
11408 UNGCPRO;
11410 unbind_to (count, Qnil);
11411 set_buffer_internal_1 (prev);
11417 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11418 F's desired tool-bar contents. F->tool_bar_items must have
11419 been set up previously by calling prepare_menu_bars. */
11421 static void
11422 build_desired_tool_bar_string (struct frame *f)
11424 int i, size, size_needed;
11425 struct gcpro gcpro1, gcpro2, gcpro3;
11426 Lisp_Object image, plist, props;
11428 image = plist = props = Qnil;
11429 GCPRO3 (image, plist, props);
11431 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11432 Otherwise, make a new string. */
11434 /* The size of the string we might be able to reuse. */
11435 size = (STRINGP (f->desired_tool_bar_string)
11436 ? SCHARS (f->desired_tool_bar_string)
11437 : 0);
11439 /* We need one space in the string for each image. */
11440 size_needed = f->n_tool_bar_items;
11442 /* Reuse f->desired_tool_bar_string, if possible. */
11443 if (size < size_needed || NILP (f->desired_tool_bar_string))
11444 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11445 make_number (' '));
11446 else
11448 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11449 Fremove_text_properties (make_number (0), make_number (size),
11450 props, f->desired_tool_bar_string);
11453 /* Put a `display' property on the string for the images to display,
11454 put a `menu_item' property on tool-bar items with a value that
11455 is the index of the item in F's tool-bar item vector. */
11456 for (i = 0; i < f->n_tool_bar_items; ++i)
11458 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11460 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11461 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11462 int hmargin, vmargin, relief, idx, end;
11464 /* If image is a vector, choose the image according to the
11465 button state. */
11466 image = PROP (TOOL_BAR_ITEM_IMAGES);
11467 if (VECTORP (image))
11469 if (enabled_p)
11470 idx = (selected_p
11471 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11472 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11473 else
11474 idx = (selected_p
11475 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11476 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11478 xassert (ASIZE (image) >= idx);
11479 image = AREF (image, idx);
11481 else
11482 idx = -1;
11484 /* Ignore invalid image specifications. */
11485 if (!valid_image_p (image))
11486 continue;
11488 /* Display the tool-bar button pressed, or depressed. */
11489 plist = Fcopy_sequence (XCDR (image));
11491 /* Compute margin and relief to draw. */
11492 relief = (tool_bar_button_relief >= 0
11493 ? tool_bar_button_relief
11494 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11495 hmargin = vmargin = relief;
11497 if (INTEGERP (Vtool_bar_button_margin)
11498 && XINT (Vtool_bar_button_margin) > 0)
11500 hmargin += XFASTINT (Vtool_bar_button_margin);
11501 vmargin += XFASTINT (Vtool_bar_button_margin);
11503 else if (CONSP (Vtool_bar_button_margin))
11505 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11506 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11507 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11509 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11510 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11511 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11514 if (auto_raise_tool_bar_buttons_p)
11516 /* Add a `:relief' property to the image spec if the item is
11517 selected. */
11518 if (selected_p)
11520 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11521 hmargin -= relief;
11522 vmargin -= relief;
11525 else
11527 /* If image is selected, display it pressed, i.e. with a
11528 negative relief. If it's not selected, display it with a
11529 raised relief. */
11530 plist = Fplist_put (plist, QCrelief,
11531 (selected_p
11532 ? make_number (-relief)
11533 : make_number (relief)));
11534 hmargin -= relief;
11535 vmargin -= relief;
11538 /* Put a margin around the image. */
11539 if (hmargin || vmargin)
11541 if (hmargin == vmargin)
11542 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11543 else
11544 plist = Fplist_put (plist, QCmargin,
11545 Fcons (make_number (hmargin),
11546 make_number (vmargin)));
11549 /* If button is not enabled, and we don't have special images
11550 for the disabled state, make the image appear disabled by
11551 applying an appropriate algorithm to it. */
11552 if (!enabled_p && idx < 0)
11553 plist = Fplist_put (plist, QCconversion, Qdisabled);
11555 /* Put a `display' text property on the string for the image to
11556 display. Put a `menu-item' property on the string that gives
11557 the start of this item's properties in the tool-bar items
11558 vector. */
11559 image = Fcons (Qimage, plist);
11560 props = list4 (Qdisplay, image,
11561 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11563 /* Let the last image hide all remaining spaces in the tool bar
11564 string. The string can be longer than needed when we reuse a
11565 previous string. */
11566 if (i + 1 == f->n_tool_bar_items)
11567 end = SCHARS (f->desired_tool_bar_string);
11568 else
11569 end = i + 1;
11570 Fadd_text_properties (make_number (i), make_number (end),
11571 props, f->desired_tool_bar_string);
11572 #undef PROP
11575 UNGCPRO;
11579 /* Display one line of the tool-bar of frame IT->f.
11581 HEIGHT specifies the desired height of the tool-bar line.
11582 If the actual height of the glyph row is less than HEIGHT, the
11583 row's height is increased to HEIGHT, and the icons are centered
11584 vertically in the new height.
11586 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11587 count a final empty row in case the tool-bar width exactly matches
11588 the window width.
11591 static void
11592 display_tool_bar_line (struct it *it, int height)
11594 struct glyph_row *row = it->glyph_row;
11595 int max_x = it->last_visible_x;
11596 struct glyph *last;
11598 prepare_desired_row (row);
11599 row->y = it->current_y;
11601 /* Note that this isn't made use of if the face hasn't a box,
11602 so there's no need to check the face here. */
11603 it->start_of_box_run_p = 1;
11605 while (it->current_x < max_x)
11607 int x, n_glyphs_before, i, nglyphs;
11608 struct it it_before;
11610 /* Get the next display element. */
11611 if (!get_next_display_element (it))
11613 /* Don't count empty row if we are counting needed tool-bar lines. */
11614 if (height < 0 && !it->hpos)
11615 return;
11616 break;
11619 /* Produce glyphs. */
11620 n_glyphs_before = row->used[TEXT_AREA];
11621 it_before = *it;
11623 PRODUCE_GLYPHS (it);
11625 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11626 i = 0;
11627 x = it_before.current_x;
11628 while (i < nglyphs)
11630 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11632 if (x + glyph->pixel_width > max_x)
11634 /* Glyph doesn't fit on line. Backtrack. */
11635 row->used[TEXT_AREA] = n_glyphs_before;
11636 *it = it_before;
11637 /* If this is the only glyph on this line, it will never fit on the
11638 tool-bar, so skip it. But ensure there is at least one glyph,
11639 so we don't accidentally disable the tool-bar. */
11640 if (n_glyphs_before == 0
11641 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11642 break;
11643 goto out;
11646 ++it->hpos;
11647 x += glyph->pixel_width;
11648 ++i;
11651 /* Stop at line end. */
11652 if (ITERATOR_AT_END_OF_LINE_P (it))
11653 break;
11655 set_iterator_to_next (it, 1);
11658 out:;
11660 row->displays_text_p = row->used[TEXT_AREA] != 0;
11662 /* Use default face for the border below the tool bar.
11664 FIXME: When auto-resize-tool-bars is grow-only, there is
11665 no additional border below the possibly empty tool-bar lines.
11666 So to make the extra empty lines look "normal", we have to
11667 use the tool-bar face for the border too. */
11668 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11669 it->face_id = DEFAULT_FACE_ID;
11671 extend_face_to_end_of_line (it);
11672 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11673 last->right_box_line_p = 1;
11674 if (last == row->glyphs[TEXT_AREA])
11675 last->left_box_line_p = 1;
11677 /* Make line the desired height and center it vertically. */
11678 if ((height -= it->max_ascent + it->max_descent) > 0)
11680 /* Don't add more than one line height. */
11681 height %= FRAME_LINE_HEIGHT (it->f);
11682 it->max_ascent += height / 2;
11683 it->max_descent += (height + 1) / 2;
11686 compute_line_metrics (it);
11688 /* If line is empty, make it occupy the rest of the tool-bar. */
11689 if (!row->displays_text_p)
11691 row->height = row->phys_height = it->last_visible_y - row->y;
11692 row->visible_height = row->height;
11693 row->ascent = row->phys_ascent = 0;
11694 row->extra_line_spacing = 0;
11697 row->full_width_p = 1;
11698 row->continued_p = 0;
11699 row->truncated_on_left_p = 0;
11700 row->truncated_on_right_p = 0;
11702 it->current_x = it->hpos = 0;
11703 it->current_y += row->height;
11704 ++it->vpos;
11705 ++it->glyph_row;
11709 /* Max tool-bar height. */
11711 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11712 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11714 /* Value is the number of screen lines needed to make all tool-bar
11715 items of frame F visible. The number of actual rows needed is
11716 returned in *N_ROWS if non-NULL. */
11718 static int
11719 tool_bar_lines_needed (struct frame *f, int *n_rows)
11721 struct window *w = XWINDOW (f->tool_bar_window);
11722 struct it it;
11723 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11724 the desired matrix, so use (unused) mode-line row as temporary row to
11725 avoid destroying the first tool-bar row. */
11726 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11728 /* Initialize an iterator for iteration over
11729 F->desired_tool_bar_string in the tool-bar window of frame F. */
11730 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11731 it.first_visible_x = 0;
11732 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11733 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11734 it.paragraph_embedding = L2R;
11736 while (!ITERATOR_AT_END_P (&it))
11738 clear_glyph_row (temp_row);
11739 it.glyph_row = temp_row;
11740 display_tool_bar_line (&it, -1);
11742 clear_glyph_row (temp_row);
11744 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11745 if (n_rows)
11746 *n_rows = it.vpos > 0 ? it.vpos : -1;
11748 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11752 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11753 0, 1, 0,
11754 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11755 (Lisp_Object frame)
11757 struct frame *f;
11758 struct window *w;
11759 int nlines = 0;
11761 if (NILP (frame))
11762 frame = selected_frame;
11763 else
11764 CHECK_FRAME (frame);
11765 f = XFRAME (frame);
11767 if (WINDOWP (f->tool_bar_window)
11768 && (w = XWINDOW (f->tool_bar_window),
11769 WINDOW_TOTAL_LINES (w) > 0))
11771 update_tool_bar (f, 1);
11772 if (f->n_tool_bar_items)
11774 build_desired_tool_bar_string (f);
11775 nlines = tool_bar_lines_needed (f, NULL);
11779 return make_number (nlines);
11783 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11784 height should be changed. */
11786 static int
11787 redisplay_tool_bar (struct frame *f)
11789 struct window *w;
11790 struct it it;
11791 struct glyph_row *row;
11793 #if defined (USE_GTK) || defined (HAVE_NS)
11794 if (FRAME_EXTERNAL_TOOL_BAR (f))
11795 update_frame_tool_bar (f);
11796 return 0;
11797 #endif
11799 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11800 do anything. This means you must start with tool-bar-lines
11801 non-zero to get the auto-sizing effect. Or in other words, you
11802 can turn off tool-bars by specifying tool-bar-lines zero. */
11803 if (!WINDOWP (f->tool_bar_window)
11804 || (w = XWINDOW (f->tool_bar_window),
11805 WINDOW_TOTAL_LINES (w) == 0))
11806 return 0;
11808 /* Set up an iterator for the tool-bar window. */
11809 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11810 it.first_visible_x = 0;
11811 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11812 row = it.glyph_row;
11814 /* Build a string that represents the contents of the tool-bar. */
11815 build_desired_tool_bar_string (f);
11816 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11817 /* FIXME: This should be controlled by a user option. But it
11818 doesn't make sense to have an R2L tool bar if the menu bar cannot
11819 be drawn also R2L, and making the menu bar R2L is tricky due
11820 toolkit-specific code that implements it. If an R2L tool bar is
11821 ever supported, display_tool_bar_line should also be augmented to
11822 call unproduce_glyphs like display_line and display_string
11823 do. */
11824 it.paragraph_embedding = L2R;
11826 if (f->n_tool_bar_rows == 0)
11828 int nlines;
11830 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11831 nlines != WINDOW_TOTAL_LINES (w)))
11833 Lisp_Object frame;
11834 int old_height = WINDOW_TOTAL_LINES (w);
11836 XSETFRAME (frame, f);
11837 Fmodify_frame_parameters (frame,
11838 Fcons (Fcons (Qtool_bar_lines,
11839 make_number (nlines)),
11840 Qnil));
11841 if (WINDOW_TOTAL_LINES (w) != old_height)
11843 clear_glyph_matrix (w->desired_matrix);
11844 fonts_changed_p = 1;
11845 return 1;
11850 /* Display as many lines as needed to display all tool-bar items. */
11852 if (f->n_tool_bar_rows > 0)
11854 int border, rows, height, extra;
11856 if (INTEGERP (Vtool_bar_border))
11857 border = XINT (Vtool_bar_border);
11858 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11859 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11860 else if (EQ (Vtool_bar_border, Qborder_width))
11861 border = f->border_width;
11862 else
11863 border = 0;
11864 if (border < 0)
11865 border = 0;
11867 rows = f->n_tool_bar_rows;
11868 height = max (1, (it.last_visible_y - border) / rows);
11869 extra = it.last_visible_y - border - height * rows;
11871 while (it.current_y < it.last_visible_y)
11873 int h = 0;
11874 if (extra > 0 && rows-- > 0)
11876 h = (extra + rows - 1) / rows;
11877 extra -= h;
11879 display_tool_bar_line (&it, height + h);
11882 else
11884 while (it.current_y < it.last_visible_y)
11885 display_tool_bar_line (&it, 0);
11888 /* It doesn't make much sense to try scrolling in the tool-bar
11889 window, so don't do it. */
11890 w->desired_matrix->no_scrolling_p = 1;
11891 w->must_be_updated_p = 1;
11893 if (!NILP (Vauto_resize_tool_bars))
11895 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11896 int change_height_p = 0;
11898 /* If we couldn't display everything, change the tool-bar's
11899 height if there is room for more. */
11900 if (IT_STRING_CHARPOS (it) < it.end_charpos
11901 && it.current_y < max_tool_bar_height)
11902 change_height_p = 1;
11904 row = it.glyph_row - 1;
11906 /* If there are blank lines at the end, except for a partially
11907 visible blank line at the end that is smaller than
11908 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11909 if (!row->displays_text_p
11910 && row->height >= FRAME_LINE_HEIGHT (f))
11911 change_height_p = 1;
11913 /* If row displays tool-bar items, but is partially visible,
11914 change the tool-bar's height. */
11915 if (row->displays_text_p
11916 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11917 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11918 change_height_p = 1;
11920 /* Resize windows as needed by changing the `tool-bar-lines'
11921 frame parameter. */
11922 if (change_height_p)
11924 Lisp_Object frame;
11925 int old_height = WINDOW_TOTAL_LINES (w);
11926 int nrows;
11927 int nlines = tool_bar_lines_needed (f, &nrows);
11929 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11930 && !f->minimize_tool_bar_window_p)
11931 ? (nlines > old_height)
11932 : (nlines != old_height));
11933 f->minimize_tool_bar_window_p = 0;
11935 if (change_height_p)
11937 XSETFRAME (frame, f);
11938 Fmodify_frame_parameters (frame,
11939 Fcons (Fcons (Qtool_bar_lines,
11940 make_number (nlines)),
11941 Qnil));
11942 if (WINDOW_TOTAL_LINES (w) != old_height)
11944 clear_glyph_matrix (w->desired_matrix);
11945 f->n_tool_bar_rows = nrows;
11946 fonts_changed_p = 1;
11947 return 1;
11953 f->minimize_tool_bar_window_p = 0;
11954 return 0;
11958 /* Get information about the tool-bar item which is displayed in GLYPH
11959 on frame F. Return in *PROP_IDX the index where tool-bar item
11960 properties start in F->tool_bar_items. Value is zero if
11961 GLYPH doesn't display a tool-bar item. */
11963 static int
11964 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11966 Lisp_Object prop;
11967 int success_p;
11968 int charpos;
11970 /* This function can be called asynchronously, which means we must
11971 exclude any possibility that Fget_text_property signals an
11972 error. */
11973 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11974 charpos = max (0, charpos);
11976 /* Get the text property `menu-item' at pos. The value of that
11977 property is the start index of this item's properties in
11978 F->tool_bar_items. */
11979 prop = Fget_text_property (make_number (charpos),
11980 Qmenu_item, f->current_tool_bar_string);
11981 if (INTEGERP (prop))
11983 *prop_idx = XINT (prop);
11984 success_p = 1;
11986 else
11987 success_p = 0;
11989 return success_p;
11993 /* Get information about the tool-bar item at position X/Y on frame F.
11994 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11995 the current matrix of the tool-bar window of F, or NULL if not
11996 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11997 item in F->tool_bar_items. Value is
11999 -1 if X/Y is not on a tool-bar item
12000 0 if X/Y is on the same item that was highlighted before.
12001 1 otherwise. */
12003 static int
12004 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12005 int *hpos, int *vpos, int *prop_idx)
12007 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12008 struct window *w = XWINDOW (f->tool_bar_window);
12009 int area;
12011 /* Find the glyph under X/Y. */
12012 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12013 if (*glyph == NULL)
12014 return -1;
12016 /* Get the start of this tool-bar item's properties in
12017 f->tool_bar_items. */
12018 if (!tool_bar_item_info (f, *glyph, prop_idx))
12019 return -1;
12021 /* Is mouse on the highlighted item? */
12022 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12023 && *vpos >= hlinfo->mouse_face_beg_row
12024 && *vpos <= hlinfo->mouse_face_end_row
12025 && (*vpos > hlinfo->mouse_face_beg_row
12026 || *hpos >= hlinfo->mouse_face_beg_col)
12027 && (*vpos < hlinfo->mouse_face_end_row
12028 || *hpos < hlinfo->mouse_face_end_col
12029 || hlinfo->mouse_face_past_end))
12030 return 0;
12032 return 1;
12036 /* EXPORT:
12037 Handle mouse button event on the tool-bar of frame F, at
12038 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12039 0 for button release. MODIFIERS is event modifiers for button
12040 release. */
12042 void
12043 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12044 unsigned int modifiers)
12046 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12047 struct window *w = XWINDOW (f->tool_bar_window);
12048 int hpos, vpos, prop_idx;
12049 struct glyph *glyph;
12050 Lisp_Object enabled_p;
12052 /* If not on the highlighted tool-bar item, return. */
12053 frame_to_window_pixel_xy (w, &x, &y);
12054 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12055 return;
12057 /* If item is disabled, do nothing. */
12058 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12059 if (NILP (enabled_p))
12060 return;
12062 if (down_p)
12064 /* Show item in pressed state. */
12065 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12066 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12067 last_tool_bar_item = prop_idx;
12069 else
12071 Lisp_Object key, frame;
12072 struct input_event event;
12073 EVENT_INIT (event);
12075 /* Show item in released state. */
12076 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12077 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12079 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12081 XSETFRAME (frame, f);
12082 event.kind = TOOL_BAR_EVENT;
12083 event.frame_or_window = frame;
12084 event.arg = frame;
12085 kbd_buffer_store_event (&event);
12087 event.kind = TOOL_BAR_EVENT;
12088 event.frame_or_window = frame;
12089 event.arg = key;
12090 event.modifiers = modifiers;
12091 kbd_buffer_store_event (&event);
12092 last_tool_bar_item = -1;
12097 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12098 tool-bar window-relative coordinates X/Y. Called from
12099 note_mouse_highlight. */
12101 static void
12102 note_tool_bar_highlight (struct frame *f, int x, int y)
12104 Lisp_Object window = f->tool_bar_window;
12105 struct window *w = XWINDOW (window);
12106 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12107 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12108 int hpos, vpos;
12109 struct glyph *glyph;
12110 struct glyph_row *row;
12111 int i;
12112 Lisp_Object enabled_p;
12113 int prop_idx;
12114 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12115 int mouse_down_p, rc;
12117 /* Function note_mouse_highlight is called with negative X/Y
12118 values when mouse moves outside of the frame. */
12119 if (x <= 0 || y <= 0)
12121 clear_mouse_face (hlinfo);
12122 return;
12125 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12126 if (rc < 0)
12128 /* Not on tool-bar item. */
12129 clear_mouse_face (hlinfo);
12130 return;
12132 else if (rc == 0)
12133 /* On same tool-bar item as before. */
12134 goto set_help_echo;
12136 clear_mouse_face (hlinfo);
12138 /* Mouse is down, but on different tool-bar item? */
12139 mouse_down_p = (dpyinfo->grabbed
12140 && f == last_mouse_frame
12141 && FRAME_LIVE_P (f));
12142 if (mouse_down_p
12143 && last_tool_bar_item != prop_idx)
12144 return;
12146 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12147 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12149 /* If tool-bar item is not enabled, don't highlight it. */
12150 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12151 if (!NILP (enabled_p))
12153 /* Compute the x-position of the glyph. In front and past the
12154 image is a space. We include this in the highlighted area. */
12155 row = MATRIX_ROW (w->current_matrix, vpos);
12156 for (i = x = 0; i < hpos; ++i)
12157 x += row->glyphs[TEXT_AREA][i].pixel_width;
12159 /* Record this as the current active region. */
12160 hlinfo->mouse_face_beg_col = hpos;
12161 hlinfo->mouse_face_beg_row = vpos;
12162 hlinfo->mouse_face_beg_x = x;
12163 hlinfo->mouse_face_beg_y = row->y;
12164 hlinfo->mouse_face_past_end = 0;
12166 hlinfo->mouse_face_end_col = hpos + 1;
12167 hlinfo->mouse_face_end_row = vpos;
12168 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12169 hlinfo->mouse_face_end_y = row->y;
12170 hlinfo->mouse_face_window = window;
12171 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12173 /* Display it as active. */
12174 show_mouse_face (hlinfo, draw);
12175 hlinfo->mouse_face_image_state = draw;
12178 set_help_echo:
12180 /* Set help_echo_string to a help string to display for this tool-bar item.
12181 XTread_socket does the rest. */
12182 help_echo_object = help_echo_window = Qnil;
12183 help_echo_pos = -1;
12184 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12185 if (NILP (help_echo_string))
12186 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12189 #endif /* HAVE_WINDOW_SYSTEM */
12193 /************************************************************************
12194 Horizontal scrolling
12195 ************************************************************************/
12197 static int hscroll_window_tree (Lisp_Object);
12198 static int hscroll_windows (Lisp_Object);
12200 /* For all leaf windows in the window tree rooted at WINDOW, set their
12201 hscroll value so that PT is (i) visible in the window, and (ii) so
12202 that it is not within a certain margin at the window's left and
12203 right border. Value is non-zero if any window's hscroll has been
12204 changed. */
12206 static int
12207 hscroll_window_tree (Lisp_Object window)
12209 int hscrolled_p = 0;
12210 int hscroll_relative_p = FLOATP (Vhscroll_step);
12211 int hscroll_step_abs = 0;
12212 double hscroll_step_rel = 0;
12214 if (hscroll_relative_p)
12216 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12217 if (hscroll_step_rel < 0)
12219 hscroll_relative_p = 0;
12220 hscroll_step_abs = 0;
12223 else if (INTEGERP (Vhscroll_step))
12225 hscroll_step_abs = XINT (Vhscroll_step);
12226 if (hscroll_step_abs < 0)
12227 hscroll_step_abs = 0;
12229 else
12230 hscroll_step_abs = 0;
12232 while (WINDOWP (window))
12234 struct window *w = XWINDOW (window);
12236 if (WINDOWP (w->hchild))
12237 hscrolled_p |= hscroll_window_tree (w->hchild);
12238 else if (WINDOWP (w->vchild))
12239 hscrolled_p |= hscroll_window_tree (w->vchild);
12240 else if (w->cursor.vpos >= 0)
12242 int h_margin;
12243 int text_area_width;
12244 struct glyph_row *current_cursor_row
12245 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12246 struct glyph_row *desired_cursor_row
12247 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12248 struct glyph_row *cursor_row
12249 = (desired_cursor_row->enabled_p
12250 ? desired_cursor_row
12251 : current_cursor_row);
12252 int row_r2l_p = cursor_row->reversed_p;
12254 text_area_width = window_box_width (w, TEXT_AREA);
12256 /* Scroll when cursor is inside this scroll margin. */
12257 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12259 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12260 /* For left-to-right rows, hscroll when cursor is either
12261 (i) inside the right hscroll margin, or (ii) if it is
12262 inside the left margin and the window is already
12263 hscrolled. */
12264 && ((!row_r2l_p
12265 && ((XFASTINT (w->hscroll)
12266 && w->cursor.x <= h_margin)
12267 || (cursor_row->enabled_p
12268 && cursor_row->truncated_on_right_p
12269 && (w->cursor.x >= text_area_width - h_margin))))
12270 /* For right-to-left rows, the logic is similar,
12271 except that rules for scrolling to left and right
12272 are reversed. E.g., if cursor.x <= h_margin, we
12273 need to hscroll "to the right" unconditionally,
12274 and that will scroll the screen to the left so as
12275 to reveal the next portion of the row. */
12276 || (row_r2l_p
12277 && ((cursor_row->enabled_p
12278 /* FIXME: It is confusing to set the
12279 truncated_on_right_p flag when R2L rows
12280 are actually truncated on the left. */
12281 && cursor_row->truncated_on_right_p
12282 && w->cursor.x <= h_margin)
12283 || (XFASTINT (w->hscroll)
12284 && (w->cursor.x >= text_area_width - h_margin))))))
12286 struct it it;
12287 int hscroll;
12288 struct buffer *saved_current_buffer;
12289 EMACS_INT pt;
12290 int wanted_x;
12292 /* Find point in a display of infinite width. */
12293 saved_current_buffer = current_buffer;
12294 current_buffer = XBUFFER (w->buffer);
12296 if (w == XWINDOW (selected_window))
12297 pt = PT;
12298 else
12300 pt = marker_position (w->pointm);
12301 pt = max (BEGV, pt);
12302 pt = min (ZV, pt);
12305 /* Move iterator to pt starting at cursor_row->start in
12306 a line with infinite width. */
12307 init_to_row_start (&it, w, cursor_row);
12308 it.last_visible_x = INFINITY;
12309 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12310 current_buffer = saved_current_buffer;
12312 /* Position cursor in window. */
12313 if (!hscroll_relative_p && hscroll_step_abs == 0)
12314 hscroll = max (0, (it.current_x
12315 - (ITERATOR_AT_END_OF_LINE_P (&it)
12316 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12317 : (text_area_width / 2))))
12318 / FRAME_COLUMN_WIDTH (it.f);
12319 else if ((!row_r2l_p
12320 && w->cursor.x >= text_area_width - h_margin)
12321 || (row_r2l_p && w->cursor.x <= h_margin))
12323 if (hscroll_relative_p)
12324 wanted_x = text_area_width * (1 - hscroll_step_rel)
12325 - h_margin;
12326 else
12327 wanted_x = text_area_width
12328 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12329 - h_margin;
12330 hscroll
12331 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12333 else
12335 if (hscroll_relative_p)
12336 wanted_x = text_area_width * hscroll_step_rel
12337 + h_margin;
12338 else
12339 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12340 + h_margin;
12341 hscroll
12342 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12344 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12346 /* Don't prevent redisplay optimizations if hscroll
12347 hasn't changed, as it will unnecessarily slow down
12348 redisplay. */
12349 if (XFASTINT (w->hscroll) != hscroll)
12351 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12352 w->hscroll = make_number (hscroll);
12353 hscrolled_p = 1;
12358 window = w->next;
12361 /* Value is non-zero if hscroll of any leaf window has been changed. */
12362 return hscrolled_p;
12366 /* Set hscroll so that cursor is visible and not inside horizontal
12367 scroll margins for all windows in the tree rooted at WINDOW. See
12368 also hscroll_window_tree above. Value is non-zero if any window's
12369 hscroll has been changed. If it has, desired matrices on the frame
12370 of WINDOW are cleared. */
12372 static int
12373 hscroll_windows (Lisp_Object window)
12375 int hscrolled_p = hscroll_window_tree (window);
12376 if (hscrolled_p)
12377 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12378 return hscrolled_p;
12383 /************************************************************************
12384 Redisplay
12385 ************************************************************************/
12387 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12388 to a non-zero value. This is sometimes handy to have in a debugger
12389 session. */
12391 #if GLYPH_DEBUG
12393 /* First and last unchanged row for try_window_id. */
12395 static int debug_first_unchanged_at_end_vpos;
12396 static int debug_last_unchanged_at_beg_vpos;
12398 /* Delta vpos and y. */
12400 static int debug_dvpos, debug_dy;
12402 /* Delta in characters and bytes for try_window_id. */
12404 static EMACS_INT debug_delta, debug_delta_bytes;
12406 /* Values of window_end_pos and window_end_vpos at the end of
12407 try_window_id. */
12409 static EMACS_INT debug_end_vpos;
12411 /* Append a string to W->desired_matrix->method. FMT is a printf
12412 format string. If trace_redisplay_p is non-zero also printf the
12413 resulting string to stderr. */
12415 static void debug_method_add (struct window *, char const *, ...)
12416 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12418 static void
12419 debug_method_add (struct window *w, char const *fmt, ...)
12421 char buffer[512];
12422 char *method = w->desired_matrix->method;
12423 int len = strlen (method);
12424 int size = sizeof w->desired_matrix->method;
12425 int remaining = size - len - 1;
12426 va_list ap;
12428 va_start (ap, fmt);
12429 vsprintf (buffer, fmt, ap);
12430 va_end (ap);
12431 if (len && remaining)
12433 method[len] = '|';
12434 --remaining, ++len;
12437 strncpy (method + len, buffer, remaining);
12439 if (trace_redisplay_p)
12440 fprintf (stderr, "%p (%s): %s\n",
12442 ((BUFFERP (w->buffer)
12443 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12444 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12445 : "no buffer"),
12446 buffer);
12449 #endif /* GLYPH_DEBUG */
12452 /* Value is non-zero if all changes in window W, which displays
12453 current_buffer, are in the text between START and END. START is a
12454 buffer position, END is given as a distance from Z. Used in
12455 redisplay_internal for display optimization. */
12457 static inline int
12458 text_outside_line_unchanged_p (struct window *w,
12459 EMACS_INT start, EMACS_INT end)
12461 int unchanged_p = 1;
12463 /* If text or overlays have changed, see where. */
12464 if (XFASTINT (w->last_modified) < MODIFF
12465 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12467 /* Gap in the line? */
12468 if (GPT < start || Z - GPT < end)
12469 unchanged_p = 0;
12471 /* Changes start in front of the line, or end after it? */
12472 if (unchanged_p
12473 && (BEG_UNCHANGED < start - 1
12474 || END_UNCHANGED < end))
12475 unchanged_p = 0;
12477 /* If selective display, can't optimize if changes start at the
12478 beginning of the line. */
12479 if (unchanged_p
12480 && INTEGERP (BVAR (current_buffer, selective_display))
12481 && XINT (BVAR (current_buffer, selective_display)) > 0
12482 && (BEG_UNCHANGED < start || GPT <= start))
12483 unchanged_p = 0;
12485 /* If there are overlays at the start or end of the line, these
12486 may have overlay strings with newlines in them. A change at
12487 START, for instance, may actually concern the display of such
12488 overlay strings as well, and they are displayed on different
12489 lines. So, quickly rule out this case. (For the future, it
12490 might be desirable to implement something more telling than
12491 just BEG/END_UNCHANGED.) */
12492 if (unchanged_p)
12494 if (BEG + BEG_UNCHANGED == start
12495 && overlay_touches_p (start))
12496 unchanged_p = 0;
12497 if (END_UNCHANGED == end
12498 && overlay_touches_p (Z - end))
12499 unchanged_p = 0;
12502 /* Under bidi reordering, adding or deleting a character in the
12503 beginning of a paragraph, before the first strong directional
12504 character, can change the base direction of the paragraph (unless
12505 the buffer specifies a fixed paragraph direction), which will
12506 require to redisplay the whole paragraph. It might be worthwhile
12507 to find the paragraph limits and widen the range of redisplayed
12508 lines to that, but for now just give up this optimization. */
12509 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12510 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12511 unchanged_p = 0;
12514 return unchanged_p;
12518 /* Do a frame update, taking possible shortcuts into account. This is
12519 the main external entry point for redisplay.
12521 If the last redisplay displayed an echo area message and that message
12522 is no longer requested, we clear the echo area or bring back the
12523 mini-buffer if that is in use. */
12525 void
12526 redisplay (void)
12528 redisplay_internal ();
12532 static Lisp_Object
12533 overlay_arrow_string_or_property (Lisp_Object var)
12535 Lisp_Object val;
12537 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12538 return val;
12540 return Voverlay_arrow_string;
12543 /* Return 1 if there are any overlay-arrows in current_buffer. */
12544 static int
12545 overlay_arrow_in_current_buffer_p (void)
12547 Lisp_Object vlist;
12549 for (vlist = Voverlay_arrow_variable_list;
12550 CONSP (vlist);
12551 vlist = XCDR (vlist))
12553 Lisp_Object var = XCAR (vlist);
12554 Lisp_Object val;
12556 if (!SYMBOLP (var))
12557 continue;
12558 val = find_symbol_value (var);
12559 if (MARKERP (val)
12560 && current_buffer == XMARKER (val)->buffer)
12561 return 1;
12563 return 0;
12567 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12568 has changed. */
12570 static int
12571 overlay_arrows_changed_p (void)
12573 Lisp_Object vlist;
12575 for (vlist = Voverlay_arrow_variable_list;
12576 CONSP (vlist);
12577 vlist = XCDR (vlist))
12579 Lisp_Object var = XCAR (vlist);
12580 Lisp_Object val, pstr;
12582 if (!SYMBOLP (var))
12583 continue;
12584 val = find_symbol_value (var);
12585 if (!MARKERP (val))
12586 continue;
12587 if (! EQ (COERCE_MARKER (val),
12588 Fget (var, Qlast_arrow_position))
12589 || ! (pstr = overlay_arrow_string_or_property (var),
12590 EQ (pstr, Fget (var, Qlast_arrow_string))))
12591 return 1;
12593 return 0;
12596 /* Mark overlay arrows to be updated on next redisplay. */
12598 static void
12599 update_overlay_arrows (int up_to_date)
12601 Lisp_Object vlist;
12603 for (vlist = Voverlay_arrow_variable_list;
12604 CONSP (vlist);
12605 vlist = XCDR (vlist))
12607 Lisp_Object var = XCAR (vlist);
12609 if (!SYMBOLP (var))
12610 continue;
12612 if (up_to_date > 0)
12614 Lisp_Object val = find_symbol_value (var);
12615 Fput (var, Qlast_arrow_position,
12616 COERCE_MARKER (val));
12617 Fput (var, Qlast_arrow_string,
12618 overlay_arrow_string_or_property (var));
12620 else if (up_to_date < 0
12621 || !NILP (Fget (var, Qlast_arrow_position)))
12623 Fput (var, Qlast_arrow_position, Qt);
12624 Fput (var, Qlast_arrow_string, Qt);
12630 /* Return overlay arrow string to display at row.
12631 Return integer (bitmap number) for arrow bitmap in left fringe.
12632 Return nil if no overlay arrow. */
12634 static Lisp_Object
12635 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12637 Lisp_Object vlist;
12639 for (vlist = Voverlay_arrow_variable_list;
12640 CONSP (vlist);
12641 vlist = XCDR (vlist))
12643 Lisp_Object var = XCAR (vlist);
12644 Lisp_Object val;
12646 if (!SYMBOLP (var))
12647 continue;
12649 val = find_symbol_value (var);
12651 if (MARKERP (val)
12652 && current_buffer == XMARKER (val)->buffer
12653 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12655 if (FRAME_WINDOW_P (it->f)
12656 /* FIXME: if ROW->reversed_p is set, this should test
12657 the right fringe, not the left one. */
12658 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12660 #ifdef HAVE_WINDOW_SYSTEM
12661 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12663 int fringe_bitmap;
12664 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12665 return make_number (fringe_bitmap);
12667 #endif
12668 return make_number (-1); /* Use default arrow bitmap */
12670 return overlay_arrow_string_or_property (var);
12674 return Qnil;
12677 /* Return 1 if point moved out of or into a composition. Otherwise
12678 return 0. PREV_BUF and PREV_PT are the last point buffer and
12679 position. BUF and PT are the current point buffer and position. */
12681 static int
12682 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12683 struct buffer *buf, EMACS_INT pt)
12685 EMACS_INT start, end;
12686 Lisp_Object prop;
12687 Lisp_Object buffer;
12689 XSETBUFFER (buffer, buf);
12690 /* Check a composition at the last point if point moved within the
12691 same buffer. */
12692 if (prev_buf == buf)
12694 if (prev_pt == pt)
12695 /* Point didn't move. */
12696 return 0;
12698 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12699 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12700 && COMPOSITION_VALID_P (start, end, prop)
12701 && start < prev_pt && end > prev_pt)
12702 /* The last point was within the composition. Return 1 iff
12703 point moved out of the composition. */
12704 return (pt <= start || pt >= end);
12707 /* Check a composition at the current point. */
12708 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12709 && find_composition (pt, -1, &start, &end, &prop, buffer)
12710 && COMPOSITION_VALID_P (start, end, prop)
12711 && start < pt && end > pt);
12715 /* Reconsider the setting of B->clip_changed which is displayed
12716 in window W. */
12718 static inline void
12719 reconsider_clip_changes (struct window *w, struct buffer *b)
12721 if (b->clip_changed
12722 && !NILP (w->window_end_valid)
12723 && w->current_matrix->buffer == b
12724 && w->current_matrix->zv == BUF_ZV (b)
12725 && w->current_matrix->begv == BUF_BEGV (b))
12726 b->clip_changed = 0;
12728 /* If display wasn't paused, and W is not a tool bar window, see if
12729 point has been moved into or out of a composition. In that case,
12730 we set b->clip_changed to 1 to force updating the screen. If
12731 b->clip_changed has already been set to 1, we can skip this
12732 check. */
12733 if (!b->clip_changed
12734 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12736 EMACS_INT pt;
12738 if (w == XWINDOW (selected_window))
12739 pt = PT;
12740 else
12741 pt = marker_position (w->pointm);
12743 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12744 || pt != XINT (w->last_point))
12745 && check_point_in_composition (w->current_matrix->buffer,
12746 XINT (w->last_point),
12747 XBUFFER (w->buffer), pt))
12748 b->clip_changed = 1;
12753 /* Select FRAME to forward the values of frame-local variables into C
12754 variables so that the redisplay routines can access those values
12755 directly. */
12757 static void
12758 select_frame_for_redisplay (Lisp_Object frame)
12760 Lisp_Object tail, tem;
12761 Lisp_Object old = selected_frame;
12762 struct Lisp_Symbol *sym;
12764 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12766 selected_frame = frame;
12768 do {
12769 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12770 if (CONSP (XCAR (tail))
12771 && (tem = XCAR (XCAR (tail)),
12772 SYMBOLP (tem))
12773 && (sym = indirect_variable (XSYMBOL (tem)),
12774 sym->redirect == SYMBOL_LOCALIZED)
12775 && sym->val.blv->frame_local)
12776 /* Use find_symbol_value rather than Fsymbol_value
12777 to avoid an error if it is void. */
12778 find_symbol_value (tem);
12779 } while (!EQ (frame, old) && (frame = old, 1));
12783 #define STOP_POLLING \
12784 do { if (! polling_stopped_here) stop_polling (); \
12785 polling_stopped_here = 1; } while (0)
12787 #define RESUME_POLLING \
12788 do { if (polling_stopped_here) start_polling (); \
12789 polling_stopped_here = 0; } while (0)
12792 /* Perhaps in the future avoid recentering windows if it
12793 is not necessary; currently that causes some problems. */
12795 static void
12796 redisplay_internal (void)
12798 struct window *w = XWINDOW (selected_window);
12799 struct window *sw;
12800 struct frame *fr;
12801 int pending;
12802 int must_finish = 0;
12803 struct text_pos tlbufpos, tlendpos;
12804 int number_of_visible_frames;
12805 int count, count1;
12806 struct frame *sf;
12807 int polling_stopped_here = 0;
12808 Lisp_Object old_frame = selected_frame;
12810 /* Non-zero means redisplay has to consider all windows on all
12811 frames. Zero means, only selected_window is considered. */
12812 int consider_all_windows_p;
12814 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12816 /* No redisplay if running in batch mode or frame is not yet fully
12817 initialized, or redisplay is explicitly turned off by setting
12818 Vinhibit_redisplay. */
12819 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12820 || !NILP (Vinhibit_redisplay))
12821 return;
12823 /* Don't examine these until after testing Vinhibit_redisplay.
12824 When Emacs is shutting down, perhaps because its connection to
12825 X has dropped, we should not look at them at all. */
12826 fr = XFRAME (w->frame);
12827 sf = SELECTED_FRAME ();
12829 if (!fr->glyphs_initialized_p)
12830 return;
12832 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12833 if (popup_activated ())
12834 return;
12835 #endif
12837 /* I don't think this happens but let's be paranoid. */
12838 if (redisplaying_p)
12839 return;
12841 /* Record a function that resets redisplaying_p to its old value
12842 when we leave this function. */
12843 count = SPECPDL_INDEX ();
12844 record_unwind_protect (unwind_redisplay,
12845 Fcons (make_number (redisplaying_p), selected_frame));
12846 ++redisplaying_p;
12847 specbind (Qinhibit_free_realized_faces, Qnil);
12850 Lisp_Object tail, frame;
12852 FOR_EACH_FRAME (tail, frame)
12854 struct frame *f = XFRAME (frame);
12855 f->already_hscrolled_p = 0;
12859 retry:
12860 /* Remember the currently selected window. */
12861 sw = w;
12863 if (!EQ (old_frame, selected_frame)
12864 && FRAME_LIVE_P (XFRAME (old_frame)))
12865 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12866 selected_frame and selected_window to be temporarily out-of-sync so
12867 when we come back here via `goto retry', we need to resync because we
12868 may need to run Elisp code (via prepare_menu_bars). */
12869 select_frame_for_redisplay (old_frame);
12871 pending = 0;
12872 reconsider_clip_changes (w, current_buffer);
12873 last_escape_glyph_frame = NULL;
12874 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12875 last_glyphless_glyph_frame = NULL;
12876 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12878 /* If new fonts have been loaded that make a glyph matrix adjustment
12879 necessary, do it. */
12880 if (fonts_changed_p)
12882 adjust_glyphs (NULL);
12883 ++windows_or_buffers_changed;
12884 fonts_changed_p = 0;
12887 /* If face_change_count is non-zero, init_iterator will free all
12888 realized faces, which includes the faces referenced from current
12889 matrices. So, we can't reuse current matrices in this case. */
12890 if (face_change_count)
12891 ++windows_or_buffers_changed;
12893 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12894 && FRAME_TTY (sf)->previous_frame != sf)
12896 /* Since frames on a single ASCII terminal share the same
12897 display area, displaying a different frame means redisplay
12898 the whole thing. */
12899 windows_or_buffers_changed++;
12900 SET_FRAME_GARBAGED (sf);
12901 #ifndef DOS_NT
12902 set_tty_color_mode (FRAME_TTY (sf), sf);
12903 #endif
12904 FRAME_TTY (sf)->previous_frame = sf;
12907 /* Set the visible flags for all frames. Do this before checking
12908 for resized or garbaged frames; they want to know if their frames
12909 are visible. See the comment in frame.h for
12910 FRAME_SAMPLE_VISIBILITY. */
12912 Lisp_Object tail, frame;
12914 number_of_visible_frames = 0;
12916 FOR_EACH_FRAME (tail, frame)
12918 struct frame *f = XFRAME (frame);
12920 FRAME_SAMPLE_VISIBILITY (f);
12921 if (FRAME_VISIBLE_P (f))
12922 ++number_of_visible_frames;
12923 clear_desired_matrices (f);
12927 /* Notice any pending interrupt request to change frame size. */
12928 do_pending_window_change (1);
12930 /* do_pending_window_change could change the selected_window due to
12931 frame resizing which makes the selected window too small. */
12932 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12934 sw = w;
12935 reconsider_clip_changes (w, current_buffer);
12938 /* Clear frames marked as garbaged. */
12939 if (frame_garbaged)
12940 clear_garbaged_frames ();
12942 /* Build menubar and tool-bar items. */
12943 if (NILP (Vmemory_full))
12944 prepare_menu_bars ();
12946 if (windows_or_buffers_changed)
12947 update_mode_lines++;
12949 /* Detect case that we need to write or remove a star in the mode line. */
12950 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12952 w->update_mode_line = Qt;
12953 if (buffer_shared > 1)
12954 update_mode_lines++;
12957 /* Avoid invocation of point motion hooks by `current_column' below. */
12958 count1 = SPECPDL_INDEX ();
12959 specbind (Qinhibit_point_motion_hooks, Qt);
12961 /* If %c is in the mode line, update it if needed. */
12962 if (!NILP (w->column_number_displayed)
12963 /* This alternative quickly identifies a common case
12964 where no change is needed. */
12965 && !(PT == XFASTINT (w->last_point)
12966 && XFASTINT (w->last_modified) >= MODIFF
12967 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12968 && (XFASTINT (w->column_number_displayed) != current_column ()))
12969 w->update_mode_line = Qt;
12971 unbind_to (count1, Qnil);
12973 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12975 /* The variable buffer_shared is set in redisplay_window and
12976 indicates that we redisplay a buffer in different windows. See
12977 there. */
12978 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12979 || cursor_type_changed);
12981 /* If specs for an arrow have changed, do thorough redisplay
12982 to ensure we remove any arrow that should no longer exist. */
12983 if (overlay_arrows_changed_p ())
12984 consider_all_windows_p = windows_or_buffers_changed = 1;
12986 /* Normally the message* functions will have already displayed and
12987 updated the echo area, but the frame may have been trashed, or
12988 the update may have been preempted, so display the echo area
12989 again here. Checking message_cleared_p captures the case that
12990 the echo area should be cleared. */
12991 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12992 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12993 || (message_cleared_p
12994 && minibuf_level == 0
12995 /* If the mini-window is currently selected, this means the
12996 echo-area doesn't show through. */
12997 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12999 int window_height_changed_p = echo_area_display (0);
13000 must_finish = 1;
13002 /* If we don't display the current message, don't clear the
13003 message_cleared_p flag, because, if we did, we wouldn't clear
13004 the echo area in the next redisplay which doesn't preserve
13005 the echo area. */
13006 if (!display_last_displayed_message_p)
13007 message_cleared_p = 0;
13009 if (fonts_changed_p)
13010 goto retry;
13011 else if (window_height_changed_p)
13013 consider_all_windows_p = 1;
13014 ++update_mode_lines;
13015 ++windows_or_buffers_changed;
13017 /* If window configuration was changed, frames may have been
13018 marked garbaged. Clear them or we will experience
13019 surprises wrt scrolling. */
13020 if (frame_garbaged)
13021 clear_garbaged_frames ();
13024 else if (EQ (selected_window, minibuf_window)
13025 && (current_buffer->clip_changed
13026 || XFASTINT (w->last_modified) < MODIFF
13027 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
13028 && resize_mini_window (w, 0))
13030 /* Resized active mini-window to fit the size of what it is
13031 showing if its contents might have changed. */
13032 must_finish = 1;
13033 /* FIXME: this causes all frames to be updated, which seems unnecessary
13034 since only the current frame needs to be considered. This function needs
13035 to be rewritten with two variables, consider_all_windows and
13036 consider_all_frames. */
13037 consider_all_windows_p = 1;
13038 ++windows_or_buffers_changed;
13039 ++update_mode_lines;
13041 /* If window configuration was changed, frames may have been
13042 marked garbaged. Clear them or we will experience
13043 surprises wrt scrolling. */
13044 if (frame_garbaged)
13045 clear_garbaged_frames ();
13049 /* If showing the region, and mark has changed, we must redisplay
13050 the whole window. The assignment to this_line_start_pos prevents
13051 the optimization directly below this if-statement. */
13052 if (((!NILP (Vtransient_mark_mode)
13053 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13054 != !NILP (w->region_showing))
13055 || (!NILP (w->region_showing)
13056 && !EQ (w->region_showing,
13057 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13058 CHARPOS (this_line_start_pos) = 0;
13060 /* Optimize the case that only the line containing the cursor in the
13061 selected window has changed. Variables starting with this_ are
13062 set in display_line and record information about the line
13063 containing the cursor. */
13064 tlbufpos = this_line_start_pos;
13065 tlendpos = this_line_end_pos;
13066 if (!consider_all_windows_p
13067 && CHARPOS (tlbufpos) > 0
13068 && NILP (w->update_mode_line)
13069 && !current_buffer->clip_changed
13070 && !current_buffer->prevent_redisplay_optimizations_p
13071 && FRAME_VISIBLE_P (XFRAME (w->frame))
13072 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13073 /* Make sure recorded data applies to current buffer, etc. */
13074 && this_line_buffer == current_buffer
13075 && current_buffer == XBUFFER (w->buffer)
13076 && NILP (w->force_start)
13077 && NILP (w->optional_new_start)
13078 /* Point must be on the line that we have info recorded about. */
13079 && PT >= CHARPOS (tlbufpos)
13080 && PT <= Z - CHARPOS (tlendpos)
13081 /* All text outside that line, including its final newline,
13082 must be unchanged. */
13083 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13084 CHARPOS (tlendpos)))
13086 if (CHARPOS (tlbufpos) > BEGV
13087 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13088 && (CHARPOS (tlbufpos) == ZV
13089 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13090 /* Former continuation line has disappeared by becoming empty. */
13091 goto cancel;
13092 else if (XFASTINT (w->last_modified) < MODIFF
13093 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13094 || MINI_WINDOW_P (w))
13096 /* We have to handle the case of continuation around a
13097 wide-column character (see the comment in indent.c around
13098 line 1340).
13100 For instance, in the following case:
13102 -------- Insert --------
13103 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13104 J_I_ ==> J_I_ `^^' are cursors.
13105 ^^ ^^
13106 -------- --------
13108 As we have to redraw the line above, we cannot use this
13109 optimization. */
13111 struct it it;
13112 int line_height_before = this_line_pixel_height;
13114 /* Note that start_display will handle the case that the
13115 line starting at tlbufpos is a continuation line. */
13116 start_display (&it, w, tlbufpos);
13118 /* Implementation note: It this still necessary? */
13119 if (it.current_x != this_line_start_x)
13120 goto cancel;
13122 TRACE ((stderr, "trying display optimization 1\n"));
13123 w->cursor.vpos = -1;
13124 overlay_arrow_seen = 0;
13125 it.vpos = this_line_vpos;
13126 it.current_y = this_line_y;
13127 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13128 display_line (&it);
13130 /* If line contains point, is not continued,
13131 and ends at same distance from eob as before, we win. */
13132 if (w->cursor.vpos >= 0
13133 /* Line is not continued, otherwise this_line_start_pos
13134 would have been set to 0 in display_line. */
13135 && CHARPOS (this_line_start_pos)
13136 /* Line ends as before. */
13137 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13138 /* Line has same height as before. Otherwise other lines
13139 would have to be shifted up or down. */
13140 && this_line_pixel_height == line_height_before)
13142 /* If this is not the window's last line, we must adjust
13143 the charstarts of the lines below. */
13144 if (it.current_y < it.last_visible_y)
13146 struct glyph_row *row
13147 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13148 EMACS_INT delta, delta_bytes;
13150 /* We used to distinguish between two cases here,
13151 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13152 when the line ends in a newline or the end of the
13153 buffer's accessible portion. But both cases did
13154 the same, so they were collapsed. */
13155 delta = (Z
13156 - CHARPOS (tlendpos)
13157 - MATRIX_ROW_START_CHARPOS (row));
13158 delta_bytes = (Z_BYTE
13159 - BYTEPOS (tlendpos)
13160 - MATRIX_ROW_START_BYTEPOS (row));
13162 increment_matrix_positions (w->current_matrix,
13163 this_line_vpos + 1,
13164 w->current_matrix->nrows,
13165 delta, delta_bytes);
13168 /* If this row displays text now but previously didn't,
13169 or vice versa, w->window_end_vpos may have to be
13170 adjusted. */
13171 if ((it.glyph_row - 1)->displays_text_p)
13173 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13174 XSETINT (w->window_end_vpos, this_line_vpos);
13176 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13177 && this_line_vpos > 0)
13178 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13179 w->window_end_valid = Qnil;
13181 /* Update hint: No need to try to scroll in update_window. */
13182 w->desired_matrix->no_scrolling_p = 1;
13184 #if GLYPH_DEBUG
13185 *w->desired_matrix->method = 0;
13186 debug_method_add (w, "optimization 1");
13187 #endif
13188 #ifdef HAVE_WINDOW_SYSTEM
13189 update_window_fringes (w, 0);
13190 #endif
13191 goto update;
13193 else
13194 goto cancel;
13196 else if (/* Cursor position hasn't changed. */
13197 PT == XFASTINT (w->last_point)
13198 /* Make sure the cursor was last displayed
13199 in this window. Otherwise we have to reposition it. */
13200 && 0 <= w->cursor.vpos
13201 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13203 if (!must_finish)
13205 do_pending_window_change (1);
13206 /* If selected_window changed, redisplay again. */
13207 if (WINDOWP (selected_window)
13208 && (w = XWINDOW (selected_window)) != sw)
13209 goto retry;
13211 /* We used to always goto end_of_redisplay here, but this
13212 isn't enough if we have a blinking cursor. */
13213 if (w->cursor_off_p == w->last_cursor_off_p)
13214 goto end_of_redisplay;
13216 goto update;
13218 /* If highlighting the region, or if the cursor is in the echo area,
13219 then we can't just move the cursor. */
13220 else if (! (!NILP (Vtransient_mark_mode)
13221 && !NILP (BVAR (current_buffer, mark_active)))
13222 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13223 || highlight_nonselected_windows)
13224 && NILP (w->region_showing)
13225 && NILP (Vshow_trailing_whitespace)
13226 && !cursor_in_echo_area)
13228 struct it it;
13229 struct glyph_row *row;
13231 /* Skip from tlbufpos to PT and see where it is. Note that
13232 PT may be in invisible text. If so, we will end at the
13233 next visible position. */
13234 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13235 NULL, DEFAULT_FACE_ID);
13236 it.current_x = this_line_start_x;
13237 it.current_y = this_line_y;
13238 it.vpos = this_line_vpos;
13240 /* The call to move_it_to stops in front of PT, but
13241 moves over before-strings. */
13242 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13244 if (it.vpos == this_line_vpos
13245 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13246 row->enabled_p))
13248 xassert (this_line_vpos == it.vpos);
13249 xassert (this_line_y == it.current_y);
13250 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13251 #if GLYPH_DEBUG
13252 *w->desired_matrix->method = 0;
13253 debug_method_add (w, "optimization 3");
13254 #endif
13255 goto update;
13257 else
13258 goto cancel;
13261 cancel:
13262 /* Text changed drastically or point moved off of line. */
13263 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13266 CHARPOS (this_line_start_pos) = 0;
13267 consider_all_windows_p |= buffer_shared > 1;
13268 ++clear_face_cache_count;
13269 #ifdef HAVE_WINDOW_SYSTEM
13270 ++clear_image_cache_count;
13271 #endif
13273 /* Build desired matrices, and update the display. If
13274 consider_all_windows_p is non-zero, do it for all windows on all
13275 frames. Otherwise do it for selected_window, only. */
13277 if (consider_all_windows_p)
13279 Lisp_Object tail, frame;
13281 FOR_EACH_FRAME (tail, frame)
13282 XFRAME (frame)->updated_p = 0;
13284 /* Recompute # windows showing selected buffer. This will be
13285 incremented each time such a window is displayed. */
13286 buffer_shared = 0;
13288 FOR_EACH_FRAME (tail, frame)
13290 struct frame *f = XFRAME (frame);
13292 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13294 if (! EQ (frame, selected_frame))
13295 /* Select the frame, for the sake of frame-local
13296 variables. */
13297 select_frame_for_redisplay (frame);
13299 /* Mark all the scroll bars to be removed; we'll redeem
13300 the ones we want when we redisplay their windows. */
13301 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13302 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13304 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13305 redisplay_windows (FRAME_ROOT_WINDOW (f));
13307 /* The X error handler may have deleted that frame. */
13308 if (!FRAME_LIVE_P (f))
13309 continue;
13311 /* Any scroll bars which redisplay_windows should have
13312 nuked should now go away. */
13313 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13314 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13316 /* If fonts changed, display again. */
13317 /* ??? rms: I suspect it is a mistake to jump all the way
13318 back to retry here. It should just retry this frame. */
13319 if (fonts_changed_p)
13320 goto retry;
13322 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13324 /* See if we have to hscroll. */
13325 if (!f->already_hscrolled_p)
13327 f->already_hscrolled_p = 1;
13328 if (hscroll_windows (f->root_window))
13329 goto retry;
13332 /* Prevent various kinds of signals during display
13333 update. stdio is not robust about handling
13334 signals, which can cause an apparent I/O
13335 error. */
13336 if (interrupt_input)
13337 unrequest_sigio ();
13338 STOP_POLLING;
13340 /* Update the display. */
13341 set_window_update_flags (XWINDOW (f->root_window), 1);
13342 pending |= update_frame (f, 0, 0);
13343 f->updated_p = 1;
13348 if (!EQ (old_frame, selected_frame)
13349 && FRAME_LIVE_P (XFRAME (old_frame)))
13350 /* We played a bit fast-and-loose above and allowed selected_frame
13351 and selected_window to be temporarily out-of-sync but let's make
13352 sure this stays contained. */
13353 select_frame_for_redisplay (old_frame);
13354 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13356 if (!pending)
13358 /* Do the mark_window_display_accurate after all windows have
13359 been redisplayed because this call resets flags in buffers
13360 which are needed for proper redisplay. */
13361 FOR_EACH_FRAME (tail, frame)
13363 struct frame *f = XFRAME (frame);
13364 if (f->updated_p)
13366 mark_window_display_accurate (f->root_window, 1);
13367 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13368 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13373 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13375 Lisp_Object mini_window;
13376 struct frame *mini_frame;
13378 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13379 /* Use list_of_error, not Qerror, so that
13380 we catch only errors and don't run the debugger. */
13381 internal_condition_case_1 (redisplay_window_1, selected_window,
13382 list_of_error,
13383 redisplay_window_error);
13385 /* Compare desired and current matrices, perform output. */
13387 update:
13388 /* If fonts changed, display again. */
13389 if (fonts_changed_p)
13390 goto retry;
13392 /* Prevent various kinds of signals during display update.
13393 stdio is not robust about handling signals,
13394 which can cause an apparent I/O error. */
13395 if (interrupt_input)
13396 unrequest_sigio ();
13397 STOP_POLLING;
13399 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13401 if (hscroll_windows (selected_window))
13402 goto retry;
13404 XWINDOW (selected_window)->must_be_updated_p = 1;
13405 pending = update_frame (sf, 0, 0);
13408 /* We may have called echo_area_display at the top of this
13409 function. If the echo area is on another frame, that may
13410 have put text on a frame other than the selected one, so the
13411 above call to update_frame would not have caught it. Catch
13412 it here. */
13413 mini_window = FRAME_MINIBUF_WINDOW (sf);
13414 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13416 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13418 XWINDOW (mini_window)->must_be_updated_p = 1;
13419 pending |= update_frame (mini_frame, 0, 0);
13420 if (!pending && hscroll_windows (mini_window))
13421 goto retry;
13425 /* If display was paused because of pending input, make sure we do a
13426 thorough update the next time. */
13427 if (pending)
13429 /* Prevent the optimization at the beginning of
13430 redisplay_internal that tries a single-line update of the
13431 line containing the cursor in the selected window. */
13432 CHARPOS (this_line_start_pos) = 0;
13434 /* Let the overlay arrow be updated the next time. */
13435 update_overlay_arrows (0);
13437 /* If we pause after scrolling, some rows in the current
13438 matrices of some windows are not valid. */
13439 if (!WINDOW_FULL_WIDTH_P (w)
13440 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13441 update_mode_lines = 1;
13443 else
13445 if (!consider_all_windows_p)
13447 /* This has already been done above if
13448 consider_all_windows_p is set. */
13449 mark_window_display_accurate_1 (w, 1);
13451 /* Say overlay arrows are up to date. */
13452 update_overlay_arrows (1);
13454 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13455 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13458 update_mode_lines = 0;
13459 windows_or_buffers_changed = 0;
13460 cursor_type_changed = 0;
13463 /* Start SIGIO interrupts coming again. Having them off during the
13464 code above makes it less likely one will discard output, but not
13465 impossible, since there might be stuff in the system buffer here.
13466 But it is much hairier to try to do anything about that. */
13467 if (interrupt_input)
13468 request_sigio ();
13469 RESUME_POLLING;
13471 /* If a frame has become visible which was not before, redisplay
13472 again, so that we display it. Expose events for such a frame
13473 (which it gets when becoming visible) don't call the parts of
13474 redisplay constructing glyphs, so simply exposing a frame won't
13475 display anything in this case. So, we have to display these
13476 frames here explicitly. */
13477 if (!pending)
13479 Lisp_Object tail, frame;
13480 int new_count = 0;
13482 FOR_EACH_FRAME (tail, frame)
13484 int this_is_visible = 0;
13486 if (XFRAME (frame)->visible)
13487 this_is_visible = 1;
13488 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13489 if (XFRAME (frame)->visible)
13490 this_is_visible = 1;
13492 if (this_is_visible)
13493 new_count++;
13496 if (new_count != number_of_visible_frames)
13497 windows_or_buffers_changed++;
13500 /* Change frame size now if a change is pending. */
13501 do_pending_window_change (1);
13503 /* If we just did a pending size change, or have additional
13504 visible frames, or selected_window changed, redisplay again. */
13505 if ((windows_or_buffers_changed && !pending)
13506 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13507 goto retry;
13509 /* Clear the face and image caches.
13511 We used to do this only if consider_all_windows_p. But the cache
13512 needs to be cleared if a timer creates images in the current
13513 buffer (e.g. the test case in Bug#6230). */
13515 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13517 clear_face_cache (0);
13518 clear_face_cache_count = 0;
13521 #ifdef HAVE_WINDOW_SYSTEM
13522 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13524 clear_image_caches (Qnil);
13525 clear_image_cache_count = 0;
13527 #endif /* HAVE_WINDOW_SYSTEM */
13529 end_of_redisplay:
13530 unbind_to (count, Qnil);
13531 RESUME_POLLING;
13535 /* Redisplay, but leave alone any recent echo area message unless
13536 another message has been requested in its place.
13538 This is useful in situations where you need to redisplay but no
13539 user action has occurred, making it inappropriate for the message
13540 area to be cleared. See tracking_off and
13541 wait_reading_process_output for examples of these situations.
13543 FROM_WHERE is an integer saying from where this function was
13544 called. This is useful for debugging. */
13546 void
13547 redisplay_preserve_echo_area (int from_where)
13549 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13551 if (!NILP (echo_area_buffer[1]))
13553 /* We have a previously displayed message, but no current
13554 message. Redisplay the previous message. */
13555 display_last_displayed_message_p = 1;
13556 redisplay_internal ();
13557 display_last_displayed_message_p = 0;
13559 else
13560 redisplay_internal ();
13562 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13563 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13564 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13568 /* Function registered with record_unwind_protect in
13569 redisplay_internal. Reset redisplaying_p to the value it had
13570 before redisplay_internal was called, and clear
13571 prevent_freeing_realized_faces_p. It also selects the previously
13572 selected frame, unless it has been deleted (by an X connection
13573 failure during redisplay, for example). */
13575 static Lisp_Object
13576 unwind_redisplay (Lisp_Object val)
13578 Lisp_Object old_redisplaying_p, old_frame;
13580 old_redisplaying_p = XCAR (val);
13581 redisplaying_p = XFASTINT (old_redisplaying_p);
13582 old_frame = XCDR (val);
13583 if (! EQ (old_frame, selected_frame)
13584 && FRAME_LIVE_P (XFRAME (old_frame)))
13585 select_frame_for_redisplay (old_frame);
13586 return Qnil;
13590 /* Mark the display of window W as accurate or inaccurate. If
13591 ACCURATE_P is non-zero mark display of W as accurate. If
13592 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13593 redisplay_internal is called. */
13595 static void
13596 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13598 if (BUFFERP (w->buffer))
13600 struct buffer *b = XBUFFER (w->buffer);
13602 w->last_modified
13603 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13604 w->last_overlay_modified
13605 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13606 w->last_had_star
13607 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13609 if (accurate_p)
13611 b->clip_changed = 0;
13612 b->prevent_redisplay_optimizations_p = 0;
13614 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13615 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13616 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13617 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13619 w->current_matrix->buffer = b;
13620 w->current_matrix->begv = BUF_BEGV (b);
13621 w->current_matrix->zv = BUF_ZV (b);
13623 w->last_cursor = w->cursor;
13624 w->last_cursor_off_p = w->cursor_off_p;
13626 if (w == XWINDOW (selected_window))
13627 w->last_point = make_number (BUF_PT (b));
13628 else
13629 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13633 if (accurate_p)
13635 w->window_end_valid = w->buffer;
13636 w->update_mode_line = Qnil;
13641 /* Mark the display of windows in the window tree rooted at WINDOW as
13642 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13643 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13644 be redisplayed the next time redisplay_internal is called. */
13646 void
13647 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13649 struct window *w;
13651 for (; !NILP (window); window = w->next)
13653 w = XWINDOW (window);
13654 mark_window_display_accurate_1 (w, accurate_p);
13656 if (!NILP (w->vchild))
13657 mark_window_display_accurate (w->vchild, accurate_p);
13658 if (!NILP (w->hchild))
13659 mark_window_display_accurate (w->hchild, accurate_p);
13662 if (accurate_p)
13664 update_overlay_arrows (1);
13666 else
13668 /* Force a thorough redisplay the next time by setting
13669 last_arrow_position and last_arrow_string to t, which is
13670 unequal to any useful value of Voverlay_arrow_... */
13671 update_overlay_arrows (-1);
13676 /* Return value in display table DP (Lisp_Char_Table *) for character
13677 C. Since a display table doesn't have any parent, we don't have to
13678 follow parent. Do not call this function directly but use the
13679 macro DISP_CHAR_VECTOR. */
13681 Lisp_Object
13682 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13684 Lisp_Object val;
13686 if (ASCII_CHAR_P (c))
13688 val = dp->ascii;
13689 if (SUB_CHAR_TABLE_P (val))
13690 val = XSUB_CHAR_TABLE (val)->contents[c];
13692 else
13694 Lisp_Object table;
13696 XSETCHAR_TABLE (table, dp);
13697 val = char_table_ref (table, c);
13699 if (NILP (val))
13700 val = dp->defalt;
13701 return val;
13706 /***********************************************************************
13707 Window Redisplay
13708 ***********************************************************************/
13710 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13712 static void
13713 redisplay_windows (Lisp_Object window)
13715 while (!NILP (window))
13717 struct window *w = XWINDOW (window);
13719 if (!NILP (w->hchild))
13720 redisplay_windows (w->hchild);
13721 else if (!NILP (w->vchild))
13722 redisplay_windows (w->vchild);
13723 else if (!NILP (w->buffer))
13725 displayed_buffer = XBUFFER (w->buffer);
13726 /* Use list_of_error, not Qerror, so that
13727 we catch only errors and don't run the debugger. */
13728 internal_condition_case_1 (redisplay_window_0, window,
13729 list_of_error,
13730 redisplay_window_error);
13733 window = w->next;
13737 static Lisp_Object
13738 redisplay_window_error (Lisp_Object ignore)
13740 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13741 return Qnil;
13744 static Lisp_Object
13745 redisplay_window_0 (Lisp_Object window)
13747 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13748 redisplay_window (window, 0);
13749 return Qnil;
13752 static Lisp_Object
13753 redisplay_window_1 (Lisp_Object window)
13755 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13756 redisplay_window (window, 1);
13757 return Qnil;
13761 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13762 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13763 which positions recorded in ROW differ from current buffer
13764 positions.
13766 Return 0 if cursor is not on this row, 1 otherwise. */
13768 static int
13769 set_cursor_from_row (struct window *w, struct glyph_row *row,
13770 struct glyph_matrix *matrix,
13771 EMACS_INT delta, EMACS_INT delta_bytes,
13772 int dy, int dvpos)
13774 struct glyph *glyph = row->glyphs[TEXT_AREA];
13775 struct glyph *end = glyph + row->used[TEXT_AREA];
13776 struct glyph *cursor = NULL;
13777 /* The last known character position in row. */
13778 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13779 int x = row->x;
13780 EMACS_INT pt_old = PT - delta;
13781 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13782 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13783 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13784 /* A glyph beyond the edge of TEXT_AREA which we should never
13785 touch. */
13786 struct glyph *glyphs_end = end;
13787 /* Non-zero means we've found a match for cursor position, but that
13788 glyph has the avoid_cursor_p flag set. */
13789 int match_with_avoid_cursor = 0;
13790 /* Non-zero means we've seen at least one glyph that came from a
13791 display string. */
13792 int string_seen = 0;
13793 /* Largest and smallest buffer positions seen so far during scan of
13794 glyph row. */
13795 EMACS_INT bpos_max = pos_before;
13796 EMACS_INT bpos_min = pos_after;
13797 /* Last buffer position covered by an overlay string with an integer
13798 `cursor' property. */
13799 EMACS_INT bpos_covered = 0;
13800 /* Non-zero means the display string on which to display the cursor
13801 comes from a text property, not from an overlay. */
13802 int string_from_text_prop = 0;
13804 /* Don't even try doing anything if called for a mode-line or
13805 header-line row, since the rest of the code isn't prepared to
13806 deal with such calamities. */
13807 xassert (!row->mode_line_p);
13808 if (row->mode_line_p)
13809 return 0;
13811 /* Skip over glyphs not having an object at the start and the end of
13812 the row. These are special glyphs like truncation marks on
13813 terminal frames. */
13814 if (row->displays_text_p)
13816 if (!row->reversed_p)
13818 while (glyph < end
13819 && INTEGERP (glyph->object)
13820 && glyph->charpos < 0)
13822 x += glyph->pixel_width;
13823 ++glyph;
13825 while (end > glyph
13826 && INTEGERP ((end - 1)->object)
13827 /* CHARPOS is zero for blanks and stretch glyphs
13828 inserted by extend_face_to_end_of_line. */
13829 && (end - 1)->charpos <= 0)
13830 --end;
13831 glyph_before = glyph - 1;
13832 glyph_after = end;
13834 else
13836 struct glyph *g;
13838 /* If the glyph row is reversed, we need to process it from back
13839 to front, so swap the edge pointers. */
13840 glyphs_end = end = glyph - 1;
13841 glyph += row->used[TEXT_AREA] - 1;
13843 while (glyph > end + 1
13844 && INTEGERP (glyph->object)
13845 && glyph->charpos < 0)
13847 --glyph;
13848 x -= glyph->pixel_width;
13850 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13851 --glyph;
13852 /* By default, in reversed rows we put the cursor on the
13853 rightmost (first in the reading order) glyph. */
13854 for (g = end + 1; g < glyph; g++)
13855 x += g->pixel_width;
13856 while (end < glyph
13857 && INTEGERP ((end + 1)->object)
13858 && (end + 1)->charpos <= 0)
13859 ++end;
13860 glyph_before = glyph + 1;
13861 glyph_after = end;
13864 else if (row->reversed_p)
13866 /* In R2L rows that don't display text, put the cursor on the
13867 rightmost glyph. Case in point: an empty last line that is
13868 part of an R2L paragraph. */
13869 cursor = end - 1;
13870 /* Avoid placing the cursor on the last glyph of the row, where
13871 on terminal frames we hold the vertical border between
13872 adjacent windows. */
13873 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13874 && !WINDOW_RIGHTMOST_P (w)
13875 && cursor == row->glyphs[LAST_AREA] - 1)
13876 cursor--;
13877 x = -1; /* will be computed below, at label compute_x */
13880 /* Step 1: Try to find the glyph whose character position
13881 corresponds to point. If that's not possible, find 2 glyphs
13882 whose character positions are the closest to point, one before
13883 point, the other after it. */
13884 if (!row->reversed_p)
13885 while (/* not marched to end of glyph row */
13886 glyph < end
13887 /* glyph was not inserted by redisplay for internal purposes */
13888 && !INTEGERP (glyph->object))
13890 if (BUFFERP (glyph->object))
13892 EMACS_INT dpos = glyph->charpos - pt_old;
13894 if (glyph->charpos > bpos_max)
13895 bpos_max = glyph->charpos;
13896 if (glyph->charpos < bpos_min)
13897 bpos_min = glyph->charpos;
13898 if (!glyph->avoid_cursor_p)
13900 /* If we hit point, we've found the glyph on which to
13901 display the cursor. */
13902 if (dpos == 0)
13904 match_with_avoid_cursor = 0;
13905 break;
13907 /* See if we've found a better approximation to
13908 POS_BEFORE or to POS_AFTER. Note that we want the
13909 first (leftmost) glyph of all those that are the
13910 closest from below, and the last (rightmost) of all
13911 those from above. */
13912 if (0 > dpos && dpos > pos_before - pt_old)
13914 pos_before = glyph->charpos;
13915 glyph_before = glyph;
13917 else if (0 < dpos && dpos <= pos_after - pt_old)
13919 pos_after = glyph->charpos;
13920 glyph_after = glyph;
13923 else if (dpos == 0)
13924 match_with_avoid_cursor = 1;
13926 else if (STRINGP (glyph->object))
13928 Lisp_Object chprop;
13929 EMACS_INT glyph_pos = glyph->charpos;
13931 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13932 glyph->object);
13933 if (!NILP (chprop))
13935 /* If the string came from a `display' text property,
13936 look up the buffer position of that property and
13937 use that position to update bpos_max, as if we
13938 actually saw such a position in one of the row's
13939 glyphs. This helps with supporting integer values
13940 of `cursor' property on the display string in
13941 situations where most or all of the row's buffer
13942 text is completely covered by display properties,
13943 so that no glyph with valid buffer positions is
13944 ever seen in the row. */
13945 EMACS_INT prop_pos =
13946 string_buffer_position_lim (glyph->object, pos_before,
13947 pos_after, 0);
13949 if (prop_pos >= pos_before)
13950 bpos_max = prop_pos - 1;
13952 if (INTEGERP (chprop))
13954 bpos_covered = bpos_max + XINT (chprop);
13955 /* If the `cursor' property covers buffer positions up
13956 to and including point, we should display cursor on
13957 this glyph. Note that, if a `cursor' property on one
13958 of the string's characters has an integer value, we
13959 will break out of the loop below _before_ we get to
13960 the position match above. IOW, integer values of
13961 the `cursor' property override the "exact match for
13962 point" strategy of positioning the cursor. */
13963 /* Implementation note: bpos_max == pt_old when, e.g.,
13964 we are in an empty line, where bpos_max is set to
13965 MATRIX_ROW_START_CHARPOS, see above. */
13966 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13968 cursor = glyph;
13969 break;
13973 string_seen = 1;
13975 x += glyph->pixel_width;
13976 ++glyph;
13978 else if (glyph > end) /* row is reversed */
13979 while (!INTEGERP (glyph->object))
13981 if (BUFFERP (glyph->object))
13983 EMACS_INT dpos = glyph->charpos - pt_old;
13985 if (glyph->charpos > bpos_max)
13986 bpos_max = glyph->charpos;
13987 if (glyph->charpos < bpos_min)
13988 bpos_min = glyph->charpos;
13989 if (!glyph->avoid_cursor_p)
13991 if (dpos == 0)
13993 match_with_avoid_cursor = 0;
13994 break;
13996 if (0 > dpos && dpos > pos_before - pt_old)
13998 pos_before = glyph->charpos;
13999 glyph_before = glyph;
14001 else if (0 < dpos && dpos <= pos_after - pt_old)
14003 pos_after = glyph->charpos;
14004 glyph_after = glyph;
14007 else if (dpos == 0)
14008 match_with_avoid_cursor = 1;
14010 else if (STRINGP (glyph->object))
14012 Lisp_Object chprop;
14013 EMACS_INT glyph_pos = glyph->charpos;
14015 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14016 glyph->object);
14017 if (!NILP (chprop))
14019 EMACS_INT prop_pos =
14020 string_buffer_position_lim (glyph->object, pos_before,
14021 pos_after, 0);
14023 if (prop_pos >= pos_before)
14024 bpos_max = prop_pos - 1;
14026 if (INTEGERP (chprop))
14028 bpos_covered = bpos_max + XINT (chprop);
14029 /* If the `cursor' property covers buffer positions up
14030 to and including point, we should display cursor on
14031 this glyph. */
14032 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14034 cursor = glyph;
14035 break;
14038 string_seen = 1;
14040 --glyph;
14041 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14043 x--; /* can't use any pixel_width */
14044 break;
14046 x -= glyph->pixel_width;
14049 /* Step 2: If we didn't find an exact match for point, we need to
14050 look for a proper place to put the cursor among glyphs between
14051 GLYPH_BEFORE and GLYPH_AFTER. */
14052 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14053 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14054 && bpos_covered < pt_old)
14056 /* An empty line has a single glyph whose OBJECT is zero and
14057 whose CHARPOS is the position of a newline on that line.
14058 Note that on a TTY, there are more glyphs after that, which
14059 were produced by extend_face_to_end_of_line, but their
14060 CHARPOS is zero or negative. */
14061 int empty_line_p =
14062 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14063 && INTEGERP (glyph->object) && glyph->charpos > 0;
14065 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14067 EMACS_INT ellipsis_pos;
14069 /* Scan back over the ellipsis glyphs. */
14070 if (!row->reversed_p)
14072 ellipsis_pos = (glyph - 1)->charpos;
14073 while (glyph > row->glyphs[TEXT_AREA]
14074 && (glyph - 1)->charpos == ellipsis_pos)
14075 glyph--, x -= glyph->pixel_width;
14076 /* That loop always goes one position too far, including
14077 the glyph before the ellipsis. So scan forward over
14078 that one. */
14079 x += glyph->pixel_width;
14080 glyph++;
14082 else /* row is reversed */
14084 ellipsis_pos = (glyph + 1)->charpos;
14085 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14086 && (glyph + 1)->charpos == ellipsis_pos)
14087 glyph++, x += glyph->pixel_width;
14088 x -= glyph->pixel_width;
14089 glyph--;
14092 else if (match_with_avoid_cursor)
14094 cursor = glyph_after;
14095 x = -1;
14097 else if (string_seen)
14099 int incr = row->reversed_p ? -1 : +1;
14101 /* Need to find the glyph that came out of a string which is
14102 present at point. That glyph is somewhere between
14103 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14104 positioned between POS_BEFORE and POS_AFTER in the
14105 buffer. */
14106 struct glyph *start, *stop;
14107 EMACS_INT pos = pos_before;
14109 x = -1;
14111 /* If the row ends in a newline from a display string,
14112 reordering could have moved the glyphs belonging to the
14113 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14114 in this case we extend the search to the last glyph in
14115 the row that was not inserted by redisplay. */
14116 if (row->ends_in_newline_from_string_p)
14118 glyph_after = end;
14119 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14122 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14123 correspond to POS_BEFORE and POS_AFTER, respectively. We
14124 need START and STOP in the order that corresponds to the
14125 row's direction as given by its reversed_p flag. If the
14126 directionality of characters between POS_BEFORE and
14127 POS_AFTER is the opposite of the row's base direction,
14128 these characters will have been reordered for display,
14129 and we need to reverse START and STOP. */
14130 if (!row->reversed_p)
14132 start = min (glyph_before, glyph_after);
14133 stop = max (glyph_before, glyph_after);
14135 else
14137 start = max (glyph_before, glyph_after);
14138 stop = min (glyph_before, glyph_after);
14140 for (glyph = start + incr;
14141 row->reversed_p ? glyph > stop : glyph < stop; )
14144 /* Any glyphs that come from the buffer are here because
14145 of bidi reordering. Skip them, and only pay
14146 attention to glyphs that came from some string. */
14147 if (STRINGP (glyph->object))
14149 Lisp_Object str;
14150 EMACS_INT tem;
14151 /* If the display property covers the newline, we
14152 need to search for it one position farther. */
14153 EMACS_INT lim = pos_after
14154 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14156 string_from_text_prop = 0;
14157 str = glyph->object;
14158 tem = string_buffer_position_lim (str, pos, lim, 0);
14159 if (tem == 0 /* from overlay */
14160 || pos <= tem)
14162 /* If the string from which this glyph came is
14163 found in the buffer at point, then we've
14164 found the glyph we've been looking for. If
14165 it comes from an overlay (tem == 0), and it
14166 has the `cursor' property on one of its
14167 glyphs, record that glyph as a candidate for
14168 displaying the cursor. (As in the
14169 unidirectional version, we will display the
14170 cursor on the last candidate we find.) */
14171 if (tem == 0 || tem == pt_old)
14173 /* The glyphs from this string could have
14174 been reordered. Find the one with the
14175 smallest string position. Or there could
14176 be a character in the string with the
14177 `cursor' property, which means display
14178 cursor on that character's glyph. */
14179 EMACS_INT strpos = glyph->charpos;
14181 if (tem)
14183 cursor = glyph;
14184 string_from_text_prop = 1;
14186 for ( ;
14187 (row->reversed_p ? glyph > stop : glyph < stop)
14188 && EQ (glyph->object, str);
14189 glyph += incr)
14191 Lisp_Object cprop;
14192 EMACS_INT gpos = glyph->charpos;
14194 cprop = Fget_char_property (make_number (gpos),
14195 Qcursor,
14196 glyph->object);
14197 if (!NILP (cprop))
14199 cursor = glyph;
14200 break;
14202 if (tem && glyph->charpos < strpos)
14204 strpos = glyph->charpos;
14205 cursor = glyph;
14209 if (tem == pt_old)
14210 goto compute_x;
14212 if (tem)
14213 pos = tem + 1; /* don't find previous instances */
14215 /* This string is not what we want; skip all of the
14216 glyphs that came from it. */
14217 while ((row->reversed_p ? glyph > stop : glyph < stop)
14218 && EQ (glyph->object, str))
14219 glyph += incr;
14221 else
14222 glyph += incr;
14225 /* If we reached the end of the line, and END was from a string,
14226 the cursor is not on this line. */
14227 if (cursor == NULL
14228 && (row->reversed_p ? glyph <= end : glyph >= end)
14229 && STRINGP (end->object)
14230 && row->continued_p)
14231 return 0;
14233 /* A truncated row may not include PT among its character positions.
14234 Setting the cursor inside the scroll margin will trigger
14235 recalculation of hscroll in hscroll_window_tree. But if a
14236 display string covers point, defer to the string-handling
14237 code below to figure this out. */
14238 else if (row->truncated_on_left_p && pt_old < bpos_min)
14240 cursor = glyph_before;
14241 x = -1;
14243 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14244 /* Zero-width characters produce no glyphs. */
14245 || (!empty_line_p
14246 && (row->reversed_p
14247 ? glyph_after > glyphs_end
14248 : glyph_after < glyphs_end)))
14250 cursor = glyph_after;
14251 x = -1;
14255 compute_x:
14256 if (cursor != NULL)
14257 glyph = cursor;
14258 if (x < 0)
14260 struct glyph *g;
14262 /* Need to compute x that corresponds to GLYPH. */
14263 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14265 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14266 abort ();
14267 x += g->pixel_width;
14271 /* ROW could be part of a continued line, which, under bidi
14272 reordering, might have other rows whose start and end charpos
14273 occlude point. Only set w->cursor if we found a better
14274 approximation to the cursor position than we have from previously
14275 examined candidate rows belonging to the same continued line. */
14276 if (/* we already have a candidate row */
14277 w->cursor.vpos >= 0
14278 /* that candidate is not the row we are processing */
14279 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14280 /* Make sure cursor.vpos specifies a row whose start and end
14281 charpos occlude point, and it is valid candidate for being a
14282 cursor-row. This is because some callers of this function
14283 leave cursor.vpos at the row where the cursor was displayed
14284 during the last redisplay cycle. */
14285 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14286 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14287 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14289 struct glyph *g1 =
14290 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14292 /* Don't consider glyphs that are outside TEXT_AREA. */
14293 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14294 return 0;
14295 /* Keep the candidate whose buffer position is the closest to
14296 point or has the `cursor' property. */
14297 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14298 w->cursor.hpos >= 0
14299 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14300 && ((BUFFERP (g1->object)
14301 && (g1->charpos == pt_old /* an exact match always wins */
14302 || (BUFFERP (glyph->object)
14303 && eabs (g1->charpos - pt_old)
14304 < eabs (glyph->charpos - pt_old))))
14305 /* previous candidate is a glyph from a string that has
14306 a non-nil `cursor' property */
14307 || (STRINGP (g1->object)
14308 && (!NILP (Fget_char_property (make_number (g1->charpos),
14309 Qcursor, g1->object))
14310 /* previous candidate is from the same display
14311 string as this one, and the display string
14312 came from a text property */
14313 || (EQ (g1->object, glyph->object)
14314 && string_from_text_prop)
14315 /* this candidate is from newline and its
14316 position is not an exact match */
14317 || (INTEGERP (glyph->object)
14318 && glyph->charpos != pt_old)))))
14319 return 0;
14320 /* If this candidate gives an exact match, use that. */
14321 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14322 /* If this candidate is a glyph created for the
14323 terminating newline of a line, and point is on that
14324 newline, it wins because it's an exact match. */
14325 || (!row->continued_p
14326 && INTEGERP (glyph->object)
14327 && glyph->charpos == 0
14328 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14329 /* Otherwise, keep the candidate that comes from a row
14330 spanning less buffer positions. This may win when one or
14331 both candidate positions are on glyphs that came from
14332 display strings, for which we cannot compare buffer
14333 positions. */
14334 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14335 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14336 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14337 return 0;
14339 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14340 w->cursor.x = x;
14341 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14342 w->cursor.y = row->y + dy;
14344 if (w == XWINDOW (selected_window))
14346 if (!row->continued_p
14347 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14348 && row->x == 0)
14350 this_line_buffer = XBUFFER (w->buffer);
14352 CHARPOS (this_line_start_pos)
14353 = MATRIX_ROW_START_CHARPOS (row) + delta;
14354 BYTEPOS (this_line_start_pos)
14355 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14357 CHARPOS (this_line_end_pos)
14358 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14359 BYTEPOS (this_line_end_pos)
14360 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14362 this_line_y = w->cursor.y;
14363 this_line_pixel_height = row->height;
14364 this_line_vpos = w->cursor.vpos;
14365 this_line_start_x = row->x;
14367 else
14368 CHARPOS (this_line_start_pos) = 0;
14371 return 1;
14375 /* Run window scroll functions, if any, for WINDOW with new window
14376 start STARTP. Sets the window start of WINDOW to that position.
14378 We assume that the window's buffer is really current. */
14380 static inline struct text_pos
14381 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14383 struct window *w = XWINDOW (window);
14384 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14386 if (current_buffer != XBUFFER (w->buffer))
14387 abort ();
14389 if (!NILP (Vwindow_scroll_functions))
14391 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14392 make_number (CHARPOS (startp)));
14393 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14394 /* In case the hook functions switch buffers. */
14395 if (current_buffer != XBUFFER (w->buffer))
14396 set_buffer_internal_1 (XBUFFER (w->buffer));
14399 return startp;
14403 /* Make sure the line containing the cursor is fully visible.
14404 A value of 1 means there is nothing to be done.
14405 (Either the line is fully visible, or it cannot be made so,
14406 or we cannot tell.)
14408 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14409 is higher than window.
14411 A value of 0 means the caller should do scrolling
14412 as if point had gone off the screen. */
14414 static int
14415 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14417 struct glyph_matrix *matrix;
14418 struct glyph_row *row;
14419 int window_height;
14421 if (!make_cursor_line_fully_visible_p)
14422 return 1;
14424 /* It's not always possible to find the cursor, e.g, when a window
14425 is full of overlay strings. Don't do anything in that case. */
14426 if (w->cursor.vpos < 0)
14427 return 1;
14429 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14430 row = MATRIX_ROW (matrix, w->cursor.vpos);
14432 /* If the cursor row is not partially visible, there's nothing to do. */
14433 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14434 return 1;
14436 /* If the row the cursor is in is taller than the window's height,
14437 it's not clear what to do, so do nothing. */
14438 window_height = window_box_height (w);
14439 if (row->height >= window_height)
14441 if (!force_p || MINI_WINDOW_P (w)
14442 || w->vscroll || w->cursor.vpos == 0)
14443 return 1;
14445 return 0;
14449 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14450 non-zero means only WINDOW is redisplayed in redisplay_internal.
14451 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14452 in redisplay_window to bring a partially visible line into view in
14453 the case that only the cursor has moved.
14455 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14456 last screen line's vertical height extends past the end of the screen.
14458 Value is
14460 1 if scrolling succeeded
14462 0 if scrolling didn't find point.
14464 -1 if new fonts have been loaded so that we must interrupt
14465 redisplay, adjust glyph matrices, and try again. */
14467 enum
14469 SCROLLING_SUCCESS,
14470 SCROLLING_FAILED,
14471 SCROLLING_NEED_LARGER_MATRICES
14474 /* If scroll-conservatively is more than this, never recenter.
14476 If you change this, don't forget to update the doc string of
14477 `scroll-conservatively' and the Emacs manual. */
14478 #define SCROLL_LIMIT 100
14480 static int
14481 try_scrolling (Lisp_Object window, int just_this_one_p,
14482 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14483 int temp_scroll_step, int last_line_misfit)
14485 struct window *w = XWINDOW (window);
14486 struct frame *f = XFRAME (w->frame);
14487 struct text_pos pos, startp;
14488 struct it it;
14489 int this_scroll_margin, scroll_max, rc, height;
14490 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14491 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14492 Lisp_Object aggressive;
14493 /* We will never try scrolling more than this number of lines. */
14494 int scroll_limit = SCROLL_LIMIT;
14496 #if GLYPH_DEBUG
14497 debug_method_add (w, "try_scrolling");
14498 #endif
14500 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14502 /* Compute scroll margin height in pixels. We scroll when point is
14503 within this distance from the top or bottom of the window. */
14504 if (scroll_margin > 0)
14505 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14506 * FRAME_LINE_HEIGHT (f);
14507 else
14508 this_scroll_margin = 0;
14510 /* Force arg_scroll_conservatively to have a reasonable value, to
14511 avoid scrolling too far away with slow move_it_* functions. Note
14512 that the user can supply scroll-conservatively equal to
14513 `most-positive-fixnum', which can be larger than INT_MAX. */
14514 if (arg_scroll_conservatively > scroll_limit)
14516 arg_scroll_conservatively = scroll_limit + 1;
14517 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14519 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14520 /* Compute how much we should try to scroll maximally to bring
14521 point into view. */
14522 scroll_max = (max (scroll_step,
14523 max (arg_scroll_conservatively, temp_scroll_step))
14524 * FRAME_LINE_HEIGHT (f));
14525 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14526 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14527 /* We're trying to scroll because of aggressive scrolling but no
14528 scroll_step is set. Choose an arbitrary one. */
14529 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14530 else
14531 scroll_max = 0;
14533 too_near_end:
14535 /* Decide whether to scroll down. */
14536 if (PT > CHARPOS (startp))
14538 int scroll_margin_y;
14540 /* Compute the pixel ypos of the scroll margin, then move IT to
14541 either that ypos or PT, whichever comes first. */
14542 start_display (&it, w, startp);
14543 scroll_margin_y = it.last_visible_y - this_scroll_margin
14544 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14545 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14546 (MOVE_TO_POS | MOVE_TO_Y));
14548 if (PT > CHARPOS (it.current.pos))
14550 int y0 = line_bottom_y (&it);
14551 /* Compute how many pixels below window bottom to stop searching
14552 for PT. This avoids costly search for PT that is far away if
14553 the user limited scrolling by a small number of lines, but
14554 always finds PT if scroll_conservatively is set to a large
14555 number, such as most-positive-fixnum. */
14556 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14557 int y_to_move = it.last_visible_y + slack;
14559 /* Compute the distance from the scroll margin to PT or to
14560 the scroll limit, whichever comes first. This should
14561 include the height of the cursor line, to make that line
14562 fully visible. */
14563 move_it_to (&it, PT, -1, y_to_move,
14564 -1, MOVE_TO_POS | MOVE_TO_Y);
14565 dy = line_bottom_y (&it) - y0;
14567 if (dy > scroll_max)
14568 return SCROLLING_FAILED;
14570 if (dy > 0)
14571 scroll_down_p = 1;
14575 if (scroll_down_p)
14577 /* Point is in or below the bottom scroll margin, so move the
14578 window start down. If scrolling conservatively, move it just
14579 enough down to make point visible. If scroll_step is set,
14580 move it down by scroll_step. */
14581 if (arg_scroll_conservatively)
14582 amount_to_scroll
14583 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14584 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14585 else if (scroll_step || temp_scroll_step)
14586 amount_to_scroll = scroll_max;
14587 else
14589 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14590 height = WINDOW_BOX_TEXT_HEIGHT (w);
14591 if (NUMBERP (aggressive))
14593 double float_amount = XFLOATINT (aggressive) * height;
14594 amount_to_scroll = float_amount;
14595 if (amount_to_scroll == 0 && float_amount > 0)
14596 amount_to_scroll = 1;
14597 /* Don't let point enter the scroll margin near top of
14598 the window. */
14599 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14600 amount_to_scroll = height - 2*this_scroll_margin + dy;
14604 if (amount_to_scroll <= 0)
14605 return SCROLLING_FAILED;
14607 start_display (&it, w, startp);
14608 if (arg_scroll_conservatively <= scroll_limit)
14609 move_it_vertically (&it, amount_to_scroll);
14610 else
14612 /* Extra precision for users who set scroll-conservatively
14613 to a large number: make sure the amount we scroll
14614 the window start is never less than amount_to_scroll,
14615 which was computed as distance from window bottom to
14616 point. This matters when lines at window top and lines
14617 below window bottom have different height. */
14618 struct it it1;
14619 void *it1data = NULL;
14620 /* We use a temporary it1 because line_bottom_y can modify
14621 its argument, if it moves one line down; see there. */
14622 int start_y;
14624 SAVE_IT (it1, it, it1data);
14625 start_y = line_bottom_y (&it1);
14626 do {
14627 RESTORE_IT (&it, &it, it1data);
14628 move_it_by_lines (&it, 1);
14629 SAVE_IT (it1, it, it1data);
14630 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14633 /* If STARTP is unchanged, move it down another screen line. */
14634 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14635 move_it_by_lines (&it, 1);
14636 startp = it.current.pos;
14638 else
14640 struct text_pos scroll_margin_pos = startp;
14642 /* See if point is inside the scroll margin at the top of the
14643 window. */
14644 if (this_scroll_margin)
14646 start_display (&it, w, startp);
14647 move_it_vertically (&it, this_scroll_margin);
14648 scroll_margin_pos = it.current.pos;
14651 if (PT < CHARPOS (scroll_margin_pos))
14653 /* Point is in the scroll margin at the top of the window or
14654 above what is displayed in the window. */
14655 int y0, y_to_move;
14657 /* Compute the vertical distance from PT to the scroll
14658 margin position. Move as far as scroll_max allows, or
14659 one screenful, or 10 screen lines, whichever is largest.
14660 Give up if distance is greater than scroll_max. */
14661 SET_TEXT_POS (pos, PT, PT_BYTE);
14662 start_display (&it, w, pos);
14663 y0 = it.current_y;
14664 y_to_move = max (it.last_visible_y,
14665 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14666 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14667 y_to_move, -1,
14668 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14669 dy = it.current_y - y0;
14670 if (dy > scroll_max)
14671 return SCROLLING_FAILED;
14673 /* Compute new window start. */
14674 start_display (&it, w, startp);
14676 if (arg_scroll_conservatively)
14677 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14678 max (scroll_step, temp_scroll_step));
14679 else if (scroll_step || temp_scroll_step)
14680 amount_to_scroll = scroll_max;
14681 else
14683 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14684 height = WINDOW_BOX_TEXT_HEIGHT (w);
14685 if (NUMBERP (aggressive))
14687 double float_amount = XFLOATINT (aggressive) * height;
14688 amount_to_scroll = float_amount;
14689 if (amount_to_scroll == 0 && float_amount > 0)
14690 amount_to_scroll = 1;
14691 amount_to_scroll -=
14692 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14693 /* Don't let point enter the scroll margin near
14694 bottom of the window. */
14695 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14696 amount_to_scroll = height - 2*this_scroll_margin + dy;
14700 if (amount_to_scroll <= 0)
14701 return SCROLLING_FAILED;
14703 move_it_vertically_backward (&it, amount_to_scroll);
14704 startp = it.current.pos;
14708 /* Run window scroll functions. */
14709 startp = run_window_scroll_functions (window, startp);
14711 /* Display the window. Give up if new fonts are loaded, or if point
14712 doesn't appear. */
14713 if (!try_window (window, startp, 0))
14714 rc = SCROLLING_NEED_LARGER_MATRICES;
14715 else if (w->cursor.vpos < 0)
14717 clear_glyph_matrix (w->desired_matrix);
14718 rc = SCROLLING_FAILED;
14720 else
14722 /* Maybe forget recorded base line for line number display. */
14723 if (!just_this_one_p
14724 || current_buffer->clip_changed
14725 || BEG_UNCHANGED < CHARPOS (startp))
14726 w->base_line_number = Qnil;
14728 /* If cursor ends up on a partially visible line,
14729 treat that as being off the bottom of the screen. */
14730 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14731 /* It's possible that the cursor is on the first line of the
14732 buffer, which is partially obscured due to a vscroll
14733 (Bug#7537). In that case, avoid looping forever . */
14734 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14736 clear_glyph_matrix (w->desired_matrix);
14737 ++extra_scroll_margin_lines;
14738 goto too_near_end;
14740 rc = SCROLLING_SUCCESS;
14743 return rc;
14747 /* Compute a suitable window start for window W if display of W starts
14748 on a continuation line. Value is non-zero if a new window start
14749 was computed.
14751 The new window start will be computed, based on W's width, starting
14752 from the start of the continued line. It is the start of the
14753 screen line with the minimum distance from the old start W->start. */
14755 static int
14756 compute_window_start_on_continuation_line (struct window *w)
14758 struct text_pos pos, start_pos;
14759 int window_start_changed_p = 0;
14761 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14763 /* If window start is on a continuation line... Window start may be
14764 < BEGV in case there's invisible text at the start of the
14765 buffer (M-x rmail, for example). */
14766 if (CHARPOS (start_pos) > BEGV
14767 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14769 struct it it;
14770 struct glyph_row *row;
14772 /* Handle the case that the window start is out of range. */
14773 if (CHARPOS (start_pos) < BEGV)
14774 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14775 else if (CHARPOS (start_pos) > ZV)
14776 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14778 /* Find the start of the continued line. This should be fast
14779 because scan_buffer is fast (newline cache). */
14780 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14781 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14782 row, DEFAULT_FACE_ID);
14783 reseat_at_previous_visible_line_start (&it);
14785 /* If the line start is "too far" away from the window start,
14786 say it takes too much time to compute a new window start. */
14787 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14788 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14790 int min_distance, distance;
14792 /* Move forward by display lines to find the new window
14793 start. If window width was enlarged, the new start can
14794 be expected to be > the old start. If window width was
14795 decreased, the new window start will be < the old start.
14796 So, we're looking for the display line start with the
14797 minimum distance from the old window start. */
14798 pos = it.current.pos;
14799 min_distance = INFINITY;
14800 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14801 distance < min_distance)
14803 min_distance = distance;
14804 pos = it.current.pos;
14805 move_it_by_lines (&it, 1);
14808 /* Set the window start there. */
14809 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14810 window_start_changed_p = 1;
14814 return window_start_changed_p;
14818 /* Try cursor movement in case text has not changed in window WINDOW,
14819 with window start STARTP. Value is
14821 CURSOR_MOVEMENT_SUCCESS if successful
14823 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14825 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14826 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14827 we want to scroll as if scroll-step were set to 1. See the code.
14829 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14830 which case we have to abort this redisplay, and adjust matrices
14831 first. */
14833 enum
14835 CURSOR_MOVEMENT_SUCCESS,
14836 CURSOR_MOVEMENT_CANNOT_BE_USED,
14837 CURSOR_MOVEMENT_MUST_SCROLL,
14838 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14841 static int
14842 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14844 struct window *w = XWINDOW (window);
14845 struct frame *f = XFRAME (w->frame);
14846 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14848 #if GLYPH_DEBUG
14849 if (inhibit_try_cursor_movement)
14850 return rc;
14851 #endif
14853 /* Handle case where text has not changed, only point, and it has
14854 not moved off the frame. */
14855 if (/* Point may be in this window. */
14856 PT >= CHARPOS (startp)
14857 /* Selective display hasn't changed. */
14858 && !current_buffer->clip_changed
14859 /* Function force-mode-line-update is used to force a thorough
14860 redisplay. It sets either windows_or_buffers_changed or
14861 update_mode_lines. So don't take a shortcut here for these
14862 cases. */
14863 && !update_mode_lines
14864 && !windows_or_buffers_changed
14865 && !cursor_type_changed
14866 /* Can't use this case if highlighting a region. When a
14867 region exists, cursor movement has to do more than just
14868 set the cursor. */
14869 && !(!NILP (Vtransient_mark_mode)
14870 && !NILP (BVAR (current_buffer, mark_active)))
14871 && NILP (w->region_showing)
14872 && NILP (Vshow_trailing_whitespace)
14873 /* Right after splitting windows, last_point may be nil. */
14874 && INTEGERP (w->last_point)
14875 /* This code is not used for mini-buffer for the sake of the case
14876 of redisplaying to replace an echo area message; since in
14877 that case the mini-buffer contents per se are usually
14878 unchanged. This code is of no real use in the mini-buffer
14879 since the handling of this_line_start_pos, etc., in redisplay
14880 handles the same cases. */
14881 && !EQ (window, minibuf_window)
14882 /* When splitting windows or for new windows, it happens that
14883 redisplay is called with a nil window_end_vpos or one being
14884 larger than the window. This should really be fixed in
14885 window.c. I don't have this on my list, now, so we do
14886 approximately the same as the old redisplay code. --gerd. */
14887 && INTEGERP (w->window_end_vpos)
14888 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14889 && (FRAME_WINDOW_P (f)
14890 || !overlay_arrow_in_current_buffer_p ()))
14892 int this_scroll_margin, top_scroll_margin;
14893 struct glyph_row *row = NULL;
14895 #if GLYPH_DEBUG
14896 debug_method_add (w, "cursor movement");
14897 #endif
14899 /* Scroll if point within this distance from the top or bottom
14900 of the window. This is a pixel value. */
14901 if (scroll_margin > 0)
14903 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14904 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14906 else
14907 this_scroll_margin = 0;
14909 top_scroll_margin = this_scroll_margin;
14910 if (WINDOW_WANTS_HEADER_LINE_P (w))
14911 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14913 /* Start with the row the cursor was displayed during the last
14914 not paused redisplay. Give up if that row is not valid. */
14915 if (w->last_cursor.vpos < 0
14916 || w->last_cursor.vpos >= w->current_matrix->nrows)
14917 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14918 else
14920 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14921 if (row->mode_line_p)
14922 ++row;
14923 if (!row->enabled_p)
14924 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14927 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14929 int scroll_p = 0, must_scroll = 0;
14930 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14932 if (PT > XFASTINT (w->last_point))
14934 /* Point has moved forward. */
14935 while (MATRIX_ROW_END_CHARPOS (row) < PT
14936 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14938 xassert (row->enabled_p);
14939 ++row;
14942 /* If the end position of a row equals the start
14943 position of the next row, and PT is at that position,
14944 we would rather display cursor in the next line. */
14945 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14946 && MATRIX_ROW_END_CHARPOS (row) == PT
14947 && row < w->current_matrix->rows
14948 + w->current_matrix->nrows - 1
14949 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14950 && !cursor_row_p (row))
14951 ++row;
14953 /* If within the scroll margin, scroll. Note that
14954 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14955 the next line would be drawn, and that
14956 this_scroll_margin can be zero. */
14957 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14958 || PT > MATRIX_ROW_END_CHARPOS (row)
14959 /* Line is completely visible last line in window
14960 and PT is to be set in the next line. */
14961 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14962 && PT == MATRIX_ROW_END_CHARPOS (row)
14963 && !row->ends_at_zv_p
14964 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14965 scroll_p = 1;
14967 else if (PT < XFASTINT (w->last_point))
14969 /* Cursor has to be moved backward. Note that PT >=
14970 CHARPOS (startp) because of the outer if-statement. */
14971 while (!row->mode_line_p
14972 && (MATRIX_ROW_START_CHARPOS (row) > PT
14973 || (MATRIX_ROW_START_CHARPOS (row) == PT
14974 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14975 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14976 row > w->current_matrix->rows
14977 && (row-1)->ends_in_newline_from_string_p))))
14978 && (row->y > top_scroll_margin
14979 || CHARPOS (startp) == BEGV))
14981 xassert (row->enabled_p);
14982 --row;
14985 /* Consider the following case: Window starts at BEGV,
14986 there is invisible, intangible text at BEGV, so that
14987 display starts at some point START > BEGV. It can
14988 happen that we are called with PT somewhere between
14989 BEGV and START. Try to handle that case. */
14990 if (row < w->current_matrix->rows
14991 || row->mode_line_p)
14993 row = w->current_matrix->rows;
14994 if (row->mode_line_p)
14995 ++row;
14998 /* Due to newlines in overlay strings, we may have to
14999 skip forward over overlay strings. */
15000 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15001 && MATRIX_ROW_END_CHARPOS (row) == PT
15002 && !cursor_row_p (row))
15003 ++row;
15005 /* If within the scroll margin, scroll. */
15006 if (row->y < top_scroll_margin
15007 && CHARPOS (startp) != BEGV)
15008 scroll_p = 1;
15010 else
15012 /* Cursor did not move. So don't scroll even if cursor line
15013 is partially visible, as it was so before. */
15014 rc = CURSOR_MOVEMENT_SUCCESS;
15017 if (PT < MATRIX_ROW_START_CHARPOS (row)
15018 || PT > MATRIX_ROW_END_CHARPOS (row))
15020 /* if PT is not in the glyph row, give up. */
15021 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15022 must_scroll = 1;
15024 else if (rc != CURSOR_MOVEMENT_SUCCESS
15025 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15027 struct glyph_row *row1;
15029 /* If rows are bidi-reordered and point moved, back up
15030 until we find a row that does not belong to a
15031 continuation line. This is because we must consider
15032 all rows of a continued line as candidates for the
15033 new cursor positioning, since row start and end
15034 positions change non-linearly with vertical position
15035 in such rows. */
15036 /* FIXME: Revisit this when glyph ``spilling'' in
15037 continuation lines' rows is implemented for
15038 bidi-reordered rows. */
15039 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15040 MATRIX_ROW_CONTINUATION_LINE_P (row);
15041 --row)
15043 /* If we hit the beginning of the displayed portion
15044 without finding the first row of a continued
15045 line, give up. */
15046 if (row <= row1)
15048 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15049 break;
15051 xassert (row->enabled_p);
15054 if (must_scroll)
15056 else if (rc != CURSOR_MOVEMENT_SUCCESS
15057 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15058 /* Make sure this isn't a header line by any chance, since
15059 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15060 && !row->mode_line_p
15061 && make_cursor_line_fully_visible_p)
15063 if (PT == MATRIX_ROW_END_CHARPOS (row)
15064 && !row->ends_at_zv_p
15065 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15066 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15067 else if (row->height > window_box_height (w))
15069 /* If we end up in a partially visible line, let's
15070 make it fully visible, except when it's taller
15071 than the window, in which case we can't do much
15072 about it. */
15073 *scroll_step = 1;
15074 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15076 else
15078 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15079 if (!cursor_row_fully_visible_p (w, 0, 1))
15080 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15081 else
15082 rc = CURSOR_MOVEMENT_SUCCESS;
15085 else if (scroll_p)
15086 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15087 else if (rc != CURSOR_MOVEMENT_SUCCESS
15088 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15090 /* With bidi-reordered rows, there could be more than
15091 one candidate row whose start and end positions
15092 occlude point. We need to let set_cursor_from_row
15093 find the best candidate. */
15094 /* FIXME: Revisit this when glyph ``spilling'' in
15095 continuation lines' rows is implemented for
15096 bidi-reordered rows. */
15097 int rv = 0;
15101 int at_zv_p = 0, exact_match_p = 0;
15103 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15104 && PT <= MATRIX_ROW_END_CHARPOS (row)
15105 && cursor_row_p (row))
15106 rv |= set_cursor_from_row (w, row, w->current_matrix,
15107 0, 0, 0, 0);
15108 /* As soon as we've found the exact match for point,
15109 or the first suitable row whose ends_at_zv_p flag
15110 is set, we are done. */
15111 at_zv_p =
15112 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15113 if (rv && !at_zv_p
15114 && w->cursor.hpos >= 0
15115 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15116 w->cursor.vpos))
15118 struct glyph_row *candidate =
15119 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15120 struct glyph *g =
15121 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15122 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15124 exact_match_p =
15125 (BUFFERP (g->object) && g->charpos == PT)
15126 || (INTEGERP (g->object)
15127 && (g->charpos == PT
15128 || (g->charpos == 0 && endpos - 1 == PT)));
15130 if (rv && (at_zv_p || exact_match_p))
15132 rc = CURSOR_MOVEMENT_SUCCESS;
15133 break;
15135 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15136 break;
15137 ++row;
15139 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15140 || row->continued_p)
15141 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15142 || (MATRIX_ROW_START_CHARPOS (row) == PT
15143 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15144 /* If we didn't find any candidate rows, or exited the
15145 loop before all the candidates were examined, signal
15146 to the caller that this method failed. */
15147 if (rc != CURSOR_MOVEMENT_SUCCESS
15148 && !(rv
15149 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15150 && !row->continued_p))
15151 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15152 else if (rv)
15153 rc = CURSOR_MOVEMENT_SUCCESS;
15155 else
15159 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15161 rc = CURSOR_MOVEMENT_SUCCESS;
15162 break;
15164 ++row;
15166 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15167 && MATRIX_ROW_START_CHARPOS (row) == PT
15168 && cursor_row_p (row));
15173 return rc;
15176 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15177 static
15178 #endif
15179 void
15180 set_vertical_scroll_bar (struct window *w)
15182 EMACS_INT start, end, whole;
15184 /* Calculate the start and end positions for the current window.
15185 At some point, it would be nice to choose between scrollbars
15186 which reflect the whole buffer size, with special markers
15187 indicating narrowing, and scrollbars which reflect only the
15188 visible region.
15190 Note that mini-buffers sometimes aren't displaying any text. */
15191 if (!MINI_WINDOW_P (w)
15192 || (w == XWINDOW (minibuf_window)
15193 && NILP (echo_area_buffer[0])))
15195 struct buffer *buf = XBUFFER (w->buffer);
15196 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15197 start = marker_position (w->start) - BUF_BEGV (buf);
15198 /* I don't think this is guaranteed to be right. For the
15199 moment, we'll pretend it is. */
15200 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15202 if (end < start)
15203 end = start;
15204 if (whole < (end - start))
15205 whole = end - start;
15207 else
15208 start = end = whole = 0;
15210 /* Indicate what this scroll bar ought to be displaying now. */
15211 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15212 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15213 (w, end - start, whole, start);
15217 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15218 selected_window is redisplayed.
15220 We can return without actually redisplaying the window if
15221 fonts_changed_p is nonzero. In that case, redisplay_internal will
15222 retry. */
15224 static void
15225 redisplay_window (Lisp_Object window, int just_this_one_p)
15227 struct window *w = XWINDOW (window);
15228 struct frame *f = XFRAME (w->frame);
15229 struct buffer *buffer = XBUFFER (w->buffer);
15230 struct buffer *old = current_buffer;
15231 struct text_pos lpoint, opoint, startp;
15232 int update_mode_line;
15233 int tem;
15234 struct it it;
15235 /* Record it now because it's overwritten. */
15236 int current_matrix_up_to_date_p = 0;
15237 int used_current_matrix_p = 0;
15238 /* This is less strict than current_matrix_up_to_date_p.
15239 It indicates that the buffer contents and narrowing are unchanged. */
15240 int buffer_unchanged_p = 0;
15241 int temp_scroll_step = 0;
15242 int count = SPECPDL_INDEX ();
15243 int rc;
15244 int centering_position = -1;
15245 int last_line_misfit = 0;
15246 EMACS_INT beg_unchanged, end_unchanged;
15248 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15249 opoint = lpoint;
15251 /* W must be a leaf window here. */
15252 xassert (!NILP (w->buffer));
15253 #if GLYPH_DEBUG
15254 *w->desired_matrix->method = 0;
15255 #endif
15257 restart:
15258 reconsider_clip_changes (w, buffer);
15260 /* Has the mode line to be updated? */
15261 update_mode_line = (!NILP (w->update_mode_line)
15262 || update_mode_lines
15263 || buffer->clip_changed
15264 || buffer->prevent_redisplay_optimizations_p);
15266 if (MINI_WINDOW_P (w))
15268 if (w == XWINDOW (echo_area_window)
15269 && !NILP (echo_area_buffer[0]))
15271 if (update_mode_line)
15272 /* We may have to update a tty frame's menu bar or a
15273 tool-bar. Example `M-x C-h C-h C-g'. */
15274 goto finish_menu_bars;
15275 else
15276 /* We've already displayed the echo area glyphs in this window. */
15277 goto finish_scroll_bars;
15279 else if ((w != XWINDOW (minibuf_window)
15280 || minibuf_level == 0)
15281 /* When buffer is nonempty, redisplay window normally. */
15282 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15283 /* Quail displays non-mini buffers in minibuffer window.
15284 In that case, redisplay the window normally. */
15285 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15287 /* W is a mini-buffer window, but it's not active, so clear
15288 it. */
15289 int yb = window_text_bottom_y (w);
15290 struct glyph_row *row;
15291 int y;
15293 for (y = 0, row = w->desired_matrix->rows;
15294 y < yb;
15295 y += row->height, ++row)
15296 blank_row (w, row, y);
15297 goto finish_scroll_bars;
15300 clear_glyph_matrix (w->desired_matrix);
15303 /* Otherwise set up data on this window; select its buffer and point
15304 value. */
15305 /* Really select the buffer, for the sake of buffer-local
15306 variables. */
15307 set_buffer_internal_1 (XBUFFER (w->buffer));
15309 current_matrix_up_to_date_p
15310 = (!NILP (w->window_end_valid)
15311 && !current_buffer->clip_changed
15312 && !current_buffer->prevent_redisplay_optimizations_p
15313 && XFASTINT (w->last_modified) >= MODIFF
15314 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15316 /* Run the window-bottom-change-functions
15317 if it is possible that the text on the screen has changed
15318 (either due to modification of the text, or any other reason). */
15319 if (!current_matrix_up_to_date_p
15320 && !NILP (Vwindow_text_change_functions))
15322 safe_run_hooks (Qwindow_text_change_functions);
15323 goto restart;
15326 beg_unchanged = BEG_UNCHANGED;
15327 end_unchanged = END_UNCHANGED;
15329 SET_TEXT_POS (opoint, PT, PT_BYTE);
15331 specbind (Qinhibit_point_motion_hooks, Qt);
15333 buffer_unchanged_p
15334 = (!NILP (w->window_end_valid)
15335 && !current_buffer->clip_changed
15336 && XFASTINT (w->last_modified) >= MODIFF
15337 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15339 /* When windows_or_buffers_changed is non-zero, we can't rely on
15340 the window end being valid, so set it to nil there. */
15341 if (windows_or_buffers_changed)
15343 /* If window starts on a continuation line, maybe adjust the
15344 window start in case the window's width changed. */
15345 if (XMARKER (w->start)->buffer == current_buffer)
15346 compute_window_start_on_continuation_line (w);
15348 w->window_end_valid = Qnil;
15351 /* Some sanity checks. */
15352 CHECK_WINDOW_END (w);
15353 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15354 abort ();
15355 if (BYTEPOS (opoint) < CHARPOS (opoint))
15356 abort ();
15358 /* If %c is in mode line, update it if needed. */
15359 if (!NILP (w->column_number_displayed)
15360 /* This alternative quickly identifies a common case
15361 where no change is needed. */
15362 && !(PT == XFASTINT (w->last_point)
15363 && XFASTINT (w->last_modified) >= MODIFF
15364 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15365 && (XFASTINT (w->column_number_displayed) != current_column ()))
15366 update_mode_line = 1;
15368 /* Count number of windows showing the selected buffer. An indirect
15369 buffer counts as its base buffer. */
15370 if (!just_this_one_p)
15372 struct buffer *current_base, *window_base;
15373 current_base = current_buffer;
15374 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15375 if (current_base->base_buffer)
15376 current_base = current_base->base_buffer;
15377 if (window_base->base_buffer)
15378 window_base = window_base->base_buffer;
15379 if (current_base == window_base)
15380 buffer_shared++;
15383 /* Point refers normally to the selected window. For any other
15384 window, set up appropriate value. */
15385 if (!EQ (window, selected_window))
15387 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15388 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15389 if (new_pt < BEGV)
15391 new_pt = BEGV;
15392 new_pt_byte = BEGV_BYTE;
15393 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15395 else if (new_pt > (ZV - 1))
15397 new_pt = ZV;
15398 new_pt_byte = ZV_BYTE;
15399 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15402 /* We don't use SET_PT so that the point-motion hooks don't run. */
15403 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15406 /* If any of the character widths specified in the display table
15407 have changed, invalidate the width run cache. It's true that
15408 this may be a bit late to catch such changes, but the rest of
15409 redisplay goes (non-fatally) haywire when the display table is
15410 changed, so why should we worry about doing any better? */
15411 if (current_buffer->width_run_cache)
15413 struct Lisp_Char_Table *disptab = buffer_display_table ();
15415 if (! disptab_matches_widthtab (disptab,
15416 XVECTOR (BVAR (current_buffer, width_table))))
15418 invalidate_region_cache (current_buffer,
15419 current_buffer->width_run_cache,
15420 BEG, Z);
15421 recompute_width_table (current_buffer, disptab);
15425 /* If window-start is screwed up, choose a new one. */
15426 if (XMARKER (w->start)->buffer != current_buffer)
15427 goto recenter;
15429 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15431 /* If someone specified a new starting point but did not insist,
15432 check whether it can be used. */
15433 if (!NILP (w->optional_new_start)
15434 && CHARPOS (startp) >= BEGV
15435 && CHARPOS (startp) <= ZV)
15437 w->optional_new_start = Qnil;
15438 start_display (&it, w, startp);
15439 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15440 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15441 if (IT_CHARPOS (it) == PT)
15442 w->force_start = Qt;
15443 /* IT may overshoot PT if text at PT is invisible. */
15444 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15445 w->force_start = Qt;
15448 force_start:
15450 /* Handle case where place to start displaying has been specified,
15451 unless the specified location is outside the accessible range. */
15452 if (!NILP (w->force_start)
15453 || w->frozen_window_start_p)
15455 /* We set this later on if we have to adjust point. */
15456 int new_vpos = -1;
15458 w->force_start = Qnil;
15459 w->vscroll = 0;
15460 w->window_end_valid = Qnil;
15462 /* Forget any recorded base line for line number display. */
15463 if (!buffer_unchanged_p)
15464 w->base_line_number = Qnil;
15466 /* Redisplay the mode line. Select the buffer properly for that.
15467 Also, run the hook window-scroll-functions
15468 because we have scrolled. */
15469 /* Note, we do this after clearing force_start because
15470 if there's an error, it is better to forget about force_start
15471 than to get into an infinite loop calling the hook functions
15472 and having them get more errors. */
15473 if (!update_mode_line
15474 || ! NILP (Vwindow_scroll_functions))
15476 update_mode_line = 1;
15477 w->update_mode_line = Qt;
15478 startp = run_window_scroll_functions (window, startp);
15481 w->last_modified = make_number (0);
15482 w->last_overlay_modified = make_number (0);
15483 if (CHARPOS (startp) < BEGV)
15484 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15485 else if (CHARPOS (startp) > ZV)
15486 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15488 /* Redisplay, then check if cursor has been set during the
15489 redisplay. Give up if new fonts were loaded. */
15490 /* We used to issue a CHECK_MARGINS argument to try_window here,
15491 but this causes scrolling to fail when point begins inside
15492 the scroll margin (bug#148) -- cyd */
15493 if (!try_window (window, startp, 0))
15495 w->force_start = Qt;
15496 clear_glyph_matrix (w->desired_matrix);
15497 goto need_larger_matrices;
15500 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15502 /* If point does not appear, try to move point so it does
15503 appear. The desired matrix has been built above, so we
15504 can use it here. */
15505 new_vpos = window_box_height (w) / 2;
15508 if (!cursor_row_fully_visible_p (w, 0, 0))
15510 /* Point does appear, but on a line partly visible at end of window.
15511 Move it back to a fully-visible line. */
15512 new_vpos = window_box_height (w);
15515 /* If we need to move point for either of the above reasons,
15516 now actually do it. */
15517 if (new_vpos >= 0)
15519 struct glyph_row *row;
15521 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15522 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15523 ++row;
15525 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15526 MATRIX_ROW_START_BYTEPOS (row));
15528 if (w != XWINDOW (selected_window))
15529 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15530 else if (current_buffer == old)
15531 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15533 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15535 /* If we are highlighting the region, then we just changed
15536 the region, so redisplay to show it. */
15537 if (!NILP (Vtransient_mark_mode)
15538 && !NILP (BVAR (current_buffer, mark_active)))
15540 clear_glyph_matrix (w->desired_matrix);
15541 if (!try_window (window, startp, 0))
15542 goto need_larger_matrices;
15546 #if GLYPH_DEBUG
15547 debug_method_add (w, "forced window start");
15548 #endif
15549 goto done;
15552 /* Handle case where text has not changed, only point, and it has
15553 not moved off the frame, and we are not retrying after hscroll.
15554 (current_matrix_up_to_date_p is nonzero when retrying.) */
15555 if (current_matrix_up_to_date_p
15556 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15557 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15559 switch (rc)
15561 case CURSOR_MOVEMENT_SUCCESS:
15562 used_current_matrix_p = 1;
15563 goto done;
15565 case CURSOR_MOVEMENT_MUST_SCROLL:
15566 goto try_to_scroll;
15568 default:
15569 abort ();
15572 /* If current starting point was originally the beginning of a line
15573 but no longer is, find a new starting point. */
15574 else if (!NILP (w->start_at_line_beg)
15575 && !(CHARPOS (startp) <= BEGV
15576 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15578 #if GLYPH_DEBUG
15579 debug_method_add (w, "recenter 1");
15580 #endif
15581 goto recenter;
15584 /* Try scrolling with try_window_id. Value is > 0 if update has
15585 been done, it is -1 if we know that the same window start will
15586 not work. It is 0 if unsuccessful for some other reason. */
15587 else if ((tem = try_window_id (w)) != 0)
15589 #if GLYPH_DEBUG
15590 debug_method_add (w, "try_window_id %d", tem);
15591 #endif
15593 if (fonts_changed_p)
15594 goto need_larger_matrices;
15595 if (tem > 0)
15596 goto done;
15598 /* Otherwise try_window_id has returned -1 which means that we
15599 don't want the alternative below this comment to execute. */
15601 else if (CHARPOS (startp) >= BEGV
15602 && CHARPOS (startp) <= ZV
15603 && PT >= CHARPOS (startp)
15604 && (CHARPOS (startp) < ZV
15605 /* Avoid starting at end of buffer. */
15606 || CHARPOS (startp) == BEGV
15607 || (XFASTINT (w->last_modified) >= MODIFF
15608 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15610 int d1, d2, d3, d4, d5, d6;
15612 /* If first window line is a continuation line, and window start
15613 is inside the modified region, but the first change is before
15614 current window start, we must select a new window start.
15616 However, if this is the result of a down-mouse event (e.g. by
15617 extending the mouse-drag-overlay), we don't want to select a
15618 new window start, since that would change the position under
15619 the mouse, resulting in an unwanted mouse-movement rather
15620 than a simple mouse-click. */
15621 if (NILP (w->start_at_line_beg)
15622 && NILP (do_mouse_tracking)
15623 && CHARPOS (startp) > BEGV
15624 && CHARPOS (startp) > BEG + beg_unchanged
15625 && CHARPOS (startp) <= Z - end_unchanged
15626 /* Even if w->start_at_line_beg is nil, a new window may
15627 start at a line_beg, since that's how set_buffer_window
15628 sets it. So, we need to check the return value of
15629 compute_window_start_on_continuation_line. (See also
15630 bug#197). */
15631 && XMARKER (w->start)->buffer == current_buffer
15632 && compute_window_start_on_continuation_line (w)
15633 /* It doesn't make sense to force the window start like we
15634 do at label force_start if it is already known that point
15635 will not be visible in the resulting window, because
15636 doing so will move point from its correct position
15637 instead of scrolling the window to bring point into view.
15638 See bug#9324. */
15639 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15641 w->force_start = Qt;
15642 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15643 goto force_start;
15646 #if GLYPH_DEBUG
15647 debug_method_add (w, "same window start");
15648 #endif
15650 /* Try to redisplay starting at same place as before.
15651 If point has not moved off frame, accept the results. */
15652 if (!current_matrix_up_to_date_p
15653 /* Don't use try_window_reusing_current_matrix in this case
15654 because a window scroll function can have changed the
15655 buffer. */
15656 || !NILP (Vwindow_scroll_functions)
15657 || MINI_WINDOW_P (w)
15658 || !(used_current_matrix_p
15659 = try_window_reusing_current_matrix (w)))
15661 IF_DEBUG (debug_method_add (w, "1"));
15662 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15663 /* -1 means we need to scroll.
15664 0 means we need new matrices, but fonts_changed_p
15665 is set in that case, so we will detect it below. */
15666 goto try_to_scroll;
15669 if (fonts_changed_p)
15670 goto need_larger_matrices;
15672 if (w->cursor.vpos >= 0)
15674 if (!just_this_one_p
15675 || current_buffer->clip_changed
15676 || BEG_UNCHANGED < CHARPOS (startp))
15677 /* Forget any recorded base line for line number display. */
15678 w->base_line_number = Qnil;
15680 if (!cursor_row_fully_visible_p (w, 1, 0))
15682 clear_glyph_matrix (w->desired_matrix);
15683 last_line_misfit = 1;
15685 /* Drop through and scroll. */
15686 else
15687 goto done;
15689 else
15690 clear_glyph_matrix (w->desired_matrix);
15693 try_to_scroll:
15695 w->last_modified = make_number (0);
15696 w->last_overlay_modified = make_number (0);
15698 /* Redisplay the mode line. Select the buffer properly for that. */
15699 if (!update_mode_line)
15701 update_mode_line = 1;
15702 w->update_mode_line = Qt;
15705 /* Try to scroll by specified few lines. */
15706 if ((scroll_conservatively
15707 || emacs_scroll_step
15708 || temp_scroll_step
15709 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15710 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15711 && CHARPOS (startp) >= BEGV
15712 && CHARPOS (startp) <= ZV)
15714 /* The function returns -1 if new fonts were loaded, 1 if
15715 successful, 0 if not successful. */
15716 int ss = try_scrolling (window, just_this_one_p,
15717 scroll_conservatively,
15718 emacs_scroll_step,
15719 temp_scroll_step, last_line_misfit);
15720 switch (ss)
15722 case SCROLLING_SUCCESS:
15723 goto done;
15725 case SCROLLING_NEED_LARGER_MATRICES:
15726 goto need_larger_matrices;
15728 case SCROLLING_FAILED:
15729 break;
15731 default:
15732 abort ();
15736 /* Finally, just choose a place to start which positions point
15737 according to user preferences. */
15739 recenter:
15741 #if GLYPH_DEBUG
15742 debug_method_add (w, "recenter");
15743 #endif
15745 /* w->vscroll = 0; */
15747 /* Forget any previously recorded base line for line number display. */
15748 if (!buffer_unchanged_p)
15749 w->base_line_number = Qnil;
15751 /* Determine the window start relative to point. */
15752 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15753 it.current_y = it.last_visible_y;
15754 if (centering_position < 0)
15756 int margin =
15757 scroll_margin > 0
15758 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15759 : 0;
15760 EMACS_INT margin_pos = CHARPOS (startp);
15761 Lisp_Object aggressive;
15762 int scrolling_up;
15764 /* If there is a scroll margin at the top of the window, find
15765 its character position. */
15766 if (margin
15767 /* Cannot call start_display if startp is not in the
15768 accessible region of the buffer. This can happen when we
15769 have just switched to a different buffer and/or changed
15770 its restriction. In that case, startp is initialized to
15771 the character position 1 (BEGV) because we did not yet
15772 have chance to display the buffer even once. */
15773 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15775 struct it it1;
15776 void *it1data = NULL;
15778 SAVE_IT (it1, it, it1data);
15779 start_display (&it1, w, startp);
15780 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15781 margin_pos = IT_CHARPOS (it1);
15782 RESTORE_IT (&it, &it, it1data);
15784 scrolling_up = PT > margin_pos;
15785 aggressive =
15786 scrolling_up
15787 ? BVAR (current_buffer, scroll_up_aggressively)
15788 : BVAR (current_buffer, scroll_down_aggressively);
15790 if (!MINI_WINDOW_P (w)
15791 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15793 int pt_offset = 0;
15795 /* Setting scroll-conservatively overrides
15796 scroll-*-aggressively. */
15797 if (!scroll_conservatively && NUMBERP (aggressive))
15799 double float_amount = XFLOATINT (aggressive);
15801 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15802 if (pt_offset == 0 && float_amount > 0)
15803 pt_offset = 1;
15804 if (pt_offset && margin > 0)
15805 margin -= 1;
15807 /* Compute how much to move the window start backward from
15808 point so that point will be displayed where the user
15809 wants it. */
15810 if (scrolling_up)
15812 centering_position = it.last_visible_y;
15813 if (pt_offset)
15814 centering_position -= pt_offset;
15815 centering_position -=
15816 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15817 + WINDOW_HEADER_LINE_HEIGHT (w);
15818 /* Don't let point enter the scroll margin near top of
15819 the window. */
15820 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15821 centering_position = margin * FRAME_LINE_HEIGHT (f);
15823 else
15824 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15826 else
15827 /* Set the window start half the height of the window backward
15828 from point. */
15829 centering_position = window_box_height (w) / 2;
15831 move_it_vertically_backward (&it, centering_position);
15833 xassert (IT_CHARPOS (it) >= BEGV);
15835 /* The function move_it_vertically_backward may move over more
15836 than the specified y-distance. If it->w is small, e.g. a
15837 mini-buffer window, we may end up in front of the window's
15838 display area. Start displaying at the start of the line
15839 containing PT in this case. */
15840 if (it.current_y <= 0)
15842 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15843 move_it_vertically_backward (&it, 0);
15844 it.current_y = 0;
15847 it.current_x = it.hpos = 0;
15849 /* Set the window start position here explicitly, to avoid an
15850 infinite loop in case the functions in window-scroll-functions
15851 get errors. */
15852 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15854 /* Run scroll hooks. */
15855 startp = run_window_scroll_functions (window, it.current.pos);
15857 /* Redisplay the window. */
15858 if (!current_matrix_up_to_date_p
15859 || windows_or_buffers_changed
15860 || cursor_type_changed
15861 /* Don't use try_window_reusing_current_matrix in this case
15862 because it can have changed the buffer. */
15863 || !NILP (Vwindow_scroll_functions)
15864 || !just_this_one_p
15865 || MINI_WINDOW_P (w)
15866 || !(used_current_matrix_p
15867 = try_window_reusing_current_matrix (w)))
15868 try_window (window, startp, 0);
15870 /* If new fonts have been loaded (due to fontsets), give up. We
15871 have to start a new redisplay since we need to re-adjust glyph
15872 matrices. */
15873 if (fonts_changed_p)
15874 goto need_larger_matrices;
15876 /* If cursor did not appear assume that the middle of the window is
15877 in the first line of the window. Do it again with the next line.
15878 (Imagine a window of height 100, displaying two lines of height
15879 60. Moving back 50 from it->last_visible_y will end in the first
15880 line.) */
15881 if (w->cursor.vpos < 0)
15883 if (!NILP (w->window_end_valid)
15884 && PT >= Z - XFASTINT (w->window_end_pos))
15886 clear_glyph_matrix (w->desired_matrix);
15887 move_it_by_lines (&it, 1);
15888 try_window (window, it.current.pos, 0);
15890 else if (PT < IT_CHARPOS (it))
15892 clear_glyph_matrix (w->desired_matrix);
15893 move_it_by_lines (&it, -1);
15894 try_window (window, it.current.pos, 0);
15896 else
15898 /* Not much we can do about it. */
15902 /* Consider the following case: Window starts at BEGV, there is
15903 invisible, intangible text at BEGV, so that display starts at
15904 some point START > BEGV. It can happen that we are called with
15905 PT somewhere between BEGV and START. Try to handle that case. */
15906 if (w->cursor.vpos < 0)
15908 struct glyph_row *row = w->current_matrix->rows;
15909 if (row->mode_line_p)
15910 ++row;
15911 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15914 if (!cursor_row_fully_visible_p (w, 0, 0))
15916 /* If vscroll is enabled, disable it and try again. */
15917 if (w->vscroll)
15919 w->vscroll = 0;
15920 clear_glyph_matrix (w->desired_matrix);
15921 goto recenter;
15924 /* Users who set scroll-conservatively to a large number want
15925 point just above/below the scroll margin. If we ended up
15926 with point's row partially visible, move the window start to
15927 make that row fully visible and out of the margin. */
15928 if (scroll_conservatively > SCROLL_LIMIT)
15930 int margin =
15931 scroll_margin > 0
15932 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15933 : 0;
15934 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15936 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15937 clear_glyph_matrix (w->desired_matrix);
15938 if (1 == try_window (window, it.current.pos,
15939 TRY_WINDOW_CHECK_MARGINS))
15940 goto done;
15943 /* If centering point failed to make the whole line visible,
15944 put point at the top instead. That has to make the whole line
15945 visible, if it can be done. */
15946 if (centering_position == 0)
15947 goto done;
15949 clear_glyph_matrix (w->desired_matrix);
15950 centering_position = 0;
15951 goto recenter;
15954 done:
15956 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15957 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15958 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15959 ? Qt : Qnil);
15961 /* Display the mode line, if we must. */
15962 if ((update_mode_line
15963 /* If window not full width, must redo its mode line
15964 if (a) the window to its side is being redone and
15965 (b) we do a frame-based redisplay. This is a consequence
15966 of how inverted lines are drawn in frame-based redisplay. */
15967 || (!just_this_one_p
15968 && !FRAME_WINDOW_P (f)
15969 && !WINDOW_FULL_WIDTH_P (w))
15970 /* Line number to display. */
15971 || INTEGERP (w->base_line_pos)
15972 /* Column number is displayed and different from the one displayed. */
15973 || (!NILP (w->column_number_displayed)
15974 && (XFASTINT (w->column_number_displayed) != current_column ())))
15975 /* This means that the window has a mode line. */
15976 && (WINDOW_WANTS_MODELINE_P (w)
15977 || WINDOW_WANTS_HEADER_LINE_P (w)))
15979 display_mode_lines (w);
15981 /* If mode line height has changed, arrange for a thorough
15982 immediate redisplay using the correct mode line height. */
15983 if (WINDOW_WANTS_MODELINE_P (w)
15984 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15986 fonts_changed_p = 1;
15987 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15988 = DESIRED_MODE_LINE_HEIGHT (w);
15991 /* If header line height has changed, arrange for a thorough
15992 immediate redisplay using the correct header line height. */
15993 if (WINDOW_WANTS_HEADER_LINE_P (w)
15994 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15996 fonts_changed_p = 1;
15997 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15998 = DESIRED_HEADER_LINE_HEIGHT (w);
16001 if (fonts_changed_p)
16002 goto need_larger_matrices;
16005 if (!line_number_displayed
16006 && !BUFFERP (w->base_line_pos))
16008 w->base_line_pos = Qnil;
16009 w->base_line_number = Qnil;
16012 finish_menu_bars:
16014 /* When we reach a frame's selected window, redo the frame's menu bar. */
16015 if (update_mode_line
16016 && EQ (FRAME_SELECTED_WINDOW (f), window))
16018 int redisplay_menu_p = 0;
16020 if (FRAME_WINDOW_P (f))
16022 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16023 || defined (HAVE_NS) || defined (USE_GTK)
16024 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16025 #else
16026 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16027 #endif
16029 else
16030 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16032 if (redisplay_menu_p)
16033 display_menu_bar (w);
16035 #ifdef HAVE_WINDOW_SYSTEM
16036 if (FRAME_WINDOW_P (f))
16038 #if defined (USE_GTK) || defined (HAVE_NS)
16039 if (FRAME_EXTERNAL_TOOL_BAR (f))
16040 redisplay_tool_bar (f);
16041 #else
16042 if (WINDOWP (f->tool_bar_window)
16043 && (FRAME_TOOL_BAR_LINES (f) > 0
16044 || !NILP (Vauto_resize_tool_bars))
16045 && redisplay_tool_bar (f))
16046 ignore_mouse_drag_p = 1;
16047 #endif
16049 #endif
16052 #ifdef HAVE_WINDOW_SYSTEM
16053 if (FRAME_WINDOW_P (f)
16054 && update_window_fringes (w, (just_this_one_p
16055 || (!used_current_matrix_p && !overlay_arrow_seen)
16056 || w->pseudo_window_p)))
16058 update_begin (f);
16059 BLOCK_INPUT;
16060 if (draw_window_fringes (w, 1))
16061 x_draw_vertical_border (w);
16062 UNBLOCK_INPUT;
16063 update_end (f);
16065 #endif /* HAVE_WINDOW_SYSTEM */
16067 /* We go to this label, with fonts_changed_p nonzero,
16068 if it is necessary to try again using larger glyph matrices.
16069 We have to redeem the scroll bar even in this case,
16070 because the loop in redisplay_internal expects that. */
16071 need_larger_matrices:
16073 finish_scroll_bars:
16075 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16077 /* Set the thumb's position and size. */
16078 set_vertical_scroll_bar (w);
16080 /* Note that we actually used the scroll bar attached to this
16081 window, so it shouldn't be deleted at the end of redisplay. */
16082 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16083 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16086 /* Restore current_buffer and value of point in it. The window
16087 update may have changed the buffer, so first make sure `opoint'
16088 is still valid (Bug#6177). */
16089 if (CHARPOS (opoint) < BEGV)
16090 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16091 else if (CHARPOS (opoint) > ZV)
16092 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16093 else
16094 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16096 set_buffer_internal_1 (old);
16097 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16098 shorter. This can be caused by log truncation in *Messages*. */
16099 if (CHARPOS (lpoint) <= ZV)
16100 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16102 unbind_to (count, Qnil);
16106 /* Build the complete desired matrix of WINDOW with a window start
16107 buffer position POS.
16109 Value is 1 if successful. It is zero if fonts were loaded during
16110 redisplay which makes re-adjusting glyph matrices necessary, and -1
16111 if point would appear in the scroll margins.
16112 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16113 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16114 set in FLAGS.) */
16117 try_window (Lisp_Object window, struct text_pos pos, int flags)
16119 struct window *w = XWINDOW (window);
16120 struct it it;
16121 struct glyph_row *last_text_row = NULL;
16122 struct frame *f = XFRAME (w->frame);
16124 /* Make POS the new window start. */
16125 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16127 /* Mark cursor position as unknown. No overlay arrow seen. */
16128 w->cursor.vpos = -1;
16129 overlay_arrow_seen = 0;
16131 /* Initialize iterator and info to start at POS. */
16132 start_display (&it, w, pos);
16134 /* Display all lines of W. */
16135 while (it.current_y < it.last_visible_y)
16137 if (display_line (&it))
16138 last_text_row = it.glyph_row - 1;
16139 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16140 return 0;
16143 /* Don't let the cursor end in the scroll margins. */
16144 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16145 && !MINI_WINDOW_P (w))
16147 int this_scroll_margin;
16149 if (scroll_margin > 0)
16151 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16152 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16154 else
16155 this_scroll_margin = 0;
16157 if ((w->cursor.y >= 0 /* not vscrolled */
16158 && w->cursor.y < this_scroll_margin
16159 && CHARPOS (pos) > BEGV
16160 && IT_CHARPOS (it) < ZV)
16161 /* rms: considering make_cursor_line_fully_visible_p here
16162 seems to give wrong results. We don't want to recenter
16163 when the last line is partly visible, we want to allow
16164 that case to be handled in the usual way. */
16165 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16167 w->cursor.vpos = -1;
16168 clear_glyph_matrix (w->desired_matrix);
16169 return -1;
16173 /* If bottom moved off end of frame, change mode line percentage. */
16174 if (XFASTINT (w->window_end_pos) <= 0
16175 && Z != IT_CHARPOS (it))
16176 w->update_mode_line = Qt;
16178 /* Set window_end_pos to the offset of the last character displayed
16179 on the window from the end of current_buffer. Set
16180 window_end_vpos to its row number. */
16181 if (last_text_row)
16183 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16184 w->window_end_bytepos
16185 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16186 w->window_end_pos
16187 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16188 w->window_end_vpos
16189 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16190 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16191 ->displays_text_p);
16193 else
16195 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16196 w->window_end_pos = make_number (Z - ZV);
16197 w->window_end_vpos = make_number (0);
16200 /* But that is not valid info until redisplay finishes. */
16201 w->window_end_valid = Qnil;
16202 return 1;
16207 /************************************************************************
16208 Window redisplay reusing current matrix when buffer has not changed
16209 ************************************************************************/
16211 /* Try redisplay of window W showing an unchanged buffer with a
16212 different window start than the last time it was displayed by
16213 reusing its current matrix. Value is non-zero if successful.
16214 W->start is the new window start. */
16216 static int
16217 try_window_reusing_current_matrix (struct window *w)
16219 struct frame *f = XFRAME (w->frame);
16220 struct glyph_row *bottom_row;
16221 struct it it;
16222 struct run run;
16223 struct text_pos start, new_start;
16224 int nrows_scrolled, i;
16225 struct glyph_row *last_text_row;
16226 struct glyph_row *last_reused_text_row;
16227 struct glyph_row *start_row;
16228 int start_vpos, min_y, max_y;
16230 #if GLYPH_DEBUG
16231 if (inhibit_try_window_reusing)
16232 return 0;
16233 #endif
16235 if (/* This function doesn't handle terminal frames. */
16236 !FRAME_WINDOW_P (f)
16237 /* Don't try to reuse the display if windows have been split
16238 or such. */
16239 || windows_or_buffers_changed
16240 || cursor_type_changed)
16241 return 0;
16243 /* Can't do this if region may have changed. */
16244 if ((!NILP (Vtransient_mark_mode)
16245 && !NILP (BVAR (current_buffer, mark_active)))
16246 || !NILP (w->region_showing)
16247 || !NILP (Vshow_trailing_whitespace))
16248 return 0;
16250 /* If top-line visibility has changed, give up. */
16251 if (WINDOW_WANTS_HEADER_LINE_P (w)
16252 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16253 return 0;
16255 /* Give up if old or new display is scrolled vertically. We could
16256 make this function handle this, but right now it doesn't. */
16257 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16258 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16259 return 0;
16261 /* The variable new_start now holds the new window start. The old
16262 start `start' can be determined from the current matrix. */
16263 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16264 start = start_row->minpos;
16265 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16267 /* Clear the desired matrix for the display below. */
16268 clear_glyph_matrix (w->desired_matrix);
16270 if (CHARPOS (new_start) <= CHARPOS (start))
16272 /* Don't use this method if the display starts with an ellipsis
16273 displayed for invisible text. It's not easy to handle that case
16274 below, and it's certainly not worth the effort since this is
16275 not a frequent case. */
16276 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16277 return 0;
16279 IF_DEBUG (debug_method_add (w, "twu1"));
16281 /* Display up to a row that can be reused. The variable
16282 last_text_row is set to the last row displayed that displays
16283 text. Note that it.vpos == 0 if or if not there is a
16284 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16285 start_display (&it, w, new_start);
16286 w->cursor.vpos = -1;
16287 last_text_row = last_reused_text_row = NULL;
16289 while (it.current_y < it.last_visible_y
16290 && !fonts_changed_p)
16292 /* If we have reached into the characters in the START row,
16293 that means the line boundaries have changed. So we
16294 can't start copying with the row START. Maybe it will
16295 work to start copying with the following row. */
16296 while (IT_CHARPOS (it) > CHARPOS (start))
16298 /* Advance to the next row as the "start". */
16299 start_row++;
16300 start = start_row->minpos;
16301 /* If there are no more rows to try, or just one, give up. */
16302 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16303 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16304 || CHARPOS (start) == ZV)
16306 clear_glyph_matrix (w->desired_matrix);
16307 return 0;
16310 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16312 /* If we have reached alignment, we can copy the rest of the
16313 rows. */
16314 if (IT_CHARPOS (it) == CHARPOS (start)
16315 /* Don't accept "alignment" inside a display vector,
16316 since start_row could have started in the middle of
16317 that same display vector (thus their character
16318 positions match), and we have no way of telling if
16319 that is the case. */
16320 && it.current.dpvec_index < 0)
16321 break;
16323 if (display_line (&it))
16324 last_text_row = it.glyph_row - 1;
16328 /* A value of current_y < last_visible_y means that we stopped
16329 at the previous window start, which in turn means that we
16330 have at least one reusable row. */
16331 if (it.current_y < it.last_visible_y)
16333 struct glyph_row *row;
16335 /* IT.vpos always starts from 0; it counts text lines. */
16336 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16338 /* Find PT if not already found in the lines displayed. */
16339 if (w->cursor.vpos < 0)
16341 int dy = it.current_y - start_row->y;
16343 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16344 row = row_containing_pos (w, PT, row, NULL, dy);
16345 if (row)
16346 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16347 dy, nrows_scrolled);
16348 else
16350 clear_glyph_matrix (w->desired_matrix);
16351 return 0;
16355 /* Scroll the display. Do it before the current matrix is
16356 changed. The problem here is that update has not yet
16357 run, i.e. part of the current matrix is not up to date.
16358 scroll_run_hook will clear the cursor, and use the
16359 current matrix to get the height of the row the cursor is
16360 in. */
16361 run.current_y = start_row->y;
16362 run.desired_y = it.current_y;
16363 run.height = it.last_visible_y - it.current_y;
16365 if (run.height > 0 && run.current_y != run.desired_y)
16367 update_begin (f);
16368 FRAME_RIF (f)->update_window_begin_hook (w);
16369 FRAME_RIF (f)->clear_window_mouse_face (w);
16370 FRAME_RIF (f)->scroll_run_hook (w, &run);
16371 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16372 update_end (f);
16375 /* Shift current matrix down by nrows_scrolled lines. */
16376 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16377 rotate_matrix (w->current_matrix,
16378 start_vpos,
16379 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16380 nrows_scrolled);
16382 /* Disable lines that must be updated. */
16383 for (i = 0; i < nrows_scrolled; ++i)
16384 (start_row + i)->enabled_p = 0;
16386 /* Re-compute Y positions. */
16387 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16388 max_y = it.last_visible_y;
16389 for (row = start_row + nrows_scrolled;
16390 row < bottom_row;
16391 ++row)
16393 row->y = it.current_y;
16394 row->visible_height = row->height;
16396 if (row->y < min_y)
16397 row->visible_height -= min_y - row->y;
16398 if (row->y + row->height > max_y)
16399 row->visible_height -= row->y + row->height - max_y;
16400 if (row->fringe_bitmap_periodic_p)
16401 row->redraw_fringe_bitmaps_p = 1;
16403 it.current_y += row->height;
16405 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16406 last_reused_text_row = row;
16407 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16408 break;
16411 /* Disable lines in the current matrix which are now
16412 below the window. */
16413 for (++row; row < bottom_row; ++row)
16414 row->enabled_p = row->mode_line_p = 0;
16417 /* Update window_end_pos etc.; last_reused_text_row is the last
16418 reused row from the current matrix containing text, if any.
16419 The value of last_text_row is the last displayed line
16420 containing text. */
16421 if (last_reused_text_row)
16423 w->window_end_bytepos
16424 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16425 w->window_end_pos
16426 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16427 w->window_end_vpos
16428 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16429 w->current_matrix));
16431 else if (last_text_row)
16433 w->window_end_bytepos
16434 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16435 w->window_end_pos
16436 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16437 w->window_end_vpos
16438 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16440 else
16442 /* This window must be completely empty. */
16443 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16444 w->window_end_pos = make_number (Z - ZV);
16445 w->window_end_vpos = make_number (0);
16447 w->window_end_valid = Qnil;
16449 /* Update hint: don't try scrolling again in update_window. */
16450 w->desired_matrix->no_scrolling_p = 1;
16452 #if GLYPH_DEBUG
16453 debug_method_add (w, "try_window_reusing_current_matrix 1");
16454 #endif
16455 return 1;
16457 else if (CHARPOS (new_start) > CHARPOS (start))
16459 struct glyph_row *pt_row, *row;
16460 struct glyph_row *first_reusable_row;
16461 struct glyph_row *first_row_to_display;
16462 int dy;
16463 int yb = window_text_bottom_y (w);
16465 /* Find the row starting at new_start, if there is one. Don't
16466 reuse a partially visible line at the end. */
16467 first_reusable_row = start_row;
16468 while (first_reusable_row->enabled_p
16469 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16470 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16471 < CHARPOS (new_start)))
16472 ++first_reusable_row;
16474 /* Give up if there is no row to reuse. */
16475 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16476 || !first_reusable_row->enabled_p
16477 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16478 != CHARPOS (new_start)))
16479 return 0;
16481 /* We can reuse fully visible rows beginning with
16482 first_reusable_row to the end of the window. Set
16483 first_row_to_display to the first row that cannot be reused.
16484 Set pt_row to the row containing point, if there is any. */
16485 pt_row = NULL;
16486 for (first_row_to_display = first_reusable_row;
16487 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16488 ++first_row_to_display)
16490 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16491 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16492 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16493 && first_row_to_display->ends_at_zv_p
16494 && pt_row == NULL)))
16495 pt_row = first_row_to_display;
16498 /* Start displaying at the start of first_row_to_display. */
16499 xassert (first_row_to_display->y < yb);
16500 init_to_row_start (&it, w, first_row_to_display);
16502 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16503 - start_vpos);
16504 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16505 - nrows_scrolled);
16506 it.current_y = (first_row_to_display->y - first_reusable_row->y
16507 + WINDOW_HEADER_LINE_HEIGHT (w));
16509 /* Display lines beginning with first_row_to_display in the
16510 desired matrix. Set last_text_row to the last row displayed
16511 that displays text. */
16512 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16513 if (pt_row == NULL)
16514 w->cursor.vpos = -1;
16515 last_text_row = NULL;
16516 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16517 if (display_line (&it))
16518 last_text_row = it.glyph_row - 1;
16520 /* If point is in a reused row, adjust y and vpos of the cursor
16521 position. */
16522 if (pt_row)
16524 w->cursor.vpos -= nrows_scrolled;
16525 w->cursor.y -= first_reusable_row->y - start_row->y;
16528 /* Give up if point isn't in a row displayed or reused. (This
16529 also handles the case where w->cursor.vpos < nrows_scrolled
16530 after the calls to display_line, which can happen with scroll
16531 margins. See bug#1295.) */
16532 if (w->cursor.vpos < 0)
16534 clear_glyph_matrix (w->desired_matrix);
16535 return 0;
16538 /* Scroll the display. */
16539 run.current_y = first_reusable_row->y;
16540 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16541 run.height = it.last_visible_y - run.current_y;
16542 dy = run.current_y - run.desired_y;
16544 if (run.height)
16546 update_begin (f);
16547 FRAME_RIF (f)->update_window_begin_hook (w);
16548 FRAME_RIF (f)->clear_window_mouse_face (w);
16549 FRAME_RIF (f)->scroll_run_hook (w, &run);
16550 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16551 update_end (f);
16554 /* Adjust Y positions of reused rows. */
16555 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16556 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16557 max_y = it.last_visible_y;
16558 for (row = first_reusable_row; row < first_row_to_display; ++row)
16560 row->y -= dy;
16561 row->visible_height = row->height;
16562 if (row->y < min_y)
16563 row->visible_height -= min_y - row->y;
16564 if (row->y + row->height > max_y)
16565 row->visible_height -= row->y + row->height - max_y;
16566 if (row->fringe_bitmap_periodic_p)
16567 row->redraw_fringe_bitmaps_p = 1;
16570 /* Scroll the current matrix. */
16571 xassert (nrows_scrolled > 0);
16572 rotate_matrix (w->current_matrix,
16573 start_vpos,
16574 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16575 -nrows_scrolled);
16577 /* Disable rows not reused. */
16578 for (row -= nrows_scrolled; row < bottom_row; ++row)
16579 row->enabled_p = 0;
16581 /* Point may have moved to a different line, so we cannot assume that
16582 the previous cursor position is valid; locate the correct row. */
16583 if (pt_row)
16585 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16586 row < bottom_row
16587 && PT >= MATRIX_ROW_END_CHARPOS (row)
16588 && !row->ends_at_zv_p;
16589 row++)
16591 w->cursor.vpos++;
16592 w->cursor.y = row->y;
16594 if (row < bottom_row)
16596 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16597 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16599 /* Can't use this optimization with bidi-reordered glyph
16600 rows, unless cursor is already at point. */
16601 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16603 if (!(w->cursor.hpos >= 0
16604 && w->cursor.hpos < row->used[TEXT_AREA]
16605 && BUFFERP (glyph->object)
16606 && glyph->charpos == PT))
16607 return 0;
16609 else
16610 for (; glyph < end
16611 && (!BUFFERP (glyph->object)
16612 || glyph->charpos < PT);
16613 glyph++)
16615 w->cursor.hpos++;
16616 w->cursor.x += glyph->pixel_width;
16621 /* Adjust window end. A null value of last_text_row means that
16622 the window end is in reused rows which in turn means that
16623 only its vpos can have changed. */
16624 if (last_text_row)
16626 w->window_end_bytepos
16627 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16628 w->window_end_pos
16629 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16630 w->window_end_vpos
16631 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16633 else
16635 w->window_end_vpos
16636 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16639 w->window_end_valid = Qnil;
16640 w->desired_matrix->no_scrolling_p = 1;
16642 #if GLYPH_DEBUG
16643 debug_method_add (w, "try_window_reusing_current_matrix 2");
16644 #endif
16645 return 1;
16648 return 0;
16653 /************************************************************************
16654 Window redisplay reusing current matrix when buffer has changed
16655 ************************************************************************/
16657 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16658 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16659 EMACS_INT *, EMACS_INT *);
16660 static struct glyph_row *
16661 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16662 struct glyph_row *);
16665 /* Return the last row in MATRIX displaying text. If row START is
16666 non-null, start searching with that row. IT gives the dimensions
16667 of the display. Value is null if matrix is empty; otherwise it is
16668 a pointer to the row found. */
16670 static struct glyph_row *
16671 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16672 struct glyph_row *start)
16674 struct glyph_row *row, *row_found;
16676 /* Set row_found to the last row in IT->w's current matrix
16677 displaying text. The loop looks funny but think of partially
16678 visible lines. */
16679 row_found = NULL;
16680 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16681 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16683 xassert (row->enabled_p);
16684 row_found = row;
16685 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16686 break;
16687 ++row;
16690 return row_found;
16694 /* Return the last row in the current matrix of W that is not affected
16695 by changes at the start of current_buffer that occurred since W's
16696 current matrix was built. Value is null if no such row exists.
16698 BEG_UNCHANGED us the number of characters unchanged at the start of
16699 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16700 first changed character in current_buffer. Characters at positions <
16701 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16702 when the current matrix was built. */
16704 static struct glyph_row *
16705 find_last_unchanged_at_beg_row (struct window *w)
16707 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16708 struct glyph_row *row;
16709 struct glyph_row *row_found = NULL;
16710 int yb = window_text_bottom_y (w);
16712 /* Find the last row displaying unchanged text. */
16713 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16714 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16715 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16716 ++row)
16718 if (/* If row ends before first_changed_pos, it is unchanged,
16719 except in some case. */
16720 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16721 /* When row ends in ZV and we write at ZV it is not
16722 unchanged. */
16723 && !row->ends_at_zv_p
16724 /* When first_changed_pos is the end of a continued line,
16725 row is not unchanged because it may be no longer
16726 continued. */
16727 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16728 && (row->continued_p
16729 || row->exact_window_width_line_p))
16730 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16731 needs to be recomputed, so don't consider this row as
16732 unchanged. This happens when the last line was
16733 bidi-reordered and was killed immediately before this
16734 redisplay cycle. In that case, ROW->end stores the
16735 buffer position of the first visual-order character of
16736 the killed text, which is now beyond ZV. */
16737 && CHARPOS (row->end.pos) <= ZV)
16738 row_found = row;
16740 /* Stop if last visible row. */
16741 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16742 break;
16745 return row_found;
16749 /* Find the first glyph row in the current matrix of W that is not
16750 affected by changes at the end of current_buffer since the
16751 time W's current matrix was built.
16753 Return in *DELTA the number of chars by which buffer positions in
16754 unchanged text at the end of current_buffer must be adjusted.
16756 Return in *DELTA_BYTES the corresponding number of bytes.
16758 Value is null if no such row exists, i.e. all rows are affected by
16759 changes. */
16761 static struct glyph_row *
16762 find_first_unchanged_at_end_row (struct window *w,
16763 EMACS_INT *delta, EMACS_INT *delta_bytes)
16765 struct glyph_row *row;
16766 struct glyph_row *row_found = NULL;
16768 *delta = *delta_bytes = 0;
16770 /* Display must not have been paused, otherwise the current matrix
16771 is not up to date. */
16772 eassert (!NILP (w->window_end_valid));
16774 /* A value of window_end_pos >= END_UNCHANGED means that the window
16775 end is in the range of changed text. If so, there is no
16776 unchanged row at the end of W's current matrix. */
16777 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16778 return NULL;
16780 /* Set row to the last row in W's current matrix displaying text. */
16781 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16783 /* If matrix is entirely empty, no unchanged row exists. */
16784 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16786 /* The value of row is the last glyph row in the matrix having a
16787 meaningful buffer position in it. The end position of row
16788 corresponds to window_end_pos. This allows us to translate
16789 buffer positions in the current matrix to current buffer
16790 positions for characters not in changed text. */
16791 EMACS_INT Z_old =
16792 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16793 EMACS_INT Z_BYTE_old =
16794 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16795 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16796 struct glyph_row *first_text_row
16797 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16799 *delta = Z - Z_old;
16800 *delta_bytes = Z_BYTE - Z_BYTE_old;
16802 /* Set last_unchanged_pos to the buffer position of the last
16803 character in the buffer that has not been changed. Z is the
16804 index + 1 of the last character in current_buffer, i.e. by
16805 subtracting END_UNCHANGED we get the index of the last
16806 unchanged character, and we have to add BEG to get its buffer
16807 position. */
16808 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16809 last_unchanged_pos_old = last_unchanged_pos - *delta;
16811 /* Search backward from ROW for a row displaying a line that
16812 starts at a minimum position >= last_unchanged_pos_old. */
16813 for (; row > first_text_row; --row)
16815 /* This used to abort, but it can happen.
16816 It is ok to just stop the search instead here. KFS. */
16817 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16818 break;
16820 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16821 row_found = row;
16825 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16827 return row_found;
16831 /* Make sure that glyph rows in the current matrix of window W
16832 reference the same glyph memory as corresponding rows in the
16833 frame's frame matrix. This function is called after scrolling W's
16834 current matrix on a terminal frame in try_window_id and
16835 try_window_reusing_current_matrix. */
16837 static void
16838 sync_frame_with_window_matrix_rows (struct window *w)
16840 struct frame *f = XFRAME (w->frame);
16841 struct glyph_row *window_row, *window_row_end, *frame_row;
16843 /* Preconditions: W must be a leaf window and full-width. Its frame
16844 must have a frame matrix. */
16845 xassert (NILP (w->hchild) && NILP (w->vchild));
16846 xassert (WINDOW_FULL_WIDTH_P (w));
16847 xassert (!FRAME_WINDOW_P (f));
16849 /* If W is a full-width window, glyph pointers in W's current matrix
16850 have, by definition, to be the same as glyph pointers in the
16851 corresponding frame matrix. Note that frame matrices have no
16852 marginal areas (see build_frame_matrix). */
16853 window_row = w->current_matrix->rows;
16854 window_row_end = window_row + w->current_matrix->nrows;
16855 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16856 while (window_row < window_row_end)
16858 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16859 struct glyph *end = window_row->glyphs[LAST_AREA];
16861 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16862 frame_row->glyphs[TEXT_AREA] = start;
16863 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16864 frame_row->glyphs[LAST_AREA] = end;
16866 /* Disable frame rows whose corresponding window rows have
16867 been disabled in try_window_id. */
16868 if (!window_row->enabled_p)
16869 frame_row->enabled_p = 0;
16871 ++window_row, ++frame_row;
16876 /* Find the glyph row in window W containing CHARPOS. Consider all
16877 rows between START and END (not inclusive). END null means search
16878 all rows to the end of the display area of W. Value is the row
16879 containing CHARPOS or null. */
16881 struct glyph_row *
16882 row_containing_pos (struct window *w, EMACS_INT charpos,
16883 struct glyph_row *start, struct glyph_row *end, int dy)
16885 struct glyph_row *row = start;
16886 struct glyph_row *best_row = NULL;
16887 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16888 int last_y;
16890 /* If we happen to start on a header-line, skip that. */
16891 if (row->mode_line_p)
16892 ++row;
16894 if ((end && row >= end) || !row->enabled_p)
16895 return NULL;
16897 last_y = window_text_bottom_y (w) - dy;
16899 while (1)
16901 /* Give up if we have gone too far. */
16902 if (end && row >= end)
16903 return NULL;
16904 /* This formerly returned if they were equal.
16905 I think that both quantities are of a "last plus one" type;
16906 if so, when they are equal, the row is within the screen. -- rms. */
16907 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16908 return NULL;
16910 /* If it is in this row, return this row. */
16911 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16912 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16913 /* The end position of a row equals the start
16914 position of the next row. If CHARPOS is there, we
16915 would rather display it in the next line, except
16916 when this line ends in ZV. */
16917 && !row->ends_at_zv_p
16918 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16919 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16921 struct glyph *g;
16923 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16924 || (!best_row && !row->continued_p))
16925 return row;
16926 /* In bidi-reordered rows, there could be several rows
16927 occluding point, all of them belonging to the same
16928 continued line. We need to find the row which fits
16929 CHARPOS the best. */
16930 for (g = row->glyphs[TEXT_AREA];
16931 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16932 g++)
16934 if (!STRINGP (g->object))
16936 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16938 mindif = eabs (g->charpos - charpos);
16939 best_row = row;
16940 /* Exact match always wins. */
16941 if (mindif == 0)
16942 return best_row;
16947 else if (best_row && !row->continued_p)
16948 return best_row;
16949 ++row;
16954 /* Try to redisplay window W by reusing its existing display. W's
16955 current matrix must be up to date when this function is called,
16956 i.e. window_end_valid must not be nil.
16958 Value is
16960 1 if display has been updated
16961 0 if otherwise unsuccessful
16962 -1 if redisplay with same window start is known not to succeed
16964 The following steps are performed:
16966 1. Find the last row in the current matrix of W that is not
16967 affected by changes at the start of current_buffer. If no such row
16968 is found, give up.
16970 2. Find the first row in W's current matrix that is not affected by
16971 changes at the end of current_buffer. Maybe there is no such row.
16973 3. Display lines beginning with the row + 1 found in step 1 to the
16974 row found in step 2 or, if step 2 didn't find a row, to the end of
16975 the window.
16977 4. If cursor is not known to appear on the window, give up.
16979 5. If display stopped at the row found in step 2, scroll the
16980 display and current matrix as needed.
16982 6. Maybe display some lines at the end of W, if we must. This can
16983 happen under various circumstances, like a partially visible line
16984 becoming fully visible, or because newly displayed lines are displayed
16985 in smaller font sizes.
16987 7. Update W's window end information. */
16989 static int
16990 try_window_id (struct window *w)
16992 struct frame *f = XFRAME (w->frame);
16993 struct glyph_matrix *current_matrix = w->current_matrix;
16994 struct glyph_matrix *desired_matrix = w->desired_matrix;
16995 struct glyph_row *last_unchanged_at_beg_row;
16996 struct glyph_row *first_unchanged_at_end_row;
16997 struct glyph_row *row;
16998 struct glyph_row *bottom_row;
16999 int bottom_vpos;
17000 struct it it;
17001 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
17002 int dvpos, dy;
17003 struct text_pos start_pos;
17004 struct run run;
17005 int first_unchanged_at_end_vpos = 0;
17006 struct glyph_row *last_text_row, *last_text_row_at_end;
17007 struct text_pos start;
17008 EMACS_INT first_changed_charpos, last_changed_charpos;
17010 #if GLYPH_DEBUG
17011 if (inhibit_try_window_id)
17012 return 0;
17013 #endif
17015 /* This is handy for debugging. */
17016 #if 0
17017 #define GIVE_UP(X) \
17018 do { \
17019 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17020 return 0; \
17021 } while (0)
17022 #else
17023 #define GIVE_UP(X) return 0
17024 #endif
17026 SET_TEXT_POS_FROM_MARKER (start, w->start);
17028 /* Don't use this for mini-windows because these can show
17029 messages and mini-buffers, and we don't handle that here. */
17030 if (MINI_WINDOW_P (w))
17031 GIVE_UP (1);
17033 /* This flag is used to prevent redisplay optimizations. */
17034 if (windows_or_buffers_changed || cursor_type_changed)
17035 GIVE_UP (2);
17037 /* Verify that narrowing has not changed.
17038 Also verify that we were not told to prevent redisplay optimizations.
17039 It would be nice to further
17040 reduce the number of cases where this prevents try_window_id. */
17041 if (current_buffer->clip_changed
17042 || current_buffer->prevent_redisplay_optimizations_p)
17043 GIVE_UP (3);
17045 /* Window must either use window-based redisplay or be full width. */
17046 if (!FRAME_WINDOW_P (f)
17047 && (!FRAME_LINE_INS_DEL_OK (f)
17048 || !WINDOW_FULL_WIDTH_P (w)))
17049 GIVE_UP (4);
17051 /* Give up if point is known NOT to appear in W. */
17052 if (PT < CHARPOS (start))
17053 GIVE_UP (5);
17055 /* Another way to prevent redisplay optimizations. */
17056 if (XFASTINT (w->last_modified) == 0)
17057 GIVE_UP (6);
17059 /* Verify that window is not hscrolled. */
17060 if (XFASTINT (w->hscroll) != 0)
17061 GIVE_UP (7);
17063 /* Verify that display wasn't paused. */
17064 if (NILP (w->window_end_valid))
17065 GIVE_UP (8);
17067 /* Can't use this if highlighting a region because a cursor movement
17068 will do more than just set the cursor. */
17069 if (!NILP (Vtransient_mark_mode)
17070 && !NILP (BVAR (current_buffer, mark_active)))
17071 GIVE_UP (9);
17073 /* Likewise if highlighting trailing whitespace. */
17074 if (!NILP (Vshow_trailing_whitespace))
17075 GIVE_UP (11);
17077 /* Likewise if showing a region. */
17078 if (!NILP (w->region_showing))
17079 GIVE_UP (10);
17081 /* Can't use this if overlay arrow position and/or string have
17082 changed. */
17083 if (overlay_arrows_changed_p ())
17084 GIVE_UP (12);
17086 /* When word-wrap is on, adding a space to the first word of a
17087 wrapped line can change the wrap position, altering the line
17088 above it. It might be worthwhile to handle this more
17089 intelligently, but for now just redisplay from scratch. */
17090 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17091 GIVE_UP (21);
17093 /* Under bidi reordering, adding or deleting a character in the
17094 beginning of a paragraph, before the first strong directional
17095 character, can change the base direction of the paragraph (unless
17096 the buffer specifies a fixed paragraph direction), which will
17097 require to redisplay the whole paragraph. It might be worthwhile
17098 to find the paragraph limits and widen the range of redisplayed
17099 lines to that, but for now just give up this optimization and
17100 redisplay from scratch. */
17101 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17102 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17103 GIVE_UP (22);
17105 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17106 only if buffer has really changed. The reason is that the gap is
17107 initially at Z for freshly visited files. The code below would
17108 set end_unchanged to 0 in that case. */
17109 if (MODIFF > SAVE_MODIFF
17110 /* This seems to happen sometimes after saving a buffer. */
17111 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17113 if (GPT - BEG < BEG_UNCHANGED)
17114 BEG_UNCHANGED = GPT - BEG;
17115 if (Z - GPT < END_UNCHANGED)
17116 END_UNCHANGED = Z - GPT;
17119 /* The position of the first and last character that has been changed. */
17120 first_changed_charpos = BEG + BEG_UNCHANGED;
17121 last_changed_charpos = Z - END_UNCHANGED;
17123 /* If window starts after a line end, and the last change is in
17124 front of that newline, then changes don't affect the display.
17125 This case happens with stealth-fontification. Note that although
17126 the display is unchanged, glyph positions in the matrix have to
17127 be adjusted, of course. */
17128 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17129 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17130 && ((last_changed_charpos < CHARPOS (start)
17131 && CHARPOS (start) == BEGV)
17132 || (last_changed_charpos < CHARPOS (start) - 1
17133 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17135 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17136 struct glyph_row *r0;
17138 /* Compute how many chars/bytes have been added to or removed
17139 from the buffer. */
17140 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17141 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17142 Z_delta = Z - Z_old;
17143 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17145 /* Give up if PT is not in the window. Note that it already has
17146 been checked at the start of try_window_id that PT is not in
17147 front of the window start. */
17148 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17149 GIVE_UP (13);
17151 /* If window start is unchanged, we can reuse the whole matrix
17152 as is, after adjusting glyph positions. No need to compute
17153 the window end again, since its offset from Z hasn't changed. */
17154 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17155 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17156 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17157 /* PT must not be in a partially visible line. */
17158 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17159 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17161 /* Adjust positions in the glyph matrix. */
17162 if (Z_delta || Z_delta_bytes)
17164 struct glyph_row *r1
17165 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17166 increment_matrix_positions (w->current_matrix,
17167 MATRIX_ROW_VPOS (r0, current_matrix),
17168 MATRIX_ROW_VPOS (r1, current_matrix),
17169 Z_delta, Z_delta_bytes);
17172 /* Set the cursor. */
17173 row = row_containing_pos (w, PT, r0, NULL, 0);
17174 if (row)
17175 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17176 else
17177 abort ();
17178 return 1;
17182 /* Handle the case that changes are all below what is displayed in
17183 the window, and that PT is in the window. This shortcut cannot
17184 be taken if ZV is visible in the window, and text has been added
17185 there that is visible in the window. */
17186 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17187 /* ZV is not visible in the window, or there are no
17188 changes at ZV, actually. */
17189 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17190 || first_changed_charpos == last_changed_charpos))
17192 struct glyph_row *r0;
17194 /* Give up if PT is not in the window. Note that it already has
17195 been checked at the start of try_window_id that PT is not in
17196 front of the window start. */
17197 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17198 GIVE_UP (14);
17200 /* If window start is unchanged, we can reuse the whole matrix
17201 as is, without changing glyph positions since no text has
17202 been added/removed in front of the window end. */
17203 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17204 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17205 /* PT must not be in a partially visible line. */
17206 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17207 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17209 /* We have to compute the window end anew since text
17210 could have been added/removed after it. */
17211 w->window_end_pos
17212 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17213 w->window_end_bytepos
17214 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17216 /* Set the cursor. */
17217 row = row_containing_pos (w, PT, r0, NULL, 0);
17218 if (row)
17219 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17220 else
17221 abort ();
17222 return 2;
17226 /* Give up if window start is in the changed area.
17228 The condition used to read
17230 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17232 but why that was tested escapes me at the moment. */
17233 if (CHARPOS (start) >= first_changed_charpos
17234 && CHARPOS (start) <= last_changed_charpos)
17235 GIVE_UP (15);
17237 /* Check that window start agrees with the start of the first glyph
17238 row in its current matrix. Check this after we know the window
17239 start is not in changed text, otherwise positions would not be
17240 comparable. */
17241 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17242 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17243 GIVE_UP (16);
17245 /* Give up if the window ends in strings. Overlay strings
17246 at the end are difficult to handle, so don't try. */
17247 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17248 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17249 GIVE_UP (20);
17251 /* Compute the position at which we have to start displaying new
17252 lines. Some of the lines at the top of the window might be
17253 reusable because they are not displaying changed text. Find the
17254 last row in W's current matrix not affected by changes at the
17255 start of current_buffer. Value is null if changes start in the
17256 first line of window. */
17257 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17258 if (last_unchanged_at_beg_row)
17260 /* Avoid starting to display in the middle of a character, a TAB
17261 for instance. This is easier than to set up the iterator
17262 exactly, and it's not a frequent case, so the additional
17263 effort wouldn't really pay off. */
17264 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17265 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17266 && last_unchanged_at_beg_row > w->current_matrix->rows)
17267 --last_unchanged_at_beg_row;
17269 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17270 GIVE_UP (17);
17272 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17273 GIVE_UP (18);
17274 start_pos = it.current.pos;
17276 /* Start displaying new lines in the desired matrix at the same
17277 vpos we would use in the current matrix, i.e. below
17278 last_unchanged_at_beg_row. */
17279 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17280 current_matrix);
17281 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17282 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17284 xassert (it.hpos == 0 && it.current_x == 0);
17286 else
17288 /* There are no reusable lines at the start of the window.
17289 Start displaying in the first text line. */
17290 start_display (&it, w, start);
17291 it.vpos = it.first_vpos;
17292 start_pos = it.current.pos;
17295 /* Find the first row that is not affected by changes at the end of
17296 the buffer. Value will be null if there is no unchanged row, in
17297 which case we must redisplay to the end of the window. delta
17298 will be set to the value by which buffer positions beginning with
17299 first_unchanged_at_end_row have to be adjusted due to text
17300 changes. */
17301 first_unchanged_at_end_row
17302 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17303 IF_DEBUG (debug_delta = delta);
17304 IF_DEBUG (debug_delta_bytes = delta_bytes);
17306 /* Set stop_pos to the buffer position up to which we will have to
17307 display new lines. If first_unchanged_at_end_row != NULL, this
17308 is the buffer position of the start of the line displayed in that
17309 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17310 that we don't stop at a buffer position. */
17311 stop_pos = 0;
17312 if (first_unchanged_at_end_row)
17314 xassert (last_unchanged_at_beg_row == NULL
17315 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17317 /* If this is a continuation line, move forward to the next one
17318 that isn't. Changes in lines above affect this line.
17319 Caution: this may move first_unchanged_at_end_row to a row
17320 not displaying text. */
17321 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17322 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17323 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17324 < it.last_visible_y))
17325 ++first_unchanged_at_end_row;
17327 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17328 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17329 >= it.last_visible_y))
17330 first_unchanged_at_end_row = NULL;
17331 else
17333 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17334 + delta);
17335 first_unchanged_at_end_vpos
17336 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17337 xassert (stop_pos >= Z - END_UNCHANGED);
17340 else if (last_unchanged_at_beg_row == NULL)
17341 GIVE_UP (19);
17344 #if GLYPH_DEBUG
17346 /* Either there is no unchanged row at the end, or the one we have
17347 now displays text. This is a necessary condition for the window
17348 end pos calculation at the end of this function. */
17349 xassert (first_unchanged_at_end_row == NULL
17350 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17352 debug_last_unchanged_at_beg_vpos
17353 = (last_unchanged_at_beg_row
17354 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17355 : -1);
17356 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17358 #endif /* GLYPH_DEBUG != 0 */
17361 /* Display new lines. Set last_text_row to the last new line
17362 displayed which has text on it, i.e. might end up as being the
17363 line where the window_end_vpos is. */
17364 w->cursor.vpos = -1;
17365 last_text_row = NULL;
17366 overlay_arrow_seen = 0;
17367 while (it.current_y < it.last_visible_y
17368 && !fonts_changed_p
17369 && (first_unchanged_at_end_row == NULL
17370 || IT_CHARPOS (it) < stop_pos))
17372 if (display_line (&it))
17373 last_text_row = it.glyph_row - 1;
17376 if (fonts_changed_p)
17377 return -1;
17380 /* Compute differences in buffer positions, y-positions etc. for
17381 lines reused at the bottom of the window. Compute what we can
17382 scroll. */
17383 if (first_unchanged_at_end_row
17384 /* No lines reused because we displayed everything up to the
17385 bottom of the window. */
17386 && it.current_y < it.last_visible_y)
17388 dvpos = (it.vpos
17389 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17390 current_matrix));
17391 dy = it.current_y - first_unchanged_at_end_row->y;
17392 run.current_y = first_unchanged_at_end_row->y;
17393 run.desired_y = run.current_y + dy;
17394 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17396 else
17398 delta = delta_bytes = dvpos = dy
17399 = run.current_y = run.desired_y = run.height = 0;
17400 first_unchanged_at_end_row = NULL;
17402 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17405 /* Find the cursor if not already found. We have to decide whether
17406 PT will appear on this window (it sometimes doesn't, but this is
17407 not a very frequent case.) This decision has to be made before
17408 the current matrix is altered. A value of cursor.vpos < 0 means
17409 that PT is either in one of the lines beginning at
17410 first_unchanged_at_end_row or below the window. Don't care for
17411 lines that might be displayed later at the window end; as
17412 mentioned, this is not a frequent case. */
17413 if (w->cursor.vpos < 0)
17415 /* Cursor in unchanged rows at the top? */
17416 if (PT < CHARPOS (start_pos)
17417 && last_unchanged_at_beg_row)
17419 row = row_containing_pos (w, PT,
17420 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17421 last_unchanged_at_beg_row + 1, 0);
17422 if (row)
17423 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17426 /* Start from first_unchanged_at_end_row looking for PT. */
17427 else if (first_unchanged_at_end_row)
17429 row = row_containing_pos (w, PT - delta,
17430 first_unchanged_at_end_row, NULL, 0);
17431 if (row)
17432 set_cursor_from_row (w, row, w->current_matrix, delta,
17433 delta_bytes, dy, dvpos);
17436 /* Give up if cursor was not found. */
17437 if (w->cursor.vpos < 0)
17439 clear_glyph_matrix (w->desired_matrix);
17440 return -1;
17444 /* Don't let the cursor end in the scroll margins. */
17446 int this_scroll_margin, cursor_height;
17448 this_scroll_margin =
17449 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17450 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17451 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17453 if ((w->cursor.y < this_scroll_margin
17454 && CHARPOS (start) > BEGV)
17455 /* Old redisplay didn't take scroll margin into account at the bottom,
17456 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17457 || (w->cursor.y + (make_cursor_line_fully_visible_p
17458 ? cursor_height + this_scroll_margin
17459 : 1)) > it.last_visible_y)
17461 w->cursor.vpos = -1;
17462 clear_glyph_matrix (w->desired_matrix);
17463 return -1;
17467 /* Scroll the display. Do it before changing the current matrix so
17468 that xterm.c doesn't get confused about where the cursor glyph is
17469 found. */
17470 if (dy && run.height)
17472 update_begin (f);
17474 if (FRAME_WINDOW_P (f))
17476 FRAME_RIF (f)->update_window_begin_hook (w);
17477 FRAME_RIF (f)->clear_window_mouse_face (w);
17478 FRAME_RIF (f)->scroll_run_hook (w, &run);
17479 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17481 else
17483 /* Terminal frame. In this case, dvpos gives the number of
17484 lines to scroll by; dvpos < 0 means scroll up. */
17485 int from_vpos
17486 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17487 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17488 int end = (WINDOW_TOP_EDGE_LINE (w)
17489 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17490 + window_internal_height (w));
17492 #if defined (HAVE_GPM) || defined (MSDOS)
17493 x_clear_window_mouse_face (w);
17494 #endif
17495 /* Perform the operation on the screen. */
17496 if (dvpos > 0)
17498 /* Scroll last_unchanged_at_beg_row to the end of the
17499 window down dvpos lines. */
17500 set_terminal_window (f, end);
17502 /* On dumb terminals delete dvpos lines at the end
17503 before inserting dvpos empty lines. */
17504 if (!FRAME_SCROLL_REGION_OK (f))
17505 ins_del_lines (f, end - dvpos, -dvpos);
17507 /* Insert dvpos empty lines in front of
17508 last_unchanged_at_beg_row. */
17509 ins_del_lines (f, from, dvpos);
17511 else if (dvpos < 0)
17513 /* Scroll up last_unchanged_at_beg_vpos to the end of
17514 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17515 set_terminal_window (f, end);
17517 /* Delete dvpos lines in front of
17518 last_unchanged_at_beg_vpos. ins_del_lines will set
17519 the cursor to the given vpos and emit |dvpos| delete
17520 line sequences. */
17521 ins_del_lines (f, from + dvpos, dvpos);
17523 /* On a dumb terminal insert dvpos empty lines at the
17524 end. */
17525 if (!FRAME_SCROLL_REGION_OK (f))
17526 ins_del_lines (f, end + dvpos, -dvpos);
17529 set_terminal_window (f, 0);
17532 update_end (f);
17535 /* Shift reused rows of the current matrix to the right position.
17536 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17537 text. */
17538 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17539 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17540 if (dvpos < 0)
17542 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17543 bottom_vpos, dvpos);
17544 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17545 bottom_vpos, 0);
17547 else if (dvpos > 0)
17549 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17550 bottom_vpos, dvpos);
17551 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17552 first_unchanged_at_end_vpos + dvpos, 0);
17555 /* For frame-based redisplay, make sure that current frame and window
17556 matrix are in sync with respect to glyph memory. */
17557 if (!FRAME_WINDOW_P (f))
17558 sync_frame_with_window_matrix_rows (w);
17560 /* Adjust buffer positions in reused rows. */
17561 if (delta || delta_bytes)
17562 increment_matrix_positions (current_matrix,
17563 first_unchanged_at_end_vpos + dvpos,
17564 bottom_vpos, delta, delta_bytes);
17566 /* Adjust Y positions. */
17567 if (dy)
17568 shift_glyph_matrix (w, current_matrix,
17569 first_unchanged_at_end_vpos + dvpos,
17570 bottom_vpos, dy);
17572 if (first_unchanged_at_end_row)
17574 first_unchanged_at_end_row += dvpos;
17575 if (first_unchanged_at_end_row->y >= it.last_visible_y
17576 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17577 first_unchanged_at_end_row = NULL;
17580 /* If scrolling up, there may be some lines to display at the end of
17581 the window. */
17582 last_text_row_at_end = NULL;
17583 if (dy < 0)
17585 /* Scrolling up can leave for example a partially visible line
17586 at the end of the window to be redisplayed. */
17587 /* Set last_row to the glyph row in the current matrix where the
17588 window end line is found. It has been moved up or down in
17589 the matrix by dvpos. */
17590 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17591 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17593 /* If last_row is the window end line, it should display text. */
17594 xassert (last_row->displays_text_p);
17596 /* If window end line was partially visible before, begin
17597 displaying at that line. Otherwise begin displaying with the
17598 line following it. */
17599 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17601 init_to_row_start (&it, w, last_row);
17602 it.vpos = last_vpos;
17603 it.current_y = last_row->y;
17605 else
17607 init_to_row_end (&it, w, last_row);
17608 it.vpos = 1 + last_vpos;
17609 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17610 ++last_row;
17613 /* We may start in a continuation line. If so, we have to
17614 get the right continuation_lines_width and current_x. */
17615 it.continuation_lines_width = last_row->continuation_lines_width;
17616 it.hpos = it.current_x = 0;
17618 /* Display the rest of the lines at the window end. */
17619 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17620 while (it.current_y < it.last_visible_y
17621 && !fonts_changed_p)
17623 /* Is it always sure that the display agrees with lines in
17624 the current matrix? I don't think so, so we mark rows
17625 displayed invalid in the current matrix by setting their
17626 enabled_p flag to zero. */
17627 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17628 if (display_line (&it))
17629 last_text_row_at_end = it.glyph_row - 1;
17633 /* Update window_end_pos and window_end_vpos. */
17634 if (first_unchanged_at_end_row
17635 && !last_text_row_at_end)
17637 /* Window end line if one of the preserved rows from the current
17638 matrix. Set row to the last row displaying text in current
17639 matrix starting at first_unchanged_at_end_row, after
17640 scrolling. */
17641 xassert (first_unchanged_at_end_row->displays_text_p);
17642 row = find_last_row_displaying_text (w->current_matrix, &it,
17643 first_unchanged_at_end_row);
17644 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17646 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17647 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17648 w->window_end_vpos
17649 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17650 xassert (w->window_end_bytepos >= 0);
17651 IF_DEBUG (debug_method_add (w, "A"));
17653 else if (last_text_row_at_end)
17655 w->window_end_pos
17656 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17657 w->window_end_bytepos
17658 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17659 w->window_end_vpos
17660 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17661 xassert (w->window_end_bytepos >= 0);
17662 IF_DEBUG (debug_method_add (w, "B"));
17664 else if (last_text_row)
17666 /* We have displayed either to the end of the window or at the
17667 end of the window, i.e. the last row with text is to be found
17668 in the desired matrix. */
17669 w->window_end_pos
17670 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17671 w->window_end_bytepos
17672 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17673 w->window_end_vpos
17674 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17675 xassert (w->window_end_bytepos >= 0);
17677 else if (first_unchanged_at_end_row == NULL
17678 && last_text_row == NULL
17679 && last_text_row_at_end == NULL)
17681 /* Displayed to end of window, but no line containing text was
17682 displayed. Lines were deleted at the end of the window. */
17683 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17684 int vpos = XFASTINT (w->window_end_vpos);
17685 struct glyph_row *current_row = current_matrix->rows + vpos;
17686 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17688 for (row = NULL;
17689 row == NULL && vpos >= first_vpos;
17690 --vpos, --current_row, --desired_row)
17692 if (desired_row->enabled_p)
17694 if (desired_row->displays_text_p)
17695 row = desired_row;
17697 else if (current_row->displays_text_p)
17698 row = current_row;
17701 xassert (row != NULL);
17702 w->window_end_vpos = make_number (vpos + 1);
17703 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17704 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17705 xassert (w->window_end_bytepos >= 0);
17706 IF_DEBUG (debug_method_add (w, "C"));
17708 else
17709 abort ();
17711 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17712 debug_end_vpos = XFASTINT (w->window_end_vpos));
17714 /* Record that display has not been completed. */
17715 w->window_end_valid = Qnil;
17716 w->desired_matrix->no_scrolling_p = 1;
17717 return 3;
17719 #undef GIVE_UP
17724 /***********************************************************************
17725 More debugging support
17726 ***********************************************************************/
17728 #if GLYPH_DEBUG
17730 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17731 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17732 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17735 /* Dump the contents of glyph matrix MATRIX on stderr.
17737 GLYPHS 0 means don't show glyph contents.
17738 GLYPHS 1 means show glyphs in short form
17739 GLYPHS > 1 means show glyphs in long form. */
17741 void
17742 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17744 int i;
17745 for (i = 0; i < matrix->nrows; ++i)
17746 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17750 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17751 the glyph row and area where the glyph comes from. */
17753 void
17754 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17756 if (glyph->type == CHAR_GLYPH)
17758 fprintf (stderr,
17759 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17760 glyph - row->glyphs[TEXT_AREA],
17761 'C',
17762 glyph->charpos,
17763 (BUFFERP (glyph->object)
17764 ? 'B'
17765 : (STRINGP (glyph->object)
17766 ? 'S'
17767 : '-')),
17768 glyph->pixel_width,
17769 glyph->u.ch,
17770 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17771 ? glyph->u.ch
17772 : '.'),
17773 glyph->face_id,
17774 glyph->left_box_line_p,
17775 glyph->right_box_line_p);
17777 else if (glyph->type == STRETCH_GLYPH)
17779 fprintf (stderr,
17780 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17781 glyph - row->glyphs[TEXT_AREA],
17782 'S',
17783 glyph->charpos,
17784 (BUFFERP (glyph->object)
17785 ? 'B'
17786 : (STRINGP (glyph->object)
17787 ? 'S'
17788 : '-')),
17789 glyph->pixel_width,
17791 '.',
17792 glyph->face_id,
17793 glyph->left_box_line_p,
17794 glyph->right_box_line_p);
17796 else if (glyph->type == IMAGE_GLYPH)
17798 fprintf (stderr,
17799 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17800 glyph - row->glyphs[TEXT_AREA],
17801 'I',
17802 glyph->charpos,
17803 (BUFFERP (glyph->object)
17804 ? 'B'
17805 : (STRINGP (glyph->object)
17806 ? 'S'
17807 : '-')),
17808 glyph->pixel_width,
17809 glyph->u.img_id,
17810 '.',
17811 glyph->face_id,
17812 glyph->left_box_line_p,
17813 glyph->right_box_line_p);
17815 else if (glyph->type == COMPOSITE_GLYPH)
17817 fprintf (stderr,
17818 " %5td %4c %6"pI"d %c %3d 0x%05x",
17819 glyph - row->glyphs[TEXT_AREA],
17820 '+',
17821 glyph->charpos,
17822 (BUFFERP (glyph->object)
17823 ? 'B'
17824 : (STRINGP (glyph->object)
17825 ? 'S'
17826 : '-')),
17827 glyph->pixel_width,
17828 glyph->u.cmp.id);
17829 if (glyph->u.cmp.automatic)
17830 fprintf (stderr,
17831 "[%d-%d]",
17832 glyph->slice.cmp.from, glyph->slice.cmp.to);
17833 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17834 glyph->face_id,
17835 glyph->left_box_line_p,
17836 glyph->right_box_line_p);
17841 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17842 GLYPHS 0 means don't show glyph contents.
17843 GLYPHS 1 means show glyphs in short form
17844 GLYPHS > 1 means show glyphs in long form. */
17846 void
17847 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17849 if (glyphs != 1)
17851 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17852 fprintf (stderr, "======================================================================\n");
17854 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17855 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17856 vpos,
17857 MATRIX_ROW_START_CHARPOS (row),
17858 MATRIX_ROW_END_CHARPOS (row),
17859 row->used[TEXT_AREA],
17860 row->contains_overlapping_glyphs_p,
17861 row->enabled_p,
17862 row->truncated_on_left_p,
17863 row->truncated_on_right_p,
17864 row->continued_p,
17865 MATRIX_ROW_CONTINUATION_LINE_P (row),
17866 row->displays_text_p,
17867 row->ends_at_zv_p,
17868 row->fill_line_p,
17869 row->ends_in_middle_of_char_p,
17870 row->starts_in_middle_of_char_p,
17871 row->mouse_face_p,
17872 row->x,
17873 row->y,
17874 row->pixel_width,
17875 row->height,
17876 row->visible_height,
17877 row->ascent,
17878 row->phys_ascent);
17879 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17880 row->end.overlay_string_index,
17881 row->continuation_lines_width);
17882 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17883 CHARPOS (row->start.string_pos),
17884 CHARPOS (row->end.string_pos));
17885 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17886 row->end.dpvec_index);
17889 if (glyphs > 1)
17891 int area;
17893 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17895 struct glyph *glyph = row->glyphs[area];
17896 struct glyph *glyph_end = glyph + row->used[area];
17898 /* Glyph for a line end in text. */
17899 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17900 ++glyph_end;
17902 if (glyph < glyph_end)
17903 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17905 for (; glyph < glyph_end; ++glyph)
17906 dump_glyph (row, glyph, area);
17909 else if (glyphs == 1)
17911 int area;
17913 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17915 char *s = (char *) alloca (row->used[area] + 1);
17916 int i;
17918 for (i = 0; i < row->used[area]; ++i)
17920 struct glyph *glyph = row->glyphs[area] + i;
17921 if (glyph->type == CHAR_GLYPH
17922 && glyph->u.ch < 0x80
17923 && glyph->u.ch >= ' ')
17924 s[i] = glyph->u.ch;
17925 else
17926 s[i] = '.';
17929 s[i] = '\0';
17930 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17936 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17937 Sdump_glyph_matrix, 0, 1, "p",
17938 doc: /* Dump the current matrix of the selected window to stderr.
17939 Shows contents of glyph row structures. With non-nil
17940 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17941 glyphs in short form, otherwise show glyphs in long form. */)
17942 (Lisp_Object glyphs)
17944 struct window *w = XWINDOW (selected_window);
17945 struct buffer *buffer = XBUFFER (w->buffer);
17947 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17948 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17949 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17950 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17951 fprintf (stderr, "=============================================\n");
17952 dump_glyph_matrix (w->current_matrix,
17953 NILP (glyphs) ? 0 : XINT (glyphs));
17954 return Qnil;
17958 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17959 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17960 (void)
17962 struct frame *f = XFRAME (selected_frame);
17963 dump_glyph_matrix (f->current_matrix, 1);
17964 return Qnil;
17968 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17969 doc: /* Dump glyph row ROW to stderr.
17970 GLYPH 0 means don't dump glyphs.
17971 GLYPH 1 means dump glyphs in short form.
17972 GLYPH > 1 or omitted means dump glyphs in long form. */)
17973 (Lisp_Object row, Lisp_Object glyphs)
17975 struct glyph_matrix *matrix;
17976 int vpos;
17978 CHECK_NUMBER (row);
17979 matrix = XWINDOW (selected_window)->current_matrix;
17980 vpos = XINT (row);
17981 if (vpos >= 0 && vpos < matrix->nrows)
17982 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17983 vpos,
17984 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17985 return Qnil;
17989 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17990 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17991 GLYPH 0 means don't dump glyphs.
17992 GLYPH 1 means dump glyphs in short form.
17993 GLYPH > 1 or omitted means dump glyphs in long form. */)
17994 (Lisp_Object row, Lisp_Object glyphs)
17996 struct frame *sf = SELECTED_FRAME ();
17997 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17998 int vpos;
18000 CHECK_NUMBER (row);
18001 vpos = XINT (row);
18002 if (vpos >= 0 && vpos < m->nrows)
18003 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18004 INTEGERP (glyphs) ? XINT (glyphs) : 2);
18005 return Qnil;
18009 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18010 doc: /* Toggle tracing of redisplay.
18011 With ARG, turn tracing on if and only if ARG is positive. */)
18012 (Lisp_Object arg)
18014 if (NILP (arg))
18015 trace_redisplay_p = !trace_redisplay_p;
18016 else
18018 arg = Fprefix_numeric_value (arg);
18019 trace_redisplay_p = XINT (arg) > 0;
18022 return Qnil;
18026 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18027 doc: /* Like `format', but print result to stderr.
18028 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18029 (ptrdiff_t nargs, Lisp_Object *args)
18031 Lisp_Object s = Fformat (nargs, args);
18032 fprintf (stderr, "%s", SDATA (s));
18033 return Qnil;
18036 #endif /* GLYPH_DEBUG */
18040 /***********************************************************************
18041 Building Desired Matrix Rows
18042 ***********************************************************************/
18044 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18045 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18047 static struct glyph_row *
18048 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18050 struct frame *f = XFRAME (WINDOW_FRAME (w));
18051 struct buffer *buffer = XBUFFER (w->buffer);
18052 struct buffer *old = current_buffer;
18053 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18054 int arrow_len = SCHARS (overlay_arrow_string);
18055 const unsigned char *arrow_end = arrow_string + arrow_len;
18056 const unsigned char *p;
18057 struct it it;
18058 int multibyte_p;
18059 int n_glyphs_before;
18061 set_buffer_temp (buffer);
18062 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18063 it.glyph_row->used[TEXT_AREA] = 0;
18064 SET_TEXT_POS (it.position, 0, 0);
18066 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18067 p = arrow_string;
18068 while (p < arrow_end)
18070 Lisp_Object face, ilisp;
18072 /* Get the next character. */
18073 if (multibyte_p)
18074 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18075 else
18077 it.c = it.char_to_display = *p, it.len = 1;
18078 if (! ASCII_CHAR_P (it.c))
18079 it.char_to_display = BYTE8_TO_CHAR (it.c);
18081 p += it.len;
18083 /* Get its face. */
18084 ilisp = make_number (p - arrow_string);
18085 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18086 it.face_id = compute_char_face (f, it.char_to_display, face);
18088 /* Compute its width, get its glyphs. */
18089 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18090 SET_TEXT_POS (it.position, -1, -1);
18091 PRODUCE_GLYPHS (&it);
18093 /* If this character doesn't fit any more in the line, we have
18094 to remove some glyphs. */
18095 if (it.current_x > it.last_visible_x)
18097 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18098 break;
18102 set_buffer_temp (old);
18103 return it.glyph_row;
18107 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18108 glyphs are only inserted for terminal frames since we can't really
18109 win with truncation glyphs when partially visible glyphs are
18110 involved. Which glyphs to insert is determined by
18111 produce_special_glyphs. */
18113 static void
18114 insert_left_trunc_glyphs (struct it *it)
18116 struct it truncate_it;
18117 struct glyph *from, *end, *to, *toend;
18119 xassert (!FRAME_WINDOW_P (it->f));
18121 /* Get the truncation glyphs. */
18122 truncate_it = *it;
18123 truncate_it.current_x = 0;
18124 truncate_it.face_id = DEFAULT_FACE_ID;
18125 truncate_it.glyph_row = &scratch_glyph_row;
18126 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18127 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18128 truncate_it.object = make_number (0);
18129 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18131 /* Overwrite glyphs from IT with truncation glyphs. */
18132 if (!it->glyph_row->reversed_p)
18134 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18135 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18136 to = it->glyph_row->glyphs[TEXT_AREA];
18137 toend = to + it->glyph_row->used[TEXT_AREA];
18139 while (from < end)
18140 *to++ = *from++;
18142 /* There may be padding glyphs left over. Overwrite them too. */
18143 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18145 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18146 while (from < end)
18147 *to++ = *from++;
18150 if (to > toend)
18151 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18153 else
18155 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18156 that back to front. */
18157 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18158 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18159 toend = it->glyph_row->glyphs[TEXT_AREA];
18160 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18162 while (from >= end && to >= toend)
18163 *to-- = *from--;
18164 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18166 from =
18167 truncate_it.glyph_row->glyphs[TEXT_AREA]
18168 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18169 while (from >= end && to >= toend)
18170 *to-- = *from--;
18172 if (from >= end)
18174 /* Need to free some room before prepending additional
18175 glyphs. */
18176 int move_by = from - end + 1;
18177 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18178 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18180 for ( ; g >= g0; g--)
18181 g[move_by] = *g;
18182 while (from >= end)
18183 *to-- = *from--;
18184 it->glyph_row->used[TEXT_AREA] += move_by;
18189 /* Compute the hash code for ROW. */
18190 unsigned
18191 row_hash (struct glyph_row *row)
18193 int area, k;
18194 unsigned hashval = 0;
18196 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18197 for (k = 0; k < row->used[area]; ++k)
18198 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18199 + row->glyphs[area][k].u.val
18200 + row->glyphs[area][k].face_id
18201 + row->glyphs[area][k].padding_p
18202 + (row->glyphs[area][k].type << 2));
18204 return hashval;
18207 /* Compute the pixel height and width of IT->glyph_row.
18209 Most of the time, ascent and height of a display line will be equal
18210 to the max_ascent and max_height values of the display iterator
18211 structure. This is not the case if
18213 1. We hit ZV without displaying anything. In this case, max_ascent
18214 and max_height will be zero.
18216 2. We have some glyphs that don't contribute to the line height.
18217 (The glyph row flag contributes_to_line_height_p is for future
18218 pixmap extensions).
18220 The first case is easily covered by using default values because in
18221 these cases, the line height does not really matter, except that it
18222 must not be zero. */
18224 static void
18225 compute_line_metrics (struct it *it)
18227 struct glyph_row *row = it->glyph_row;
18229 if (FRAME_WINDOW_P (it->f))
18231 int i, min_y, max_y;
18233 /* The line may consist of one space only, that was added to
18234 place the cursor on it. If so, the row's height hasn't been
18235 computed yet. */
18236 if (row->height == 0)
18238 if (it->max_ascent + it->max_descent == 0)
18239 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18240 row->ascent = it->max_ascent;
18241 row->height = it->max_ascent + it->max_descent;
18242 row->phys_ascent = it->max_phys_ascent;
18243 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18244 row->extra_line_spacing = it->max_extra_line_spacing;
18247 /* Compute the width of this line. */
18248 row->pixel_width = row->x;
18249 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18250 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18252 xassert (row->pixel_width >= 0);
18253 xassert (row->ascent >= 0 && row->height > 0);
18255 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18256 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18258 /* If first line's physical ascent is larger than its logical
18259 ascent, use the physical ascent, and make the row taller.
18260 This makes accented characters fully visible. */
18261 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18262 && row->phys_ascent > row->ascent)
18264 row->height += row->phys_ascent - row->ascent;
18265 row->ascent = row->phys_ascent;
18268 /* Compute how much of the line is visible. */
18269 row->visible_height = row->height;
18271 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18272 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18274 if (row->y < min_y)
18275 row->visible_height -= min_y - row->y;
18276 if (row->y + row->height > max_y)
18277 row->visible_height -= row->y + row->height - max_y;
18279 else
18281 row->pixel_width = row->used[TEXT_AREA];
18282 if (row->continued_p)
18283 row->pixel_width -= it->continuation_pixel_width;
18284 else if (row->truncated_on_right_p)
18285 row->pixel_width -= it->truncation_pixel_width;
18286 row->ascent = row->phys_ascent = 0;
18287 row->height = row->phys_height = row->visible_height = 1;
18288 row->extra_line_spacing = 0;
18291 /* Compute a hash code for this row. */
18292 row->hash = row_hash (row);
18294 it->max_ascent = it->max_descent = 0;
18295 it->max_phys_ascent = it->max_phys_descent = 0;
18299 /* Append one space to the glyph row of iterator IT if doing a
18300 window-based redisplay. The space has the same face as
18301 IT->face_id. Value is non-zero if a space was added.
18303 This function is called to make sure that there is always one glyph
18304 at the end of a glyph row that the cursor can be set on under
18305 window-systems. (If there weren't such a glyph we would not know
18306 how wide and tall a box cursor should be displayed).
18308 At the same time this space let's a nicely handle clearing to the
18309 end of the line if the row ends in italic text. */
18311 static int
18312 append_space_for_newline (struct it *it, int default_face_p)
18314 if (FRAME_WINDOW_P (it->f))
18316 int n = it->glyph_row->used[TEXT_AREA];
18318 if (it->glyph_row->glyphs[TEXT_AREA] + n
18319 < it->glyph_row->glyphs[1 + TEXT_AREA])
18321 /* Save some values that must not be changed.
18322 Must save IT->c and IT->len because otherwise
18323 ITERATOR_AT_END_P wouldn't work anymore after
18324 append_space_for_newline has been called. */
18325 enum display_element_type saved_what = it->what;
18326 int saved_c = it->c, saved_len = it->len;
18327 int saved_char_to_display = it->char_to_display;
18328 int saved_x = it->current_x;
18329 int saved_face_id = it->face_id;
18330 struct text_pos saved_pos;
18331 Lisp_Object saved_object;
18332 struct face *face;
18334 saved_object = it->object;
18335 saved_pos = it->position;
18337 it->what = IT_CHARACTER;
18338 memset (&it->position, 0, sizeof it->position);
18339 it->object = make_number (0);
18340 it->c = it->char_to_display = ' ';
18341 it->len = 1;
18343 /* If the default face was remapped, be sure to use the
18344 remapped face for the appended newline. */
18345 if (default_face_p)
18346 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18347 else if (it->face_before_selective_p)
18348 it->face_id = it->saved_face_id;
18349 face = FACE_FROM_ID (it->f, it->face_id);
18350 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18352 PRODUCE_GLYPHS (it);
18354 it->override_ascent = -1;
18355 it->constrain_row_ascent_descent_p = 0;
18356 it->current_x = saved_x;
18357 it->object = saved_object;
18358 it->position = saved_pos;
18359 it->what = saved_what;
18360 it->face_id = saved_face_id;
18361 it->len = saved_len;
18362 it->c = saved_c;
18363 it->char_to_display = saved_char_to_display;
18364 return 1;
18368 return 0;
18372 /* Extend the face of the last glyph in the text area of IT->glyph_row
18373 to the end of the display line. Called from display_line. If the
18374 glyph row is empty, add a space glyph to it so that we know the
18375 face to draw. Set the glyph row flag fill_line_p. If the glyph
18376 row is R2L, prepend a stretch glyph to cover the empty space to the
18377 left of the leftmost glyph. */
18379 static void
18380 extend_face_to_end_of_line (struct it *it)
18382 struct face *face, *default_face;
18383 struct frame *f = it->f;
18385 /* If line is already filled, do nothing. Non window-system frames
18386 get a grace of one more ``pixel'' because their characters are
18387 1-``pixel'' wide, so they hit the equality too early. This grace
18388 is needed only for R2L rows that are not continued, to produce
18389 one extra blank where we could display the cursor. */
18390 if (it->current_x >= it->last_visible_x
18391 + (!FRAME_WINDOW_P (f)
18392 && it->glyph_row->reversed_p
18393 && !it->glyph_row->continued_p))
18394 return;
18396 /* The default face, possibly remapped. */
18397 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18399 /* Face extension extends the background and box of IT->face_id
18400 to the end of the line. If the background equals the background
18401 of the frame, we don't have to do anything. */
18402 if (it->face_before_selective_p)
18403 face = FACE_FROM_ID (f, it->saved_face_id);
18404 else
18405 face = FACE_FROM_ID (f, it->face_id);
18407 if (FRAME_WINDOW_P (f)
18408 && it->glyph_row->displays_text_p
18409 && face->box == FACE_NO_BOX
18410 && face->background == FRAME_BACKGROUND_PIXEL (f)
18411 && !face->stipple
18412 && !it->glyph_row->reversed_p)
18413 return;
18415 /* Set the glyph row flag indicating that the face of the last glyph
18416 in the text area has to be drawn to the end of the text area. */
18417 it->glyph_row->fill_line_p = 1;
18419 /* If current character of IT is not ASCII, make sure we have the
18420 ASCII face. This will be automatically undone the next time
18421 get_next_display_element returns a multibyte character. Note
18422 that the character will always be single byte in unibyte
18423 text. */
18424 if (!ASCII_CHAR_P (it->c))
18426 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18429 if (FRAME_WINDOW_P (f))
18431 /* If the row is empty, add a space with the current face of IT,
18432 so that we know which face to draw. */
18433 if (it->glyph_row->used[TEXT_AREA] == 0)
18435 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18436 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18437 it->glyph_row->used[TEXT_AREA] = 1;
18439 #ifdef HAVE_WINDOW_SYSTEM
18440 if (it->glyph_row->reversed_p)
18442 /* Prepend a stretch glyph to the row, such that the
18443 rightmost glyph will be drawn flushed all the way to the
18444 right margin of the window. The stretch glyph that will
18445 occupy the empty space, if any, to the left of the
18446 glyphs. */
18447 struct font *font = face->font ? face->font : FRAME_FONT (f);
18448 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18449 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18450 struct glyph *g;
18451 int row_width, stretch_ascent, stretch_width;
18452 struct text_pos saved_pos;
18453 int saved_face_id, saved_avoid_cursor;
18455 for (row_width = 0, g = row_start; g < row_end; g++)
18456 row_width += g->pixel_width;
18457 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18458 if (stretch_width > 0)
18460 stretch_ascent =
18461 (((it->ascent + it->descent)
18462 * FONT_BASE (font)) / FONT_HEIGHT (font));
18463 saved_pos = it->position;
18464 memset (&it->position, 0, sizeof it->position);
18465 saved_avoid_cursor = it->avoid_cursor_p;
18466 it->avoid_cursor_p = 1;
18467 saved_face_id = it->face_id;
18468 /* The last row's stretch glyph should get the default
18469 face, to avoid painting the rest of the window with
18470 the region face, if the region ends at ZV. */
18471 if (it->glyph_row->ends_at_zv_p)
18472 it->face_id = default_face->id;
18473 else
18474 it->face_id = face->id;
18475 append_stretch_glyph (it, make_number (0), stretch_width,
18476 it->ascent + it->descent, stretch_ascent);
18477 it->position = saved_pos;
18478 it->avoid_cursor_p = saved_avoid_cursor;
18479 it->face_id = saved_face_id;
18482 #endif /* HAVE_WINDOW_SYSTEM */
18484 else
18486 /* Save some values that must not be changed. */
18487 int saved_x = it->current_x;
18488 struct text_pos saved_pos;
18489 Lisp_Object saved_object;
18490 enum display_element_type saved_what = it->what;
18491 int saved_face_id = it->face_id;
18493 saved_object = it->object;
18494 saved_pos = it->position;
18496 it->what = IT_CHARACTER;
18497 memset (&it->position, 0, sizeof it->position);
18498 it->object = make_number (0);
18499 it->c = it->char_to_display = ' ';
18500 it->len = 1;
18501 /* The last row's blank glyphs should get the default face, to
18502 avoid painting the rest of the window with the region face,
18503 if the region ends at ZV. */
18504 if (it->glyph_row->ends_at_zv_p)
18505 it->face_id = default_face->id;
18506 else
18507 it->face_id = face->id;
18509 PRODUCE_GLYPHS (it);
18511 while (it->current_x <= it->last_visible_x)
18512 PRODUCE_GLYPHS (it);
18514 /* Don't count these blanks really. It would let us insert a left
18515 truncation glyph below and make us set the cursor on them, maybe. */
18516 it->current_x = saved_x;
18517 it->object = saved_object;
18518 it->position = saved_pos;
18519 it->what = saved_what;
18520 it->face_id = saved_face_id;
18525 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18526 trailing whitespace. */
18528 static int
18529 trailing_whitespace_p (EMACS_INT charpos)
18531 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18532 int c = 0;
18534 while (bytepos < ZV_BYTE
18535 && (c = FETCH_CHAR (bytepos),
18536 c == ' ' || c == '\t'))
18537 ++bytepos;
18539 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18541 if (bytepos != PT_BYTE)
18542 return 1;
18544 return 0;
18548 /* Highlight trailing whitespace, if any, in ROW. */
18550 static void
18551 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18553 int used = row->used[TEXT_AREA];
18555 if (used)
18557 struct glyph *start = row->glyphs[TEXT_AREA];
18558 struct glyph *glyph = start + used - 1;
18560 if (row->reversed_p)
18562 /* Right-to-left rows need to be processed in the opposite
18563 direction, so swap the edge pointers. */
18564 glyph = start;
18565 start = row->glyphs[TEXT_AREA] + used - 1;
18568 /* Skip over glyphs inserted to display the cursor at the
18569 end of a line, for extending the face of the last glyph
18570 to the end of the line on terminals, and for truncation
18571 and continuation glyphs. */
18572 if (!row->reversed_p)
18574 while (glyph >= start
18575 && glyph->type == CHAR_GLYPH
18576 && INTEGERP (glyph->object))
18577 --glyph;
18579 else
18581 while (glyph <= start
18582 && glyph->type == CHAR_GLYPH
18583 && INTEGERP (glyph->object))
18584 ++glyph;
18587 /* If last glyph is a space or stretch, and it's trailing
18588 whitespace, set the face of all trailing whitespace glyphs in
18589 IT->glyph_row to `trailing-whitespace'. */
18590 if ((row->reversed_p ? glyph <= start : glyph >= start)
18591 && BUFFERP (glyph->object)
18592 && (glyph->type == STRETCH_GLYPH
18593 || (glyph->type == CHAR_GLYPH
18594 && glyph->u.ch == ' '))
18595 && trailing_whitespace_p (glyph->charpos))
18597 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18598 if (face_id < 0)
18599 return;
18601 if (!row->reversed_p)
18603 while (glyph >= start
18604 && BUFFERP (glyph->object)
18605 && (glyph->type == STRETCH_GLYPH
18606 || (glyph->type == CHAR_GLYPH
18607 && glyph->u.ch == ' ')))
18608 (glyph--)->face_id = face_id;
18610 else
18612 while (glyph <= start
18613 && BUFFERP (glyph->object)
18614 && (glyph->type == STRETCH_GLYPH
18615 || (glyph->type == CHAR_GLYPH
18616 && glyph->u.ch == ' ')))
18617 (glyph++)->face_id = face_id;
18624 /* Value is non-zero if glyph row ROW should be
18625 used to hold the cursor. */
18627 static int
18628 cursor_row_p (struct glyph_row *row)
18630 int result = 1;
18632 if (PT == CHARPOS (row->end.pos)
18633 || PT == MATRIX_ROW_END_CHARPOS (row))
18635 /* Suppose the row ends on a string.
18636 Unless the row is continued, that means it ends on a newline
18637 in the string. If it's anything other than a display string
18638 (e.g., a before-string from an overlay), we don't want the
18639 cursor there. (This heuristic seems to give the optimal
18640 behavior for the various types of multi-line strings.)
18641 One exception: if the string has `cursor' property on one of
18642 its characters, we _do_ want the cursor there. */
18643 if (CHARPOS (row->end.string_pos) >= 0)
18645 if (row->continued_p)
18646 result = 1;
18647 else
18649 /* Check for `display' property. */
18650 struct glyph *beg = row->glyphs[TEXT_AREA];
18651 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18652 struct glyph *glyph;
18654 result = 0;
18655 for (glyph = end; glyph >= beg; --glyph)
18656 if (STRINGP (glyph->object))
18658 Lisp_Object prop
18659 = Fget_char_property (make_number (PT),
18660 Qdisplay, Qnil);
18661 result =
18662 (!NILP (prop)
18663 && display_prop_string_p (prop, glyph->object));
18664 /* If there's a `cursor' property on one of the
18665 string's characters, this row is a cursor row,
18666 even though this is not a display string. */
18667 if (!result)
18669 Lisp_Object s = glyph->object;
18671 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18673 EMACS_INT gpos = glyph->charpos;
18675 if (!NILP (Fget_char_property (make_number (gpos),
18676 Qcursor, s)))
18678 result = 1;
18679 break;
18683 break;
18687 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18689 /* If the row ends in middle of a real character,
18690 and the line is continued, we want the cursor here.
18691 That's because CHARPOS (ROW->end.pos) would equal
18692 PT if PT is before the character. */
18693 if (!row->ends_in_ellipsis_p)
18694 result = row->continued_p;
18695 else
18696 /* If the row ends in an ellipsis, then
18697 CHARPOS (ROW->end.pos) will equal point after the
18698 invisible text. We want that position to be displayed
18699 after the ellipsis. */
18700 result = 0;
18702 /* If the row ends at ZV, display the cursor at the end of that
18703 row instead of at the start of the row below. */
18704 else if (row->ends_at_zv_p)
18705 result = 1;
18706 else
18707 result = 0;
18710 return result;
18715 /* Push the property PROP so that it will be rendered at the current
18716 position in IT. Return 1 if PROP was successfully pushed, 0
18717 otherwise. Called from handle_line_prefix to handle the
18718 `line-prefix' and `wrap-prefix' properties. */
18720 static int
18721 push_prefix_prop (struct it *it, Lisp_Object prop)
18723 struct text_pos pos =
18724 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18726 xassert (it->method == GET_FROM_BUFFER
18727 || it->method == GET_FROM_DISPLAY_VECTOR
18728 || it->method == GET_FROM_STRING);
18730 /* We need to save the current buffer/string position, so it will be
18731 restored by pop_it, because iterate_out_of_display_property
18732 depends on that being set correctly, but some situations leave
18733 it->position not yet set when this function is called. */
18734 push_it (it, &pos);
18736 if (STRINGP (prop))
18738 if (SCHARS (prop) == 0)
18740 pop_it (it);
18741 return 0;
18744 it->string = prop;
18745 it->string_from_prefix_prop_p = 1;
18746 it->multibyte_p = STRING_MULTIBYTE (it->string);
18747 it->current.overlay_string_index = -1;
18748 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18749 it->end_charpos = it->string_nchars = SCHARS (it->string);
18750 it->method = GET_FROM_STRING;
18751 it->stop_charpos = 0;
18752 it->prev_stop = 0;
18753 it->base_level_stop = 0;
18755 /* Force paragraph direction to be that of the parent
18756 buffer/string. */
18757 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18758 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18759 else
18760 it->paragraph_embedding = L2R;
18762 /* Set up the bidi iterator for this display string. */
18763 if (it->bidi_p)
18765 it->bidi_it.string.lstring = it->string;
18766 it->bidi_it.string.s = NULL;
18767 it->bidi_it.string.schars = it->end_charpos;
18768 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18769 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18770 it->bidi_it.string.unibyte = !it->multibyte_p;
18771 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18774 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18776 it->method = GET_FROM_STRETCH;
18777 it->object = prop;
18779 #ifdef HAVE_WINDOW_SYSTEM
18780 else if (IMAGEP (prop))
18782 it->what = IT_IMAGE;
18783 it->image_id = lookup_image (it->f, prop);
18784 it->method = GET_FROM_IMAGE;
18786 #endif /* HAVE_WINDOW_SYSTEM */
18787 else
18789 pop_it (it); /* bogus display property, give up */
18790 return 0;
18793 return 1;
18796 /* Return the character-property PROP at the current position in IT. */
18798 static Lisp_Object
18799 get_it_property (struct it *it, Lisp_Object prop)
18801 Lisp_Object position;
18803 if (STRINGP (it->object))
18804 position = make_number (IT_STRING_CHARPOS (*it));
18805 else if (BUFFERP (it->object))
18806 position = make_number (IT_CHARPOS (*it));
18807 else
18808 return Qnil;
18810 return Fget_char_property (position, prop, it->object);
18813 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18815 static void
18816 handle_line_prefix (struct it *it)
18818 Lisp_Object prefix;
18820 if (it->continuation_lines_width > 0)
18822 prefix = get_it_property (it, Qwrap_prefix);
18823 if (NILP (prefix))
18824 prefix = Vwrap_prefix;
18826 else
18828 prefix = get_it_property (it, Qline_prefix);
18829 if (NILP (prefix))
18830 prefix = Vline_prefix;
18832 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18834 /* If the prefix is wider than the window, and we try to wrap
18835 it, it would acquire its own wrap prefix, and so on till the
18836 iterator stack overflows. So, don't wrap the prefix. */
18837 it->line_wrap = TRUNCATE;
18838 it->avoid_cursor_p = 1;
18844 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18845 only for R2L lines from display_line and display_string, when they
18846 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18847 the line/string needs to be continued on the next glyph row. */
18848 static void
18849 unproduce_glyphs (struct it *it, int n)
18851 struct glyph *glyph, *end;
18853 xassert (it->glyph_row);
18854 xassert (it->glyph_row->reversed_p);
18855 xassert (it->area == TEXT_AREA);
18856 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18858 if (n > it->glyph_row->used[TEXT_AREA])
18859 n = it->glyph_row->used[TEXT_AREA];
18860 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18861 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18862 for ( ; glyph < end; glyph++)
18863 glyph[-n] = *glyph;
18866 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18867 and ROW->maxpos. */
18868 static void
18869 find_row_edges (struct it *it, struct glyph_row *row,
18870 EMACS_INT min_pos, EMACS_INT min_bpos,
18871 EMACS_INT max_pos, EMACS_INT max_bpos)
18873 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18874 lines' rows is implemented for bidi-reordered rows. */
18876 /* ROW->minpos is the value of min_pos, the minimal buffer position
18877 we have in ROW, or ROW->start.pos if that is smaller. */
18878 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18879 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18880 else
18881 /* We didn't find buffer positions smaller than ROW->start, or
18882 didn't find _any_ valid buffer positions in any of the glyphs,
18883 so we must trust the iterator's computed positions. */
18884 row->minpos = row->start.pos;
18885 if (max_pos <= 0)
18887 max_pos = CHARPOS (it->current.pos);
18888 max_bpos = BYTEPOS (it->current.pos);
18891 /* Here are the various use-cases for ending the row, and the
18892 corresponding values for ROW->maxpos:
18894 Line ends in a newline from buffer eol_pos + 1
18895 Line is continued from buffer max_pos + 1
18896 Line is truncated on right it->current.pos
18897 Line ends in a newline from string max_pos + 1(*)
18898 (*) + 1 only when line ends in a forward scan
18899 Line is continued from string max_pos
18900 Line is continued from display vector max_pos
18901 Line is entirely from a string min_pos == max_pos
18902 Line is entirely from a display vector min_pos == max_pos
18903 Line that ends at ZV ZV
18905 If you discover other use-cases, please add them here as
18906 appropriate. */
18907 if (row->ends_at_zv_p)
18908 row->maxpos = it->current.pos;
18909 else if (row->used[TEXT_AREA])
18911 int seen_this_string = 0;
18912 struct glyph_row *r1 = row - 1;
18914 /* Did we see the same display string on the previous row? */
18915 if (STRINGP (it->object)
18916 /* this is not the first row */
18917 && row > it->w->desired_matrix->rows
18918 /* previous row is not the header line */
18919 && !r1->mode_line_p
18920 /* previous row also ends in a newline from a string */
18921 && r1->ends_in_newline_from_string_p)
18923 struct glyph *start, *end;
18925 /* Search for the last glyph of the previous row that came
18926 from buffer or string. Depending on whether the row is
18927 L2R or R2L, we need to process it front to back or the
18928 other way round. */
18929 if (!r1->reversed_p)
18931 start = r1->glyphs[TEXT_AREA];
18932 end = start + r1->used[TEXT_AREA];
18933 /* Glyphs inserted by redisplay have an integer (zero)
18934 as their object. */
18935 while (end > start
18936 && INTEGERP ((end - 1)->object)
18937 && (end - 1)->charpos <= 0)
18938 --end;
18939 if (end > start)
18941 if (EQ ((end - 1)->object, it->object))
18942 seen_this_string = 1;
18944 else
18945 /* If all the glyphs of the previous row were inserted
18946 by redisplay, it means the previous row was
18947 produced from a single newline, which is only
18948 possible if that newline came from the same string
18949 as the one which produced this ROW. */
18950 seen_this_string = 1;
18952 else
18954 end = r1->glyphs[TEXT_AREA] - 1;
18955 start = end + r1->used[TEXT_AREA];
18956 while (end < start
18957 && INTEGERP ((end + 1)->object)
18958 && (end + 1)->charpos <= 0)
18959 ++end;
18960 if (end < start)
18962 if (EQ ((end + 1)->object, it->object))
18963 seen_this_string = 1;
18965 else
18966 seen_this_string = 1;
18969 /* Take note of each display string that covers a newline only
18970 once, the first time we see it. This is for when a display
18971 string includes more than one newline in it. */
18972 if (row->ends_in_newline_from_string_p && !seen_this_string)
18974 /* If we were scanning the buffer forward when we displayed
18975 the string, we want to account for at least one buffer
18976 position that belongs to this row (position covered by
18977 the display string), so that cursor positioning will
18978 consider this row as a candidate when point is at the end
18979 of the visual line represented by this row. This is not
18980 required when scanning back, because max_pos will already
18981 have a much larger value. */
18982 if (CHARPOS (row->end.pos) > max_pos)
18983 INC_BOTH (max_pos, max_bpos);
18984 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18986 else if (CHARPOS (it->eol_pos) > 0)
18987 SET_TEXT_POS (row->maxpos,
18988 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18989 else if (row->continued_p)
18991 /* If max_pos is different from IT's current position, it
18992 means IT->method does not belong to the display element
18993 at max_pos. However, it also means that the display
18994 element at max_pos was displayed in its entirety on this
18995 line, which is equivalent to saying that the next line
18996 starts at the next buffer position. */
18997 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18998 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18999 else
19001 INC_BOTH (max_pos, max_bpos);
19002 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19005 else if (row->truncated_on_right_p)
19006 /* display_line already called reseat_at_next_visible_line_start,
19007 which puts the iterator at the beginning of the next line, in
19008 the logical order. */
19009 row->maxpos = it->current.pos;
19010 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19011 /* A line that is entirely from a string/image/stretch... */
19012 row->maxpos = row->minpos;
19013 else
19014 abort ();
19016 else
19017 row->maxpos = it->current.pos;
19020 /* Construct the glyph row IT->glyph_row in the desired matrix of
19021 IT->w from text at the current position of IT. See dispextern.h
19022 for an overview of struct it. Value is non-zero if
19023 IT->glyph_row displays text, as opposed to a line displaying ZV
19024 only. */
19026 static int
19027 display_line (struct it *it)
19029 struct glyph_row *row = it->glyph_row;
19030 Lisp_Object overlay_arrow_string;
19031 struct it wrap_it;
19032 void *wrap_data = NULL;
19033 int may_wrap = 0, wrap_x IF_LINT (= 0);
19034 int wrap_row_used = -1;
19035 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19036 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19037 int wrap_row_extra_line_spacing IF_LINT (= 0);
19038 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19039 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19040 int cvpos;
19041 EMACS_INT min_pos = ZV + 1, max_pos = 0;
19042 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19044 /* We always start displaying at hpos zero even if hscrolled. */
19045 xassert (it->hpos == 0 && it->current_x == 0);
19047 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19048 >= it->w->desired_matrix->nrows)
19050 it->w->nrows_scale_factor++;
19051 fonts_changed_p = 1;
19052 return 0;
19055 /* Is IT->w showing the region? */
19056 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19058 /* Clear the result glyph row and enable it. */
19059 prepare_desired_row (row);
19061 row->y = it->current_y;
19062 row->start = it->start;
19063 row->continuation_lines_width = it->continuation_lines_width;
19064 row->displays_text_p = 1;
19065 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19066 it->starts_in_middle_of_char_p = 0;
19068 /* Arrange the overlays nicely for our purposes. Usually, we call
19069 display_line on only one line at a time, in which case this
19070 can't really hurt too much, or we call it on lines which appear
19071 one after another in the buffer, in which case all calls to
19072 recenter_overlay_lists but the first will be pretty cheap. */
19073 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19075 /* Move over display elements that are not visible because we are
19076 hscrolled. This may stop at an x-position < IT->first_visible_x
19077 if the first glyph is partially visible or if we hit a line end. */
19078 if (it->current_x < it->first_visible_x)
19080 this_line_min_pos = row->start.pos;
19081 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19082 MOVE_TO_POS | MOVE_TO_X);
19083 /* Record the smallest positions seen while we moved over
19084 display elements that are not visible. This is needed by
19085 redisplay_internal for optimizing the case where the cursor
19086 stays inside the same line. The rest of this function only
19087 considers positions that are actually displayed, so
19088 RECORD_MAX_MIN_POS will not otherwise record positions that
19089 are hscrolled to the left of the left edge of the window. */
19090 min_pos = CHARPOS (this_line_min_pos);
19091 min_bpos = BYTEPOS (this_line_min_pos);
19093 else
19095 /* We only do this when not calling `move_it_in_display_line_to'
19096 above, because move_it_in_display_line_to calls
19097 handle_line_prefix itself. */
19098 handle_line_prefix (it);
19101 /* Get the initial row height. This is either the height of the
19102 text hscrolled, if there is any, or zero. */
19103 row->ascent = it->max_ascent;
19104 row->height = it->max_ascent + it->max_descent;
19105 row->phys_ascent = it->max_phys_ascent;
19106 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19107 row->extra_line_spacing = it->max_extra_line_spacing;
19109 /* Utility macro to record max and min buffer positions seen until now. */
19110 #define RECORD_MAX_MIN_POS(IT) \
19111 do \
19113 int composition_p = !STRINGP ((IT)->string) \
19114 && ((IT)->what == IT_COMPOSITION); \
19115 EMACS_INT current_pos = \
19116 composition_p ? (IT)->cmp_it.charpos \
19117 : IT_CHARPOS (*(IT)); \
19118 EMACS_INT current_bpos = \
19119 composition_p ? CHAR_TO_BYTE (current_pos) \
19120 : IT_BYTEPOS (*(IT)); \
19121 if (current_pos < min_pos) \
19123 min_pos = current_pos; \
19124 min_bpos = current_bpos; \
19126 if (IT_CHARPOS (*it) > max_pos) \
19128 max_pos = IT_CHARPOS (*it); \
19129 max_bpos = IT_BYTEPOS (*it); \
19132 while (0)
19134 /* Loop generating characters. The loop is left with IT on the next
19135 character to display. */
19136 while (1)
19138 int n_glyphs_before, hpos_before, x_before;
19139 int x, nglyphs;
19140 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19142 /* Retrieve the next thing to display. Value is zero if end of
19143 buffer reached. */
19144 if (!get_next_display_element (it))
19146 /* Maybe add a space at the end of this line that is used to
19147 display the cursor there under X. Set the charpos of the
19148 first glyph of blank lines not corresponding to any text
19149 to -1. */
19150 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19151 row->exact_window_width_line_p = 1;
19152 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19153 || row->used[TEXT_AREA] == 0)
19155 row->glyphs[TEXT_AREA]->charpos = -1;
19156 row->displays_text_p = 0;
19158 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19159 && (!MINI_WINDOW_P (it->w)
19160 || (minibuf_level && EQ (it->window, minibuf_window))))
19161 row->indicate_empty_line_p = 1;
19164 it->continuation_lines_width = 0;
19165 row->ends_at_zv_p = 1;
19166 /* A row that displays right-to-left text must always have
19167 its last face extended all the way to the end of line,
19168 even if this row ends in ZV, because we still write to
19169 the screen left to right. We also need to extend the
19170 last face if the default face is remapped to some
19171 different face, otherwise the functions that clear
19172 portions of the screen will clear with the default face's
19173 background color. */
19174 if (row->reversed_p
19175 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19176 extend_face_to_end_of_line (it);
19177 break;
19180 /* Now, get the metrics of what we want to display. This also
19181 generates glyphs in `row' (which is IT->glyph_row). */
19182 n_glyphs_before = row->used[TEXT_AREA];
19183 x = it->current_x;
19185 /* Remember the line height so far in case the next element doesn't
19186 fit on the line. */
19187 if (it->line_wrap != TRUNCATE)
19189 ascent = it->max_ascent;
19190 descent = it->max_descent;
19191 phys_ascent = it->max_phys_ascent;
19192 phys_descent = it->max_phys_descent;
19194 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19196 if (IT_DISPLAYING_WHITESPACE (it))
19197 may_wrap = 1;
19198 else if (may_wrap)
19200 SAVE_IT (wrap_it, *it, wrap_data);
19201 wrap_x = x;
19202 wrap_row_used = row->used[TEXT_AREA];
19203 wrap_row_ascent = row->ascent;
19204 wrap_row_height = row->height;
19205 wrap_row_phys_ascent = row->phys_ascent;
19206 wrap_row_phys_height = row->phys_height;
19207 wrap_row_extra_line_spacing = row->extra_line_spacing;
19208 wrap_row_min_pos = min_pos;
19209 wrap_row_min_bpos = min_bpos;
19210 wrap_row_max_pos = max_pos;
19211 wrap_row_max_bpos = max_bpos;
19212 may_wrap = 0;
19217 PRODUCE_GLYPHS (it);
19219 /* If this display element was in marginal areas, continue with
19220 the next one. */
19221 if (it->area != TEXT_AREA)
19223 row->ascent = max (row->ascent, it->max_ascent);
19224 row->height = max (row->height, it->max_ascent + it->max_descent);
19225 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19226 row->phys_height = max (row->phys_height,
19227 it->max_phys_ascent + it->max_phys_descent);
19228 row->extra_line_spacing = max (row->extra_line_spacing,
19229 it->max_extra_line_spacing);
19230 set_iterator_to_next (it, 1);
19231 continue;
19234 /* Does the display element fit on the line? If we truncate
19235 lines, we should draw past the right edge of the window. If
19236 we don't truncate, we want to stop so that we can display the
19237 continuation glyph before the right margin. If lines are
19238 continued, there are two possible strategies for characters
19239 resulting in more than 1 glyph (e.g. tabs): Display as many
19240 glyphs as possible in this line and leave the rest for the
19241 continuation line, or display the whole element in the next
19242 line. Original redisplay did the former, so we do it also. */
19243 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19244 hpos_before = it->hpos;
19245 x_before = x;
19247 if (/* Not a newline. */
19248 nglyphs > 0
19249 /* Glyphs produced fit entirely in the line. */
19250 && it->current_x < it->last_visible_x)
19252 it->hpos += nglyphs;
19253 row->ascent = max (row->ascent, it->max_ascent);
19254 row->height = max (row->height, it->max_ascent + it->max_descent);
19255 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19256 row->phys_height = max (row->phys_height,
19257 it->max_phys_ascent + it->max_phys_descent);
19258 row->extra_line_spacing = max (row->extra_line_spacing,
19259 it->max_extra_line_spacing);
19260 if (it->current_x - it->pixel_width < it->first_visible_x)
19261 row->x = x - it->first_visible_x;
19262 /* Record the maximum and minimum buffer positions seen so
19263 far in glyphs that will be displayed by this row. */
19264 if (it->bidi_p)
19265 RECORD_MAX_MIN_POS (it);
19267 else
19269 int i, new_x;
19270 struct glyph *glyph;
19272 for (i = 0; i < nglyphs; ++i, x = new_x)
19274 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19275 new_x = x + glyph->pixel_width;
19277 if (/* Lines are continued. */
19278 it->line_wrap != TRUNCATE
19279 && (/* Glyph doesn't fit on the line. */
19280 new_x > it->last_visible_x
19281 /* Or it fits exactly on a window system frame. */
19282 || (new_x == it->last_visible_x
19283 && FRAME_WINDOW_P (it->f))))
19285 /* End of a continued line. */
19287 if (it->hpos == 0
19288 || (new_x == it->last_visible_x
19289 && FRAME_WINDOW_P (it->f)))
19291 /* Current glyph is the only one on the line or
19292 fits exactly on the line. We must continue
19293 the line because we can't draw the cursor
19294 after the glyph. */
19295 row->continued_p = 1;
19296 it->current_x = new_x;
19297 it->continuation_lines_width += new_x;
19298 ++it->hpos;
19299 if (i == nglyphs - 1)
19301 /* If line-wrap is on, check if a previous
19302 wrap point was found. */
19303 if (wrap_row_used > 0
19304 /* Even if there is a previous wrap
19305 point, continue the line here as
19306 usual, if (i) the previous character
19307 was a space or tab AND (ii) the
19308 current character is not. */
19309 && (!may_wrap
19310 || IT_DISPLAYING_WHITESPACE (it)))
19311 goto back_to_wrap;
19313 /* Record the maximum and minimum buffer
19314 positions seen so far in glyphs that will be
19315 displayed by this row. */
19316 if (it->bidi_p)
19317 RECORD_MAX_MIN_POS (it);
19318 set_iterator_to_next (it, 1);
19319 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19321 if (!get_next_display_element (it))
19323 row->exact_window_width_line_p = 1;
19324 it->continuation_lines_width = 0;
19325 row->continued_p = 0;
19326 row->ends_at_zv_p = 1;
19328 else if (ITERATOR_AT_END_OF_LINE_P (it))
19330 row->continued_p = 0;
19331 row->exact_window_width_line_p = 1;
19335 else if (it->bidi_p)
19336 RECORD_MAX_MIN_POS (it);
19338 else if (CHAR_GLYPH_PADDING_P (*glyph)
19339 && !FRAME_WINDOW_P (it->f))
19341 /* A padding glyph that doesn't fit on this line.
19342 This means the whole character doesn't fit
19343 on the line. */
19344 if (row->reversed_p)
19345 unproduce_glyphs (it, row->used[TEXT_AREA]
19346 - n_glyphs_before);
19347 row->used[TEXT_AREA] = n_glyphs_before;
19349 /* Fill the rest of the row with continuation
19350 glyphs like in 20.x. */
19351 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19352 < row->glyphs[1 + TEXT_AREA])
19353 produce_special_glyphs (it, IT_CONTINUATION);
19355 row->continued_p = 1;
19356 it->current_x = x_before;
19357 it->continuation_lines_width += x_before;
19359 /* Restore the height to what it was before the
19360 element not fitting on the line. */
19361 it->max_ascent = ascent;
19362 it->max_descent = descent;
19363 it->max_phys_ascent = phys_ascent;
19364 it->max_phys_descent = phys_descent;
19366 else if (wrap_row_used > 0)
19368 back_to_wrap:
19369 if (row->reversed_p)
19370 unproduce_glyphs (it,
19371 row->used[TEXT_AREA] - wrap_row_used);
19372 RESTORE_IT (it, &wrap_it, wrap_data);
19373 it->continuation_lines_width += wrap_x;
19374 row->used[TEXT_AREA] = wrap_row_used;
19375 row->ascent = wrap_row_ascent;
19376 row->height = wrap_row_height;
19377 row->phys_ascent = wrap_row_phys_ascent;
19378 row->phys_height = wrap_row_phys_height;
19379 row->extra_line_spacing = wrap_row_extra_line_spacing;
19380 min_pos = wrap_row_min_pos;
19381 min_bpos = wrap_row_min_bpos;
19382 max_pos = wrap_row_max_pos;
19383 max_bpos = wrap_row_max_bpos;
19384 row->continued_p = 1;
19385 row->ends_at_zv_p = 0;
19386 row->exact_window_width_line_p = 0;
19387 it->continuation_lines_width += x;
19389 /* Make sure that a non-default face is extended
19390 up to the right margin of the window. */
19391 extend_face_to_end_of_line (it);
19393 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19395 /* A TAB that extends past the right edge of the
19396 window. This produces a single glyph on
19397 window system frames. We leave the glyph in
19398 this row and let it fill the row, but don't
19399 consume the TAB. */
19400 it->continuation_lines_width += it->last_visible_x;
19401 row->ends_in_middle_of_char_p = 1;
19402 row->continued_p = 1;
19403 glyph->pixel_width = it->last_visible_x - x;
19404 it->starts_in_middle_of_char_p = 1;
19406 else
19408 /* Something other than a TAB that draws past
19409 the right edge of the window. Restore
19410 positions to values before the element. */
19411 if (row->reversed_p)
19412 unproduce_glyphs (it, row->used[TEXT_AREA]
19413 - (n_glyphs_before + i));
19414 row->used[TEXT_AREA] = n_glyphs_before + i;
19416 /* Display continuation glyphs. */
19417 if (!FRAME_WINDOW_P (it->f))
19418 produce_special_glyphs (it, IT_CONTINUATION);
19419 row->continued_p = 1;
19421 it->current_x = x_before;
19422 it->continuation_lines_width += x;
19423 extend_face_to_end_of_line (it);
19425 if (nglyphs > 1 && i > 0)
19427 row->ends_in_middle_of_char_p = 1;
19428 it->starts_in_middle_of_char_p = 1;
19431 /* Restore the height to what it was before the
19432 element not fitting on the line. */
19433 it->max_ascent = ascent;
19434 it->max_descent = descent;
19435 it->max_phys_ascent = phys_ascent;
19436 it->max_phys_descent = phys_descent;
19439 break;
19441 else if (new_x > it->first_visible_x)
19443 /* Increment number of glyphs actually displayed. */
19444 ++it->hpos;
19446 /* Record the maximum and minimum buffer positions
19447 seen so far in glyphs that will be displayed by
19448 this row. */
19449 if (it->bidi_p)
19450 RECORD_MAX_MIN_POS (it);
19452 if (x < it->first_visible_x)
19453 /* Glyph is partially visible, i.e. row starts at
19454 negative X position. */
19455 row->x = x - it->first_visible_x;
19457 else
19459 /* Glyph is completely off the left margin of the
19460 window. This should not happen because of the
19461 move_it_in_display_line at the start of this
19462 function, unless the text display area of the
19463 window is empty. */
19464 xassert (it->first_visible_x <= it->last_visible_x);
19467 /* Even if this display element produced no glyphs at all,
19468 we want to record its position. */
19469 if (it->bidi_p && nglyphs == 0)
19470 RECORD_MAX_MIN_POS (it);
19472 row->ascent = max (row->ascent, it->max_ascent);
19473 row->height = max (row->height, it->max_ascent + it->max_descent);
19474 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19475 row->phys_height = max (row->phys_height,
19476 it->max_phys_ascent + it->max_phys_descent);
19477 row->extra_line_spacing = max (row->extra_line_spacing,
19478 it->max_extra_line_spacing);
19480 /* End of this display line if row is continued. */
19481 if (row->continued_p || row->ends_at_zv_p)
19482 break;
19485 at_end_of_line:
19486 /* Is this a line end? If yes, we're also done, after making
19487 sure that a non-default face is extended up to the right
19488 margin of the window. */
19489 if (ITERATOR_AT_END_OF_LINE_P (it))
19491 int used_before = row->used[TEXT_AREA];
19493 row->ends_in_newline_from_string_p = STRINGP (it->object);
19495 /* Add a space at the end of the line that is used to
19496 display the cursor there. */
19497 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19498 append_space_for_newline (it, 0);
19500 /* Extend the face to the end of the line. */
19501 extend_face_to_end_of_line (it);
19503 /* Make sure we have the position. */
19504 if (used_before == 0)
19505 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19507 /* Record the position of the newline, for use in
19508 find_row_edges. */
19509 it->eol_pos = it->current.pos;
19511 /* Consume the line end. This skips over invisible lines. */
19512 set_iterator_to_next (it, 1);
19513 it->continuation_lines_width = 0;
19514 break;
19517 /* Proceed with next display element. Note that this skips
19518 over lines invisible because of selective display. */
19519 set_iterator_to_next (it, 1);
19521 /* If we truncate lines, we are done when the last displayed
19522 glyphs reach past the right margin of the window. */
19523 if (it->line_wrap == TRUNCATE
19524 && (FRAME_WINDOW_P (it->f)
19525 ? (it->current_x >= it->last_visible_x)
19526 : (it->current_x > it->last_visible_x)))
19528 /* Maybe add truncation glyphs. */
19529 if (!FRAME_WINDOW_P (it->f))
19531 int i, n;
19533 if (!row->reversed_p)
19535 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19536 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19537 break;
19539 else
19541 for (i = 0; i < row->used[TEXT_AREA]; i++)
19542 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19543 break;
19544 /* Remove any padding glyphs at the front of ROW, to
19545 make room for the truncation glyphs we will be
19546 adding below. The loop below always inserts at
19547 least one truncation glyph, so also remove the
19548 last glyph added to ROW. */
19549 unproduce_glyphs (it, i + 1);
19550 /* Adjust i for the loop below. */
19551 i = row->used[TEXT_AREA] - (i + 1);
19554 for (n = row->used[TEXT_AREA]; i < n; ++i)
19556 row->used[TEXT_AREA] = i;
19557 produce_special_glyphs (it, IT_TRUNCATION);
19560 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19562 /* Don't truncate if we can overflow newline into fringe. */
19563 if (!get_next_display_element (it))
19565 it->continuation_lines_width = 0;
19566 row->ends_at_zv_p = 1;
19567 row->exact_window_width_line_p = 1;
19568 break;
19570 if (ITERATOR_AT_END_OF_LINE_P (it))
19572 row->exact_window_width_line_p = 1;
19573 goto at_end_of_line;
19577 row->truncated_on_right_p = 1;
19578 it->continuation_lines_width = 0;
19579 reseat_at_next_visible_line_start (it, 0);
19580 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19581 it->hpos = hpos_before;
19582 it->current_x = x_before;
19583 break;
19587 if (wrap_data)
19588 bidi_unshelve_cache (wrap_data, 1);
19590 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19591 at the left window margin. */
19592 if (it->first_visible_x
19593 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19595 if (!FRAME_WINDOW_P (it->f))
19596 insert_left_trunc_glyphs (it);
19597 row->truncated_on_left_p = 1;
19600 /* Remember the position at which this line ends.
19602 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19603 cannot be before the call to find_row_edges below, since that is
19604 where these positions are determined. */
19605 row->end = it->current;
19606 if (!it->bidi_p)
19608 row->minpos = row->start.pos;
19609 row->maxpos = row->end.pos;
19611 else
19613 /* ROW->minpos and ROW->maxpos must be the smallest and
19614 `1 + the largest' buffer positions in ROW. But if ROW was
19615 bidi-reordered, these two positions can be anywhere in the
19616 row, so we must determine them now. */
19617 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19620 /* If the start of this line is the overlay arrow-position, then
19621 mark this glyph row as the one containing the overlay arrow.
19622 This is clearly a mess with variable size fonts. It would be
19623 better to let it be displayed like cursors under X. */
19624 if ((row->displays_text_p || !overlay_arrow_seen)
19625 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19626 !NILP (overlay_arrow_string)))
19628 /* Overlay arrow in window redisplay is a fringe bitmap. */
19629 if (STRINGP (overlay_arrow_string))
19631 struct glyph_row *arrow_row
19632 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19633 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19634 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19635 struct glyph *p = row->glyphs[TEXT_AREA];
19636 struct glyph *p2, *end;
19638 /* Copy the arrow glyphs. */
19639 while (glyph < arrow_end)
19640 *p++ = *glyph++;
19642 /* Throw away padding glyphs. */
19643 p2 = p;
19644 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19645 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19646 ++p2;
19647 if (p2 > p)
19649 while (p2 < end)
19650 *p++ = *p2++;
19651 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19654 else
19656 xassert (INTEGERP (overlay_arrow_string));
19657 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19659 overlay_arrow_seen = 1;
19662 /* Highlight trailing whitespace. */
19663 if (!NILP (Vshow_trailing_whitespace))
19664 highlight_trailing_whitespace (it->f, it->glyph_row);
19666 /* Compute pixel dimensions of this line. */
19667 compute_line_metrics (it);
19669 /* Implementation note: No changes in the glyphs of ROW or in their
19670 faces can be done past this point, because compute_line_metrics
19671 computes ROW's hash value and stores it within the glyph_row
19672 structure. */
19674 /* Record whether this row ends inside an ellipsis. */
19675 row->ends_in_ellipsis_p
19676 = (it->method == GET_FROM_DISPLAY_VECTOR
19677 && it->ellipsis_p);
19679 /* Save fringe bitmaps in this row. */
19680 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19681 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19682 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19683 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19685 it->left_user_fringe_bitmap = 0;
19686 it->left_user_fringe_face_id = 0;
19687 it->right_user_fringe_bitmap = 0;
19688 it->right_user_fringe_face_id = 0;
19690 /* Maybe set the cursor. */
19691 cvpos = it->w->cursor.vpos;
19692 if ((cvpos < 0
19693 /* In bidi-reordered rows, keep checking for proper cursor
19694 position even if one has been found already, because buffer
19695 positions in such rows change non-linearly with ROW->VPOS,
19696 when a line is continued. One exception: when we are at ZV,
19697 display cursor on the first suitable glyph row, since all
19698 the empty rows after that also have their position set to ZV. */
19699 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19700 lines' rows is implemented for bidi-reordered rows. */
19701 || (it->bidi_p
19702 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19703 && PT >= MATRIX_ROW_START_CHARPOS (row)
19704 && PT <= MATRIX_ROW_END_CHARPOS (row)
19705 && cursor_row_p (row))
19706 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19708 /* Prepare for the next line. This line starts horizontally at (X
19709 HPOS) = (0 0). Vertical positions are incremented. As a
19710 convenience for the caller, IT->glyph_row is set to the next
19711 row to be used. */
19712 it->current_x = it->hpos = 0;
19713 it->current_y += row->height;
19714 SET_TEXT_POS (it->eol_pos, 0, 0);
19715 ++it->vpos;
19716 ++it->glyph_row;
19717 /* The next row should by default use the same value of the
19718 reversed_p flag as this one. set_iterator_to_next decides when
19719 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19720 the flag accordingly. */
19721 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19722 it->glyph_row->reversed_p = row->reversed_p;
19723 it->start = row->end;
19724 return row->displays_text_p;
19726 #undef RECORD_MAX_MIN_POS
19729 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19730 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19731 doc: /* Return paragraph direction at point in BUFFER.
19732 Value is either `left-to-right' or `right-to-left'.
19733 If BUFFER is omitted or nil, it defaults to the current buffer.
19735 Paragraph direction determines how the text in the paragraph is displayed.
19736 In left-to-right paragraphs, text begins at the left margin of the window
19737 and the reading direction is generally left to right. In right-to-left
19738 paragraphs, text begins at the right margin and is read from right to left.
19740 See also `bidi-paragraph-direction'. */)
19741 (Lisp_Object buffer)
19743 struct buffer *buf = current_buffer;
19744 struct buffer *old = buf;
19746 if (! NILP (buffer))
19748 CHECK_BUFFER (buffer);
19749 buf = XBUFFER (buffer);
19752 if (NILP (BVAR (buf, bidi_display_reordering))
19753 || NILP (BVAR (buf, enable_multibyte_characters))
19754 /* When we are loading loadup.el, the character property tables
19755 needed for bidi iteration are not yet available. */
19756 || !NILP (Vpurify_flag))
19757 return Qleft_to_right;
19758 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19759 return BVAR (buf, bidi_paragraph_direction);
19760 else
19762 /* Determine the direction from buffer text. We could try to
19763 use current_matrix if it is up to date, but this seems fast
19764 enough as it is. */
19765 struct bidi_it itb;
19766 EMACS_INT pos = BUF_PT (buf);
19767 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19768 int c;
19769 void *itb_data = bidi_shelve_cache ();
19771 set_buffer_temp (buf);
19772 /* bidi_paragraph_init finds the base direction of the paragraph
19773 by searching forward from paragraph start. We need the base
19774 direction of the current or _previous_ paragraph, so we need
19775 to make sure we are within that paragraph. To that end, find
19776 the previous non-empty line. */
19777 if (pos >= ZV && pos > BEGV)
19779 pos--;
19780 bytepos = CHAR_TO_BYTE (pos);
19782 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19783 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19785 while ((c = FETCH_BYTE (bytepos)) == '\n'
19786 || c == ' ' || c == '\t' || c == '\f')
19788 if (bytepos <= BEGV_BYTE)
19789 break;
19790 bytepos--;
19791 pos--;
19793 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19794 bytepos--;
19796 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19797 itb.paragraph_dir = NEUTRAL_DIR;
19798 itb.string.s = NULL;
19799 itb.string.lstring = Qnil;
19800 itb.string.bufpos = 0;
19801 itb.string.unibyte = 0;
19802 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19803 bidi_unshelve_cache (itb_data, 0);
19804 set_buffer_temp (old);
19805 switch (itb.paragraph_dir)
19807 case L2R:
19808 return Qleft_to_right;
19809 break;
19810 case R2L:
19811 return Qright_to_left;
19812 break;
19813 default:
19814 abort ();
19821 /***********************************************************************
19822 Menu Bar
19823 ***********************************************************************/
19825 /* Redisplay the menu bar in the frame for window W.
19827 The menu bar of X frames that don't have X toolkit support is
19828 displayed in a special window W->frame->menu_bar_window.
19830 The menu bar of terminal frames is treated specially as far as
19831 glyph matrices are concerned. Menu bar lines are not part of
19832 windows, so the update is done directly on the frame matrix rows
19833 for the menu bar. */
19835 static void
19836 display_menu_bar (struct window *w)
19838 struct frame *f = XFRAME (WINDOW_FRAME (w));
19839 struct it it;
19840 Lisp_Object items;
19841 int i;
19843 /* Don't do all this for graphical frames. */
19844 #ifdef HAVE_NTGUI
19845 if (FRAME_W32_P (f))
19846 return;
19847 #endif
19848 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19849 if (FRAME_X_P (f))
19850 return;
19851 #endif
19853 #ifdef HAVE_NS
19854 if (FRAME_NS_P (f))
19855 return;
19856 #endif /* HAVE_NS */
19858 #ifdef USE_X_TOOLKIT
19859 xassert (!FRAME_WINDOW_P (f));
19860 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19861 it.first_visible_x = 0;
19862 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19863 #else /* not USE_X_TOOLKIT */
19864 if (FRAME_WINDOW_P (f))
19866 /* Menu bar lines are displayed in the desired matrix of the
19867 dummy window menu_bar_window. */
19868 struct window *menu_w;
19869 xassert (WINDOWP (f->menu_bar_window));
19870 menu_w = XWINDOW (f->menu_bar_window);
19871 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19872 MENU_FACE_ID);
19873 it.first_visible_x = 0;
19874 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19876 else
19878 /* This is a TTY frame, i.e. character hpos/vpos are used as
19879 pixel x/y. */
19880 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19881 MENU_FACE_ID);
19882 it.first_visible_x = 0;
19883 it.last_visible_x = FRAME_COLS (f);
19885 #endif /* not USE_X_TOOLKIT */
19887 /* FIXME: This should be controlled by a user option. See the
19888 comments in redisplay_tool_bar and display_mode_line about
19889 this. */
19890 it.paragraph_embedding = L2R;
19892 if (! mode_line_inverse_video)
19893 /* Force the menu-bar to be displayed in the default face. */
19894 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19896 /* Clear all rows of the menu bar. */
19897 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19899 struct glyph_row *row = it.glyph_row + i;
19900 clear_glyph_row (row);
19901 row->enabled_p = 1;
19902 row->full_width_p = 1;
19905 /* Display all items of the menu bar. */
19906 items = FRAME_MENU_BAR_ITEMS (it.f);
19907 for (i = 0; i < ASIZE (items); i += 4)
19909 Lisp_Object string;
19911 /* Stop at nil string. */
19912 string = AREF (items, i + 1);
19913 if (NILP (string))
19914 break;
19916 /* Remember where item was displayed. */
19917 ASET (items, i + 3, make_number (it.hpos));
19919 /* Display the item, pad with one space. */
19920 if (it.current_x < it.last_visible_x)
19921 display_string (NULL, string, Qnil, 0, 0, &it,
19922 SCHARS (string) + 1, 0, 0, -1);
19925 /* Fill out the line with spaces. */
19926 if (it.current_x < it.last_visible_x)
19927 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19929 /* Compute the total height of the lines. */
19930 compute_line_metrics (&it);
19935 /***********************************************************************
19936 Mode Line
19937 ***********************************************************************/
19939 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19940 FORCE is non-zero, redisplay mode lines unconditionally.
19941 Otherwise, redisplay only mode lines that are garbaged. Value is
19942 the number of windows whose mode lines were redisplayed. */
19944 static int
19945 redisplay_mode_lines (Lisp_Object window, int force)
19947 int nwindows = 0;
19949 while (!NILP (window))
19951 struct window *w = XWINDOW (window);
19953 if (WINDOWP (w->hchild))
19954 nwindows += redisplay_mode_lines (w->hchild, force);
19955 else if (WINDOWP (w->vchild))
19956 nwindows += redisplay_mode_lines (w->vchild, force);
19957 else if (force
19958 || FRAME_GARBAGED_P (XFRAME (w->frame))
19959 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19961 struct text_pos lpoint;
19962 struct buffer *old = current_buffer;
19964 /* Set the window's buffer for the mode line display. */
19965 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19966 set_buffer_internal_1 (XBUFFER (w->buffer));
19968 /* Point refers normally to the selected window. For any
19969 other window, set up appropriate value. */
19970 if (!EQ (window, selected_window))
19972 struct text_pos pt;
19974 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19975 if (CHARPOS (pt) < BEGV)
19976 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19977 else if (CHARPOS (pt) > (ZV - 1))
19978 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19979 else
19980 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19983 /* Display mode lines. */
19984 clear_glyph_matrix (w->desired_matrix);
19985 if (display_mode_lines (w))
19987 ++nwindows;
19988 w->must_be_updated_p = 1;
19991 /* Restore old settings. */
19992 set_buffer_internal_1 (old);
19993 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19996 window = w->next;
19999 return nwindows;
20003 /* Display the mode and/or header line of window W. Value is the
20004 sum number of mode lines and header lines displayed. */
20006 static int
20007 display_mode_lines (struct window *w)
20009 Lisp_Object old_selected_window, old_selected_frame;
20010 int n = 0;
20012 old_selected_frame = selected_frame;
20013 selected_frame = w->frame;
20014 old_selected_window = selected_window;
20015 XSETWINDOW (selected_window, w);
20017 /* These will be set while the mode line specs are processed. */
20018 line_number_displayed = 0;
20019 w->column_number_displayed = Qnil;
20021 if (WINDOW_WANTS_MODELINE_P (w))
20023 struct window *sel_w = XWINDOW (old_selected_window);
20025 /* Select mode line face based on the real selected window. */
20026 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20027 BVAR (current_buffer, mode_line_format));
20028 ++n;
20031 if (WINDOW_WANTS_HEADER_LINE_P (w))
20033 display_mode_line (w, HEADER_LINE_FACE_ID,
20034 BVAR (current_buffer, header_line_format));
20035 ++n;
20038 selected_frame = old_selected_frame;
20039 selected_window = old_selected_window;
20040 return n;
20044 /* Display mode or header line of window W. FACE_ID specifies which
20045 line to display; it is either MODE_LINE_FACE_ID or
20046 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20047 display. Value is the pixel height of the mode/header line
20048 displayed. */
20050 static int
20051 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20053 struct it it;
20054 struct face *face;
20055 int count = SPECPDL_INDEX ();
20057 init_iterator (&it, w, -1, -1, NULL, face_id);
20058 /* Don't extend on a previously drawn mode-line.
20059 This may happen if called from pos_visible_p. */
20060 it.glyph_row->enabled_p = 0;
20061 prepare_desired_row (it.glyph_row);
20063 it.glyph_row->mode_line_p = 1;
20065 if (! mode_line_inverse_video)
20066 /* Force the mode-line to be displayed in the default face. */
20067 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20069 /* FIXME: This should be controlled by a user option. But
20070 supporting such an option is not trivial, since the mode line is
20071 made up of many separate strings. */
20072 it.paragraph_embedding = L2R;
20074 record_unwind_protect (unwind_format_mode_line,
20075 format_mode_line_unwind_data (NULL, Qnil, 0));
20077 mode_line_target = MODE_LINE_DISPLAY;
20079 /* Temporarily make frame's keyboard the current kboard so that
20080 kboard-local variables in the mode_line_format will get the right
20081 values. */
20082 push_kboard (FRAME_KBOARD (it.f));
20083 record_unwind_save_match_data ();
20084 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20085 pop_kboard ();
20087 unbind_to (count, Qnil);
20089 /* Fill up with spaces. */
20090 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20092 compute_line_metrics (&it);
20093 it.glyph_row->full_width_p = 1;
20094 it.glyph_row->continued_p = 0;
20095 it.glyph_row->truncated_on_left_p = 0;
20096 it.glyph_row->truncated_on_right_p = 0;
20098 /* Make a 3D mode-line have a shadow at its right end. */
20099 face = FACE_FROM_ID (it.f, face_id);
20100 extend_face_to_end_of_line (&it);
20101 if (face->box != FACE_NO_BOX)
20103 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20104 + it.glyph_row->used[TEXT_AREA] - 1);
20105 last->right_box_line_p = 1;
20108 return it.glyph_row->height;
20111 /* Move element ELT in LIST to the front of LIST.
20112 Return the updated list. */
20114 static Lisp_Object
20115 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20117 register Lisp_Object tail, prev;
20118 register Lisp_Object tem;
20120 tail = list;
20121 prev = Qnil;
20122 while (CONSP (tail))
20124 tem = XCAR (tail);
20126 if (EQ (elt, tem))
20128 /* Splice out the link TAIL. */
20129 if (NILP (prev))
20130 list = XCDR (tail);
20131 else
20132 Fsetcdr (prev, XCDR (tail));
20134 /* Now make it the first. */
20135 Fsetcdr (tail, list);
20136 return tail;
20138 else
20139 prev = tail;
20140 tail = XCDR (tail);
20141 QUIT;
20144 /* Not found--return unchanged LIST. */
20145 return list;
20148 /* Contribute ELT to the mode line for window IT->w. How it
20149 translates into text depends on its data type.
20151 IT describes the display environment in which we display, as usual.
20153 DEPTH is the depth in recursion. It is used to prevent
20154 infinite recursion here.
20156 FIELD_WIDTH is the number of characters the display of ELT should
20157 occupy in the mode line, and PRECISION is the maximum number of
20158 characters to display from ELT's representation. See
20159 display_string for details.
20161 Returns the hpos of the end of the text generated by ELT.
20163 PROPS is a property list to add to any string we encounter.
20165 If RISKY is nonzero, remove (disregard) any properties in any string
20166 we encounter, and ignore :eval and :propertize.
20168 The global variable `mode_line_target' determines whether the
20169 output is passed to `store_mode_line_noprop',
20170 `store_mode_line_string', or `display_string'. */
20172 static int
20173 display_mode_element (struct it *it, int depth, int field_width, int precision,
20174 Lisp_Object elt, Lisp_Object props, int risky)
20176 int n = 0, field, prec;
20177 int literal = 0;
20179 tail_recurse:
20180 if (depth > 100)
20181 elt = build_string ("*too-deep*");
20183 depth++;
20185 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20187 case Lisp_String:
20189 /* A string: output it and check for %-constructs within it. */
20190 unsigned char c;
20191 EMACS_INT offset = 0;
20193 if (SCHARS (elt) > 0
20194 && (!NILP (props) || risky))
20196 Lisp_Object oprops, aelt;
20197 oprops = Ftext_properties_at (make_number (0), elt);
20199 /* If the starting string's properties are not what
20200 we want, translate the string. Also, if the string
20201 is risky, do that anyway. */
20203 if (NILP (Fequal (props, oprops)) || risky)
20205 /* If the starting string has properties,
20206 merge the specified ones onto the existing ones. */
20207 if (! NILP (oprops) && !risky)
20209 Lisp_Object tem;
20211 oprops = Fcopy_sequence (oprops);
20212 tem = props;
20213 while (CONSP (tem))
20215 oprops = Fplist_put (oprops, XCAR (tem),
20216 XCAR (XCDR (tem)));
20217 tem = XCDR (XCDR (tem));
20219 props = oprops;
20222 aelt = Fassoc (elt, mode_line_proptrans_alist);
20223 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20225 /* AELT is what we want. Move it to the front
20226 without consing. */
20227 elt = XCAR (aelt);
20228 mode_line_proptrans_alist
20229 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20231 else
20233 Lisp_Object tem;
20235 /* If AELT has the wrong props, it is useless.
20236 so get rid of it. */
20237 if (! NILP (aelt))
20238 mode_line_proptrans_alist
20239 = Fdelq (aelt, mode_line_proptrans_alist);
20241 elt = Fcopy_sequence (elt);
20242 Fset_text_properties (make_number (0), Flength (elt),
20243 props, elt);
20244 /* Add this item to mode_line_proptrans_alist. */
20245 mode_line_proptrans_alist
20246 = Fcons (Fcons (elt, props),
20247 mode_line_proptrans_alist);
20248 /* Truncate mode_line_proptrans_alist
20249 to at most 50 elements. */
20250 tem = Fnthcdr (make_number (50),
20251 mode_line_proptrans_alist);
20252 if (! NILP (tem))
20253 XSETCDR (tem, Qnil);
20258 offset = 0;
20260 if (literal)
20262 prec = precision - n;
20263 switch (mode_line_target)
20265 case MODE_LINE_NOPROP:
20266 case MODE_LINE_TITLE:
20267 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20268 break;
20269 case MODE_LINE_STRING:
20270 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20271 break;
20272 case MODE_LINE_DISPLAY:
20273 n += display_string (NULL, elt, Qnil, 0, 0, it,
20274 0, prec, 0, STRING_MULTIBYTE (elt));
20275 break;
20278 break;
20281 /* Handle the non-literal case. */
20283 while ((precision <= 0 || n < precision)
20284 && SREF (elt, offset) != 0
20285 && (mode_line_target != MODE_LINE_DISPLAY
20286 || it->current_x < it->last_visible_x))
20288 EMACS_INT last_offset = offset;
20290 /* Advance to end of string or next format specifier. */
20291 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20294 if (offset - 1 != last_offset)
20296 EMACS_INT nchars, nbytes;
20298 /* Output to end of string or up to '%'. Field width
20299 is length of string. Don't output more than
20300 PRECISION allows us. */
20301 offset--;
20303 prec = c_string_width (SDATA (elt) + last_offset,
20304 offset - last_offset, precision - n,
20305 &nchars, &nbytes);
20307 switch (mode_line_target)
20309 case MODE_LINE_NOPROP:
20310 case MODE_LINE_TITLE:
20311 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20312 break;
20313 case MODE_LINE_STRING:
20315 EMACS_INT bytepos = last_offset;
20316 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20317 EMACS_INT endpos = (precision <= 0
20318 ? string_byte_to_char (elt, offset)
20319 : charpos + nchars);
20321 n += store_mode_line_string (NULL,
20322 Fsubstring (elt, make_number (charpos),
20323 make_number (endpos)),
20324 0, 0, 0, Qnil);
20326 break;
20327 case MODE_LINE_DISPLAY:
20329 EMACS_INT bytepos = last_offset;
20330 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20332 if (precision <= 0)
20333 nchars = string_byte_to_char (elt, offset) - charpos;
20334 n += display_string (NULL, elt, Qnil, 0, charpos,
20335 it, 0, nchars, 0,
20336 STRING_MULTIBYTE (elt));
20338 break;
20341 else /* c == '%' */
20343 EMACS_INT percent_position = offset;
20345 /* Get the specified minimum width. Zero means
20346 don't pad. */
20347 field = 0;
20348 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20349 field = field * 10 + c - '0';
20351 /* Don't pad beyond the total padding allowed. */
20352 if (field_width - n > 0 && field > field_width - n)
20353 field = field_width - n;
20355 /* Note that either PRECISION <= 0 or N < PRECISION. */
20356 prec = precision - n;
20358 if (c == 'M')
20359 n += display_mode_element (it, depth, field, prec,
20360 Vglobal_mode_string, props,
20361 risky);
20362 else if (c != 0)
20364 int multibyte;
20365 EMACS_INT bytepos, charpos;
20366 const char *spec;
20367 Lisp_Object string;
20369 bytepos = percent_position;
20370 charpos = (STRING_MULTIBYTE (elt)
20371 ? string_byte_to_char (elt, bytepos)
20372 : bytepos);
20373 spec = decode_mode_spec (it->w, c, field, &string);
20374 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20376 switch (mode_line_target)
20378 case MODE_LINE_NOPROP:
20379 case MODE_LINE_TITLE:
20380 n += store_mode_line_noprop (spec, field, prec);
20381 break;
20382 case MODE_LINE_STRING:
20384 Lisp_Object tem = build_string (spec);
20385 props = Ftext_properties_at (make_number (charpos), elt);
20386 /* Should only keep face property in props */
20387 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20389 break;
20390 case MODE_LINE_DISPLAY:
20392 int nglyphs_before, nwritten;
20394 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20395 nwritten = display_string (spec, string, elt,
20396 charpos, 0, it,
20397 field, prec, 0,
20398 multibyte);
20400 /* Assign to the glyphs written above the
20401 string where the `%x' came from, position
20402 of the `%'. */
20403 if (nwritten > 0)
20405 struct glyph *glyph
20406 = (it->glyph_row->glyphs[TEXT_AREA]
20407 + nglyphs_before);
20408 int i;
20410 for (i = 0; i < nwritten; ++i)
20412 glyph[i].object = elt;
20413 glyph[i].charpos = charpos;
20416 n += nwritten;
20419 break;
20422 else /* c == 0 */
20423 break;
20427 break;
20429 case Lisp_Symbol:
20430 /* A symbol: process the value of the symbol recursively
20431 as if it appeared here directly. Avoid error if symbol void.
20432 Special case: if value of symbol is a string, output the string
20433 literally. */
20435 register Lisp_Object tem;
20437 /* If the variable is not marked as risky to set
20438 then its contents are risky to use. */
20439 if (NILP (Fget (elt, Qrisky_local_variable)))
20440 risky = 1;
20442 tem = Fboundp (elt);
20443 if (!NILP (tem))
20445 tem = Fsymbol_value (elt);
20446 /* If value is a string, output that string literally:
20447 don't check for % within it. */
20448 if (STRINGP (tem))
20449 literal = 1;
20451 if (!EQ (tem, elt))
20453 /* Give up right away for nil or t. */
20454 elt = tem;
20455 goto tail_recurse;
20459 break;
20461 case Lisp_Cons:
20463 register Lisp_Object car, tem;
20465 /* A cons cell: five distinct cases.
20466 If first element is :eval or :propertize, do something special.
20467 If first element is a string or a cons, process all the elements
20468 and effectively concatenate them.
20469 If first element is a negative number, truncate displaying cdr to
20470 at most that many characters. If positive, pad (with spaces)
20471 to at least that many characters.
20472 If first element is a symbol, process the cadr or caddr recursively
20473 according to whether the symbol's value is non-nil or nil. */
20474 car = XCAR (elt);
20475 if (EQ (car, QCeval))
20477 /* An element of the form (:eval FORM) means evaluate FORM
20478 and use the result as mode line elements. */
20480 if (risky)
20481 break;
20483 if (CONSP (XCDR (elt)))
20485 Lisp_Object spec;
20486 spec = safe_eval (XCAR (XCDR (elt)));
20487 n += display_mode_element (it, depth, field_width - n,
20488 precision - n, spec, props,
20489 risky);
20492 else if (EQ (car, QCpropertize))
20494 /* An element of the form (:propertize ELT PROPS...)
20495 means display ELT but applying properties PROPS. */
20497 if (risky)
20498 break;
20500 if (CONSP (XCDR (elt)))
20501 n += display_mode_element (it, depth, field_width - n,
20502 precision - n, XCAR (XCDR (elt)),
20503 XCDR (XCDR (elt)), risky);
20505 else if (SYMBOLP (car))
20507 tem = Fboundp (car);
20508 elt = XCDR (elt);
20509 if (!CONSP (elt))
20510 goto invalid;
20511 /* elt is now the cdr, and we know it is a cons cell.
20512 Use its car if CAR has a non-nil value. */
20513 if (!NILP (tem))
20515 tem = Fsymbol_value (car);
20516 if (!NILP (tem))
20518 elt = XCAR (elt);
20519 goto tail_recurse;
20522 /* Symbol's value is nil (or symbol is unbound)
20523 Get the cddr of the original list
20524 and if possible find the caddr and use that. */
20525 elt = XCDR (elt);
20526 if (NILP (elt))
20527 break;
20528 else if (!CONSP (elt))
20529 goto invalid;
20530 elt = XCAR (elt);
20531 goto tail_recurse;
20533 else if (INTEGERP (car))
20535 register int lim = XINT (car);
20536 elt = XCDR (elt);
20537 if (lim < 0)
20539 /* Negative int means reduce maximum width. */
20540 if (precision <= 0)
20541 precision = -lim;
20542 else
20543 precision = min (precision, -lim);
20545 else if (lim > 0)
20547 /* Padding specified. Don't let it be more than
20548 current maximum. */
20549 if (precision > 0)
20550 lim = min (precision, lim);
20552 /* If that's more padding than already wanted, queue it.
20553 But don't reduce padding already specified even if
20554 that is beyond the current truncation point. */
20555 field_width = max (lim, field_width);
20557 goto tail_recurse;
20559 else if (STRINGP (car) || CONSP (car))
20561 Lisp_Object halftail = elt;
20562 int len = 0;
20564 while (CONSP (elt)
20565 && (precision <= 0 || n < precision))
20567 n += display_mode_element (it, depth,
20568 /* Do padding only after the last
20569 element in the list. */
20570 (! CONSP (XCDR (elt))
20571 ? field_width - n
20572 : 0),
20573 precision - n, XCAR (elt),
20574 props, risky);
20575 elt = XCDR (elt);
20576 len++;
20577 if ((len & 1) == 0)
20578 halftail = XCDR (halftail);
20579 /* Check for cycle. */
20580 if (EQ (halftail, elt))
20581 break;
20585 break;
20587 default:
20588 invalid:
20589 elt = build_string ("*invalid*");
20590 goto tail_recurse;
20593 /* Pad to FIELD_WIDTH. */
20594 if (field_width > 0 && n < field_width)
20596 switch (mode_line_target)
20598 case MODE_LINE_NOPROP:
20599 case MODE_LINE_TITLE:
20600 n += store_mode_line_noprop ("", field_width - n, 0);
20601 break;
20602 case MODE_LINE_STRING:
20603 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20604 break;
20605 case MODE_LINE_DISPLAY:
20606 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20607 0, 0, 0);
20608 break;
20612 return n;
20615 /* Store a mode-line string element in mode_line_string_list.
20617 If STRING is non-null, display that C string. Otherwise, the Lisp
20618 string LISP_STRING is displayed.
20620 FIELD_WIDTH is the minimum number of output glyphs to produce.
20621 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20622 with spaces. FIELD_WIDTH <= 0 means don't pad.
20624 PRECISION is the maximum number of characters to output from
20625 STRING. PRECISION <= 0 means don't truncate the string.
20627 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20628 properties to the string.
20630 PROPS are the properties to add to the string.
20631 The mode_line_string_face face property is always added to the string.
20634 static int
20635 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20636 int field_width, int precision, Lisp_Object props)
20638 EMACS_INT len;
20639 int n = 0;
20641 if (string != NULL)
20643 len = strlen (string);
20644 if (precision > 0 && len > precision)
20645 len = precision;
20646 lisp_string = make_string (string, len);
20647 if (NILP (props))
20648 props = mode_line_string_face_prop;
20649 else if (!NILP (mode_line_string_face))
20651 Lisp_Object face = Fplist_get (props, Qface);
20652 props = Fcopy_sequence (props);
20653 if (NILP (face))
20654 face = mode_line_string_face;
20655 else
20656 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20657 props = Fplist_put (props, Qface, face);
20659 Fadd_text_properties (make_number (0), make_number (len),
20660 props, lisp_string);
20662 else
20664 len = XFASTINT (Flength (lisp_string));
20665 if (precision > 0 && len > precision)
20667 len = precision;
20668 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20669 precision = -1;
20671 if (!NILP (mode_line_string_face))
20673 Lisp_Object face;
20674 if (NILP (props))
20675 props = Ftext_properties_at (make_number (0), lisp_string);
20676 face = Fplist_get (props, Qface);
20677 if (NILP (face))
20678 face = mode_line_string_face;
20679 else
20680 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20681 props = Fcons (Qface, Fcons (face, Qnil));
20682 if (copy_string)
20683 lisp_string = Fcopy_sequence (lisp_string);
20685 if (!NILP (props))
20686 Fadd_text_properties (make_number (0), make_number (len),
20687 props, lisp_string);
20690 if (len > 0)
20692 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20693 n += len;
20696 if (field_width > len)
20698 field_width -= len;
20699 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20700 if (!NILP (props))
20701 Fadd_text_properties (make_number (0), make_number (field_width),
20702 props, lisp_string);
20703 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20704 n += field_width;
20707 return n;
20711 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20712 1, 4, 0,
20713 doc: /* Format a string out of a mode line format specification.
20714 First arg FORMAT specifies the mode line format (see `mode-line-format'
20715 for details) to use.
20717 By default, the format is evaluated for the currently selected window.
20719 Optional second arg FACE specifies the face property to put on all
20720 characters for which no face is specified. The value nil means the
20721 default face. The value t means whatever face the window's mode line
20722 currently uses (either `mode-line' or `mode-line-inactive',
20723 depending on whether the window is the selected window or not).
20724 An integer value means the value string has no text
20725 properties.
20727 Optional third and fourth args WINDOW and BUFFER specify the window
20728 and buffer to use as the context for the formatting (defaults
20729 are the selected window and the WINDOW's buffer). */)
20730 (Lisp_Object format, Lisp_Object face,
20731 Lisp_Object window, Lisp_Object buffer)
20733 struct it it;
20734 int len;
20735 struct window *w;
20736 struct buffer *old_buffer = NULL;
20737 int face_id;
20738 int no_props = INTEGERP (face);
20739 int count = SPECPDL_INDEX ();
20740 Lisp_Object str;
20741 int string_start = 0;
20743 if (NILP (window))
20744 window = selected_window;
20745 CHECK_WINDOW (window);
20746 w = XWINDOW (window);
20748 if (NILP (buffer))
20749 buffer = w->buffer;
20750 CHECK_BUFFER (buffer);
20752 /* Make formatting the modeline a non-op when noninteractive, otherwise
20753 there will be problems later caused by a partially initialized frame. */
20754 if (NILP (format) || noninteractive)
20755 return empty_unibyte_string;
20757 if (no_props)
20758 face = Qnil;
20760 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20761 : EQ (face, Qt) ? (EQ (window, selected_window)
20762 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20763 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20764 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20765 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20766 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20767 : DEFAULT_FACE_ID;
20769 if (XBUFFER (buffer) != current_buffer)
20770 old_buffer = current_buffer;
20772 /* Save things including mode_line_proptrans_alist,
20773 and set that to nil so that we don't alter the outer value. */
20774 record_unwind_protect (unwind_format_mode_line,
20775 format_mode_line_unwind_data
20776 (old_buffer, selected_window, 1));
20777 mode_line_proptrans_alist = Qnil;
20779 Fselect_window (window, Qt);
20780 if (old_buffer)
20781 set_buffer_internal_1 (XBUFFER (buffer));
20783 init_iterator (&it, w, -1, -1, NULL, face_id);
20785 if (no_props)
20787 mode_line_target = MODE_LINE_NOPROP;
20788 mode_line_string_face_prop = Qnil;
20789 mode_line_string_list = Qnil;
20790 string_start = MODE_LINE_NOPROP_LEN (0);
20792 else
20794 mode_line_target = MODE_LINE_STRING;
20795 mode_line_string_list = Qnil;
20796 mode_line_string_face = face;
20797 mode_line_string_face_prop
20798 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20801 push_kboard (FRAME_KBOARD (it.f));
20802 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20803 pop_kboard ();
20805 if (no_props)
20807 len = MODE_LINE_NOPROP_LEN (string_start);
20808 str = make_string (mode_line_noprop_buf + string_start, len);
20810 else
20812 mode_line_string_list = Fnreverse (mode_line_string_list);
20813 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20814 empty_unibyte_string);
20817 unbind_to (count, Qnil);
20818 return str;
20821 /* Write a null-terminated, right justified decimal representation of
20822 the positive integer D to BUF using a minimal field width WIDTH. */
20824 static void
20825 pint2str (register char *buf, register int width, register EMACS_INT d)
20827 register char *p = buf;
20829 if (d <= 0)
20830 *p++ = '0';
20831 else
20833 while (d > 0)
20835 *p++ = d % 10 + '0';
20836 d /= 10;
20840 for (width -= (int) (p - buf); width > 0; --width)
20841 *p++ = ' ';
20842 *p-- = '\0';
20843 while (p > buf)
20845 d = *buf;
20846 *buf++ = *p;
20847 *p-- = d;
20851 /* Write a null-terminated, right justified decimal and "human
20852 readable" representation of the nonnegative integer D to BUF using
20853 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20855 static const char power_letter[] =
20857 0, /* no letter */
20858 'k', /* kilo */
20859 'M', /* mega */
20860 'G', /* giga */
20861 'T', /* tera */
20862 'P', /* peta */
20863 'E', /* exa */
20864 'Z', /* zetta */
20865 'Y' /* yotta */
20868 static void
20869 pint2hrstr (char *buf, int width, EMACS_INT d)
20871 /* We aim to represent the nonnegative integer D as
20872 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20873 EMACS_INT quotient = d;
20874 int remainder = 0;
20875 /* -1 means: do not use TENTHS. */
20876 int tenths = -1;
20877 int exponent = 0;
20879 /* Length of QUOTIENT.TENTHS as a string. */
20880 int length;
20882 char * psuffix;
20883 char * p;
20885 if (1000 <= quotient)
20887 /* Scale to the appropriate EXPONENT. */
20890 remainder = quotient % 1000;
20891 quotient /= 1000;
20892 exponent++;
20894 while (1000 <= quotient);
20896 /* Round to nearest and decide whether to use TENTHS or not. */
20897 if (quotient <= 9)
20899 tenths = remainder / 100;
20900 if (50 <= remainder % 100)
20902 if (tenths < 9)
20903 tenths++;
20904 else
20906 quotient++;
20907 if (quotient == 10)
20908 tenths = -1;
20909 else
20910 tenths = 0;
20914 else
20915 if (500 <= remainder)
20917 if (quotient < 999)
20918 quotient++;
20919 else
20921 quotient = 1;
20922 exponent++;
20923 tenths = 0;
20928 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20929 if (tenths == -1 && quotient <= 99)
20930 if (quotient <= 9)
20931 length = 1;
20932 else
20933 length = 2;
20934 else
20935 length = 3;
20936 p = psuffix = buf + max (width, length);
20938 /* Print EXPONENT. */
20939 *psuffix++ = power_letter[exponent];
20940 *psuffix = '\0';
20942 /* Print TENTHS. */
20943 if (tenths >= 0)
20945 *--p = '0' + tenths;
20946 *--p = '.';
20949 /* Print QUOTIENT. */
20952 int digit = quotient % 10;
20953 *--p = '0' + digit;
20955 while ((quotient /= 10) != 0);
20957 /* Print leading spaces. */
20958 while (buf < p)
20959 *--p = ' ';
20962 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20963 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20964 type of CODING_SYSTEM. Return updated pointer into BUF. */
20966 static unsigned char invalid_eol_type[] = "(*invalid*)";
20968 static char *
20969 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20971 Lisp_Object val;
20972 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20973 const unsigned char *eol_str;
20974 int eol_str_len;
20975 /* The EOL conversion we are using. */
20976 Lisp_Object eoltype;
20978 val = CODING_SYSTEM_SPEC (coding_system);
20979 eoltype = Qnil;
20981 if (!VECTORP (val)) /* Not yet decided. */
20983 if (multibyte)
20984 *buf++ = '-';
20985 if (eol_flag)
20986 eoltype = eol_mnemonic_undecided;
20987 /* Don't mention EOL conversion if it isn't decided. */
20989 else
20991 Lisp_Object attrs;
20992 Lisp_Object eolvalue;
20994 attrs = AREF (val, 0);
20995 eolvalue = AREF (val, 2);
20997 if (multibyte)
20998 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
21000 if (eol_flag)
21002 /* The EOL conversion that is normal on this system. */
21004 if (NILP (eolvalue)) /* Not yet decided. */
21005 eoltype = eol_mnemonic_undecided;
21006 else if (VECTORP (eolvalue)) /* Not yet decided. */
21007 eoltype = eol_mnemonic_undecided;
21008 else /* eolvalue is Qunix, Qdos, or Qmac. */
21009 eoltype = (EQ (eolvalue, Qunix)
21010 ? eol_mnemonic_unix
21011 : (EQ (eolvalue, Qdos) == 1
21012 ? eol_mnemonic_dos : eol_mnemonic_mac));
21016 if (eol_flag)
21018 /* Mention the EOL conversion if it is not the usual one. */
21019 if (STRINGP (eoltype))
21021 eol_str = SDATA (eoltype);
21022 eol_str_len = SBYTES (eoltype);
21024 else if (CHARACTERP (eoltype))
21026 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21027 int c = XFASTINT (eoltype);
21028 eol_str_len = CHAR_STRING (c, tmp);
21029 eol_str = tmp;
21031 else
21033 eol_str = invalid_eol_type;
21034 eol_str_len = sizeof (invalid_eol_type) - 1;
21036 memcpy (buf, eol_str, eol_str_len);
21037 buf += eol_str_len;
21040 return buf;
21043 /* Return a string for the output of a mode line %-spec for window W,
21044 generated by character C. FIELD_WIDTH > 0 means pad the string
21045 returned with spaces to that value. Return a Lisp string in
21046 *STRING if the resulting string is taken from that Lisp string.
21048 Note we operate on the current buffer for most purposes,
21049 the exception being w->base_line_pos. */
21051 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21053 static const char *
21054 decode_mode_spec (struct window *w, register int c, int field_width,
21055 Lisp_Object *string)
21057 Lisp_Object obj;
21058 struct frame *f = XFRAME (WINDOW_FRAME (w));
21059 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21060 struct buffer *b = current_buffer;
21062 obj = Qnil;
21063 *string = Qnil;
21065 switch (c)
21067 case '*':
21068 if (!NILP (BVAR (b, read_only)))
21069 return "%";
21070 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21071 return "*";
21072 return "-";
21074 case '+':
21075 /* This differs from %* only for a modified read-only buffer. */
21076 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21077 return "*";
21078 if (!NILP (BVAR (b, read_only)))
21079 return "%";
21080 return "-";
21082 case '&':
21083 /* This differs from %* in ignoring read-only-ness. */
21084 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21085 return "*";
21086 return "-";
21088 case '%':
21089 return "%";
21091 case '[':
21093 int i;
21094 char *p;
21096 if (command_loop_level > 5)
21097 return "[[[... ";
21098 p = decode_mode_spec_buf;
21099 for (i = 0; i < command_loop_level; i++)
21100 *p++ = '[';
21101 *p = 0;
21102 return decode_mode_spec_buf;
21105 case ']':
21107 int i;
21108 char *p;
21110 if (command_loop_level > 5)
21111 return " ...]]]";
21112 p = decode_mode_spec_buf;
21113 for (i = 0; i < command_loop_level; i++)
21114 *p++ = ']';
21115 *p = 0;
21116 return decode_mode_spec_buf;
21119 case '-':
21121 register int i;
21123 /* Let lots_of_dashes be a string of infinite length. */
21124 if (mode_line_target == MODE_LINE_NOPROP ||
21125 mode_line_target == MODE_LINE_STRING)
21126 return "--";
21127 if (field_width <= 0
21128 || field_width > sizeof (lots_of_dashes))
21130 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21131 decode_mode_spec_buf[i] = '-';
21132 decode_mode_spec_buf[i] = '\0';
21133 return decode_mode_spec_buf;
21135 else
21136 return lots_of_dashes;
21139 case 'b':
21140 obj = BVAR (b, name);
21141 break;
21143 case 'c':
21144 /* %c and %l are ignored in `frame-title-format'.
21145 (In redisplay_internal, the frame title is drawn _before_ the
21146 windows are updated, so the stuff which depends on actual
21147 window contents (such as %l) may fail to render properly, or
21148 even crash emacs.) */
21149 if (mode_line_target == MODE_LINE_TITLE)
21150 return "";
21151 else
21153 EMACS_INT col = current_column ();
21154 w->column_number_displayed = make_number (col);
21155 pint2str (decode_mode_spec_buf, field_width, col);
21156 return decode_mode_spec_buf;
21159 case 'e':
21160 #ifndef SYSTEM_MALLOC
21162 if (NILP (Vmemory_full))
21163 return "";
21164 else
21165 return "!MEM FULL! ";
21167 #else
21168 return "";
21169 #endif
21171 case 'F':
21172 /* %F displays the frame name. */
21173 if (!NILP (f->title))
21174 return SSDATA (f->title);
21175 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21176 return SSDATA (f->name);
21177 return "Emacs";
21179 case 'f':
21180 obj = BVAR (b, filename);
21181 break;
21183 case 'i':
21185 EMACS_INT size = ZV - BEGV;
21186 pint2str (decode_mode_spec_buf, field_width, size);
21187 return decode_mode_spec_buf;
21190 case 'I':
21192 EMACS_INT size = ZV - BEGV;
21193 pint2hrstr (decode_mode_spec_buf, field_width, size);
21194 return decode_mode_spec_buf;
21197 case 'l':
21199 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21200 EMACS_INT topline, nlines, height;
21201 EMACS_INT junk;
21203 /* %c and %l are ignored in `frame-title-format'. */
21204 if (mode_line_target == MODE_LINE_TITLE)
21205 return "";
21207 startpos = XMARKER (w->start)->charpos;
21208 startpos_byte = marker_byte_position (w->start);
21209 height = WINDOW_TOTAL_LINES (w);
21211 /* If we decided that this buffer isn't suitable for line numbers,
21212 don't forget that too fast. */
21213 if (EQ (w->base_line_pos, w->buffer))
21214 goto no_value;
21215 /* But do forget it, if the window shows a different buffer now. */
21216 else if (BUFFERP (w->base_line_pos))
21217 w->base_line_pos = Qnil;
21219 /* If the buffer is very big, don't waste time. */
21220 if (INTEGERP (Vline_number_display_limit)
21221 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21223 w->base_line_pos = Qnil;
21224 w->base_line_number = Qnil;
21225 goto no_value;
21228 if (INTEGERP (w->base_line_number)
21229 && INTEGERP (w->base_line_pos)
21230 && XFASTINT (w->base_line_pos) <= startpos)
21232 line = XFASTINT (w->base_line_number);
21233 linepos = XFASTINT (w->base_line_pos);
21234 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21236 else
21238 line = 1;
21239 linepos = BUF_BEGV (b);
21240 linepos_byte = BUF_BEGV_BYTE (b);
21243 /* Count lines from base line to window start position. */
21244 nlines = display_count_lines (linepos_byte,
21245 startpos_byte,
21246 startpos, &junk);
21248 topline = nlines + line;
21250 /* Determine a new base line, if the old one is too close
21251 or too far away, or if we did not have one.
21252 "Too close" means it's plausible a scroll-down would
21253 go back past it. */
21254 if (startpos == BUF_BEGV (b))
21256 w->base_line_number = make_number (topline);
21257 w->base_line_pos = make_number (BUF_BEGV (b));
21259 else if (nlines < height + 25 || nlines > height * 3 + 50
21260 || linepos == BUF_BEGV (b))
21262 EMACS_INT limit = BUF_BEGV (b);
21263 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21264 EMACS_INT position;
21265 EMACS_INT distance =
21266 (height * 2 + 30) * line_number_display_limit_width;
21268 if (startpos - distance > limit)
21270 limit = startpos - distance;
21271 limit_byte = CHAR_TO_BYTE (limit);
21274 nlines = display_count_lines (startpos_byte,
21275 limit_byte,
21276 - (height * 2 + 30),
21277 &position);
21278 /* If we couldn't find the lines we wanted within
21279 line_number_display_limit_width chars per line,
21280 give up on line numbers for this window. */
21281 if (position == limit_byte && limit == startpos - distance)
21283 w->base_line_pos = w->buffer;
21284 w->base_line_number = Qnil;
21285 goto no_value;
21288 w->base_line_number = make_number (topline - nlines);
21289 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21292 /* Now count lines from the start pos to point. */
21293 nlines = display_count_lines (startpos_byte,
21294 PT_BYTE, PT, &junk);
21296 /* Record that we did display the line number. */
21297 line_number_displayed = 1;
21299 /* Make the string to show. */
21300 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21301 return decode_mode_spec_buf;
21302 no_value:
21304 char* p = decode_mode_spec_buf;
21305 int pad = field_width - 2;
21306 while (pad-- > 0)
21307 *p++ = ' ';
21308 *p++ = '?';
21309 *p++ = '?';
21310 *p = '\0';
21311 return decode_mode_spec_buf;
21314 break;
21316 case 'm':
21317 obj = BVAR (b, mode_name);
21318 break;
21320 case 'n':
21321 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21322 return " Narrow";
21323 break;
21325 case 'p':
21327 EMACS_INT pos = marker_position (w->start);
21328 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21330 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21332 if (pos <= BUF_BEGV (b))
21333 return "All";
21334 else
21335 return "Bottom";
21337 else if (pos <= BUF_BEGV (b))
21338 return "Top";
21339 else
21341 if (total > 1000000)
21342 /* Do it differently for a large value, to avoid overflow. */
21343 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21344 else
21345 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21346 /* We can't normally display a 3-digit number,
21347 so get us a 2-digit number that is close. */
21348 if (total == 100)
21349 total = 99;
21350 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21351 return decode_mode_spec_buf;
21355 /* Display percentage of size above the bottom of the screen. */
21356 case 'P':
21358 EMACS_INT toppos = marker_position (w->start);
21359 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21360 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21362 if (botpos >= BUF_ZV (b))
21364 if (toppos <= BUF_BEGV (b))
21365 return "All";
21366 else
21367 return "Bottom";
21369 else
21371 if (total > 1000000)
21372 /* Do it differently for a large value, to avoid overflow. */
21373 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21374 else
21375 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21376 /* We can't normally display a 3-digit number,
21377 so get us a 2-digit number that is close. */
21378 if (total == 100)
21379 total = 99;
21380 if (toppos <= BUF_BEGV (b))
21381 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21382 else
21383 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21384 return decode_mode_spec_buf;
21388 case 's':
21389 /* status of process */
21390 obj = Fget_buffer_process (Fcurrent_buffer ());
21391 if (NILP (obj))
21392 return "no process";
21393 #ifndef MSDOS
21394 obj = Fsymbol_name (Fprocess_status (obj));
21395 #endif
21396 break;
21398 case '@':
21400 int count = inhibit_garbage_collection ();
21401 Lisp_Object val = call1 (intern ("file-remote-p"),
21402 BVAR (current_buffer, directory));
21403 unbind_to (count, Qnil);
21405 if (NILP (val))
21406 return "-";
21407 else
21408 return "@";
21411 case 't': /* indicate TEXT or BINARY */
21412 return "T";
21414 case 'z':
21415 /* coding-system (not including end-of-line format) */
21416 case 'Z':
21417 /* coding-system (including end-of-line type) */
21419 int eol_flag = (c == 'Z');
21420 char *p = decode_mode_spec_buf;
21422 if (! FRAME_WINDOW_P (f))
21424 /* No need to mention EOL here--the terminal never needs
21425 to do EOL conversion. */
21426 p = decode_mode_spec_coding (CODING_ID_NAME
21427 (FRAME_KEYBOARD_CODING (f)->id),
21428 p, 0);
21429 p = decode_mode_spec_coding (CODING_ID_NAME
21430 (FRAME_TERMINAL_CODING (f)->id),
21431 p, 0);
21433 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21434 p, eol_flag);
21436 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21437 #ifdef subprocesses
21438 obj = Fget_buffer_process (Fcurrent_buffer ());
21439 if (PROCESSP (obj))
21441 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21442 p, eol_flag);
21443 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21444 p, eol_flag);
21446 #endif /* subprocesses */
21447 #endif /* 0 */
21448 *p = 0;
21449 return decode_mode_spec_buf;
21453 if (STRINGP (obj))
21455 *string = obj;
21456 return SSDATA (obj);
21458 else
21459 return "";
21463 /* Count up to COUNT lines starting from START_BYTE.
21464 But don't go beyond LIMIT_BYTE.
21465 Return the number of lines thus found (always nonnegative).
21467 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21469 static EMACS_INT
21470 display_count_lines (EMACS_INT start_byte,
21471 EMACS_INT limit_byte, EMACS_INT count,
21472 EMACS_INT *byte_pos_ptr)
21474 register unsigned char *cursor;
21475 unsigned char *base;
21477 register EMACS_INT ceiling;
21478 register unsigned char *ceiling_addr;
21479 EMACS_INT orig_count = count;
21481 /* If we are not in selective display mode,
21482 check only for newlines. */
21483 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21484 && !INTEGERP (BVAR (current_buffer, selective_display)));
21486 if (count > 0)
21488 while (start_byte < limit_byte)
21490 ceiling = BUFFER_CEILING_OF (start_byte);
21491 ceiling = min (limit_byte - 1, ceiling);
21492 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21493 base = (cursor = BYTE_POS_ADDR (start_byte));
21494 while (1)
21496 if (selective_display)
21497 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21499 else
21500 while (*cursor != '\n' && ++cursor != ceiling_addr)
21503 if (cursor != ceiling_addr)
21505 if (--count == 0)
21507 start_byte += cursor - base + 1;
21508 *byte_pos_ptr = start_byte;
21509 return orig_count;
21511 else
21512 if (++cursor == ceiling_addr)
21513 break;
21515 else
21516 break;
21518 start_byte += cursor - base;
21521 else
21523 while (start_byte > limit_byte)
21525 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21526 ceiling = max (limit_byte, ceiling);
21527 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21528 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21529 while (1)
21531 if (selective_display)
21532 while (--cursor != ceiling_addr
21533 && *cursor != '\n' && *cursor != 015)
21535 else
21536 while (--cursor != ceiling_addr && *cursor != '\n')
21539 if (cursor != ceiling_addr)
21541 if (++count == 0)
21543 start_byte += cursor - base + 1;
21544 *byte_pos_ptr = start_byte;
21545 /* When scanning backwards, we should
21546 not count the newline posterior to which we stop. */
21547 return - orig_count - 1;
21550 else
21551 break;
21553 /* Here we add 1 to compensate for the last decrement
21554 of CURSOR, which took it past the valid range. */
21555 start_byte += cursor - base + 1;
21559 *byte_pos_ptr = limit_byte;
21561 if (count < 0)
21562 return - orig_count + count;
21563 return orig_count - count;
21569 /***********************************************************************
21570 Displaying strings
21571 ***********************************************************************/
21573 /* Display a NUL-terminated string, starting with index START.
21575 If STRING is non-null, display that C string. Otherwise, the Lisp
21576 string LISP_STRING is displayed. There's a case that STRING is
21577 non-null and LISP_STRING is not nil. It means STRING is a string
21578 data of LISP_STRING. In that case, we display LISP_STRING while
21579 ignoring its text properties.
21581 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21582 FACE_STRING. Display STRING or LISP_STRING with the face at
21583 FACE_STRING_POS in FACE_STRING:
21585 Display the string in the environment given by IT, but use the
21586 standard display table, temporarily.
21588 FIELD_WIDTH is the minimum number of output glyphs to produce.
21589 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21590 with spaces. If STRING has more characters, more than FIELD_WIDTH
21591 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21593 PRECISION is the maximum number of characters to output from
21594 STRING. PRECISION < 0 means don't truncate the string.
21596 This is roughly equivalent to printf format specifiers:
21598 FIELD_WIDTH PRECISION PRINTF
21599 ----------------------------------------
21600 -1 -1 %s
21601 -1 10 %.10s
21602 10 -1 %10s
21603 20 10 %20.10s
21605 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21606 display them, and < 0 means obey the current buffer's value of
21607 enable_multibyte_characters.
21609 Value is the number of columns displayed. */
21611 static int
21612 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21613 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21614 int field_width, int precision, int max_x, int multibyte)
21616 int hpos_at_start = it->hpos;
21617 int saved_face_id = it->face_id;
21618 struct glyph_row *row = it->glyph_row;
21619 EMACS_INT it_charpos;
21621 /* Initialize the iterator IT for iteration over STRING beginning
21622 with index START. */
21623 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21624 precision, field_width, multibyte);
21625 if (string && STRINGP (lisp_string))
21626 /* LISP_STRING is the one returned by decode_mode_spec. We should
21627 ignore its text properties. */
21628 it->stop_charpos = it->end_charpos;
21630 /* If displaying STRING, set up the face of the iterator from
21631 FACE_STRING, if that's given. */
21632 if (STRINGP (face_string))
21634 EMACS_INT endptr;
21635 struct face *face;
21637 it->face_id
21638 = face_at_string_position (it->w, face_string, face_string_pos,
21639 0, it->region_beg_charpos,
21640 it->region_end_charpos,
21641 &endptr, it->base_face_id, 0);
21642 face = FACE_FROM_ID (it->f, it->face_id);
21643 it->face_box_p = face->box != FACE_NO_BOX;
21646 /* Set max_x to the maximum allowed X position. Don't let it go
21647 beyond the right edge of the window. */
21648 if (max_x <= 0)
21649 max_x = it->last_visible_x;
21650 else
21651 max_x = min (max_x, it->last_visible_x);
21653 /* Skip over display elements that are not visible. because IT->w is
21654 hscrolled. */
21655 if (it->current_x < it->first_visible_x)
21656 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21657 MOVE_TO_POS | MOVE_TO_X);
21659 row->ascent = it->max_ascent;
21660 row->height = it->max_ascent + it->max_descent;
21661 row->phys_ascent = it->max_phys_ascent;
21662 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21663 row->extra_line_spacing = it->max_extra_line_spacing;
21665 if (STRINGP (it->string))
21666 it_charpos = IT_STRING_CHARPOS (*it);
21667 else
21668 it_charpos = IT_CHARPOS (*it);
21670 /* This condition is for the case that we are called with current_x
21671 past last_visible_x. */
21672 while (it->current_x < max_x)
21674 int x_before, x, n_glyphs_before, i, nglyphs;
21676 /* Get the next display element. */
21677 if (!get_next_display_element (it))
21678 break;
21680 /* Produce glyphs. */
21681 x_before = it->current_x;
21682 n_glyphs_before = row->used[TEXT_AREA];
21683 PRODUCE_GLYPHS (it);
21685 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21686 i = 0;
21687 x = x_before;
21688 while (i < nglyphs)
21690 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21692 if (it->line_wrap != TRUNCATE
21693 && x + glyph->pixel_width > max_x)
21695 /* End of continued line or max_x reached. */
21696 if (CHAR_GLYPH_PADDING_P (*glyph))
21698 /* A wide character is unbreakable. */
21699 if (row->reversed_p)
21700 unproduce_glyphs (it, row->used[TEXT_AREA]
21701 - n_glyphs_before);
21702 row->used[TEXT_AREA] = n_glyphs_before;
21703 it->current_x = x_before;
21705 else
21707 if (row->reversed_p)
21708 unproduce_glyphs (it, row->used[TEXT_AREA]
21709 - (n_glyphs_before + i));
21710 row->used[TEXT_AREA] = n_glyphs_before + i;
21711 it->current_x = x;
21713 break;
21715 else if (x + glyph->pixel_width >= it->first_visible_x)
21717 /* Glyph is at least partially visible. */
21718 ++it->hpos;
21719 if (x < it->first_visible_x)
21720 row->x = x - it->first_visible_x;
21722 else
21724 /* Glyph is off the left margin of the display area.
21725 Should not happen. */
21726 abort ();
21729 row->ascent = max (row->ascent, it->max_ascent);
21730 row->height = max (row->height, it->max_ascent + it->max_descent);
21731 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21732 row->phys_height = max (row->phys_height,
21733 it->max_phys_ascent + it->max_phys_descent);
21734 row->extra_line_spacing = max (row->extra_line_spacing,
21735 it->max_extra_line_spacing);
21736 x += glyph->pixel_width;
21737 ++i;
21740 /* Stop if max_x reached. */
21741 if (i < nglyphs)
21742 break;
21744 /* Stop at line ends. */
21745 if (ITERATOR_AT_END_OF_LINE_P (it))
21747 it->continuation_lines_width = 0;
21748 break;
21751 set_iterator_to_next (it, 1);
21752 if (STRINGP (it->string))
21753 it_charpos = IT_STRING_CHARPOS (*it);
21754 else
21755 it_charpos = IT_CHARPOS (*it);
21757 /* Stop if truncating at the right edge. */
21758 if (it->line_wrap == TRUNCATE
21759 && it->current_x >= it->last_visible_x)
21761 /* Add truncation mark, but don't do it if the line is
21762 truncated at a padding space. */
21763 if (it_charpos < it->string_nchars)
21765 if (!FRAME_WINDOW_P (it->f))
21767 int ii, n;
21769 if (it->current_x > it->last_visible_x)
21771 if (!row->reversed_p)
21773 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21774 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21775 break;
21777 else
21779 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21780 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21781 break;
21782 unproduce_glyphs (it, ii + 1);
21783 ii = row->used[TEXT_AREA] - (ii + 1);
21785 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21787 row->used[TEXT_AREA] = ii;
21788 produce_special_glyphs (it, IT_TRUNCATION);
21791 produce_special_glyphs (it, IT_TRUNCATION);
21793 row->truncated_on_right_p = 1;
21795 break;
21799 /* Maybe insert a truncation at the left. */
21800 if (it->first_visible_x
21801 && it_charpos > 0)
21803 if (!FRAME_WINDOW_P (it->f))
21804 insert_left_trunc_glyphs (it);
21805 row->truncated_on_left_p = 1;
21808 it->face_id = saved_face_id;
21810 /* Value is number of columns displayed. */
21811 return it->hpos - hpos_at_start;
21816 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21817 appears as an element of LIST or as the car of an element of LIST.
21818 If PROPVAL is a list, compare each element against LIST in that
21819 way, and return 1/2 if any element of PROPVAL is found in LIST.
21820 Otherwise return 0. This function cannot quit.
21821 The return value is 2 if the text is invisible but with an ellipsis
21822 and 1 if it's invisible and without an ellipsis. */
21825 invisible_p (register Lisp_Object propval, Lisp_Object list)
21827 register Lisp_Object tail, proptail;
21829 for (tail = list; CONSP (tail); tail = XCDR (tail))
21831 register Lisp_Object tem;
21832 tem = XCAR (tail);
21833 if (EQ (propval, tem))
21834 return 1;
21835 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21836 return NILP (XCDR (tem)) ? 1 : 2;
21839 if (CONSP (propval))
21841 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21843 Lisp_Object propelt;
21844 propelt = XCAR (proptail);
21845 for (tail = list; CONSP (tail); tail = XCDR (tail))
21847 register Lisp_Object tem;
21848 tem = XCAR (tail);
21849 if (EQ (propelt, tem))
21850 return 1;
21851 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21852 return NILP (XCDR (tem)) ? 1 : 2;
21857 return 0;
21860 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21861 doc: /* Non-nil if the property makes the text invisible.
21862 POS-OR-PROP can be a marker or number, in which case it is taken to be
21863 a position in the current buffer and the value of the `invisible' property
21864 is checked; or it can be some other value, which is then presumed to be the
21865 value of the `invisible' property of the text of interest.
21866 The non-nil value returned can be t for truly invisible text or something
21867 else if the text is replaced by an ellipsis. */)
21868 (Lisp_Object pos_or_prop)
21870 Lisp_Object prop
21871 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21872 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21873 : pos_or_prop);
21874 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21875 return (invis == 0 ? Qnil
21876 : invis == 1 ? Qt
21877 : make_number (invis));
21880 /* Calculate a width or height in pixels from a specification using
21881 the following elements:
21883 SPEC ::=
21884 NUM - a (fractional) multiple of the default font width/height
21885 (NUM) - specifies exactly NUM pixels
21886 UNIT - a fixed number of pixels, see below.
21887 ELEMENT - size of a display element in pixels, see below.
21888 (NUM . SPEC) - equals NUM * SPEC
21889 (+ SPEC SPEC ...) - add pixel values
21890 (- SPEC SPEC ...) - subtract pixel values
21891 (- SPEC) - negate pixel value
21893 NUM ::=
21894 INT or FLOAT - a number constant
21895 SYMBOL - use symbol's (buffer local) variable binding.
21897 UNIT ::=
21898 in - pixels per inch *)
21899 mm - pixels per 1/1000 meter *)
21900 cm - pixels per 1/100 meter *)
21901 width - width of current font in pixels.
21902 height - height of current font in pixels.
21904 *) using the ratio(s) defined in display-pixels-per-inch.
21906 ELEMENT ::=
21908 left-fringe - left fringe width in pixels
21909 right-fringe - right fringe width in pixels
21911 left-margin - left margin width in pixels
21912 right-margin - right margin width in pixels
21914 scroll-bar - scroll-bar area width in pixels
21916 Examples:
21918 Pixels corresponding to 5 inches:
21919 (5 . in)
21921 Total width of non-text areas on left side of window (if scroll-bar is on left):
21922 '(space :width (+ left-fringe left-margin scroll-bar))
21924 Align to first text column (in header line):
21925 '(space :align-to 0)
21927 Align to middle of text area minus half the width of variable `my-image'
21928 containing a loaded image:
21929 '(space :align-to (0.5 . (- text my-image)))
21931 Width of left margin minus width of 1 character in the default font:
21932 '(space :width (- left-margin 1))
21934 Width of left margin minus width of 2 characters in the current font:
21935 '(space :width (- left-margin (2 . width)))
21937 Center 1 character over left-margin (in header line):
21938 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21940 Different ways to express width of left fringe plus left margin minus one pixel:
21941 '(space :width (- (+ left-fringe left-margin) (1)))
21942 '(space :width (+ left-fringe left-margin (- (1))))
21943 '(space :width (+ left-fringe left-margin (-1)))
21947 #define NUMVAL(X) \
21948 ((INTEGERP (X) || FLOATP (X)) \
21949 ? XFLOATINT (X) \
21950 : - 1)
21952 static int
21953 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21954 struct font *font, int width_p, int *align_to)
21956 double pixels;
21958 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21959 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21961 if (NILP (prop))
21962 return OK_PIXELS (0);
21964 xassert (FRAME_LIVE_P (it->f));
21966 if (SYMBOLP (prop))
21968 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21970 char *unit = SSDATA (SYMBOL_NAME (prop));
21972 if (unit[0] == 'i' && unit[1] == 'n')
21973 pixels = 1.0;
21974 else if (unit[0] == 'm' && unit[1] == 'm')
21975 pixels = 25.4;
21976 else if (unit[0] == 'c' && unit[1] == 'm')
21977 pixels = 2.54;
21978 else
21979 pixels = 0;
21980 if (pixels > 0)
21982 double ppi;
21983 #ifdef HAVE_WINDOW_SYSTEM
21984 if (FRAME_WINDOW_P (it->f)
21985 && (ppi = (width_p
21986 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21987 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21988 ppi > 0))
21989 return OK_PIXELS (ppi / pixels);
21990 #endif
21992 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21993 || (CONSP (Vdisplay_pixels_per_inch)
21994 && (ppi = (width_p
21995 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21996 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21997 ppi > 0)))
21998 return OK_PIXELS (ppi / pixels);
22000 return 0;
22004 #ifdef HAVE_WINDOW_SYSTEM
22005 if (EQ (prop, Qheight))
22006 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22007 if (EQ (prop, Qwidth))
22008 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22009 #else
22010 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22011 return OK_PIXELS (1);
22012 #endif
22014 if (EQ (prop, Qtext))
22015 return OK_PIXELS (width_p
22016 ? window_box_width (it->w, TEXT_AREA)
22017 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22019 if (align_to && *align_to < 0)
22021 *res = 0;
22022 if (EQ (prop, Qleft))
22023 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22024 if (EQ (prop, Qright))
22025 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22026 if (EQ (prop, Qcenter))
22027 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22028 + window_box_width (it->w, TEXT_AREA) / 2);
22029 if (EQ (prop, Qleft_fringe))
22030 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22031 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22032 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22033 if (EQ (prop, Qright_fringe))
22034 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22035 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22036 : window_box_right_offset (it->w, TEXT_AREA));
22037 if (EQ (prop, Qleft_margin))
22038 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22039 if (EQ (prop, Qright_margin))
22040 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22041 if (EQ (prop, Qscroll_bar))
22042 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22044 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22045 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22046 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22047 : 0)));
22049 else
22051 if (EQ (prop, Qleft_fringe))
22052 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22053 if (EQ (prop, Qright_fringe))
22054 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22055 if (EQ (prop, Qleft_margin))
22056 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22057 if (EQ (prop, Qright_margin))
22058 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22059 if (EQ (prop, Qscroll_bar))
22060 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22063 prop = Fbuffer_local_value (prop, it->w->buffer);
22066 if (INTEGERP (prop) || FLOATP (prop))
22068 int base_unit = (width_p
22069 ? FRAME_COLUMN_WIDTH (it->f)
22070 : FRAME_LINE_HEIGHT (it->f));
22071 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22074 if (CONSP (prop))
22076 Lisp_Object car = XCAR (prop);
22077 Lisp_Object cdr = XCDR (prop);
22079 if (SYMBOLP (car))
22081 #ifdef HAVE_WINDOW_SYSTEM
22082 if (FRAME_WINDOW_P (it->f)
22083 && valid_image_p (prop))
22085 ptrdiff_t id = lookup_image (it->f, prop);
22086 struct image *img = IMAGE_FROM_ID (it->f, id);
22088 return OK_PIXELS (width_p ? img->width : img->height);
22090 #endif
22091 if (EQ (car, Qplus) || EQ (car, Qminus))
22093 int first = 1;
22094 double px;
22096 pixels = 0;
22097 while (CONSP (cdr))
22099 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22100 font, width_p, align_to))
22101 return 0;
22102 if (first)
22103 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22104 else
22105 pixels += px;
22106 cdr = XCDR (cdr);
22108 if (EQ (car, Qminus))
22109 pixels = -pixels;
22110 return OK_PIXELS (pixels);
22113 car = Fbuffer_local_value (car, it->w->buffer);
22116 if (INTEGERP (car) || FLOATP (car))
22118 double fact;
22119 pixels = XFLOATINT (car);
22120 if (NILP (cdr))
22121 return OK_PIXELS (pixels);
22122 if (calc_pixel_width_or_height (&fact, it, cdr,
22123 font, width_p, align_to))
22124 return OK_PIXELS (pixels * fact);
22125 return 0;
22128 return 0;
22131 return 0;
22135 /***********************************************************************
22136 Glyph Display
22137 ***********************************************************************/
22139 #ifdef HAVE_WINDOW_SYSTEM
22141 #if GLYPH_DEBUG
22143 void
22144 dump_glyph_string (struct glyph_string *s)
22146 fprintf (stderr, "glyph string\n");
22147 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22148 s->x, s->y, s->width, s->height);
22149 fprintf (stderr, " ybase = %d\n", s->ybase);
22150 fprintf (stderr, " hl = %d\n", s->hl);
22151 fprintf (stderr, " left overhang = %d, right = %d\n",
22152 s->left_overhang, s->right_overhang);
22153 fprintf (stderr, " nchars = %d\n", s->nchars);
22154 fprintf (stderr, " extends to end of line = %d\n",
22155 s->extends_to_end_of_line_p);
22156 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22157 fprintf (stderr, " bg width = %d\n", s->background_width);
22160 #endif /* GLYPH_DEBUG */
22162 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22163 of XChar2b structures for S; it can't be allocated in
22164 init_glyph_string because it must be allocated via `alloca'. W
22165 is the window on which S is drawn. ROW and AREA are the glyph row
22166 and area within the row from which S is constructed. START is the
22167 index of the first glyph structure covered by S. HL is a
22168 face-override for drawing S. */
22170 #ifdef HAVE_NTGUI
22171 #define OPTIONAL_HDC(hdc) HDC hdc,
22172 #define DECLARE_HDC(hdc) HDC hdc;
22173 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22174 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22175 #endif
22177 #ifndef OPTIONAL_HDC
22178 #define OPTIONAL_HDC(hdc)
22179 #define DECLARE_HDC(hdc)
22180 #define ALLOCATE_HDC(hdc, f)
22181 #define RELEASE_HDC(hdc, f)
22182 #endif
22184 static void
22185 init_glyph_string (struct glyph_string *s,
22186 OPTIONAL_HDC (hdc)
22187 XChar2b *char2b, struct window *w, struct glyph_row *row,
22188 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22190 memset (s, 0, sizeof *s);
22191 s->w = w;
22192 s->f = XFRAME (w->frame);
22193 #ifdef HAVE_NTGUI
22194 s->hdc = hdc;
22195 #endif
22196 s->display = FRAME_X_DISPLAY (s->f);
22197 s->window = FRAME_X_WINDOW (s->f);
22198 s->char2b = char2b;
22199 s->hl = hl;
22200 s->row = row;
22201 s->area = area;
22202 s->first_glyph = row->glyphs[area] + start;
22203 s->height = row->height;
22204 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22205 s->ybase = s->y + row->ascent;
22209 /* Append the list of glyph strings with head H and tail T to the list
22210 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22212 static inline void
22213 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22214 struct glyph_string *h, struct glyph_string *t)
22216 if (h)
22218 if (*head)
22219 (*tail)->next = h;
22220 else
22221 *head = h;
22222 h->prev = *tail;
22223 *tail = t;
22228 /* Prepend the list of glyph strings with head H and tail T to the
22229 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22230 result. */
22232 static inline void
22233 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22234 struct glyph_string *h, struct glyph_string *t)
22236 if (h)
22238 if (*head)
22239 (*head)->prev = t;
22240 else
22241 *tail = t;
22242 t->next = *head;
22243 *head = h;
22248 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22249 Set *HEAD and *TAIL to the resulting list. */
22251 static inline void
22252 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22253 struct glyph_string *s)
22255 s->next = s->prev = NULL;
22256 append_glyph_string_lists (head, tail, s, s);
22260 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22261 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22262 make sure that X resources for the face returned are allocated.
22263 Value is a pointer to a realized face that is ready for display if
22264 DISPLAY_P is non-zero. */
22266 static inline struct face *
22267 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22268 XChar2b *char2b, int display_p)
22270 struct face *face = FACE_FROM_ID (f, face_id);
22272 if (face->font)
22274 unsigned code = face->font->driver->encode_char (face->font, c);
22276 if (code != FONT_INVALID_CODE)
22277 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22278 else
22279 STORE_XCHAR2B (char2b, 0, 0);
22282 /* Make sure X resources of the face are allocated. */
22283 #ifdef HAVE_X_WINDOWS
22284 if (display_p)
22285 #endif
22287 xassert (face != NULL);
22288 PREPARE_FACE_FOR_DISPLAY (f, face);
22291 return face;
22295 /* Get face and two-byte form of character glyph GLYPH on frame F.
22296 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22297 a pointer to a realized face that is ready for display. */
22299 static inline struct face *
22300 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22301 XChar2b *char2b, int *two_byte_p)
22303 struct face *face;
22305 xassert (glyph->type == CHAR_GLYPH);
22306 face = FACE_FROM_ID (f, glyph->face_id);
22308 if (two_byte_p)
22309 *two_byte_p = 0;
22311 if (face->font)
22313 unsigned code;
22315 if (CHAR_BYTE8_P (glyph->u.ch))
22316 code = CHAR_TO_BYTE8 (glyph->u.ch);
22317 else
22318 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22320 if (code != FONT_INVALID_CODE)
22321 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22322 else
22323 STORE_XCHAR2B (char2b, 0, 0);
22326 /* Make sure X resources of the face are allocated. */
22327 xassert (face != NULL);
22328 PREPARE_FACE_FOR_DISPLAY (f, face);
22329 return face;
22333 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22334 Return 1 if FONT has a glyph for C, otherwise return 0. */
22336 static inline int
22337 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22339 unsigned code;
22341 if (CHAR_BYTE8_P (c))
22342 code = CHAR_TO_BYTE8 (c);
22343 else
22344 code = font->driver->encode_char (font, c);
22346 if (code == FONT_INVALID_CODE)
22347 return 0;
22348 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22349 return 1;
22353 /* Fill glyph string S with composition components specified by S->cmp.
22355 BASE_FACE is the base face of the composition.
22356 S->cmp_from is the index of the first component for S.
22358 OVERLAPS non-zero means S should draw the foreground only, and use
22359 its physical height for clipping. See also draw_glyphs.
22361 Value is the index of a component not in S. */
22363 static int
22364 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22365 int overlaps)
22367 int i;
22368 /* For all glyphs of this composition, starting at the offset
22369 S->cmp_from, until we reach the end of the definition or encounter a
22370 glyph that requires the different face, add it to S. */
22371 struct face *face;
22373 xassert (s);
22375 s->for_overlaps = overlaps;
22376 s->face = NULL;
22377 s->font = NULL;
22378 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22380 int c = COMPOSITION_GLYPH (s->cmp, i);
22382 /* TAB in a composition means display glyphs with padding space
22383 on the left or right. */
22384 if (c != '\t')
22386 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22387 -1, Qnil);
22389 face = get_char_face_and_encoding (s->f, c, face_id,
22390 s->char2b + i, 1);
22391 if (face)
22393 if (! s->face)
22395 s->face = face;
22396 s->font = s->face->font;
22398 else if (s->face != face)
22399 break;
22402 ++s->nchars;
22404 s->cmp_to = i;
22406 if (s->face == NULL)
22408 s->face = base_face->ascii_face;
22409 s->font = s->face->font;
22412 /* All glyph strings for the same composition has the same width,
22413 i.e. the width set for the first component of the composition. */
22414 s->width = s->first_glyph->pixel_width;
22416 /* If the specified font could not be loaded, use the frame's
22417 default font, but record the fact that we couldn't load it in
22418 the glyph string so that we can draw rectangles for the
22419 characters of the glyph string. */
22420 if (s->font == NULL)
22422 s->font_not_found_p = 1;
22423 s->font = FRAME_FONT (s->f);
22426 /* Adjust base line for subscript/superscript text. */
22427 s->ybase += s->first_glyph->voffset;
22429 /* This glyph string must always be drawn with 16-bit functions. */
22430 s->two_byte_p = 1;
22432 return s->cmp_to;
22435 static int
22436 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22437 int start, int end, int overlaps)
22439 struct glyph *glyph, *last;
22440 Lisp_Object lgstring;
22441 int i;
22443 s->for_overlaps = overlaps;
22444 glyph = s->row->glyphs[s->area] + start;
22445 last = s->row->glyphs[s->area] + end;
22446 s->cmp_id = glyph->u.cmp.id;
22447 s->cmp_from = glyph->slice.cmp.from;
22448 s->cmp_to = glyph->slice.cmp.to + 1;
22449 s->face = FACE_FROM_ID (s->f, face_id);
22450 lgstring = composition_gstring_from_id (s->cmp_id);
22451 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22452 glyph++;
22453 while (glyph < last
22454 && glyph->u.cmp.automatic
22455 && glyph->u.cmp.id == s->cmp_id
22456 && s->cmp_to == glyph->slice.cmp.from)
22457 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22459 for (i = s->cmp_from; i < s->cmp_to; i++)
22461 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22462 unsigned code = LGLYPH_CODE (lglyph);
22464 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22466 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22467 return glyph - s->row->glyphs[s->area];
22471 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22472 See the comment of fill_glyph_string for arguments.
22473 Value is the index of the first glyph not in S. */
22476 static int
22477 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22478 int start, int end, int overlaps)
22480 struct glyph *glyph, *last;
22481 int voffset;
22483 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22484 s->for_overlaps = overlaps;
22485 glyph = s->row->glyphs[s->area] + start;
22486 last = s->row->glyphs[s->area] + end;
22487 voffset = glyph->voffset;
22488 s->face = FACE_FROM_ID (s->f, face_id);
22489 s->font = s->face->font;
22490 s->nchars = 1;
22491 s->width = glyph->pixel_width;
22492 glyph++;
22493 while (glyph < last
22494 && glyph->type == GLYPHLESS_GLYPH
22495 && glyph->voffset == voffset
22496 && glyph->face_id == face_id)
22498 s->nchars++;
22499 s->width += glyph->pixel_width;
22500 glyph++;
22502 s->ybase += voffset;
22503 return glyph - s->row->glyphs[s->area];
22507 /* Fill glyph string S from a sequence of character glyphs.
22509 FACE_ID is the face id of the string. START is the index of the
22510 first glyph to consider, END is the index of the last + 1.
22511 OVERLAPS non-zero means S should draw the foreground only, and use
22512 its physical height for clipping. See also draw_glyphs.
22514 Value is the index of the first glyph not in S. */
22516 static int
22517 fill_glyph_string (struct glyph_string *s, int face_id,
22518 int start, int end, int overlaps)
22520 struct glyph *glyph, *last;
22521 int voffset;
22522 int glyph_not_available_p;
22524 xassert (s->f == XFRAME (s->w->frame));
22525 xassert (s->nchars == 0);
22526 xassert (start >= 0 && end > start);
22528 s->for_overlaps = overlaps;
22529 glyph = s->row->glyphs[s->area] + start;
22530 last = s->row->glyphs[s->area] + end;
22531 voffset = glyph->voffset;
22532 s->padding_p = glyph->padding_p;
22533 glyph_not_available_p = glyph->glyph_not_available_p;
22535 while (glyph < last
22536 && glyph->type == CHAR_GLYPH
22537 && glyph->voffset == voffset
22538 /* Same face id implies same font, nowadays. */
22539 && glyph->face_id == face_id
22540 && glyph->glyph_not_available_p == glyph_not_available_p)
22542 int two_byte_p;
22544 s->face = get_glyph_face_and_encoding (s->f, glyph,
22545 s->char2b + s->nchars,
22546 &two_byte_p);
22547 s->two_byte_p = two_byte_p;
22548 ++s->nchars;
22549 xassert (s->nchars <= end - start);
22550 s->width += glyph->pixel_width;
22551 if (glyph++->padding_p != s->padding_p)
22552 break;
22555 s->font = s->face->font;
22557 /* If the specified font could not be loaded, use the frame's font,
22558 but record the fact that we couldn't load it in
22559 S->font_not_found_p so that we can draw rectangles for the
22560 characters of the glyph string. */
22561 if (s->font == NULL || glyph_not_available_p)
22563 s->font_not_found_p = 1;
22564 s->font = FRAME_FONT (s->f);
22567 /* Adjust base line for subscript/superscript text. */
22568 s->ybase += voffset;
22570 xassert (s->face && s->face->gc);
22571 return glyph - s->row->glyphs[s->area];
22575 /* Fill glyph string S from image glyph S->first_glyph. */
22577 static void
22578 fill_image_glyph_string (struct glyph_string *s)
22580 xassert (s->first_glyph->type == IMAGE_GLYPH);
22581 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22582 xassert (s->img);
22583 s->slice = s->first_glyph->slice.img;
22584 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22585 s->font = s->face->font;
22586 s->width = s->first_glyph->pixel_width;
22588 /* Adjust base line for subscript/superscript text. */
22589 s->ybase += s->first_glyph->voffset;
22593 /* Fill glyph string S from a sequence of stretch glyphs.
22595 START is the index of the first glyph to consider,
22596 END is the index of the last + 1.
22598 Value is the index of the first glyph not in S. */
22600 static int
22601 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22603 struct glyph *glyph, *last;
22604 int voffset, face_id;
22606 xassert (s->first_glyph->type == STRETCH_GLYPH);
22608 glyph = s->row->glyphs[s->area] + start;
22609 last = s->row->glyphs[s->area] + end;
22610 face_id = glyph->face_id;
22611 s->face = FACE_FROM_ID (s->f, face_id);
22612 s->font = s->face->font;
22613 s->width = glyph->pixel_width;
22614 s->nchars = 1;
22615 voffset = glyph->voffset;
22617 for (++glyph;
22618 (glyph < last
22619 && glyph->type == STRETCH_GLYPH
22620 && glyph->voffset == voffset
22621 && glyph->face_id == face_id);
22622 ++glyph)
22623 s->width += glyph->pixel_width;
22625 /* Adjust base line for subscript/superscript text. */
22626 s->ybase += voffset;
22628 /* The case that face->gc == 0 is handled when drawing the glyph
22629 string by calling PREPARE_FACE_FOR_DISPLAY. */
22630 xassert (s->face);
22631 return glyph - s->row->glyphs[s->area];
22634 static struct font_metrics *
22635 get_per_char_metric (struct font *font, XChar2b *char2b)
22637 static struct font_metrics metrics;
22638 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22640 if (! font || code == FONT_INVALID_CODE)
22641 return NULL;
22642 font->driver->text_extents (font, &code, 1, &metrics);
22643 return &metrics;
22646 /* EXPORT for RIF:
22647 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22648 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22649 assumed to be zero. */
22651 void
22652 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22654 *left = *right = 0;
22656 if (glyph->type == CHAR_GLYPH)
22658 struct face *face;
22659 XChar2b char2b;
22660 struct font_metrics *pcm;
22662 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22663 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22665 if (pcm->rbearing > pcm->width)
22666 *right = pcm->rbearing - pcm->width;
22667 if (pcm->lbearing < 0)
22668 *left = -pcm->lbearing;
22671 else if (glyph->type == COMPOSITE_GLYPH)
22673 if (! glyph->u.cmp.automatic)
22675 struct composition *cmp = composition_table[glyph->u.cmp.id];
22677 if (cmp->rbearing > cmp->pixel_width)
22678 *right = cmp->rbearing - cmp->pixel_width;
22679 if (cmp->lbearing < 0)
22680 *left = - cmp->lbearing;
22682 else
22684 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22685 struct font_metrics metrics;
22687 composition_gstring_width (gstring, glyph->slice.cmp.from,
22688 glyph->slice.cmp.to + 1, &metrics);
22689 if (metrics.rbearing > metrics.width)
22690 *right = metrics.rbearing - metrics.width;
22691 if (metrics.lbearing < 0)
22692 *left = - metrics.lbearing;
22698 /* Return the index of the first glyph preceding glyph string S that
22699 is overwritten by S because of S's left overhang. Value is -1
22700 if no glyphs are overwritten. */
22702 static int
22703 left_overwritten (struct glyph_string *s)
22705 int k;
22707 if (s->left_overhang)
22709 int x = 0, i;
22710 struct glyph *glyphs = s->row->glyphs[s->area];
22711 int first = s->first_glyph - glyphs;
22713 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22714 x -= glyphs[i].pixel_width;
22716 k = i + 1;
22718 else
22719 k = -1;
22721 return k;
22725 /* Return the index of the first glyph preceding glyph string S that
22726 is overwriting S because of its right overhang. Value is -1 if no
22727 glyph in front of S overwrites S. */
22729 static int
22730 left_overwriting (struct glyph_string *s)
22732 int i, k, x;
22733 struct glyph *glyphs = s->row->glyphs[s->area];
22734 int first = s->first_glyph - glyphs;
22736 k = -1;
22737 x = 0;
22738 for (i = first - 1; i >= 0; --i)
22740 int left, right;
22741 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22742 if (x + right > 0)
22743 k = i;
22744 x -= glyphs[i].pixel_width;
22747 return k;
22751 /* Return the index of the last glyph following glyph string S that is
22752 overwritten by S because of S's right overhang. Value is -1 if
22753 no such glyph is found. */
22755 static int
22756 right_overwritten (struct glyph_string *s)
22758 int k = -1;
22760 if (s->right_overhang)
22762 int x = 0, i;
22763 struct glyph *glyphs = s->row->glyphs[s->area];
22764 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22765 int end = s->row->used[s->area];
22767 for (i = first; i < end && s->right_overhang > x; ++i)
22768 x += glyphs[i].pixel_width;
22770 k = i;
22773 return k;
22777 /* Return the index of the last glyph following glyph string S that
22778 overwrites S because of its left overhang. Value is negative
22779 if no such glyph is found. */
22781 static int
22782 right_overwriting (struct glyph_string *s)
22784 int i, k, x;
22785 int end = s->row->used[s->area];
22786 struct glyph *glyphs = s->row->glyphs[s->area];
22787 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22789 k = -1;
22790 x = 0;
22791 for (i = first; i < end; ++i)
22793 int left, right;
22794 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22795 if (x - left < 0)
22796 k = i;
22797 x += glyphs[i].pixel_width;
22800 return k;
22804 /* Set background width of glyph string S. START is the index of the
22805 first glyph following S. LAST_X is the right-most x-position + 1
22806 in the drawing area. */
22808 static inline void
22809 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22811 /* If the face of this glyph string has to be drawn to the end of
22812 the drawing area, set S->extends_to_end_of_line_p. */
22814 if (start == s->row->used[s->area]
22815 && s->area == TEXT_AREA
22816 && ((s->row->fill_line_p
22817 && (s->hl == DRAW_NORMAL_TEXT
22818 || s->hl == DRAW_IMAGE_RAISED
22819 || s->hl == DRAW_IMAGE_SUNKEN))
22820 || s->hl == DRAW_MOUSE_FACE))
22821 s->extends_to_end_of_line_p = 1;
22823 /* If S extends its face to the end of the line, set its
22824 background_width to the distance to the right edge of the drawing
22825 area. */
22826 if (s->extends_to_end_of_line_p)
22827 s->background_width = last_x - s->x + 1;
22828 else
22829 s->background_width = s->width;
22833 /* Compute overhangs and x-positions for glyph string S and its
22834 predecessors, or successors. X is the starting x-position for S.
22835 BACKWARD_P non-zero means process predecessors. */
22837 static void
22838 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22840 if (backward_p)
22842 while (s)
22844 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22845 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22846 x -= s->width;
22847 s->x = x;
22848 s = s->prev;
22851 else
22853 while (s)
22855 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22856 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22857 s->x = x;
22858 x += s->width;
22859 s = s->next;
22866 /* The following macros are only called from draw_glyphs below.
22867 They reference the following parameters of that function directly:
22868 `w', `row', `area', and `overlap_p'
22869 as well as the following local variables:
22870 `s', `f', and `hdc' (in W32) */
22872 #ifdef HAVE_NTGUI
22873 /* On W32, silently add local `hdc' variable to argument list of
22874 init_glyph_string. */
22875 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22876 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22877 #else
22878 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22879 init_glyph_string (s, char2b, w, row, area, start, hl)
22880 #endif
22882 /* Add a glyph string for a stretch glyph to the list of strings
22883 between HEAD and TAIL. START is the index of the stretch glyph in
22884 row area AREA of glyph row ROW. END is the index of the last glyph
22885 in that glyph row area. X is the current output position assigned
22886 to the new glyph string constructed. HL overrides that face of the
22887 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22888 is the right-most x-position of the drawing area. */
22890 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22891 and below -- keep them on one line. */
22892 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22893 do \
22895 s = (struct glyph_string *) alloca (sizeof *s); \
22896 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22897 START = fill_stretch_glyph_string (s, START, END); \
22898 append_glyph_string (&HEAD, &TAIL, s); \
22899 s->x = (X); \
22901 while (0)
22904 /* Add a glyph string for an image glyph to the list of strings
22905 between HEAD and TAIL. START is the index of the image glyph in
22906 row area AREA of glyph row ROW. END is the index of the last glyph
22907 in that glyph row area. X is the current output position assigned
22908 to the new glyph string constructed. HL overrides that face of the
22909 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22910 is the right-most x-position of the drawing area. */
22912 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22913 do \
22915 s = (struct glyph_string *) alloca (sizeof *s); \
22916 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22917 fill_image_glyph_string (s); \
22918 append_glyph_string (&HEAD, &TAIL, s); \
22919 ++START; \
22920 s->x = (X); \
22922 while (0)
22925 /* Add a glyph string for a sequence of character glyphs to the list
22926 of strings between HEAD and TAIL. START is the index of the first
22927 glyph in row area AREA of glyph row ROW that is part of the new
22928 glyph string. END is the index of the last glyph in that glyph row
22929 area. X is the current output position assigned to the new glyph
22930 string constructed. HL overrides that face of the glyph; e.g. it
22931 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22932 right-most x-position of the drawing area. */
22934 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22935 do \
22937 int face_id; \
22938 XChar2b *char2b; \
22940 face_id = (row)->glyphs[area][START].face_id; \
22942 s = (struct glyph_string *) alloca (sizeof *s); \
22943 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22944 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22945 append_glyph_string (&HEAD, &TAIL, s); \
22946 s->x = (X); \
22947 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22949 while (0)
22952 /* Add a glyph string for a composite sequence to the list of strings
22953 between HEAD and TAIL. START is the index of the first glyph in
22954 row area AREA of glyph row ROW that is part of the new glyph
22955 string. END is the index of the last glyph in that glyph row area.
22956 X is the current output position assigned to the new glyph string
22957 constructed. HL overrides that face of the glyph; e.g. it is
22958 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22959 x-position of the drawing area. */
22961 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22962 do { \
22963 int face_id = (row)->glyphs[area][START].face_id; \
22964 struct face *base_face = FACE_FROM_ID (f, face_id); \
22965 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22966 struct composition *cmp = composition_table[cmp_id]; \
22967 XChar2b *char2b; \
22968 struct glyph_string *first_s = NULL; \
22969 int n; \
22971 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22973 /* Make glyph_strings for each glyph sequence that is drawable by \
22974 the same face, and append them to HEAD/TAIL. */ \
22975 for (n = 0; n < cmp->glyph_len;) \
22977 s = (struct glyph_string *) alloca (sizeof *s); \
22978 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22979 append_glyph_string (&(HEAD), &(TAIL), s); \
22980 s->cmp = cmp; \
22981 s->cmp_from = n; \
22982 s->x = (X); \
22983 if (n == 0) \
22984 first_s = s; \
22985 n = fill_composite_glyph_string (s, base_face, overlaps); \
22988 ++START; \
22989 s = first_s; \
22990 } while (0)
22993 /* Add a glyph string for a glyph-string sequence to the list of strings
22994 between HEAD and TAIL. */
22996 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22997 do { \
22998 int face_id; \
22999 XChar2b *char2b; \
23000 Lisp_Object gstring; \
23002 face_id = (row)->glyphs[area][START].face_id; \
23003 gstring = (composition_gstring_from_id \
23004 ((row)->glyphs[area][START].u.cmp.id)); \
23005 s = (struct glyph_string *) alloca (sizeof *s); \
23006 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23007 * LGSTRING_GLYPH_LEN (gstring)); \
23008 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23009 append_glyph_string (&(HEAD), &(TAIL), s); \
23010 s->x = (X); \
23011 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23012 } while (0)
23015 /* Add a glyph string for a sequence of glyphless character's glyphs
23016 to the list of strings between HEAD and TAIL. The meanings of
23017 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23019 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23020 do \
23022 int face_id; \
23024 face_id = (row)->glyphs[area][START].face_id; \
23026 s = (struct glyph_string *) alloca (sizeof *s); \
23027 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23028 append_glyph_string (&HEAD, &TAIL, s); \
23029 s->x = (X); \
23030 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23031 overlaps); \
23033 while (0)
23036 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23037 of AREA of glyph row ROW on window W between indices START and END.
23038 HL overrides the face for drawing glyph strings, e.g. it is
23039 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23040 x-positions of the drawing area.
23042 This is an ugly monster macro construct because we must use alloca
23043 to allocate glyph strings (because draw_glyphs can be called
23044 asynchronously). */
23046 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23047 do \
23049 HEAD = TAIL = NULL; \
23050 while (START < END) \
23052 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23053 switch (first_glyph->type) \
23055 case CHAR_GLYPH: \
23056 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23057 HL, X, LAST_X); \
23058 break; \
23060 case COMPOSITE_GLYPH: \
23061 if (first_glyph->u.cmp.automatic) \
23062 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23063 HL, X, LAST_X); \
23064 else \
23065 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23066 HL, X, LAST_X); \
23067 break; \
23069 case STRETCH_GLYPH: \
23070 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23071 HL, X, LAST_X); \
23072 break; \
23074 case IMAGE_GLYPH: \
23075 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23076 HL, X, LAST_X); \
23077 break; \
23079 case GLYPHLESS_GLYPH: \
23080 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23081 HL, X, LAST_X); \
23082 break; \
23084 default: \
23085 abort (); \
23088 if (s) \
23090 set_glyph_string_background_width (s, START, LAST_X); \
23091 (X) += s->width; \
23094 } while (0)
23097 /* Draw glyphs between START and END in AREA of ROW on window W,
23098 starting at x-position X. X is relative to AREA in W. HL is a
23099 face-override with the following meaning:
23101 DRAW_NORMAL_TEXT draw normally
23102 DRAW_CURSOR draw in cursor face
23103 DRAW_MOUSE_FACE draw in mouse face.
23104 DRAW_INVERSE_VIDEO draw in mode line face
23105 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23106 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23108 If OVERLAPS is non-zero, draw only the foreground of characters and
23109 clip to the physical height of ROW. Non-zero value also defines
23110 the overlapping part to be drawn:
23112 OVERLAPS_PRED overlap with preceding rows
23113 OVERLAPS_SUCC overlap with succeeding rows
23114 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23115 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23117 Value is the x-position reached, relative to AREA of W. */
23119 static int
23120 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23121 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23122 enum draw_glyphs_face hl, int overlaps)
23124 struct glyph_string *head, *tail;
23125 struct glyph_string *s;
23126 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23127 int i, j, x_reached, last_x, area_left = 0;
23128 struct frame *f = XFRAME (WINDOW_FRAME (w));
23129 DECLARE_HDC (hdc);
23131 ALLOCATE_HDC (hdc, f);
23133 /* Let's rather be paranoid than getting a SEGV. */
23134 end = min (end, row->used[area]);
23135 start = max (0, start);
23136 start = min (end, start);
23138 /* Translate X to frame coordinates. Set last_x to the right
23139 end of the drawing area. */
23140 if (row->full_width_p)
23142 /* X is relative to the left edge of W, without scroll bars
23143 or fringes. */
23144 area_left = WINDOW_LEFT_EDGE_X (w);
23145 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23147 else
23149 area_left = window_box_left (w, area);
23150 last_x = area_left + window_box_width (w, area);
23152 x += area_left;
23154 /* Build a doubly-linked list of glyph_string structures between
23155 head and tail from what we have to draw. Note that the macro
23156 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23157 the reason we use a separate variable `i'. */
23158 i = start;
23159 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23160 if (tail)
23161 x_reached = tail->x + tail->background_width;
23162 else
23163 x_reached = x;
23165 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23166 the row, redraw some glyphs in front or following the glyph
23167 strings built above. */
23168 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23170 struct glyph_string *h, *t;
23171 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23172 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23173 int check_mouse_face = 0;
23174 int dummy_x = 0;
23176 /* If mouse highlighting is on, we may need to draw adjacent
23177 glyphs using mouse-face highlighting. */
23178 if (area == TEXT_AREA && row->mouse_face_p)
23180 struct glyph_row *mouse_beg_row, *mouse_end_row;
23182 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23183 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23185 if (row >= mouse_beg_row && row <= mouse_end_row)
23187 check_mouse_face = 1;
23188 mouse_beg_col = (row == mouse_beg_row)
23189 ? hlinfo->mouse_face_beg_col : 0;
23190 mouse_end_col = (row == mouse_end_row)
23191 ? hlinfo->mouse_face_end_col
23192 : row->used[TEXT_AREA];
23196 /* Compute overhangs for all glyph strings. */
23197 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23198 for (s = head; s; s = s->next)
23199 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23201 /* Prepend glyph strings for glyphs in front of the first glyph
23202 string that are overwritten because of the first glyph
23203 string's left overhang. The background of all strings
23204 prepended must be drawn because the first glyph string
23205 draws over it. */
23206 i = left_overwritten (head);
23207 if (i >= 0)
23209 enum draw_glyphs_face overlap_hl;
23211 /* If this row contains mouse highlighting, attempt to draw
23212 the overlapped glyphs with the correct highlight. This
23213 code fails if the overlap encompasses more than one glyph
23214 and mouse-highlight spans only some of these glyphs.
23215 However, making it work perfectly involves a lot more
23216 code, and I don't know if the pathological case occurs in
23217 practice, so we'll stick to this for now. --- cyd */
23218 if (check_mouse_face
23219 && mouse_beg_col < start && mouse_end_col > i)
23220 overlap_hl = DRAW_MOUSE_FACE;
23221 else
23222 overlap_hl = DRAW_NORMAL_TEXT;
23224 j = i;
23225 BUILD_GLYPH_STRINGS (j, start, h, t,
23226 overlap_hl, dummy_x, last_x);
23227 start = i;
23228 compute_overhangs_and_x (t, head->x, 1);
23229 prepend_glyph_string_lists (&head, &tail, h, t);
23230 clip_head = head;
23233 /* Prepend glyph strings for glyphs in front of the first glyph
23234 string that overwrite that glyph string because of their
23235 right overhang. For these strings, only the foreground must
23236 be drawn, because it draws over the glyph string at `head'.
23237 The background must not be drawn because this would overwrite
23238 right overhangs of preceding glyphs for which no glyph
23239 strings exist. */
23240 i = left_overwriting (head);
23241 if (i >= 0)
23243 enum draw_glyphs_face overlap_hl;
23245 if (check_mouse_face
23246 && mouse_beg_col < start && mouse_end_col > i)
23247 overlap_hl = DRAW_MOUSE_FACE;
23248 else
23249 overlap_hl = DRAW_NORMAL_TEXT;
23251 clip_head = head;
23252 BUILD_GLYPH_STRINGS (i, start, h, t,
23253 overlap_hl, dummy_x, last_x);
23254 for (s = h; s; s = s->next)
23255 s->background_filled_p = 1;
23256 compute_overhangs_and_x (t, head->x, 1);
23257 prepend_glyph_string_lists (&head, &tail, h, t);
23260 /* Append glyphs strings for glyphs following the last glyph
23261 string tail that are overwritten by tail. The background of
23262 these strings has to be drawn because tail's foreground draws
23263 over it. */
23264 i = right_overwritten (tail);
23265 if (i >= 0)
23267 enum draw_glyphs_face overlap_hl;
23269 if (check_mouse_face
23270 && mouse_beg_col < i && mouse_end_col > end)
23271 overlap_hl = DRAW_MOUSE_FACE;
23272 else
23273 overlap_hl = DRAW_NORMAL_TEXT;
23275 BUILD_GLYPH_STRINGS (end, i, h, t,
23276 overlap_hl, x, last_x);
23277 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23278 we don't have `end = i;' here. */
23279 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23280 append_glyph_string_lists (&head, &tail, h, t);
23281 clip_tail = tail;
23284 /* Append glyph strings for glyphs following the last glyph
23285 string tail that overwrite tail. The foreground of such
23286 glyphs has to be drawn because it writes into the background
23287 of tail. The background must not be drawn because it could
23288 paint over the foreground of following glyphs. */
23289 i = right_overwriting (tail);
23290 if (i >= 0)
23292 enum draw_glyphs_face overlap_hl;
23293 if (check_mouse_face
23294 && mouse_beg_col < i && mouse_end_col > end)
23295 overlap_hl = DRAW_MOUSE_FACE;
23296 else
23297 overlap_hl = DRAW_NORMAL_TEXT;
23299 clip_tail = tail;
23300 i++; /* We must include the Ith glyph. */
23301 BUILD_GLYPH_STRINGS (end, i, h, t,
23302 overlap_hl, x, last_x);
23303 for (s = h; s; s = s->next)
23304 s->background_filled_p = 1;
23305 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23306 append_glyph_string_lists (&head, &tail, h, t);
23308 if (clip_head || clip_tail)
23309 for (s = head; s; s = s->next)
23311 s->clip_head = clip_head;
23312 s->clip_tail = clip_tail;
23316 /* Draw all strings. */
23317 for (s = head; s; s = s->next)
23318 FRAME_RIF (f)->draw_glyph_string (s);
23320 #ifndef HAVE_NS
23321 /* When focus a sole frame and move horizontally, this sets on_p to 0
23322 causing a failure to erase prev cursor position. */
23323 if (area == TEXT_AREA
23324 && !row->full_width_p
23325 /* When drawing overlapping rows, only the glyph strings'
23326 foreground is drawn, which doesn't erase a cursor
23327 completely. */
23328 && !overlaps)
23330 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23331 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23332 : (tail ? tail->x + tail->background_width : x));
23333 x0 -= area_left;
23334 x1 -= area_left;
23336 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23337 row->y, MATRIX_ROW_BOTTOM_Y (row));
23339 #endif
23341 /* Value is the x-position up to which drawn, relative to AREA of W.
23342 This doesn't include parts drawn because of overhangs. */
23343 if (row->full_width_p)
23344 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23345 else
23346 x_reached -= area_left;
23348 RELEASE_HDC (hdc, f);
23350 return x_reached;
23353 /* Expand row matrix if too narrow. Don't expand if area
23354 is not present. */
23356 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23358 if (!fonts_changed_p \
23359 && (it->glyph_row->glyphs[area] \
23360 < it->glyph_row->glyphs[area + 1])) \
23362 it->w->ncols_scale_factor++; \
23363 fonts_changed_p = 1; \
23367 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23368 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23370 static inline void
23371 append_glyph (struct it *it)
23373 struct glyph *glyph;
23374 enum glyph_row_area area = it->area;
23376 xassert (it->glyph_row);
23377 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23379 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23380 if (glyph < it->glyph_row->glyphs[area + 1])
23382 /* If the glyph row is reversed, we need to prepend the glyph
23383 rather than append it. */
23384 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23386 struct glyph *g;
23388 /* Make room for the additional glyph. */
23389 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23390 g[1] = *g;
23391 glyph = it->glyph_row->glyphs[area];
23393 glyph->charpos = CHARPOS (it->position);
23394 glyph->object = it->object;
23395 if (it->pixel_width > 0)
23397 glyph->pixel_width = it->pixel_width;
23398 glyph->padding_p = 0;
23400 else
23402 /* Assure at least 1-pixel width. Otherwise, cursor can't
23403 be displayed correctly. */
23404 glyph->pixel_width = 1;
23405 glyph->padding_p = 1;
23407 glyph->ascent = it->ascent;
23408 glyph->descent = it->descent;
23409 glyph->voffset = it->voffset;
23410 glyph->type = CHAR_GLYPH;
23411 glyph->avoid_cursor_p = it->avoid_cursor_p;
23412 glyph->multibyte_p = it->multibyte_p;
23413 glyph->left_box_line_p = it->start_of_box_run_p;
23414 glyph->right_box_line_p = it->end_of_box_run_p;
23415 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23416 || it->phys_descent > it->descent);
23417 glyph->glyph_not_available_p = it->glyph_not_available_p;
23418 glyph->face_id = it->face_id;
23419 glyph->u.ch = it->char_to_display;
23420 glyph->slice.img = null_glyph_slice;
23421 glyph->font_type = FONT_TYPE_UNKNOWN;
23422 if (it->bidi_p)
23424 glyph->resolved_level = it->bidi_it.resolved_level;
23425 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23426 abort ();
23427 glyph->bidi_type = it->bidi_it.type;
23429 else
23431 glyph->resolved_level = 0;
23432 glyph->bidi_type = UNKNOWN_BT;
23434 ++it->glyph_row->used[area];
23436 else
23437 IT_EXPAND_MATRIX_WIDTH (it, area);
23440 /* Store one glyph for the composition IT->cmp_it.id in
23441 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23442 non-null. */
23444 static inline void
23445 append_composite_glyph (struct it *it)
23447 struct glyph *glyph;
23448 enum glyph_row_area area = it->area;
23450 xassert (it->glyph_row);
23452 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23453 if (glyph < it->glyph_row->glyphs[area + 1])
23455 /* If the glyph row is reversed, we need to prepend the glyph
23456 rather than append it. */
23457 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23459 struct glyph *g;
23461 /* Make room for the new glyph. */
23462 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23463 g[1] = *g;
23464 glyph = it->glyph_row->glyphs[it->area];
23466 glyph->charpos = it->cmp_it.charpos;
23467 glyph->object = it->object;
23468 glyph->pixel_width = it->pixel_width;
23469 glyph->ascent = it->ascent;
23470 glyph->descent = it->descent;
23471 glyph->voffset = it->voffset;
23472 glyph->type = COMPOSITE_GLYPH;
23473 if (it->cmp_it.ch < 0)
23475 glyph->u.cmp.automatic = 0;
23476 glyph->u.cmp.id = it->cmp_it.id;
23477 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23479 else
23481 glyph->u.cmp.automatic = 1;
23482 glyph->u.cmp.id = it->cmp_it.id;
23483 glyph->slice.cmp.from = it->cmp_it.from;
23484 glyph->slice.cmp.to = it->cmp_it.to - 1;
23486 glyph->avoid_cursor_p = it->avoid_cursor_p;
23487 glyph->multibyte_p = it->multibyte_p;
23488 glyph->left_box_line_p = it->start_of_box_run_p;
23489 glyph->right_box_line_p = it->end_of_box_run_p;
23490 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23491 || it->phys_descent > it->descent);
23492 glyph->padding_p = 0;
23493 glyph->glyph_not_available_p = 0;
23494 glyph->face_id = it->face_id;
23495 glyph->font_type = FONT_TYPE_UNKNOWN;
23496 if (it->bidi_p)
23498 glyph->resolved_level = it->bidi_it.resolved_level;
23499 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23500 abort ();
23501 glyph->bidi_type = it->bidi_it.type;
23503 ++it->glyph_row->used[area];
23505 else
23506 IT_EXPAND_MATRIX_WIDTH (it, area);
23510 /* Change IT->ascent and IT->height according to the setting of
23511 IT->voffset. */
23513 static inline void
23514 take_vertical_position_into_account (struct it *it)
23516 if (it->voffset)
23518 if (it->voffset < 0)
23519 /* Increase the ascent so that we can display the text higher
23520 in the line. */
23521 it->ascent -= it->voffset;
23522 else
23523 /* Increase the descent so that we can display the text lower
23524 in the line. */
23525 it->descent += it->voffset;
23530 /* Produce glyphs/get display metrics for the image IT is loaded with.
23531 See the description of struct display_iterator in dispextern.h for
23532 an overview of struct display_iterator. */
23534 static void
23535 produce_image_glyph (struct it *it)
23537 struct image *img;
23538 struct face *face;
23539 int glyph_ascent, crop;
23540 struct glyph_slice slice;
23542 xassert (it->what == IT_IMAGE);
23544 face = FACE_FROM_ID (it->f, it->face_id);
23545 xassert (face);
23546 /* Make sure X resources of the face is loaded. */
23547 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23549 if (it->image_id < 0)
23551 /* Fringe bitmap. */
23552 it->ascent = it->phys_ascent = 0;
23553 it->descent = it->phys_descent = 0;
23554 it->pixel_width = 0;
23555 it->nglyphs = 0;
23556 return;
23559 img = IMAGE_FROM_ID (it->f, it->image_id);
23560 xassert (img);
23561 /* Make sure X resources of the image is loaded. */
23562 prepare_image_for_display (it->f, img);
23564 slice.x = slice.y = 0;
23565 slice.width = img->width;
23566 slice.height = img->height;
23568 if (INTEGERP (it->slice.x))
23569 slice.x = XINT (it->slice.x);
23570 else if (FLOATP (it->slice.x))
23571 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23573 if (INTEGERP (it->slice.y))
23574 slice.y = XINT (it->slice.y);
23575 else if (FLOATP (it->slice.y))
23576 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23578 if (INTEGERP (it->slice.width))
23579 slice.width = XINT (it->slice.width);
23580 else if (FLOATP (it->slice.width))
23581 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23583 if (INTEGERP (it->slice.height))
23584 slice.height = XINT (it->slice.height);
23585 else if (FLOATP (it->slice.height))
23586 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23588 if (slice.x >= img->width)
23589 slice.x = img->width;
23590 if (slice.y >= img->height)
23591 slice.y = img->height;
23592 if (slice.x + slice.width >= img->width)
23593 slice.width = img->width - slice.x;
23594 if (slice.y + slice.height > img->height)
23595 slice.height = img->height - slice.y;
23597 if (slice.width == 0 || slice.height == 0)
23598 return;
23600 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23602 it->descent = slice.height - glyph_ascent;
23603 if (slice.y == 0)
23604 it->descent += img->vmargin;
23605 if (slice.y + slice.height == img->height)
23606 it->descent += img->vmargin;
23607 it->phys_descent = it->descent;
23609 it->pixel_width = slice.width;
23610 if (slice.x == 0)
23611 it->pixel_width += img->hmargin;
23612 if (slice.x + slice.width == img->width)
23613 it->pixel_width += img->hmargin;
23615 /* It's quite possible for images to have an ascent greater than
23616 their height, so don't get confused in that case. */
23617 if (it->descent < 0)
23618 it->descent = 0;
23620 it->nglyphs = 1;
23622 if (face->box != FACE_NO_BOX)
23624 if (face->box_line_width > 0)
23626 if (slice.y == 0)
23627 it->ascent += face->box_line_width;
23628 if (slice.y + slice.height == img->height)
23629 it->descent += face->box_line_width;
23632 if (it->start_of_box_run_p && slice.x == 0)
23633 it->pixel_width += eabs (face->box_line_width);
23634 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23635 it->pixel_width += eabs (face->box_line_width);
23638 take_vertical_position_into_account (it);
23640 /* Automatically crop wide image glyphs at right edge so we can
23641 draw the cursor on same display row. */
23642 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23643 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23645 it->pixel_width -= crop;
23646 slice.width -= crop;
23649 if (it->glyph_row)
23651 struct glyph *glyph;
23652 enum glyph_row_area area = it->area;
23654 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23655 if (glyph < it->glyph_row->glyphs[area + 1])
23657 glyph->charpos = CHARPOS (it->position);
23658 glyph->object = it->object;
23659 glyph->pixel_width = it->pixel_width;
23660 glyph->ascent = glyph_ascent;
23661 glyph->descent = it->descent;
23662 glyph->voffset = it->voffset;
23663 glyph->type = IMAGE_GLYPH;
23664 glyph->avoid_cursor_p = it->avoid_cursor_p;
23665 glyph->multibyte_p = it->multibyte_p;
23666 glyph->left_box_line_p = it->start_of_box_run_p;
23667 glyph->right_box_line_p = it->end_of_box_run_p;
23668 glyph->overlaps_vertically_p = 0;
23669 glyph->padding_p = 0;
23670 glyph->glyph_not_available_p = 0;
23671 glyph->face_id = it->face_id;
23672 glyph->u.img_id = img->id;
23673 glyph->slice.img = slice;
23674 glyph->font_type = FONT_TYPE_UNKNOWN;
23675 if (it->bidi_p)
23677 glyph->resolved_level = it->bidi_it.resolved_level;
23678 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23679 abort ();
23680 glyph->bidi_type = it->bidi_it.type;
23682 ++it->glyph_row->used[area];
23684 else
23685 IT_EXPAND_MATRIX_WIDTH (it, area);
23690 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23691 of the glyph, WIDTH and HEIGHT are the width and height of the
23692 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23694 static void
23695 append_stretch_glyph (struct it *it, Lisp_Object object,
23696 int width, int height, int ascent)
23698 struct glyph *glyph;
23699 enum glyph_row_area area = it->area;
23701 xassert (ascent >= 0 && ascent <= height);
23703 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23704 if (glyph < it->glyph_row->glyphs[area + 1])
23706 /* If the glyph row is reversed, we need to prepend the glyph
23707 rather than append it. */
23708 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23710 struct glyph *g;
23712 /* Make room for the additional glyph. */
23713 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23714 g[1] = *g;
23715 glyph = it->glyph_row->glyphs[area];
23717 glyph->charpos = CHARPOS (it->position);
23718 glyph->object = object;
23719 glyph->pixel_width = width;
23720 glyph->ascent = ascent;
23721 glyph->descent = height - ascent;
23722 glyph->voffset = it->voffset;
23723 glyph->type = STRETCH_GLYPH;
23724 glyph->avoid_cursor_p = it->avoid_cursor_p;
23725 glyph->multibyte_p = it->multibyte_p;
23726 glyph->left_box_line_p = it->start_of_box_run_p;
23727 glyph->right_box_line_p = it->end_of_box_run_p;
23728 glyph->overlaps_vertically_p = 0;
23729 glyph->padding_p = 0;
23730 glyph->glyph_not_available_p = 0;
23731 glyph->face_id = it->face_id;
23732 glyph->u.stretch.ascent = ascent;
23733 glyph->u.stretch.height = height;
23734 glyph->slice.img = null_glyph_slice;
23735 glyph->font_type = FONT_TYPE_UNKNOWN;
23736 if (it->bidi_p)
23738 glyph->resolved_level = it->bidi_it.resolved_level;
23739 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23740 abort ();
23741 glyph->bidi_type = it->bidi_it.type;
23743 else
23745 glyph->resolved_level = 0;
23746 glyph->bidi_type = UNKNOWN_BT;
23748 ++it->glyph_row->used[area];
23750 else
23751 IT_EXPAND_MATRIX_WIDTH (it, area);
23754 #endif /* HAVE_WINDOW_SYSTEM */
23756 /* Produce a stretch glyph for iterator IT. IT->object is the value
23757 of the glyph property displayed. The value must be a list
23758 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23759 being recognized:
23761 1. `:width WIDTH' specifies that the space should be WIDTH *
23762 canonical char width wide. WIDTH may be an integer or floating
23763 point number.
23765 2. `:relative-width FACTOR' specifies that the width of the stretch
23766 should be computed from the width of the first character having the
23767 `glyph' property, and should be FACTOR times that width.
23769 3. `:align-to HPOS' specifies that the space should be wide enough
23770 to reach HPOS, a value in canonical character units.
23772 Exactly one of the above pairs must be present.
23774 4. `:height HEIGHT' specifies that the height of the stretch produced
23775 should be HEIGHT, measured in canonical character units.
23777 5. `:relative-height FACTOR' specifies that the height of the
23778 stretch should be FACTOR times the height of the characters having
23779 the glyph property.
23781 Either none or exactly one of 4 or 5 must be present.
23783 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23784 of the stretch should be used for the ascent of the stretch.
23785 ASCENT must be in the range 0 <= ASCENT <= 100. */
23787 void
23788 produce_stretch_glyph (struct it *it)
23790 /* (space :width WIDTH :height HEIGHT ...) */
23791 Lisp_Object prop, plist;
23792 int width = 0, height = 0, align_to = -1;
23793 int zero_width_ok_p = 0;
23794 int ascent = 0;
23795 double tem;
23796 struct face *face = NULL;
23797 struct font *font = NULL;
23799 #ifdef HAVE_WINDOW_SYSTEM
23800 int zero_height_ok_p = 0;
23802 if (FRAME_WINDOW_P (it->f))
23804 face = FACE_FROM_ID (it->f, it->face_id);
23805 font = face->font ? face->font : FRAME_FONT (it->f);
23806 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23808 #endif
23810 /* List should start with `space'. */
23811 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23812 plist = XCDR (it->object);
23814 /* Compute the width of the stretch. */
23815 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23816 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23818 /* Absolute width `:width WIDTH' specified and valid. */
23819 zero_width_ok_p = 1;
23820 width = (int)tem;
23822 #ifdef HAVE_WINDOW_SYSTEM
23823 else if (FRAME_WINDOW_P (it->f)
23824 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23826 /* Relative width `:relative-width FACTOR' specified and valid.
23827 Compute the width of the characters having the `glyph'
23828 property. */
23829 struct it it2;
23830 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23832 it2 = *it;
23833 if (it->multibyte_p)
23834 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23835 else
23837 it2.c = it2.char_to_display = *p, it2.len = 1;
23838 if (! ASCII_CHAR_P (it2.c))
23839 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23842 it2.glyph_row = NULL;
23843 it2.what = IT_CHARACTER;
23844 x_produce_glyphs (&it2);
23845 width = NUMVAL (prop) * it2.pixel_width;
23847 #endif /* HAVE_WINDOW_SYSTEM */
23848 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23849 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23851 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23852 align_to = (align_to < 0
23854 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23855 else if (align_to < 0)
23856 align_to = window_box_left_offset (it->w, TEXT_AREA);
23857 width = max (0, (int)tem + align_to - it->current_x);
23858 zero_width_ok_p = 1;
23860 else
23861 /* Nothing specified -> width defaults to canonical char width. */
23862 width = FRAME_COLUMN_WIDTH (it->f);
23864 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23865 width = 1;
23867 #ifdef HAVE_WINDOW_SYSTEM
23868 /* Compute height. */
23869 if (FRAME_WINDOW_P (it->f))
23871 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23872 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23874 height = (int)tem;
23875 zero_height_ok_p = 1;
23877 else if (prop = Fplist_get (plist, QCrelative_height),
23878 NUMVAL (prop) > 0)
23879 height = FONT_HEIGHT (font) * NUMVAL (prop);
23880 else
23881 height = FONT_HEIGHT (font);
23883 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23884 height = 1;
23886 /* Compute percentage of height used for ascent. If
23887 `:ascent ASCENT' is present and valid, use that. Otherwise,
23888 derive the ascent from the font in use. */
23889 if (prop = Fplist_get (plist, QCascent),
23890 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23891 ascent = height * NUMVAL (prop) / 100.0;
23892 else if (!NILP (prop)
23893 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23894 ascent = min (max (0, (int)tem), height);
23895 else
23896 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23898 else
23899 #endif /* HAVE_WINDOW_SYSTEM */
23900 height = 1;
23902 if (width > 0 && it->line_wrap != TRUNCATE
23903 && it->current_x + width > it->last_visible_x)
23905 width = it->last_visible_x - it->current_x;
23906 #ifdef HAVE_WINDOW_SYSTEM
23907 /* Subtract one more pixel from the stretch width, but only on
23908 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23909 width -= FRAME_WINDOW_P (it->f);
23910 #endif
23913 if (width > 0 && height > 0 && it->glyph_row)
23915 Lisp_Object o_object = it->object;
23916 Lisp_Object object = it->stack[it->sp - 1].string;
23917 int n = width;
23919 if (!STRINGP (object))
23920 object = it->w->buffer;
23921 #ifdef HAVE_WINDOW_SYSTEM
23922 if (FRAME_WINDOW_P (it->f))
23923 append_stretch_glyph (it, object, width, height, ascent);
23924 else
23925 #endif
23927 it->object = object;
23928 it->char_to_display = ' ';
23929 it->pixel_width = it->len = 1;
23930 while (n--)
23931 tty_append_glyph (it);
23932 it->object = o_object;
23936 it->pixel_width = width;
23937 #ifdef HAVE_WINDOW_SYSTEM
23938 if (FRAME_WINDOW_P (it->f))
23940 it->ascent = it->phys_ascent = ascent;
23941 it->descent = it->phys_descent = height - it->ascent;
23942 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23943 take_vertical_position_into_account (it);
23945 else
23946 #endif
23947 it->nglyphs = width;
23950 #ifdef HAVE_WINDOW_SYSTEM
23952 /* Calculate line-height and line-spacing properties.
23953 An integer value specifies explicit pixel value.
23954 A float value specifies relative value to current face height.
23955 A cons (float . face-name) specifies relative value to
23956 height of specified face font.
23958 Returns height in pixels, or nil. */
23961 static Lisp_Object
23962 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23963 int boff, int override)
23965 Lisp_Object face_name = Qnil;
23966 int ascent, descent, height;
23968 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23969 return val;
23971 if (CONSP (val))
23973 face_name = XCAR (val);
23974 val = XCDR (val);
23975 if (!NUMBERP (val))
23976 val = make_number (1);
23977 if (NILP (face_name))
23979 height = it->ascent + it->descent;
23980 goto scale;
23984 if (NILP (face_name))
23986 font = FRAME_FONT (it->f);
23987 boff = FRAME_BASELINE_OFFSET (it->f);
23989 else if (EQ (face_name, Qt))
23991 override = 0;
23993 else
23995 int face_id;
23996 struct face *face;
23998 face_id = lookup_named_face (it->f, face_name, 0);
23999 if (face_id < 0)
24000 return make_number (-1);
24002 face = FACE_FROM_ID (it->f, face_id);
24003 font = face->font;
24004 if (font == NULL)
24005 return make_number (-1);
24006 boff = font->baseline_offset;
24007 if (font->vertical_centering)
24008 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24011 ascent = FONT_BASE (font) + boff;
24012 descent = FONT_DESCENT (font) - boff;
24014 if (override)
24016 it->override_ascent = ascent;
24017 it->override_descent = descent;
24018 it->override_boff = boff;
24021 height = ascent + descent;
24023 scale:
24024 if (FLOATP (val))
24025 height = (int)(XFLOAT_DATA (val) * height);
24026 else if (INTEGERP (val))
24027 height *= XINT (val);
24029 return make_number (height);
24033 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24034 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24035 and only if this is for a character for which no font was found.
24037 If the display method (it->glyphless_method) is
24038 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24039 length of the acronym or the hexadecimal string, UPPER_XOFF and
24040 UPPER_YOFF are pixel offsets for the upper part of the string,
24041 LOWER_XOFF and LOWER_YOFF are for the lower part.
24043 For the other display methods, LEN through LOWER_YOFF are zero. */
24045 static void
24046 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24047 short upper_xoff, short upper_yoff,
24048 short lower_xoff, short lower_yoff)
24050 struct glyph *glyph;
24051 enum glyph_row_area area = it->area;
24053 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24054 if (glyph < it->glyph_row->glyphs[area + 1])
24056 /* If the glyph row is reversed, we need to prepend the glyph
24057 rather than append it. */
24058 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24060 struct glyph *g;
24062 /* Make room for the additional glyph. */
24063 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24064 g[1] = *g;
24065 glyph = it->glyph_row->glyphs[area];
24067 glyph->charpos = CHARPOS (it->position);
24068 glyph->object = it->object;
24069 glyph->pixel_width = it->pixel_width;
24070 glyph->ascent = it->ascent;
24071 glyph->descent = it->descent;
24072 glyph->voffset = it->voffset;
24073 glyph->type = GLYPHLESS_GLYPH;
24074 glyph->u.glyphless.method = it->glyphless_method;
24075 glyph->u.glyphless.for_no_font = for_no_font;
24076 glyph->u.glyphless.len = len;
24077 glyph->u.glyphless.ch = it->c;
24078 glyph->slice.glyphless.upper_xoff = upper_xoff;
24079 glyph->slice.glyphless.upper_yoff = upper_yoff;
24080 glyph->slice.glyphless.lower_xoff = lower_xoff;
24081 glyph->slice.glyphless.lower_yoff = lower_yoff;
24082 glyph->avoid_cursor_p = it->avoid_cursor_p;
24083 glyph->multibyte_p = it->multibyte_p;
24084 glyph->left_box_line_p = it->start_of_box_run_p;
24085 glyph->right_box_line_p = it->end_of_box_run_p;
24086 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24087 || it->phys_descent > it->descent);
24088 glyph->padding_p = 0;
24089 glyph->glyph_not_available_p = 0;
24090 glyph->face_id = face_id;
24091 glyph->font_type = FONT_TYPE_UNKNOWN;
24092 if (it->bidi_p)
24094 glyph->resolved_level = it->bidi_it.resolved_level;
24095 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24096 abort ();
24097 glyph->bidi_type = it->bidi_it.type;
24099 ++it->glyph_row->used[area];
24101 else
24102 IT_EXPAND_MATRIX_WIDTH (it, area);
24106 /* Produce a glyph for a glyphless character for iterator IT.
24107 IT->glyphless_method specifies which method to use for displaying
24108 the character. See the description of enum
24109 glyphless_display_method in dispextern.h for the detail.
24111 FOR_NO_FONT is nonzero if and only if this is for a character for
24112 which no font was found. ACRONYM, if non-nil, is an acronym string
24113 for the character. */
24115 static void
24116 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24118 int face_id;
24119 struct face *face;
24120 struct font *font;
24121 int base_width, base_height, width, height;
24122 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24123 int len;
24125 /* Get the metrics of the base font. We always refer to the current
24126 ASCII face. */
24127 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24128 font = face->font ? face->font : FRAME_FONT (it->f);
24129 it->ascent = FONT_BASE (font) + font->baseline_offset;
24130 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24131 base_height = it->ascent + it->descent;
24132 base_width = font->average_width;
24134 /* Get a face ID for the glyph by utilizing a cache (the same way as
24135 done for `escape-glyph' in get_next_display_element). */
24136 if (it->f == last_glyphless_glyph_frame
24137 && it->face_id == last_glyphless_glyph_face_id)
24139 face_id = last_glyphless_glyph_merged_face_id;
24141 else
24143 /* Merge the `glyphless-char' face into the current face. */
24144 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24145 last_glyphless_glyph_frame = it->f;
24146 last_glyphless_glyph_face_id = it->face_id;
24147 last_glyphless_glyph_merged_face_id = face_id;
24150 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24152 it->pixel_width = THIN_SPACE_WIDTH;
24153 len = 0;
24154 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24156 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24158 width = CHAR_WIDTH (it->c);
24159 if (width == 0)
24160 width = 1;
24161 else if (width > 4)
24162 width = 4;
24163 it->pixel_width = base_width * width;
24164 len = 0;
24165 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24167 else
24169 char buf[7];
24170 const char *str;
24171 unsigned int code[6];
24172 int upper_len;
24173 int ascent, descent;
24174 struct font_metrics metrics_upper, metrics_lower;
24176 face = FACE_FROM_ID (it->f, face_id);
24177 font = face->font ? face->font : FRAME_FONT (it->f);
24178 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24180 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24182 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24183 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24184 if (CONSP (acronym))
24185 acronym = XCAR (acronym);
24186 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24188 else
24190 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24191 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24192 str = buf;
24194 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24195 code[len] = font->driver->encode_char (font, str[len]);
24196 upper_len = (len + 1) / 2;
24197 font->driver->text_extents (font, code, upper_len,
24198 &metrics_upper);
24199 font->driver->text_extents (font, code + upper_len, len - upper_len,
24200 &metrics_lower);
24204 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24205 width = max (metrics_upper.width, metrics_lower.width) + 4;
24206 upper_xoff = upper_yoff = 2; /* the typical case */
24207 if (base_width >= width)
24209 /* Align the upper to the left, the lower to the right. */
24210 it->pixel_width = base_width;
24211 lower_xoff = base_width - 2 - metrics_lower.width;
24213 else
24215 /* Center the shorter one. */
24216 it->pixel_width = width;
24217 if (metrics_upper.width >= metrics_lower.width)
24218 lower_xoff = (width - metrics_lower.width) / 2;
24219 else
24221 /* FIXME: This code doesn't look right. It formerly was
24222 missing the "lower_xoff = 0;", which couldn't have
24223 been right since it left lower_xoff uninitialized. */
24224 lower_xoff = 0;
24225 upper_xoff = (width - metrics_upper.width) / 2;
24229 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24230 top, bottom, and between upper and lower strings. */
24231 height = (metrics_upper.ascent + metrics_upper.descent
24232 + metrics_lower.ascent + metrics_lower.descent) + 5;
24233 /* Center vertically.
24234 H:base_height, D:base_descent
24235 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24237 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24238 descent = D - H/2 + h/2;
24239 lower_yoff = descent - 2 - ld;
24240 upper_yoff = lower_yoff - la - 1 - ud; */
24241 ascent = - (it->descent - (base_height + height + 1) / 2);
24242 descent = it->descent - (base_height - height) / 2;
24243 lower_yoff = descent - 2 - metrics_lower.descent;
24244 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24245 - metrics_upper.descent);
24246 /* Don't make the height shorter than the base height. */
24247 if (height > base_height)
24249 it->ascent = ascent;
24250 it->descent = descent;
24254 it->phys_ascent = it->ascent;
24255 it->phys_descent = it->descent;
24256 if (it->glyph_row)
24257 append_glyphless_glyph (it, face_id, for_no_font, len,
24258 upper_xoff, upper_yoff,
24259 lower_xoff, lower_yoff);
24260 it->nglyphs = 1;
24261 take_vertical_position_into_account (it);
24265 /* RIF:
24266 Produce glyphs/get display metrics for the display element IT is
24267 loaded with. See the description of struct it in dispextern.h
24268 for an overview of struct it. */
24270 void
24271 x_produce_glyphs (struct it *it)
24273 int extra_line_spacing = it->extra_line_spacing;
24275 it->glyph_not_available_p = 0;
24277 if (it->what == IT_CHARACTER)
24279 XChar2b char2b;
24280 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24281 struct font *font = face->font;
24282 struct font_metrics *pcm = NULL;
24283 int boff; /* baseline offset */
24285 if (font == NULL)
24287 /* When no suitable font is found, display this character by
24288 the method specified in the first extra slot of
24289 Vglyphless_char_display. */
24290 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24292 xassert (it->what == IT_GLYPHLESS);
24293 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24294 goto done;
24297 boff = font->baseline_offset;
24298 if (font->vertical_centering)
24299 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24301 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24303 int stretched_p;
24305 it->nglyphs = 1;
24307 if (it->override_ascent >= 0)
24309 it->ascent = it->override_ascent;
24310 it->descent = it->override_descent;
24311 boff = it->override_boff;
24313 else
24315 it->ascent = FONT_BASE (font) + boff;
24316 it->descent = FONT_DESCENT (font) - boff;
24319 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24321 pcm = get_per_char_metric (font, &char2b);
24322 if (pcm->width == 0
24323 && pcm->rbearing == 0 && pcm->lbearing == 0)
24324 pcm = NULL;
24327 if (pcm)
24329 it->phys_ascent = pcm->ascent + boff;
24330 it->phys_descent = pcm->descent - boff;
24331 it->pixel_width = pcm->width;
24333 else
24335 it->glyph_not_available_p = 1;
24336 it->phys_ascent = it->ascent;
24337 it->phys_descent = it->descent;
24338 it->pixel_width = font->space_width;
24341 if (it->constrain_row_ascent_descent_p)
24343 if (it->descent > it->max_descent)
24345 it->ascent += it->descent - it->max_descent;
24346 it->descent = it->max_descent;
24348 if (it->ascent > it->max_ascent)
24350 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24351 it->ascent = it->max_ascent;
24353 it->phys_ascent = min (it->phys_ascent, it->ascent);
24354 it->phys_descent = min (it->phys_descent, it->descent);
24355 extra_line_spacing = 0;
24358 /* If this is a space inside a region of text with
24359 `space-width' property, change its width. */
24360 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24361 if (stretched_p)
24362 it->pixel_width *= XFLOATINT (it->space_width);
24364 /* If face has a box, add the box thickness to the character
24365 height. If character has a box line to the left and/or
24366 right, add the box line width to the character's width. */
24367 if (face->box != FACE_NO_BOX)
24369 int thick = face->box_line_width;
24371 if (thick > 0)
24373 it->ascent += thick;
24374 it->descent += thick;
24376 else
24377 thick = -thick;
24379 if (it->start_of_box_run_p)
24380 it->pixel_width += thick;
24381 if (it->end_of_box_run_p)
24382 it->pixel_width += thick;
24385 /* If face has an overline, add the height of the overline
24386 (1 pixel) and a 1 pixel margin to the character height. */
24387 if (face->overline_p)
24388 it->ascent += overline_margin;
24390 if (it->constrain_row_ascent_descent_p)
24392 if (it->ascent > it->max_ascent)
24393 it->ascent = it->max_ascent;
24394 if (it->descent > it->max_descent)
24395 it->descent = it->max_descent;
24398 take_vertical_position_into_account (it);
24400 /* If we have to actually produce glyphs, do it. */
24401 if (it->glyph_row)
24403 if (stretched_p)
24405 /* Translate a space with a `space-width' property
24406 into a stretch glyph. */
24407 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24408 / FONT_HEIGHT (font));
24409 append_stretch_glyph (it, it->object, it->pixel_width,
24410 it->ascent + it->descent, ascent);
24412 else
24413 append_glyph (it);
24415 /* If characters with lbearing or rbearing are displayed
24416 in this line, record that fact in a flag of the
24417 glyph row. This is used to optimize X output code. */
24418 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24419 it->glyph_row->contains_overlapping_glyphs_p = 1;
24421 if (! stretched_p && it->pixel_width == 0)
24422 /* We assure that all visible glyphs have at least 1-pixel
24423 width. */
24424 it->pixel_width = 1;
24426 else if (it->char_to_display == '\n')
24428 /* A newline has no width, but we need the height of the
24429 line. But if previous part of the line sets a height,
24430 don't increase that height */
24432 Lisp_Object height;
24433 Lisp_Object total_height = Qnil;
24435 it->override_ascent = -1;
24436 it->pixel_width = 0;
24437 it->nglyphs = 0;
24439 height = get_it_property (it, Qline_height);
24440 /* Split (line-height total-height) list */
24441 if (CONSP (height)
24442 && CONSP (XCDR (height))
24443 && NILP (XCDR (XCDR (height))))
24445 total_height = XCAR (XCDR (height));
24446 height = XCAR (height);
24448 height = calc_line_height_property (it, height, font, boff, 1);
24450 if (it->override_ascent >= 0)
24452 it->ascent = it->override_ascent;
24453 it->descent = it->override_descent;
24454 boff = it->override_boff;
24456 else
24458 it->ascent = FONT_BASE (font) + boff;
24459 it->descent = FONT_DESCENT (font) - boff;
24462 if (EQ (height, Qt))
24464 if (it->descent > it->max_descent)
24466 it->ascent += it->descent - it->max_descent;
24467 it->descent = it->max_descent;
24469 if (it->ascent > it->max_ascent)
24471 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24472 it->ascent = it->max_ascent;
24474 it->phys_ascent = min (it->phys_ascent, it->ascent);
24475 it->phys_descent = min (it->phys_descent, it->descent);
24476 it->constrain_row_ascent_descent_p = 1;
24477 extra_line_spacing = 0;
24479 else
24481 Lisp_Object spacing;
24483 it->phys_ascent = it->ascent;
24484 it->phys_descent = it->descent;
24486 if ((it->max_ascent > 0 || it->max_descent > 0)
24487 && face->box != FACE_NO_BOX
24488 && face->box_line_width > 0)
24490 it->ascent += face->box_line_width;
24491 it->descent += face->box_line_width;
24493 if (!NILP (height)
24494 && XINT (height) > it->ascent + it->descent)
24495 it->ascent = XINT (height) - it->descent;
24497 if (!NILP (total_height))
24498 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24499 else
24501 spacing = get_it_property (it, Qline_spacing);
24502 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24504 if (INTEGERP (spacing))
24506 extra_line_spacing = XINT (spacing);
24507 if (!NILP (total_height))
24508 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24512 else /* i.e. (it->char_to_display == '\t') */
24514 if (font->space_width > 0)
24516 int tab_width = it->tab_width * font->space_width;
24517 int x = it->current_x + it->continuation_lines_width;
24518 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24520 /* If the distance from the current position to the next tab
24521 stop is less than a space character width, use the
24522 tab stop after that. */
24523 if (next_tab_x - x < font->space_width)
24524 next_tab_x += tab_width;
24526 it->pixel_width = next_tab_x - x;
24527 it->nglyphs = 1;
24528 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24529 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24531 if (it->glyph_row)
24533 append_stretch_glyph (it, it->object, it->pixel_width,
24534 it->ascent + it->descent, it->ascent);
24537 else
24539 it->pixel_width = 0;
24540 it->nglyphs = 1;
24544 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24546 /* A static composition.
24548 Note: A composition is represented as one glyph in the
24549 glyph matrix. There are no padding glyphs.
24551 Important note: pixel_width, ascent, and descent are the
24552 values of what is drawn by draw_glyphs (i.e. the values of
24553 the overall glyphs composed). */
24554 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24555 int boff; /* baseline offset */
24556 struct composition *cmp = composition_table[it->cmp_it.id];
24557 int glyph_len = cmp->glyph_len;
24558 struct font *font = face->font;
24560 it->nglyphs = 1;
24562 /* If we have not yet calculated pixel size data of glyphs of
24563 the composition for the current face font, calculate them
24564 now. Theoretically, we have to check all fonts for the
24565 glyphs, but that requires much time and memory space. So,
24566 here we check only the font of the first glyph. This may
24567 lead to incorrect display, but it's very rare, and C-l
24568 (recenter-top-bottom) can correct the display anyway. */
24569 if (! cmp->font || cmp->font != font)
24571 /* Ascent and descent of the font of the first character
24572 of this composition (adjusted by baseline offset).
24573 Ascent and descent of overall glyphs should not be less
24574 than these, respectively. */
24575 int font_ascent, font_descent, font_height;
24576 /* Bounding box of the overall glyphs. */
24577 int leftmost, rightmost, lowest, highest;
24578 int lbearing, rbearing;
24579 int i, width, ascent, descent;
24580 int left_padded = 0, right_padded = 0;
24581 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24582 XChar2b char2b;
24583 struct font_metrics *pcm;
24584 int font_not_found_p;
24585 EMACS_INT pos;
24587 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24588 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24589 break;
24590 if (glyph_len < cmp->glyph_len)
24591 right_padded = 1;
24592 for (i = 0; i < glyph_len; i++)
24594 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24595 break;
24596 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24598 if (i > 0)
24599 left_padded = 1;
24601 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24602 : IT_CHARPOS (*it));
24603 /* If no suitable font is found, use the default font. */
24604 font_not_found_p = font == NULL;
24605 if (font_not_found_p)
24607 face = face->ascii_face;
24608 font = face->font;
24610 boff = font->baseline_offset;
24611 if (font->vertical_centering)
24612 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24613 font_ascent = FONT_BASE (font) + boff;
24614 font_descent = FONT_DESCENT (font) - boff;
24615 font_height = FONT_HEIGHT (font);
24617 cmp->font = (void *) font;
24619 pcm = NULL;
24620 if (! font_not_found_p)
24622 get_char_face_and_encoding (it->f, c, it->face_id,
24623 &char2b, 0);
24624 pcm = get_per_char_metric (font, &char2b);
24627 /* Initialize the bounding box. */
24628 if (pcm)
24630 width = cmp->glyph_len > 0 ? pcm->width : 0;
24631 ascent = pcm->ascent;
24632 descent = pcm->descent;
24633 lbearing = pcm->lbearing;
24634 rbearing = pcm->rbearing;
24636 else
24638 width = cmp->glyph_len > 0 ? font->space_width : 0;
24639 ascent = FONT_BASE (font);
24640 descent = FONT_DESCENT (font);
24641 lbearing = 0;
24642 rbearing = width;
24645 rightmost = width;
24646 leftmost = 0;
24647 lowest = - descent + boff;
24648 highest = ascent + boff;
24650 if (! font_not_found_p
24651 && font->default_ascent
24652 && CHAR_TABLE_P (Vuse_default_ascent)
24653 && !NILP (Faref (Vuse_default_ascent,
24654 make_number (it->char_to_display))))
24655 highest = font->default_ascent + boff;
24657 /* Draw the first glyph at the normal position. It may be
24658 shifted to right later if some other glyphs are drawn
24659 at the left. */
24660 cmp->offsets[i * 2] = 0;
24661 cmp->offsets[i * 2 + 1] = boff;
24662 cmp->lbearing = lbearing;
24663 cmp->rbearing = rbearing;
24665 /* Set cmp->offsets for the remaining glyphs. */
24666 for (i++; i < glyph_len; i++)
24668 int left, right, btm, top;
24669 int ch = COMPOSITION_GLYPH (cmp, i);
24670 int face_id;
24671 struct face *this_face;
24673 if (ch == '\t')
24674 ch = ' ';
24675 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24676 this_face = FACE_FROM_ID (it->f, face_id);
24677 font = this_face->font;
24679 if (font == NULL)
24680 pcm = NULL;
24681 else
24683 get_char_face_and_encoding (it->f, ch, face_id,
24684 &char2b, 0);
24685 pcm = get_per_char_metric (font, &char2b);
24687 if (! pcm)
24688 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24689 else
24691 width = pcm->width;
24692 ascent = pcm->ascent;
24693 descent = pcm->descent;
24694 lbearing = pcm->lbearing;
24695 rbearing = pcm->rbearing;
24696 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24698 /* Relative composition with or without
24699 alternate chars. */
24700 left = (leftmost + rightmost - width) / 2;
24701 btm = - descent + boff;
24702 if (font->relative_compose
24703 && (! CHAR_TABLE_P (Vignore_relative_composition)
24704 || NILP (Faref (Vignore_relative_composition,
24705 make_number (ch)))))
24708 if (- descent >= font->relative_compose)
24709 /* One extra pixel between two glyphs. */
24710 btm = highest + 1;
24711 else if (ascent <= 0)
24712 /* One extra pixel between two glyphs. */
24713 btm = lowest - 1 - ascent - descent;
24716 else
24718 /* A composition rule is specified by an integer
24719 value that encodes global and new reference
24720 points (GREF and NREF). GREF and NREF are
24721 specified by numbers as below:
24723 0---1---2 -- ascent
24727 9--10--11 -- center
24729 ---3---4---5--- baseline
24731 6---7---8 -- descent
24733 int rule = COMPOSITION_RULE (cmp, i);
24734 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24736 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24737 grefx = gref % 3, nrefx = nref % 3;
24738 grefy = gref / 3, nrefy = nref / 3;
24739 if (xoff)
24740 xoff = font_height * (xoff - 128) / 256;
24741 if (yoff)
24742 yoff = font_height * (yoff - 128) / 256;
24744 left = (leftmost
24745 + grefx * (rightmost - leftmost) / 2
24746 - nrefx * width / 2
24747 + xoff);
24749 btm = ((grefy == 0 ? highest
24750 : grefy == 1 ? 0
24751 : grefy == 2 ? lowest
24752 : (highest + lowest) / 2)
24753 - (nrefy == 0 ? ascent + descent
24754 : nrefy == 1 ? descent - boff
24755 : nrefy == 2 ? 0
24756 : (ascent + descent) / 2)
24757 + yoff);
24760 cmp->offsets[i * 2] = left;
24761 cmp->offsets[i * 2 + 1] = btm + descent;
24763 /* Update the bounding box of the overall glyphs. */
24764 if (width > 0)
24766 right = left + width;
24767 if (left < leftmost)
24768 leftmost = left;
24769 if (right > rightmost)
24770 rightmost = right;
24772 top = btm + descent + ascent;
24773 if (top > highest)
24774 highest = top;
24775 if (btm < lowest)
24776 lowest = btm;
24778 if (cmp->lbearing > left + lbearing)
24779 cmp->lbearing = left + lbearing;
24780 if (cmp->rbearing < left + rbearing)
24781 cmp->rbearing = left + rbearing;
24785 /* If there are glyphs whose x-offsets are negative,
24786 shift all glyphs to the right and make all x-offsets
24787 non-negative. */
24788 if (leftmost < 0)
24790 for (i = 0; i < cmp->glyph_len; i++)
24791 cmp->offsets[i * 2] -= leftmost;
24792 rightmost -= leftmost;
24793 cmp->lbearing -= leftmost;
24794 cmp->rbearing -= leftmost;
24797 if (left_padded && cmp->lbearing < 0)
24799 for (i = 0; i < cmp->glyph_len; i++)
24800 cmp->offsets[i * 2] -= cmp->lbearing;
24801 rightmost -= cmp->lbearing;
24802 cmp->rbearing -= cmp->lbearing;
24803 cmp->lbearing = 0;
24805 if (right_padded && rightmost < cmp->rbearing)
24807 rightmost = cmp->rbearing;
24810 cmp->pixel_width = rightmost;
24811 cmp->ascent = highest;
24812 cmp->descent = - lowest;
24813 if (cmp->ascent < font_ascent)
24814 cmp->ascent = font_ascent;
24815 if (cmp->descent < font_descent)
24816 cmp->descent = font_descent;
24819 if (it->glyph_row
24820 && (cmp->lbearing < 0
24821 || cmp->rbearing > cmp->pixel_width))
24822 it->glyph_row->contains_overlapping_glyphs_p = 1;
24824 it->pixel_width = cmp->pixel_width;
24825 it->ascent = it->phys_ascent = cmp->ascent;
24826 it->descent = it->phys_descent = cmp->descent;
24827 if (face->box != FACE_NO_BOX)
24829 int thick = face->box_line_width;
24831 if (thick > 0)
24833 it->ascent += thick;
24834 it->descent += thick;
24836 else
24837 thick = - thick;
24839 if (it->start_of_box_run_p)
24840 it->pixel_width += thick;
24841 if (it->end_of_box_run_p)
24842 it->pixel_width += thick;
24845 /* If face has an overline, add the height of the overline
24846 (1 pixel) and a 1 pixel margin to the character height. */
24847 if (face->overline_p)
24848 it->ascent += overline_margin;
24850 take_vertical_position_into_account (it);
24851 if (it->ascent < 0)
24852 it->ascent = 0;
24853 if (it->descent < 0)
24854 it->descent = 0;
24856 if (it->glyph_row && cmp->glyph_len > 0)
24857 append_composite_glyph (it);
24859 else if (it->what == IT_COMPOSITION)
24861 /* A dynamic (automatic) composition. */
24862 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24863 Lisp_Object gstring;
24864 struct font_metrics metrics;
24866 it->nglyphs = 1;
24868 gstring = composition_gstring_from_id (it->cmp_it.id);
24869 it->pixel_width
24870 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24871 &metrics);
24872 if (it->glyph_row
24873 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24874 it->glyph_row->contains_overlapping_glyphs_p = 1;
24875 it->ascent = it->phys_ascent = metrics.ascent;
24876 it->descent = it->phys_descent = metrics.descent;
24877 if (face->box != FACE_NO_BOX)
24879 int thick = face->box_line_width;
24881 if (thick > 0)
24883 it->ascent += thick;
24884 it->descent += thick;
24886 else
24887 thick = - thick;
24889 if (it->start_of_box_run_p)
24890 it->pixel_width += thick;
24891 if (it->end_of_box_run_p)
24892 it->pixel_width += thick;
24894 /* If face has an overline, add the height of the overline
24895 (1 pixel) and a 1 pixel margin to the character height. */
24896 if (face->overline_p)
24897 it->ascent += overline_margin;
24898 take_vertical_position_into_account (it);
24899 if (it->ascent < 0)
24900 it->ascent = 0;
24901 if (it->descent < 0)
24902 it->descent = 0;
24904 if (it->glyph_row)
24905 append_composite_glyph (it);
24907 else if (it->what == IT_GLYPHLESS)
24908 produce_glyphless_glyph (it, 0, Qnil);
24909 else if (it->what == IT_IMAGE)
24910 produce_image_glyph (it);
24911 else if (it->what == IT_STRETCH)
24912 produce_stretch_glyph (it);
24914 done:
24915 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24916 because this isn't true for images with `:ascent 100'. */
24917 xassert (it->ascent >= 0 && it->descent >= 0);
24918 if (it->area == TEXT_AREA)
24919 it->current_x += it->pixel_width;
24921 if (extra_line_spacing > 0)
24923 it->descent += extra_line_spacing;
24924 if (extra_line_spacing > it->max_extra_line_spacing)
24925 it->max_extra_line_spacing = extra_line_spacing;
24928 it->max_ascent = max (it->max_ascent, it->ascent);
24929 it->max_descent = max (it->max_descent, it->descent);
24930 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24931 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24934 /* EXPORT for RIF:
24935 Output LEN glyphs starting at START at the nominal cursor position.
24936 Advance the nominal cursor over the text. The global variable
24937 updated_window contains the window being updated, updated_row is
24938 the glyph row being updated, and updated_area is the area of that
24939 row being updated. */
24941 void
24942 x_write_glyphs (struct glyph *start, int len)
24944 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24946 xassert (updated_window && updated_row);
24947 /* When the window is hscrolled, cursor hpos can legitimately be out
24948 of bounds, but we draw the cursor at the corresponding window
24949 margin in that case. */
24950 if (!updated_row->reversed_p && chpos < 0)
24951 chpos = 0;
24952 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24953 chpos = updated_row->used[TEXT_AREA] - 1;
24955 BLOCK_INPUT;
24957 /* Write glyphs. */
24959 hpos = start - updated_row->glyphs[updated_area];
24960 x = draw_glyphs (updated_window, output_cursor.x,
24961 updated_row, updated_area,
24962 hpos, hpos + len,
24963 DRAW_NORMAL_TEXT, 0);
24965 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24966 if (updated_area == TEXT_AREA
24967 && updated_window->phys_cursor_on_p
24968 && updated_window->phys_cursor.vpos == output_cursor.vpos
24969 && chpos >= hpos
24970 && chpos < hpos + len)
24971 updated_window->phys_cursor_on_p = 0;
24973 UNBLOCK_INPUT;
24975 /* Advance the output cursor. */
24976 output_cursor.hpos += len;
24977 output_cursor.x = x;
24981 /* EXPORT for RIF:
24982 Insert LEN glyphs from START at the nominal cursor position. */
24984 void
24985 x_insert_glyphs (struct glyph *start, int len)
24987 struct frame *f;
24988 struct window *w;
24989 int line_height, shift_by_width, shifted_region_width;
24990 struct glyph_row *row;
24991 struct glyph *glyph;
24992 int frame_x, frame_y;
24993 EMACS_INT hpos;
24995 xassert (updated_window && updated_row);
24996 BLOCK_INPUT;
24997 w = updated_window;
24998 f = XFRAME (WINDOW_FRAME (w));
25000 /* Get the height of the line we are in. */
25001 row = updated_row;
25002 line_height = row->height;
25004 /* Get the width of the glyphs to insert. */
25005 shift_by_width = 0;
25006 for (glyph = start; glyph < start + len; ++glyph)
25007 shift_by_width += glyph->pixel_width;
25009 /* Get the width of the region to shift right. */
25010 shifted_region_width = (window_box_width (w, updated_area)
25011 - output_cursor.x
25012 - shift_by_width);
25014 /* Shift right. */
25015 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25016 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25018 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25019 line_height, shift_by_width);
25021 /* Write the glyphs. */
25022 hpos = start - row->glyphs[updated_area];
25023 draw_glyphs (w, output_cursor.x, row, updated_area,
25024 hpos, hpos + len,
25025 DRAW_NORMAL_TEXT, 0);
25027 /* Advance the output cursor. */
25028 output_cursor.hpos += len;
25029 output_cursor.x += shift_by_width;
25030 UNBLOCK_INPUT;
25034 /* EXPORT for RIF:
25035 Erase the current text line from the nominal cursor position
25036 (inclusive) to pixel column TO_X (exclusive). The idea is that
25037 everything from TO_X onward is already erased.
25039 TO_X is a pixel position relative to updated_area of
25040 updated_window. TO_X == -1 means clear to the end of this area. */
25042 void
25043 x_clear_end_of_line (int to_x)
25045 struct frame *f;
25046 struct window *w = updated_window;
25047 int max_x, min_y, max_y;
25048 int from_x, from_y, to_y;
25050 xassert (updated_window && updated_row);
25051 f = XFRAME (w->frame);
25053 if (updated_row->full_width_p)
25054 max_x = WINDOW_TOTAL_WIDTH (w);
25055 else
25056 max_x = window_box_width (w, updated_area);
25057 max_y = window_text_bottom_y (w);
25059 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25060 of window. For TO_X > 0, truncate to end of drawing area. */
25061 if (to_x == 0)
25062 return;
25063 else if (to_x < 0)
25064 to_x = max_x;
25065 else
25066 to_x = min (to_x, max_x);
25068 to_y = min (max_y, output_cursor.y + updated_row->height);
25070 /* Notice if the cursor will be cleared by this operation. */
25071 if (!updated_row->full_width_p)
25072 notice_overwritten_cursor (w, updated_area,
25073 output_cursor.x, -1,
25074 updated_row->y,
25075 MATRIX_ROW_BOTTOM_Y (updated_row));
25077 from_x = output_cursor.x;
25079 /* Translate to frame coordinates. */
25080 if (updated_row->full_width_p)
25082 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25083 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25085 else
25087 int area_left = window_box_left (w, updated_area);
25088 from_x += area_left;
25089 to_x += area_left;
25092 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25093 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25094 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25096 /* Prevent inadvertently clearing to end of the X window. */
25097 if (to_x > from_x && to_y > from_y)
25099 BLOCK_INPUT;
25100 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25101 to_x - from_x, to_y - from_y);
25102 UNBLOCK_INPUT;
25106 #endif /* HAVE_WINDOW_SYSTEM */
25110 /***********************************************************************
25111 Cursor types
25112 ***********************************************************************/
25114 /* Value is the internal representation of the specified cursor type
25115 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25116 of the bar cursor. */
25118 static enum text_cursor_kinds
25119 get_specified_cursor_type (Lisp_Object arg, int *width)
25121 enum text_cursor_kinds type;
25123 if (NILP (arg))
25124 return NO_CURSOR;
25126 if (EQ (arg, Qbox))
25127 return FILLED_BOX_CURSOR;
25129 if (EQ (arg, Qhollow))
25130 return HOLLOW_BOX_CURSOR;
25132 if (EQ (arg, Qbar))
25134 *width = 2;
25135 return BAR_CURSOR;
25138 if (CONSP (arg)
25139 && EQ (XCAR (arg), Qbar)
25140 && INTEGERP (XCDR (arg))
25141 && XINT (XCDR (arg)) >= 0)
25143 *width = XINT (XCDR (arg));
25144 return BAR_CURSOR;
25147 if (EQ (arg, Qhbar))
25149 *width = 2;
25150 return HBAR_CURSOR;
25153 if (CONSP (arg)
25154 && EQ (XCAR (arg), Qhbar)
25155 && INTEGERP (XCDR (arg))
25156 && XINT (XCDR (arg)) >= 0)
25158 *width = XINT (XCDR (arg));
25159 return HBAR_CURSOR;
25162 /* Treat anything unknown as "hollow box cursor".
25163 It was bad to signal an error; people have trouble fixing
25164 .Xdefaults with Emacs, when it has something bad in it. */
25165 type = HOLLOW_BOX_CURSOR;
25167 return type;
25170 /* Set the default cursor types for specified frame. */
25171 void
25172 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25174 int width = 1;
25175 Lisp_Object tem;
25177 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25178 FRAME_CURSOR_WIDTH (f) = width;
25180 /* By default, set up the blink-off state depending on the on-state. */
25182 tem = Fassoc (arg, Vblink_cursor_alist);
25183 if (!NILP (tem))
25185 FRAME_BLINK_OFF_CURSOR (f)
25186 = get_specified_cursor_type (XCDR (tem), &width);
25187 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25189 else
25190 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25194 #ifdef HAVE_WINDOW_SYSTEM
25196 /* Return the cursor we want to be displayed in window W. Return
25197 width of bar/hbar cursor through WIDTH arg. Return with
25198 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25199 (i.e. if the `system caret' should track this cursor).
25201 In a mini-buffer window, we want the cursor only to appear if we
25202 are reading input from this window. For the selected window, we
25203 want the cursor type given by the frame parameter or buffer local
25204 setting of cursor-type. If explicitly marked off, draw no cursor.
25205 In all other cases, we want a hollow box cursor. */
25207 static enum text_cursor_kinds
25208 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25209 int *active_cursor)
25211 struct frame *f = XFRAME (w->frame);
25212 struct buffer *b = XBUFFER (w->buffer);
25213 int cursor_type = DEFAULT_CURSOR;
25214 Lisp_Object alt_cursor;
25215 int non_selected = 0;
25217 *active_cursor = 1;
25219 /* Echo area */
25220 if (cursor_in_echo_area
25221 && FRAME_HAS_MINIBUF_P (f)
25222 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25224 if (w == XWINDOW (echo_area_window))
25226 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25228 *width = FRAME_CURSOR_WIDTH (f);
25229 return FRAME_DESIRED_CURSOR (f);
25231 else
25232 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25235 *active_cursor = 0;
25236 non_selected = 1;
25239 /* Detect a nonselected window or nonselected frame. */
25240 else if (w != XWINDOW (f->selected_window)
25241 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25243 *active_cursor = 0;
25245 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25246 return NO_CURSOR;
25248 non_selected = 1;
25251 /* Never display a cursor in a window in which cursor-type is nil. */
25252 if (NILP (BVAR (b, cursor_type)))
25253 return NO_CURSOR;
25255 /* Get the normal cursor type for this window. */
25256 if (EQ (BVAR (b, cursor_type), Qt))
25258 cursor_type = FRAME_DESIRED_CURSOR (f);
25259 *width = FRAME_CURSOR_WIDTH (f);
25261 else
25262 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25264 /* Use cursor-in-non-selected-windows instead
25265 for non-selected window or frame. */
25266 if (non_selected)
25268 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25269 if (!EQ (Qt, alt_cursor))
25270 return get_specified_cursor_type (alt_cursor, width);
25271 /* t means modify the normal cursor type. */
25272 if (cursor_type == FILLED_BOX_CURSOR)
25273 cursor_type = HOLLOW_BOX_CURSOR;
25274 else if (cursor_type == BAR_CURSOR && *width > 1)
25275 --*width;
25276 return cursor_type;
25279 /* Use normal cursor if not blinked off. */
25280 if (!w->cursor_off_p)
25282 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25284 if (cursor_type == FILLED_BOX_CURSOR)
25286 /* Using a block cursor on large images can be very annoying.
25287 So use a hollow cursor for "large" images.
25288 If image is not transparent (no mask), also use hollow cursor. */
25289 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25290 if (img != NULL && IMAGEP (img->spec))
25292 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25293 where N = size of default frame font size.
25294 This should cover most of the "tiny" icons people may use. */
25295 if (!img->mask
25296 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25297 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25298 cursor_type = HOLLOW_BOX_CURSOR;
25301 else if (cursor_type != NO_CURSOR)
25303 /* Display current only supports BOX and HOLLOW cursors for images.
25304 So for now, unconditionally use a HOLLOW cursor when cursor is
25305 not a solid box cursor. */
25306 cursor_type = HOLLOW_BOX_CURSOR;
25309 return cursor_type;
25312 /* Cursor is blinked off, so determine how to "toggle" it. */
25314 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25315 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25316 return get_specified_cursor_type (XCDR (alt_cursor), width);
25318 /* Then see if frame has specified a specific blink off cursor type. */
25319 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25321 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25322 return FRAME_BLINK_OFF_CURSOR (f);
25325 #if 0
25326 /* Some people liked having a permanently visible blinking cursor,
25327 while others had very strong opinions against it. So it was
25328 decided to remove it. KFS 2003-09-03 */
25330 /* Finally perform built-in cursor blinking:
25331 filled box <-> hollow box
25332 wide [h]bar <-> narrow [h]bar
25333 narrow [h]bar <-> no cursor
25334 other type <-> no cursor */
25336 if (cursor_type == FILLED_BOX_CURSOR)
25337 return HOLLOW_BOX_CURSOR;
25339 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25341 *width = 1;
25342 return cursor_type;
25344 #endif
25346 return NO_CURSOR;
25350 /* Notice when the text cursor of window W has been completely
25351 overwritten by a drawing operation that outputs glyphs in AREA
25352 starting at X0 and ending at X1 in the line starting at Y0 and
25353 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25354 the rest of the line after X0 has been written. Y coordinates
25355 are window-relative. */
25357 static void
25358 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25359 int x0, int x1, int y0, int y1)
25361 int cx0, cx1, cy0, cy1;
25362 struct glyph_row *row;
25364 if (!w->phys_cursor_on_p)
25365 return;
25366 if (area != TEXT_AREA)
25367 return;
25369 if (w->phys_cursor.vpos < 0
25370 || w->phys_cursor.vpos >= w->current_matrix->nrows
25371 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25372 !(row->enabled_p && row->displays_text_p)))
25373 return;
25375 if (row->cursor_in_fringe_p)
25377 row->cursor_in_fringe_p = 0;
25378 draw_fringe_bitmap (w, row, row->reversed_p);
25379 w->phys_cursor_on_p = 0;
25380 return;
25383 cx0 = w->phys_cursor.x;
25384 cx1 = cx0 + w->phys_cursor_width;
25385 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25386 return;
25388 /* The cursor image will be completely removed from the
25389 screen if the output area intersects the cursor area in
25390 y-direction. When we draw in [y0 y1[, and some part of
25391 the cursor is at y < y0, that part must have been drawn
25392 before. When scrolling, the cursor is erased before
25393 actually scrolling, so we don't come here. When not
25394 scrolling, the rows above the old cursor row must have
25395 changed, and in this case these rows must have written
25396 over the cursor image.
25398 Likewise if part of the cursor is below y1, with the
25399 exception of the cursor being in the first blank row at
25400 the buffer and window end because update_text_area
25401 doesn't draw that row. (Except when it does, but
25402 that's handled in update_text_area.) */
25404 cy0 = w->phys_cursor.y;
25405 cy1 = cy0 + w->phys_cursor_height;
25406 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25407 return;
25409 w->phys_cursor_on_p = 0;
25412 #endif /* HAVE_WINDOW_SYSTEM */
25415 /************************************************************************
25416 Mouse Face
25417 ************************************************************************/
25419 #ifdef HAVE_WINDOW_SYSTEM
25421 /* EXPORT for RIF:
25422 Fix the display of area AREA of overlapping row ROW in window W
25423 with respect to the overlapping part OVERLAPS. */
25425 void
25426 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25427 enum glyph_row_area area, int overlaps)
25429 int i, x;
25431 BLOCK_INPUT;
25433 x = 0;
25434 for (i = 0; i < row->used[area];)
25436 if (row->glyphs[area][i].overlaps_vertically_p)
25438 int start = i, start_x = x;
25442 x += row->glyphs[area][i].pixel_width;
25443 ++i;
25445 while (i < row->used[area]
25446 && row->glyphs[area][i].overlaps_vertically_p);
25448 draw_glyphs (w, start_x, row, area,
25449 start, i,
25450 DRAW_NORMAL_TEXT, overlaps);
25452 else
25454 x += row->glyphs[area][i].pixel_width;
25455 ++i;
25459 UNBLOCK_INPUT;
25463 /* EXPORT:
25464 Draw the cursor glyph of window W in glyph row ROW. See the
25465 comment of draw_glyphs for the meaning of HL. */
25467 void
25468 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25469 enum draw_glyphs_face hl)
25471 /* If cursor hpos is out of bounds, don't draw garbage. This can
25472 happen in mini-buffer windows when switching between echo area
25473 glyphs and mini-buffer. */
25474 if ((row->reversed_p
25475 ? (w->phys_cursor.hpos >= 0)
25476 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25478 int on_p = w->phys_cursor_on_p;
25479 int x1;
25480 int hpos = w->phys_cursor.hpos;
25482 /* When the window is hscrolled, cursor hpos can legitimately be
25483 out of bounds, but we draw the cursor at the corresponding
25484 window margin in that case. */
25485 if (!row->reversed_p && hpos < 0)
25486 hpos = 0;
25487 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25488 hpos = row->used[TEXT_AREA] - 1;
25490 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25491 hl, 0);
25492 w->phys_cursor_on_p = on_p;
25494 if (hl == DRAW_CURSOR)
25495 w->phys_cursor_width = x1 - w->phys_cursor.x;
25496 /* When we erase the cursor, and ROW is overlapped by other
25497 rows, make sure that these overlapping parts of other rows
25498 are redrawn. */
25499 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25501 w->phys_cursor_width = x1 - w->phys_cursor.x;
25503 if (row > w->current_matrix->rows
25504 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25505 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25506 OVERLAPS_ERASED_CURSOR);
25508 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25509 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25510 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25511 OVERLAPS_ERASED_CURSOR);
25517 /* EXPORT:
25518 Erase the image of a cursor of window W from the screen. */
25520 void
25521 erase_phys_cursor (struct window *w)
25523 struct frame *f = XFRAME (w->frame);
25524 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25525 int hpos = w->phys_cursor.hpos;
25526 int vpos = w->phys_cursor.vpos;
25527 int mouse_face_here_p = 0;
25528 struct glyph_matrix *active_glyphs = w->current_matrix;
25529 struct glyph_row *cursor_row;
25530 struct glyph *cursor_glyph;
25531 enum draw_glyphs_face hl;
25533 /* No cursor displayed or row invalidated => nothing to do on the
25534 screen. */
25535 if (w->phys_cursor_type == NO_CURSOR)
25536 goto mark_cursor_off;
25538 /* VPOS >= active_glyphs->nrows means that window has been resized.
25539 Don't bother to erase the cursor. */
25540 if (vpos >= active_glyphs->nrows)
25541 goto mark_cursor_off;
25543 /* If row containing cursor is marked invalid, there is nothing we
25544 can do. */
25545 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25546 if (!cursor_row->enabled_p)
25547 goto mark_cursor_off;
25549 /* If line spacing is > 0, old cursor may only be partially visible in
25550 window after split-window. So adjust visible height. */
25551 cursor_row->visible_height = min (cursor_row->visible_height,
25552 window_text_bottom_y (w) - cursor_row->y);
25554 /* If row is completely invisible, don't attempt to delete a cursor which
25555 isn't there. This can happen if cursor is at top of a window, and
25556 we switch to a buffer with a header line in that window. */
25557 if (cursor_row->visible_height <= 0)
25558 goto mark_cursor_off;
25560 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25561 if (cursor_row->cursor_in_fringe_p)
25563 cursor_row->cursor_in_fringe_p = 0;
25564 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25565 goto mark_cursor_off;
25568 /* This can happen when the new row is shorter than the old one.
25569 In this case, either draw_glyphs or clear_end_of_line
25570 should have cleared the cursor. Note that we wouldn't be
25571 able to erase the cursor in this case because we don't have a
25572 cursor glyph at hand. */
25573 if ((cursor_row->reversed_p
25574 ? (w->phys_cursor.hpos < 0)
25575 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25576 goto mark_cursor_off;
25578 /* When the window is hscrolled, cursor hpos can legitimately be out
25579 of bounds, but we draw the cursor at the corresponding window
25580 margin in that case. */
25581 if (!cursor_row->reversed_p && hpos < 0)
25582 hpos = 0;
25583 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25584 hpos = cursor_row->used[TEXT_AREA] - 1;
25586 /* If the cursor is in the mouse face area, redisplay that when
25587 we clear the cursor. */
25588 if (! NILP (hlinfo->mouse_face_window)
25589 && coords_in_mouse_face_p (w, hpos, vpos)
25590 /* Don't redraw the cursor's spot in mouse face if it is at the
25591 end of a line (on a newline). The cursor appears there, but
25592 mouse highlighting does not. */
25593 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25594 mouse_face_here_p = 1;
25596 /* Maybe clear the display under the cursor. */
25597 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25599 int x, y, left_x;
25600 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25601 int width;
25603 cursor_glyph = get_phys_cursor_glyph (w);
25604 if (cursor_glyph == NULL)
25605 goto mark_cursor_off;
25607 width = cursor_glyph->pixel_width;
25608 left_x = window_box_left_offset (w, TEXT_AREA);
25609 x = w->phys_cursor.x;
25610 if (x < left_x)
25611 width -= left_x - x;
25612 width = min (width, window_box_width (w, TEXT_AREA) - x);
25613 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25614 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25616 if (width > 0)
25617 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25620 /* Erase the cursor by redrawing the character underneath it. */
25621 if (mouse_face_here_p)
25622 hl = DRAW_MOUSE_FACE;
25623 else
25624 hl = DRAW_NORMAL_TEXT;
25625 draw_phys_cursor_glyph (w, cursor_row, hl);
25627 mark_cursor_off:
25628 w->phys_cursor_on_p = 0;
25629 w->phys_cursor_type = NO_CURSOR;
25633 /* EXPORT:
25634 Display or clear cursor of window W. If ON is zero, clear the
25635 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25636 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25638 void
25639 display_and_set_cursor (struct window *w, int on,
25640 int hpos, int vpos, int x, int y)
25642 struct frame *f = XFRAME (w->frame);
25643 int new_cursor_type;
25644 int new_cursor_width;
25645 int active_cursor;
25646 struct glyph_row *glyph_row;
25647 struct glyph *glyph;
25649 /* This is pointless on invisible frames, and dangerous on garbaged
25650 windows and frames; in the latter case, the frame or window may
25651 be in the midst of changing its size, and x and y may be off the
25652 window. */
25653 if (! FRAME_VISIBLE_P (f)
25654 || FRAME_GARBAGED_P (f)
25655 || vpos >= w->current_matrix->nrows
25656 || hpos >= w->current_matrix->matrix_w)
25657 return;
25659 /* If cursor is off and we want it off, return quickly. */
25660 if (!on && !w->phys_cursor_on_p)
25661 return;
25663 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25664 /* If cursor row is not enabled, we don't really know where to
25665 display the cursor. */
25666 if (!glyph_row->enabled_p)
25668 w->phys_cursor_on_p = 0;
25669 return;
25672 glyph = NULL;
25673 if (!glyph_row->exact_window_width_line_p
25674 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25675 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25677 xassert (interrupt_input_blocked);
25679 /* Set new_cursor_type to the cursor we want to be displayed. */
25680 new_cursor_type = get_window_cursor_type (w, glyph,
25681 &new_cursor_width, &active_cursor);
25683 /* If cursor is currently being shown and we don't want it to be or
25684 it is in the wrong place, or the cursor type is not what we want,
25685 erase it. */
25686 if (w->phys_cursor_on_p
25687 && (!on
25688 || w->phys_cursor.x != x
25689 || w->phys_cursor.y != y
25690 || new_cursor_type != w->phys_cursor_type
25691 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25692 && new_cursor_width != w->phys_cursor_width)))
25693 erase_phys_cursor (w);
25695 /* Don't check phys_cursor_on_p here because that flag is only set
25696 to zero in some cases where we know that the cursor has been
25697 completely erased, to avoid the extra work of erasing the cursor
25698 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25699 still not be visible, or it has only been partly erased. */
25700 if (on)
25702 w->phys_cursor_ascent = glyph_row->ascent;
25703 w->phys_cursor_height = glyph_row->height;
25705 /* Set phys_cursor_.* before x_draw_.* is called because some
25706 of them may need the information. */
25707 w->phys_cursor.x = x;
25708 w->phys_cursor.y = glyph_row->y;
25709 w->phys_cursor.hpos = hpos;
25710 w->phys_cursor.vpos = vpos;
25713 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25714 new_cursor_type, new_cursor_width,
25715 on, active_cursor);
25719 /* Switch the display of W's cursor on or off, according to the value
25720 of ON. */
25722 static void
25723 update_window_cursor (struct window *w, int on)
25725 /* Don't update cursor in windows whose frame is in the process
25726 of being deleted. */
25727 if (w->current_matrix)
25729 int hpos = w->phys_cursor.hpos;
25730 int vpos = w->phys_cursor.vpos;
25731 struct glyph_row *row;
25733 if (vpos >= w->current_matrix->nrows
25734 || hpos >= w->current_matrix->matrix_w)
25735 return;
25737 row = MATRIX_ROW (w->current_matrix, vpos);
25739 /* When the window is hscrolled, cursor hpos can legitimately be
25740 out of bounds, but we draw the cursor at the corresponding
25741 window margin in that case. */
25742 if (!row->reversed_p && hpos < 0)
25743 hpos = 0;
25744 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25745 hpos = row->used[TEXT_AREA] - 1;
25747 BLOCK_INPUT;
25748 display_and_set_cursor (w, on, hpos, vpos,
25749 w->phys_cursor.x, w->phys_cursor.y);
25750 UNBLOCK_INPUT;
25755 /* Call update_window_cursor with parameter ON_P on all leaf windows
25756 in the window tree rooted at W. */
25758 static void
25759 update_cursor_in_window_tree (struct window *w, int on_p)
25761 while (w)
25763 if (!NILP (w->hchild))
25764 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25765 else if (!NILP (w->vchild))
25766 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25767 else
25768 update_window_cursor (w, on_p);
25770 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25775 /* EXPORT:
25776 Display the cursor on window W, or clear it, according to ON_P.
25777 Don't change the cursor's position. */
25779 void
25780 x_update_cursor (struct frame *f, int on_p)
25782 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25786 /* EXPORT:
25787 Clear the cursor of window W to background color, and mark the
25788 cursor as not shown. This is used when the text where the cursor
25789 is about to be rewritten. */
25791 void
25792 x_clear_cursor (struct window *w)
25794 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25795 update_window_cursor (w, 0);
25798 #endif /* HAVE_WINDOW_SYSTEM */
25800 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25801 and MSDOS. */
25802 static void
25803 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25804 int start_hpos, int end_hpos,
25805 enum draw_glyphs_face draw)
25807 #ifdef HAVE_WINDOW_SYSTEM
25808 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25810 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25811 return;
25813 #endif
25814 #if defined (HAVE_GPM) || defined (MSDOS)
25815 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25816 #endif
25819 /* Display the active region described by mouse_face_* according to DRAW. */
25821 static void
25822 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25824 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25825 struct frame *f = XFRAME (WINDOW_FRAME (w));
25827 if (/* If window is in the process of being destroyed, don't bother
25828 to do anything. */
25829 w->current_matrix != NULL
25830 /* Don't update mouse highlight if hidden */
25831 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25832 /* Recognize when we are called to operate on rows that don't exist
25833 anymore. This can happen when a window is split. */
25834 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25836 int phys_cursor_on_p = w->phys_cursor_on_p;
25837 struct glyph_row *row, *first, *last;
25839 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25840 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25842 for (row = first; row <= last && row->enabled_p; ++row)
25844 int start_hpos, end_hpos, start_x;
25846 /* For all but the first row, the highlight starts at column 0. */
25847 if (row == first)
25849 /* R2L rows have BEG and END in reversed order, but the
25850 screen drawing geometry is always left to right. So
25851 we need to mirror the beginning and end of the
25852 highlighted area in R2L rows. */
25853 if (!row->reversed_p)
25855 start_hpos = hlinfo->mouse_face_beg_col;
25856 start_x = hlinfo->mouse_face_beg_x;
25858 else if (row == last)
25860 start_hpos = hlinfo->mouse_face_end_col;
25861 start_x = hlinfo->mouse_face_end_x;
25863 else
25865 start_hpos = 0;
25866 start_x = 0;
25869 else if (row->reversed_p && row == last)
25871 start_hpos = hlinfo->mouse_face_end_col;
25872 start_x = hlinfo->mouse_face_end_x;
25874 else
25876 start_hpos = 0;
25877 start_x = 0;
25880 if (row == last)
25882 if (!row->reversed_p)
25883 end_hpos = hlinfo->mouse_face_end_col;
25884 else if (row == first)
25885 end_hpos = hlinfo->mouse_face_beg_col;
25886 else
25888 end_hpos = row->used[TEXT_AREA];
25889 if (draw == DRAW_NORMAL_TEXT)
25890 row->fill_line_p = 1; /* Clear to end of line */
25893 else if (row->reversed_p && row == first)
25894 end_hpos = hlinfo->mouse_face_beg_col;
25895 else
25897 end_hpos = row->used[TEXT_AREA];
25898 if (draw == DRAW_NORMAL_TEXT)
25899 row->fill_line_p = 1; /* Clear to end of line */
25902 if (end_hpos > start_hpos)
25904 draw_row_with_mouse_face (w, start_x, row,
25905 start_hpos, end_hpos, draw);
25907 row->mouse_face_p
25908 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25912 #ifdef HAVE_WINDOW_SYSTEM
25913 /* When we've written over the cursor, arrange for it to
25914 be displayed again. */
25915 if (FRAME_WINDOW_P (f)
25916 && phys_cursor_on_p && !w->phys_cursor_on_p)
25918 int hpos = w->phys_cursor.hpos;
25920 /* When the window is hscrolled, cursor hpos can legitimately be
25921 out of bounds, but we draw the cursor at the corresponding
25922 window margin in that case. */
25923 if (!row->reversed_p && hpos < 0)
25924 hpos = 0;
25925 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25926 hpos = row->used[TEXT_AREA] - 1;
25928 BLOCK_INPUT;
25929 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25930 w->phys_cursor.x, w->phys_cursor.y);
25931 UNBLOCK_INPUT;
25933 #endif /* HAVE_WINDOW_SYSTEM */
25936 #ifdef HAVE_WINDOW_SYSTEM
25937 /* Change the mouse cursor. */
25938 if (FRAME_WINDOW_P (f))
25940 if (draw == DRAW_NORMAL_TEXT
25941 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25942 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25943 else if (draw == DRAW_MOUSE_FACE)
25944 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25945 else
25946 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25948 #endif /* HAVE_WINDOW_SYSTEM */
25951 /* EXPORT:
25952 Clear out the mouse-highlighted active region.
25953 Redraw it un-highlighted first. Value is non-zero if mouse
25954 face was actually drawn unhighlighted. */
25957 clear_mouse_face (Mouse_HLInfo *hlinfo)
25959 int cleared = 0;
25961 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25963 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25964 cleared = 1;
25967 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25968 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25969 hlinfo->mouse_face_window = Qnil;
25970 hlinfo->mouse_face_overlay = Qnil;
25971 return cleared;
25974 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25975 within the mouse face on that window. */
25976 static int
25977 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25979 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25981 /* Quickly resolve the easy cases. */
25982 if (!(WINDOWP (hlinfo->mouse_face_window)
25983 && XWINDOW (hlinfo->mouse_face_window) == w))
25984 return 0;
25985 if (vpos < hlinfo->mouse_face_beg_row
25986 || vpos > hlinfo->mouse_face_end_row)
25987 return 0;
25988 if (vpos > hlinfo->mouse_face_beg_row
25989 && vpos < hlinfo->mouse_face_end_row)
25990 return 1;
25992 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25994 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25996 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25997 return 1;
25999 else if ((vpos == hlinfo->mouse_face_beg_row
26000 && hpos >= hlinfo->mouse_face_beg_col)
26001 || (vpos == hlinfo->mouse_face_end_row
26002 && hpos < hlinfo->mouse_face_end_col))
26003 return 1;
26005 else
26007 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26009 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26010 return 1;
26012 else if ((vpos == hlinfo->mouse_face_beg_row
26013 && hpos <= hlinfo->mouse_face_beg_col)
26014 || (vpos == hlinfo->mouse_face_end_row
26015 && hpos > hlinfo->mouse_face_end_col))
26016 return 1;
26018 return 0;
26022 /* EXPORT:
26023 Non-zero if physical cursor of window W is within mouse face. */
26026 cursor_in_mouse_face_p (struct window *w)
26028 int hpos = w->phys_cursor.hpos;
26029 int vpos = w->phys_cursor.vpos;
26030 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26032 /* When the window is hscrolled, cursor hpos can legitimately be out
26033 of bounds, but we draw the cursor at the corresponding window
26034 margin in that case. */
26035 if (!row->reversed_p && hpos < 0)
26036 hpos = 0;
26037 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26038 hpos = row->used[TEXT_AREA] - 1;
26040 return coords_in_mouse_face_p (w, hpos, vpos);
26045 /* Find the glyph rows START_ROW and END_ROW of window W that display
26046 characters between buffer positions START_CHARPOS and END_CHARPOS
26047 (excluding END_CHARPOS). DISP_STRING is a display string that
26048 covers these buffer positions. This is similar to
26049 row_containing_pos, but is more accurate when bidi reordering makes
26050 buffer positions change non-linearly with glyph rows. */
26051 static void
26052 rows_from_pos_range (struct window *w,
26053 EMACS_INT start_charpos, EMACS_INT end_charpos,
26054 Lisp_Object disp_string,
26055 struct glyph_row **start, struct glyph_row **end)
26057 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26058 int last_y = window_text_bottom_y (w);
26059 struct glyph_row *row;
26061 *start = NULL;
26062 *end = NULL;
26064 while (!first->enabled_p
26065 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26066 first++;
26068 /* Find the START row. */
26069 for (row = first;
26070 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26071 row++)
26073 /* A row can potentially be the START row if the range of the
26074 characters it displays intersects the range
26075 [START_CHARPOS..END_CHARPOS). */
26076 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26077 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26078 /* See the commentary in row_containing_pos, for the
26079 explanation of the complicated way to check whether
26080 some position is beyond the end of the characters
26081 displayed by a row. */
26082 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26083 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26084 && !row->ends_at_zv_p
26085 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26086 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26087 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26088 && !row->ends_at_zv_p
26089 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26091 /* Found a candidate row. Now make sure at least one of the
26092 glyphs it displays has a charpos from the range
26093 [START_CHARPOS..END_CHARPOS).
26095 This is not obvious because bidi reordering could make
26096 buffer positions of a row be 1,2,3,102,101,100, and if we
26097 want to highlight characters in [50..60), we don't want
26098 this row, even though [50..60) does intersect [1..103),
26099 the range of character positions given by the row's start
26100 and end positions. */
26101 struct glyph *g = row->glyphs[TEXT_AREA];
26102 struct glyph *e = g + row->used[TEXT_AREA];
26104 while (g < e)
26106 if (((BUFFERP (g->object) || INTEGERP (g->object))
26107 && start_charpos <= g->charpos && g->charpos < end_charpos)
26108 /* A glyph that comes from DISP_STRING is by
26109 definition to be highlighted. */
26110 || EQ (g->object, disp_string))
26111 *start = row;
26112 g++;
26114 if (*start)
26115 break;
26119 /* Find the END row. */
26120 if (!*start
26121 /* If the last row is partially visible, start looking for END
26122 from that row, instead of starting from FIRST. */
26123 && !(row->enabled_p
26124 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26125 row = first;
26126 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26128 struct glyph_row *next = row + 1;
26129 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26131 if (!next->enabled_p
26132 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26133 /* The first row >= START whose range of displayed characters
26134 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26135 is the row END + 1. */
26136 || (start_charpos < next_start
26137 && end_charpos < next_start)
26138 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26139 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26140 && !next->ends_at_zv_p
26141 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26142 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26143 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26144 && !next->ends_at_zv_p
26145 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26147 *end = row;
26148 break;
26150 else
26152 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26153 but none of the characters it displays are in the range, it is
26154 also END + 1. */
26155 struct glyph *g = next->glyphs[TEXT_AREA];
26156 struct glyph *s = g;
26157 struct glyph *e = g + next->used[TEXT_AREA];
26159 while (g < e)
26161 if (((BUFFERP (g->object) || INTEGERP (g->object))
26162 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26163 /* If the buffer position of the first glyph in
26164 the row is equal to END_CHARPOS, it means
26165 the last character to be highlighted is the
26166 newline of ROW, and we must consider NEXT as
26167 END, not END+1. */
26168 || (((!next->reversed_p && g == s)
26169 || (next->reversed_p && g == e - 1))
26170 && (g->charpos == end_charpos
26171 /* Special case for when NEXT is an
26172 empty line at ZV. */
26173 || (g->charpos == -1
26174 && !row->ends_at_zv_p
26175 && next_start == end_charpos)))))
26176 /* A glyph that comes from DISP_STRING is by
26177 definition to be highlighted. */
26178 || EQ (g->object, disp_string))
26179 break;
26180 g++;
26182 if (g == e)
26184 *end = row;
26185 break;
26187 /* The first row that ends at ZV must be the last to be
26188 highlighted. */
26189 else if (next->ends_at_zv_p)
26191 *end = next;
26192 break;
26198 /* This function sets the mouse_face_* elements of HLINFO, assuming
26199 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26200 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26201 for the overlay or run of text properties specifying the mouse
26202 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26203 before-string and after-string that must also be highlighted.
26204 DISP_STRING, if non-nil, is a display string that may cover some
26205 or all of the highlighted text. */
26207 static void
26208 mouse_face_from_buffer_pos (Lisp_Object window,
26209 Mouse_HLInfo *hlinfo,
26210 EMACS_INT mouse_charpos,
26211 EMACS_INT start_charpos,
26212 EMACS_INT end_charpos,
26213 Lisp_Object before_string,
26214 Lisp_Object after_string,
26215 Lisp_Object disp_string)
26217 struct window *w = XWINDOW (window);
26218 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26219 struct glyph_row *r1, *r2;
26220 struct glyph *glyph, *end;
26221 EMACS_INT ignore, pos;
26222 int x;
26224 xassert (NILP (disp_string) || STRINGP (disp_string));
26225 xassert (NILP (before_string) || STRINGP (before_string));
26226 xassert (NILP (after_string) || STRINGP (after_string));
26228 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26229 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26230 if (r1 == NULL)
26231 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26232 /* If the before-string or display-string contains newlines,
26233 rows_from_pos_range skips to its last row. Move back. */
26234 if (!NILP (before_string) || !NILP (disp_string))
26236 struct glyph_row *prev;
26237 while ((prev = r1 - 1, prev >= first)
26238 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26239 && prev->used[TEXT_AREA] > 0)
26241 struct glyph *beg = prev->glyphs[TEXT_AREA];
26242 glyph = beg + prev->used[TEXT_AREA];
26243 while (--glyph >= beg && INTEGERP (glyph->object));
26244 if (glyph < beg
26245 || !(EQ (glyph->object, before_string)
26246 || EQ (glyph->object, disp_string)))
26247 break;
26248 r1 = prev;
26251 if (r2 == NULL)
26253 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26254 hlinfo->mouse_face_past_end = 1;
26256 else if (!NILP (after_string))
26258 /* If the after-string has newlines, advance to its last row. */
26259 struct glyph_row *next;
26260 struct glyph_row *last
26261 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26263 for (next = r2 + 1;
26264 next <= last
26265 && next->used[TEXT_AREA] > 0
26266 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26267 ++next)
26268 r2 = next;
26270 /* The rest of the display engine assumes that mouse_face_beg_row is
26271 either above mouse_face_end_row or identical to it. But with
26272 bidi-reordered continued lines, the row for START_CHARPOS could
26273 be below the row for END_CHARPOS. If so, swap the rows and store
26274 them in correct order. */
26275 if (r1->y > r2->y)
26277 struct glyph_row *tem = r2;
26279 r2 = r1;
26280 r1 = tem;
26283 hlinfo->mouse_face_beg_y = r1->y;
26284 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26285 hlinfo->mouse_face_end_y = r2->y;
26286 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26288 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26289 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26290 could be anywhere in the row and in any order. The strategy
26291 below is to find the leftmost and the rightmost glyph that
26292 belongs to either of these 3 strings, or whose position is
26293 between START_CHARPOS and END_CHARPOS, and highlight all the
26294 glyphs between those two. This may cover more than just the text
26295 between START_CHARPOS and END_CHARPOS if the range of characters
26296 strides the bidi level boundary, e.g. if the beginning is in R2L
26297 text while the end is in L2R text or vice versa. */
26298 if (!r1->reversed_p)
26300 /* This row is in a left to right paragraph. Scan it left to
26301 right. */
26302 glyph = r1->glyphs[TEXT_AREA];
26303 end = glyph + r1->used[TEXT_AREA];
26304 x = r1->x;
26306 /* Skip truncation glyphs at the start of the glyph row. */
26307 if (r1->displays_text_p)
26308 for (; glyph < end
26309 && INTEGERP (glyph->object)
26310 && glyph->charpos < 0;
26311 ++glyph)
26312 x += glyph->pixel_width;
26314 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26315 or DISP_STRING, and the first glyph from buffer whose
26316 position is between START_CHARPOS and END_CHARPOS. */
26317 for (; glyph < end
26318 && !INTEGERP (glyph->object)
26319 && !EQ (glyph->object, disp_string)
26320 && !(BUFFERP (glyph->object)
26321 && (glyph->charpos >= start_charpos
26322 && glyph->charpos < end_charpos));
26323 ++glyph)
26325 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26326 are present at buffer positions between START_CHARPOS and
26327 END_CHARPOS, or if they come from an overlay. */
26328 if (EQ (glyph->object, before_string))
26330 pos = string_buffer_position (before_string,
26331 start_charpos);
26332 /* If pos == 0, it means before_string came from an
26333 overlay, not from a buffer position. */
26334 if (!pos || (pos >= start_charpos && pos < end_charpos))
26335 break;
26337 else if (EQ (glyph->object, after_string))
26339 pos = string_buffer_position (after_string, end_charpos);
26340 if (!pos || (pos >= start_charpos && pos < end_charpos))
26341 break;
26343 x += glyph->pixel_width;
26345 hlinfo->mouse_face_beg_x = x;
26346 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26348 else
26350 /* This row is in a right to left paragraph. Scan it right to
26351 left. */
26352 struct glyph *g;
26354 end = r1->glyphs[TEXT_AREA] - 1;
26355 glyph = end + r1->used[TEXT_AREA];
26357 /* Skip truncation glyphs at the start of the glyph row. */
26358 if (r1->displays_text_p)
26359 for (; glyph > end
26360 && INTEGERP (glyph->object)
26361 && glyph->charpos < 0;
26362 --glyph)
26365 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26366 or DISP_STRING, and the first glyph from buffer whose
26367 position is between START_CHARPOS and END_CHARPOS. */
26368 for (; glyph > end
26369 && !INTEGERP (glyph->object)
26370 && !EQ (glyph->object, disp_string)
26371 && !(BUFFERP (glyph->object)
26372 && (glyph->charpos >= start_charpos
26373 && glyph->charpos < end_charpos));
26374 --glyph)
26376 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26377 are present at buffer positions between START_CHARPOS and
26378 END_CHARPOS, or if they come from an overlay. */
26379 if (EQ (glyph->object, before_string))
26381 pos = string_buffer_position (before_string, start_charpos);
26382 /* If pos == 0, it means before_string came from an
26383 overlay, not from a buffer position. */
26384 if (!pos || (pos >= start_charpos && pos < end_charpos))
26385 break;
26387 else if (EQ (glyph->object, after_string))
26389 pos = string_buffer_position (after_string, end_charpos);
26390 if (!pos || (pos >= start_charpos && pos < end_charpos))
26391 break;
26395 glyph++; /* first glyph to the right of the highlighted area */
26396 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26397 x += g->pixel_width;
26398 hlinfo->mouse_face_beg_x = x;
26399 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26402 /* If the highlight ends in a different row, compute GLYPH and END
26403 for the end row. Otherwise, reuse the values computed above for
26404 the row where the highlight begins. */
26405 if (r2 != r1)
26407 if (!r2->reversed_p)
26409 glyph = r2->glyphs[TEXT_AREA];
26410 end = glyph + r2->used[TEXT_AREA];
26411 x = r2->x;
26413 else
26415 end = r2->glyphs[TEXT_AREA] - 1;
26416 glyph = end + r2->used[TEXT_AREA];
26420 if (!r2->reversed_p)
26422 /* Skip truncation and continuation glyphs near the end of the
26423 row, and also blanks and stretch glyphs inserted by
26424 extend_face_to_end_of_line. */
26425 while (end > glyph
26426 && INTEGERP ((end - 1)->object))
26427 --end;
26428 /* Scan the rest of the glyph row from the end, looking for the
26429 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26430 DISP_STRING, or whose position is between START_CHARPOS
26431 and END_CHARPOS */
26432 for (--end;
26433 end > glyph
26434 && !INTEGERP (end->object)
26435 && !EQ (end->object, disp_string)
26436 && !(BUFFERP (end->object)
26437 && (end->charpos >= start_charpos
26438 && end->charpos < end_charpos));
26439 --end)
26441 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26442 are present at buffer positions between START_CHARPOS and
26443 END_CHARPOS, or if they come from an overlay. */
26444 if (EQ (end->object, before_string))
26446 pos = string_buffer_position (before_string, start_charpos);
26447 if (!pos || (pos >= start_charpos && pos < end_charpos))
26448 break;
26450 else if (EQ (end->object, after_string))
26452 pos = string_buffer_position (after_string, end_charpos);
26453 if (!pos || (pos >= start_charpos && pos < end_charpos))
26454 break;
26457 /* Find the X coordinate of the last glyph to be highlighted. */
26458 for (; glyph <= end; ++glyph)
26459 x += glyph->pixel_width;
26461 hlinfo->mouse_face_end_x = x;
26462 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26464 else
26466 /* Skip truncation and continuation glyphs near the end of the
26467 row, and also blanks and stretch glyphs inserted by
26468 extend_face_to_end_of_line. */
26469 x = r2->x;
26470 end++;
26471 while (end < glyph
26472 && INTEGERP (end->object))
26474 x += end->pixel_width;
26475 ++end;
26477 /* Scan the rest of the glyph row from the end, looking for the
26478 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26479 DISP_STRING, or whose position is between START_CHARPOS
26480 and END_CHARPOS */
26481 for ( ;
26482 end < glyph
26483 && !INTEGERP (end->object)
26484 && !EQ (end->object, disp_string)
26485 && !(BUFFERP (end->object)
26486 && (end->charpos >= start_charpos
26487 && end->charpos < end_charpos));
26488 ++end)
26490 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26491 are present at buffer positions between START_CHARPOS and
26492 END_CHARPOS, or if they come from an overlay. */
26493 if (EQ (end->object, before_string))
26495 pos = string_buffer_position (before_string, start_charpos);
26496 if (!pos || (pos >= start_charpos && pos < end_charpos))
26497 break;
26499 else if (EQ (end->object, after_string))
26501 pos = string_buffer_position (after_string, end_charpos);
26502 if (!pos || (pos >= start_charpos && pos < end_charpos))
26503 break;
26505 x += end->pixel_width;
26507 /* If we exited the above loop because we arrived at the last
26508 glyph of the row, and its buffer position is still not in
26509 range, it means the last character in range is the preceding
26510 newline. Bump the end column and x values to get past the
26511 last glyph. */
26512 if (end == glyph
26513 && BUFFERP (end->object)
26514 && (end->charpos < start_charpos
26515 || end->charpos >= end_charpos))
26517 x += end->pixel_width;
26518 ++end;
26520 hlinfo->mouse_face_end_x = x;
26521 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26524 hlinfo->mouse_face_window = window;
26525 hlinfo->mouse_face_face_id
26526 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26527 mouse_charpos + 1,
26528 !hlinfo->mouse_face_hidden, -1);
26529 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26532 /* The following function is not used anymore (replaced with
26533 mouse_face_from_string_pos), but I leave it here for the time
26534 being, in case someone would. */
26536 #if 0 /* not used */
26538 /* Find the position of the glyph for position POS in OBJECT in
26539 window W's current matrix, and return in *X, *Y the pixel
26540 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26542 RIGHT_P non-zero means return the position of the right edge of the
26543 glyph, RIGHT_P zero means return the left edge position.
26545 If no glyph for POS exists in the matrix, return the position of
26546 the glyph with the next smaller position that is in the matrix, if
26547 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26548 exists in the matrix, return the position of the glyph with the
26549 next larger position in OBJECT.
26551 Value is non-zero if a glyph was found. */
26553 static int
26554 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26555 int *hpos, int *vpos, int *x, int *y, int right_p)
26557 int yb = window_text_bottom_y (w);
26558 struct glyph_row *r;
26559 struct glyph *best_glyph = NULL;
26560 struct glyph_row *best_row = NULL;
26561 int best_x = 0;
26563 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26564 r->enabled_p && r->y < yb;
26565 ++r)
26567 struct glyph *g = r->glyphs[TEXT_AREA];
26568 struct glyph *e = g + r->used[TEXT_AREA];
26569 int gx;
26571 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26572 if (EQ (g->object, object))
26574 if (g->charpos == pos)
26576 best_glyph = g;
26577 best_x = gx;
26578 best_row = r;
26579 goto found;
26581 else if (best_glyph == NULL
26582 || ((eabs (g->charpos - pos)
26583 < eabs (best_glyph->charpos - pos))
26584 && (right_p
26585 ? g->charpos < pos
26586 : g->charpos > pos)))
26588 best_glyph = g;
26589 best_x = gx;
26590 best_row = r;
26595 found:
26597 if (best_glyph)
26599 *x = best_x;
26600 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26602 if (right_p)
26604 *x += best_glyph->pixel_width;
26605 ++*hpos;
26608 *y = best_row->y;
26609 *vpos = best_row - w->current_matrix->rows;
26612 return best_glyph != NULL;
26614 #endif /* not used */
26616 /* Find the positions of the first and the last glyphs in window W's
26617 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26618 (assumed to be a string), and return in HLINFO's mouse_face_*
26619 members the pixel and column/row coordinates of those glyphs. */
26621 static void
26622 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26623 Lisp_Object object,
26624 EMACS_INT startpos, EMACS_INT endpos)
26626 int yb = window_text_bottom_y (w);
26627 struct glyph_row *r;
26628 struct glyph *g, *e;
26629 int gx;
26630 int found = 0;
26632 /* Find the glyph row with at least one position in the range
26633 [STARTPOS..ENDPOS], and the first glyph in that row whose
26634 position belongs to that range. */
26635 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26636 r->enabled_p && r->y < yb;
26637 ++r)
26639 if (!r->reversed_p)
26641 g = r->glyphs[TEXT_AREA];
26642 e = g + r->used[TEXT_AREA];
26643 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26644 if (EQ (g->object, object)
26645 && startpos <= g->charpos && g->charpos <= endpos)
26647 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26648 hlinfo->mouse_face_beg_y = r->y;
26649 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26650 hlinfo->mouse_face_beg_x = gx;
26651 found = 1;
26652 break;
26655 else
26657 struct glyph *g1;
26659 e = r->glyphs[TEXT_AREA];
26660 g = e + r->used[TEXT_AREA];
26661 for ( ; g > e; --g)
26662 if (EQ ((g-1)->object, object)
26663 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26665 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26666 hlinfo->mouse_face_beg_y = r->y;
26667 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26668 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26669 gx += g1->pixel_width;
26670 hlinfo->mouse_face_beg_x = gx;
26671 found = 1;
26672 break;
26675 if (found)
26676 break;
26679 if (!found)
26680 return;
26682 /* Starting with the next row, look for the first row which does NOT
26683 include any glyphs whose positions are in the range. */
26684 for (++r; r->enabled_p && r->y < yb; ++r)
26686 g = r->glyphs[TEXT_AREA];
26687 e = g + r->used[TEXT_AREA];
26688 found = 0;
26689 for ( ; g < e; ++g)
26690 if (EQ (g->object, object)
26691 && startpos <= g->charpos && g->charpos <= endpos)
26693 found = 1;
26694 break;
26696 if (!found)
26697 break;
26700 /* The highlighted region ends on the previous row. */
26701 r--;
26703 /* Set the end row and its vertical pixel coordinate. */
26704 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26705 hlinfo->mouse_face_end_y = r->y;
26707 /* Compute and set the end column and the end column's horizontal
26708 pixel coordinate. */
26709 if (!r->reversed_p)
26711 g = r->glyphs[TEXT_AREA];
26712 e = g + r->used[TEXT_AREA];
26713 for ( ; e > g; --e)
26714 if (EQ ((e-1)->object, object)
26715 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26716 break;
26717 hlinfo->mouse_face_end_col = e - g;
26719 for (gx = r->x; g < e; ++g)
26720 gx += g->pixel_width;
26721 hlinfo->mouse_face_end_x = gx;
26723 else
26725 e = r->glyphs[TEXT_AREA];
26726 g = e + r->used[TEXT_AREA];
26727 for (gx = r->x ; e < g; ++e)
26729 if (EQ (e->object, object)
26730 && startpos <= e->charpos && e->charpos <= endpos)
26731 break;
26732 gx += e->pixel_width;
26734 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26735 hlinfo->mouse_face_end_x = gx;
26739 #ifdef HAVE_WINDOW_SYSTEM
26741 /* See if position X, Y is within a hot-spot of an image. */
26743 static int
26744 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26746 if (!CONSP (hot_spot))
26747 return 0;
26749 if (EQ (XCAR (hot_spot), Qrect))
26751 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26752 Lisp_Object rect = XCDR (hot_spot);
26753 Lisp_Object tem;
26754 if (!CONSP (rect))
26755 return 0;
26756 if (!CONSP (XCAR (rect)))
26757 return 0;
26758 if (!CONSP (XCDR (rect)))
26759 return 0;
26760 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26761 return 0;
26762 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26763 return 0;
26764 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26765 return 0;
26766 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26767 return 0;
26768 return 1;
26770 else if (EQ (XCAR (hot_spot), Qcircle))
26772 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26773 Lisp_Object circ = XCDR (hot_spot);
26774 Lisp_Object lr, lx0, ly0;
26775 if (CONSP (circ)
26776 && CONSP (XCAR (circ))
26777 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26778 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26779 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26781 double r = XFLOATINT (lr);
26782 double dx = XINT (lx0) - x;
26783 double dy = XINT (ly0) - y;
26784 return (dx * dx + dy * dy <= r * r);
26787 else if (EQ (XCAR (hot_spot), Qpoly))
26789 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26790 if (VECTORP (XCDR (hot_spot)))
26792 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26793 Lisp_Object *poly = v->contents;
26794 int n = v->header.size;
26795 int i;
26796 int inside = 0;
26797 Lisp_Object lx, ly;
26798 int x0, y0;
26800 /* Need an even number of coordinates, and at least 3 edges. */
26801 if (n < 6 || n & 1)
26802 return 0;
26804 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26805 If count is odd, we are inside polygon. Pixels on edges
26806 may or may not be included depending on actual geometry of the
26807 polygon. */
26808 if ((lx = poly[n-2], !INTEGERP (lx))
26809 || (ly = poly[n-1], !INTEGERP (lx)))
26810 return 0;
26811 x0 = XINT (lx), y0 = XINT (ly);
26812 for (i = 0; i < n; i += 2)
26814 int x1 = x0, y1 = y0;
26815 if ((lx = poly[i], !INTEGERP (lx))
26816 || (ly = poly[i+1], !INTEGERP (ly)))
26817 return 0;
26818 x0 = XINT (lx), y0 = XINT (ly);
26820 /* Does this segment cross the X line? */
26821 if (x0 >= x)
26823 if (x1 >= x)
26824 continue;
26826 else if (x1 < x)
26827 continue;
26828 if (y > y0 && y > y1)
26829 continue;
26830 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26831 inside = !inside;
26833 return inside;
26836 return 0;
26839 Lisp_Object
26840 find_hot_spot (Lisp_Object map, int x, int y)
26842 while (CONSP (map))
26844 if (CONSP (XCAR (map))
26845 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26846 return XCAR (map);
26847 map = XCDR (map);
26850 return Qnil;
26853 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26854 3, 3, 0,
26855 doc: /* Lookup in image map MAP coordinates X and Y.
26856 An image map is an alist where each element has the format (AREA ID PLIST).
26857 An AREA is specified as either a rectangle, a circle, or a polygon:
26858 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26859 pixel coordinates of the upper left and bottom right corners.
26860 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26861 and the radius of the circle; r may be a float or integer.
26862 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26863 vector describes one corner in the polygon.
26864 Returns the alist element for the first matching AREA in MAP. */)
26865 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26867 if (NILP (map))
26868 return Qnil;
26870 CHECK_NUMBER (x);
26871 CHECK_NUMBER (y);
26873 return find_hot_spot (map, XINT (x), XINT (y));
26877 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26878 static void
26879 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26881 /* Do not change cursor shape while dragging mouse. */
26882 if (!NILP (do_mouse_tracking))
26883 return;
26885 if (!NILP (pointer))
26887 if (EQ (pointer, Qarrow))
26888 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26889 else if (EQ (pointer, Qhand))
26890 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26891 else if (EQ (pointer, Qtext))
26892 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26893 else if (EQ (pointer, intern ("hdrag")))
26894 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26895 #ifdef HAVE_X_WINDOWS
26896 else if (EQ (pointer, intern ("vdrag")))
26897 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26898 #endif
26899 else if (EQ (pointer, intern ("hourglass")))
26900 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26901 else if (EQ (pointer, Qmodeline))
26902 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26903 else
26904 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26907 if (cursor != No_Cursor)
26908 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26911 #endif /* HAVE_WINDOW_SYSTEM */
26913 /* Take proper action when mouse has moved to the mode or header line
26914 or marginal area AREA of window W, x-position X and y-position Y.
26915 X is relative to the start of the text display area of W, so the
26916 width of bitmap areas and scroll bars must be subtracted to get a
26917 position relative to the start of the mode line. */
26919 static void
26920 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26921 enum window_part area)
26923 struct window *w = XWINDOW (window);
26924 struct frame *f = XFRAME (w->frame);
26925 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26926 #ifdef HAVE_WINDOW_SYSTEM
26927 Display_Info *dpyinfo;
26928 #endif
26929 Cursor cursor = No_Cursor;
26930 Lisp_Object pointer = Qnil;
26931 int dx, dy, width, height;
26932 EMACS_INT charpos;
26933 Lisp_Object string, object = Qnil;
26934 Lisp_Object pos, help;
26936 Lisp_Object mouse_face;
26937 int original_x_pixel = x;
26938 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26939 struct glyph_row *row;
26941 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26943 int x0;
26944 struct glyph *end;
26946 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26947 returns them in row/column units! */
26948 string = mode_line_string (w, area, &x, &y, &charpos,
26949 &object, &dx, &dy, &width, &height);
26951 row = (area == ON_MODE_LINE
26952 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26953 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26955 /* Find the glyph under the mouse pointer. */
26956 if (row->mode_line_p && row->enabled_p)
26958 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26959 end = glyph + row->used[TEXT_AREA];
26961 for (x0 = original_x_pixel;
26962 glyph < end && x0 >= glyph->pixel_width;
26963 ++glyph)
26964 x0 -= glyph->pixel_width;
26966 if (glyph >= end)
26967 glyph = NULL;
26970 else
26972 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26973 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26974 returns them in row/column units! */
26975 string = marginal_area_string (w, area, &x, &y, &charpos,
26976 &object, &dx, &dy, &width, &height);
26979 help = Qnil;
26981 #ifdef HAVE_WINDOW_SYSTEM
26982 if (IMAGEP (object))
26984 Lisp_Object image_map, hotspot;
26985 if ((image_map = Fplist_get (XCDR (object), QCmap),
26986 !NILP (image_map))
26987 && (hotspot = find_hot_spot (image_map, dx, dy),
26988 CONSP (hotspot))
26989 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26991 Lisp_Object plist;
26993 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26994 If so, we could look for mouse-enter, mouse-leave
26995 properties in PLIST (and do something...). */
26996 hotspot = XCDR (hotspot);
26997 if (CONSP (hotspot)
26998 && (plist = XCAR (hotspot), CONSP (plist)))
27000 pointer = Fplist_get (plist, Qpointer);
27001 if (NILP (pointer))
27002 pointer = Qhand;
27003 help = Fplist_get (plist, Qhelp_echo);
27004 if (!NILP (help))
27006 help_echo_string = help;
27007 /* Is this correct? ++kfs */
27008 XSETWINDOW (help_echo_window, w);
27009 help_echo_object = w->buffer;
27010 help_echo_pos = charpos;
27014 if (NILP (pointer))
27015 pointer = Fplist_get (XCDR (object), QCpointer);
27017 #endif /* HAVE_WINDOW_SYSTEM */
27019 if (STRINGP (string))
27021 pos = make_number (charpos);
27022 /* If we're on a string with `help-echo' text property, arrange
27023 for the help to be displayed. This is done by setting the
27024 global variable help_echo_string to the help string. */
27025 if (NILP (help))
27027 help = Fget_text_property (pos, Qhelp_echo, string);
27028 if (!NILP (help))
27030 help_echo_string = help;
27031 XSETWINDOW (help_echo_window, w);
27032 help_echo_object = string;
27033 help_echo_pos = charpos;
27037 #ifdef HAVE_WINDOW_SYSTEM
27038 if (FRAME_WINDOW_P (f))
27040 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27041 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27042 if (NILP (pointer))
27043 pointer = Fget_text_property (pos, Qpointer, string);
27045 /* Change the mouse pointer according to what is under X/Y. */
27046 if (NILP (pointer)
27047 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27049 Lisp_Object map;
27050 map = Fget_text_property (pos, Qlocal_map, string);
27051 if (!KEYMAPP (map))
27052 map = Fget_text_property (pos, Qkeymap, string);
27053 if (!KEYMAPP (map))
27054 cursor = dpyinfo->vertical_scroll_bar_cursor;
27057 #endif
27059 /* Change the mouse face according to what is under X/Y. */
27060 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27061 if (!NILP (mouse_face)
27062 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27063 && glyph)
27065 Lisp_Object b, e;
27067 struct glyph * tmp_glyph;
27069 int gpos;
27070 int gseq_length;
27071 int total_pixel_width;
27072 EMACS_INT begpos, endpos, ignore;
27074 int vpos, hpos;
27076 b = Fprevious_single_property_change (make_number (charpos + 1),
27077 Qmouse_face, string, Qnil);
27078 if (NILP (b))
27079 begpos = 0;
27080 else
27081 begpos = XINT (b);
27083 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27084 if (NILP (e))
27085 endpos = SCHARS (string);
27086 else
27087 endpos = XINT (e);
27089 /* Calculate the glyph position GPOS of GLYPH in the
27090 displayed string, relative to the beginning of the
27091 highlighted part of the string.
27093 Note: GPOS is different from CHARPOS. CHARPOS is the
27094 position of GLYPH in the internal string object. A mode
27095 line string format has structures which are converted to
27096 a flattened string by the Emacs Lisp interpreter. The
27097 internal string is an element of those structures. The
27098 displayed string is the flattened string. */
27099 tmp_glyph = row_start_glyph;
27100 while (tmp_glyph < glyph
27101 && (!(EQ (tmp_glyph->object, glyph->object)
27102 && begpos <= tmp_glyph->charpos
27103 && tmp_glyph->charpos < endpos)))
27104 tmp_glyph++;
27105 gpos = glyph - tmp_glyph;
27107 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27108 the highlighted part of the displayed string to which
27109 GLYPH belongs. Note: GSEQ_LENGTH is different from
27110 SCHARS (STRING), because the latter returns the length of
27111 the internal string. */
27112 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27113 tmp_glyph > glyph
27114 && (!(EQ (tmp_glyph->object, glyph->object)
27115 && begpos <= tmp_glyph->charpos
27116 && tmp_glyph->charpos < endpos));
27117 tmp_glyph--)
27119 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27121 /* Calculate the total pixel width of all the glyphs between
27122 the beginning of the highlighted area and GLYPH. */
27123 total_pixel_width = 0;
27124 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27125 total_pixel_width += tmp_glyph->pixel_width;
27127 /* Pre calculation of re-rendering position. Note: X is in
27128 column units here, after the call to mode_line_string or
27129 marginal_area_string. */
27130 hpos = x - gpos;
27131 vpos = (area == ON_MODE_LINE
27132 ? (w->current_matrix)->nrows - 1
27133 : 0);
27135 /* If GLYPH's position is included in the region that is
27136 already drawn in mouse face, we have nothing to do. */
27137 if ( EQ (window, hlinfo->mouse_face_window)
27138 && (!row->reversed_p
27139 ? (hlinfo->mouse_face_beg_col <= hpos
27140 && hpos < hlinfo->mouse_face_end_col)
27141 /* In R2L rows we swap BEG and END, see below. */
27142 : (hlinfo->mouse_face_end_col <= hpos
27143 && hpos < hlinfo->mouse_face_beg_col))
27144 && hlinfo->mouse_face_beg_row == vpos )
27145 return;
27147 if (clear_mouse_face (hlinfo))
27148 cursor = No_Cursor;
27150 if (!row->reversed_p)
27152 hlinfo->mouse_face_beg_col = hpos;
27153 hlinfo->mouse_face_beg_x = original_x_pixel
27154 - (total_pixel_width + dx);
27155 hlinfo->mouse_face_end_col = hpos + gseq_length;
27156 hlinfo->mouse_face_end_x = 0;
27158 else
27160 /* In R2L rows, show_mouse_face expects BEG and END
27161 coordinates to be swapped. */
27162 hlinfo->mouse_face_end_col = hpos;
27163 hlinfo->mouse_face_end_x = original_x_pixel
27164 - (total_pixel_width + dx);
27165 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27166 hlinfo->mouse_face_beg_x = 0;
27169 hlinfo->mouse_face_beg_row = vpos;
27170 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27171 hlinfo->mouse_face_beg_y = 0;
27172 hlinfo->mouse_face_end_y = 0;
27173 hlinfo->mouse_face_past_end = 0;
27174 hlinfo->mouse_face_window = window;
27176 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27177 charpos,
27178 0, 0, 0,
27179 &ignore,
27180 glyph->face_id,
27182 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27184 if (NILP (pointer))
27185 pointer = Qhand;
27187 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27188 clear_mouse_face (hlinfo);
27190 #ifdef HAVE_WINDOW_SYSTEM
27191 if (FRAME_WINDOW_P (f))
27192 define_frame_cursor1 (f, cursor, pointer);
27193 #endif
27197 /* EXPORT:
27198 Take proper action when the mouse has moved to position X, Y on
27199 frame F as regards highlighting characters that have mouse-face
27200 properties. Also de-highlighting chars where the mouse was before.
27201 X and Y can be negative or out of range. */
27203 void
27204 note_mouse_highlight (struct frame *f, int x, int y)
27206 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27207 enum window_part part = ON_NOTHING;
27208 Lisp_Object window;
27209 struct window *w;
27210 Cursor cursor = No_Cursor;
27211 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27212 struct buffer *b;
27214 /* When a menu is active, don't highlight because this looks odd. */
27215 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27216 if (popup_activated ())
27217 return;
27218 #endif
27220 if (NILP (Vmouse_highlight)
27221 || !f->glyphs_initialized_p
27222 || f->pointer_invisible)
27223 return;
27225 hlinfo->mouse_face_mouse_x = x;
27226 hlinfo->mouse_face_mouse_y = y;
27227 hlinfo->mouse_face_mouse_frame = f;
27229 if (hlinfo->mouse_face_defer)
27230 return;
27232 if (gc_in_progress)
27234 hlinfo->mouse_face_deferred_gc = 1;
27235 return;
27238 /* Which window is that in? */
27239 window = window_from_coordinates (f, x, y, &part, 1);
27241 /* If displaying active text in another window, clear that. */
27242 if (! EQ (window, hlinfo->mouse_face_window)
27243 /* Also clear if we move out of text area in same window. */
27244 || (!NILP (hlinfo->mouse_face_window)
27245 && !NILP (window)
27246 && part != ON_TEXT
27247 && part != ON_MODE_LINE
27248 && part != ON_HEADER_LINE))
27249 clear_mouse_face (hlinfo);
27251 /* Not on a window -> return. */
27252 if (!WINDOWP (window))
27253 return;
27255 /* Reset help_echo_string. It will get recomputed below. */
27256 help_echo_string = Qnil;
27258 /* Convert to window-relative pixel coordinates. */
27259 w = XWINDOW (window);
27260 frame_to_window_pixel_xy (w, &x, &y);
27262 #ifdef HAVE_WINDOW_SYSTEM
27263 /* Handle tool-bar window differently since it doesn't display a
27264 buffer. */
27265 if (EQ (window, f->tool_bar_window))
27267 note_tool_bar_highlight (f, x, y);
27268 return;
27270 #endif
27272 /* Mouse is on the mode, header line or margin? */
27273 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27274 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27276 note_mode_line_or_margin_highlight (window, x, y, part);
27277 return;
27280 #ifdef HAVE_WINDOW_SYSTEM
27281 if (part == ON_VERTICAL_BORDER)
27283 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27284 help_echo_string = build_string ("drag-mouse-1: resize");
27286 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27287 || part == ON_SCROLL_BAR)
27288 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27289 else
27290 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27291 #endif
27293 /* Are we in a window whose display is up to date?
27294 And verify the buffer's text has not changed. */
27295 b = XBUFFER (w->buffer);
27296 if (part == ON_TEXT
27297 && EQ (w->window_end_valid, w->buffer)
27298 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27299 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27301 int hpos, vpos, dx, dy, area = LAST_AREA;
27302 EMACS_INT pos;
27303 struct glyph *glyph;
27304 Lisp_Object object;
27305 Lisp_Object mouse_face = Qnil, position;
27306 Lisp_Object *overlay_vec = NULL;
27307 ptrdiff_t i, noverlays;
27308 struct buffer *obuf;
27309 EMACS_INT obegv, ozv;
27310 int same_region;
27312 /* Find the glyph under X/Y. */
27313 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27315 #ifdef HAVE_WINDOW_SYSTEM
27316 /* Look for :pointer property on image. */
27317 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27319 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27320 if (img != NULL && IMAGEP (img->spec))
27322 Lisp_Object image_map, hotspot;
27323 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27324 !NILP (image_map))
27325 && (hotspot = find_hot_spot (image_map,
27326 glyph->slice.img.x + dx,
27327 glyph->slice.img.y + dy),
27328 CONSP (hotspot))
27329 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27331 Lisp_Object plist;
27333 /* Could check XCAR (hotspot) to see if we enter/leave
27334 this hot-spot.
27335 If so, we could look for mouse-enter, mouse-leave
27336 properties in PLIST (and do something...). */
27337 hotspot = XCDR (hotspot);
27338 if (CONSP (hotspot)
27339 && (plist = XCAR (hotspot), CONSP (plist)))
27341 pointer = Fplist_get (plist, Qpointer);
27342 if (NILP (pointer))
27343 pointer = Qhand;
27344 help_echo_string = Fplist_get (plist, Qhelp_echo);
27345 if (!NILP (help_echo_string))
27347 help_echo_window = window;
27348 help_echo_object = glyph->object;
27349 help_echo_pos = glyph->charpos;
27353 if (NILP (pointer))
27354 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27357 #endif /* HAVE_WINDOW_SYSTEM */
27359 /* Clear mouse face if X/Y not over text. */
27360 if (glyph == NULL
27361 || area != TEXT_AREA
27362 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27363 /* Glyph's OBJECT is an integer for glyphs inserted by the
27364 display engine for its internal purposes, like truncation
27365 and continuation glyphs and blanks beyond the end of
27366 line's text on text terminals. If we are over such a
27367 glyph, we are not over any text. */
27368 || INTEGERP (glyph->object)
27369 /* R2L rows have a stretch glyph at their front, which
27370 stands for no text, whereas L2R rows have no glyphs at
27371 all beyond the end of text. Treat such stretch glyphs
27372 like we do with NULL glyphs in L2R rows. */
27373 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27374 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27375 && glyph->type == STRETCH_GLYPH
27376 && glyph->avoid_cursor_p))
27378 if (clear_mouse_face (hlinfo))
27379 cursor = No_Cursor;
27380 #ifdef HAVE_WINDOW_SYSTEM
27381 if (FRAME_WINDOW_P (f) && NILP (pointer))
27383 if (area != TEXT_AREA)
27384 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27385 else
27386 pointer = Vvoid_text_area_pointer;
27388 #endif
27389 goto set_cursor;
27392 pos = glyph->charpos;
27393 object = glyph->object;
27394 if (!STRINGP (object) && !BUFFERP (object))
27395 goto set_cursor;
27397 /* If we get an out-of-range value, return now; avoid an error. */
27398 if (BUFFERP (object) && pos > BUF_Z (b))
27399 goto set_cursor;
27401 /* Make the window's buffer temporarily current for
27402 overlays_at and compute_char_face. */
27403 obuf = current_buffer;
27404 current_buffer = b;
27405 obegv = BEGV;
27406 ozv = ZV;
27407 BEGV = BEG;
27408 ZV = Z;
27410 /* Is this char mouse-active or does it have help-echo? */
27411 position = make_number (pos);
27413 if (BUFFERP (object))
27415 /* Put all the overlays we want in a vector in overlay_vec. */
27416 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27417 /* Sort overlays into increasing priority order. */
27418 noverlays = sort_overlays (overlay_vec, noverlays, w);
27420 else
27421 noverlays = 0;
27423 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27425 if (same_region)
27426 cursor = No_Cursor;
27428 /* Check mouse-face highlighting. */
27429 if (! same_region
27430 /* If there exists an overlay with mouse-face overlapping
27431 the one we are currently highlighting, we have to
27432 check if we enter the overlapping overlay, and then
27433 highlight only that. */
27434 || (OVERLAYP (hlinfo->mouse_face_overlay)
27435 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27437 /* Find the highest priority overlay with a mouse-face. */
27438 Lisp_Object overlay = Qnil;
27439 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27441 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27442 if (!NILP (mouse_face))
27443 overlay = overlay_vec[i];
27446 /* If we're highlighting the same overlay as before, there's
27447 no need to do that again. */
27448 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27449 goto check_help_echo;
27450 hlinfo->mouse_face_overlay = overlay;
27452 /* Clear the display of the old active region, if any. */
27453 if (clear_mouse_face (hlinfo))
27454 cursor = No_Cursor;
27456 /* If no overlay applies, get a text property. */
27457 if (NILP (overlay))
27458 mouse_face = Fget_text_property (position, Qmouse_face, object);
27460 /* Next, compute the bounds of the mouse highlighting and
27461 display it. */
27462 if (!NILP (mouse_face) && STRINGP (object))
27464 /* The mouse-highlighting comes from a display string
27465 with a mouse-face. */
27466 Lisp_Object s, e;
27467 EMACS_INT ignore;
27469 s = Fprevious_single_property_change
27470 (make_number (pos + 1), Qmouse_face, object, Qnil);
27471 e = Fnext_single_property_change
27472 (position, Qmouse_face, object, Qnil);
27473 if (NILP (s))
27474 s = make_number (0);
27475 if (NILP (e))
27476 e = make_number (SCHARS (object) - 1);
27477 mouse_face_from_string_pos (w, hlinfo, object,
27478 XINT (s), XINT (e));
27479 hlinfo->mouse_face_past_end = 0;
27480 hlinfo->mouse_face_window = window;
27481 hlinfo->mouse_face_face_id
27482 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27483 glyph->face_id, 1);
27484 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27485 cursor = No_Cursor;
27487 else
27489 /* The mouse-highlighting, if any, comes from an overlay
27490 or text property in the buffer. */
27491 Lisp_Object buffer IF_LINT (= Qnil);
27492 Lisp_Object disp_string IF_LINT (= Qnil);
27494 if (STRINGP (object))
27496 /* If we are on a display string with no mouse-face,
27497 check if the text under it has one. */
27498 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27499 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27500 pos = string_buffer_position (object, start);
27501 if (pos > 0)
27503 mouse_face = get_char_property_and_overlay
27504 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27505 buffer = w->buffer;
27506 disp_string = object;
27509 else
27511 buffer = object;
27512 disp_string = Qnil;
27515 if (!NILP (mouse_face))
27517 Lisp_Object before, after;
27518 Lisp_Object before_string, after_string;
27519 /* To correctly find the limits of mouse highlight
27520 in a bidi-reordered buffer, we must not use the
27521 optimization of limiting the search in
27522 previous-single-property-change and
27523 next-single-property-change, because
27524 rows_from_pos_range needs the real start and end
27525 positions to DTRT in this case. That's because
27526 the first row visible in a window does not
27527 necessarily display the character whose position
27528 is the smallest. */
27529 Lisp_Object lim1 =
27530 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27531 ? Fmarker_position (w->start)
27532 : Qnil;
27533 Lisp_Object lim2 =
27534 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27535 ? make_number (BUF_Z (XBUFFER (buffer))
27536 - XFASTINT (w->window_end_pos))
27537 : Qnil;
27539 if (NILP (overlay))
27541 /* Handle the text property case. */
27542 before = Fprevious_single_property_change
27543 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27544 after = Fnext_single_property_change
27545 (make_number (pos), Qmouse_face, buffer, lim2);
27546 before_string = after_string = Qnil;
27548 else
27550 /* Handle the overlay case. */
27551 before = Foverlay_start (overlay);
27552 after = Foverlay_end (overlay);
27553 before_string = Foverlay_get (overlay, Qbefore_string);
27554 after_string = Foverlay_get (overlay, Qafter_string);
27556 if (!STRINGP (before_string)) before_string = Qnil;
27557 if (!STRINGP (after_string)) after_string = Qnil;
27560 mouse_face_from_buffer_pos (window, hlinfo, pos,
27561 NILP (before)
27563 : XFASTINT (before),
27564 NILP (after)
27565 ? BUF_Z (XBUFFER (buffer))
27566 : XFASTINT (after),
27567 before_string, after_string,
27568 disp_string);
27569 cursor = No_Cursor;
27574 check_help_echo:
27576 /* Look for a `help-echo' property. */
27577 if (NILP (help_echo_string)) {
27578 Lisp_Object help, overlay;
27580 /* Check overlays first. */
27581 help = overlay = Qnil;
27582 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27584 overlay = overlay_vec[i];
27585 help = Foverlay_get (overlay, Qhelp_echo);
27588 if (!NILP (help))
27590 help_echo_string = help;
27591 help_echo_window = window;
27592 help_echo_object = overlay;
27593 help_echo_pos = pos;
27595 else
27597 Lisp_Object obj = glyph->object;
27598 EMACS_INT charpos = glyph->charpos;
27600 /* Try text properties. */
27601 if (STRINGP (obj)
27602 && charpos >= 0
27603 && charpos < SCHARS (obj))
27605 help = Fget_text_property (make_number (charpos),
27606 Qhelp_echo, obj);
27607 if (NILP (help))
27609 /* If the string itself doesn't specify a help-echo,
27610 see if the buffer text ``under'' it does. */
27611 struct glyph_row *r
27612 = MATRIX_ROW (w->current_matrix, vpos);
27613 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27614 EMACS_INT p = string_buffer_position (obj, start);
27615 if (p > 0)
27617 help = Fget_char_property (make_number (p),
27618 Qhelp_echo, w->buffer);
27619 if (!NILP (help))
27621 charpos = p;
27622 obj = w->buffer;
27627 else if (BUFFERP (obj)
27628 && charpos >= BEGV
27629 && charpos < ZV)
27630 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27631 obj);
27633 if (!NILP (help))
27635 help_echo_string = help;
27636 help_echo_window = window;
27637 help_echo_object = obj;
27638 help_echo_pos = charpos;
27643 #ifdef HAVE_WINDOW_SYSTEM
27644 /* Look for a `pointer' property. */
27645 if (FRAME_WINDOW_P (f) && NILP (pointer))
27647 /* Check overlays first. */
27648 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27649 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27651 if (NILP (pointer))
27653 Lisp_Object obj = glyph->object;
27654 EMACS_INT charpos = glyph->charpos;
27656 /* Try text properties. */
27657 if (STRINGP (obj)
27658 && charpos >= 0
27659 && charpos < SCHARS (obj))
27661 pointer = Fget_text_property (make_number (charpos),
27662 Qpointer, obj);
27663 if (NILP (pointer))
27665 /* If the string itself doesn't specify a pointer,
27666 see if the buffer text ``under'' it does. */
27667 struct glyph_row *r
27668 = MATRIX_ROW (w->current_matrix, vpos);
27669 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27670 EMACS_INT p = string_buffer_position (obj, start);
27671 if (p > 0)
27672 pointer = Fget_char_property (make_number (p),
27673 Qpointer, w->buffer);
27676 else if (BUFFERP (obj)
27677 && charpos >= BEGV
27678 && charpos < ZV)
27679 pointer = Fget_text_property (make_number (charpos),
27680 Qpointer, obj);
27683 #endif /* HAVE_WINDOW_SYSTEM */
27685 BEGV = obegv;
27686 ZV = ozv;
27687 current_buffer = obuf;
27690 set_cursor:
27692 #ifdef HAVE_WINDOW_SYSTEM
27693 if (FRAME_WINDOW_P (f))
27694 define_frame_cursor1 (f, cursor, pointer);
27695 #else
27696 /* This is here to prevent a compiler error, about "label at end of
27697 compound statement". */
27698 return;
27699 #endif
27703 /* EXPORT for RIF:
27704 Clear any mouse-face on window W. This function is part of the
27705 redisplay interface, and is called from try_window_id and similar
27706 functions to ensure the mouse-highlight is off. */
27708 void
27709 x_clear_window_mouse_face (struct window *w)
27711 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27712 Lisp_Object window;
27714 BLOCK_INPUT;
27715 XSETWINDOW (window, w);
27716 if (EQ (window, hlinfo->mouse_face_window))
27717 clear_mouse_face (hlinfo);
27718 UNBLOCK_INPUT;
27722 /* EXPORT:
27723 Just discard the mouse face information for frame F, if any.
27724 This is used when the size of F is changed. */
27726 void
27727 cancel_mouse_face (struct frame *f)
27729 Lisp_Object window;
27730 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27732 window = hlinfo->mouse_face_window;
27733 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27735 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27736 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27737 hlinfo->mouse_face_window = Qnil;
27743 /***********************************************************************
27744 Exposure Events
27745 ***********************************************************************/
27747 #ifdef HAVE_WINDOW_SYSTEM
27749 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27750 which intersects rectangle R. R is in window-relative coordinates. */
27752 static void
27753 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27754 enum glyph_row_area area)
27756 struct glyph *first = row->glyphs[area];
27757 struct glyph *end = row->glyphs[area] + row->used[area];
27758 struct glyph *last;
27759 int first_x, start_x, x;
27761 if (area == TEXT_AREA && row->fill_line_p)
27762 /* If row extends face to end of line write the whole line. */
27763 draw_glyphs (w, 0, row, area,
27764 0, row->used[area],
27765 DRAW_NORMAL_TEXT, 0);
27766 else
27768 /* Set START_X to the window-relative start position for drawing glyphs of
27769 AREA. The first glyph of the text area can be partially visible.
27770 The first glyphs of other areas cannot. */
27771 start_x = window_box_left_offset (w, area);
27772 x = start_x;
27773 if (area == TEXT_AREA)
27774 x += row->x;
27776 /* Find the first glyph that must be redrawn. */
27777 while (first < end
27778 && x + first->pixel_width < r->x)
27780 x += first->pixel_width;
27781 ++first;
27784 /* Find the last one. */
27785 last = first;
27786 first_x = x;
27787 while (last < end
27788 && x < r->x + r->width)
27790 x += last->pixel_width;
27791 ++last;
27794 /* Repaint. */
27795 if (last > first)
27796 draw_glyphs (w, first_x - start_x, row, area,
27797 first - row->glyphs[area], last - row->glyphs[area],
27798 DRAW_NORMAL_TEXT, 0);
27803 /* Redraw the parts of the glyph row ROW on window W intersecting
27804 rectangle R. R is in window-relative coordinates. Value is
27805 non-zero if mouse-face was overwritten. */
27807 static int
27808 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27810 xassert (row->enabled_p);
27812 if (row->mode_line_p || w->pseudo_window_p)
27813 draw_glyphs (w, 0, row, TEXT_AREA,
27814 0, row->used[TEXT_AREA],
27815 DRAW_NORMAL_TEXT, 0);
27816 else
27818 if (row->used[LEFT_MARGIN_AREA])
27819 expose_area (w, row, r, LEFT_MARGIN_AREA);
27820 if (row->used[TEXT_AREA])
27821 expose_area (w, row, r, TEXT_AREA);
27822 if (row->used[RIGHT_MARGIN_AREA])
27823 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27824 draw_row_fringe_bitmaps (w, row);
27827 return row->mouse_face_p;
27831 /* Redraw those parts of glyphs rows during expose event handling that
27832 overlap other rows. Redrawing of an exposed line writes over parts
27833 of lines overlapping that exposed line; this function fixes that.
27835 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27836 row in W's current matrix that is exposed and overlaps other rows.
27837 LAST_OVERLAPPING_ROW is the last such row. */
27839 static void
27840 expose_overlaps (struct window *w,
27841 struct glyph_row *first_overlapping_row,
27842 struct glyph_row *last_overlapping_row,
27843 XRectangle *r)
27845 struct glyph_row *row;
27847 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27848 if (row->overlapping_p)
27850 xassert (row->enabled_p && !row->mode_line_p);
27852 row->clip = r;
27853 if (row->used[LEFT_MARGIN_AREA])
27854 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27856 if (row->used[TEXT_AREA])
27857 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27859 if (row->used[RIGHT_MARGIN_AREA])
27860 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27861 row->clip = NULL;
27866 /* Return non-zero if W's cursor intersects rectangle R. */
27868 static int
27869 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27871 XRectangle cr, result;
27872 struct glyph *cursor_glyph;
27873 struct glyph_row *row;
27875 if (w->phys_cursor.vpos >= 0
27876 && w->phys_cursor.vpos < w->current_matrix->nrows
27877 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27878 row->enabled_p)
27879 && row->cursor_in_fringe_p)
27881 /* Cursor is in the fringe. */
27882 cr.x = window_box_right_offset (w,
27883 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27884 ? RIGHT_MARGIN_AREA
27885 : TEXT_AREA));
27886 cr.y = row->y;
27887 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27888 cr.height = row->height;
27889 return x_intersect_rectangles (&cr, r, &result);
27892 cursor_glyph = get_phys_cursor_glyph (w);
27893 if (cursor_glyph)
27895 /* r is relative to W's box, but w->phys_cursor.x is relative
27896 to left edge of W's TEXT area. Adjust it. */
27897 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27898 cr.y = w->phys_cursor.y;
27899 cr.width = cursor_glyph->pixel_width;
27900 cr.height = w->phys_cursor_height;
27901 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27902 I assume the effect is the same -- and this is portable. */
27903 return x_intersect_rectangles (&cr, r, &result);
27905 /* If we don't understand the format, pretend we're not in the hot-spot. */
27906 return 0;
27910 /* EXPORT:
27911 Draw a vertical window border to the right of window W if W doesn't
27912 have vertical scroll bars. */
27914 void
27915 x_draw_vertical_border (struct window *w)
27917 struct frame *f = XFRAME (WINDOW_FRAME (w));
27919 /* We could do better, if we knew what type of scroll-bar the adjacent
27920 windows (on either side) have... But we don't :-(
27921 However, I think this works ok. ++KFS 2003-04-25 */
27923 /* Redraw borders between horizontally adjacent windows. Don't
27924 do it for frames with vertical scroll bars because either the
27925 right scroll bar of a window, or the left scroll bar of its
27926 neighbor will suffice as a border. */
27927 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27928 return;
27930 if (!WINDOW_RIGHTMOST_P (w)
27931 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27933 int x0, x1, y0, y1;
27935 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27936 y1 -= 1;
27938 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27939 x1 -= 1;
27941 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27943 else if (!WINDOW_LEFTMOST_P (w)
27944 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27946 int x0, x1, y0, y1;
27948 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27949 y1 -= 1;
27951 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27952 x0 -= 1;
27954 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27959 /* Redraw the part of window W intersection rectangle FR. Pixel
27960 coordinates in FR are frame-relative. Call this function with
27961 input blocked. Value is non-zero if the exposure overwrites
27962 mouse-face. */
27964 static int
27965 expose_window (struct window *w, XRectangle *fr)
27967 struct frame *f = XFRAME (w->frame);
27968 XRectangle wr, r;
27969 int mouse_face_overwritten_p = 0;
27971 /* If window is not yet fully initialized, do nothing. This can
27972 happen when toolkit scroll bars are used and a window is split.
27973 Reconfiguring the scroll bar will generate an expose for a newly
27974 created window. */
27975 if (w->current_matrix == NULL)
27976 return 0;
27978 /* When we're currently updating the window, display and current
27979 matrix usually don't agree. Arrange for a thorough display
27980 later. */
27981 if (w == updated_window)
27983 SET_FRAME_GARBAGED (f);
27984 return 0;
27987 /* Frame-relative pixel rectangle of W. */
27988 wr.x = WINDOW_LEFT_EDGE_X (w);
27989 wr.y = WINDOW_TOP_EDGE_Y (w);
27990 wr.width = WINDOW_TOTAL_WIDTH (w);
27991 wr.height = WINDOW_TOTAL_HEIGHT (w);
27993 if (x_intersect_rectangles (fr, &wr, &r))
27995 int yb = window_text_bottom_y (w);
27996 struct glyph_row *row;
27997 int cursor_cleared_p, phys_cursor_on_p;
27998 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28000 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28001 r.x, r.y, r.width, r.height));
28003 /* Convert to window coordinates. */
28004 r.x -= WINDOW_LEFT_EDGE_X (w);
28005 r.y -= WINDOW_TOP_EDGE_Y (w);
28007 /* Turn off the cursor. */
28008 if (!w->pseudo_window_p
28009 && phys_cursor_in_rect_p (w, &r))
28011 x_clear_cursor (w);
28012 cursor_cleared_p = 1;
28014 else
28015 cursor_cleared_p = 0;
28017 /* If the row containing the cursor extends face to end of line,
28018 then expose_area might overwrite the cursor outside the
28019 rectangle and thus notice_overwritten_cursor might clear
28020 w->phys_cursor_on_p. We remember the original value and
28021 check later if it is changed. */
28022 phys_cursor_on_p = w->phys_cursor_on_p;
28024 /* Update lines intersecting rectangle R. */
28025 first_overlapping_row = last_overlapping_row = NULL;
28026 for (row = w->current_matrix->rows;
28027 row->enabled_p;
28028 ++row)
28030 int y0 = row->y;
28031 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28033 if ((y0 >= r.y && y0 < r.y + r.height)
28034 || (y1 > r.y && y1 < r.y + r.height)
28035 || (r.y >= y0 && r.y < y1)
28036 || (r.y + r.height > y0 && r.y + r.height < y1))
28038 /* A header line may be overlapping, but there is no need
28039 to fix overlapping areas for them. KFS 2005-02-12 */
28040 if (row->overlapping_p && !row->mode_line_p)
28042 if (first_overlapping_row == NULL)
28043 first_overlapping_row = row;
28044 last_overlapping_row = row;
28047 row->clip = fr;
28048 if (expose_line (w, row, &r))
28049 mouse_face_overwritten_p = 1;
28050 row->clip = NULL;
28052 else if (row->overlapping_p)
28054 /* We must redraw a row overlapping the exposed area. */
28055 if (y0 < r.y
28056 ? y0 + row->phys_height > r.y
28057 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28059 if (first_overlapping_row == NULL)
28060 first_overlapping_row = row;
28061 last_overlapping_row = row;
28065 if (y1 >= yb)
28066 break;
28069 /* Display the mode line if there is one. */
28070 if (WINDOW_WANTS_MODELINE_P (w)
28071 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28072 row->enabled_p)
28073 && row->y < r.y + r.height)
28075 if (expose_line (w, row, &r))
28076 mouse_face_overwritten_p = 1;
28079 if (!w->pseudo_window_p)
28081 /* Fix the display of overlapping rows. */
28082 if (first_overlapping_row)
28083 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28084 fr);
28086 /* Draw border between windows. */
28087 x_draw_vertical_border (w);
28089 /* Turn the cursor on again. */
28090 if (cursor_cleared_p
28091 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28092 update_window_cursor (w, 1);
28096 return mouse_face_overwritten_p;
28101 /* Redraw (parts) of all windows in the window tree rooted at W that
28102 intersect R. R contains frame pixel coordinates. Value is
28103 non-zero if the exposure overwrites mouse-face. */
28105 static int
28106 expose_window_tree (struct window *w, XRectangle *r)
28108 struct frame *f = XFRAME (w->frame);
28109 int mouse_face_overwritten_p = 0;
28111 while (w && !FRAME_GARBAGED_P (f))
28113 if (!NILP (w->hchild))
28114 mouse_face_overwritten_p
28115 |= expose_window_tree (XWINDOW (w->hchild), r);
28116 else if (!NILP (w->vchild))
28117 mouse_face_overwritten_p
28118 |= expose_window_tree (XWINDOW (w->vchild), r);
28119 else
28120 mouse_face_overwritten_p |= expose_window (w, r);
28122 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28125 return mouse_face_overwritten_p;
28129 /* EXPORT:
28130 Redisplay an exposed area of frame F. X and Y are the upper-left
28131 corner of the exposed rectangle. W and H are width and height of
28132 the exposed area. All are pixel values. W or H zero means redraw
28133 the entire frame. */
28135 void
28136 expose_frame (struct frame *f, int x, int y, int w, int h)
28138 XRectangle r;
28139 int mouse_face_overwritten_p = 0;
28141 TRACE ((stderr, "expose_frame "));
28143 /* No need to redraw if frame will be redrawn soon. */
28144 if (FRAME_GARBAGED_P (f))
28146 TRACE ((stderr, " garbaged\n"));
28147 return;
28150 /* If basic faces haven't been realized yet, there is no point in
28151 trying to redraw anything. This can happen when we get an expose
28152 event while Emacs is starting, e.g. by moving another window. */
28153 if (FRAME_FACE_CACHE (f) == NULL
28154 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28156 TRACE ((stderr, " no faces\n"));
28157 return;
28160 if (w == 0 || h == 0)
28162 r.x = r.y = 0;
28163 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28164 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28166 else
28168 r.x = x;
28169 r.y = y;
28170 r.width = w;
28171 r.height = h;
28174 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28175 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28177 if (WINDOWP (f->tool_bar_window))
28178 mouse_face_overwritten_p
28179 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28181 #ifdef HAVE_X_WINDOWS
28182 #ifndef MSDOS
28183 #ifndef USE_X_TOOLKIT
28184 if (WINDOWP (f->menu_bar_window))
28185 mouse_face_overwritten_p
28186 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28187 #endif /* not USE_X_TOOLKIT */
28188 #endif
28189 #endif
28191 /* Some window managers support a focus-follows-mouse style with
28192 delayed raising of frames. Imagine a partially obscured frame,
28193 and moving the mouse into partially obscured mouse-face on that
28194 frame. The visible part of the mouse-face will be highlighted,
28195 then the WM raises the obscured frame. With at least one WM, KDE
28196 2.1, Emacs is not getting any event for the raising of the frame
28197 (even tried with SubstructureRedirectMask), only Expose events.
28198 These expose events will draw text normally, i.e. not
28199 highlighted. Which means we must redo the highlight here.
28200 Subsume it under ``we love X''. --gerd 2001-08-15 */
28201 /* Included in Windows version because Windows most likely does not
28202 do the right thing if any third party tool offers
28203 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28204 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28206 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28207 if (f == hlinfo->mouse_face_mouse_frame)
28209 int mouse_x = hlinfo->mouse_face_mouse_x;
28210 int mouse_y = hlinfo->mouse_face_mouse_y;
28211 clear_mouse_face (hlinfo);
28212 note_mouse_highlight (f, mouse_x, mouse_y);
28218 /* EXPORT:
28219 Determine the intersection of two rectangles R1 and R2. Return
28220 the intersection in *RESULT. Value is non-zero if RESULT is not
28221 empty. */
28224 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28226 XRectangle *left, *right;
28227 XRectangle *upper, *lower;
28228 int intersection_p = 0;
28230 /* Rearrange so that R1 is the left-most rectangle. */
28231 if (r1->x < r2->x)
28232 left = r1, right = r2;
28233 else
28234 left = r2, right = r1;
28236 /* X0 of the intersection is right.x0, if this is inside R1,
28237 otherwise there is no intersection. */
28238 if (right->x <= left->x + left->width)
28240 result->x = right->x;
28242 /* The right end of the intersection is the minimum of
28243 the right ends of left and right. */
28244 result->width = (min (left->x + left->width, right->x + right->width)
28245 - result->x);
28247 /* Same game for Y. */
28248 if (r1->y < r2->y)
28249 upper = r1, lower = r2;
28250 else
28251 upper = r2, lower = r1;
28253 /* The upper end of the intersection is lower.y0, if this is inside
28254 of upper. Otherwise, there is no intersection. */
28255 if (lower->y <= upper->y + upper->height)
28257 result->y = lower->y;
28259 /* The lower end of the intersection is the minimum of the lower
28260 ends of upper and lower. */
28261 result->height = (min (lower->y + lower->height,
28262 upper->y + upper->height)
28263 - result->y);
28264 intersection_p = 1;
28268 return intersection_p;
28271 #endif /* HAVE_WINDOW_SYSTEM */
28274 /***********************************************************************
28275 Initialization
28276 ***********************************************************************/
28278 void
28279 syms_of_xdisp (void)
28281 Vwith_echo_area_save_vector = Qnil;
28282 staticpro (&Vwith_echo_area_save_vector);
28284 Vmessage_stack = Qnil;
28285 staticpro (&Vmessage_stack);
28287 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28289 message_dolog_marker1 = Fmake_marker ();
28290 staticpro (&message_dolog_marker1);
28291 message_dolog_marker2 = Fmake_marker ();
28292 staticpro (&message_dolog_marker2);
28293 message_dolog_marker3 = Fmake_marker ();
28294 staticpro (&message_dolog_marker3);
28296 #if GLYPH_DEBUG
28297 defsubr (&Sdump_frame_glyph_matrix);
28298 defsubr (&Sdump_glyph_matrix);
28299 defsubr (&Sdump_glyph_row);
28300 defsubr (&Sdump_tool_bar_row);
28301 defsubr (&Strace_redisplay);
28302 defsubr (&Strace_to_stderr);
28303 #endif
28304 #ifdef HAVE_WINDOW_SYSTEM
28305 defsubr (&Stool_bar_lines_needed);
28306 defsubr (&Slookup_image_map);
28307 #endif
28308 defsubr (&Sformat_mode_line);
28309 defsubr (&Sinvisible_p);
28310 defsubr (&Scurrent_bidi_paragraph_direction);
28312 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28313 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28314 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28315 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28316 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28317 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28318 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28319 DEFSYM (Qeval, "eval");
28320 DEFSYM (QCdata, ":data");
28321 DEFSYM (Qdisplay, "display");
28322 DEFSYM (Qspace_width, "space-width");
28323 DEFSYM (Qraise, "raise");
28324 DEFSYM (Qslice, "slice");
28325 DEFSYM (Qspace, "space");
28326 DEFSYM (Qmargin, "margin");
28327 DEFSYM (Qpointer, "pointer");
28328 DEFSYM (Qleft_margin, "left-margin");
28329 DEFSYM (Qright_margin, "right-margin");
28330 DEFSYM (Qcenter, "center");
28331 DEFSYM (Qline_height, "line-height");
28332 DEFSYM (QCalign_to, ":align-to");
28333 DEFSYM (QCrelative_width, ":relative-width");
28334 DEFSYM (QCrelative_height, ":relative-height");
28335 DEFSYM (QCeval, ":eval");
28336 DEFSYM (QCpropertize, ":propertize");
28337 DEFSYM (QCfile, ":file");
28338 DEFSYM (Qfontified, "fontified");
28339 DEFSYM (Qfontification_functions, "fontification-functions");
28340 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28341 DEFSYM (Qescape_glyph, "escape-glyph");
28342 DEFSYM (Qnobreak_space, "nobreak-space");
28343 DEFSYM (Qimage, "image");
28344 DEFSYM (Qtext, "text");
28345 DEFSYM (Qboth, "both");
28346 DEFSYM (Qboth_horiz, "both-horiz");
28347 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28348 DEFSYM (QCmap, ":map");
28349 DEFSYM (QCpointer, ":pointer");
28350 DEFSYM (Qrect, "rect");
28351 DEFSYM (Qcircle, "circle");
28352 DEFSYM (Qpoly, "poly");
28353 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28354 DEFSYM (Qgrow_only, "grow-only");
28355 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28356 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28357 DEFSYM (Qposition, "position");
28358 DEFSYM (Qbuffer_position, "buffer-position");
28359 DEFSYM (Qobject, "object");
28360 DEFSYM (Qbar, "bar");
28361 DEFSYM (Qhbar, "hbar");
28362 DEFSYM (Qbox, "box");
28363 DEFSYM (Qhollow, "hollow");
28364 DEFSYM (Qhand, "hand");
28365 DEFSYM (Qarrow, "arrow");
28366 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28368 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28369 Fcons (intern_c_string ("void-variable"), Qnil)),
28370 Qnil);
28371 staticpro (&list_of_error);
28373 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28374 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28375 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28376 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28378 echo_buffer[0] = echo_buffer[1] = Qnil;
28379 staticpro (&echo_buffer[0]);
28380 staticpro (&echo_buffer[1]);
28382 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28383 staticpro (&echo_area_buffer[0]);
28384 staticpro (&echo_area_buffer[1]);
28386 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28387 staticpro (&Vmessages_buffer_name);
28389 mode_line_proptrans_alist = Qnil;
28390 staticpro (&mode_line_proptrans_alist);
28391 mode_line_string_list = Qnil;
28392 staticpro (&mode_line_string_list);
28393 mode_line_string_face = Qnil;
28394 staticpro (&mode_line_string_face);
28395 mode_line_string_face_prop = Qnil;
28396 staticpro (&mode_line_string_face_prop);
28397 Vmode_line_unwind_vector = Qnil;
28398 staticpro (&Vmode_line_unwind_vector);
28400 help_echo_string = Qnil;
28401 staticpro (&help_echo_string);
28402 help_echo_object = Qnil;
28403 staticpro (&help_echo_object);
28404 help_echo_window = Qnil;
28405 staticpro (&help_echo_window);
28406 previous_help_echo_string = Qnil;
28407 staticpro (&previous_help_echo_string);
28408 help_echo_pos = -1;
28410 DEFSYM (Qright_to_left, "right-to-left");
28411 DEFSYM (Qleft_to_right, "left-to-right");
28413 #ifdef HAVE_WINDOW_SYSTEM
28414 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28415 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28416 For example, if a block cursor is over a tab, it will be drawn as
28417 wide as that tab on the display. */);
28418 x_stretch_cursor_p = 0;
28419 #endif
28421 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28422 doc: /* *Non-nil means highlight trailing whitespace.
28423 The face used for trailing whitespace is `trailing-whitespace'. */);
28424 Vshow_trailing_whitespace = Qnil;
28426 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28427 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28428 If the value is t, Emacs highlights non-ASCII chars which have the
28429 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28430 or `escape-glyph' face respectively.
28432 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28433 U+2011 (non-breaking hyphen) are affected.
28435 Any other non-nil value means to display these characters as a escape
28436 glyph followed by an ordinary space or hyphen.
28438 A value of nil means no special handling of these characters. */);
28439 Vnobreak_char_display = Qt;
28441 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28442 doc: /* *The pointer shape to show in void text areas.
28443 A value of nil means to show the text pointer. Other options are `arrow',
28444 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28445 Vvoid_text_area_pointer = Qarrow;
28447 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28448 doc: /* Non-nil means don't actually do any redisplay.
28449 This is used for internal purposes. */);
28450 Vinhibit_redisplay = Qnil;
28452 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28453 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28454 Vglobal_mode_string = Qnil;
28456 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28457 doc: /* Marker for where to display an arrow on top of the buffer text.
28458 This must be the beginning of a line in order to work.
28459 See also `overlay-arrow-string'. */);
28460 Voverlay_arrow_position = Qnil;
28462 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28463 doc: /* String to display as an arrow in non-window frames.
28464 See also `overlay-arrow-position'. */);
28465 Voverlay_arrow_string = make_pure_c_string ("=>");
28467 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28468 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28469 The symbols on this list are examined during redisplay to determine
28470 where to display overlay arrows. */);
28471 Voverlay_arrow_variable_list
28472 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28474 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28475 doc: /* *The number of lines to try scrolling a window by when point moves out.
28476 If that fails to bring point back on frame, point is centered instead.
28477 If this is zero, point is always centered after it moves off frame.
28478 If you want scrolling to always be a line at a time, you should set
28479 `scroll-conservatively' to a large value rather than set this to 1. */);
28481 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28482 doc: /* *Scroll up to this many lines, to bring point back on screen.
28483 If point moves off-screen, redisplay will scroll by up to
28484 `scroll-conservatively' lines in order to bring point just barely
28485 onto the screen again. If that cannot be done, then redisplay
28486 recenters point as usual.
28488 If the value is greater than 100, redisplay will never recenter point,
28489 but will always scroll just enough text to bring point into view, even
28490 if you move far away.
28492 A value of zero means always recenter point if it moves off screen. */);
28493 scroll_conservatively = 0;
28495 DEFVAR_INT ("scroll-margin", scroll_margin,
28496 doc: /* *Number of lines of margin at the top and bottom of a window.
28497 Recenter the window whenever point gets within this many lines
28498 of the top or bottom of the window. */);
28499 scroll_margin = 0;
28501 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28502 doc: /* Pixels per inch value for non-window system displays.
28503 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28504 Vdisplay_pixels_per_inch = make_float (72.0);
28506 #if GLYPH_DEBUG
28507 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28508 #endif
28510 DEFVAR_LISP ("truncate-partial-width-windows",
28511 Vtruncate_partial_width_windows,
28512 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28513 For an integer value, truncate lines in each window narrower than the
28514 full frame width, provided the window width is less than that integer;
28515 otherwise, respect the value of `truncate-lines'.
28517 For any other non-nil value, truncate lines in all windows that do
28518 not span the full frame width.
28520 A value of nil means to respect the value of `truncate-lines'.
28522 If `word-wrap' is enabled, you might want to reduce this. */);
28523 Vtruncate_partial_width_windows = make_number (50);
28525 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28526 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28527 Any other value means to use the appropriate face, `mode-line',
28528 `header-line', or `menu' respectively. */);
28529 mode_line_inverse_video = 1;
28531 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28532 doc: /* *Maximum buffer size for which line number should be displayed.
28533 If the buffer is bigger than this, the line number does not appear
28534 in the mode line. A value of nil means no limit. */);
28535 Vline_number_display_limit = Qnil;
28537 DEFVAR_INT ("line-number-display-limit-width",
28538 line_number_display_limit_width,
28539 doc: /* *Maximum line width (in characters) for line number display.
28540 If the average length of the lines near point is bigger than this, then the
28541 line number may be omitted from the mode line. */);
28542 line_number_display_limit_width = 200;
28544 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28545 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28546 highlight_nonselected_windows = 0;
28548 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28549 doc: /* Non-nil if more than one frame is visible on this display.
28550 Minibuffer-only frames don't count, but iconified frames do.
28551 This variable is not guaranteed to be accurate except while processing
28552 `frame-title-format' and `icon-title-format'. */);
28554 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28555 doc: /* Template for displaying the title bar of visible frames.
28556 \(Assuming the window manager supports this feature.)
28558 This variable has the same structure as `mode-line-format', except that
28559 the %c and %l constructs are ignored. It is used only on frames for
28560 which no explicit name has been set \(see `modify-frame-parameters'). */);
28562 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28563 doc: /* Template for displaying the title bar of an iconified frame.
28564 \(Assuming the window manager supports this feature.)
28565 This variable has the same structure as `mode-line-format' (which see),
28566 and is used only on frames for which no explicit name has been set
28567 \(see `modify-frame-parameters'). */);
28568 Vicon_title_format
28569 = Vframe_title_format
28570 = pure_cons (intern_c_string ("multiple-frames"),
28571 pure_cons (make_pure_c_string ("%b"),
28572 pure_cons (pure_cons (empty_unibyte_string,
28573 pure_cons (intern_c_string ("invocation-name"),
28574 pure_cons (make_pure_c_string ("@"),
28575 pure_cons (intern_c_string ("system-name"),
28576 Qnil)))),
28577 Qnil)));
28579 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28580 doc: /* Maximum number of lines to keep in the message log buffer.
28581 If nil, disable message logging. If t, log messages but don't truncate
28582 the buffer when it becomes large. */);
28583 Vmessage_log_max = make_number (100);
28585 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28586 doc: /* Functions called before redisplay, if window sizes have changed.
28587 The value should be a list of functions that take one argument.
28588 Just before redisplay, for each frame, if any of its windows have changed
28589 size since the last redisplay, or have been split or deleted,
28590 all the functions in the list are called, with the frame as argument. */);
28591 Vwindow_size_change_functions = Qnil;
28593 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28594 doc: /* List of functions to call before redisplaying a window with scrolling.
28595 Each function is called with two arguments, the window and its new
28596 display-start position. Note that these functions are also called by
28597 `set-window-buffer'. Also note that the value of `window-end' is not
28598 valid when these functions are called.
28600 Warning: Do not use this feature to alter the way the window
28601 is scrolled. It is not designed for that, and such use probably won't
28602 work. */);
28603 Vwindow_scroll_functions = Qnil;
28605 DEFVAR_LISP ("window-text-change-functions",
28606 Vwindow_text_change_functions,
28607 doc: /* Functions to call in redisplay when text in the window might change. */);
28608 Vwindow_text_change_functions = Qnil;
28610 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28611 doc: /* Functions called when redisplay of a window reaches the end trigger.
28612 Each function is called with two arguments, the window and the end trigger value.
28613 See `set-window-redisplay-end-trigger'. */);
28614 Vredisplay_end_trigger_functions = Qnil;
28616 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28617 doc: /* *Non-nil means autoselect window with mouse pointer.
28618 If nil, do not autoselect windows.
28619 A positive number means delay autoselection by that many seconds: a
28620 window is autoselected only after the mouse has remained in that
28621 window for the duration of the delay.
28622 A negative number has a similar effect, but causes windows to be
28623 autoselected only after the mouse has stopped moving. \(Because of
28624 the way Emacs compares mouse events, you will occasionally wait twice
28625 that time before the window gets selected.\)
28626 Any other value means to autoselect window instantaneously when the
28627 mouse pointer enters it.
28629 Autoselection selects the minibuffer only if it is active, and never
28630 unselects the minibuffer if it is active.
28632 When customizing this variable make sure that the actual value of
28633 `focus-follows-mouse' matches the behavior of your window manager. */);
28634 Vmouse_autoselect_window = Qnil;
28636 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28637 doc: /* *Non-nil means automatically resize tool-bars.
28638 This dynamically changes the tool-bar's height to the minimum height
28639 that is needed to make all tool-bar items visible.
28640 If value is `grow-only', the tool-bar's height is only increased
28641 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28642 Vauto_resize_tool_bars = Qt;
28644 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28645 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28646 auto_raise_tool_bar_buttons_p = 1;
28648 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28649 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28650 make_cursor_line_fully_visible_p = 1;
28652 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28653 doc: /* *Border below tool-bar in pixels.
28654 If an integer, use it as the height of the border.
28655 If it is one of `internal-border-width' or `border-width', use the
28656 value of the corresponding frame parameter.
28657 Otherwise, no border is added below the tool-bar. */);
28658 Vtool_bar_border = Qinternal_border_width;
28660 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28661 doc: /* *Margin around tool-bar buttons in pixels.
28662 If an integer, use that for both horizontal and vertical margins.
28663 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28664 HORZ specifying the horizontal margin, and VERT specifying the
28665 vertical margin. */);
28666 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28668 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28669 doc: /* *Relief thickness of tool-bar buttons. */);
28670 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28672 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28673 doc: /* Tool bar style to use.
28674 It can be one of
28675 image - show images only
28676 text - show text only
28677 both - show both, text below image
28678 both-horiz - show text to the right of the image
28679 text-image-horiz - show text to the left of the image
28680 any other - use system default or image if no system default. */);
28681 Vtool_bar_style = Qnil;
28683 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28684 doc: /* *Maximum number of characters a label can have to be shown.
28685 The tool bar style must also show labels for this to have any effect, see
28686 `tool-bar-style'. */);
28687 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28689 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28690 doc: /* List of functions to call to fontify regions of text.
28691 Each function is called with one argument POS. Functions must
28692 fontify a region starting at POS in the current buffer, and give
28693 fontified regions the property `fontified'. */);
28694 Vfontification_functions = Qnil;
28695 Fmake_variable_buffer_local (Qfontification_functions);
28697 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28698 unibyte_display_via_language_environment,
28699 doc: /* *Non-nil means display unibyte text according to language environment.
28700 Specifically, this means that raw bytes in the range 160-255 decimal
28701 are displayed by converting them to the equivalent multibyte characters
28702 according to the current language environment. As a result, they are
28703 displayed according to the current fontset.
28705 Note that this variable affects only how these bytes are displayed,
28706 but does not change the fact they are interpreted as raw bytes. */);
28707 unibyte_display_via_language_environment = 0;
28709 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28710 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28711 If a float, it specifies a fraction of the mini-window frame's height.
28712 If an integer, it specifies a number of lines. */);
28713 Vmax_mini_window_height = make_float (0.25);
28715 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28716 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28717 A value of nil means don't automatically resize mini-windows.
28718 A value of t means resize them to fit the text displayed in them.
28719 A value of `grow-only', the default, means let mini-windows grow only;
28720 they return to their normal size when the minibuffer is closed, or the
28721 echo area becomes empty. */);
28722 Vresize_mini_windows = Qgrow_only;
28724 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28725 doc: /* Alist specifying how to blink the cursor off.
28726 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28727 `cursor-type' frame-parameter or variable equals ON-STATE,
28728 comparing using `equal', Emacs uses OFF-STATE to specify
28729 how to blink it off. ON-STATE and OFF-STATE are values for
28730 the `cursor-type' frame parameter.
28732 If a frame's ON-STATE has no entry in this list,
28733 the frame's other specifications determine how to blink the cursor off. */);
28734 Vblink_cursor_alist = Qnil;
28736 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28737 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28738 If non-nil, windows are automatically scrolled horizontally to make
28739 point visible. */);
28740 automatic_hscrolling_p = 1;
28741 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28743 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28744 doc: /* *How many columns away from the window edge point is allowed to get
28745 before automatic hscrolling will horizontally scroll the window. */);
28746 hscroll_margin = 5;
28748 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28749 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28750 When point is less than `hscroll-margin' columns from the window
28751 edge, automatic hscrolling will scroll the window by the amount of columns
28752 determined by this variable. If its value is a positive integer, scroll that
28753 many columns. If it's a positive floating-point number, it specifies the
28754 fraction of the window's width to scroll. If it's nil or zero, point will be
28755 centered horizontally after the scroll. Any other value, including negative
28756 numbers, are treated as if the value were zero.
28758 Automatic hscrolling always moves point outside the scroll margin, so if
28759 point was more than scroll step columns inside the margin, the window will
28760 scroll more than the value given by the scroll step.
28762 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28763 and `scroll-right' overrides this variable's effect. */);
28764 Vhscroll_step = make_number (0);
28766 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28767 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28768 Bind this around calls to `message' to let it take effect. */);
28769 message_truncate_lines = 0;
28771 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28772 doc: /* Normal hook run to update the menu bar definitions.
28773 Redisplay runs this hook before it redisplays the menu bar.
28774 This is used to update submenus such as Buffers,
28775 whose contents depend on various data. */);
28776 Vmenu_bar_update_hook = Qnil;
28778 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28779 doc: /* Frame for which we are updating a menu.
28780 The enable predicate for a menu binding should check this variable. */);
28781 Vmenu_updating_frame = Qnil;
28783 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28784 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28785 inhibit_menubar_update = 0;
28787 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28788 doc: /* Prefix prepended to all continuation lines at display time.
28789 The value may be a string, an image, or a stretch-glyph; it is
28790 interpreted in the same way as the value of a `display' text property.
28792 This variable is overridden by any `wrap-prefix' text or overlay
28793 property.
28795 To add a prefix to non-continuation lines, use `line-prefix'. */);
28796 Vwrap_prefix = Qnil;
28797 DEFSYM (Qwrap_prefix, "wrap-prefix");
28798 Fmake_variable_buffer_local (Qwrap_prefix);
28800 DEFVAR_LISP ("line-prefix", Vline_prefix,
28801 doc: /* Prefix prepended to all non-continuation lines at display time.
28802 The value may be a string, an image, or a stretch-glyph; it is
28803 interpreted in the same way as the value of a `display' text property.
28805 This variable is overridden by any `line-prefix' text or overlay
28806 property.
28808 To add a prefix to continuation lines, use `wrap-prefix'. */);
28809 Vline_prefix = Qnil;
28810 DEFSYM (Qline_prefix, "line-prefix");
28811 Fmake_variable_buffer_local (Qline_prefix);
28813 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28814 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28815 inhibit_eval_during_redisplay = 0;
28817 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28818 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28819 inhibit_free_realized_faces = 0;
28821 #if GLYPH_DEBUG
28822 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28823 doc: /* Inhibit try_window_id display optimization. */);
28824 inhibit_try_window_id = 0;
28826 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28827 doc: /* Inhibit try_window_reusing display optimization. */);
28828 inhibit_try_window_reusing = 0;
28830 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28831 doc: /* Inhibit try_cursor_movement display optimization. */);
28832 inhibit_try_cursor_movement = 0;
28833 #endif /* GLYPH_DEBUG */
28835 DEFVAR_INT ("overline-margin", overline_margin,
28836 doc: /* *Space between overline and text, in pixels.
28837 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28838 margin to the character height. */);
28839 overline_margin = 2;
28841 DEFVAR_INT ("underline-minimum-offset",
28842 underline_minimum_offset,
28843 doc: /* Minimum distance between baseline and underline.
28844 This can improve legibility of underlined text at small font sizes,
28845 particularly when using variable `x-use-underline-position-properties'
28846 with fonts that specify an UNDERLINE_POSITION relatively close to the
28847 baseline. The default value is 1. */);
28848 underline_minimum_offset = 1;
28850 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28851 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28852 This feature only works when on a window system that can change
28853 cursor shapes. */);
28854 display_hourglass_p = 1;
28856 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28857 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28858 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28860 hourglass_atimer = NULL;
28861 hourglass_shown_p = 0;
28863 DEFSYM (Qglyphless_char, "glyphless-char");
28864 DEFSYM (Qhex_code, "hex-code");
28865 DEFSYM (Qempty_box, "empty-box");
28866 DEFSYM (Qthin_space, "thin-space");
28867 DEFSYM (Qzero_width, "zero-width");
28869 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28870 /* Intern this now in case it isn't already done.
28871 Setting this variable twice is harmless.
28872 But don't staticpro it here--that is done in alloc.c. */
28873 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28874 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28876 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28877 doc: /* Char-table defining glyphless characters.
28878 Each element, if non-nil, should be one of the following:
28879 an ASCII acronym string: display this string in a box
28880 `hex-code': display the hexadecimal code of a character in a box
28881 `empty-box': display as an empty box
28882 `thin-space': display as 1-pixel width space
28883 `zero-width': don't display
28884 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28885 display method for graphical terminals and text terminals respectively.
28886 GRAPHICAL and TEXT should each have one of the values listed above.
28888 The char-table has one extra slot to control the display of a character for
28889 which no font is found. This slot only takes effect on graphical terminals.
28890 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28891 `thin-space'. The default is `empty-box'. */);
28892 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28893 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28894 Qempty_box);
28898 /* Initialize this module when Emacs starts. */
28900 void
28901 init_xdisp (void)
28903 current_header_line_height = current_mode_line_height = -1;
28905 CHARPOS (this_line_start_pos) = 0;
28907 if (!noninteractive)
28909 struct window *m = XWINDOW (minibuf_window);
28910 Lisp_Object frame = m->frame;
28911 struct frame *f = XFRAME (frame);
28912 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28913 struct window *r = XWINDOW (root);
28914 int i;
28916 echo_area_window = minibuf_window;
28918 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28919 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28920 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28921 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28922 XSETFASTINT (m->total_lines, 1);
28923 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28925 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28926 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28927 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28929 /* The default ellipsis glyphs `...'. */
28930 for (i = 0; i < 3; ++i)
28931 default_invis_vector[i] = make_number ('.');
28935 /* Allocate the buffer for frame titles.
28936 Also used for `format-mode-line'. */
28937 int size = 100;
28938 mode_line_noprop_buf = (char *) xmalloc (size);
28939 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28940 mode_line_noprop_ptr = mode_line_noprop_buf;
28941 mode_line_target = MODE_LINE_DISPLAY;
28944 help_echo_showing_p = 0;
28947 /* Since w32 does not support atimers, it defines its own implementation of
28948 the following three functions in w32fns.c. */
28949 #ifndef WINDOWSNT
28951 /* Platform-independent portion of hourglass implementation. */
28953 /* Return non-zero if hourglass timer has been started or hourglass is
28954 shown. */
28956 hourglass_started (void)
28958 return hourglass_shown_p || hourglass_atimer != NULL;
28961 /* Cancel a currently active hourglass timer, and start a new one. */
28962 void
28963 start_hourglass (void)
28965 #if defined (HAVE_WINDOW_SYSTEM)
28966 EMACS_TIME delay;
28967 int secs, usecs = 0;
28969 cancel_hourglass ();
28971 if (INTEGERP (Vhourglass_delay)
28972 && XINT (Vhourglass_delay) > 0)
28973 secs = XFASTINT (Vhourglass_delay);
28974 else if (FLOATP (Vhourglass_delay)
28975 && XFLOAT_DATA (Vhourglass_delay) > 0)
28977 Lisp_Object tem;
28978 tem = Ftruncate (Vhourglass_delay, Qnil);
28979 secs = XFASTINT (tem);
28980 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28982 else
28983 secs = DEFAULT_HOURGLASS_DELAY;
28985 EMACS_SET_SECS_USECS (delay, secs, usecs);
28986 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28987 show_hourglass, NULL);
28988 #endif
28992 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28993 shown. */
28994 void
28995 cancel_hourglass (void)
28997 #if defined (HAVE_WINDOW_SYSTEM)
28998 if (hourglass_atimer)
29000 cancel_atimer (hourglass_atimer);
29001 hourglass_atimer = NULL;
29004 if (hourglass_shown_p)
29005 hide_hourglass ();
29006 #endif
29008 #endif /* ! WINDOWSNT */