Fix bug #9284 with line/wrap-prefix property on display strings.
[emacs.git] / src / xdisp.c
bloba3bdcbdf4c684bae4b1c7ab4c29685faa3975090
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 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 pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, EMACS_INT, EMACS_INT,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
866 EMACS_INT *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, EMACS_INT);
872 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, EMACS_INT);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, EMACS_INT, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, EMACS_INT);
905 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
906 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
907 static EMACS_INT number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static EMACS_INT next_overlay_change (EMACS_INT);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, EMACS_INT, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, EMACS_INT, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
925 #ifdef HAVE_WINDOW_SYSTEM
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
940 #endif /* HAVE_WINDOW_SYSTEM */
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
955 This is the height of W minus the height of a mode line, if any. */
957 inline int
958 window_text_bottom_y (struct window *w)
960 int height = WINDOW_TOTAL_HEIGHT (w);
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
971 inline int
972 window_box_width (struct window *w, int area)
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
977 if (!w->pseudo_window_p)
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
981 if (area == TEXT_AREA)
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
989 else if (area == LEFT_MARGIN_AREA)
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
995 else if (area == RIGHT_MARGIN_AREA)
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1010 inline int
1011 window_box_height (struct window *w)
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1016 xassert (height >= 0);
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1024 if (WINDOW_WANTS_MODELINE_P (w))
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1057 inline int
1058 window_box_left_offset (struct window *w, int area)
1060 int x;
1062 if (w->pseudo_window_p)
1063 return 0;
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1081 return x;
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1089 inline int
1090 window_box_right_offset (struct window *w, int area)
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1099 inline int
1100 window_box_left (struct window *w, int area)
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1111 return x;
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1119 inline int
1120 window_box_right (struct window *w, int area)
1122 return window_box_left (w, area) + window_box_width (w, area);
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1132 inline void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1179 line_bottom_y (struct it *it)
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1184 if (line_height == 0)
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1195 else
1197 struct glyph_row *row = it->glyph_row;
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1210 return line_top_y + line_height;
1214 /* Return 1 if position CHARPOS is visible in window W.
1215 CHARPOS < 0 means return info about WINDOW_END position.
1216 If visible, set *X and *Y to pixel coordinates of top left corner.
1217 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1218 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1221 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1222 int *rtop, int *rbot, int *rowh, int *vpos)
1224 struct it it;
1225 void *itdata = bidi_shelve_cache ();
1226 struct text_pos top;
1227 int visible_p = 0;
1228 struct buffer *old_buffer = NULL;
1230 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1231 return visible_p;
1233 if (XBUFFER (w->buffer) != current_buffer)
1235 old_buffer = current_buffer;
1236 set_buffer_internal_1 (XBUFFER (w->buffer));
1239 SET_TEXT_POS_FROM_MARKER (top, w->start);
1241 /* Compute exact mode line heights. */
1242 if (WINDOW_WANTS_MODELINE_P (w))
1243 current_mode_line_height
1244 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1245 BVAR (current_buffer, mode_line_format));
1247 if (WINDOW_WANTS_HEADER_LINE_P (w))
1248 current_header_line_height
1249 = display_mode_line (w, HEADER_LINE_FACE_ID,
1250 BVAR (current_buffer, header_line_format));
1252 start_display (&it, w, top);
1253 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1254 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1256 if (charpos >= 0
1257 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1258 && IT_CHARPOS (it) >= charpos)
1259 /* When scanning backwards under bidi iteration, move_it_to
1260 stops at or _before_ CHARPOS, because it stops at or to
1261 the _right_ of the character at CHARPOS. */
1262 || (it.bidi_p && it.bidi_it.scan_dir == -1
1263 && IT_CHARPOS (it) <= charpos)))
1265 /* We have reached CHARPOS, or passed it. How the call to
1266 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1267 or covered by a display property, move_it_to stops at the end
1268 of the invisible text, to the right of CHARPOS. (ii) If
1269 CHARPOS is in a display vector, move_it_to stops on its last
1270 glyph. */
1271 int top_x = it.current_x;
1272 int top_y = it.current_y;
1273 enum it_method it_method = it.method;
1274 /* Calling line_bottom_y may change it.method, it.position, etc. */
1275 int bottom_y = (last_height = 0, line_bottom_y (&it));
1276 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1278 if (top_y < window_top_y)
1279 visible_p = bottom_y > window_top_y;
1280 else if (top_y < it.last_visible_y)
1281 visible_p = 1;
1282 if (visible_p)
1284 if (it_method == GET_FROM_DISPLAY_VECTOR)
1286 /* We stopped on the last glyph of a display vector.
1287 Try and recompute. Hack alert! */
1288 if (charpos < 2 || top.charpos >= charpos)
1289 top_x = it.glyph_row->x;
1290 else
1292 struct it it2;
1293 start_display (&it2, w, top);
1294 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1295 get_next_display_element (&it2);
1296 PRODUCE_GLYPHS (&it2);
1297 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1298 || it2.current_x > it2.last_visible_x)
1299 top_x = it.glyph_row->x;
1300 else
1302 top_x = it2.current_x;
1303 top_y = it2.current_y;
1308 *x = top_x;
1309 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1310 *rtop = max (0, window_top_y - top_y);
1311 *rbot = max (0, bottom_y - it.last_visible_y);
1312 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1313 - max (top_y, window_top_y)));
1314 *vpos = it.vpos;
1317 else
1319 /* We were asked to provide info about WINDOW_END. */
1320 struct it it2;
1321 void *it2data = NULL;
1323 SAVE_IT (it2, it, it2data);
1324 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1325 move_it_by_lines (&it, 1);
1326 if (charpos < IT_CHARPOS (it)
1327 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1329 visible_p = 1;
1330 RESTORE_IT (&it2, &it2, it2data);
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1337 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1338 it.last_visible_y)
1339 - max (it2.current_y,
1340 WINDOW_HEADER_LINE_HEIGHT (w))));
1341 *vpos = it2.vpos;
1343 else
1344 bidi_unshelve_cache (it2data, 1);
1346 bidi_unshelve_cache (itdata, 0);
1348 if (old_buffer)
1349 set_buffer_internal_1 (old_buffer);
1351 current_header_line_height = current_mode_line_height = -1;
1353 if (visible_p && XFASTINT (w->hscroll) > 0)
1354 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1356 #if 0
1357 /* Debugging code. */
1358 if (visible_p)
1359 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1360 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1361 else
1362 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1363 #endif
1365 return visible_p;
1369 /* Return the next character from STR. Return in *LEN the length of
1370 the character. This is like STRING_CHAR_AND_LENGTH but never
1371 returns an invalid character. If we find one, we return a `?', but
1372 with the length of the invalid character. */
1374 static inline int
1375 string_char_and_length (const unsigned char *str, int *len)
1377 int c;
1379 c = STRING_CHAR_AND_LENGTH (str, *len);
1380 if (!CHAR_VALID_P (c))
1381 /* We may not change the length here because other places in Emacs
1382 don't use this function, i.e. they silently accept invalid
1383 characters. */
1384 c = '?';
1386 return c;
1391 /* Given a position POS containing a valid character and byte position
1392 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1394 static struct text_pos
1395 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1397 xassert (STRINGP (string) && nchars >= 0);
1399 if (STRING_MULTIBYTE (string))
1401 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1402 int len;
1404 while (nchars--)
1406 string_char_and_length (p, &len);
1407 p += len;
1408 CHARPOS (pos) += 1;
1409 BYTEPOS (pos) += len;
1412 else
1413 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1415 return pos;
1419 /* Value is the text position, i.e. character and byte position,
1420 for character position CHARPOS in STRING. */
1422 static inline struct text_pos
1423 string_pos (EMACS_INT charpos, Lisp_Object string)
1425 struct text_pos pos;
1426 xassert (STRINGP (string));
1427 xassert (charpos >= 0);
1428 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1429 return pos;
1433 /* Value is a text position, i.e. character and byte position, for
1434 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1435 means recognize multibyte characters. */
1437 static struct text_pos
1438 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1440 struct text_pos pos;
1442 xassert (s != NULL);
1443 xassert (charpos >= 0);
1445 if (multibyte_p)
1447 int len;
1449 SET_TEXT_POS (pos, 0, 0);
1450 while (charpos--)
1452 string_char_and_length ((const unsigned char *) s, &len);
1453 s += len;
1454 CHARPOS (pos) += 1;
1455 BYTEPOS (pos) += len;
1458 else
1459 SET_TEXT_POS (pos, charpos, charpos);
1461 return pos;
1465 /* Value is the number of characters in C string S. MULTIBYTE_P
1466 non-zero means recognize multibyte characters. */
1468 static EMACS_INT
1469 number_of_chars (const char *s, int multibyte_p)
1471 EMACS_INT nchars;
1473 if (multibyte_p)
1475 EMACS_INT rest = strlen (s);
1476 int len;
1477 const unsigned char *p = (const unsigned char *) s;
1479 for (nchars = 0; rest > 0; ++nchars)
1481 string_char_and_length (p, &len);
1482 rest -= len, p += len;
1485 else
1486 nchars = strlen (s);
1488 return nchars;
1492 /* Compute byte position NEWPOS->bytepos corresponding to
1493 NEWPOS->charpos. POS is a known position in string STRING.
1494 NEWPOS->charpos must be >= POS.charpos. */
1496 static void
1497 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1499 xassert (STRINGP (string));
1500 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1502 if (STRING_MULTIBYTE (string))
1503 *newpos = string_pos_nchars_ahead (pos, string,
1504 CHARPOS (*newpos) - CHARPOS (pos));
1505 else
1506 BYTEPOS (*newpos) = CHARPOS (*newpos);
1509 /* EXPORT:
1510 Return an estimation of the pixel height of mode or header lines on
1511 frame F. FACE_ID specifies what line's height to estimate. */
1514 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 if (FRAME_WINDOW_P (f))
1519 int height = FONT_HEIGHT (FRAME_FONT (f));
1521 /* This function is called so early when Emacs starts that the face
1522 cache and mode line face are not yet initialized. */
1523 if (FRAME_FACE_CACHE (f))
1525 struct face *face = FACE_FROM_ID (f, face_id);
1526 if (face)
1528 if (face->font)
1529 height = FONT_HEIGHT (face->font);
1530 if (face->box_line_width > 0)
1531 height += 2 * face->box_line_width;
1535 return height;
1537 #endif
1539 return 1;
1542 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1543 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1544 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1545 not force the value into range. */
1547 void
1548 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1549 int *x, int *y, NativeRectangle *bounds, int noclip)
1552 #ifdef HAVE_WINDOW_SYSTEM
1553 if (FRAME_WINDOW_P (f))
1555 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1556 even for negative values. */
1557 if (pix_x < 0)
1558 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1559 if (pix_y < 0)
1560 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1562 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1563 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1565 if (bounds)
1566 STORE_NATIVE_RECT (*bounds,
1567 FRAME_COL_TO_PIXEL_X (f, pix_x),
1568 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1569 FRAME_COLUMN_WIDTH (f) - 1,
1570 FRAME_LINE_HEIGHT (f) - 1);
1572 if (!noclip)
1574 if (pix_x < 0)
1575 pix_x = 0;
1576 else if (pix_x > FRAME_TOTAL_COLS (f))
1577 pix_x = FRAME_TOTAL_COLS (f);
1579 if (pix_y < 0)
1580 pix_y = 0;
1581 else if (pix_y > FRAME_LINES (f))
1582 pix_y = FRAME_LINES (f);
1585 #endif
1587 *x = pix_x;
1588 *y = pix_y;
1592 /* Find the glyph under window-relative coordinates X/Y in window W.
1593 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1594 strings. Return in *HPOS and *VPOS the row and column number of
1595 the glyph found. Return in *AREA the glyph area containing X.
1596 Value is a pointer to the glyph found or null if X/Y is not on
1597 text, or we can't tell because W's current matrix is not up to
1598 date. */
1600 static
1601 struct glyph *
1602 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1603 int *dx, int *dy, int *area)
1605 struct glyph *glyph, *end;
1606 struct glyph_row *row = NULL;
1607 int x0, i;
1609 /* Find row containing Y. Give up if some row is not enabled. */
1610 for (i = 0; i < w->current_matrix->nrows; ++i)
1612 row = MATRIX_ROW (w->current_matrix, i);
1613 if (!row->enabled_p)
1614 return NULL;
1615 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1616 break;
1619 *vpos = i;
1620 *hpos = 0;
1622 /* Give up if Y is not in the window. */
1623 if (i == w->current_matrix->nrows)
1624 return NULL;
1626 /* Get the glyph area containing X. */
1627 if (w->pseudo_window_p)
1629 *area = TEXT_AREA;
1630 x0 = 0;
1632 else
1634 if (x < window_box_left_offset (w, TEXT_AREA))
1636 *area = LEFT_MARGIN_AREA;
1637 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1639 else if (x < window_box_right_offset (w, TEXT_AREA))
1641 *area = TEXT_AREA;
1642 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1644 else
1646 *area = RIGHT_MARGIN_AREA;
1647 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1651 /* Find glyph containing X. */
1652 glyph = row->glyphs[*area];
1653 end = glyph + row->used[*area];
1654 x -= x0;
1655 while (glyph < end && x >= glyph->pixel_width)
1657 x -= glyph->pixel_width;
1658 ++glyph;
1661 if (glyph == end)
1662 return NULL;
1664 if (dx)
1666 *dx = x;
1667 *dy = y - (row->y + row->ascent - glyph->ascent);
1670 *hpos = glyph - row->glyphs[*area];
1671 return glyph;
1674 /* Convert frame-relative x/y to coordinates relative to window W.
1675 Takes pseudo-windows into account. */
1677 static void
1678 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1680 if (w->pseudo_window_p)
1682 /* A pseudo-window is always full-width, and starts at the
1683 left edge of the frame, plus a frame border. */
1684 struct frame *f = XFRAME (w->frame);
1685 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1686 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1688 else
1690 *x -= WINDOW_LEFT_EDGE_X (w);
1691 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1695 #ifdef HAVE_WINDOW_SYSTEM
1697 /* EXPORT:
1698 Return in RECTS[] at most N clipping rectangles for glyph string S.
1699 Return the number of stored rectangles. */
1702 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1704 XRectangle r;
1706 if (n <= 0)
1707 return 0;
1709 if (s->row->full_width_p)
1711 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1712 r.x = WINDOW_LEFT_EDGE_X (s->w);
1713 r.width = WINDOW_TOTAL_WIDTH (s->w);
1715 /* Unless displaying a mode or menu bar line, which are always
1716 fully visible, clip to the visible part of the row. */
1717 if (s->w->pseudo_window_p)
1718 r.height = s->row->visible_height;
1719 else
1720 r.height = s->height;
1722 else
1724 /* This is a text line that may be partially visible. */
1725 r.x = window_box_left (s->w, s->area);
1726 r.width = window_box_width (s->w, s->area);
1727 r.height = s->row->visible_height;
1730 if (s->clip_head)
1731 if (r.x < s->clip_head->x)
1733 if (r.width >= s->clip_head->x - r.x)
1734 r.width -= s->clip_head->x - r.x;
1735 else
1736 r.width = 0;
1737 r.x = s->clip_head->x;
1739 if (s->clip_tail)
1740 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1742 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1743 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1744 else
1745 r.width = 0;
1748 /* If S draws overlapping rows, it's sufficient to use the top and
1749 bottom of the window for clipping because this glyph string
1750 intentionally draws over other lines. */
1751 if (s->for_overlaps)
1753 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1754 r.height = window_text_bottom_y (s->w) - r.y;
1756 /* Alas, the above simple strategy does not work for the
1757 environments with anti-aliased text: if the same text is
1758 drawn onto the same place multiple times, it gets thicker.
1759 If the overlap we are processing is for the erased cursor, we
1760 take the intersection with the rectagle of the cursor. */
1761 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1763 XRectangle rc, r_save = r;
1765 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1766 rc.y = s->w->phys_cursor.y;
1767 rc.width = s->w->phys_cursor_width;
1768 rc.height = s->w->phys_cursor_height;
1770 x_intersect_rectangles (&r_save, &rc, &r);
1773 else
1775 /* Don't use S->y for clipping because it doesn't take partially
1776 visible lines into account. For example, it can be negative for
1777 partially visible lines at the top of a window. */
1778 if (!s->row->full_width_p
1779 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1780 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1781 else
1782 r.y = max (0, s->row->y);
1785 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1787 /* If drawing the cursor, don't let glyph draw outside its
1788 advertised boundaries. Cleartype does this under some circumstances. */
1789 if (s->hl == DRAW_CURSOR)
1791 struct glyph *glyph = s->first_glyph;
1792 int height, max_y;
1794 if (s->x > r.x)
1796 r.width -= s->x - r.x;
1797 r.x = s->x;
1799 r.width = min (r.width, glyph->pixel_width);
1801 /* If r.y is below window bottom, ensure that we still see a cursor. */
1802 height = min (glyph->ascent + glyph->descent,
1803 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1804 max_y = window_text_bottom_y (s->w) - height;
1805 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1806 if (s->ybase - glyph->ascent > max_y)
1808 r.y = max_y;
1809 r.height = height;
1811 else
1813 /* Don't draw cursor glyph taller than our actual glyph. */
1814 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1815 if (height < r.height)
1817 max_y = r.y + r.height;
1818 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1819 r.height = min (max_y - r.y, height);
1824 if (s->row->clip)
1826 XRectangle r_save = r;
1828 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1829 r.width = 0;
1832 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1833 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1835 #ifdef CONVERT_FROM_XRECT
1836 CONVERT_FROM_XRECT (r, *rects);
1837 #else
1838 *rects = r;
1839 #endif
1840 return 1;
1842 else
1844 /* If we are processing overlapping and allowed to return
1845 multiple clipping rectangles, we exclude the row of the glyph
1846 string from the clipping rectangle. This is to avoid drawing
1847 the same text on the environment with anti-aliasing. */
1848 #ifdef CONVERT_FROM_XRECT
1849 XRectangle rs[2];
1850 #else
1851 XRectangle *rs = rects;
1852 #endif
1853 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1855 if (s->for_overlaps & OVERLAPS_PRED)
1857 rs[i] = r;
1858 if (r.y + r.height > row_y)
1860 if (r.y < row_y)
1861 rs[i].height = row_y - r.y;
1862 else
1863 rs[i].height = 0;
1865 i++;
1867 if (s->for_overlaps & OVERLAPS_SUCC)
1869 rs[i] = r;
1870 if (r.y < row_y + s->row->visible_height)
1872 if (r.y + r.height > row_y + s->row->visible_height)
1874 rs[i].y = row_y + s->row->visible_height;
1875 rs[i].height = r.y + r.height - rs[i].y;
1877 else
1878 rs[i].height = 0;
1880 i++;
1883 n = i;
1884 #ifdef CONVERT_FROM_XRECT
1885 for (i = 0; i < n; i++)
1886 CONVERT_FROM_XRECT (rs[i], rects[i]);
1887 #endif
1888 return n;
1892 /* EXPORT:
1893 Return in *NR the clipping rectangle for glyph string S. */
1895 void
1896 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1898 get_glyph_string_clip_rects (s, nr, 1);
1902 /* EXPORT:
1903 Return the position and height of the phys cursor in window W.
1904 Set w->phys_cursor_width to width of phys cursor.
1907 void
1908 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1909 struct glyph *glyph, int *xp, int *yp, int *heightp)
1911 struct frame *f = XFRAME (WINDOW_FRAME (w));
1912 int x, y, wd, h, h0, y0;
1914 /* Compute the width of the rectangle to draw. If on a stretch
1915 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1916 rectangle as wide as the glyph, but use a canonical character
1917 width instead. */
1918 wd = glyph->pixel_width - 1;
1919 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1920 wd++; /* Why? */
1921 #endif
1923 x = w->phys_cursor.x;
1924 if (x < 0)
1926 wd += x;
1927 x = 0;
1930 if (glyph->type == STRETCH_GLYPH
1931 && !x_stretch_cursor_p)
1932 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1933 w->phys_cursor_width = wd;
1935 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1937 /* If y is below window bottom, ensure that we still see a cursor. */
1938 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1940 h = max (h0, glyph->ascent + glyph->descent);
1941 h0 = min (h0, glyph->ascent + glyph->descent);
1943 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1944 if (y < y0)
1946 h = max (h - (y0 - y) + 1, h0);
1947 y = y0 - 1;
1949 else
1951 y0 = window_text_bottom_y (w) - h0;
1952 if (y > y0)
1954 h += y - y0;
1955 y = y0;
1959 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1960 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1961 *heightp = h;
1965 * Remember which glyph the mouse is over.
1968 void
1969 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1971 Lisp_Object window;
1972 struct window *w;
1973 struct glyph_row *r, *gr, *end_row;
1974 enum window_part part;
1975 enum glyph_row_area area;
1976 int x, y, width, height;
1978 /* Try to determine frame pixel position and size of the glyph under
1979 frame pixel coordinates X/Y on frame F. */
1981 if (!f->glyphs_initialized_p
1982 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1983 NILP (window)))
1985 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1986 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1987 goto virtual_glyph;
1990 w = XWINDOW (window);
1991 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1992 height = WINDOW_FRAME_LINE_HEIGHT (w);
1994 x = window_relative_x_coord (w, part, gx);
1995 y = gy - WINDOW_TOP_EDGE_Y (w);
1997 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1998 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2000 if (w->pseudo_window_p)
2002 area = TEXT_AREA;
2003 part = ON_MODE_LINE; /* Don't adjust margin. */
2004 goto text_glyph;
2007 switch (part)
2009 case ON_LEFT_MARGIN:
2010 area = LEFT_MARGIN_AREA;
2011 goto text_glyph;
2013 case ON_RIGHT_MARGIN:
2014 area = RIGHT_MARGIN_AREA;
2015 goto text_glyph;
2017 case ON_HEADER_LINE:
2018 case ON_MODE_LINE:
2019 gr = (part == ON_HEADER_LINE
2020 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2021 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2022 gy = gr->y;
2023 area = TEXT_AREA;
2024 goto text_glyph_row_found;
2026 case ON_TEXT:
2027 area = TEXT_AREA;
2029 text_glyph:
2030 gr = 0; gy = 0;
2031 for (; r <= end_row && r->enabled_p; ++r)
2032 if (r->y + r->height > y)
2034 gr = r; gy = r->y;
2035 break;
2038 text_glyph_row_found:
2039 if (gr && gy <= y)
2041 struct glyph *g = gr->glyphs[area];
2042 struct glyph *end = g + gr->used[area];
2044 height = gr->height;
2045 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2046 if (gx + g->pixel_width > x)
2047 break;
2049 if (g < end)
2051 if (g->type == IMAGE_GLYPH)
2053 /* Don't remember when mouse is over image, as
2054 image may have hot-spots. */
2055 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2056 return;
2058 width = g->pixel_width;
2060 else
2062 /* Use nominal char spacing at end of line. */
2063 x -= gx;
2064 gx += (x / width) * width;
2067 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2068 gx += window_box_left_offset (w, area);
2070 else
2072 /* Use nominal line height at end of window. */
2073 gx = (x / width) * width;
2074 y -= gy;
2075 gy += (y / height) * height;
2077 break;
2079 case ON_LEFT_FRINGE:
2080 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2081 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2082 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2083 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2084 goto row_glyph;
2086 case ON_RIGHT_FRINGE:
2087 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2088 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2089 : window_box_right_offset (w, TEXT_AREA));
2090 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2091 goto row_glyph;
2093 case ON_SCROLL_BAR:
2094 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2096 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2097 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2098 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2099 : 0)));
2100 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2102 row_glyph:
2103 gr = 0, gy = 0;
2104 for (; r <= end_row && r->enabled_p; ++r)
2105 if (r->y + r->height > y)
2107 gr = r; gy = r->y;
2108 break;
2111 if (gr && gy <= y)
2112 height = gr->height;
2113 else
2115 /* Use nominal line height at end of window. */
2116 y -= gy;
2117 gy += (y / height) * height;
2119 break;
2121 default:
2123 virtual_glyph:
2124 /* If there is no glyph under the mouse, then we divide the screen
2125 into a grid of the smallest glyph in the frame, and use that
2126 as our "glyph". */
2128 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2129 round down even for negative values. */
2130 if (gx < 0)
2131 gx -= width - 1;
2132 if (gy < 0)
2133 gy -= height - 1;
2135 gx = (gx / width) * width;
2136 gy = (gy / height) * height;
2138 goto store_rect;
2141 gx += WINDOW_LEFT_EDGE_X (w);
2142 gy += WINDOW_TOP_EDGE_Y (w);
2144 store_rect:
2145 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2147 /* Visible feedback for debugging. */
2148 #if 0
2149 #if HAVE_X_WINDOWS
2150 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2151 f->output_data.x->normal_gc,
2152 gx, gy, width, height);
2153 #endif
2154 #endif
2158 #endif /* HAVE_WINDOW_SYSTEM */
2161 /***********************************************************************
2162 Lisp form evaluation
2163 ***********************************************************************/
2165 /* Error handler for safe_eval and safe_call. */
2167 static Lisp_Object
2168 safe_eval_handler (Lisp_Object arg)
2170 add_to_log ("Error during redisplay: %S", arg, Qnil);
2171 return Qnil;
2175 /* Evaluate SEXPR and return the result, or nil if something went
2176 wrong. Prevent redisplay during the evaluation. */
2178 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2179 Return the result, or nil if something went wrong. Prevent
2180 redisplay during the evaluation. */
2182 Lisp_Object
2183 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2185 Lisp_Object val;
2187 if (inhibit_eval_during_redisplay)
2188 val = Qnil;
2189 else
2191 int count = SPECPDL_INDEX ();
2192 struct gcpro gcpro1;
2194 GCPRO1 (args[0]);
2195 gcpro1.nvars = nargs;
2196 specbind (Qinhibit_redisplay, Qt);
2197 /* Use Qt to ensure debugger does not run,
2198 so there is no possibility of wanting to redisplay. */
2199 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2200 safe_eval_handler);
2201 UNGCPRO;
2202 val = unbind_to (count, val);
2205 return val;
2209 /* Call function FN with one argument ARG.
2210 Return the result, or nil if something went wrong. */
2212 Lisp_Object
2213 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2215 Lisp_Object args[2];
2216 args[0] = fn;
2217 args[1] = arg;
2218 return safe_call (2, args);
2221 static Lisp_Object Qeval;
2223 Lisp_Object
2224 safe_eval (Lisp_Object sexpr)
2226 return safe_call1 (Qeval, sexpr);
2229 /* Call function FN with one argument ARG.
2230 Return the result, or nil if something went wrong. */
2232 Lisp_Object
2233 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2235 Lisp_Object args[3];
2236 args[0] = fn;
2237 args[1] = arg1;
2238 args[2] = arg2;
2239 return safe_call (3, args);
2244 /***********************************************************************
2245 Debugging
2246 ***********************************************************************/
2248 #if 0
2250 /* Define CHECK_IT to perform sanity checks on iterators.
2251 This is for debugging. It is too slow to do unconditionally. */
2253 static void
2254 check_it (struct it *it)
2256 if (it->method == GET_FROM_STRING)
2258 xassert (STRINGP (it->string));
2259 xassert (IT_STRING_CHARPOS (*it) >= 0);
2261 else
2263 xassert (IT_STRING_CHARPOS (*it) < 0);
2264 if (it->method == GET_FROM_BUFFER)
2266 /* Check that character and byte positions agree. */
2267 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2271 if (it->dpvec)
2272 xassert (it->current.dpvec_index >= 0);
2273 else
2274 xassert (it->current.dpvec_index < 0);
2277 #define CHECK_IT(IT) check_it ((IT))
2279 #else /* not 0 */
2281 #define CHECK_IT(IT) (void) 0
2283 #endif /* not 0 */
2286 #if GLYPH_DEBUG && XASSERTS
2288 /* Check that the window end of window W is what we expect it
2289 to be---the last row in the current matrix displaying text. */
2291 static void
2292 check_window_end (struct window *w)
2294 if (!MINI_WINDOW_P (w)
2295 && !NILP (w->window_end_valid))
2297 struct glyph_row *row;
2298 xassert ((row = MATRIX_ROW (w->current_matrix,
2299 XFASTINT (w->window_end_vpos)),
2300 !row->enabled_p
2301 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2302 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2306 #define CHECK_WINDOW_END(W) check_window_end ((W))
2308 #else
2310 #define CHECK_WINDOW_END(W) (void) 0
2312 #endif
2316 /***********************************************************************
2317 Iterator initialization
2318 ***********************************************************************/
2320 /* Initialize IT for displaying current_buffer in window W, starting
2321 at character position CHARPOS. CHARPOS < 0 means that no buffer
2322 position is specified which is useful when the iterator is assigned
2323 a position later. BYTEPOS is the byte position corresponding to
2324 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2326 If ROW is not null, calls to produce_glyphs with IT as parameter
2327 will produce glyphs in that row.
2329 BASE_FACE_ID is the id of a base face to use. It must be one of
2330 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2331 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2332 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2334 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2335 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2336 will be initialized to use the corresponding mode line glyph row of
2337 the desired matrix of W. */
2339 void
2340 init_iterator (struct it *it, struct window *w,
2341 EMACS_INT charpos, EMACS_INT bytepos,
2342 struct glyph_row *row, enum face_id base_face_id)
2344 int highlight_region_p;
2345 enum face_id remapped_base_face_id = base_face_id;
2347 /* Some precondition checks. */
2348 xassert (w != NULL && it != NULL);
2349 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2350 && charpos <= ZV));
2352 /* If face attributes have been changed since the last redisplay,
2353 free realized faces now because they depend on face definitions
2354 that might have changed. Don't free faces while there might be
2355 desired matrices pending which reference these faces. */
2356 if (face_change_count && !inhibit_free_realized_faces)
2358 face_change_count = 0;
2359 free_all_realized_faces (Qnil);
2362 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2363 if (! NILP (Vface_remapping_alist))
2364 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2366 /* Use one of the mode line rows of W's desired matrix if
2367 appropriate. */
2368 if (row == NULL)
2370 if (base_face_id == MODE_LINE_FACE_ID
2371 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2372 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2373 else if (base_face_id == HEADER_LINE_FACE_ID)
2374 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2377 /* Clear IT. */
2378 memset (it, 0, sizeof *it);
2379 it->current.overlay_string_index = -1;
2380 it->current.dpvec_index = -1;
2381 it->base_face_id = remapped_base_face_id;
2382 it->string = Qnil;
2383 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2384 it->paragraph_embedding = L2R;
2385 it->bidi_it.string.lstring = Qnil;
2386 it->bidi_it.string.s = NULL;
2387 it->bidi_it.string.bufpos = 0;
2389 /* The window in which we iterate over current_buffer: */
2390 XSETWINDOW (it->window, w);
2391 it->w = w;
2392 it->f = XFRAME (w->frame);
2394 it->cmp_it.id = -1;
2396 /* Extra space between lines (on window systems only). */
2397 if (base_face_id == DEFAULT_FACE_ID
2398 && FRAME_WINDOW_P (it->f))
2400 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2401 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2402 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2403 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2404 * FRAME_LINE_HEIGHT (it->f));
2405 else if (it->f->extra_line_spacing > 0)
2406 it->extra_line_spacing = it->f->extra_line_spacing;
2407 it->max_extra_line_spacing = 0;
2410 /* If realized faces have been removed, e.g. because of face
2411 attribute changes of named faces, recompute them. When running
2412 in batch mode, the face cache of the initial frame is null. If
2413 we happen to get called, make a dummy face cache. */
2414 if (FRAME_FACE_CACHE (it->f) == NULL)
2415 init_frame_faces (it->f);
2416 if (FRAME_FACE_CACHE (it->f)->used == 0)
2417 recompute_basic_faces (it->f);
2419 /* Current value of the `slice', `space-width', and 'height' properties. */
2420 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2421 it->space_width = Qnil;
2422 it->font_height = Qnil;
2423 it->override_ascent = -1;
2425 /* Are control characters displayed as `^C'? */
2426 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2428 /* -1 means everything between a CR and the following line end
2429 is invisible. >0 means lines indented more than this value are
2430 invisible. */
2431 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2432 ? XINT (BVAR (current_buffer, selective_display))
2433 : (!NILP (BVAR (current_buffer, selective_display))
2434 ? -1 : 0));
2435 it->selective_display_ellipsis_p
2436 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2438 /* Display table to use. */
2439 it->dp = window_display_table (w);
2441 /* Are multibyte characters enabled in current_buffer? */
2442 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2444 /* Non-zero if we should highlight the region. */
2445 highlight_region_p
2446 = (!NILP (Vtransient_mark_mode)
2447 && !NILP (BVAR (current_buffer, mark_active))
2448 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2450 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2451 start and end of a visible region in window IT->w. Set both to
2452 -1 to indicate no region. */
2453 if (highlight_region_p
2454 /* Maybe highlight only in selected window. */
2455 && (/* Either show region everywhere. */
2456 highlight_nonselected_windows
2457 /* Or show region in the selected window. */
2458 || w == XWINDOW (selected_window)
2459 /* Or show the region if we are in the mini-buffer and W is
2460 the window the mini-buffer refers to. */
2461 || (MINI_WINDOW_P (XWINDOW (selected_window))
2462 && WINDOWP (minibuf_selected_window)
2463 && w == XWINDOW (minibuf_selected_window))))
2465 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2466 it->region_beg_charpos = min (PT, markpos);
2467 it->region_end_charpos = max (PT, markpos);
2469 else
2470 it->region_beg_charpos = it->region_end_charpos = -1;
2472 /* Get the position at which the redisplay_end_trigger hook should
2473 be run, if it is to be run at all. */
2474 if (MARKERP (w->redisplay_end_trigger)
2475 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2476 it->redisplay_end_trigger_charpos
2477 = marker_position (w->redisplay_end_trigger);
2478 else if (INTEGERP (w->redisplay_end_trigger))
2479 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2481 /* Correct bogus values of tab_width. */
2482 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2483 if (it->tab_width <= 0 || it->tab_width > 1000)
2484 it->tab_width = 8;
2486 /* Are lines in the display truncated? */
2487 if (base_face_id != DEFAULT_FACE_ID
2488 || XINT (it->w->hscroll)
2489 || (! WINDOW_FULL_WIDTH_P (it->w)
2490 && ((!NILP (Vtruncate_partial_width_windows)
2491 && !INTEGERP (Vtruncate_partial_width_windows))
2492 || (INTEGERP (Vtruncate_partial_width_windows)
2493 && (WINDOW_TOTAL_COLS (it->w)
2494 < XINT (Vtruncate_partial_width_windows))))))
2495 it->line_wrap = TRUNCATE;
2496 else if (NILP (BVAR (current_buffer, truncate_lines)))
2497 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2498 ? WINDOW_WRAP : WORD_WRAP;
2499 else
2500 it->line_wrap = TRUNCATE;
2502 /* Get dimensions of truncation and continuation glyphs. These are
2503 displayed as fringe bitmaps under X, so we don't need them for such
2504 frames. */
2505 if (!FRAME_WINDOW_P (it->f))
2507 if (it->line_wrap == TRUNCATE)
2509 /* We will need the truncation glyph. */
2510 xassert (it->glyph_row == NULL);
2511 produce_special_glyphs (it, IT_TRUNCATION);
2512 it->truncation_pixel_width = it->pixel_width;
2514 else
2516 /* We will need the continuation glyph. */
2517 xassert (it->glyph_row == NULL);
2518 produce_special_glyphs (it, IT_CONTINUATION);
2519 it->continuation_pixel_width = it->pixel_width;
2522 /* Reset these values to zero because the produce_special_glyphs
2523 above has changed them. */
2524 it->pixel_width = it->ascent = it->descent = 0;
2525 it->phys_ascent = it->phys_descent = 0;
2528 /* Set this after getting the dimensions of truncation and
2529 continuation glyphs, so that we don't produce glyphs when calling
2530 produce_special_glyphs, above. */
2531 it->glyph_row = row;
2532 it->area = TEXT_AREA;
2534 /* Forget any previous info about this row being reversed. */
2535 if (it->glyph_row)
2536 it->glyph_row->reversed_p = 0;
2538 /* Get the dimensions of the display area. The display area
2539 consists of the visible window area plus a horizontally scrolled
2540 part to the left of the window. All x-values are relative to the
2541 start of this total display area. */
2542 if (base_face_id != DEFAULT_FACE_ID)
2544 /* Mode lines, menu bar in terminal frames. */
2545 it->first_visible_x = 0;
2546 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2548 else
2550 it->first_visible_x
2551 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2552 it->last_visible_x = (it->first_visible_x
2553 + window_box_width (w, TEXT_AREA));
2555 /* If we truncate lines, leave room for the truncator glyph(s) at
2556 the right margin. Otherwise, leave room for the continuation
2557 glyph(s). Truncation and continuation glyphs are not inserted
2558 for window-based redisplay. */
2559 if (!FRAME_WINDOW_P (it->f))
2561 if (it->line_wrap == TRUNCATE)
2562 it->last_visible_x -= it->truncation_pixel_width;
2563 else
2564 it->last_visible_x -= it->continuation_pixel_width;
2567 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2568 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2571 /* Leave room for a border glyph. */
2572 if (!FRAME_WINDOW_P (it->f)
2573 && !WINDOW_RIGHTMOST_P (it->w))
2574 it->last_visible_x -= 1;
2576 it->last_visible_y = window_text_bottom_y (w);
2578 /* For mode lines and alike, arrange for the first glyph having a
2579 left box line if the face specifies a box. */
2580 if (base_face_id != DEFAULT_FACE_ID)
2582 struct face *face;
2584 it->face_id = remapped_base_face_id;
2586 /* If we have a boxed mode line, make the first character appear
2587 with a left box line. */
2588 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2589 if (face->box != FACE_NO_BOX)
2590 it->start_of_box_run_p = 1;
2593 /* If a buffer position was specified, set the iterator there,
2594 getting overlays and face properties from that position. */
2595 if (charpos >= BUF_BEG (current_buffer))
2597 it->end_charpos = ZV;
2598 it->face_id = -1;
2599 IT_CHARPOS (*it) = charpos;
2601 /* Compute byte position if not specified. */
2602 if (bytepos < charpos)
2603 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2604 else
2605 IT_BYTEPOS (*it) = bytepos;
2607 it->start = it->current;
2608 /* Do we need to reorder bidirectional text? Not if this is a
2609 unibyte buffer: by definition, none of the single-byte
2610 characters are strong R2L, so no reordering is needed. And
2611 bidi.c doesn't support unibyte buffers anyway. */
2612 it->bidi_p =
2613 !NILP (BVAR (current_buffer, bidi_display_reordering))
2614 && it->multibyte_p;
2616 /* If we are to reorder bidirectional text, init the bidi
2617 iterator. */
2618 if (it->bidi_p)
2620 /* Note the paragraph direction that this buffer wants to
2621 use. */
2622 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2623 Qleft_to_right))
2624 it->paragraph_embedding = L2R;
2625 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2626 Qright_to_left))
2627 it->paragraph_embedding = R2L;
2628 else
2629 it->paragraph_embedding = NEUTRAL_DIR;
2630 bidi_unshelve_cache (NULL, 0);
2631 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2632 &it->bidi_it);
2635 /* Compute faces etc. */
2636 reseat (it, it->current.pos, 1);
2639 CHECK_IT (it);
2643 /* Initialize IT for the display of window W with window start POS. */
2645 void
2646 start_display (struct it *it, struct window *w, struct text_pos pos)
2648 struct glyph_row *row;
2649 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2651 row = w->desired_matrix->rows + first_vpos;
2652 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2653 it->first_vpos = first_vpos;
2655 /* Don't reseat to previous visible line start if current start
2656 position is in a string or image. */
2657 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2659 int start_at_line_beg_p;
2660 int first_y = it->current_y;
2662 /* If window start is not at a line start, skip forward to POS to
2663 get the correct continuation lines width. */
2664 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2665 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2666 if (!start_at_line_beg_p)
2668 int new_x;
2670 reseat_at_previous_visible_line_start (it);
2671 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2673 new_x = it->current_x + it->pixel_width;
2675 /* If lines are continued, this line may end in the middle
2676 of a multi-glyph character (e.g. a control character
2677 displayed as \003, or in the middle of an overlay
2678 string). In this case move_it_to above will not have
2679 taken us to the start of the continuation line but to the
2680 end of the continued line. */
2681 if (it->current_x > 0
2682 && it->line_wrap != TRUNCATE /* Lines are continued. */
2683 && (/* And glyph doesn't fit on the line. */
2684 new_x > it->last_visible_x
2685 /* Or it fits exactly and we're on a window
2686 system frame. */
2687 || (new_x == it->last_visible_x
2688 && FRAME_WINDOW_P (it->f))))
2690 if (it->current.dpvec_index >= 0
2691 || it->current.overlay_string_index >= 0)
2693 set_iterator_to_next (it, 1);
2694 move_it_in_display_line_to (it, -1, -1, 0);
2697 it->continuation_lines_width += it->current_x;
2700 /* We're starting a new display line, not affected by the
2701 height of the continued line, so clear the appropriate
2702 fields in the iterator structure. */
2703 it->max_ascent = it->max_descent = 0;
2704 it->max_phys_ascent = it->max_phys_descent = 0;
2706 it->current_y = first_y;
2707 it->vpos = 0;
2708 it->current_x = it->hpos = 0;
2714 /* Return 1 if POS is a position in ellipses displayed for invisible
2715 text. W is the window we display, for text property lookup. */
2717 static int
2718 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2720 Lisp_Object prop, window;
2721 int ellipses_p = 0;
2722 EMACS_INT charpos = CHARPOS (pos->pos);
2724 /* If POS specifies a position in a display vector, this might
2725 be for an ellipsis displayed for invisible text. We won't
2726 get the iterator set up for delivering that ellipsis unless
2727 we make sure that it gets aware of the invisible text. */
2728 if (pos->dpvec_index >= 0
2729 && pos->overlay_string_index < 0
2730 && CHARPOS (pos->string_pos) < 0
2731 && charpos > BEGV
2732 && (XSETWINDOW (window, w),
2733 prop = Fget_char_property (make_number (charpos),
2734 Qinvisible, window),
2735 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2737 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2738 window);
2739 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2742 return ellipses_p;
2746 /* Initialize IT for stepping through current_buffer in window W,
2747 starting at position POS that includes overlay string and display
2748 vector/ control character translation position information. Value
2749 is zero if there are overlay strings with newlines at POS. */
2751 static int
2752 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2754 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2755 int i, overlay_strings_with_newlines = 0;
2757 /* If POS specifies a position in a display vector, this might
2758 be for an ellipsis displayed for invisible text. We won't
2759 get the iterator set up for delivering that ellipsis unless
2760 we make sure that it gets aware of the invisible text. */
2761 if (in_ellipses_for_invisible_text_p (pos, w))
2763 --charpos;
2764 bytepos = 0;
2767 /* Keep in mind: the call to reseat in init_iterator skips invisible
2768 text, so we might end up at a position different from POS. This
2769 is only a problem when POS is a row start after a newline and an
2770 overlay starts there with an after-string, and the overlay has an
2771 invisible property. Since we don't skip invisible text in
2772 display_line and elsewhere immediately after consuming the
2773 newline before the row start, such a POS will not be in a string,
2774 but the call to init_iterator below will move us to the
2775 after-string. */
2776 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2778 /* This only scans the current chunk -- it should scan all chunks.
2779 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2780 to 16 in 22.1 to make this a lesser problem. */
2781 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2783 const char *s = SSDATA (it->overlay_strings[i]);
2784 const char *e = s + SBYTES (it->overlay_strings[i]);
2786 while (s < e && *s != '\n')
2787 ++s;
2789 if (s < e)
2791 overlay_strings_with_newlines = 1;
2792 break;
2796 /* If position is within an overlay string, set up IT to the right
2797 overlay string. */
2798 if (pos->overlay_string_index >= 0)
2800 int relative_index;
2802 /* If the first overlay string happens to have a `display'
2803 property for an image, the iterator will be set up for that
2804 image, and we have to undo that setup first before we can
2805 correct the overlay string index. */
2806 if (it->method == GET_FROM_IMAGE)
2807 pop_it (it);
2809 /* We already have the first chunk of overlay strings in
2810 IT->overlay_strings. Load more until the one for
2811 pos->overlay_string_index is in IT->overlay_strings. */
2812 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2814 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2815 it->current.overlay_string_index = 0;
2816 while (n--)
2818 load_overlay_strings (it, 0);
2819 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2823 it->current.overlay_string_index = pos->overlay_string_index;
2824 relative_index = (it->current.overlay_string_index
2825 % OVERLAY_STRING_CHUNK_SIZE);
2826 it->string = it->overlay_strings[relative_index];
2827 xassert (STRINGP (it->string));
2828 it->current.string_pos = pos->string_pos;
2829 it->method = GET_FROM_STRING;
2832 if (CHARPOS (pos->string_pos) >= 0)
2834 /* Recorded position is not in an overlay string, but in another
2835 string. This can only be a string from a `display' property.
2836 IT should already be filled with that string. */
2837 it->current.string_pos = pos->string_pos;
2838 xassert (STRINGP (it->string));
2841 /* Restore position in display vector translations, control
2842 character translations or ellipses. */
2843 if (pos->dpvec_index >= 0)
2845 if (it->dpvec == NULL)
2846 get_next_display_element (it);
2847 xassert (it->dpvec && it->current.dpvec_index == 0);
2848 it->current.dpvec_index = pos->dpvec_index;
2851 CHECK_IT (it);
2852 return !overlay_strings_with_newlines;
2856 /* Initialize IT for stepping through current_buffer in window W
2857 starting at ROW->start. */
2859 static void
2860 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2862 init_from_display_pos (it, w, &row->start);
2863 it->start = row->start;
2864 it->continuation_lines_width = row->continuation_lines_width;
2865 CHECK_IT (it);
2869 /* Initialize IT for stepping through current_buffer in window W
2870 starting in the line following ROW, i.e. starting at ROW->end.
2871 Value is zero if there are overlay strings with newlines at ROW's
2872 end position. */
2874 static int
2875 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2877 int success = 0;
2879 if (init_from_display_pos (it, w, &row->end))
2881 if (row->continued_p)
2882 it->continuation_lines_width
2883 = row->continuation_lines_width + row->pixel_width;
2884 CHECK_IT (it);
2885 success = 1;
2888 return success;
2894 /***********************************************************************
2895 Text properties
2896 ***********************************************************************/
2898 /* Called when IT reaches IT->stop_charpos. Handle text property and
2899 overlay changes. Set IT->stop_charpos to the next position where
2900 to stop. */
2902 static void
2903 handle_stop (struct it *it)
2905 enum prop_handled handled;
2906 int handle_overlay_change_p;
2907 struct props *p;
2909 it->dpvec = NULL;
2910 it->current.dpvec_index = -1;
2911 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2912 it->ignore_overlay_strings_at_pos_p = 0;
2913 it->ellipsis_p = 0;
2915 /* Use face of preceding text for ellipsis (if invisible) */
2916 if (it->selective_display_ellipsis_p)
2917 it->saved_face_id = it->face_id;
2921 handled = HANDLED_NORMALLY;
2923 /* Call text property handlers. */
2924 for (p = it_props; p->handler; ++p)
2926 handled = p->handler (it);
2928 if (handled == HANDLED_RECOMPUTE_PROPS)
2929 break;
2930 else if (handled == HANDLED_RETURN)
2932 /* We still want to show before and after strings from
2933 overlays even if the actual buffer text is replaced. */
2934 if (!handle_overlay_change_p
2935 || it->sp > 1
2936 || !get_overlay_strings_1 (it, 0, 0))
2938 if (it->ellipsis_p)
2939 setup_for_ellipsis (it, 0);
2940 /* When handling a display spec, we might load an
2941 empty string. In that case, discard it here. We
2942 used to discard it in handle_single_display_spec,
2943 but that causes get_overlay_strings_1, above, to
2944 ignore overlay strings that we must check. */
2945 if (STRINGP (it->string) && !SCHARS (it->string))
2946 pop_it (it);
2947 return;
2949 else if (STRINGP (it->string) && !SCHARS (it->string))
2950 pop_it (it);
2951 else
2953 it->ignore_overlay_strings_at_pos_p = 1;
2954 it->string_from_display_prop_p = 0;
2955 it->from_disp_prop_p = 0;
2956 handle_overlay_change_p = 0;
2958 handled = HANDLED_RECOMPUTE_PROPS;
2959 break;
2961 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2962 handle_overlay_change_p = 0;
2965 if (handled != HANDLED_RECOMPUTE_PROPS)
2967 /* Don't check for overlay strings below when set to deliver
2968 characters from a display vector. */
2969 if (it->method == GET_FROM_DISPLAY_VECTOR)
2970 handle_overlay_change_p = 0;
2972 /* Handle overlay changes.
2973 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2974 if it finds overlays. */
2975 if (handle_overlay_change_p)
2976 handled = handle_overlay_change (it);
2979 if (it->ellipsis_p)
2981 setup_for_ellipsis (it, 0);
2982 break;
2985 while (handled == HANDLED_RECOMPUTE_PROPS);
2987 /* Determine where to stop next. */
2988 if (handled == HANDLED_NORMALLY)
2989 compute_stop_pos (it);
2993 /* Compute IT->stop_charpos from text property and overlay change
2994 information for IT's current position. */
2996 static void
2997 compute_stop_pos (struct it *it)
2999 register INTERVAL iv, next_iv;
3000 Lisp_Object object, limit, position;
3001 EMACS_INT charpos, bytepos;
3003 /* If nowhere else, stop at the end. */
3004 it->stop_charpos = it->end_charpos;
3006 if (STRINGP (it->string))
3008 /* Strings are usually short, so don't limit the search for
3009 properties. */
3010 object = it->string;
3011 limit = Qnil;
3012 charpos = IT_STRING_CHARPOS (*it);
3013 bytepos = IT_STRING_BYTEPOS (*it);
3015 else
3017 EMACS_INT pos;
3019 /* If next overlay change is in front of the current stop pos
3020 (which is IT->end_charpos), stop there. Note: value of
3021 next_overlay_change is point-max if no overlay change
3022 follows. */
3023 charpos = IT_CHARPOS (*it);
3024 bytepos = IT_BYTEPOS (*it);
3025 pos = next_overlay_change (charpos);
3026 if (pos < it->stop_charpos)
3027 it->stop_charpos = pos;
3029 /* If showing the region, we have to stop at the region
3030 start or end because the face might change there. */
3031 if (it->region_beg_charpos > 0)
3033 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3034 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3035 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3036 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3039 /* Set up variables for computing the stop position from text
3040 property changes. */
3041 XSETBUFFER (object, current_buffer);
3042 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3045 /* Get the interval containing IT's position. Value is a null
3046 interval if there isn't such an interval. */
3047 position = make_number (charpos);
3048 iv = validate_interval_range (object, &position, &position, 0);
3049 if (!NULL_INTERVAL_P (iv))
3051 Lisp_Object values_here[LAST_PROP_IDX];
3052 struct props *p;
3054 /* Get properties here. */
3055 for (p = it_props; p->handler; ++p)
3056 values_here[p->idx] = textget (iv->plist, *p->name);
3058 /* Look for an interval following iv that has different
3059 properties. */
3060 for (next_iv = next_interval (iv);
3061 (!NULL_INTERVAL_P (next_iv)
3062 && (NILP (limit)
3063 || XFASTINT (limit) > next_iv->position));
3064 next_iv = next_interval (next_iv))
3066 for (p = it_props; p->handler; ++p)
3068 Lisp_Object new_value;
3070 new_value = textget (next_iv->plist, *p->name);
3071 if (!EQ (values_here[p->idx], new_value))
3072 break;
3075 if (p->handler)
3076 break;
3079 if (!NULL_INTERVAL_P (next_iv))
3081 if (INTEGERP (limit)
3082 && next_iv->position >= XFASTINT (limit))
3083 /* No text property change up to limit. */
3084 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3085 else
3086 /* Text properties change in next_iv. */
3087 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3091 if (it->cmp_it.id < 0)
3093 EMACS_INT stoppos = it->end_charpos;
3095 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3096 stoppos = -1;
3097 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3098 stoppos, it->string);
3101 xassert (STRINGP (it->string)
3102 || (it->stop_charpos >= BEGV
3103 && it->stop_charpos >= IT_CHARPOS (*it)));
3107 /* Return the position of the next overlay change after POS in
3108 current_buffer. Value is point-max if no overlay change
3109 follows. This is like `next-overlay-change' but doesn't use
3110 xmalloc. */
3112 static EMACS_INT
3113 next_overlay_change (EMACS_INT pos)
3115 ptrdiff_t i, noverlays;
3116 EMACS_INT endpos;
3117 Lisp_Object *overlays;
3119 /* Get all overlays at the given position. */
3120 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3122 /* If any of these overlays ends before endpos,
3123 use its ending point instead. */
3124 for (i = 0; i < noverlays; ++i)
3126 Lisp_Object oend;
3127 EMACS_INT oendpos;
3129 oend = OVERLAY_END (overlays[i]);
3130 oendpos = OVERLAY_POSITION (oend);
3131 endpos = min (endpos, oendpos);
3134 return endpos;
3137 /* How many characters forward to search for a display property or
3138 display string. Enough for a screenful of 100 lines x 50
3139 characters in a line. */
3140 #define MAX_DISP_SCAN 5000
3142 /* Return the character position of a display string at or after
3143 position specified by POSITION. If no display string exists at or
3144 after POSITION, return ZV. A display string is either an overlay
3145 with `display' property whose value is a string, or a `display'
3146 text property whose value is a string. STRING is data about the
3147 string to iterate; if STRING->lstring is nil, we are iterating a
3148 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3149 on a GUI frame. */
3150 EMACS_INT
3151 compute_display_string_pos (struct text_pos *position,
3152 struct bidi_string_data *string,
3153 int frame_window_p, int *disp_prop_p)
3155 /* OBJECT = nil means current buffer. */
3156 Lisp_Object object =
3157 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3158 Lisp_Object pos, spec, limpos;
3159 int string_p = (string && (STRINGP (string->lstring) || string->s));
3160 EMACS_INT eob = string_p ? string->schars : ZV;
3161 EMACS_INT begb = string_p ? 0 : BEGV;
3162 EMACS_INT bufpos, charpos = CHARPOS (*position);
3163 EMACS_INT lim =
3164 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3165 struct text_pos tpos;
3167 *disp_prop_p = 1;
3169 if (charpos >= eob
3170 /* We don't support display properties whose values are strings
3171 that have display string properties. */
3172 || string->from_disp_str
3173 /* C strings cannot have display properties. */
3174 || (string->s && !STRINGP (object)))
3176 *disp_prop_p = 0;
3177 return eob;
3180 /* If the character at CHARPOS is where the display string begins,
3181 return CHARPOS. */
3182 pos = make_number (charpos);
3183 if (STRINGP (object))
3184 bufpos = string->bufpos;
3185 else
3186 bufpos = charpos;
3187 tpos = *position;
3188 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3189 && (charpos <= begb
3190 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3191 object),
3192 spec))
3193 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3194 frame_window_p))
3196 return charpos;
3199 /* Look forward for the first character with a `display' property
3200 that will replace the underlying text when displayed. */
3201 limpos = make_number (lim);
3202 do {
3203 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3204 CHARPOS (tpos) = XFASTINT (pos);
3205 if (CHARPOS (tpos) >= lim)
3207 *disp_prop_p = 0;
3208 break;
3210 if (STRINGP (object))
3211 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3212 else
3213 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3214 spec = Fget_char_property (pos, Qdisplay, object);
3215 if (!STRINGP (object))
3216 bufpos = CHARPOS (tpos);
3217 } while (NILP (spec)
3218 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3219 frame_window_p));
3221 return CHARPOS (tpos);
3224 /* Return the character position of the end of the display string that
3225 started at CHARPOS. A display string is either an overlay with
3226 `display' property whose value is a string or a `display' text
3227 property whose value is a string. */
3228 EMACS_INT
3229 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3231 /* OBJECT = nil means current buffer. */
3232 Lisp_Object object =
3233 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3234 Lisp_Object pos = make_number (charpos);
3235 EMACS_INT eob =
3236 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3238 if (charpos >= eob || (string->s && !STRINGP (object)))
3239 return eob;
3241 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3242 abort ();
3244 /* Look forward for the first character where the `display' property
3245 changes. */
3246 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3248 return XFASTINT (pos);
3253 /***********************************************************************
3254 Fontification
3255 ***********************************************************************/
3257 /* Handle changes in the `fontified' property of the current buffer by
3258 calling hook functions from Qfontification_functions to fontify
3259 regions of text. */
3261 static enum prop_handled
3262 handle_fontified_prop (struct it *it)
3264 Lisp_Object prop, pos;
3265 enum prop_handled handled = HANDLED_NORMALLY;
3267 if (!NILP (Vmemory_full))
3268 return handled;
3270 /* Get the value of the `fontified' property at IT's current buffer
3271 position. (The `fontified' property doesn't have a special
3272 meaning in strings.) If the value is nil, call functions from
3273 Qfontification_functions. */
3274 if (!STRINGP (it->string)
3275 && it->s == NULL
3276 && !NILP (Vfontification_functions)
3277 && !NILP (Vrun_hooks)
3278 && (pos = make_number (IT_CHARPOS (*it)),
3279 prop = Fget_char_property (pos, Qfontified, Qnil),
3280 /* Ignore the special cased nil value always present at EOB since
3281 no amount of fontifying will be able to change it. */
3282 NILP (prop) && IT_CHARPOS (*it) < Z))
3284 int count = SPECPDL_INDEX ();
3285 Lisp_Object val;
3286 struct buffer *obuf = current_buffer;
3287 int begv = BEGV, zv = ZV;
3288 int old_clip_changed = current_buffer->clip_changed;
3290 val = Vfontification_functions;
3291 specbind (Qfontification_functions, Qnil);
3293 xassert (it->end_charpos == ZV);
3295 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3296 safe_call1 (val, pos);
3297 else
3299 Lisp_Object fns, fn;
3300 struct gcpro gcpro1, gcpro2;
3302 fns = Qnil;
3303 GCPRO2 (val, fns);
3305 for (; CONSP (val); val = XCDR (val))
3307 fn = XCAR (val);
3309 if (EQ (fn, Qt))
3311 /* A value of t indicates this hook has a local
3312 binding; it means to run the global binding too.
3313 In a global value, t should not occur. If it
3314 does, we must ignore it to avoid an endless
3315 loop. */
3316 for (fns = Fdefault_value (Qfontification_functions);
3317 CONSP (fns);
3318 fns = XCDR (fns))
3320 fn = XCAR (fns);
3321 if (!EQ (fn, Qt))
3322 safe_call1 (fn, pos);
3325 else
3326 safe_call1 (fn, pos);
3329 UNGCPRO;
3332 unbind_to (count, Qnil);
3334 /* Fontification functions routinely call `save-restriction'.
3335 Normally, this tags clip_changed, which can confuse redisplay
3336 (see discussion in Bug#6671). Since we don't perform any
3337 special handling of fontification changes in the case where
3338 `save-restriction' isn't called, there's no point doing so in
3339 this case either. So, if the buffer's restrictions are
3340 actually left unchanged, reset clip_changed. */
3341 if (obuf == current_buffer)
3343 if (begv == BEGV && zv == ZV)
3344 current_buffer->clip_changed = old_clip_changed;
3346 /* There isn't much we can reasonably do to protect against
3347 misbehaving fontification, but here's a fig leaf. */
3348 else if (!NILP (BVAR (obuf, name)))
3349 set_buffer_internal_1 (obuf);
3351 /* The fontification code may have added/removed text.
3352 It could do even a lot worse, but let's at least protect against
3353 the most obvious case where only the text past `pos' gets changed',
3354 as is/was done in grep.el where some escapes sequences are turned
3355 into face properties (bug#7876). */
3356 it->end_charpos = ZV;
3358 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3359 something. This avoids an endless loop if they failed to
3360 fontify the text for which reason ever. */
3361 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3362 handled = HANDLED_RECOMPUTE_PROPS;
3365 return handled;
3370 /***********************************************************************
3371 Faces
3372 ***********************************************************************/
3374 /* Set up iterator IT from face properties at its current position.
3375 Called from handle_stop. */
3377 static enum prop_handled
3378 handle_face_prop (struct it *it)
3380 int new_face_id;
3381 EMACS_INT next_stop;
3383 if (!STRINGP (it->string))
3385 new_face_id
3386 = face_at_buffer_position (it->w,
3387 IT_CHARPOS (*it),
3388 it->region_beg_charpos,
3389 it->region_end_charpos,
3390 &next_stop,
3391 (IT_CHARPOS (*it)
3392 + TEXT_PROP_DISTANCE_LIMIT),
3393 0, it->base_face_id);
3395 /* Is this a start of a run of characters with box face?
3396 Caveat: this can be called for a freshly initialized
3397 iterator; face_id is -1 in this case. We know that the new
3398 face will not change until limit, i.e. if the new face has a
3399 box, all characters up to limit will have one. But, as
3400 usual, we don't know whether limit is really the end. */
3401 if (new_face_id != it->face_id)
3403 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3405 /* If new face has a box but old face has not, this is
3406 the start of a run of characters with box, i.e. it has
3407 a shadow on the left side. The value of face_id of the
3408 iterator will be -1 if this is the initial call that gets
3409 the face. In this case, we have to look in front of IT's
3410 position and see whether there is a face != new_face_id. */
3411 it->start_of_box_run_p
3412 = (new_face->box != FACE_NO_BOX
3413 && (it->face_id >= 0
3414 || IT_CHARPOS (*it) == BEG
3415 || new_face_id != face_before_it_pos (it)));
3416 it->face_box_p = new_face->box != FACE_NO_BOX;
3419 else
3421 int base_face_id;
3422 EMACS_INT bufpos;
3423 int i;
3424 Lisp_Object from_overlay
3425 = (it->current.overlay_string_index >= 0
3426 ? it->string_overlays[it->current.overlay_string_index]
3427 : Qnil);
3429 /* See if we got to this string directly or indirectly from
3430 an overlay property. That includes the before-string or
3431 after-string of an overlay, strings in display properties
3432 provided by an overlay, their text properties, etc.
3434 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3435 if (! NILP (from_overlay))
3436 for (i = it->sp - 1; i >= 0; i--)
3438 if (it->stack[i].current.overlay_string_index >= 0)
3439 from_overlay
3440 = it->string_overlays[it->stack[i].current.overlay_string_index];
3441 else if (! NILP (it->stack[i].from_overlay))
3442 from_overlay = it->stack[i].from_overlay;
3444 if (!NILP (from_overlay))
3445 break;
3448 if (! NILP (from_overlay))
3450 bufpos = IT_CHARPOS (*it);
3451 /* For a string from an overlay, the base face depends
3452 only on text properties and ignores overlays. */
3453 base_face_id
3454 = face_for_overlay_string (it->w,
3455 IT_CHARPOS (*it),
3456 it->region_beg_charpos,
3457 it->region_end_charpos,
3458 &next_stop,
3459 (IT_CHARPOS (*it)
3460 + TEXT_PROP_DISTANCE_LIMIT),
3462 from_overlay);
3464 else
3466 bufpos = 0;
3468 /* For strings from a `display' property, use the face at
3469 IT's current buffer position as the base face to merge
3470 with, so that overlay strings appear in the same face as
3471 surrounding text, unless they specify their own
3472 faces. */
3473 base_face_id = underlying_face_id (it);
3476 new_face_id = face_at_string_position (it->w,
3477 it->string,
3478 IT_STRING_CHARPOS (*it),
3479 bufpos,
3480 it->region_beg_charpos,
3481 it->region_end_charpos,
3482 &next_stop,
3483 base_face_id, 0);
3485 /* Is this a start of a run of characters with box? Caveat:
3486 this can be called for a freshly allocated iterator; face_id
3487 is -1 is this case. We know that the new face will not
3488 change until the next check pos, i.e. if the new face has a
3489 box, all characters up to that position will have a
3490 box. But, as usual, we don't know whether that position
3491 is really the end. */
3492 if (new_face_id != it->face_id)
3494 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3495 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3497 /* If new face has a box but old face hasn't, this is the
3498 start of a run of characters with box, i.e. it has a
3499 shadow on the left side. */
3500 it->start_of_box_run_p
3501 = new_face->box && (old_face == NULL || !old_face->box);
3502 it->face_box_p = new_face->box != FACE_NO_BOX;
3506 it->face_id = new_face_id;
3507 return HANDLED_NORMALLY;
3511 /* Return the ID of the face ``underlying'' IT's current position,
3512 which is in a string. If the iterator is associated with a
3513 buffer, return the face at IT's current buffer position.
3514 Otherwise, use the iterator's base_face_id. */
3516 static int
3517 underlying_face_id (struct it *it)
3519 int face_id = it->base_face_id, i;
3521 xassert (STRINGP (it->string));
3523 for (i = it->sp - 1; i >= 0; --i)
3524 if (NILP (it->stack[i].string))
3525 face_id = it->stack[i].face_id;
3527 return face_id;
3531 /* Compute the face one character before or after the current position
3532 of IT, in the visual order. BEFORE_P non-zero means get the face
3533 in front (to the left in L2R paragraphs, to the right in R2L
3534 paragraphs) of IT's screen position. Value is the ID of the face. */
3536 static int
3537 face_before_or_after_it_pos (struct it *it, int before_p)
3539 int face_id, limit;
3540 EMACS_INT next_check_charpos;
3541 struct it it_copy;
3542 void *it_copy_data = NULL;
3544 xassert (it->s == NULL);
3546 if (STRINGP (it->string))
3548 EMACS_INT bufpos, charpos;
3549 int base_face_id;
3551 /* No face change past the end of the string (for the case
3552 we are padding with spaces). No face change before the
3553 string start. */
3554 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3555 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3556 return it->face_id;
3558 if (!it->bidi_p)
3560 /* Set charpos to the position before or after IT's current
3561 position, in the logical order, which in the non-bidi
3562 case is the same as the visual order. */
3563 if (before_p)
3564 charpos = IT_STRING_CHARPOS (*it) - 1;
3565 else if (it->what == IT_COMPOSITION)
3566 /* For composition, we must check the character after the
3567 composition. */
3568 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3569 else
3570 charpos = IT_STRING_CHARPOS (*it) + 1;
3572 else
3574 if (before_p)
3576 /* With bidi iteration, the character before the current
3577 in the visual order cannot be found by simple
3578 iteration, because "reverse" reordering is not
3579 supported. Instead, we need to use the move_it_*
3580 family of functions. */
3581 /* Ignore face changes before the first visible
3582 character on this display line. */
3583 if (it->current_x <= it->first_visible_x)
3584 return it->face_id;
3585 SAVE_IT (it_copy, *it, it_copy_data);
3586 /* Implementation note: Since move_it_in_display_line
3587 works in the iterator geometry, and thinks the first
3588 character is always the leftmost, even in R2L lines,
3589 we don't need to distinguish between the R2L and L2R
3590 cases here. */
3591 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3592 it_copy.current_x - 1, MOVE_TO_X);
3593 charpos = IT_STRING_CHARPOS (it_copy);
3594 RESTORE_IT (it, it, it_copy_data);
3596 else
3598 /* Set charpos to the string position of the character
3599 that comes after IT's current position in the visual
3600 order. */
3601 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3603 it_copy = *it;
3604 while (n--)
3605 bidi_move_to_visually_next (&it_copy.bidi_it);
3607 charpos = it_copy.bidi_it.charpos;
3610 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3612 if (it->current.overlay_string_index >= 0)
3613 bufpos = IT_CHARPOS (*it);
3614 else
3615 bufpos = 0;
3617 base_face_id = underlying_face_id (it);
3619 /* Get the face for ASCII, or unibyte. */
3620 face_id = face_at_string_position (it->w,
3621 it->string,
3622 charpos,
3623 bufpos,
3624 it->region_beg_charpos,
3625 it->region_end_charpos,
3626 &next_check_charpos,
3627 base_face_id, 0);
3629 /* Correct the face for charsets different from ASCII. Do it
3630 for the multibyte case only. The face returned above is
3631 suitable for unibyte text if IT->string is unibyte. */
3632 if (STRING_MULTIBYTE (it->string))
3634 struct text_pos pos1 = string_pos (charpos, it->string);
3635 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3636 int c, len;
3637 struct face *face = FACE_FROM_ID (it->f, face_id);
3639 c = string_char_and_length (p, &len);
3640 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3643 else
3645 struct text_pos pos;
3647 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3648 || (IT_CHARPOS (*it) <= BEGV && before_p))
3649 return it->face_id;
3651 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3652 pos = it->current.pos;
3654 if (!it->bidi_p)
3656 if (before_p)
3657 DEC_TEXT_POS (pos, it->multibyte_p);
3658 else
3660 if (it->what == IT_COMPOSITION)
3662 /* For composition, we must check the position after
3663 the composition. */
3664 pos.charpos += it->cmp_it.nchars;
3665 pos.bytepos += it->len;
3667 else
3668 INC_TEXT_POS (pos, it->multibyte_p);
3671 else
3673 if (before_p)
3675 /* With bidi iteration, the character before the current
3676 in the visual order cannot be found by simple
3677 iteration, because "reverse" reordering is not
3678 supported. Instead, we need to use the move_it_*
3679 family of functions. */
3680 /* Ignore face changes before the first visible
3681 character on this display line. */
3682 if (it->current_x <= it->first_visible_x)
3683 return it->face_id;
3684 SAVE_IT (it_copy, *it, it_copy_data);
3685 /* Implementation note: Since move_it_in_display_line
3686 works in the iterator geometry, and thinks the first
3687 character is always the leftmost, even in R2L lines,
3688 we don't need to distinguish between the R2L and L2R
3689 cases here. */
3690 move_it_in_display_line (&it_copy, ZV,
3691 it_copy.current_x - 1, MOVE_TO_X);
3692 pos = it_copy.current.pos;
3693 RESTORE_IT (it, it, it_copy_data);
3695 else
3697 /* Set charpos to the buffer position of the character
3698 that comes after IT's current position in the visual
3699 order. */
3700 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3702 it_copy = *it;
3703 while (n--)
3704 bidi_move_to_visually_next (&it_copy.bidi_it);
3706 SET_TEXT_POS (pos,
3707 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3710 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3712 /* Determine face for CHARSET_ASCII, or unibyte. */
3713 face_id = face_at_buffer_position (it->w,
3714 CHARPOS (pos),
3715 it->region_beg_charpos,
3716 it->region_end_charpos,
3717 &next_check_charpos,
3718 limit, 0, -1);
3720 /* Correct the face for charsets different from ASCII. Do it
3721 for the multibyte case only. The face returned above is
3722 suitable for unibyte text if current_buffer is unibyte. */
3723 if (it->multibyte_p)
3725 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3726 struct face *face = FACE_FROM_ID (it->f, face_id);
3727 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3731 return face_id;
3736 /***********************************************************************
3737 Invisible text
3738 ***********************************************************************/
3740 /* Set up iterator IT from invisible properties at its current
3741 position. Called from handle_stop. */
3743 static enum prop_handled
3744 handle_invisible_prop (struct it *it)
3746 enum prop_handled handled = HANDLED_NORMALLY;
3748 if (STRINGP (it->string))
3750 Lisp_Object prop, end_charpos, limit, charpos;
3752 /* Get the value of the invisible text property at the
3753 current position. Value will be nil if there is no such
3754 property. */
3755 charpos = make_number (IT_STRING_CHARPOS (*it));
3756 prop = Fget_text_property (charpos, Qinvisible, it->string);
3758 if (!NILP (prop)
3759 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3761 EMACS_INT endpos;
3763 handled = HANDLED_RECOMPUTE_PROPS;
3765 /* Get the position at which the next change of the
3766 invisible text property can be found in IT->string.
3767 Value will be nil if the property value is the same for
3768 all the rest of IT->string. */
3769 XSETINT (limit, SCHARS (it->string));
3770 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3771 it->string, limit);
3773 /* Text at current position is invisible. The next
3774 change in the property is at position end_charpos.
3775 Move IT's current position to that position. */
3776 if (INTEGERP (end_charpos)
3777 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3779 struct text_pos old;
3780 EMACS_INT oldpos;
3782 old = it->current.string_pos;
3783 oldpos = CHARPOS (old);
3784 if (it->bidi_p)
3786 if (it->bidi_it.first_elt
3787 && it->bidi_it.charpos < SCHARS (it->string))
3788 bidi_paragraph_init (it->paragraph_embedding,
3789 &it->bidi_it, 1);
3790 /* Bidi-iterate out of the invisible text. */
3793 bidi_move_to_visually_next (&it->bidi_it);
3795 while (oldpos <= it->bidi_it.charpos
3796 && it->bidi_it.charpos < endpos);
3798 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3799 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3800 if (IT_CHARPOS (*it) >= endpos)
3801 it->prev_stop = endpos;
3803 else
3805 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3806 compute_string_pos (&it->current.string_pos, old, it->string);
3809 else
3811 /* The rest of the string is invisible. If this is an
3812 overlay string, proceed with the next overlay string
3813 or whatever comes and return a character from there. */
3814 if (it->current.overlay_string_index >= 0)
3816 next_overlay_string (it);
3817 /* Don't check for overlay strings when we just
3818 finished processing them. */
3819 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3821 else
3823 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3824 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3829 else
3831 int invis_p;
3832 EMACS_INT newpos, next_stop, start_charpos, tem;
3833 Lisp_Object pos, prop, overlay;
3835 /* First of all, is there invisible text at this position? */
3836 tem = start_charpos = IT_CHARPOS (*it);
3837 pos = make_number (tem);
3838 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3839 &overlay);
3840 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3842 /* If we are on invisible text, skip over it. */
3843 if (invis_p && start_charpos < it->end_charpos)
3845 /* Record whether we have to display an ellipsis for the
3846 invisible text. */
3847 int display_ellipsis_p = invis_p == 2;
3849 handled = HANDLED_RECOMPUTE_PROPS;
3851 /* Loop skipping over invisible text. The loop is left at
3852 ZV or with IT on the first char being visible again. */
3855 /* Try to skip some invisible text. Return value is the
3856 position reached which can be equal to where we start
3857 if there is nothing invisible there. This skips both
3858 over invisible text properties and overlays with
3859 invisible property. */
3860 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3862 /* If we skipped nothing at all we weren't at invisible
3863 text in the first place. If everything to the end of
3864 the buffer was skipped, end the loop. */
3865 if (newpos == tem || newpos >= ZV)
3866 invis_p = 0;
3867 else
3869 /* We skipped some characters but not necessarily
3870 all there are. Check if we ended up on visible
3871 text. Fget_char_property returns the property of
3872 the char before the given position, i.e. if we
3873 get invis_p = 0, this means that the char at
3874 newpos is visible. */
3875 pos = make_number (newpos);
3876 prop = Fget_char_property (pos, Qinvisible, it->window);
3877 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3880 /* If we ended up on invisible text, proceed to
3881 skip starting with next_stop. */
3882 if (invis_p)
3883 tem = next_stop;
3885 /* If there are adjacent invisible texts, don't lose the
3886 second one's ellipsis. */
3887 if (invis_p == 2)
3888 display_ellipsis_p = 1;
3890 while (invis_p);
3892 /* The position newpos is now either ZV or on visible text. */
3893 if (it->bidi_p && newpos < ZV)
3895 /* With bidi iteration, the region of invisible text
3896 could start and/or end in the middle of a non-base
3897 embedding level. Therefore, we need to skip
3898 invisible text using the bidi iterator, starting at
3899 IT's current position, until we find ourselves
3900 outside the invisible text. Skipping invisible text
3901 _after_ bidi iteration avoids affecting the visual
3902 order of the displayed text when invisible properties
3903 are added or removed. */
3904 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3906 /* If we were `reseat'ed to a new paragraph,
3907 determine the paragraph base direction. We need
3908 to do it now because next_element_from_buffer may
3909 not have a chance to do it, if we are going to
3910 skip any text at the beginning, which resets the
3911 FIRST_ELT flag. */
3912 bidi_paragraph_init (it->paragraph_embedding,
3913 &it->bidi_it, 1);
3917 bidi_move_to_visually_next (&it->bidi_it);
3919 while (it->stop_charpos <= it->bidi_it.charpos
3920 && it->bidi_it.charpos < newpos);
3921 IT_CHARPOS (*it) = it->bidi_it.charpos;
3922 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3923 /* If we overstepped NEWPOS, record its position in the
3924 iterator, so that we skip invisible text if later the
3925 bidi iteration lands us in the invisible region
3926 again. */
3927 if (IT_CHARPOS (*it) >= newpos)
3928 it->prev_stop = newpos;
3930 else
3932 IT_CHARPOS (*it) = newpos;
3933 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3936 /* If there are before-strings at the start of invisible
3937 text, and the text is invisible because of a text
3938 property, arrange to show before-strings because 20.x did
3939 it that way. (If the text is invisible because of an
3940 overlay property instead of a text property, this is
3941 already handled in the overlay code.) */
3942 if (NILP (overlay)
3943 && get_overlay_strings (it, it->stop_charpos))
3945 handled = HANDLED_RECOMPUTE_PROPS;
3946 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3948 else if (display_ellipsis_p)
3950 /* Make sure that the glyphs of the ellipsis will get
3951 correct `charpos' values. If we would not update
3952 it->position here, the glyphs would belong to the
3953 last visible character _before_ the invisible
3954 text, which confuses `set_cursor_from_row'.
3956 We use the last invisible position instead of the
3957 first because this way the cursor is always drawn on
3958 the first "." of the ellipsis, whenever PT is inside
3959 the invisible text. Otherwise the cursor would be
3960 placed _after_ the ellipsis when the point is after the
3961 first invisible character. */
3962 if (!STRINGP (it->object))
3964 it->position.charpos = newpos - 1;
3965 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3967 it->ellipsis_p = 1;
3968 /* Let the ellipsis display before
3969 considering any properties of the following char.
3970 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3971 handled = HANDLED_RETURN;
3976 return handled;
3980 /* Make iterator IT return `...' next.
3981 Replaces LEN characters from buffer. */
3983 static void
3984 setup_for_ellipsis (struct it *it, int len)
3986 /* Use the display table definition for `...'. Invalid glyphs
3987 will be handled by the method returning elements from dpvec. */
3988 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3990 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3991 it->dpvec = v->contents;
3992 it->dpend = v->contents + v->header.size;
3994 else
3996 /* Default `...'. */
3997 it->dpvec = default_invis_vector;
3998 it->dpend = default_invis_vector + 3;
4001 it->dpvec_char_len = len;
4002 it->current.dpvec_index = 0;
4003 it->dpvec_face_id = -1;
4005 /* Remember the current face id in case glyphs specify faces.
4006 IT's face is restored in set_iterator_to_next.
4007 saved_face_id was set to preceding char's face in handle_stop. */
4008 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4009 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4011 it->method = GET_FROM_DISPLAY_VECTOR;
4012 it->ellipsis_p = 1;
4017 /***********************************************************************
4018 'display' property
4019 ***********************************************************************/
4021 /* Set up iterator IT from `display' property at its current position.
4022 Called from handle_stop.
4023 We return HANDLED_RETURN if some part of the display property
4024 overrides the display of the buffer text itself.
4025 Otherwise we return HANDLED_NORMALLY. */
4027 static enum prop_handled
4028 handle_display_prop (struct it *it)
4030 Lisp_Object propval, object, overlay;
4031 struct text_pos *position;
4032 EMACS_INT bufpos;
4033 /* Nonzero if some property replaces the display of the text itself. */
4034 int display_replaced_p = 0;
4036 if (STRINGP (it->string))
4038 object = it->string;
4039 position = &it->current.string_pos;
4040 bufpos = CHARPOS (it->current.pos);
4042 else
4044 XSETWINDOW (object, it->w);
4045 position = &it->current.pos;
4046 bufpos = CHARPOS (*position);
4049 /* Reset those iterator values set from display property values. */
4050 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4051 it->space_width = Qnil;
4052 it->font_height = Qnil;
4053 it->voffset = 0;
4055 /* We don't support recursive `display' properties, i.e. string
4056 values that have a string `display' property, that have a string
4057 `display' property etc. */
4058 if (!it->string_from_display_prop_p)
4059 it->area = TEXT_AREA;
4061 propval = get_char_property_and_overlay (make_number (position->charpos),
4062 Qdisplay, object, &overlay);
4063 if (NILP (propval))
4064 return HANDLED_NORMALLY;
4065 /* Now OVERLAY is the overlay that gave us this property, or nil
4066 if it was a text property. */
4068 if (!STRINGP (it->string))
4069 object = it->w->buffer;
4071 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4072 position, bufpos,
4073 FRAME_WINDOW_P (it->f));
4075 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4078 /* Subroutine of handle_display_prop. Returns non-zero if the display
4079 specification in SPEC is a replacing specification, i.e. it would
4080 replace the text covered by `display' property with something else,
4081 such as an image or a display string.
4083 See handle_single_display_spec for documentation of arguments.
4084 frame_window_p is non-zero if the window being redisplayed is on a
4085 GUI frame; this argument is used only if IT is NULL, see below.
4087 IT can be NULL, if this is called by the bidi reordering code
4088 through compute_display_string_pos, which see. In that case, this
4089 function only examines SPEC, but does not otherwise "handle" it, in
4090 the sense that it doesn't set up members of IT from the display
4091 spec. */
4092 static int
4093 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4094 Lisp_Object overlay, struct text_pos *position,
4095 EMACS_INT bufpos, int frame_window_p)
4097 int replacing_p = 0;
4099 if (CONSP (spec)
4100 /* Simple specerties. */
4101 && !EQ (XCAR (spec), Qimage)
4102 && !EQ (XCAR (spec), Qspace)
4103 && !EQ (XCAR (spec), Qwhen)
4104 && !EQ (XCAR (spec), Qslice)
4105 && !EQ (XCAR (spec), Qspace_width)
4106 && !EQ (XCAR (spec), Qheight)
4107 && !EQ (XCAR (spec), Qraise)
4108 /* Marginal area specifications. */
4109 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4110 && !EQ (XCAR (spec), Qleft_fringe)
4111 && !EQ (XCAR (spec), Qright_fringe)
4112 && !NILP (XCAR (spec)))
4114 for (; CONSP (spec); spec = XCDR (spec))
4116 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4117 position, bufpos, replacing_p,
4118 frame_window_p))
4120 replacing_p = 1;
4121 /* If some text in a string is replaced, `position' no
4122 longer points to the position of `object'. */
4123 if (!it || STRINGP (object))
4124 break;
4128 else if (VECTORP (spec))
4130 int i;
4131 for (i = 0; i < ASIZE (spec); ++i)
4132 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4133 position, bufpos, replacing_p,
4134 frame_window_p))
4136 replacing_p = 1;
4137 /* If some text in a string is replaced, `position' no
4138 longer points to the position of `object'. */
4139 if (!it || STRINGP (object))
4140 break;
4143 else
4145 if (handle_single_display_spec (it, spec, object, overlay,
4146 position, bufpos, 0, frame_window_p))
4147 replacing_p = 1;
4150 return replacing_p;
4153 /* Value is the position of the end of the `display' property starting
4154 at START_POS in OBJECT. */
4156 static struct text_pos
4157 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4159 Lisp_Object end;
4160 struct text_pos end_pos;
4162 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4163 Qdisplay, object, Qnil);
4164 CHARPOS (end_pos) = XFASTINT (end);
4165 if (STRINGP (object))
4166 compute_string_pos (&end_pos, start_pos, it->string);
4167 else
4168 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4170 return end_pos;
4174 /* Set up IT from a single `display' property specification SPEC. OBJECT
4175 is the object in which the `display' property was found. *POSITION
4176 is the position in OBJECT at which the `display' property was found.
4177 BUFPOS is the buffer position of OBJECT (different from POSITION if
4178 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4179 previously saw a display specification which already replaced text
4180 display with something else, for example an image; we ignore such
4181 properties after the first one has been processed.
4183 OVERLAY is the overlay this `display' property came from,
4184 or nil if it was a text property.
4186 If SPEC is a `space' or `image' specification, and in some other
4187 cases too, set *POSITION to the position where the `display'
4188 property ends.
4190 If IT is NULL, only examine the property specification in SPEC, but
4191 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4192 is intended to be displayed in a window on a GUI frame.
4194 Value is non-zero if something was found which replaces the display
4195 of buffer or string text. */
4197 static int
4198 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4199 Lisp_Object overlay, struct text_pos *position,
4200 EMACS_INT bufpos, int display_replaced_p,
4201 int frame_window_p)
4203 Lisp_Object form;
4204 Lisp_Object location, value;
4205 struct text_pos start_pos = *position;
4206 int valid_p;
4208 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4209 If the result is non-nil, use VALUE instead of SPEC. */
4210 form = Qt;
4211 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4213 spec = XCDR (spec);
4214 if (!CONSP (spec))
4215 return 0;
4216 form = XCAR (spec);
4217 spec = XCDR (spec);
4220 if (!NILP (form) && !EQ (form, Qt))
4222 int count = SPECPDL_INDEX ();
4223 struct gcpro gcpro1;
4225 /* Bind `object' to the object having the `display' property, a
4226 buffer or string. Bind `position' to the position in the
4227 object where the property was found, and `buffer-position'
4228 to the current position in the buffer. */
4230 if (NILP (object))
4231 XSETBUFFER (object, current_buffer);
4232 specbind (Qobject, object);
4233 specbind (Qposition, make_number (CHARPOS (*position)));
4234 specbind (Qbuffer_position, make_number (bufpos));
4235 GCPRO1 (form);
4236 form = safe_eval (form);
4237 UNGCPRO;
4238 unbind_to (count, Qnil);
4241 if (NILP (form))
4242 return 0;
4244 /* Handle `(height HEIGHT)' specifications. */
4245 if (CONSP (spec)
4246 && EQ (XCAR (spec), Qheight)
4247 && CONSP (XCDR (spec)))
4249 if (it)
4251 if (!FRAME_WINDOW_P (it->f))
4252 return 0;
4254 it->font_height = XCAR (XCDR (spec));
4255 if (!NILP (it->font_height))
4257 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4258 int new_height = -1;
4260 if (CONSP (it->font_height)
4261 && (EQ (XCAR (it->font_height), Qplus)
4262 || EQ (XCAR (it->font_height), Qminus))
4263 && CONSP (XCDR (it->font_height))
4264 && INTEGERP (XCAR (XCDR (it->font_height))))
4266 /* `(+ N)' or `(- N)' where N is an integer. */
4267 int steps = XINT (XCAR (XCDR (it->font_height)));
4268 if (EQ (XCAR (it->font_height), Qplus))
4269 steps = - steps;
4270 it->face_id = smaller_face (it->f, it->face_id, steps);
4272 else if (FUNCTIONP (it->font_height))
4274 /* Call function with current height as argument.
4275 Value is the new height. */
4276 Lisp_Object height;
4277 height = safe_call1 (it->font_height,
4278 face->lface[LFACE_HEIGHT_INDEX]);
4279 if (NUMBERP (height))
4280 new_height = XFLOATINT (height);
4282 else if (NUMBERP (it->font_height))
4284 /* Value is a multiple of the canonical char height. */
4285 struct face *f;
4287 f = FACE_FROM_ID (it->f,
4288 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4289 new_height = (XFLOATINT (it->font_height)
4290 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4292 else
4294 /* Evaluate IT->font_height with `height' bound to the
4295 current specified height to get the new height. */
4296 int count = SPECPDL_INDEX ();
4298 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4299 value = safe_eval (it->font_height);
4300 unbind_to (count, Qnil);
4302 if (NUMBERP (value))
4303 new_height = XFLOATINT (value);
4306 if (new_height > 0)
4307 it->face_id = face_with_height (it->f, it->face_id, new_height);
4311 return 0;
4314 /* Handle `(space-width WIDTH)'. */
4315 if (CONSP (spec)
4316 && EQ (XCAR (spec), Qspace_width)
4317 && CONSP (XCDR (spec)))
4319 if (it)
4321 if (!FRAME_WINDOW_P (it->f))
4322 return 0;
4324 value = XCAR (XCDR (spec));
4325 if (NUMBERP (value) && XFLOATINT (value) > 0)
4326 it->space_width = value;
4329 return 0;
4332 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4333 if (CONSP (spec)
4334 && EQ (XCAR (spec), Qslice))
4336 Lisp_Object tem;
4338 if (it)
4340 if (!FRAME_WINDOW_P (it->f))
4341 return 0;
4343 if (tem = XCDR (spec), CONSP (tem))
4345 it->slice.x = XCAR (tem);
4346 if (tem = XCDR (tem), CONSP (tem))
4348 it->slice.y = XCAR (tem);
4349 if (tem = XCDR (tem), CONSP (tem))
4351 it->slice.width = XCAR (tem);
4352 if (tem = XCDR (tem), CONSP (tem))
4353 it->slice.height = XCAR (tem);
4359 return 0;
4362 /* Handle `(raise FACTOR)'. */
4363 if (CONSP (spec)
4364 && EQ (XCAR (spec), Qraise)
4365 && CONSP (XCDR (spec)))
4367 if (it)
4369 if (!FRAME_WINDOW_P (it->f))
4370 return 0;
4372 #ifdef HAVE_WINDOW_SYSTEM
4373 value = XCAR (XCDR (spec));
4374 if (NUMBERP (value))
4376 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4377 it->voffset = - (XFLOATINT (value)
4378 * (FONT_HEIGHT (face->font)));
4380 #endif /* HAVE_WINDOW_SYSTEM */
4383 return 0;
4386 /* Don't handle the other kinds of display specifications
4387 inside a string that we got from a `display' property. */
4388 if (it && it->string_from_display_prop_p)
4389 return 0;
4391 /* Characters having this form of property are not displayed, so
4392 we have to find the end of the property. */
4393 if (it)
4395 start_pos = *position;
4396 *position = display_prop_end (it, object, start_pos);
4398 value = Qnil;
4400 /* Stop the scan at that end position--we assume that all
4401 text properties change there. */
4402 if (it)
4403 it->stop_charpos = position->charpos;
4405 /* Handle `(left-fringe BITMAP [FACE])'
4406 and `(right-fringe BITMAP [FACE])'. */
4407 if (CONSP (spec)
4408 && (EQ (XCAR (spec), Qleft_fringe)
4409 || EQ (XCAR (spec), Qright_fringe))
4410 && CONSP (XCDR (spec)))
4412 int fringe_bitmap;
4414 if (it)
4416 if (!FRAME_WINDOW_P (it->f))
4417 /* If we return here, POSITION has been advanced
4418 across the text with this property. */
4419 return 0;
4421 else if (!frame_window_p)
4422 return 0;
4424 #ifdef HAVE_WINDOW_SYSTEM
4425 value = XCAR (XCDR (spec));
4426 if (!SYMBOLP (value)
4427 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4428 /* If we return here, POSITION has been advanced
4429 across the text with this property. */
4430 return 0;
4432 if (it)
4434 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4436 if (CONSP (XCDR (XCDR (spec))))
4438 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4439 int face_id2 = lookup_derived_face (it->f, face_name,
4440 FRINGE_FACE_ID, 0);
4441 if (face_id2 >= 0)
4442 face_id = face_id2;
4445 /* Save current settings of IT so that we can restore them
4446 when we are finished with the glyph property value. */
4447 push_it (it, position);
4449 it->area = TEXT_AREA;
4450 it->what = IT_IMAGE;
4451 it->image_id = -1; /* no image */
4452 it->position = start_pos;
4453 it->object = NILP (object) ? it->w->buffer : object;
4454 it->method = GET_FROM_IMAGE;
4455 it->from_overlay = Qnil;
4456 it->face_id = face_id;
4457 it->from_disp_prop_p = 1;
4459 /* Say that we haven't consumed the characters with
4460 `display' property yet. The call to pop_it in
4461 set_iterator_to_next will clean this up. */
4462 *position = start_pos;
4464 if (EQ (XCAR (spec), Qleft_fringe))
4466 it->left_user_fringe_bitmap = fringe_bitmap;
4467 it->left_user_fringe_face_id = face_id;
4469 else
4471 it->right_user_fringe_bitmap = fringe_bitmap;
4472 it->right_user_fringe_face_id = face_id;
4475 #endif /* HAVE_WINDOW_SYSTEM */
4476 return 1;
4479 /* Prepare to handle `((margin left-margin) ...)',
4480 `((margin right-margin) ...)' and `((margin nil) ...)'
4481 prefixes for display specifications. */
4482 location = Qunbound;
4483 if (CONSP (spec) && CONSP (XCAR (spec)))
4485 Lisp_Object tem;
4487 value = XCDR (spec);
4488 if (CONSP (value))
4489 value = XCAR (value);
4491 tem = XCAR (spec);
4492 if (EQ (XCAR (tem), Qmargin)
4493 && (tem = XCDR (tem),
4494 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4495 (NILP (tem)
4496 || EQ (tem, Qleft_margin)
4497 || EQ (tem, Qright_margin))))
4498 location = tem;
4501 if (EQ (location, Qunbound))
4503 location = Qnil;
4504 value = spec;
4507 /* After this point, VALUE is the property after any
4508 margin prefix has been stripped. It must be a string,
4509 an image specification, or `(space ...)'.
4511 LOCATION specifies where to display: `left-margin',
4512 `right-margin' or nil. */
4514 valid_p = (STRINGP (value)
4515 #ifdef HAVE_WINDOW_SYSTEM
4516 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4517 && valid_image_p (value))
4518 #endif /* not HAVE_WINDOW_SYSTEM */
4519 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4521 if (valid_p && !display_replaced_p)
4523 if (!it)
4524 return 1;
4526 /* Save current settings of IT so that we can restore them
4527 when we are finished with the glyph property value. */
4528 push_it (it, position);
4529 it->from_overlay = overlay;
4530 it->from_disp_prop_p = 1;
4532 if (NILP (location))
4533 it->area = TEXT_AREA;
4534 else if (EQ (location, Qleft_margin))
4535 it->area = LEFT_MARGIN_AREA;
4536 else
4537 it->area = RIGHT_MARGIN_AREA;
4539 if (STRINGP (value))
4541 it->string = value;
4542 it->multibyte_p = STRING_MULTIBYTE (it->string);
4543 it->current.overlay_string_index = -1;
4544 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4545 it->end_charpos = it->string_nchars = SCHARS (it->string);
4546 it->method = GET_FROM_STRING;
4547 it->stop_charpos = 0;
4548 it->prev_stop = 0;
4549 it->base_level_stop = 0;
4550 it->string_from_display_prop_p = 1;
4551 /* Say that we haven't consumed the characters with
4552 `display' property yet. The call to pop_it in
4553 set_iterator_to_next will clean this up. */
4554 if (BUFFERP (object))
4555 *position = start_pos;
4557 /* Force paragraph direction to be that of the parent
4558 object. If the parent object's paragraph direction is
4559 not yet determined, default to L2R. */
4560 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4561 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4562 else
4563 it->paragraph_embedding = L2R;
4565 /* Set up the bidi iterator for this display string. */
4566 if (it->bidi_p)
4568 it->bidi_it.string.lstring = it->string;
4569 it->bidi_it.string.s = NULL;
4570 it->bidi_it.string.schars = it->end_charpos;
4571 it->bidi_it.string.bufpos = bufpos;
4572 it->bidi_it.string.from_disp_str = 1;
4573 it->bidi_it.string.unibyte = !it->multibyte_p;
4574 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4577 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4579 it->method = GET_FROM_STRETCH;
4580 it->object = value;
4581 *position = it->position = start_pos;
4583 #ifdef HAVE_WINDOW_SYSTEM
4584 else
4586 it->what = IT_IMAGE;
4587 it->image_id = lookup_image (it->f, value);
4588 it->position = start_pos;
4589 it->object = NILP (object) ? it->w->buffer : object;
4590 it->method = GET_FROM_IMAGE;
4592 /* Say that we haven't consumed the characters with
4593 `display' property yet. The call to pop_it in
4594 set_iterator_to_next will clean this up. */
4595 *position = start_pos;
4597 #endif /* HAVE_WINDOW_SYSTEM */
4599 return 1;
4602 /* Invalid property or property not supported. Restore
4603 POSITION to what it was before. */
4604 *position = start_pos;
4605 return 0;
4608 /* Check if PROP is a display property value whose text should be
4609 treated as intangible. OVERLAY is the overlay from which PROP
4610 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4611 specify the buffer position covered by PROP. */
4614 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4615 EMACS_INT charpos, EMACS_INT bytepos)
4617 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4618 struct text_pos position;
4620 SET_TEXT_POS (position, charpos, bytepos);
4621 return handle_display_spec (NULL, prop, Qnil, overlay,
4622 &position, charpos, frame_window_p);
4626 /* Return 1 if PROP is a display sub-property value containing STRING.
4628 Implementation note: this and the following function are really
4629 special cases of handle_display_spec and
4630 handle_single_display_spec, and should ideally use the same code.
4631 Until they do, these two pairs must be consistent and must be
4632 modified in sync. */
4634 static int
4635 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4637 if (EQ (string, prop))
4638 return 1;
4640 /* Skip over `when FORM'. */
4641 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4643 prop = XCDR (prop);
4644 if (!CONSP (prop))
4645 return 0;
4646 /* Actually, the condition following `when' should be eval'ed,
4647 like handle_single_display_spec does, and we should return
4648 zero if it evaluates to nil. However, this function is
4649 called only when the buffer was already displayed and some
4650 glyph in the glyph matrix was found to come from a display
4651 string. Therefore, the condition was already evaluated, and
4652 the result was non-nil, otherwise the display string wouldn't
4653 have been displayed and we would have never been called for
4654 this property. Thus, we can skip the evaluation and assume
4655 its result is non-nil. */
4656 prop = XCDR (prop);
4659 if (CONSP (prop))
4660 /* Skip over `margin LOCATION'. */
4661 if (EQ (XCAR (prop), Qmargin))
4663 prop = XCDR (prop);
4664 if (!CONSP (prop))
4665 return 0;
4667 prop = XCDR (prop);
4668 if (!CONSP (prop))
4669 return 0;
4672 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4676 /* Return 1 if STRING appears in the `display' property PROP. */
4678 static int
4679 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4681 if (CONSP (prop)
4682 && !EQ (XCAR (prop), Qwhen)
4683 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4685 /* A list of sub-properties. */
4686 while (CONSP (prop))
4688 if (single_display_spec_string_p (XCAR (prop), string))
4689 return 1;
4690 prop = XCDR (prop);
4693 else if (VECTORP (prop))
4695 /* A vector of sub-properties. */
4696 int i;
4697 for (i = 0; i < ASIZE (prop); ++i)
4698 if (single_display_spec_string_p (AREF (prop, i), string))
4699 return 1;
4701 else
4702 return single_display_spec_string_p (prop, string);
4704 return 0;
4707 /* Look for STRING in overlays and text properties in the current
4708 buffer, between character positions FROM and TO (excluding TO).
4709 BACK_P non-zero means look back (in this case, TO is supposed to be
4710 less than FROM).
4711 Value is the first character position where STRING was found, or
4712 zero if it wasn't found before hitting TO.
4714 This function may only use code that doesn't eval because it is
4715 called asynchronously from note_mouse_highlight. */
4717 static EMACS_INT
4718 string_buffer_position_lim (Lisp_Object string,
4719 EMACS_INT from, EMACS_INT to, int back_p)
4721 Lisp_Object limit, prop, pos;
4722 int found = 0;
4724 pos = make_number (from);
4726 if (!back_p) /* looking forward */
4728 limit = make_number (min (to, ZV));
4729 while (!found && !EQ (pos, limit))
4731 prop = Fget_char_property (pos, Qdisplay, Qnil);
4732 if (!NILP (prop) && display_prop_string_p (prop, string))
4733 found = 1;
4734 else
4735 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4736 limit);
4739 else /* looking back */
4741 limit = make_number (max (to, BEGV));
4742 while (!found && !EQ (pos, limit))
4744 prop = Fget_char_property (pos, Qdisplay, Qnil);
4745 if (!NILP (prop) && display_prop_string_p (prop, string))
4746 found = 1;
4747 else
4748 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4749 limit);
4753 return found ? XINT (pos) : 0;
4756 /* Determine which buffer position in current buffer STRING comes from.
4757 AROUND_CHARPOS is an approximate position where it could come from.
4758 Value is the buffer position or 0 if it couldn't be determined.
4760 This function is necessary because we don't record buffer positions
4761 in glyphs generated from strings (to keep struct glyph small).
4762 This function may only use code that doesn't eval because it is
4763 called asynchronously from note_mouse_highlight. */
4765 static EMACS_INT
4766 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4768 const int MAX_DISTANCE = 1000;
4769 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4770 around_charpos + MAX_DISTANCE,
4773 if (!found)
4774 found = string_buffer_position_lim (string, around_charpos,
4775 around_charpos - MAX_DISTANCE, 1);
4776 return found;
4781 /***********************************************************************
4782 `composition' property
4783 ***********************************************************************/
4785 /* Set up iterator IT from `composition' property at its current
4786 position. Called from handle_stop. */
4788 static enum prop_handled
4789 handle_composition_prop (struct it *it)
4791 Lisp_Object prop, string;
4792 EMACS_INT pos, pos_byte, start, end;
4794 if (STRINGP (it->string))
4796 unsigned char *s;
4798 pos = IT_STRING_CHARPOS (*it);
4799 pos_byte = IT_STRING_BYTEPOS (*it);
4800 string = it->string;
4801 s = SDATA (string) + pos_byte;
4802 it->c = STRING_CHAR (s);
4804 else
4806 pos = IT_CHARPOS (*it);
4807 pos_byte = IT_BYTEPOS (*it);
4808 string = Qnil;
4809 it->c = FETCH_CHAR (pos_byte);
4812 /* If there's a valid composition and point is not inside of the
4813 composition (in the case that the composition is from the current
4814 buffer), draw a glyph composed from the composition components. */
4815 if (find_composition (pos, -1, &start, &end, &prop, string)
4816 && COMPOSITION_VALID_P (start, end, prop)
4817 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4819 if (start < pos)
4820 /* As we can't handle this situation (perhaps font-lock added
4821 a new composition), we just return here hoping that next
4822 redisplay will detect this composition much earlier. */
4823 return HANDLED_NORMALLY;
4824 if (start != pos)
4826 if (STRINGP (it->string))
4827 pos_byte = string_char_to_byte (it->string, start);
4828 else
4829 pos_byte = CHAR_TO_BYTE (start);
4831 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4832 prop, string);
4834 if (it->cmp_it.id >= 0)
4836 it->cmp_it.ch = -1;
4837 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4838 it->cmp_it.nglyphs = -1;
4842 return HANDLED_NORMALLY;
4847 /***********************************************************************
4848 Overlay strings
4849 ***********************************************************************/
4851 /* The following structure is used to record overlay strings for
4852 later sorting in load_overlay_strings. */
4854 struct overlay_entry
4856 Lisp_Object overlay;
4857 Lisp_Object string;
4858 int priority;
4859 int after_string_p;
4863 /* Set up iterator IT from overlay strings at its current position.
4864 Called from handle_stop. */
4866 static enum prop_handled
4867 handle_overlay_change (struct it *it)
4869 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4870 return HANDLED_RECOMPUTE_PROPS;
4871 else
4872 return HANDLED_NORMALLY;
4876 /* Set up the next overlay string for delivery by IT, if there is an
4877 overlay string to deliver. Called by set_iterator_to_next when the
4878 end of the current overlay string is reached. If there are more
4879 overlay strings to display, IT->string and
4880 IT->current.overlay_string_index are set appropriately here.
4881 Otherwise IT->string is set to nil. */
4883 static void
4884 next_overlay_string (struct it *it)
4886 ++it->current.overlay_string_index;
4887 if (it->current.overlay_string_index == it->n_overlay_strings)
4889 /* No more overlay strings. Restore IT's settings to what
4890 they were before overlay strings were processed, and
4891 continue to deliver from current_buffer. */
4893 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4894 pop_it (it);
4895 xassert (it->sp > 0
4896 || (NILP (it->string)
4897 && it->method == GET_FROM_BUFFER
4898 && it->stop_charpos >= BEGV
4899 && it->stop_charpos <= it->end_charpos));
4900 it->current.overlay_string_index = -1;
4901 it->n_overlay_strings = 0;
4902 it->overlay_strings_charpos = -1;
4904 /* If we're at the end of the buffer, record that we have
4905 processed the overlay strings there already, so that
4906 next_element_from_buffer doesn't try it again. */
4907 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4908 it->overlay_strings_at_end_processed_p = 1;
4910 else
4912 /* There are more overlay strings to process. If
4913 IT->current.overlay_string_index has advanced to a position
4914 where we must load IT->overlay_strings with more strings, do
4915 it. We must load at the IT->overlay_strings_charpos where
4916 IT->n_overlay_strings was originally computed; when invisible
4917 text is present, this might not be IT_CHARPOS (Bug#7016). */
4918 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4920 if (it->current.overlay_string_index && i == 0)
4921 load_overlay_strings (it, it->overlay_strings_charpos);
4923 /* Initialize IT to deliver display elements from the overlay
4924 string. */
4925 it->string = it->overlay_strings[i];
4926 it->multibyte_p = STRING_MULTIBYTE (it->string);
4927 SET_TEXT_POS (it->current.string_pos, 0, 0);
4928 it->method = GET_FROM_STRING;
4929 it->stop_charpos = 0;
4930 if (it->cmp_it.stop_pos >= 0)
4931 it->cmp_it.stop_pos = 0;
4932 it->prev_stop = 0;
4933 it->base_level_stop = 0;
4935 /* Set up the bidi iterator for this overlay string. */
4936 if (it->bidi_p)
4938 it->bidi_it.string.lstring = it->string;
4939 it->bidi_it.string.s = NULL;
4940 it->bidi_it.string.schars = SCHARS (it->string);
4941 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4942 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4943 it->bidi_it.string.unibyte = !it->multibyte_p;
4944 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4948 CHECK_IT (it);
4952 /* Compare two overlay_entry structures E1 and E2. Used as a
4953 comparison function for qsort in load_overlay_strings. Overlay
4954 strings for the same position are sorted so that
4956 1. All after-strings come in front of before-strings, except
4957 when they come from the same overlay.
4959 2. Within after-strings, strings are sorted so that overlay strings
4960 from overlays with higher priorities come first.
4962 2. Within before-strings, strings are sorted so that overlay
4963 strings from overlays with higher priorities come last.
4965 Value is analogous to strcmp. */
4968 static int
4969 compare_overlay_entries (const void *e1, const void *e2)
4971 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4972 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4973 int result;
4975 if (entry1->after_string_p != entry2->after_string_p)
4977 /* Let after-strings appear in front of before-strings if
4978 they come from different overlays. */
4979 if (EQ (entry1->overlay, entry2->overlay))
4980 result = entry1->after_string_p ? 1 : -1;
4981 else
4982 result = entry1->after_string_p ? -1 : 1;
4984 else if (entry1->after_string_p)
4985 /* After-strings sorted in order of decreasing priority. */
4986 result = entry2->priority - entry1->priority;
4987 else
4988 /* Before-strings sorted in order of increasing priority. */
4989 result = entry1->priority - entry2->priority;
4991 return result;
4995 /* Load the vector IT->overlay_strings with overlay strings from IT's
4996 current buffer position, or from CHARPOS if that is > 0. Set
4997 IT->n_overlays to the total number of overlay strings found.
4999 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5000 a time. On entry into load_overlay_strings,
5001 IT->current.overlay_string_index gives the number of overlay
5002 strings that have already been loaded by previous calls to this
5003 function.
5005 IT->add_overlay_start contains an additional overlay start
5006 position to consider for taking overlay strings from, if non-zero.
5007 This position comes into play when the overlay has an `invisible'
5008 property, and both before and after-strings. When we've skipped to
5009 the end of the overlay, because of its `invisible' property, we
5010 nevertheless want its before-string to appear.
5011 IT->add_overlay_start will contain the overlay start position
5012 in this case.
5014 Overlay strings are sorted so that after-string strings come in
5015 front of before-string strings. Within before and after-strings,
5016 strings are sorted by overlay priority. See also function
5017 compare_overlay_entries. */
5019 static void
5020 load_overlay_strings (struct it *it, EMACS_INT charpos)
5022 Lisp_Object overlay, window, str, invisible;
5023 struct Lisp_Overlay *ov;
5024 EMACS_INT start, end;
5025 int size = 20;
5026 int n = 0, i, j, invis_p;
5027 struct overlay_entry *entries
5028 = (struct overlay_entry *) alloca (size * sizeof *entries);
5030 if (charpos <= 0)
5031 charpos = IT_CHARPOS (*it);
5033 /* Append the overlay string STRING of overlay OVERLAY to vector
5034 `entries' which has size `size' and currently contains `n'
5035 elements. AFTER_P non-zero means STRING is an after-string of
5036 OVERLAY. */
5037 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5038 do \
5040 Lisp_Object priority; \
5042 if (n == size) \
5044 int new_size = 2 * size; \
5045 struct overlay_entry *old = entries; \
5046 entries = \
5047 (struct overlay_entry *) alloca (new_size \
5048 * sizeof *entries); \
5049 memcpy (entries, old, size * sizeof *entries); \
5050 size = new_size; \
5053 entries[n].string = (STRING); \
5054 entries[n].overlay = (OVERLAY); \
5055 priority = Foverlay_get ((OVERLAY), Qpriority); \
5056 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5057 entries[n].after_string_p = (AFTER_P); \
5058 ++n; \
5060 while (0)
5062 /* Process overlay before the overlay center. */
5063 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5065 XSETMISC (overlay, ov);
5066 xassert (OVERLAYP (overlay));
5067 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5068 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5070 if (end < charpos)
5071 break;
5073 /* Skip this overlay if it doesn't start or end at IT's current
5074 position. */
5075 if (end != charpos && start != charpos)
5076 continue;
5078 /* Skip this overlay if it doesn't apply to IT->w. */
5079 window = Foverlay_get (overlay, Qwindow);
5080 if (WINDOWP (window) && XWINDOW (window) != it->w)
5081 continue;
5083 /* If the text ``under'' the overlay is invisible, both before-
5084 and after-strings from this overlay are visible; start and
5085 end position are indistinguishable. */
5086 invisible = Foverlay_get (overlay, Qinvisible);
5087 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5089 /* If overlay has a non-empty before-string, record it. */
5090 if ((start == charpos || (end == charpos && invis_p))
5091 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5092 && SCHARS (str))
5093 RECORD_OVERLAY_STRING (overlay, str, 0);
5095 /* If overlay has a non-empty after-string, record it. */
5096 if ((end == charpos || (start == charpos && invis_p))
5097 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5098 && SCHARS (str))
5099 RECORD_OVERLAY_STRING (overlay, str, 1);
5102 /* Process overlays after the overlay center. */
5103 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5105 XSETMISC (overlay, ov);
5106 xassert (OVERLAYP (overlay));
5107 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5108 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5110 if (start > charpos)
5111 break;
5113 /* Skip this overlay if it doesn't start or end at IT's current
5114 position. */
5115 if (end != charpos && start != charpos)
5116 continue;
5118 /* Skip this overlay if it doesn't apply to IT->w. */
5119 window = Foverlay_get (overlay, Qwindow);
5120 if (WINDOWP (window) && XWINDOW (window) != it->w)
5121 continue;
5123 /* If the text ``under'' the overlay is invisible, it has a zero
5124 dimension, and both before- and after-strings apply. */
5125 invisible = Foverlay_get (overlay, Qinvisible);
5126 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5128 /* If overlay has a non-empty before-string, record it. */
5129 if ((start == charpos || (end == charpos && invis_p))
5130 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5131 && SCHARS (str))
5132 RECORD_OVERLAY_STRING (overlay, str, 0);
5134 /* If overlay has a non-empty after-string, record it. */
5135 if ((end == charpos || (start == charpos && invis_p))
5136 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5137 && SCHARS (str))
5138 RECORD_OVERLAY_STRING (overlay, str, 1);
5141 #undef RECORD_OVERLAY_STRING
5143 /* Sort entries. */
5144 if (n > 1)
5145 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5147 /* Record number of overlay strings, and where we computed it. */
5148 it->n_overlay_strings = n;
5149 it->overlay_strings_charpos = charpos;
5151 /* IT->current.overlay_string_index is the number of overlay strings
5152 that have already been consumed by IT. Copy some of the
5153 remaining overlay strings to IT->overlay_strings. */
5154 i = 0;
5155 j = it->current.overlay_string_index;
5156 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5158 it->overlay_strings[i] = entries[j].string;
5159 it->string_overlays[i++] = entries[j++].overlay;
5162 CHECK_IT (it);
5166 /* Get the first chunk of overlay strings at IT's current buffer
5167 position, or at CHARPOS if that is > 0. Value is non-zero if at
5168 least one overlay string was found. */
5170 static int
5171 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5173 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5174 process. This fills IT->overlay_strings with strings, and sets
5175 IT->n_overlay_strings to the total number of strings to process.
5176 IT->pos.overlay_string_index has to be set temporarily to zero
5177 because load_overlay_strings needs this; it must be set to -1
5178 when no overlay strings are found because a zero value would
5179 indicate a position in the first overlay string. */
5180 it->current.overlay_string_index = 0;
5181 load_overlay_strings (it, charpos);
5183 /* If we found overlay strings, set up IT to deliver display
5184 elements from the first one. Otherwise set up IT to deliver
5185 from current_buffer. */
5186 if (it->n_overlay_strings)
5188 /* Make sure we know settings in current_buffer, so that we can
5189 restore meaningful values when we're done with the overlay
5190 strings. */
5191 if (compute_stop_p)
5192 compute_stop_pos (it);
5193 xassert (it->face_id >= 0);
5195 /* Save IT's settings. They are restored after all overlay
5196 strings have been processed. */
5197 xassert (!compute_stop_p || it->sp == 0);
5199 /* When called from handle_stop, there might be an empty display
5200 string loaded. In that case, don't bother saving it. */
5201 if (!STRINGP (it->string) || SCHARS (it->string))
5202 push_it (it, NULL);
5204 /* Set up IT to deliver display elements from the first overlay
5205 string. */
5206 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5207 it->string = it->overlay_strings[0];
5208 it->from_overlay = Qnil;
5209 it->stop_charpos = 0;
5210 xassert (STRINGP (it->string));
5211 it->end_charpos = SCHARS (it->string);
5212 it->prev_stop = 0;
5213 it->base_level_stop = 0;
5214 it->multibyte_p = STRING_MULTIBYTE (it->string);
5215 it->method = GET_FROM_STRING;
5216 it->from_disp_prop_p = 0;
5218 /* Force paragraph direction to be that of the parent
5219 buffer. */
5220 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5221 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5222 else
5223 it->paragraph_embedding = L2R;
5225 /* Set up the bidi iterator for this overlay string. */
5226 if (it->bidi_p)
5228 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5230 it->bidi_it.string.lstring = it->string;
5231 it->bidi_it.string.s = NULL;
5232 it->bidi_it.string.schars = SCHARS (it->string);
5233 it->bidi_it.string.bufpos = pos;
5234 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5235 it->bidi_it.string.unibyte = !it->multibyte_p;
5236 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5238 return 1;
5241 it->current.overlay_string_index = -1;
5242 return 0;
5245 static int
5246 get_overlay_strings (struct it *it, EMACS_INT charpos)
5248 it->string = Qnil;
5249 it->method = GET_FROM_BUFFER;
5251 (void) get_overlay_strings_1 (it, charpos, 1);
5253 CHECK_IT (it);
5255 /* Value is non-zero if we found at least one overlay string. */
5256 return STRINGP (it->string);
5261 /***********************************************************************
5262 Saving and restoring state
5263 ***********************************************************************/
5265 /* Save current settings of IT on IT->stack. Called, for example,
5266 before setting up IT for an overlay string, to be able to restore
5267 IT's settings to what they were after the overlay string has been
5268 processed. If POSITION is non-NULL, it is the position to save on
5269 the stack instead of IT->position. */
5271 static void
5272 push_it (struct it *it, struct text_pos *position)
5274 struct iterator_stack_entry *p;
5276 xassert (it->sp < IT_STACK_SIZE);
5277 p = it->stack + it->sp;
5279 p->stop_charpos = it->stop_charpos;
5280 p->prev_stop = it->prev_stop;
5281 p->base_level_stop = it->base_level_stop;
5282 p->cmp_it = it->cmp_it;
5283 xassert (it->face_id >= 0);
5284 p->face_id = it->face_id;
5285 p->string = it->string;
5286 p->method = it->method;
5287 p->from_overlay = it->from_overlay;
5288 switch (p->method)
5290 case GET_FROM_IMAGE:
5291 p->u.image.object = it->object;
5292 p->u.image.image_id = it->image_id;
5293 p->u.image.slice = it->slice;
5294 break;
5295 case GET_FROM_STRETCH:
5296 p->u.stretch.object = it->object;
5297 break;
5299 p->position = position ? *position : it->position;
5300 p->current = it->current;
5301 p->end_charpos = it->end_charpos;
5302 p->string_nchars = it->string_nchars;
5303 p->area = it->area;
5304 p->multibyte_p = it->multibyte_p;
5305 p->avoid_cursor_p = it->avoid_cursor_p;
5306 p->space_width = it->space_width;
5307 p->font_height = it->font_height;
5308 p->voffset = it->voffset;
5309 p->string_from_display_prop_p = it->string_from_display_prop_p;
5310 p->display_ellipsis_p = 0;
5311 p->line_wrap = it->line_wrap;
5312 p->bidi_p = it->bidi_p;
5313 p->paragraph_embedding = it->paragraph_embedding;
5314 p->from_disp_prop_p = it->from_disp_prop_p;
5315 ++it->sp;
5317 /* Save the state of the bidi iterator as well. */
5318 if (it->bidi_p)
5319 bidi_push_it (&it->bidi_it);
5322 static void
5323 iterate_out_of_display_property (struct it *it)
5325 int buffer_p = BUFFERP (it->object);
5326 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5327 EMACS_INT bob = (buffer_p ? BEGV : 0);
5329 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5331 /* Maybe initialize paragraph direction. If we are at the beginning
5332 of a new paragraph, next_element_from_buffer may not have a
5333 chance to do that. */
5334 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5335 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5336 /* prev_stop can be zero, so check against BEGV as well. */
5337 while (it->bidi_it.charpos >= bob
5338 && it->prev_stop <= it->bidi_it.charpos
5339 && it->bidi_it.charpos < CHARPOS (it->position)
5340 && it->bidi_it.charpos < eob)
5341 bidi_move_to_visually_next (&it->bidi_it);
5342 /* Record the stop_pos we just crossed, for when we cross it
5343 back, maybe. */
5344 if (it->bidi_it.charpos > CHARPOS (it->position))
5345 it->prev_stop = CHARPOS (it->position);
5346 /* If we ended up not where pop_it put us, resync IT's
5347 positional members with the bidi iterator. */
5348 if (it->bidi_it.charpos != CHARPOS (it->position))
5349 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5350 if (buffer_p)
5351 it->current.pos = it->position;
5352 else
5353 it->current.string_pos = it->position;
5356 /* Restore IT's settings from IT->stack. Called, for example, when no
5357 more overlay strings must be processed, and we return to delivering
5358 display elements from a buffer, or when the end of a string from a
5359 `display' property is reached and we return to delivering display
5360 elements from an overlay string, or from a buffer. */
5362 static void
5363 pop_it (struct it *it)
5365 struct iterator_stack_entry *p;
5366 int from_display_prop = it->from_disp_prop_p;
5368 xassert (it->sp > 0);
5369 --it->sp;
5370 p = it->stack + it->sp;
5371 it->stop_charpos = p->stop_charpos;
5372 it->prev_stop = p->prev_stop;
5373 it->base_level_stop = p->base_level_stop;
5374 it->cmp_it = p->cmp_it;
5375 it->face_id = p->face_id;
5376 it->current = p->current;
5377 it->position = p->position;
5378 it->string = p->string;
5379 it->from_overlay = p->from_overlay;
5380 if (NILP (it->string))
5381 SET_TEXT_POS (it->current.string_pos, -1, -1);
5382 it->method = p->method;
5383 switch (it->method)
5385 case GET_FROM_IMAGE:
5386 it->image_id = p->u.image.image_id;
5387 it->object = p->u.image.object;
5388 it->slice = p->u.image.slice;
5389 break;
5390 case GET_FROM_STRETCH:
5391 it->object = p->u.stretch.object;
5392 break;
5393 case GET_FROM_BUFFER:
5394 it->object = it->w->buffer;
5395 break;
5396 case GET_FROM_STRING:
5397 it->object = it->string;
5398 break;
5399 case GET_FROM_DISPLAY_VECTOR:
5400 if (it->s)
5401 it->method = GET_FROM_C_STRING;
5402 else if (STRINGP (it->string))
5403 it->method = GET_FROM_STRING;
5404 else
5406 it->method = GET_FROM_BUFFER;
5407 it->object = it->w->buffer;
5410 it->end_charpos = p->end_charpos;
5411 it->string_nchars = p->string_nchars;
5412 it->area = p->area;
5413 it->multibyte_p = p->multibyte_p;
5414 it->avoid_cursor_p = p->avoid_cursor_p;
5415 it->space_width = p->space_width;
5416 it->font_height = p->font_height;
5417 it->voffset = p->voffset;
5418 it->string_from_display_prop_p = p->string_from_display_prop_p;
5419 it->line_wrap = p->line_wrap;
5420 it->bidi_p = p->bidi_p;
5421 it->paragraph_embedding = p->paragraph_embedding;
5422 it->from_disp_prop_p = p->from_disp_prop_p;
5423 if (it->bidi_p)
5425 bidi_pop_it (&it->bidi_it);
5426 /* Bidi-iterate until we get out of the portion of text, if any,
5427 covered by a `display' text property or by an overlay with
5428 `display' property. (We cannot just jump there, because the
5429 internal coherency of the bidi iterator state can not be
5430 preserved across such jumps.) We also must determine the
5431 paragraph base direction if the overlay we just processed is
5432 at the beginning of a new paragraph. */
5433 if (from_display_prop
5434 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5435 iterate_out_of_display_property (it);
5437 xassert ((BUFFERP (it->object)
5438 && IT_CHARPOS (*it) == it->bidi_it.charpos
5439 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5440 || (STRINGP (it->object)
5441 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5442 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5448 /***********************************************************************
5449 Moving over lines
5450 ***********************************************************************/
5452 /* Set IT's current position to the previous line start. */
5454 static void
5455 back_to_previous_line_start (struct it *it)
5457 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5458 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5462 /* Move IT to the next line start.
5464 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5465 we skipped over part of the text (as opposed to moving the iterator
5466 continuously over the text). Otherwise, don't change the value
5467 of *SKIPPED_P.
5469 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5470 iterator on the newline, if it was found.
5472 Newlines may come from buffer text, overlay strings, or strings
5473 displayed via the `display' property. That's the reason we can't
5474 simply use find_next_newline_no_quit.
5476 Note that this function may not skip over invisible text that is so
5477 because of text properties and immediately follows a newline. If
5478 it would, function reseat_at_next_visible_line_start, when called
5479 from set_iterator_to_next, would effectively make invisible
5480 characters following a newline part of the wrong glyph row, which
5481 leads to wrong cursor motion. */
5483 static int
5484 forward_to_next_line_start (struct it *it, int *skipped_p,
5485 struct bidi_it *bidi_it_prev)
5487 EMACS_INT old_selective;
5488 int newline_found_p, n;
5489 const int MAX_NEWLINE_DISTANCE = 500;
5491 /* If already on a newline, just consume it to avoid unintended
5492 skipping over invisible text below. */
5493 if (it->what == IT_CHARACTER
5494 && it->c == '\n'
5495 && CHARPOS (it->position) == IT_CHARPOS (*it))
5497 if (it->bidi_p && bidi_it_prev)
5498 *bidi_it_prev = it->bidi_it;
5499 set_iterator_to_next (it, 0);
5500 it->c = 0;
5501 return 1;
5504 /* Don't handle selective display in the following. It's (a)
5505 unnecessary because it's done by the caller, and (b) leads to an
5506 infinite recursion because next_element_from_ellipsis indirectly
5507 calls this function. */
5508 old_selective = it->selective;
5509 it->selective = 0;
5511 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5512 from buffer text. */
5513 for (n = newline_found_p = 0;
5514 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5515 n += STRINGP (it->string) ? 0 : 1)
5517 if (!get_next_display_element (it))
5518 return 0;
5519 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5520 if (newline_found_p && it->bidi_p && bidi_it_prev)
5521 *bidi_it_prev = it->bidi_it;
5522 set_iterator_to_next (it, 0);
5525 /* If we didn't find a newline near enough, see if we can use a
5526 short-cut. */
5527 if (!newline_found_p)
5529 EMACS_INT start = IT_CHARPOS (*it);
5530 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5531 Lisp_Object pos;
5533 xassert (!STRINGP (it->string));
5535 /* If there isn't any `display' property in sight, and no
5536 overlays, we can just use the position of the newline in
5537 buffer text. */
5538 if (it->stop_charpos >= limit
5539 || ((pos = Fnext_single_property_change (make_number (start),
5540 Qdisplay, Qnil,
5541 make_number (limit)),
5542 NILP (pos))
5543 && next_overlay_change (start) == ZV))
5545 if (!it->bidi_p)
5547 IT_CHARPOS (*it) = limit;
5548 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5550 else
5552 struct bidi_it bprev;
5554 /* Help bidi.c avoid expensive searches for display
5555 properties and overlays, by telling it that there are
5556 none up to `limit'. */
5557 if (it->bidi_it.disp_pos < limit)
5559 it->bidi_it.disp_pos = limit;
5560 it->bidi_it.disp_prop_p = 0;
5562 do {
5563 bprev = it->bidi_it;
5564 bidi_move_to_visually_next (&it->bidi_it);
5565 } while (it->bidi_it.charpos != limit);
5566 IT_CHARPOS (*it) = limit;
5567 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5568 if (bidi_it_prev)
5569 *bidi_it_prev = bprev;
5571 *skipped_p = newline_found_p = 1;
5573 else
5575 while (get_next_display_element (it)
5576 && !newline_found_p)
5578 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5579 if (newline_found_p && it->bidi_p && bidi_it_prev)
5580 *bidi_it_prev = it->bidi_it;
5581 set_iterator_to_next (it, 0);
5586 it->selective = old_selective;
5587 return newline_found_p;
5591 /* Set IT's current position to the previous visible line start. Skip
5592 invisible text that is so either due to text properties or due to
5593 selective display. Caution: this does not change IT->current_x and
5594 IT->hpos. */
5596 static void
5597 back_to_previous_visible_line_start (struct it *it)
5599 while (IT_CHARPOS (*it) > BEGV)
5601 back_to_previous_line_start (it);
5603 if (IT_CHARPOS (*it) <= BEGV)
5604 break;
5606 /* If selective > 0, then lines indented more than its value are
5607 invisible. */
5608 if (it->selective > 0
5609 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5610 it->selective))
5611 continue;
5613 /* Check the newline before point for invisibility. */
5615 Lisp_Object prop;
5616 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5617 Qinvisible, it->window);
5618 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5619 continue;
5622 if (IT_CHARPOS (*it) <= BEGV)
5623 break;
5626 struct it it2;
5627 void *it2data = NULL;
5628 EMACS_INT pos;
5629 EMACS_INT beg, end;
5630 Lisp_Object val, overlay;
5632 SAVE_IT (it2, *it, it2data);
5634 /* If newline is part of a composition, continue from start of composition */
5635 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5636 && beg < IT_CHARPOS (*it))
5637 goto replaced;
5639 /* If newline is replaced by a display property, find start of overlay
5640 or interval and continue search from that point. */
5641 pos = --IT_CHARPOS (it2);
5642 --IT_BYTEPOS (it2);
5643 it2.sp = 0;
5644 bidi_unshelve_cache (NULL, 0);
5645 it2.string_from_display_prop_p = 0;
5646 it2.from_disp_prop_p = 0;
5647 if (handle_display_prop (&it2) == HANDLED_RETURN
5648 && !NILP (val = get_char_property_and_overlay
5649 (make_number (pos), Qdisplay, Qnil, &overlay))
5650 && (OVERLAYP (overlay)
5651 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5652 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5654 RESTORE_IT (it, it, it2data);
5655 goto replaced;
5658 /* Newline is not replaced by anything -- so we are done. */
5659 RESTORE_IT (it, it, it2data);
5660 break;
5662 replaced:
5663 if (beg < BEGV)
5664 beg = BEGV;
5665 IT_CHARPOS (*it) = beg;
5666 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5670 it->continuation_lines_width = 0;
5672 xassert (IT_CHARPOS (*it) >= BEGV);
5673 xassert (IT_CHARPOS (*it) == BEGV
5674 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5675 CHECK_IT (it);
5679 /* Reseat iterator IT at the previous visible line start. Skip
5680 invisible text that is so either due to text properties or due to
5681 selective display. At the end, update IT's overlay information,
5682 face information etc. */
5684 void
5685 reseat_at_previous_visible_line_start (struct it *it)
5687 back_to_previous_visible_line_start (it);
5688 reseat (it, it->current.pos, 1);
5689 CHECK_IT (it);
5693 /* Reseat iterator IT on the next visible line start in the current
5694 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5695 preceding the line start. Skip over invisible text that is so
5696 because of selective display. Compute faces, overlays etc at the
5697 new position. Note that this function does not skip over text that
5698 is invisible because of text properties. */
5700 static void
5701 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5703 int newline_found_p, skipped_p = 0;
5704 struct bidi_it bidi_it_prev;
5706 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5708 /* Skip over lines that are invisible because they are indented
5709 more than the value of IT->selective. */
5710 if (it->selective > 0)
5711 while (IT_CHARPOS (*it) < ZV
5712 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5713 it->selective))
5715 xassert (IT_BYTEPOS (*it) == BEGV
5716 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5717 newline_found_p =
5718 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5721 /* Position on the newline if that's what's requested. */
5722 if (on_newline_p && newline_found_p)
5724 if (STRINGP (it->string))
5726 if (IT_STRING_CHARPOS (*it) > 0)
5728 if (!it->bidi_p)
5730 --IT_STRING_CHARPOS (*it);
5731 --IT_STRING_BYTEPOS (*it);
5733 else
5735 /* We need to restore the bidi iterator to the state
5736 it had on the newline, and resync the IT's
5737 position with that. */
5738 it->bidi_it = bidi_it_prev;
5739 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
5740 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
5744 else if (IT_CHARPOS (*it) > BEGV)
5746 if (!it->bidi_p)
5748 --IT_CHARPOS (*it);
5749 --IT_BYTEPOS (*it);
5751 else
5753 /* We need to restore the bidi iterator to the state it
5754 had on the newline and resync IT with that. */
5755 it->bidi_it = bidi_it_prev;
5756 IT_CHARPOS (*it) = it->bidi_it.charpos;
5757 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5759 reseat (it, it->current.pos, 0);
5762 else if (skipped_p)
5763 reseat (it, it->current.pos, 0);
5765 CHECK_IT (it);
5770 /***********************************************************************
5771 Changing an iterator's position
5772 ***********************************************************************/
5774 /* Change IT's current position to POS in current_buffer. If FORCE_P
5775 is non-zero, always check for text properties at the new position.
5776 Otherwise, text properties are only looked up if POS >=
5777 IT->check_charpos of a property. */
5779 static void
5780 reseat (struct it *it, struct text_pos pos, int force_p)
5782 EMACS_INT original_pos = IT_CHARPOS (*it);
5784 reseat_1 (it, pos, 0);
5786 /* Determine where to check text properties. Avoid doing it
5787 where possible because text property lookup is very expensive. */
5788 if (force_p
5789 || CHARPOS (pos) > it->stop_charpos
5790 || CHARPOS (pos) < original_pos)
5792 if (it->bidi_p)
5794 /* For bidi iteration, we need to prime prev_stop and
5795 base_level_stop with our best estimations. */
5796 /* Implementation note: Of course, POS is not necessarily a
5797 stop position, so assigning prev_pos to it is a lie; we
5798 should have called compute_stop_backwards. However, if
5799 the current buffer does not include any R2L characters,
5800 that call would be a waste of cycles, because the
5801 iterator will never move back, and thus never cross this
5802 "fake" stop position. So we delay that backward search
5803 until the time we really need it, in next_element_from_buffer. */
5804 if (CHARPOS (pos) != it->prev_stop)
5805 it->prev_stop = CHARPOS (pos);
5806 if (CHARPOS (pos) < it->base_level_stop)
5807 it->base_level_stop = 0; /* meaning it's unknown */
5808 handle_stop (it);
5810 else
5812 handle_stop (it);
5813 it->prev_stop = it->base_level_stop = 0;
5818 CHECK_IT (it);
5822 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5823 IT->stop_pos to POS, also. */
5825 static void
5826 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5828 /* Don't call this function when scanning a C string. */
5829 xassert (it->s == NULL);
5831 /* POS must be a reasonable value. */
5832 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5834 it->current.pos = it->position = pos;
5835 it->end_charpos = ZV;
5836 it->dpvec = NULL;
5837 it->current.dpvec_index = -1;
5838 it->current.overlay_string_index = -1;
5839 IT_STRING_CHARPOS (*it) = -1;
5840 IT_STRING_BYTEPOS (*it) = -1;
5841 it->string = Qnil;
5842 it->method = GET_FROM_BUFFER;
5843 it->object = it->w->buffer;
5844 it->area = TEXT_AREA;
5845 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5846 it->sp = 0;
5847 it->string_from_display_prop_p = 0;
5848 it->from_disp_prop_p = 0;
5849 it->face_before_selective_p = 0;
5850 if (it->bidi_p)
5852 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5853 &it->bidi_it);
5854 bidi_unshelve_cache (NULL, 0);
5855 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5856 it->bidi_it.string.s = NULL;
5857 it->bidi_it.string.lstring = Qnil;
5858 it->bidi_it.string.bufpos = 0;
5859 it->bidi_it.string.unibyte = 0;
5862 if (set_stop_p)
5864 it->stop_charpos = CHARPOS (pos);
5865 it->base_level_stop = CHARPOS (pos);
5870 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5871 If S is non-null, it is a C string to iterate over. Otherwise,
5872 STRING gives a Lisp string to iterate over.
5874 If PRECISION > 0, don't return more then PRECISION number of
5875 characters from the string.
5877 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5878 characters have been returned. FIELD_WIDTH < 0 means an infinite
5879 field width.
5881 MULTIBYTE = 0 means disable processing of multibyte characters,
5882 MULTIBYTE > 0 means enable it,
5883 MULTIBYTE < 0 means use IT->multibyte_p.
5885 IT must be initialized via a prior call to init_iterator before
5886 calling this function. */
5888 static void
5889 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5890 EMACS_INT charpos, EMACS_INT precision, int field_width,
5891 int multibyte)
5893 /* No region in strings. */
5894 it->region_beg_charpos = it->region_end_charpos = -1;
5896 /* No text property checks performed by default, but see below. */
5897 it->stop_charpos = -1;
5899 /* Set iterator position and end position. */
5900 memset (&it->current, 0, sizeof it->current);
5901 it->current.overlay_string_index = -1;
5902 it->current.dpvec_index = -1;
5903 xassert (charpos >= 0);
5905 /* If STRING is specified, use its multibyteness, otherwise use the
5906 setting of MULTIBYTE, if specified. */
5907 if (multibyte >= 0)
5908 it->multibyte_p = multibyte > 0;
5910 /* Bidirectional reordering of strings is controlled by the default
5911 value of bidi-display-reordering. */
5912 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5914 if (s == NULL)
5916 xassert (STRINGP (string));
5917 it->string = string;
5918 it->s = NULL;
5919 it->end_charpos = it->string_nchars = SCHARS (string);
5920 it->method = GET_FROM_STRING;
5921 it->current.string_pos = string_pos (charpos, string);
5923 if (it->bidi_p)
5925 it->bidi_it.string.lstring = string;
5926 it->bidi_it.string.s = NULL;
5927 it->bidi_it.string.schars = it->end_charpos;
5928 it->bidi_it.string.bufpos = 0;
5929 it->bidi_it.string.from_disp_str = 0;
5930 it->bidi_it.string.unibyte = !it->multibyte_p;
5931 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5932 FRAME_WINDOW_P (it->f), &it->bidi_it);
5935 else
5937 it->s = (const unsigned char *) s;
5938 it->string = Qnil;
5940 /* Note that we use IT->current.pos, not it->current.string_pos,
5941 for displaying C strings. */
5942 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5943 if (it->multibyte_p)
5945 it->current.pos = c_string_pos (charpos, s, 1);
5946 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5948 else
5950 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5951 it->end_charpos = it->string_nchars = strlen (s);
5954 if (it->bidi_p)
5956 it->bidi_it.string.lstring = Qnil;
5957 it->bidi_it.string.s = (const unsigned char *) s;
5958 it->bidi_it.string.schars = it->end_charpos;
5959 it->bidi_it.string.bufpos = 0;
5960 it->bidi_it.string.from_disp_str = 0;
5961 it->bidi_it.string.unibyte = !it->multibyte_p;
5962 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5963 &it->bidi_it);
5965 it->method = GET_FROM_C_STRING;
5968 /* PRECISION > 0 means don't return more than PRECISION characters
5969 from the string. */
5970 if (precision > 0 && it->end_charpos - charpos > precision)
5972 it->end_charpos = it->string_nchars = charpos + precision;
5973 if (it->bidi_p)
5974 it->bidi_it.string.schars = it->end_charpos;
5977 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5978 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5979 FIELD_WIDTH < 0 means infinite field width. This is useful for
5980 padding with `-' at the end of a mode line. */
5981 if (field_width < 0)
5982 field_width = INFINITY;
5983 /* Implementation note: We deliberately don't enlarge
5984 it->bidi_it.string.schars here to fit it->end_charpos, because
5985 the bidi iterator cannot produce characters out of thin air. */
5986 if (field_width > it->end_charpos - charpos)
5987 it->end_charpos = charpos + field_width;
5989 /* Use the standard display table for displaying strings. */
5990 if (DISP_TABLE_P (Vstandard_display_table))
5991 it->dp = XCHAR_TABLE (Vstandard_display_table);
5993 it->stop_charpos = charpos;
5994 it->prev_stop = charpos;
5995 it->base_level_stop = 0;
5996 if (it->bidi_p)
5998 it->bidi_it.first_elt = 1;
5999 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6000 it->bidi_it.disp_pos = -1;
6002 if (s == NULL && it->multibyte_p)
6004 EMACS_INT endpos = SCHARS (it->string);
6005 if (endpos > it->end_charpos)
6006 endpos = it->end_charpos;
6007 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6008 it->string);
6010 CHECK_IT (it);
6015 /***********************************************************************
6016 Iteration
6017 ***********************************************************************/
6019 /* Map enum it_method value to corresponding next_element_from_* function. */
6021 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6023 next_element_from_buffer,
6024 next_element_from_display_vector,
6025 next_element_from_string,
6026 next_element_from_c_string,
6027 next_element_from_image,
6028 next_element_from_stretch
6031 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6034 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6035 (possibly with the following characters). */
6037 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6038 ((IT)->cmp_it.id >= 0 \
6039 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6040 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6041 END_CHARPOS, (IT)->w, \
6042 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6043 (IT)->string)))
6046 /* Lookup the char-table Vglyphless_char_display for character C (-1
6047 if we want information for no-font case), and return the display
6048 method symbol. By side-effect, update it->what and
6049 it->glyphless_method. This function is called from
6050 get_next_display_element for each character element, and from
6051 x_produce_glyphs when no suitable font was found. */
6053 Lisp_Object
6054 lookup_glyphless_char_display (int c, struct it *it)
6056 Lisp_Object glyphless_method = Qnil;
6058 if (CHAR_TABLE_P (Vglyphless_char_display)
6059 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6061 if (c >= 0)
6063 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6064 if (CONSP (glyphless_method))
6065 glyphless_method = FRAME_WINDOW_P (it->f)
6066 ? XCAR (glyphless_method)
6067 : XCDR (glyphless_method);
6069 else
6070 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6073 retry:
6074 if (NILP (glyphless_method))
6076 if (c >= 0)
6077 /* The default is to display the character by a proper font. */
6078 return Qnil;
6079 /* The default for the no-font case is to display an empty box. */
6080 glyphless_method = Qempty_box;
6082 if (EQ (glyphless_method, Qzero_width))
6084 if (c >= 0)
6085 return glyphless_method;
6086 /* This method can't be used for the no-font case. */
6087 glyphless_method = Qempty_box;
6089 if (EQ (glyphless_method, Qthin_space))
6090 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6091 else if (EQ (glyphless_method, Qempty_box))
6092 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6093 else if (EQ (glyphless_method, Qhex_code))
6094 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6095 else if (STRINGP (glyphless_method))
6096 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6097 else
6099 /* Invalid value. We use the default method. */
6100 glyphless_method = Qnil;
6101 goto retry;
6103 it->what = IT_GLYPHLESS;
6104 return glyphless_method;
6107 /* Load IT's display element fields with information about the next
6108 display element from the current position of IT. Value is zero if
6109 end of buffer (or C string) is reached. */
6111 static struct frame *last_escape_glyph_frame = NULL;
6112 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6113 static int last_escape_glyph_merged_face_id = 0;
6115 struct frame *last_glyphless_glyph_frame = NULL;
6116 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6117 int last_glyphless_glyph_merged_face_id = 0;
6119 static int
6120 get_next_display_element (struct it *it)
6122 /* Non-zero means that we found a display element. Zero means that
6123 we hit the end of what we iterate over. Performance note: the
6124 function pointer `method' used here turns out to be faster than
6125 using a sequence of if-statements. */
6126 int success_p;
6128 get_next:
6129 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6131 if (it->what == IT_CHARACTER)
6133 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6134 and only if (a) the resolved directionality of that character
6135 is R..." */
6136 /* FIXME: Do we need an exception for characters from display
6137 tables? */
6138 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6139 it->c = bidi_mirror_char (it->c);
6140 /* Map via display table or translate control characters.
6141 IT->c, IT->len etc. have been set to the next character by
6142 the function call above. If we have a display table, and it
6143 contains an entry for IT->c, translate it. Don't do this if
6144 IT->c itself comes from a display table, otherwise we could
6145 end up in an infinite recursion. (An alternative could be to
6146 count the recursion depth of this function and signal an
6147 error when a certain maximum depth is reached.) Is it worth
6148 it? */
6149 if (success_p && it->dpvec == NULL)
6151 Lisp_Object dv;
6152 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6153 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6154 nbsp_or_shy = char_is_other;
6155 int c = it->c; /* This is the character to display. */
6157 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6159 xassert (SINGLE_BYTE_CHAR_P (c));
6160 if (unibyte_display_via_language_environment)
6162 c = DECODE_CHAR (unibyte, c);
6163 if (c < 0)
6164 c = BYTE8_TO_CHAR (it->c);
6166 else
6167 c = BYTE8_TO_CHAR (it->c);
6170 if (it->dp
6171 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6172 VECTORP (dv)))
6174 struct Lisp_Vector *v = XVECTOR (dv);
6176 /* Return the first character from the display table
6177 entry, if not empty. If empty, don't display the
6178 current character. */
6179 if (v->header.size)
6181 it->dpvec_char_len = it->len;
6182 it->dpvec = v->contents;
6183 it->dpend = v->contents + v->header.size;
6184 it->current.dpvec_index = 0;
6185 it->dpvec_face_id = -1;
6186 it->saved_face_id = it->face_id;
6187 it->method = GET_FROM_DISPLAY_VECTOR;
6188 it->ellipsis_p = 0;
6190 else
6192 set_iterator_to_next (it, 0);
6194 goto get_next;
6197 if (! NILP (lookup_glyphless_char_display (c, it)))
6199 if (it->what == IT_GLYPHLESS)
6200 goto done;
6201 /* Don't display this character. */
6202 set_iterator_to_next (it, 0);
6203 goto get_next;
6206 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6207 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6208 : c == 0xAD ? char_is_soft_hyphen
6209 : char_is_other);
6211 /* Translate control characters into `\003' or `^C' form.
6212 Control characters coming from a display table entry are
6213 currently not translated because we use IT->dpvec to hold
6214 the translation. This could easily be changed but I
6215 don't believe that it is worth doing.
6217 NBSP and SOFT-HYPEN are property translated too.
6219 Non-printable characters and raw-byte characters are also
6220 translated to octal form. */
6221 if (((c < ' ' || c == 127) /* ASCII control chars */
6222 ? (it->area != TEXT_AREA
6223 /* In mode line, treat \n, \t like other crl chars. */
6224 || (c != '\t'
6225 && it->glyph_row
6226 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6227 || (c != '\n' && c != '\t'))
6228 : (nbsp_or_shy
6229 || CHAR_BYTE8_P (c)
6230 || ! CHAR_PRINTABLE_P (c))))
6232 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6233 or a non-printable character which must be displayed
6234 either as '\003' or as `^C' where the '\\' and '^'
6235 can be defined in the display table. Fill
6236 IT->ctl_chars with glyphs for what we have to
6237 display. Then, set IT->dpvec to these glyphs. */
6238 Lisp_Object gc;
6239 int ctl_len;
6240 int face_id;
6241 EMACS_INT lface_id = 0;
6242 int escape_glyph;
6244 /* Handle control characters with ^. */
6246 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6248 int g;
6250 g = '^'; /* default glyph for Control */
6251 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6252 if (it->dp
6253 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6254 && GLYPH_CODE_CHAR_VALID_P (gc))
6256 g = GLYPH_CODE_CHAR (gc);
6257 lface_id = GLYPH_CODE_FACE (gc);
6259 if (lface_id)
6261 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6263 else if (it->f == last_escape_glyph_frame
6264 && it->face_id == last_escape_glyph_face_id)
6266 face_id = last_escape_glyph_merged_face_id;
6268 else
6270 /* Merge the escape-glyph face into the current face. */
6271 face_id = merge_faces (it->f, Qescape_glyph, 0,
6272 it->face_id);
6273 last_escape_glyph_frame = it->f;
6274 last_escape_glyph_face_id = it->face_id;
6275 last_escape_glyph_merged_face_id = face_id;
6278 XSETINT (it->ctl_chars[0], g);
6279 XSETINT (it->ctl_chars[1], c ^ 0100);
6280 ctl_len = 2;
6281 goto display_control;
6284 /* Handle non-break space in the mode where it only gets
6285 highlighting. */
6287 if (EQ (Vnobreak_char_display, Qt)
6288 && nbsp_or_shy == char_is_nbsp)
6290 /* Merge the no-break-space face into the current face. */
6291 face_id = merge_faces (it->f, Qnobreak_space, 0,
6292 it->face_id);
6294 c = ' ';
6295 XSETINT (it->ctl_chars[0], ' ');
6296 ctl_len = 1;
6297 goto display_control;
6300 /* Handle sequences that start with the "escape glyph". */
6302 /* the default escape glyph is \. */
6303 escape_glyph = '\\';
6305 if (it->dp
6306 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6307 && GLYPH_CODE_CHAR_VALID_P (gc))
6309 escape_glyph = GLYPH_CODE_CHAR (gc);
6310 lface_id = GLYPH_CODE_FACE (gc);
6312 if (lface_id)
6314 /* The display table specified a face.
6315 Merge it into face_id and also into escape_glyph. */
6316 face_id = merge_faces (it->f, Qt, lface_id,
6317 it->face_id);
6319 else if (it->f == last_escape_glyph_frame
6320 && it->face_id == last_escape_glyph_face_id)
6322 face_id = last_escape_glyph_merged_face_id;
6324 else
6326 /* Merge the escape-glyph face into the current face. */
6327 face_id = merge_faces (it->f, Qescape_glyph, 0,
6328 it->face_id);
6329 last_escape_glyph_frame = it->f;
6330 last_escape_glyph_face_id = it->face_id;
6331 last_escape_glyph_merged_face_id = face_id;
6334 /* Handle soft hyphens in the mode where they only get
6335 highlighting. */
6337 if (EQ (Vnobreak_char_display, Qt)
6338 && nbsp_or_shy == char_is_soft_hyphen)
6340 XSETINT (it->ctl_chars[0], '-');
6341 ctl_len = 1;
6342 goto display_control;
6345 /* Handle non-break space and soft hyphen
6346 with the escape glyph. */
6348 if (nbsp_or_shy)
6350 XSETINT (it->ctl_chars[0], escape_glyph);
6351 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6352 XSETINT (it->ctl_chars[1], c);
6353 ctl_len = 2;
6354 goto display_control;
6358 char str[10];
6359 int len, i;
6361 if (CHAR_BYTE8_P (c))
6362 /* Display \200 instead of \17777600. */
6363 c = CHAR_TO_BYTE8 (c);
6364 len = sprintf (str, "%03o", c);
6366 XSETINT (it->ctl_chars[0], escape_glyph);
6367 for (i = 0; i < len; i++)
6368 XSETINT (it->ctl_chars[i + 1], str[i]);
6369 ctl_len = len + 1;
6372 display_control:
6373 /* Set up IT->dpvec and return first character from it. */
6374 it->dpvec_char_len = it->len;
6375 it->dpvec = it->ctl_chars;
6376 it->dpend = it->dpvec + ctl_len;
6377 it->current.dpvec_index = 0;
6378 it->dpvec_face_id = face_id;
6379 it->saved_face_id = it->face_id;
6380 it->method = GET_FROM_DISPLAY_VECTOR;
6381 it->ellipsis_p = 0;
6382 goto get_next;
6384 it->char_to_display = c;
6386 else if (success_p)
6388 it->char_to_display = it->c;
6392 /* Adjust face id for a multibyte character. There are no multibyte
6393 character in unibyte text. */
6394 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6395 && it->multibyte_p
6396 && success_p
6397 && FRAME_WINDOW_P (it->f))
6399 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6401 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6403 /* Automatic composition with glyph-string. */
6404 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6406 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6408 else
6410 EMACS_INT pos = (it->s ? -1
6411 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6412 : IT_CHARPOS (*it));
6413 int c;
6415 if (it->what == IT_CHARACTER)
6416 c = it->char_to_display;
6417 else
6419 struct composition *cmp = composition_table[it->cmp_it.id];
6420 int i;
6422 c = ' ';
6423 for (i = 0; i < cmp->glyph_len; i++)
6424 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6425 break;
6427 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6431 done:
6432 /* Is this character the last one of a run of characters with
6433 box? If yes, set IT->end_of_box_run_p to 1. */
6434 if (it->face_box_p
6435 && it->s == NULL)
6437 if (it->method == GET_FROM_STRING && it->sp)
6439 int face_id = underlying_face_id (it);
6440 struct face *face = FACE_FROM_ID (it->f, face_id);
6442 if (face)
6444 if (face->box == FACE_NO_BOX)
6446 /* If the box comes from face properties in a
6447 display string, check faces in that string. */
6448 int string_face_id = face_after_it_pos (it);
6449 it->end_of_box_run_p
6450 = (FACE_FROM_ID (it->f, string_face_id)->box
6451 == FACE_NO_BOX);
6453 /* Otherwise, the box comes from the underlying face.
6454 If this is the last string character displayed, check
6455 the next buffer location. */
6456 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6457 && (it->current.overlay_string_index
6458 == it->n_overlay_strings - 1))
6460 EMACS_INT ignore;
6461 int next_face_id;
6462 struct text_pos pos = it->current.pos;
6463 INC_TEXT_POS (pos, it->multibyte_p);
6465 next_face_id = face_at_buffer_position
6466 (it->w, CHARPOS (pos), it->region_beg_charpos,
6467 it->region_end_charpos, &ignore,
6468 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6469 -1);
6470 it->end_of_box_run_p
6471 = (FACE_FROM_ID (it->f, next_face_id)->box
6472 == FACE_NO_BOX);
6476 else
6478 int face_id = face_after_it_pos (it);
6479 it->end_of_box_run_p
6480 = (face_id != it->face_id
6481 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6485 /* Value is 0 if end of buffer or string reached. */
6486 return success_p;
6490 /* Move IT to the next display element.
6492 RESEAT_P non-zero means if called on a newline in buffer text,
6493 skip to the next visible line start.
6495 Functions get_next_display_element and set_iterator_to_next are
6496 separate because I find this arrangement easier to handle than a
6497 get_next_display_element function that also increments IT's
6498 position. The way it is we can first look at an iterator's current
6499 display element, decide whether it fits on a line, and if it does,
6500 increment the iterator position. The other way around we probably
6501 would either need a flag indicating whether the iterator has to be
6502 incremented the next time, or we would have to implement a
6503 decrement position function which would not be easy to write. */
6505 void
6506 set_iterator_to_next (struct it *it, int reseat_p)
6508 /* Reset flags indicating start and end of a sequence of characters
6509 with box. Reset them at the start of this function because
6510 moving the iterator to a new position might set them. */
6511 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6513 switch (it->method)
6515 case GET_FROM_BUFFER:
6516 /* The current display element of IT is a character from
6517 current_buffer. Advance in the buffer, and maybe skip over
6518 invisible lines that are so because of selective display. */
6519 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6520 reseat_at_next_visible_line_start (it, 0);
6521 else if (it->cmp_it.id >= 0)
6523 /* We are currently getting glyphs from a composition. */
6524 int i;
6526 if (! it->bidi_p)
6528 IT_CHARPOS (*it) += it->cmp_it.nchars;
6529 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6530 if (it->cmp_it.to < it->cmp_it.nglyphs)
6532 it->cmp_it.from = it->cmp_it.to;
6534 else
6536 it->cmp_it.id = -1;
6537 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6538 IT_BYTEPOS (*it),
6539 it->end_charpos, Qnil);
6542 else if (! it->cmp_it.reversed_p)
6544 /* Composition created while scanning forward. */
6545 /* Update IT's char/byte positions to point to the first
6546 character of the next grapheme cluster, or to the
6547 character visually after the current composition. */
6548 for (i = 0; i < it->cmp_it.nchars; i++)
6549 bidi_move_to_visually_next (&it->bidi_it);
6550 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6551 IT_CHARPOS (*it) = it->bidi_it.charpos;
6553 if (it->cmp_it.to < it->cmp_it.nglyphs)
6555 /* Proceed to the next grapheme cluster. */
6556 it->cmp_it.from = it->cmp_it.to;
6558 else
6560 /* No more grapheme clusters in this composition.
6561 Find the next stop position. */
6562 EMACS_INT stop = it->end_charpos;
6563 if (it->bidi_it.scan_dir < 0)
6564 /* Now we are scanning backward and don't know
6565 where to stop. */
6566 stop = -1;
6567 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6568 IT_BYTEPOS (*it), stop, Qnil);
6571 else
6573 /* Composition created while scanning backward. */
6574 /* Update IT's char/byte positions to point to the last
6575 character of the previous grapheme cluster, or the
6576 character visually after the current composition. */
6577 for (i = 0; i < it->cmp_it.nchars; i++)
6578 bidi_move_to_visually_next (&it->bidi_it);
6579 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6580 IT_CHARPOS (*it) = it->bidi_it.charpos;
6581 if (it->cmp_it.from > 0)
6583 /* Proceed to the previous grapheme cluster. */
6584 it->cmp_it.to = it->cmp_it.from;
6586 else
6588 /* No more grapheme clusters in this composition.
6589 Find the next stop position. */
6590 EMACS_INT stop = it->end_charpos;
6591 if (it->bidi_it.scan_dir < 0)
6592 /* Now we are scanning backward and don't know
6593 where to stop. */
6594 stop = -1;
6595 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6596 IT_BYTEPOS (*it), stop, Qnil);
6600 else
6602 xassert (it->len != 0);
6604 if (!it->bidi_p)
6606 IT_BYTEPOS (*it) += it->len;
6607 IT_CHARPOS (*it) += 1;
6609 else
6611 int prev_scan_dir = it->bidi_it.scan_dir;
6612 /* If this is a new paragraph, determine its base
6613 direction (a.k.a. its base embedding level). */
6614 if (it->bidi_it.new_paragraph)
6615 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6616 bidi_move_to_visually_next (&it->bidi_it);
6617 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6618 IT_CHARPOS (*it) = it->bidi_it.charpos;
6619 if (prev_scan_dir != it->bidi_it.scan_dir)
6621 /* As the scan direction was changed, we must
6622 re-compute the stop position for composition. */
6623 EMACS_INT stop = it->end_charpos;
6624 if (it->bidi_it.scan_dir < 0)
6625 stop = -1;
6626 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6627 IT_BYTEPOS (*it), stop, Qnil);
6630 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6632 break;
6634 case GET_FROM_C_STRING:
6635 /* Current display element of IT is from a C string. */
6636 if (!it->bidi_p
6637 /* If the string position is beyond string's end, it means
6638 next_element_from_c_string is padding the string with
6639 blanks, in which case we bypass the bidi iterator,
6640 because it cannot deal with such virtual characters. */
6641 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6643 IT_BYTEPOS (*it) += it->len;
6644 IT_CHARPOS (*it) += 1;
6646 else
6648 bidi_move_to_visually_next (&it->bidi_it);
6649 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6650 IT_CHARPOS (*it) = it->bidi_it.charpos;
6652 break;
6654 case GET_FROM_DISPLAY_VECTOR:
6655 /* Current display element of IT is from a display table entry.
6656 Advance in the display table definition. Reset it to null if
6657 end reached, and continue with characters from buffers/
6658 strings. */
6659 ++it->current.dpvec_index;
6661 /* Restore face of the iterator to what they were before the
6662 display vector entry (these entries may contain faces). */
6663 it->face_id = it->saved_face_id;
6665 if (it->dpvec + it->current.dpvec_index == it->dpend)
6667 int recheck_faces = it->ellipsis_p;
6669 if (it->s)
6670 it->method = GET_FROM_C_STRING;
6671 else if (STRINGP (it->string))
6672 it->method = GET_FROM_STRING;
6673 else
6675 it->method = GET_FROM_BUFFER;
6676 it->object = it->w->buffer;
6679 it->dpvec = NULL;
6680 it->current.dpvec_index = -1;
6682 /* Skip over characters which were displayed via IT->dpvec. */
6683 if (it->dpvec_char_len < 0)
6684 reseat_at_next_visible_line_start (it, 1);
6685 else if (it->dpvec_char_len > 0)
6687 if (it->method == GET_FROM_STRING
6688 && it->n_overlay_strings > 0)
6689 it->ignore_overlay_strings_at_pos_p = 1;
6690 it->len = it->dpvec_char_len;
6691 set_iterator_to_next (it, reseat_p);
6694 /* Maybe recheck faces after display vector */
6695 if (recheck_faces)
6696 it->stop_charpos = IT_CHARPOS (*it);
6698 break;
6700 case GET_FROM_STRING:
6701 /* Current display element is a character from a Lisp string. */
6702 xassert (it->s == NULL && STRINGP (it->string));
6703 if (it->cmp_it.id >= 0)
6705 int i;
6707 if (! it->bidi_p)
6709 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6710 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6711 if (it->cmp_it.to < it->cmp_it.nglyphs)
6712 it->cmp_it.from = it->cmp_it.to;
6713 else
6715 it->cmp_it.id = -1;
6716 composition_compute_stop_pos (&it->cmp_it,
6717 IT_STRING_CHARPOS (*it),
6718 IT_STRING_BYTEPOS (*it),
6719 it->end_charpos, it->string);
6722 else if (! it->cmp_it.reversed_p)
6724 for (i = 0; i < it->cmp_it.nchars; i++)
6725 bidi_move_to_visually_next (&it->bidi_it);
6726 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6727 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6729 if (it->cmp_it.to < it->cmp_it.nglyphs)
6730 it->cmp_it.from = it->cmp_it.to;
6731 else
6733 EMACS_INT stop = it->end_charpos;
6734 if (it->bidi_it.scan_dir < 0)
6735 stop = -1;
6736 composition_compute_stop_pos (&it->cmp_it,
6737 IT_STRING_CHARPOS (*it),
6738 IT_STRING_BYTEPOS (*it), stop,
6739 it->string);
6742 else
6744 for (i = 0; i < it->cmp_it.nchars; i++)
6745 bidi_move_to_visually_next (&it->bidi_it);
6746 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6747 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6748 if (it->cmp_it.from > 0)
6749 it->cmp_it.to = it->cmp_it.from;
6750 else
6752 EMACS_INT stop = it->end_charpos;
6753 if (it->bidi_it.scan_dir < 0)
6754 stop = -1;
6755 composition_compute_stop_pos (&it->cmp_it,
6756 IT_STRING_CHARPOS (*it),
6757 IT_STRING_BYTEPOS (*it), stop,
6758 it->string);
6762 else
6764 if (!it->bidi_p
6765 /* If the string position is beyond string's end, it
6766 means next_element_from_string is padding the string
6767 with blanks, in which case we bypass the bidi
6768 iterator, because it cannot deal with such virtual
6769 characters. */
6770 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6772 IT_STRING_BYTEPOS (*it) += it->len;
6773 IT_STRING_CHARPOS (*it) += 1;
6775 else
6777 int prev_scan_dir = it->bidi_it.scan_dir;
6779 bidi_move_to_visually_next (&it->bidi_it);
6780 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6781 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6782 if (prev_scan_dir != it->bidi_it.scan_dir)
6784 EMACS_INT stop = it->end_charpos;
6786 if (it->bidi_it.scan_dir < 0)
6787 stop = -1;
6788 composition_compute_stop_pos (&it->cmp_it,
6789 IT_STRING_CHARPOS (*it),
6790 IT_STRING_BYTEPOS (*it), stop,
6791 it->string);
6796 consider_string_end:
6798 if (it->current.overlay_string_index >= 0)
6800 /* IT->string is an overlay string. Advance to the
6801 next, if there is one. */
6802 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6804 it->ellipsis_p = 0;
6805 next_overlay_string (it);
6806 if (it->ellipsis_p)
6807 setup_for_ellipsis (it, 0);
6810 else
6812 /* IT->string is not an overlay string. If we reached
6813 its end, and there is something on IT->stack, proceed
6814 with what is on the stack. This can be either another
6815 string, this time an overlay string, or a buffer. */
6816 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6817 && it->sp > 0)
6819 pop_it (it);
6820 if (it->method == GET_FROM_STRING)
6821 goto consider_string_end;
6824 break;
6826 case GET_FROM_IMAGE:
6827 case GET_FROM_STRETCH:
6828 /* The position etc with which we have to proceed are on
6829 the stack. The position may be at the end of a string,
6830 if the `display' property takes up the whole string. */
6831 xassert (it->sp > 0);
6832 pop_it (it);
6833 if (it->method == GET_FROM_STRING)
6834 goto consider_string_end;
6835 break;
6837 default:
6838 /* There are no other methods defined, so this should be a bug. */
6839 abort ();
6842 xassert (it->method != GET_FROM_STRING
6843 || (STRINGP (it->string)
6844 && IT_STRING_CHARPOS (*it) >= 0));
6847 /* Load IT's display element fields with information about the next
6848 display element which comes from a display table entry or from the
6849 result of translating a control character to one of the forms `^C'
6850 or `\003'.
6852 IT->dpvec holds the glyphs to return as characters.
6853 IT->saved_face_id holds the face id before the display vector--it
6854 is restored into IT->face_id in set_iterator_to_next. */
6856 static int
6857 next_element_from_display_vector (struct it *it)
6859 Lisp_Object gc;
6861 /* Precondition. */
6862 xassert (it->dpvec && it->current.dpvec_index >= 0);
6864 it->face_id = it->saved_face_id;
6866 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6867 That seemed totally bogus - so I changed it... */
6868 gc = it->dpvec[it->current.dpvec_index];
6870 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6872 it->c = GLYPH_CODE_CHAR (gc);
6873 it->len = CHAR_BYTES (it->c);
6875 /* The entry may contain a face id to use. Such a face id is
6876 the id of a Lisp face, not a realized face. A face id of
6877 zero means no face is specified. */
6878 if (it->dpvec_face_id >= 0)
6879 it->face_id = it->dpvec_face_id;
6880 else
6882 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
6883 if (lface_id > 0)
6884 it->face_id = merge_faces (it->f, Qt, lface_id,
6885 it->saved_face_id);
6888 else
6889 /* Display table entry is invalid. Return a space. */
6890 it->c = ' ', it->len = 1;
6892 /* Don't change position and object of the iterator here. They are
6893 still the values of the character that had this display table
6894 entry or was translated, and that's what we want. */
6895 it->what = IT_CHARACTER;
6896 return 1;
6899 /* Get the first element of string/buffer in the visual order, after
6900 being reseated to a new position in a string or a buffer. */
6901 static void
6902 get_visually_first_element (struct it *it)
6904 int string_p = STRINGP (it->string) || it->s;
6905 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6906 EMACS_INT bob = (string_p ? 0 : BEGV);
6908 if (STRINGP (it->string))
6910 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6911 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6913 else
6915 it->bidi_it.charpos = IT_CHARPOS (*it);
6916 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6919 if (it->bidi_it.charpos == eob)
6921 /* Nothing to do, but reset the FIRST_ELT flag, like
6922 bidi_paragraph_init does, because we are not going to
6923 call it. */
6924 it->bidi_it.first_elt = 0;
6926 else if (it->bidi_it.charpos == bob
6927 || (!string_p
6928 /* FIXME: Should support all Unicode line separators. */
6929 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6930 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6932 /* If we are at the beginning of a line/string, we can produce
6933 the next element right away. */
6934 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6935 bidi_move_to_visually_next (&it->bidi_it);
6937 else
6939 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6941 /* We need to prime the bidi iterator starting at the line's or
6942 string's beginning, before we will be able to produce the
6943 next element. */
6944 if (string_p)
6945 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6946 else
6948 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6949 -1);
6950 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6952 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6955 /* Now return to buffer/string position where we were asked
6956 to get the next display element, and produce that. */
6957 bidi_move_to_visually_next (&it->bidi_it);
6959 while (it->bidi_it.bytepos != orig_bytepos
6960 && it->bidi_it.charpos < eob);
6963 /* Adjust IT's position information to where we ended up. */
6964 if (STRINGP (it->string))
6966 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6967 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6969 else
6971 IT_CHARPOS (*it) = it->bidi_it.charpos;
6972 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6975 if (STRINGP (it->string) || !it->s)
6977 EMACS_INT stop, charpos, bytepos;
6979 if (STRINGP (it->string))
6981 xassert (!it->s);
6982 stop = SCHARS (it->string);
6983 if (stop > it->end_charpos)
6984 stop = it->end_charpos;
6985 charpos = IT_STRING_CHARPOS (*it);
6986 bytepos = IT_STRING_BYTEPOS (*it);
6988 else
6990 stop = it->end_charpos;
6991 charpos = IT_CHARPOS (*it);
6992 bytepos = IT_BYTEPOS (*it);
6994 if (it->bidi_it.scan_dir < 0)
6995 stop = -1;
6996 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6997 it->string);
7001 /* Load IT with the next display element from Lisp string IT->string.
7002 IT->current.string_pos is the current position within the string.
7003 If IT->current.overlay_string_index >= 0, the Lisp string is an
7004 overlay string. */
7006 static int
7007 next_element_from_string (struct it *it)
7009 struct text_pos position;
7011 xassert (STRINGP (it->string));
7012 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7013 xassert (IT_STRING_CHARPOS (*it) >= 0);
7014 position = it->current.string_pos;
7016 /* With bidi reordering, the character to display might not be the
7017 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7018 that we were reseat()ed to a new string, whose paragraph
7019 direction is not known. */
7020 if (it->bidi_p && it->bidi_it.first_elt)
7022 get_visually_first_element (it);
7023 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7026 /* Time to check for invisible text? */
7027 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7029 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7031 if (!(!it->bidi_p
7032 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7033 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7035 /* With bidi non-linear iteration, we could find
7036 ourselves far beyond the last computed stop_charpos,
7037 with several other stop positions in between that we
7038 missed. Scan them all now, in buffer's logical
7039 order, until we find and handle the last stop_charpos
7040 that precedes our current position. */
7041 handle_stop_backwards (it, it->stop_charpos);
7042 return GET_NEXT_DISPLAY_ELEMENT (it);
7044 else
7046 if (it->bidi_p)
7048 /* Take note of the stop position we just moved
7049 across, for when we will move back across it. */
7050 it->prev_stop = it->stop_charpos;
7051 /* If we are at base paragraph embedding level, take
7052 note of the last stop position seen at this
7053 level. */
7054 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7055 it->base_level_stop = it->stop_charpos;
7057 handle_stop (it);
7059 /* Since a handler may have changed IT->method, we must
7060 recurse here. */
7061 return GET_NEXT_DISPLAY_ELEMENT (it);
7064 else if (it->bidi_p
7065 /* If we are before prev_stop, we may have overstepped
7066 on our way backwards a stop_pos, and if so, we need
7067 to handle that stop_pos. */
7068 && IT_STRING_CHARPOS (*it) < it->prev_stop
7069 /* We can sometimes back up for reasons that have nothing
7070 to do with bidi reordering. E.g., compositions. The
7071 code below is only needed when we are above the base
7072 embedding level, so test for that explicitly. */
7073 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7075 /* If we lost track of base_level_stop, we have no better
7076 place for handle_stop_backwards to start from than string
7077 beginning. This happens, e.g., when we were reseated to
7078 the previous screenful of text by vertical-motion. */
7079 if (it->base_level_stop <= 0
7080 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7081 it->base_level_stop = 0;
7082 handle_stop_backwards (it, it->base_level_stop);
7083 return GET_NEXT_DISPLAY_ELEMENT (it);
7087 if (it->current.overlay_string_index >= 0)
7089 /* Get the next character from an overlay string. In overlay
7090 strings, There is no field width or padding with spaces to
7091 do. */
7092 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7094 it->what = IT_EOB;
7095 return 0;
7097 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7098 IT_STRING_BYTEPOS (*it),
7099 it->bidi_it.scan_dir < 0
7100 ? -1
7101 : SCHARS (it->string))
7102 && next_element_from_composition (it))
7104 return 1;
7106 else if (STRING_MULTIBYTE (it->string))
7108 const unsigned char *s = (SDATA (it->string)
7109 + IT_STRING_BYTEPOS (*it));
7110 it->c = string_char_and_length (s, &it->len);
7112 else
7114 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7115 it->len = 1;
7118 else
7120 /* Get the next character from a Lisp string that is not an
7121 overlay string. Such strings come from the mode line, for
7122 example. We may have to pad with spaces, or truncate the
7123 string. See also next_element_from_c_string. */
7124 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7126 it->what = IT_EOB;
7127 return 0;
7129 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7131 /* Pad with spaces. */
7132 it->c = ' ', it->len = 1;
7133 CHARPOS (position) = BYTEPOS (position) = -1;
7135 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7136 IT_STRING_BYTEPOS (*it),
7137 it->bidi_it.scan_dir < 0
7138 ? -1
7139 : it->string_nchars)
7140 && next_element_from_composition (it))
7142 return 1;
7144 else if (STRING_MULTIBYTE (it->string))
7146 const unsigned char *s = (SDATA (it->string)
7147 + IT_STRING_BYTEPOS (*it));
7148 it->c = string_char_and_length (s, &it->len);
7150 else
7152 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7153 it->len = 1;
7157 /* Record what we have and where it came from. */
7158 it->what = IT_CHARACTER;
7159 it->object = it->string;
7160 it->position = position;
7161 return 1;
7165 /* Load IT with next display element from C string IT->s.
7166 IT->string_nchars is the maximum number of characters to return
7167 from the string. IT->end_charpos may be greater than
7168 IT->string_nchars when this function is called, in which case we
7169 may have to return padding spaces. Value is zero if end of string
7170 reached, including padding spaces. */
7172 static int
7173 next_element_from_c_string (struct it *it)
7175 int success_p = 1;
7177 xassert (it->s);
7178 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7179 it->what = IT_CHARACTER;
7180 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7181 it->object = Qnil;
7183 /* With bidi reordering, the character to display might not be the
7184 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7185 we were reseated to a new string, whose paragraph direction is
7186 not known. */
7187 if (it->bidi_p && it->bidi_it.first_elt)
7188 get_visually_first_element (it);
7190 /* IT's position can be greater than IT->string_nchars in case a
7191 field width or precision has been specified when the iterator was
7192 initialized. */
7193 if (IT_CHARPOS (*it) >= it->end_charpos)
7195 /* End of the game. */
7196 it->what = IT_EOB;
7197 success_p = 0;
7199 else if (IT_CHARPOS (*it) >= it->string_nchars)
7201 /* Pad with spaces. */
7202 it->c = ' ', it->len = 1;
7203 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7205 else if (it->multibyte_p)
7206 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7207 else
7208 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7210 return success_p;
7214 /* Set up IT to return characters from an ellipsis, if appropriate.
7215 The definition of the ellipsis glyphs may come from a display table
7216 entry. This function fills IT with the first glyph from the
7217 ellipsis if an ellipsis is to be displayed. */
7219 static int
7220 next_element_from_ellipsis (struct it *it)
7222 if (it->selective_display_ellipsis_p)
7223 setup_for_ellipsis (it, it->len);
7224 else
7226 /* The face at the current position may be different from the
7227 face we find after the invisible text. Remember what it
7228 was in IT->saved_face_id, and signal that it's there by
7229 setting face_before_selective_p. */
7230 it->saved_face_id = it->face_id;
7231 it->method = GET_FROM_BUFFER;
7232 it->object = it->w->buffer;
7233 reseat_at_next_visible_line_start (it, 1);
7234 it->face_before_selective_p = 1;
7237 return GET_NEXT_DISPLAY_ELEMENT (it);
7241 /* Deliver an image display element. The iterator IT is already
7242 filled with image information (done in handle_display_prop). Value
7243 is always 1. */
7246 static int
7247 next_element_from_image (struct it *it)
7249 it->what = IT_IMAGE;
7250 it->ignore_overlay_strings_at_pos_p = 0;
7251 return 1;
7255 /* Fill iterator IT with next display element from a stretch glyph
7256 property. IT->object is the value of the text property. Value is
7257 always 1. */
7259 static int
7260 next_element_from_stretch (struct it *it)
7262 it->what = IT_STRETCH;
7263 return 1;
7266 /* Scan backwards from IT's current position until we find a stop
7267 position, or until BEGV. This is called when we find ourself
7268 before both the last known prev_stop and base_level_stop while
7269 reordering bidirectional text. */
7271 static void
7272 compute_stop_pos_backwards (struct it *it)
7274 const int SCAN_BACK_LIMIT = 1000;
7275 struct text_pos pos;
7276 struct display_pos save_current = it->current;
7277 struct text_pos save_position = it->position;
7278 EMACS_INT charpos = IT_CHARPOS (*it);
7279 EMACS_INT where_we_are = charpos;
7280 EMACS_INT save_stop_pos = it->stop_charpos;
7281 EMACS_INT save_end_pos = it->end_charpos;
7283 xassert (NILP (it->string) && !it->s);
7284 xassert (it->bidi_p);
7285 it->bidi_p = 0;
7288 it->end_charpos = min (charpos + 1, ZV);
7289 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7290 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7291 reseat_1 (it, pos, 0);
7292 compute_stop_pos (it);
7293 /* We must advance forward, right? */
7294 if (it->stop_charpos <= charpos)
7295 abort ();
7297 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7299 if (it->stop_charpos <= where_we_are)
7300 it->prev_stop = it->stop_charpos;
7301 else
7302 it->prev_stop = BEGV;
7303 it->bidi_p = 1;
7304 it->current = save_current;
7305 it->position = save_position;
7306 it->stop_charpos = save_stop_pos;
7307 it->end_charpos = save_end_pos;
7310 /* Scan forward from CHARPOS in the current buffer/string, until we
7311 find a stop position > current IT's position. Then handle the stop
7312 position before that. This is called when we bump into a stop
7313 position while reordering bidirectional text. CHARPOS should be
7314 the last previously processed stop_pos (or BEGV/0, if none were
7315 processed yet) whose position is less that IT's current
7316 position. */
7318 static void
7319 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7321 int bufp = !STRINGP (it->string);
7322 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7323 struct display_pos save_current = it->current;
7324 struct text_pos save_position = it->position;
7325 struct text_pos pos1;
7326 EMACS_INT next_stop;
7328 /* Scan in strict logical order. */
7329 xassert (it->bidi_p);
7330 it->bidi_p = 0;
7333 it->prev_stop = charpos;
7334 if (bufp)
7336 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7337 reseat_1 (it, pos1, 0);
7339 else
7340 it->current.string_pos = string_pos (charpos, it->string);
7341 compute_stop_pos (it);
7342 /* We must advance forward, right? */
7343 if (it->stop_charpos <= it->prev_stop)
7344 abort ();
7345 charpos = it->stop_charpos;
7347 while (charpos <= where_we_are);
7349 it->bidi_p = 1;
7350 it->current = save_current;
7351 it->position = save_position;
7352 next_stop = it->stop_charpos;
7353 it->stop_charpos = it->prev_stop;
7354 handle_stop (it);
7355 it->stop_charpos = next_stop;
7358 /* Load IT with the next display element from current_buffer. Value
7359 is zero if end of buffer reached. IT->stop_charpos is the next
7360 position at which to stop and check for text properties or buffer
7361 end. */
7363 static int
7364 next_element_from_buffer (struct it *it)
7366 int success_p = 1;
7368 xassert (IT_CHARPOS (*it) >= BEGV);
7369 xassert (NILP (it->string) && !it->s);
7370 xassert (!it->bidi_p
7371 || (EQ (it->bidi_it.string.lstring, Qnil)
7372 && it->bidi_it.string.s == NULL));
7374 /* With bidi reordering, the character to display might not be the
7375 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7376 we were reseat()ed to a new buffer position, which is potentially
7377 a different paragraph. */
7378 if (it->bidi_p && it->bidi_it.first_elt)
7380 get_visually_first_element (it);
7381 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7384 if (IT_CHARPOS (*it) >= it->stop_charpos)
7386 if (IT_CHARPOS (*it) >= it->end_charpos)
7388 int overlay_strings_follow_p;
7390 /* End of the game, except when overlay strings follow that
7391 haven't been returned yet. */
7392 if (it->overlay_strings_at_end_processed_p)
7393 overlay_strings_follow_p = 0;
7394 else
7396 it->overlay_strings_at_end_processed_p = 1;
7397 overlay_strings_follow_p = get_overlay_strings (it, 0);
7400 if (overlay_strings_follow_p)
7401 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7402 else
7404 it->what = IT_EOB;
7405 it->position = it->current.pos;
7406 success_p = 0;
7409 else if (!(!it->bidi_p
7410 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7411 || IT_CHARPOS (*it) == it->stop_charpos))
7413 /* With bidi non-linear iteration, we could find ourselves
7414 far beyond the last computed stop_charpos, with several
7415 other stop positions in between that we missed. Scan
7416 them all now, in buffer's logical order, until we find
7417 and handle the last stop_charpos that precedes our
7418 current position. */
7419 handle_stop_backwards (it, it->stop_charpos);
7420 return GET_NEXT_DISPLAY_ELEMENT (it);
7422 else
7424 if (it->bidi_p)
7426 /* Take note of the stop position we just moved across,
7427 for when we will move back across it. */
7428 it->prev_stop = it->stop_charpos;
7429 /* If we are at base paragraph embedding level, take
7430 note of the last stop position seen at this
7431 level. */
7432 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7433 it->base_level_stop = it->stop_charpos;
7435 handle_stop (it);
7436 return GET_NEXT_DISPLAY_ELEMENT (it);
7439 else if (it->bidi_p
7440 /* If we are before prev_stop, we may have overstepped on
7441 our way backwards a stop_pos, and if so, we need to
7442 handle that stop_pos. */
7443 && IT_CHARPOS (*it) < it->prev_stop
7444 /* We can sometimes back up for reasons that have nothing
7445 to do with bidi reordering. E.g., compositions. The
7446 code below is only needed when we are above the base
7447 embedding level, so test for that explicitly. */
7448 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7450 if (it->base_level_stop <= 0
7451 || IT_CHARPOS (*it) < it->base_level_stop)
7453 /* If we lost track of base_level_stop, we need to find
7454 prev_stop by looking backwards. This happens, e.g., when
7455 we were reseated to the previous screenful of text by
7456 vertical-motion. */
7457 it->base_level_stop = BEGV;
7458 compute_stop_pos_backwards (it);
7459 handle_stop_backwards (it, it->prev_stop);
7461 else
7462 handle_stop_backwards (it, it->base_level_stop);
7463 return GET_NEXT_DISPLAY_ELEMENT (it);
7465 else
7467 /* No face changes, overlays etc. in sight, so just return a
7468 character from current_buffer. */
7469 unsigned char *p;
7470 EMACS_INT stop;
7472 /* Maybe run the redisplay end trigger hook. Performance note:
7473 This doesn't seem to cost measurable time. */
7474 if (it->redisplay_end_trigger_charpos
7475 && it->glyph_row
7476 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7477 run_redisplay_end_trigger_hook (it);
7479 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7480 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7481 stop)
7482 && next_element_from_composition (it))
7484 return 1;
7487 /* Get the next character, maybe multibyte. */
7488 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7489 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7490 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7491 else
7492 it->c = *p, it->len = 1;
7494 /* Record what we have and where it came from. */
7495 it->what = IT_CHARACTER;
7496 it->object = it->w->buffer;
7497 it->position = it->current.pos;
7499 /* Normally we return the character found above, except when we
7500 really want to return an ellipsis for selective display. */
7501 if (it->selective)
7503 if (it->c == '\n')
7505 /* A value of selective > 0 means hide lines indented more
7506 than that number of columns. */
7507 if (it->selective > 0
7508 && IT_CHARPOS (*it) + 1 < ZV
7509 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7510 IT_BYTEPOS (*it) + 1,
7511 it->selective))
7513 success_p = next_element_from_ellipsis (it);
7514 it->dpvec_char_len = -1;
7517 else if (it->c == '\r' && it->selective == -1)
7519 /* A value of selective == -1 means that everything from the
7520 CR to the end of the line is invisible, with maybe an
7521 ellipsis displayed for it. */
7522 success_p = next_element_from_ellipsis (it);
7523 it->dpvec_char_len = -1;
7528 /* Value is zero if end of buffer reached. */
7529 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7530 return success_p;
7534 /* Run the redisplay end trigger hook for IT. */
7536 static void
7537 run_redisplay_end_trigger_hook (struct it *it)
7539 Lisp_Object args[3];
7541 /* IT->glyph_row should be non-null, i.e. we should be actually
7542 displaying something, or otherwise we should not run the hook. */
7543 xassert (it->glyph_row);
7545 /* Set up hook arguments. */
7546 args[0] = Qredisplay_end_trigger_functions;
7547 args[1] = it->window;
7548 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7549 it->redisplay_end_trigger_charpos = 0;
7551 /* Since we are *trying* to run these functions, don't try to run
7552 them again, even if they get an error. */
7553 it->w->redisplay_end_trigger = Qnil;
7554 Frun_hook_with_args (3, args);
7556 /* Notice if it changed the face of the character we are on. */
7557 handle_face_prop (it);
7561 /* Deliver a composition display element. Unlike the other
7562 next_element_from_XXX, this function is not registered in the array
7563 get_next_element[]. It is called from next_element_from_buffer and
7564 next_element_from_string when necessary. */
7566 static int
7567 next_element_from_composition (struct it *it)
7569 it->what = IT_COMPOSITION;
7570 it->len = it->cmp_it.nbytes;
7571 if (STRINGP (it->string))
7573 if (it->c < 0)
7575 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7576 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7577 return 0;
7579 it->position = it->current.string_pos;
7580 it->object = it->string;
7581 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7582 IT_STRING_BYTEPOS (*it), it->string);
7584 else
7586 if (it->c < 0)
7588 IT_CHARPOS (*it) += it->cmp_it.nchars;
7589 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7590 if (it->bidi_p)
7592 if (it->bidi_it.new_paragraph)
7593 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7594 /* Resync the bidi iterator with IT's new position.
7595 FIXME: this doesn't support bidirectional text. */
7596 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7597 bidi_move_to_visually_next (&it->bidi_it);
7599 return 0;
7601 it->position = it->current.pos;
7602 it->object = it->w->buffer;
7603 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7604 IT_BYTEPOS (*it), Qnil);
7606 return 1;
7611 /***********************************************************************
7612 Moving an iterator without producing glyphs
7613 ***********************************************************************/
7615 /* Check if iterator is at a position corresponding to a valid buffer
7616 position after some move_it_ call. */
7618 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7619 ((it)->method == GET_FROM_STRING \
7620 ? IT_STRING_CHARPOS (*it) == 0 \
7621 : 1)
7624 /* Move iterator IT to a specified buffer or X position within one
7625 line on the display without producing glyphs.
7627 OP should be a bit mask including some or all of these bits:
7628 MOVE_TO_X: Stop upon reaching x-position TO_X.
7629 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7630 Regardless of OP's value, stop upon reaching the end of the display line.
7632 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7633 This means, in particular, that TO_X includes window's horizontal
7634 scroll amount.
7636 The return value has several possible values that
7637 say what condition caused the scan to stop:
7639 MOVE_POS_MATCH_OR_ZV
7640 - when TO_POS or ZV was reached.
7642 MOVE_X_REACHED
7643 -when TO_X was reached before TO_POS or ZV were reached.
7645 MOVE_LINE_CONTINUED
7646 - when we reached the end of the display area and the line must
7647 be continued.
7649 MOVE_LINE_TRUNCATED
7650 - when we reached the end of the display area and the line is
7651 truncated.
7653 MOVE_NEWLINE_OR_CR
7654 - when we stopped at a line end, i.e. a newline or a CR and selective
7655 display is on. */
7657 static enum move_it_result
7658 move_it_in_display_line_to (struct it *it,
7659 EMACS_INT to_charpos, int to_x,
7660 enum move_operation_enum op)
7662 enum move_it_result result = MOVE_UNDEFINED;
7663 struct glyph_row *saved_glyph_row;
7664 struct it wrap_it, atpos_it, atx_it, ppos_it;
7665 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7666 void *ppos_data = NULL;
7667 int may_wrap = 0;
7668 enum it_method prev_method = it->method;
7669 EMACS_INT prev_pos = IT_CHARPOS (*it);
7670 int saw_smaller_pos = prev_pos < to_charpos;
7672 /* Don't produce glyphs in produce_glyphs. */
7673 saved_glyph_row = it->glyph_row;
7674 it->glyph_row = NULL;
7676 /* Use wrap_it to save a copy of IT wherever a word wrap could
7677 occur. Use atpos_it to save a copy of IT at the desired buffer
7678 position, if found, so that we can scan ahead and check if the
7679 word later overshoots the window edge. Use atx_it similarly, for
7680 pixel positions. */
7681 wrap_it.sp = -1;
7682 atpos_it.sp = -1;
7683 atx_it.sp = -1;
7685 /* Use ppos_it under bidi reordering to save a copy of IT for the
7686 position > CHARPOS that is the closest to CHARPOS. We restore
7687 that position in IT when we have scanned the entire display line
7688 without finding a match for CHARPOS and all the character
7689 positions are greater than CHARPOS. */
7690 if (it->bidi_p)
7692 SAVE_IT (ppos_it, *it, ppos_data);
7693 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7694 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7695 SAVE_IT (ppos_it, *it, ppos_data);
7698 #define BUFFER_POS_REACHED_P() \
7699 ((op & MOVE_TO_POS) != 0 \
7700 && BUFFERP (it->object) \
7701 && (IT_CHARPOS (*it) == to_charpos \
7702 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7703 && (it->method == GET_FROM_BUFFER \
7704 || (it->method == GET_FROM_DISPLAY_VECTOR \
7705 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7707 /* If there's a line-/wrap-prefix, handle it. */
7708 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7709 && it->current_y < it->last_visible_y)
7710 handle_line_prefix (it);
7712 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7713 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7715 while (1)
7717 int x, i, ascent = 0, descent = 0;
7719 /* Utility macro to reset an iterator with x, ascent, and descent. */
7720 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7721 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7722 (IT)->max_descent = descent)
7724 /* Stop if we move beyond TO_CHARPOS (after an image or a
7725 display string or stretch glyph). */
7726 if ((op & MOVE_TO_POS) != 0
7727 && BUFFERP (it->object)
7728 && it->method == GET_FROM_BUFFER
7729 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7730 || (it->bidi_p
7731 && (prev_method == GET_FROM_IMAGE
7732 || prev_method == GET_FROM_STRETCH
7733 || prev_method == GET_FROM_STRING)
7734 /* Passed TO_CHARPOS from left to right. */
7735 && ((prev_pos < to_charpos
7736 && IT_CHARPOS (*it) > to_charpos)
7737 /* Passed TO_CHARPOS from right to left. */
7738 || (prev_pos > to_charpos
7739 && IT_CHARPOS (*it) < to_charpos)))))
7741 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7743 result = MOVE_POS_MATCH_OR_ZV;
7744 break;
7746 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7747 /* If wrap_it is valid, the current position might be in a
7748 word that is wrapped. So, save the iterator in
7749 atpos_it and continue to see if wrapping happens. */
7750 SAVE_IT (atpos_it, *it, atpos_data);
7753 /* Stop when ZV reached.
7754 We used to stop here when TO_CHARPOS reached as well, but that is
7755 too soon if this glyph does not fit on this line. So we handle it
7756 explicitly below. */
7757 if (!get_next_display_element (it))
7759 result = MOVE_POS_MATCH_OR_ZV;
7760 break;
7763 if (it->line_wrap == TRUNCATE)
7765 if (BUFFER_POS_REACHED_P ())
7767 result = MOVE_POS_MATCH_OR_ZV;
7768 break;
7771 else
7773 if (it->line_wrap == WORD_WRAP)
7775 if (IT_DISPLAYING_WHITESPACE (it))
7776 may_wrap = 1;
7777 else if (may_wrap)
7779 /* We have reached a glyph that follows one or more
7780 whitespace characters. If the position is
7781 already found, we are done. */
7782 if (atpos_it.sp >= 0)
7784 RESTORE_IT (it, &atpos_it, atpos_data);
7785 result = MOVE_POS_MATCH_OR_ZV;
7786 goto done;
7788 if (atx_it.sp >= 0)
7790 RESTORE_IT (it, &atx_it, atx_data);
7791 result = MOVE_X_REACHED;
7792 goto done;
7794 /* Otherwise, we can wrap here. */
7795 SAVE_IT (wrap_it, *it, wrap_data);
7796 may_wrap = 0;
7801 /* Remember the line height for the current line, in case
7802 the next element doesn't fit on the line. */
7803 ascent = it->max_ascent;
7804 descent = it->max_descent;
7806 /* The call to produce_glyphs will get the metrics of the
7807 display element IT is loaded with. Record the x-position
7808 before this display element, in case it doesn't fit on the
7809 line. */
7810 x = it->current_x;
7812 PRODUCE_GLYPHS (it);
7814 if (it->area != TEXT_AREA)
7816 prev_method = it->method;
7817 if (it->method == GET_FROM_BUFFER)
7818 prev_pos = IT_CHARPOS (*it);
7819 set_iterator_to_next (it, 1);
7820 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7821 SET_TEXT_POS (this_line_min_pos,
7822 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7823 if (it->bidi_p
7824 && (op & MOVE_TO_POS)
7825 && IT_CHARPOS (*it) > to_charpos
7826 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
7827 SAVE_IT (ppos_it, *it, ppos_data);
7828 continue;
7831 /* The number of glyphs we get back in IT->nglyphs will normally
7832 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7833 character on a terminal frame, or (iii) a line end. For the
7834 second case, IT->nglyphs - 1 padding glyphs will be present.
7835 (On X frames, there is only one glyph produced for a
7836 composite character.)
7838 The behavior implemented below means, for continuation lines,
7839 that as many spaces of a TAB as fit on the current line are
7840 displayed there. For terminal frames, as many glyphs of a
7841 multi-glyph character are displayed in the current line, too.
7842 This is what the old redisplay code did, and we keep it that
7843 way. Under X, the whole shape of a complex character must
7844 fit on the line or it will be completely displayed in the
7845 next line.
7847 Note that both for tabs and padding glyphs, all glyphs have
7848 the same width. */
7849 if (it->nglyphs)
7851 /* More than one glyph or glyph doesn't fit on line. All
7852 glyphs have the same width. */
7853 int single_glyph_width = it->pixel_width / it->nglyphs;
7854 int new_x;
7855 int x_before_this_char = x;
7856 int hpos_before_this_char = it->hpos;
7858 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7860 new_x = x + single_glyph_width;
7862 /* We want to leave anything reaching TO_X to the caller. */
7863 if ((op & MOVE_TO_X) && new_x > to_x)
7865 if (BUFFER_POS_REACHED_P ())
7867 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7868 goto buffer_pos_reached;
7869 if (atpos_it.sp < 0)
7871 SAVE_IT (atpos_it, *it, atpos_data);
7872 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7875 else
7877 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7879 it->current_x = x;
7880 result = MOVE_X_REACHED;
7881 break;
7883 if (atx_it.sp < 0)
7885 SAVE_IT (atx_it, *it, atx_data);
7886 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7891 if (/* Lines are continued. */
7892 it->line_wrap != TRUNCATE
7893 && (/* And glyph doesn't fit on the line. */
7894 new_x > it->last_visible_x
7895 /* Or it fits exactly and we're on a window
7896 system frame. */
7897 || (new_x == it->last_visible_x
7898 && FRAME_WINDOW_P (it->f))))
7900 if (/* IT->hpos == 0 means the very first glyph
7901 doesn't fit on the line, e.g. a wide image. */
7902 it->hpos == 0
7903 || (new_x == it->last_visible_x
7904 && FRAME_WINDOW_P (it->f)))
7906 ++it->hpos;
7907 it->current_x = new_x;
7909 /* The character's last glyph just barely fits
7910 in this row. */
7911 if (i == it->nglyphs - 1)
7913 /* If this is the destination position,
7914 return a position *before* it in this row,
7915 now that we know it fits in this row. */
7916 if (BUFFER_POS_REACHED_P ())
7918 if (it->line_wrap != WORD_WRAP
7919 || wrap_it.sp < 0)
7921 it->hpos = hpos_before_this_char;
7922 it->current_x = x_before_this_char;
7923 result = MOVE_POS_MATCH_OR_ZV;
7924 break;
7926 if (it->line_wrap == WORD_WRAP
7927 && atpos_it.sp < 0)
7929 SAVE_IT (atpos_it, *it, atpos_data);
7930 atpos_it.current_x = x_before_this_char;
7931 atpos_it.hpos = hpos_before_this_char;
7935 prev_method = it->method;
7936 if (it->method == GET_FROM_BUFFER)
7937 prev_pos = IT_CHARPOS (*it);
7938 set_iterator_to_next (it, 1);
7939 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7940 SET_TEXT_POS (this_line_min_pos,
7941 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7942 /* On graphical terminals, newlines may
7943 "overflow" into the fringe if
7944 overflow-newline-into-fringe is non-nil.
7945 On text-only terminals, newlines may
7946 overflow into the last glyph on the
7947 display line.*/
7948 if (!FRAME_WINDOW_P (it->f)
7949 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7951 if (!get_next_display_element (it))
7953 result = MOVE_POS_MATCH_OR_ZV;
7954 break;
7956 if (BUFFER_POS_REACHED_P ())
7958 if (ITERATOR_AT_END_OF_LINE_P (it))
7959 result = MOVE_POS_MATCH_OR_ZV;
7960 else
7961 result = MOVE_LINE_CONTINUED;
7962 break;
7964 if (ITERATOR_AT_END_OF_LINE_P (it))
7966 result = MOVE_NEWLINE_OR_CR;
7967 break;
7972 else
7973 IT_RESET_X_ASCENT_DESCENT (it);
7975 if (wrap_it.sp >= 0)
7977 RESTORE_IT (it, &wrap_it, wrap_data);
7978 atpos_it.sp = -1;
7979 atx_it.sp = -1;
7982 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7983 IT_CHARPOS (*it)));
7984 result = MOVE_LINE_CONTINUED;
7985 break;
7988 if (BUFFER_POS_REACHED_P ())
7990 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7991 goto buffer_pos_reached;
7992 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7994 SAVE_IT (atpos_it, *it, atpos_data);
7995 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7999 if (new_x > it->first_visible_x)
8001 /* Glyph is visible. Increment number of glyphs that
8002 would be displayed. */
8003 ++it->hpos;
8007 if (result != MOVE_UNDEFINED)
8008 break;
8010 else if (BUFFER_POS_REACHED_P ())
8012 buffer_pos_reached:
8013 IT_RESET_X_ASCENT_DESCENT (it);
8014 result = MOVE_POS_MATCH_OR_ZV;
8015 break;
8017 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8019 /* Stop when TO_X specified and reached. This check is
8020 necessary here because of lines consisting of a line end,
8021 only. The line end will not produce any glyphs and we
8022 would never get MOVE_X_REACHED. */
8023 xassert (it->nglyphs == 0);
8024 result = MOVE_X_REACHED;
8025 break;
8028 /* Is this a line end? If yes, we're done. */
8029 if (ITERATOR_AT_END_OF_LINE_P (it))
8031 /* If we are past TO_CHARPOS, but never saw any character
8032 positions smaller than TO_CHARPOS, return
8033 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8034 did. */
8035 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8037 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8039 if (IT_CHARPOS (ppos_it) < ZV)
8040 RESTORE_IT (it, &ppos_it, ppos_data);
8041 goto buffer_pos_reached;
8043 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8044 && IT_CHARPOS (*it) > to_charpos)
8045 goto buffer_pos_reached;
8046 else
8047 result = MOVE_NEWLINE_OR_CR;
8049 else
8050 result = MOVE_NEWLINE_OR_CR;
8051 break;
8054 prev_method = it->method;
8055 if (it->method == GET_FROM_BUFFER)
8056 prev_pos = IT_CHARPOS (*it);
8057 /* The current display element has been consumed. Advance
8058 to the next. */
8059 set_iterator_to_next (it, 1);
8060 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8061 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8062 if (IT_CHARPOS (*it) < to_charpos)
8063 saw_smaller_pos = 1;
8064 if (it->bidi_p
8065 && (op & MOVE_TO_POS)
8066 && IT_CHARPOS (*it) >= to_charpos
8067 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8068 SAVE_IT (ppos_it, *it, ppos_data);
8070 /* Stop if lines are truncated and IT's current x-position is
8071 past the right edge of the window now. */
8072 if (it->line_wrap == TRUNCATE
8073 && it->current_x >= it->last_visible_x)
8075 if (!FRAME_WINDOW_P (it->f)
8076 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8078 int at_eob_p = 0;
8080 if ((at_eob_p = !get_next_display_element (it))
8081 || BUFFER_POS_REACHED_P ()
8082 /* If we are past TO_CHARPOS, but never saw any
8083 character positions smaller than TO_CHARPOS,
8084 return MOVE_POS_MATCH_OR_ZV, like the
8085 unidirectional display did. */
8086 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8087 && !saw_smaller_pos
8088 && IT_CHARPOS (*it) > to_charpos))
8090 if (!at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8091 RESTORE_IT (it, &ppos_it, ppos_data);
8092 goto buffer_pos_reached;
8094 if (ITERATOR_AT_END_OF_LINE_P (it))
8096 result = MOVE_NEWLINE_OR_CR;
8097 break;
8100 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8101 && !saw_smaller_pos
8102 && IT_CHARPOS (*it) > to_charpos)
8104 if (IT_CHARPOS (ppos_it) < ZV)
8105 RESTORE_IT (it, &ppos_it, ppos_data);
8106 goto buffer_pos_reached;
8108 result = MOVE_LINE_TRUNCATED;
8109 break;
8111 #undef IT_RESET_X_ASCENT_DESCENT
8114 #undef BUFFER_POS_REACHED_P
8116 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8117 restore the saved iterator. */
8118 if (atpos_it.sp >= 0)
8119 RESTORE_IT (it, &atpos_it, atpos_data);
8120 else if (atx_it.sp >= 0)
8121 RESTORE_IT (it, &atx_it, atx_data);
8123 done:
8125 if (atpos_data)
8126 bidi_unshelve_cache (atpos_data, 1);
8127 if (atx_data)
8128 bidi_unshelve_cache (atx_data, 1);
8129 if (wrap_data)
8130 bidi_unshelve_cache (wrap_data, 1);
8131 if (ppos_data)
8132 bidi_unshelve_cache (ppos_data, 1);
8134 /* Restore the iterator settings altered at the beginning of this
8135 function. */
8136 it->glyph_row = saved_glyph_row;
8137 return result;
8140 /* For external use. */
8141 void
8142 move_it_in_display_line (struct it *it,
8143 EMACS_INT to_charpos, int to_x,
8144 enum move_operation_enum op)
8146 if (it->line_wrap == WORD_WRAP
8147 && (op & MOVE_TO_X))
8149 struct it save_it;
8150 void *save_data = NULL;
8151 int skip;
8153 SAVE_IT (save_it, *it, save_data);
8154 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8155 /* When word-wrap is on, TO_X may lie past the end
8156 of a wrapped line. Then it->current is the
8157 character on the next line, so backtrack to the
8158 space before the wrap point. */
8159 if (skip == MOVE_LINE_CONTINUED)
8161 int prev_x = max (it->current_x - 1, 0);
8162 RESTORE_IT (it, &save_it, save_data);
8163 move_it_in_display_line_to
8164 (it, -1, prev_x, MOVE_TO_X);
8166 else
8167 bidi_unshelve_cache (save_data, 1);
8169 else
8170 move_it_in_display_line_to (it, to_charpos, to_x, op);
8174 /* Move IT forward until it satisfies one or more of the criteria in
8175 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8177 OP is a bit-mask that specifies where to stop, and in particular,
8178 which of those four position arguments makes a difference. See the
8179 description of enum move_operation_enum.
8181 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8182 screen line, this function will set IT to the next position that is
8183 displayed to the right of TO_CHARPOS on the screen. */
8185 void
8186 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8188 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8189 int line_height, line_start_x = 0, reached = 0;
8190 void *backup_data = NULL;
8192 for (;;)
8194 if (op & MOVE_TO_VPOS)
8196 /* If no TO_CHARPOS and no TO_X specified, stop at the
8197 start of the line TO_VPOS. */
8198 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8200 if (it->vpos == to_vpos)
8202 reached = 1;
8203 break;
8205 else
8206 skip = move_it_in_display_line_to (it, -1, -1, 0);
8208 else
8210 /* TO_VPOS >= 0 means stop at TO_X in the line at
8211 TO_VPOS, or at TO_POS, whichever comes first. */
8212 if (it->vpos == to_vpos)
8214 reached = 2;
8215 break;
8218 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8220 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8222 reached = 3;
8223 break;
8225 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8227 /* We have reached TO_X but not in the line we want. */
8228 skip = move_it_in_display_line_to (it, to_charpos,
8229 -1, MOVE_TO_POS);
8230 if (skip == MOVE_POS_MATCH_OR_ZV)
8232 reached = 4;
8233 break;
8238 else if (op & MOVE_TO_Y)
8240 struct it it_backup;
8242 if (it->line_wrap == WORD_WRAP)
8243 SAVE_IT (it_backup, *it, backup_data);
8245 /* TO_Y specified means stop at TO_X in the line containing
8246 TO_Y---or at TO_CHARPOS if this is reached first. The
8247 problem is that we can't really tell whether the line
8248 contains TO_Y before we have completely scanned it, and
8249 this may skip past TO_X. What we do is to first scan to
8250 TO_X.
8252 If TO_X is not specified, use a TO_X of zero. The reason
8253 is to make the outcome of this function more predictable.
8254 If we didn't use TO_X == 0, we would stop at the end of
8255 the line which is probably not what a caller would expect
8256 to happen. */
8257 skip = move_it_in_display_line_to
8258 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8259 (MOVE_TO_X | (op & MOVE_TO_POS)));
8261 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8262 if (skip == MOVE_POS_MATCH_OR_ZV)
8263 reached = 5;
8264 else if (skip == MOVE_X_REACHED)
8266 /* If TO_X was reached, we want to know whether TO_Y is
8267 in the line. We know this is the case if the already
8268 scanned glyphs make the line tall enough. Otherwise,
8269 we must check by scanning the rest of the line. */
8270 line_height = it->max_ascent + it->max_descent;
8271 if (to_y >= it->current_y
8272 && to_y < it->current_y + line_height)
8274 reached = 6;
8275 break;
8277 SAVE_IT (it_backup, *it, backup_data);
8278 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8279 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8280 op & MOVE_TO_POS);
8281 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8282 line_height = it->max_ascent + it->max_descent;
8283 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8285 if (to_y >= it->current_y
8286 && to_y < it->current_y + line_height)
8288 /* If TO_Y is in this line and TO_X was reached
8289 above, we scanned too far. We have to restore
8290 IT's settings to the ones before skipping. */
8291 RESTORE_IT (it, &it_backup, backup_data);
8292 reached = 6;
8294 else
8296 skip = skip2;
8297 if (skip == MOVE_POS_MATCH_OR_ZV)
8298 reached = 7;
8301 else
8303 /* Check whether TO_Y is in this line. */
8304 line_height = it->max_ascent + it->max_descent;
8305 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8307 if (to_y >= it->current_y
8308 && to_y < it->current_y + line_height)
8310 /* When word-wrap is on, TO_X may lie past the end
8311 of a wrapped line. Then it->current is the
8312 character on the next line, so backtrack to the
8313 space before the wrap point. */
8314 if (skip == MOVE_LINE_CONTINUED
8315 && it->line_wrap == WORD_WRAP)
8317 int prev_x = max (it->current_x - 1, 0);
8318 RESTORE_IT (it, &it_backup, backup_data);
8319 skip = move_it_in_display_line_to
8320 (it, -1, prev_x, MOVE_TO_X);
8322 reached = 6;
8326 if (reached)
8327 break;
8329 else if (BUFFERP (it->object)
8330 && (it->method == GET_FROM_BUFFER
8331 || it->method == GET_FROM_STRETCH)
8332 && IT_CHARPOS (*it) >= to_charpos)
8333 skip = MOVE_POS_MATCH_OR_ZV;
8334 else
8335 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8337 switch (skip)
8339 case MOVE_POS_MATCH_OR_ZV:
8340 reached = 8;
8341 goto out;
8343 case MOVE_NEWLINE_OR_CR:
8344 set_iterator_to_next (it, 1);
8345 it->continuation_lines_width = 0;
8346 break;
8348 case MOVE_LINE_TRUNCATED:
8349 it->continuation_lines_width = 0;
8350 reseat_at_next_visible_line_start (it, 0);
8351 if ((op & MOVE_TO_POS) != 0
8352 && IT_CHARPOS (*it) > to_charpos)
8354 reached = 9;
8355 goto out;
8357 break;
8359 case MOVE_LINE_CONTINUED:
8360 /* For continued lines ending in a tab, some of the glyphs
8361 associated with the tab are displayed on the current
8362 line. Since it->current_x does not include these glyphs,
8363 we use it->last_visible_x instead. */
8364 if (it->c == '\t')
8366 it->continuation_lines_width += it->last_visible_x;
8367 /* When moving by vpos, ensure that the iterator really
8368 advances to the next line (bug#847, bug#969). Fixme:
8369 do we need to do this in other circumstances? */
8370 if (it->current_x != it->last_visible_x
8371 && (op & MOVE_TO_VPOS)
8372 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8374 line_start_x = it->current_x + it->pixel_width
8375 - it->last_visible_x;
8376 set_iterator_to_next (it, 0);
8379 else
8380 it->continuation_lines_width += it->current_x;
8381 break;
8383 default:
8384 abort ();
8387 /* Reset/increment for the next run. */
8388 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8389 it->current_x = line_start_x;
8390 line_start_x = 0;
8391 it->hpos = 0;
8392 it->current_y += it->max_ascent + it->max_descent;
8393 ++it->vpos;
8394 last_height = it->max_ascent + it->max_descent;
8395 last_max_ascent = it->max_ascent;
8396 it->max_ascent = it->max_descent = 0;
8399 out:
8401 /* On text terminals, we may stop at the end of a line in the middle
8402 of a multi-character glyph. If the glyph itself is continued,
8403 i.e. it is actually displayed on the next line, don't treat this
8404 stopping point as valid; move to the next line instead (unless
8405 that brings us offscreen). */
8406 if (!FRAME_WINDOW_P (it->f)
8407 && op & MOVE_TO_POS
8408 && IT_CHARPOS (*it) == to_charpos
8409 && it->what == IT_CHARACTER
8410 && it->nglyphs > 1
8411 && it->line_wrap == WINDOW_WRAP
8412 && it->current_x == it->last_visible_x - 1
8413 && it->c != '\n'
8414 && it->c != '\t'
8415 && it->vpos < XFASTINT (it->w->window_end_vpos))
8417 it->continuation_lines_width += it->current_x;
8418 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8419 it->current_y += it->max_ascent + it->max_descent;
8420 ++it->vpos;
8421 last_height = it->max_ascent + it->max_descent;
8422 last_max_ascent = it->max_ascent;
8425 if (backup_data)
8426 bidi_unshelve_cache (backup_data, 1);
8428 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8432 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8434 If DY > 0, move IT backward at least that many pixels. DY = 0
8435 means move IT backward to the preceding line start or BEGV. This
8436 function may move over more than DY pixels if IT->current_y - DY
8437 ends up in the middle of a line; in this case IT->current_y will be
8438 set to the top of the line moved to. */
8440 void
8441 move_it_vertically_backward (struct it *it, int dy)
8443 int nlines, h;
8444 struct it it2, it3;
8445 void *it2data = NULL, *it3data = NULL;
8446 EMACS_INT start_pos;
8448 move_further_back:
8449 xassert (dy >= 0);
8451 start_pos = IT_CHARPOS (*it);
8453 /* Estimate how many newlines we must move back. */
8454 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8456 /* Set the iterator's position that many lines back. */
8457 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8458 back_to_previous_visible_line_start (it);
8460 /* Reseat the iterator here. When moving backward, we don't want
8461 reseat to skip forward over invisible text, set up the iterator
8462 to deliver from overlay strings at the new position etc. So,
8463 use reseat_1 here. */
8464 reseat_1 (it, it->current.pos, 1);
8466 /* We are now surely at a line start. */
8467 it->current_x = it->hpos = 0;
8468 it->continuation_lines_width = 0;
8470 /* Move forward and see what y-distance we moved. First move to the
8471 start of the next line so that we get its height. We need this
8472 height to be able to tell whether we reached the specified
8473 y-distance. */
8474 SAVE_IT (it2, *it, it2data);
8475 it2.max_ascent = it2.max_descent = 0;
8478 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8479 MOVE_TO_POS | MOVE_TO_VPOS);
8481 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8482 xassert (IT_CHARPOS (*it) >= BEGV);
8483 SAVE_IT (it3, it2, it3data);
8485 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8486 xassert (IT_CHARPOS (*it) >= BEGV);
8487 /* H is the actual vertical distance from the position in *IT
8488 and the starting position. */
8489 h = it2.current_y - it->current_y;
8490 /* NLINES is the distance in number of lines. */
8491 nlines = it2.vpos - it->vpos;
8493 /* Correct IT's y and vpos position
8494 so that they are relative to the starting point. */
8495 it->vpos -= nlines;
8496 it->current_y -= h;
8498 if (dy == 0)
8500 /* DY == 0 means move to the start of the screen line. The
8501 value of nlines is > 0 if continuation lines were involved. */
8502 RESTORE_IT (it, it, it2data);
8503 if (nlines > 0)
8504 move_it_by_lines (it, nlines);
8505 bidi_unshelve_cache (it3data, 1);
8507 else
8509 /* The y-position we try to reach, relative to *IT.
8510 Note that H has been subtracted in front of the if-statement. */
8511 int target_y = it->current_y + h - dy;
8512 int y0 = it3.current_y;
8513 int y1;
8514 int line_height;
8516 RESTORE_IT (&it3, &it3, it3data);
8517 y1 = line_bottom_y (&it3);
8518 line_height = y1 - y0;
8519 RESTORE_IT (it, it, it2data);
8520 /* If we did not reach target_y, try to move further backward if
8521 we can. If we moved too far backward, try to move forward. */
8522 if (target_y < it->current_y
8523 /* This is heuristic. In a window that's 3 lines high, with
8524 a line height of 13 pixels each, recentering with point
8525 on the bottom line will try to move -39/2 = 19 pixels
8526 backward. Try to avoid moving into the first line. */
8527 && (it->current_y - target_y
8528 > min (window_box_height (it->w), line_height * 2 / 3))
8529 && IT_CHARPOS (*it) > BEGV)
8531 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8532 target_y - it->current_y));
8533 dy = it->current_y - target_y;
8534 goto move_further_back;
8536 else if (target_y >= it->current_y + line_height
8537 && IT_CHARPOS (*it) < ZV)
8539 /* Should move forward by at least one line, maybe more.
8541 Note: Calling move_it_by_lines can be expensive on
8542 terminal frames, where compute_motion is used (via
8543 vmotion) to do the job, when there are very long lines
8544 and truncate-lines is nil. That's the reason for
8545 treating terminal frames specially here. */
8547 if (!FRAME_WINDOW_P (it->f))
8548 move_it_vertically (it, target_y - (it->current_y + line_height));
8549 else
8553 move_it_by_lines (it, 1);
8555 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8562 /* Move IT by a specified amount of pixel lines DY. DY negative means
8563 move backwards. DY = 0 means move to start of screen line. At the
8564 end, IT will be on the start of a screen line. */
8566 void
8567 move_it_vertically (struct it *it, int dy)
8569 if (dy <= 0)
8570 move_it_vertically_backward (it, -dy);
8571 else
8573 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8574 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8575 MOVE_TO_POS | MOVE_TO_Y);
8576 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8578 /* If buffer ends in ZV without a newline, move to the start of
8579 the line to satisfy the post-condition. */
8580 if (IT_CHARPOS (*it) == ZV
8581 && ZV > BEGV
8582 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8583 move_it_by_lines (it, 0);
8588 /* Move iterator IT past the end of the text line it is in. */
8590 void
8591 move_it_past_eol (struct it *it)
8593 enum move_it_result rc;
8595 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8596 if (rc == MOVE_NEWLINE_OR_CR)
8597 set_iterator_to_next (it, 0);
8601 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8602 negative means move up. DVPOS == 0 means move to the start of the
8603 screen line.
8605 Optimization idea: If we would know that IT->f doesn't use
8606 a face with proportional font, we could be faster for
8607 truncate-lines nil. */
8609 void
8610 move_it_by_lines (struct it *it, int dvpos)
8613 /* The commented-out optimization uses vmotion on terminals. This
8614 gives bad results, because elements like it->what, on which
8615 callers such as pos_visible_p rely, aren't updated. */
8616 /* struct position pos;
8617 if (!FRAME_WINDOW_P (it->f))
8619 struct text_pos textpos;
8621 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8622 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8623 reseat (it, textpos, 1);
8624 it->vpos += pos.vpos;
8625 it->current_y += pos.vpos;
8627 else */
8629 if (dvpos == 0)
8631 /* DVPOS == 0 means move to the start of the screen line. */
8632 move_it_vertically_backward (it, 0);
8633 xassert (it->current_x == 0 && it->hpos == 0);
8634 /* Let next call to line_bottom_y calculate real line height */
8635 last_height = 0;
8637 else if (dvpos > 0)
8639 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8640 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8641 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8643 else
8645 struct it it2;
8646 void *it2data = NULL;
8647 EMACS_INT start_charpos, i;
8649 /* Start at the beginning of the screen line containing IT's
8650 position. This may actually move vertically backwards,
8651 in case of overlays, so adjust dvpos accordingly. */
8652 dvpos += it->vpos;
8653 move_it_vertically_backward (it, 0);
8654 dvpos -= it->vpos;
8656 /* Go back -DVPOS visible lines and reseat the iterator there. */
8657 start_charpos = IT_CHARPOS (*it);
8658 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8659 back_to_previous_visible_line_start (it);
8660 reseat (it, it->current.pos, 1);
8662 /* Move further back if we end up in a string or an image. */
8663 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8665 /* First try to move to start of display line. */
8666 dvpos += it->vpos;
8667 move_it_vertically_backward (it, 0);
8668 dvpos -= it->vpos;
8669 if (IT_POS_VALID_AFTER_MOVE_P (it))
8670 break;
8671 /* If start of line is still in string or image,
8672 move further back. */
8673 back_to_previous_visible_line_start (it);
8674 reseat (it, it->current.pos, 1);
8675 dvpos--;
8678 it->current_x = it->hpos = 0;
8680 /* Above call may have moved too far if continuation lines
8681 are involved. Scan forward and see if it did. */
8682 SAVE_IT (it2, *it, it2data);
8683 it2.vpos = it2.current_y = 0;
8684 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8685 it->vpos -= it2.vpos;
8686 it->current_y -= it2.current_y;
8687 it->current_x = it->hpos = 0;
8689 /* If we moved too far back, move IT some lines forward. */
8690 if (it2.vpos > -dvpos)
8692 int delta = it2.vpos + dvpos;
8694 RESTORE_IT (&it2, &it2, it2data);
8695 SAVE_IT (it2, *it, it2data);
8696 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8697 /* Move back again if we got too far ahead. */
8698 if (IT_CHARPOS (*it) >= start_charpos)
8699 RESTORE_IT (it, &it2, it2data);
8700 else
8701 bidi_unshelve_cache (it2data, 1);
8703 else
8704 RESTORE_IT (it, it, it2data);
8708 /* Return 1 if IT points into the middle of a display vector. */
8711 in_display_vector_p (struct it *it)
8713 return (it->method == GET_FROM_DISPLAY_VECTOR
8714 && it->current.dpvec_index > 0
8715 && it->dpvec + it->current.dpvec_index != it->dpend);
8719 /***********************************************************************
8720 Messages
8721 ***********************************************************************/
8724 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8725 to *Messages*. */
8727 void
8728 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8730 Lisp_Object args[3];
8731 Lisp_Object msg, fmt;
8732 char *buffer;
8733 EMACS_INT len;
8734 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8735 USE_SAFE_ALLOCA;
8737 /* Do nothing if called asynchronously. Inserting text into
8738 a buffer may call after-change-functions and alike and
8739 that would means running Lisp asynchronously. */
8740 if (handling_signal)
8741 return;
8743 fmt = msg = Qnil;
8744 GCPRO4 (fmt, msg, arg1, arg2);
8746 args[0] = fmt = build_string (format);
8747 args[1] = arg1;
8748 args[2] = arg2;
8749 msg = Fformat (3, args);
8751 len = SBYTES (msg) + 1;
8752 SAFE_ALLOCA (buffer, char *, len);
8753 memcpy (buffer, SDATA (msg), len);
8755 message_dolog (buffer, len - 1, 1, 0);
8756 SAFE_FREE ();
8758 UNGCPRO;
8762 /* Output a newline in the *Messages* buffer if "needs" one. */
8764 void
8765 message_log_maybe_newline (void)
8767 if (message_log_need_newline)
8768 message_dolog ("", 0, 1, 0);
8772 /* Add a string M of length NBYTES to the message log, optionally
8773 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8774 nonzero, means interpret the contents of M as multibyte. This
8775 function calls low-level routines in order to bypass text property
8776 hooks, etc. which might not be safe to run.
8778 This may GC (insert may run before/after change hooks),
8779 so the buffer M must NOT point to a Lisp string. */
8781 void
8782 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8784 const unsigned char *msg = (const unsigned char *) m;
8786 if (!NILP (Vmemory_full))
8787 return;
8789 if (!NILP (Vmessage_log_max))
8791 struct buffer *oldbuf;
8792 Lisp_Object oldpoint, oldbegv, oldzv;
8793 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8794 EMACS_INT point_at_end = 0;
8795 EMACS_INT zv_at_end = 0;
8796 Lisp_Object old_deactivate_mark, tem;
8797 struct gcpro gcpro1;
8799 old_deactivate_mark = Vdeactivate_mark;
8800 oldbuf = current_buffer;
8801 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8802 BVAR (current_buffer, undo_list) = Qt;
8804 oldpoint = message_dolog_marker1;
8805 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8806 oldbegv = message_dolog_marker2;
8807 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8808 oldzv = message_dolog_marker3;
8809 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8810 GCPRO1 (old_deactivate_mark);
8812 if (PT == Z)
8813 point_at_end = 1;
8814 if (ZV == Z)
8815 zv_at_end = 1;
8817 BEGV = BEG;
8818 BEGV_BYTE = BEG_BYTE;
8819 ZV = Z;
8820 ZV_BYTE = Z_BYTE;
8821 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8823 /* Insert the string--maybe converting multibyte to single byte
8824 or vice versa, so that all the text fits the buffer. */
8825 if (multibyte
8826 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8828 EMACS_INT i;
8829 int c, char_bytes;
8830 char work[1];
8832 /* Convert a multibyte string to single-byte
8833 for the *Message* buffer. */
8834 for (i = 0; i < nbytes; i += char_bytes)
8836 c = string_char_and_length (msg + i, &char_bytes);
8837 work[0] = (ASCII_CHAR_P (c)
8839 : multibyte_char_to_unibyte (c));
8840 insert_1_both (work, 1, 1, 1, 0, 0);
8843 else if (! multibyte
8844 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8846 EMACS_INT i;
8847 int c, char_bytes;
8848 unsigned char str[MAX_MULTIBYTE_LENGTH];
8849 /* Convert a single-byte string to multibyte
8850 for the *Message* buffer. */
8851 for (i = 0; i < nbytes; i++)
8853 c = msg[i];
8854 MAKE_CHAR_MULTIBYTE (c);
8855 char_bytes = CHAR_STRING (c, str);
8856 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8859 else if (nbytes)
8860 insert_1 (m, nbytes, 1, 0, 0);
8862 if (nlflag)
8864 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8865 printmax_t dups;
8866 insert_1 ("\n", 1, 1, 0, 0);
8868 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8869 this_bol = PT;
8870 this_bol_byte = PT_BYTE;
8872 /* See if this line duplicates the previous one.
8873 If so, combine duplicates. */
8874 if (this_bol > BEG)
8876 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8877 prev_bol = PT;
8878 prev_bol_byte = PT_BYTE;
8880 dups = message_log_check_duplicate (prev_bol_byte,
8881 this_bol_byte);
8882 if (dups)
8884 del_range_both (prev_bol, prev_bol_byte,
8885 this_bol, this_bol_byte, 0);
8886 if (dups > 1)
8888 char dupstr[sizeof " [ times]"
8889 + INT_STRLEN_BOUND (printmax_t)];
8890 int duplen;
8892 /* If you change this format, don't forget to also
8893 change message_log_check_duplicate. */
8894 sprintf (dupstr, " [%"pMd" times]", dups);
8895 duplen = strlen (dupstr);
8896 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8897 insert_1 (dupstr, duplen, 1, 0, 1);
8902 /* If we have more than the desired maximum number of lines
8903 in the *Messages* buffer now, delete the oldest ones.
8904 This is safe because we don't have undo in this buffer. */
8906 if (NATNUMP (Vmessage_log_max))
8908 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8909 -XFASTINT (Vmessage_log_max) - 1, 0);
8910 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8913 BEGV = XMARKER (oldbegv)->charpos;
8914 BEGV_BYTE = marker_byte_position (oldbegv);
8916 if (zv_at_end)
8918 ZV = Z;
8919 ZV_BYTE = Z_BYTE;
8921 else
8923 ZV = XMARKER (oldzv)->charpos;
8924 ZV_BYTE = marker_byte_position (oldzv);
8927 if (point_at_end)
8928 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8929 else
8930 /* We can't do Fgoto_char (oldpoint) because it will run some
8931 Lisp code. */
8932 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8933 XMARKER (oldpoint)->bytepos);
8935 UNGCPRO;
8936 unchain_marker (XMARKER (oldpoint));
8937 unchain_marker (XMARKER (oldbegv));
8938 unchain_marker (XMARKER (oldzv));
8940 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8941 set_buffer_internal (oldbuf);
8942 if (NILP (tem))
8943 windows_or_buffers_changed = old_windows_or_buffers_changed;
8944 message_log_need_newline = !nlflag;
8945 Vdeactivate_mark = old_deactivate_mark;
8950 /* We are at the end of the buffer after just having inserted a newline.
8951 (Note: We depend on the fact we won't be crossing the gap.)
8952 Check to see if the most recent message looks a lot like the previous one.
8953 Return 0 if different, 1 if the new one should just replace it, or a
8954 value N > 1 if we should also append " [N times]". */
8956 static intmax_t
8957 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8959 EMACS_INT i;
8960 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8961 int seen_dots = 0;
8962 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8963 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8965 for (i = 0; i < len; i++)
8967 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8968 seen_dots = 1;
8969 if (p1[i] != p2[i])
8970 return seen_dots;
8972 p1 += len;
8973 if (*p1 == '\n')
8974 return 2;
8975 if (*p1++ == ' ' && *p1++ == '[')
8977 char *pend;
8978 intmax_t n = strtoimax ((char *) p1, &pend, 10);
8979 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
8980 return n+1;
8982 return 0;
8986 /* Display an echo area message M with a specified length of NBYTES
8987 bytes. The string may include null characters. If M is 0, clear
8988 out any existing message, and let the mini-buffer text show
8989 through.
8991 This may GC, so the buffer M must NOT point to a Lisp string. */
8993 void
8994 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8996 /* First flush out any partial line written with print. */
8997 message_log_maybe_newline ();
8998 if (m)
8999 message_dolog (m, nbytes, 1, multibyte);
9000 message2_nolog (m, nbytes, multibyte);
9004 /* The non-logging counterpart of message2. */
9006 void
9007 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9009 struct frame *sf = SELECTED_FRAME ();
9010 message_enable_multibyte = multibyte;
9012 if (FRAME_INITIAL_P (sf))
9014 if (noninteractive_need_newline)
9015 putc ('\n', stderr);
9016 noninteractive_need_newline = 0;
9017 if (m)
9018 fwrite (m, nbytes, 1, stderr);
9019 if (cursor_in_echo_area == 0)
9020 fprintf (stderr, "\n");
9021 fflush (stderr);
9023 /* A null message buffer means that the frame hasn't really been
9024 initialized yet. Error messages get reported properly by
9025 cmd_error, so this must be just an informative message; toss it. */
9026 else if (INTERACTIVE
9027 && sf->glyphs_initialized_p
9028 && FRAME_MESSAGE_BUF (sf))
9030 Lisp_Object mini_window;
9031 struct frame *f;
9033 /* Get the frame containing the mini-buffer
9034 that the selected frame is using. */
9035 mini_window = FRAME_MINIBUF_WINDOW (sf);
9036 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9038 FRAME_SAMPLE_VISIBILITY (f);
9039 if (FRAME_VISIBLE_P (sf)
9040 && ! FRAME_VISIBLE_P (f))
9041 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9043 if (m)
9045 set_message (m, Qnil, nbytes, multibyte);
9046 if (minibuffer_auto_raise)
9047 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9049 else
9050 clear_message (1, 1);
9052 do_pending_window_change (0);
9053 echo_area_display (1);
9054 do_pending_window_change (0);
9055 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9056 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9061 /* Display an echo area message M with a specified length of NBYTES
9062 bytes. The string may include null characters. If M is not a
9063 string, clear out any existing message, and let the mini-buffer
9064 text show through.
9066 This function cancels echoing. */
9068 void
9069 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9071 struct gcpro gcpro1;
9073 GCPRO1 (m);
9074 clear_message (1,1);
9075 cancel_echoing ();
9077 /* First flush out any partial line written with print. */
9078 message_log_maybe_newline ();
9079 if (STRINGP (m))
9081 char *buffer;
9082 USE_SAFE_ALLOCA;
9084 SAFE_ALLOCA (buffer, char *, nbytes);
9085 memcpy (buffer, SDATA (m), nbytes);
9086 message_dolog (buffer, nbytes, 1, multibyte);
9087 SAFE_FREE ();
9089 message3_nolog (m, nbytes, multibyte);
9091 UNGCPRO;
9095 /* The non-logging version of message3.
9096 This does not cancel echoing, because it is used for echoing.
9097 Perhaps we need to make a separate function for echoing
9098 and make this cancel echoing. */
9100 void
9101 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9103 struct frame *sf = SELECTED_FRAME ();
9104 message_enable_multibyte = multibyte;
9106 if (FRAME_INITIAL_P (sf))
9108 if (noninteractive_need_newline)
9109 putc ('\n', stderr);
9110 noninteractive_need_newline = 0;
9111 if (STRINGP (m))
9112 fwrite (SDATA (m), nbytes, 1, stderr);
9113 if (cursor_in_echo_area == 0)
9114 fprintf (stderr, "\n");
9115 fflush (stderr);
9117 /* A null message buffer means that the frame hasn't really been
9118 initialized yet. Error messages get reported properly by
9119 cmd_error, so this must be just an informative message; toss it. */
9120 else if (INTERACTIVE
9121 && sf->glyphs_initialized_p
9122 && FRAME_MESSAGE_BUF (sf))
9124 Lisp_Object mini_window;
9125 Lisp_Object frame;
9126 struct frame *f;
9128 /* Get the frame containing the mini-buffer
9129 that the selected frame is using. */
9130 mini_window = FRAME_MINIBUF_WINDOW (sf);
9131 frame = XWINDOW (mini_window)->frame;
9132 f = XFRAME (frame);
9134 FRAME_SAMPLE_VISIBILITY (f);
9135 if (FRAME_VISIBLE_P (sf)
9136 && !FRAME_VISIBLE_P (f))
9137 Fmake_frame_visible (frame);
9139 if (STRINGP (m) && SCHARS (m) > 0)
9141 set_message (NULL, m, nbytes, multibyte);
9142 if (minibuffer_auto_raise)
9143 Fraise_frame (frame);
9144 /* Assume we are not echoing.
9145 (If we are, echo_now will override this.) */
9146 echo_message_buffer = Qnil;
9148 else
9149 clear_message (1, 1);
9151 do_pending_window_change (0);
9152 echo_area_display (1);
9153 do_pending_window_change (0);
9154 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9155 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9160 /* Display a null-terminated echo area message M. If M is 0, clear
9161 out any existing message, and let the mini-buffer text show through.
9163 The buffer M must continue to exist until after the echo area gets
9164 cleared or some other message gets displayed there. Do not pass
9165 text that is stored in a Lisp string. Do not pass text in a buffer
9166 that was alloca'd. */
9168 void
9169 message1 (const char *m)
9171 message2 (m, (m ? strlen (m) : 0), 0);
9175 /* The non-logging counterpart of message1. */
9177 void
9178 message1_nolog (const char *m)
9180 message2_nolog (m, (m ? strlen (m) : 0), 0);
9183 /* Display a message M which contains a single %s
9184 which gets replaced with STRING. */
9186 void
9187 message_with_string (const char *m, Lisp_Object string, int log)
9189 CHECK_STRING (string);
9191 if (noninteractive)
9193 if (m)
9195 if (noninteractive_need_newline)
9196 putc ('\n', stderr);
9197 noninteractive_need_newline = 0;
9198 fprintf (stderr, m, SDATA (string));
9199 if (!cursor_in_echo_area)
9200 fprintf (stderr, "\n");
9201 fflush (stderr);
9204 else if (INTERACTIVE)
9206 /* The frame whose minibuffer we're going to display the message on.
9207 It may be larger than the selected frame, so we need
9208 to use its buffer, not the selected frame's buffer. */
9209 Lisp_Object mini_window;
9210 struct frame *f, *sf = SELECTED_FRAME ();
9212 /* Get the frame containing the minibuffer
9213 that the selected frame is using. */
9214 mini_window = FRAME_MINIBUF_WINDOW (sf);
9215 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9217 /* A null message buffer means that the frame hasn't really been
9218 initialized yet. Error messages get reported properly by
9219 cmd_error, so this must be just an informative message; toss it. */
9220 if (FRAME_MESSAGE_BUF (f))
9222 Lisp_Object args[2], msg;
9223 struct gcpro gcpro1, gcpro2;
9225 args[0] = build_string (m);
9226 args[1] = msg = string;
9227 GCPRO2 (args[0], msg);
9228 gcpro1.nvars = 2;
9230 msg = Fformat (2, args);
9232 if (log)
9233 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9234 else
9235 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9237 UNGCPRO;
9239 /* Print should start at the beginning of the message
9240 buffer next time. */
9241 message_buf_print = 0;
9247 /* Dump an informative message to the minibuf. If M is 0, clear out
9248 any existing message, and let the mini-buffer text show through. */
9250 static void
9251 vmessage (const char *m, va_list ap)
9253 if (noninteractive)
9255 if (m)
9257 if (noninteractive_need_newline)
9258 putc ('\n', stderr);
9259 noninteractive_need_newline = 0;
9260 vfprintf (stderr, m, ap);
9261 if (cursor_in_echo_area == 0)
9262 fprintf (stderr, "\n");
9263 fflush (stderr);
9266 else if (INTERACTIVE)
9268 /* The frame whose mini-buffer we're going to display the message
9269 on. It may be larger than the selected frame, so we need to
9270 use its buffer, not the selected frame's buffer. */
9271 Lisp_Object mini_window;
9272 struct frame *f, *sf = SELECTED_FRAME ();
9274 /* Get the frame containing the mini-buffer
9275 that the selected frame is using. */
9276 mini_window = FRAME_MINIBUF_WINDOW (sf);
9277 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9279 /* A null message buffer means that the frame hasn't really been
9280 initialized yet. Error messages get reported properly by
9281 cmd_error, so this must be just an informative message; toss
9282 it. */
9283 if (FRAME_MESSAGE_BUF (f))
9285 if (m)
9287 ptrdiff_t len;
9289 len = doprnt (FRAME_MESSAGE_BUF (f),
9290 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9292 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9294 else
9295 message1 (0);
9297 /* Print should start at the beginning of the message
9298 buffer next time. */
9299 message_buf_print = 0;
9304 void
9305 message (const char *m, ...)
9307 va_list ap;
9308 va_start (ap, m);
9309 vmessage (m, ap);
9310 va_end (ap);
9314 #if 0
9315 /* The non-logging version of message. */
9317 void
9318 message_nolog (const char *m, ...)
9320 Lisp_Object old_log_max;
9321 va_list ap;
9322 va_start (ap, m);
9323 old_log_max = Vmessage_log_max;
9324 Vmessage_log_max = Qnil;
9325 vmessage (m, ap);
9326 Vmessage_log_max = old_log_max;
9327 va_end (ap);
9329 #endif
9332 /* Display the current message in the current mini-buffer. This is
9333 only called from error handlers in process.c, and is not time
9334 critical. */
9336 void
9337 update_echo_area (void)
9339 if (!NILP (echo_area_buffer[0]))
9341 Lisp_Object string;
9342 string = Fcurrent_message ();
9343 message3 (string, SBYTES (string),
9344 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9349 /* Make sure echo area buffers in `echo_buffers' are live.
9350 If they aren't, make new ones. */
9352 static void
9353 ensure_echo_area_buffers (void)
9355 int i;
9357 for (i = 0; i < 2; ++i)
9358 if (!BUFFERP (echo_buffer[i])
9359 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9361 char name[30];
9362 Lisp_Object old_buffer;
9363 int j;
9365 old_buffer = echo_buffer[i];
9366 sprintf (name, " *Echo Area %d*", i);
9367 echo_buffer[i] = Fget_buffer_create (build_string (name));
9368 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9369 /* to force word wrap in echo area -
9370 it was decided to postpone this*/
9371 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9373 for (j = 0; j < 2; ++j)
9374 if (EQ (old_buffer, echo_area_buffer[j]))
9375 echo_area_buffer[j] = echo_buffer[i];
9380 /* Call FN with args A1..A4 with either the current or last displayed
9381 echo_area_buffer as current buffer.
9383 WHICH zero means use the current message buffer
9384 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9385 from echo_buffer[] and clear it.
9387 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9388 suitable buffer from echo_buffer[] and clear it.
9390 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9391 that the current message becomes the last displayed one, make
9392 choose a suitable buffer for echo_area_buffer[0], and clear it.
9394 Value is what FN returns. */
9396 static int
9397 with_echo_area_buffer (struct window *w, int which,
9398 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9399 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9401 Lisp_Object buffer;
9402 int this_one, the_other, clear_buffer_p, rc;
9403 int count = SPECPDL_INDEX ();
9405 /* If buffers aren't live, make new ones. */
9406 ensure_echo_area_buffers ();
9408 clear_buffer_p = 0;
9410 if (which == 0)
9411 this_one = 0, the_other = 1;
9412 else if (which > 0)
9413 this_one = 1, the_other = 0;
9414 else
9416 this_one = 0, the_other = 1;
9417 clear_buffer_p = 1;
9419 /* We need a fresh one in case the current echo buffer equals
9420 the one containing the last displayed echo area message. */
9421 if (!NILP (echo_area_buffer[this_one])
9422 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9423 echo_area_buffer[this_one] = Qnil;
9426 /* Choose a suitable buffer from echo_buffer[] is we don't
9427 have one. */
9428 if (NILP (echo_area_buffer[this_one]))
9430 echo_area_buffer[this_one]
9431 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9432 ? echo_buffer[the_other]
9433 : echo_buffer[this_one]);
9434 clear_buffer_p = 1;
9437 buffer = echo_area_buffer[this_one];
9439 /* Don't get confused by reusing the buffer used for echoing
9440 for a different purpose. */
9441 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9442 cancel_echoing ();
9444 record_unwind_protect (unwind_with_echo_area_buffer,
9445 with_echo_area_buffer_unwind_data (w));
9447 /* Make the echo area buffer current. Note that for display
9448 purposes, it is not necessary that the displayed window's buffer
9449 == current_buffer, except for text property lookup. So, let's
9450 only set that buffer temporarily here without doing a full
9451 Fset_window_buffer. We must also change w->pointm, though,
9452 because otherwise an assertions in unshow_buffer fails, and Emacs
9453 aborts. */
9454 set_buffer_internal_1 (XBUFFER (buffer));
9455 if (w)
9457 w->buffer = buffer;
9458 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9461 BVAR (current_buffer, undo_list) = Qt;
9462 BVAR (current_buffer, read_only) = Qnil;
9463 specbind (Qinhibit_read_only, Qt);
9464 specbind (Qinhibit_modification_hooks, Qt);
9466 if (clear_buffer_p && Z > BEG)
9467 del_range (BEG, Z);
9469 xassert (BEGV >= BEG);
9470 xassert (ZV <= Z && ZV >= BEGV);
9472 rc = fn (a1, a2, a3, a4);
9474 xassert (BEGV >= BEG);
9475 xassert (ZV <= Z && ZV >= BEGV);
9477 unbind_to (count, Qnil);
9478 return rc;
9482 /* Save state that should be preserved around the call to the function
9483 FN called in with_echo_area_buffer. */
9485 static Lisp_Object
9486 with_echo_area_buffer_unwind_data (struct window *w)
9488 int i = 0;
9489 Lisp_Object vector, tmp;
9491 /* Reduce consing by keeping one vector in
9492 Vwith_echo_area_save_vector. */
9493 vector = Vwith_echo_area_save_vector;
9494 Vwith_echo_area_save_vector = Qnil;
9496 if (NILP (vector))
9497 vector = Fmake_vector (make_number (7), Qnil);
9499 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9500 ASET (vector, i, Vdeactivate_mark); ++i;
9501 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9503 if (w)
9505 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9506 ASET (vector, i, w->buffer); ++i;
9507 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9508 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9510 else
9512 int end = i + 4;
9513 for (; i < end; ++i)
9514 ASET (vector, i, Qnil);
9517 xassert (i == ASIZE (vector));
9518 return vector;
9522 /* Restore global state from VECTOR which was created by
9523 with_echo_area_buffer_unwind_data. */
9525 static Lisp_Object
9526 unwind_with_echo_area_buffer (Lisp_Object vector)
9528 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9529 Vdeactivate_mark = AREF (vector, 1);
9530 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9532 if (WINDOWP (AREF (vector, 3)))
9534 struct window *w;
9535 Lisp_Object buffer, charpos, bytepos;
9537 w = XWINDOW (AREF (vector, 3));
9538 buffer = AREF (vector, 4);
9539 charpos = AREF (vector, 5);
9540 bytepos = AREF (vector, 6);
9542 w->buffer = buffer;
9543 set_marker_both (w->pointm, buffer,
9544 XFASTINT (charpos), XFASTINT (bytepos));
9547 Vwith_echo_area_save_vector = vector;
9548 return Qnil;
9552 /* Set up the echo area for use by print functions. MULTIBYTE_P
9553 non-zero means we will print multibyte. */
9555 void
9556 setup_echo_area_for_printing (int multibyte_p)
9558 /* If we can't find an echo area any more, exit. */
9559 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9560 Fkill_emacs (Qnil);
9562 ensure_echo_area_buffers ();
9564 if (!message_buf_print)
9566 /* A message has been output since the last time we printed.
9567 Choose a fresh echo area buffer. */
9568 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9569 echo_area_buffer[0] = echo_buffer[1];
9570 else
9571 echo_area_buffer[0] = echo_buffer[0];
9573 /* Switch to that buffer and clear it. */
9574 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9575 BVAR (current_buffer, truncate_lines) = Qnil;
9577 if (Z > BEG)
9579 int count = SPECPDL_INDEX ();
9580 specbind (Qinhibit_read_only, Qt);
9581 /* Note that undo recording is always disabled. */
9582 del_range (BEG, Z);
9583 unbind_to (count, Qnil);
9585 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9587 /* Set up the buffer for the multibyteness we need. */
9588 if (multibyte_p
9589 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9590 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9592 /* Raise the frame containing the echo area. */
9593 if (minibuffer_auto_raise)
9595 struct frame *sf = SELECTED_FRAME ();
9596 Lisp_Object mini_window;
9597 mini_window = FRAME_MINIBUF_WINDOW (sf);
9598 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9601 message_log_maybe_newline ();
9602 message_buf_print = 1;
9604 else
9606 if (NILP (echo_area_buffer[0]))
9608 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9609 echo_area_buffer[0] = echo_buffer[1];
9610 else
9611 echo_area_buffer[0] = echo_buffer[0];
9614 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9616 /* Someone switched buffers between print requests. */
9617 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9618 BVAR (current_buffer, truncate_lines) = Qnil;
9624 /* Display an echo area message in window W. Value is non-zero if W's
9625 height is changed. If display_last_displayed_message_p is
9626 non-zero, display the message that was last displayed, otherwise
9627 display the current message. */
9629 static int
9630 display_echo_area (struct window *w)
9632 int i, no_message_p, window_height_changed_p, count;
9634 /* Temporarily disable garbage collections while displaying the echo
9635 area. This is done because a GC can print a message itself.
9636 That message would modify the echo area buffer's contents while a
9637 redisplay of the buffer is going on, and seriously confuse
9638 redisplay. */
9639 count = inhibit_garbage_collection ();
9641 /* If there is no message, we must call display_echo_area_1
9642 nevertheless because it resizes the window. But we will have to
9643 reset the echo_area_buffer in question to nil at the end because
9644 with_echo_area_buffer will sets it to an empty buffer. */
9645 i = display_last_displayed_message_p ? 1 : 0;
9646 no_message_p = NILP (echo_area_buffer[i]);
9648 window_height_changed_p
9649 = with_echo_area_buffer (w, display_last_displayed_message_p,
9650 display_echo_area_1,
9651 (intptr_t) w, Qnil, 0, 0);
9653 if (no_message_p)
9654 echo_area_buffer[i] = Qnil;
9656 unbind_to (count, Qnil);
9657 return window_height_changed_p;
9661 /* Helper for display_echo_area. Display the current buffer which
9662 contains the current echo area message in window W, a mini-window,
9663 a pointer to which is passed in A1. A2..A4 are currently not used.
9664 Change the height of W so that all of the message is displayed.
9665 Value is non-zero if height of W was changed. */
9667 static int
9668 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9670 intptr_t i1 = a1;
9671 struct window *w = (struct window *) i1;
9672 Lisp_Object window;
9673 struct text_pos start;
9674 int window_height_changed_p = 0;
9676 /* Do this before displaying, so that we have a large enough glyph
9677 matrix for the display. If we can't get enough space for the
9678 whole text, display the last N lines. That works by setting w->start. */
9679 window_height_changed_p = resize_mini_window (w, 0);
9681 /* Use the starting position chosen by resize_mini_window. */
9682 SET_TEXT_POS_FROM_MARKER (start, w->start);
9684 /* Display. */
9685 clear_glyph_matrix (w->desired_matrix);
9686 XSETWINDOW (window, w);
9687 try_window (window, start, 0);
9689 return window_height_changed_p;
9693 /* Resize the echo area window to exactly the size needed for the
9694 currently displayed message, if there is one. If a mini-buffer
9695 is active, don't shrink it. */
9697 void
9698 resize_echo_area_exactly (void)
9700 if (BUFFERP (echo_area_buffer[0])
9701 && WINDOWP (echo_area_window))
9703 struct window *w = XWINDOW (echo_area_window);
9704 int resized_p;
9705 Lisp_Object resize_exactly;
9707 if (minibuf_level == 0)
9708 resize_exactly = Qt;
9709 else
9710 resize_exactly = Qnil;
9712 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9713 (intptr_t) w, resize_exactly,
9714 0, 0);
9715 if (resized_p)
9717 ++windows_or_buffers_changed;
9718 ++update_mode_lines;
9719 redisplay_internal ();
9725 /* Callback function for with_echo_area_buffer, when used from
9726 resize_echo_area_exactly. A1 contains a pointer to the window to
9727 resize, EXACTLY non-nil means resize the mini-window exactly to the
9728 size of the text displayed. A3 and A4 are not used. Value is what
9729 resize_mini_window returns. */
9731 static int
9732 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9734 intptr_t i1 = a1;
9735 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9739 /* Resize mini-window W to fit the size of its contents. EXACT_P
9740 means size the window exactly to the size needed. Otherwise, it's
9741 only enlarged until W's buffer is empty.
9743 Set W->start to the right place to begin display. If the whole
9744 contents fit, start at the beginning. Otherwise, start so as
9745 to make the end of the contents appear. This is particularly
9746 important for y-or-n-p, but seems desirable generally.
9748 Value is non-zero if the window height has been changed. */
9751 resize_mini_window (struct window *w, int exact_p)
9753 struct frame *f = XFRAME (w->frame);
9754 int window_height_changed_p = 0;
9756 xassert (MINI_WINDOW_P (w));
9758 /* By default, start display at the beginning. */
9759 set_marker_both (w->start, w->buffer,
9760 BUF_BEGV (XBUFFER (w->buffer)),
9761 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9763 /* Don't resize windows while redisplaying a window; it would
9764 confuse redisplay functions when the size of the window they are
9765 displaying changes from under them. Such a resizing can happen,
9766 for instance, when which-func prints a long message while
9767 we are running fontification-functions. We're running these
9768 functions with safe_call which binds inhibit-redisplay to t. */
9769 if (!NILP (Vinhibit_redisplay))
9770 return 0;
9772 /* Nil means don't try to resize. */
9773 if (NILP (Vresize_mini_windows)
9774 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9775 return 0;
9777 if (!FRAME_MINIBUF_ONLY_P (f))
9779 struct it it;
9780 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9781 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9782 int height, max_height;
9783 int unit = FRAME_LINE_HEIGHT (f);
9784 struct text_pos start;
9785 struct buffer *old_current_buffer = NULL;
9787 if (current_buffer != XBUFFER (w->buffer))
9789 old_current_buffer = current_buffer;
9790 set_buffer_internal (XBUFFER (w->buffer));
9793 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9795 /* Compute the max. number of lines specified by the user. */
9796 if (FLOATP (Vmax_mini_window_height))
9797 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9798 else if (INTEGERP (Vmax_mini_window_height))
9799 max_height = XINT (Vmax_mini_window_height);
9800 else
9801 max_height = total_height / 4;
9803 /* Correct that max. height if it's bogus. */
9804 max_height = max (1, max_height);
9805 max_height = min (total_height, max_height);
9807 /* Find out the height of the text in the window. */
9808 if (it.line_wrap == TRUNCATE)
9809 height = 1;
9810 else
9812 last_height = 0;
9813 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9814 if (it.max_ascent == 0 && it.max_descent == 0)
9815 height = it.current_y + last_height;
9816 else
9817 height = it.current_y + it.max_ascent + it.max_descent;
9818 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9819 height = (height + unit - 1) / unit;
9822 /* Compute a suitable window start. */
9823 if (height > max_height)
9825 height = max_height;
9826 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9827 move_it_vertically_backward (&it, (height - 1) * unit);
9828 start = it.current.pos;
9830 else
9831 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9832 SET_MARKER_FROM_TEXT_POS (w->start, start);
9834 if (EQ (Vresize_mini_windows, Qgrow_only))
9836 /* Let it grow only, until we display an empty message, in which
9837 case the window shrinks again. */
9838 if (height > WINDOW_TOTAL_LINES (w))
9840 int old_height = WINDOW_TOTAL_LINES (w);
9841 freeze_window_starts (f, 1);
9842 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9843 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9845 else if (height < WINDOW_TOTAL_LINES (w)
9846 && (exact_p || BEGV == ZV))
9848 int old_height = WINDOW_TOTAL_LINES (w);
9849 freeze_window_starts (f, 0);
9850 shrink_mini_window (w);
9851 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9854 else
9856 /* Always resize to exact size needed. */
9857 if (height > WINDOW_TOTAL_LINES (w))
9859 int old_height = WINDOW_TOTAL_LINES (w);
9860 freeze_window_starts (f, 1);
9861 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9862 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9864 else if (height < WINDOW_TOTAL_LINES (w))
9866 int old_height = WINDOW_TOTAL_LINES (w);
9867 freeze_window_starts (f, 0);
9868 shrink_mini_window (w);
9870 if (height)
9872 freeze_window_starts (f, 1);
9873 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9876 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9880 if (old_current_buffer)
9881 set_buffer_internal (old_current_buffer);
9884 return window_height_changed_p;
9888 /* Value is the current message, a string, or nil if there is no
9889 current message. */
9891 Lisp_Object
9892 current_message (void)
9894 Lisp_Object msg;
9896 if (!BUFFERP (echo_area_buffer[0]))
9897 msg = Qnil;
9898 else
9900 with_echo_area_buffer (0, 0, current_message_1,
9901 (intptr_t) &msg, Qnil, 0, 0);
9902 if (NILP (msg))
9903 echo_area_buffer[0] = Qnil;
9906 return msg;
9910 static int
9911 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9913 intptr_t i1 = a1;
9914 Lisp_Object *msg = (Lisp_Object *) i1;
9916 if (Z > BEG)
9917 *msg = make_buffer_string (BEG, Z, 1);
9918 else
9919 *msg = Qnil;
9920 return 0;
9924 /* Push the current message on Vmessage_stack for later restauration
9925 by restore_message. Value is non-zero if the current message isn't
9926 empty. This is a relatively infrequent operation, so it's not
9927 worth optimizing. */
9930 push_message (void)
9932 Lisp_Object msg;
9933 msg = current_message ();
9934 Vmessage_stack = Fcons (msg, Vmessage_stack);
9935 return STRINGP (msg);
9939 /* Restore message display from the top of Vmessage_stack. */
9941 void
9942 restore_message (void)
9944 Lisp_Object msg;
9946 xassert (CONSP (Vmessage_stack));
9947 msg = XCAR (Vmessage_stack);
9948 if (STRINGP (msg))
9949 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9950 else
9951 message3_nolog (msg, 0, 0);
9955 /* Handler for record_unwind_protect calling pop_message. */
9957 Lisp_Object
9958 pop_message_unwind (Lisp_Object dummy)
9960 pop_message ();
9961 return Qnil;
9964 /* Pop the top-most entry off Vmessage_stack. */
9966 static void
9967 pop_message (void)
9969 xassert (CONSP (Vmessage_stack));
9970 Vmessage_stack = XCDR (Vmessage_stack);
9974 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9975 exits. If the stack is not empty, we have a missing pop_message
9976 somewhere. */
9978 void
9979 check_message_stack (void)
9981 if (!NILP (Vmessage_stack))
9982 abort ();
9986 /* Truncate to NCHARS what will be displayed in the echo area the next
9987 time we display it---but don't redisplay it now. */
9989 void
9990 truncate_echo_area (EMACS_INT nchars)
9992 if (nchars == 0)
9993 echo_area_buffer[0] = Qnil;
9994 /* A null message buffer means that the frame hasn't really been
9995 initialized yet. Error messages get reported properly by
9996 cmd_error, so this must be just an informative message; toss it. */
9997 else if (!noninteractive
9998 && INTERACTIVE
9999 && !NILP (echo_area_buffer[0]))
10001 struct frame *sf = SELECTED_FRAME ();
10002 if (FRAME_MESSAGE_BUF (sf))
10003 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10008 /* Helper function for truncate_echo_area. Truncate the current
10009 message to at most NCHARS characters. */
10011 static int
10012 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10014 if (BEG + nchars < Z)
10015 del_range (BEG + nchars, Z);
10016 if (Z == BEG)
10017 echo_area_buffer[0] = Qnil;
10018 return 0;
10022 /* Set the current message to a substring of S or STRING.
10024 If STRING is a Lisp string, set the message to the first NBYTES
10025 bytes from STRING. NBYTES zero means use the whole string. If
10026 STRING is multibyte, the message will be displayed multibyte.
10028 If S is not null, set the message to the first LEN bytes of S. LEN
10029 zero means use the whole string. MULTIBYTE_P non-zero means S is
10030 multibyte. Display the message multibyte in that case.
10032 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10033 to t before calling set_message_1 (which calls insert).
10036 static void
10037 set_message (const char *s, Lisp_Object string,
10038 EMACS_INT nbytes, int multibyte_p)
10040 message_enable_multibyte
10041 = ((s && multibyte_p)
10042 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10044 with_echo_area_buffer (0, -1, set_message_1,
10045 (intptr_t) s, string, nbytes, multibyte_p);
10046 message_buf_print = 0;
10047 help_echo_showing_p = 0;
10051 /* Helper function for set_message. Arguments have the same meaning
10052 as there, with A1 corresponding to S and A2 corresponding to STRING
10053 This function is called with the echo area buffer being
10054 current. */
10056 static int
10057 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10059 intptr_t i1 = a1;
10060 const char *s = (const char *) i1;
10061 const unsigned char *msg = (const unsigned char *) s;
10062 Lisp_Object string = a2;
10064 /* Change multibyteness of the echo buffer appropriately. */
10065 if (message_enable_multibyte
10066 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10067 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10069 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10070 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10071 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10073 /* Insert new message at BEG. */
10074 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10076 if (STRINGP (string))
10078 EMACS_INT nchars;
10080 if (nbytes == 0)
10081 nbytes = SBYTES (string);
10082 nchars = string_byte_to_char (string, nbytes);
10084 /* This function takes care of single/multibyte conversion. We
10085 just have to ensure that the echo area buffer has the right
10086 setting of enable_multibyte_characters. */
10087 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10089 else if (s)
10091 if (nbytes == 0)
10092 nbytes = strlen (s);
10094 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10096 /* Convert from multi-byte to single-byte. */
10097 EMACS_INT i;
10098 int c, n;
10099 char work[1];
10101 /* Convert a multibyte string to single-byte. */
10102 for (i = 0; i < nbytes; i += n)
10104 c = string_char_and_length (msg + i, &n);
10105 work[0] = (ASCII_CHAR_P (c)
10107 : multibyte_char_to_unibyte (c));
10108 insert_1_both (work, 1, 1, 1, 0, 0);
10111 else if (!multibyte_p
10112 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10114 /* Convert from single-byte to multi-byte. */
10115 EMACS_INT i;
10116 int c, n;
10117 unsigned char str[MAX_MULTIBYTE_LENGTH];
10119 /* Convert a single-byte string to multibyte. */
10120 for (i = 0; i < nbytes; i++)
10122 c = msg[i];
10123 MAKE_CHAR_MULTIBYTE (c);
10124 n = CHAR_STRING (c, str);
10125 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10128 else
10129 insert_1 (s, nbytes, 1, 0, 0);
10132 return 0;
10136 /* Clear messages. CURRENT_P non-zero means clear the current
10137 message. LAST_DISPLAYED_P non-zero means clear the message
10138 last displayed. */
10140 void
10141 clear_message (int current_p, int last_displayed_p)
10143 if (current_p)
10145 echo_area_buffer[0] = Qnil;
10146 message_cleared_p = 1;
10149 if (last_displayed_p)
10150 echo_area_buffer[1] = Qnil;
10152 message_buf_print = 0;
10155 /* Clear garbaged frames.
10157 This function is used where the old redisplay called
10158 redraw_garbaged_frames which in turn called redraw_frame which in
10159 turn called clear_frame. The call to clear_frame was a source of
10160 flickering. I believe a clear_frame is not necessary. It should
10161 suffice in the new redisplay to invalidate all current matrices,
10162 and ensure a complete redisplay of all windows. */
10164 static void
10165 clear_garbaged_frames (void)
10167 if (frame_garbaged)
10169 Lisp_Object tail, frame;
10170 int changed_count = 0;
10172 FOR_EACH_FRAME (tail, frame)
10174 struct frame *f = XFRAME (frame);
10176 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10178 if (f->resized_p)
10180 Fredraw_frame (frame);
10181 f->force_flush_display_p = 1;
10183 clear_current_matrices (f);
10184 changed_count++;
10185 f->garbaged = 0;
10186 f->resized_p = 0;
10190 frame_garbaged = 0;
10191 if (changed_count)
10192 ++windows_or_buffers_changed;
10197 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10198 is non-zero update selected_frame. Value is non-zero if the
10199 mini-windows height has been changed. */
10201 static int
10202 echo_area_display (int update_frame_p)
10204 Lisp_Object mini_window;
10205 struct window *w;
10206 struct frame *f;
10207 int window_height_changed_p = 0;
10208 struct frame *sf = SELECTED_FRAME ();
10210 mini_window = FRAME_MINIBUF_WINDOW (sf);
10211 w = XWINDOW (mini_window);
10212 f = XFRAME (WINDOW_FRAME (w));
10214 /* Don't display if frame is invisible or not yet initialized. */
10215 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10216 return 0;
10218 #ifdef HAVE_WINDOW_SYSTEM
10219 /* When Emacs starts, selected_frame may be the initial terminal
10220 frame. If we let this through, a message would be displayed on
10221 the terminal. */
10222 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10223 return 0;
10224 #endif /* HAVE_WINDOW_SYSTEM */
10226 /* Redraw garbaged frames. */
10227 if (frame_garbaged)
10228 clear_garbaged_frames ();
10230 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10232 echo_area_window = mini_window;
10233 window_height_changed_p = display_echo_area (w);
10234 w->must_be_updated_p = 1;
10236 /* Update the display, unless called from redisplay_internal.
10237 Also don't update the screen during redisplay itself. The
10238 update will happen at the end of redisplay, and an update
10239 here could cause confusion. */
10240 if (update_frame_p && !redisplaying_p)
10242 int n = 0;
10244 /* If the display update has been interrupted by pending
10245 input, update mode lines in the frame. Due to the
10246 pending input, it might have been that redisplay hasn't
10247 been called, so that mode lines above the echo area are
10248 garbaged. This looks odd, so we prevent it here. */
10249 if (!display_completed)
10250 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10252 if (window_height_changed_p
10253 /* Don't do this if Emacs is shutting down. Redisplay
10254 needs to run hooks. */
10255 && !NILP (Vrun_hooks))
10257 /* Must update other windows. Likewise as in other
10258 cases, don't let this update be interrupted by
10259 pending input. */
10260 int count = SPECPDL_INDEX ();
10261 specbind (Qredisplay_dont_pause, Qt);
10262 windows_or_buffers_changed = 1;
10263 redisplay_internal ();
10264 unbind_to (count, Qnil);
10266 else if (FRAME_WINDOW_P (f) && n == 0)
10268 /* Window configuration is the same as before.
10269 Can do with a display update of the echo area,
10270 unless we displayed some mode lines. */
10271 update_single_window (w, 1);
10272 FRAME_RIF (f)->flush_display (f);
10274 else
10275 update_frame (f, 1, 1);
10277 /* If cursor is in the echo area, make sure that the next
10278 redisplay displays the minibuffer, so that the cursor will
10279 be replaced with what the minibuffer wants. */
10280 if (cursor_in_echo_area)
10281 ++windows_or_buffers_changed;
10284 else if (!EQ (mini_window, selected_window))
10285 windows_or_buffers_changed++;
10287 /* Last displayed message is now the current message. */
10288 echo_area_buffer[1] = echo_area_buffer[0];
10289 /* Inform read_char that we're not echoing. */
10290 echo_message_buffer = Qnil;
10292 /* Prevent redisplay optimization in redisplay_internal by resetting
10293 this_line_start_pos. This is done because the mini-buffer now
10294 displays the message instead of its buffer text. */
10295 if (EQ (mini_window, selected_window))
10296 CHARPOS (this_line_start_pos) = 0;
10298 return window_height_changed_p;
10303 /***********************************************************************
10304 Mode Lines and Frame Titles
10305 ***********************************************************************/
10307 /* A buffer for constructing non-propertized mode-line strings and
10308 frame titles in it; allocated from the heap in init_xdisp and
10309 resized as needed in store_mode_line_noprop_char. */
10311 static char *mode_line_noprop_buf;
10313 /* The buffer's end, and a current output position in it. */
10315 static char *mode_line_noprop_buf_end;
10316 static char *mode_line_noprop_ptr;
10318 #define MODE_LINE_NOPROP_LEN(start) \
10319 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10321 static enum {
10322 MODE_LINE_DISPLAY = 0,
10323 MODE_LINE_TITLE,
10324 MODE_LINE_NOPROP,
10325 MODE_LINE_STRING
10326 } mode_line_target;
10328 /* Alist that caches the results of :propertize.
10329 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10330 static Lisp_Object mode_line_proptrans_alist;
10332 /* List of strings making up the mode-line. */
10333 static Lisp_Object mode_line_string_list;
10335 /* Base face property when building propertized mode line string. */
10336 static Lisp_Object mode_line_string_face;
10337 static Lisp_Object mode_line_string_face_prop;
10340 /* Unwind data for mode line strings */
10342 static Lisp_Object Vmode_line_unwind_vector;
10344 static Lisp_Object
10345 format_mode_line_unwind_data (struct buffer *obuf,
10346 Lisp_Object owin,
10347 int save_proptrans)
10349 Lisp_Object vector, tmp;
10351 /* Reduce consing by keeping one vector in
10352 Vwith_echo_area_save_vector. */
10353 vector = Vmode_line_unwind_vector;
10354 Vmode_line_unwind_vector = Qnil;
10356 if (NILP (vector))
10357 vector = Fmake_vector (make_number (8), Qnil);
10359 ASET (vector, 0, make_number (mode_line_target));
10360 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10361 ASET (vector, 2, mode_line_string_list);
10362 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10363 ASET (vector, 4, mode_line_string_face);
10364 ASET (vector, 5, mode_line_string_face_prop);
10366 if (obuf)
10367 XSETBUFFER (tmp, obuf);
10368 else
10369 tmp = Qnil;
10370 ASET (vector, 6, tmp);
10371 ASET (vector, 7, owin);
10373 return vector;
10376 static Lisp_Object
10377 unwind_format_mode_line (Lisp_Object vector)
10379 mode_line_target = XINT (AREF (vector, 0));
10380 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10381 mode_line_string_list = AREF (vector, 2);
10382 if (! EQ (AREF (vector, 3), Qt))
10383 mode_line_proptrans_alist = AREF (vector, 3);
10384 mode_line_string_face = AREF (vector, 4);
10385 mode_line_string_face_prop = AREF (vector, 5);
10387 if (!NILP (AREF (vector, 7)))
10388 /* Select window before buffer, since it may change the buffer. */
10389 Fselect_window (AREF (vector, 7), Qt);
10391 if (!NILP (AREF (vector, 6)))
10393 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10394 ASET (vector, 6, Qnil);
10397 Vmode_line_unwind_vector = vector;
10398 return Qnil;
10402 /* Store a single character C for the frame title in mode_line_noprop_buf.
10403 Re-allocate mode_line_noprop_buf if necessary. */
10405 static void
10406 store_mode_line_noprop_char (char c)
10408 /* If output position has reached the end of the allocated buffer,
10409 double the buffer's size. */
10410 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10412 int len = MODE_LINE_NOPROP_LEN (0);
10413 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10414 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10415 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10416 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10419 *mode_line_noprop_ptr++ = c;
10423 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10424 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10425 characters that yield more columns than PRECISION; PRECISION <= 0
10426 means copy the whole string. Pad with spaces until FIELD_WIDTH
10427 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10428 pad. Called from display_mode_element when it is used to build a
10429 frame title. */
10431 static int
10432 store_mode_line_noprop (const char *string, int field_width, int precision)
10434 const unsigned char *str = (const unsigned char *) string;
10435 int n = 0;
10436 EMACS_INT dummy, nbytes;
10438 /* Copy at most PRECISION chars from STR. */
10439 nbytes = strlen (string);
10440 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10441 while (nbytes--)
10442 store_mode_line_noprop_char (*str++);
10444 /* Fill up with spaces until FIELD_WIDTH reached. */
10445 while (field_width > 0
10446 && n < field_width)
10448 store_mode_line_noprop_char (' ');
10449 ++n;
10452 return n;
10455 /***********************************************************************
10456 Frame Titles
10457 ***********************************************************************/
10459 #ifdef HAVE_WINDOW_SYSTEM
10461 /* Set the title of FRAME, if it has changed. The title format is
10462 Vicon_title_format if FRAME is iconified, otherwise it is
10463 frame_title_format. */
10465 static void
10466 x_consider_frame_title (Lisp_Object frame)
10468 struct frame *f = XFRAME (frame);
10470 if (FRAME_WINDOW_P (f)
10471 || FRAME_MINIBUF_ONLY_P (f)
10472 || f->explicit_name)
10474 /* Do we have more than one visible frame on this X display? */
10475 Lisp_Object tail;
10476 Lisp_Object fmt;
10477 int title_start;
10478 char *title;
10479 int len;
10480 struct it it;
10481 int count = SPECPDL_INDEX ();
10483 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10485 Lisp_Object other_frame = XCAR (tail);
10486 struct frame *tf = XFRAME (other_frame);
10488 if (tf != f
10489 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10490 && !FRAME_MINIBUF_ONLY_P (tf)
10491 && !EQ (other_frame, tip_frame)
10492 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10493 break;
10496 /* Set global variable indicating that multiple frames exist. */
10497 multiple_frames = CONSP (tail);
10499 /* Switch to the buffer of selected window of the frame. Set up
10500 mode_line_target so that display_mode_element will output into
10501 mode_line_noprop_buf; then display the title. */
10502 record_unwind_protect (unwind_format_mode_line,
10503 format_mode_line_unwind_data
10504 (current_buffer, selected_window, 0));
10506 Fselect_window (f->selected_window, Qt);
10507 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10508 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10510 mode_line_target = MODE_LINE_TITLE;
10511 title_start = MODE_LINE_NOPROP_LEN (0);
10512 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10513 NULL, DEFAULT_FACE_ID);
10514 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10515 len = MODE_LINE_NOPROP_LEN (title_start);
10516 title = mode_line_noprop_buf + title_start;
10517 unbind_to (count, Qnil);
10519 /* Set the title only if it's changed. This avoids consing in
10520 the common case where it hasn't. (If it turns out that we've
10521 already wasted too much time by walking through the list with
10522 display_mode_element, then we might need to optimize at a
10523 higher level than this.) */
10524 if (! STRINGP (f->name)
10525 || SBYTES (f->name) != len
10526 || memcmp (title, SDATA (f->name), len) != 0)
10527 x_implicitly_set_name (f, make_string (title, len), Qnil);
10531 #endif /* not HAVE_WINDOW_SYSTEM */
10536 /***********************************************************************
10537 Menu Bars
10538 ***********************************************************************/
10541 /* Prepare for redisplay by updating menu-bar item lists when
10542 appropriate. This can call eval. */
10544 void
10545 prepare_menu_bars (void)
10547 int all_windows;
10548 struct gcpro gcpro1, gcpro2;
10549 struct frame *f;
10550 Lisp_Object tooltip_frame;
10552 #ifdef HAVE_WINDOW_SYSTEM
10553 tooltip_frame = tip_frame;
10554 #else
10555 tooltip_frame = Qnil;
10556 #endif
10558 /* Update all frame titles based on their buffer names, etc. We do
10559 this before the menu bars so that the buffer-menu will show the
10560 up-to-date frame titles. */
10561 #ifdef HAVE_WINDOW_SYSTEM
10562 if (windows_or_buffers_changed || update_mode_lines)
10564 Lisp_Object tail, frame;
10566 FOR_EACH_FRAME (tail, frame)
10568 f = XFRAME (frame);
10569 if (!EQ (frame, tooltip_frame)
10570 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10571 x_consider_frame_title (frame);
10574 #endif /* HAVE_WINDOW_SYSTEM */
10576 /* Update the menu bar item lists, if appropriate. This has to be
10577 done before any actual redisplay or generation of display lines. */
10578 all_windows = (update_mode_lines
10579 || buffer_shared > 1
10580 || windows_or_buffers_changed);
10581 if (all_windows)
10583 Lisp_Object tail, frame;
10584 int count = SPECPDL_INDEX ();
10585 /* 1 means that update_menu_bar has run its hooks
10586 so any further calls to update_menu_bar shouldn't do so again. */
10587 int menu_bar_hooks_run = 0;
10589 record_unwind_save_match_data ();
10591 FOR_EACH_FRAME (tail, frame)
10593 f = XFRAME (frame);
10595 /* Ignore tooltip frame. */
10596 if (EQ (frame, tooltip_frame))
10597 continue;
10599 /* If a window on this frame changed size, report that to
10600 the user and clear the size-change flag. */
10601 if (FRAME_WINDOW_SIZES_CHANGED (f))
10603 Lisp_Object functions;
10605 /* Clear flag first in case we get an error below. */
10606 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10607 functions = Vwindow_size_change_functions;
10608 GCPRO2 (tail, functions);
10610 while (CONSP (functions))
10612 if (!EQ (XCAR (functions), Qt))
10613 call1 (XCAR (functions), frame);
10614 functions = XCDR (functions);
10616 UNGCPRO;
10619 GCPRO1 (tail);
10620 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10621 #ifdef HAVE_WINDOW_SYSTEM
10622 update_tool_bar (f, 0);
10623 #endif
10624 #ifdef HAVE_NS
10625 if (windows_or_buffers_changed
10626 && FRAME_NS_P (f))
10627 ns_set_doc_edited (f, Fbuffer_modified_p
10628 (XWINDOW (f->selected_window)->buffer));
10629 #endif
10630 UNGCPRO;
10633 unbind_to (count, Qnil);
10635 else
10637 struct frame *sf = SELECTED_FRAME ();
10638 update_menu_bar (sf, 1, 0);
10639 #ifdef HAVE_WINDOW_SYSTEM
10640 update_tool_bar (sf, 1);
10641 #endif
10646 /* Update the menu bar item list for frame F. This has to be done
10647 before we start to fill in any display lines, because it can call
10648 eval.
10650 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10652 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10653 already ran the menu bar hooks for this redisplay, so there
10654 is no need to run them again. The return value is the
10655 updated value of this flag, to pass to the next call. */
10657 static int
10658 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10660 Lisp_Object window;
10661 register struct window *w;
10663 /* If called recursively during a menu update, do nothing. This can
10664 happen when, for instance, an activate-menubar-hook causes a
10665 redisplay. */
10666 if (inhibit_menubar_update)
10667 return hooks_run;
10669 window = FRAME_SELECTED_WINDOW (f);
10670 w = XWINDOW (window);
10672 if (FRAME_WINDOW_P (f)
10674 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10675 || defined (HAVE_NS) || defined (USE_GTK)
10676 FRAME_EXTERNAL_MENU_BAR (f)
10677 #else
10678 FRAME_MENU_BAR_LINES (f) > 0
10679 #endif
10680 : FRAME_MENU_BAR_LINES (f) > 0)
10682 /* If the user has switched buffers or windows, we need to
10683 recompute to reflect the new bindings. But we'll
10684 recompute when update_mode_lines is set too; that means
10685 that people can use force-mode-line-update to request
10686 that the menu bar be recomputed. The adverse effect on
10687 the rest of the redisplay algorithm is about the same as
10688 windows_or_buffers_changed anyway. */
10689 if (windows_or_buffers_changed
10690 /* This used to test w->update_mode_line, but we believe
10691 there is no need to recompute the menu in that case. */
10692 || update_mode_lines
10693 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10694 < BUF_MODIFF (XBUFFER (w->buffer)))
10695 != !NILP (w->last_had_star))
10696 || ((!NILP (Vtransient_mark_mode)
10697 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10698 != !NILP (w->region_showing)))
10700 struct buffer *prev = current_buffer;
10701 int count = SPECPDL_INDEX ();
10703 specbind (Qinhibit_menubar_update, Qt);
10705 set_buffer_internal_1 (XBUFFER (w->buffer));
10706 if (save_match_data)
10707 record_unwind_save_match_data ();
10708 if (NILP (Voverriding_local_map_menu_flag))
10710 specbind (Qoverriding_terminal_local_map, Qnil);
10711 specbind (Qoverriding_local_map, Qnil);
10714 if (!hooks_run)
10716 /* Run the Lucid hook. */
10717 safe_run_hooks (Qactivate_menubar_hook);
10719 /* If it has changed current-menubar from previous value,
10720 really recompute the menu-bar from the value. */
10721 if (! NILP (Vlucid_menu_bar_dirty_flag))
10722 call0 (Qrecompute_lucid_menubar);
10724 safe_run_hooks (Qmenu_bar_update_hook);
10726 hooks_run = 1;
10729 XSETFRAME (Vmenu_updating_frame, f);
10730 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10732 /* Redisplay the menu bar in case we changed it. */
10733 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10734 || defined (HAVE_NS) || defined (USE_GTK)
10735 if (FRAME_WINDOW_P (f))
10737 #if defined (HAVE_NS)
10738 /* All frames on Mac OS share the same menubar. So only
10739 the selected frame should be allowed to set it. */
10740 if (f == SELECTED_FRAME ())
10741 #endif
10742 set_frame_menubar (f, 0, 0);
10744 else
10745 /* On a terminal screen, the menu bar is an ordinary screen
10746 line, and this makes it get updated. */
10747 w->update_mode_line = Qt;
10748 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10749 /* In the non-toolkit version, the menu bar is an ordinary screen
10750 line, and this makes it get updated. */
10751 w->update_mode_line = Qt;
10752 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10754 unbind_to (count, Qnil);
10755 set_buffer_internal_1 (prev);
10759 return hooks_run;
10764 /***********************************************************************
10765 Output Cursor
10766 ***********************************************************************/
10768 #ifdef HAVE_WINDOW_SYSTEM
10770 /* EXPORT:
10771 Nominal cursor position -- where to draw output.
10772 HPOS and VPOS are window relative glyph matrix coordinates.
10773 X and Y are window relative pixel coordinates. */
10775 struct cursor_pos output_cursor;
10778 /* EXPORT:
10779 Set the global variable output_cursor to CURSOR. All cursor
10780 positions are relative to updated_window. */
10782 void
10783 set_output_cursor (struct cursor_pos *cursor)
10785 output_cursor.hpos = cursor->hpos;
10786 output_cursor.vpos = cursor->vpos;
10787 output_cursor.x = cursor->x;
10788 output_cursor.y = cursor->y;
10792 /* EXPORT for RIF:
10793 Set a nominal cursor position.
10795 HPOS and VPOS are column/row positions in a window glyph matrix. X
10796 and Y are window text area relative pixel positions.
10798 If this is done during an update, updated_window will contain the
10799 window that is being updated and the position is the future output
10800 cursor position for that window. If updated_window is null, use
10801 selected_window and display the cursor at the given position. */
10803 void
10804 x_cursor_to (int vpos, int hpos, int y, int x)
10806 struct window *w;
10808 /* If updated_window is not set, work on selected_window. */
10809 if (updated_window)
10810 w = updated_window;
10811 else
10812 w = XWINDOW (selected_window);
10814 /* Set the output cursor. */
10815 output_cursor.hpos = hpos;
10816 output_cursor.vpos = vpos;
10817 output_cursor.x = x;
10818 output_cursor.y = y;
10820 /* If not called as part of an update, really display the cursor.
10821 This will also set the cursor position of W. */
10822 if (updated_window == NULL)
10824 BLOCK_INPUT;
10825 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10826 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10827 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10828 UNBLOCK_INPUT;
10832 #endif /* HAVE_WINDOW_SYSTEM */
10835 /***********************************************************************
10836 Tool-bars
10837 ***********************************************************************/
10839 #ifdef HAVE_WINDOW_SYSTEM
10841 /* Where the mouse was last time we reported a mouse event. */
10843 FRAME_PTR last_mouse_frame;
10845 /* Tool-bar item index of the item on which a mouse button was pressed
10846 or -1. */
10848 int last_tool_bar_item;
10851 static Lisp_Object
10852 update_tool_bar_unwind (Lisp_Object frame)
10854 selected_frame = frame;
10855 return Qnil;
10858 /* Update the tool-bar item list for frame F. This has to be done
10859 before we start to fill in any display lines. Called from
10860 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10861 and restore it here. */
10863 static void
10864 update_tool_bar (struct frame *f, int save_match_data)
10866 #if defined (USE_GTK) || defined (HAVE_NS)
10867 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10868 #else
10869 int do_update = WINDOWP (f->tool_bar_window)
10870 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10871 #endif
10873 if (do_update)
10875 Lisp_Object window;
10876 struct window *w;
10878 window = FRAME_SELECTED_WINDOW (f);
10879 w = XWINDOW (window);
10881 /* If the user has switched buffers or windows, we need to
10882 recompute to reflect the new bindings. But we'll
10883 recompute when update_mode_lines is set too; that means
10884 that people can use force-mode-line-update to request
10885 that the menu bar be recomputed. The adverse effect on
10886 the rest of the redisplay algorithm is about the same as
10887 windows_or_buffers_changed anyway. */
10888 if (windows_or_buffers_changed
10889 || !NILP (w->update_mode_line)
10890 || update_mode_lines
10891 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10892 < BUF_MODIFF (XBUFFER (w->buffer)))
10893 != !NILP (w->last_had_star))
10894 || ((!NILP (Vtransient_mark_mode)
10895 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10896 != !NILP (w->region_showing)))
10898 struct buffer *prev = current_buffer;
10899 int count = SPECPDL_INDEX ();
10900 Lisp_Object frame, new_tool_bar;
10901 int new_n_tool_bar;
10902 struct gcpro gcpro1;
10904 /* Set current_buffer to the buffer of the selected
10905 window of the frame, so that we get the right local
10906 keymaps. */
10907 set_buffer_internal_1 (XBUFFER (w->buffer));
10909 /* Save match data, if we must. */
10910 if (save_match_data)
10911 record_unwind_save_match_data ();
10913 /* Make sure that we don't accidentally use bogus keymaps. */
10914 if (NILP (Voverriding_local_map_menu_flag))
10916 specbind (Qoverriding_terminal_local_map, Qnil);
10917 specbind (Qoverriding_local_map, Qnil);
10920 GCPRO1 (new_tool_bar);
10922 /* We must temporarily set the selected frame to this frame
10923 before calling tool_bar_items, because the calculation of
10924 the tool-bar keymap uses the selected frame (see
10925 `tool-bar-make-keymap' in tool-bar.el). */
10926 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10927 XSETFRAME (frame, f);
10928 selected_frame = frame;
10930 /* Build desired tool-bar items from keymaps. */
10931 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10932 &new_n_tool_bar);
10934 /* Redisplay the tool-bar if we changed it. */
10935 if (new_n_tool_bar != f->n_tool_bar_items
10936 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10938 /* Redisplay that happens asynchronously due to an expose event
10939 may access f->tool_bar_items. Make sure we update both
10940 variables within BLOCK_INPUT so no such event interrupts. */
10941 BLOCK_INPUT;
10942 f->tool_bar_items = new_tool_bar;
10943 f->n_tool_bar_items = new_n_tool_bar;
10944 w->update_mode_line = Qt;
10945 UNBLOCK_INPUT;
10948 UNGCPRO;
10950 unbind_to (count, Qnil);
10951 set_buffer_internal_1 (prev);
10957 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10958 F's desired tool-bar contents. F->tool_bar_items must have
10959 been set up previously by calling prepare_menu_bars. */
10961 static void
10962 build_desired_tool_bar_string (struct frame *f)
10964 int i, size, size_needed;
10965 struct gcpro gcpro1, gcpro2, gcpro3;
10966 Lisp_Object image, plist, props;
10968 image = plist = props = Qnil;
10969 GCPRO3 (image, plist, props);
10971 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10972 Otherwise, make a new string. */
10974 /* The size of the string we might be able to reuse. */
10975 size = (STRINGP (f->desired_tool_bar_string)
10976 ? SCHARS (f->desired_tool_bar_string)
10977 : 0);
10979 /* We need one space in the string for each image. */
10980 size_needed = f->n_tool_bar_items;
10982 /* Reuse f->desired_tool_bar_string, if possible. */
10983 if (size < size_needed || NILP (f->desired_tool_bar_string))
10984 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10985 make_number (' '));
10986 else
10988 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10989 Fremove_text_properties (make_number (0), make_number (size),
10990 props, f->desired_tool_bar_string);
10993 /* Put a `display' property on the string for the images to display,
10994 put a `menu_item' property on tool-bar items with a value that
10995 is the index of the item in F's tool-bar item vector. */
10996 for (i = 0; i < f->n_tool_bar_items; ++i)
10998 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11000 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11001 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11002 int hmargin, vmargin, relief, idx, end;
11004 /* If image is a vector, choose the image according to the
11005 button state. */
11006 image = PROP (TOOL_BAR_ITEM_IMAGES);
11007 if (VECTORP (image))
11009 if (enabled_p)
11010 idx = (selected_p
11011 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11012 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11013 else
11014 idx = (selected_p
11015 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11016 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11018 xassert (ASIZE (image) >= idx);
11019 image = AREF (image, idx);
11021 else
11022 idx = -1;
11024 /* Ignore invalid image specifications. */
11025 if (!valid_image_p (image))
11026 continue;
11028 /* Display the tool-bar button pressed, or depressed. */
11029 plist = Fcopy_sequence (XCDR (image));
11031 /* Compute margin and relief to draw. */
11032 relief = (tool_bar_button_relief >= 0
11033 ? tool_bar_button_relief
11034 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11035 hmargin = vmargin = relief;
11037 if (INTEGERP (Vtool_bar_button_margin)
11038 && XINT (Vtool_bar_button_margin) > 0)
11040 hmargin += XFASTINT (Vtool_bar_button_margin);
11041 vmargin += XFASTINT (Vtool_bar_button_margin);
11043 else if (CONSP (Vtool_bar_button_margin))
11045 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11046 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11047 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11049 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11050 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11051 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11054 if (auto_raise_tool_bar_buttons_p)
11056 /* Add a `:relief' property to the image spec if the item is
11057 selected. */
11058 if (selected_p)
11060 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11061 hmargin -= relief;
11062 vmargin -= relief;
11065 else
11067 /* If image is selected, display it pressed, i.e. with a
11068 negative relief. If it's not selected, display it with a
11069 raised relief. */
11070 plist = Fplist_put (plist, QCrelief,
11071 (selected_p
11072 ? make_number (-relief)
11073 : make_number (relief)));
11074 hmargin -= relief;
11075 vmargin -= relief;
11078 /* Put a margin around the image. */
11079 if (hmargin || vmargin)
11081 if (hmargin == vmargin)
11082 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11083 else
11084 plist = Fplist_put (plist, QCmargin,
11085 Fcons (make_number (hmargin),
11086 make_number (vmargin)));
11089 /* If button is not enabled, and we don't have special images
11090 for the disabled state, make the image appear disabled by
11091 applying an appropriate algorithm to it. */
11092 if (!enabled_p && idx < 0)
11093 plist = Fplist_put (plist, QCconversion, Qdisabled);
11095 /* Put a `display' text property on the string for the image to
11096 display. Put a `menu-item' property on the string that gives
11097 the start of this item's properties in the tool-bar items
11098 vector. */
11099 image = Fcons (Qimage, plist);
11100 props = list4 (Qdisplay, image,
11101 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11103 /* Let the last image hide all remaining spaces in the tool bar
11104 string. The string can be longer than needed when we reuse a
11105 previous string. */
11106 if (i + 1 == f->n_tool_bar_items)
11107 end = SCHARS (f->desired_tool_bar_string);
11108 else
11109 end = i + 1;
11110 Fadd_text_properties (make_number (i), make_number (end),
11111 props, f->desired_tool_bar_string);
11112 #undef PROP
11115 UNGCPRO;
11119 /* Display one line of the tool-bar of frame IT->f.
11121 HEIGHT specifies the desired height of the tool-bar line.
11122 If the actual height of the glyph row is less than HEIGHT, the
11123 row's height is increased to HEIGHT, and the icons are centered
11124 vertically in the new height.
11126 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11127 count a final empty row in case the tool-bar width exactly matches
11128 the window width.
11131 static void
11132 display_tool_bar_line (struct it *it, int height)
11134 struct glyph_row *row = it->glyph_row;
11135 int max_x = it->last_visible_x;
11136 struct glyph *last;
11138 prepare_desired_row (row);
11139 row->y = it->current_y;
11141 /* Note that this isn't made use of if the face hasn't a box,
11142 so there's no need to check the face here. */
11143 it->start_of_box_run_p = 1;
11145 while (it->current_x < max_x)
11147 int x, n_glyphs_before, i, nglyphs;
11148 struct it it_before;
11150 /* Get the next display element. */
11151 if (!get_next_display_element (it))
11153 /* Don't count empty row if we are counting needed tool-bar lines. */
11154 if (height < 0 && !it->hpos)
11155 return;
11156 break;
11159 /* Produce glyphs. */
11160 n_glyphs_before = row->used[TEXT_AREA];
11161 it_before = *it;
11163 PRODUCE_GLYPHS (it);
11165 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11166 i = 0;
11167 x = it_before.current_x;
11168 while (i < nglyphs)
11170 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11172 if (x + glyph->pixel_width > max_x)
11174 /* Glyph doesn't fit on line. Backtrack. */
11175 row->used[TEXT_AREA] = n_glyphs_before;
11176 *it = it_before;
11177 /* If this is the only glyph on this line, it will never fit on the
11178 tool-bar, so skip it. But ensure there is at least one glyph,
11179 so we don't accidentally disable the tool-bar. */
11180 if (n_glyphs_before == 0
11181 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11182 break;
11183 goto out;
11186 ++it->hpos;
11187 x += glyph->pixel_width;
11188 ++i;
11191 /* Stop at line end. */
11192 if (ITERATOR_AT_END_OF_LINE_P (it))
11193 break;
11195 set_iterator_to_next (it, 1);
11198 out:;
11200 row->displays_text_p = row->used[TEXT_AREA] != 0;
11202 /* Use default face for the border below the tool bar.
11204 FIXME: When auto-resize-tool-bars is grow-only, there is
11205 no additional border below the possibly empty tool-bar lines.
11206 So to make the extra empty lines look "normal", we have to
11207 use the tool-bar face for the border too. */
11208 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11209 it->face_id = DEFAULT_FACE_ID;
11211 extend_face_to_end_of_line (it);
11212 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11213 last->right_box_line_p = 1;
11214 if (last == row->glyphs[TEXT_AREA])
11215 last->left_box_line_p = 1;
11217 /* Make line the desired height and center it vertically. */
11218 if ((height -= it->max_ascent + it->max_descent) > 0)
11220 /* Don't add more than one line height. */
11221 height %= FRAME_LINE_HEIGHT (it->f);
11222 it->max_ascent += height / 2;
11223 it->max_descent += (height + 1) / 2;
11226 compute_line_metrics (it);
11228 /* If line is empty, make it occupy the rest of the tool-bar. */
11229 if (!row->displays_text_p)
11231 row->height = row->phys_height = it->last_visible_y - row->y;
11232 row->visible_height = row->height;
11233 row->ascent = row->phys_ascent = 0;
11234 row->extra_line_spacing = 0;
11237 row->full_width_p = 1;
11238 row->continued_p = 0;
11239 row->truncated_on_left_p = 0;
11240 row->truncated_on_right_p = 0;
11242 it->current_x = it->hpos = 0;
11243 it->current_y += row->height;
11244 ++it->vpos;
11245 ++it->glyph_row;
11249 /* Max tool-bar height. */
11251 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11252 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11254 /* Value is the number of screen lines needed to make all tool-bar
11255 items of frame F visible. The number of actual rows needed is
11256 returned in *N_ROWS if non-NULL. */
11258 static int
11259 tool_bar_lines_needed (struct frame *f, int *n_rows)
11261 struct window *w = XWINDOW (f->tool_bar_window);
11262 struct it it;
11263 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11264 the desired matrix, so use (unused) mode-line row as temporary row to
11265 avoid destroying the first tool-bar row. */
11266 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11268 /* Initialize an iterator for iteration over
11269 F->desired_tool_bar_string in the tool-bar window of frame F. */
11270 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11271 it.first_visible_x = 0;
11272 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11273 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11274 it.paragraph_embedding = L2R;
11276 while (!ITERATOR_AT_END_P (&it))
11278 clear_glyph_row (temp_row);
11279 it.glyph_row = temp_row;
11280 display_tool_bar_line (&it, -1);
11282 clear_glyph_row (temp_row);
11284 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11285 if (n_rows)
11286 *n_rows = it.vpos > 0 ? it.vpos : -1;
11288 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11292 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11293 0, 1, 0,
11294 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11295 (Lisp_Object frame)
11297 struct frame *f;
11298 struct window *w;
11299 int nlines = 0;
11301 if (NILP (frame))
11302 frame = selected_frame;
11303 else
11304 CHECK_FRAME (frame);
11305 f = XFRAME (frame);
11307 if (WINDOWP (f->tool_bar_window)
11308 && (w = XWINDOW (f->tool_bar_window),
11309 WINDOW_TOTAL_LINES (w) > 0))
11311 update_tool_bar (f, 1);
11312 if (f->n_tool_bar_items)
11314 build_desired_tool_bar_string (f);
11315 nlines = tool_bar_lines_needed (f, NULL);
11319 return make_number (nlines);
11323 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11324 height should be changed. */
11326 static int
11327 redisplay_tool_bar (struct frame *f)
11329 struct window *w;
11330 struct it it;
11331 struct glyph_row *row;
11333 #if defined (USE_GTK) || defined (HAVE_NS)
11334 if (FRAME_EXTERNAL_TOOL_BAR (f))
11335 update_frame_tool_bar (f);
11336 return 0;
11337 #endif
11339 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11340 do anything. This means you must start with tool-bar-lines
11341 non-zero to get the auto-sizing effect. Or in other words, you
11342 can turn off tool-bars by specifying tool-bar-lines zero. */
11343 if (!WINDOWP (f->tool_bar_window)
11344 || (w = XWINDOW (f->tool_bar_window),
11345 WINDOW_TOTAL_LINES (w) == 0))
11346 return 0;
11348 /* Set up an iterator for the tool-bar window. */
11349 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11350 it.first_visible_x = 0;
11351 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11352 row = it.glyph_row;
11354 /* Build a string that represents the contents of the tool-bar. */
11355 build_desired_tool_bar_string (f);
11356 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11357 /* FIXME: This should be controlled by a user option. But it
11358 doesn't make sense to have an R2L tool bar if the menu bar cannot
11359 be drawn also R2L, and making the menu bar R2L is tricky due
11360 toolkit-specific code that implements it. If an R2L tool bar is
11361 ever supported, display_tool_bar_line should also be augmented to
11362 call unproduce_glyphs like display_line and display_string
11363 do. */
11364 it.paragraph_embedding = L2R;
11366 if (f->n_tool_bar_rows == 0)
11368 int nlines;
11370 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11371 nlines != WINDOW_TOTAL_LINES (w)))
11373 Lisp_Object frame;
11374 int old_height = WINDOW_TOTAL_LINES (w);
11376 XSETFRAME (frame, f);
11377 Fmodify_frame_parameters (frame,
11378 Fcons (Fcons (Qtool_bar_lines,
11379 make_number (nlines)),
11380 Qnil));
11381 if (WINDOW_TOTAL_LINES (w) != old_height)
11383 clear_glyph_matrix (w->desired_matrix);
11384 fonts_changed_p = 1;
11385 return 1;
11390 /* Display as many lines as needed to display all tool-bar items. */
11392 if (f->n_tool_bar_rows > 0)
11394 int border, rows, height, extra;
11396 if (INTEGERP (Vtool_bar_border))
11397 border = XINT (Vtool_bar_border);
11398 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11399 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11400 else if (EQ (Vtool_bar_border, Qborder_width))
11401 border = f->border_width;
11402 else
11403 border = 0;
11404 if (border < 0)
11405 border = 0;
11407 rows = f->n_tool_bar_rows;
11408 height = max (1, (it.last_visible_y - border) / rows);
11409 extra = it.last_visible_y - border - height * rows;
11411 while (it.current_y < it.last_visible_y)
11413 int h = 0;
11414 if (extra > 0 && rows-- > 0)
11416 h = (extra + rows - 1) / rows;
11417 extra -= h;
11419 display_tool_bar_line (&it, height + h);
11422 else
11424 while (it.current_y < it.last_visible_y)
11425 display_tool_bar_line (&it, 0);
11428 /* It doesn't make much sense to try scrolling in the tool-bar
11429 window, so don't do it. */
11430 w->desired_matrix->no_scrolling_p = 1;
11431 w->must_be_updated_p = 1;
11433 if (!NILP (Vauto_resize_tool_bars))
11435 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11436 int change_height_p = 0;
11438 /* If we couldn't display everything, change the tool-bar's
11439 height if there is room for more. */
11440 if (IT_STRING_CHARPOS (it) < it.end_charpos
11441 && it.current_y < max_tool_bar_height)
11442 change_height_p = 1;
11444 row = it.glyph_row - 1;
11446 /* If there are blank lines at the end, except for a partially
11447 visible blank line at the end that is smaller than
11448 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11449 if (!row->displays_text_p
11450 && row->height >= FRAME_LINE_HEIGHT (f))
11451 change_height_p = 1;
11453 /* If row displays tool-bar items, but is partially visible,
11454 change the tool-bar's height. */
11455 if (row->displays_text_p
11456 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11457 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11458 change_height_p = 1;
11460 /* Resize windows as needed by changing the `tool-bar-lines'
11461 frame parameter. */
11462 if (change_height_p)
11464 Lisp_Object frame;
11465 int old_height = WINDOW_TOTAL_LINES (w);
11466 int nrows;
11467 int nlines = tool_bar_lines_needed (f, &nrows);
11469 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11470 && !f->minimize_tool_bar_window_p)
11471 ? (nlines > old_height)
11472 : (nlines != old_height));
11473 f->minimize_tool_bar_window_p = 0;
11475 if (change_height_p)
11477 XSETFRAME (frame, f);
11478 Fmodify_frame_parameters (frame,
11479 Fcons (Fcons (Qtool_bar_lines,
11480 make_number (nlines)),
11481 Qnil));
11482 if (WINDOW_TOTAL_LINES (w) != old_height)
11484 clear_glyph_matrix (w->desired_matrix);
11485 f->n_tool_bar_rows = nrows;
11486 fonts_changed_p = 1;
11487 return 1;
11493 f->minimize_tool_bar_window_p = 0;
11494 return 0;
11498 /* Get information about the tool-bar item which is displayed in GLYPH
11499 on frame F. Return in *PROP_IDX the index where tool-bar item
11500 properties start in F->tool_bar_items. Value is zero if
11501 GLYPH doesn't display a tool-bar item. */
11503 static int
11504 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11506 Lisp_Object prop;
11507 int success_p;
11508 int charpos;
11510 /* This function can be called asynchronously, which means we must
11511 exclude any possibility that Fget_text_property signals an
11512 error. */
11513 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11514 charpos = max (0, charpos);
11516 /* Get the text property `menu-item' at pos. The value of that
11517 property is the start index of this item's properties in
11518 F->tool_bar_items. */
11519 prop = Fget_text_property (make_number (charpos),
11520 Qmenu_item, f->current_tool_bar_string);
11521 if (INTEGERP (prop))
11523 *prop_idx = XINT (prop);
11524 success_p = 1;
11526 else
11527 success_p = 0;
11529 return success_p;
11533 /* Get information about the tool-bar item at position X/Y on frame F.
11534 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11535 the current matrix of the tool-bar window of F, or NULL if not
11536 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11537 item in F->tool_bar_items. Value is
11539 -1 if X/Y is not on a tool-bar item
11540 0 if X/Y is on the same item that was highlighted before.
11541 1 otherwise. */
11543 static int
11544 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11545 int *hpos, int *vpos, int *prop_idx)
11547 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11548 struct window *w = XWINDOW (f->tool_bar_window);
11549 int area;
11551 /* Find the glyph under X/Y. */
11552 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11553 if (*glyph == NULL)
11554 return -1;
11556 /* Get the start of this tool-bar item's properties in
11557 f->tool_bar_items. */
11558 if (!tool_bar_item_info (f, *glyph, prop_idx))
11559 return -1;
11561 /* Is mouse on the highlighted item? */
11562 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11563 && *vpos >= hlinfo->mouse_face_beg_row
11564 && *vpos <= hlinfo->mouse_face_end_row
11565 && (*vpos > hlinfo->mouse_face_beg_row
11566 || *hpos >= hlinfo->mouse_face_beg_col)
11567 && (*vpos < hlinfo->mouse_face_end_row
11568 || *hpos < hlinfo->mouse_face_end_col
11569 || hlinfo->mouse_face_past_end))
11570 return 0;
11572 return 1;
11576 /* EXPORT:
11577 Handle mouse button event on the tool-bar of frame F, at
11578 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11579 0 for button release. MODIFIERS is event modifiers for button
11580 release. */
11582 void
11583 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11584 unsigned int modifiers)
11586 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11587 struct window *w = XWINDOW (f->tool_bar_window);
11588 int hpos, vpos, prop_idx;
11589 struct glyph *glyph;
11590 Lisp_Object enabled_p;
11592 /* If not on the highlighted tool-bar item, return. */
11593 frame_to_window_pixel_xy (w, &x, &y);
11594 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11595 return;
11597 /* If item is disabled, do nothing. */
11598 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11599 if (NILP (enabled_p))
11600 return;
11602 if (down_p)
11604 /* Show item in pressed state. */
11605 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11606 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11607 last_tool_bar_item = prop_idx;
11609 else
11611 Lisp_Object key, frame;
11612 struct input_event event;
11613 EVENT_INIT (event);
11615 /* Show item in released state. */
11616 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11617 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11619 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11621 XSETFRAME (frame, f);
11622 event.kind = TOOL_BAR_EVENT;
11623 event.frame_or_window = frame;
11624 event.arg = frame;
11625 kbd_buffer_store_event (&event);
11627 event.kind = TOOL_BAR_EVENT;
11628 event.frame_or_window = frame;
11629 event.arg = key;
11630 event.modifiers = modifiers;
11631 kbd_buffer_store_event (&event);
11632 last_tool_bar_item = -1;
11637 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11638 tool-bar window-relative coordinates X/Y. Called from
11639 note_mouse_highlight. */
11641 static void
11642 note_tool_bar_highlight (struct frame *f, int x, int y)
11644 Lisp_Object window = f->tool_bar_window;
11645 struct window *w = XWINDOW (window);
11646 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11647 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11648 int hpos, vpos;
11649 struct glyph *glyph;
11650 struct glyph_row *row;
11651 int i;
11652 Lisp_Object enabled_p;
11653 int prop_idx;
11654 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11655 int mouse_down_p, rc;
11657 /* Function note_mouse_highlight is called with negative X/Y
11658 values when mouse moves outside of the frame. */
11659 if (x <= 0 || y <= 0)
11661 clear_mouse_face (hlinfo);
11662 return;
11665 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11666 if (rc < 0)
11668 /* Not on tool-bar item. */
11669 clear_mouse_face (hlinfo);
11670 return;
11672 else if (rc == 0)
11673 /* On same tool-bar item as before. */
11674 goto set_help_echo;
11676 clear_mouse_face (hlinfo);
11678 /* Mouse is down, but on different tool-bar item? */
11679 mouse_down_p = (dpyinfo->grabbed
11680 && f == last_mouse_frame
11681 && FRAME_LIVE_P (f));
11682 if (mouse_down_p
11683 && last_tool_bar_item != prop_idx)
11684 return;
11686 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11687 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11689 /* If tool-bar item is not enabled, don't highlight it. */
11690 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11691 if (!NILP (enabled_p))
11693 /* Compute the x-position of the glyph. In front and past the
11694 image is a space. We include this in the highlighted area. */
11695 row = MATRIX_ROW (w->current_matrix, vpos);
11696 for (i = x = 0; i < hpos; ++i)
11697 x += row->glyphs[TEXT_AREA][i].pixel_width;
11699 /* Record this as the current active region. */
11700 hlinfo->mouse_face_beg_col = hpos;
11701 hlinfo->mouse_face_beg_row = vpos;
11702 hlinfo->mouse_face_beg_x = x;
11703 hlinfo->mouse_face_beg_y = row->y;
11704 hlinfo->mouse_face_past_end = 0;
11706 hlinfo->mouse_face_end_col = hpos + 1;
11707 hlinfo->mouse_face_end_row = vpos;
11708 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11709 hlinfo->mouse_face_end_y = row->y;
11710 hlinfo->mouse_face_window = window;
11711 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11713 /* Display it as active. */
11714 show_mouse_face (hlinfo, draw);
11715 hlinfo->mouse_face_image_state = draw;
11718 set_help_echo:
11720 /* Set help_echo_string to a help string to display for this tool-bar item.
11721 XTread_socket does the rest. */
11722 help_echo_object = help_echo_window = Qnil;
11723 help_echo_pos = -1;
11724 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11725 if (NILP (help_echo_string))
11726 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11729 #endif /* HAVE_WINDOW_SYSTEM */
11733 /************************************************************************
11734 Horizontal scrolling
11735 ************************************************************************/
11737 static int hscroll_window_tree (Lisp_Object);
11738 static int hscroll_windows (Lisp_Object);
11740 /* For all leaf windows in the window tree rooted at WINDOW, set their
11741 hscroll value so that PT is (i) visible in the window, and (ii) so
11742 that it is not within a certain margin at the window's left and
11743 right border. Value is non-zero if any window's hscroll has been
11744 changed. */
11746 static int
11747 hscroll_window_tree (Lisp_Object window)
11749 int hscrolled_p = 0;
11750 int hscroll_relative_p = FLOATP (Vhscroll_step);
11751 int hscroll_step_abs = 0;
11752 double hscroll_step_rel = 0;
11754 if (hscroll_relative_p)
11756 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11757 if (hscroll_step_rel < 0)
11759 hscroll_relative_p = 0;
11760 hscroll_step_abs = 0;
11763 else if (INTEGERP (Vhscroll_step))
11765 hscroll_step_abs = XINT (Vhscroll_step);
11766 if (hscroll_step_abs < 0)
11767 hscroll_step_abs = 0;
11769 else
11770 hscroll_step_abs = 0;
11772 while (WINDOWP (window))
11774 struct window *w = XWINDOW (window);
11776 if (WINDOWP (w->hchild))
11777 hscrolled_p |= hscroll_window_tree (w->hchild);
11778 else if (WINDOWP (w->vchild))
11779 hscrolled_p |= hscroll_window_tree (w->vchild);
11780 else if (w->cursor.vpos >= 0)
11782 int h_margin;
11783 int text_area_width;
11784 struct glyph_row *current_cursor_row
11785 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11786 struct glyph_row *desired_cursor_row
11787 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11788 struct glyph_row *cursor_row
11789 = (desired_cursor_row->enabled_p
11790 ? desired_cursor_row
11791 : current_cursor_row);
11793 text_area_width = window_box_width (w, TEXT_AREA);
11795 /* Scroll when cursor is inside this scroll margin. */
11796 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11798 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11799 && ((XFASTINT (w->hscroll)
11800 && w->cursor.x <= h_margin)
11801 || (cursor_row->enabled_p
11802 && cursor_row->truncated_on_right_p
11803 && (w->cursor.x >= text_area_width - h_margin))))
11805 struct it it;
11806 int hscroll;
11807 struct buffer *saved_current_buffer;
11808 EMACS_INT pt;
11809 int wanted_x;
11811 /* Find point in a display of infinite width. */
11812 saved_current_buffer = current_buffer;
11813 current_buffer = XBUFFER (w->buffer);
11815 if (w == XWINDOW (selected_window))
11816 pt = PT;
11817 else
11819 pt = marker_position (w->pointm);
11820 pt = max (BEGV, pt);
11821 pt = min (ZV, pt);
11824 /* Move iterator to pt starting at cursor_row->start in
11825 a line with infinite width. */
11826 init_to_row_start (&it, w, cursor_row);
11827 it.last_visible_x = INFINITY;
11828 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11829 current_buffer = saved_current_buffer;
11831 /* Position cursor in window. */
11832 if (!hscroll_relative_p && hscroll_step_abs == 0)
11833 hscroll = max (0, (it.current_x
11834 - (ITERATOR_AT_END_OF_LINE_P (&it)
11835 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11836 : (text_area_width / 2))))
11837 / FRAME_COLUMN_WIDTH (it.f);
11838 else if (w->cursor.x >= text_area_width - h_margin)
11840 if (hscroll_relative_p)
11841 wanted_x = text_area_width * (1 - hscroll_step_rel)
11842 - h_margin;
11843 else
11844 wanted_x = text_area_width
11845 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11846 - h_margin;
11847 hscroll
11848 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11850 else
11852 if (hscroll_relative_p)
11853 wanted_x = text_area_width * hscroll_step_rel
11854 + h_margin;
11855 else
11856 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11857 + h_margin;
11858 hscroll
11859 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11861 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11863 /* Don't call Fset_window_hscroll if value hasn't
11864 changed because it will prevent redisplay
11865 optimizations. */
11866 if (XFASTINT (w->hscroll) != hscroll)
11868 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11869 w->hscroll = make_number (hscroll);
11870 hscrolled_p = 1;
11875 window = w->next;
11878 /* Value is non-zero if hscroll of any leaf window has been changed. */
11879 return hscrolled_p;
11883 /* Set hscroll so that cursor is visible and not inside horizontal
11884 scroll margins for all windows in the tree rooted at WINDOW. See
11885 also hscroll_window_tree above. Value is non-zero if any window's
11886 hscroll has been changed. If it has, desired matrices on the frame
11887 of WINDOW are cleared. */
11889 static int
11890 hscroll_windows (Lisp_Object window)
11892 int hscrolled_p = hscroll_window_tree (window);
11893 if (hscrolled_p)
11894 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11895 return hscrolled_p;
11900 /************************************************************************
11901 Redisplay
11902 ************************************************************************/
11904 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11905 to a non-zero value. This is sometimes handy to have in a debugger
11906 session. */
11908 #if GLYPH_DEBUG
11910 /* First and last unchanged row for try_window_id. */
11912 static int debug_first_unchanged_at_end_vpos;
11913 static int debug_last_unchanged_at_beg_vpos;
11915 /* Delta vpos and y. */
11917 static int debug_dvpos, debug_dy;
11919 /* Delta in characters and bytes for try_window_id. */
11921 static EMACS_INT debug_delta, debug_delta_bytes;
11923 /* Values of window_end_pos and window_end_vpos at the end of
11924 try_window_id. */
11926 static EMACS_INT debug_end_vpos;
11928 /* Append a string to W->desired_matrix->method. FMT is a printf
11929 format string. If trace_redisplay_p is non-zero also printf the
11930 resulting string to stderr. */
11932 static void debug_method_add (struct window *, char const *, ...)
11933 ATTRIBUTE_FORMAT_PRINTF (2, 3);
11935 static void
11936 debug_method_add (struct window *w, char const *fmt, ...)
11938 char buffer[512];
11939 char *method = w->desired_matrix->method;
11940 int len = strlen (method);
11941 int size = sizeof w->desired_matrix->method;
11942 int remaining = size - len - 1;
11943 va_list ap;
11945 va_start (ap, fmt);
11946 vsprintf (buffer, fmt, ap);
11947 va_end (ap);
11948 if (len && remaining)
11950 method[len] = '|';
11951 --remaining, ++len;
11954 strncpy (method + len, buffer, remaining);
11956 if (trace_redisplay_p)
11957 fprintf (stderr, "%p (%s): %s\n",
11959 ((BUFFERP (w->buffer)
11960 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
11961 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
11962 : "no buffer"),
11963 buffer);
11966 #endif /* GLYPH_DEBUG */
11969 /* Value is non-zero if all changes in window W, which displays
11970 current_buffer, are in the text between START and END. START is a
11971 buffer position, END is given as a distance from Z. Used in
11972 redisplay_internal for display optimization. */
11974 static inline int
11975 text_outside_line_unchanged_p (struct window *w,
11976 EMACS_INT start, EMACS_INT end)
11978 int unchanged_p = 1;
11980 /* If text or overlays have changed, see where. */
11981 if (XFASTINT (w->last_modified) < MODIFF
11982 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11984 /* Gap in the line? */
11985 if (GPT < start || Z - GPT < end)
11986 unchanged_p = 0;
11988 /* Changes start in front of the line, or end after it? */
11989 if (unchanged_p
11990 && (BEG_UNCHANGED < start - 1
11991 || END_UNCHANGED < end))
11992 unchanged_p = 0;
11994 /* If selective display, can't optimize if changes start at the
11995 beginning of the line. */
11996 if (unchanged_p
11997 && INTEGERP (BVAR (current_buffer, selective_display))
11998 && XINT (BVAR (current_buffer, selective_display)) > 0
11999 && (BEG_UNCHANGED < start || GPT <= start))
12000 unchanged_p = 0;
12002 /* If there are overlays at the start or end of the line, these
12003 may have overlay strings with newlines in them. A change at
12004 START, for instance, may actually concern the display of such
12005 overlay strings as well, and they are displayed on different
12006 lines. So, quickly rule out this case. (For the future, it
12007 might be desirable to implement something more telling than
12008 just BEG/END_UNCHANGED.) */
12009 if (unchanged_p)
12011 if (BEG + BEG_UNCHANGED == start
12012 && overlay_touches_p (start))
12013 unchanged_p = 0;
12014 if (END_UNCHANGED == end
12015 && overlay_touches_p (Z - end))
12016 unchanged_p = 0;
12019 /* Under bidi reordering, adding or deleting a character in the
12020 beginning of a paragraph, before the first strong directional
12021 character, can change the base direction of the paragraph (unless
12022 the buffer specifies a fixed paragraph direction), which will
12023 require to redisplay the whole paragraph. It might be worthwhile
12024 to find the paragraph limits and widen the range of redisplayed
12025 lines to that, but for now just give up this optimization. */
12026 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12027 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12028 unchanged_p = 0;
12031 return unchanged_p;
12035 /* Do a frame update, taking possible shortcuts into account. This is
12036 the main external entry point for redisplay.
12038 If the last redisplay displayed an echo area message and that message
12039 is no longer requested, we clear the echo area or bring back the
12040 mini-buffer if that is in use. */
12042 void
12043 redisplay (void)
12045 redisplay_internal ();
12049 static Lisp_Object
12050 overlay_arrow_string_or_property (Lisp_Object var)
12052 Lisp_Object val;
12054 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12055 return val;
12057 return Voverlay_arrow_string;
12060 /* Return 1 if there are any overlay-arrows in current_buffer. */
12061 static int
12062 overlay_arrow_in_current_buffer_p (void)
12064 Lisp_Object vlist;
12066 for (vlist = Voverlay_arrow_variable_list;
12067 CONSP (vlist);
12068 vlist = XCDR (vlist))
12070 Lisp_Object var = XCAR (vlist);
12071 Lisp_Object val;
12073 if (!SYMBOLP (var))
12074 continue;
12075 val = find_symbol_value (var);
12076 if (MARKERP (val)
12077 && current_buffer == XMARKER (val)->buffer)
12078 return 1;
12080 return 0;
12084 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12085 has changed. */
12087 static int
12088 overlay_arrows_changed_p (void)
12090 Lisp_Object vlist;
12092 for (vlist = Voverlay_arrow_variable_list;
12093 CONSP (vlist);
12094 vlist = XCDR (vlist))
12096 Lisp_Object var = XCAR (vlist);
12097 Lisp_Object val, pstr;
12099 if (!SYMBOLP (var))
12100 continue;
12101 val = find_symbol_value (var);
12102 if (!MARKERP (val))
12103 continue;
12104 if (! EQ (COERCE_MARKER (val),
12105 Fget (var, Qlast_arrow_position))
12106 || ! (pstr = overlay_arrow_string_or_property (var),
12107 EQ (pstr, Fget (var, Qlast_arrow_string))))
12108 return 1;
12110 return 0;
12113 /* Mark overlay arrows to be updated on next redisplay. */
12115 static void
12116 update_overlay_arrows (int up_to_date)
12118 Lisp_Object vlist;
12120 for (vlist = Voverlay_arrow_variable_list;
12121 CONSP (vlist);
12122 vlist = XCDR (vlist))
12124 Lisp_Object var = XCAR (vlist);
12126 if (!SYMBOLP (var))
12127 continue;
12129 if (up_to_date > 0)
12131 Lisp_Object val = find_symbol_value (var);
12132 Fput (var, Qlast_arrow_position,
12133 COERCE_MARKER (val));
12134 Fput (var, Qlast_arrow_string,
12135 overlay_arrow_string_or_property (var));
12137 else if (up_to_date < 0
12138 || !NILP (Fget (var, Qlast_arrow_position)))
12140 Fput (var, Qlast_arrow_position, Qt);
12141 Fput (var, Qlast_arrow_string, Qt);
12147 /* Return overlay arrow string to display at row.
12148 Return integer (bitmap number) for arrow bitmap in left fringe.
12149 Return nil if no overlay arrow. */
12151 static Lisp_Object
12152 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12154 Lisp_Object vlist;
12156 for (vlist = Voverlay_arrow_variable_list;
12157 CONSP (vlist);
12158 vlist = XCDR (vlist))
12160 Lisp_Object var = XCAR (vlist);
12161 Lisp_Object val;
12163 if (!SYMBOLP (var))
12164 continue;
12166 val = find_symbol_value (var);
12168 if (MARKERP (val)
12169 && current_buffer == XMARKER (val)->buffer
12170 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12172 if (FRAME_WINDOW_P (it->f)
12173 /* FIXME: if ROW->reversed_p is set, this should test
12174 the right fringe, not the left one. */
12175 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12177 #ifdef HAVE_WINDOW_SYSTEM
12178 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12180 int fringe_bitmap;
12181 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12182 return make_number (fringe_bitmap);
12184 #endif
12185 return make_number (-1); /* Use default arrow bitmap */
12187 return overlay_arrow_string_or_property (var);
12191 return Qnil;
12194 /* Return 1 if point moved out of or into a composition. Otherwise
12195 return 0. PREV_BUF and PREV_PT are the last point buffer and
12196 position. BUF and PT are the current point buffer and position. */
12198 static int
12199 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12200 struct buffer *buf, EMACS_INT pt)
12202 EMACS_INT start, end;
12203 Lisp_Object prop;
12204 Lisp_Object buffer;
12206 XSETBUFFER (buffer, buf);
12207 /* Check a composition at the last point if point moved within the
12208 same buffer. */
12209 if (prev_buf == buf)
12211 if (prev_pt == pt)
12212 /* Point didn't move. */
12213 return 0;
12215 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12216 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12217 && COMPOSITION_VALID_P (start, end, prop)
12218 && start < prev_pt && end > prev_pt)
12219 /* The last point was within the composition. Return 1 iff
12220 point moved out of the composition. */
12221 return (pt <= start || pt >= end);
12224 /* Check a composition at the current point. */
12225 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12226 && find_composition (pt, -1, &start, &end, &prop, buffer)
12227 && COMPOSITION_VALID_P (start, end, prop)
12228 && start < pt && end > pt);
12232 /* Reconsider the setting of B->clip_changed which is displayed
12233 in window W. */
12235 static inline void
12236 reconsider_clip_changes (struct window *w, struct buffer *b)
12238 if (b->clip_changed
12239 && !NILP (w->window_end_valid)
12240 && w->current_matrix->buffer == b
12241 && w->current_matrix->zv == BUF_ZV (b)
12242 && w->current_matrix->begv == BUF_BEGV (b))
12243 b->clip_changed = 0;
12245 /* If display wasn't paused, and W is not a tool bar window, see if
12246 point has been moved into or out of a composition. In that case,
12247 we set b->clip_changed to 1 to force updating the screen. If
12248 b->clip_changed has already been set to 1, we can skip this
12249 check. */
12250 if (!b->clip_changed
12251 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12253 EMACS_INT pt;
12255 if (w == XWINDOW (selected_window))
12256 pt = PT;
12257 else
12258 pt = marker_position (w->pointm);
12260 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12261 || pt != XINT (w->last_point))
12262 && check_point_in_composition (w->current_matrix->buffer,
12263 XINT (w->last_point),
12264 XBUFFER (w->buffer), pt))
12265 b->clip_changed = 1;
12270 /* Select FRAME to forward the values of frame-local variables into C
12271 variables so that the redisplay routines can access those values
12272 directly. */
12274 static void
12275 select_frame_for_redisplay (Lisp_Object frame)
12277 Lisp_Object tail, tem;
12278 Lisp_Object old = selected_frame;
12279 struct Lisp_Symbol *sym;
12281 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12283 selected_frame = frame;
12285 do {
12286 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12287 if (CONSP (XCAR (tail))
12288 && (tem = XCAR (XCAR (tail)),
12289 SYMBOLP (tem))
12290 && (sym = indirect_variable (XSYMBOL (tem)),
12291 sym->redirect == SYMBOL_LOCALIZED)
12292 && sym->val.blv->frame_local)
12293 /* Use find_symbol_value rather than Fsymbol_value
12294 to avoid an error if it is void. */
12295 find_symbol_value (tem);
12296 } while (!EQ (frame, old) && (frame = old, 1));
12300 #define STOP_POLLING \
12301 do { if (! polling_stopped_here) stop_polling (); \
12302 polling_stopped_here = 1; } while (0)
12304 #define RESUME_POLLING \
12305 do { if (polling_stopped_here) start_polling (); \
12306 polling_stopped_here = 0; } while (0)
12309 /* Perhaps in the future avoid recentering windows if it
12310 is not necessary; currently that causes some problems. */
12312 static void
12313 redisplay_internal (void)
12315 struct window *w = XWINDOW (selected_window);
12316 struct window *sw;
12317 struct frame *fr;
12318 int pending;
12319 int must_finish = 0;
12320 struct text_pos tlbufpos, tlendpos;
12321 int number_of_visible_frames;
12322 int count, count1;
12323 struct frame *sf;
12324 int polling_stopped_here = 0;
12325 Lisp_Object old_frame = selected_frame;
12327 /* Non-zero means redisplay has to consider all windows on all
12328 frames. Zero means, only selected_window is considered. */
12329 int consider_all_windows_p;
12331 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12333 /* No redisplay if running in batch mode or frame is not yet fully
12334 initialized, or redisplay is explicitly turned off by setting
12335 Vinhibit_redisplay. */
12336 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12337 || !NILP (Vinhibit_redisplay))
12338 return;
12340 /* Don't examine these until after testing Vinhibit_redisplay.
12341 When Emacs is shutting down, perhaps because its connection to
12342 X has dropped, we should not look at them at all. */
12343 fr = XFRAME (w->frame);
12344 sf = SELECTED_FRAME ();
12346 if (!fr->glyphs_initialized_p)
12347 return;
12349 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12350 if (popup_activated ())
12351 return;
12352 #endif
12354 /* I don't think this happens but let's be paranoid. */
12355 if (redisplaying_p)
12356 return;
12358 /* Record a function that resets redisplaying_p to its old value
12359 when we leave this function. */
12360 count = SPECPDL_INDEX ();
12361 record_unwind_protect (unwind_redisplay,
12362 Fcons (make_number (redisplaying_p), selected_frame));
12363 ++redisplaying_p;
12364 specbind (Qinhibit_free_realized_faces, Qnil);
12367 Lisp_Object tail, frame;
12369 FOR_EACH_FRAME (tail, frame)
12371 struct frame *f = XFRAME (frame);
12372 f->already_hscrolled_p = 0;
12376 retry:
12377 /* Remember the currently selected window. */
12378 sw = w;
12380 if (!EQ (old_frame, selected_frame)
12381 && FRAME_LIVE_P (XFRAME (old_frame)))
12382 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12383 selected_frame and selected_window to be temporarily out-of-sync so
12384 when we come back here via `goto retry', we need to resync because we
12385 may need to run Elisp code (via prepare_menu_bars). */
12386 select_frame_for_redisplay (old_frame);
12388 pending = 0;
12389 reconsider_clip_changes (w, current_buffer);
12390 last_escape_glyph_frame = NULL;
12391 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12392 last_glyphless_glyph_frame = NULL;
12393 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12395 /* If new fonts have been loaded that make a glyph matrix adjustment
12396 necessary, do it. */
12397 if (fonts_changed_p)
12399 adjust_glyphs (NULL);
12400 ++windows_or_buffers_changed;
12401 fonts_changed_p = 0;
12404 /* If face_change_count is non-zero, init_iterator will free all
12405 realized faces, which includes the faces referenced from current
12406 matrices. So, we can't reuse current matrices in this case. */
12407 if (face_change_count)
12408 ++windows_or_buffers_changed;
12410 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12411 && FRAME_TTY (sf)->previous_frame != sf)
12413 /* Since frames on a single ASCII terminal share the same
12414 display area, displaying a different frame means redisplay
12415 the whole thing. */
12416 windows_or_buffers_changed++;
12417 SET_FRAME_GARBAGED (sf);
12418 #ifndef DOS_NT
12419 set_tty_color_mode (FRAME_TTY (sf), sf);
12420 #endif
12421 FRAME_TTY (sf)->previous_frame = sf;
12424 /* Set the visible flags for all frames. Do this before checking
12425 for resized or garbaged frames; they want to know if their frames
12426 are visible. See the comment in frame.h for
12427 FRAME_SAMPLE_VISIBILITY. */
12429 Lisp_Object tail, frame;
12431 number_of_visible_frames = 0;
12433 FOR_EACH_FRAME (tail, frame)
12435 struct frame *f = XFRAME (frame);
12437 FRAME_SAMPLE_VISIBILITY (f);
12438 if (FRAME_VISIBLE_P (f))
12439 ++number_of_visible_frames;
12440 clear_desired_matrices (f);
12444 /* Notice any pending interrupt request to change frame size. */
12445 do_pending_window_change (1);
12447 /* do_pending_window_change could change the selected_window due to
12448 frame resizing which makes the selected window too small. */
12449 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12451 sw = w;
12452 reconsider_clip_changes (w, current_buffer);
12455 /* Clear frames marked as garbaged. */
12456 if (frame_garbaged)
12457 clear_garbaged_frames ();
12459 /* Build menubar and tool-bar items. */
12460 if (NILP (Vmemory_full))
12461 prepare_menu_bars ();
12463 if (windows_or_buffers_changed)
12464 update_mode_lines++;
12466 /* Detect case that we need to write or remove a star in the mode line. */
12467 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12469 w->update_mode_line = Qt;
12470 if (buffer_shared > 1)
12471 update_mode_lines++;
12474 /* Avoid invocation of point motion hooks by `current_column' below. */
12475 count1 = SPECPDL_INDEX ();
12476 specbind (Qinhibit_point_motion_hooks, Qt);
12478 /* If %c is in the mode line, update it if needed. */
12479 if (!NILP (w->column_number_displayed)
12480 /* This alternative quickly identifies a common case
12481 where no change is needed. */
12482 && !(PT == XFASTINT (w->last_point)
12483 && XFASTINT (w->last_modified) >= MODIFF
12484 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12485 && (XFASTINT (w->column_number_displayed) != current_column ()))
12486 w->update_mode_line = Qt;
12488 unbind_to (count1, Qnil);
12490 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12492 /* The variable buffer_shared is set in redisplay_window and
12493 indicates that we redisplay a buffer in different windows. See
12494 there. */
12495 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12496 || cursor_type_changed);
12498 /* If specs for an arrow have changed, do thorough redisplay
12499 to ensure we remove any arrow that should no longer exist. */
12500 if (overlay_arrows_changed_p ())
12501 consider_all_windows_p = windows_or_buffers_changed = 1;
12503 /* Normally the message* functions will have already displayed and
12504 updated the echo area, but the frame may have been trashed, or
12505 the update may have been preempted, so display the echo area
12506 again here. Checking message_cleared_p captures the case that
12507 the echo area should be cleared. */
12508 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12509 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12510 || (message_cleared_p
12511 && minibuf_level == 0
12512 /* If the mini-window is currently selected, this means the
12513 echo-area doesn't show through. */
12514 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12516 int window_height_changed_p = echo_area_display (0);
12517 must_finish = 1;
12519 /* If we don't display the current message, don't clear the
12520 message_cleared_p flag, because, if we did, we wouldn't clear
12521 the echo area in the next redisplay which doesn't preserve
12522 the echo area. */
12523 if (!display_last_displayed_message_p)
12524 message_cleared_p = 0;
12526 if (fonts_changed_p)
12527 goto retry;
12528 else if (window_height_changed_p)
12530 consider_all_windows_p = 1;
12531 ++update_mode_lines;
12532 ++windows_or_buffers_changed;
12534 /* If window configuration was changed, frames may have been
12535 marked garbaged. Clear them or we will experience
12536 surprises wrt scrolling. */
12537 if (frame_garbaged)
12538 clear_garbaged_frames ();
12541 else if (EQ (selected_window, minibuf_window)
12542 && (current_buffer->clip_changed
12543 || XFASTINT (w->last_modified) < MODIFF
12544 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12545 && resize_mini_window (w, 0))
12547 /* Resized active mini-window to fit the size of what it is
12548 showing if its contents might have changed. */
12549 must_finish = 1;
12550 /* FIXME: this causes all frames to be updated, which seems unnecessary
12551 since only the current frame needs to be considered. This function needs
12552 to be rewritten with two variables, consider_all_windows and
12553 consider_all_frames. */
12554 consider_all_windows_p = 1;
12555 ++windows_or_buffers_changed;
12556 ++update_mode_lines;
12558 /* If window configuration was changed, frames may have been
12559 marked garbaged. Clear them or we will experience
12560 surprises wrt scrolling. */
12561 if (frame_garbaged)
12562 clear_garbaged_frames ();
12566 /* If showing the region, and mark has changed, we must redisplay
12567 the whole window. The assignment to this_line_start_pos prevents
12568 the optimization directly below this if-statement. */
12569 if (((!NILP (Vtransient_mark_mode)
12570 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12571 != !NILP (w->region_showing))
12572 || (!NILP (w->region_showing)
12573 && !EQ (w->region_showing,
12574 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12575 CHARPOS (this_line_start_pos) = 0;
12577 /* Optimize the case that only the line containing the cursor in the
12578 selected window has changed. Variables starting with this_ are
12579 set in display_line and record information about the line
12580 containing the cursor. */
12581 tlbufpos = this_line_start_pos;
12582 tlendpos = this_line_end_pos;
12583 if (!consider_all_windows_p
12584 && CHARPOS (tlbufpos) > 0
12585 && NILP (w->update_mode_line)
12586 && !current_buffer->clip_changed
12587 && !current_buffer->prevent_redisplay_optimizations_p
12588 && FRAME_VISIBLE_P (XFRAME (w->frame))
12589 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12590 /* Make sure recorded data applies to current buffer, etc. */
12591 && this_line_buffer == current_buffer
12592 && current_buffer == XBUFFER (w->buffer)
12593 && NILP (w->force_start)
12594 && NILP (w->optional_new_start)
12595 /* Point must be on the line that we have info recorded about. */
12596 && PT >= CHARPOS (tlbufpos)
12597 && PT <= Z - CHARPOS (tlendpos)
12598 /* All text outside that line, including its final newline,
12599 must be unchanged. */
12600 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12601 CHARPOS (tlendpos)))
12603 if (CHARPOS (tlbufpos) > BEGV
12604 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12605 && (CHARPOS (tlbufpos) == ZV
12606 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12607 /* Former continuation line has disappeared by becoming empty. */
12608 goto cancel;
12609 else if (XFASTINT (w->last_modified) < MODIFF
12610 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12611 || MINI_WINDOW_P (w))
12613 /* We have to handle the case of continuation around a
12614 wide-column character (see the comment in indent.c around
12615 line 1340).
12617 For instance, in the following case:
12619 -------- Insert --------
12620 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12621 J_I_ ==> J_I_ `^^' are cursors.
12622 ^^ ^^
12623 -------- --------
12625 As we have to redraw the line above, we cannot use this
12626 optimization. */
12628 struct it it;
12629 int line_height_before = this_line_pixel_height;
12631 /* Note that start_display will handle the case that the
12632 line starting at tlbufpos is a continuation line. */
12633 start_display (&it, w, tlbufpos);
12635 /* Implementation note: It this still necessary? */
12636 if (it.current_x != this_line_start_x)
12637 goto cancel;
12639 TRACE ((stderr, "trying display optimization 1\n"));
12640 w->cursor.vpos = -1;
12641 overlay_arrow_seen = 0;
12642 it.vpos = this_line_vpos;
12643 it.current_y = this_line_y;
12644 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12645 display_line (&it);
12647 /* If line contains point, is not continued,
12648 and ends at same distance from eob as before, we win. */
12649 if (w->cursor.vpos >= 0
12650 /* Line is not continued, otherwise this_line_start_pos
12651 would have been set to 0 in display_line. */
12652 && CHARPOS (this_line_start_pos)
12653 /* Line ends as before. */
12654 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12655 /* Line has same height as before. Otherwise other lines
12656 would have to be shifted up or down. */
12657 && this_line_pixel_height == line_height_before)
12659 /* If this is not the window's last line, we must adjust
12660 the charstarts of the lines below. */
12661 if (it.current_y < it.last_visible_y)
12663 struct glyph_row *row
12664 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12665 EMACS_INT delta, delta_bytes;
12667 /* We used to distinguish between two cases here,
12668 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12669 when the line ends in a newline or the end of the
12670 buffer's accessible portion. But both cases did
12671 the same, so they were collapsed. */
12672 delta = (Z
12673 - CHARPOS (tlendpos)
12674 - MATRIX_ROW_START_CHARPOS (row));
12675 delta_bytes = (Z_BYTE
12676 - BYTEPOS (tlendpos)
12677 - MATRIX_ROW_START_BYTEPOS (row));
12679 increment_matrix_positions (w->current_matrix,
12680 this_line_vpos + 1,
12681 w->current_matrix->nrows,
12682 delta, delta_bytes);
12685 /* If this row displays text now but previously didn't,
12686 or vice versa, w->window_end_vpos may have to be
12687 adjusted. */
12688 if ((it.glyph_row - 1)->displays_text_p)
12690 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12691 XSETINT (w->window_end_vpos, this_line_vpos);
12693 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12694 && this_line_vpos > 0)
12695 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12696 w->window_end_valid = Qnil;
12698 /* Update hint: No need to try to scroll in update_window. */
12699 w->desired_matrix->no_scrolling_p = 1;
12701 #if GLYPH_DEBUG
12702 *w->desired_matrix->method = 0;
12703 debug_method_add (w, "optimization 1");
12704 #endif
12705 #ifdef HAVE_WINDOW_SYSTEM
12706 update_window_fringes (w, 0);
12707 #endif
12708 goto update;
12710 else
12711 goto cancel;
12713 else if (/* Cursor position hasn't changed. */
12714 PT == XFASTINT (w->last_point)
12715 /* Make sure the cursor was last displayed
12716 in this window. Otherwise we have to reposition it. */
12717 && 0 <= w->cursor.vpos
12718 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12720 if (!must_finish)
12722 do_pending_window_change (1);
12723 /* If selected_window changed, redisplay again. */
12724 if (WINDOWP (selected_window)
12725 && (w = XWINDOW (selected_window)) != sw)
12726 goto retry;
12728 /* We used to always goto end_of_redisplay here, but this
12729 isn't enough if we have a blinking cursor. */
12730 if (w->cursor_off_p == w->last_cursor_off_p)
12731 goto end_of_redisplay;
12733 goto update;
12735 /* If highlighting the region, or if the cursor is in the echo area,
12736 then we can't just move the cursor. */
12737 else if (! (!NILP (Vtransient_mark_mode)
12738 && !NILP (BVAR (current_buffer, mark_active)))
12739 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12740 || highlight_nonselected_windows)
12741 && NILP (w->region_showing)
12742 && NILP (Vshow_trailing_whitespace)
12743 && !cursor_in_echo_area)
12745 struct it it;
12746 struct glyph_row *row;
12748 /* Skip from tlbufpos to PT and see where it is. Note that
12749 PT may be in invisible text. If so, we will end at the
12750 next visible position. */
12751 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12752 NULL, DEFAULT_FACE_ID);
12753 it.current_x = this_line_start_x;
12754 it.current_y = this_line_y;
12755 it.vpos = this_line_vpos;
12757 /* The call to move_it_to stops in front of PT, but
12758 moves over before-strings. */
12759 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12761 if (it.vpos == this_line_vpos
12762 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12763 row->enabled_p))
12765 xassert (this_line_vpos == it.vpos);
12766 xassert (this_line_y == it.current_y);
12767 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12768 #if GLYPH_DEBUG
12769 *w->desired_matrix->method = 0;
12770 debug_method_add (w, "optimization 3");
12771 #endif
12772 goto update;
12774 else
12775 goto cancel;
12778 cancel:
12779 /* Text changed drastically or point moved off of line. */
12780 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12783 CHARPOS (this_line_start_pos) = 0;
12784 consider_all_windows_p |= buffer_shared > 1;
12785 ++clear_face_cache_count;
12786 #ifdef HAVE_WINDOW_SYSTEM
12787 ++clear_image_cache_count;
12788 #endif
12790 /* Build desired matrices, and update the display. If
12791 consider_all_windows_p is non-zero, do it for all windows on all
12792 frames. Otherwise do it for selected_window, only. */
12794 if (consider_all_windows_p)
12796 Lisp_Object tail, frame;
12798 FOR_EACH_FRAME (tail, frame)
12799 XFRAME (frame)->updated_p = 0;
12801 /* Recompute # windows showing selected buffer. This will be
12802 incremented each time such a window is displayed. */
12803 buffer_shared = 0;
12805 FOR_EACH_FRAME (tail, frame)
12807 struct frame *f = XFRAME (frame);
12809 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12811 if (! EQ (frame, selected_frame))
12812 /* Select the frame, for the sake of frame-local
12813 variables. */
12814 select_frame_for_redisplay (frame);
12816 /* Mark all the scroll bars to be removed; we'll redeem
12817 the ones we want when we redisplay their windows. */
12818 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12819 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12821 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12822 redisplay_windows (FRAME_ROOT_WINDOW (f));
12824 /* The X error handler may have deleted that frame. */
12825 if (!FRAME_LIVE_P (f))
12826 continue;
12828 /* Any scroll bars which redisplay_windows should have
12829 nuked should now go away. */
12830 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12831 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12833 /* If fonts changed, display again. */
12834 /* ??? rms: I suspect it is a mistake to jump all the way
12835 back to retry here. It should just retry this frame. */
12836 if (fonts_changed_p)
12837 goto retry;
12839 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12841 /* See if we have to hscroll. */
12842 if (!f->already_hscrolled_p)
12844 f->already_hscrolled_p = 1;
12845 if (hscroll_windows (f->root_window))
12846 goto retry;
12849 /* Prevent various kinds of signals during display
12850 update. stdio is not robust about handling
12851 signals, which can cause an apparent I/O
12852 error. */
12853 if (interrupt_input)
12854 unrequest_sigio ();
12855 STOP_POLLING;
12857 /* Update the display. */
12858 set_window_update_flags (XWINDOW (f->root_window), 1);
12859 pending |= update_frame (f, 0, 0);
12860 f->updated_p = 1;
12865 if (!EQ (old_frame, selected_frame)
12866 && FRAME_LIVE_P (XFRAME (old_frame)))
12867 /* We played a bit fast-and-loose above and allowed selected_frame
12868 and selected_window to be temporarily out-of-sync but let's make
12869 sure this stays contained. */
12870 select_frame_for_redisplay (old_frame);
12871 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12873 if (!pending)
12875 /* Do the mark_window_display_accurate after all windows have
12876 been redisplayed because this call resets flags in buffers
12877 which are needed for proper redisplay. */
12878 FOR_EACH_FRAME (tail, frame)
12880 struct frame *f = XFRAME (frame);
12881 if (f->updated_p)
12883 mark_window_display_accurate (f->root_window, 1);
12884 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12885 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12890 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12892 Lisp_Object mini_window;
12893 struct frame *mini_frame;
12895 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12896 /* Use list_of_error, not Qerror, so that
12897 we catch only errors and don't run the debugger. */
12898 internal_condition_case_1 (redisplay_window_1, selected_window,
12899 list_of_error,
12900 redisplay_window_error);
12902 /* Compare desired and current matrices, perform output. */
12904 update:
12905 /* If fonts changed, display again. */
12906 if (fonts_changed_p)
12907 goto retry;
12909 /* Prevent various kinds of signals during display update.
12910 stdio is not robust about handling signals,
12911 which can cause an apparent I/O error. */
12912 if (interrupt_input)
12913 unrequest_sigio ();
12914 STOP_POLLING;
12916 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12918 if (hscroll_windows (selected_window))
12919 goto retry;
12921 XWINDOW (selected_window)->must_be_updated_p = 1;
12922 pending = update_frame (sf, 0, 0);
12925 /* We may have called echo_area_display at the top of this
12926 function. If the echo area is on another frame, that may
12927 have put text on a frame other than the selected one, so the
12928 above call to update_frame would not have caught it. Catch
12929 it here. */
12930 mini_window = FRAME_MINIBUF_WINDOW (sf);
12931 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12933 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12935 XWINDOW (mini_window)->must_be_updated_p = 1;
12936 pending |= update_frame (mini_frame, 0, 0);
12937 if (!pending && hscroll_windows (mini_window))
12938 goto retry;
12942 /* If display was paused because of pending input, make sure we do a
12943 thorough update the next time. */
12944 if (pending)
12946 /* Prevent the optimization at the beginning of
12947 redisplay_internal that tries a single-line update of the
12948 line containing the cursor in the selected window. */
12949 CHARPOS (this_line_start_pos) = 0;
12951 /* Let the overlay arrow be updated the next time. */
12952 update_overlay_arrows (0);
12954 /* If we pause after scrolling, some rows in the current
12955 matrices of some windows are not valid. */
12956 if (!WINDOW_FULL_WIDTH_P (w)
12957 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12958 update_mode_lines = 1;
12960 else
12962 if (!consider_all_windows_p)
12964 /* This has already been done above if
12965 consider_all_windows_p is set. */
12966 mark_window_display_accurate_1 (w, 1);
12968 /* Say overlay arrows are up to date. */
12969 update_overlay_arrows (1);
12971 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12972 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12975 update_mode_lines = 0;
12976 windows_or_buffers_changed = 0;
12977 cursor_type_changed = 0;
12980 /* Start SIGIO interrupts coming again. Having them off during the
12981 code above makes it less likely one will discard output, but not
12982 impossible, since there might be stuff in the system buffer here.
12983 But it is much hairier to try to do anything about that. */
12984 if (interrupt_input)
12985 request_sigio ();
12986 RESUME_POLLING;
12988 /* If a frame has become visible which was not before, redisplay
12989 again, so that we display it. Expose events for such a frame
12990 (which it gets when becoming visible) don't call the parts of
12991 redisplay constructing glyphs, so simply exposing a frame won't
12992 display anything in this case. So, we have to display these
12993 frames here explicitly. */
12994 if (!pending)
12996 Lisp_Object tail, frame;
12997 int new_count = 0;
12999 FOR_EACH_FRAME (tail, frame)
13001 int this_is_visible = 0;
13003 if (XFRAME (frame)->visible)
13004 this_is_visible = 1;
13005 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13006 if (XFRAME (frame)->visible)
13007 this_is_visible = 1;
13009 if (this_is_visible)
13010 new_count++;
13013 if (new_count != number_of_visible_frames)
13014 windows_or_buffers_changed++;
13017 /* Change frame size now if a change is pending. */
13018 do_pending_window_change (1);
13020 /* If we just did a pending size change, or have additional
13021 visible frames, or selected_window changed, redisplay again. */
13022 if ((windows_or_buffers_changed && !pending)
13023 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13024 goto retry;
13026 /* Clear the face and image caches.
13028 We used to do this only if consider_all_windows_p. But the cache
13029 needs to be cleared if a timer creates images in the current
13030 buffer (e.g. the test case in Bug#6230). */
13032 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13034 clear_face_cache (0);
13035 clear_face_cache_count = 0;
13038 #ifdef HAVE_WINDOW_SYSTEM
13039 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13041 clear_image_caches (Qnil);
13042 clear_image_cache_count = 0;
13044 #endif /* HAVE_WINDOW_SYSTEM */
13046 end_of_redisplay:
13047 unbind_to (count, Qnil);
13048 RESUME_POLLING;
13052 /* Redisplay, but leave alone any recent echo area message unless
13053 another message has been requested in its place.
13055 This is useful in situations where you need to redisplay but no
13056 user action has occurred, making it inappropriate for the message
13057 area to be cleared. See tracking_off and
13058 wait_reading_process_output for examples of these situations.
13060 FROM_WHERE is an integer saying from where this function was
13061 called. This is useful for debugging. */
13063 void
13064 redisplay_preserve_echo_area (int from_where)
13066 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13068 if (!NILP (echo_area_buffer[1]))
13070 /* We have a previously displayed message, but no current
13071 message. Redisplay the previous message. */
13072 display_last_displayed_message_p = 1;
13073 redisplay_internal ();
13074 display_last_displayed_message_p = 0;
13076 else
13077 redisplay_internal ();
13079 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13080 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13081 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13085 /* Function registered with record_unwind_protect in
13086 redisplay_internal. Reset redisplaying_p to the value it had
13087 before redisplay_internal was called, and clear
13088 prevent_freeing_realized_faces_p. It also selects the previously
13089 selected frame, unless it has been deleted (by an X connection
13090 failure during redisplay, for example). */
13092 static Lisp_Object
13093 unwind_redisplay (Lisp_Object val)
13095 Lisp_Object old_redisplaying_p, old_frame;
13097 old_redisplaying_p = XCAR (val);
13098 redisplaying_p = XFASTINT (old_redisplaying_p);
13099 old_frame = XCDR (val);
13100 if (! EQ (old_frame, selected_frame)
13101 && FRAME_LIVE_P (XFRAME (old_frame)))
13102 select_frame_for_redisplay (old_frame);
13103 return Qnil;
13107 /* Mark the display of window W as accurate or inaccurate. If
13108 ACCURATE_P is non-zero mark display of W as accurate. If
13109 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13110 redisplay_internal is called. */
13112 static void
13113 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13115 if (BUFFERP (w->buffer))
13117 struct buffer *b = XBUFFER (w->buffer);
13119 w->last_modified
13120 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13121 w->last_overlay_modified
13122 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13123 w->last_had_star
13124 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13126 if (accurate_p)
13128 b->clip_changed = 0;
13129 b->prevent_redisplay_optimizations_p = 0;
13131 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13132 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13133 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13134 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13136 w->current_matrix->buffer = b;
13137 w->current_matrix->begv = BUF_BEGV (b);
13138 w->current_matrix->zv = BUF_ZV (b);
13140 w->last_cursor = w->cursor;
13141 w->last_cursor_off_p = w->cursor_off_p;
13143 if (w == XWINDOW (selected_window))
13144 w->last_point = make_number (BUF_PT (b));
13145 else
13146 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13150 if (accurate_p)
13152 w->window_end_valid = w->buffer;
13153 w->update_mode_line = Qnil;
13158 /* Mark the display of windows in the window tree rooted at WINDOW as
13159 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13160 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13161 be redisplayed the next time redisplay_internal is called. */
13163 void
13164 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13166 struct window *w;
13168 for (; !NILP (window); window = w->next)
13170 w = XWINDOW (window);
13171 mark_window_display_accurate_1 (w, accurate_p);
13173 if (!NILP (w->vchild))
13174 mark_window_display_accurate (w->vchild, accurate_p);
13175 if (!NILP (w->hchild))
13176 mark_window_display_accurate (w->hchild, accurate_p);
13179 if (accurate_p)
13181 update_overlay_arrows (1);
13183 else
13185 /* Force a thorough redisplay the next time by setting
13186 last_arrow_position and last_arrow_string to t, which is
13187 unequal to any useful value of Voverlay_arrow_... */
13188 update_overlay_arrows (-1);
13193 /* Return value in display table DP (Lisp_Char_Table *) for character
13194 C. Since a display table doesn't have any parent, we don't have to
13195 follow parent. Do not call this function directly but use the
13196 macro DISP_CHAR_VECTOR. */
13198 Lisp_Object
13199 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13201 Lisp_Object val;
13203 if (ASCII_CHAR_P (c))
13205 val = dp->ascii;
13206 if (SUB_CHAR_TABLE_P (val))
13207 val = XSUB_CHAR_TABLE (val)->contents[c];
13209 else
13211 Lisp_Object table;
13213 XSETCHAR_TABLE (table, dp);
13214 val = char_table_ref (table, c);
13216 if (NILP (val))
13217 val = dp->defalt;
13218 return val;
13223 /***********************************************************************
13224 Window Redisplay
13225 ***********************************************************************/
13227 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13229 static void
13230 redisplay_windows (Lisp_Object window)
13232 while (!NILP (window))
13234 struct window *w = XWINDOW (window);
13236 if (!NILP (w->hchild))
13237 redisplay_windows (w->hchild);
13238 else if (!NILP (w->vchild))
13239 redisplay_windows (w->vchild);
13240 else if (!NILP (w->buffer))
13242 displayed_buffer = XBUFFER (w->buffer);
13243 /* Use list_of_error, not Qerror, so that
13244 we catch only errors and don't run the debugger. */
13245 internal_condition_case_1 (redisplay_window_0, window,
13246 list_of_error,
13247 redisplay_window_error);
13250 window = w->next;
13254 static Lisp_Object
13255 redisplay_window_error (Lisp_Object ignore)
13257 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13258 return Qnil;
13261 static Lisp_Object
13262 redisplay_window_0 (Lisp_Object window)
13264 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13265 redisplay_window (window, 0);
13266 return Qnil;
13269 static Lisp_Object
13270 redisplay_window_1 (Lisp_Object window)
13272 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13273 redisplay_window (window, 1);
13274 return Qnil;
13278 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13279 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13280 which positions recorded in ROW differ from current buffer
13281 positions.
13283 Return 0 if cursor is not on this row, 1 otherwise. */
13285 static int
13286 set_cursor_from_row (struct window *w, struct glyph_row *row,
13287 struct glyph_matrix *matrix,
13288 EMACS_INT delta, EMACS_INT delta_bytes,
13289 int dy, int dvpos)
13291 struct glyph *glyph = row->glyphs[TEXT_AREA];
13292 struct glyph *end = glyph + row->used[TEXT_AREA];
13293 struct glyph *cursor = NULL;
13294 /* The last known character position in row. */
13295 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13296 int x = row->x;
13297 EMACS_INT pt_old = PT - delta;
13298 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13299 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13300 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13301 /* A glyph beyond the edge of TEXT_AREA which we should never
13302 touch. */
13303 struct glyph *glyphs_end = end;
13304 /* Non-zero means we've found a match for cursor position, but that
13305 glyph has the avoid_cursor_p flag set. */
13306 int match_with_avoid_cursor = 0;
13307 /* Non-zero means we've seen at least one glyph that came from a
13308 display string. */
13309 int string_seen = 0;
13310 /* Largest and smalles buffer positions seen so far during scan of
13311 glyph row. */
13312 EMACS_INT bpos_max = pos_before;
13313 EMACS_INT bpos_min = pos_after;
13314 /* Last buffer position covered by an overlay string with an integer
13315 `cursor' property. */
13316 EMACS_INT bpos_covered = 0;
13317 /* Non-zero means the display string on which to display the cursor
13318 comes from a text property, not from an overlay. */
13319 int string_from_text_prop = 0;
13321 /* Skip over glyphs not having an object at the start and the end of
13322 the row. These are special glyphs like truncation marks on
13323 terminal frames. */
13324 if (row->displays_text_p)
13326 if (!row->reversed_p)
13328 while (glyph < end
13329 && INTEGERP (glyph->object)
13330 && glyph->charpos < 0)
13332 x += glyph->pixel_width;
13333 ++glyph;
13335 while (end > glyph
13336 && INTEGERP ((end - 1)->object)
13337 /* CHARPOS is zero for blanks and stretch glyphs
13338 inserted by extend_face_to_end_of_line. */
13339 && (end - 1)->charpos <= 0)
13340 --end;
13341 glyph_before = glyph - 1;
13342 glyph_after = end;
13344 else
13346 struct glyph *g;
13348 /* If the glyph row is reversed, we need to process it from back
13349 to front, so swap the edge pointers. */
13350 glyphs_end = end = glyph - 1;
13351 glyph += row->used[TEXT_AREA] - 1;
13353 while (glyph > end + 1
13354 && INTEGERP (glyph->object)
13355 && glyph->charpos < 0)
13357 --glyph;
13358 x -= glyph->pixel_width;
13360 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13361 --glyph;
13362 /* By default, in reversed rows we put the cursor on the
13363 rightmost (first in the reading order) glyph. */
13364 for (g = end + 1; g < glyph; g++)
13365 x += g->pixel_width;
13366 while (end < glyph
13367 && INTEGERP ((end + 1)->object)
13368 && (end + 1)->charpos <= 0)
13369 ++end;
13370 glyph_before = glyph + 1;
13371 glyph_after = end;
13374 else if (row->reversed_p)
13376 /* In R2L rows that don't display text, put the cursor on the
13377 rightmost glyph. Case in point: an empty last line that is
13378 part of an R2L paragraph. */
13379 cursor = end - 1;
13380 /* Avoid placing the cursor on the last glyph of the row, where
13381 on terminal frames we hold the vertical border between
13382 adjacent windows. */
13383 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13384 && !WINDOW_RIGHTMOST_P (w)
13385 && cursor == row->glyphs[LAST_AREA] - 1)
13386 cursor--;
13387 x = -1; /* will be computed below, at label compute_x */
13390 /* Step 1: Try to find the glyph whose character position
13391 corresponds to point. If that's not possible, find 2 glyphs
13392 whose character positions are the closest to point, one before
13393 point, the other after it. */
13394 if (!row->reversed_p)
13395 while (/* not marched to end of glyph row */
13396 glyph < end
13397 /* glyph was not inserted by redisplay for internal purposes */
13398 && !INTEGERP (glyph->object))
13400 if (BUFFERP (glyph->object))
13402 EMACS_INT dpos = glyph->charpos - pt_old;
13404 if (glyph->charpos > bpos_max)
13405 bpos_max = glyph->charpos;
13406 if (glyph->charpos < bpos_min)
13407 bpos_min = glyph->charpos;
13408 if (!glyph->avoid_cursor_p)
13410 /* If we hit point, we've found the glyph on which to
13411 display the cursor. */
13412 if (dpos == 0)
13414 match_with_avoid_cursor = 0;
13415 break;
13417 /* See if we've found a better approximation to
13418 POS_BEFORE or to POS_AFTER. Note that we want the
13419 first (leftmost) glyph of all those that are the
13420 closest from below, and the last (rightmost) of all
13421 those from above. */
13422 if (0 > dpos && dpos > pos_before - pt_old)
13424 pos_before = glyph->charpos;
13425 glyph_before = glyph;
13427 else if (0 < dpos && dpos <= pos_after - pt_old)
13429 pos_after = glyph->charpos;
13430 glyph_after = glyph;
13433 else if (dpos == 0)
13434 match_with_avoid_cursor = 1;
13436 else if (STRINGP (glyph->object))
13438 Lisp_Object chprop;
13439 EMACS_INT glyph_pos = glyph->charpos;
13441 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13442 glyph->object);
13443 if (INTEGERP (chprop))
13445 bpos_covered = bpos_max + XINT (chprop);
13446 /* If the `cursor' property covers buffer positions up
13447 to and including point, we should display cursor on
13448 this glyph. Note that overlays and text properties
13449 with string values stop bidi reordering, so every
13450 buffer position to the left of the string is always
13451 smaller than any position to the right of the
13452 string. Therefore, if a `cursor' property on one
13453 of the string's characters has an integer value, we
13454 will break out of the loop below _before_ we get to
13455 the position match above. IOW, integer values of
13456 the `cursor' property override the "exact match for
13457 point" strategy of positioning the cursor. */
13458 /* Implementation note: bpos_max == pt_old when, e.g.,
13459 we are in an empty line, where bpos_max is set to
13460 MATRIX_ROW_START_CHARPOS, see above. */
13461 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13463 cursor = glyph;
13464 break;
13468 string_seen = 1;
13470 x += glyph->pixel_width;
13471 ++glyph;
13473 else if (glyph > end) /* row is reversed */
13474 while (!INTEGERP (glyph->object))
13476 if (BUFFERP (glyph->object))
13478 EMACS_INT dpos = glyph->charpos - pt_old;
13480 if (glyph->charpos > bpos_max)
13481 bpos_max = glyph->charpos;
13482 if (glyph->charpos < bpos_min)
13483 bpos_min = glyph->charpos;
13484 if (!glyph->avoid_cursor_p)
13486 if (dpos == 0)
13488 match_with_avoid_cursor = 0;
13489 break;
13491 if (0 > dpos && dpos > pos_before - pt_old)
13493 pos_before = glyph->charpos;
13494 glyph_before = glyph;
13496 else if (0 < dpos && dpos <= pos_after - pt_old)
13498 pos_after = glyph->charpos;
13499 glyph_after = glyph;
13502 else if (dpos == 0)
13503 match_with_avoid_cursor = 1;
13505 else if (STRINGP (glyph->object))
13507 Lisp_Object chprop;
13508 EMACS_INT glyph_pos = glyph->charpos;
13510 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13511 glyph->object);
13512 if (INTEGERP (chprop))
13514 bpos_covered = bpos_max + XINT (chprop);
13515 /* If the `cursor' property covers buffer positions up
13516 to and including point, we should display cursor on
13517 this glyph. */
13518 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13520 cursor = glyph;
13521 break;
13524 string_seen = 1;
13526 --glyph;
13527 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13529 x--; /* can't use any pixel_width */
13530 break;
13532 x -= glyph->pixel_width;
13535 /* Step 2: If we didn't find an exact match for point, we need to
13536 look for a proper place to put the cursor among glyphs between
13537 GLYPH_BEFORE and GLYPH_AFTER. */
13538 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13539 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13540 && bpos_covered < pt_old)
13542 /* An empty line has a single glyph whose OBJECT is zero and
13543 whose CHARPOS is the position of a newline on that line.
13544 Note that on a TTY, there are more glyphs after that, which
13545 were produced by extend_face_to_end_of_line, but their
13546 CHARPOS is zero or negative. */
13547 int empty_line_p =
13548 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13549 && INTEGERP (glyph->object) && glyph->charpos > 0;
13551 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13553 EMACS_INT ellipsis_pos;
13555 /* Scan back over the ellipsis glyphs. */
13556 if (!row->reversed_p)
13558 ellipsis_pos = (glyph - 1)->charpos;
13559 while (glyph > row->glyphs[TEXT_AREA]
13560 && (glyph - 1)->charpos == ellipsis_pos)
13561 glyph--, x -= glyph->pixel_width;
13562 /* That loop always goes one position too far, including
13563 the glyph before the ellipsis. So scan forward over
13564 that one. */
13565 x += glyph->pixel_width;
13566 glyph++;
13568 else /* row is reversed */
13570 ellipsis_pos = (glyph + 1)->charpos;
13571 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13572 && (glyph + 1)->charpos == ellipsis_pos)
13573 glyph++, x += glyph->pixel_width;
13574 x -= glyph->pixel_width;
13575 glyph--;
13578 else if (match_with_avoid_cursor
13579 /* A truncated row may not include PT among its
13580 character positions. Setting the cursor inside the
13581 scroll margin will trigger recalculation of hscroll
13582 in hscroll_window_tree. */
13583 || (row->truncated_on_left_p && pt_old < bpos_min)
13584 || (row->truncated_on_right_p && pt_old > bpos_max)
13585 /* Zero-width characters produce no glyphs. */
13586 || (!string_seen
13587 && !empty_line_p
13588 && (row->reversed_p
13589 ? glyph_after > glyphs_end
13590 : glyph_after < glyphs_end)))
13592 cursor = glyph_after;
13593 x = -1;
13595 else if (string_seen)
13597 int incr = row->reversed_p ? -1 : +1;
13599 /* Need to find the glyph that came out of a string which is
13600 present at point. That glyph is somewhere between
13601 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13602 positioned between POS_BEFORE and POS_AFTER in the
13603 buffer. */
13604 struct glyph *start, *stop;
13605 EMACS_INT pos = pos_before;
13607 x = -1;
13609 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13610 correspond to POS_BEFORE and POS_AFTER, respectively. We
13611 need START and STOP in the order that corresponds to the
13612 row's direction as given by its reversed_p flag. If the
13613 directionality of characters between POS_BEFORE and
13614 POS_AFTER is the opposite of the row's base direction,
13615 these characters will have been reordered for display,
13616 and we need to reverse START and STOP. */
13617 if (!row->reversed_p)
13619 start = min (glyph_before, glyph_after);
13620 stop = max (glyph_before, glyph_after);
13622 else
13624 start = max (glyph_before, glyph_after);
13625 stop = min (glyph_before, glyph_after);
13627 for (glyph = start + incr;
13628 row->reversed_p ? glyph > stop : glyph < stop; )
13631 /* Any glyphs that come from the buffer are here because
13632 of bidi reordering. Skip them, and only pay
13633 attention to glyphs that came from some string. */
13634 if (STRINGP (glyph->object))
13636 Lisp_Object str;
13637 EMACS_INT tem;
13638 /* If the display property covers the newline, we
13639 need to search for it one position farther. */
13640 EMACS_INT lim = pos_after
13641 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13643 string_from_text_prop = 0;
13644 str = glyph->object;
13645 tem = string_buffer_position_lim (str, pos, lim, 0);
13646 if (tem == 0 /* from overlay */
13647 || pos <= tem)
13649 /* If the string from which this glyph came is
13650 found in the buffer at point, then we've
13651 found the glyph we've been looking for. If
13652 it comes from an overlay (tem == 0), and it
13653 has the `cursor' property on one of its
13654 glyphs, record that glyph as a candidate for
13655 displaying the cursor. (As in the
13656 unidirectional version, we will display the
13657 cursor on the last candidate we find.) */
13658 if (tem == 0 || tem == pt_old)
13660 /* The glyphs from this string could have
13661 been reordered. Find the one with the
13662 smallest string position. Or there could
13663 be a character in the string with the
13664 `cursor' property, which means display
13665 cursor on that character's glyph. */
13666 EMACS_INT strpos = glyph->charpos;
13668 if (tem)
13670 cursor = glyph;
13671 string_from_text_prop = 1;
13673 for ( ;
13674 (row->reversed_p ? glyph > stop : glyph < stop)
13675 && EQ (glyph->object, str);
13676 glyph += incr)
13678 Lisp_Object cprop;
13679 EMACS_INT gpos = glyph->charpos;
13681 cprop = Fget_char_property (make_number (gpos),
13682 Qcursor,
13683 glyph->object);
13684 if (!NILP (cprop))
13686 cursor = glyph;
13687 break;
13689 if (tem && glyph->charpos < strpos)
13691 strpos = glyph->charpos;
13692 cursor = glyph;
13696 if (tem == pt_old)
13697 goto compute_x;
13699 if (tem)
13700 pos = tem + 1; /* don't find previous instances */
13702 /* This string is not what we want; skip all of the
13703 glyphs that came from it. */
13704 while ((row->reversed_p ? glyph > stop : glyph < stop)
13705 && EQ (glyph->object, str))
13706 glyph += incr;
13708 else
13709 glyph += incr;
13712 /* If we reached the end of the line, and END was from a string,
13713 the cursor is not on this line. */
13714 if (cursor == NULL
13715 && (row->reversed_p ? glyph <= end : glyph >= end)
13716 && STRINGP (end->object)
13717 && row->continued_p)
13718 return 0;
13722 compute_x:
13723 if (cursor != NULL)
13724 glyph = cursor;
13725 if (x < 0)
13727 struct glyph *g;
13729 /* Need to compute x that corresponds to GLYPH. */
13730 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13732 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13733 abort ();
13734 x += g->pixel_width;
13738 /* ROW could be part of a continued line, which, under bidi
13739 reordering, might have other rows whose start and end charpos
13740 occlude point. Only set w->cursor if we found a better
13741 approximation to the cursor position than we have from previously
13742 examined candidate rows belonging to the same continued line. */
13743 if (/* we already have a candidate row */
13744 w->cursor.vpos >= 0
13745 /* that candidate is not the row we are processing */
13746 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13747 /* Make sure cursor.vpos specifies a row whose start and end
13748 charpos occlude point. This is because some callers of this
13749 function leave cursor.vpos at the row where the cursor was
13750 displayed during the last redisplay cycle. */
13751 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13752 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13754 struct glyph *g1 =
13755 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13757 /* Don't consider glyphs that are outside TEXT_AREA. */
13758 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13759 return 0;
13760 /* Keep the candidate whose buffer position is the closest to
13761 point or has the `cursor' property. */
13762 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13763 w->cursor.hpos >= 0
13764 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13765 && ((BUFFERP (g1->object)
13766 && (g1->charpos == pt_old /* an exact match always wins */
13767 || (BUFFERP (glyph->object)
13768 && eabs (g1->charpos - pt_old)
13769 < eabs (glyph->charpos - pt_old))))
13770 /* previous candidate is a glyph from a string that has
13771 a non-nil `cursor' property */
13772 || (STRINGP (g1->object)
13773 && (!NILP (Fget_char_property (make_number (g1->charpos),
13774 Qcursor, g1->object))
13775 /* pevious candidate is from the same display
13776 string as this one, and the display string
13777 came from a text property */
13778 || (EQ (g1->object, glyph->object)
13779 && string_from_text_prop)
13780 /* this candidate is from newline and its
13781 position is not an exact match */
13782 || (INTEGERP (glyph->object)
13783 && glyph->charpos != pt_old)))))
13784 return 0;
13785 /* If this candidate gives an exact match, use that. */
13786 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13787 /* Otherwise, keep the candidate that comes from a row
13788 spanning less buffer positions. This may win when one or
13789 both candidate positions are on glyphs that came from
13790 display strings, for which we cannot compare buffer
13791 positions. */
13792 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13793 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13794 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13795 return 0;
13797 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13798 w->cursor.x = x;
13799 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13800 w->cursor.y = row->y + dy;
13802 if (w == XWINDOW (selected_window))
13804 if (!row->continued_p
13805 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13806 && row->x == 0)
13808 this_line_buffer = XBUFFER (w->buffer);
13810 CHARPOS (this_line_start_pos)
13811 = MATRIX_ROW_START_CHARPOS (row) + delta;
13812 BYTEPOS (this_line_start_pos)
13813 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13815 CHARPOS (this_line_end_pos)
13816 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13817 BYTEPOS (this_line_end_pos)
13818 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13820 this_line_y = w->cursor.y;
13821 this_line_pixel_height = row->height;
13822 this_line_vpos = w->cursor.vpos;
13823 this_line_start_x = row->x;
13825 else
13826 CHARPOS (this_line_start_pos) = 0;
13829 return 1;
13833 /* Run window scroll functions, if any, for WINDOW with new window
13834 start STARTP. Sets the window start of WINDOW to that position.
13836 We assume that the window's buffer is really current. */
13838 static inline struct text_pos
13839 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13841 struct window *w = XWINDOW (window);
13842 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13844 if (current_buffer != XBUFFER (w->buffer))
13845 abort ();
13847 if (!NILP (Vwindow_scroll_functions))
13849 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13850 make_number (CHARPOS (startp)));
13851 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13852 /* In case the hook functions switch buffers. */
13853 if (current_buffer != XBUFFER (w->buffer))
13854 set_buffer_internal_1 (XBUFFER (w->buffer));
13857 return startp;
13861 /* Make sure the line containing the cursor is fully visible.
13862 A value of 1 means there is nothing to be done.
13863 (Either the line is fully visible, or it cannot be made so,
13864 or we cannot tell.)
13866 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13867 is higher than window.
13869 A value of 0 means the caller should do scrolling
13870 as if point had gone off the screen. */
13872 static int
13873 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13875 struct glyph_matrix *matrix;
13876 struct glyph_row *row;
13877 int window_height;
13879 if (!make_cursor_line_fully_visible_p)
13880 return 1;
13882 /* It's not always possible to find the cursor, e.g, when a window
13883 is full of overlay strings. Don't do anything in that case. */
13884 if (w->cursor.vpos < 0)
13885 return 1;
13887 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13888 row = MATRIX_ROW (matrix, w->cursor.vpos);
13890 /* If the cursor row is not partially visible, there's nothing to do. */
13891 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13892 return 1;
13894 /* If the row the cursor is in is taller than the window's height,
13895 it's not clear what to do, so do nothing. */
13896 window_height = window_box_height (w);
13897 if (row->height >= window_height)
13899 if (!force_p || MINI_WINDOW_P (w)
13900 || w->vscroll || w->cursor.vpos == 0)
13901 return 1;
13903 return 0;
13907 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13908 non-zero means only WINDOW is redisplayed in redisplay_internal.
13909 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13910 in redisplay_window to bring a partially visible line into view in
13911 the case that only the cursor has moved.
13913 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13914 last screen line's vertical height extends past the end of the screen.
13916 Value is
13918 1 if scrolling succeeded
13920 0 if scrolling didn't find point.
13922 -1 if new fonts have been loaded so that we must interrupt
13923 redisplay, adjust glyph matrices, and try again. */
13925 enum
13927 SCROLLING_SUCCESS,
13928 SCROLLING_FAILED,
13929 SCROLLING_NEED_LARGER_MATRICES
13932 /* If scroll-conservatively is more than this, never recenter.
13934 If you change this, don't forget to update the doc string of
13935 `scroll-conservatively' and the Emacs manual. */
13936 #define SCROLL_LIMIT 100
13938 static int
13939 try_scrolling (Lisp_Object window, int just_this_one_p,
13940 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13941 int temp_scroll_step, int last_line_misfit)
13943 struct window *w = XWINDOW (window);
13944 struct frame *f = XFRAME (w->frame);
13945 struct text_pos pos, startp;
13946 struct it it;
13947 int this_scroll_margin, scroll_max, rc, height;
13948 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13949 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13950 Lisp_Object aggressive;
13951 /* We will never try scrolling more than this number of lines. */
13952 int scroll_limit = SCROLL_LIMIT;
13954 #if GLYPH_DEBUG
13955 debug_method_add (w, "try_scrolling");
13956 #endif
13958 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13960 /* Compute scroll margin height in pixels. We scroll when point is
13961 within this distance from the top or bottom of the window. */
13962 if (scroll_margin > 0)
13963 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13964 * FRAME_LINE_HEIGHT (f);
13965 else
13966 this_scroll_margin = 0;
13968 /* Force arg_scroll_conservatively to have a reasonable value, to
13969 avoid scrolling too far away with slow move_it_* functions. Note
13970 that the user can supply scroll-conservatively equal to
13971 `most-positive-fixnum', which can be larger than INT_MAX. */
13972 if (arg_scroll_conservatively > scroll_limit)
13974 arg_scroll_conservatively = scroll_limit + 1;
13975 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13977 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13978 /* Compute how much we should try to scroll maximally to bring
13979 point into view. */
13980 scroll_max = (max (scroll_step,
13981 max (arg_scroll_conservatively, temp_scroll_step))
13982 * FRAME_LINE_HEIGHT (f));
13983 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13984 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13985 /* We're trying to scroll because of aggressive scrolling but no
13986 scroll_step is set. Choose an arbitrary one. */
13987 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13988 else
13989 scroll_max = 0;
13991 too_near_end:
13993 /* Decide whether to scroll down. */
13994 if (PT > CHARPOS (startp))
13996 int scroll_margin_y;
13998 /* Compute the pixel ypos of the scroll margin, then move it to
13999 either that ypos or PT, whichever comes first. */
14000 start_display (&it, w, startp);
14001 scroll_margin_y = it.last_visible_y - this_scroll_margin
14002 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14003 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14004 (MOVE_TO_POS | MOVE_TO_Y));
14006 if (PT > CHARPOS (it.current.pos))
14008 int y0 = line_bottom_y (&it);
14009 /* Compute how many pixels below window bottom to stop searching
14010 for PT. This avoids costly search for PT that is far away if
14011 the user limited scrolling by a small number of lines, but
14012 always finds PT if scroll_conservatively is set to a large
14013 number, such as most-positive-fixnum. */
14014 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14015 int y_to_move = it.last_visible_y + slack;
14017 /* Compute the distance from the scroll margin to PT or to
14018 the scroll limit, whichever comes first. This should
14019 include the height of the cursor line, to make that line
14020 fully visible. */
14021 move_it_to (&it, PT, -1, y_to_move,
14022 -1, MOVE_TO_POS | MOVE_TO_Y);
14023 dy = line_bottom_y (&it) - y0;
14025 if (dy > scroll_max)
14026 return SCROLLING_FAILED;
14028 scroll_down_p = 1;
14032 if (scroll_down_p)
14034 /* Point is in or below the bottom scroll margin, so move the
14035 window start down. If scrolling conservatively, move it just
14036 enough down to make point visible. If scroll_step is set,
14037 move it down by scroll_step. */
14038 if (arg_scroll_conservatively)
14039 amount_to_scroll
14040 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14041 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14042 else if (scroll_step || temp_scroll_step)
14043 amount_to_scroll = scroll_max;
14044 else
14046 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14047 height = WINDOW_BOX_TEXT_HEIGHT (w);
14048 if (NUMBERP (aggressive))
14050 double float_amount = XFLOATINT (aggressive) * height;
14051 amount_to_scroll = float_amount;
14052 if (amount_to_scroll == 0 && float_amount > 0)
14053 amount_to_scroll = 1;
14054 /* Don't let point enter the scroll margin near top of
14055 the window. */
14056 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14057 amount_to_scroll = height - 2*this_scroll_margin + dy;
14061 if (amount_to_scroll <= 0)
14062 return SCROLLING_FAILED;
14064 start_display (&it, w, startp);
14065 if (arg_scroll_conservatively <= scroll_limit)
14066 move_it_vertically (&it, amount_to_scroll);
14067 else
14069 /* Extra precision for users who set scroll-conservatively
14070 to a large number: make sure the amount we scroll
14071 the window start is never less than amount_to_scroll,
14072 which was computed as distance from window bottom to
14073 point. This matters when lines at window top and lines
14074 below window bottom have different height. */
14075 struct it it1;
14076 void *it1data = NULL;
14077 /* We use a temporary it1 because line_bottom_y can modify
14078 its argument, if it moves one line down; see there. */
14079 int start_y;
14081 SAVE_IT (it1, it, it1data);
14082 start_y = line_bottom_y (&it1);
14083 do {
14084 RESTORE_IT (&it, &it, it1data);
14085 move_it_by_lines (&it, 1);
14086 SAVE_IT (it1, it, it1data);
14087 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14090 /* If STARTP is unchanged, move it down another screen line. */
14091 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14092 move_it_by_lines (&it, 1);
14093 startp = it.current.pos;
14095 else
14097 struct text_pos scroll_margin_pos = startp;
14099 /* See if point is inside the scroll margin at the top of the
14100 window. */
14101 if (this_scroll_margin)
14103 start_display (&it, w, startp);
14104 move_it_vertically (&it, this_scroll_margin);
14105 scroll_margin_pos = it.current.pos;
14108 if (PT < CHARPOS (scroll_margin_pos))
14110 /* Point is in the scroll margin at the top of the window or
14111 above what is displayed in the window. */
14112 int y0, y_to_move;
14114 /* Compute the vertical distance from PT to the scroll
14115 margin position. Move as far as scroll_max allows, or
14116 one screenful, or 10 screen lines, whichever is largest.
14117 Give up if distance is greater than scroll_max. */
14118 SET_TEXT_POS (pos, PT, PT_BYTE);
14119 start_display (&it, w, pos);
14120 y0 = it.current_y;
14121 y_to_move = max (it.last_visible_y,
14122 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14123 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14124 y_to_move, -1,
14125 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14126 dy = it.current_y - y0;
14127 if (dy > scroll_max)
14128 return SCROLLING_FAILED;
14130 /* Compute new window start. */
14131 start_display (&it, w, startp);
14133 if (arg_scroll_conservatively)
14134 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14135 max (scroll_step, temp_scroll_step));
14136 else if (scroll_step || temp_scroll_step)
14137 amount_to_scroll = scroll_max;
14138 else
14140 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14141 height = WINDOW_BOX_TEXT_HEIGHT (w);
14142 if (NUMBERP (aggressive))
14144 double float_amount = XFLOATINT (aggressive) * height;
14145 amount_to_scroll = float_amount;
14146 if (amount_to_scroll == 0 && float_amount > 0)
14147 amount_to_scroll = 1;
14148 amount_to_scroll -=
14149 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14150 /* Don't let point enter the scroll margin near
14151 bottom of the window. */
14152 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14153 amount_to_scroll = height - 2*this_scroll_margin + dy;
14157 if (amount_to_scroll <= 0)
14158 return SCROLLING_FAILED;
14160 move_it_vertically_backward (&it, amount_to_scroll);
14161 startp = it.current.pos;
14165 /* Run window scroll functions. */
14166 startp = run_window_scroll_functions (window, startp);
14168 /* Display the window. Give up if new fonts are loaded, or if point
14169 doesn't appear. */
14170 if (!try_window (window, startp, 0))
14171 rc = SCROLLING_NEED_LARGER_MATRICES;
14172 else if (w->cursor.vpos < 0)
14174 clear_glyph_matrix (w->desired_matrix);
14175 rc = SCROLLING_FAILED;
14177 else
14179 /* Maybe forget recorded base line for line number display. */
14180 if (!just_this_one_p
14181 || current_buffer->clip_changed
14182 || BEG_UNCHANGED < CHARPOS (startp))
14183 w->base_line_number = Qnil;
14185 /* If cursor ends up on a partially visible line,
14186 treat that as being off the bottom of the screen. */
14187 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14188 /* It's possible that the cursor is on the first line of the
14189 buffer, which is partially obscured due to a vscroll
14190 (Bug#7537). In that case, avoid looping forever . */
14191 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14193 clear_glyph_matrix (w->desired_matrix);
14194 ++extra_scroll_margin_lines;
14195 goto too_near_end;
14197 rc = SCROLLING_SUCCESS;
14200 return rc;
14204 /* Compute a suitable window start for window W if display of W starts
14205 on a continuation line. Value is non-zero if a new window start
14206 was computed.
14208 The new window start will be computed, based on W's width, starting
14209 from the start of the continued line. It is the start of the
14210 screen line with the minimum distance from the old start W->start. */
14212 static int
14213 compute_window_start_on_continuation_line (struct window *w)
14215 struct text_pos pos, start_pos;
14216 int window_start_changed_p = 0;
14218 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14220 /* If window start is on a continuation line... Window start may be
14221 < BEGV in case there's invisible text at the start of the
14222 buffer (M-x rmail, for example). */
14223 if (CHARPOS (start_pos) > BEGV
14224 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14226 struct it it;
14227 struct glyph_row *row;
14229 /* Handle the case that the window start is out of range. */
14230 if (CHARPOS (start_pos) < BEGV)
14231 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14232 else if (CHARPOS (start_pos) > ZV)
14233 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14235 /* Find the start of the continued line. This should be fast
14236 because scan_buffer is fast (newline cache). */
14237 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14238 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14239 row, DEFAULT_FACE_ID);
14240 reseat_at_previous_visible_line_start (&it);
14242 /* If the line start is "too far" away from the window start,
14243 say it takes too much time to compute a new window start. */
14244 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14245 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14247 int min_distance, distance;
14249 /* Move forward by display lines to find the new window
14250 start. If window width was enlarged, the new start can
14251 be expected to be > the old start. If window width was
14252 decreased, the new window start will be < the old start.
14253 So, we're looking for the display line start with the
14254 minimum distance from the old window start. */
14255 pos = it.current.pos;
14256 min_distance = INFINITY;
14257 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14258 distance < min_distance)
14260 min_distance = distance;
14261 pos = it.current.pos;
14262 move_it_by_lines (&it, 1);
14265 /* Set the window start there. */
14266 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14267 window_start_changed_p = 1;
14271 return window_start_changed_p;
14275 /* Try cursor movement in case text has not changed in window WINDOW,
14276 with window start STARTP. Value is
14278 CURSOR_MOVEMENT_SUCCESS if successful
14280 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14282 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14283 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14284 we want to scroll as if scroll-step were set to 1. See the code.
14286 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14287 which case we have to abort this redisplay, and adjust matrices
14288 first. */
14290 enum
14292 CURSOR_MOVEMENT_SUCCESS,
14293 CURSOR_MOVEMENT_CANNOT_BE_USED,
14294 CURSOR_MOVEMENT_MUST_SCROLL,
14295 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14298 static int
14299 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14301 struct window *w = XWINDOW (window);
14302 struct frame *f = XFRAME (w->frame);
14303 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14305 #if GLYPH_DEBUG
14306 if (inhibit_try_cursor_movement)
14307 return rc;
14308 #endif
14310 /* Handle case where text has not changed, only point, and it has
14311 not moved off the frame. */
14312 if (/* Point may be in this window. */
14313 PT >= CHARPOS (startp)
14314 /* Selective display hasn't changed. */
14315 && !current_buffer->clip_changed
14316 /* Function force-mode-line-update is used to force a thorough
14317 redisplay. It sets either windows_or_buffers_changed or
14318 update_mode_lines. So don't take a shortcut here for these
14319 cases. */
14320 && !update_mode_lines
14321 && !windows_or_buffers_changed
14322 && !cursor_type_changed
14323 /* Can't use this case if highlighting a region. When a
14324 region exists, cursor movement has to do more than just
14325 set the cursor. */
14326 && !(!NILP (Vtransient_mark_mode)
14327 && !NILP (BVAR (current_buffer, mark_active)))
14328 && NILP (w->region_showing)
14329 && NILP (Vshow_trailing_whitespace)
14330 /* Right after splitting windows, last_point may be nil. */
14331 && INTEGERP (w->last_point)
14332 /* This code is not used for mini-buffer for the sake of the case
14333 of redisplaying to replace an echo area message; since in
14334 that case the mini-buffer contents per se are usually
14335 unchanged. This code is of no real use in the mini-buffer
14336 since the handling of this_line_start_pos, etc., in redisplay
14337 handles the same cases. */
14338 && !EQ (window, minibuf_window)
14339 /* When splitting windows or for new windows, it happens that
14340 redisplay is called with a nil window_end_vpos or one being
14341 larger than the window. This should really be fixed in
14342 window.c. I don't have this on my list, now, so we do
14343 approximately the same as the old redisplay code. --gerd. */
14344 && INTEGERP (w->window_end_vpos)
14345 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14346 && (FRAME_WINDOW_P (f)
14347 || !overlay_arrow_in_current_buffer_p ()))
14349 int this_scroll_margin, top_scroll_margin;
14350 struct glyph_row *row = NULL;
14352 #if GLYPH_DEBUG
14353 debug_method_add (w, "cursor movement");
14354 #endif
14356 /* Scroll if point within this distance from the top or bottom
14357 of the window. This is a pixel value. */
14358 if (scroll_margin > 0)
14360 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14361 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14363 else
14364 this_scroll_margin = 0;
14366 top_scroll_margin = this_scroll_margin;
14367 if (WINDOW_WANTS_HEADER_LINE_P (w))
14368 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14370 /* Start with the row the cursor was displayed during the last
14371 not paused redisplay. Give up if that row is not valid. */
14372 if (w->last_cursor.vpos < 0
14373 || w->last_cursor.vpos >= w->current_matrix->nrows)
14374 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14375 else
14377 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14378 if (row->mode_line_p)
14379 ++row;
14380 if (!row->enabled_p)
14381 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14384 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14386 int scroll_p = 0, must_scroll = 0;
14387 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14389 if (PT > XFASTINT (w->last_point))
14391 /* Point has moved forward. */
14392 while (MATRIX_ROW_END_CHARPOS (row) < PT
14393 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14395 xassert (row->enabled_p);
14396 ++row;
14399 /* If the end position of a row equals the start
14400 position of the next row, and PT is at that position,
14401 we would rather display cursor in the next line. */
14402 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14403 && MATRIX_ROW_END_CHARPOS (row) == PT
14404 && row < w->current_matrix->rows
14405 + w->current_matrix->nrows - 1
14406 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14407 && !cursor_row_p (row))
14408 ++row;
14410 /* If within the scroll margin, scroll. Note that
14411 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14412 the next line would be drawn, and that
14413 this_scroll_margin can be zero. */
14414 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14415 || PT > MATRIX_ROW_END_CHARPOS (row)
14416 /* Line is completely visible last line in window
14417 and PT is to be set in the next line. */
14418 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14419 && PT == MATRIX_ROW_END_CHARPOS (row)
14420 && !row->ends_at_zv_p
14421 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14422 scroll_p = 1;
14424 else if (PT < XFASTINT (w->last_point))
14426 /* Cursor has to be moved backward. Note that PT >=
14427 CHARPOS (startp) because of the outer if-statement. */
14428 while (!row->mode_line_p
14429 && (MATRIX_ROW_START_CHARPOS (row) > PT
14430 || (MATRIX_ROW_START_CHARPOS (row) == PT
14431 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14432 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14433 row > w->current_matrix->rows
14434 && (row-1)->ends_in_newline_from_string_p))))
14435 && (row->y > top_scroll_margin
14436 || CHARPOS (startp) == BEGV))
14438 xassert (row->enabled_p);
14439 --row;
14442 /* Consider the following case: Window starts at BEGV,
14443 there is invisible, intangible text at BEGV, so that
14444 display starts at some point START > BEGV. It can
14445 happen that we are called with PT somewhere between
14446 BEGV and START. Try to handle that case. */
14447 if (row < w->current_matrix->rows
14448 || row->mode_line_p)
14450 row = w->current_matrix->rows;
14451 if (row->mode_line_p)
14452 ++row;
14455 /* Due to newlines in overlay strings, we may have to
14456 skip forward over overlay strings. */
14457 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14458 && MATRIX_ROW_END_CHARPOS (row) == PT
14459 && !cursor_row_p (row))
14460 ++row;
14462 /* If within the scroll margin, scroll. */
14463 if (row->y < top_scroll_margin
14464 && CHARPOS (startp) != BEGV)
14465 scroll_p = 1;
14467 else
14469 /* Cursor did not move. So don't scroll even if cursor line
14470 is partially visible, as it was so before. */
14471 rc = CURSOR_MOVEMENT_SUCCESS;
14474 if (PT < MATRIX_ROW_START_CHARPOS (row)
14475 || PT > MATRIX_ROW_END_CHARPOS (row))
14477 /* if PT is not in the glyph row, give up. */
14478 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14479 must_scroll = 1;
14481 else if (rc != CURSOR_MOVEMENT_SUCCESS
14482 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14484 /* If rows are bidi-reordered and point moved, back up
14485 until we find a row that does not belong to a
14486 continuation line. This is because we must consider
14487 all rows of a continued line as candidates for the
14488 new cursor positioning, since row start and end
14489 positions change non-linearly with vertical position
14490 in such rows. */
14491 /* FIXME: Revisit this when glyph ``spilling'' in
14492 continuation lines' rows is implemented for
14493 bidi-reordered rows. */
14494 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14496 xassert (row->enabled_p);
14497 --row;
14498 /* If we hit the beginning of the displayed portion
14499 without finding the first row of a continued
14500 line, give up. */
14501 if (row <= w->current_matrix->rows)
14503 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14504 break;
14509 if (must_scroll)
14511 else if (rc != CURSOR_MOVEMENT_SUCCESS
14512 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14513 && make_cursor_line_fully_visible_p)
14515 if (PT == MATRIX_ROW_END_CHARPOS (row)
14516 && !row->ends_at_zv_p
14517 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14518 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14519 else if (row->height > window_box_height (w))
14521 /* If we end up in a partially visible line, let's
14522 make it fully visible, except when it's taller
14523 than the window, in which case we can't do much
14524 about it. */
14525 *scroll_step = 1;
14526 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14528 else
14530 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14531 if (!cursor_row_fully_visible_p (w, 0, 1))
14532 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14533 else
14534 rc = CURSOR_MOVEMENT_SUCCESS;
14537 else if (scroll_p)
14538 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14539 else if (rc != CURSOR_MOVEMENT_SUCCESS
14540 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14542 /* With bidi-reordered rows, there could be more than
14543 one candidate row whose start and end positions
14544 occlude point. We need to let set_cursor_from_row
14545 find the best candidate. */
14546 /* FIXME: Revisit this when glyph ``spilling'' in
14547 continuation lines' rows is implemented for
14548 bidi-reordered rows. */
14549 int rv = 0;
14553 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14554 && PT <= MATRIX_ROW_END_CHARPOS (row)
14555 && cursor_row_p (row))
14556 rv |= set_cursor_from_row (w, row, w->current_matrix,
14557 0, 0, 0, 0);
14558 /* As soon as we've found the first suitable row
14559 whose ends_at_zv_p flag is set, we are done. */
14560 if (rv
14561 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14563 rc = CURSOR_MOVEMENT_SUCCESS;
14564 break;
14566 ++row;
14568 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14569 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14570 || (MATRIX_ROW_START_CHARPOS (row) == PT
14571 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14572 /* If we didn't find any candidate rows, or exited the
14573 loop before all the candidates were examined, signal
14574 to the caller that this method failed. */
14575 if (rc != CURSOR_MOVEMENT_SUCCESS
14576 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14577 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14578 else if (rv)
14579 rc = CURSOR_MOVEMENT_SUCCESS;
14581 else
14585 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14587 rc = CURSOR_MOVEMENT_SUCCESS;
14588 break;
14590 ++row;
14592 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14593 && MATRIX_ROW_START_CHARPOS (row) == PT
14594 && cursor_row_p (row));
14599 return rc;
14602 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14603 static
14604 #endif
14605 void
14606 set_vertical_scroll_bar (struct window *w)
14608 EMACS_INT start, end, whole;
14610 /* Calculate the start and end positions for the current window.
14611 At some point, it would be nice to choose between scrollbars
14612 which reflect the whole buffer size, with special markers
14613 indicating narrowing, and scrollbars which reflect only the
14614 visible region.
14616 Note that mini-buffers sometimes aren't displaying any text. */
14617 if (!MINI_WINDOW_P (w)
14618 || (w == XWINDOW (minibuf_window)
14619 && NILP (echo_area_buffer[0])))
14621 struct buffer *buf = XBUFFER (w->buffer);
14622 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14623 start = marker_position (w->start) - BUF_BEGV (buf);
14624 /* I don't think this is guaranteed to be right. For the
14625 moment, we'll pretend it is. */
14626 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14628 if (end < start)
14629 end = start;
14630 if (whole < (end - start))
14631 whole = end - start;
14633 else
14634 start = end = whole = 0;
14636 /* Indicate what this scroll bar ought to be displaying now. */
14637 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14638 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14639 (w, end - start, whole, start);
14643 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14644 selected_window is redisplayed.
14646 We can return without actually redisplaying the window if
14647 fonts_changed_p is nonzero. In that case, redisplay_internal will
14648 retry. */
14650 static void
14651 redisplay_window (Lisp_Object window, int just_this_one_p)
14653 struct window *w = XWINDOW (window);
14654 struct frame *f = XFRAME (w->frame);
14655 struct buffer *buffer = XBUFFER (w->buffer);
14656 struct buffer *old = current_buffer;
14657 struct text_pos lpoint, opoint, startp;
14658 int update_mode_line;
14659 int tem;
14660 struct it it;
14661 /* Record it now because it's overwritten. */
14662 int current_matrix_up_to_date_p = 0;
14663 int used_current_matrix_p = 0;
14664 /* This is less strict than current_matrix_up_to_date_p.
14665 It indictes that the buffer contents and narrowing are unchanged. */
14666 int buffer_unchanged_p = 0;
14667 int temp_scroll_step = 0;
14668 int count = SPECPDL_INDEX ();
14669 int rc;
14670 int centering_position = -1;
14671 int last_line_misfit = 0;
14672 EMACS_INT beg_unchanged, end_unchanged;
14674 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14675 opoint = lpoint;
14677 /* W must be a leaf window here. */
14678 xassert (!NILP (w->buffer));
14679 #if GLYPH_DEBUG
14680 *w->desired_matrix->method = 0;
14681 #endif
14683 restart:
14684 reconsider_clip_changes (w, buffer);
14686 /* Has the mode line to be updated? */
14687 update_mode_line = (!NILP (w->update_mode_line)
14688 || update_mode_lines
14689 || buffer->clip_changed
14690 || buffer->prevent_redisplay_optimizations_p);
14692 if (MINI_WINDOW_P (w))
14694 if (w == XWINDOW (echo_area_window)
14695 && !NILP (echo_area_buffer[0]))
14697 if (update_mode_line)
14698 /* We may have to update a tty frame's menu bar or a
14699 tool-bar. Example `M-x C-h C-h C-g'. */
14700 goto finish_menu_bars;
14701 else
14702 /* We've already displayed the echo area glyphs in this window. */
14703 goto finish_scroll_bars;
14705 else if ((w != XWINDOW (minibuf_window)
14706 || minibuf_level == 0)
14707 /* When buffer is nonempty, redisplay window normally. */
14708 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14709 /* Quail displays non-mini buffers in minibuffer window.
14710 In that case, redisplay the window normally. */
14711 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14713 /* W is a mini-buffer window, but it's not active, so clear
14714 it. */
14715 int yb = window_text_bottom_y (w);
14716 struct glyph_row *row;
14717 int y;
14719 for (y = 0, row = w->desired_matrix->rows;
14720 y < yb;
14721 y += row->height, ++row)
14722 blank_row (w, row, y);
14723 goto finish_scroll_bars;
14726 clear_glyph_matrix (w->desired_matrix);
14729 /* Otherwise set up data on this window; select its buffer and point
14730 value. */
14731 /* Really select the buffer, for the sake of buffer-local
14732 variables. */
14733 set_buffer_internal_1 (XBUFFER (w->buffer));
14735 current_matrix_up_to_date_p
14736 = (!NILP (w->window_end_valid)
14737 && !current_buffer->clip_changed
14738 && !current_buffer->prevent_redisplay_optimizations_p
14739 && XFASTINT (w->last_modified) >= MODIFF
14740 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14742 /* Run the window-bottom-change-functions
14743 if it is possible that the text on the screen has changed
14744 (either due to modification of the text, or any other reason). */
14745 if (!current_matrix_up_to_date_p
14746 && !NILP (Vwindow_text_change_functions))
14748 safe_run_hooks (Qwindow_text_change_functions);
14749 goto restart;
14752 beg_unchanged = BEG_UNCHANGED;
14753 end_unchanged = END_UNCHANGED;
14755 SET_TEXT_POS (opoint, PT, PT_BYTE);
14757 specbind (Qinhibit_point_motion_hooks, Qt);
14759 buffer_unchanged_p
14760 = (!NILP (w->window_end_valid)
14761 && !current_buffer->clip_changed
14762 && XFASTINT (w->last_modified) >= MODIFF
14763 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14765 /* When windows_or_buffers_changed is non-zero, we can't rely on
14766 the window end being valid, so set it to nil there. */
14767 if (windows_or_buffers_changed)
14769 /* If window starts on a continuation line, maybe adjust the
14770 window start in case the window's width changed. */
14771 if (XMARKER (w->start)->buffer == current_buffer)
14772 compute_window_start_on_continuation_line (w);
14774 w->window_end_valid = Qnil;
14777 /* Some sanity checks. */
14778 CHECK_WINDOW_END (w);
14779 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14780 abort ();
14781 if (BYTEPOS (opoint) < CHARPOS (opoint))
14782 abort ();
14784 /* If %c is in mode line, update it if needed. */
14785 if (!NILP (w->column_number_displayed)
14786 /* This alternative quickly identifies a common case
14787 where no change is needed. */
14788 && !(PT == XFASTINT (w->last_point)
14789 && XFASTINT (w->last_modified) >= MODIFF
14790 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14791 && (XFASTINT (w->column_number_displayed) != current_column ()))
14792 update_mode_line = 1;
14794 /* Count number of windows showing the selected buffer. An indirect
14795 buffer counts as its base buffer. */
14796 if (!just_this_one_p)
14798 struct buffer *current_base, *window_base;
14799 current_base = current_buffer;
14800 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14801 if (current_base->base_buffer)
14802 current_base = current_base->base_buffer;
14803 if (window_base->base_buffer)
14804 window_base = window_base->base_buffer;
14805 if (current_base == window_base)
14806 buffer_shared++;
14809 /* Point refers normally to the selected window. For any other
14810 window, set up appropriate value. */
14811 if (!EQ (window, selected_window))
14813 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14814 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14815 if (new_pt < BEGV)
14817 new_pt = BEGV;
14818 new_pt_byte = BEGV_BYTE;
14819 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14821 else if (new_pt > (ZV - 1))
14823 new_pt = ZV;
14824 new_pt_byte = ZV_BYTE;
14825 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14828 /* We don't use SET_PT so that the point-motion hooks don't run. */
14829 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14832 /* If any of the character widths specified in the display table
14833 have changed, invalidate the width run cache. It's true that
14834 this may be a bit late to catch such changes, but the rest of
14835 redisplay goes (non-fatally) haywire when the display table is
14836 changed, so why should we worry about doing any better? */
14837 if (current_buffer->width_run_cache)
14839 struct Lisp_Char_Table *disptab = buffer_display_table ();
14841 if (! disptab_matches_widthtab (disptab,
14842 XVECTOR (BVAR (current_buffer, width_table))))
14844 invalidate_region_cache (current_buffer,
14845 current_buffer->width_run_cache,
14846 BEG, Z);
14847 recompute_width_table (current_buffer, disptab);
14851 /* If window-start is screwed up, choose a new one. */
14852 if (XMARKER (w->start)->buffer != current_buffer)
14853 goto recenter;
14855 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14857 /* If someone specified a new starting point but did not insist,
14858 check whether it can be used. */
14859 if (!NILP (w->optional_new_start)
14860 && CHARPOS (startp) >= BEGV
14861 && CHARPOS (startp) <= ZV)
14863 w->optional_new_start = Qnil;
14864 start_display (&it, w, startp);
14865 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14866 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14867 if (IT_CHARPOS (it) == PT)
14868 w->force_start = Qt;
14869 /* IT may overshoot PT if text at PT is invisible. */
14870 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14871 w->force_start = Qt;
14874 force_start:
14876 /* Handle case where place to start displaying has been specified,
14877 unless the specified location is outside the accessible range. */
14878 if (!NILP (w->force_start)
14879 || w->frozen_window_start_p)
14881 /* We set this later on if we have to adjust point. */
14882 int new_vpos = -1;
14884 w->force_start = Qnil;
14885 w->vscroll = 0;
14886 w->window_end_valid = Qnil;
14888 /* Forget any recorded base line for line number display. */
14889 if (!buffer_unchanged_p)
14890 w->base_line_number = Qnil;
14892 /* Redisplay the mode line. Select the buffer properly for that.
14893 Also, run the hook window-scroll-functions
14894 because we have scrolled. */
14895 /* Note, we do this after clearing force_start because
14896 if there's an error, it is better to forget about force_start
14897 than to get into an infinite loop calling the hook functions
14898 and having them get more errors. */
14899 if (!update_mode_line
14900 || ! NILP (Vwindow_scroll_functions))
14902 update_mode_line = 1;
14903 w->update_mode_line = Qt;
14904 startp = run_window_scroll_functions (window, startp);
14907 w->last_modified = make_number (0);
14908 w->last_overlay_modified = make_number (0);
14909 if (CHARPOS (startp) < BEGV)
14910 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14911 else if (CHARPOS (startp) > ZV)
14912 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14914 /* Redisplay, then check if cursor has been set during the
14915 redisplay. Give up if new fonts were loaded. */
14916 /* We used to issue a CHECK_MARGINS argument to try_window here,
14917 but this causes scrolling to fail when point begins inside
14918 the scroll margin (bug#148) -- cyd */
14919 if (!try_window (window, startp, 0))
14921 w->force_start = Qt;
14922 clear_glyph_matrix (w->desired_matrix);
14923 goto need_larger_matrices;
14926 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14928 /* If point does not appear, try to move point so it does
14929 appear. The desired matrix has been built above, so we
14930 can use it here. */
14931 new_vpos = window_box_height (w) / 2;
14934 if (!cursor_row_fully_visible_p (w, 0, 0))
14936 /* Point does appear, but on a line partly visible at end of window.
14937 Move it back to a fully-visible line. */
14938 new_vpos = window_box_height (w);
14941 /* If we need to move point for either of the above reasons,
14942 now actually do it. */
14943 if (new_vpos >= 0)
14945 struct glyph_row *row;
14947 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14948 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14949 ++row;
14951 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14952 MATRIX_ROW_START_BYTEPOS (row));
14954 if (w != XWINDOW (selected_window))
14955 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14956 else if (current_buffer == old)
14957 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14959 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14961 /* If we are highlighting the region, then we just changed
14962 the region, so redisplay to show it. */
14963 if (!NILP (Vtransient_mark_mode)
14964 && !NILP (BVAR (current_buffer, mark_active)))
14966 clear_glyph_matrix (w->desired_matrix);
14967 if (!try_window (window, startp, 0))
14968 goto need_larger_matrices;
14972 #if GLYPH_DEBUG
14973 debug_method_add (w, "forced window start");
14974 #endif
14975 goto done;
14978 /* Handle case where text has not changed, only point, and it has
14979 not moved off the frame, and we are not retrying after hscroll.
14980 (current_matrix_up_to_date_p is nonzero when retrying.) */
14981 if (current_matrix_up_to_date_p
14982 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14983 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14985 switch (rc)
14987 case CURSOR_MOVEMENT_SUCCESS:
14988 used_current_matrix_p = 1;
14989 goto done;
14991 case CURSOR_MOVEMENT_MUST_SCROLL:
14992 goto try_to_scroll;
14994 default:
14995 abort ();
14998 /* If current starting point was originally the beginning of a line
14999 but no longer is, find a new starting point. */
15000 else if (!NILP (w->start_at_line_beg)
15001 && !(CHARPOS (startp) <= BEGV
15002 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15004 #if GLYPH_DEBUG
15005 debug_method_add (w, "recenter 1");
15006 #endif
15007 goto recenter;
15010 /* Try scrolling with try_window_id. Value is > 0 if update has
15011 been done, it is -1 if we know that the same window start will
15012 not work. It is 0 if unsuccessful for some other reason. */
15013 else if ((tem = try_window_id (w)) != 0)
15015 #if GLYPH_DEBUG
15016 debug_method_add (w, "try_window_id %d", tem);
15017 #endif
15019 if (fonts_changed_p)
15020 goto need_larger_matrices;
15021 if (tem > 0)
15022 goto done;
15024 /* Otherwise try_window_id has returned -1 which means that we
15025 don't want the alternative below this comment to execute. */
15027 else if (CHARPOS (startp) >= BEGV
15028 && CHARPOS (startp) <= ZV
15029 && PT >= CHARPOS (startp)
15030 && (CHARPOS (startp) < ZV
15031 /* Avoid starting at end of buffer. */
15032 || CHARPOS (startp) == BEGV
15033 || (XFASTINT (w->last_modified) >= MODIFF
15034 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15037 /* If first window line is a continuation line, and window start
15038 is inside the modified region, but the first change is before
15039 current window start, we must select a new window start.
15041 However, if this is the result of a down-mouse event (e.g. by
15042 extending the mouse-drag-overlay), we don't want to select a
15043 new window start, since that would change the position under
15044 the mouse, resulting in an unwanted mouse-movement rather
15045 than a simple mouse-click. */
15046 if (NILP (w->start_at_line_beg)
15047 && NILP (do_mouse_tracking)
15048 && CHARPOS (startp) > BEGV
15049 && CHARPOS (startp) > BEG + beg_unchanged
15050 && CHARPOS (startp) <= Z - end_unchanged
15051 /* Even if w->start_at_line_beg is nil, a new window may
15052 start at a line_beg, since that's how set_buffer_window
15053 sets it. So, we need to check the return value of
15054 compute_window_start_on_continuation_line. (See also
15055 bug#197). */
15056 && XMARKER (w->start)->buffer == current_buffer
15057 && compute_window_start_on_continuation_line (w))
15059 w->force_start = Qt;
15060 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15061 goto force_start;
15064 #if GLYPH_DEBUG
15065 debug_method_add (w, "same window start");
15066 #endif
15068 /* Try to redisplay starting at same place as before.
15069 If point has not moved off frame, accept the results. */
15070 if (!current_matrix_up_to_date_p
15071 /* Don't use try_window_reusing_current_matrix in this case
15072 because a window scroll function can have changed the
15073 buffer. */
15074 || !NILP (Vwindow_scroll_functions)
15075 || MINI_WINDOW_P (w)
15076 || !(used_current_matrix_p
15077 = try_window_reusing_current_matrix (w)))
15079 IF_DEBUG (debug_method_add (w, "1"));
15080 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15081 /* -1 means we need to scroll.
15082 0 means we need new matrices, but fonts_changed_p
15083 is set in that case, so we will detect it below. */
15084 goto try_to_scroll;
15087 if (fonts_changed_p)
15088 goto need_larger_matrices;
15090 if (w->cursor.vpos >= 0)
15092 if (!just_this_one_p
15093 || current_buffer->clip_changed
15094 || BEG_UNCHANGED < CHARPOS (startp))
15095 /* Forget any recorded base line for line number display. */
15096 w->base_line_number = Qnil;
15098 if (!cursor_row_fully_visible_p (w, 1, 0))
15100 clear_glyph_matrix (w->desired_matrix);
15101 last_line_misfit = 1;
15103 /* Drop through and scroll. */
15104 else
15105 goto done;
15107 else
15108 clear_glyph_matrix (w->desired_matrix);
15111 try_to_scroll:
15113 w->last_modified = make_number (0);
15114 w->last_overlay_modified = make_number (0);
15116 /* Redisplay the mode line. Select the buffer properly for that. */
15117 if (!update_mode_line)
15119 update_mode_line = 1;
15120 w->update_mode_line = Qt;
15123 /* Try to scroll by specified few lines. */
15124 if ((scroll_conservatively
15125 || emacs_scroll_step
15126 || temp_scroll_step
15127 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15128 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15129 && CHARPOS (startp) >= BEGV
15130 && CHARPOS (startp) <= ZV)
15132 /* The function returns -1 if new fonts were loaded, 1 if
15133 successful, 0 if not successful. */
15134 int ss = try_scrolling (window, just_this_one_p,
15135 scroll_conservatively,
15136 emacs_scroll_step,
15137 temp_scroll_step, last_line_misfit);
15138 switch (ss)
15140 case SCROLLING_SUCCESS:
15141 goto done;
15143 case SCROLLING_NEED_LARGER_MATRICES:
15144 goto need_larger_matrices;
15146 case SCROLLING_FAILED:
15147 break;
15149 default:
15150 abort ();
15154 /* Finally, just choose a place to start which positions point
15155 according to user preferences. */
15157 recenter:
15159 #if GLYPH_DEBUG
15160 debug_method_add (w, "recenter");
15161 #endif
15163 /* w->vscroll = 0; */
15165 /* Forget any previously recorded base line for line number display. */
15166 if (!buffer_unchanged_p)
15167 w->base_line_number = Qnil;
15169 /* Determine the window start relative to point. */
15170 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15171 it.current_y = it.last_visible_y;
15172 if (centering_position < 0)
15174 int margin =
15175 scroll_margin > 0
15176 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15177 : 0;
15178 EMACS_INT margin_pos = CHARPOS (startp);
15179 int scrolling_up;
15180 Lisp_Object aggressive;
15182 /* If there is a scroll margin at the top of the window, find
15183 its character position. */
15184 if (margin
15185 /* Cannot call start_display if startp is not in the
15186 accessible region of the buffer. This can happen when we
15187 have just switched to a different buffer and/or changed
15188 its restriction. In that case, startp is initialized to
15189 the character position 1 (BEG) because we did not yet
15190 have chance to display the buffer even once. */
15191 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15193 struct it it1;
15194 void *it1data = NULL;
15196 SAVE_IT (it1, it, it1data);
15197 start_display (&it1, w, startp);
15198 move_it_vertically (&it1, margin);
15199 margin_pos = IT_CHARPOS (it1);
15200 RESTORE_IT (&it, &it, it1data);
15202 scrolling_up = PT > margin_pos;
15203 aggressive =
15204 scrolling_up
15205 ? BVAR (current_buffer, scroll_up_aggressively)
15206 : BVAR (current_buffer, scroll_down_aggressively);
15208 if (!MINI_WINDOW_P (w)
15209 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15211 int pt_offset = 0;
15213 /* Setting scroll-conservatively overrides
15214 scroll-*-aggressively. */
15215 if (!scroll_conservatively && NUMBERP (aggressive))
15217 double float_amount = XFLOATINT (aggressive);
15219 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15220 if (pt_offset == 0 && float_amount > 0)
15221 pt_offset = 1;
15222 if (pt_offset)
15223 margin -= 1;
15225 /* Compute how much to move the window start backward from
15226 point so that point will be displayed where the user
15227 wants it. */
15228 if (scrolling_up)
15230 centering_position = it.last_visible_y;
15231 if (pt_offset)
15232 centering_position -= pt_offset;
15233 centering_position -=
15234 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
15235 /* Don't let point enter the scroll margin near top of
15236 the window. */
15237 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15238 centering_position = margin * FRAME_LINE_HEIGHT (f);
15240 else
15241 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15243 else
15244 /* Set the window start half the height of the window backward
15245 from point. */
15246 centering_position = window_box_height (w) / 2;
15248 move_it_vertically_backward (&it, centering_position);
15250 xassert (IT_CHARPOS (it) >= BEGV);
15252 /* The function move_it_vertically_backward may move over more
15253 than the specified y-distance. If it->w is small, e.g. a
15254 mini-buffer window, we may end up in front of the window's
15255 display area. Start displaying at the start of the line
15256 containing PT in this case. */
15257 if (it.current_y <= 0)
15259 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15260 move_it_vertically_backward (&it, 0);
15261 it.current_y = 0;
15264 it.current_x = it.hpos = 0;
15266 /* Set the window start position here explicitly, to avoid an
15267 infinite loop in case the functions in window-scroll-functions
15268 get errors. */
15269 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15271 /* Run scroll hooks. */
15272 startp = run_window_scroll_functions (window, it.current.pos);
15274 /* Redisplay the window. */
15275 if (!current_matrix_up_to_date_p
15276 || windows_or_buffers_changed
15277 || cursor_type_changed
15278 /* Don't use try_window_reusing_current_matrix in this case
15279 because it can have changed the buffer. */
15280 || !NILP (Vwindow_scroll_functions)
15281 || !just_this_one_p
15282 || MINI_WINDOW_P (w)
15283 || !(used_current_matrix_p
15284 = try_window_reusing_current_matrix (w)))
15285 try_window (window, startp, 0);
15287 /* If new fonts have been loaded (due to fontsets), give up. We
15288 have to start a new redisplay since we need to re-adjust glyph
15289 matrices. */
15290 if (fonts_changed_p)
15291 goto need_larger_matrices;
15293 /* If cursor did not appear assume that the middle of the window is
15294 in the first line of the window. Do it again with the next line.
15295 (Imagine a window of height 100, displaying two lines of height
15296 60. Moving back 50 from it->last_visible_y will end in the first
15297 line.) */
15298 if (w->cursor.vpos < 0)
15300 if (!NILP (w->window_end_valid)
15301 && PT >= Z - XFASTINT (w->window_end_pos))
15303 clear_glyph_matrix (w->desired_matrix);
15304 move_it_by_lines (&it, 1);
15305 try_window (window, it.current.pos, 0);
15307 else if (PT < IT_CHARPOS (it))
15309 clear_glyph_matrix (w->desired_matrix);
15310 move_it_by_lines (&it, -1);
15311 try_window (window, it.current.pos, 0);
15313 else
15315 /* Not much we can do about it. */
15319 /* Consider the following case: Window starts at BEGV, there is
15320 invisible, intangible text at BEGV, so that display starts at
15321 some point START > BEGV. It can happen that we are called with
15322 PT somewhere between BEGV and START. Try to handle that case. */
15323 if (w->cursor.vpos < 0)
15325 struct glyph_row *row = w->current_matrix->rows;
15326 if (row->mode_line_p)
15327 ++row;
15328 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15331 if (!cursor_row_fully_visible_p (w, 0, 0))
15333 /* If vscroll is enabled, disable it and try again. */
15334 if (w->vscroll)
15336 w->vscroll = 0;
15337 clear_glyph_matrix (w->desired_matrix);
15338 goto recenter;
15341 /* If centering point failed to make the whole line visible,
15342 put point at the top instead. That has to make the whole line
15343 visible, if it can be done. */
15344 if (centering_position == 0)
15345 goto done;
15347 clear_glyph_matrix (w->desired_matrix);
15348 centering_position = 0;
15349 goto recenter;
15352 done:
15354 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15355 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15356 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15357 ? Qt : Qnil);
15359 /* Display the mode line, if we must. */
15360 if ((update_mode_line
15361 /* If window not full width, must redo its mode line
15362 if (a) the window to its side is being redone and
15363 (b) we do a frame-based redisplay. This is a consequence
15364 of how inverted lines are drawn in frame-based redisplay. */
15365 || (!just_this_one_p
15366 && !FRAME_WINDOW_P (f)
15367 && !WINDOW_FULL_WIDTH_P (w))
15368 /* Line number to display. */
15369 || INTEGERP (w->base_line_pos)
15370 /* Column number is displayed and different from the one displayed. */
15371 || (!NILP (w->column_number_displayed)
15372 && (XFASTINT (w->column_number_displayed) != current_column ())))
15373 /* This means that the window has a mode line. */
15374 && (WINDOW_WANTS_MODELINE_P (w)
15375 || WINDOW_WANTS_HEADER_LINE_P (w)))
15377 display_mode_lines (w);
15379 /* If mode line height has changed, arrange for a thorough
15380 immediate redisplay using the correct mode line height. */
15381 if (WINDOW_WANTS_MODELINE_P (w)
15382 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15384 fonts_changed_p = 1;
15385 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15386 = DESIRED_MODE_LINE_HEIGHT (w);
15389 /* If header line height has changed, arrange for a thorough
15390 immediate redisplay using the correct header line height. */
15391 if (WINDOW_WANTS_HEADER_LINE_P (w)
15392 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15394 fonts_changed_p = 1;
15395 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15396 = DESIRED_HEADER_LINE_HEIGHT (w);
15399 if (fonts_changed_p)
15400 goto need_larger_matrices;
15403 if (!line_number_displayed
15404 && !BUFFERP (w->base_line_pos))
15406 w->base_line_pos = Qnil;
15407 w->base_line_number = Qnil;
15410 finish_menu_bars:
15412 /* When we reach a frame's selected window, redo the frame's menu bar. */
15413 if (update_mode_line
15414 && EQ (FRAME_SELECTED_WINDOW (f), window))
15416 int redisplay_menu_p = 0;
15418 if (FRAME_WINDOW_P (f))
15420 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15421 || defined (HAVE_NS) || defined (USE_GTK)
15422 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15423 #else
15424 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15425 #endif
15427 else
15428 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15430 if (redisplay_menu_p)
15431 display_menu_bar (w);
15433 #ifdef HAVE_WINDOW_SYSTEM
15434 if (FRAME_WINDOW_P (f))
15436 #if defined (USE_GTK) || defined (HAVE_NS)
15437 if (FRAME_EXTERNAL_TOOL_BAR (f))
15438 redisplay_tool_bar (f);
15439 #else
15440 if (WINDOWP (f->tool_bar_window)
15441 && (FRAME_TOOL_BAR_LINES (f) > 0
15442 || !NILP (Vauto_resize_tool_bars))
15443 && redisplay_tool_bar (f))
15444 ignore_mouse_drag_p = 1;
15445 #endif
15447 #endif
15450 #ifdef HAVE_WINDOW_SYSTEM
15451 if (FRAME_WINDOW_P (f)
15452 && update_window_fringes (w, (just_this_one_p
15453 || (!used_current_matrix_p && !overlay_arrow_seen)
15454 || w->pseudo_window_p)))
15456 update_begin (f);
15457 BLOCK_INPUT;
15458 if (draw_window_fringes (w, 1))
15459 x_draw_vertical_border (w);
15460 UNBLOCK_INPUT;
15461 update_end (f);
15463 #endif /* HAVE_WINDOW_SYSTEM */
15465 /* We go to this label, with fonts_changed_p nonzero,
15466 if it is necessary to try again using larger glyph matrices.
15467 We have to redeem the scroll bar even in this case,
15468 because the loop in redisplay_internal expects that. */
15469 need_larger_matrices:
15471 finish_scroll_bars:
15473 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15475 /* Set the thumb's position and size. */
15476 set_vertical_scroll_bar (w);
15478 /* Note that we actually used the scroll bar attached to this
15479 window, so it shouldn't be deleted at the end of redisplay. */
15480 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15481 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15484 /* Restore current_buffer and value of point in it. The window
15485 update may have changed the buffer, so first make sure `opoint'
15486 is still valid (Bug#6177). */
15487 if (CHARPOS (opoint) < BEGV)
15488 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15489 else if (CHARPOS (opoint) > ZV)
15490 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15491 else
15492 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15494 set_buffer_internal_1 (old);
15495 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15496 shorter. This can be caused by log truncation in *Messages*. */
15497 if (CHARPOS (lpoint) <= ZV)
15498 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15500 unbind_to (count, Qnil);
15504 /* Build the complete desired matrix of WINDOW with a window start
15505 buffer position POS.
15507 Value is 1 if successful. It is zero if fonts were loaded during
15508 redisplay which makes re-adjusting glyph matrices necessary, and -1
15509 if point would appear in the scroll margins.
15510 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15511 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15512 set in FLAGS.) */
15515 try_window (Lisp_Object window, struct text_pos pos, int flags)
15517 struct window *w = XWINDOW (window);
15518 struct it it;
15519 struct glyph_row *last_text_row = NULL;
15520 struct frame *f = XFRAME (w->frame);
15522 /* Make POS the new window start. */
15523 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15525 /* Mark cursor position as unknown. No overlay arrow seen. */
15526 w->cursor.vpos = -1;
15527 overlay_arrow_seen = 0;
15529 /* Initialize iterator and info to start at POS. */
15530 start_display (&it, w, pos);
15532 /* Display all lines of W. */
15533 while (it.current_y < it.last_visible_y)
15535 if (display_line (&it))
15536 last_text_row = it.glyph_row - 1;
15537 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15538 return 0;
15541 /* Don't let the cursor end in the scroll margins. */
15542 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15543 && !MINI_WINDOW_P (w))
15545 int this_scroll_margin;
15547 if (scroll_margin > 0)
15549 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15550 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15552 else
15553 this_scroll_margin = 0;
15555 if ((w->cursor.y >= 0 /* not vscrolled */
15556 && w->cursor.y < this_scroll_margin
15557 && CHARPOS (pos) > BEGV
15558 && IT_CHARPOS (it) < ZV)
15559 /* rms: considering make_cursor_line_fully_visible_p here
15560 seems to give wrong results. We don't want to recenter
15561 when the last line is partly visible, we want to allow
15562 that case to be handled in the usual way. */
15563 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15565 w->cursor.vpos = -1;
15566 clear_glyph_matrix (w->desired_matrix);
15567 return -1;
15571 /* If bottom moved off end of frame, change mode line percentage. */
15572 if (XFASTINT (w->window_end_pos) <= 0
15573 && Z != IT_CHARPOS (it))
15574 w->update_mode_line = Qt;
15576 /* Set window_end_pos to the offset of the last character displayed
15577 on the window from the end of current_buffer. Set
15578 window_end_vpos to its row number. */
15579 if (last_text_row)
15581 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15582 w->window_end_bytepos
15583 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15584 w->window_end_pos
15585 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15586 w->window_end_vpos
15587 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15588 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15589 ->displays_text_p);
15591 else
15593 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15594 w->window_end_pos = make_number (Z - ZV);
15595 w->window_end_vpos = make_number (0);
15598 /* But that is not valid info until redisplay finishes. */
15599 w->window_end_valid = Qnil;
15600 return 1;
15605 /************************************************************************
15606 Window redisplay reusing current matrix when buffer has not changed
15607 ************************************************************************/
15609 /* Try redisplay of window W showing an unchanged buffer with a
15610 different window start than the last time it was displayed by
15611 reusing its current matrix. Value is non-zero if successful.
15612 W->start is the new window start. */
15614 static int
15615 try_window_reusing_current_matrix (struct window *w)
15617 struct frame *f = XFRAME (w->frame);
15618 struct glyph_row *bottom_row;
15619 struct it it;
15620 struct run run;
15621 struct text_pos start, new_start;
15622 int nrows_scrolled, i;
15623 struct glyph_row *last_text_row;
15624 struct glyph_row *last_reused_text_row;
15625 struct glyph_row *start_row;
15626 int start_vpos, min_y, max_y;
15628 #if GLYPH_DEBUG
15629 if (inhibit_try_window_reusing)
15630 return 0;
15631 #endif
15633 if (/* This function doesn't handle terminal frames. */
15634 !FRAME_WINDOW_P (f)
15635 /* Don't try to reuse the display if windows have been split
15636 or such. */
15637 || windows_or_buffers_changed
15638 || cursor_type_changed)
15639 return 0;
15641 /* Can't do this if region may have changed. */
15642 if ((!NILP (Vtransient_mark_mode)
15643 && !NILP (BVAR (current_buffer, mark_active)))
15644 || !NILP (w->region_showing)
15645 || !NILP (Vshow_trailing_whitespace))
15646 return 0;
15648 /* If top-line visibility has changed, give up. */
15649 if (WINDOW_WANTS_HEADER_LINE_P (w)
15650 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15651 return 0;
15653 /* Give up if old or new display is scrolled vertically. We could
15654 make this function handle this, but right now it doesn't. */
15655 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15656 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15657 return 0;
15659 /* The variable new_start now holds the new window start. The old
15660 start `start' can be determined from the current matrix. */
15661 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15662 start = start_row->minpos;
15663 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15665 /* Clear the desired matrix for the display below. */
15666 clear_glyph_matrix (w->desired_matrix);
15668 if (CHARPOS (new_start) <= CHARPOS (start))
15670 /* Don't use this method if the display starts with an ellipsis
15671 displayed for invisible text. It's not easy to handle that case
15672 below, and it's certainly not worth the effort since this is
15673 not a frequent case. */
15674 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15675 return 0;
15677 IF_DEBUG (debug_method_add (w, "twu1"));
15679 /* Display up to a row that can be reused. The variable
15680 last_text_row is set to the last row displayed that displays
15681 text. Note that it.vpos == 0 if or if not there is a
15682 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15683 start_display (&it, w, new_start);
15684 w->cursor.vpos = -1;
15685 last_text_row = last_reused_text_row = NULL;
15687 while (it.current_y < it.last_visible_y
15688 && !fonts_changed_p)
15690 /* If we have reached into the characters in the START row,
15691 that means the line boundaries have changed. So we
15692 can't start copying with the row START. Maybe it will
15693 work to start copying with the following row. */
15694 while (IT_CHARPOS (it) > CHARPOS (start))
15696 /* Advance to the next row as the "start". */
15697 start_row++;
15698 start = start_row->minpos;
15699 /* If there are no more rows to try, or just one, give up. */
15700 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15701 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15702 || CHARPOS (start) == ZV)
15704 clear_glyph_matrix (w->desired_matrix);
15705 return 0;
15708 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15710 /* If we have reached alignment,
15711 we can copy the rest of the rows. */
15712 if (IT_CHARPOS (it) == CHARPOS (start))
15713 break;
15715 if (display_line (&it))
15716 last_text_row = it.glyph_row - 1;
15719 /* A value of current_y < last_visible_y means that we stopped
15720 at the previous window start, which in turn means that we
15721 have at least one reusable row. */
15722 if (it.current_y < it.last_visible_y)
15724 struct glyph_row *row;
15726 /* IT.vpos always starts from 0; it counts text lines. */
15727 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15729 /* Find PT if not already found in the lines displayed. */
15730 if (w->cursor.vpos < 0)
15732 int dy = it.current_y - start_row->y;
15734 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15735 row = row_containing_pos (w, PT, row, NULL, dy);
15736 if (row)
15737 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15738 dy, nrows_scrolled);
15739 else
15741 clear_glyph_matrix (w->desired_matrix);
15742 return 0;
15746 /* Scroll the display. Do it before the current matrix is
15747 changed. The problem here is that update has not yet
15748 run, i.e. part of the current matrix is not up to date.
15749 scroll_run_hook will clear the cursor, and use the
15750 current matrix to get the height of the row the cursor is
15751 in. */
15752 run.current_y = start_row->y;
15753 run.desired_y = it.current_y;
15754 run.height = it.last_visible_y - it.current_y;
15756 if (run.height > 0 && run.current_y != run.desired_y)
15758 update_begin (f);
15759 FRAME_RIF (f)->update_window_begin_hook (w);
15760 FRAME_RIF (f)->clear_window_mouse_face (w);
15761 FRAME_RIF (f)->scroll_run_hook (w, &run);
15762 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15763 update_end (f);
15766 /* Shift current matrix down by nrows_scrolled lines. */
15767 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15768 rotate_matrix (w->current_matrix,
15769 start_vpos,
15770 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15771 nrows_scrolled);
15773 /* Disable lines that must be updated. */
15774 for (i = 0; i < nrows_scrolled; ++i)
15775 (start_row + i)->enabled_p = 0;
15777 /* Re-compute Y positions. */
15778 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15779 max_y = it.last_visible_y;
15780 for (row = start_row + nrows_scrolled;
15781 row < bottom_row;
15782 ++row)
15784 row->y = it.current_y;
15785 row->visible_height = row->height;
15787 if (row->y < min_y)
15788 row->visible_height -= min_y - row->y;
15789 if (row->y + row->height > max_y)
15790 row->visible_height -= row->y + row->height - max_y;
15791 if (row->fringe_bitmap_periodic_p)
15792 row->redraw_fringe_bitmaps_p = 1;
15794 it.current_y += row->height;
15796 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15797 last_reused_text_row = row;
15798 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15799 break;
15802 /* Disable lines in the current matrix which are now
15803 below the window. */
15804 for (++row; row < bottom_row; ++row)
15805 row->enabled_p = row->mode_line_p = 0;
15808 /* Update window_end_pos etc.; last_reused_text_row is the last
15809 reused row from the current matrix containing text, if any.
15810 The value of last_text_row is the last displayed line
15811 containing text. */
15812 if (last_reused_text_row)
15814 w->window_end_bytepos
15815 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15816 w->window_end_pos
15817 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15818 w->window_end_vpos
15819 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15820 w->current_matrix));
15822 else if (last_text_row)
15824 w->window_end_bytepos
15825 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15826 w->window_end_pos
15827 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15828 w->window_end_vpos
15829 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15831 else
15833 /* This window must be completely empty. */
15834 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15835 w->window_end_pos = make_number (Z - ZV);
15836 w->window_end_vpos = make_number (0);
15838 w->window_end_valid = Qnil;
15840 /* Update hint: don't try scrolling again in update_window. */
15841 w->desired_matrix->no_scrolling_p = 1;
15843 #if GLYPH_DEBUG
15844 debug_method_add (w, "try_window_reusing_current_matrix 1");
15845 #endif
15846 return 1;
15848 else if (CHARPOS (new_start) > CHARPOS (start))
15850 struct glyph_row *pt_row, *row;
15851 struct glyph_row *first_reusable_row;
15852 struct glyph_row *first_row_to_display;
15853 int dy;
15854 int yb = window_text_bottom_y (w);
15856 /* Find the row starting at new_start, if there is one. Don't
15857 reuse a partially visible line at the end. */
15858 first_reusable_row = start_row;
15859 while (first_reusable_row->enabled_p
15860 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15861 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15862 < CHARPOS (new_start)))
15863 ++first_reusable_row;
15865 /* Give up if there is no row to reuse. */
15866 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15867 || !first_reusable_row->enabled_p
15868 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15869 != CHARPOS (new_start)))
15870 return 0;
15872 /* We can reuse fully visible rows beginning with
15873 first_reusable_row to the end of the window. Set
15874 first_row_to_display to the first row that cannot be reused.
15875 Set pt_row to the row containing point, if there is any. */
15876 pt_row = NULL;
15877 for (first_row_to_display = first_reusable_row;
15878 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15879 ++first_row_to_display)
15881 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15882 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15883 pt_row = first_row_to_display;
15886 /* Start displaying at the start of first_row_to_display. */
15887 xassert (first_row_to_display->y < yb);
15888 init_to_row_start (&it, w, first_row_to_display);
15890 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15891 - start_vpos);
15892 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15893 - nrows_scrolled);
15894 it.current_y = (first_row_to_display->y - first_reusable_row->y
15895 + WINDOW_HEADER_LINE_HEIGHT (w));
15897 /* Display lines beginning with first_row_to_display in the
15898 desired matrix. Set last_text_row to the last row displayed
15899 that displays text. */
15900 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15901 if (pt_row == NULL)
15902 w->cursor.vpos = -1;
15903 last_text_row = NULL;
15904 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15905 if (display_line (&it))
15906 last_text_row = it.glyph_row - 1;
15908 /* If point is in a reused row, adjust y and vpos of the cursor
15909 position. */
15910 if (pt_row)
15912 w->cursor.vpos -= nrows_scrolled;
15913 w->cursor.y -= first_reusable_row->y - start_row->y;
15916 /* Give up if point isn't in a row displayed or reused. (This
15917 also handles the case where w->cursor.vpos < nrows_scrolled
15918 after the calls to display_line, which can happen with scroll
15919 margins. See bug#1295.) */
15920 if (w->cursor.vpos < 0)
15922 clear_glyph_matrix (w->desired_matrix);
15923 return 0;
15926 /* Scroll the display. */
15927 run.current_y = first_reusable_row->y;
15928 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15929 run.height = it.last_visible_y - run.current_y;
15930 dy = run.current_y - run.desired_y;
15932 if (run.height)
15934 update_begin (f);
15935 FRAME_RIF (f)->update_window_begin_hook (w);
15936 FRAME_RIF (f)->clear_window_mouse_face (w);
15937 FRAME_RIF (f)->scroll_run_hook (w, &run);
15938 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15939 update_end (f);
15942 /* Adjust Y positions of reused rows. */
15943 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15944 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15945 max_y = it.last_visible_y;
15946 for (row = first_reusable_row; row < first_row_to_display; ++row)
15948 row->y -= dy;
15949 row->visible_height = row->height;
15950 if (row->y < min_y)
15951 row->visible_height -= min_y - row->y;
15952 if (row->y + row->height > max_y)
15953 row->visible_height -= row->y + row->height - max_y;
15954 if (row->fringe_bitmap_periodic_p)
15955 row->redraw_fringe_bitmaps_p = 1;
15958 /* Scroll the current matrix. */
15959 xassert (nrows_scrolled > 0);
15960 rotate_matrix (w->current_matrix,
15961 start_vpos,
15962 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15963 -nrows_scrolled);
15965 /* Disable rows not reused. */
15966 for (row -= nrows_scrolled; row < bottom_row; ++row)
15967 row->enabled_p = 0;
15969 /* Point may have moved to a different line, so we cannot assume that
15970 the previous cursor position is valid; locate the correct row. */
15971 if (pt_row)
15973 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15974 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15975 row++)
15977 w->cursor.vpos++;
15978 w->cursor.y = row->y;
15980 if (row < bottom_row)
15982 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15983 struct glyph *end = glyph + row->used[TEXT_AREA];
15985 /* Can't use this optimization with bidi-reordered glyph
15986 rows, unless cursor is already at point. */
15987 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15989 if (!(w->cursor.hpos >= 0
15990 && w->cursor.hpos < row->used[TEXT_AREA]
15991 && BUFFERP (glyph->object)
15992 && glyph->charpos == PT))
15993 return 0;
15995 else
15996 for (; glyph < end
15997 && (!BUFFERP (glyph->object)
15998 || glyph->charpos < PT);
15999 glyph++)
16001 w->cursor.hpos++;
16002 w->cursor.x += glyph->pixel_width;
16007 /* Adjust window end. A null value of last_text_row means that
16008 the window end is in reused rows which in turn means that
16009 only its vpos can have changed. */
16010 if (last_text_row)
16012 w->window_end_bytepos
16013 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16014 w->window_end_pos
16015 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16016 w->window_end_vpos
16017 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16019 else
16021 w->window_end_vpos
16022 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16025 w->window_end_valid = Qnil;
16026 w->desired_matrix->no_scrolling_p = 1;
16028 #if GLYPH_DEBUG
16029 debug_method_add (w, "try_window_reusing_current_matrix 2");
16030 #endif
16031 return 1;
16034 return 0;
16039 /************************************************************************
16040 Window redisplay reusing current matrix when buffer has changed
16041 ************************************************************************/
16043 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16044 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16045 EMACS_INT *, EMACS_INT *);
16046 static struct glyph_row *
16047 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16048 struct glyph_row *);
16051 /* Return the last row in MATRIX displaying text. If row START is
16052 non-null, start searching with that row. IT gives the dimensions
16053 of the display. Value is null if matrix is empty; otherwise it is
16054 a pointer to the row found. */
16056 static struct glyph_row *
16057 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16058 struct glyph_row *start)
16060 struct glyph_row *row, *row_found;
16062 /* Set row_found to the last row in IT->w's current matrix
16063 displaying text. The loop looks funny but think of partially
16064 visible lines. */
16065 row_found = NULL;
16066 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16067 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16069 xassert (row->enabled_p);
16070 row_found = row;
16071 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16072 break;
16073 ++row;
16076 return row_found;
16080 /* Return the last row in the current matrix of W that is not affected
16081 by changes at the start of current_buffer that occurred since W's
16082 current matrix was built. Value is null if no such row exists.
16084 BEG_UNCHANGED us the number of characters unchanged at the start of
16085 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16086 first changed character in current_buffer. Characters at positions <
16087 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16088 when the current matrix was built. */
16090 static struct glyph_row *
16091 find_last_unchanged_at_beg_row (struct window *w)
16093 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16094 struct glyph_row *row;
16095 struct glyph_row *row_found = NULL;
16096 int yb = window_text_bottom_y (w);
16098 /* Find the last row displaying unchanged text. */
16099 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16100 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16101 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16102 ++row)
16104 if (/* If row ends before first_changed_pos, it is unchanged,
16105 except in some case. */
16106 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16107 /* When row ends in ZV and we write at ZV it is not
16108 unchanged. */
16109 && !row->ends_at_zv_p
16110 /* When first_changed_pos is the end of a continued line,
16111 row is not unchanged because it may be no longer
16112 continued. */
16113 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16114 && (row->continued_p
16115 || row->exact_window_width_line_p)))
16116 row_found = row;
16118 /* Stop if last visible row. */
16119 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16120 break;
16123 return row_found;
16127 /* Find the first glyph row in the current matrix of W that is not
16128 affected by changes at the end of current_buffer since the
16129 time W's current matrix was built.
16131 Return in *DELTA the number of chars by which buffer positions in
16132 unchanged text at the end of current_buffer must be adjusted.
16134 Return in *DELTA_BYTES the corresponding number of bytes.
16136 Value is null if no such row exists, i.e. all rows are affected by
16137 changes. */
16139 static struct glyph_row *
16140 find_first_unchanged_at_end_row (struct window *w,
16141 EMACS_INT *delta, EMACS_INT *delta_bytes)
16143 struct glyph_row *row;
16144 struct glyph_row *row_found = NULL;
16146 *delta = *delta_bytes = 0;
16148 /* Display must not have been paused, otherwise the current matrix
16149 is not up to date. */
16150 eassert (!NILP (w->window_end_valid));
16152 /* A value of window_end_pos >= END_UNCHANGED means that the window
16153 end is in the range of changed text. If so, there is no
16154 unchanged row at the end of W's current matrix. */
16155 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16156 return NULL;
16158 /* Set row to the last row in W's current matrix displaying text. */
16159 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16161 /* If matrix is entirely empty, no unchanged row exists. */
16162 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16164 /* The value of row is the last glyph row in the matrix having a
16165 meaningful buffer position in it. The end position of row
16166 corresponds to window_end_pos. This allows us to translate
16167 buffer positions in the current matrix to current buffer
16168 positions for characters not in changed text. */
16169 EMACS_INT Z_old =
16170 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16171 EMACS_INT Z_BYTE_old =
16172 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16173 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16174 struct glyph_row *first_text_row
16175 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16177 *delta = Z - Z_old;
16178 *delta_bytes = Z_BYTE - Z_BYTE_old;
16180 /* Set last_unchanged_pos to the buffer position of the last
16181 character in the buffer that has not been changed. Z is the
16182 index + 1 of the last character in current_buffer, i.e. by
16183 subtracting END_UNCHANGED we get the index of the last
16184 unchanged character, and we have to add BEG to get its buffer
16185 position. */
16186 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16187 last_unchanged_pos_old = last_unchanged_pos - *delta;
16189 /* Search backward from ROW for a row displaying a line that
16190 starts at a minimum position >= last_unchanged_pos_old. */
16191 for (; row > first_text_row; --row)
16193 /* This used to abort, but it can happen.
16194 It is ok to just stop the search instead here. KFS. */
16195 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16196 break;
16198 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16199 row_found = row;
16203 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16205 return row_found;
16209 /* Make sure that glyph rows in the current matrix of window W
16210 reference the same glyph memory as corresponding rows in the
16211 frame's frame matrix. This function is called after scrolling W's
16212 current matrix on a terminal frame in try_window_id and
16213 try_window_reusing_current_matrix. */
16215 static void
16216 sync_frame_with_window_matrix_rows (struct window *w)
16218 struct frame *f = XFRAME (w->frame);
16219 struct glyph_row *window_row, *window_row_end, *frame_row;
16221 /* Preconditions: W must be a leaf window and full-width. Its frame
16222 must have a frame matrix. */
16223 xassert (NILP (w->hchild) && NILP (w->vchild));
16224 xassert (WINDOW_FULL_WIDTH_P (w));
16225 xassert (!FRAME_WINDOW_P (f));
16227 /* If W is a full-width window, glyph pointers in W's current matrix
16228 have, by definition, to be the same as glyph pointers in the
16229 corresponding frame matrix. Note that frame matrices have no
16230 marginal areas (see build_frame_matrix). */
16231 window_row = w->current_matrix->rows;
16232 window_row_end = window_row + w->current_matrix->nrows;
16233 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16234 while (window_row < window_row_end)
16236 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16237 struct glyph *end = window_row->glyphs[LAST_AREA];
16239 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16240 frame_row->glyphs[TEXT_AREA] = start;
16241 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16242 frame_row->glyphs[LAST_AREA] = end;
16244 /* Disable frame rows whose corresponding window rows have
16245 been disabled in try_window_id. */
16246 if (!window_row->enabled_p)
16247 frame_row->enabled_p = 0;
16249 ++window_row, ++frame_row;
16254 /* Find the glyph row in window W containing CHARPOS. Consider all
16255 rows between START and END (not inclusive). END null means search
16256 all rows to the end of the display area of W. Value is the row
16257 containing CHARPOS or null. */
16259 struct glyph_row *
16260 row_containing_pos (struct window *w, EMACS_INT charpos,
16261 struct glyph_row *start, struct glyph_row *end, int dy)
16263 struct glyph_row *row = start;
16264 struct glyph_row *best_row = NULL;
16265 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16266 int last_y;
16268 /* If we happen to start on a header-line, skip that. */
16269 if (row->mode_line_p)
16270 ++row;
16272 if ((end && row >= end) || !row->enabled_p)
16273 return NULL;
16275 last_y = window_text_bottom_y (w) - dy;
16277 while (1)
16279 /* Give up if we have gone too far. */
16280 if (end && row >= end)
16281 return NULL;
16282 /* This formerly returned if they were equal.
16283 I think that both quantities are of a "last plus one" type;
16284 if so, when they are equal, the row is within the screen. -- rms. */
16285 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16286 return NULL;
16288 /* If it is in this row, return this row. */
16289 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16290 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16291 /* The end position of a row equals the start
16292 position of the next row. If CHARPOS is there, we
16293 would rather display it in the next line, except
16294 when this line ends in ZV. */
16295 && !row->ends_at_zv_p
16296 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16297 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16299 struct glyph *g;
16301 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16302 || (!best_row && !row->continued_p))
16303 return row;
16304 /* In bidi-reordered rows, there could be several rows
16305 occluding point, all of them belonging to the same
16306 continued line. We need to find the row which fits
16307 CHARPOS the best. */
16308 for (g = row->glyphs[TEXT_AREA];
16309 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16310 g++)
16312 if (!STRINGP (g->object))
16314 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16316 mindif = eabs (g->charpos - charpos);
16317 best_row = row;
16318 /* Exact match always wins. */
16319 if (mindif == 0)
16320 return best_row;
16325 else if (best_row && !row->continued_p)
16326 return best_row;
16327 ++row;
16332 /* Try to redisplay window W by reusing its existing display. W's
16333 current matrix must be up to date when this function is called,
16334 i.e. window_end_valid must not be nil.
16336 Value is
16338 1 if display has been updated
16339 0 if otherwise unsuccessful
16340 -1 if redisplay with same window start is known not to succeed
16342 The following steps are performed:
16344 1. Find the last row in the current matrix of W that is not
16345 affected by changes at the start of current_buffer. If no such row
16346 is found, give up.
16348 2. Find the first row in W's current matrix that is not affected by
16349 changes at the end of current_buffer. Maybe there is no such row.
16351 3. Display lines beginning with the row + 1 found in step 1 to the
16352 row found in step 2 or, if step 2 didn't find a row, to the end of
16353 the window.
16355 4. If cursor is not known to appear on the window, give up.
16357 5. If display stopped at the row found in step 2, scroll the
16358 display and current matrix as needed.
16360 6. Maybe display some lines at the end of W, if we must. This can
16361 happen under various circumstances, like a partially visible line
16362 becoming fully visible, or because newly displayed lines are displayed
16363 in smaller font sizes.
16365 7. Update W's window end information. */
16367 static int
16368 try_window_id (struct window *w)
16370 struct frame *f = XFRAME (w->frame);
16371 struct glyph_matrix *current_matrix = w->current_matrix;
16372 struct glyph_matrix *desired_matrix = w->desired_matrix;
16373 struct glyph_row *last_unchanged_at_beg_row;
16374 struct glyph_row *first_unchanged_at_end_row;
16375 struct glyph_row *row;
16376 struct glyph_row *bottom_row;
16377 int bottom_vpos;
16378 struct it it;
16379 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16380 int dvpos, dy;
16381 struct text_pos start_pos;
16382 struct run run;
16383 int first_unchanged_at_end_vpos = 0;
16384 struct glyph_row *last_text_row, *last_text_row_at_end;
16385 struct text_pos start;
16386 EMACS_INT first_changed_charpos, last_changed_charpos;
16388 #if GLYPH_DEBUG
16389 if (inhibit_try_window_id)
16390 return 0;
16391 #endif
16393 /* This is handy for debugging. */
16394 #if 0
16395 #define GIVE_UP(X) \
16396 do { \
16397 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16398 return 0; \
16399 } while (0)
16400 #else
16401 #define GIVE_UP(X) return 0
16402 #endif
16404 SET_TEXT_POS_FROM_MARKER (start, w->start);
16406 /* Don't use this for mini-windows because these can show
16407 messages and mini-buffers, and we don't handle that here. */
16408 if (MINI_WINDOW_P (w))
16409 GIVE_UP (1);
16411 /* This flag is used to prevent redisplay optimizations. */
16412 if (windows_or_buffers_changed || cursor_type_changed)
16413 GIVE_UP (2);
16415 /* Verify that narrowing has not changed.
16416 Also verify that we were not told to prevent redisplay optimizations.
16417 It would be nice to further
16418 reduce the number of cases where this prevents try_window_id. */
16419 if (current_buffer->clip_changed
16420 || current_buffer->prevent_redisplay_optimizations_p)
16421 GIVE_UP (3);
16423 /* Window must either use window-based redisplay or be full width. */
16424 if (!FRAME_WINDOW_P (f)
16425 && (!FRAME_LINE_INS_DEL_OK (f)
16426 || !WINDOW_FULL_WIDTH_P (w)))
16427 GIVE_UP (4);
16429 /* Give up if point is known NOT to appear in W. */
16430 if (PT < CHARPOS (start))
16431 GIVE_UP (5);
16433 /* Another way to prevent redisplay optimizations. */
16434 if (XFASTINT (w->last_modified) == 0)
16435 GIVE_UP (6);
16437 /* Verify that window is not hscrolled. */
16438 if (XFASTINT (w->hscroll) != 0)
16439 GIVE_UP (7);
16441 /* Verify that display wasn't paused. */
16442 if (NILP (w->window_end_valid))
16443 GIVE_UP (8);
16445 /* Can't use this if highlighting a region because a cursor movement
16446 will do more than just set the cursor. */
16447 if (!NILP (Vtransient_mark_mode)
16448 && !NILP (BVAR (current_buffer, mark_active)))
16449 GIVE_UP (9);
16451 /* Likewise if highlighting trailing whitespace. */
16452 if (!NILP (Vshow_trailing_whitespace))
16453 GIVE_UP (11);
16455 /* Likewise if showing a region. */
16456 if (!NILP (w->region_showing))
16457 GIVE_UP (10);
16459 /* Can't use this if overlay arrow position and/or string have
16460 changed. */
16461 if (overlay_arrows_changed_p ())
16462 GIVE_UP (12);
16464 /* When word-wrap is on, adding a space to the first word of a
16465 wrapped line can change the wrap position, altering the line
16466 above it. It might be worthwhile to handle this more
16467 intelligently, but for now just redisplay from scratch. */
16468 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16469 GIVE_UP (21);
16471 /* Under bidi reordering, adding or deleting a character in the
16472 beginning of a paragraph, before the first strong directional
16473 character, can change the base direction of the paragraph (unless
16474 the buffer specifies a fixed paragraph direction), which will
16475 require to redisplay the whole paragraph. It might be worthwhile
16476 to find the paragraph limits and widen the range of redisplayed
16477 lines to that, but for now just give up this optimization and
16478 redisplay from scratch. */
16479 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16480 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16481 GIVE_UP (22);
16483 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16484 only if buffer has really changed. The reason is that the gap is
16485 initially at Z for freshly visited files. The code below would
16486 set end_unchanged to 0 in that case. */
16487 if (MODIFF > SAVE_MODIFF
16488 /* This seems to happen sometimes after saving a buffer. */
16489 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16491 if (GPT - BEG < BEG_UNCHANGED)
16492 BEG_UNCHANGED = GPT - BEG;
16493 if (Z - GPT < END_UNCHANGED)
16494 END_UNCHANGED = Z - GPT;
16497 /* The position of the first and last character that has been changed. */
16498 first_changed_charpos = BEG + BEG_UNCHANGED;
16499 last_changed_charpos = Z - END_UNCHANGED;
16501 /* If window starts after a line end, and the last change is in
16502 front of that newline, then changes don't affect the display.
16503 This case happens with stealth-fontification. Note that although
16504 the display is unchanged, glyph positions in the matrix have to
16505 be adjusted, of course. */
16506 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16507 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16508 && ((last_changed_charpos < CHARPOS (start)
16509 && CHARPOS (start) == BEGV)
16510 || (last_changed_charpos < CHARPOS (start) - 1
16511 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16513 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16514 struct glyph_row *r0;
16516 /* Compute how many chars/bytes have been added to or removed
16517 from the buffer. */
16518 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16519 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16520 Z_delta = Z - Z_old;
16521 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16523 /* Give up if PT is not in the window. Note that it already has
16524 been checked at the start of try_window_id that PT is not in
16525 front of the window start. */
16526 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16527 GIVE_UP (13);
16529 /* If window start is unchanged, we can reuse the whole matrix
16530 as is, after adjusting glyph positions. No need to compute
16531 the window end again, since its offset from Z hasn't changed. */
16532 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16533 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16534 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16535 /* PT must not be in a partially visible line. */
16536 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16537 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16539 /* Adjust positions in the glyph matrix. */
16540 if (Z_delta || Z_delta_bytes)
16542 struct glyph_row *r1
16543 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16544 increment_matrix_positions (w->current_matrix,
16545 MATRIX_ROW_VPOS (r0, current_matrix),
16546 MATRIX_ROW_VPOS (r1, current_matrix),
16547 Z_delta, Z_delta_bytes);
16550 /* Set the cursor. */
16551 row = row_containing_pos (w, PT, r0, NULL, 0);
16552 if (row)
16553 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16554 else
16555 abort ();
16556 return 1;
16560 /* Handle the case that changes are all below what is displayed in
16561 the window, and that PT is in the window. This shortcut cannot
16562 be taken if ZV is visible in the window, and text has been added
16563 there that is visible in the window. */
16564 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16565 /* ZV is not visible in the window, or there are no
16566 changes at ZV, actually. */
16567 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16568 || first_changed_charpos == last_changed_charpos))
16570 struct glyph_row *r0;
16572 /* Give up if PT is not in the window. Note that it already has
16573 been checked at the start of try_window_id that PT is not in
16574 front of the window start. */
16575 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16576 GIVE_UP (14);
16578 /* If window start is unchanged, we can reuse the whole matrix
16579 as is, without changing glyph positions since no text has
16580 been added/removed in front of the window end. */
16581 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16582 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16583 /* PT must not be in a partially visible line. */
16584 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16585 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16587 /* We have to compute the window end anew since text
16588 could have been added/removed after it. */
16589 w->window_end_pos
16590 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16591 w->window_end_bytepos
16592 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16594 /* Set the cursor. */
16595 row = row_containing_pos (w, PT, r0, NULL, 0);
16596 if (row)
16597 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16598 else
16599 abort ();
16600 return 2;
16604 /* Give up if window start is in the changed area.
16606 The condition used to read
16608 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16610 but why that was tested escapes me at the moment. */
16611 if (CHARPOS (start) >= first_changed_charpos
16612 && CHARPOS (start) <= last_changed_charpos)
16613 GIVE_UP (15);
16615 /* Check that window start agrees with the start of the first glyph
16616 row in its current matrix. Check this after we know the window
16617 start is not in changed text, otherwise positions would not be
16618 comparable. */
16619 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16620 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16621 GIVE_UP (16);
16623 /* Give up if the window ends in strings. Overlay strings
16624 at the end are difficult to handle, so don't try. */
16625 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16626 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16627 GIVE_UP (20);
16629 /* Compute the position at which we have to start displaying new
16630 lines. Some of the lines at the top of the window might be
16631 reusable because they are not displaying changed text. Find the
16632 last row in W's current matrix not affected by changes at the
16633 start of current_buffer. Value is null if changes start in the
16634 first line of window. */
16635 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16636 if (last_unchanged_at_beg_row)
16638 /* Avoid starting to display in the moddle of a character, a TAB
16639 for instance. This is easier than to set up the iterator
16640 exactly, and it's not a frequent case, so the additional
16641 effort wouldn't really pay off. */
16642 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16643 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16644 && last_unchanged_at_beg_row > w->current_matrix->rows)
16645 --last_unchanged_at_beg_row;
16647 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16648 GIVE_UP (17);
16650 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16651 GIVE_UP (18);
16652 start_pos = it.current.pos;
16654 /* Start displaying new lines in the desired matrix at the same
16655 vpos we would use in the current matrix, i.e. below
16656 last_unchanged_at_beg_row. */
16657 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16658 current_matrix);
16659 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16660 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16662 xassert (it.hpos == 0 && it.current_x == 0);
16664 else
16666 /* There are no reusable lines at the start of the window.
16667 Start displaying in the first text line. */
16668 start_display (&it, w, start);
16669 it.vpos = it.first_vpos;
16670 start_pos = it.current.pos;
16673 /* Find the first row that is not affected by changes at the end of
16674 the buffer. Value will be null if there is no unchanged row, in
16675 which case we must redisplay to the end of the window. delta
16676 will be set to the value by which buffer positions beginning with
16677 first_unchanged_at_end_row have to be adjusted due to text
16678 changes. */
16679 first_unchanged_at_end_row
16680 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16681 IF_DEBUG (debug_delta = delta);
16682 IF_DEBUG (debug_delta_bytes = delta_bytes);
16684 /* Set stop_pos to the buffer position up to which we will have to
16685 display new lines. If first_unchanged_at_end_row != NULL, this
16686 is the buffer position of the start of the line displayed in that
16687 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16688 that we don't stop at a buffer position. */
16689 stop_pos = 0;
16690 if (first_unchanged_at_end_row)
16692 xassert (last_unchanged_at_beg_row == NULL
16693 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16695 /* If this is a continuation line, move forward to the next one
16696 that isn't. Changes in lines above affect this line.
16697 Caution: this may move first_unchanged_at_end_row to a row
16698 not displaying text. */
16699 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16700 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16701 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16702 < it.last_visible_y))
16703 ++first_unchanged_at_end_row;
16705 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16706 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16707 >= it.last_visible_y))
16708 first_unchanged_at_end_row = NULL;
16709 else
16711 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16712 + delta);
16713 first_unchanged_at_end_vpos
16714 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16715 xassert (stop_pos >= Z - END_UNCHANGED);
16718 else if (last_unchanged_at_beg_row == NULL)
16719 GIVE_UP (19);
16722 #if GLYPH_DEBUG
16724 /* Either there is no unchanged row at the end, or the one we have
16725 now displays text. This is a necessary condition for the window
16726 end pos calculation at the end of this function. */
16727 xassert (first_unchanged_at_end_row == NULL
16728 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16730 debug_last_unchanged_at_beg_vpos
16731 = (last_unchanged_at_beg_row
16732 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16733 : -1);
16734 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16736 #endif /* GLYPH_DEBUG != 0 */
16739 /* Display new lines. Set last_text_row to the last new line
16740 displayed which has text on it, i.e. might end up as being the
16741 line where the window_end_vpos is. */
16742 w->cursor.vpos = -1;
16743 last_text_row = NULL;
16744 overlay_arrow_seen = 0;
16745 while (it.current_y < it.last_visible_y
16746 && !fonts_changed_p
16747 && (first_unchanged_at_end_row == NULL
16748 || IT_CHARPOS (it) < stop_pos))
16750 if (display_line (&it))
16751 last_text_row = it.glyph_row - 1;
16754 if (fonts_changed_p)
16755 return -1;
16758 /* Compute differences in buffer positions, y-positions etc. for
16759 lines reused at the bottom of the window. Compute what we can
16760 scroll. */
16761 if (first_unchanged_at_end_row
16762 /* No lines reused because we displayed everything up to the
16763 bottom of the window. */
16764 && it.current_y < it.last_visible_y)
16766 dvpos = (it.vpos
16767 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16768 current_matrix));
16769 dy = it.current_y - first_unchanged_at_end_row->y;
16770 run.current_y = first_unchanged_at_end_row->y;
16771 run.desired_y = run.current_y + dy;
16772 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16774 else
16776 delta = delta_bytes = dvpos = dy
16777 = run.current_y = run.desired_y = run.height = 0;
16778 first_unchanged_at_end_row = NULL;
16780 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16783 /* Find the cursor if not already found. We have to decide whether
16784 PT will appear on this window (it sometimes doesn't, but this is
16785 not a very frequent case.) This decision has to be made before
16786 the current matrix is altered. A value of cursor.vpos < 0 means
16787 that PT is either in one of the lines beginning at
16788 first_unchanged_at_end_row or below the window. Don't care for
16789 lines that might be displayed later at the window end; as
16790 mentioned, this is not a frequent case. */
16791 if (w->cursor.vpos < 0)
16793 /* Cursor in unchanged rows at the top? */
16794 if (PT < CHARPOS (start_pos)
16795 && last_unchanged_at_beg_row)
16797 row = row_containing_pos (w, PT,
16798 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16799 last_unchanged_at_beg_row + 1, 0);
16800 if (row)
16801 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16804 /* Start from first_unchanged_at_end_row looking for PT. */
16805 else if (first_unchanged_at_end_row)
16807 row = row_containing_pos (w, PT - delta,
16808 first_unchanged_at_end_row, NULL, 0);
16809 if (row)
16810 set_cursor_from_row (w, row, w->current_matrix, delta,
16811 delta_bytes, dy, dvpos);
16814 /* Give up if cursor was not found. */
16815 if (w->cursor.vpos < 0)
16817 clear_glyph_matrix (w->desired_matrix);
16818 return -1;
16822 /* Don't let the cursor end in the scroll margins. */
16824 int this_scroll_margin, cursor_height;
16826 this_scroll_margin = max (0, scroll_margin);
16827 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16828 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16829 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16831 if ((w->cursor.y < this_scroll_margin
16832 && CHARPOS (start) > BEGV)
16833 /* Old redisplay didn't take scroll margin into account at the bottom,
16834 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16835 || (w->cursor.y + (make_cursor_line_fully_visible_p
16836 ? cursor_height + this_scroll_margin
16837 : 1)) > it.last_visible_y)
16839 w->cursor.vpos = -1;
16840 clear_glyph_matrix (w->desired_matrix);
16841 return -1;
16845 /* Scroll the display. Do it before changing the current matrix so
16846 that xterm.c doesn't get confused about where the cursor glyph is
16847 found. */
16848 if (dy && run.height)
16850 update_begin (f);
16852 if (FRAME_WINDOW_P (f))
16854 FRAME_RIF (f)->update_window_begin_hook (w);
16855 FRAME_RIF (f)->clear_window_mouse_face (w);
16856 FRAME_RIF (f)->scroll_run_hook (w, &run);
16857 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16859 else
16861 /* Terminal frame. In this case, dvpos gives the number of
16862 lines to scroll by; dvpos < 0 means scroll up. */
16863 int from_vpos
16864 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16865 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16866 int end = (WINDOW_TOP_EDGE_LINE (w)
16867 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16868 + window_internal_height (w));
16870 #if defined (HAVE_GPM) || defined (MSDOS)
16871 x_clear_window_mouse_face (w);
16872 #endif
16873 /* Perform the operation on the screen. */
16874 if (dvpos > 0)
16876 /* Scroll last_unchanged_at_beg_row to the end of the
16877 window down dvpos lines. */
16878 set_terminal_window (f, end);
16880 /* On dumb terminals delete dvpos lines at the end
16881 before inserting dvpos empty lines. */
16882 if (!FRAME_SCROLL_REGION_OK (f))
16883 ins_del_lines (f, end - dvpos, -dvpos);
16885 /* Insert dvpos empty lines in front of
16886 last_unchanged_at_beg_row. */
16887 ins_del_lines (f, from, dvpos);
16889 else if (dvpos < 0)
16891 /* Scroll up last_unchanged_at_beg_vpos to the end of
16892 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16893 set_terminal_window (f, end);
16895 /* Delete dvpos lines in front of
16896 last_unchanged_at_beg_vpos. ins_del_lines will set
16897 the cursor to the given vpos and emit |dvpos| delete
16898 line sequences. */
16899 ins_del_lines (f, from + dvpos, dvpos);
16901 /* On a dumb terminal insert dvpos empty lines at the
16902 end. */
16903 if (!FRAME_SCROLL_REGION_OK (f))
16904 ins_del_lines (f, end + dvpos, -dvpos);
16907 set_terminal_window (f, 0);
16910 update_end (f);
16913 /* Shift reused rows of the current matrix to the right position.
16914 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16915 text. */
16916 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16917 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16918 if (dvpos < 0)
16920 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16921 bottom_vpos, dvpos);
16922 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16923 bottom_vpos, 0);
16925 else if (dvpos > 0)
16927 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16928 bottom_vpos, dvpos);
16929 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16930 first_unchanged_at_end_vpos + dvpos, 0);
16933 /* For frame-based redisplay, make sure that current frame and window
16934 matrix are in sync with respect to glyph memory. */
16935 if (!FRAME_WINDOW_P (f))
16936 sync_frame_with_window_matrix_rows (w);
16938 /* Adjust buffer positions in reused rows. */
16939 if (delta || delta_bytes)
16940 increment_matrix_positions (current_matrix,
16941 first_unchanged_at_end_vpos + dvpos,
16942 bottom_vpos, delta, delta_bytes);
16944 /* Adjust Y positions. */
16945 if (dy)
16946 shift_glyph_matrix (w, current_matrix,
16947 first_unchanged_at_end_vpos + dvpos,
16948 bottom_vpos, dy);
16950 if (first_unchanged_at_end_row)
16952 first_unchanged_at_end_row += dvpos;
16953 if (first_unchanged_at_end_row->y >= it.last_visible_y
16954 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16955 first_unchanged_at_end_row = NULL;
16958 /* If scrolling up, there may be some lines to display at the end of
16959 the window. */
16960 last_text_row_at_end = NULL;
16961 if (dy < 0)
16963 /* Scrolling up can leave for example a partially visible line
16964 at the end of the window to be redisplayed. */
16965 /* Set last_row to the glyph row in the current matrix where the
16966 window end line is found. It has been moved up or down in
16967 the matrix by dvpos. */
16968 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16969 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16971 /* If last_row is the window end line, it should display text. */
16972 xassert (last_row->displays_text_p);
16974 /* If window end line was partially visible before, begin
16975 displaying at that line. Otherwise begin displaying with the
16976 line following it. */
16977 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16979 init_to_row_start (&it, w, last_row);
16980 it.vpos = last_vpos;
16981 it.current_y = last_row->y;
16983 else
16985 init_to_row_end (&it, w, last_row);
16986 it.vpos = 1 + last_vpos;
16987 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16988 ++last_row;
16991 /* We may start in a continuation line. If so, we have to
16992 get the right continuation_lines_width and current_x. */
16993 it.continuation_lines_width = last_row->continuation_lines_width;
16994 it.hpos = it.current_x = 0;
16996 /* Display the rest of the lines at the window end. */
16997 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16998 while (it.current_y < it.last_visible_y
16999 && !fonts_changed_p)
17001 /* Is it always sure that the display agrees with lines in
17002 the current matrix? I don't think so, so we mark rows
17003 displayed invalid in the current matrix by setting their
17004 enabled_p flag to zero. */
17005 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17006 if (display_line (&it))
17007 last_text_row_at_end = it.glyph_row - 1;
17011 /* Update window_end_pos and window_end_vpos. */
17012 if (first_unchanged_at_end_row
17013 && !last_text_row_at_end)
17015 /* Window end line if one of the preserved rows from the current
17016 matrix. Set row to the last row displaying text in current
17017 matrix starting at first_unchanged_at_end_row, after
17018 scrolling. */
17019 xassert (first_unchanged_at_end_row->displays_text_p);
17020 row = find_last_row_displaying_text (w->current_matrix, &it,
17021 first_unchanged_at_end_row);
17022 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17024 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17025 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17026 w->window_end_vpos
17027 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17028 xassert (w->window_end_bytepos >= 0);
17029 IF_DEBUG (debug_method_add (w, "A"));
17031 else if (last_text_row_at_end)
17033 w->window_end_pos
17034 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17035 w->window_end_bytepos
17036 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17037 w->window_end_vpos
17038 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17039 xassert (w->window_end_bytepos >= 0);
17040 IF_DEBUG (debug_method_add (w, "B"));
17042 else if (last_text_row)
17044 /* We have displayed either to the end of the window or at the
17045 end of the window, i.e. the last row with text is to be found
17046 in the desired matrix. */
17047 w->window_end_pos
17048 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17049 w->window_end_bytepos
17050 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17051 w->window_end_vpos
17052 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17053 xassert (w->window_end_bytepos >= 0);
17055 else if (first_unchanged_at_end_row == NULL
17056 && last_text_row == NULL
17057 && last_text_row_at_end == NULL)
17059 /* Displayed to end of window, but no line containing text was
17060 displayed. Lines were deleted at the end of the window. */
17061 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17062 int vpos = XFASTINT (w->window_end_vpos);
17063 struct glyph_row *current_row = current_matrix->rows + vpos;
17064 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17066 for (row = NULL;
17067 row == NULL && vpos >= first_vpos;
17068 --vpos, --current_row, --desired_row)
17070 if (desired_row->enabled_p)
17072 if (desired_row->displays_text_p)
17073 row = desired_row;
17075 else if (current_row->displays_text_p)
17076 row = current_row;
17079 xassert (row != NULL);
17080 w->window_end_vpos = make_number (vpos + 1);
17081 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17082 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17083 xassert (w->window_end_bytepos >= 0);
17084 IF_DEBUG (debug_method_add (w, "C"));
17086 else
17087 abort ();
17089 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17090 debug_end_vpos = XFASTINT (w->window_end_vpos));
17092 /* Record that display has not been completed. */
17093 w->window_end_valid = Qnil;
17094 w->desired_matrix->no_scrolling_p = 1;
17095 return 3;
17097 #undef GIVE_UP
17102 /***********************************************************************
17103 More debugging support
17104 ***********************************************************************/
17106 #if GLYPH_DEBUG
17108 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17109 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17110 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17113 /* Dump the contents of glyph matrix MATRIX on stderr.
17115 GLYPHS 0 means don't show glyph contents.
17116 GLYPHS 1 means show glyphs in short form
17117 GLYPHS > 1 means show glyphs in long form. */
17119 void
17120 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17122 int i;
17123 for (i = 0; i < matrix->nrows; ++i)
17124 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17128 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17129 the glyph row and area where the glyph comes from. */
17131 void
17132 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17134 if (glyph->type == CHAR_GLYPH)
17136 fprintf (stderr,
17137 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17138 glyph - row->glyphs[TEXT_AREA],
17139 'C',
17140 glyph->charpos,
17141 (BUFFERP (glyph->object)
17142 ? 'B'
17143 : (STRINGP (glyph->object)
17144 ? 'S'
17145 : '-')),
17146 glyph->pixel_width,
17147 glyph->u.ch,
17148 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17149 ? glyph->u.ch
17150 : '.'),
17151 glyph->face_id,
17152 glyph->left_box_line_p,
17153 glyph->right_box_line_p);
17155 else if (glyph->type == STRETCH_GLYPH)
17157 fprintf (stderr,
17158 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17159 glyph - row->glyphs[TEXT_AREA],
17160 'S',
17161 glyph->charpos,
17162 (BUFFERP (glyph->object)
17163 ? 'B'
17164 : (STRINGP (glyph->object)
17165 ? 'S'
17166 : '-')),
17167 glyph->pixel_width,
17169 '.',
17170 glyph->face_id,
17171 glyph->left_box_line_p,
17172 glyph->right_box_line_p);
17174 else if (glyph->type == IMAGE_GLYPH)
17176 fprintf (stderr,
17177 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17178 glyph - row->glyphs[TEXT_AREA],
17179 'I',
17180 glyph->charpos,
17181 (BUFFERP (glyph->object)
17182 ? 'B'
17183 : (STRINGP (glyph->object)
17184 ? 'S'
17185 : '-')),
17186 glyph->pixel_width,
17187 glyph->u.img_id,
17188 '.',
17189 glyph->face_id,
17190 glyph->left_box_line_p,
17191 glyph->right_box_line_p);
17193 else if (glyph->type == COMPOSITE_GLYPH)
17195 fprintf (stderr,
17196 " %5td %4c %6"pI"d %c %3d 0x%05x",
17197 glyph - row->glyphs[TEXT_AREA],
17198 '+',
17199 glyph->charpos,
17200 (BUFFERP (glyph->object)
17201 ? 'B'
17202 : (STRINGP (glyph->object)
17203 ? 'S'
17204 : '-')),
17205 glyph->pixel_width,
17206 glyph->u.cmp.id);
17207 if (glyph->u.cmp.automatic)
17208 fprintf (stderr,
17209 "[%d-%d]",
17210 glyph->slice.cmp.from, glyph->slice.cmp.to);
17211 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17212 glyph->face_id,
17213 glyph->left_box_line_p,
17214 glyph->right_box_line_p);
17219 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17220 GLYPHS 0 means don't show glyph contents.
17221 GLYPHS 1 means show glyphs in short form
17222 GLYPHS > 1 means show glyphs in long form. */
17224 void
17225 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17227 if (glyphs != 1)
17229 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17230 fprintf (stderr, "======================================================================\n");
17232 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17233 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17234 vpos,
17235 MATRIX_ROW_START_CHARPOS (row),
17236 MATRIX_ROW_END_CHARPOS (row),
17237 row->used[TEXT_AREA],
17238 row->contains_overlapping_glyphs_p,
17239 row->enabled_p,
17240 row->truncated_on_left_p,
17241 row->truncated_on_right_p,
17242 row->continued_p,
17243 MATRIX_ROW_CONTINUATION_LINE_P (row),
17244 row->displays_text_p,
17245 row->ends_at_zv_p,
17246 row->fill_line_p,
17247 row->ends_in_middle_of_char_p,
17248 row->starts_in_middle_of_char_p,
17249 row->mouse_face_p,
17250 row->x,
17251 row->y,
17252 row->pixel_width,
17253 row->height,
17254 row->visible_height,
17255 row->ascent,
17256 row->phys_ascent);
17257 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17258 row->end.overlay_string_index,
17259 row->continuation_lines_width);
17260 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17261 CHARPOS (row->start.string_pos),
17262 CHARPOS (row->end.string_pos));
17263 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17264 row->end.dpvec_index);
17267 if (glyphs > 1)
17269 int area;
17271 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17273 struct glyph *glyph = row->glyphs[area];
17274 struct glyph *glyph_end = glyph + row->used[area];
17276 /* Glyph for a line end in text. */
17277 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17278 ++glyph_end;
17280 if (glyph < glyph_end)
17281 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17283 for (; glyph < glyph_end; ++glyph)
17284 dump_glyph (row, glyph, area);
17287 else if (glyphs == 1)
17289 int area;
17291 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17293 char *s = (char *) alloca (row->used[area] + 1);
17294 int i;
17296 for (i = 0; i < row->used[area]; ++i)
17298 struct glyph *glyph = row->glyphs[area] + i;
17299 if (glyph->type == CHAR_GLYPH
17300 && glyph->u.ch < 0x80
17301 && glyph->u.ch >= ' ')
17302 s[i] = glyph->u.ch;
17303 else
17304 s[i] = '.';
17307 s[i] = '\0';
17308 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17314 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17315 Sdump_glyph_matrix, 0, 1, "p",
17316 doc: /* Dump the current matrix of the selected window to stderr.
17317 Shows contents of glyph row structures. With non-nil
17318 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17319 glyphs in short form, otherwise show glyphs in long form. */)
17320 (Lisp_Object glyphs)
17322 struct window *w = XWINDOW (selected_window);
17323 struct buffer *buffer = XBUFFER (w->buffer);
17325 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17326 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17327 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17328 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17329 fprintf (stderr, "=============================================\n");
17330 dump_glyph_matrix (w->current_matrix,
17331 NILP (glyphs) ? 0 : XINT (glyphs));
17332 return Qnil;
17336 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17337 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17338 (void)
17340 struct frame *f = XFRAME (selected_frame);
17341 dump_glyph_matrix (f->current_matrix, 1);
17342 return Qnil;
17346 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17347 doc: /* Dump glyph row ROW to stderr.
17348 GLYPH 0 means don't dump glyphs.
17349 GLYPH 1 means dump glyphs in short form.
17350 GLYPH > 1 or omitted means dump glyphs in long form. */)
17351 (Lisp_Object row, Lisp_Object glyphs)
17353 struct glyph_matrix *matrix;
17354 int vpos;
17356 CHECK_NUMBER (row);
17357 matrix = XWINDOW (selected_window)->current_matrix;
17358 vpos = XINT (row);
17359 if (vpos >= 0 && vpos < matrix->nrows)
17360 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17361 vpos,
17362 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17363 return Qnil;
17367 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17368 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17369 GLYPH 0 means don't dump glyphs.
17370 GLYPH 1 means dump glyphs in short form.
17371 GLYPH > 1 or omitted means dump glyphs in long form. */)
17372 (Lisp_Object row, Lisp_Object glyphs)
17374 struct frame *sf = SELECTED_FRAME ();
17375 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17376 int vpos;
17378 CHECK_NUMBER (row);
17379 vpos = XINT (row);
17380 if (vpos >= 0 && vpos < m->nrows)
17381 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17382 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17383 return Qnil;
17387 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17388 doc: /* Toggle tracing of redisplay.
17389 With ARG, turn tracing on if and only if ARG is positive. */)
17390 (Lisp_Object arg)
17392 if (NILP (arg))
17393 trace_redisplay_p = !trace_redisplay_p;
17394 else
17396 arg = Fprefix_numeric_value (arg);
17397 trace_redisplay_p = XINT (arg) > 0;
17400 return Qnil;
17404 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17405 doc: /* Like `format', but print result to stderr.
17406 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17407 (ptrdiff_t nargs, Lisp_Object *args)
17409 Lisp_Object s = Fformat (nargs, args);
17410 fprintf (stderr, "%s", SDATA (s));
17411 return Qnil;
17414 #endif /* GLYPH_DEBUG */
17418 /***********************************************************************
17419 Building Desired Matrix Rows
17420 ***********************************************************************/
17422 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17423 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17425 static struct glyph_row *
17426 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17428 struct frame *f = XFRAME (WINDOW_FRAME (w));
17429 struct buffer *buffer = XBUFFER (w->buffer);
17430 struct buffer *old = current_buffer;
17431 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17432 int arrow_len = SCHARS (overlay_arrow_string);
17433 const unsigned char *arrow_end = arrow_string + arrow_len;
17434 const unsigned char *p;
17435 struct it it;
17436 int multibyte_p;
17437 int n_glyphs_before;
17439 set_buffer_temp (buffer);
17440 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17441 it.glyph_row->used[TEXT_AREA] = 0;
17442 SET_TEXT_POS (it.position, 0, 0);
17444 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17445 p = arrow_string;
17446 while (p < arrow_end)
17448 Lisp_Object face, ilisp;
17450 /* Get the next character. */
17451 if (multibyte_p)
17452 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17453 else
17455 it.c = it.char_to_display = *p, it.len = 1;
17456 if (! ASCII_CHAR_P (it.c))
17457 it.char_to_display = BYTE8_TO_CHAR (it.c);
17459 p += it.len;
17461 /* Get its face. */
17462 ilisp = make_number (p - arrow_string);
17463 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17464 it.face_id = compute_char_face (f, it.char_to_display, face);
17466 /* Compute its width, get its glyphs. */
17467 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17468 SET_TEXT_POS (it.position, -1, -1);
17469 PRODUCE_GLYPHS (&it);
17471 /* If this character doesn't fit any more in the line, we have
17472 to remove some glyphs. */
17473 if (it.current_x > it.last_visible_x)
17475 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17476 break;
17480 set_buffer_temp (old);
17481 return it.glyph_row;
17485 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17486 glyphs are only inserted for terminal frames since we can't really
17487 win with truncation glyphs when partially visible glyphs are
17488 involved. Which glyphs to insert is determined by
17489 produce_special_glyphs. */
17491 static void
17492 insert_left_trunc_glyphs (struct it *it)
17494 struct it truncate_it;
17495 struct glyph *from, *end, *to, *toend;
17497 xassert (!FRAME_WINDOW_P (it->f));
17499 /* Get the truncation glyphs. */
17500 truncate_it = *it;
17501 truncate_it.current_x = 0;
17502 truncate_it.face_id = DEFAULT_FACE_ID;
17503 truncate_it.glyph_row = &scratch_glyph_row;
17504 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17505 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17506 truncate_it.object = make_number (0);
17507 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17509 /* Overwrite glyphs from IT with truncation glyphs. */
17510 if (!it->glyph_row->reversed_p)
17512 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17513 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17514 to = it->glyph_row->glyphs[TEXT_AREA];
17515 toend = to + it->glyph_row->used[TEXT_AREA];
17517 while (from < end)
17518 *to++ = *from++;
17520 /* There may be padding glyphs left over. Overwrite them too. */
17521 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17523 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17524 while (from < end)
17525 *to++ = *from++;
17528 if (to > toend)
17529 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17531 else
17533 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17534 that back to front. */
17535 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17536 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17537 toend = it->glyph_row->glyphs[TEXT_AREA];
17538 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17540 while (from >= end && to >= toend)
17541 *to-- = *from--;
17542 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17544 from =
17545 truncate_it.glyph_row->glyphs[TEXT_AREA]
17546 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17547 while (from >= end && to >= toend)
17548 *to-- = *from--;
17550 if (from >= end)
17552 /* Need to free some room before prepending additional
17553 glyphs. */
17554 int move_by = from - end + 1;
17555 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17556 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17558 for ( ; g >= g0; g--)
17559 g[move_by] = *g;
17560 while (from >= end)
17561 *to-- = *from--;
17562 it->glyph_row->used[TEXT_AREA] += move_by;
17568 /* Compute the pixel height and width of IT->glyph_row.
17570 Most of the time, ascent and height of a display line will be equal
17571 to the max_ascent and max_height values of the display iterator
17572 structure. This is not the case if
17574 1. We hit ZV without displaying anything. In this case, max_ascent
17575 and max_height will be zero.
17577 2. We have some glyphs that don't contribute to the line height.
17578 (The glyph row flag contributes_to_line_height_p is for future
17579 pixmap extensions).
17581 The first case is easily covered by using default values because in
17582 these cases, the line height does not really matter, except that it
17583 must not be zero. */
17585 static void
17586 compute_line_metrics (struct it *it)
17588 struct glyph_row *row = it->glyph_row;
17590 if (FRAME_WINDOW_P (it->f))
17592 int i, min_y, max_y;
17594 /* The line may consist of one space only, that was added to
17595 place the cursor on it. If so, the row's height hasn't been
17596 computed yet. */
17597 if (row->height == 0)
17599 if (it->max_ascent + it->max_descent == 0)
17600 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17601 row->ascent = it->max_ascent;
17602 row->height = it->max_ascent + it->max_descent;
17603 row->phys_ascent = it->max_phys_ascent;
17604 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17605 row->extra_line_spacing = it->max_extra_line_spacing;
17608 /* Compute the width of this line. */
17609 row->pixel_width = row->x;
17610 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17611 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17613 xassert (row->pixel_width >= 0);
17614 xassert (row->ascent >= 0 && row->height > 0);
17616 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17617 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17619 /* If first line's physical ascent is larger than its logical
17620 ascent, use the physical ascent, and make the row taller.
17621 This makes accented characters fully visible. */
17622 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17623 && row->phys_ascent > row->ascent)
17625 row->height += row->phys_ascent - row->ascent;
17626 row->ascent = row->phys_ascent;
17629 /* Compute how much of the line is visible. */
17630 row->visible_height = row->height;
17632 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17633 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17635 if (row->y < min_y)
17636 row->visible_height -= min_y - row->y;
17637 if (row->y + row->height > max_y)
17638 row->visible_height -= row->y + row->height - max_y;
17640 else
17642 row->pixel_width = row->used[TEXT_AREA];
17643 if (row->continued_p)
17644 row->pixel_width -= it->continuation_pixel_width;
17645 else if (row->truncated_on_right_p)
17646 row->pixel_width -= it->truncation_pixel_width;
17647 row->ascent = row->phys_ascent = 0;
17648 row->height = row->phys_height = row->visible_height = 1;
17649 row->extra_line_spacing = 0;
17652 /* Compute a hash code for this row. */
17654 int area, i;
17655 row->hash = 0;
17656 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17657 for (i = 0; i < row->used[area]; ++i)
17658 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17659 + row->glyphs[area][i].u.val
17660 + row->glyphs[area][i].face_id
17661 + row->glyphs[area][i].padding_p
17662 + (row->glyphs[area][i].type << 2));
17665 it->max_ascent = it->max_descent = 0;
17666 it->max_phys_ascent = it->max_phys_descent = 0;
17670 /* Append one space to the glyph row of iterator IT if doing a
17671 window-based redisplay. The space has the same face as
17672 IT->face_id. Value is non-zero if a space was added.
17674 This function is called to make sure that there is always one glyph
17675 at the end of a glyph row that the cursor can be set on under
17676 window-systems. (If there weren't such a glyph we would not know
17677 how wide and tall a box cursor should be displayed).
17679 At the same time this space let's a nicely handle clearing to the
17680 end of the line if the row ends in italic text. */
17682 static int
17683 append_space_for_newline (struct it *it, int default_face_p)
17685 if (FRAME_WINDOW_P (it->f))
17687 int n = it->glyph_row->used[TEXT_AREA];
17689 if (it->glyph_row->glyphs[TEXT_AREA] + n
17690 < it->glyph_row->glyphs[1 + TEXT_AREA])
17692 /* Save some values that must not be changed.
17693 Must save IT->c and IT->len because otherwise
17694 ITERATOR_AT_END_P wouldn't work anymore after
17695 append_space_for_newline has been called. */
17696 enum display_element_type saved_what = it->what;
17697 int saved_c = it->c, saved_len = it->len;
17698 int saved_char_to_display = it->char_to_display;
17699 int saved_x = it->current_x;
17700 int saved_face_id = it->face_id;
17701 struct text_pos saved_pos;
17702 Lisp_Object saved_object;
17703 struct face *face;
17705 saved_object = it->object;
17706 saved_pos = it->position;
17708 it->what = IT_CHARACTER;
17709 memset (&it->position, 0, sizeof it->position);
17710 it->object = make_number (0);
17711 it->c = it->char_to_display = ' ';
17712 it->len = 1;
17714 if (default_face_p)
17715 it->face_id = DEFAULT_FACE_ID;
17716 else if (it->face_before_selective_p)
17717 it->face_id = it->saved_face_id;
17718 face = FACE_FROM_ID (it->f, it->face_id);
17719 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17721 PRODUCE_GLYPHS (it);
17723 it->override_ascent = -1;
17724 it->constrain_row_ascent_descent_p = 0;
17725 it->current_x = saved_x;
17726 it->object = saved_object;
17727 it->position = saved_pos;
17728 it->what = saved_what;
17729 it->face_id = saved_face_id;
17730 it->len = saved_len;
17731 it->c = saved_c;
17732 it->char_to_display = saved_char_to_display;
17733 return 1;
17737 return 0;
17741 /* Extend the face of the last glyph in the text area of IT->glyph_row
17742 to the end of the display line. Called from display_line. If the
17743 glyph row is empty, add a space glyph to it so that we know the
17744 face to draw. Set the glyph row flag fill_line_p. If the glyph
17745 row is R2L, prepend a stretch glyph to cover the empty space to the
17746 left of the leftmost glyph. */
17748 static void
17749 extend_face_to_end_of_line (struct it *it)
17751 struct face *face;
17752 struct frame *f = it->f;
17754 /* If line is already filled, do nothing. Non window-system frames
17755 get a grace of one more ``pixel'' because their characters are
17756 1-``pixel'' wide, so they hit the equality too early. This grace
17757 is needed only for R2L rows that are not continued, to produce
17758 one extra blank where we could display the cursor. */
17759 if (it->current_x >= it->last_visible_x
17760 + (!FRAME_WINDOW_P (f)
17761 && it->glyph_row->reversed_p
17762 && !it->glyph_row->continued_p))
17763 return;
17765 /* Face extension extends the background and box of IT->face_id
17766 to the end of the line. If the background equals the background
17767 of the frame, we don't have to do anything. */
17768 if (it->face_before_selective_p)
17769 face = FACE_FROM_ID (f, it->saved_face_id);
17770 else
17771 face = FACE_FROM_ID (f, it->face_id);
17773 if (FRAME_WINDOW_P (f)
17774 && it->glyph_row->displays_text_p
17775 && face->box == FACE_NO_BOX
17776 && face->background == FRAME_BACKGROUND_PIXEL (f)
17777 && !face->stipple
17778 && !it->glyph_row->reversed_p)
17779 return;
17781 /* Set the glyph row flag indicating that the face of the last glyph
17782 in the text area has to be drawn to the end of the text area. */
17783 it->glyph_row->fill_line_p = 1;
17785 /* If current character of IT is not ASCII, make sure we have the
17786 ASCII face. This will be automatically undone the next time
17787 get_next_display_element returns a multibyte character. Note
17788 that the character will always be single byte in unibyte
17789 text. */
17790 if (!ASCII_CHAR_P (it->c))
17792 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17795 if (FRAME_WINDOW_P (f))
17797 /* If the row is empty, add a space with the current face of IT,
17798 so that we know which face to draw. */
17799 if (it->glyph_row->used[TEXT_AREA] == 0)
17801 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17802 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17803 it->glyph_row->used[TEXT_AREA] = 1;
17805 #ifdef HAVE_WINDOW_SYSTEM
17806 if (it->glyph_row->reversed_p)
17808 /* Prepend a stretch glyph to the row, such that the
17809 rightmost glyph will be drawn flushed all the way to the
17810 right margin of the window. The stretch glyph that will
17811 occupy the empty space, if any, to the left of the
17812 glyphs. */
17813 struct font *font = face->font ? face->font : FRAME_FONT (f);
17814 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17815 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17816 struct glyph *g;
17817 int row_width, stretch_ascent, stretch_width;
17818 struct text_pos saved_pos;
17819 int saved_face_id, saved_avoid_cursor;
17821 for (row_width = 0, g = row_start; g < row_end; g++)
17822 row_width += g->pixel_width;
17823 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17824 if (stretch_width > 0)
17826 stretch_ascent =
17827 (((it->ascent + it->descent)
17828 * FONT_BASE (font)) / FONT_HEIGHT (font));
17829 saved_pos = it->position;
17830 memset (&it->position, 0, sizeof it->position);
17831 saved_avoid_cursor = it->avoid_cursor_p;
17832 it->avoid_cursor_p = 1;
17833 saved_face_id = it->face_id;
17834 /* The last row's stretch glyph should get the default
17835 face, to avoid painting the rest of the window with
17836 the region face, if the region ends at ZV. */
17837 if (it->glyph_row->ends_at_zv_p)
17838 it->face_id = DEFAULT_FACE_ID;
17839 else
17840 it->face_id = face->id;
17841 append_stretch_glyph (it, make_number (0), stretch_width,
17842 it->ascent + it->descent, stretch_ascent);
17843 it->position = saved_pos;
17844 it->avoid_cursor_p = saved_avoid_cursor;
17845 it->face_id = saved_face_id;
17848 #endif /* HAVE_WINDOW_SYSTEM */
17850 else
17852 /* Save some values that must not be changed. */
17853 int saved_x = it->current_x;
17854 struct text_pos saved_pos;
17855 Lisp_Object saved_object;
17856 enum display_element_type saved_what = it->what;
17857 int saved_face_id = it->face_id;
17859 saved_object = it->object;
17860 saved_pos = it->position;
17862 it->what = IT_CHARACTER;
17863 memset (&it->position, 0, sizeof it->position);
17864 it->object = make_number (0);
17865 it->c = it->char_to_display = ' ';
17866 it->len = 1;
17867 /* The last row's blank glyphs should get the default face, to
17868 avoid painting the rest of the window with the region face,
17869 if the region ends at ZV. */
17870 if (it->glyph_row->ends_at_zv_p)
17871 it->face_id = DEFAULT_FACE_ID;
17872 else
17873 it->face_id = face->id;
17875 PRODUCE_GLYPHS (it);
17877 while (it->current_x <= it->last_visible_x)
17878 PRODUCE_GLYPHS (it);
17880 /* Don't count these blanks really. It would let us insert a left
17881 truncation glyph below and make us set the cursor on them, maybe. */
17882 it->current_x = saved_x;
17883 it->object = saved_object;
17884 it->position = saved_pos;
17885 it->what = saved_what;
17886 it->face_id = saved_face_id;
17891 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17892 trailing whitespace. */
17894 static int
17895 trailing_whitespace_p (EMACS_INT charpos)
17897 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17898 int c = 0;
17900 while (bytepos < ZV_BYTE
17901 && (c = FETCH_CHAR (bytepos),
17902 c == ' ' || c == '\t'))
17903 ++bytepos;
17905 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17907 if (bytepos != PT_BYTE)
17908 return 1;
17910 return 0;
17914 /* Highlight trailing whitespace, if any, in ROW. */
17916 static void
17917 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17919 int used = row->used[TEXT_AREA];
17921 if (used)
17923 struct glyph *start = row->glyphs[TEXT_AREA];
17924 struct glyph *glyph = start + used - 1;
17926 if (row->reversed_p)
17928 /* Right-to-left rows need to be processed in the opposite
17929 direction, so swap the edge pointers. */
17930 glyph = start;
17931 start = row->glyphs[TEXT_AREA] + used - 1;
17934 /* Skip over glyphs inserted to display the cursor at the
17935 end of a line, for extending the face of the last glyph
17936 to the end of the line on terminals, and for truncation
17937 and continuation glyphs. */
17938 if (!row->reversed_p)
17940 while (glyph >= start
17941 && glyph->type == CHAR_GLYPH
17942 && INTEGERP (glyph->object))
17943 --glyph;
17945 else
17947 while (glyph <= start
17948 && glyph->type == CHAR_GLYPH
17949 && INTEGERP (glyph->object))
17950 ++glyph;
17953 /* If last glyph is a space or stretch, and it's trailing
17954 whitespace, set the face of all trailing whitespace glyphs in
17955 IT->glyph_row to `trailing-whitespace'. */
17956 if ((row->reversed_p ? glyph <= start : glyph >= start)
17957 && BUFFERP (glyph->object)
17958 && (glyph->type == STRETCH_GLYPH
17959 || (glyph->type == CHAR_GLYPH
17960 && glyph->u.ch == ' '))
17961 && trailing_whitespace_p (glyph->charpos))
17963 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17964 if (face_id < 0)
17965 return;
17967 if (!row->reversed_p)
17969 while (glyph >= start
17970 && BUFFERP (glyph->object)
17971 && (glyph->type == STRETCH_GLYPH
17972 || (glyph->type == CHAR_GLYPH
17973 && glyph->u.ch == ' ')))
17974 (glyph--)->face_id = face_id;
17976 else
17978 while (glyph <= start
17979 && BUFFERP (glyph->object)
17980 && (glyph->type == STRETCH_GLYPH
17981 || (glyph->type == CHAR_GLYPH
17982 && glyph->u.ch == ' ')))
17983 (glyph++)->face_id = face_id;
17990 /* Value is non-zero if glyph row ROW should be
17991 used to hold the cursor. */
17993 static int
17994 cursor_row_p (struct glyph_row *row)
17996 int result = 1;
17998 if (PT == CHARPOS (row->end.pos))
18000 /* Suppose the row ends on a string.
18001 Unless the row is continued, that means it ends on a newline
18002 in the string. If it's anything other than a display string
18003 (e.g. a before-string from an overlay), we don't want the
18004 cursor there. (This heuristic seems to give the optimal
18005 behavior for the various types of multi-line strings.) */
18006 if (CHARPOS (row->end.string_pos) >= 0)
18008 if (row->continued_p)
18009 result = 1;
18010 else
18012 /* Check for `display' property. */
18013 struct glyph *beg = row->glyphs[TEXT_AREA];
18014 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18015 struct glyph *glyph;
18017 result = 0;
18018 for (glyph = end; glyph >= beg; --glyph)
18019 if (STRINGP (glyph->object))
18021 Lisp_Object prop
18022 = Fget_char_property (make_number (PT),
18023 Qdisplay, Qnil);
18024 result =
18025 (!NILP (prop)
18026 && display_prop_string_p (prop, glyph->object));
18027 break;
18031 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18033 /* If the row ends in middle of a real character,
18034 and the line is continued, we want the cursor here.
18035 That's because CHARPOS (ROW->end.pos) would equal
18036 PT if PT is before the character. */
18037 if (!row->ends_in_ellipsis_p)
18038 result = row->continued_p;
18039 else
18040 /* If the row ends in an ellipsis, then
18041 CHARPOS (ROW->end.pos) will equal point after the
18042 invisible text. We want that position to be displayed
18043 after the ellipsis. */
18044 result = 0;
18046 /* If the row ends at ZV, display the cursor at the end of that
18047 row instead of at the start of the row below. */
18048 else if (row->ends_at_zv_p)
18049 result = 1;
18050 else
18051 result = 0;
18054 return result;
18059 /* Push the property PROP so that it will be rendered at the current
18060 position in IT. Return 1 if PROP was successfully pushed, 0
18061 otherwise. Called from handle_line_prefix to handle the
18062 `line-prefix' and `wrap-prefix' properties. */
18064 static int
18065 push_display_prop (struct it *it, Lisp_Object prop)
18067 struct text_pos pos =
18068 (it->method == GET_FROM_STRING) ? it->current.string_pos : it->current.pos;
18070 xassert (it->method == GET_FROM_BUFFER
18071 || it->method == GET_FROM_STRING);
18073 /* We need to save the current buffer/string position, so it will be
18074 restored by pop_it, because iterate_out_of_display_property
18075 depends on that being set correctly, but some situations leave
18076 it->position not yet set when this function is called. */
18077 push_it (it, &pos);
18079 if (STRINGP (prop))
18081 if (SCHARS (prop) == 0)
18083 pop_it (it);
18084 return 0;
18087 it->string = prop;
18088 it->multibyte_p = STRING_MULTIBYTE (it->string);
18089 it->current.overlay_string_index = -1;
18090 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18091 it->end_charpos = it->string_nchars = SCHARS (it->string);
18092 it->method = GET_FROM_STRING;
18093 it->stop_charpos = 0;
18094 it->prev_stop = 0;
18095 it->base_level_stop = 0;
18097 /* Force paragraph direction to be that of the parent
18098 buffer/string. */
18099 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18100 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18101 else
18102 it->paragraph_embedding = L2R;
18104 /* Set up the bidi iterator for this display string. */
18105 if (it->bidi_p)
18107 it->bidi_it.string.lstring = it->string;
18108 it->bidi_it.string.s = NULL;
18109 it->bidi_it.string.schars = it->end_charpos;
18110 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18111 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18112 it->bidi_it.string.unibyte = !it->multibyte_p;
18113 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18116 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18118 it->method = GET_FROM_STRETCH;
18119 it->object = prop;
18121 #ifdef HAVE_WINDOW_SYSTEM
18122 else if (IMAGEP (prop))
18124 it->what = IT_IMAGE;
18125 it->image_id = lookup_image (it->f, prop);
18126 it->method = GET_FROM_IMAGE;
18128 #endif /* HAVE_WINDOW_SYSTEM */
18129 else
18131 pop_it (it); /* bogus display property, give up */
18132 return 0;
18135 return 1;
18138 /* Return the character-property PROP at the current position in IT. */
18140 static Lisp_Object
18141 get_it_property (struct it *it, Lisp_Object prop)
18143 Lisp_Object position;
18145 if (STRINGP (it->object))
18146 position = make_number (IT_STRING_CHARPOS (*it));
18147 else if (BUFFERP (it->object))
18148 position = make_number (IT_CHARPOS (*it));
18149 else
18150 return Qnil;
18152 return Fget_char_property (position, prop, it->object);
18155 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18157 static void
18158 handle_line_prefix (struct it *it)
18160 Lisp_Object prefix;
18162 if (it->continuation_lines_width > 0)
18164 prefix = get_it_property (it, Qwrap_prefix);
18165 if (NILP (prefix))
18166 prefix = Vwrap_prefix;
18168 else
18170 prefix = get_it_property (it, Qline_prefix);
18171 if (NILP (prefix))
18172 prefix = Vline_prefix;
18174 if (! NILP (prefix) && push_display_prop (it, prefix))
18176 /* If the prefix is wider than the window, and we try to wrap
18177 it, it would acquire its own wrap prefix, and so on till the
18178 iterator stack overflows. So, don't wrap the prefix. */
18179 it->line_wrap = TRUNCATE;
18180 it->avoid_cursor_p = 1;
18186 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18187 only for R2L lines from display_line and display_string, when they
18188 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18189 the line/string needs to be continued on the next glyph row. */
18190 static void
18191 unproduce_glyphs (struct it *it, int n)
18193 struct glyph *glyph, *end;
18195 xassert (it->glyph_row);
18196 xassert (it->glyph_row->reversed_p);
18197 xassert (it->area == TEXT_AREA);
18198 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18200 if (n > it->glyph_row->used[TEXT_AREA])
18201 n = it->glyph_row->used[TEXT_AREA];
18202 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18203 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18204 for ( ; glyph < end; glyph++)
18205 glyph[-n] = *glyph;
18208 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18209 and ROW->maxpos. */
18210 static void
18211 find_row_edges (struct it *it, struct glyph_row *row,
18212 EMACS_INT min_pos, EMACS_INT min_bpos,
18213 EMACS_INT max_pos, EMACS_INT max_bpos)
18215 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18216 lines' rows is implemented for bidi-reordered rows. */
18218 /* ROW->minpos is the value of min_pos, the minimal buffer position
18219 we have in ROW, or ROW->start.pos if that is smaller. */
18220 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18221 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18222 else
18223 /* We didn't find buffer positions smaller than ROW->start, or
18224 didn't find _any_ valid buffer positions in any of the glyphs,
18225 so we must trust the iterator's computed positions. */
18226 row->minpos = row->start.pos;
18227 if (max_pos <= 0)
18229 max_pos = CHARPOS (it->current.pos);
18230 max_bpos = BYTEPOS (it->current.pos);
18233 /* Here are the various use-cases for ending the row, and the
18234 corresponding values for ROW->maxpos:
18236 Line ends in a newline from buffer eol_pos + 1
18237 Line is continued from buffer max_pos + 1
18238 Line is truncated on right it->current.pos
18239 Line ends in a newline from string max_pos
18240 Line is continued from string max_pos
18241 Line is continued from display vector max_pos
18242 Line is entirely from a string min_pos == max_pos
18243 Line is entirely from a display vector min_pos == max_pos
18244 Line that ends at ZV ZV
18246 If you discover other use-cases, please add them here as
18247 appropriate. */
18248 if (row->ends_at_zv_p)
18249 row->maxpos = it->current.pos;
18250 else if (row->used[TEXT_AREA])
18252 if (row->ends_in_newline_from_string_p)
18253 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18254 else if (CHARPOS (it->eol_pos) > 0)
18255 SET_TEXT_POS (row->maxpos,
18256 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18257 else if (row->continued_p)
18259 /* If max_pos is different from IT's current position, it
18260 means IT->method does not belong to the display element
18261 at max_pos. However, it also means that the display
18262 element at max_pos was displayed in its entirety on this
18263 line, which is equivalent to saying that the next line
18264 starts at the next buffer position. */
18265 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18266 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18267 else
18269 INC_BOTH (max_pos, max_bpos);
18270 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18273 else if (row->truncated_on_right_p)
18274 /* display_line already called reseat_at_next_visible_line_start,
18275 which puts the iterator at the beginning of the next line, in
18276 the logical order. */
18277 row->maxpos = it->current.pos;
18278 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18279 /* A line that is entirely from a string/image/stretch... */
18280 row->maxpos = row->minpos;
18281 else
18282 abort ();
18284 else
18285 row->maxpos = it->current.pos;
18288 /* Construct the glyph row IT->glyph_row in the desired matrix of
18289 IT->w from text at the current position of IT. See dispextern.h
18290 for an overview of struct it. Value is non-zero if
18291 IT->glyph_row displays text, as opposed to a line displaying ZV
18292 only. */
18294 static int
18295 display_line (struct it *it)
18297 struct glyph_row *row = it->glyph_row;
18298 Lisp_Object overlay_arrow_string;
18299 struct it wrap_it;
18300 void *wrap_data = NULL;
18301 int may_wrap = 0, wrap_x IF_LINT (= 0);
18302 int wrap_row_used = -1;
18303 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18304 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18305 int wrap_row_extra_line_spacing IF_LINT (= 0);
18306 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18307 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18308 int cvpos;
18309 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18310 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18312 /* We always start displaying at hpos zero even if hscrolled. */
18313 xassert (it->hpos == 0 && it->current_x == 0);
18315 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18316 >= it->w->desired_matrix->nrows)
18318 it->w->nrows_scale_factor++;
18319 fonts_changed_p = 1;
18320 return 0;
18323 /* Is IT->w showing the region? */
18324 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18326 /* Clear the result glyph row and enable it. */
18327 prepare_desired_row (row);
18329 row->y = it->current_y;
18330 row->start = it->start;
18331 row->continuation_lines_width = it->continuation_lines_width;
18332 row->displays_text_p = 1;
18333 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18334 it->starts_in_middle_of_char_p = 0;
18336 /* Arrange the overlays nicely for our purposes. Usually, we call
18337 display_line on only one line at a time, in which case this
18338 can't really hurt too much, or we call it on lines which appear
18339 one after another in the buffer, in which case all calls to
18340 recenter_overlay_lists but the first will be pretty cheap. */
18341 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18343 /* Move over display elements that are not visible because we are
18344 hscrolled. This may stop at an x-position < IT->first_visible_x
18345 if the first glyph is partially visible or if we hit a line end. */
18346 if (it->current_x < it->first_visible_x)
18348 this_line_min_pos = row->start.pos;
18349 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18350 MOVE_TO_POS | MOVE_TO_X);
18351 /* Record the smallest positions seen while we moved over
18352 display elements that are not visible. This is needed by
18353 redisplay_internal for optimizing the case where the cursor
18354 stays inside the same line. The rest of this function only
18355 considers positions that are actually displayed, so
18356 RECORD_MAX_MIN_POS will not otherwise record positions that
18357 are hscrolled to the left of the left edge of the window. */
18358 min_pos = CHARPOS (this_line_min_pos);
18359 min_bpos = BYTEPOS (this_line_min_pos);
18361 else
18363 /* We only do this when not calling `move_it_in_display_line_to'
18364 above, because move_it_in_display_line_to calls
18365 handle_line_prefix itself. */
18366 handle_line_prefix (it);
18369 /* Get the initial row height. This is either the height of the
18370 text hscrolled, if there is any, or zero. */
18371 row->ascent = it->max_ascent;
18372 row->height = it->max_ascent + it->max_descent;
18373 row->phys_ascent = it->max_phys_ascent;
18374 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18375 row->extra_line_spacing = it->max_extra_line_spacing;
18377 /* Utility macro to record max and min buffer positions seen until now. */
18378 #define RECORD_MAX_MIN_POS(IT) \
18379 do \
18381 if (IT_CHARPOS (*(IT)) < min_pos) \
18383 min_pos = IT_CHARPOS (*(IT)); \
18384 min_bpos = IT_BYTEPOS (*(IT)); \
18386 if (IT_CHARPOS (*(IT)) > max_pos) \
18388 max_pos = IT_CHARPOS (*(IT)); \
18389 max_bpos = IT_BYTEPOS (*(IT)); \
18392 while (0)
18394 /* Loop generating characters. The loop is left with IT on the next
18395 character to display. */
18396 while (1)
18398 int n_glyphs_before, hpos_before, x_before;
18399 int x, nglyphs;
18400 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18402 /* Retrieve the next thing to display. Value is zero if end of
18403 buffer reached. */
18404 if (!get_next_display_element (it))
18406 /* Maybe add a space at the end of this line that is used to
18407 display the cursor there under X. Set the charpos of the
18408 first glyph of blank lines not corresponding to any text
18409 to -1. */
18410 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18411 row->exact_window_width_line_p = 1;
18412 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18413 || row->used[TEXT_AREA] == 0)
18415 row->glyphs[TEXT_AREA]->charpos = -1;
18416 row->displays_text_p = 0;
18418 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18419 && (!MINI_WINDOW_P (it->w)
18420 || (minibuf_level && EQ (it->window, minibuf_window))))
18421 row->indicate_empty_line_p = 1;
18424 it->continuation_lines_width = 0;
18425 row->ends_at_zv_p = 1;
18426 /* A row that displays right-to-left text must always have
18427 its last face extended all the way to the end of line,
18428 even if this row ends in ZV, because we still write to
18429 the screen left to right. */
18430 if (row->reversed_p)
18431 extend_face_to_end_of_line (it);
18432 break;
18435 /* Now, get the metrics of what we want to display. This also
18436 generates glyphs in `row' (which is IT->glyph_row). */
18437 n_glyphs_before = row->used[TEXT_AREA];
18438 x = it->current_x;
18440 /* Remember the line height so far in case the next element doesn't
18441 fit on the line. */
18442 if (it->line_wrap != TRUNCATE)
18444 ascent = it->max_ascent;
18445 descent = it->max_descent;
18446 phys_ascent = it->max_phys_ascent;
18447 phys_descent = it->max_phys_descent;
18449 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18451 if (IT_DISPLAYING_WHITESPACE (it))
18452 may_wrap = 1;
18453 else if (may_wrap)
18455 SAVE_IT (wrap_it, *it, wrap_data);
18456 wrap_x = x;
18457 wrap_row_used = row->used[TEXT_AREA];
18458 wrap_row_ascent = row->ascent;
18459 wrap_row_height = row->height;
18460 wrap_row_phys_ascent = row->phys_ascent;
18461 wrap_row_phys_height = row->phys_height;
18462 wrap_row_extra_line_spacing = row->extra_line_spacing;
18463 wrap_row_min_pos = min_pos;
18464 wrap_row_min_bpos = min_bpos;
18465 wrap_row_max_pos = max_pos;
18466 wrap_row_max_bpos = max_bpos;
18467 may_wrap = 0;
18472 PRODUCE_GLYPHS (it);
18474 /* If this display element was in marginal areas, continue with
18475 the next one. */
18476 if (it->area != TEXT_AREA)
18478 row->ascent = max (row->ascent, it->max_ascent);
18479 row->height = max (row->height, it->max_ascent + it->max_descent);
18480 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18481 row->phys_height = max (row->phys_height,
18482 it->max_phys_ascent + it->max_phys_descent);
18483 row->extra_line_spacing = max (row->extra_line_spacing,
18484 it->max_extra_line_spacing);
18485 set_iterator_to_next (it, 1);
18486 continue;
18489 /* Does the display element fit on the line? If we truncate
18490 lines, we should draw past the right edge of the window. If
18491 we don't truncate, we want to stop so that we can display the
18492 continuation glyph before the right margin. If lines are
18493 continued, there are two possible strategies for characters
18494 resulting in more than 1 glyph (e.g. tabs): Display as many
18495 glyphs as possible in this line and leave the rest for the
18496 continuation line, or display the whole element in the next
18497 line. Original redisplay did the former, so we do it also. */
18498 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18499 hpos_before = it->hpos;
18500 x_before = x;
18502 if (/* Not a newline. */
18503 nglyphs > 0
18504 /* Glyphs produced fit entirely in the line. */
18505 && it->current_x < it->last_visible_x)
18507 it->hpos += nglyphs;
18508 row->ascent = max (row->ascent, it->max_ascent);
18509 row->height = max (row->height, it->max_ascent + it->max_descent);
18510 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18511 row->phys_height = max (row->phys_height,
18512 it->max_phys_ascent + it->max_phys_descent);
18513 row->extra_line_spacing = max (row->extra_line_spacing,
18514 it->max_extra_line_spacing);
18515 if (it->current_x - it->pixel_width < it->first_visible_x)
18516 row->x = x - it->first_visible_x;
18517 /* Record the maximum and minimum buffer positions seen so
18518 far in glyphs that will be displayed by this row. */
18519 if (it->bidi_p)
18520 RECORD_MAX_MIN_POS (it);
18522 else
18524 int i, new_x;
18525 struct glyph *glyph;
18527 for (i = 0; i < nglyphs; ++i, x = new_x)
18529 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18530 new_x = x + glyph->pixel_width;
18532 if (/* Lines are continued. */
18533 it->line_wrap != TRUNCATE
18534 && (/* Glyph doesn't fit on the line. */
18535 new_x > it->last_visible_x
18536 /* Or it fits exactly on a window system frame. */
18537 || (new_x == it->last_visible_x
18538 && FRAME_WINDOW_P (it->f))))
18540 /* End of a continued line. */
18542 if (it->hpos == 0
18543 || (new_x == it->last_visible_x
18544 && FRAME_WINDOW_P (it->f)))
18546 /* Current glyph is the only one on the line or
18547 fits exactly on the line. We must continue
18548 the line because we can't draw the cursor
18549 after the glyph. */
18550 row->continued_p = 1;
18551 it->current_x = new_x;
18552 it->continuation_lines_width += new_x;
18553 ++it->hpos;
18554 /* Record the maximum and minimum buffer
18555 positions seen so far in glyphs that will be
18556 displayed by this row. */
18557 if (it->bidi_p)
18558 RECORD_MAX_MIN_POS (it);
18559 if (i == nglyphs - 1)
18561 /* If line-wrap is on, check if a previous
18562 wrap point was found. */
18563 if (wrap_row_used > 0
18564 /* Even if there is a previous wrap
18565 point, continue the line here as
18566 usual, if (i) the previous character
18567 was a space or tab AND (ii) the
18568 current character is not. */
18569 && (!may_wrap
18570 || IT_DISPLAYING_WHITESPACE (it)))
18571 goto back_to_wrap;
18573 set_iterator_to_next (it, 1);
18574 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18576 if (!get_next_display_element (it))
18578 row->exact_window_width_line_p = 1;
18579 it->continuation_lines_width = 0;
18580 row->continued_p = 0;
18581 row->ends_at_zv_p = 1;
18583 else if (ITERATOR_AT_END_OF_LINE_P (it))
18585 row->continued_p = 0;
18586 row->exact_window_width_line_p = 1;
18591 else if (CHAR_GLYPH_PADDING_P (*glyph)
18592 && !FRAME_WINDOW_P (it->f))
18594 /* A padding glyph that doesn't fit on this line.
18595 This means the whole character doesn't fit
18596 on the line. */
18597 if (row->reversed_p)
18598 unproduce_glyphs (it, row->used[TEXT_AREA]
18599 - n_glyphs_before);
18600 row->used[TEXT_AREA] = n_glyphs_before;
18602 /* Fill the rest of the row with continuation
18603 glyphs like in 20.x. */
18604 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18605 < row->glyphs[1 + TEXT_AREA])
18606 produce_special_glyphs (it, IT_CONTINUATION);
18608 row->continued_p = 1;
18609 it->current_x = x_before;
18610 it->continuation_lines_width += x_before;
18612 /* Restore the height to what it was before the
18613 element not fitting on the line. */
18614 it->max_ascent = ascent;
18615 it->max_descent = descent;
18616 it->max_phys_ascent = phys_ascent;
18617 it->max_phys_descent = phys_descent;
18619 else if (wrap_row_used > 0)
18621 back_to_wrap:
18622 if (row->reversed_p)
18623 unproduce_glyphs (it,
18624 row->used[TEXT_AREA] - wrap_row_used);
18625 RESTORE_IT (it, &wrap_it, wrap_data);
18626 it->continuation_lines_width += wrap_x;
18627 row->used[TEXT_AREA] = wrap_row_used;
18628 row->ascent = wrap_row_ascent;
18629 row->height = wrap_row_height;
18630 row->phys_ascent = wrap_row_phys_ascent;
18631 row->phys_height = wrap_row_phys_height;
18632 row->extra_line_spacing = wrap_row_extra_line_spacing;
18633 min_pos = wrap_row_min_pos;
18634 min_bpos = wrap_row_min_bpos;
18635 max_pos = wrap_row_max_pos;
18636 max_bpos = wrap_row_max_bpos;
18637 row->continued_p = 1;
18638 row->ends_at_zv_p = 0;
18639 row->exact_window_width_line_p = 0;
18640 it->continuation_lines_width += x;
18642 /* Make sure that a non-default face is extended
18643 up to the right margin of the window. */
18644 extend_face_to_end_of_line (it);
18646 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18648 /* A TAB that extends past the right edge of the
18649 window. This produces a single glyph on
18650 window system frames. We leave the glyph in
18651 this row and let it fill the row, but don't
18652 consume the TAB. */
18653 it->continuation_lines_width += it->last_visible_x;
18654 row->ends_in_middle_of_char_p = 1;
18655 row->continued_p = 1;
18656 glyph->pixel_width = it->last_visible_x - x;
18657 it->starts_in_middle_of_char_p = 1;
18659 else
18661 /* Something other than a TAB that draws past
18662 the right edge of the window. Restore
18663 positions to values before the element. */
18664 if (row->reversed_p)
18665 unproduce_glyphs (it, row->used[TEXT_AREA]
18666 - (n_glyphs_before + i));
18667 row->used[TEXT_AREA] = n_glyphs_before + i;
18669 /* Display continuation glyphs. */
18670 if (!FRAME_WINDOW_P (it->f))
18671 produce_special_glyphs (it, IT_CONTINUATION);
18672 row->continued_p = 1;
18674 it->current_x = x_before;
18675 it->continuation_lines_width += x;
18676 extend_face_to_end_of_line (it);
18678 if (nglyphs > 1 && i > 0)
18680 row->ends_in_middle_of_char_p = 1;
18681 it->starts_in_middle_of_char_p = 1;
18684 /* Restore the height to what it was before the
18685 element not fitting on the line. */
18686 it->max_ascent = ascent;
18687 it->max_descent = descent;
18688 it->max_phys_ascent = phys_ascent;
18689 it->max_phys_descent = phys_descent;
18692 break;
18694 else if (new_x > it->first_visible_x)
18696 /* Increment number of glyphs actually displayed. */
18697 ++it->hpos;
18699 /* Record the maximum and minimum buffer positions
18700 seen so far in glyphs that will be displayed by
18701 this row. */
18702 if (it->bidi_p)
18703 RECORD_MAX_MIN_POS (it);
18705 if (x < it->first_visible_x)
18706 /* Glyph is partially visible, i.e. row starts at
18707 negative X position. */
18708 row->x = x - it->first_visible_x;
18710 else
18712 /* Glyph is completely off the left margin of the
18713 window. This should not happen because of the
18714 move_it_in_display_line at the start of this
18715 function, unless the text display area of the
18716 window is empty. */
18717 xassert (it->first_visible_x <= it->last_visible_x);
18721 row->ascent = max (row->ascent, it->max_ascent);
18722 row->height = max (row->height, it->max_ascent + it->max_descent);
18723 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18724 row->phys_height = max (row->phys_height,
18725 it->max_phys_ascent + it->max_phys_descent);
18726 row->extra_line_spacing = max (row->extra_line_spacing,
18727 it->max_extra_line_spacing);
18729 /* End of this display line if row is continued. */
18730 if (row->continued_p || row->ends_at_zv_p)
18731 break;
18734 at_end_of_line:
18735 /* Is this a line end? If yes, we're also done, after making
18736 sure that a non-default face is extended up to the right
18737 margin of the window. */
18738 if (ITERATOR_AT_END_OF_LINE_P (it))
18740 int used_before = row->used[TEXT_AREA];
18742 row->ends_in_newline_from_string_p = STRINGP (it->object);
18744 /* Add a space at the end of the line that is used to
18745 display the cursor there. */
18746 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18747 append_space_for_newline (it, 0);
18749 /* Extend the face to the end of the line. */
18750 extend_face_to_end_of_line (it);
18752 /* Make sure we have the position. */
18753 if (used_before == 0)
18754 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18756 /* Record the position of the newline, for use in
18757 find_row_edges. */
18758 it->eol_pos = it->current.pos;
18760 /* Consume the line end. This skips over invisible lines. */
18761 set_iterator_to_next (it, 1);
18762 it->continuation_lines_width = 0;
18763 break;
18766 /* Proceed with next display element. Note that this skips
18767 over lines invisible because of selective display. */
18768 set_iterator_to_next (it, 1);
18770 /* If we truncate lines, we are done when the last displayed
18771 glyphs reach past the right margin of the window. */
18772 if (it->line_wrap == TRUNCATE
18773 && (FRAME_WINDOW_P (it->f)
18774 ? (it->current_x >= it->last_visible_x)
18775 : (it->current_x > it->last_visible_x)))
18777 /* Maybe add truncation glyphs. */
18778 if (!FRAME_WINDOW_P (it->f))
18780 int i, n;
18782 if (!row->reversed_p)
18784 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18785 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18786 break;
18788 else
18790 for (i = 0; i < row->used[TEXT_AREA]; i++)
18791 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18792 break;
18793 /* Remove any padding glyphs at the front of ROW, to
18794 make room for the truncation glyphs we will be
18795 adding below. The loop below always inserts at
18796 least one truncation glyph, so also remove the
18797 last glyph added to ROW. */
18798 unproduce_glyphs (it, i + 1);
18799 /* Adjust i for the loop below. */
18800 i = row->used[TEXT_AREA] - (i + 1);
18803 for (n = row->used[TEXT_AREA]; i < n; ++i)
18805 row->used[TEXT_AREA] = i;
18806 produce_special_glyphs (it, IT_TRUNCATION);
18809 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18811 /* Don't truncate if we can overflow newline into fringe. */
18812 if (!get_next_display_element (it))
18814 it->continuation_lines_width = 0;
18815 row->ends_at_zv_p = 1;
18816 row->exact_window_width_line_p = 1;
18817 break;
18819 if (ITERATOR_AT_END_OF_LINE_P (it))
18821 row->exact_window_width_line_p = 1;
18822 goto at_end_of_line;
18826 row->truncated_on_right_p = 1;
18827 it->continuation_lines_width = 0;
18828 reseat_at_next_visible_line_start (it, 0);
18829 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18830 it->hpos = hpos_before;
18831 it->current_x = x_before;
18832 break;
18836 if (wrap_data)
18837 bidi_unshelve_cache (wrap_data, 1);
18839 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18840 at the left window margin. */
18841 if (it->first_visible_x
18842 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18844 if (!FRAME_WINDOW_P (it->f))
18845 insert_left_trunc_glyphs (it);
18846 row->truncated_on_left_p = 1;
18849 /* Remember the position at which this line ends.
18851 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18852 cannot be before the call to find_row_edges below, since that is
18853 where these positions are determined. */
18854 row->end = it->current;
18855 if (!it->bidi_p)
18857 row->minpos = row->start.pos;
18858 row->maxpos = row->end.pos;
18860 else
18862 /* ROW->minpos and ROW->maxpos must be the smallest and
18863 `1 + the largest' buffer positions in ROW. But if ROW was
18864 bidi-reordered, these two positions can be anywhere in the
18865 row, so we must determine them now. */
18866 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18869 /* If the start of this line is the overlay arrow-position, then
18870 mark this glyph row as the one containing the overlay arrow.
18871 This is clearly a mess with variable size fonts. It would be
18872 better to let it be displayed like cursors under X. */
18873 if ((row->displays_text_p || !overlay_arrow_seen)
18874 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18875 !NILP (overlay_arrow_string)))
18877 /* Overlay arrow in window redisplay is a fringe bitmap. */
18878 if (STRINGP (overlay_arrow_string))
18880 struct glyph_row *arrow_row
18881 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18882 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18883 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18884 struct glyph *p = row->glyphs[TEXT_AREA];
18885 struct glyph *p2, *end;
18887 /* Copy the arrow glyphs. */
18888 while (glyph < arrow_end)
18889 *p++ = *glyph++;
18891 /* Throw away padding glyphs. */
18892 p2 = p;
18893 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18894 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18895 ++p2;
18896 if (p2 > p)
18898 while (p2 < end)
18899 *p++ = *p2++;
18900 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18903 else
18905 xassert (INTEGERP (overlay_arrow_string));
18906 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18908 overlay_arrow_seen = 1;
18911 /* Compute pixel dimensions of this line. */
18912 compute_line_metrics (it);
18914 /* Record whether this row ends inside an ellipsis. */
18915 row->ends_in_ellipsis_p
18916 = (it->method == GET_FROM_DISPLAY_VECTOR
18917 && it->ellipsis_p);
18919 /* Save fringe bitmaps in this row. */
18920 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18921 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18922 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18923 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18925 it->left_user_fringe_bitmap = 0;
18926 it->left_user_fringe_face_id = 0;
18927 it->right_user_fringe_bitmap = 0;
18928 it->right_user_fringe_face_id = 0;
18930 /* Maybe set the cursor. */
18931 cvpos = it->w->cursor.vpos;
18932 if ((cvpos < 0
18933 /* In bidi-reordered rows, keep checking for proper cursor
18934 position even if one has been found already, because buffer
18935 positions in such rows change non-linearly with ROW->VPOS,
18936 when a line is continued. One exception: when we are at ZV,
18937 display cursor on the first suitable glyph row, since all
18938 the empty rows after that also have their position set to ZV. */
18939 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18940 lines' rows is implemented for bidi-reordered rows. */
18941 || (it->bidi_p
18942 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18943 && PT >= MATRIX_ROW_START_CHARPOS (row)
18944 && PT <= MATRIX_ROW_END_CHARPOS (row)
18945 && cursor_row_p (row))
18946 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18948 /* Highlight trailing whitespace. */
18949 if (!NILP (Vshow_trailing_whitespace))
18950 highlight_trailing_whitespace (it->f, it->glyph_row);
18952 /* Prepare for the next line. This line starts horizontally at (X
18953 HPOS) = (0 0). Vertical positions are incremented. As a
18954 convenience for the caller, IT->glyph_row is set to the next
18955 row to be used. */
18956 it->current_x = it->hpos = 0;
18957 it->current_y += row->height;
18958 SET_TEXT_POS (it->eol_pos, 0, 0);
18959 ++it->vpos;
18960 ++it->glyph_row;
18961 /* The next row should by default use the same value of the
18962 reversed_p flag as this one. set_iterator_to_next decides when
18963 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18964 the flag accordingly. */
18965 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18966 it->glyph_row->reversed_p = row->reversed_p;
18967 it->start = row->end;
18968 return row->displays_text_p;
18970 #undef RECORD_MAX_MIN_POS
18973 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18974 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18975 doc: /* Return paragraph direction at point in BUFFER.
18976 Value is either `left-to-right' or `right-to-left'.
18977 If BUFFER is omitted or nil, it defaults to the current buffer.
18979 Paragraph direction determines how the text in the paragraph is displayed.
18980 In left-to-right paragraphs, text begins at the left margin of the window
18981 and the reading direction is generally left to right. In right-to-left
18982 paragraphs, text begins at the right margin and is read from right to left.
18984 See also `bidi-paragraph-direction'. */)
18985 (Lisp_Object buffer)
18987 struct buffer *buf = current_buffer;
18988 struct buffer *old = buf;
18990 if (! NILP (buffer))
18992 CHECK_BUFFER (buffer);
18993 buf = XBUFFER (buffer);
18996 if (NILP (BVAR (buf, bidi_display_reordering)))
18997 return Qleft_to_right;
18998 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18999 return BVAR (buf, bidi_paragraph_direction);
19000 else
19002 /* Determine the direction from buffer text. We could try to
19003 use current_matrix if it is up to date, but this seems fast
19004 enough as it is. */
19005 struct bidi_it itb;
19006 EMACS_INT pos = BUF_PT (buf);
19007 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19008 int c;
19010 set_buffer_temp (buf);
19011 /* bidi_paragraph_init finds the base direction of the paragraph
19012 by searching forward from paragraph start. We need the base
19013 direction of the current or _previous_ paragraph, so we need
19014 to make sure we are within that paragraph. To that end, find
19015 the previous non-empty line. */
19016 if (pos >= ZV && pos > BEGV)
19018 pos--;
19019 bytepos = CHAR_TO_BYTE (pos);
19021 while ((c = FETCH_BYTE (bytepos)) == '\n'
19022 || c == ' ' || c == '\t' || c == '\f')
19024 if (bytepos <= BEGV_BYTE)
19025 break;
19026 bytepos--;
19027 pos--;
19029 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19030 bytepos--;
19031 itb.charpos = pos;
19032 itb.bytepos = bytepos;
19033 itb.nchars = -1;
19034 itb.string.s = NULL;
19035 itb.string.lstring = Qnil;
19036 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
19037 itb.first_elt = 1;
19038 itb.separator_limit = -1;
19039 itb.paragraph_dir = NEUTRAL_DIR;
19041 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19042 set_buffer_temp (old);
19043 switch (itb.paragraph_dir)
19045 case L2R:
19046 return Qleft_to_right;
19047 break;
19048 case R2L:
19049 return Qright_to_left;
19050 break;
19051 default:
19052 abort ();
19059 /***********************************************************************
19060 Menu Bar
19061 ***********************************************************************/
19063 /* Redisplay the menu bar in the frame for window W.
19065 The menu bar of X frames that don't have X toolkit support is
19066 displayed in a special window W->frame->menu_bar_window.
19068 The menu bar of terminal frames is treated specially as far as
19069 glyph matrices are concerned. Menu bar lines are not part of
19070 windows, so the update is done directly on the frame matrix rows
19071 for the menu bar. */
19073 static void
19074 display_menu_bar (struct window *w)
19076 struct frame *f = XFRAME (WINDOW_FRAME (w));
19077 struct it it;
19078 Lisp_Object items;
19079 int i;
19081 /* Don't do all this for graphical frames. */
19082 #ifdef HAVE_NTGUI
19083 if (FRAME_W32_P (f))
19084 return;
19085 #endif
19086 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19087 if (FRAME_X_P (f))
19088 return;
19089 #endif
19091 #ifdef HAVE_NS
19092 if (FRAME_NS_P (f))
19093 return;
19094 #endif /* HAVE_NS */
19096 #ifdef USE_X_TOOLKIT
19097 xassert (!FRAME_WINDOW_P (f));
19098 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19099 it.first_visible_x = 0;
19100 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19101 #else /* not USE_X_TOOLKIT */
19102 if (FRAME_WINDOW_P (f))
19104 /* Menu bar lines are displayed in the desired matrix of the
19105 dummy window menu_bar_window. */
19106 struct window *menu_w;
19107 xassert (WINDOWP (f->menu_bar_window));
19108 menu_w = XWINDOW (f->menu_bar_window);
19109 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19110 MENU_FACE_ID);
19111 it.first_visible_x = 0;
19112 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19114 else
19116 /* This is a TTY frame, i.e. character hpos/vpos are used as
19117 pixel x/y. */
19118 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19119 MENU_FACE_ID);
19120 it.first_visible_x = 0;
19121 it.last_visible_x = FRAME_COLS (f);
19123 #endif /* not USE_X_TOOLKIT */
19125 /* FIXME: This should be controlled by a user option. See the
19126 comments in redisplay_tool_bar and display_mode_line about
19127 this. */
19128 it.paragraph_embedding = L2R;
19130 if (! mode_line_inverse_video)
19131 /* Force the menu-bar to be displayed in the default face. */
19132 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19134 /* Clear all rows of the menu bar. */
19135 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19137 struct glyph_row *row = it.glyph_row + i;
19138 clear_glyph_row (row);
19139 row->enabled_p = 1;
19140 row->full_width_p = 1;
19143 /* Display all items of the menu bar. */
19144 items = FRAME_MENU_BAR_ITEMS (it.f);
19145 for (i = 0; i < ASIZE (items); i += 4)
19147 Lisp_Object string;
19149 /* Stop at nil string. */
19150 string = AREF (items, i + 1);
19151 if (NILP (string))
19152 break;
19154 /* Remember where item was displayed. */
19155 ASET (items, i + 3, make_number (it.hpos));
19157 /* Display the item, pad with one space. */
19158 if (it.current_x < it.last_visible_x)
19159 display_string (NULL, string, Qnil, 0, 0, &it,
19160 SCHARS (string) + 1, 0, 0, -1);
19163 /* Fill out the line with spaces. */
19164 if (it.current_x < it.last_visible_x)
19165 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19167 /* Compute the total height of the lines. */
19168 compute_line_metrics (&it);
19173 /***********************************************************************
19174 Mode Line
19175 ***********************************************************************/
19177 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19178 FORCE is non-zero, redisplay mode lines unconditionally.
19179 Otherwise, redisplay only mode lines that are garbaged. Value is
19180 the number of windows whose mode lines were redisplayed. */
19182 static int
19183 redisplay_mode_lines (Lisp_Object window, int force)
19185 int nwindows = 0;
19187 while (!NILP (window))
19189 struct window *w = XWINDOW (window);
19191 if (WINDOWP (w->hchild))
19192 nwindows += redisplay_mode_lines (w->hchild, force);
19193 else if (WINDOWP (w->vchild))
19194 nwindows += redisplay_mode_lines (w->vchild, force);
19195 else if (force
19196 || FRAME_GARBAGED_P (XFRAME (w->frame))
19197 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19199 struct text_pos lpoint;
19200 struct buffer *old = current_buffer;
19202 /* Set the window's buffer for the mode line display. */
19203 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19204 set_buffer_internal_1 (XBUFFER (w->buffer));
19206 /* Point refers normally to the selected window. For any
19207 other window, set up appropriate value. */
19208 if (!EQ (window, selected_window))
19210 struct text_pos pt;
19212 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19213 if (CHARPOS (pt) < BEGV)
19214 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19215 else if (CHARPOS (pt) > (ZV - 1))
19216 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19217 else
19218 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19221 /* Display mode lines. */
19222 clear_glyph_matrix (w->desired_matrix);
19223 if (display_mode_lines (w))
19225 ++nwindows;
19226 w->must_be_updated_p = 1;
19229 /* Restore old settings. */
19230 set_buffer_internal_1 (old);
19231 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19234 window = w->next;
19237 return nwindows;
19241 /* Display the mode and/or header line of window W. Value is the
19242 sum number of mode lines and header lines displayed. */
19244 static int
19245 display_mode_lines (struct window *w)
19247 Lisp_Object old_selected_window, old_selected_frame;
19248 int n = 0;
19250 old_selected_frame = selected_frame;
19251 selected_frame = w->frame;
19252 old_selected_window = selected_window;
19253 XSETWINDOW (selected_window, w);
19255 /* These will be set while the mode line specs are processed. */
19256 line_number_displayed = 0;
19257 w->column_number_displayed = Qnil;
19259 if (WINDOW_WANTS_MODELINE_P (w))
19261 struct window *sel_w = XWINDOW (old_selected_window);
19263 /* Select mode line face based on the real selected window. */
19264 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19265 BVAR (current_buffer, mode_line_format));
19266 ++n;
19269 if (WINDOW_WANTS_HEADER_LINE_P (w))
19271 display_mode_line (w, HEADER_LINE_FACE_ID,
19272 BVAR (current_buffer, header_line_format));
19273 ++n;
19276 selected_frame = old_selected_frame;
19277 selected_window = old_selected_window;
19278 return n;
19282 /* Display mode or header line of window W. FACE_ID specifies which
19283 line to display; it is either MODE_LINE_FACE_ID or
19284 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19285 display. Value is the pixel height of the mode/header line
19286 displayed. */
19288 static int
19289 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19291 struct it it;
19292 struct face *face;
19293 int count = SPECPDL_INDEX ();
19295 init_iterator (&it, w, -1, -1, NULL, face_id);
19296 /* Don't extend on a previously drawn mode-line.
19297 This may happen if called from pos_visible_p. */
19298 it.glyph_row->enabled_p = 0;
19299 prepare_desired_row (it.glyph_row);
19301 it.glyph_row->mode_line_p = 1;
19303 if (! mode_line_inverse_video)
19304 /* Force the mode-line to be displayed in the default face. */
19305 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19307 /* FIXME: This should be controlled by a user option. But
19308 supporting such an option is not trivial, since the mode line is
19309 made up of many separate strings. */
19310 it.paragraph_embedding = L2R;
19312 record_unwind_protect (unwind_format_mode_line,
19313 format_mode_line_unwind_data (NULL, Qnil, 0));
19315 mode_line_target = MODE_LINE_DISPLAY;
19317 /* Temporarily make frame's keyboard the current kboard so that
19318 kboard-local variables in the mode_line_format will get the right
19319 values. */
19320 push_kboard (FRAME_KBOARD (it.f));
19321 record_unwind_save_match_data ();
19322 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19323 pop_kboard ();
19325 unbind_to (count, Qnil);
19327 /* Fill up with spaces. */
19328 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19330 compute_line_metrics (&it);
19331 it.glyph_row->full_width_p = 1;
19332 it.glyph_row->continued_p = 0;
19333 it.glyph_row->truncated_on_left_p = 0;
19334 it.glyph_row->truncated_on_right_p = 0;
19336 /* Make a 3D mode-line have a shadow at its right end. */
19337 face = FACE_FROM_ID (it.f, face_id);
19338 extend_face_to_end_of_line (&it);
19339 if (face->box != FACE_NO_BOX)
19341 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19342 + it.glyph_row->used[TEXT_AREA] - 1);
19343 last->right_box_line_p = 1;
19346 return it.glyph_row->height;
19349 /* Move element ELT in LIST to the front of LIST.
19350 Return the updated list. */
19352 static Lisp_Object
19353 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19355 register Lisp_Object tail, prev;
19356 register Lisp_Object tem;
19358 tail = list;
19359 prev = Qnil;
19360 while (CONSP (tail))
19362 tem = XCAR (tail);
19364 if (EQ (elt, tem))
19366 /* Splice out the link TAIL. */
19367 if (NILP (prev))
19368 list = XCDR (tail);
19369 else
19370 Fsetcdr (prev, XCDR (tail));
19372 /* Now make it the first. */
19373 Fsetcdr (tail, list);
19374 return tail;
19376 else
19377 prev = tail;
19378 tail = XCDR (tail);
19379 QUIT;
19382 /* Not found--return unchanged LIST. */
19383 return list;
19386 /* Contribute ELT to the mode line for window IT->w. How it
19387 translates into text depends on its data type.
19389 IT describes the display environment in which we display, as usual.
19391 DEPTH is the depth in recursion. It is used to prevent
19392 infinite recursion here.
19394 FIELD_WIDTH is the number of characters the display of ELT should
19395 occupy in the mode line, and PRECISION is the maximum number of
19396 characters to display from ELT's representation. See
19397 display_string for details.
19399 Returns the hpos of the end of the text generated by ELT.
19401 PROPS is a property list to add to any string we encounter.
19403 If RISKY is nonzero, remove (disregard) any properties in any string
19404 we encounter, and ignore :eval and :propertize.
19406 The global variable `mode_line_target' determines whether the
19407 output is passed to `store_mode_line_noprop',
19408 `store_mode_line_string', or `display_string'. */
19410 static int
19411 display_mode_element (struct it *it, int depth, int field_width, int precision,
19412 Lisp_Object elt, Lisp_Object props, int risky)
19414 int n = 0, field, prec;
19415 int literal = 0;
19417 tail_recurse:
19418 if (depth > 100)
19419 elt = build_string ("*too-deep*");
19421 depth++;
19423 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19425 case Lisp_String:
19427 /* A string: output it and check for %-constructs within it. */
19428 unsigned char c;
19429 EMACS_INT offset = 0;
19431 if (SCHARS (elt) > 0
19432 && (!NILP (props) || risky))
19434 Lisp_Object oprops, aelt;
19435 oprops = Ftext_properties_at (make_number (0), elt);
19437 /* If the starting string's properties are not what
19438 we want, translate the string. Also, if the string
19439 is risky, do that anyway. */
19441 if (NILP (Fequal (props, oprops)) || risky)
19443 /* If the starting string has properties,
19444 merge the specified ones onto the existing ones. */
19445 if (! NILP (oprops) && !risky)
19447 Lisp_Object tem;
19449 oprops = Fcopy_sequence (oprops);
19450 tem = props;
19451 while (CONSP (tem))
19453 oprops = Fplist_put (oprops, XCAR (tem),
19454 XCAR (XCDR (tem)));
19455 tem = XCDR (XCDR (tem));
19457 props = oprops;
19460 aelt = Fassoc (elt, mode_line_proptrans_alist);
19461 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19463 /* AELT is what we want. Move it to the front
19464 without consing. */
19465 elt = XCAR (aelt);
19466 mode_line_proptrans_alist
19467 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19469 else
19471 Lisp_Object tem;
19473 /* If AELT has the wrong props, it is useless.
19474 so get rid of it. */
19475 if (! NILP (aelt))
19476 mode_line_proptrans_alist
19477 = Fdelq (aelt, mode_line_proptrans_alist);
19479 elt = Fcopy_sequence (elt);
19480 Fset_text_properties (make_number (0), Flength (elt),
19481 props, elt);
19482 /* Add this item to mode_line_proptrans_alist. */
19483 mode_line_proptrans_alist
19484 = Fcons (Fcons (elt, props),
19485 mode_line_proptrans_alist);
19486 /* Truncate mode_line_proptrans_alist
19487 to at most 50 elements. */
19488 tem = Fnthcdr (make_number (50),
19489 mode_line_proptrans_alist);
19490 if (! NILP (tem))
19491 XSETCDR (tem, Qnil);
19496 offset = 0;
19498 if (literal)
19500 prec = precision - n;
19501 switch (mode_line_target)
19503 case MODE_LINE_NOPROP:
19504 case MODE_LINE_TITLE:
19505 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19506 break;
19507 case MODE_LINE_STRING:
19508 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19509 break;
19510 case MODE_LINE_DISPLAY:
19511 n += display_string (NULL, elt, Qnil, 0, 0, it,
19512 0, prec, 0, STRING_MULTIBYTE (elt));
19513 break;
19516 break;
19519 /* Handle the non-literal case. */
19521 while ((precision <= 0 || n < precision)
19522 && SREF (elt, offset) != 0
19523 && (mode_line_target != MODE_LINE_DISPLAY
19524 || it->current_x < it->last_visible_x))
19526 EMACS_INT last_offset = offset;
19528 /* Advance to end of string or next format specifier. */
19529 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19532 if (offset - 1 != last_offset)
19534 EMACS_INT nchars, nbytes;
19536 /* Output to end of string or up to '%'. Field width
19537 is length of string. Don't output more than
19538 PRECISION allows us. */
19539 offset--;
19541 prec = c_string_width (SDATA (elt) + last_offset,
19542 offset - last_offset, precision - n,
19543 &nchars, &nbytes);
19545 switch (mode_line_target)
19547 case MODE_LINE_NOPROP:
19548 case MODE_LINE_TITLE:
19549 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19550 break;
19551 case MODE_LINE_STRING:
19553 EMACS_INT bytepos = last_offset;
19554 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19555 EMACS_INT endpos = (precision <= 0
19556 ? string_byte_to_char (elt, offset)
19557 : charpos + nchars);
19559 n += store_mode_line_string (NULL,
19560 Fsubstring (elt, make_number (charpos),
19561 make_number (endpos)),
19562 0, 0, 0, Qnil);
19564 break;
19565 case MODE_LINE_DISPLAY:
19567 EMACS_INT bytepos = last_offset;
19568 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19570 if (precision <= 0)
19571 nchars = string_byte_to_char (elt, offset) - charpos;
19572 n += display_string (NULL, elt, Qnil, 0, charpos,
19573 it, 0, nchars, 0,
19574 STRING_MULTIBYTE (elt));
19576 break;
19579 else /* c == '%' */
19581 EMACS_INT percent_position = offset;
19583 /* Get the specified minimum width. Zero means
19584 don't pad. */
19585 field = 0;
19586 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19587 field = field * 10 + c - '0';
19589 /* Don't pad beyond the total padding allowed. */
19590 if (field_width - n > 0 && field > field_width - n)
19591 field = field_width - n;
19593 /* Note that either PRECISION <= 0 or N < PRECISION. */
19594 prec = precision - n;
19596 if (c == 'M')
19597 n += display_mode_element (it, depth, field, prec,
19598 Vglobal_mode_string, props,
19599 risky);
19600 else if (c != 0)
19602 int multibyte;
19603 EMACS_INT bytepos, charpos;
19604 const char *spec;
19605 Lisp_Object string;
19607 bytepos = percent_position;
19608 charpos = (STRING_MULTIBYTE (elt)
19609 ? string_byte_to_char (elt, bytepos)
19610 : bytepos);
19611 spec = decode_mode_spec (it->w, c, field, &string);
19612 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19614 switch (mode_line_target)
19616 case MODE_LINE_NOPROP:
19617 case MODE_LINE_TITLE:
19618 n += store_mode_line_noprop (spec, field, prec);
19619 break;
19620 case MODE_LINE_STRING:
19622 Lisp_Object tem = build_string (spec);
19623 props = Ftext_properties_at (make_number (charpos), elt);
19624 /* Should only keep face property in props */
19625 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19627 break;
19628 case MODE_LINE_DISPLAY:
19630 int nglyphs_before, nwritten;
19632 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19633 nwritten = display_string (spec, string, elt,
19634 charpos, 0, it,
19635 field, prec, 0,
19636 multibyte);
19638 /* Assign to the glyphs written above the
19639 string where the `%x' came from, position
19640 of the `%'. */
19641 if (nwritten > 0)
19643 struct glyph *glyph
19644 = (it->glyph_row->glyphs[TEXT_AREA]
19645 + nglyphs_before);
19646 int i;
19648 for (i = 0; i < nwritten; ++i)
19650 glyph[i].object = elt;
19651 glyph[i].charpos = charpos;
19654 n += nwritten;
19657 break;
19660 else /* c == 0 */
19661 break;
19665 break;
19667 case Lisp_Symbol:
19668 /* A symbol: process the value of the symbol recursively
19669 as if it appeared here directly. Avoid error if symbol void.
19670 Special case: if value of symbol is a string, output the string
19671 literally. */
19673 register Lisp_Object tem;
19675 /* If the variable is not marked as risky to set
19676 then its contents are risky to use. */
19677 if (NILP (Fget (elt, Qrisky_local_variable)))
19678 risky = 1;
19680 tem = Fboundp (elt);
19681 if (!NILP (tem))
19683 tem = Fsymbol_value (elt);
19684 /* If value is a string, output that string literally:
19685 don't check for % within it. */
19686 if (STRINGP (tem))
19687 literal = 1;
19689 if (!EQ (tem, elt))
19691 /* Give up right away for nil or t. */
19692 elt = tem;
19693 goto tail_recurse;
19697 break;
19699 case Lisp_Cons:
19701 register Lisp_Object car, tem;
19703 /* A cons cell: five distinct cases.
19704 If first element is :eval or :propertize, do something special.
19705 If first element is a string or a cons, process all the elements
19706 and effectively concatenate them.
19707 If first element is a negative number, truncate displaying cdr to
19708 at most that many characters. If positive, pad (with spaces)
19709 to at least that many characters.
19710 If first element is a symbol, process the cadr or caddr recursively
19711 according to whether the symbol's value is non-nil or nil. */
19712 car = XCAR (elt);
19713 if (EQ (car, QCeval))
19715 /* An element of the form (:eval FORM) means evaluate FORM
19716 and use the result as mode line elements. */
19718 if (risky)
19719 break;
19721 if (CONSP (XCDR (elt)))
19723 Lisp_Object spec;
19724 spec = safe_eval (XCAR (XCDR (elt)));
19725 n += display_mode_element (it, depth, field_width - n,
19726 precision - n, spec, props,
19727 risky);
19730 else if (EQ (car, QCpropertize))
19732 /* An element of the form (:propertize ELT PROPS...)
19733 means display ELT but applying properties PROPS. */
19735 if (risky)
19736 break;
19738 if (CONSP (XCDR (elt)))
19739 n += display_mode_element (it, depth, field_width - n,
19740 precision - n, XCAR (XCDR (elt)),
19741 XCDR (XCDR (elt)), risky);
19743 else if (SYMBOLP (car))
19745 tem = Fboundp (car);
19746 elt = XCDR (elt);
19747 if (!CONSP (elt))
19748 goto invalid;
19749 /* elt is now the cdr, and we know it is a cons cell.
19750 Use its car if CAR has a non-nil value. */
19751 if (!NILP (tem))
19753 tem = Fsymbol_value (car);
19754 if (!NILP (tem))
19756 elt = XCAR (elt);
19757 goto tail_recurse;
19760 /* Symbol's value is nil (or symbol is unbound)
19761 Get the cddr of the original list
19762 and if possible find the caddr and use that. */
19763 elt = XCDR (elt);
19764 if (NILP (elt))
19765 break;
19766 else if (!CONSP (elt))
19767 goto invalid;
19768 elt = XCAR (elt);
19769 goto tail_recurse;
19771 else if (INTEGERP (car))
19773 register int lim = XINT (car);
19774 elt = XCDR (elt);
19775 if (lim < 0)
19777 /* Negative int means reduce maximum width. */
19778 if (precision <= 0)
19779 precision = -lim;
19780 else
19781 precision = min (precision, -lim);
19783 else if (lim > 0)
19785 /* Padding specified. Don't let it be more than
19786 current maximum. */
19787 if (precision > 0)
19788 lim = min (precision, lim);
19790 /* If that's more padding than already wanted, queue it.
19791 But don't reduce padding already specified even if
19792 that is beyond the current truncation point. */
19793 field_width = max (lim, field_width);
19795 goto tail_recurse;
19797 else if (STRINGP (car) || CONSP (car))
19799 Lisp_Object halftail = elt;
19800 int len = 0;
19802 while (CONSP (elt)
19803 && (precision <= 0 || n < precision))
19805 n += display_mode_element (it, depth,
19806 /* Do padding only after the last
19807 element in the list. */
19808 (! CONSP (XCDR (elt))
19809 ? field_width - n
19810 : 0),
19811 precision - n, XCAR (elt),
19812 props, risky);
19813 elt = XCDR (elt);
19814 len++;
19815 if ((len & 1) == 0)
19816 halftail = XCDR (halftail);
19817 /* Check for cycle. */
19818 if (EQ (halftail, elt))
19819 break;
19823 break;
19825 default:
19826 invalid:
19827 elt = build_string ("*invalid*");
19828 goto tail_recurse;
19831 /* Pad to FIELD_WIDTH. */
19832 if (field_width > 0 && n < field_width)
19834 switch (mode_line_target)
19836 case MODE_LINE_NOPROP:
19837 case MODE_LINE_TITLE:
19838 n += store_mode_line_noprop ("", field_width - n, 0);
19839 break;
19840 case MODE_LINE_STRING:
19841 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19842 break;
19843 case MODE_LINE_DISPLAY:
19844 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19845 0, 0, 0);
19846 break;
19850 return n;
19853 /* Store a mode-line string element in mode_line_string_list.
19855 If STRING is non-null, display that C string. Otherwise, the Lisp
19856 string LISP_STRING is displayed.
19858 FIELD_WIDTH is the minimum number of output glyphs to produce.
19859 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19860 with spaces. FIELD_WIDTH <= 0 means don't pad.
19862 PRECISION is the maximum number of characters to output from
19863 STRING. PRECISION <= 0 means don't truncate the string.
19865 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19866 properties to the string.
19868 PROPS are the properties to add to the string.
19869 The mode_line_string_face face property is always added to the string.
19872 static int
19873 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19874 int field_width, int precision, Lisp_Object props)
19876 EMACS_INT len;
19877 int n = 0;
19879 if (string != NULL)
19881 len = strlen (string);
19882 if (precision > 0 && len > precision)
19883 len = precision;
19884 lisp_string = make_string (string, len);
19885 if (NILP (props))
19886 props = mode_line_string_face_prop;
19887 else if (!NILP (mode_line_string_face))
19889 Lisp_Object face = Fplist_get (props, Qface);
19890 props = Fcopy_sequence (props);
19891 if (NILP (face))
19892 face = mode_line_string_face;
19893 else
19894 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19895 props = Fplist_put (props, Qface, face);
19897 Fadd_text_properties (make_number (0), make_number (len),
19898 props, lisp_string);
19900 else
19902 len = XFASTINT (Flength (lisp_string));
19903 if (precision > 0 && len > precision)
19905 len = precision;
19906 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19907 precision = -1;
19909 if (!NILP (mode_line_string_face))
19911 Lisp_Object face;
19912 if (NILP (props))
19913 props = Ftext_properties_at (make_number (0), lisp_string);
19914 face = Fplist_get (props, Qface);
19915 if (NILP (face))
19916 face = mode_line_string_face;
19917 else
19918 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19919 props = Fcons (Qface, Fcons (face, Qnil));
19920 if (copy_string)
19921 lisp_string = Fcopy_sequence (lisp_string);
19923 if (!NILP (props))
19924 Fadd_text_properties (make_number (0), make_number (len),
19925 props, lisp_string);
19928 if (len > 0)
19930 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19931 n += len;
19934 if (field_width > len)
19936 field_width -= len;
19937 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19938 if (!NILP (props))
19939 Fadd_text_properties (make_number (0), make_number (field_width),
19940 props, lisp_string);
19941 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19942 n += field_width;
19945 return n;
19949 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19950 1, 4, 0,
19951 doc: /* Format a string out of a mode line format specification.
19952 First arg FORMAT specifies the mode line format (see `mode-line-format'
19953 for details) to use.
19955 By default, the format is evaluated for the currently selected window.
19957 Optional second arg FACE specifies the face property to put on all
19958 characters for which no face is specified. The value nil means the
19959 default face. The value t means whatever face the window's mode line
19960 currently uses (either `mode-line' or `mode-line-inactive',
19961 depending on whether the window is the selected window or not).
19962 An integer value means the value string has no text
19963 properties.
19965 Optional third and fourth args WINDOW and BUFFER specify the window
19966 and buffer to use as the context for the formatting (defaults
19967 are the selected window and the WINDOW's buffer). */)
19968 (Lisp_Object format, Lisp_Object face,
19969 Lisp_Object window, Lisp_Object buffer)
19971 struct it it;
19972 int len;
19973 struct window *w;
19974 struct buffer *old_buffer = NULL;
19975 int face_id;
19976 int no_props = INTEGERP (face);
19977 int count = SPECPDL_INDEX ();
19978 Lisp_Object str;
19979 int string_start = 0;
19981 if (NILP (window))
19982 window = selected_window;
19983 CHECK_WINDOW (window);
19984 w = XWINDOW (window);
19986 if (NILP (buffer))
19987 buffer = w->buffer;
19988 CHECK_BUFFER (buffer);
19990 /* Make formatting the modeline a non-op when noninteractive, otherwise
19991 there will be problems later caused by a partially initialized frame. */
19992 if (NILP (format) || noninteractive)
19993 return empty_unibyte_string;
19995 if (no_props)
19996 face = Qnil;
19998 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19999 : EQ (face, Qt) ? (EQ (window, selected_window)
20000 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20001 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20002 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20003 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20004 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20005 : DEFAULT_FACE_ID;
20007 if (XBUFFER (buffer) != current_buffer)
20008 old_buffer = current_buffer;
20010 /* Save things including mode_line_proptrans_alist,
20011 and set that to nil so that we don't alter the outer value. */
20012 record_unwind_protect (unwind_format_mode_line,
20013 format_mode_line_unwind_data
20014 (old_buffer, selected_window, 1));
20015 mode_line_proptrans_alist = Qnil;
20017 Fselect_window (window, Qt);
20018 if (old_buffer)
20019 set_buffer_internal_1 (XBUFFER (buffer));
20021 init_iterator (&it, w, -1, -1, NULL, face_id);
20023 if (no_props)
20025 mode_line_target = MODE_LINE_NOPROP;
20026 mode_line_string_face_prop = Qnil;
20027 mode_line_string_list = Qnil;
20028 string_start = MODE_LINE_NOPROP_LEN (0);
20030 else
20032 mode_line_target = MODE_LINE_STRING;
20033 mode_line_string_list = Qnil;
20034 mode_line_string_face = face;
20035 mode_line_string_face_prop
20036 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20039 push_kboard (FRAME_KBOARD (it.f));
20040 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20041 pop_kboard ();
20043 if (no_props)
20045 len = MODE_LINE_NOPROP_LEN (string_start);
20046 str = make_string (mode_line_noprop_buf + string_start, len);
20048 else
20050 mode_line_string_list = Fnreverse (mode_line_string_list);
20051 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20052 empty_unibyte_string);
20055 unbind_to (count, Qnil);
20056 return str;
20059 /* Write a null-terminated, right justified decimal representation of
20060 the positive integer D to BUF using a minimal field width WIDTH. */
20062 static void
20063 pint2str (register char *buf, register int width, register EMACS_INT d)
20065 register char *p = buf;
20067 if (d <= 0)
20068 *p++ = '0';
20069 else
20071 while (d > 0)
20073 *p++ = d % 10 + '0';
20074 d /= 10;
20078 for (width -= (int) (p - buf); width > 0; --width)
20079 *p++ = ' ';
20080 *p-- = '\0';
20081 while (p > buf)
20083 d = *buf;
20084 *buf++ = *p;
20085 *p-- = d;
20089 /* Write a null-terminated, right justified decimal and "human
20090 readable" representation of the nonnegative integer D to BUF using
20091 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20093 static const char power_letter[] =
20095 0, /* no letter */
20096 'k', /* kilo */
20097 'M', /* mega */
20098 'G', /* giga */
20099 'T', /* tera */
20100 'P', /* peta */
20101 'E', /* exa */
20102 'Z', /* zetta */
20103 'Y' /* yotta */
20106 static void
20107 pint2hrstr (char *buf, int width, EMACS_INT d)
20109 /* We aim to represent the nonnegative integer D as
20110 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20111 EMACS_INT quotient = d;
20112 int remainder = 0;
20113 /* -1 means: do not use TENTHS. */
20114 int tenths = -1;
20115 int exponent = 0;
20117 /* Length of QUOTIENT.TENTHS as a string. */
20118 int length;
20120 char * psuffix;
20121 char * p;
20123 if (1000 <= quotient)
20125 /* Scale to the appropriate EXPONENT. */
20128 remainder = quotient % 1000;
20129 quotient /= 1000;
20130 exponent++;
20132 while (1000 <= quotient);
20134 /* Round to nearest and decide whether to use TENTHS or not. */
20135 if (quotient <= 9)
20137 tenths = remainder / 100;
20138 if (50 <= remainder % 100)
20140 if (tenths < 9)
20141 tenths++;
20142 else
20144 quotient++;
20145 if (quotient == 10)
20146 tenths = -1;
20147 else
20148 tenths = 0;
20152 else
20153 if (500 <= remainder)
20155 if (quotient < 999)
20156 quotient++;
20157 else
20159 quotient = 1;
20160 exponent++;
20161 tenths = 0;
20166 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20167 if (tenths == -1 && quotient <= 99)
20168 if (quotient <= 9)
20169 length = 1;
20170 else
20171 length = 2;
20172 else
20173 length = 3;
20174 p = psuffix = buf + max (width, length);
20176 /* Print EXPONENT. */
20177 *psuffix++ = power_letter[exponent];
20178 *psuffix = '\0';
20180 /* Print TENTHS. */
20181 if (tenths >= 0)
20183 *--p = '0' + tenths;
20184 *--p = '.';
20187 /* Print QUOTIENT. */
20190 int digit = quotient % 10;
20191 *--p = '0' + digit;
20193 while ((quotient /= 10) != 0);
20195 /* Print leading spaces. */
20196 while (buf < p)
20197 *--p = ' ';
20200 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20201 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20202 type of CODING_SYSTEM. Return updated pointer into BUF. */
20204 static unsigned char invalid_eol_type[] = "(*invalid*)";
20206 static char *
20207 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20209 Lisp_Object val;
20210 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20211 const unsigned char *eol_str;
20212 int eol_str_len;
20213 /* The EOL conversion we are using. */
20214 Lisp_Object eoltype;
20216 val = CODING_SYSTEM_SPEC (coding_system);
20217 eoltype = Qnil;
20219 if (!VECTORP (val)) /* Not yet decided. */
20221 if (multibyte)
20222 *buf++ = '-';
20223 if (eol_flag)
20224 eoltype = eol_mnemonic_undecided;
20225 /* Don't mention EOL conversion if it isn't decided. */
20227 else
20229 Lisp_Object attrs;
20230 Lisp_Object eolvalue;
20232 attrs = AREF (val, 0);
20233 eolvalue = AREF (val, 2);
20235 if (multibyte)
20236 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20238 if (eol_flag)
20240 /* The EOL conversion that is normal on this system. */
20242 if (NILP (eolvalue)) /* Not yet decided. */
20243 eoltype = eol_mnemonic_undecided;
20244 else if (VECTORP (eolvalue)) /* Not yet decided. */
20245 eoltype = eol_mnemonic_undecided;
20246 else /* eolvalue is Qunix, Qdos, or Qmac. */
20247 eoltype = (EQ (eolvalue, Qunix)
20248 ? eol_mnemonic_unix
20249 : (EQ (eolvalue, Qdos) == 1
20250 ? eol_mnemonic_dos : eol_mnemonic_mac));
20254 if (eol_flag)
20256 /* Mention the EOL conversion if it is not the usual one. */
20257 if (STRINGP (eoltype))
20259 eol_str = SDATA (eoltype);
20260 eol_str_len = SBYTES (eoltype);
20262 else if (CHARACTERP (eoltype))
20264 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20265 int c = XFASTINT (eoltype);
20266 eol_str_len = CHAR_STRING (c, tmp);
20267 eol_str = tmp;
20269 else
20271 eol_str = invalid_eol_type;
20272 eol_str_len = sizeof (invalid_eol_type) - 1;
20274 memcpy (buf, eol_str, eol_str_len);
20275 buf += eol_str_len;
20278 return buf;
20281 /* Return a string for the output of a mode line %-spec for window W,
20282 generated by character C. FIELD_WIDTH > 0 means pad the string
20283 returned with spaces to that value. Return a Lisp string in
20284 *STRING if the resulting string is taken from that Lisp string.
20286 Note we operate on the current buffer for most purposes,
20287 the exception being w->base_line_pos. */
20289 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20291 static const char *
20292 decode_mode_spec (struct window *w, register int c, int field_width,
20293 Lisp_Object *string)
20295 Lisp_Object obj;
20296 struct frame *f = XFRAME (WINDOW_FRAME (w));
20297 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20298 struct buffer *b = current_buffer;
20300 obj = Qnil;
20301 *string = Qnil;
20303 switch (c)
20305 case '*':
20306 if (!NILP (BVAR (b, read_only)))
20307 return "%";
20308 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20309 return "*";
20310 return "-";
20312 case '+':
20313 /* This differs from %* only for a modified read-only buffer. */
20314 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20315 return "*";
20316 if (!NILP (BVAR (b, read_only)))
20317 return "%";
20318 return "-";
20320 case '&':
20321 /* This differs from %* in ignoring read-only-ness. */
20322 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20323 return "*";
20324 return "-";
20326 case '%':
20327 return "%";
20329 case '[':
20331 int i;
20332 char *p;
20334 if (command_loop_level > 5)
20335 return "[[[... ";
20336 p = decode_mode_spec_buf;
20337 for (i = 0; i < command_loop_level; i++)
20338 *p++ = '[';
20339 *p = 0;
20340 return decode_mode_spec_buf;
20343 case ']':
20345 int i;
20346 char *p;
20348 if (command_loop_level > 5)
20349 return " ...]]]";
20350 p = decode_mode_spec_buf;
20351 for (i = 0; i < command_loop_level; i++)
20352 *p++ = ']';
20353 *p = 0;
20354 return decode_mode_spec_buf;
20357 case '-':
20359 register int i;
20361 /* Let lots_of_dashes be a string of infinite length. */
20362 if (mode_line_target == MODE_LINE_NOPROP ||
20363 mode_line_target == MODE_LINE_STRING)
20364 return "--";
20365 if (field_width <= 0
20366 || field_width > sizeof (lots_of_dashes))
20368 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20369 decode_mode_spec_buf[i] = '-';
20370 decode_mode_spec_buf[i] = '\0';
20371 return decode_mode_spec_buf;
20373 else
20374 return lots_of_dashes;
20377 case 'b':
20378 obj = BVAR (b, name);
20379 break;
20381 case 'c':
20382 /* %c and %l are ignored in `frame-title-format'.
20383 (In redisplay_internal, the frame title is drawn _before_ the
20384 windows are updated, so the stuff which depends on actual
20385 window contents (such as %l) may fail to render properly, or
20386 even crash emacs.) */
20387 if (mode_line_target == MODE_LINE_TITLE)
20388 return "";
20389 else
20391 EMACS_INT col = current_column ();
20392 w->column_number_displayed = make_number (col);
20393 pint2str (decode_mode_spec_buf, field_width, col);
20394 return decode_mode_spec_buf;
20397 case 'e':
20398 #ifndef SYSTEM_MALLOC
20400 if (NILP (Vmemory_full))
20401 return "";
20402 else
20403 return "!MEM FULL! ";
20405 #else
20406 return "";
20407 #endif
20409 case 'F':
20410 /* %F displays the frame name. */
20411 if (!NILP (f->title))
20412 return SSDATA (f->title);
20413 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20414 return SSDATA (f->name);
20415 return "Emacs";
20417 case 'f':
20418 obj = BVAR (b, filename);
20419 break;
20421 case 'i':
20423 EMACS_INT size = ZV - BEGV;
20424 pint2str (decode_mode_spec_buf, field_width, size);
20425 return decode_mode_spec_buf;
20428 case 'I':
20430 EMACS_INT size = ZV - BEGV;
20431 pint2hrstr (decode_mode_spec_buf, field_width, size);
20432 return decode_mode_spec_buf;
20435 case 'l':
20437 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20438 EMACS_INT topline, nlines, height;
20439 EMACS_INT junk;
20441 /* %c and %l are ignored in `frame-title-format'. */
20442 if (mode_line_target == MODE_LINE_TITLE)
20443 return "";
20445 startpos = XMARKER (w->start)->charpos;
20446 startpos_byte = marker_byte_position (w->start);
20447 height = WINDOW_TOTAL_LINES (w);
20449 /* If we decided that this buffer isn't suitable for line numbers,
20450 don't forget that too fast. */
20451 if (EQ (w->base_line_pos, w->buffer))
20452 goto no_value;
20453 /* But do forget it, if the window shows a different buffer now. */
20454 else if (BUFFERP (w->base_line_pos))
20455 w->base_line_pos = Qnil;
20457 /* If the buffer is very big, don't waste time. */
20458 if (INTEGERP (Vline_number_display_limit)
20459 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20461 w->base_line_pos = Qnil;
20462 w->base_line_number = Qnil;
20463 goto no_value;
20466 if (INTEGERP (w->base_line_number)
20467 && INTEGERP (w->base_line_pos)
20468 && XFASTINT (w->base_line_pos) <= startpos)
20470 line = XFASTINT (w->base_line_number);
20471 linepos = XFASTINT (w->base_line_pos);
20472 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20474 else
20476 line = 1;
20477 linepos = BUF_BEGV (b);
20478 linepos_byte = BUF_BEGV_BYTE (b);
20481 /* Count lines from base line to window start position. */
20482 nlines = display_count_lines (linepos_byte,
20483 startpos_byte,
20484 startpos, &junk);
20486 topline = nlines + line;
20488 /* Determine a new base line, if the old one is too close
20489 or too far away, or if we did not have one.
20490 "Too close" means it's plausible a scroll-down would
20491 go back past it. */
20492 if (startpos == BUF_BEGV (b))
20494 w->base_line_number = make_number (topline);
20495 w->base_line_pos = make_number (BUF_BEGV (b));
20497 else if (nlines < height + 25 || nlines > height * 3 + 50
20498 || linepos == BUF_BEGV (b))
20500 EMACS_INT limit = BUF_BEGV (b);
20501 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20502 EMACS_INT position;
20503 EMACS_INT distance =
20504 (height * 2 + 30) * line_number_display_limit_width;
20506 if (startpos - distance > limit)
20508 limit = startpos - distance;
20509 limit_byte = CHAR_TO_BYTE (limit);
20512 nlines = display_count_lines (startpos_byte,
20513 limit_byte,
20514 - (height * 2 + 30),
20515 &position);
20516 /* If we couldn't find the lines we wanted within
20517 line_number_display_limit_width chars per line,
20518 give up on line numbers for this window. */
20519 if (position == limit_byte && limit == startpos - distance)
20521 w->base_line_pos = w->buffer;
20522 w->base_line_number = Qnil;
20523 goto no_value;
20526 w->base_line_number = make_number (topline - nlines);
20527 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20530 /* Now count lines from the start pos to point. */
20531 nlines = display_count_lines (startpos_byte,
20532 PT_BYTE, PT, &junk);
20534 /* Record that we did display the line number. */
20535 line_number_displayed = 1;
20537 /* Make the string to show. */
20538 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20539 return decode_mode_spec_buf;
20540 no_value:
20542 char* p = decode_mode_spec_buf;
20543 int pad = field_width - 2;
20544 while (pad-- > 0)
20545 *p++ = ' ';
20546 *p++ = '?';
20547 *p++ = '?';
20548 *p = '\0';
20549 return decode_mode_spec_buf;
20552 break;
20554 case 'm':
20555 obj = BVAR (b, mode_name);
20556 break;
20558 case 'n':
20559 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20560 return " Narrow";
20561 break;
20563 case 'p':
20565 EMACS_INT pos = marker_position (w->start);
20566 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20568 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20570 if (pos <= BUF_BEGV (b))
20571 return "All";
20572 else
20573 return "Bottom";
20575 else if (pos <= BUF_BEGV (b))
20576 return "Top";
20577 else
20579 if (total > 1000000)
20580 /* Do it differently for a large value, to avoid overflow. */
20581 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20582 else
20583 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20584 /* We can't normally display a 3-digit number,
20585 so get us a 2-digit number that is close. */
20586 if (total == 100)
20587 total = 99;
20588 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20589 return decode_mode_spec_buf;
20593 /* Display percentage of size above the bottom of the screen. */
20594 case 'P':
20596 EMACS_INT toppos = marker_position (w->start);
20597 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20598 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20600 if (botpos >= BUF_ZV (b))
20602 if (toppos <= BUF_BEGV (b))
20603 return "All";
20604 else
20605 return "Bottom";
20607 else
20609 if (total > 1000000)
20610 /* Do it differently for a large value, to avoid overflow. */
20611 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20612 else
20613 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20614 /* We can't normally display a 3-digit number,
20615 so get us a 2-digit number that is close. */
20616 if (total == 100)
20617 total = 99;
20618 if (toppos <= BUF_BEGV (b))
20619 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20620 else
20621 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20622 return decode_mode_spec_buf;
20626 case 's':
20627 /* status of process */
20628 obj = Fget_buffer_process (Fcurrent_buffer ());
20629 if (NILP (obj))
20630 return "no process";
20631 #ifndef MSDOS
20632 obj = Fsymbol_name (Fprocess_status (obj));
20633 #endif
20634 break;
20636 case '@':
20638 int count = inhibit_garbage_collection ();
20639 Lisp_Object val = call1 (intern ("file-remote-p"),
20640 BVAR (current_buffer, directory));
20641 unbind_to (count, Qnil);
20643 if (NILP (val))
20644 return "-";
20645 else
20646 return "@";
20649 case 't': /* indicate TEXT or BINARY */
20650 return "T";
20652 case 'z':
20653 /* coding-system (not including end-of-line format) */
20654 case 'Z':
20655 /* coding-system (including end-of-line type) */
20657 int eol_flag = (c == 'Z');
20658 char *p = decode_mode_spec_buf;
20660 if (! FRAME_WINDOW_P (f))
20662 /* No need to mention EOL here--the terminal never needs
20663 to do EOL conversion. */
20664 p = decode_mode_spec_coding (CODING_ID_NAME
20665 (FRAME_KEYBOARD_CODING (f)->id),
20666 p, 0);
20667 p = decode_mode_spec_coding (CODING_ID_NAME
20668 (FRAME_TERMINAL_CODING (f)->id),
20669 p, 0);
20671 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20672 p, eol_flag);
20674 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20675 #ifdef subprocesses
20676 obj = Fget_buffer_process (Fcurrent_buffer ());
20677 if (PROCESSP (obj))
20679 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20680 p, eol_flag);
20681 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20682 p, eol_flag);
20684 #endif /* subprocesses */
20685 #endif /* 0 */
20686 *p = 0;
20687 return decode_mode_spec_buf;
20691 if (STRINGP (obj))
20693 *string = obj;
20694 return SSDATA (obj);
20696 else
20697 return "";
20701 /* Count up to COUNT lines starting from START_BYTE.
20702 But don't go beyond LIMIT_BYTE.
20703 Return the number of lines thus found (always nonnegative).
20705 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20707 static EMACS_INT
20708 display_count_lines (EMACS_INT start_byte,
20709 EMACS_INT limit_byte, EMACS_INT count,
20710 EMACS_INT *byte_pos_ptr)
20712 register unsigned char *cursor;
20713 unsigned char *base;
20715 register EMACS_INT ceiling;
20716 register unsigned char *ceiling_addr;
20717 EMACS_INT orig_count = count;
20719 /* If we are not in selective display mode,
20720 check only for newlines. */
20721 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20722 && !INTEGERP (BVAR (current_buffer, selective_display)));
20724 if (count > 0)
20726 while (start_byte < limit_byte)
20728 ceiling = BUFFER_CEILING_OF (start_byte);
20729 ceiling = min (limit_byte - 1, ceiling);
20730 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20731 base = (cursor = BYTE_POS_ADDR (start_byte));
20732 while (1)
20734 if (selective_display)
20735 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20737 else
20738 while (*cursor != '\n' && ++cursor != ceiling_addr)
20741 if (cursor != ceiling_addr)
20743 if (--count == 0)
20745 start_byte += cursor - base + 1;
20746 *byte_pos_ptr = start_byte;
20747 return orig_count;
20749 else
20750 if (++cursor == ceiling_addr)
20751 break;
20753 else
20754 break;
20756 start_byte += cursor - base;
20759 else
20761 while (start_byte > limit_byte)
20763 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20764 ceiling = max (limit_byte, ceiling);
20765 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20766 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20767 while (1)
20769 if (selective_display)
20770 while (--cursor != ceiling_addr
20771 && *cursor != '\n' && *cursor != 015)
20773 else
20774 while (--cursor != ceiling_addr && *cursor != '\n')
20777 if (cursor != ceiling_addr)
20779 if (++count == 0)
20781 start_byte += cursor - base + 1;
20782 *byte_pos_ptr = start_byte;
20783 /* When scanning backwards, we should
20784 not count the newline posterior to which we stop. */
20785 return - orig_count - 1;
20788 else
20789 break;
20791 /* Here we add 1 to compensate for the last decrement
20792 of CURSOR, which took it past the valid range. */
20793 start_byte += cursor - base + 1;
20797 *byte_pos_ptr = limit_byte;
20799 if (count < 0)
20800 return - orig_count + count;
20801 return orig_count - count;
20807 /***********************************************************************
20808 Displaying strings
20809 ***********************************************************************/
20811 /* Display a NUL-terminated string, starting with index START.
20813 If STRING is non-null, display that C string. Otherwise, the Lisp
20814 string LISP_STRING is displayed. There's a case that STRING is
20815 non-null and LISP_STRING is not nil. It means STRING is a string
20816 data of LISP_STRING. In that case, we display LISP_STRING while
20817 ignoring its text properties.
20819 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20820 FACE_STRING. Display STRING or LISP_STRING with the face at
20821 FACE_STRING_POS in FACE_STRING:
20823 Display the string in the environment given by IT, but use the
20824 standard display table, temporarily.
20826 FIELD_WIDTH is the minimum number of output glyphs to produce.
20827 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20828 with spaces. If STRING has more characters, more than FIELD_WIDTH
20829 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20831 PRECISION is the maximum number of characters to output from
20832 STRING. PRECISION < 0 means don't truncate the string.
20834 This is roughly equivalent to printf format specifiers:
20836 FIELD_WIDTH PRECISION PRINTF
20837 ----------------------------------------
20838 -1 -1 %s
20839 -1 10 %.10s
20840 10 -1 %10s
20841 20 10 %20.10s
20843 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20844 display them, and < 0 means obey the current buffer's value of
20845 enable_multibyte_characters.
20847 Value is the number of columns displayed. */
20849 static int
20850 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20851 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20852 int field_width, int precision, int max_x, int multibyte)
20854 int hpos_at_start = it->hpos;
20855 int saved_face_id = it->face_id;
20856 struct glyph_row *row = it->glyph_row;
20857 EMACS_INT it_charpos;
20859 /* Initialize the iterator IT for iteration over STRING beginning
20860 with index START. */
20861 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20862 precision, field_width, multibyte);
20863 if (string && STRINGP (lisp_string))
20864 /* LISP_STRING is the one returned by decode_mode_spec. We should
20865 ignore its text properties. */
20866 it->stop_charpos = it->end_charpos;
20868 /* If displaying STRING, set up the face of the iterator from
20869 FACE_STRING, if that's given. */
20870 if (STRINGP (face_string))
20872 EMACS_INT endptr;
20873 struct face *face;
20875 it->face_id
20876 = face_at_string_position (it->w, face_string, face_string_pos,
20877 0, it->region_beg_charpos,
20878 it->region_end_charpos,
20879 &endptr, it->base_face_id, 0);
20880 face = FACE_FROM_ID (it->f, it->face_id);
20881 it->face_box_p = face->box != FACE_NO_BOX;
20884 /* Set max_x to the maximum allowed X position. Don't let it go
20885 beyond the right edge of the window. */
20886 if (max_x <= 0)
20887 max_x = it->last_visible_x;
20888 else
20889 max_x = min (max_x, it->last_visible_x);
20891 /* Skip over display elements that are not visible. because IT->w is
20892 hscrolled. */
20893 if (it->current_x < it->first_visible_x)
20894 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20895 MOVE_TO_POS | MOVE_TO_X);
20897 row->ascent = it->max_ascent;
20898 row->height = it->max_ascent + it->max_descent;
20899 row->phys_ascent = it->max_phys_ascent;
20900 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20901 row->extra_line_spacing = it->max_extra_line_spacing;
20903 if (STRINGP (it->string))
20904 it_charpos = IT_STRING_CHARPOS (*it);
20905 else
20906 it_charpos = IT_CHARPOS (*it);
20908 /* This condition is for the case that we are called with current_x
20909 past last_visible_x. */
20910 while (it->current_x < max_x)
20912 int x_before, x, n_glyphs_before, i, nglyphs;
20914 /* Get the next display element. */
20915 if (!get_next_display_element (it))
20916 break;
20918 /* Produce glyphs. */
20919 x_before = it->current_x;
20920 n_glyphs_before = row->used[TEXT_AREA];
20921 PRODUCE_GLYPHS (it);
20923 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20924 i = 0;
20925 x = x_before;
20926 while (i < nglyphs)
20928 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20930 if (it->line_wrap != TRUNCATE
20931 && x + glyph->pixel_width > max_x)
20933 /* End of continued line or max_x reached. */
20934 if (CHAR_GLYPH_PADDING_P (*glyph))
20936 /* A wide character is unbreakable. */
20937 if (row->reversed_p)
20938 unproduce_glyphs (it, row->used[TEXT_AREA]
20939 - n_glyphs_before);
20940 row->used[TEXT_AREA] = n_glyphs_before;
20941 it->current_x = x_before;
20943 else
20945 if (row->reversed_p)
20946 unproduce_glyphs (it, row->used[TEXT_AREA]
20947 - (n_glyphs_before + i));
20948 row->used[TEXT_AREA] = n_glyphs_before + i;
20949 it->current_x = x;
20951 break;
20953 else if (x + glyph->pixel_width >= it->first_visible_x)
20955 /* Glyph is at least partially visible. */
20956 ++it->hpos;
20957 if (x < it->first_visible_x)
20958 row->x = x - it->first_visible_x;
20960 else
20962 /* Glyph is off the left margin of the display area.
20963 Should not happen. */
20964 abort ();
20967 row->ascent = max (row->ascent, it->max_ascent);
20968 row->height = max (row->height, it->max_ascent + it->max_descent);
20969 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20970 row->phys_height = max (row->phys_height,
20971 it->max_phys_ascent + it->max_phys_descent);
20972 row->extra_line_spacing = max (row->extra_line_spacing,
20973 it->max_extra_line_spacing);
20974 x += glyph->pixel_width;
20975 ++i;
20978 /* Stop if max_x reached. */
20979 if (i < nglyphs)
20980 break;
20982 /* Stop at line ends. */
20983 if (ITERATOR_AT_END_OF_LINE_P (it))
20985 it->continuation_lines_width = 0;
20986 break;
20989 set_iterator_to_next (it, 1);
20990 if (STRINGP (it->string))
20991 it_charpos = IT_STRING_CHARPOS (*it);
20992 else
20993 it_charpos = IT_CHARPOS (*it);
20995 /* Stop if truncating at the right edge. */
20996 if (it->line_wrap == TRUNCATE
20997 && it->current_x >= it->last_visible_x)
20999 /* Add truncation mark, but don't do it if the line is
21000 truncated at a padding space. */
21001 if (it_charpos < it->string_nchars)
21003 if (!FRAME_WINDOW_P (it->f))
21005 int ii, n;
21007 if (it->current_x > it->last_visible_x)
21009 if (!row->reversed_p)
21011 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21012 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21013 break;
21015 else
21017 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21018 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21019 break;
21020 unproduce_glyphs (it, ii + 1);
21021 ii = row->used[TEXT_AREA] - (ii + 1);
21023 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21025 row->used[TEXT_AREA] = ii;
21026 produce_special_glyphs (it, IT_TRUNCATION);
21029 produce_special_glyphs (it, IT_TRUNCATION);
21031 row->truncated_on_right_p = 1;
21033 break;
21037 /* Maybe insert a truncation at the left. */
21038 if (it->first_visible_x
21039 && it_charpos > 0)
21041 if (!FRAME_WINDOW_P (it->f))
21042 insert_left_trunc_glyphs (it);
21043 row->truncated_on_left_p = 1;
21046 it->face_id = saved_face_id;
21048 /* Value is number of columns displayed. */
21049 return it->hpos - hpos_at_start;
21054 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21055 appears as an element of LIST or as the car of an element of LIST.
21056 If PROPVAL is a list, compare each element against LIST in that
21057 way, and return 1/2 if any element of PROPVAL is found in LIST.
21058 Otherwise return 0. This function cannot quit.
21059 The return value is 2 if the text is invisible but with an ellipsis
21060 and 1 if it's invisible and without an ellipsis. */
21063 invisible_p (register Lisp_Object propval, Lisp_Object list)
21065 register Lisp_Object tail, proptail;
21067 for (tail = list; CONSP (tail); tail = XCDR (tail))
21069 register Lisp_Object tem;
21070 tem = XCAR (tail);
21071 if (EQ (propval, tem))
21072 return 1;
21073 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21074 return NILP (XCDR (tem)) ? 1 : 2;
21077 if (CONSP (propval))
21079 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21081 Lisp_Object propelt;
21082 propelt = XCAR (proptail);
21083 for (tail = list; CONSP (tail); tail = XCDR (tail))
21085 register Lisp_Object tem;
21086 tem = XCAR (tail);
21087 if (EQ (propelt, tem))
21088 return 1;
21089 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21090 return NILP (XCDR (tem)) ? 1 : 2;
21095 return 0;
21098 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21099 doc: /* Non-nil if the property makes the text invisible.
21100 POS-OR-PROP can be a marker or number, in which case it is taken to be
21101 a position in the current buffer and the value of the `invisible' property
21102 is checked; or it can be some other value, which is then presumed to be the
21103 value of the `invisible' property of the text of interest.
21104 The non-nil value returned can be t for truly invisible text or something
21105 else if the text is replaced by an ellipsis. */)
21106 (Lisp_Object pos_or_prop)
21108 Lisp_Object prop
21109 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21110 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21111 : pos_or_prop);
21112 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21113 return (invis == 0 ? Qnil
21114 : invis == 1 ? Qt
21115 : make_number (invis));
21118 /* Calculate a width or height in pixels from a specification using
21119 the following elements:
21121 SPEC ::=
21122 NUM - a (fractional) multiple of the default font width/height
21123 (NUM) - specifies exactly NUM pixels
21124 UNIT - a fixed number of pixels, see below.
21125 ELEMENT - size of a display element in pixels, see below.
21126 (NUM . SPEC) - equals NUM * SPEC
21127 (+ SPEC SPEC ...) - add pixel values
21128 (- SPEC SPEC ...) - subtract pixel values
21129 (- SPEC) - negate pixel value
21131 NUM ::=
21132 INT or FLOAT - a number constant
21133 SYMBOL - use symbol's (buffer local) variable binding.
21135 UNIT ::=
21136 in - pixels per inch *)
21137 mm - pixels per 1/1000 meter *)
21138 cm - pixels per 1/100 meter *)
21139 width - width of current font in pixels.
21140 height - height of current font in pixels.
21142 *) using the ratio(s) defined in display-pixels-per-inch.
21144 ELEMENT ::=
21146 left-fringe - left fringe width in pixels
21147 right-fringe - right fringe width in pixels
21149 left-margin - left margin width in pixels
21150 right-margin - right margin width in pixels
21152 scroll-bar - scroll-bar area width in pixels
21154 Examples:
21156 Pixels corresponding to 5 inches:
21157 (5 . in)
21159 Total width of non-text areas on left side of window (if scroll-bar is on left):
21160 '(space :width (+ left-fringe left-margin scroll-bar))
21162 Align to first text column (in header line):
21163 '(space :align-to 0)
21165 Align to middle of text area minus half the width of variable `my-image'
21166 containing a loaded image:
21167 '(space :align-to (0.5 . (- text my-image)))
21169 Width of left margin minus width of 1 character in the default font:
21170 '(space :width (- left-margin 1))
21172 Width of left margin minus width of 2 characters in the current font:
21173 '(space :width (- left-margin (2 . width)))
21175 Center 1 character over left-margin (in header line):
21176 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21178 Different ways to express width of left fringe plus left margin minus one pixel:
21179 '(space :width (- (+ left-fringe left-margin) (1)))
21180 '(space :width (+ left-fringe left-margin (- (1))))
21181 '(space :width (+ left-fringe left-margin (-1)))
21185 #define NUMVAL(X) \
21186 ((INTEGERP (X) || FLOATP (X)) \
21187 ? XFLOATINT (X) \
21188 : - 1)
21191 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21192 struct font *font, int width_p, int *align_to)
21194 double pixels;
21196 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21197 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21199 if (NILP (prop))
21200 return OK_PIXELS (0);
21202 xassert (FRAME_LIVE_P (it->f));
21204 if (SYMBOLP (prop))
21206 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21208 char *unit = SSDATA (SYMBOL_NAME (prop));
21210 if (unit[0] == 'i' && unit[1] == 'n')
21211 pixels = 1.0;
21212 else if (unit[0] == 'm' && unit[1] == 'm')
21213 pixels = 25.4;
21214 else if (unit[0] == 'c' && unit[1] == 'm')
21215 pixels = 2.54;
21216 else
21217 pixels = 0;
21218 if (pixels > 0)
21220 double ppi;
21221 #ifdef HAVE_WINDOW_SYSTEM
21222 if (FRAME_WINDOW_P (it->f)
21223 && (ppi = (width_p
21224 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21225 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21226 ppi > 0))
21227 return OK_PIXELS (ppi / pixels);
21228 #endif
21230 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21231 || (CONSP (Vdisplay_pixels_per_inch)
21232 && (ppi = (width_p
21233 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21234 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21235 ppi > 0)))
21236 return OK_PIXELS (ppi / pixels);
21238 return 0;
21242 #ifdef HAVE_WINDOW_SYSTEM
21243 if (EQ (prop, Qheight))
21244 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21245 if (EQ (prop, Qwidth))
21246 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21247 #else
21248 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21249 return OK_PIXELS (1);
21250 #endif
21252 if (EQ (prop, Qtext))
21253 return OK_PIXELS (width_p
21254 ? window_box_width (it->w, TEXT_AREA)
21255 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21257 if (align_to && *align_to < 0)
21259 *res = 0;
21260 if (EQ (prop, Qleft))
21261 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21262 if (EQ (prop, Qright))
21263 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21264 if (EQ (prop, Qcenter))
21265 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21266 + window_box_width (it->w, TEXT_AREA) / 2);
21267 if (EQ (prop, Qleft_fringe))
21268 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21269 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21270 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21271 if (EQ (prop, Qright_fringe))
21272 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21273 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21274 : window_box_right_offset (it->w, TEXT_AREA));
21275 if (EQ (prop, Qleft_margin))
21276 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21277 if (EQ (prop, Qright_margin))
21278 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21279 if (EQ (prop, Qscroll_bar))
21280 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21282 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21283 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21284 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21285 : 0)));
21287 else
21289 if (EQ (prop, Qleft_fringe))
21290 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21291 if (EQ (prop, Qright_fringe))
21292 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21293 if (EQ (prop, Qleft_margin))
21294 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21295 if (EQ (prop, Qright_margin))
21296 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21297 if (EQ (prop, Qscroll_bar))
21298 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21301 prop = Fbuffer_local_value (prop, it->w->buffer);
21304 if (INTEGERP (prop) || FLOATP (prop))
21306 int base_unit = (width_p
21307 ? FRAME_COLUMN_WIDTH (it->f)
21308 : FRAME_LINE_HEIGHT (it->f));
21309 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21312 if (CONSP (prop))
21314 Lisp_Object car = XCAR (prop);
21315 Lisp_Object cdr = XCDR (prop);
21317 if (SYMBOLP (car))
21319 #ifdef HAVE_WINDOW_SYSTEM
21320 if (FRAME_WINDOW_P (it->f)
21321 && valid_image_p (prop))
21323 int id = lookup_image (it->f, prop);
21324 struct image *img = IMAGE_FROM_ID (it->f, id);
21326 return OK_PIXELS (width_p ? img->width : img->height);
21328 #endif
21329 if (EQ (car, Qplus) || EQ (car, Qminus))
21331 int first = 1;
21332 double px;
21334 pixels = 0;
21335 while (CONSP (cdr))
21337 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21338 font, width_p, align_to))
21339 return 0;
21340 if (first)
21341 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21342 else
21343 pixels += px;
21344 cdr = XCDR (cdr);
21346 if (EQ (car, Qminus))
21347 pixels = -pixels;
21348 return OK_PIXELS (pixels);
21351 car = Fbuffer_local_value (car, it->w->buffer);
21354 if (INTEGERP (car) || FLOATP (car))
21356 double fact;
21357 pixels = XFLOATINT (car);
21358 if (NILP (cdr))
21359 return OK_PIXELS (pixels);
21360 if (calc_pixel_width_or_height (&fact, it, cdr,
21361 font, width_p, align_to))
21362 return OK_PIXELS (pixels * fact);
21363 return 0;
21366 return 0;
21369 return 0;
21373 /***********************************************************************
21374 Glyph Display
21375 ***********************************************************************/
21377 #ifdef HAVE_WINDOW_SYSTEM
21379 #if GLYPH_DEBUG
21381 void
21382 dump_glyph_string (struct glyph_string *s)
21384 fprintf (stderr, "glyph string\n");
21385 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21386 s->x, s->y, s->width, s->height);
21387 fprintf (stderr, " ybase = %d\n", s->ybase);
21388 fprintf (stderr, " hl = %d\n", s->hl);
21389 fprintf (stderr, " left overhang = %d, right = %d\n",
21390 s->left_overhang, s->right_overhang);
21391 fprintf (stderr, " nchars = %d\n", s->nchars);
21392 fprintf (stderr, " extends to end of line = %d\n",
21393 s->extends_to_end_of_line_p);
21394 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21395 fprintf (stderr, " bg width = %d\n", s->background_width);
21398 #endif /* GLYPH_DEBUG */
21400 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21401 of XChar2b structures for S; it can't be allocated in
21402 init_glyph_string because it must be allocated via `alloca'. W
21403 is the window on which S is drawn. ROW and AREA are the glyph row
21404 and area within the row from which S is constructed. START is the
21405 index of the first glyph structure covered by S. HL is a
21406 face-override for drawing S. */
21408 #ifdef HAVE_NTGUI
21409 #define OPTIONAL_HDC(hdc) HDC hdc,
21410 #define DECLARE_HDC(hdc) HDC hdc;
21411 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21412 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21413 #endif
21415 #ifndef OPTIONAL_HDC
21416 #define OPTIONAL_HDC(hdc)
21417 #define DECLARE_HDC(hdc)
21418 #define ALLOCATE_HDC(hdc, f)
21419 #define RELEASE_HDC(hdc, f)
21420 #endif
21422 static void
21423 init_glyph_string (struct glyph_string *s,
21424 OPTIONAL_HDC (hdc)
21425 XChar2b *char2b, struct window *w, struct glyph_row *row,
21426 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21428 memset (s, 0, sizeof *s);
21429 s->w = w;
21430 s->f = XFRAME (w->frame);
21431 #ifdef HAVE_NTGUI
21432 s->hdc = hdc;
21433 #endif
21434 s->display = FRAME_X_DISPLAY (s->f);
21435 s->window = FRAME_X_WINDOW (s->f);
21436 s->char2b = char2b;
21437 s->hl = hl;
21438 s->row = row;
21439 s->area = area;
21440 s->first_glyph = row->glyphs[area] + start;
21441 s->height = row->height;
21442 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21443 s->ybase = s->y + row->ascent;
21447 /* Append the list of glyph strings with head H and tail T to the list
21448 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21450 static inline void
21451 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21452 struct glyph_string *h, struct glyph_string *t)
21454 if (h)
21456 if (*head)
21457 (*tail)->next = h;
21458 else
21459 *head = h;
21460 h->prev = *tail;
21461 *tail = t;
21466 /* Prepend the list of glyph strings with head H and tail T to the
21467 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21468 result. */
21470 static inline void
21471 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21472 struct glyph_string *h, struct glyph_string *t)
21474 if (h)
21476 if (*head)
21477 (*head)->prev = t;
21478 else
21479 *tail = t;
21480 t->next = *head;
21481 *head = h;
21486 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21487 Set *HEAD and *TAIL to the resulting list. */
21489 static inline void
21490 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21491 struct glyph_string *s)
21493 s->next = s->prev = NULL;
21494 append_glyph_string_lists (head, tail, s, s);
21498 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21499 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21500 make sure that X resources for the face returned are allocated.
21501 Value is a pointer to a realized face that is ready for display if
21502 DISPLAY_P is non-zero. */
21504 static inline struct face *
21505 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21506 XChar2b *char2b, int display_p)
21508 struct face *face = FACE_FROM_ID (f, face_id);
21510 if (face->font)
21512 unsigned code = face->font->driver->encode_char (face->font, c);
21514 if (code != FONT_INVALID_CODE)
21515 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21516 else
21517 STORE_XCHAR2B (char2b, 0, 0);
21520 /* Make sure X resources of the face are allocated. */
21521 #ifdef HAVE_X_WINDOWS
21522 if (display_p)
21523 #endif
21525 xassert (face != NULL);
21526 PREPARE_FACE_FOR_DISPLAY (f, face);
21529 return face;
21533 /* Get face and two-byte form of character glyph GLYPH on frame F.
21534 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21535 a pointer to a realized face that is ready for display. */
21537 static inline struct face *
21538 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21539 XChar2b *char2b, int *two_byte_p)
21541 struct face *face;
21543 xassert (glyph->type == CHAR_GLYPH);
21544 face = FACE_FROM_ID (f, glyph->face_id);
21546 if (two_byte_p)
21547 *two_byte_p = 0;
21549 if (face->font)
21551 unsigned code;
21553 if (CHAR_BYTE8_P (glyph->u.ch))
21554 code = CHAR_TO_BYTE8 (glyph->u.ch);
21555 else
21556 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21558 if (code != FONT_INVALID_CODE)
21559 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21560 else
21561 STORE_XCHAR2B (char2b, 0, 0);
21564 /* Make sure X resources of the face are allocated. */
21565 xassert (face != NULL);
21566 PREPARE_FACE_FOR_DISPLAY (f, face);
21567 return face;
21571 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21572 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21574 static inline int
21575 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21577 unsigned code;
21579 if (CHAR_BYTE8_P (c))
21580 code = CHAR_TO_BYTE8 (c);
21581 else
21582 code = font->driver->encode_char (font, c);
21584 if (code == FONT_INVALID_CODE)
21585 return 0;
21586 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21587 return 1;
21591 /* Fill glyph string S with composition components specified by S->cmp.
21593 BASE_FACE is the base face of the composition.
21594 S->cmp_from is the index of the first component for S.
21596 OVERLAPS non-zero means S should draw the foreground only, and use
21597 its physical height for clipping. See also draw_glyphs.
21599 Value is the index of a component not in S. */
21601 static int
21602 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21603 int overlaps)
21605 int i;
21606 /* For all glyphs of this composition, starting at the offset
21607 S->cmp_from, until we reach the end of the definition or encounter a
21608 glyph that requires the different face, add it to S. */
21609 struct face *face;
21611 xassert (s);
21613 s->for_overlaps = overlaps;
21614 s->face = NULL;
21615 s->font = NULL;
21616 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21618 int c = COMPOSITION_GLYPH (s->cmp, i);
21620 if (c != '\t')
21622 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21623 -1, Qnil);
21625 face = get_char_face_and_encoding (s->f, c, face_id,
21626 s->char2b + i, 1);
21627 if (face)
21629 if (! s->face)
21631 s->face = face;
21632 s->font = s->face->font;
21634 else if (s->face != face)
21635 break;
21638 ++s->nchars;
21640 s->cmp_to = i;
21642 /* All glyph strings for the same composition has the same width,
21643 i.e. the width set for the first component of the composition. */
21644 s->width = s->first_glyph->pixel_width;
21646 /* If the specified font could not be loaded, use the frame's
21647 default font, but record the fact that we couldn't load it in
21648 the glyph string so that we can draw rectangles for the
21649 characters of the glyph string. */
21650 if (s->font == NULL)
21652 s->font_not_found_p = 1;
21653 s->font = FRAME_FONT (s->f);
21656 /* Adjust base line for subscript/superscript text. */
21657 s->ybase += s->first_glyph->voffset;
21659 /* This glyph string must always be drawn with 16-bit functions. */
21660 s->two_byte_p = 1;
21662 return s->cmp_to;
21665 static int
21666 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21667 int start, int end, int overlaps)
21669 struct glyph *glyph, *last;
21670 Lisp_Object lgstring;
21671 int i;
21673 s->for_overlaps = overlaps;
21674 glyph = s->row->glyphs[s->area] + start;
21675 last = s->row->glyphs[s->area] + end;
21676 s->cmp_id = glyph->u.cmp.id;
21677 s->cmp_from = glyph->slice.cmp.from;
21678 s->cmp_to = glyph->slice.cmp.to + 1;
21679 s->face = FACE_FROM_ID (s->f, face_id);
21680 lgstring = composition_gstring_from_id (s->cmp_id);
21681 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21682 glyph++;
21683 while (glyph < last
21684 && glyph->u.cmp.automatic
21685 && glyph->u.cmp.id == s->cmp_id
21686 && s->cmp_to == glyph->slice.cmp.from)
21687 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21689 for (i = s->cmp_from; i < s->cmp_to; i++)
21691 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21692 unsigned code = LGLYPH_CODE (lglyph);
21694 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21696 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21697 return glyph - s->row->glyphs[s->area];
21701 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21702 See the comment of fill_glyph_string for arguments.
21703 Value is the index of the first glyph not in S. */
21706 static int
21707 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21708 int start, int end, int overlaps)
21710 struct glyph *glyph, *last;
21711 int voffset;
21713 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21714 s->for_overlaps = overlaps;
21715 glyph = s->row->glyphs[s->area] + start;
21716 last = s->row->glyphs[s->area] + end;
21717 voffset = glyph->voffset;
21718 s->face = FACE_FROM_ID (s->f, face_id);
21719 s->font = s->face->font;
21720 s->nchars = 1;
21721 s->width = glyph->pixel_width;
21722 glyph++;
21723 while (glyph < last
21724 && glyph->type == GLYPHLESS_GLYPH
21725 && glyph->voffset == voffset
21726 && glyph->face_id == face_id)
21728 s->nchars++;
21729 s->width += glyph->pixel_width;
21730 glyph++;
21732 s->ybase += voffset;
21733 return glyph - s->row->glyphs[s->area];
21737 /* Fill glyph string S from a sequence of character glyphs.
21739 FACE_ID is the face id of the string. START is the index of the
21740 first glyph to consider, END is the index of the last + 1.
21741 OVERLAPS non-zero means S should draw the foreground only, and use
21742 its physical height for clipping. See also draw_glyphs.
21744 Value is the index of the first glyph not in S. */
21746 static int
21747 fill_glyph_string (struct glyph_string *s, int face_id,
21748 int start, int end, int overlaps)
21750 struct glyph *glyph, *last;
21751 int voffset;
21752 int glyph_not_available_p;
21754 xassert (s->f == XFRAME (s->w->frame));
21755 xassert (s->nchars == 0);
21756 xassert (start >= 0 && end > start);
21758 s->for_overlaps = overlaps;
21759 glyph = s->row->glyphs[s->area] + start;
21760 last = s->row->glyphs[s->area] + end;
21761 voffset = glyph->voffset;
21762 s->padding_p = glyph->padding_p;
21763 glyph_not_available_p = glyph->glyph_not_available_p;
21765 while (glyph < last
21766 && glyph->type == CHAR_GLYPH
21767 && glyph->voffset == voffset
21768 /* Same face id implies same font, nowadays. */
21769 && glyph->face_id == face_id
21770 && glyph->glyph_not_available_p == glyph_not_available_p)
21772 int two_byte_p;
21774 s->face = get_glyph_face_and_encoding (s->f, glyph,
21775 s->char2b + s->nchars,
21776 &two_byte_p);
21777 s->two_byte_p = two_byte_p;
21778 ++s->nchars;
21779 xassert (s->nchars <= end - start);
21780 s->width += glyph->pixel_width;
21781 if (glyph++->padding_p != s->padding_p)
21782 break;
21785 s->font = s->face->font;
21787 /* If the specified font could not be loaded, use the frame's font,
21788 but record the fact that we couldn't load it in
21789 S->font_not_found_p so that we can draw rectangles for the
21790 characters of the glyph string. */
21791 if (s->font == NULL || glyph_not_available_p)
21793 s->font_not_found_p = 1;
21794 s->font = FRAME_FONT (s->f);
21797 /* Adjust base line for subscript/superscript text. */
21798 s->ybase += voffset;
21800 xassert (s->face && s->face->gc);
21801 return glyph - s->row->glyphs[s->area];
21805 /* Fill glyph string S from image glyph S->first_glyph. */
21807 static void
21808 fill_image_glyph_string (struct glyph_string *s)
21810 xassert (s->first_glyph->type == IMAGE_GLYPH);
21811 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21812 xassert (s->img);
21813 s->slice = s->first_glyph->slice.img;
21814 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21815 s->font = s->face->font;
21816 s->width = s->first_glyph->pixel_width;
21818 /* Adjust base line for subscript/superscript text. */
21819 s->ybase += s->first_glyph->voffset;
21823 /* Fill glyph string S from a sequence of stretch glyphs.
21825 START is the index of the first glyph to consider,
21826 END is the index of the last + 1.
21828 Value is the index of the first glyph not in S. */
21830 static int
21831 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21833 struct glyph *glyph, *last;
21834 int voffset, face_id;
21836 xassert (s->first_glyph->type == STRETCH_GLYPH);
21838 glyph = s->row->glyphs[s->area] + start;
21839 last = s->row->glyphs[s->area] + end;
21840 face_id = glyph->face_id;
21841 s->face = FACE_FROM_ID (s->f, face_id);
21842 s->font = s->face->font;
21843 s->width = glyph->pixel_width;
21844 s->nchars = 1;
21845 voffset = glyph->voffset;
21847 for (++glyph;
21848 (glyph < last
21849 && glyph->type == STRETCH_GLYPH
21850 && glyph->voffset == voffset
21851 && glyph->face_id == face_id);
21852 ++glyph)
21853 s->width += glyph->pixel_width;
21855 /* Adjust base line for subscript/superscript text. */
21856 s->ybase += voffset;
21858 /* The case that face->gc == 0 is handled when drawing the glyph
21859 string by calling PREPARE_FACE_FOR_DISPLAY. */
21860 xassert (s->face);
21861 return glyph - s->row->glyphs[s->area];
21864 static struct font_metrics *
21865 get_per_char_metric (struct font *font, XChar2b *char2b)
21867 static struct font_metrics metrics;
21868 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21870 if (! font || code == FONT_INVALID_CODE)
21871 return NULL;
21872 font->driver->text_extents (font, &code, 1, &metrics);
21873 return &metrics;
21876 /* EXPORT for RIF:
21877 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21878 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21879 assumed to be zero. */
21881 void
21882 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21884 *left = *right = 0;
21886 if (glyph->type == CHAR_GLYPH)
21888 struct face *face;
21889 XChar2b char2b;
21890 struct font_metrics *pcm;
21892 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21893 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21895 if (pcm->rbearing > pcm->width)
21896 *right = pcm->rbearing - pcm->width;
21897 if (pcm->lbearing < 0)
21898 *left = -pcm->lbearing;
21901 else if (glyph->type == COMPOSITE_GLYPH)
21903 if (! glyph->u.cmp.automatic)
21905 struct composition *cmp = composition_table[glyph->u.cmp.id];
21907 if (cmp->rbearing > cmp->pixel_width)
21908 *right = cmp->rbearing - cmp->pixel_width;
21909 if (cmp->lbearing < 0)
21910 *left = - cmp->lbearing;
21912 else
21914 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21915 struct font_metrics metrics;
21917 composition_gstring_width (gstring, glyph->slice.cmp.from,
21918 glyph->slice.cmp.to + 1, &metrics);
21919 if (metrics.rbearing > metrics.width)
21920 *right = metrics.rbearing - metrics.width;
21921 if (metrics.lbearing < 0)
21922 *left = - metrics.lbearing;
21928 /* Return the index of the first glyph preceding glyph string S that
21929 is overwritten by S because of S's left overhang. Value is -1
21930 if no glyphs are overwritten. */
21932 static int
21933 left_overwritten (struct glyph_string *s)
21935 int k;
21937 if (s->left_overhang)
21939 int x = 0, i;
21940 struct glyph *glyphs = s->row->glyphs[s->area];
21941 int first = s->first_glyph - glyphs;
21943 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21944 x -= glyphs[i].pixel_width;
21946 k = i + 1;
21948 else
21949 k = -1;
21951 return k;
21955 /* Return the index of the first glyph preceding glyph string S that
21956 is overwriting S because of its right overhang. Value is -1 if no
21957 glyph in front of S overwrites S. */
21959 static int
21960 left_overwriting (struct glyph_string *s)
21962 int i, k, x;
21963 struct glyph *glyphs = s->row->glyphs[s->area];
21964 int first = s->first_glyph - glyphs;
21966 k = -1;
21967 x = 0;
21968 for (i = first - 1; i >= 0; --i)
21970 int left, right;
21971 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21972 if (x + right > 0)
21973 k = i;
21974 x -= glyphs[i].pixel_width;
21977 return k;
21981 /* Return the index of the last glyph following glyph string S that is
21982 overwritten by S because of S's right overhang. Value is -1 if
21983 no such glyph is found. */
21985 static int
21986 right_overwritten (struct glyph_string *s)
21988 int k = -1;
21990 if (s->right_overhang)
21992 int x = 0, i;
21993 struct glyph *glyphs = s->row->glyphs[s->area];
21994 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21995 int end = s->row->used[s->area];
21997 for (i = first; i < end && s->right_overhang > x; ++i)
21998 x += glyphs[i].pixel_width;
22000 k = i;
22003 return k;
22007 /* Return the index of the last glyph following glyph string S that
22008 overwrites S because of its left overhang. Value is negative
22009 if no such glyph is found. */
22011 static int
22012 right_overwriting (struct glyph_string *s)
22014 int i, k, x;
22015 int end = s->row->used[s->area];
22016 struct glyph *glyphs = s->row->glyphs[s->area];
22017 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22019 k = -1;
22020 x = 0;
22021 for (i = first; i < end; ++i)
22023 int left, right;
22024 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22025 if (x - left < 0)
22026 k = i;
22027 x += glyphs[i].pixel_width;
22030 return k;
22034 /* Set background width of glyph string S. START is the index of the
22035 first glyph following S. LAST_X is the right-most x-position + 1
22036 in the drawing area. */
22038 static inline void
22039 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22041 /* If the face of this glyph string has to be drawn to the end of
22042 the drawing area, set S->extends_to_end_of_line_p. */
22044 if (start == s->row->used[s->area]
22045 && s->area == TEXT_AREA
22046 && ((s->row->fill_line_p
22047 && (s->hl == DRAW_NORMAL_TEXT
22048 || s->hl == DRAW_IMAGE_RAISED
22049 || s->hl == DRAW_IMAGE_SUNKEN))
22050 || s->hl == DRAW_MOUSE_FACE))
22051 s->extends_to_end_of_line_p = 1;
22053 /* If S extends its face to the end of the line, set its
22054 background_width to the distance to the right edge of the drawing
22055 area. */
22056 if (s->extends_to_end_of_line_p)
22057 s->background_width = last_x - s->x + 1;
22058 else
22059 s->background_width = s->width;
22063 /* Compute overhangs and x-positions for glyph string S and its
22064 predecessors, or successors. X is the starting x-position for S.
22065 BACKWARD_P non-zero means process predecessors. */
22067 static void
22068 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22070 if (backward_p)
22072 while (s)
22074 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22075 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22076 x -= s->width;
22077 s->x = x;
22078 s = s->prev;
22081 else
22083 while (s)
22085 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22086 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22087 s->x = x;
22088 x += s->width;
22089 s = s->next;
22096 /* The following macros are only called from draw_glyphs below.
22097 They reference the following parameters of that function directly:
22098 `w', `row', `area', and `overlap_p'
22099 as well as the following local variables:
22100 `s', `f', and `hdc' (in W32) */
22102 #ifdef HAVE_NTGUI
22103 /* On W32, silently add local `hdc' variable to argument list of
22104 init_glyph_string. */
22105 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22106 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22107 #else
22108 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22109 init_glyph_string (s, char2b, w, row, area, start, hl)
22110 #endif
22112 /* Add a glyph string for a stretch glyph to the list of strings
22113 between HEAD and TAIL. START is the index of the stretch glyph in
22114 row area AREA of glyph row ROW. END is the index of the last glyph
22115 in that glyph row area. X is the current output position assigned
22116 to the new glyph string constructed. HL overrides that face of the
22117 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22118 is the right-most x-position of the drawing area. */
22120 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22121 and below -- keep them on one line. */
22122 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22123 do \
22125 s = (struct glyph_string *) alloca (sizeof *s); \
22126 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22127 START = fill_stretch_glyph_string (s, START, END); \
22128 append_glyph_string (&HEAD, &TAIL, s); \
22129 s->x = (X); \
22131 while (0)
22134 /* Add a glyph string for an image glyph to the list of strings
22135 between HEAD and TAIL. START is the index of the image glyph in
22136 row area AREA of glyph row ROW. END is the index of the last glyph
22137 in that glyph row area. X is the current output position assigned
22138 to the new glyph string constructed. HL overrides that face of the
22139 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22140 is the right-most x-position of the drawing area. */
22142 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22143 do \
22145 s = (struct glyph_string *) alloca (sizeof *s); \
22146 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22147 fill_image_glyph_string (s); \
22148 append_glyph_string (&HEAD, &TAIL, s); \
22149 ++START; \
22150 s->x = (X); \
22152 while (0)
22155 /* Add a glyph string for a sequence of character glyphs to the list
22156 of strings between HEAD and TAIL. START is the index of the first
22157 glyph in row area AREA of glyph row ROW that is part of the new
22158 glyph string. END is the index of the last glyph in that glyph row
22159 area. X is the current output position assigned to the new glyph
22160 string constructed. HL overrides that face of the glyph; e.g. it
22161 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22162 right-most x-position of the drawing area. */
22164 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22165 do \
22167 int face_id; \
22168 XChar2b *char2b; \
22170 face_id = (row)->glyphs[area][START].face_id; \
22172 s = (struct glyph_string *) alloca (sizeof *s); \
22173 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22174 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22175 append_glyph_string (&HEAD, &TAIL, s); \
22176 s->x = (X); \
22177 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22179 while (0)
22182 /* Add a glyph string for a composite sequence to the list of strings
22183 between HEAD and TAIL. START is the index of the first glyph in
22184 row area AREA of glyph row ROW that is part of the new glyph
22185 string. END is the index of the last glyph in that glyph row area.
22186 X is the current output position assigned to the new glyph string
22187 constructed. HL overrides that face of the glyph; e.g. it is
22188 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22189 x-position of the drawing area. */
22191 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22192 do { \
22193 int face_id = (row)->glyphs[area][START].face_id; \
22194 struct face *base_face = FACE_FROM_ID (f, face_id); \
22195 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22196 struct composition *cmp = composition_table[cmp_id]; \
22197 XChar2b *char2b; \
22198 struct glyph_string *first_s IF_LINT (= NULL); \
22199 int n; \
22201 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22203 /* Make glyph_strings for each glyph sequence that is drawable by \
22204 the same face, and append them to HEAD/TAIL. */ \
22205 for (n = 0; n < cmp->glyph_len;) \
22207 s = (struct glyph_string *) alloca (sizeof *s); \
22208 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22209 append_glyph_string (&(HEAD), &(TAIL), s); \
22210 s->cmp = cmp; \
22211 s->cmp_from = n; \
22212 s->x = (X); \
22213 if (n == 0) \
22214 first_s = s; \
22215 n = fill_composite_glyph_string (s, base_face, overlaps); \
22218 ++START; \
22219 s = first_s; \
22220 } while (0)
22223 /* Add a glyph string for a glyph-string sequence to the list of strings
22224 between HEAD and TAIL. */
22226 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22227 do { \
22228 int face_id; \
22229 XChar2b *char2b; \
22230 Lisp_Object gstring; \
22232 face_id = (row)->glyphs[area][START].face_id; \
22233 gstring = (composition_gstring_from_id \
22234 ((row)->glyphs[area][START].u.cmp.id)); \
22235 s = (struct glyph_string *) alloca (sizeof *s); \
22236 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22237 * LGSTRING_GLYPH_LEN (gstring)); \
22238 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22239 append_glyph_string (&(HEAD), &(TAIL), s); \
22240 s->x = (X); \
22241 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22242 } while (0)
22245 /* Add a glyph string for a sequence of glyphless character's glyphs
22246 to the list of strings between HEAD and TAIL. The meanings of
22247 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22249 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22250 do \
22252 int face_id; \
22254 face_id = (row)->glyphs[area][START].face_id; \
22256 s = (struct glyph_string *) alloca (sizeof *s); \
22257 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22258 append_glyph_string (&HEAD, &TAIL, s); \
22259 s->x = (X); \
22260 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22261 overlaps); \
22263 while (0)
22266 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22267 of AREA of glyph row ROW on window W between indices START and END.
22268 HL overrides the face for drawing glyph strings, e.g. it is
22269 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22270 x-positions of the drawing area.
22272 This is an ugly monster macro construct because we must use alloca
22273 to allocate glyph strings (because draw_glyphs can be called
22274 asynchronously). */
22276 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22277 do \
22279 HEAD = TAIL = NULL; \
22280 while (START < END) \
22282 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22283 switch (first_glyph->type) \
22285 case CHAR_GLYPH: \
22286 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22287 HL, X, LAST_X); \
22288 break; \
22290 case COMPOSITE_GLYPH: \
22291 if (first_glyph->u.cmp.automatic) \
22292 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22293 HL, X, LAST_X); \
22294 else \
22295 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22296 HL, X, LAST_X); \
22297 break; \
22299 case STRETCH_GLYPH: \
22300 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22301 HL, X, LAST_X); \
22302 break; \
22304 case IMAGE_GLYPH: \
22305 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22306 HL, X, LAST_X); \
22307 break; \
22309 case GLYPHLESS_GLYPH: \
22310 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22311 HL, X, LAST_X); \
22312 break; \
22314 default: \
22315 abort (); \
22318 if (s) \
22320 set_glyph_string_background_width (s, START, LAST_X); \
22321 (X) += s->width; \
22324 } while (0)
22327 /* Draw glyphs between START and END in AREA of ROW on window W,
22328 starting at x-position X. X is relative to AREA in W. HL is a
22329 face-override with the following meaning:
22331 DRAW_NORMAL_TEXT draw normally
22332 DRAW_CURSOR draw in cursor face
22333 DRAW_MOUSE_FACE draw in mouse face.
22334 DRAW_INVERSE_VIDEO draw in mode line face
22335 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22336 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22338 If OVERLAPS is non-zero, draw only the foreground of characters and
22339 clip to the physical height of ROW. Non-zero value also defines
22340 the overlapping part to be drawn:
22342 OVERLAPS_PRED overlap with preceding rows
22343 OVERLAPS_SUCC overlap with succeeding rows
22344 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22345 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22347 Value is the x-position reached, relative to AREA of W. */
22349 static int
22350 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22351 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22352 enum draw_glyphs_face hl, int overlaps)
22354 struct glyph_string *head, *tail;
22355 struct glyph_string *s;
22356 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22357 int i, j, x_reached, last_x, area_left = 0;
22358 struct frame *f = XFRAME (WINDOW_FRAME (w));
22359 DECLARE_HDC (hdc);
22361 ALLOCATE_HDC (hdc, f);
22363 /* Let's rather be paranoid than getting a SEGV. */
22364 end = min (end, row->used[area]);
22365 start = max (0, start);
22366 start = min (end, start);
22368 /* Translate X to frame coordinates. Set last_x to the right
22369 end of the drawing area. */
22370 if (row->full_width_p)
22372 /* X is relative to the left edge of W, without scroll bars
22373 or fringes. */
22374 area_left = WINDOW_LEFT_EDGE_X (w);
22375 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22377 else
22379 area_left = window_box_left (w, area);
22380 last_x = area_left + window_box_width (w, area);
22382 x += area_left;
22384 /* Build a doubly-linked list of glyph_string structures between
22385 head and tail from what we have to draw. Note that the macro
22386 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22387 the reason we use a separate variable `i'. */
22388 i = start;
22389 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22390 if (tail)
22391 x_reached = tail->x + tail->background_width;
22392 else
22393 x_reached = x;
22395 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22396 the row, redraw some glyphs in front or following the glyph
22397 strings built above. */
22398 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22400 struct glyph_string *h, *t;
22401 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22402 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22403 int check_mouse_face = 0;
22404 int dummy_x = 0;
22406 /* If mouse highlighting is on, we may need to draw adjacent
22407 glyphs using mouse-face highlighting. */
22408 if (area == TEXT_AREA && row->mouse_face_p)
22410 struct glyph_row *mouse_beg_row, *mouse_end_row;
22412 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22413 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22415 if (row >= mouse_beg_row && row <= mouse_end_row)
22417 check_mouse_face = 1;
22418 mouse_beg_col = (row == mouse_beg_row)
22419 ? hlinfo->mouse_face_beg_col : 0;
22420 mouse_end_col = (row == mouse_end_row)
22421 ? hlinfo->mouse_face_end_col
22422 : row->used[TEXT_AREA];
22426 /* Compute overhangs for all glyph strings. */
22427 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22428 for (s = head; s; s = s->next)
22429 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22431 /* Prepend glyph strings for glyphs in front of the first glyph
22432 string that are overwritten because of the first glyph
22433 string's left overhang. The background of all strings
22434 prepended must be drawn because the first glyph string
22435 draws over it. */
22436 i = left_overwritten (head);
22437 if (i >= 0)
22439 enum draw_glyphs_face overlap_hl;
22441 /* If this row contains mouse highlighting, attempt to draw
22442 the overlapped glyphs with the correct highlight. This
22443 code fails if the overlap encompasses more than one glyph
22444 and mouse-highlight spans only some of these glyphs.
22445 However, making it work perfectly involves a lot more
22446 code, and I don't know if the pathological case occurs in
22447 practice, so we'll stick to this for now. --- cyd */
22448 if (check_mouse_face
22449 && mouse_beg_col < start && mouse_end_col > i)
22450 overlap_hl = DRAW_MOUSE_FACE;
22451 else
22452 overlap_hl = DRAW_NORMAL_TEXT;
22454 j = i;
22455 BUILD_GLYPH_STRINGS (j, start, h, t,
22456 overlap_hl, dummy_x, last_x);
22457 start = i;
22458 compute_overhangs_and_x (t, head->x, 1);
22459 prepend_glyph_string_lists (&head, &tail, h, t);
22460 clip_head = head;
22463 /* Prepend glyph strings for glyphs in front of the first glyph
22464 string that overwrite that glyph string because of their
22465 right overhang. For these strings, only the foreground must
22466 be drawn, because it draws over the glyph string at `head'.
22467 The background must not be drawn because this would overwrite
22468 right overhangs of preceding glyphs for which no glyph
22469 strings exist. */
22470 i = left_overwriting (head);
22471 if (i >= 0)
22473 enum draw_glyphs_face overlap_hl;
22475 if (check_mouse_face
22476 && mouse_beg_col < start && mouse_end_col > i)
22477 overlap_hl = DRAW_MOUSE_FACE;
22478 else
22479 overlap_hl = DRAW_NORMAL_TEXT;
22481 clip_head = head;
22482 BUILD_GLYPH_STRINGS (i, start, h, t,
22483 overlap_hl, dummy_x, last_x);
22484 for (s = h; s; s = s->next)
22485 s->background_filled_p = 1;
22486 compute_overhangs_and_x (t, head->x, 1);
22487 prepend_glyph_string_lists (&head, &tail, h, t);
22490 /* Append glyphs strings for glyphs following the last glyph
22491 string tail that are overwritten by tail. The background of
22492 these strings has to be drawn because tail's foreground draws
22493 over it. */
22494 i = right_overwritten (tail);
22495 if (i >= 0)
22497 enum draw_glyphs_face overlap_hl;
22499 if (check_mouse_face
22500 && mouse_beg_col < i && mouse_end_col > end)
22501 overlap_hl = DRAW_MOUSE_FACE;
22502 else
22503 overlap_hl = DRAW_NORMAL_TEXT;
22505 BUILD_GLYPH_STRINGS (end, i, h, t,
22506 overlap_hl, x, last_x);
22507 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22508 we don't have `end = i;' here. */
22509 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22510 append_glyph_string_lists (&head, &tail, h, t);
22511 clip_tail = tail;
22514 /* Append glyph strings for glyphs following the last glyph
22515 string tail that overwrite tail. The foreground of such
22516 glyphs has to be drawn because it writes into the background
22517 of tail. The background must not be drawn because it could
22518 paint over the foreground of following glyphs. */
22519 i = right_overwriting (tail);
22520 if (i >= 0)
22522 enum draw_glyphs_face overlap_hl;
22523 if (check_mouse_face
22524 && mouse_beg_col < i && mouse_end_col > end)
22525 overlap_hl = DRAW_MOUSE_FACE;
22526 else
22527 overlap_hl = DRAW_NORMAL_TEXT;
22529 clip_tail = tail;
22530 i++; /* We must include the Ith glyph. */
22531 BUILD_GLYPH_STRINGS (end, i, h, t,
22532 overlap_hl, x, last_x);
22533 for (s = h; s; s = s->next)
22534 s->background_filled_p = 1;
22535 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22536 append_glyph_string_lists (&head, &tail, h, t);
22538 if (clip_head || clip_tail)
22539 for (s = head; s; s = s->next)
22541 s->clip_head = clip_head;
22542 s->clip_tail = clip_tail;
22546 /* Draw all strings. */
22547 for (s = head; s; s = s->next)
22548 FRAME_RIF (f)->draw_glyph_string (s);
22550 #ifndef HAVE_NS
22551 /* When focus a sole frame and move horizontally, this sets on_p to 0
22552 causing a failure to erase prev cursor position. */
22553 if (area == TEXT_AREA
22554 && !row->full_width_p
22555 /* When drawing overlapping rows, only the glyph strings'
22556 foreground is drawn, which doesn't erase a cursor
22557 completely. */
22558 && !overlaps)
22560 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22561 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22562 : (tail ? tail->x + tail->background_width : x));
22563 x0 -= area_left;
22564 x1 -= area_left;
22566 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22567 row->y, MATRIX_ROW_BOTTOM_Y (row));
22569 #endif
22571 /* Value is the x-position up to which drawn, relative to AREA of W.
22572 This doesn't include parts drawn because of overhangs. */
22573 if (row->full_width_p)
22574 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22575 else
22576 x_reached -= area_left;
22578 RELEASE_HDC (hdc, f);
22580 return x_reached;
22583 /* Expand row matrix if too narrow. Don't expand if area
22584 is not present. */
22586 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22588 if (!fonts_changed_p \
22589 && (it->glyph_row->glyphs[area] \
22590 < it->glyph_row->glyphs[area + 1])) \
22592 it->w->ncols_scale_factor++; \
22593 fonts_changed_p = 1; \
22597 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22598 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22600 static inline void
22601 append_glyph (struct it *it)
22603 struct glyph *glyph;
22604 enum glyph_row_area area = it->area;
22606 xassert (it->glyph_row);
22607 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22609 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22610 if (glyph < it->glyph_row->glyphs[area + 1])
22612 /* If the glyph row is reversed, we need to prepend the glyph
22613 rather than append it. */
22614 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22616 struct glyph *g;
22618 /* Make room for the additional glyph. */
22619 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22620 g[1] = *g;
22621 glyph = it->glyph_row->glyphs[area];
22623 glyph->charpos = CHARPOS (it->position);
22624 glyph->object = it->object;
22625 if (it->pixel_width > 0)
22627 glyph->pixel_width = it->pixel_width;
22628 glyph->padding_p = 0;
22630 else
22632 /* Assure at least 1-pixel width. Otherwise, cursor can't
22633 be displayed correctly. */
22634 glyph->pixel_width = 1;
22635 glyph->padding_p = 1;
22637 glyph->ascent = it->ascent;
22638 glyph->descent = it->descent;
22639 glyph->voffset = it->voffset;
22640 glyph->type = CHAR_GLYPH;
22641 glyph->avoid_cursor_p = it->avoid_cursor_p;
22642 glyph->multibyte_p = it->multibyte_p;
22643 glyph->left_box_line_p = it->start_of_box_run_p;
22644 glyph->right_box_line_p = it->end_of_box_run_p;
22645 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22646 || it->phys_descent > it->descent);
22647 glyph->glyph_not_available_p = it->glyph_not_available_p;
22648 glyph->face_id = it->face_id;
22649 glyph->u.ch = it->char_to_display;
22650 glyph->slice.img = null_glyph_slice;
22651 glyph->font_type = FONT_TYPE_UNKNOWN;
22652 if (it->bidi_p)
22654 glyph->resolved_level = it->bidi_it.resolved_level;
22655 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22656 abort ();
22657 glyph->bidi_type = it->bidi_it.type;
22659 else
22661 glyph->resolved_level = 0;
22662 glyph->bidi_type = UNKNOWN_BT;
22664 ++it->glyph_row->used[area];
22666 else
22667 IT_EXPAND_MATRIX_WIDTH (it, area);
22670 /* Store one glyph for the composition IT->cmp_it.id in
22671 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22672 non-null. */
22674 static inline void
22675 append_composite_glyph (struct it *it)
22677 struct glyph *glyph;
22678 enum glyph_row_area area = it->area;
22680 xassert (it->glyph_row);
22682 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22683 if (glyph < it->glyph_row->glyphs[area + 1])
22685 /* If the glyph row is reversed, we need to prepend the glyph
22686 rather than append it. */
22687 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22689 struct glyph *g;
22691 /* Make room for the new glyph. */
22692 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22693 g[1] = *g;
22694 glyph = it->glyph_row->glyphs[it->area];
22696 glyph->charpos = it->cmp_it.charpos;
22697 glyph->object = it->object;
22698 glyph->pixel_width = it->pixel_width;
22699 glyph->ascent = it->ascent;
22700 glyph->descent = it->descent;
22701 glyph->voffset = it->voffset;
22702 glyph->type = COMPOSITE_GLYPH;
22703 if (it->cmp_it.ch < 0)
22705 glyph->u.cmp.automatic = 0;
22706 glyph->u.cmp.id = it->cmp_it.id;
22707 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22709 else
22711 glyph->u.cmp.automatic = 1;
22712 glyph->u.cmp.id = it->cmp_it.id;
22713 glyph->slice.cmp.from = it->cmp_it.from;
22714 glyph->slice.cmp.to = it->cmp_it.to - 1;
22716 glyph->avoid_cursor_p = it->avoid_cursor_p;
22717 glyph->multibyte_p = it->multibyte_p;
22718 glyph->left_box_line_p = it->start_of_box_run_p;
22719 glyph->right_box_line_p = it->end_of_box_run_p;
22720 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22721 || it->phys_descent > it->descent);
22722 glyph->padding_p = 0;
22723 glyph->glyph_not_available_p = 0;
22724 glyph->face_id = it->face_id;
22725 glyph->font_type = FONT_TYPE_UNKNOWN;
22726 if (it->bidi_p)
22728 glyph->resolved_level = it->bidi_it.resolved_level;
22729 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22730 abort ();
22731 glyph->bidi_type = it->bidi_it.type;
22733 ++it->glyph_row->used[area];
22735 else
22736 IT_EXPAND_MATRIX_WIDTH (it, area);
22740 /* Change IT->ascent and IT->height according to the setting of
22741 IT->voffset. */
22743 static inline void
22744 take_vertical_position_into_account (struct it *it)
22746 if (it->voffset)
22748 if (it->voffset < 0)
22749 /* Increase the ascent so that we can display the text higher
22750 in the line. */
22751 it->ascent -= it->voffset;
22752 else
22753 /* Increase the descent so that we can display the text lower
22754 in the line. */
22755 it->descent += it->voffset;
22760 /* Produce glyphs/get display metrics for the image IT is loaded with.
22761 See the description of struct display_iterator in dispextern.h for
22762 an overview of struct display_iterator. */
22764 static void
22765 produce_image_glyph (struct it *it)
22767 struct image *img;
22768 struct face *face;
22769 int glyph_ascent, crop;
22770 struct glyph_slice slice;
22772 xassert (it->what == IT_IMAGE);
22774 face = FACE_FROM_ID (it->f, it->face_id);
22775 xassert (face);
22776 /* Make sure X resources of the face is loaded. */
22777 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22779 if (it->image_id < 0)
22781 /* Fringe bitmap. */
22782 it->ascent = it->phys_ascent = 0;
22783 it->descent = it->phys_descent = 0;
22784 it->pixel_width = 0;
22785 it->nglyphs = 0;
22786 return;
22789 img = IMAGE_FROM_ID (it->f, it->image_id);
22790 xassert (img);
22791 /* Make sure X resources of the image is loaded. */
22792 prepare_image_for_display (it->f, img);
22794 slice.x = slice.y = 0;
22795 slice.width = img->width;
22796 slice.height = img->height;
22798 if (INTEGERP (it->slice.x))
22799 slice.x = XINT (it->slice.x);
22800 else if (FLOATP (it->slice.x))
22801 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22803 if (INTEGERP (it->slice.y))
22804 slice.y = XINT (it->slice.y);
22805 else if (FLOATP (it->slice.y))
22806 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22808 if (INTEGERP (it->slice.width))
22809 slice.width = XINT (it->slice.width);
22810 else if (FLOATP (it->slice.width))
22811 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22813 if (INTEGERP (it->slice.height))
22814 slice.height = XINT (it->slice.height);
22815 else if (FLOATP (it->slice.height))
22816 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22818 if (slice.x >= img->width)
22819 slice.x = img->width;
22820 if (slice.y >= img->height)
22821 slice.y = img->height;
22822 if (slice.x + slice.width >= img->width)
22823 slice.width = img->width - slice.x;
22824 if (slice.y + slice.height > img->height)
22825 slice.height = img->height - slice.y;
22827 if (slice.width == 0 || slice.height == 0)
22828 return;
22830 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22832 it->descent = slice.height - glyph_ascent;
22833 if (slice.y == 0)
22834 it->descent += img->vmargin;
22835 if (slice.y + slice.height == img->height)
22836 it->descent += img->vmargin;
22837 it->phys_descent = it->descent;
22839 it->pixel_width = slice.width;
22840 if (slice.x == 0)
22841 it->pixel_width += img->hmargin;
22842 if (slice.x + slice.width == img->width)
22843 it->pixel_width += img->hmargin;
22845 /* It's quite possible for images to have an ascent greater than
22846 their height, so don't get confused in that case. */
22847 if (it->descent < 0)
22848 it->descent = 0;
22850 it->nglyphs = 1;
22852 if (face->box != FACE_NO_BOX)
22854 if (face->box_line_width > 0)
22856 if (slice.y == 0)
22857 it->ascent += face->box_line_width;
22858 if (slice.y + slice.height == img->height)
22859 it->descent += face->box_line_width;
22862 if (it->start_of_box_run_p && slice.x == 0)
22863 it->pixel_width += eabs (face->box_line_width);
22864 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22865 it->pixel_width += eabs (face->box_line_width);
22868 take_vertical_position_into_account (it);
22870 /* Automatically crop wide image glyphs at right edge so we can
22871 draw the cursor on same display row. */
22872 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22873 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22875 it->pixel_width -= crop;
22876 slice.width -= crop;
22879 if (it->glyph_row)
22881 struct glyph *glyph;
22882 enum glyph_row_area area = it->area;
22884 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22885 if (glyph < it->glyph_row->glyphs[area + 1])
22887 glyph->charpos = CHARPOS (it->position);
22888 glyph->object = it->object;
22889 glyph->pixel_width = it->pixel_width;
22890 glyph->ascent = glyph_ascent;
22891 glyph->descent = it->descent;
22892 glyph->voffset = it->voffset;
22893 glyph->type = IMAGE_GLYPH;
22894 glyph->avoid_cursor_p = it->avoid_cursor_p;
22895 glyph->multibyte_p = it->multibyte_p;
22896 glyph->left_box_line_p = it->start_of_box_run_p;
22897 glyph->right_box_line_p = it->end_of_box_run_p;
22898 glyph->overlaps_vertically_p = 0;
22899 glyph->padding_p = 0;
22900 glyph->glyph_not_available_p = 0;
22901 glyph->face_id = it->face_id;
22902 glyph->u.img_id = img->id;
22903 glyph->slice.img = slice;
22904 glyph->font_type = FONT_TYPE_UNKNOWN;
22905 if (it->bidi_p)
22907 glyph->resolved_level = it->bidi_it.resolved_level;
22908 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22909 abort ();
22910 glyph->bidi_type = it->bidi_it.type;
22912 ++it->glyph_row->used[area];
22914 else
22915 IT_EXPAND_MATRIX_WIDTH (it, area);
22920 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22921 of the glyph, WIDTH and HEIGHT are the width and height of the
22922 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22924 static void
22925 append_stretch_glyph (struct it *it, Lisp_Object object,
22926 int width, int height, int ascent)
22928 struct glyph *glyph;
22929 enum glyph_row_area area = it->area;
22931 xassert (ascent >= 0 && ascent <= height);
22933 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22934 if (glyph < it->glyph_row->glyphs[area + 1])
22936 /* If the glyph row is reversed, we need to prepend the glyph
22937 rather than append it. */
22938 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22940 struct glyph *g;
22942 /* Make room for the additional glyph. */
22943 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22944 g[1] = *g;
22945 glyph = it->glyph_row->glyphs[area];
22947 glyph->charpos = CHARPOS (it->position);
22948 glyph->object = object;
22949 glyph->pixel_width = width;
22950 glyph->ascent = ascent;
22951 glyph->descent = height - ascent;
22952 glyph->voffset = it->voffset;
22953 glyph->type = STRETCH_GLYPH;
22954 glyph->avoid_cursor_p = it->avoid_cursor_p;
22955 glyph->multibyte_p = it->multibyte_p;
22956 glyph->left_box_line_p = it->start_of_box_run_p;
22957 glyph->right_box_line_p = it->end_of_box_run_p;
22958 glyph->overlaps_vertically_p = 0;
22959 glyph->padding_p = 0;
22960 glyph->glyph_not_available_p = 0;
22961 glyph->face_id = it->face_id;
22962 glyph->u.stretch.ascent = ascent;
22963 glyph->u.stretch.height = height;
22964 glyph->slice.img = null_glyph_slice;
22965 glyph->font_type = FONT_TYPE_UNKNOWN;
22966 if (it->bidi_p)
22968 glyph->resolved_level = it->bidi_it.resolved_level;
22969 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22970 abort ();
22971 glyph->bidi_type = it->bidi_it.type;
22973 else
22975 glyph->resolved_level = 0;
22976 glyph->bidi_type = UNKNOWN_BT;
22978 ++it->glyph_row->used[area];
22980 else
22981 IT_EXPAND_MATRIX_WIDTH (it, area);
22985 /* Produce a stretch glyph for iterator IT. IT->object is the value
22986 of the glyph property displayed. The value must be a list
22987 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22988 being recognized:
22990 1. `:width WIDTH' specifies that the space should be WIDTH *
22991 canonical char width wide. WIDTH may be an integer or floating
22992 point number.
22994 2. `:relative-width FACTOR' specifies that the width of the stretch
22995 should be computed from the width of the first character having the
22996 `glyph' property, and should be FACTOR times that width.
22998 3. `:align-to HPOS' specifies that the space should be wide enough
22999 to reach HPOS, a value in canonical character units.
23001 Exactly one of the above pairs must be present.
23003 4. `:height HEIGHT' specifies that the height of the stretch produced
23004 should be HEIGHT, measured in canonical character units.
23006 5. `:relative-height FACTOR' specifies that the height of the
23007 stretch should be FACTOR times the height of the characters having
23008 the glyph property.
23010 Either none or exactly one of 4 or 5 must be present.
23012 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23013 of the stretch should be used for the ascent of the stretch.
23014 ASCENT must be in the range 0 <= ASCENT <= 100. */
23016 static void
23017 produce_stretch_glyph (struct it *it)
23019 /* (space :width WIDTH :height HEIGHT ...) */
23020 Lisp_Object prop, plist;
23021 int width = 0, height = 0, align_to = -1;
23022 int zero_width_ok_p = 0, zero_height_ok_p = 0;
23023 int ascent = 0;
23024 double tem;
23025 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23026 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
23028 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23030 /* List should start with `space'. */
23031 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23032 plist = XCDR (it->object);
23034 /* Compute the width of the stretch. */
23035 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23036 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23038 /* Absolute width `:width WIDTH' specified and valid. */
23039 zero_width_ok_p = 1;
23040 width = (int)tem;
23042 else if (prop = Fplist_get (plist, QCrelative_width),
23043 NUMVAL (prop) > 0)
23045 /* Relative width `:relative-width FACTOR' specified and valid.
23046 Compute the width of the characters having the `glyph'
23047 property. */
23048 struct it it2;
23049 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23051 it2 = *it;
23052 if (it->multibyte_p)
23053 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23054 else
23056 it2.c = it2.char_to_display = *p, it2.len = 1;
23057 if (! ASCII_CHAR_P (it2.c))
23058 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23061 it2.glyph_row = NULL;
23062 it2.what = IT_CHARACTER;
23063 x_produce_glyphs (&it2);
23064 width = NUMVAL (prop) * it2.pixel_width;
23066 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23067 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23069 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23070 align_to = (align_to < 0
23072 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23073 else if (align_to < 0)
23074 align_to = window_box_left_offset (it->w, TEXT_AREA);
23075 width = max (0, (int)tem + align_to - it->current_x);
23076 zero_width_ok_p = 1;
23078 else
23079 /* Nothing specified -> width defaults to canonical char width. */
23080 width = FRAME_COLUMN_WIDTH (it->f);
23082 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23083 width = 1;
23085 /* Compute height. */
23086 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23087 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23089 height = (int)tem;
23090 zero_height_ok_p = 1;
23092 else if (prop = Fplist_get (plist, QCrelative_height),
23093 NUMVAL (prop) > 0)
23094 height = FONT_HEIGHT (font) * NUMVAL (prop);
23095 else
23096 height = FONT_HEIGHT (font);
23098 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23099 height = 1;
23101 /* Compute percentage of height used for ascent. If
23102 `:ascent ASCENT' is present and valid, use that. Otherwise,
23103 derive the ascent from the font in use. */
23104 if (prop = Fplist_get (plist, QCascent),
23105 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23106 ascent = height * NUMVAL (prop) / 100.0;
23107 else if (!NILP (prop)
23108 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23109 ascent = min (max (0, (int)tem), height);
23110 else
23111 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23113 if (width > 0 && it->line_wrap != TRUNCATE
23114 && it->current_x + width > it->last_visible_x)
23115 width = it->last_visible_x - it->current_x - 1;
23117 if (width > 0 && height > 0 && it->glyph_row)
23119 Lisp_Object object = it->stack[it->sp - 1].string;
23120 if (!STRINGP (object))
23121 object = it->w->buffer;
23122 append_stretch_glyph (it, object, width, height, ascent);
23125 it->pixel_width = width;
23126 it->ascent = it->phys_ascent = ascent;
23127 it->descent = it->phys_descent = height - it->ascent;
23128 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23130 take_vertical_position_into_account (it);
23133 /* Calculate line-height and line-spacing properties.
23134 An integer value specifies explicit pixel value.
23135 A float value specifies relative value to current face height.
23136 A cons (float . face-name) specifies relative value to
23137 height of specified face font.
23139 Returns height in pixels, or nil. */
23142 static Lisp_Object
23143 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23144 int boff, int override)
23146 Lisp_Object face_name = Qnil;
23147 int ascent, descent, height;
23149 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23150 return val;
23152 if (CONSP (val))
23154 face_name = XCAR (val);
23155 val = XCDR (val);
23156 if (!NUMBERP (val))
23157 val = make_number (1);
23158 if (NILP (face_name))
23160 height = it->ascent + it->descent;
23161 goto scale;
23165 if (NILP (face_name))
23167 font = FRAME_FONT (it->f);
23168 boff = FRAME_BASELINE_OFFSET (it->f);
23170 else if (EQ (face_name, Qt))
23172 override = 0;
23174 else
23176 int face_id;
23177 struct face *face;
23179 face_id = lookup_named_face (it->f, face_name, 0);
23180 if (face_id < 0)
23181 return make_number (-1);
23183 face = FACE_FROM_ID (it->f, face_id);
23184 font = face->font;
23185 if (font == NULL)
23186 return make_number (-1);
23187 boff = font->baseline_offset;
23188 if (font->vertical_centering)
23189 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23192 ascent = FONT_BASE (font) + boff;
23193 descent = FONT_DESCENT (font) - boff;
23195 if (override)
23197 it->override_ascent = ascent;
23198 it->override_descent = descent;
23199 it->override_boff = boff;
23202 height = ascent + descent;
23204 scale:
23205 if (FLOATP (val))
23206 height = (int)(XFLOAT_DATA (val) * height);
23207 else if (INTEGERP (val))
23208 height *= XINT (val);
23210 return make_number (height);
23214 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23215 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23216 and only if this is for a character for which no font was found.
23218 If the display method (it->glyphless_method) is
23219 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23220 length of the acronym or the hexadecimal string, UPPER_XOFF and
23221 UPPER_YOFF are pixel offsets for the upper part of the string,
23222 LOWER_XOFF and LOWER_YOFF are for the lower part.
23224 For the other display methods, LEN through LOWER_YOFF are zero. */
23226 static void
23227 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23228 short upper_xoff, short upper_yoff,
23229 short lower_xoff, short lower_yoff)
23231 struct glyph *glyph;
23232 enum glyph_row_area area = it->area;
23234 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23235 if (glyph < it->glyph_row->glyphs[area + 1])
23237 /* If the glyph row is reversed, we need to prepend the glyph
23238 rather than append it. */
23239 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23241 struct glyph *g;
23243 /* Make room for the additional glyph. */
23244 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23245 g[1] = *g;
23246 glyph = it->glyph_row->glyphs[area];
23248 glyph->charpos = CHARPOS (it->position);
23249 glyph->object = it->object;
23250 glyph->pixel_width = it->pixel_width;
23251 glyph->ascent = it->ascent;
23252 glyph->descent = it->descent;
23253 glyph->voffset = it->voffset;
23254 glyph->type = GLYPHLESS_GLYPH;
23255 glyph->u.glyphless.method = it->glyphless_method;
23256 glyph->u.glyphless.for_no_font = for_no_font;
23257 glyph->u.glyphless.len = len;
23258 glyph->u.glyphless.ch = it->c;
23259 glyph->slice.glyphless.upper_xoff = upper_xoff;
23260 glyph->slice.glyphless.upper_yoff = upper_yoff;
23261 glyph->slice.glyphless.lower_xoff = lower_xoff;
23262 glyph->slice.glyphless.lower_yoff = lower_yoff;
23263 glyph->avoid_cursor_p = it->avoid_cursor_p;
23264 glyph->multibyte_p = it->multibyte_p;
23265 glyph->left_box_line_p = it->start_of_box_run_p;
23266 glyph->right_box_line_p = it->end_of_box_run_p;
23267 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23268 || it->phys_descent > it->descent);
23269 glyph->padding_p = 0;
23270 glyph->glyph_not_available_p = 0;
23271 glyph->face_id = face_id;
23272 glyph->font_type = FONT_TYPE_UNKNOWN;
23273 if (it->bidi_p)
23275 glyph->resolved_level = it->bidi_it.resolved_level;
23276 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23277 abort ();
23278 glyph->bidi_type = it->bidi_it.type;
23280 ++it->glyph_row->used[area];
23282 else
23283 IT_EXPAND_MATRIX_WIDTH (it, area);
23287 /* Produce a glyph for a glyphless character for iterator IT.
23288 IT->glyphless_method specifies which method to use for displaying
23289 the character. See the description of enum
23290 glyphless_display_method in dispextern.h for the detail.
23292 FOR_NO_FONT is nonzero if and only if this is for a character for
23293 which no font was found. ACRONYM, if non-nil, is an acronym string
23294 for the character. */
23296 static void
23297 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23299 int face_id;
23300 struct face *face;
23301 struct font *font;
23302 int base_width, base_height, width, height;
23303 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23304 int len;
23306 /* Get the metrics of the base font. We always refer to the current
23307 ASCII face. */
23308 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23309 font = face->font ? face->font : FRAME_FONT (it->f);
23310 it->ascent = FONT_BASE (font) + font->baseline_offset;
23311 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23312 base_height = it->ascent + it->descent;
23313 base_width = font->average_width;
23315 /* Get a face ID for the glyph by utilizing a cache (the same way as
23316 done for `escape-glyph' in get_next_display_element). */
23317 if (it->f == last_glyphless_glyph_frame
23318 && it->face_id == last_glyphless_glyph_face_id)
23320 face_id = last_glyphless_glyph_merged_face_id;
23322 else
23324 /* Merge the `glyphless-char' face into the current face. */
23325 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23326 last_glyphless_glyph_frame = it->f;
23327 last_glyphless_glyph_face_id = it->face_id;
23328 last_glyphless_glyph_merged_face_id = face_id;
23331 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23333 it->pixel_width = THIN_SPACE_WIDTH;
23334 len = 0;
23335 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23337 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23339 width = CHAR_WIDTH (it->c);
23340 if (width == 0)
23341 width = 1;
23342 else if (width > 4)
23343 width = 4;
23344 it->pixel_width = base_width * width;
23345 len = 0;
23346 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23348 else
23350 char buf[7];
23351 const char *str;
23352 unsigned int code[6];
23353 int upper_len;
23354 int ascent, descent;
23355 struct font_metrics metrics_upper, metrics_lower;
23357 face = FACE_FROM_ID (it->f, face_id);
23358 font = face->font ? face->font : FRAME_FONT (it->f);
23359 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23361 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23363 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23364 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23365 if (CONSP (acronym))
23366 acronym = XCAR (acronym);
23367 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23369 else
23371 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23372 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23373 str = buf;
23375 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23376 code[len] = font->driver->encode_char (font, str[len]);
23377 upper_len = (len + 1) / 2;
23378 font->driver->text_extents (font, code, upper_len,
23379 &metrics_upper);
23380 font->driver->text_extents (font, code + upper_len, len - upper_len,
23381 &metrics_lower);
23385 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23386 width = max (metrics_upper.width, metrics_lower.width) + 4;
23387 upper_xoff = upper_yoff = 2; /* the typical case */
23388 if (base_width >= width)
23390 /* Align the upper to the left, the lower to the right. */
23391 it->pixel_width = base_width;
23392 lower_xoff = base_width - 2 - metrics_lower.width;
23394 else
23396 /* Center the shorter one. */
23397 it->pixel_width = width;
23398 if (metrics_upper.width >= metrics_lower.width)
23399 lower_xoff = (width - metrics_lower.width) / 2;
23400 else
23402 /* FIXME: This code doesn't look right. It formerly was
23403 missing the "lower_xoff = 0;", which couldn't have
23404 been right since it left lower_xoff uninitialized. */
23405 lower_xoff = 0;
23406 upper_xoff = (width - metrics_upper.width) / 2;
23410 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23411 top, bottom, and between upper and lower strings. */
23412 height = (metrics_upper.ascent + metrics_upper.descent
23413 + metrics_lower.ascent + metrics_lower.descent) + 5;
23414 /* Center vertically.
23415 H:base_height, D:base_descent
23416 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23418 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23419 descent = D - H/2 + h/2;
23420 lower_yoff = descent - 2 - ld;
23421 upper_yoff = lower_yoff - la - 1 - ud; */
23422 ascent = - (it->descent - (base_height + height + 1) / 2);
23423 descent = it->descent - (base_height - height) / 2;
23424 lower_yoff = descent - 2 - metrics_lower.descent;
23425 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23426 - metrics_upper.descent);
23427 /* Don't make the height shorter than the base height. */
23428 if (height > base_height)
23430 it->ascent = ascent;
23431 it->descent = descent;
23435 it->phys_ascent = it->ascent;
23436 it->phys_descent = it->descent;
23437 if (it->glyph_row)
23438 append_glyphless_glyph (it, face_id, for_no_font, len,
23439 upper_xoff, upper_yoff,
23440 lower_xoff, lower_yoff);
23441 it->nglyphs = 1;
23442 take_vertical_position_into_account (it);
23446 /* RIF:
23447 Produce glyphs/get display metrics for the display element IT is
23448 loaded with. See the description of struct it in dispextern.h
23449 for an overview of struct it. */
23451 void
23452 x_produce_glyphs (struct it *it)
23454 int extra_line_spacing = it->extra_line_spacing;
23456 it->glyph_not_available_p = 0;
23458 if (it->what == IT_CHARACTER)
23460 XChar2b char2b;
23461 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23462 struct font *font = face->font;
23463 struct font_metrics *pcm = NULL;
23464 int boff; /* baseline offset */
23466 if (font == NULL)
23468 /* When no suitable font is found, display this character by
23469 the method specified in the first extra slot of
23470 Vglyphless_char_display. */
23471 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23473 xassert (it->what == IT_GLYPHLESS);
23474 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23475 goto done;
23478 boff = font->baseline_offset;
23479 if (font->vertical_centering)
23480 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23482 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23484 int stretched_p;
23486 it->nglyphs = 1;
23488 if (it->override_ascent >= 0)
23490 it->ascent = it->override_ascent;
23491 it->descent = it->override_descent;
23492 boff = it->override_boff;
23494 else
23496 it->ascent = FONT_BASE (font) + boff;
23497 it->descent = FONT_DESCENT (font) - boff;
23500 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23502 pcm = get_per_char_metric (font, &char2b);
23503 if (pcm->width == 0
23504 && pcm->rbearing == 0 && pcm->lbearing == 0)
23505 pcm = NULL;
23508 if (pcm)
23510 it->phys_ascent = pcm->ascent + boff;
23511 it->phys_descent = pcm->descent - boff;
23512 it->pixel_width = pcm->width;
23514 else
23516 it->glyph_not_available_p = 1;
23517 it->phys_ascent = it->ascent;
23518 it->phys_descent = it->descent;
23519 it->pixel_width = font->space_width;
23522 if (it->constrain_row_ascent_descent_p)
23524 if (it->descent > it->max_descent)
23526 it->ascent += it->descent - it->max_descent;
23527 it->descent = it->max_descent;
23529 if (it->ascent > it->max_ascent)
23531 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23532 it->ascent = it->max_ascent;
23534 it->phys_ascent = min (it->phys_ascent, it->ascent);
23535 it->phys_descent = min (it->phys_descent, it->descent);
23536 extra_line_spacing = 0;
23539 /* If this is a space inside a region of text with
23540 `space-width' property, change its width. */
23541 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23542 if (stretched_p)
23543 it->pixel_width *= XFLOATINT (it->space_width);
23545 /* If face has a box, add the box thickness to the character
23546 height. If character has a box line to the left and/or
23547 right, add the box line width to the character's width. */
23548 if (face->box != FACE_NO_BOX)
23550 int thick = face->box_line_width;
23552 if (thick > 0)
23554 it->ascent += thick;
23555 it->descent += thick;
23557 else
23558 thick = -thick;
23560 if (it->start_of_box_run_p)
23561 it->pixel_width += thick;
23562 if (it->end_of_box_run_p)
23563 it->pixel_width += thick;
23566 /* If face has an overline, add the height of the overline
23567 (1 pixel) and a 1 pixel margin to the character height. */
23568 if (face->overline_p)
23569 it->ascent += overline_margin;
23571 if (it->constrain_row_ascent_descent_p)
23573 if (it->ascent > it->max_ascent)
23574 it->ascent = it->max_ascent;
23575 if (it->descent > it->max_descent)
23576 it->descent = it->max_descent;
23579 take_vertical_position_into_account (it);
23581 /* If we have to actually produce glyphs, do it. */
23582 if (it->glyph_row)
23584 if (stretched_p)
23586 /* Translate a space with a `space-width' property
23587 into a stretch glyph. */
23588 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23589 / FONT_HEIGHT (font));
23590 append_stretch_glyph (it, it->object, it->pixel_width,
23591 it->ascent + it->descent, ascent);
23593 else
23594 append_glyph (it);
23596 /* If characters with lbearing or rbearing are displayed
23597 in this line, record that fact in a flag of the
23598 glyph row. This is used to optimize X output code. */
23599 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23600 it->glyph_row->contains_overlapping_glyphs_p = 1;
23602 if (! stretched_p && it->pixel_width == 0)
23603 /* We assure that all visible glyphs have at least 1-pixel
23604 width. */
23605 it->pixel_width = 1;
23607 else if (it->char_to_display == '\n')
23609 /* A newline has no width, but we need the height of the
23610 line. But if previous part of the line sets a height,
23611 don't increase that height */
23613 Lisp_Object height;
23614 Lisp_Object total_height = Qnil;
23616 it->override_ascent = -1;
23617 it->pixel_width = 0;
23618 it->nglyphs = 0;
23620 height = get_it_property (it, Qline_height);
23621 /* Split (line-height total-height) list */
23622 if (CONSP (height)
23623 && CONSP (XCDR (height))
23624 && NILP (XCDR (XCDR (height))))
23626 total_height = XCAR (XCDR (height));
23627 height = XCAR (height);
23629 height = calc_line_height_property (it, height, font, boff, 1);
23631 if (it->override_ascent >= 0)
23633 it->ascent = it->override_ascent;
23634 it->descent = it->override_descent;
23635 boff = it->override_boff;
23637 else
23639 it->ascent = FONT_BASE (font) + boff;
23640 it->descent = FONT_DESCENT (font) - boff;
23643 if (EQ (height, Qt))
23645 if (it->descent > it->max_descent)
23647 it->ascent += it->descent - it->max_descent;
23648 it->descent = it->max_descent;
23650 if (it->ascent > it->max_ascent)
23652 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23653 it->ascent = it->max_ascent;
23655 it->phys_ascent = min (it->phys_ascent, it->ascent);
23656 it->phys_descent = min (it->phys_descent, it->descent);
23657 it->constrain_row_ascent_descent_p = 1;
23658 extra_line_spacing = 0;
23660 else
23662 Lisp_Object spacing;
23664 it->phys_ascent = it->ascent;
23665 it->phys_descent = it->descent;
23667 if ((it->max_ascent > 0 || it->max_descent > 0)
23668 && face->box != FACE_NO_BOX
23669 && face->box_line_width > 0)
23671 it->ascent += face->box_line_width;
23672 it->descent += face->box_line_width;
23674 if (!NILP (height)
23675 && XINT (height) > it->ascent + it->descent)
23676 it->ascent = XINT (height) - it->descent;
23678 if (!NILP (total_height))
23679 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23680 else
23682 spacing = get_it_property (it, Qline_spacing);
23683 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23685 if (INTEGERP (spacing))
23687 extra_line_spacing = XINT (spacing);
23688 if (!NILP (total_height))
23689 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23693 else /* i.e. (it->char_to_display == '\t') */
23695 if (font->space_width > 0)
23697 int tab_width = it->tab_width * font->space_width;
23698 int x = it->current_x + it->continuation_lines_width;
23699 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23701 /* If the distance from the current position to the next tab
23702 stop is less than a space character width, use the
23703 tab stop after that. */
23704 if (next_tab_x - x < font->space_width)
23705 next_tab_x += tab_width;
23707 it->pixel_width = next_tab_x - x;
23708 it->nglyphs = 1;
23709 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23710 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23712 if (it->glyph_row)
23714 append_stretch_glyph (it, it->object, it->pixel_width,
23715 it->ascent + it->descent, it->ascent);
23718 else
23720 it->pixel_width = 0;
23721 it->nglyphs = 1;
23725 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23727 /* A static composition.
23729 Note: A composition is represented as one glyph in the
23730 glyph matrix. There are no padding glyphs.
23732 Important note: pixel_width, ascent, and descent are the
23733 values of what is drawn by draw_glyphs (i.e. the values of
23734 the overall glyphs composed). */
23735 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23736 int boff; /* baseline offset */
23737 struct composition *cmp = composition_table[it->cmp_it.id];
23738 int glyph_len = cmp->glyph_len;
23739 struct font *font = face->font;
23741 it->nglyphs = 1;
23743 /* If we have not yet calculated pixel size data of glyphs of
23744 the composition for the current face font, calculate them
23745 now. Theoretically, we have to check all fonts for the
23746 glyphs, but that requires much time and memory space. So,
23747 here we check only the font of the first glyph. This may
23748 lead to incorrect display, but it's very rare, and C-l
23749 (recenter-top-bottom) can correct the display anyway. */
23750 if (! cmp->font || cmp->font != font)
23752 /* Ascent and descent of the font of the first character
23753 of this composition (adjusted by baseline offset).
23754 Ascent and descent of overall glyphs should not be less
23755 than these, respectively. */
23756 int font_ascent, font_descent, font_height;
23757 /* Bounding box of the overall glyphs. */
23758 int leftmost, rightmost, lowest, highest;
23759 int lbearing, rbearing;
23760 int i, width, ascent, descent;
23761 int left_padded = 0, right_padded = 0;
23762 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23763 XChar2b char2b;
23764 struct font_metrics *pcm;
23765 int font_not_found_p;
23766 EMACS_INT pos;
23768 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23769 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23770 break;
23771 if (glyph_len < cmp->glyph_len)
23772 right_padded = 1;
23773 for (i = 0; i < glyph_len; i++)
23775 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23776 break;
23777 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23779 if (i > 0)
23780 left_padded = 1;
23782 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23783 : IT_CHARPOS (*it));
23784 /* If no suitable font is found, use the default font. */
23785 font_not_found_p = font == NULL;
23786 if (font_not_found_p)
23788 face = face->ascii_face;
23789 font = face->font;
23791 boff = font->baseline_offset;
23792 if (font->vertical_centering)
23793 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23794 font_ascent = FONT_BASE (font) + boff;
23795 font_descent = FONT_DESCENT (font) - boff;
23796 font_height = FONT_HEIGHT (font);
23798 cmp->font = (void *) font;
23800 pcm = NULL;
23801 if (! font_not_found_p)
23803 get_char_face_and_encoding (it->f, c, it->face_id,
23804 &char2b, 0);
23805 pcm = get_per_char_metric (font, &char2b);
23808 /* Initialize the bounding box. */
23809 if (pcm)
23811 width = pcm->width;
23812 ascent = pcm->ascent;
23813 descent = pcm->descent;
23814 lbearing = pcm->lbearing;
23815 rbearing = pcm->rbearing;
23817 else
23819 width = font->space_width;
23820 ascent = FONT_BASE (font);
23821 descent = FONT_DESCENT (font);
23822 lbearing = 0;
23823 rbearing = width;
23826 rightmost = width;
23827 leftmost = 0;
23828 lowest = - descent + boff;
23829 highest = ascent + boff;
23831 if (! font_not_found_p
23832 && font->default_ascent
23833 && CHAR_TABLE_P (Vuse_default_ascent)
23834 && !NILP (Faref (Vuse_default_ascent,
23835 make_number (it->char_to_display))))
23836 highest = font->default_ascent + boff;
23838 /* Draw the first glyph at the normal position. It may be
23839 shifted to right later if some other glyphs are drawn
23840 at the left. */
23841 cmp->offsets[i * 2] = 0;
23842 cmp->offsets[i * 2 + 1] = boff;
23843 cmp->lbearing = lbearing;
23844 cmp->rbearing = rbearing;
23846 /* Set cmp->offsets for the remaining glyphs. */
23847 for (i++; i < glyph_len; i++)
23849 int left, right, btm, top;
23850 int ch = COMPOSITION_GLYPH (cmp, i);
23851 int face_id;
23852 struct face *this_face;
23854 if (ch == '\t')
23855 ch = ' ';
23856 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23857 this_face = FACE_FROM_ID (it->f, face_id);
23858 font = this_face->font;
23860 if (font == NULL)
23861 pcm = NULL;
23862 else
23864 get_char_face_and_encoding (it->f, ch, face_id,
23865 &char2b, 0);
23866 pcm = get_per_char_metric (font, &char2b);
23868 if (! pcm)
23869 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23870 else
23872 width = pcm->width;
23873 ascent = pcm->ascent;
23874 descent = pcm->descent;
23875 lbearing = pcm->lbearing;
23876 rbearing = pcm->rbearing;
23877 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23879 /* Relative composition with or without
23880 alternate chars. */
23881 left = (leftmost + rightmost - width) / 2;
23882 btm = - descent + boff;
23883 if (font->relative_compose
23884 && (! CHAR_TABLE_P (Vignore_relative_composition)
23885 || NILP (Faref (Vignore_relative_composition,
23886 make_number (ch)))))
23889 if (- descent >= font->relative_compose)
23890 /* One extra pixel between two glyphs. */
23891 btm = highest + 1;
23892 else if (ascent <= 0)
23893 /* One extra pixel between two glyphs. */
23894 btm = lowest - 1 - ascent - descent;
23897 else
23899 /* A composition rule is specified by an integer
23900 value that encodes global and new reference
23901 points (GREF and NREF). GREF and NREF are
23902 specified by numbers as below:
23904 0---1---2 -- ascent
23908 9--10--11 -- center
23910 ---3---4---5--- baseline
23912 6---7---8 -- descent
23914 int rule = COMPOSITION_RULE (cmp, i);
23915 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23917 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23918 grefx = gref % 3, nrefx = nref % 3;
23919 grefy = gref / 3, nrefy = nref / 3;
23920 if (xoff)
23921 xoff = font_height * (xoff - 128) / 256;
23922 if (yoff)
23923 yoff = font_height * (yoff - 128) / 256;
23925 left = (leftmost
23926 + grefx * (rightmost - leftmost) / 2
23927 - nrefx * width / 2
23928 + xoff);
23930 btm = ((grefy == 0 ? highest
23931 : grefy == 1 ? 0
23932 : grefy == 2 ? lowest
23933 : (highest + lowest) / 2)
23934 - (nrefy == 0 ? ascent + descent
23935 : nrefy == 1 ? descent - boff
23936 : nrefy == 2 ? 0
23937 : (ascent + descent) / 2)
23938 + yoff);
23941 cmp->offsets[i * 2] = left;
23942 cmp->offsets[i * 2 + 1] = btm + descent;
23944 /* Update the bounding box of the overall glyphs. */
23945 if (width > 0)
23947 right = left + width;
23948 if (left < leftmost)
23949 leftmost = left;
23950 if (right > rightmost)
23951 rightmost = right;
23953 top = btm + descent + ascent;
23954 if (top > highest)
23955 highest = top;
23956 if (btm < lowest)
23957 lowest = btm;
23959 if (cmp->lbearing > left + lbearing)
23960 cmp->lbearing = left + lbearing;
23961 if (cmp->rbearing < left + rbearing)
23962 cmp->rbearing = left + rbearing;
23966 /* If there are glyphs whose x-offsets are negative,
23967 shift all glyphs to the right and make all x-offsets
23968 non-negative. */
23969 if (leftmost < 0)
23971 for (i = 0; i < cmp->glyph_len; i++)
23972 cmp->offsets[i * 2] -= leftmost;
23973 rightmost -= leftmost;
23974 cmp->lbearing -= leftmost;
23975 cmp->rbearing -= leftmost;
23978 if (left_padded && cmp->lbearing < 0)
23980 for (i = 0; i < cmp->glyph_len; i++)
23981 cmp->offsets[i * 2] -= cmp->lbearing;
23982 rightmost -= cmp->lbearing;
23983 cmp->rbearing -= cmp->lbearing;
23984 cmp->lbearing = 0;
23986 if (right_padded && rightmost < cmp->rbearing)
23988 rightmost = cmp->rbearing;
23991 cmp->pixel_width = rightmost;
23992 cmp->ascent = highest;
23993 cmp->descent = - lowest;
23994 if (cmp->ascent < font_ascent)
23995 cmp->ascent = font_ascent;
23996 if (cmp->descent < font_descent)
23997 cmp->descent = font_descent;
24000 if (it->glyph_row
24001 && (cmp->lbearing < 0
24002 || cmp->rbearing > cmp->pixel_width))
24003 it->glyph_row->contains_overlapping_glyphs_p = 1;
24005 it->pixel_width = cmp->pixel_width;
24006 it->ascent = it->phys_ascent = cmp->ascent;
24007 it->descent = it->phys_descent = cmp->descent;
24008 if (face->box != FACE_NO_BOX)
24010 int thick = face->box_line_width;
24012 if (thick > 0)
24014 it->ascent += thick;
24015 it->descent += thick;
24017 else
24018 thick = - thick;
24020 if (it->start_of_box_run_p)
24021 it->pixel_width += thick;
24022 if (it->end_of_box_run_p)
24023 it->pixel_width += thick;
24026 /* If face has an overline, add the height of the overline
24027 (1 pixel) and a 1 pixel margin to the character height. */
24028 if (face->overline_p)
24029 it->ascent += overline_margin;
24031 take_vertical_position_into_account (it);
24032 if (it->ascent < 0)
24033 it->ascent = 0;
24034 if (it->descent < 0)
24035 it->descent = 0;
24037 if (it->glyph_row)
24038 append_composite_glyph (it);
24040 else if (it->what == IT_COMPOSITION)
24042 /* A dynamic (automatic) composition. */
24043 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24044 Lisp_Object gstring;
24045 struct font_metrics metrics;
24047 gstring = composition_gstring_from_id (it->cmp_it.id);
24048 it->pixel_width
24049 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24050 &metrics);
24051 if (it->glyph_row
24052 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24053 it->glyph_row->contains_overlapping_glyphs_p = 1;
24054 it->ascent = it->phys_ascent = metrics.ascent;
24055 it->descent = it->phys_descent = metrics.descent;
24056 if (face->box != FACE_NO_BOX)
24058 int thick = face->box_line_width;
24060 if (thick > 0)
24062 it->ascent += thick;
24063 it->descent += thick;
24065 else
24066 thick = - thick;
24068 if (it->start_of_box_run_p)
24069 it->pixel_width += thick;
24070 if (it->end_of_box_run_p)
24071 it->pixel_width += thick;
24073 /* If face has an overline, add the height of the overline
24074 (1 pixel) and a 1 pixel margin to the character height. */
24075 if (face->overline_p)
24076 it->ascent += overline_margin;
24077 take_vertical_position_into_account (it);
24078 if (it->ascent < 0)
24079 it->ascent = 0;
24080 if (it->descent < 0)
24081 it->descent = 0;
24083 if (it->glyph_row)
24084 append_composite_glyph (it);
24086 else if (it->what == IT_GLYPHLESS)
24087 produce_glyphless_glyph (it, 0, Qnil);
24088 else if (it->what == IT_IMAGE)
24089 produce_image_glyph (it);
24090 else if (it->what == IT_STRETCH)
24091 produce_stretch_glyph (it);
24093 done:
24094 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24095 because this isn't true for images with `:ascent 100'. */
24096 xassert (it->ascent >= 0 && it->descent >= 0);
24097 if (it->area == TEXT_AREA)
24098 it->current_x += it->pixel_width;
24100 if (extra_line_spacing > 0)
24102 it->descent += extra_line_spacing;
24103 if (extra_line_spacing > it->max_extra_line_spacing)
24104 it->max_extra_line_spacing = extra_line_spacing;
24107 it->max_ascent = max (it->max_ascent, it->ascent);
24108 it->max_descent = max (it->max_descent, it->descent);
24109 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24110 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24113 /* EXPORT for RIF:
24114 Output LEN glyphs starting at START at the nominal cursor position.
24115 Advance the nominal cursor over the text. The global variable
24116 updated_window contains the window being updated, updated_row is
24117 the glyph row being updated, and updated_area is the area of that
24118 row being updated. */
24120 void
24121 x_write_glyphs (struct glyph *start, int len)
24123 int x, hpos;
24125 xassert (updated_window && updated_row);
24126 BLOCK_INPUT;
24128 /* Write glyphs. */
24130 hpos = start - updated_row->glyphs[updated_area];
24131 x = draw_glyphs (updated_window, output_cursor.x,
24132 updated_row, updated_area,
24133 hpos, hpos + len,
24134 DRAW_NORMAL_TEXT, 0);
24136 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24137 if (updated_area == TEXT_AREA
24138 && updated_window->phys_cursor_on_p
24139 && updated_window->phys_cursor.vpos == output_cursor.vpos
24140 && updated_window->phys_cursor.hpos >= hpos
24141 && updated_window->phys_cursor.hpos < hpos + len)
24142 updated_window->phys_cursor_on_p = 0;
24144 UNBLOCK_INPUT;
24146 /* Advance the output cursor. */
24147 output_cursor.hpos += len;
24148 output_cursor.x = x;
24152 /* EXPORT for RIF:
24153 Insert LEN glyphs from START at the nominal cursor position. */
24155 void
24156 x_insert_glyphs (struct glyph *start, int len)
24158 struct frame *f;
24159 struct window *w;
24160 int line_height, shift_by_width, shifted_region_width;
24161 struct glyph_row *row;
24162 struct glyph *glyph;
24163 int frame_x, frame_y;
24164 EMACS_INT hpos;
24166 xassert (updated_window && updated_row);
24167 BLOCK_INPUT;
24168 w = updated_window;
24169 f = XFRAME (WINDOW_FRAME (w));
24171 /* Get the height of the line we are in. */
24172 row = updated_row;
24173 line_height = row->height;
24175 /* Get the width of the glyphs to insert. */
24176 shift_by_width = 0;
24177 for (glyph = start; glyph < start + len; ++glyph)
24178 shift_by_width += glyph->pixel_width;
24180 /* Get the width of the region to shift right. */
24181 shifted_region_width = (window_box_width (w, updated_area)
24182 - output_cursor.x
24183 - shift_by_width);
24185 /* Shift right. */
24186 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24187 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24189 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24190 line_height, shift_by_width);
24192 /* Write the glyphs. */
24193 hpos = start - row->glyphs[updated_area];
24194 draw_glyphs (w, output_cursor.x, row, updated_area,
24195 hpos, hpos + len,
24196 DRAW_NORMAL_TEXT, 0);
24198 /* Advance the output cursor. */
24199 output_cursor.hpos += len;
24200 output_cursor.x += shift_by_width;
24201 UNBLOCK_INPUT;
24205 /* EXPORT for RIF:
24206 Erase the current text line from the nominal cursor position
24207 (inclusive) to pixel column TO_X (exclusive). The idea is that
24208 everything from TO_X onward is already erased.
24210 TO_X is a pixel position relative to updated_area of
24211 updated_window. TO_X == -1 means clear to the end of this area. */
24213 void
24214 x_clear_end_of_line (int to_x)
24216 struct frame *f;
24217 struct window *w = updated_window;
24218 int max_x, min_y, max_y;
24219 int from_x, from_y, to_y;
24221 xassert (updated_window && updated_row);
24222 f = XFRAME (w->frame);
24224 if (updated_row->full_width_p)
24225 max_x = WINDOW_TOTAL_WIDTH (w);
24226 else
24227 max_x = window_box_width (w, updated_area);
24228 max_y = window_text_bottom_y (w);
24230 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24231 of window. For TO_X > 0, truncate to end of drawing area. */
24232 if (to_x == 0)
24233 return;
24234 else if (to_x < 0)
24235 to_x = max_x;
24236 else
24237 to_x = min (to_x, max_x);
24239 to_y = min (max_y, output_cursor.y + updated_row->height);
24241 /* Notice if the cursor will be cleared by this operation. */
24242 if (!updated_row->full_width_p)
24243 notice_overwritten_cursor (w, updated_area,
24244 output_cursor.x, -1,
24245 updated_row->y,
24246 MATRIX_ROW_BOTTOM_Y (updated_row));
24248 from_x = output_cursor.x;
24250 /* Translate to frame coordinates. */
24251 if (updated_row->full_width_p)
24253 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24254 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24256 else
24258 int area_left = window_box_left (w, updated_area);
24259 from_x += area_left;
24260 to_x += area_left;
24263 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24264 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24265 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24267 /* Prevent inadvertently clearing to end of the X window. */
24268 if (to_x > from_x && to_y > from_y)
24270 BLOCK_INPUT;
24271 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24272 to_x - from_x, to_y - from_y);
24273 UNBLOCK_INPUT;
24277 #endif /* HAVE_WINDOW_SYSTEM */
24281 /***********************************************************************
24282 Cursor types
24283 ***********************************************************************/
24285 /* Value is the internal representation of the specified cursor type
24286 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24287 of the bar cursor. */
24289 static enum text_cursor_kinds
24290 get_specified_cursor_type (Lisp_Object arg, int *width)
24292 enum text_cursor_kinds type;
24294 if (NILP (arg))
24295 return NO_CURSOR;
24297 if (EQ (arg, Qbox))
24298 return FILLED_BOX_CURSOR;
24300 if (EQ (arg, Qhollow))
24301 return HOLLOW_BOX_CURSOR;
24303 if (EQ (arg, Qbar))
24305 *width = 2;
24306 return BAR_CURSOR;
24309 if (CONSP (arg)
24310 && EQ (XCAR (arg), Qbar)
24311 && INTEGERP (XCDR (arg))
24312 && XINT (XCDR (arg)) >= 0)
24314 *width = XINT (XCDR (arg));
24315 return BAR_CURSOR;
24318 if (EQ (arg, Qhbar))
24320 *width = 2;
24321 return HBAR_CURSOR;
24324 if (CONSP (arg)
24325 && EQ (XCAR (arg), Qhbar)
24326 && INTEGERP (XCDR (arg))
24327 && XINT (XCDR (arg)) >= 0)
24329 *width = XINT (XCDR (arg));
24330 return HBAR_CURSOR;
24333 /* Treat anything unknown as "hollow box cursor".
24334 It was bad to signal an error; people have trouble fixing
24335 .Xdefaults with Emacs, when it has something bad in it. */
24336 type = HOLLOW_BOX_CURSOR;
24338 return type;
24341 /* Set the default cursor types for specified frame. */
24342 void
24343 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24345 int width = 1;
24346 Lisp_Object tem;
24348 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24349 FRAME_CURSOR_WIDTH (f) = width;
24351 /* By default, set up the blink-off state depending on the on-state. */
24353 tem = Fassoc (arg, Vblink_cursor_alist);
24354 if (!NILP (tem))
24356 FRAME_BLINK_OFF_CURSOR (f)
24357 = get_specified_cursor_type (XCDR (tem), &width);
24358 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24360 else
24361 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24365 #ifdef HAVE_WINDOW_SYSTEM
24367 /* Return the cursor we want to be displayed in window W. Return
24368 width of bar/hbar cursor through WIDTH arg. Return with
24369 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24370 (i.e. if the `system caret' should track this cursor).
24372 In a mini-buffer window, we want the cursor only to appear if we
24373 are reading input from this window. For the selected window, we
24374 want the cursor type given by the frame parameter or buffer local
24375 setting of cursor-type. If explicitly marked off, draw no cursor.
24376 In all other cases, we want a hollow box cursor. */
24378 static enum text_cursor_kinds
24379 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24380 int *active_cursor)
24382 struct frame *f = XFRAME (w->frame);
24383 struct buffer *b = XBUFFER (w->buffer);
24384 int cursor_type = DEFAULT_CURSOR;
24385 Lisp_Object alt_cursor;
24386 int non_selected = 0;
24388 *active_cursor = 1;
24390 /* Echo area */
24391 if (cursor_in_echo_area
24392 && FRAME_HAS_MINIBUF_P (f)
24393 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24395 if (w == XWINDOW (echo_area_window))
24397 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24399 *width = FRAME_CURSOR_WIDTH (f);
24400 return FRAME_DESIRED_CURSOR (f);
24402 else
24403 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24406 *active_cursor = 0;
24407 non_selected = 1;
24410 /* Detect a nonselected window or nonselected frame. */
24411 else if (w != XWINDOW (f->selected_window)
24412 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24414 *active_cursor = 0;
24416 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24417 return NO_CURSOR;
24419 non_selected = 1;
24422 /* Never display a cursor in a window in which cursor-type is nil. */
24423 if (NILP (BVAR (b, cursor_type)))
24424 return NO_CURSOR;
24426 /* Get the normal cursor type for this window. */
24427 if (EQ (BVAR (b, cursor_type), Qt))
24429 cursor_type = FRAME_DESIRED_CURSOR (f);
24430 *width = FRAME_CURSOR_WIDTH (f);
24432 else
24433 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24435 /* Use cursor-in-non-selected-windows instead
24436 for non-selected window or frame. */
24437 if (non_selected)
24439 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24440 if (!EQ (Qt, alt_cursor))
24441 return get_specified_cursor_type (alt_cursor, width);
24442 /* t means modify the normal cursor type. */
24443 if (cursor_type == FILLED_BOX_CURSOR)
24444 cursor_type = HOLLOW_BOX_CURSOR;
24445 else if (cursor_type == BAR_CURSOR && *width > 1)
24446 --*width;
24447 return cursor_type;
24450 /* Use normal cursor if not blinked off. */
24451 if (!w->cursor_off_p)
24453 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24455 if (cursor_type == FILLED_BOX_CURSOR)
24457 /* Using a block cursor on large images can be very annoying.
24458 So use a hollow cursor for "large" images.
24459 If image is not transparent (no mask), also use hollow cursor. */
24460 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24461 if (img != NULL && IMAGEP (img->spec))
24463 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24464 where N = size of default frame font size.
24465 This should cover most of the "tiny" icons people may use. */
24466 if (!img->mask
24467 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24468 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24469 cursor_type = HOLLOW_BOX_CURSOR;
24472 else if (cursor_type != NO_CURSOR)
24474 /* Display current only supports BOX and HOLLOW cursors for images.
24475 So for now, unconditionally use a HOLLOW cursor when cursor is
24476 not a solid box cursor. */
24477 cursor_type = HOLLOW_BOX_CURSOR;
24480 return cursor_type;
24483 /* Cursor is blinked off, so determine how to "toggle" it. */
24485 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24486 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24487 return get_specified_cursor_type (XCDR (alt_cursor), width);
24489 /* Then see if frame has specified a specific blink off cursor type. */
24490 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24492 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24493 return FRAME_BLINK_OFF_CURSOR (f);
24496 #if 0
24497 /* Some people liked having a permanently visible blinking cursor,
24498 while others had very strong opinions against it. So it was
24499 decided to remove it. KFS 2003-09-03 */
24501 /* Finally perform built-in cursor blinking:
24502 filled box <-> hollow box
24503 wide [h]bar <-> narrow [h]bar
24504 narrow [h]bar <-> no cursor
24505 other type <-> no cursor */
24507 if (cursor_type == FILLED_BOX_CURSOR)
24508 return HOLLOW_BOX_CURSOR;
24510 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24512 *width = 1;
24513 return cursor_type;
24515 #endif
24517 return NO_CURSOR;
24521 /* Notice when the text cursor of window W has been completely
24522 overwritten by a drawing operation that outputs glyphs in AREA
24523 starting at X0 and ending at X1 in the line starting at Y0 and
24524 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24525 the rest of the line after X0 has been written. Y coordinates
24526 are window-relative. */
24528 static void
24529 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24530 int x0, int x1, int y0, int y1)
24532 int cx0, cx1, cy0, cy1;
24533 struct glyph_row *row;
24535 if (!w->phys_cursor_on_p)
24536 return;
24537 if (area != TEXT_AREA)
24538 return;
24540 if (w->phys_cursor.vpos < 0
24541 || w->phys_cursor.vpos >= w->current_matrix->nrows
24542 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24543 !(row->enabled_p && row->displays_text_p)))
24544 return;
24546 if (row->cursor_in_fringe_p)
24548 row->cursor_in_fringe_p = 0;
24549 draw_fringe_bitmap (w, row, row->reversed_p);
24550 w->phys_cursor_on_p = 0;
24551 return;
24554 cx0 = w->phys_cursor.x;
24555 cx1 = cx0 + w->phys_cursor_width;
24556 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24557 return;
24559 /* The cursor image will be completely removed from the
24560 screen if the output area intersects the cursor area in
24561 y-direction. When we draw in [y0 y1[, and some part of
24562 the cursor is at y < y0, that part must have been drawn
24563 before. When scrolling, the cursor is erased before
24564 actually scrolling, so we don't come here. When not
24565 scrolling, the rows above the old cursor row must have
24566 changed, and in this case these rows must have written
24567 over the cursor image.
24569 Likewise if part of the cursor is below y1, with the
24570 exception of the cursor being in the first blank row at
24571 the buffer and window end because update_text_area
24572 doesn't draw that row. (Except when it does, but
24573 that's handled in update_text_area.) */
24575 cy0 = w->phys_cursor.y;
24576 cy1 = cy0 + w->phys_cursor_height;
24577 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24578 return;
24580 w->phys_cursor_on_p = 0;
24583 #endif /* HAVE_WINDOW_SYSTEM */
24586 /************************************************************************
24587 Mouse Face
24588 ************************************************************************/
24590 #ifdef HAVE_WINDOW_SYSTEM
24592 /* EXPORT for RIF:
24593 Fix the display of area AREA of overlapping row ROW in window W
24594 with respect to the overlapping part OVERLAPS. */
24596 void
24597 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24598 enum glyph_row_area area, int overlaps)
24600 int i, x;
24602 BLOCK_INPUT;
24604 x = 0;
24605 for (i = 0; i < row->used[area];)
24607 if (row->glyphs[area][i].overlaps_vertically_p)
24609 int start = i, start_x = x;
24613 x += row->glyphs[area][i].pixel_width;
24614 ++i;
24616 while (i < row->used[area]
24617 && row->glyphs[area][i].overlaps_vertically_p);
24619 draw_glyphs (w, start_x, row, area,
24620 start, i,
24621 DRAW_NORMAL_TEXT, overlaps);
24623 else
24625 x += row->glyphs[area][i].pixel_width;
24626 ++i;
24630 UNBLOCK_INPUT;
24634 /* EXPORT:
24635 Draw the cursor glyph of window W in glyph row ROW. See the
24636 comment of draw_glyphs for the meaning of HL. */
24638 void
24639 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24640 enum draw_glyphs_face hl)
24642 /* If cursor hpos is out of bounds, don't draw garbage. This can
24643 happen in mini-buffer windows when switching between echo area
24644 glyphs and mini-buffer. */
24645 if ((row->reversed_p
24646 ? (w->phys_cursor.hpos >= 0)
24647 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24649 int on_p = w->phys_cursor_on_p;
24650 int x1;
24651 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24652 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24653 hl, 0);
24654 w->phys_cursor_on_p = on_p;
24656 if (hl == DRAW_CURSOR)
24657 w->phys_cursor_width = x1 - w->phys_cursor.x;
24658 /* When we erase the cursor, and ROW is overlapped by other
24659 rows, make sure that these overlapping parts of other rows
24660 are redrawn. */
24661 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24663 w->phys_cursor_width = x1 - w->phys_cursor.x;
24665 if (row > w->current_matrix->rows
24666 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24667 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24668 OVERLAPS_ERASED_CURSOR);
24670 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24671 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24672 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24673 OVERLAPS_ERASED_CURSOR);
24679 /* EXPORT:
24680 Erase the image of a cursor of window W from the screen. */
24682 void
24683 erase_phys_cursor (struct window *w)
24685 struct frame *f = XFRAME (w->frame);
24686 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24687 int hpos = w->phys_cursor.hpos;
24688 int vpos = w->phys_cursor.vpos;
24689 int mouse_face_here_p = 0;
24690 struct glyph_matrix *active_glyphs = w->current_matrix;
24691 struct glyph_row *cursor_row;
24692 struct glyph *cursor_glyph;
24693 enum draw_glyphs_face hl;
24695 /* No cursor displayed or row invalidated => nothing to do on the
24696 screen. */
24697 if (w->phys_cursor_type == NO_CURSOR)
24698 goto mark_cursor_off;
24700 /* VPOS >= active_glyphs->nrows means that window has been resized.
24701 Don't bother to erase the cursor. */
24702 if (vpos >= active_glyphs->nrows)
24703 goto mark_cursor_off;
24705 /* If row containing cursor is marked invalid, there is nothing we
24706 can do. */
24707 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24708 if (!cursor_row->enabled_p)
24709 goto mark_cursor_off;
24711 /* If line spacing is > 0, old cursor may only be partially visible in
24712 window after split-window. So adjust visible height. */
24713 cursor_row->visible_height = min (cursor_row->visible_height,
24714 window_text_bottom_y (w) - cursor_row->y);
24716 /* If row is completely invisible, don't attempt to delete a cursor which
24717 isn't there. This can happen if cursor is at top of a window, and
24718 we switch to a buffer with a header line in that window. */
24719 if (cursor_row->visible_height <= 0)
24720 goto mark_cursor_off;
24722 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24723 if (cursor_row->cursor_in_fringe_p)
24725 cursor_row->cursor_in_fringe_p = 0;
24726 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24727 goto mark_cursor_off;
24730 /* This can happen when the new row is shorter than the old one.
24731 In this case, either draw_glyphs or clear_end_of_line
24732 should have cleared the cursor. Note that we wouldn't be
24733 able to erase the cursor in this case because we don't have a
24734 cursor glyph at hand. */
24735 if ((cursor_row->reversed_p
24736 ? (w->phys_cursor.hpos < 0)
24737 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24738 goto mark_cursor_off;
24740 /* If the cursor is in the mouse face area, redisplay that when
24741 we clear the cursor. */
24742 if (! NILP (hlinfo->mouse_face_window)
24743 && coords_in_mouse_face_p (w, hpos, vpos)
24744 /* Don't redraw the cursor's spot in mouse face if it is at the
24745 end of a line (on a newline). The cursor appears there, but
24746 mouse highlighting does not. */
24747 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24748 mouse_face_here_p = 1;
24750 /* Maybe clear the display under the cursor. */
24751 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24753 int x, y, left_x;
24754 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24755 int width;
24757 cursor_glyph = get_phys_cursor_glyph (w);
24758 if (cursor_glyph == NULL)
24759 goto mark_cursor_off;
24761 width = cursor_glyph->pixel_width;
24762 left_x = window_box_left_offset (w, TEXT_AREA);
24763 x = w->phys_cursor.x;
24764 if (x < left_x)
24765 width -= left_x - x;
24766 width = min (width, window_box_width (w, TEXT_AREA) - x);
24767 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24768 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24770 if (width > 0)
24771 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24774 /* Erase the cursor by redrawing the character underneath it. */
24775 if (mouse_face_here_p)
24776 hl = DRAW_MOUSE_FACE;
24777 else
24778 hl = DRAW_NORMAL_TEXT;
24779 draw_phys_cursor_glyph (w, cursor_row, hl);
24781 mark_cursor_off:
24782 w->phys_cursor_on_p = 0;
24783 w->phys_cursor_type = NO_CURSOR;
24787 /* EXPORT:
24788 Display or clear cursor of window W. If ON is zero, clear the
24789 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24790 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24792 void
24793 display_and_set_cursor (struct window *w, int on,
24794 int hpos, int vpos, int x, int y)
24796 struct frame *f = XFRAME (w->frame);
24797 int new_cursor_type;
24798 int new_cursor_width;
24799 int active_cursor;
24800 struct glyph_row *glyph_row;
24801 struct glyph *glyph;
24803 /* This is pointless on invisible frames, and dangerous on garbaged
24804 windows and frames; in the latter case, the frame or window may
24805 be in the midst of changing its size, and x and y may be off the
24806 window. */
24807 if (! FRAME_VISIBLE_P (f)
24808 || FRAME_GARBAGED_P (f)
24809 || vpos >= w->current_matrix->nrows
24810 || hpos >= w->current_matrix->matrix_w)
24811 return;
24813 /* If cursor is off and we want it off, return quickly. */
24814 if (!on && !w->phys_cursor_on_p)
24815 return;
24817 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24818 /* If cursor row is not enabled, we don't really know where to
24819 display the cursor. */
24820 if (!glyph_row->enabled_p)
24822 w->phys_cursor_on_p = 0;
24823 return;
24826 glyph = NULL;
24827 if (!glyph_row->exact_window_width_line_p
24828 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24829 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24831 xassert (interrupt_input_blocked);
24833 /* Set new_cursor_type to the cursor we want to be displayed. */
24834 new_cursor_type = get_window_cursor_type (w, glyph,
24835 &new_cursor_width, &active_cursor);
24837 /* If cursor is currently being shown and we don't want it to be or
24838 it is in the wrong place, or the cursor type is not what we want,
24839 erase it. */
24840 if (w->phys_cursor_on_p
24841 && (!on
24842 || w->phys_cursor.x != x
24843 || w->phys_cursor.y != y
24844 || new_cursor_type != w->phys_cursor_type
24845 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24846 && new_cursor_width != w->phys_cursor_width)))
24847 erase_phys_cursor (w);
24849 /* Don't check phys_cursor_on_p here because that flag is only set
24850 to zero in some cases where we know that the cursor has been
24851 completely erased, to avoid the extra work of erasing the cursor
24852 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24853 still not be visible, or it has only been partly erased. */
24854 if (on)
24856 w->phys_cursor_ascent = glyph_row->ascent;
24857 w->phys_cursor_height = glyph_row->height;
24859 /* Set phys_cursor_.* before x_draw_.* is called because some
24860 of them may need the information. */
24861 w->phys_cursor.x = x;
24862 w->phys_cursor.y = glyph_row->y;
24863 w->phys_cursor.hpos = hpos;
24864 w->phys_cursor.vpos = vpos;
24867 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24868 new_cursor_type, new_cursor_width,
24869 on, active_cursor);
24873 /* Switch the display of W's cursor on or off, according to the value
24874 of ON. */
24876 static void
24877 update_window_cursor (struct window *w, int on)
24879 /* Don't update cursor in windows whose frame is in the process
24880 of being deleted. */
24881 if (w->current_matrix)
24883 BLOCK_INPUT;
24884 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24885 w->phys_cursor.x, w->phys_cursor.y);
24886 UNBLOCK_INPUT;
24891 /* Call update_window_cursor with parameter ON_P on all leaf windows
24892 in the window tree rooted at W. */
24894 static void
24895 update_cursor_in_window_tree (struct window *w, int on_p)
24897 while (w)
24899 if (!NILP (w->hchild))
24900 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24901 else if (!NILP (w->vchild))
24902 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24903 else
24904 update_window_cursor (w, on_p);
24906 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24911 /* EXPORT:
24912 Display the cursor on window W, or clear it, according to ON_P.
24913 Don't change the cursor's position. */
24915 void
24916 x_update_cursor (struct frame *f, int on_p)
24918 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24922 /* EXPORT:
24923 Clear the cursor of window W to background color, and mark the
24924 cursor as not shown. This is used when the text where the cursor
24925 is about to be rewritten. */
24927 void
24928 x_clear_cursor (struct window *w)
24930 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24931 update_window_cursor (w, 0);
24934 #endif /* HAVE_WINDOW_SYSTEM */
24936 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24937 and MSDOS. */
24938 static void
24939 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24940 int start_hpos, int end_hpos,
24941 enum draw_glyphs_face draw)
24943 #ifdef HAVE_WINDOW_SYSTEM
24944 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24946 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24947 return;
24949 #endif
24950 #if defined (HAVE_GPM) || defined (MSDOS)
24951 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24952 #endif
24955 /* Display the active region described by mouse_face_* according to DRAW. */
24957 static void
24958 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24960 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24961 struct frame *f = XFRAME (WINDOW_FRAME (w));
24963 if (/* If window is in the process of being destroyed, don't bother
24964 to do anything. */
24965 w->current_matrix != NULL
24966 /* Don't update mouse highlight if hidden */
24967 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24968 /* Recognize when we are called to operate on rows that don't exist
24969 anymore. This can happen when a window is split. */
24970 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24972 int phys_cursor_on_p = w->phys_cursor_on_p;
24973 struct glyph_row *row, *first, *last;
24975 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24976 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24978 for (row = first; row <= last && row->enabled_p; ++row)
24980 int start_hpos, end_hpos, start_x;
24982 /* For all but the first row, the highlight starts at column 0. */
24983 if (row == first)
24985 /* R2L rows have BEG and END in reversed order, but the
24986 screen drawing geometry is always left to right. So
24987 we need to mirror the beginning and end of the
24988 highlighted area in R2L rows. */
24989 if (!row->reversed_p)
24991 start_hpos = hlinfo->mouse_face_beg_col;
24992 start_x = hlinfo->mouse_face_beg_x;
24994 else if (row == last)
24996 start_hpos = hlinfo->mouse_face_end_col;
24997 start_x = hlinfo->mouse_face_end_x;
24999 else
25001 start_hpos = 0;
25002 start_x = 0;
25005 else if (row->reversed_p && row == last)
25007 start_hpos = hlinfo->mouse_face_end_col;
25008 start_x = hlinfo->mouse_face_end_x;
25010 else
25012 start_hpos = 0;
25013 start_x = 0;
25016 if (row == last)
25018 if (!row->reversed_p)
25019 end_hpos = hlinfo->mouse_face_end_col;
25020 else if (row == first)
25021 end_hpos = hlinfo->mouse_face_beg_col;
25022 else
25024 end_hpos = row->used[TEXT_AREA];
25025 if (draw == DRAW_NORMAL_TEXT)
25026 row->fill_line_p = 1; /* Clear to end of line */
25029 else if (row->reversed_p && row == first)
25030 end_hpos = hlinfo->mouse_face_beg_col;
25031 else
25033 end_hpos = row->used[TEXT_AREA];
25034 if (draw == DRAW_NORMAL_TEXT)
25035 row->fill_line_p = 1; /* Clear to end of line */
25038 if (end_hpos > start_hpos)
25040 draw_row_with_mouse_face (w, start_x, row,
25041 start_hpos, end_hpos, draw);
25043 row->mouse_face_p
25044 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25048 #ifdef HAVE_WINDOW_SYSTEM
25049 /* When we've written over the cursor, arrange for it to
25050 be displayed again. */
25051 if (FRAME_WINDOW_P (f)
25052 && phys_cursor_on_p && !w->phys_cursor_on_p)
25054 BLOCK_INPUT;
25055 display_and_set_cursor (w, 1,
25056 w->phys_cursor.hpos, w->phys_cursor.vpos,
25057 w->phys_cursor.x, w->phys_cursor.y);
25058 UNBLOCK_INPUT;
25060 #endif /* HAVE_WINDOW_SYSTEM */
25063 #ifdef HAVE_WINDOW_SYSTEM
25064 /* Change the mouse cursor. */
25065 if (FRAME_WINDOW_P (f))
25067 if (draw == DRAW_NORMAL_TEXT
25068 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25069 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25070 else if (draw == DRAW_MOUSE_FACE)
25071 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25072 else
25073 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25075 #endif /* HAVE_WINDOW_SYSTEM */
25078 /* EXPORT:
25079 Clear out the mouse-highlighted active region.
25080 Redraw it un-highlighted first. Value is non-zero if mouse
25081 face was actually drawn unhighlighted. */
25084 clear_mouse_face (Mouse_HLInfo *hlinfo)
25086 int cleared = 0;
25088 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25090 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25091 cleared = 1;
25094 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25095 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25096 hlinfo->mouse_face_window = Qnil;
25097 hlinfo->mouse_face_overlay = Qnil;
25098 return cleared;
25101 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25102 within the mouse face on that window. */
25103 static int
25104 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25106 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25108 /* Quickly resolve the easy cases. */
25109 if (!(WINDOWP (hlinfo->mouse_face_window)
25110 && XWINDOW (hlinfo->mouse_face_window) == w))
25111 return 0;
25112 if (vpos < hlinfo->mouse_face_beg_row
25113 || vpos > hlinfo->mouse_face_end_row)
25114 return 0;
25115 if (vpos > hlinfo->mouse_face_beg_row
25116 && vpos < hlinfo->mouse_face_end_row)
25117 return 1;
25119 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25121 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25123 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25124 return 1;
25126 else if ((vpos == hlinfo->mouse_face_beg_row
25127 && hpos >= hlinfo->mouse_face_beg_col)
25128 || (vpos == hlinfo->mouse_face_end_row
25129 && hpos < hlinfo->mouse_face_end_col))
25130 return 1;
25132 else
25134 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25136 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25137 return 1;
25139 else if ((vpos == hlinfo->mouse_face_beg_row
25140 && hpos <= hlinfo->mouse_face_beg_col)
25141 || (vpos == hlinfo->mouse_face_end_row
25142 && hpos > hlinfo->mouse_face_end_col))
25143 return 1;
25145 return 0;
25149 /* EXPORT:
25150 Non-zero if physical cursor of window W is within mouse face. */
25153 cursor_in_mouse_face_p (struct window *w)
25155 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
25160 /* Find the glyph rows START_ROW and END_ROW of window W that display
25161 characters between buffer positions START_CHARPOS and END_CHARPOS
25162 (excluding END_CHARPOS). This is similar to row_containing_pos,
25163 but is more accurate when bidi reordering makes buffer positions
25164 change non-linearly with glyph rows. */
25165 static void
25166 rows_from_pos_range (struct window *w,
25167 EMACS_INT start_charpos, EMACS_INT end_charpos,
25168 struct glyph_row **start, struct glyph_row **end)
25170 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25171 int last_y = window_text_bottom_y (w);
25172 struct glyph_row *row;
25174 *start = NULL;
25175 *end = NULL;
25177 while (!first->enabled_p
25178 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25179 first++;
25181 /* Find the START row. */
25182 for (row = first;
25183 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25184 row++)
25186 /* A row can potentially be the START row if the range of the
25187 characters it displays intersects the range
25188 [START_CHARPOS..END_CHARPOS). */
25189 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25190 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25191 /* See the commentary in row_containing_pos, for the
25192 explanation of the complicated way to check whether
25193 some position is beyond the end of the characters
25194 displayed by a row. */
25195 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25196 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25197 && !row->ends_at_zv_p
25198 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25199 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25200 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25201 && !row->ends_at_zv_p
25202 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25204 /* Found a candidate row. Now make sure at least one of the
25205 glyphs it displays has a charpos from the range
25206 [START_CHARPOS..END_CHARPOS).
25208 This is not obvious because bidi reordering could make
25209 buffer positions of a row be 1,2,3,102,101,100, and if we
25210 want to highlight characters in [50..60), we don't want
25211 this row, even though [50..60) does intersect [1..103),
25212 the range of character positions given by the row's start
25213 and end positions. */
25214 struct glyph *g = row->glyphs[TEXT_AREA];
25215 struct glyph *e = g + row->used[TEXT_AREA];
25217 while (g < e)
25219 if ((BUFFERP (g->object) || INTEGERP (g->object))
25220 && start_charpos <= g->charpos && g->charpos < end_charpos)
25221 *start = row;
25222 g++;
25224 if (*start)
25225 break;
25229 /* Find the END row. */
25230 if (!*start
25231 /* If the last row is partially visible, start looking for END
25232 from that row, instead of starting from FIRST. */
25233 && !(row->enabled_p
25234 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25235 row = first;
25236 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25238 struct glyph_row *next = row + 1;
25240 if (!next->enabled_p
25241 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25242 /* The first row >= START whose range of displayed characters
25243 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25244 is the row END + 1. */
25245 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25246 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25247 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25248 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25249 && !next->ends_at_zv_p
25250 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25251 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25252 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25253 && !next->ends_at_zv_p
25254 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25256 *end = row;
25257 break;
25259 else
25261 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25262 but none of the characters it displays are in the range, it is
25263 also END + 1. */
25264 struct glyph *g = next->glyphs[TEXT_AREA];
25265 struct glyph *e = g + next->used[TEXT_AREA];
25267 while (g < e)
25269 if ((BUFFERP (g->object) || INTEGERP (g->object))
25270 && start_charpos <= g->charpos && g->charpos < end_charpos)
25271 break;
25272 g++;
25274 if (g == e)
25276 *end = row;
25277 break;
25283 /* This function sets the mouse_face_* elements of HLINFO, assuming
25284 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25285 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25286 for the overlay or run of text properties specifying the mouse
25287 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25288 before-string and after-string that must also be highlighted.
25289 COVER_STRING, if non-nil, is a display string that may cover some
25290 or all of the highlighted text. */
25292 static void
25293 mouse_face_from_buffer_pos (Lisp_Object window,
25294 Mouse_HLInfo *hlinfo,
25295 EMACS_INT mouse_charpos,
25296 EMACS_INT start_charpos,
25297 EMACS_INT end_charpos,
25298 Lisp_Object before_string,
25299 Lisp_Object after_string,
25300 Lisp_Object cover_string)
25302 struct window *w = XWINDOW (window);
25303 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25304 struct glyph_row *r1, *r2;
25305 struct glyph *glyph, *end;
25306 EMACS_INT ignore, pos;
25307 int x;
25309 xassert (NILP (cover_string) || STRINGP (cover_string));
25310 xassert (NILP (before_string) || STRINGP (before_string));
25311 xassert (NILP (after_string) || STRINGP (after_string));
25313 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25314 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25315 if (r1 == NULL)
25316 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25317 /* If the before-string or display-string contains newlines,
25318 rows_from_pos_range skips to its last row. Move back. */
25319 if (!NILP (before_string) || !NILP (cover_string))
25321 struct glyph_row *prev;
25322 while ((prev = r1 - 1, prev >= first)
25323 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25324 && prev->used[TEXT_AREA] > 0)
25326 struct glyph *beg = prev->glyphs[TEXT_AREA];
25327 glyph = beg + prev->used[TEXT_AREA];
25328 while (--glyph >= beg && INTEGERP (glyph->object));
25329 if (glyph < beg
25330 || !(EQ (glyph->object, before_string)
25331 || EQ (glyph->object, cover_string)))
25332 break;
25333 r1 = prev;
25336 if (r2 == NULL)
25338 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25339 hlinfo->mouse_face_past_end = 1;
25341 else if (!NILP (after_string))
25343 /* If the after-string has newlines, advance to its last row. */
25344 struct glyph_row *next;
25345 struct glyph_row *last
25346 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25348 for (next = r2 + 1;
25349 next <= last
25350 && next->used[TEXT_AREA] > 0
25351 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25352 ++next)
25353 r2 = next;
25355 /* The rest of the display engine assumes that mouse_face_beg_row is
25356 either above below mouse_face_end_row or identical to it. But
25357 with bidi-reordered continued lines, the row for START_CHARPOS
25358 could be below the row for END_CHARPOS. If so, swap the rows and
25359 store them in correct order. */
25360 if (r1->y > r2->y)
25362 struct glyph_row *tem = r2;
25364 r2 = r1;
25365 r1 = tem;
25368 hlinfo->mouse_face_beg_y = r1->y;
25369 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25370 hlinfo->mouse_face_end_y = r2->y;
25371 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25373 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25374 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25375 could be anywhere in the row and in any order. The strategy
25376 below is to find the leftmost and the rightmost glyph that
25377 belongs to either of these 3 strings, or whose position is
25378 between START_CHARPOS and END_CHARPOS, and highlight all the
25379 glyphs between those two. This may cover more than just the text
25380 between START_CHARPOS and END_CHARPOS if the range of characters
25381 strides the bidi level boundary, e.g. if the beginning is in R2L
25382 text while the end is in L2R text or vice versa. */
25383 if (!r1->reversed_p)
25385 /* This row is in a left to right paragraph. Scan it left to
25386 right. */
25387 glyph = r1->glyphs[TEXT_AREA];
25388 end = glyph + r1->used[TEXT_AREA];
25389 x = r1->x;
25391 /* Skip truncation glyphs at the start of the glyph row. */
25392 if (r1->displays_text_p)
25393 for (; glyph < end
25394 && INTEGERP (glyph->object)
25395 && glyph->charpos < 0;
25396 ++glyph)
25397 x += glyph->pixel_width;
25399 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25400 or COVER_STRING, and the first glyph from buffer whose
25401 position is between START_CHARPOS and END_CHARPOS. */
25402 for (; glyph < end
25403 && !INTEGERP (glyph->object)
25404 && !EQ (glyph->object, cover_string)
25405 && !(BUFFERP (glyph->object)
25406 && (glyph->charpos >= start_charpos
25407 && glyph->charpos < end_charpos));
25408 ++glyph)
25410 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25411 are present at buffer positions between START_CHARPOS and
25412 END_CHARPOS, or if they come from an overlay. */
25413 if (EQ (glyph->object, before_string))
25415 pos = string_buffer_position (before_string,
25416 start_charpos);
25417 /* If pos == 0, it means before_string came from an
25418 overlay, not from a buffer position. */
25419 if (!pos || (pos >= start_charpos && pos < end_charpos))
25420 break;
25422 else if (EQ (glyph->object, after_string))
25424 pos = string_buffer_position (after_string, end_charpos);
25425 if (!pos || (pos >= start_charpos && pos < end_charpos))
25426 break;
25428 x += glyph->pixel_width;
25430 hlinfo->mouse_face_beg_x = x;
25431 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25433 else
25435 /* This row is in a right to left paragraph. Scan it right to
25436 left. */
25437 struct glyph *g;
25439 end = r1->glyphs[TEXT_AREA] - 1;
25440 glyph = end + r1->used[TEXT_AREA];
25442 /* Skip truncation glyphs at the start of the glyph row. */
25443 if (r1->displays_text_p)
25444 for (; glyph > end
25445 && INTEGERP (glyph->object)
25446 && glyph->charpos < 0;
25447 --glyph)
25450 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25451 or COVER_STRING, and the first glyph from buffer whose
25452 position is between START_CHARPOS and END_CHARPOS. */
25453 for (; glyph > end
25454 && !INTEGERP (glyph->object)
25455 && !EQ (glyph->object, cover_string)
25456 && !(BUFFERP (glyph->object)
25457 && (glyph->charpos >= start_charpos
25458 && glyph->charpos < end_charpos));
25459 --glyph)
25461 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25462 are present at buffer positions between START_CHARPOS and
25463 END_CHARPOS, or if they come from an overlay. */
25464 if (EQ (glyph->object, before_string))
25466 pos = string_buffer_position (before_string, start_charpos);
25467 /* If pos == 0, it means before_string came from an
25468 overlay, not from a buffer position. */
25469 if (!pos || (pos >= start_charpos && pos < end_charpos))
25470 break;
25472 else if (EQ (glyph->object, after_string))
25474 pos = string_buffer_position (after_string, end_charpos);
25475 if (!pos || (pos >= start_charpos && pos < end_charpos))
25476 break;
25480 glyph++; /* first glyph to the right of the highlighted area */
25481 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25482 x += g->pixel_width;
25483 hlinfo->mouse_face_beg_x = x;
25484 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25487 /* If the highlight ends in a different row, compute GLYPH and END
25488 for the end row. Otherwise, reuse the values computed above for
25489 the row where the highlight begins. */
25490 if (r2 != r1)
25492 if (!r2->reversed_p)
25494 glyph = r2->glyphs[TEXT_AREA];
25495 end = glyph + r2->used[TEXT_AREA];
25496 x = r2->x;
25498 else
25500 end = r2->glyphs[TEXT_AREA] - 1;
25501 glyph = end + r2->used[TEXT_AREA];
25505 if (!r2->reversed_p)
25507 /* Skip truncation and continuation glyphs near the end of the
25508 row, and also blanks and stretch glyphs inserted by
25509 extend_face_to_end_of_line. */
25510 while (end > glyph
25511 && INTEGERP ((end - 1)->object)
25512 && (end - 1)->charpos <= 0)
25513 --end;
25514 /* Scan the rest of the glyph row from the end, looking for the
25515 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25516 COVER_STRING, or whose position is between START_CHARPOS
25517 and END_CHARPOS */
25518 for (--end;
25519 end > glyph
25520 && !INTEGERP (end->object)
25521 && !EQ (end->object, cover_string)
25522 && !(BUFFERP (end->object)
25523 && (end->charpos >= start_charpos
25524 && end->charpos < end_charpos));
25525 --end)
25527 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25528 are present at buffer positions between START_CHARPOS and
25529 END_CHARPOS, or if they come from an overlay. */
25530 if (EQ (end->object, before_string))
25532 pos = string_buffer_position (before_string, start_charpos);
25533 if (!pos || (pos >= start_charpos && pos < end_charpos))
25534 break;
25536 else if (EQ (end->object, after_string))
25538 pos = string_buffer_position (after_string, end_charpos);
25539 if (!pos || (pos >= start_charpos && pos < end_charpos))
25540 break;
25543 /* Find the X coordinate of the last glyph to be highlighted. */
25544 for (; glyph <= end; ++glyph)
25545 x += glyph->pixel_width;
25547 hlinfo->mouse_face_end_x = x;
25548 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25550 else
25552 /* Skip truncation and continuation glyphs near the end of the
25553 row, and also blanks and stretch glyphs inserted by
25554 extend_face_to_end_of_line. */
25555 x = r2->x;
25556 end++;
25557 while (end < glyph
25558 && INTEGERP (end->object)
25559 && end->charpos <= 0)
25561 x += end->pixel_width;
25562 ++end;
25564 /* Scan the rest of the glyph row from the end, looking for the
25565 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25566 COVER_STRING, or whose position is between START_CHARPOS
25567 and END_CHARPOS */
25568 for ( ;
25569 end < glyph
25570 && !INTEGERP (end->object)
25571 && !EQ (end->object, cover_string)
25572 && !(BUFFERP (end->object)
25573 && (end->charpos >= start_charpos
25574 && end->charpos < end_charpos));
25575 ++end)
25577 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25578 are present at buffer positions between START_CHARPOS and
25579 END_CHARPOS, or if they come from an overlay. */
25580 if (EQ (end->object, before_string))
25582 pos = string_buffer_position (before_string, start_charpos);
25583 if (!pos || (pos >= start_charpos && pos < end_charpos))
25584 break;
25586 else if (EQ (end->object, after_string))
25588 pos = string_buffer_position (after_string, end_charpos);
25589 if (!pos || (pos >= start_charpos && pos < end_charpos))
25590 break;
25592 x += end->pixel_width;
25594 hlinfo->mouse_face_end_x = x;
25595 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25598 hlinfo->mouse_face_window = window;
25599 hlinfo->mouse_face_face_id
25600 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25601 mouse_charpos + 1,
25602 !hlinfo->mouse_face_hidden, -1);
25603 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25606 /* The following function is not used anymore (replaced with
25607 mouse_face_from_string_pos), but I leave it here for the time
25608 being, in case someone would. */
25610 #if 0 /* not used */
25612 /* Find the position of the glyph for position POS in OBJECT in
25613 window W's current matrix, and return in *X, *Y the pixel
25614 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25616 RIGHT_P non-zero means return the position of the right edge of the
25617 glyph, RIGHT_P zero means return the left edge position.
25619 If no glyph for POS exists in the matrix, return the position of
25620 the glyph with the next smaller position that is in the matrix, if
25621 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25622 exists in the matrix, return the position of the glyph with the
25623 next larger position in OBJECT.
25625 Value is non-zero if a glyph was found. */
25627 static int
25628 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25629 int *hpos, int *vpos, int *x, int *y, int right_p)
25631 int yb = window_text_bottom_y (w);
25632 struct glyph_row *r;
25633 struct glyph *best_glyph = NULL;
25634 struct glyph_row *best_row = NULL;
25635 int best_x = 0;
25637 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25638 r->enabled_p && r->y < yb;
25639 ++r)
25641 struct glyph *g = r->glyphs[TEXT_AREA];
25642 struct glyph *e = g + r->used[TEXT_AREA];
25643 int gx;
25645 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25646 if (EQ (g->object, object))
25648 if (g->charpos == pos)
25650 best_glyph = g;
25651 best_x = gx;
25652 best_row = r;
25653 goto found;
25655 else if (best_glyph == NULL
25656 || ((eabs (g->charpos - pos)
25657 < eabs (best_glyph->charpos - pos))
25658 && (right_p
25659 ? g->charpos < pos
25660 : g->charpos > pos)))
25662 best_glyph = g;
25663 best_x = gx;
25664 best_row = r;
25669 found:
25671 if (best_glyph)
25673 *x = best_x;
25674 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25676 if (right_p)
25678 *x += best_glyph->pixel_width;
25679 ++*hpos;
25682 *y = best_row->y;
25683 *vpos = best_row - w->current_matrix->rows;
25686 return best_glyph != NULL;
25688 #endif /* not used */
25690 /* Find the positions of the first and the last glyphs in window W's
25691 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25692 (assumed to be a string), and return in HLINFO's mouse_face_*
25693 members the pixel and column/row coordinates of those glyphs. */
25695 static void
25696 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25697 Lisp_Object object,
25698 EMACS_INT startpos, EMACS_INT endpos)
25700 int yb = window_text_bottom_y (w);
25701 struct glyph_row *r;
25702 struct glyph *g, *e;
25703 int gx;
25704 int found = 0;
25706 /* Find the glyph row with at least one position in the range
25707 [STARTPOS..ENDPOS], and the first glyph in that row whose
25708 position belongs to that range. */
25709 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25710 r->enabled_p && r->y < yb;
25711 ++r)
25713 if (!r->reversed_p)
25715 g = r->glyphs[TEXT_AREA];
25716 e = g + r->used[TEXT_AREA];
25717 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25718 if (EQ (g->object, object)
25719 && startpos <= g->charpos && g->charpos <= endpos)
25721 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25722 hlinfo->mouse_face_beg_y = r->y;
25723 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25724 hlinfo->mouse_face_beg_x = gx;
25725 found = 1;
25726 break;
25729 else
25731 struct glyph *g1;
25733 e = r->glyphs[TEXT_AREA];
25734 g = e + r->used[TEXT_AREA];
25735 for ( ; g > e; --g)
25736 if (EQ ((g-1)->object, object)
25737 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25739 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25740 hlinfo->mouse_face_beg_y = r->y;
25741 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25742 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25743 gx += g1->pixel_width;
25744 hlinfo->mouse_face_beg_x = gx;
25745 found = 1;
25746 break;
25749 if (found)
25750 break;
25753 if (!found)
25754 return;
25756 /* Starting with the next row, look for the first row which does NOT
25757 include any glyphs whose positions are in the range. */
25758 for (++r; r->enabled_p && r->y < yb; ++r)
25760 g = r->glyphs[TEXT_AREA];
25761 e = g + r->used[TEXT_AREA];
25762 found = 0;
25763 for ( ; g < e; ++g)
25764 if (EQ (g->object, object)
25765 && startpos <= g->charpos && g->charpos <= endpos)
25767 found = 1;
25768 break;
25770 if (!found)
25771 break;
25774 /* The highlighted region ends on the previous row. */
25775 r--;
25777 /* Set the end row and its vertical pixel coordinate. */
25778 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25779 hlinfo->mouse_face_end_y = r->y;
25781 /* Compute and set the end column and the end column's horizontal
25782 pixel coordinate. */
25783 if (!r->reversed_p)
25785 g = r->glyphs[TEXT_AREA];
25786 e = g + r->used[TEXT_AREA];
25787 for ( ; e > g; --e)
25788 if (EQ ((e-1)->object, object)
25789 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25790 break;
25791 hlinfo->mouse_face_end_col = e - g;
25793 for (gx = r->x; g < e; ++g)
25794 gx += g->pixel_width;
25795 hlinfo->mouse_face_end_x = gx;
25797 else
25799 e = r->glyphs[TEXT_AREA];
25800 g = e + r->used[TEXT_AREA];
25801 for (gx = r->x ; e < g; ++e)
25803 if (EQ (e->object, object)
25804 && startpos <= e->charpos && e->charpos <= endpos)
25805 break;
25806 gx += e->pixel_width;
25808 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25809 hlinfo->mouse_face_end_x = gx;
25813 #ifdef HAVE_WINDOW_SYSTEM
25815 /* See if position X, Y is within a hot-spot of an image. */
25817 static int
25818 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25820 if (!CONSP (hot_spot))
25821 return 0;
25823 if (EQ (XCAR (hot_spot), Qrect))
25825 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25826 Lisp_Object rect = XCDR (hot_spot);
25827 Lisp_Object tem;
25828 if (!CONSP (rect))
25829 return 0;
25830 if (!CONSP (XCAR (rect)))
25831 return 0;
25832 if (!CONSP (XCDR (rect)))
25833 return 0;
25834 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25835 return 0;
25836 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25837 return 0;
25838 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25839 return 0;
25840 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25841 return 0;
25842 return 1;
25844 else if (EQ (XCAR (hot_spot), Qcircle))
25846 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25847 Lisp_Object circ = XCDR (hot_spot);
25848 Lisp_Object lr, lx0, ly0;
25849 if (CONSP (circ)
25850 && CONSP (XCAR (circ))
25851 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25852 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25853 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25855 double r = XFLOATINT (lr);
25856 double dx = XINT (lx0) - x;
25857 double dy = XINT (ly0) - y;
25858 return (dx * dx + dy * dy <= r * r);
25861 else if (EQ (XCAR (hot_spot), Qpoly))
25863 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25864 if (VECTORP (XCDR (hot_spot)))
25866 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25867 Lisp_Object *poly = v->contents;
25868 int n = v->header.size;
25869 int i;
25870 int inside = 0;
25871 Lisp_Object lx, ly;
25872 int x0, y0;
25874 /* Need an even number of coordinates, and at least 3 edges. */
25875 if (n < 6 || n & 1)
25876 return 0;
25878 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25879 If count is odd, we are inside polygon. Pixels on edges
25880 may or may not be included depending on actual geometry of the
25881 polygon. */
25882 if ((lx = poly[n-2], !INTEGERP (lx))
25883 || (ly = poly[n-1], !INTEGERP (lx)))
25884 return 0;
25885 x0 = XINT (lx), y0 = XINT (ly);
25886 for (i = 0; i < n; i += 2)
25888 int x1 = x0, y1 = y0;
25889 if ((lx = poly[i], !INTEGERP (lx))
25890 || (ly = poly[i+1], !INTEGERP (ly)))
25891 return 0;
25892 x0 = XINT (lx), y0 = XINT (ly);
25894 /* Does this segment cross the X line? */
25895 if (x0 >= x)
25897 if (x1 >= x)
25898 continue;
25900 else if (x1 < x)
25901 continue;
25902 if (y > y0 && y > y1)
25903 continue;
25904 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25905 inside = !inside;
25907 return inside;
25910 return 0;
25913 Lisp_Object
25914 find_hot_spot (Lisp_Object map, int x, int y)
25916 while (CONSP (map))
25918 if (CONSP (XCAR (map))
25919 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25920 return XCAR (map);
25921 map = XCDR (map);
25924 return Qnil;
25927 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25928 3, 3, 0,
25929 doc: /* Lookup in image map MAP coordinates X and Y.
25930 An image map is an alist where each element has the format (AREA ID PLIST).
25931 An AREA is specified as either a rectangle, a circle, or a polygon:
25932 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25933 pixel coordinates of the upper left and bottom right corners.
25934 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25935 and the radius of the circle; r may be a float or integer.
25936 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25937 vector describes one corner in the polygon.
25938 Returns the alist element for the first matching AREA in MAP. */)
25939 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25941 if (NILP (map))
25942 return Qnil;
25944 CHECK_NUMBER (x);
25945 CHECK_NUMBER (y);
25947 return find_hot_spot (map, XINT (x), XINT (y));
25951 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25952 static void
25953 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25955 /* Do not change cursor shape while dragging mouse. */
25956 if (!NILP (do_mouse_tracking))
25957 return;
25959 if (!NILP (pointer))
25961 if (EQ (pointer, Qarrow))
25962 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25963 else if (EQ (pointer, Qhand))
25964 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25965 else if (EQ (pointer, Qtext))
25966 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25967 else if (EQ (pointer, intern ("hdrag")))
25968 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25969 #ifdef HAVE_X_WINDOWS
25970 else if (EQ (pointer, intern ("vdrag")))
25971 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25972 #endif
25973 else if (EQ (pointer, intern ("hourglass")))
25974 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25975 else if (EQ (pointer, Qmodeline))
25976 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25977 else
25978 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25981 if (cursor != No_Cursor)
25982 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25985 #endif /* HAVE_WINDOW_SYSTEM */
25987 /* Take proper action when mouse has moved to the mode or header line
25988 or marginal area AREA of window W, x-position X and y-position Y.
25989 X is relative to the start of the text display area of W, so the
25990 width of bitmap areas and scroll bars must be subtracted to get a
25991 position relative to the start of the mode line. */
25993 static void
25994 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25995 enum window_part area)
25997 struct window *w = XWINDOW (window);
25998 struct frame *f = XFRAME (w->frame);
25999 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26000 #ifdef HAVE_WINDOW_SYSTEM
26001 Display_Info *dpyinfo;
26002 #endif
26003 Cursor cursor = No_Cursor;
26004 Lisp_Object pointer = Qnil;
26005 int dx, dy, width, height;
26006 EMACS_INT charpos;
26007 Lisp_Object string, object = Qnil;
26008 Lisp_Object pos, help;
26010 Lisp_Object mouse_face;
26011 int original_x_pixel = x;
26012 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26013 struct glyph_row *row;
26015 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26017 int x0;
26018 struct glyph *end;
26020 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26021 returns them in row/column units! */
26022 string = mode_line_string (w, area, &x, &y, &charpos,
26023 &object, &dx, &dy, &width, &height);
26025 row = (area == ON_MODE_LINE
26026 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26027 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26029 /* Find the glyph under the mouse pointer. */
26030 if (row->mode_line_p && row->enabled_p)
26032 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26033 end = glyph + row->used[TEXT_AREA];
26035 for (x0 = original_x_pixel;
26036 glyph < end && x0 >= glyph->pixel_width;
26037 ++glyph)
26038 x0 -= glyph->pixel_width;
26040 if (glyph >= end)
26041 glyph = NULL;
26044 else
26046 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26047 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26048 returns them in row/column units! */
26049 string = marginal_area_string (w, area, &x, &y, &charpos,
26050 &object, &dx, &dy, &width, &height);
26053 help = Qnil;
26055 #ifdef HAVE_WINDOW_SYSTEM
26056 if (IMAGEP (object))
26058 Lisp_Object image_map, hotspot;
26059 if ((image_map = Fplist_get (XCDR (object), QCmap),
26060 !NILP (image_map))
26061 && (hotspot = find_hot_spot (image_map, dx, dy),
26062 CONSP (hotspot))
26063 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26065 Lisp_Object plist;
26067 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26068 If so, we could look for mouse-enter, mouse-leave
26069 properties in PLIST (and do something...). */
26070 hotspot = XCDR (hotspot);
26071 if (CONSP (hotspot)
26072 && (plist = XCAR (hotspot), CONSP (plist)))
26074 pointer = Fplist_get (plist, Qpointer);
26075 if (NILP (pointer))
26076 pointer = Qhand;
26077 help = Fplist_get (plist, Qhelp_echo);
26078 if (!NILP (help))
26080 help_echo_string = help;
26081 /* Is this correct? ++kfs */
26082 XSETWINDOW (help_echo_window, w);
26083 help_echo_object = w->buffer;
26084 help_echo_pos = charpos;
26088 if (NILP (pointer))
26089 pointer = Fplist_get (XCDR (object), QCpointer);
26091 #endif /* HAVE_WINDOW_SYSTEM */
26093 if (STRINGP (string))
26095 pos = make_number (charpos);
26096 /* If we're on a string with `help-echo' text property, arrange
26097 for the help to be displayed. This is done by setting the
26098 global variable help_echo_string to the help string. */
26099 if (NILP (help))
26101 help = Fget_text_property (pos, Qhelp_echo, string);
26102 if (!NILP (help))
26104 help_echo_string = help;
26105 XSETWINDOW (help_echo_window, w);
26106 help_echo_object = string;
26107 help_echo_pos = charpos;
26111 #ifdef HAVE_WINDOW_SYSTEM
26112 if (FRAME_WINDOW_P (f))
26114 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26115 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26116 if (NILP (pointer))
26117 pointer = Fget_text_property (pos, Qpointer, string);
26119 /* Change the mouse pointer according to what is under X/Y. */
26120 if (NILP (pointer)
26121 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26123 Lisp_Object map;
26124 map = Fget_text_property (pos, Qlocal_map, string);
26125 if (!KEYMAPP (map))
26126 map = Fget_text_property (pos, Qkeymap, string);
26127 if (!KEYMAPP (map))
26128 cursor = dpyinfo->vertical_scroll_bar_cursor;
26131 #endif
26133 /* Change the mouse face according to what is under X/Y. */
26134 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26135 if (!NILP (mouse_face)
26136 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26137 && glyph)
26139 Lisp_Object b, e;
26141 struct glyph * tmp_glyph;
26143 int gpos;
26144 int gseq_length;
26145 int total_pixel_width;
26146 EMACS_INT begpos, endpos, ignore;
26148 int vpos, hpos;
26150 b = Fprevious_single_property_change (make_number (charpos + 1),
26151 Qmouse_face, string, Qnil);
26152 if (NILP (b))
26153 begpos = 0;
26154 else
26155 begpos = XINT (b);
26157 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26158 if (NILP (e))
26159 endpos = SCHARS (string);
26160 else
26161 endpos = XINT (e);
26163 /* Calculate the glyph position GPOS of GLYPH in the
26164 displayed string, relative to the beginning of the
26165 highlighted part of the string.
26167 Note: GPOS is different from CHARPOS. CHARPOS is the
26168 position of GLYPH in the internal string object. A mode
26169 line string format has structures which are converted to
26170 a flattened string by the Emacs Lisp interpreter. The
26171 internal string is an element of those structures. The
26172 displayed string is the flattened string. */
26173 tmp_glyph = row_start_glyph;
26174 while (tmp_glyph < glyph
26175 && (!(EQ (tmp_glyph->object, glyph->object)
26176 && begpos <= tmp_glyph->charpos
26177 && tmp_glyph->charpos < endpos)))
26178 tmp_glyph++;
26179 gpos = glyph - tmp_glyph;
26181 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26182 the highlighted part of the displayed string to which
26183 GLYPH belongs. Note: GSEQ_LENGTH is different from
26184 SCHARS (STRING), because the latter returns the length of
26185 the internal string. */
26186 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26187 tmp_glyph > glyph
26188 && (!(EQ (tmp_glyph->object, glyph->object)
26189 && begpos <= tmp_glyph->charpos
26190 && tmp_glyph->charpos < endpos));
26191 tmp_glyph--)
26193 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26195 /* Calculate the total pixel width of all the glyphs between
26196 the beginning of the highlighted area and GLYPH. */
26197 total_pixel_width = 0;
26198 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26199 total_pixel_width += tmp_glyph->pixel_width;
26201 /* Pre calculation of re-rendering position. Note: X is in
26202 column units here, after the call to mode_line_string or
26203 marginal_area_string. */
26204 hpos = x - gpos;
26205 vpos = (area == ON_MODE_LINE
26206 ? (w->current_matrix)->nrows - 1
26207 : 0);
26209 /* If GLYPH's position is included in the region that is
26210 already drawn in mouse face, we have nothing to do. */
26211 if ( EQ (window, hlinfo->mouse_face_window)
26212 && (!row->reversed_p
26213 ? (hlinfo->mouse_face_beg_col <= hpos
26214 && hpos < hlinfo->mouse_face_end_col)
26215 /* In R2L rows we swap BEG and END, see below. */
26216 : (hlinfo->mouse_face_end_col <= hpos
26217 && hpos < hlinfo->mouse_face_beg_col))
26218 && hlinfo->mouse_face_beg_row == vpos )
26219 return;
26221 if (clear_mouse_face (hlinfo))
26222 cursor = No_Cursor;
26224 if (!row->reversed_p)
26226 hlinfo->mouse_face_beg_col = hpos;
26227 hlinfo->mouse_face_beg_x = original_x_pixel
26228 - (total_pixel_width + dx);
26229 hlinfo->mouse_face_end_col = hpos + gseq_length;
26230 hlinfo->mouse_face_end_x = 0;
26232 else
26234 /* In R2L rows, show_mouse_face expects BEG and END
26235 coordinates to be swapped. */
26236 hlinfo->mouse_face_end_col = hpos;
26237 hlinfo->mouse_face_end_x = original_x_pixel
26238 - (total_pixel_width + dx);
26239 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26240 hlinfo->mouse_face_beg_x = 0;
26243 hlinfo->mouse_face_beg_row = vpos;
26244 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26245 hlinfo->mouse_face_beg_y = 0;
26246 hlinfo->mouse_face_end_y = 0;
26247 hlinfo->mouse_face_past_end = 0;
26248 hlinfo->mouse_face_window = window;
26250 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26251 charpos,
26252 0, 0, 0,
26253 &ignore,
26254 glyph->face_id,
26256 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26258 if (NILP (pointer))
26259 pointer = Qhand;
26261 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26262 clear_mouse_face (hlinfo);
26264 #ifdef HAVE_WINDOW_SYSTEM
26265 if (FRAME_WINDOW_P (f))
26266 define_frame_cursor1 (f, cursor, pointer);
26267 #endif
26271 /* EXPORT:
26272 Take proper action when the mouse has moved to position X, Y on
26273 frame F as regards highlighting characters that have mouse-face
26274 properties. Also de-highlighting chars where the mouse was before.
26275 X and Y can be negative or out of range. */
26277 void
26278 note_mouse_highlight (struct frame *f, int x, int y)
26280 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26281 enum window_part part;
26282 Lisp_Object window;
26283 struct window *w;
26284 Cursor cursor = No_Cursor;
26285 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26286 struct buffer *b;
26288 /* When a menu is active, don't highlight because this looks odd. */
26289 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26290 if (popup_activated ())
26291 return;
26292 #endif
26294 if (NILP (Vmouse_highlight)
26295 || !f->glyphs_initialized_p
26296 || f->pointer_invisible)
26297 return;
26299 hlinfo->mouse_face_mouse_x = x;
26300 hlinfo->mouse_face_mouse_y = y;
26301 hlinfo->mouse_face_mouse_frame = f;
26303 if (hlinfo->mouse_face_defer)
26304 return;
26306 if (gc_in_progress)
26308 hlinfo->mouse_face_deferred_gc = 1;
26309 return;
26312 /* Which window is that in? */
26313 window = window_from_coordinates (f, x, y, &part, 1);
26315 /* If we were displaying active text in another window, clear that.
26316 Also clear if we move out of text area in same window. */
26317 if (! EQ (window, hlinfo->mouse_face_window)
26318 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26319 && !NILP (hlinfo->mouse_face_window)))
26320 clear_mouse_face (hlinfo);
26322 /* Not on a window -> return. */
26323 if (!WINDOWP (window))
26324 return;
26326 /* Reset help_echo_string. It will get recomputed below. */
26327 help_echo_string = Qnil;
26329 /* Convert to window-relative pixel coordinates. */
26330 w = XWINDOW (window);
26331 frame_to_window_pixel_xy (w, &x, &y);
26333 #ifdef HAVE_WINDOW_SYSTEM
26334 /* Handle tool-bar window differently since it doesn't display a
26335 buffer. */
26336 if (EQ (window, f->tool_bar_window))
26338 note_tool_bar_highlight (f, x, y);
26339 return;
26341 #endif
26343 /* Mouse is on the mode, header line or margin? */
26344 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26345 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26347 note_mode_line_or_margin_highlight (window, x, y, part);
26348 return;
26351 #ifdef HAVE_WINDOW_SYSTEM
26352 if (part == ON_VERTICAL_BORDER)
26354 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26355 help_echo_string = build_string ("drag-mouse-1: resize");
26357 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26358 || part == ON_SCROLL_BAR)
26359 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26360 else
26361 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26362 #endif
26364 /* Are we in a window whose display is up to date?
26365 And verify the buffer's text has not changed. */
26366 b = XBUFFER (w->buffer);
26367 if (part == ON_TEXT
26368 && EQ (w->window_end_valid, w->buffer)
26369 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26370 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26372 int hpos, vpos, dx, dy, area;
26373 EMACS_INT pos;
26374 struct glyph *glyph;
26375 Lisp_Object object;
26376 Lisp_Object mouse_face = Qnil, position;
26377 Lisp_Object *overlay_vec = NULL;
26378 ptrdiff_t i, noverlays;
26379 struct buffer *obuf;
26380 EMACS_INT obegv, ozv;
26381 int same_region;
26383 /* Find the glyph under X/Y. */
26384 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26386 #ifdef HAVE_WINDOW_SYSTEM
26387 /* Look for :pointer property on image. */
26388 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26390 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26391 if (img != NULL && IMAGEP (img->spec))
26393 Lisp_Object image_map, hotspot;
26394 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26395 !NILP (image_map))
26396 && (hotspot = find_hot_spot (image_map,
26397 glyph->slice.img.x + dx,
26398 glyph->slice.img.y + dy),
26399 CONSP (hotspot))
26400 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26402 Lisp_Object plist;
26404 /* Could check XCAR (hotspot) to see if we enter/leave
26405 this hot-spot.
26406 If so, we could look for mouse-enter, mouse-leave
26407 properties in PLIST (and do something...). */
26408 hotspot = XCDR (hotspot);
26409 if (CONSP (hotspot)
26410 && (plist = XCAR (hotspot), CONSP (plist)))
26412 pointer = Fplist_get (plist, Qpointer);
26413 if (NILP (pointer))
26414 pointer = Qhand;
26415 help_echo_string = Fplist_get (plist, Qhelp_echo);
26416 if (!NILP (help_echo_string))
26418 help_echo_window = window;
26419 help_echo_object = glyph->object;
26420 help_echo_pos = glyph->charpos;
26424 if (NILP (pointer))
26425 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26428 #endif /* HAVE_WINDOW_SYSTEM */
26430 /* Clear mouse face if X/Y not over text. */
26431 if (glyph == NULL
26432 || area != TEXT_AREA
26433 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26434 /* Glyph's OBJECT is an integer for glyphs inserted by the
26435 display engine for its internal purposes, like truncation
26436 and continuation glyphs and blanks beyond the end of
26437 line's text on text terminals. If we are over such a
26438 glyph, we are not over any text. */
26439 || INTEGERP (glyph->object)
26440 /* R2L rows have a stretch glyph at their front, which
26441 stands for no text, whereas L2R rows have no glyphs at
26442 all beyond the end of text. Treat such stretch glyphs
26443 like we do with NULL glyphs in L2R rows. */
26444 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26445 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26446 && glyph->type == STRETCH_GLYPH
26447 && glyph->avoid_cursor_p))
26449 if (clear_mouse_face (hlinfo))
26450 cursor = No_Cursor;
26451 #ifdef HAVE_WINDOW_SYSTEM
26452 if (FRAME_WINDOW_P (f) && NILP (pointer))
26454 if (area != TEXT_AREA)
26455 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26456 else
26457 pointer = Vvoid_text_area_pointer;
26459 #endif
26460 goto set_cursor;
26463 pos = glyph->charpos;
26464 object = glyph->object;
26465 if (!STRINGP (object) && !BUFFERP (object))
26466 goto set_cursor;
26468 /* If we get an out-of-range value, return now; avoid an error. */
26469 if (BUFFERP (object) && pos > BUF_Z (b))
26470 goto set_cursor;
26472 /* Make the window's buffer temporarily current for
26473 overlays_at and compute_char_face. */
26474 obuf = current_buffer;
26475 current_buffer = b;
26476 obegv = BEGV;
26477 ozv = ZV;
26478 BEGV = BEG;
26479 ZV = Z;
26481 /* Is this char mouse-active or does it have help-echo? */
26482 position = make_number (pos);
26484 if (BUFFERP (object))
26486 /* Put all the overlays we want in a vector in overlay_vec. */
26487 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26488 /* Sort overlays into increasing priority order. */
26489 noverlays = sort_overlays (overlay_vec, noverlays, w);
26491 else
26492 noverlays = 0;
26494 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26496 if (same_region)
26497 cursor = No_Cursor;
26499 /* Check mouse-face highlighting. */
26500 if (! same_region
26501 /* If there exists an overlay with mouse-face overlapping
26502 the one we are currently highlighting, we have to
26503 check if we enter the overlapping overlay, and then
26504 highlight only that. */
26505 || (OVERLAYP (hlinfo->mouse_face_overlay)
26506 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26508 /* Find the highest priority overlay with a mouse-face. */
26509 Lisp_Object overlay = Qnil;
26510 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26512 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26513 if (!NILP (mouse_face))
26514 overlay = overlay_vec[i];
26517 /* If we're highlighting the same overlay as before, there's
26518 no need to do that again. */
26519 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26520 goto check_help_echo;
26521 hlinfo->mouse_face_overlay = overlay;
26523 /* Clear the display of the old active region, if any. */
26524 if (clear_mouse_face (hlinfo))
26525 cursor = No_Cursor;
26527 /* If no overlay applies, get a text property. */
26528 if (NILP (overlay))
26529 mouse_face = Fget_text_property (position, Qmouse_face, object);
26531 /* Next, compute the bounds of the mouse highlighting and
26532 display it. */
26533 if (!NILP (mouse_face) && STRINGP (object))
26535 /* The mouse-highlighting comes from a display string
26536 with a mouse-face. */
26537 Lisp_Object s, e;
26538 EMACS_INT ignore;
26540 s = Fprevious_single_property_change
26541 (make_number (pos + 1), Qmouse_face, object, Qnil);
26542 e = Fnext_single_property_change
26543 (position, Qmouse_face, object, Qnil);
26544 if (NILP (s))
26545 s = make_number (0);
26546 if (NILP (e))
26547 e = make_number (SCHARS (object) - 1);
26548 mouse_face_from_string_pos (w, hlinfo, object,
26549 XINT (s), XINT (e));
26550 hlinfo->mouse_face_past_end = 0;
26551 hlinfo->mouse_face_window = window;
26552 hlinfo->mouse_face_face_id
26553 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26554 glyph->face_id, 1);
26555 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26556 cursor = No_Cursor;
26558 else
26560 /* The mouse-highlighting, if any, comes from an overlay
26561 or text property in the buffer. */
26562 Lisp_Object buffer IF_LINT (= Qnil);
26563 Lisp_Object cover_string IF_LINT (= Qnil);
26565 if (STRINGP (object))
26567 /* If we are on a display string with no mouse-face,
26568 check if the text under it has one. */
26569 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26570 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26571 pos = string_buffer_position (object, start);
26572 if (pos > 0)
26574 mouse_face = get_char_property_and_overlay
26575 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26576 buffer = w->buffer;
26577 cover_string = object;
26580 else
26582 buffer = object;
26583 cover_string = Qnil;
26586 if (!NILP (mouse_face))
26588 Lisp_Object before, after;
26589 Lisp_Object before_string, after_string;
26590 /* To correctly find the limits of mouse highlight
26591 in a bidi-reordered buffer, we must not use the
26592 optimization of limiting the search in
26593 previous-single-property-change and
26594 next-single-property-change, because
26595 rows_from_pos_range needs the real start and end
26596 positions to DTRT in this case. That's because
26597 the first row visible in a window does not
26598 necessarily display the character whose position
26599 is the smallest. */
26600 Lisp_Object lim1 =
26601 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26602 ? Fmarker_position (w->start)
26603 : Qnil;
26604 Lisp_Object lim2 =
26605 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26606 ? make_number (BUF_Z (XBUFFER (buffer))
26607 - XFASTINT (w->window_end_pos))
26608 : Qnil;
26610 if (NILP (overlay))
26612 /* Handle the text property case. */
26613 before = Fprevious_single_property_change
26614 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26615 after = Fnext_single_property_change
26616 (make_number (pos), Qmouse_face, buffer, lim2);
26617 before_string = after_string = Qnil;
26619 else
26621 /* Handle the overlay case. */
26622 before = Foverlay_start (overlay);
26623 after = Foverlay_end (overlay);
26624 before_string = Foverlay_get (overlay, Qbefore_string);
26625 after_string = Foverlay_get (overlay, Qafter_string);
26627 if (!STRINGP (before_string)) before_string = Qnil;
26628 if (!STRINGP (after_string)) after_string = Qnil;
26631 mouse_face_from_buffer_pos (window, hlinfo, pos,
26632 XFASTINT (before),
26633 XFASTINT (after),
26634 before_string, after_string,
26635 cover_string);
26636 cursor = No_Cursor;
26641 check_help_echo:
26643 /* Look for a `help-echo' property. */
26644 if (NILP (help_echo_string)) {
26645 Lisp_Object help, overlay;
26647 /* Check overlays first. */
26648 help = overlay = Qnil;
26649 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26651 overlay = overlay_vec[i];
26652 help = Foverlay_get (overlay, Qhelp_echo);
26655 if (!NILP (help))
26657 help_echo_string = help;
26658 help_echo_window = window;
26659 help_echo_object = overlay;
26660 help_echo_pos = pos;
26662 else
26664 Lisp_Object obj = glyph->object;
26665 EMACS_INT charpos = glyph->charpos;
26667 /* Try text properties. */
26668 if (STRINGP (obj)
26669 && charpos >= 0
26670 && charpos < SCHARS (obj))
26672 help = Fget_text_property (make_number (charpos),
26673 Qhelp_echo, obj);
26674 if (NILP (help))
26676 /* If the string itself doesn't specify a help-echo,
26677 see if the buffer text ``under'' it does. */
26678 struct glyph_row *r
26679 = MATRIX_ROW (w->current_matrix, vpos);
26680 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26681 EMACS_INT p = string_buffer_position (obj, start);
26682 if (p > 0)
26684 help = Fget_char_property (make_number (p),
26685 Qhelp_echo, w->buffer);
26686 if (!NILP (help))
26688 charpos = p;
26689 obj = w->buffer;
26694 else if (BUFFERP (obj)
26695 && charpos >= BEGV
26696 && charpos < ZV)
26697 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26698 obj);
26700 if (!NILP (help))
26702 help_echo_string = help;
26703 help_echo_window = window;
26704 help_echo_object = obj;
26705 help_echo_pos = charpos;
26710 #ifdef HAVE_WINDOW_SYSTEM
26711 /* Look for a `pointer' property. */
26712 if (FRAME_WINDOW_P (f) && NILP (pointer))
26714 /* Check overlays first. */
26715 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26716 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26718 if (NILP (pointer))
26720 Lisp_Object obj = glyph->object;
26721 EMACS_INT charpos = glyph->charpos;
26723 /* Try text properties. */
26724 if (STRINGP (obj)
26725 && charpos >= 0
26726 && charpos < SCHARS (obj))
26728 pointer = Fget_text_property (make_number (charpos),
26729 Qpointer, obj);
26730 if (NILP (pointer))
26732 /* If the string itself doesn't specify a pointer,
26733 see if the buffer text ``under'' it does. */
26734 struct glyph_row *r
26735 = MATRIX_ROW (w->current_matrix, vpos);
26736 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26737 EMACS_INT p = string_buffer_position (obj, start);
26738 if (p > 0)
26739 pointer = Fget_char_property (make_number (p),
26740 Qpointer, w->buffer);
26743 else if (BUFFERP (obj)
26744 && charpos >= BEGV
26745 && charpos < ZV)
26746 pointer = Fget_text_property (make_number (charpos),
26747 Qpointer, obj);
26750 #endif /* HAVE_WINDOW_SYSTEM */
26752 BEGV = obegv;
26753 ZV = ozv;
26754 current_buffer = obuf;
26757 set_cursor:
26759 #ifdef HAVE_WINDOW_SYSTEM
26760 if (FRAME_WINDOW_P (f))
26761 define_frame_cursor1 (f, cursor, pointer);
26762 #else
26763 /* This is here to prevent a compiler error, about "label at end of
26764 compound statement". */
26765 return;
26766 #endif
26770 /* EXPORT for RIF:
26771 Clear any mouse-face on window W. This function is part of the
26772 redisplay interface, and is called from try_window_id and similar
26773 functions to ensure the mouse-highlight is off. */
26775 void
26776 x_clear_window_mouse_face (struct window *w)
26778 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26779 Lisp_Object window;
26781 BLOCK_INPUT;
26782 XSETWINDOW (window, w);
26783 if (EQ (window, hlinfo->mouse_face_window))
26784 clear_mouse_face (hlinfo);
26785 UNBLOCK_INPUT;
26789 /* EXPORT:
26790 Just discard the mouse face information for frame F, if any.
26791 This is used when the size of F is changed. */
26793 void
26794 cancel_mouse_face (struct frame *f)
26796 Lisp_Object window;
26797 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26799 window = hlinfo->mouse_face_window;
26800 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26802 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26803 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26804 hlinfo->mouse_face_window = Qnil;
26810 /***********************************************************************
26811 Exposure Events
26812 ***********************************************************************/
26814 #ifdef HAVE_WINDOW_SYSTEM
26816 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26817 which intersects rectangle R. R is in window-relative coordinates. */
26819 static void
26820 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26821 enum glyph_row_area area)
26823 struct glyph *first = row->glyphs[area];
26824 struct glyph *end = row->glyphs[area] + row->used[area];
26825 struct glyph *last;
26826 int first_x, start_x, x;
26828 if (area == TEXT_AREA && row->fill_line_p)
26829 /* If row extends face to end of line write the whole line. */
26830 draw_glyphs (w, 0, row, area,
26831 0, row->used[area],
26832 DRAW_NORMAL_TEXT, 0);
26833 else
26835 /* Set START_X to the window-relative start position for drawing glyphs of
26836 AREA. The first glyph of the text area can be partially visible.
26837 The first glyphs of other areas cannot. */
26838 start_x = window_box_left_offset (w, area);
26839 x = start_x;
26840 if (area == TEXT_AREA)
26841 x += row->x;
26843 /* Find the first glyph that must be redrawn. */
26844 while (first < end
26845 && x + first->pixel_width < r->x)
26847 x += first->pixel_width;
26848 ++first;
26851 /* Find the last one. */
26852 last = first;
26853 first_x = x;
26854 while (last < end
26855 && x < r->x + r->width)
26857 x += last->pixel_width;
26858 ++last;
26861 /* Repaint. */
26862 if (last > first)
26863 draw_glyphs (w, first_x - start_x, row, area,
26864 first - row->glyphs[area], last - row->glyphs[area],
26865 DRAW_NORMAL_TEXT, 0);
26870 /* Redraw the parts of the glyph row ROW on window W intersecting
26871 rectangle R. R is in window-relative coordinates. Value is
26872 non-zero if mouse-face was overwritten. */
26874 static int
26875 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26877 xassert (row->enabled_p);
26879 if (row->mode_line_p || w->pseudo_window_p)
26880 draw_glyphs (w, 0, row, TEXT_AREA,
26881 0, row->used[TEXT_AREA],
26882 DRAW_NORMAL_TEXT, 0);
26883 else
26885 if (row->used[LEFT_MARGIN_AREA])
26886 expose_area (w, row, r, LEFT_MARGIN_AREA);
26887 if (row->used[TEXT_AREA])
26888 expose_area (w, row, r, TEXT_AREA);
26889 if (row->used[RIGHT_MARGIN_AREA])
26890 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26891 draw_row_fringe_bitmaps (w, row);
26894 return row->mouse_face_p;
26898 /* Redraw those parts of glyphs rows during expose event handling that
26899 overlap other rows. Redrawing of an exposed line writes over parts
26900 of lines overlapping that exposed line; this function fixes that.
26902 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26903 row in W's current matrix that is exposed and overlaps other rows.
26904 LAST_OVERLAPPING_ROW is the last such row. */
26906 static void
26907 expose_overlaps (struct window *w,
26908 struct glyph_row *first_overlapping_row,
26909 struct glyph_row *last_overlapping_row,
26910 XRectangle *r)
26912 struct glyph_row *row;
26914 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26915 if (row->overlapping_p)
26917 xassert (row->enabled_p && !row->mode_line_p);
26919 row->clip = r;
26920 if (row->used[LEFT_MARGIN_AREA])
26921 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26923 if (row->used[TEXT_AREA])
26924 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26926 if (row->used[RIGHT_MARGIN_AREA])
26927 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26928 row->clip = NULL;
26933 /* Return non-zero if W's cursor intersects rectangle R. */
26935 static int
26936 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26938 XRectangle cr, result;
26939 struct glyph *cursor_glyph;
26940 struct glyph_row *row;
26942 if (w->phys_cursor.vpos >= 0
26943 && w->phys_cursor.vpos < w->current_matrix->nrows
26944 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26945 row->enabled_p)
26946 && row->cursor_in_fringe_p)
26948 /* Cursor is in the fringe. */
26949 cr.x = window_box_right_offset (w,
26950 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26951 ? RIGHT_MARGIN_AREA
26952 : TEXT_AREA));
26953 cr.y = row->y;
26954 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26955 cr.height = row->height;
26956 return x_intersect_rectangles (&cr, r, &result);
26959 cursor_glyph = get_phys_cursor_glyph (w);
26960 if (cursor_glyph)
26962 /* r is relative to W's box, but w->phys_cursor.x is relative
26963 to left edge of W's TEXT area. Adjust it. */
26964 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26965 cr.y = w->phys_cursor.y;
26966 cr.width = cursor_glyph->pixel_width;
26967 cr.height = w->phys_cursor_height;
26968 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26969 I assume the effect is the same -- and this is portable. */
26970 return x_intersect_rectangles (&cr, r, &result);
26972 /* If we don't understand the format, pretend we're not in the hot-spot. */
26973 return 0;
26977 /* EXPORT:
26978 Draw a vertical window border to the right of window W if W doesn't
26979 have vertical scroll bars. */
26981 void
26982 x_draw_vertical_border (struct window *w)
26984 struct frame *f = XFRAME (WINDOW_FRAME (w));
26986 /* We could do better, if we knew what type of scroll-bar the adjacent
26987 windows (on either side) have... But we don't :-(
26988 However, I think this works ok. ++KFS 2003-04-25 */
26990 /* Redraw borders between horizontally adjacent windows. Don't
26991 do it for frames with vertical scroll bars because either the
26992 right scroll bar of a window, or the left scroll bar of its
26993 neighbor will suffice as a border. */
26994 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26995 return;
26997 if (!WINDOW_RIGHTMOST_P (w)
26998 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27000 int x0, x1, y0, y1;
27002 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27003 y1 -= 1;
27005 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27006 x1 -= 1;
27008 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27010 else if (!WINDOW_LEFTMOST_P (w)
27011 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27013 int x0, x1, y0, y1;
27015 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27016 y1 -= 1;
27018 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27019 x0 -= 1;
27021 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27026 /* Redraw the part of window W intersection rectangle FR. Pixel
27027 coordinates in FR are frame-relative. Call this function with
27028 input blocked. Value is non-zero if the exposure overwrites
27029 mouse-face. */
27031 static int
27032 expose_window (struct window *w, XRectangle *fr)
27034 struct frame *f = XFRAME (w->frame);
27035 XRectangle wr, r;
27036 int mouse_face_overwritten_p = 0;
27038 /* If window is not yet fully initialized, do nothing. This can
27039 happen when toolkit scroll bars are used and a window is split.
27040 Reconfiguring the scroll bar will generate an expose for a newly
27041 created window. */
27042 if (w->current_matrix == NULL)
27043 return 0;
27045 /* When we're currently updating the window, display and current
27046 matrix usually don't agree. Arrange for a thorough display
27047 later. */
27048 if (w == updated_window)
27050 SET_FRAME_GARBAGED (f);
27051 return 0;
27054 /* Frame-relative pixel rectangle of W. */
27055 wr.x = WINDOW_LEFT_EDGE_X (w);
27056 wr.y = WINDOW_TOP_EDGE_Y (w);
27057 wr.width = WINDOW_TOTAL_WIDTH (w);
27058 wr.height = WINDOW_TOTAL_HEIGHT (w);
27060 if (x_intersect_rectangles (fr, &wr, &r))
27062 int yb = window_text_bottom_y (w);
27063 struct glyph_row *row;
27064 int cursor_cleared_p;
27065 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27067 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27068 r.x, r.y, r.width, r.height));
27070 /* Convert to window coordinates. */
27071 r.x -= WINDOW_LEFT_EDGE_X (w);
27072 r.y -= WINDOW_TOP_EDGE_Y (w);
27074 /* Turn off the cursor. */
27075 if (!w->pseudo_window_p
27076 && phys_cursor_in_rect_p (w, &r))
27078 x_clear_cursor (w);
27079 cursor_cleared_p = 1;
27081 else
27082 cursor_cleared_p = 0;
27084 /* Update lines intersecting rectangle R. */
27085 first_overlapping_row = last_overlapping_row = NULL;
27086 for (row = w->current_matrix->rows;
27087 row->enabled_p;
27088 ++row)
27090 int y0 = row->y;
27091 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27093 if ((y0 >= r.y && y0 < r.y + r.height)
27094 || (y1 > r.y && y1 < r.y + r.height)
27095 || (r.y >= y0 && r.y < y1)
27096 || (r.y + r.height > y0 && r.y + r.height < y1))
27098 /* A header line may be overlapping, but there is no need
27099 to fix overlapping areas for them. KFS 2005-02-12 */
27100 if (row->overlapping_p && !row->mode_line_p)
27102 if (first_overlapping_row == NULL)
27103 first_overlapping_row = row;
27104 last_overlapping_row = row;
27107 row->clip = fr;
27108 if (expose_line (w, row, &r))
27109 mouse_face_overwritten_p = 1;
27110 row->clip = NULL;
27112 else if (row->overlapping_p)
27114 /* We must redraw a row overlapping the exposed area. */
27115 if (y0 < r.y
27116 ? y0 + row->phys_height > r.y
27117 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27119 if (first_overlapping_row == NULL)
27120 first_overlapping_row = row;
27121 last_overlapping_row = row;
27125 if (y1 >= yb)
27126 break;
27129 /* Display the mode line if there is one. */
27130 if (WINDOW_WANTS_MODELINE_P (w)
27131 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27132 row->enabled_p)
27133 && row->y < r.y + r.height)
27135 if (expose_line (w, row, &r))
27136 mouse_face_overwritten_p = 1;
27139 if (!w->pseudo_window_p)
27141 /* Fix the display of overlapping rows. */
27142 if (first_overlapping_row)
27143 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27144 fr);
27146 /* Draw border between windows. */
27147 x_draw_vertical_border (w);
27149 /* Turn the cursor on again. */
27150 if (cursor_cleared_p)
27151 update_window_cursor (w, 1);
27155 return mouse_face_overwritten_p;
27160 /* Redraw (parts) of all windows in the window tree rooted at W that
27161 intersect R. R contains frame pixel coordinates. Value is
27162 non-zero if the exposure overwrites mouse-face. */
27164 static int
27165 expose_window_tree (struct window *w, XRectangle *r)
27167 struct frame *f = XFRAME (w->frame);
27168 int mouse_face_overwritten_p = 0;
27170 while (w && !FRAME_GARBAGED_P (f))
27172 if (!NILP (w->hchild))
27173 mouse_face_overwritten_p
27174 |= expose_window_tree (XWINDOW (w->hchild), r);
27175 else if (!NILP (w->vchild))
27176 mouse_face_overwritten_p
27177 |= expose_window_tree (XWINDOW (w->vchild), r);
27178 else
27179 mouse_face_overwritten_p |= expose_window (w, r);
27181 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27184 return mouse_face_overwritten_p;
27188 /* EXPORT:
27189 Redisplay an exposed area of frame F. X and Y are the upper-left
27190 corner of the exposed rectangle. W and H are width and height of
27191 the exposed area. All are pixel values. W or H zero means redraw
27192 the entire frame. */
27194 void
27195 expose_frame (struct frame *f, int x, int y, int w, int h)
27197 XRectangle r;
27198 int mouse_face_overwritten_p = 0;
27200 TRACE ((stderr, "expose_frame "));
27202 /* No need to redraw if frame will be redrawn soon. */
27203 if (FRAME_GARBAGED_P (f))
27205 TRACE ((stderr, " garbaged\n"));
27206 return;
27209 /* If basic faces haven't been realized yet, there is no point in
27210 trying to redraw anything. This can happen when we get an expose
27211 event while Emacs is starting, e.g. by moving another window. */
27212 if (FRAME_FACE_CACHE (f) == NULL
27213 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27215 TRACE ((stderr, " no faces\n"));
27216 return;
27219 if (w == 0 || h == 0)
27221 r.x = r.y = 0;
27222 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27223 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27225 else
27227 r.x = x;
27228 r.y = y;
27229 r.width = w;
27230 r.height = h;
27233 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27234 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27236 if (WINDOWP (f->tool_bar_window))
27237 mouse_face_overwritten_p
27238 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27240 #ifdef HAVE_X_WINDOWS
27241 #ifndef MSDOS
27242 #ifndef USE_X_TOOLKIT
27243 if (WINDOWP (f->menu_bar_window))
27244 mouse_face_overwritten_p
27245 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27246 #endif /* not USE_X_TOOLKIT */
27247 #endif
27248 #endif
27250 /* Some window managers support a focus-follows-mouse style with
27251 delayed raising of frames. Imagine a partially obscured frame,
27252 and moving the mouse into partially obscured mouse-face on that
27253 frame. The visible part of the mouse-face will be highlighted,
27254 then the WM raises the obscured frame. With at least one WM, KDE
27255 2.1, Emacs is not getting any event for the raising of the frame
27256 (even tried with SubstructureRedirectMask), only Expose events.
27257 These expose events will draw text normally, i.e. not
27258 highlighted. Which means we must redo the highlight here.
27259 Subsume it under ``we love X''. --gerd 2001-08-15 */
27260 /* Included in Windows version because Windows most likely does not
27261 do the right thing if any third party tool offers
27262 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27263 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27265 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27266 if (f == hlinfo->mouse_face_mouse_frame)
27268 int mouse_x = hlinfo->mouse_face_mouse_x;
27269 int mouse_y = hlinfo->mouse_face_mouse_y;
27270 clear_mouse_face (hlinfo);
27271 note_mouse_highlight (f, mouse_x, mouse_y);
27277 /* EXPORT:
27278 Determine the intersection of two rectangles R1 and R2. Return
27279 the intersection in *RESULT. Value is non-zero if RESULT is not
27280 empty. */
27283 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27285 XRectangle *left, *right;
27286 XRectangle *upper, *lower;
27287 int intersection_p = 0;
27289 /* Rearrange so that R1 is the left-most rectangle. */
27290 if (r1->x < r2->x)
27291 left = r1, right = r2;
27292 else
27293 left = r2, right = r1;
27295 /* X0 of the intersection is right.x0, if this is inside R1,
27296 otherwise there is no intersection. */
27297 if (right->x <= left->x + left->width)
27299 result->x = right->x;
27301 /* The right end of the intersection is the minimum of
27302 the right ends of left and right. */
27303 result->width = (min (left->x + left->width, right->x + right->width)
27304 - result->x);
27306 /* Same game for Y. */
27307 if (r1->y < r2->y)
27308 upper = r1, lower = r2;
27309 else
27310 upper = r2, lower = r1;
27312 /* The upper end of the intersection is lower.y0, if this is inside
27313 of upper. Otherwise, there is no intersection. */
27314 if (lower->y <= upper->y + upper->height)
27316 result->y = lower->y;
27318 /* The lower end of the intersection is the minimum of the lower
27319 ends of upper and lower. */
27320 result->height = (min (lower->y + lower->height,
27321 upper->y + upper->height)
27322 - result->y);
27323 intersection_p = 1;
27327 return intersection_p;
27330 #endif /* HAVE_WINDOW_SYSTEM */
27333 /***********************************************************************
27334 Initialization
27335 ***********************************************************************/
27337 void
27338 syms_of_xdisp (void)
27340 Vwith_echo_area_save_vector = Qnil;
27341 staticpro (&Vwith_echo_area_save_vector);
27343 Vmessage_stack = Qnil;
27344 staticpro (&Vmessage_stack);
27346 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
27348 message_dolog_marker1 = Fmake_marker ();
27349 staticpro (&message_dolog_marker1);
27350 message_dolog_marker2 = Fmake_marker ();
27351 staticpro (&message_dolog_marker2);
27352 message_dolog_marker3 = Fmake_marker ();
27353 staticpro (&message_dolog_marker3);
27355 #if GLYPH_DEBUG
27356 defsubr (&Sdump_frame_glyph_matrix);
27357 defsubr (&Sdump_glyph_matrix);
27358 defsubr (&Sdump_glyph_row);
27359 defsubr (&Sdump_tool_bar_row);
27360 defsubr (&Strace_redisplay);
27361 defsubr (&Strace_to_stderr);
27362 #endif
27363 #ifdef HAVE_WINDOW_SYSTEM
27364 defsubr (&Stool_bar_lines_needed);
27365 defsubr (&Slookup_image_map);
27366 #endif
27367 defsubr (&Sformat_mode_line);
27368 defsubr (&Sinvisible_p);
27369 defsubr (&Scurrent_bidi_paragraph_direction);
27371 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
27372 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
27373 DEFSYM (Qoverriding_local_map, "overriding-local-map");
27374 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
27375 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
27376 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
27377 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
27378 DEFSYM (Qeval, "eval");
27379 DEFSYM (QCdata, ":data");
27380 DEFSYM (Qdisplay, "display");
27381 DEFSYM (Qspace_width, "space-width");
27382 DEFSYM (Qraise, "raise");
27383 DEFSYM (Qslice, "slice");
27384 DEFSYM (Qspace, "space");
27385 DEFSYM (Qmargin, "margin");
27386 DEFSYM (Qpointer, "pointer");
27387 DEFSYM (Qleft_margin, "left-margin");
27388 DEFSYM (Qright_margin, "right-margin");
27389 DEFSYM (Qcenter, "center");
27390 DEFSYM (Qline_height, "line-height");
27391 DEFSYM (QCalign_to, ":align-to");
27392 DEFSYM (QCrelative_width, ":relative-width");
27393 DEFSYM (QCrelative_height, ":relative-height");
27394 DEFSYM (QCeval, ":eval");
27395 DEFSYM (QCpropertize, ":propertize");
27396 DEFSYM (QCfile, ":file");
27397 DEFSYM (Qfontified, "fontified");
27398 DEFSYM (Qfontification_functions, "fontification-functions");
27399 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
27400 DEFSYM (Qescape_glyph, "escape-glyph");
27401 DEFSYM (Qnobreak_space, "nobreak-space");
27402 DEFSYM (Qimage, "image");
27403 DEFSYM (Qtext, "text");
27404 DEFSYM (Qboth, "both");
27405 DEFSYM (Qboth_horiz, "both-horiz");
27406 DEFSYM (Qtext_image_horiz, "text-image-horiz");
27407 DEFSYM (QCmap, ":map");
27408 DEFSYM (QCpointer, ":pointer");
27409 DEFSYM (Qrect, "rect");
27410 DEFSYM (Qcircle, "circle");
27411 DEFSYM (Qpoly, "poly");
27412 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
27413 DEFSYM (Qgrow_only, "grow-only");
27414 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
27415 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
27416 DEFSYM (Qposition, "position");
27417 DEFSYM (Qbuffer_position, "buffer-position");
27418 DEFSYM (Qobject, "object");
27419 DEFSYM (Qbar, "bar");
27420 DEFSYM (Qhbar, "hbar");
27421 DEFSYM (Qbox, "box");
27422 DEFSYM (Qhollow, "hollow");
27423 DEFSYM (Qhand, "hand");
27424 DEFSYM (Qarrow, "arrow");
27425 DEFSYM (Qtext, "text");
27426 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
27428 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27429 Fcons (intern_c_string ("void-variable"), Qnil)),
27430 Qnil);
27431 staticpro (&list_of_error);
27433 DEFSYM (Qlast_arrow_position, "last-arrow-position");
27434 DEFSYM (Qlast_arrow_string, "last-arrow-string");
27435 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
27436 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
27438 echo_buffer[0] = echo_buffer[1] = Qnil;
27439 staticpro (&echo_buffer[0]);
27440 staticpro (&echo_buffer[1]);
27442 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27443 staticpro (&echo_area_buffer[0]);
27444 staticpro (&echo_area_buffer[1]);
27446 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27447 staticpro (&Vmessages_buffer_name);
27449 mode_line_proptrans_alist = Qnil;
27450 staticpro (&mode_line_proptrans_alist);
27451 mode_line_string_list = Qnil;
27452 staticpro (&mode_line_string_list);
27453 mode_line_string_face = Qnil;
27454 staticpro (&mode_line_string_face);
27455 mode_line_string_face_prop = Qnil;
27456 staticpro (&mode_line_string_face_prop);
27457 Vmode_line_unwind_vector = Qnil;
27458 staticpro (&Vmode_line_unwind_vector);
27460 help_echo_string = Qnil;
27461 staticpro (&help_echo_string);
27462 help_echo_object = Qnil;
27463 staticpro (&help_echo_object);
27464 help_echo_window = Qnil;
27465 staticpro (&help_echo_window);
27466 previous_help_echo_string = Qnil;
27467 staticpro (&previous_help_echo_string);
27468 help_echo_pos = -1;
27470 DEFSYM (Qright_to_left, "right-to-left");
27471 DEFSYM (Qleft_to_right, "left-to-right");
27473 #ifdef HAVE_WINDOW_SYSTEM
27474 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27475 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27476 For example, if a block cursor is over a tab, it will be drawn as
27477 wide as that tab on the display. */);
27478 x_stretch_cursor_p = 0;
27479 #endif
27481 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27482 doc: /* *Non-nil means highlight trailing whitespace.
27483 The face used for trailing whitespace is `trailing-whitespace'. */);
27484 Vshow_trailing_whitespace = Qnil;
27486 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27487 doc: /* *Control highlighting of nobreak space and soft hyphen.
27488 A value of t means highlight the character itself (for nobreak space,
27489 use face `nobreak-space').
27490 A value of nil means no highlighting.
27491 Other values mean display the escape glyph followed by an ordinary
27492 space or ordinary hyphen. */);
27493 Vnobreak_char_display = Qt;
27495 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27496 doc: /* *The pointer shape to show in void text areas.
27497 A value of nil means to show the text pointer. Other options are `arrow',
27498 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27499 Vvoid_text_area_pointer = Qarrow;
27501 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27502 doc: /* Non-nil means don't actually do any redisplay.
27503 This is used for internal purposes. */);
27504 Vinhibit_redisplay = Qnil;
27506 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27507 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27508 Vglobal_mode_string = Qnil;
27510 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27511 doc: /* Marker for where to display an arrow on top of the buffer text.
27512 This must be the beginning of a line in order to work.
27513 See also `overlay-arrow-string'. */);
27514 Voverlay_arrow_position = Qnil;
27516 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27517 doc: /* String to display as an arrow in non-window frames.
27518 See also `overlay-arrow-position'. */);
27519 Voverlay_arrow_string = make_pure_c_string ("=>");
27521 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27522 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27523 The symbols on this list are examined during redisplay to determine
27524 where to display overlay arrows. */);
27525 Voverlay_arrow_variable_list
27526 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27528 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27529 doc: /* *The number of lines to try scrolling a window by when point moves out.
27530 If that fails to bring point back on frame, point is centered instead.
27531 If this is zero, point is always centered after it moves off frame.
27532 If you want scrolling to always be a line at a time, you should set
27533 `scroll-conservatively' to a large value rather than set this to 1. */);
27535 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27536 doc: /* *Scroll up to this many lines, to bring point back on screen.
27537 If point moves off-screen, redisplay will scroll by up to
27538 `scroll-conservatively' lines in order to bring point just barely
27539 onto the screen again. If that cannot be done, then redisplay
27540 recenters point as usual.
27542 If the value is greater than 100, redisplay will never recenter point,
27543 but will always scroll just enough text to bring point into view, even
27544 if you move far away.
27546 A value of zero means always recenter point if it moves off screen. */);
27547 scroll_conservatively = 0;
27549 DEFVAR_INT ("scroll-margin", scroll_margin,
27550 doc: /* *Number of lines of margin at the top and bottom of a window.
27551 Recenter the window whenever point gets within this many lines
27552 of the top or bottom of the window. */);
27553 scroll_margin = 0;
27555 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27556 doc: /* Pixels per inch value for non-window system displays.
27557 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27558 Vdisplay_pixels_per_inch = make_float (72.0);
27560 #if GLYPH_DEBUG
27561 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27562 #endif
27564 DEFVAR_LISP ("truncate-partial-width-windows",
27565 Vtruncate_partial_width_windows,
27566 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27567 For an integer value, truncate lines in each window narrower than the
27568 full frame width, provided the window width is less than that integer;
27569 otherwise, respect the value of `truncate-lines'.
27571 For any other non-nil value, truncate lines in all windows that do
27572 not span the full frame width.
27574 A value of nil means to respect the value of `truncate-lines'.
27576 If `word-wrap' is enabled, you might want to reduce this. */);
27577 Vtruncate_partial_width_windows = make_number (50);
27579 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27580 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27581 Any other value means to use the appropriate face, `mode-line',
27582 `header-line', or `menu' respectively. */);
27583 mode_line_inverse_video = 1;
27585 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27586 doc: /* *Maximum buffer size for which line number should be displayed.
27587 If the buffer is bigger than this, the line number does not appear
27588 in the mode line. A value of nil means no limit. */);
27589 Vline_number_display_limit = Qnil;
27591 DEFVAR_INT ("line-number-display-limit-width",
27592 line_number_display_limit_width,
27593 doc: /* *Maximum line width (in characters) for line number display.
27594 If the average length of the lines near point is bigger than this, then the
27595 line number may be omitted from the mode line. */);
27596 line_number_display_limit_width = 200;
27598 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27599 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27600 highlight_nonselected_windows = 0;
27602 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27603 doc: /* Non-nil if more than one frame is visible on this display.
27604 Minibuffer-only frames don't count, but iconified frames do.
27605 This variable is not guaranteed to be accurate except while processing
27606 `frame-title-format' and `icon-title-format'. */);
27608 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27609 doc: /* Template for displaying the title bar of visible frames.
27610 \(Assuming the window manager supports this feature.)
27612 This variable has the same structure as `mode-line-format', except that
27613 the %c and %l constructs are ignored. It is used only on frames for
27614 which no explicit name has been set \(see `modify-frame-parameters'). */);
27616 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27617 doc: /* Template for displaying the title bar of an iconified frame.
27618 \(Assuming the window manager supports this feature.)
27619 This variable has the same structure as `mode-line-format' (which see),
27620 and is used only on frames for which no explicit name has been set
27621 \(see `modify-frame-parameters'). */);
27622 Vicon_title_format
27623 = Vframe_title_format
27624 = pure_cons (intern_c_string ("multiple-frames"),
27625 pure_cons (make_pure_c_string ("%b"),
27626 pure_cons (pure_cons (empty_unibyte_string,
27627 pure_cons (intern_c_string ("invocation-name"),
27628 pure_cons (make_pure_c_string ("@"),
27629 pure_cons (intern_c_string ("system-name"),
27630 Qnil)))),
27631 Qnil)));
27633 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27634 doc: /* Maximum number of lines to keep in the message log buffer.
27635 If nil, disable message logging. If t, log messages but don't truncate
27636 the buffer when it becomes large. */);
27637 Vmessage_log_max = make_number (100);
27639 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27640 doc: /* Functions called before redisplay, if window sizes have changed.
27641 The value should be a list of functions that take one argument.
27642 Just before redisplay, for each frame, if any of its windows have changed
27643 size since the last redisplay, or have been split or deleted,
27644 all the functions in the list are called, with the frame as argument. */);
27645 Vwindow_size_change_functions = Qnil;
27647 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27648 doc: /* List of functions to call before redisplaying a window with scrolling.
27649 Each function is called with two arguments, the window and its new
27650 display-start position. Note that these functions are also called by
27651 `set-window-buffer'. Also note that the value of `window-end' is not
27652 valid when these functions are called. */);
27653 Vwindow_scroll_functions = Qnil;
27655 DEFVAR_LISP ("window-text-change-functions",
27656 Vwindow_text_change_functions,
27657 doc: /* Functions to call in redisplay when text in the window might change. */);
27658 Vwindow_text_change_functions = Qnil;
27660 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27661 doc: /* Functions called when redisplay of a window reaches the end trigger.
27662 Each function is called with two arguments, the window and the end trigger value.
27663 See `set-window-redisplay-end-trigger'. */);
27664 Vredisplay_end_trigger_functions = Qnil;
27666 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27667 doc: /* *Non-nil means autoselect window with mouse pointer.
27668 If nil, do not autoselect windows.
27669 A positive number means delay autoselection by that many seconds: a
27670 window is autoselected only after the mouse has remained in that
27671 window for the duration of the delay.
27672 A negative number has a similar effect, but causes windows to be
27673 autoselected only after the mouse has stopped moving. \(Because of
27674 the way Emacs compares mouse events, you will occasionally wait twice
27675 that time before the window gets selected.\)
27676 Any other value means to autoselect window instantaneously when the
27677 mouse pointer enters it.
27679 Autoselection selects the minibuffer only if it is active, and never
27680 unselects the minibuffer if it is active.
27682 When customizing this variable make sure that the actual value of
27683 `focus-follows-mouse' matches the behavior of your window manager. */);
27684 Vmouse_autoselect_window = Qnil;
27686 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27687 doc: /* *Non-nil means automatically resize tool-bars.
27688 This dynamically changes the tool-bar's height to the minimum height
27689 that is needed to make all tool-bar items visible.
27690 If value is `grow-only', the tool-bar's height is only increased
27691 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27692 Vauto_resize_tool_bars = Qt;
27694 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27695 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27696 auto_raise_tool_bar_buttons_p = 1;
27698 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27699 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27700 make_cursor_line_fully_visible_p = 1;
27702 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27703 doc: /* *Border below tool-bar in pixels.
27704 If an integer, use it as the height of the border.
27705 If it is one of `internal-border-width' or `border-width', use the
27706 value of the corresponding frame parameter.
27707 Otherwise, no border is added below the tool-bar. */);
27708 Vtool_bar_border = Qinternal_border_width;
27710 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27711 doc: /* *Margin around tool-bar buttons in pixels.
27712 If an integer, use that for both horizontal and vertical margins.
27713 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27714 HORZ specifying the horizontal margin, and VERT specifying the
27715 vertical margin. */);
27716 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27718 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27719 doc: /* *Relief thickness of tool-bar buttons. */);
27720 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27722 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27723 doc: /* Tool bar style to use.
27724 It can be one of
27725 image - show images only
27726 text - show text only
27727 both - show both, text below image
27728 both-horiz - show text to the right of the image
27729 text-image-horiz - show text to the left of the image
27730 any other - use system default or image if no system default. */);
27731 Vtool_bar_style = Qnil;
27733 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27734 doc: /* *Maximum number of characters a label can have to be shown.
27735 The tool bar style must also show labels for this to have any effect, see
27736 `tool-bar-style'. */);
27737 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27739 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27740 doc: /* List of functions to call to fontify regions of text.
27741 Each function is called with one argument POS. Functions must
27742 fontify a region starting at POS in the current buffer, and give
27743 fontified regions the property `fontified'. */);
27744 Vfontification_functions = Qnil;
27745 Fmake_variable_buffer_local (Qfontification_functions);
27747 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27748 unibyte_display_via_language_environment,
27749 doc: /* *Non-nil means display unibyte text according to language environment.
27750 Specifically, this means that raw bytes in the range 160-255 decimal
27751 are displayed by converting them to the equivalent multibyte characters
27752 according to the current language environment. As a result, they are
27753 displayed according to the current fontset.
27755 Note that this variable affects only how these bytes are displayed,
27756 but does not change the fact they are interpreted as raw bytes. */);
27757 unibyte_display_via_language_environment = 0;
27759 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27760 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
27761 If a float, it specifies a fraction of the mini-window frame's height.
27762 If an integer, it specifies a number of lines. */);
27763 Vmax_mini_window_height = make_float (0.25);
27765 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27766 doc: /* How to resize mini-windows (the minibuffer and the echo area).
27767 A value of nil means don't automatically resize mini-windows.
27768 A value of t means resize them to fit the text displayed in them.
27769 A value of `grow-only', the default, means let mini-windows grow only;
27770 they return to their normal size when the minibuffer is closed, or the
27771 echo area becomes empty. */);
27772 Vresize_mini_windows = Qgrow_only;
27774 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27775 doc: /* Alist specifying how to blink the cursor off.
27776 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27777 `cursor-type' frame-parameter or variable equals ON-STATE,
27778 comparing using `equal', Emacs uses OFF-STATE to specify
27779 how to blink it off. ON-STATE and OFF-STATE are values for
27780 the `cursor-type' frame parameter.
27782 If a frame's ON-STATE has no entry in this list,
27783 the frame's other specifications determine how to blink the cursor off. */);
27784 Vblink_cursor_alist = Qnil;
27786 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27787 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27788 If non-nil, windows are automatically scrolled horizontally to make
27789 point visible. */);
27790 automatic_hscrolling_p = 1;
27791 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
27793 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27794 doc: /* *How many columns away from the window edge point is allowed to get
27795 before automatic hscrolling will horizontally scroll the window. */);
27796 hscroll_margin = 5;
27798 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27799 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27800 When point is less than `hscroll-margin' columns from the window
27801 edge, automatic hscrolling will scroll the window by the amount of columns
27802 determined by this variable. If its value is a positive integer, scroll that
27803 many columns. If it's a positive floating-point number, it specifies the
27804 fraction of the window's width to scroll. If it's nil or zero, point will be
27805 centered horizontally after the scroll. Any other value, including negative
27806 numbers, are treated as if the value were zero.
27808 Automatic hscrolling always moves point outside the scroll margin, so if
27809 point was more than scroll step columns inside the margin, the window will
27810 scroll more than the value given by the scroll step.
27812 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27813 and `scroll-right' overrides this variable's effect. */);
27814 Vhscroll_step = make_number (0);
27816 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27817 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27818 Bind this around calls to `message' to let it take effect. */);
27819 message_truncate_lines = 0;
27821 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27822 doc: /* Normal hook run to update the menu bar definitions.
27823 Redisplay runs this hook before it redisplays the menu bar.
27824 This is used to update submenus such as Buffers,
27825 whose contents depend on various data. */);
27826 Vmenu_bar_update_hook = Qnil;
27828 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27829 doc: /* Frame for which we are updating a menu.
27830 The enable predicate for a menu binding should check this variable. */);
27831 Vmenu_updating_frame = Qnil;
27833 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27834 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27835 inhibit_menubar_update = 0;
27837 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27838 doc: /* Prefix prepended to all continuation lines at display time.
27839 The value may be a string, an image, or a stretch-glyph; it is
27840 interpreted in the same way as the value of a `display' text property.
27842 This variable is overridden by any `wrap-prefix' text or overlay
27843 property.
27845 To add a prefix to non-continuation lines, use `line-prefix'. */);
27846 Vwrap_prefix = Qnil;
27847 DEFSYM (Qwrap_prefix, "wrap-prefix");
27848 Fmake_variable_buffer_local (Qwrap_prefix);
27850 DEFVAR_LISP ("line-prefix", Vline_prefix,
27851 doc: /* Prefix prepended to all non-continuation lines at display time.
27852 The value may be a string, an image, or a stretch-glyph; it is
27853 interpreted in the same way as the value of a `display' text property.
27855 This variable is overridden by any `line-prefix' text or overlay
27856 property.
27858 To add a prefix to continuation lines, use `wrap-prefix'. */);
27859 Vline_prefix = Qnil;
27860 DEFSYM (Qline_prefix, "line-prefix");
27861 Fmake_variable_buffer_local (Qline_prefix);
27863 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27864 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27865 inhibit_eval_during_redisplay = 0;
27867 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27868 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27869 inhibit_free_realized_faces = 0;
27871 #if GLYPH_DEBUG
27872 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27873 doc: /* Inhibit try_window_id display optimization. */);
27874 inhibit_try_window_id = 0;
27876 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27877 doc: /* Inhibit try_window_reusing display optimization. */);
27878 inhibit_try_window_reusing = 0;
27880 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27881 doc: /* Inhibit try_cursor_movement display optimization. */);
27882 inhibit_try_cursor_movement = 0;
27883 #endif /* GLYPH_DEBUG */
27885 DEFVAR_INT ("overline-margin", overline_margin,
27886 doc: /* *Space between overline and text, in pixels.
27887 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27888 margin to the caracter height. */);
27889 overline_margin = 2;
27891 DEFVAR_INT ("underline-minimum-offset",
27892 underline_minimum_offset,
27893 doc: /* Minimum distance between baseline and underline.
27894 This can improve legibility of underlined text at small font sizes,
27895 particularly when using variable `x-use-underline-position-properties'
27896 with fonts that specify an UNDERLINE_POSITION relatively close to the
27897 baseline. The default value is 1. */);
27898 underline_minimum_offset = 1;
27900 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27901 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27902 This feature only works when on a window system that can change
27903 cursor shapes. */);
27904 display_hourglass_p = 1;
27906 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27907 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27908 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27910 hourglass_atimer = NULL;
27911 hourglass_shown_p = 0;
27913 DEFSYM (Qglyphless_char, "glyphless-char");
27914 DEFSYM (Qhex_code, "hex-code");
27915 DEFSYM (Qempty_box, "empty-box");
27916 DEFSYM (Qthin_space, "thin-space");
27917 DEFSYM (Qzero_width, "zero-width");
27919 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27920 /* Intern this now in case it isn't already done.
27921 Setting this variable twice is harmless.
27922 But don't staticpro it here--that is done in alloc.c. */
27923 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27924 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27926 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27927 doc: /* Char-table defining glyphless characters.
27928 Each element, if non-nil, should be one of the following:
27929 an ASCII acronym string: display this string in a box
27930 `hex-code': display the hexadecimal code of a character in a box
27931 `empty-box': display as an empty box
27932 `thin-space': display as 1-pixel width space
27933 `zero-width': don't display
27934 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27935 display method for graphical terminals and text terminals respectively.
27936 GRAPHICAL and TEXT should each have one of the values listed above.
27938 The char-table has one extra slot to control the display of a character for
27939 which no font is found. This slot only takes effect on graphical terminals.
27940 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27941 `thin-space'. The default is `empty-box'. */);
27942 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27943 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27944 Qempty_box);
27948 /* Initialize this module when Emacs starts. */
27950 void
27951 init_xdisp (void)
27953 current_header_line_height = current_mode_line_height = -1;
27955 CHARPOS (this_line_start_pos) = 0;
27957 if (!noninteractive)
27959 struct window *m = XWINDOW (minibuf_window);
27960 Lisp_Object frame = m->frame;
27961 struct frame *f = XFRAME (frame);
27962 Lisp_Object root = FRAME_ROOT_WINDOW (f);
27963 struct window *r = XWINDOW (root);
27964 int i;
27966 echo_area_window = minibuf_window;
27968 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
27969 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
27970 XSETFASTINT (r->total_cols, FRAME_COLS (f));
27971 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
27972 XSETFASTINT (m->total_lines, 1);
27973 XSETFASTINT (m->total_cols, FRAME_COLS (f));
27975 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27976 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27977 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27979 /* The default ellipsis glyphs `...'. */
27980 for (i = 0; i < 3; ++i)
27981 default_invis_vector[i] = make_number ('.');
27985 /* Allocate the buffer for frame titles.
27986 Also used for `format-mode-line'. */
27987 int size = 100;
27988 mode_line_noprop_buf = (char *) xmalloc (size);
27989 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27990 mode_line_noprop_ptr = mode_line_noprop_buf;
27991 mode_line_target = MODE_LINE_DISPLAY;
27994 help_echo_showing_p = 0;
27997 /* Since w32 does not support atimers, it defines its own implementation of
27998 the following three functions in w32fns.c. */
27999 #ifndef WINDOWSNT
28001 /* Platform-independent portion of hourglass implementation. */
28003 /* Return non-zero if houglass timer has been started or hourglass is shown. */
28005 hourglass_started (void)
28007 return hourglass_shown_p || hourglass_atimer != NULL;
28010 /* Cancel a currently active hourglass timer, and start a new one. */
28011 void
28012 start_hourglass (void)
28014 #if defined (HAVE_WINDOW_SYSTEM)
28015 EMACS_TIME delay;
28016 int secs, usecs = 0;
28018 cancel_hourglass ();
28020 if (INTEGERP (Vhourglass_delay)
28021 && XINT (Vhourglass_delay) > 0)
28022 secs = XFASTINT (Vhourglass_delay);
28023 else if (FLOATP (Vhourglass_delay)
28024 && XFLOAT_DATA (Vhourglass_delay) > 0)
28026 Lisp_Object tem;
28027 tem = Ftruncate (Vhourglass_delay, Qnil);
28028 secs = XFASTINT (tem);
28029 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28031 else
28032 secs = DEFAULT_HOURGLASS_DELAY;
28034 EMACS_SET_SECS_USECS (delay, secs, usecs);
28035 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28036 show_hourglass, NULL);
28037 #endif
28041 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28042 shown. */
28043 void
28044 cancel_hourglass (void)
28046 #if defined (HAVE_WINDOW_SYSTEM)
28047 if (hourglass_atimer)
28049 cancel_atimer (hourglass_atimer);
28050 hourglass_atimer = NULL;
28053 if (hourglass_shown_p)
28054 hide_hourglass ();
28055 #endif
28057 #endif /* ! WINDOWSNT */