(post-read-decode-hz)
[emacs.git] / src / xdisp.c
bloba35145cf38164b0a4f8b9b37cb5ca7767b390580
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 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 2, or (at your option)
10 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; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
192 #ifdef HAVE_X_WINDOWS
193 #include "xterm.h"
194 #endif
195 #ifdef WINDOWSNT
196 #include "w32term.h"
197 #endif
198 #ifdef macintosh
199 #include "macterm.h"
200 #endif
202 #define INFINITY 10000000
204 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
205 extern void set_frame_menubar P_ ((struct frame *f, int, int));
206 extern int pending_menu_activation;
207 #endif
209 extern int interrupt_input;
210 extern int command_loop_level;
212 extern int minibuffer_auto_raise;
214 extern Lisp_Object Qface;
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
230 /* Functions called to fontify regions of text. */
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
238 int auto_raise_tool_bar_buttons_p;
240 /* Margin around tool bar buttons in pixels. */
242 Lisp_Object Vtool_bar_button_margin;
244 /* Thickness of shadow to draw around tool bar buttons. */
246 int tool_bar_button_relief;
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
251 int auto_resize_tool_bars_p;
253 /* Non-nil means don't actually do any redisplay. */
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
259 int inhibit_eval_during_redisplay;
261 /* Names of text properties relevant for redisplay. */
263 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
264 extern Lisp_Object Qface, Qinvisible, Qwidth;
266 /* Symbols used in text property values. */
268 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
269 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
270 Lisp_Object Qmargin;
271 extern Lisp_Object Qheight;
273 /* Non-nil means highlight trailing whitespace. */
275 Lisp_Object Vshow_trailing_whitespace;
277 /* Name of the face used to highlight trailing whitespace. */
279 Lisp_Object Qtrailing_whitespace;
281 /* The symbol `image' which is the car of the lists used to represent
282 images in Lisp. */
284 Lisp_Object Qimage;
286 /* Non-zero means print newline to stdout before next mini-buffer
287 message. */
289 int noninteractive_need_newline;
291 /* Non-zero means print newline to message log before next message. */
293 static int message_log_need_newline;
295 /* Three markers that message_dolog uses.
296 It could allocate them itself, but that causes trouble
297 in handling memory-full errors. */
298 static Lisp_Object message_dolog_marker1;
299 static Lisp_Object message_dolog_marker2;
300 static Lisp_Object message_dolog_marker3;
302 /* The buffer position of the first character appearing entirely or
303 partially on the line of the selected window which contains the
304 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
305 redisplay optimization in redisplay_internal. */
307 static struct text_pos this_line_start_pos;
309 /* Number of characters past the end of the line above, including the
310 terminating newline. */
312 static struct text_pos this_line_end_pos;
314 /* The vertical positions and the height of this line. */
316 static int this_line_vpos;
317 static int this_line_y;
318 static int this_line_pixel_height;
320 /* X position at which this display line starts. Usually zero;
321 negative if first character is partially visible. */
323 static int this_line_start_x;
325 /* Buffer that this_line_.* variables are referring to. */
327 static struct buffer *this_line_buffer;
329 /* Nonzero means truncate lines in all windows less wide than the
330 frame. */
332 int truncate_partial_width_windows;
334 /* A flag to control how to display unibyte 8-bit character. */
336 int unibyte_display_via_language_environment;
338 /* Nonzero means we have more than one non-mini-buffer-only frame.
339 Not guaranteed to be accurate except while parsing
340 frame-title-format. */
342 int multiple_frames;
344 Lisp_Object Vglobal_mode_string;
346 /* Marker for where to display an arrow on top of the buffer text. */
348 Lisp_Object Voverlay_arrow_position;
350 /* String to display for the arrow. Only used on terminal frames. */
352 Lisp_Object Voverlay_arrow_string;
354 /* Values of those variables at last redisplay. However, if
355 Voverlay_arrow_position is a marker, last_arrow_position is its
356 numerical position. */
358 static Lisp_Object last_arrow_position, last_arrow_string;
360 /* Like mode-line-format, but for the title bar on a visible frame. */
362 Lisp_Object Vframe_title_format;
364 /* Like mode-line-format, but for the title bar on an iconified frame. */
366 Lisp_Object Vicon_title_format;
368 /* List of functions to call when a window's size changes. These
369 functions get one arg, a frame on which one or more windows' sizes
370 have changed. */
372 static Lisp_Object Vwindow_size_change_functions;
374 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
376 /* Nonzero if overlay arrow has been displayed once in this window. */
378 static int overlay_arrow_seen;
380 /* Nonzero means highlight the region even in nonselected windows. */
382 int highlight_nonselected_windows;
384 /* If cursor motion alone moves point off frame, try scrolling this
385 many lines up or down if that will bring it back. */
387 static int scroll_step;
389 /* Nonzero means scroll just far enough to bring point back on the
390 screen, when appropriate. */
392 static int scroll_conservatively;
394 /* Recenter the window whenever point gets within this many lines of
395 the top or bottom of the window. This value is translated into a
396 pixel value by multiplying it with CANON_Y_UNIT, which means that
397 there is really a fixed pixel height scroll margin. */
399 int scroll_margin;
401 /* Number of windows showing the buffer of the selected window (or
402 another buffer with the same base buffer). keyboard.c refers to
403 this. */
405 int buffer_shared;
407 /* Vector containing glyphs for an ellipsis `...'. */
409 static Lisp_Object default_invis_vector[3];
411 /* Zero means display the mode-line/header-line/menu-bar in the default face
412 (this slightly odd definition is for compatibility with previous versions
413 of emacs), non-zero means display them using their respective faces.
415 This variable is deprecated. */
417 int mode_line_inverse_video;
419 /* Prompt to display in front of the mini-buffer contents. */
421 Lisp_Object minibuf_prompt;
423 /* Width of current mini-buffer prompt. Only set after display_line
424 of the line that contains the prompt. */
426 int minibuf_prompt_width;
427 int minibuf_prompt_pixel_width;
429 /* This is the window where the echo area message was displayed. It
430 is always a mini-buffer window, but it may not be the same window
431 currently active as a mini-buffer. */
433 Lisp_Object echo_area_window;
435 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
436 pushes the current message and the value of
437 message_enable_multibyte on the stack, the function restore_message
438 pops the stack and displays MESSAGE again. */
440 Lisp_Object Vmessage_stack;
442 /* Nonzero means multibyte characters were enabled when the echo area
443 message was specified. */
445 int message_enable_multibyte;
447 /* Nonzero if we should redraw the mode lines on the next redisplay. */
449 int update_mode_lines;
451 /* Nonzero if window sizes or contents have changed since last
452 redisplay that finished. */
454 int windows_or_buffers_changed;
456 /* Nonzero after display_mode_line if %l was used and it displayed a
457 line number. */
459 int line_number_displayed;
461 /* Maximum buffer size for which to display line numbers. */
463 Lisp_Object Vline_number_display_limit;
465 /* Line width to consider when repositioning for line number display. */
467 static int line_number_display_limit_width;
469 /* Number of lines to keep in the message log buffer. t means
470 infinite. nil means don't log at all. */
472 Lisp_Object Vmessage_log_max;
474 /* The name of the *Messages* buffer, a string. */
476 static Lisp_Object Vmessages_buffer_name;
478 /* Current, index 0, and last displayed echo area message. Either
479 buffers from echo_buffers, or nil to indicate no message. */
481 Lisp_Object echo_area_buffer[2];
483 /* The buffers referenced from echo_area_buffer. */
485 static Lisp_Object echo_buffer[2];
487 /* A vector saved used in with_area_buffer to reduce consing. */
489 static Lisp_Object Vwith_echo_area_save_vector;
491 /* Non-zero means display_echo_area should display the last echo area
492 message again. Set by redisplay_preserve_echo_area. */
494 static int display_last_displayed_message_p;
496 /* Nonzero if echo area is being used by print; zero if being used by
497 message. */
499 int message_buf_print;
501 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
503 Lisp_Object Qinhibit_menubar_update;
504 int inhibit_menubar_update;
506 /* Maximum height for resizing mini-windows. Either a float
507 specifying a fraction of the available height, or an integer
508 specifying a number of lines. */
510 Lisp_Object Vmax_mini_window_height;
512 /* Non-zero means messages should be displayed with truncated
513 lines instead of being continued. */
515 int message_truncate_lines;
516 Lisp_Object Qmessage_truncate_lines;
518 /* Set to 1 in clear_message to make redisplay_internal aware
519 of an emptied echo area. */
521 static int message_cleared_p;
523 /* Non-zero means we want a hollow cursor in windows that are not
524 selected. Zero means there's no cursor in such windows. */
526 int cursor_in_non_selected_windows;
527 Lisp_Object Qcursor_in_non_selected_windows;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_max_ascent, last_height;
540 /* Non-zero if there's a help-echo in the echo area. */
542 int help_echo_showing_p;
544 /* If >= 0, computed, exact values of mode-line and header-line height
545 to use in the macros CURRENT_MODE_LINE_HEIGHT and
546 CURRENT_HEADER_LINE_HEIGHT. */
548 int current_mode_line_height, current_header_line_height;
550 /* The maximum distance to look ahead for text properties. Values
551 that are too small let us call compute_char_face and similar
552 functions too often which is expensive. Values that are too large
553 let us call compute_char_face and alike too often because we
554 might not be interested in text properties that far away. */
556 #define TEXT_PROP_DISTANCE_LIMIT 100
558 #if GLYPH_DEBUG
560 /* Variables to turn off display optimizations from Lisp. */
562 int inhibit_try_window_id, inhibit_try_window_reusing;
563 int inhibit_try_cursor_movement;
565 /* Non-zero means print traces of redisplay if compiled with
566 GLYPH_DEBUG != 0. */
568 int trace_redisplay_p;
570 #endif /* GLYPH_DEBUG */
572 #ifdef DEBUG_TRACE_MOVE
573 /* Non-zero means trace with TRACE_MOVE to stderr. */
574 int trace_move;
576 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
577 #else
578 #define TRACE_MOVE(x) (void) 0
579 #endif
581 /* Non-zero means automatically scroll windows horizontally to make
582 point visible. */
584 int automatic_hscrolling_p;
586 /* How close to the margin can point get before the window is scrolled
587 horizontally. */
588 int automatic_hscroll_margin;
590 /* How much to scroll horizontally when point is inside the above margin. */
591 Lisp_Object Vautomatic_hscroll_step;
593 /* A list of symbols, one for each supported image type. */
595 Lisp_Object Vimage_types;
597 /* The variable `resize-mini-windows'. If nil, don't resize
598 mini-windows. If t, always resize them to fit the text they
599 display. If `grow-only', let mini-windows grow only until they
600 become empty. */
602 Lisp_Object Vresize_mini_windows;
604 /* Buffer being redisplayed -- for redisplay_window_error. */
606 struct buffer *displayed_buffer;
608 /* Value returned from text property handlers (see below). */
610 enum prop_handled
612 HANDLED_NORMALLY,
613 HANDLED_RECOMPUTE_PROPS,
614 HANDLED_OVERLAY_STRING_CONSUMED,
615 HANDLED_RETURN
618 /* A description of text properties that redisplay is interested
619 in. */
621 struct props
623 /* The name of the property. */
624 Lisp_Object *name;
626 /* A unique index for the property. */
627 enum prop_idx idx;
629 /* A handler function called to set up iterator IT from the property
630 at IT's current position. Value is used to steer handle_stop. */
631 enum prop_handled (*handler) P_ ((struct it *it));
634 static enum prop_handled handle_face_prop P_ ((struct it *));
635 static enum prop_handled handle_invisible_prop P_ ((struct it *));
636 static enum prop_handled handle_display_prop P_ ((struct it *));
637 static enum prop_handled handle_composition_prop P_ ((struct it *));
638 static enum prop_handled handle_overlay_change P_ ((struct it *));
639 static enum prop_handled handle_fontified_prop P_ ((struct it *));
641 /* Properties handled by iterators. */
643 static struct props it_props[] =
645 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
646 /* Handle `face' before `display' because some sub-properties of
647 `display' need to know the face. */
648 {&Qface, FACE_PROP_IDX, handle_face_prop},
649 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
650 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
651 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
652 {NULL, 0, NULL}
655 /* Value is the position described by X. If X is a marker, value is
656 the marker_position of X. Otherwise, value is X. */
658 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
660 /* Enumeration returned by some move_it_.* functions internally. */
662 enum move_it_result
664 /* Not used. Undefined value. */
665 MOVE_UNDEFINED,
667 /* Move ended at the requested buffer position or ZV. */
668 MOVE_POS_MATCH_OR_ZV,
670 /* Move ended at the requested X pixel position. */
671 MOVE_X_REACHED,
673 /* Move within a line ended at the end of a line that must be
674 continued. */
675 MOVE_LINE_CONTINUED,
677 /* Move within a line ended at the end of a line that would
678 be displayed truncated. */
679 MOVE_LINE_TRUNCATED,
681 /* Move within a line ended at a line end. */
682 MOVE_NEWLINE_OR_CR
687 /* Function prototypes. */
689 static void setup_for_ellipsis P_ ((struct it *));
690 static void mark_window_display_accurate_1 P_ ((struct window *, int));
691 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
692 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
693 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
694 static int redisplay_mode_lines P_ ((Lisp_Object, int));
695 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
697 #if 0
698 static int invisible_text_between_p P_ ((struct it *, int, int));
699 #endif
701 static int next_element_from_ellipsis P_ ((struct it *));
702 static void pint2str P_ ((char *, int, int));
703 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
704 struct text_pos));
705 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
706 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
707 static void store_frame_title_char P_ ((char));
708 static int store_frame_title P_ ((unsigned char *, int, int));
709 static void x_consider_frame_title P_ ((Lisp_Object));
710 static void handle_stop P_ ((struct it *));
711 static int tool_bar_lines_needed P_ ((struct frame *));
712 static int single_display_prop_intangible_p P_ ((Lisp_Object));
713 static void ensure_echo_area_buffers P_ ((void));
714 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
715 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
716 static int with_echo_area_buffer P_ ((struct window *, int,
717 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
718 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
719 static void clear_garbaged_frames P_ ((void));
720 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
722 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
723 static int display_echo_area P_ ((struct window *));
724 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
725 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
726 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
727 static int string_char_and_length P_ ((unsigned char *, int, int *));
728 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
729 struct text_pos));
730 static int compute_window_start_on_continuation_line P_ ((struct window *));
731 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
732 static void insert_left_trunc_glyphs P_ ((struct it *));
733 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
734 static void extend_face_to_end_of_line P_ ((struct it *));
735 static int append_space P_ ((struct it *, int));
736 static int make_cursor_line_fully_visible P_ ((struct window *));
737 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
738 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
739 static int trailing_whitespace_p P_ ((int));
740 static int message_log_check_duplicate P_ ((int, int, int, int));
741 static void push_it P_ ((struct it *));
742 static void pop_it P_ ((struct it *));
743 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
744 static void redisplay_internal P_ ((int));
745 static int echo_area_display P_ ((int));
746 static void redisplay_windows P_ ((Lisp_Object));
747 static void redisplay_window P_ ((Lisp_Object, int));
748 static Lisp_Object redisplay_window_error ();
749 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
750 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
751 static void update_menu_bar P_ ((struct frame *, int));
752 static int try_window_reusing_current_matrix P_ ((struct window *));
753 static int try_window_id P_ ((struct window *));
754 static int display_line P_ ((struct it *));
755 static int display_mode_lines P_ ((struct window *));
756 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
757 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
758 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
759 static void display_menu_bar P_ ((struct window *));
760 static int display_count_lines P_ ((int, int, int, int, int *));
761 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
762 int, int, struct it *, int, int, int, int));
763 static void compute_line_metrics P_ ((struct it *));
764 static void run_redisplay_end_trigger_hook P_ ((struct it *));
765 static int get_overlay_strings P_ ((struct it *, int));
766 static void next_overlay_string P_ ((struct it *));
767 static void reseat P_ ((struct it *, struct text_pos, int));
768 static void reseat_1 P_ ((struct it *, struct text_pos, int));
769 static void back_to_previous_visible_line_start P_ ((struct it *));
770 static void reseat_at_previous_visible_line_start P_ ((struct it *));
771 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
772 static int next_element_from_display_vector P_ ((struct it *));
773 static int next_element_from_string P_ ((struct it *));
774 static int next_element_from_c_string P_ ((struct it *));
775 static int next_element_from_buffer P_ ((struct it *));
776 static int next_element_from_composition P_ ((struct it *));
777 static int next_element_from_image P_ ((struct it *));
778 static int next_element_from_stretch P_ ((struct it *));
779 static void load_overlay_strings P_ ((struct it *, int));
780 static int init_from_display_pos P_ ((struct it *, struct window *,
781 struct display_pos *));
782 static void reseat_to_string P_ ((struct it *, unsigned char *,
783 Lisp_Object, int, int, int, int));
784 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
785 int, int, int));
786 void move_it_vertically_backward P_ ((struct it *, int));
787 static void init_to_row_start P_ ((struct it *, struct window *,
788 struct glyph_row *));
789 static int init_to_row_end P_ ((struct it *, struct window *,
790 struct glyph_row *));
791 static void back_to_previous_line_start P_ ((struct it *));
792 static int forward_to_next_line_start P_ ((struct it *, int *));
793 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
794 Lisp_Object, int));
795 static struct text_pos string_pos P_ ((int, Lisp_Object));
796 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
797 static int number_of_chars P_ ((unsigned char *, int));
798 static void compute_stop_pos P_ ((struct it *));
799 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
800 Lisp_Object));
801 static int face_before_or_after_it_pos P_ ((struct it *, int));
802 static int next_overlay_change P_ ((int));
803 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
804 Lisp_Object, struct text_pos *,
805 int));
806 static int underlying_face_id P_ ((struct it *));
807 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
808 struct window *));
810 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
811 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
813 #ifdef HAVE_WINDOW_SYSTEM
815 static void update_tool_bar P_ ((struct frame *, int));
816 static void build_desired_tool_bar_string P_ ((struct frame *f));
817 static int redisplay_tool_bar P_ ((struct frame *));
818 static void display_tool_bar_line P_ ((struct it *));
820 #endif /* HAVE_WINDOW_SYSTEM */
823 /***********************************************************************
824 Window display dimensions
825 ***********************************************************************/
827 /* Return the window-relative maximum y + 1 for glyph rows displaying
828 text in window W. This is the height of W minus the height of a
829 mode line, if any. */
831 INLINE int
832 window_text_bottom_y (w)
833 struct window *w;
835 struct frame *f = XFRAME (w->frame);
836 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
838 if (WINDOW_WANTS_MODELINE_P (w))
839 height -= CURRENT_MODE_LINE_HEIGHT (w);
840 return height;
844 /* Return the pixel width of display area AREA of window W. AREA < 0
845 means return the total width of W, not including fringes to
846 the left and right of the window. */
848 INLINE int
849 window_box_width (w, area)
850 struct window *w;
851 int area;
853 struct frame *f = XFRAME (w->frame);
854 int width = XFASTINT (w->width);
856 if (!w->pseudo_window_p)
858 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
860 if (area == TEXT_AREA)
862 if (INTEGERP (w->left_margin_width))
863 width -= XFASTINT (w->left_margin_width);
864 if (INTEGERP (w->right_margin_width))
865 width -= XFASTINT (w->right_margin_width);
867 else if (area == LEFT_MARGIN_AREA)
868 width = (INTEGERP (w->left_margin_width)
869 ? XFASTINT (w->left_margin_width) : 0);
870 else if (area == RIGHT_MARGIN_AREA)
871 width = (INTEGERP (w->right_margin_width)
872 ? XFASTINT (w->right_margin_width) : 0);
875 return width * CANON_X_UNIT (f);
879 /* Return the pixel height of the display area of window W, not
880 including mode lines of W, if any. */
882 INLINE int
883 window_box_height (w)
884 struct window *w;
886 struct frame *f = XFRAME (w->frame);
887 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
889 xassert (height >= 0);
891 /* Note: the code below that determines the mode-line/header-line
892 height is essentially the same as that contained in the macro
893 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
894 the appropriate glyph row has its `mode_line_p' flag set,
895 and if it doesn't, uses estimate_mode_line_height instead. */
897 if (WINDOW_WANTS_MODELINE_P (w))
899 struct glyph_row *ml_row
900 = (w->current_matrix && w->current_matrix->rows
901 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
902 : 0);
903 if (ml_row && ml_row->mode_line_p)
904 height -= ml_row->height;
905 else
906 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
909 if (WINDOW_WANTS_HEADER_LINE_P (w))
911 struct glyph_row *hl_row
912 = (w->current_matrix && w->current_matrix->rows
913 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
914 : 0);
915 if (hl_row && hl_row->mode_line_p)
916 height -= hl_row->height;
917 else
918 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
921 /* With a very small font and a mode-line that's taller than
922 default, we might end up with a negative height. */
923 return max (0, height);
927 /* Return the frame-relative coordinate of the left edge of display
928 area AREA of window W. AREA < 0 means return the left edge of the
929 whole window, to the right of the left fringe of W. */
931 INLINE int
932 window_box_left (w, area)
933 struct window *w;
934 int area;
936 struct frame *f = XFRAME (w->frame);
937 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
939 if (!w->pseudo_window_p)
941 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
942 + FRAME_LEFT_FRINGE_WIDTH (f));
944 if (area == TEXT_AREA)
945 x += window_box_width (w, LEFT_MARGIN_AREA);
946 else if (area == RIGHT_MARGIN_AREA)
947 x += (window_box_width (w, LEFT_MARGIN_AREA)
948 + window_box_width (w, TEXT_AREA));
951 return x;
955 /* Return the frame-relative coordinate of the right edge of display
956 area AREA of window W. AREA < 0 means return the left edge of the
957 whole window, to the left of the right fringe of W. */
959 INLINE int
960 window_box_right (w, area)
961 struct window *w;
962 int area;
964 return window_box_left (w, area) + window_box_width (w, area);
968 /* Get the bounding box of the display area AREA of window W, without
969 mode lines, in frame-relative coordinates. AREA < 0 means the
970 whole window, not including the left and right fringes of
971 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
972 coordinates of the upper-left corner of the box. Return in
973 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
975 INLINE void
976 window_box (w, area, box_x, box_y, box_width, box_height)
977 struct window *w;
978 int area;
979 int *box_x, *box_y, *box_width, *box_height;
981 struct frame *f = XFRAME (w->frame);
983 *box_width = window_box_width (w, area);
984 *box_height = window_box_height (w);
985 *box_x = window_box_left (w, area);
986 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
987 + XFASTINT (w->top) * CANON_Y_UNIT (f));
988 if (WINDOW_WANTS_HEADER_LINE_P (w))
989 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
993 /* Get the bounding box of the display area AREA of window W, without
994 mode lines. AREA < 0 means the whole window, not including the
995 left and right fringe of the window. Return in *TOP_LEFT_X
996 and TOP_LEFT_Y the frame-relative pixel coordinates of the
997 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
998 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
999 box. */
1001 INLINE void
1002 window_box_edges (w, area, top_left_x, top_left_y,
1003 bottom_right_x, bottom_right_y)
1004 struct window *w;
1005 int area;
1006 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1008 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1009 bottom_right_y);
1010 *bottom_right_x += *top_left_x;
1011 *bottom_right_y += *top_left_y;
1016 /***********************************************************************
1017 Utilities
1018 ***********************************************************************/
1020 /* Return the bottom y-position of the line the iterator IT is in.
1021 This can modify IT's settings. */
1024 line_bottom_y (it)
1025 struct it *it;
1027 int line_height = it->max_ascent + it->max_descent;
1028 int line_top_y = it->current_y;
1030 if (line_height == 0)
1032 if (last_height)
1033 line_height = last_height;
1034 else if (IT_CHARPOS (*it) < ZV)
1036 move_it_by_lines (it, 1, 1);
1037 line_height = (it->max_ascent || it->max_descent
1038 ? it->max_ascent + it->max_descent
1039 : last_height);
1041 else
1043 struct glyph_row *row = it->glyph_row;
1045 /* Use the default character height. */
1046 it->glyph_row = NULL;
1047 it->what = IT_CHARACTER;
1048 it->c = ' ';
1049 it->len = 1;
1050 PRODUCE_GLYPHS (it);
1051 line_height = it->ascent + it->descent;
1052 it->glyph_row = row;
1056 return line_top_y + line_height;
1060 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1061 1 if POS is visible and the line containing POS is fully visible.
1062 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1063 and header-lines heights. */
1066 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1067 struct window *w;
1068 int charpos, *fully, exact_mode_line_heights_p;
1070 struct it it;
1071 struct text_pos top;
1072 int visible_p;
1073 struct buffer *old_buffer = NULL;
1075 if (XBUFFER (w->buffer) != current_buffer)
1077 old_buffer = current_buffer;
1078 set_buffer_internal_1 (XBUFFER (w->buffer));
1081 *fully = visible_p = 0;
1082 SET_TEXT_POS_FROM_MARKER (top, w->start);
1084 /* Compute exact mode line heights, if requested. */
1085 if (exact_mode_line_heights_p)
1087 if (WINDOW_WANTS_MODELINE_P (w))
1088 current_mode_line_height
1089 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1090 current_buffer->mode_line_format);
1092 if (WINDOW_WANTS_HEADER_LINE_P (w))
1093 current_header_line_height
1094 = display_mode_line (w, HEADER_LINE_FACE_ID,
1095 current_buffer->header_line_format);
1098 start_display (&it, w, top);
1099 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1100 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1102 /* Note that we may overshoot because of invisible text. */
1103 if (IT_CHARPOS (it) >= charpos)
1105 int top_y = it.current_y;
1106 int bottom_y = line_bottom_y (&it);
1107 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1109 if (top_y < window_top_y)
1110 visible_p = bottom_y > window_top_y;
1111 else if (top_y < it.last_visible_y)
1113 visible_p = 1;
1114 *fully = bottom_y <= it.last_visible_y;
1117 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1119 move_it_by_lines (&it, 1, 0);
1120 if (charpos < IT_CHARPOS (it))
1122 visible_p = 1;
1123 *fully = 0;
1127 if (old_buffer)
1128 set_buffer_internal_1 (old_buffer);
1130 current_header_line_height = current_mode_line_height = -1;
1131 return visible_p;
1135 /* Return the next character from STR which is MAXLEN bytes long.
1136 Return in *LEN the length of the character. This is like
1137 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1138 we find one, we return a `?', but with the length of the invalid
1139 character. */
1141 static INLINE int
1142 string_char_and_length (str, maxlen, len)
1143 unsigned char *str;
1144 int maxlen, *len;
1146 int c;
1148 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1149 if (!CHAR_VALID_P (c, 1))
1150 /* We may not change the length here because other places in Emacs
1151 don't use this function, i.e. they silently accept invalid
1152 characters. */
1153 c = '?';
1155 return c;
1160 /* Given a position POS containing a valid character and byte position
1161 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1163 static struct text_pos
1164 string_pos_nchars_ahead (pos, string, nchars)
1165 struct text_pos pos;
1166 Lisp_Object string;
1167 int nchars;
1169 xassert (STRINGP (string) && nchars >= 0);
1171 if (STRING_MULTIBYTE (string))
1173 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1174 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1175 int len;
1177 while (nchars--)
1179 string_char_and_length (p, rest, &len);
1180 p += len, rest -= len;
1181 xassert (rest >= 0);
1182 CHARPOS (pos) += 1;
1183 BYTEPOS (pos) += len;
1186 else
1187 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1189 return pos;
1193 /* Value is the text position, i.e. character and byte position,
1194 for character position CHARPOS in STRING. */
1196 static INLINE struct text_pos
1197 string_pos (charpos, string)
1198 int charpos;
1199 Lisp_Object string;
1201 struct text_pos pos;
1202 xassert (STRINGP (string));
1203 xassert (charpos >= 0);
1204 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1205 return pos;
1209 /* Value is a text position, i.e. character and byte position, for
1210 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1211 means recognize multibyte characters. */
1213 static struct text_pos
1214 c_string_pos (charpos, s, multibyte_p)
1215 int charpos;
1216 unsigned char *s;
1217 int multibyte_p;
1219 struct text_pos pos;
1221 xassert (s != NULL);
1222 xassert (charpos >= 0);
1224 if (multibyte_p)
1226 int rest = strlen (s), len;
1228 SET_TEXT_POS (pos, 0, 0);
1229 while (charpos--)
1231 string_char_and_length (s, rest, &len);
1232 s += len, rest -= len;
1233 xassert (rest >= 0);
1234 CHARPOS (pos) += 1;
1235 BYTEPOS (pos) += len;
1238 else
1239 SET_TEXT_POS (pos, charpos, charpos);
1241 return pos;
1245 /* Value is the number of characters in C string S. MULTIBYTE_P
1246 non-zero means recognize multibyte characters. */
1248 static int
1249 number_of_chars (s, multibyte_p)
1250 unsigned char *s;
1251 int multibyte_p;
1253 int nchars;
1255 if (multibyte_p)
1257 int rest = strlen (s), len;
1258 unsigned char *p = (unsigned char *) s;
1260 for (nchars = 0; rest > 0; ++nchars)
1262 string_char_and_length (p, rest, &len);
1263 rest -= len, p += len;
1266 else
1267 nchars = strlen (s);
1269 return nchars;
1273 /* Compute byte position NEWPOS->bytepos corresponding to
1274 NEWPOS->charpos. POS is a known position in string STRING.
1275 NEWPOS->charpos must be >= POS.charpos. */
1277 static void
1278 compute_string_pos (newpos, pos, string)
1279 struct text_pos *newpos, pos;
1280 Lisp_Object string;
1282 xassert (STRINGP (string));
1283 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1285 if (STRING_MULTIBYTE (string))
1286 *newpos = string_pos_nchars_ahead (pos, string,
1287 CHARPOS (*newpos) - CHARPOS (pos));
1288 else
1289 BYTEPOS (*newpos) = CHARPOS (*newpos);
1294 /***********************************************************************
1295 Lisp form evaluation
1296 ***********************************************************************/
1298 /* Error handler for safe_eval and safe_call. */
1300 static Lisp_Object
1301 safe_eval_handler (arg)
1302 Lisp_Object arg;
1304 add_to_log ("Error during redisplay: %s", arg, Qnil);
1305 return Qnil;
1309 /* Evaluate SEXPR and return the result, or nil if something went
1310 wrong. Prevent redisplay during the evaluation. */
1312 Lisp_Object
1313 safe_eval (sexpr)
1314 Lisp_Object sexpr;
1316 Lisp_Object val;
1318 if (inhibit_eval_during_redisplay)
1319 val = Qnil;
1320 else
1322 int count = BINDING_STACK_SIZE ();
1323 struct gcpro gcpro1;
1325 GCPRO1 (sexpr);
1326 specbind (Qinhibit_redisplay, Qt);
1327 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1328 safe_eval_handler);
1329 UNGCPRO;
1330 val = unbind_to (count, val);
1333 return val;
1337 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1338 Return the result, or nil if something went wrong. Prevent
1339 redisplay during the evaluation. */
1341 Lisp_Object
1342 safe_call (nargs, args)
1343 int nargs;
1344 Lisp_Object *args;
1346 Lisp_Object val;
1348 if (inhibit_eval_during_redisplay)
1349 val = Qnil;
1350 else
1352 int count = BINDING_STACK_SIZE ();
1353 struct gcpro gcpro1;
1355 GCPRO1 (args[0]);
1356 gcpro1.nvars = nargs;
1357 specbind (Qinhibit_redisplay, Qt);
1358 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1359 safe_eval_handler);
1360 UNGCPRO;
1361 val = unbind_to (count, val);
1364 return val;
1368 /* Call function FN with one argument ARG.
1369 Return the result, or nil if something went wrong. */
1371 Lisp_Object
1372 safe_call1 (fn, arg)
1373 Lisp_Object fn, arg;
1375 Lisp_Object args[2];
1376 args[0] = fn;
1377 args[1] = arg;
1378 return safe_call (2, args);
1383 /***********************************************************************
1384 Debugging
1385 ***********************************************************************/
1387 #if 0
1389 /* Define CHECK_IT to perform sanity checks on iterators.
1390 This is for debugging. It is too slow to do unconditionally. */
1392 static void
1393 check_it (it)
1394 struct it *it;
1396 if (it->method == next_element_from_string)
1398 xassert (STRINGP (it->string));
1399 xassert (IT_STRING_CHARPOS (*it) >= 0);
1401 else if (it->method == next_element_from_buffer)
1403 /* Check that character and byte positions agree. */
1404 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1407 if (it->dpvec)
1408 xassert (it->current.dpvec_index >= 0);
1409 else
1410 xassert (it->current.dpvec_index < 0);
1413 #define CHECK_IT(IT) check_it ((IT))
1415 #else /* not 0 */
1417 #define CHECK_IT(IT) (void) 0
1419 #endif /* not 0 */
1422 #if GLYPH_DEBUG
1424 /* Check that the window end of window W is what we expect it
1425 to be---the last row in the current matrix displaying text. */
1427 static void
1428 check_window_end (w)
1429 struct window *w;
1431 if (!MINI_WINDOW_P (w)
1432 && !NILP (w->window_end_valid))
1434 struct glyph_row *row;
1435 xassert ((row = MATRIX_ROW (w->current_matrix,
1436 XFASTINT (w->window_end_vpos)),
1437 !row->enabled_p
1438 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1439 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1443 #define CHECK_WINDOW_END(W) check_window_end ((W))
1445 #else /* not GLYPH_DEBUG */
1447 #define CHECK_WINDOW_END(W) (void) 0
1449 #endif /* not GLYPH_DEBUG */
1453 /***********************************************************************
1454 Iterator initialization
1455 ***********************************************************************/
1457 /* Initialize IT for displaying current_buffer in window W, starting
1458 at character position CHARPOS. CHARPOS < 0 means that no buffer
1459 position is specified which is useful when the iterator is assigned
1460 a position later. BYTEPOS is the byte position corresponding to
1461 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1463 If ROW is not null, calls to produce_glyphs with IT as parameter
1464 will produce glyphs in that row.
1466 BASE_FACE_ID is the id of a base face to use. It must be one of
1467 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1468 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1469 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1471 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1472 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1473 will be initialized to use the corresponding mode line glyph row of
1474 the desired matrix of W. */
1476 void
1477 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1478 struct it *it;
1479 struct window *w;
1480 int charpos, bytepos;
1481 struct glyph_row *row;
1482 enum face_id base_face_id;
1484 int highlight_region_p;
1486 /* Some precondition checks. */
1487 xassert (w != NULL && it != NULL);
1488 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1489 && charpos <= ZV));
1491 /* If face attributes have been changed since the last redisplay,
1492 free realized faces now because they depend on face definitions
1493 that might have changed. */
1494 if (face_change_count)
1496 face_change_count = 0;
1497 free_all_realized_faces (Qnil);
1500 /* Use one of the mode line rows of W's desired matrix if
1501 appropriate. */
1502 if (row == NULL)
1504 if (base_face_id == MODE_LINE_FACE_ID
1505 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1506 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1507 else if (base_face_id == HEADER_LINE_FACE_ID)
1508 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1511 /* Clear IT. */
1512 bzero (it, sizeof *it);
1513 it->current.overlay_string_index = -1;
1514 it->current.dpvec_index = -1;
1515 it->base_face_id = base_face_id;
1517 /* The window in which we iterate over current_buffer: */
1518 XSETWINDOW (it->window, w);
1519 it->w = w;
1520 it->f = XFRAME (w->frame);
1522 /* Extra space between lines (on window systems only). */
1523 if (base_face_id == DEFAULT_FACE_ID
1524 && FRAME_WINDOW_P (it->f))
1526 if (NATNUMP (current_buffer->extra_line_spacing))
1527 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1528 else if (it->f->extra_line_spacing > 0)
1529 it->extra_line_spacing = it->f->extra_line_spacing;
1532 /* If realized faces have been removed, e.g. because of face
1533 attribute changes of named faces, recompute them. When running
1534 in batch mode, the face cache of Vterminal_frame is null. If
1535 we happen to get called, make a dummy face cache. */
1536 if (
1537 #ifndef WINDOWSNT
1538 noninteractive &&
1539 #endif
1540 FRAME_FACE_CACHE (it->f) == NULL)
1541 init_frame_faces (it->f);
1542 if (FRAME_FACE_CACHE (it->f)->used == 0)
1543 recompute_basic_faces (it->f);
1545 /* Current value of the `space-width', and 'height' properties. */
1546 it->space_width = Qnil;
1547 it->font_height = Qnil;
1549 /* Are control characters displayed as `^C'? */
1550 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1552 /* -1 means everything between a CR and the following line end
1553 is invisible. >0 means lines indented more than this value are
1554 invisible. */
1555 it->selective = (INTEGERP (current_buffer->selective_display)
1556 ? XFASTINT (current_buffer->selective_display)
1557 : (!NILP (current_buffer->selective_display)
1558 ? -1 : 0));
1559 it->selective_display_ellipsis_p
1560 = !NILP (current_buffer->selective_display_ellipses);
1562 /* Display table to use. */
1563 it->dp = window_display_table (w);
1565 /* Are multibyte characters enabled in current_buffer? */
1566 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1568 /* Non-zero if we should highlight the region. */
1569 highlight_region_p
1570 = (!NILP (Vtransient_mark_mode)
1571 && !NILP (current_buffer->mark_active)
1572 && XMARKER (current_buffer->mark)->buffer != 0);
1574 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1575 start and end of a visible region in window IT->w. Set both to
1576 -1 to indicate no region. */
1577 if (highlight_region_p
1578 /* Maybe highlight only in selected window. */
1579 && (/* Either show region everywhere. */
1580 highlight_nonselected_windows
1581 /* Or show region in the selected window. */
1582 || w == XWINDOW (selected_window)
1583 /* Or show the region if we are in the mini-buffer and W is
1584 the window the mini-buffer refers to. */
1585 || (MINI_WINDOW_P (XWINDOW (selected_window))
1586 && WINDOWP (Vminibuf_selected_window)
1587 && w == XWINDOW (Vminibuf_selected_window))))
1589 int charpos = marker_position (current_buffer->mark);
1590 it->region_beg_charpos = min (PT, charpos);
1591 it->region_end_charpos = max (PT, charpos);
1593 else
1594 it->region_beg_charpos = it->region_end_charpos = -1;
1596 /* Get the position at which the redisplay_end_trigger hook should
1597 be run, if it is to be run at all. */
1598 if (MARKERP (w->redisplay_end_trigger)
1599 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1600 it->redisplay_end_trigger_charpos
1601 = marker_position (w->redisplay_end_trigger);
1602 else if (INTEGERP (w->redisplay_end_trigger))
1603 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1605 /* Correct bogus values of tab_width. */
1606 it->tab_width = XINT (current_buffer->tab_width);
1607 if (it->tab_width <= 0 || it->tab_width > 1000)
1608 it->tab_width = 8;
1610 /* Are lines in the display truncated? */
1611 it->truncate_lines_p
1612 = (base_face_id != DEFAULT_FACE_ID
1613 || XINT (it->w->hscroll)
1614 || (truncate_partial_width_windows
1615 && !WINDOW_FULL_WIDTH_P (it->w))
1616 || !NILP (current_buffer->truncate_lines));
1618 /* Get dimensions of truncation and continuation glyphs. These are
1619 displayed as fringe bitmaps under X, so we don't need them for such
1620 frames. */
1621 if (!FRAME_WINDOW_P (it->f))
1623 if (it->truncate_lines_p)
1625 /* We will need the truncation glyph. */
1626 xassert (it->glyph_row == NULL);
1627 produce_special_glyphs (it, IT_TRUNCATION);
1628 it->truncation_pixel_width = it->pixel_width;
1630 else
1632 /* We will need the continuation glyph. */
1633 xassert (it->glyph_row == NULL);
1634 produce_special_glyphs (it, IT_CONTINUATION);
1635 it->continuation_pixel_width = it->pixel_width;
1638 /* Reset these values to zero because the produce_special_glyphs
1639 above has changed them. */
1640 it->pixel_width = it->ascent = it->descent = 0;
1641 it->phys_ascent = it->phys_descent = 0;
1644 /* Set this after getting the dimensions of truncation and
1645 continuation glyphs, so that we don't produce glyphs when calling
1646 produce_special_glyphs, above. */
1647 it->glyph_row = row;
1648 it->area = TEXT_AREA;
1650 /* Get the dimensions of the display area. The display area
1651 consists of the visible window area plus a horizontally scrolled
1652 part to the left of the window. All x-values are relative to the
1653 start of this total display area. */
1654 if (base_face_id != DEFAULT_FACE_ID)
1656 /* Mode lines, menu bar in terminal frames. */
1657 it->first_visible_x = 0;
1658 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1660 else
1662 it->first_visible_x
1663 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1664 it->last_visible_x = (it->first_visible_x
1665 + window_box_width (w, TEXT_AREA));
1667 /* If we truncate lines, leave room for the truncator glyph(s) at
1668 the right margin. Otherwise, leave room for the continuation
1669 glyph(s). Truncation and continuation glyphs are not inserted
1670 for window-based redisplay. */
1671 if (!FRAME_WINDOW_P (it->f))
1673 if (it->truncate_lines_p)
1674 it->last_visible_x -= it->truncation_pixel_width;
1675 else
1676 it->last_visible_x -= it->continuation_pixel_width;
1679 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1680 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1683 /* Leave room for a border glyph. */
1684 if (!FRAME_WINDOW_P (it->f)
1685 && !WINDOW_RIGHTMOST_P (it->w))
1686 it->last_visible_x -= 1;
1688 it->last_visible_y = window_text_bottom_y (w);
1690 /* For mode lines and alike, arrange for the first glyph having a
1691 left box line if the face specifies a box. */
1692 if (base_face_id != DEFAULT_FACE_ID)
1694 struct face *face;
1696 it->face_id = base_face_id;
1698 /* If we have a boxed mode line, make the first character appear
1699 with a left box line. */
1700 face = FACE_FROM_ID (it->f, base_face_id);
1701 if (face->box != FACE_NO_BOX)
1702 it->start_of_box_run_p = 1;
1705 /* If a buffer position was specified, set the iterator there,
1706 getting overlays and face properties from that position. */
1707 if (charpos >= BUF_BEG (current_buffer))
1709 it->end_charpos = ZV;
1710 it->face_id = -1;
1711 IT_CHARPOS (*it) = charpos;
1713 /* Compute byte position if not specified. */
1714 if (bytepos < charpos)
1715 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1716 else
1717 IT_BYTEPOS (*it) = bytepos;
1719 /* Compute faces etc. */
1720 reseat (it, it->current.pos, 1);
1723 CHECK_IT (it);
1727 /* Initialize IT for the display of window W with window start POS. */
1729 void
1730 start_display (it, w, pos)
1731 struct it *it;
1732 struct window *w;
1733 struct text_pos pos;
1735 struct glyph_row *row;
1736 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1738 row = w->desired_matrix->rows + first_vpos;
1739 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1741 if (!it->truncate_lines_p)
1743 int start_at_line_beg_p;
1744 int first_y = it->current_y;
1746 /* If window start is not at a line start, skip forward to POS to
1747 get the correct continuation lines width. */
1748 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1749 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1750 if (!start_at_line_beg_p)
1752 reseat_at_previous_visible_line_start (it);
1753 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1755 /* If lines are continued, this line may end in the middle
1756 of a multi-glyph character (e.g. a control character
1757 displayed as \003, or in the middle of an overlay
1758 string). In this case move_it_to above will not have
1759 taken us to the start of the continuation line but to the
1760 end of the continued line. */
1761 if (it->current_x > 0)
1763 if (it->current.dpvec_index >= 0
1764 || it->current.overlay_string_index >= 0)
1766 set_iterator_to_next (it, 1);
1767 move_it_in_display_line_to (it, -1, -1, 0);
1770 it->continuation_lines_width += it->current_x;
1773 /* We're starting a new display line, not affected by the
1774 height of the continued line, so clear the appropriate
1775 fields in the iterator structure. */
1776 it->max_ascent = it->max_descent = 0;
1777 it->max_phys_ascent = it->max_phys_descent = 0;
1779 it->current_y = first_y;
1780 it->vpos = 0;
1781 it->current_x = it->hpos = 0;
1785 #if 0 /* Don't assert the following because start_display is sometimes
1786 called intentionally with a window start that is not at a
1787 line start. Please leave this code in as a comment. */
1789 /* Window start should be on a line start, now. */
1790 xassert (it->continuation_lines_width
1791 || IT_CHARPOS (it) == BEGV
1792 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1793 #endif /* 0 */
1797 /* Return 1 if POS is a position in ellipses displayed for invisible
1798 text. W is the window we display, for text property lookup. */
1800 static int
1801 in_ellipses_for_invisible_text_p (pos, w)
1802 struct display_pos *pos;
1803 struct window *w;
1805 Lisp_Object prop, window;
1806 int ellipses_p = 0;
1807 int charpos = CHARPOS (pos->pos);
1809 /* If POS specifies a position in a display vector, this might
1810 be for an ellipsis displayed for invisible text. We won't
1811 get the iterator set up for delivering that ellipsis unless
1812 we make sure that it gets aware of the invisible text. */
1813 if (pos->dpvec_index >= 0
1814 && pos->overlay_string_index < 0
1815 && CHARPOS (pos->string_pos) < 0
1816 && charpos > BEGV
1817 && (XSETWINDOW (window, w),
1818 prop = Fget_char_property (make_number (charpos),
1819 Qinvisible, window),
1820 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1822 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1823 window);
1824 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1827 return ellipses_p;
1831 /* Initialize IT for stepping through current_buffer in window W,
1832 starting at position POS that includes overlay string and display
1833 vector/ control character translation position information. Value
1834 is zero if there are overlay strings with newlines at POS. */
1836 static int
1837 init_from_display_pos (it, w, pos)
1838 struct it *it;
1839 struct window *w;
1840 struct display_pos *pos;
1842 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1843 int i, overlay_strings_with_newlines = 0;
1845 /* If POS specifies a position in a display vector, this might
1846 be for an ellipsis displayed for invisible text. We won't
1847 get the iterator set up for delivering that ellipsis unless
1848 we make sure that it gets aware of the invisible text. */
1849 if (in_ellipses_for_invisible_text_p (pos, w))
1851 --charpos;
1852 bytepos = 0;
1855 /* Keep in mind: the call to reseat in init_iterator skips invisible
1856 text, so we might end up at a position different from POS. This
1857 is only a problem when POS is a row start after a newline and an
1858 overlay starts there with an after-string, and the overlay has an
1859 invisible property. Since we don't skip invisible text in
1860 display_line and elsewhere immediately after consuming the
1861 newline before the row start, such a POS will not be in a string,
1862 but the call to init_iterator below will move us to the
1863 after-string. */
1864 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1866 for (i = 0; i < it->n_overlay_strings; ++i)
1868 char *s = XSTRING (it->overlay_strings[i])->data;
1869 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1871 while (s < e && *s != '\n')
1872 ++s;
1874 if (s < e)
1876 overlay_strings_with_newlines = 1;
1877 break;
1881 /* If position is within an overlay string, set up IT to the right
1882 overlay string. */
1883 if (pos->overlay_string_index >= 0)
1885 int relative_index;
1887 /* If the first overlay string happens to have a `display'
1888 property for an image, the iterator will be set up for that
1889 image, and we have to undo that setup first before we can
1890 correct the overlay string index. */
1891 if (it->method == next_element_from_image)
1892 pop_it (it);
1894 /* We already have the first chunk of overlay strings in
1895 IT->overlay_strings. Load more until the one for
1896 pos->overlay_string_index is in IT->overlay_strings. */
1897 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1899 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1900 it->current.overlay_string_index = 0;
1901 while (n--)
1903 load_overlay_strings (it, 0);
1904 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1908 it->current.overlay_string_index = pos->overlay_string_index;
1909 relative_index = (it->current.overlay_string_index
1910 % OVERLAY_STRING_CHUNK_SIZE);
1911 it->string = it->overlay_strings[relative_index];
1912 xassert (STRINGP (it->string));
1913 it->current.string_pos = pos->string_pos;
1914 it->method = next_element_from_string;
1917 #if 0 /* This is bogus because POS not having an overlay string
1918 position does not mean it's after the string. Example: A
1919 line starting with a before-string and initialization of IT
1920 to the previous row's end position. */
1921 else if (it->current.overlay_string_index >= 0)
1923 /* If POS says we're already after an overlay string ending at
1924 POS, make sure to pop the iterator because it will be in
1925 front of that overlay string. When POS is ZV, we've thereby
1926 also ``processed'' overlay strings at ZV. */
1927 while (it->sp)
1928 pop_it (it);
1929 it->current.overlay_string_index = -1;
1930 it->method = next_element_from_buffer;
1931 if (CHARPOS (pos->pos) == ZV)
1932 it->overlay_strings_at_end_processed_p = 1;
1934 #endif /* 0 */
1936 if (CHARPOS (pos->string_pos) >= 0)
1938 /* Recorded position is not in an overlay string, but in another
1939 string. This can only be a string from a `display' property.
1940 IT should already be filled with that string. */
1941 it->current.string_pos = pos->string_pos;
1942 xassert (STRINGP (it->string));
1945 /* Restore position in display vector translations, control
1946 character translations or ellipses. */
1947 if (pos->dpvec_index >= 0)
1949 if (it->dpvec == NULL)
1950 get_next_display_element (it);
1951 xassert (it->dpvec && it->current.dpvec_index == 0);
1952 it->current.dpvec_index = pos->dpvec_index;
1955 CHECK_IT (it);
1956 return !overlay_strings_with_newlines;
1960 /* Initialize IT for stepping through current_buffer in window W
1961 starting at ROW->start. */
1963 static void
1964 init_to_row_start (it, w, row)
1965 struct it *it;
1966 struct window *w;
1967 struct glyph_row *row;
1969 init_from_display_pos (it, w, &row->start);
1970 it->continuation_lines_width = row->continuation_lines_width;
1971 CHECK_IT (it);
1975 /* Initialize IT for stepping through current_buffer in window W
1976 starting in the line following ROW, i.e. starting at ROW->end.
1977 Value is zero if there are overlay strings with newlines at ROW's
1978 end position. */
1980 static int
1981 init_to_row_end (it, w, row)
1982 struct it *it;
1983 struct window *w;
1984 struct glyph_row *row;
1986 int success = 0;
1988 if (init_from_display_pos (it, w, &row->end))
1990 if (row->continued_p)
1991 it->continuation_lines_width
1992 = row->continuation_lines_width + row->pixel_width;
1993 CHECK_IT (it);
1994 success = 1;
1997 return success;
2003 /***********************************************************************
2004 Text properties
2005 ***********************************************************************/
2007 /* Called when IT reaches IT->stop_charpos. Handle text property and
2008 overlay changes. Set IT->stop_charpos to the next position where
2009 to stop. */
2011 static void
2012 handle_stop (it)
2013 struct it *it;
2015 enum prop_handled handled;
2016 int handle_overlay_change_p = 1;
2017 struct props *p;
2019 it->dpvec = NULL;
2020 it->current.dpvec_index = -1;
2024 handled = HANDLED_NORMALLY;
2026 /* Call text property handlers. */
2027 for (p = it_props; p->handler; ++p)
2029 handled = p->handler (it);
2031 if (handled == HANDLED_RECOMPUTE_PROPS)
2032 break;
2033 else if (handled == HANDLED_RETURN)
2034 return;
2035 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2036 handle_overlay_change_p = 0;
2039 if (handled != HANDLED_RECOMPUTE_PROPS)
2041 /* Don't check for overlay strings below when set to deliver
2042 characters from a display vector. */
2043 if (it->method == next_element_from_display_vector)
2044 handle_overlay_change_p = 0;
2046 /* Handle overlay changes. */
2047 if (handle_overlay_change_p)
2048 handled = handle_overlay_change (it);
2050 /* Determine where to stop next. */
2051 if (handled == HANDLED_NORMALLY)
2052 compute_stop_pos (it);
2055 while (handled == HANDLED_RECOMPUTE_PROPS);
2059 /* Compute IT->stop_charpos from text property and overlay change
2060 information for IT's current position. */
2062 static void
2063 compute_stop_pos (it)
2064 struct it *it;
2066 register INTERVAL iv, next_iv;
2067 Lisp_Object object, limit, position;
2069 /* If nowhere else, stop at the end. */
2070 it->stop_charpos = it->end_charpos;
2072 if (STRINGP (it->string))
2074 /* Strings are usually short, so don't limit the search for
2075 properties. */
2076 object = it->string;
2077 limit = Qnil;
2078 position = make_number (IT_STRING_CHARPOS (*it));
2080 else
2082 int charpos;
2084 /* If next overlay change is in front of the current stop pos
2085 (which is IT->end_charpos), stop there. Note: value of
2086 next_overlay_change is point-max if no overlay change
2087 follows. */
2088 charpos = next_overlay_change (IT_CHARPOS (*it));
2089 if (charpos < it->stop_charpos)
2090 it->stop_charpos = charpos;
2092 /* If showing the region, we have to stop at the region
2093 start or end because the face might change there. */
2094 if (it->region_beg_charpos > 0)
2096 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2097 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2098 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2099 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2102 /* Set up variables for computing the stop position from text
2103 property changes. */
2104 XSETBUFFER (object, current_buffer);
2105 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2106 position = make_number (IT_CHARPOS (*it));
2110 /* Get the interval containing IT's position. Value is a null
2111 interval if there isn't such an interval. */
2112 iv = validate_interval_range (object, &position, &position, 0);
2113 if (!NULL_INTERVAL_P (iv))
2115 Lisp_Object values_here[LAST_PROP_IDX];
2116 struct props *p;
2118 /* Get properties here. */
2119 for (p = it_props; p->handler; ++p)
2120 values_here[p->idx] = textget (iv->plist, *p->name);
2122 /* Look for an interval following iv that has different
2123 properties. */
2124 for (next_iv = next_interval (iv);
2125 (!NULL_INTERVAL_P (next_iv)
2126 && (NILP (limit)
2127 || XFASTINT (limit) > next_iv->position));
2128 next_iv = next_interval (next_iv))
2130 for (p = it_props; p->handler; ++p)
2132 Lisp_Object new_value;
2134 new_value = textget (next_iv->plist, *p->name);
2135 if (!EQ (values_here[p->idx], new_value))
2136 break;
2139 if (p->handler)
2140 break;
2143 if (!NULL_INTERVAL_P (next_iv))
2145 if (INTEGERP (limit)
2146 && next_iv->position >= XFASTINT (limit))
2147 /* No text property change up to limit. */
2148 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2149 else
2150 /* Text properties change in next_iv. */
2151 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2155 xassert (STRINGP (it->string)
2156 || (it->stop_charpos >= BEGV
2157 && it->stop_charpos >= IT_CHARPOS (*it)));
2161 /* Return the position of the next overlay change after POS in
2162 current_buffer. Value is point-max if no overlay change
2163 follows. This is like `next-overlay-change' but doesn't use
2164 xmalloc. */
2166 static int
2167 next_overlay_change (pos)
2168 int pos;
2170 int noverlays;
2171 int endpos;
2172 Lisp_Object *overlays;
2173 int len;
2174 int i;
2176 /* Get all overlays at the given position. */
2177 len = 10;
2178 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2179 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2180 if (noverlays > len)
2182 len = noverlays;
2183 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2184 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2187 /* If any of these overlays ends before endpos,
2188 use its ending point instead. */
2189 for (i = 0; i < noverlays; ++i)
2191 Lisp_Object oend;
2192 int oendpos;
2194 oend = OVERLAY_END (overlays[i]);
2195 oendpos = OVERLAY_POSITION (oend);
2196 endpos = min (endpos, oendpos);
2199 return endpos;
2204 /***********************************************************************
2205 Fontification
2206 ***********************************************************************/
2208 /* Handle changes in the `fontified' property of the current buffer by
2209 calling hook functions from Qfontification_functions to fontify
2210 regions of text. */
2212 static enum prop_handled
2213 handle_fontified_prop (it)
2214 struct it *it;
2216 Lisp_Object prop, pos;
2217 enum prop_handled handled = HANDLED_NORMALLY;
2219 /* Get the value of the `fontified' property at IT's current buffer
2220 position. (The `fontified' property doesn't have a special
2221 meaning in strings.) If the value is nil, call functions from
2222 Qfontification_functions. */
2223 if (!STRINGP (it->string)
2224 && it->s == NULL
2225 && !NILP (Vfontification_functions)
2226 && !NILP (Vrun_hooks)
2227 && (pos = make_number (IT_CHARPOS (*it)),
2228 prop = Fget_char_property (pos, Qfontified, Qnil),
2229 NILP (prop)))
2231 int count = BINDING_STACK_SIZE ();
2232 Lisp_Object val;
2234 val = Vfontification_functions;
2235 specbind (Qfontification_functions, Qnil);
2237 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2238 safe_call1 (val, pos);
2239 else
2241 Lisp_Object globals, fn;
2242 struct gcpro gcpro1, gcpro2;
2244 globals = Qnil;
2245 GCPRO2 (val, globals);
2247 for (; CONSP (val); val = XCDR (val))
2249 fn = XCAR (val);
2251 if (EQ (fn, Qt))
2253 /* A value of t indicates this hook has a local
2254 binding; it means to run the global binding too.
2255 In a global value, t should not occur. If it
2256 does, we must ignore it to avoid an endless
2257 loop. */
2258 for (globals = Fdefault_value (Qfontification_functions);
2259 CONSP (globals);
2260 globals = XCDR (globals))
2262 fn = XCAR (globals);
2263 if (!EQ (fn, Qt))
2264 safe_call1 (fn, pos);
2267 else
2268 safe_call1 (fn, pos);
2271 UNGCPRO;
2274 unbind_to (count, Qnil);
2276 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2277 something. This avoids an endless loop if they failed to
2278 fontify the text for which reason ever. */
2279 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2280 handled = HANDLED_RECOMPUTE_PROPS;
2283 return handled;
2288 /***********************************************************************
2289 Faces
2290 ***********************************************************************/
2292 /* Set up iterator IT from face properties at its current position.
2293 Called from handle_stop. */
2295 static enum prop_handled
2296 handle_face_prop (it)
2297 struct it *it;
2299 int new_face_id, next_stop;
2301 if (!STRINGP (it->string))
2303 new_face_id
2304 = face_at_buffer_position (it->w,
2305 IT_CHARPOS (*it),
2306 it->region_beg_charpos,
2307 it->region_end_charpos,
2308 &next_stop,
2309 (IT_CHARPOS (*it)
2310 + TEXT_PROP_DISTANCE_LIMIT),
2313 /* Is this a start of a run of characters with box face?
2314 Caveat: this can be called for a freshly initialized
2315 iterator; face_id is -1 in this case. We know that the new
2316 face will not change until limit, i.e. if the new face has a
2317 box, all characters up to limit will have one. But, as
2318 usual, we don't know whether limit is really the end. */
2319 if (new_face_id != it->face_id)
2321 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2323 /* If new face has a box but old face has not, this is
2324 the start of a run of characters with box, i.e. it has
2325 a shadow on the left side. The value of face_id of the
2326 iterator will be -1 if this is the initial call that gets
2327 the face. In this case, we have to look in front of IT's
2328 position and see whether there is a face != new_face_id. */
2329 it->start_of_box_run_p
2330 = (new_face->box != FACE_NO_BOX
2331 && (it->face_id >= 0
2332 || IT_CHARPOS (*it) == BEG
2333 || new_face_id != face_before_it_pos (it)));
2334 it->face_box_p = new_face->box != FACE_NO_BOX;
2337 else
2339 int base_face_id, bufpos;
2341 if (it->current.overlay_string_index >= 0)
2342 bufpos = IT_CHARPOS (*it);
2343 else
2344 bufpos = 0;
2346 /* For strings from a buffer, i.e. overlay strings or strings
2347 from a `display' property, use the face at IT's current
2348 buffer position as the base face to merge with, so that
2349 overlay strings appear in the same face as surrounding
2350 text, unless they specify their own faces. */
2351 base_face_id = underlying_face_id (it);
2353 new_face_id = face_at_string_position (it->w,
2354 it->string,
2355 IT_STRING_CHARPOS (*it),
2356 bufpos,
2357 it->region_beg_charpos,
2358 it->region_end_charpos,
2359 &next_stop,
2360 base_face_id, 0);
2362 #if 0 /* This shouldn't be neccessary. Let's check it. */
2363 /* If IT is used to display a mode line we would really like to
2364 use the mode line face instead of the frame's default face. */
2365 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2366 && new_face_id == DEFAULT_FACE_ID)
2367 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2368 #endif
2370 /* Is this a start of a run of characters with box? Caveat:
2371 this can be called for a freshly allocated iterator; face_id
2372 is -1 is this case. We know that the new face will not
2373 change until the next check pos, i.e. if the new face has a
2374 box, all characters up to that position will have a
2375 box. But, as usual, we don't know whether that position
2376 is really the end. */
2377 if (new_face_id != it->face_id)
2379 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2380 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2382 /* If new face has a box but old face hasn't, this is the
2383 start of a run of characters with box, i.e. it has a
2384 shadow on the left side. */
2385 it->start_of_box_run_p
2386 = new_face->box && (old_face == NULL || !old_face->box);
2387 it->face_box_p = new_face->box != FACE_NO_BOX;
2391 it->face_id = new_face_id;
2392 return HANDLED_NORMALLY;
2396 /* Return the ID of the face ``underlying'' IT's current position,
2397 which is in a string. If the iterator is associated with a
2398 buffer, return the face at IT's current buffer position.
2399 Otherwise, use the iterator's base_face_id. */
2401 static int
2402 underlying_face_id (it)
2403 struct it *it;
2405 int face_id = it->base_face_id, i;
2407 xassert (STRINGP (it->string));
2409 for (i = it->sp - 1; i >= 0; --i)
2410 if (NILP (it->stack[i].string))
2411 face_id = it->stack[i].face_id;
2413 return face_id;
2417 /* Compute the face one character before or after the current position
2418 of IT. BEFORE_P non-zero means get the face in front of IT's
2419 position. Value is the id of the face. */
2421 static int
2422 face_before_or_after_it_pos (it, before_p)
2423 struct it *it;
2424 int before_p;
2426 int face_id, limit;
2427 int next_check_charpos;
2428 struct text_pos pos;
2430 xassert (it->s == NULL);
2432 if (STRINGP (it->string))
2434 int bufpos, base_face_id;
2436 /* No face change past the end of the string (for the case
2437 we are padding with spaces). No face change before the
2438 string start. */
2439 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2440 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2441 return it->face_id;
2443 /* Set pos to the position before or after IT's current position. */
2444 if (before_p)
2445 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2446 else
2447 /* For composition, we must check the character after the
2448 composition. */
2449 pos = (it->what == IT_COMPOSITION
2450 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2451 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2453 if (it->current.overlay_string_index >= 0)
2454 bufpos = IT_CHARPOS (*it);
2455 else
2456 bufpos = 0;
2458 base_face_id = underlying_face_id (it);
2460 /* Get the face for ASCII, or unibyte. */
2461 face_id = face_at_string_position (it->w,
2462 it->string,
2463 CHARPOS (pos),
2464 bufpos,
2465 it->region_beg_charpos,
2466 it->region_end_charpos,
2467 &next_check_charpos,
2468 base_face_id, 0);
2470 /* Correct the face for charsets different from ASCII. Do it
2471 for the multibyte case only. The face returned above is
2472 suitable for unibyte text if IT->string is unibyte. */
2473 if (STRING_MULTIBYTE (it->string))
2475 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2476 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2477 int c, len;
2478 struct face *face = FACE_FROM_ID (it->f, face_id);
2480 c = string_char_and_length (p, rest, &len);
2481 face_id = FACE_FOR_CHAR (it->f, face, c);
2484 else
2486 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2487 || (IT_CHARPOS (*it) <= BEGV && before_p))
2488 return it->face_id;
2490 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2491 pos = it->current.pos;
2493 if (before_p)
2494 DEC_TEXT_POS (pos, it->multibyte_p);
2495 else
2497 if (it->what == IT_COMPOSITION)
2498 /* For composition, we must check the position after the
2499 composition. */
2500 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2501 else
2502 INC_TEXT_POS (pos, it->multibyte_p);
2505 /* Determine face for CHARSET_ASCII, or unibyte. */
2506 face_id = face_at_buffer_position (it->w,
2507 CHARPOS (pos),
2508 it->region_beg_charpos,
2509 it->region_end_charpos,
2510 &next_check_charpos,
2511 limit, 0);
2513 /* Correct the face for charsets different from ASCII. Do it
2514 for the multibyte case only. The face returned above is
2515 suitable for unibyte text if current_buffer is unibyte. */
2516 if (it->multibyte_p)
2518 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2519 struct face *face = FACE_FROM_ID (it->f, face_id);
2520 face_id = FACE_FOR_CHAR (it->f, face, c);
2524 return face_id;
2529 /***********************************************************************
2530 Invisible text
2531 ***********************************************************************/
2533 /* Set up iterator IT from invisible properties at its current
2534 position. Called from handle_stop. */
2536 static enum prop_handled
2537 handle_invisible_prop (it)
2538 struct it *it;
2540 enum prop_handled handled = HANDLED_NORMALLY;
2542 if (STRINGP (it->string))
2544 extern Lisp_Object Qinvisible;
2545 Lisp_Object prop, end_charpos, limit, charpos;
2547 /* Get the value of the invisible text property at the
2548 current position. Value will be nil if there is no such
2549 property. */
2550 charpos = make_number (IT_STRING_CHARPOS (*it));
2551 prop = Fget_text_property (charpos, Qinvisible, it->string);
2553 if (!NILP (prop)
2554 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2556 handled = HANDLED_RECOMPUTE_PROPS;
2558 /* Get the position at which the next change of the
2559 invisible text property can be found in IT->string.
2560 Value will be nil if the property value is the same for
2561 all the rest of IT->string. */
2562 XSETINT (limit, XSTRING (it->string)->size);
2563 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2564 it->string, limit);
2566 /* Text at current position is invisible. The next
2567 change in the property is at position end_charpos.
2568 Move IT's current position to that position. */
2569 if (INTEGERP (end_charpos)
2570 && XFASTINT (end_charpos) < XFASTINT (limit))
2572 struct text_pos old;
2573 old = it->current.string_pos;
2574 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2575 compute_string_pos (&it->current.string_pos, old, it->string);
2577 else
2579 /* The rest of the string is invisible. If this is an
2580 overlay string, proceed with the next overlay string
2581 or whatever comes and return a character from there. */
2582 if (it->current.overlay_string_index >= 0)
2584 next_overlay_string (it);
2585 /* Don't check for overlay strings when we just
2586 finished processing them. */
2587 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2589 else
2591 struct Lisp_String *s = XSTRING (it->string);
2592 IT_STRING_CHARPOS (*it) = s->size;
2593 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2598 else
2600 int invis_p, newpos, next_stop, start_charpos;
2601 Lisp_Object pos, prop, overlay;
2603 /* First of all, is there invisible text at this position? */
2604 start_charpos = IT_CHARPOS (*it);
2605 pos = make_number (IT_CHARPOS (*it));
2606 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2607 &overlay);
2608 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2610 /* If we are on invisible text, skip over it. */
2611 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2613 /* Record whether we have to display an ellipsis for the
2614 invisible text. */
2615 int display_ellipsis_p = invis_p == 2;
2617 handled = HANDLED_RECOMPUTE_PROPS;
2619 /* Loop skipping over invisible text. The loop is left at
2620 ZV or with IT on the first char being visible again. */
2623 /* Try to skip some invisible text. Return value is the
2624 position reached which can be equal to IT's position
2625 if there is nothing invisible here. This skips both
2626 over invisible text properties and overlays with
2627 invisible property. */
2628 newpos = skip_invisible (IT_CHARPOS (*it),
2629 &next_stop, ZV, it->window);
2631 /* If we skipped nothing at all we weren't at invisible
2632 text in the first place. If everything to the end of
2633 the buffer was skipped, end the loop. */
2634 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2635 invis_p = 0;
2636 else
2638 /* We skipped some characters but not necessarily
2639 all there are. Check if we ended up on visible
2640 text. Fget_char_property returns the property of
2641 the char before the given position, i.e. if we
2642 get invis_p = 0, this means that the char at
2643 newpos is visible. */
2644 pos = make_number (newpos);
2645 prop = Fget_char_property (pos, Qinvisible, it->window);
2646 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2649 /* If we ended up on invisible text, proceed to
2650 skip starting with next_stop. */
2651 if (invis_p)
2652 IT_CHARPOS (*it) = next_stop;
2654 while (invis_p);
2656 /* The position newpos is now either ZV or on visible text. */
2657 IT_CHARPOS (*it) = newpos;
2658 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2660 /* If there are before-strings at the start of invisible
2661 text, and the text is invisible because of a text
2662 property, arrange to show before-strings because 20.x did
2663 it that way. (If the text is invisible because of an
2664 overlay property instead of a text property, this is
2665 already handled in the overlay code.) */
2666 if (NILP (overlay)
2667 && get_overlay_strings (it, start_charpos))
2669 handled = HANDLED_RECOMPUTE_PROPS;
2670 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2672 else if (display_ellipsis_p)
2673 setup_for_ellipsis (it);
2677 return handled;
2681 /* Make iterator IT return `...' next. */
2683 static void
2684 setup_for_ellipsis (it)
2685 struct it *it;
2687 if (it->dp
2688 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2690 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2691 it->dpvec = v->contents;
2692 it->dpend = v->contents + v->size;
2694 else
2696 /* Default `...'. */
2697 it->dpvec = default_invis_vector;
2698 it->dpend = default_invis_vector + 3;
2701 /* The ellipsis display does not replace the display of the
2702 character at the new position. Indicate this by setting
2703 IT->dpvec_char_len to zero. */
2704 it->dpvec_char_len = 0;
2706 it->current.dpvec_index = 0;
2707 it->method = next_element_from_display_vector;
2712 /***********************************************************************
2713 'display' property
2714 ***********************************************************************/
2716 /* Set up iterator IT from `display' property at its current position.
2717 Called from handle_stop. */
2719 static enum prop_handled
2720 handle_display_prop (it)
2721 struct it *it;
2723 Lisp_Object prop, object;
2724 struct text_pos *position;
2725 int display_replaced_p = 0;
2727 if (STRINGP (it->string))
2729 object = it->string;
2730 position = &it->current.string_pos;
2732 else
2734 object = it->w->buffer;
2735 position = &it->current.pos;
2738 /* Reset those iterator values set from display property values. */
2739 it->font_height = Qnil;
2740 it->space_width = Qnil;
2741 it->voffset = 0;
2743 /* We don't support recursive `display' properties, i.e. string
2744 values that have a string `display' property, that have a string
2745 `display' property etc. */
2746 if (!it->string_from_display_prop_p)
2747 it->area = TEXT_AREA;
2749 prop = Fget_char_property (make_number (position->charpos),
2750 Qdisplay, object);
2751 if (NILP (prop))
2752 return HANDLED_NORMALLY;
2754 if (CONSP (prop)
2755 /* Simple properties. */
2756 && !EQ (XCAR (prop), Qimage)
2757 && !EQ (XCAR (prop), Qspace)
2758 && !EQ (XCAR (prop), Qwhen)
2759 && !EQ (XCAR (prop), Qspace_width)
2760 && !EQ (XCAR (prop), Qheight)
2761 && !EQ (XCAR (prop), Qraise)
2762 /* Marginal area specifications. */
2763 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2764 && !NILP (XCAR (prop)))
2766 for (; CONSP (prop); prop = XCDR (prop))
2768 if (handle_single_display_prop (it, XCAR (prop), object,
2769 position, display_replaced_p))
2770 display_replaced_p = 1;
2773 else if (VECTORP (prop))
2775 int i;
2776 for (i = 0; i < ASIZE (prop); ++i)
2777 if (handle_single_display_prop (it, AREF (prop, i), object,
2778 position, display_replaced_p))
2779 display_replaced_p = 1;
2781 else
2783 if (handle_single_display_prop (it, prop, object, position, 0))
2784 display_replaced_p = 1;
2787 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2791 /* Value is the position of the end of the `display' property starting
2792 at START_POS in OBJECT. */
2794 static struct text_pos
2795 display_prop_end (it, object, start_pos)
2796 struct it *it;
2797 Lisp_Object object;
2798 struct text_pos start_pos;
2800 Lisp_Object end;
2801 struct text_pos end_pos;
2803 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2804 Qdisplay, object, Qnil);
2805 CHARPOS (end_pos) = XFASTINT (end);
2806 if (STRINGP (object))
2807 compute_string_pos (&end_pos, start_pos, it->string);
2808 else
2809 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2811 return end_pos;
2815 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2816 is the object in which the `display' property was found. *POSITION
2817 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2818 means that we previously saw a display sub-property which already
2819 replaced text display with something else, for example an image;
2820 ignore such properties after the first one has been processed.
2822 If PROP is a `space' or `image' sub-property, set *POSITION to the
2823 end position of the `display' property.
2825 Value is non-zero if something was found which replaces the display
2826 of buffer or string text. */
2828 static int
2829 handle_single_display_prop (it, prop, object, position,
2830 display_replaced_before_p)
2831 struct it *it;
2832 Lisp_Object prop;
2833 Lisp_Object object;
2834 struct text_pos *position;
2835 int display_replaced_before_p;
2837 Lisp_Object value;
2838 int replaces_text_display_p = 0;
2839 Lisp_Object form;
2841 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2842 evaluated. If the result is nil, VALUE is ignored. */
2843 form = Qt;
2844 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2846 prop = XCDR (prop);
2847 if (!CONSP (prop))
2848 return 0;
2849 form = XCAR (prop);
2850 prop = XCDR (prop);
2853 if (!NILP (form) && !EQ (form, Qt))
2855 int count = BINDING_STACK_SIZE ();
2856 struct gcpro gcpro1;
2858 /* Bind `object' to the object having the `display' property, a
2859 buffer or string. Bind `position' to the position in the
2860 object where the property was found, and `buffer-position'
2861 to the current position in the buffer. */
2862 specbind (Qobject, object);
2863 specbind (Qposition, make_number (CHARPOS (*position)));
2864 specbind (Qbuffer_position,
2865 make_number (STRINGP (object)
2866 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2867 GCPRO1 (form);
2868 form = safe_eval (form);
2869 UNGCPRO;
2870 unbind_to (count, Qnil);
2873 if (NILP (form))
2874 return 0;
2876 if (CONSP (prop)
2877 && EQ (XCAR (prop), Qheight)
2878 && CONSP (XCDR (prop)))
2880 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2881 return 0;
2883 /* `(height HEIGHT)'. */
2884 it->font_height = XCAR (XCDR (prop));
2885 if (!NILP (it->font_height))
2887 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2888 int new_height = -1;
2890 if (CONSP (it->font_height)
2891 && (EQ (XCAR (it->font_height), Qplus)
2892 || EQ (XCAR (it->font_height), Qminus))
2893 && CONSP (XCDR (it->font_height))
2894 && INTEGERP (XCAR (XCDR (it->font_height))))
2896 /* `(+ N)' or `(- N)' where N is an integer. */
2897 int steps = XINT (XCAR (XCDR (it->font_height)));
2898 if (EQ (XCAR (it->font_height), Qplus))
2899 steps = - steps;
2900 it->face_id = smaller_face (it->f, it->face_id, steps);
2902 else if (FUNCTIONP (it->font_height))
2904 /* Call function with current height as argument.
2905 Value is the new height. */
2906 Lisp_Object height;
2907 height = safe_call1 (it->font_height,
2908 face->lface[LFACE_HEIGHT_INDEX]);
2909 if (NUMBERP (height))
2910 new_height = XFLOATINT (height);
2912 else if (NUMBERP (it->font_height))
2914 /* Value is a multiple of the canonical char height. */
2915 struct face *face;
2917 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2918 new_height = (XFLOATINT (it->font_height)
2919 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2921 else
2923 /* Evaluate IT->font_height with `height' bound to the
2924 current specified height to get the new height. */
2925 Lisp_Object value;
2926 int count = BINDING_STACK_SIZE ();
2928 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2929 value = safe_eval (it->font_height);
2930 unbind_to (count, Qnil);
2932 if (NUMBERP (value))
2933 new_height = XFLOATINT (value);
2936 if (new_height > 0)
2937 it->face_id = face_with_height (it->f, it->face_id, new_height);
2940 else if (CONSP (prop)
2941 && EQ (XCAR (prop), Qspace_width)
2942 && CONSP (XCDR (prop)))
2944 /* `(space_width WIDTH)'. */
2945 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2946 return 0;
2948 value = XCAR (XCDR (prop));
2949 if (NUMBERP (value) && XFLOATINT (value) > 0)
2950 it->space_width = value;
2952 else if (CONSP (prop)
2953 && EQ (XCAR (prop), Qraise)
2954 && CONSP (XCDR (prop)))
2956 /* `(raise FACTOR)'. */
2957 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2958 return 0;
2960 #ifdef HAVE_WINDOW_SYSTEM
2961 value = XCAR (XCDR (prop));
2962 if (NUMBERP (value))
2964 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2965 it->voffset = - (XFLOATINT (value)
2966 * (FONT_HEIGHT (face->font)));
2968 #endif /* HAVE_WINDOW_SYSTEM */
2970 else if (!it->string_from_display_prop_p)
2972 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2973 VALUE) or `((margin nil) VALUE)' or VALUE. */
2974 Lisp_Object location, value;
2975 struct text_pos start_pos;
2976 int valid_p;
2978 /* Characters having this form of property are not displayed, so
2979 we have to find the end of the property. */
2980 start_pos = *position;
2981 *position = display_prop_end (it, object, start_pos);
2982 value = Qnil;
2984 /* Let's stop at the new position and assume that all
2985 text properties change there. */
2986 it->stop_charpos = position->charpos;
2988 location = Qunbound;
2989 if (CONSP (prop) && CONSP (XCAR (prop)))
2991 Lisp_Object tem;
2993 value = XCDR (prop);
2994 if (CONSP (value))
2995 value = XCAR (value);
2997 tem = XCAR (prop);
2998 if (EQ (XCAR (tem), Qmargin)
2999 && (tem = XCDR (tem),
3000 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3001 (NILP (tem)
3002 || EQ (tem, Qleft_margin)
3003 || EQ (tem, Qright_margin))))
3004 location = tem;
3007 if (EQ (location, Qunbound))
3009 location = Qnil;
3010 value = prop;
3013 #ifdef HAVE_WINDOW_SYSTEM
3014 if (FRAME_TERMCAP_P (it->f))
3015 valid_p = STRINGP (value);
3016 else
3017 valid_p = (STRINGP (value)
3018 || (CONSP (value) && EQ (XCAR (value), Qspace))
3019 || valid_image_p (value));
3020 #else /* not HAVE_WINDOW_SYSTEM */
3021 valid_p = STRINGP (value);
3022 #endif /* not HAVE_WINDOW_SYSTEM */
3024 if ((EQ (location, Qleft_margin)
3025 || EQ (location, Qright_margin)
3026 || NILP (location))
3027 && valid_p
3028 && !display_replaced_before_p)
3030 replaces_text_display_p = 1;
3032 /* Save current settings of IT so that we can restore them
3033 when we are finished with the glyph property value. */
3034 push_it (it);
3036 if (NILP (location))
3037 it->area = TEXT_AREA;
3038 else if (EQ (location, Qleft_margin))
3039 it->area = LEFT_MARGIN_AREA;
3040 else
3041 it->area = RIGHT_MARGIN_AREA;
3043 if (STRINGP (value))
3045 it->string = value;
3046 it->multibyte_p = STRING_MULTIBYTE (it->string);
3047 it->current.overlay_string_index = -1;
3048 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3049 it->end_charpos = it->string_nchars
3050 = XSTRING (it->string)->size;
3051 it->method = next_element_from_string;
3052 it->stop_charpos = 0;
3053 it->string_from_display_prop_p = 1;
3054 /* Say that we haven't consumed the characters with
3055 `display' property yet. The call to pop_it in
3056 set_iterator_to_next will clean this up. */
3057 *position = start_pos;
3059 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3061 it->method = next_element_from_stretch;
3062 it->object = value;
3063 it->current.pos = it->position = start_pos;
3065 #ifdef HAVE_WINDOW_SYSTEM
3066 else
3068 it->what = IT_IMAGE;
3069 it->image_id = lookup_image (it->f, value);
3070 it->position = start_pos;
3071 it->object = NILP (object) ? it->w->buffer : object;
3072 it->method = next_element_from_image;
3074 /* Say that we haven't consumed the characters with
3075 `display' property yet. The call to pop_it in
3076 set_iterator_to_next will clean this up. */
3077 *position = start_pos;
3079 #endif /* HAVE_WINDOW_SYSTEM */
3081 else
3082 /* Invalid property or property not supported. Restore
3083 the position to what it was before. */
3084 *position = start_pos;
3087 return replaces_text_display_p;
3091 /* Check if PROP is a display sub-property value whose text should be
3092 treated as intangible. */
3094 static int
3095 single_display_prop_intangible_p (prop)
3096 Lisp_Object prop;
3098 /* Skip over `when FORM'. */
3099 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3101 prop = XCDR (prop);
3102 if (!CONSP (prop))
3103 return 0;
3104 prop = XCDR (prop);
3107 if (!CONSP (prop))
3108 return 0;
3110 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3111 we don't need to treat text as intangible. */
3112 if (EQ (XCAR (prop), Qmargin))
3114 prop = XCDR (prop);
3115 if (!CONSP (prop))
3116 return 0;
3118 prop = XCDR (prop);
3119 if (!CONSP (prop)
3120 || EQ (XCAR (prop), Qleft_margin)
3121 || EQ (XCAR (prop), Qright_margin))
3122 return 0;
3125 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3129 /* Check if PROP is a display property value whose text should be
3130 treated as intangible. */
3133 display_prop_intangible_p (prop)
3134 Lisp_Object prop;
3136 if (CONSP (prop)
3137 && CONSP (XCAR (prop))
3138 && !EQ (Qmargin, XCAR (XCAR (prop))))
3140 /* A list of sub-properties. */
3141 while (CONSP (prop))
3143 if (single_display_prop_intangible_p (XCAR (prop)))
3144 return 1;
3145 prop = XCDR (prop);
3148 else if (VECTORP (prop))
3150 /* A vector of sub-properties. */
3151 int i;
3152 for (i = 0; i < ASIZE (prop); ++i)
3153 if (single_display_prop_intangible_p (AREF (prop, i)))
3154 return 1;
3156 else
3157 return single_display_prop_intangible_p (prop);
3159 return 0;
3163 /* Return 1 if PROP is a display sub-property value containing STRING. */
3165 static int
3166 single_display_prop_string_p (prop, string)
3167 Lisp_Object prop, string;
3169 if (EQ (string, prop))
3170 return 1;
3172 /* Skip over `when FORM'. */
3173 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3175 prop = XCDR (prop);
3176 if (!CONSP (prop))
3177 return 0;
3178 prop = XCDR (prop);
3181 if (CONSP (prop))
3182 /* Skip over `margin LOCATION'. */
3183 if (EQ (XCAR (prop), Qmargin))
3185 prop = XCDR (prop);
3186 if (!CONSP (prop))
3187 return 0;
3189 prop = XCDR (prop);
3190 if (!CONSP (prop))
3191 return 0;
3194 return CONSP (prop) && EQ (XCAR (prop), string);
3198 /* Return 1 if STRING appears in the `display' property PROP. */
3200 static int
3201 display_prop_string_p (prop, string)
3202 Lisp_Object prop, string;
3204 if (CONSP (prop)
3205 && CONSP (XCAR (prop))
3206 && !EQ (Qmargin, XCAR (XCAR (prop))))
3208 /* A list of sub-properties. */
3209 while (CONSP (prop))
3211 if (single_display_prop_string_p (XCAR (prop), string))
3212 return 1;
3213 prop = XCDR (prop);
3216 else if (VECTORP (prop))
3218 /* A vector of sub-properties. */
3219 int i;
3220 for (i = 0; i < ASIZE (prop); ++i)
3221 if (single_display_prop_string_p (AREF (prop, i), string))
3222 return 1;
3224 else
3225 return single_display_prop_string_p (prop, string);
3227 return 0;
3231 /* Determine from which buffer position in W's buffer STRING comes
3232 from. AROUND_CHARPOS is an approximate position where it could
3233 be from. Value is the buffer position or 0 if it couldn't be
3234 determined.
3236 W's buffer must be current.
3238 This function is necessary because we don't record buffer positions
3239 in glyphs generated from strings (to keep struct glyph small).
3240 This function may only use code that doesn't eval because it is
3241 called asynchronously from note_mouse_highlight. */
3244 string_buffer_position (w, string, around_charpos)
3245 struct window *w;
3246 Lisp_Object string;
3247 int around_charpos;
3249 Lisp_Object limit, prop, pos;
3250 const int MAX_DISTANCE = 1000;
3251 int found = 0;
3253 pos = make_number (around_charpos);
3254 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3255 while (!found && !EQ (pos, limit))
3257 prop = Fget_char_property (pos, Qdisplay, Qnil);
3258 if (!NILP (prop) && display_prop_string_p (prop, string))
3259 found = 1;
3260 else
3261 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3264 if (!found)
3266 pos = make_number (around_charpos);
3267 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3268 while (!found && !EQ (pos, limit))
3270 prop = Fget_char_property (pos, Qdisplay, Qnil);
3271 if (!NILP (prop) && display_prop_string_p (prop, string))
3272 found = 1;
3273 else
3274 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3275 limit);
3279 return found ? XINT (pos) : 0;
3284 /***********************************************************************
3285 `composition' property
3286 ***********************************************************************/
3288 /* Set up iterator IT from `composition' property at its current
3289 position. Called from handle_stop. */
3291 static enum prop_handled
3292 handle_composition_prop (it)
3293 struct it *it;
3295 Lisp_Object prop, string;
3296 int pos, pos_byte, end;
3297 enum prop_handled handled = HANDLED_NORMALLY;
3299 if (STRINGP (it->string))
3301 pos = IT_STRING_CHARPOS (*it);
3302 pos_byte = IT_STRING_BYTEPOS (*it);
3303 string = it->string;
3305 else
3307 pos = IT_CHARPOS (*it);
3308 pos_byte = IT_BYTEPOS (*it);
3309 string = Qnil;
3312 /* If there's a valid composition and point is not inside of the
3313 composition (in the case that the composition is from the current
3314 buffer), draw a glyph composed from the composition components. */
3315 if (find_composition (pos, -1, &pos, &end, &prop, string)
3316 && COMPOSITION_VALID_P (pos, end, prop)
3317 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3319 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3321 if (id >= 0)
3323 it->method = next_element_from_composition;
3324 it->cmp_id = id;
3325 it->cmp_len = COMPOSITION_LENGTH (prop);
3326 /* For a terminal, draw only the first character of the
3327 components. */
3328 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3329 it->len = (STRINGP (it->string)
3330 ? string_char_to_byte (it->string, end)
3331 : CHAR_TO_BYTE (end)) - pos_byte;
3332 it->stop_charpos = end;
3333 handled = HANDLED_RETURN;
3337 return handled;
3342 /***********************************************************************
3343 Overlay strings
3344 ***********************************************************************/
3346 /* The following structure is used to record overlay strings for
3347 later sorting in load_overlay_strings. */
3349 struct overlay_entry
3351 Lisp_Object overlay;
3352 Lisp_Object string;
3353 int priority;
3354 int after_string_p;
3358 /* Set up iterator IT from overlay strings at its current position.
3359 Called from handle_stop. */
3361 static enum prop_handled
3362 handle_overlay_change (it)
3363 struct it *it;
3365 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3366 return HANDLED_RECOMPUTE_PROPS;
3367 else
3368 return HANDLED_NORMALLY;
3372 /* Set up the next overlay string for delivery by IT, if there is an
3373 overlay string to deliver. Called by set_iterator_to_next when the
3374 end of the current overlay string is reached. If there are more
3375 overlay strings to display, IT->string and
3376 IT->current.overlay_string_index are set appropriately here.
3377 Otherwise IT->string is set to nil. */
3379 static void
3380 next_overlay_string (it)
3381 struct it *it;
3383 ++it->current.overlay_string_index;
3384 if (it->current.overlay_string_index == it->n_overlay_strings)
3386 /* No more overlay strings. Restore IT's settings to what
3387 they were before overlay strings were processed, and
3388 continue to deliver from current_buffer. */
3389 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3391 pop_it (it);
3392 xassert (it->stop_charpos >= BEGV
3393 && it->stop_charpos <= it->end_charpos);
3394 it->string = Qnil;
3395 it->current.overlay_string_index = -1;
3396 SET_TEXT_POS (it->current.string_pos, -1, -1);
3397 it->n_overlay_strings = 0;
3398 it->method = next_element_from_buffer;
3400 /* If we're at the end of the buffer, record that we have
3401 processed the overlay strings there already, so that
3402 next_element_from_buffer doesn't try it again. */
3403 if (IT_CHARPOS (*it) >= it->end_charpos)
3404 it->overlay_strings_at_end_processed_p = 1;
3406 /* If we have to display `...' for invisible text, set
3407 the iterator up for that. */
3408 if (display_ellipsis_p)
3409 setup_for_ellipsis (it);
3411 else
3413 /* There are more overlay strings to process. If
3414 IT->current.overlay_string_index has advanced to a position
3415 where we must load IT->overlay_strings with more strings, do
3416 it. */
3417 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3419 if (it->current.overlay_string_index && i == 0)
3420 load_overlay_strings (it, 0);
3422 /* Initialize IT to deliver display elements from the overlay
3423 string. */
3424 it->string = it->overlay_strings[i];
3425 it->multibyte_p = STRING_MULTIBYTE (it->string);
3426 SET_TEXT_POS (it->current.string_pos, 0, 0);
3427 it->method = next_element_from_string;
3428 it->stop_charpos = 0;
3431 CHECK_IT (it);
3435 /* Compare two overlay_entry structures E1 and E2. Used as a
3436 comparison function for qsort in load_overlay_strings. Overlay
3437 strings for the same position are sorted so that
3439 1. All after-strings come in front of before-strings, except
3440 when they come from the same overlay.
3442 2. Within after-strings, strings are sorted so that overlay strings
3443 from overlays with higher priorities come first.
3445 2. Within before-strings, strings are sorted so that overlay
3446 strings from overlays with higher priorities come last.
3448 Value is analogous to strcmp. */
3451 static int
3452 compare_overlay_entries (e1, e2)
3453 void *e1, *e2;
3455 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3456 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3457 int result;
3459 if (entry1->after_string_p != entry2->after_string_p)
3461 /* Let after-strings appear in front of before-strings if
3462 they come from different overlays. */
3463 if (EQ (entry1->overlay, entry2->overlay))
3464 result = entry1->after_string_p ? 1 : -1;
3465 else
3466 result = entry1->after_string_p ? -1 : 1;
3468 else if (entry1->after_string_p)
3469 /* After-strings sorted in order of decreasing priority. */
3470 result = entry2->priority - entry1->priority;
3471 else
3472 /* Before-strings sorted in order of increasing priority. */
3473 result = entry1->priority - entry2->priority;
3475 return result;
3479 /* Load the vector IT->overlay_strings with overlay strings from IT's
3480 current buffer position, or from CHARPOS if that is > 0. Set
3481 IT->n_overlays to the total number of overlay strings found.
3483 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3484 a time. On entry into load_overlay_strings,
3485 IT->current.overlay_string_index gives the number of overlay
3486 strings that have already been loaded by previous calls to this
3487 function.
3489 IT->add_overlay_start contains an additional overlay start
3490 position to consider for taking overlay strings from, if non-zero.
3491 This position comes into play when the overlay has an `invisible'
3492 property, and both before and after-strings. When we've skipped to
3493 the end of the overlay, because of its `invisible' property, we
3494 nevertheless want its before-string to appear.
3495 IT->add_overlay_start will contain the overlay start position
3496 in this case.
3498 Overlay strings are sorted so that after-string strings come in
3499 front of before-string strings. Within before and after-strings,
3500 strings are sorted by overlay priority. See also function
3501 compare_overlay_entries. */
3503 static void
3504 load_overlay_strings (it, charpos)
3505 struct it *it;
3506 int charpos;
3508 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3509 Lisp_Object ov, overlay, window, str, invisible;
3510 int start, end;
3511 int size = 20;
3512 int n = 0, i, j, invis_p;
3513 struct overlay_entry *entries
3514 = (struct overlay_entry *) alloca (size * sizeof *entries);
3516 if (charpos <= 0)
3517 charpos = IT_CHARPOS (*it);
3519 /* Append the overlay string STRING of overlay OVERLAY to vector
3520 `entries' which has size `size' and currently contains `n'
3521 elements. AFTER_P non-zero means STRING is an after-string of
3522 OVERLAY. */
3523 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3524 do \
3526 Lisp_Object priority; \
3528 if (n == size) \
3530 int new_size = 2 * size; \
3531 struct overlay_entry *old = entries; \
3532 entries = \
3533 (struct overlay_entry *) alloca (new_size \
3534 * sizeof *entries); \
3535 bcopy (old, entries, size * sizeof *entries); \
3536 size = new_size; \
3539 entries[n].string = (STRING); \
3540 entries[n].overlay = (OVERLAY); \
3541 priority = Foverlay_get ((OVERLAY), Qpriority); \
3542 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3543 entries[n].after_string_p = (AFTER_P); \
3544 ++n; \
3546 while (0)
3548 /* Process overlay before the overlay center. */
3549 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3551 overlay = XCAR (ov);
3552 xassert (OVERLAYP (overlay));
3553 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3554 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3556 if (end < charpos)
3557 break;
3559 /* Skip this overlay if it doesn't start or end at IT's current
3560 position. */
3561 if (end != charpos && start != charpos)
3562 continue;
3564 /* Skip this overlay if it doesn't apply to IT->w. */
3565 window = Foverlay_get (overlay, Qwindow);
3566 if (WINDOWP (window) && XWINDOW (window) != it->w)
3567 continue;
3569 /* If the text ``under'' the overlay is invisible, both before-
3570 and after-strings from this overlay are visible; start and
3571 end position are indistinguishable. */
3572 invisible = Foverlay_get (overlay, Qinvisible);
3573 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3575 /* If overlay has a non-empty before-string, record it. */
3576 if ((start == charpos || (end == charpos && invis_p))
3577 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3578 && XSTRING (str)->size)
3579 RECORD_OVERLAY_STRING (overlay, str, 0);
3581 /* If overlay has a non-empty after-string, record it. */
3582 if ((end == charpos || (start == charpos && invis_p))
3583 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3584 && XSTRING (str)->size)
3585 RECORD_OVERLAY_STRING (overlay, str, 1);
3588 /* Process overlays after the overlay center. */
3589 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3591 overlay = XCAR (ov);
3592 xassert (OVERLAYP (overlay));
3593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3596 if (start > charpos)
3597 break;
3599 /* Skip this overlay if it doesn't start or end at IT's current
3600 position. */
3601 if (end != charpos && start != charpos)
3602 continue;
3604 /* Skip this overlay if it doesn't apply to IT->w. */
3605 window = Foverlay_get (overlay, Qwindow);
3606 if (WINDOWP (window) && XWINDOW (window) != it->w)
3607 continue;
3609 /* If the text ``under'' the overlay is invisible, it has a zero
3610 dimension, and both before- and after-strings apply. */
3611 invisible = Foverlay_get (overlay, Qinvisible);
3612 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3614 /* If overlay has a non-empty before-string, record it. */
3615 if ((start == charpos || (end == charpos && invis_p))
3616 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3617 && XSTRING (str)->size)
3618 RECORD_OVERLAY_STRING (overlay, str, 0);
3620 /* If overlay has a non-empty after-string, record it. */
3621 if ((end == charpos || (start == charpos && invis_p))
3622 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3623 && XSTRING (str)->size)
3624 RECORD_OVERLAY_STRING (overlay, str, 1);
3627 #undef RECORD_OVERLAY_STRING
3629 /* Sort entries. */
3630 if (n > 1)
3631 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3633 /* Record the total number of strings to process. */
3634 it->n_overlay_strings = n;
3636 /* IT->current.overlay_string_index is the number of overlay strings
3637 that have already been consumed by IT. Copy some of the
3638 remaining overlay strings to IT->overlay_strings. */
3639 i = 0;
3640 j = it->current.overlay_string_index;
3641 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3642 it->overlay_strings[i++] = entries[j++].string;
3644 CHECK_IT (it);
3648 /* Get the first chunk of overlay strings at IT's current buffer
3649 position, or at CHARPOS if that is > 0. Value is non-zero if at
3650 least one overlay string was found. */
3652 static int
3653 get_overlay_strings (it, charpos)
3654 struct it *it;
3655 int charpos;
3657 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3658 process. This fills IT->overlay_strings with strings, and sets
3659 IT->n_overlay_strings to the total number of strings to process.
3660 IT->pos.overlay_string_index has to be set temporarily to zero
3661 because load_overlay_strings needs this; it must be set to -1
3662 when no overlay strings are found because a zero value would
3663 indicate a position in the first overlay string. */
3664 it->current.overlay_string_index = 0;
3665 load_overlay_strings (it, charpos);
3667 /* If we found overlay strings, set up IT to deliver display
3668 elements from the first one. Otherwise set up IT to deliver
3669 from current_buffer. */
3670 if (it->n_overlay_strings)
3672 /* Make sure we know settings in current_buffer, so that we can
3673 restore meaningful values when we're done with the overlay
3674 strings. */
3675 compute_stop_pos (it);
3676 xassert (it->face_id >= 0);
3678 /* Save IT's settings. They are restored after all overlay
3679 strings have been processed. */
3680 xassert (it->sp == 0);
3681 push_it (it);
3683 /* Set up IT to deliver display elements from the first overlay
3684 string. */
3685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3686 it->string = it->overlay_strings[0];
3687 it->stop_charpos = 0;
3688 it->end_charpos = XSTRING (it->string)->size;
3689 it->multibyte_p = STRING_MULTIBYTE (it->string);
3690 xassert (STRINGP (it->string));
3691 it->method = next_element_from_string;
3693 else
3695 it->string = Qnil;
3696 it->current.overlay_string_index = -1;
3697 it->method = next_element_from_buffer;
3700 CHECK_IT (it);
3702 /* Value is non-zero if we found at least one overlay string. */
3703 return STRINGP (it->string);
3708 /***********************************************************************
3709 Saving and restoring state
3710 ***********************************************************************/
3712 /* Save current settings of IT on IT->stack. Called, for example,
3713 before setting up IT for an overlay string, to be able to restore
3714 IT's settings to what they were after the overlay string has been
3715 processed. */
3717 static void
3718 push_it (it)
3719 struct it *it;
3721 struct iterator_stack_entry *p;
3723 xassert (it->sp < 2);
3724 p = it->stack + it->sp;
3726 p->stop_charpos = it->stop_charpos;
3727 xassert (it->face_id >= 0);
3728 p->face_id = it->face_id;
3729 p->string = it->string;
3730 p->pos = it->current;
3731 p->end_charpos = it->end_charpos;
3732 p->string_nchars = it->string_nchars;
3733 p->area = it->area;
3734 p->multibyte_p = it->multibyte_p;
3735 p->space_width = it->space_width;
3736 p->font_height = it->font_height;
3737 p->voffset = it->voffset;
3738 p->string_from_display_prop_p = it->string_from_display_prop_p;
3739 p->display_ellipsis_p = 0;
3740 ++it->sp;
3744 /* Restore IT's settings from IT->stack. Called, for example, when no
3745 more overlay strings must be processed, and we return to delivering
3746 display elements from a buffer, or when the end of a string from a
3747 `display' property is reached and we return to delivering display
3748 elements from an overlay string, or from a buffer. */
3750 static void
3751 pop_it (it)
3752 struct it *it;
3754 struct iterator_stack_entry *p;
3756 xassert (it->sp > 0);
3757 --it->sp;
3758 p = it->stack + it->sp;
3759 it->stop_charpos = p->stop_charpos;
3760 it->face_id = p->face_id;
3761 it->string = p->string;
3762 it->current = p->pos;
3763 it->end_charpos = p->end_charpos;
3764 it->string_nchars = p->string_nchars;
3765 it->area = p->area;
3766 it->multibyte_p = p->multibyte_p;
3767 it->space_width = p->space_width;
3768 it->font_height = p->font_height;
3769 it->voffset = p->voffset;
3770 it->string_from_display_prop_p = p->string_from_display_prop_p;
3775 /***********************************************************************
3776 Moving over lines
3777 ***********************************************************************/
3779 /* Set IT's current position to the previous line start. */
3781 static void
3782 back_to_previous_line_start (it)
3783 struct it *it;
3785 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3786 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3790 /* Move IT to the next line start.
3792 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3793 we skipped over part of the text (as opposed to moving the iterator
3794 continuously over the text). Otherwise, don't change the value
3795 of *SKIPPED_P.
3797 Newlines may come from buffer text, overlay strings, or strings
3798 displayed via the `display' property. That's the reason we can't
3799 simply use find_next_newline_no_quit.
3801 Note that this function may not skip over invisible text that is so
3802 because of text properties and immediately follows a newline. If
3803 it would, function reseat_at_next_visible_line_start, when called
3804 from set_iterator_to_next, would effectively make invisible
3805 characters following a newline part of the wrong glyph row, which
3806 leads to wrong cursor motion. */
3808 static int
3809 forward_to_next_line_start (it, skipped_p)
3810 struct it *it;
3811 int *skipped_p;
3813 int old_selective, newline_found_p, n;
3814 const int MAX_NEWLINE_DISTANCE = 500;
3816 /* If already on a newline, just consume it to avoid unintended
3817 skipping over invisible text below. */
3818 if (it->what == IT_CHARACTER
3819 && it->c == '\n'
3820 && CHARPOS (it->position) == IT_CHARPOS (*it))
3822 set_iterator_to_next (it, 0);
3823 it->c = 0;
3824 return 1;
3827 /* Don't handle selective display in the following. It's (a)
3828 unnecessary because it's done by the caller, and (b) leads to an
3829 infinite recursion because next_element_from_ellipsis indirectly
3830 calls this function. */
3831 old_selective = it->selective;
3832 it->selective = 0;
3834 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3835 from buffer text. */
3836 for (n = newline_found_p = 0;
3837 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3838 n += STRINGP (it->string) ? 0 : 1)
3840 if (!get_next_display_element (it))
3841 break;
3842 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3843 set_iterator_to_next (it, 0);
3846 /* If we didn't find a newline near enough, see if we can use a
3847 short-cut. */
3848 if (n == MAX_NEWLINE_DISTANCE)
3850 int start = IT_CHARPOS (*it);
3851 int limit = find_next_newline_no_quit (start, 1);
3852 Lisp_Object pos;
3854 xassert (!STRINGP (it->string));
3856 /* If there isn't any `display' property in sight, and no
3857 overlays, we can just use the position of the newline in
3858 buffer text. */
3859 if (it->stop_charpos >= limit
3860 || ((pos = Fnext_single_property_change (make_number (start),
3861 Qdisplay,
3862 Qnil, make_number (limit)),
3863 NILP (pos))
3864 && next_overlay_change (start) == ZV))
3866 IT_CHARPOS (*it) = limit;
3867 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3868 *skipped_p = newline_found_p = 1;
3870 else
3872 while (get_next_display_element (it)
3873 && !newline_found_p)
3875 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3876 set_iterator_to_next (it, 0);
3881 it->selective = old_selective;
3882 return newline_found_p;
3886 /* Set IT's current position to the previous visible line start. Skip
3887 invisible text that is so either due to text properties or due to
3888 selective display. Caution: this does not change IT->current_x and
3889 IT->hpos. */
3891 static void
3892 back_to_previous_visible_line_start (it)
3893 struct it *it;
3895 int visible_p = 0;
3897 /* Go back one newline if not on BEGV already. */
3898 if (IT_CHARPOS (*it) > BEGV)
3899 back_to_previous_line_start (it);
3901 /* Move over lines that are invisible because of selective display
3902 or text properties. */
3903 while (IT_CHARPOS (*it) > BEGV
3904 && !visible_p)
3906 visible_p = 1;
3908 /* If selective > 0, then lines indented more than that values
3909 are invisible. */
3910 if (it->selective > 0
3911 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3912 it->selective))
3913 visible_p = 0;
3914 else
3916 Lisp_Object prop;
3918 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3919 Qinvisible, it->window);
3920 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3921 visible_p = 0;
3924 /* Back one more newline if the current one is invisible. */
3925 if (!visible_p)
3926 back_to_previous_line_start (it);
3929 xassert (IT_CHARPOS (*it) >= BEGV);
3930 xassert (IT_CHARPOS (*it) == BEGV
3931 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3932 CHECK_IT (it);
3936 /* Reseat iterator IT at the previous visible line start. Skip
3937 invisible text that is so either due to text properties or due to
3938 selective display. At the end, update IT's overlay information,
3939 face information etc. */
3941 static void
3942 reseat_at_previous_visible_line_start (it)
3943 struct it *it;
3945 back_to_previous_visible_line_start (it);
3946 reseat (it, it->current.pos, 1);
3947 CHECK_IT (it);
3951 /* Reseat iterator IT on the next visible line start in the current
3952 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3953 preceding the line start. Skip over invisible text that is so
3954 because of selective display. Compute faces, overlays etc at the
3955 new position. Note that this function does not skip over text that
3956 is invisible because of text properties. */
3958 static void
3959 reseat_at_next_visible_line_start (it, on_newline_p)
3960 struct it *it;
3961 int on_newline_p;
3963 int newline_found_p, skipped_p = 0;
3965 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3967 /* Skip over lines that are invisible because they are indented
3968 more than the value of IT->selective. */
3969 if (it->selective > 0)
3970 while (IT_CHARPOS (*it) < ZV
3971 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3972 it->selective))
3974 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3975 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3978 /* Position on the newline if that's what's requested. */
3979 if (on_newline_p && newline_found_p)
3981 if (STRINGP (it->string))
3983 if (IT_STRING_CHARPOS (*it) > 0)
3985 --IT_STRING_CHARPOS (*it);
3986 --IT_STRING_BYTEPOS (*it);
3989 else if (IT_CHARPOS (*it) > BEGV)
3991 --IT_CHARPOS (*it);
3992 --IT_BYTEPOS (*it);
3993 reseat (it, it->current.pos, 0);
3996 else if (skipped_p)
3997 reseat (it, it->current.pos, 0);
3999 CHECK_IT (it);
4004 /***********************************************************************
4005 Changing an iterator's position
4006 ***********************************************************************/
4008 /* Change IT's current position to POS in current_buffer. If FORCE_P
4009 is non-zero, always check for text properties at the new position.
4010 Otherwise, text properties are only looked up if POS >=
4011 IT->check_charpos of a property. */
4013 static void
4014 reseat (it, pos, force_p)
4015 struct it *it;
4016 struct text_pos pos;
4017 int force_p;
4019 int original_pos = IT_CHARPOS (*it);
4021 reseat_1 (it, pos, 0);
4023 /* Determine where to check text properties. Avoid doing it
4024 where possible because text property lookup is very expensive. */
4025 if (force_p
4026 || CHARPOS (pos) > it->stop_charpos
4027 || CHARPOS (pos) < original_pos)
4028 handle_stop (it);
4030 CHECK_IT (it);
4034 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4035 IT->stop_pos to POS, also. */
4037 static void
4038 reseat_1 (it, pos, set_stop_p)
4039 struct it *it;
4040 struct text_pos pos;
4041 int set_stop_p;
4043 /* Don't call this function when scanning a C string. */
4044 xassert (it->s == NULL);
4046 /* POS must be a reasonable value. */
4047 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4049 it->current.pos = it->position = pos;
4050 XSETBUFFER (it->object, current_buffer);
4051 it->end_charpos = ZV;
4052 it->dpvec = NULL;
4053 it->current.dpvec_index = -1;
4054 it->current.overlay_string_index = -1;
4055 IT_STRING_CHARPOS (*it) = -1;
4056 IT_STRING_BYTEPOS (*it) = -1;
4057 it->string = Qnil;
4058 it->method = next_element_from_buffer;
4059 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4060 it->sp = 0;
4061 it->face_before_selective_p = 0;
4063 if (set_stop_p)
4064 it->stop_charpos = CHARPOS (pos);
4068 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4069 If S is non-null, it is a C string to iterate over. Otherwise,
4070 STRING gives a Lisp string to iterate over.
4072 If PRECISION > 0, don't return more then PRECISION number of
4073 characters from the string.
4075 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4076 characters have been returned. FIELD_WIDTH < 0 means an infinite
4077 field width.
4079 MULTIBYTE = 0 means disable processing of multibyte characters,
4080 MULTIBYTE > 0 means enable it,
4081 MULTIBYTE < 0 means use IT->multibyte_p.
4083 IT must be initialized via a prior call to init_iterator before
4084 calling this function. */
4086 static void
4087 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4088 struct it *it;
4089 unsigned char *s;
4090 Lisp_Object string;
4091 int charpos;
4092 int precision, field_width, multibyte;
4094 /* No region in strings. */
4095 it->region_beg_charpos = it->region_end_charpos = -1;
4097 /* No text property checks performed by default, but see below. */
4098 it->stop_charpos = -1;
4100 /* Set iterator position and end position. */
4101 bzero (&it->current, sizeof it->current);
4102 it->current.overlay_string_index = -1;
4103 it->current.dpvec_index = -1;
4104 xassert (charpos >= 0);
4106 /* If STRING is specified, use its multibyteness, otherwise use the
4107 setting of MULTIBYTE, if specified. */
4108 if (multibyte >= 0)
4109 it->multibyte_p = multibyte > 0;
4111 if (s == NULL)
4113 xassert (STRINGP (string));
4114 it->string = string;
4115 it->s = NULL;
4116 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4117 it->method = next_element_from_string;
4118 it->current.string_pos = string_pos (charpos, string);
4120 else
4122 it->s = s;
4123 it->string = Qnil;
4125 /* Note that we use IT->current.pos, not it->current.string_pos,
4126 for displaying C strings. */
4127 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4128 if (it->multibyte_p)
4130 it->current.pos = c_string_pos (charpos, s, 1);
4131 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4133 else
4135 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4136 it->end_charpos = it->string_nchars = strlen (s);
4139 it->method = next_element_from_c_string;
4142 /* PRECISION > 0 means don't return more than PRECISION characters
4143 from the string. */
4144 if (precision > 0 && it->end_charpos - charpos > precision)
4145 it->end_charpos = it->string_nchars = charpos + precision;
4147 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4148 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4149 FIELD_WIDTH < 0 means infinite field width. This is useful for
4150 padding with `-' at the end of a mode line. */
4151 if (field_width < 0)
4152 field_width = INFINITY;
4153 if (field_width > it->end_charpos - charpos)
4154 it->end_charpos = charpos + field_width;
4156 /* Use the standard display table for displaying strings. */
4157 if (DISP_TABLE_P (Vstandard_display_table))
4158 it->dp = XCHAR_TABLE (Vstandard_display_table);
4160 it->stop_charpos = charpos;
4161 CHECK_IT (it);
4166 /***********************************************************************
4167 Iteration
4168 ***********************************************************************/
4170 /* Load IT's display element fields with information about the next
4171 display element from the current position of IT. Value is zero if
4172 end of buffer (or C string) is reached. */
4175 get_next_display_element (it)
4176 struct it *it;
4178 /* Non-zero means that we found an display element. Zero means that
4179 we hit the end of what we iterate over. Performance note: the
4180 function pointer `method' used here turns out to be faster than
4181 using a sequence of if-statements. */
4182 int success_p = (*it->method) (it);
4184 if (it->what == IT_CHARACTER)
4186 /* Map via display table or translate control characters.
4187 IT->c, IT->len etc. have been set to the next character by
4188 the function call above. If we have a display table, and it
4189 contains an entry for IT->c, translate it. Don't do this if
4190 IT->c itself comes from a display table, otherwise we could
4191 end up in an infinite recursion. (An alternative could be to
4192 count the recursion depth of this function and signal an
4193 error when a certain maximum depth is reached.) Is it worth
4194 it? */
4195 if (success_p && it->dpvec == NULL)
4197 Lisp_Object dv;
4199 if (it->dp
4200 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4201 VECTORP (dv)))
4203 struct Lisp_Vector *v = XVECTOR (dv);
4205 /* Return the first character from the display table
4206 entry, if not empty. If empty, don't display the
4207 current character. */
4208 if (v->size)
4210 it->dpvec_char_len = it->len;
4211 it->dpvec = v->contents;
4212 it->dpend = v->contents + v->size;
4213 it->current.dpvec_index = 0;
4214 it->method = next_element_from_display_vector;
4215 success_p = get_next_display_element (it);
4217 else
4219 set_iterator_to_next (it, 0);
4220 success_p = get_next_display_element (it);
4224 /* Translate control characters into `\003' or `^C' form.
4225 Control characters coming from a display table entry are
4226 currently not translated because we use IT->dpvec to hold
4227 the translation. This could easily be changed but I
4228 don't believe that it is worth doing.
4230 Non-printable multibyte characters are also translated
4231 octal form. */
4232 else if ((it->c < ' '
4233 && (it->area != TEXT_AREA
4234 || (it->c != '\n' && it->c != '\t')))
4235 || (it->multibyte_p ? CHAR_BYTE8_P (it->c) : it->c >= 127)
4236 || (it->c != '\n' && it->c != '\t'
4237 && !CHAR_PRINTABLE_P (it->c)))
4239 /* IT->c is a control character which must be displayed
4240 either as '\003' or as `^C' where the '\\' and '^'
4241 can be defined in the display table. Fill
4242 IT->ctl_chars with glyphs for what we have to
4243 display. Then, set IT->dpvec to these glyphs. */
4244 GLYPH g;
4246 if (it->c < 128 && it->ctl_arrow_p)
4248 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4249 if (it->dp
4250 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4251 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4252 g = XINT (DISP_CTRL_GLYPH (it->dp));
4253 else
4254 g = FAST_MAKE_GLYPH ('^', 0);
4255 XSETINT (it->ctl_chars[0], g);
4257 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4258 XSETINT (it->ctl_chars[1], g);
4260 /* Set up IT->dpvec and return first character from it. */
4261 it->dpvec_char_len = it->len;
4262 it->dpvec = it->ctl_chars;
4263 it->dpend = it->dpvec + 2;
4264 it->current.dpvec_index = 0;
4265 it->method = next_element_from_display_vector;
4266 get_next_display_element (it);
4268 else
4270 unsigned char str[MAX_MULTIBYTE_LENGTH];
4271 int len;
4272 int i;
4273 GLYPH escape_glyph;
4275 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4276 if (it->dp
4277 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4278 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4279 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4280 else
4281 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4283 if (CHAR_BYTE8_P (it->c))
4285 str[0] = CHAR_TO_BYTE8 (it->c);
4286 len = 1;
4288 else if (it->c < 256)
4290 str[0] = it->c;
4291 len = 1;
4293 else
4295 /* It's an invalid character, which
4296 shouldn't happen actually, but due to
4297 bugs it may happen. Let's print the char
4298 as is, there's not much meaningful we can
4299 do with it. */
4300 str[0] = it->c;
4301 str[1] = it->c >> 8;
4302 str[2] = it->c >> 16;
4303 str[3] = it->c >> 24;
4304 len = 4;
4307 for (i = 0; i < len; i++)
4309 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4310 /* Insert three more glyphs into IT->ctl_chars for
4311 the octal display of the character. */
4312 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4313 XSETINT (it->ctl_chars[i * 4 + 1], g);
4314 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4315 XSETINT (it->ctl_chars[i * 4 + 2], g);
4316 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4317 XSETINT (it->ctl_chars[i * 4 + 3], g);
4320 /* Set up IT->dpvec and return the first character
4321 from it. */
4322 it->dpvec_char_len = it->len;
4323 it->dpvec = it->ctl_chars;
4324 it->dpend = it->dpvec + len * 4;
4325 it->current.dpvec_index = 0;
4326 it->method = next_element_from_display_vector;
4327 get_next_display_element (it);
4332 /* Adjust face id for a multibyte character. There are no
4333 multibyte character in unibyte text. */
4334 if (it->multibyte_p
4335 && success_p
4336 && FRAME_WINDOW_P (it->f))
4338 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4339 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4343 /* Is this character the last one of a run of characters with
4344 box? If yes, set IT->end_of_box_run_p to 1. */
4345 if (it->face_box_p
4346 && it->s == NULL)
4348 int face_id;
4349 struct face *face;
4351 it->end_of_box_run_p
4352 = ((face_id = face_after_it_pos (it),
4353 face_id != it->face_id)
4354 && (face = FACE_FROM_ID (it->f, face_id),
4355 face->box == FACE_NO_BOX));
4358 /* Value is 0 if end of buffer or string reached. */
4359 return success_p;
4363 /* Move IT to the next display element.
4365 RESEAT_P non-zero means if called on a newline in buffer text,
4366 skip to the next visible line start.
4368 Functions get_next_display_element and set_iterator_to_next are
4369 separate because I find this arrangement easier to handle than a
4370 get_next_display_element function that also increments IT's
4371 position. The way it is we can first look at an iterator's current
4372 display element, decide whether it fits on a line, and if it does,
4373 increment the iterator position. The other way around we probably
4374 would either need a flag indicating whether the iterator has to be
4375 incremented the next time, or we would have to implement a
4376 decrement position function which would not be easy to write. */
4378 void
4379 set_iterator_to_next (it, reseat_p)
4380 struct it *it;
4381 int reseat_p;
4383 /* Reset flags indicating start and end of a sequence of characters
4384 with box. Reset them at the start of this function because
4385 moving the iterator to a new position might set them. */
4386 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4388 if (it->method == next_element_from_buffer)
4390 /* The current display element of IT is a character from
4391 current_buffer. Advance in the buffer, and maybe skip over
4392 invisible lines that are so because of selective display. */
4393 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4394 reseat_at_next_visible_line_start (it, 0);
4395 else
4397 xassert (it->len != 0);
4398 IT_BYTEPOS (*it) += it->len;
4399 IT_CHARPOS (*it) += 1;
4400 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4403 else if (it->method == next_element_from_composition)
4405 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4406 if (STRINGP (it->string))
4408 IT_STRING_BYTEPOS (*it) += it->len;
4409 IT_STRING_CHARPOS (*it) += it->cmp_len;
4410 it->method = next_element_from_string;
4411 goto consider_string_end;
4413 else
4415 IT_BYTEPOS (*it) += it->len;
4416 IT_CHARPOS (*it) += it->cmp_len;
4417 it->method = next_element_from_buffer;
4420 else if (it->method == next_element_from_c_string)
4422 /* Current display element of IT is from a C string. */
4423 IT_BYTEPOS (*it) += it->len;
4424 IT_CHARPOS (*it) += 1;
4426 else if (it->method == next_element_from_display_vector)
4428 /* Current display element of IT is from a display table entry.
4429 Advance in the display table definition. Reset it to null if
4430 end reached, and continue with characters from buffers/
4431 strings. */
4432 ++it->current.dpvec_index;
4434 /* Restore face of the iterator to what they were before the
4435 display vector entry (these entries may contain faces). */
4436 it->face_id = it->saved_face_id;
4438 if (it->dpvec + it->current.dpvec_index == it->dpend)
4440 if (it->s)
4441 it->method = next_element_from_c_string;
4442 else if (STRINGP (it->string))
4443 it->method = next_element_from_string;
4444 else
4445 it->method = next_element_from_buffer;
4447 it->dpvec = NULL;
4448 it->current.dpvec_index = -1;
4450 /* Skip over characters which were displayed via IT->dpvec. */
4451 if (it->dpvec_char_len < 0)
4452 reseat_at_next_visible_line_start (it, 1);
4453 else if (it->dpvec_char_len > 0)
4455 it->len = it->dpvec_char_len;
4456 set_iterator_to_next (it, reseat_p);
4460 else if (it->method == next_element_from_string)
4462 /* Current display element is a character from a Lisp string. */
4463 xassert (it->s == NULL && STRINGP (it->string));
4464 IT_STRING_BYTEPOS (*it) += it->len;
4465 IT_STRING_CHARPOS (*it) += 1;
4467 consider_string_end:
4469 if (it->current.overlay_string_index >= 0)
4471 /* IT->string is an overlay string. Advance to the
4472 next, if there is one. */
4473 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4474 next_overlay_string (it);
4476 else
4478 /* IT->string is not an overlay string. If we reached
4479 its end, and there is something on IT->stack, proceed
4480 with what is on the stack. This can be either another
4481 string, this time an overlay string, or a buffer. */
4482 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4483 && it->sp > 0)
4485 pop_it (it);
4486 if (!STRINGP (it->string))
4487 it->method = next_element_from_buffer;
4488 else
4489 goto consider_string_end;
4493 else if (it->method == next_element_from_image
4494 || it->method == next_element_from_stretch)
4496 /* The position etc with which we have to proceed are on
4497 the stack. The position may be at the end of a string,
4498 if the `display' property takes up the whole string. */
4499 pop_it (it);
4500 it->image_id = 0;
4501 if (STRINGP (it->string))
4503 it->method = next_element_from_string;
4504 goto consider_string_end;
4506 else
4507 it->method = next_element_from_buffer;
4509 else
4510 /* There are no other methods defined, so this should be a bug. */
4511 abort ();
4513 xassert (it->method != next_element_from_string
4514 || (STRINGP (it->string)
4515 && IT_STRING_CHARPOS (*it) >= 0));
4519 /* Load IT's display element fields with information about the next
4520 display element which comes from a display table entry or from the
4521 result of translating a control character to one of the forms `^C'
4522 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4524 static int
4525 next_element_from_display_vector (it)
4526 struct it *it;
4528 /* Precondition. */
4529 xassert (it->dpvec && it->current.dpvec_index >= 0);
4531 /* Remember the current face id in case glyphs specify faces.
4532 IT's face is restored in set_iterator_to_next. */
4533 it->saved_face_id = it->face_id;
4535 if (INTEGERP (*it->dpvec)
4536 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4538 int lface_id;
4539 GLYPH g;
4541 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4542 it->c = FAST_GLYPH_CHAR (g);
4543 it->len = CHAR_BYTES (it->c);
4545 /* The entry may contain a face id to use. Such a face id is
4546 the id of a Lisp face, not a realized face. A face id of
4547 zero means no face is specified. */
4548 lface_id = FAST_GLYPH_FACE (g);
4549 if (lface_id)
4551 /* The function returns -1 if lface_id is invalid. */
4552 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4553 if (face_id >= 0)
4554 it->face_id = face_id;
4557 else
4558 /* Display table entry is invalid. Return a space. */
4559 it->c = ' ', it->len = 1;
4561 /* Don't change position and object of the iterator here. They are
4562 still the values of the character that had this display table
4563 entry or was translated, and that's what we want. */
4564 it->what = IT_CHARACTER;
4565 return 1;
4569 /* Load IT with the next display element from Lisp string IT->string.
4570 IT->current.string_pos is the current position within the string.
4571 If IT->current.overlay_string_index >= 0, the Lisp string is an
4572 overlay string. */
4574 static int
4575 next_element_from_string (it)
4576 struct it *it;
4578 struct text_pos position;
4580 xassert (STRINGP (it->string));
4581 xassert (IT_STRING_CHARPOS (*it) >= 0);
4582 position = it->current.string_pos;
4584 /* Time to check for invisible text? */
4585 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4586 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4588 handle_stop (it);
4590 /* Since a handler may have changed IT->method, we must
4591 recurse here. */
4592 return get_next_display_element (it);
4595 if (it->current.overlay_string_index >= 0)
4597 /* Get the next character from an overlay string. In overlay
4598 strings, There is no field width or padding with spaces to
4599 do. */
4600 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4602 it->what = IT_EOB;
4603 return 0;
4605 else if (STRING_MULTIBYTE (it->string))
4607 int remaining = (STRING_BYTES (XSTRING (it->string))
4608 - IT_STRING_BYTEPOS (*it));
4609 unsigned char *s = (XSTRING (it->string)->data
4610 + IT_STRING_BYTEPOS (*it));
4611 it->c = string_char_and_length (s, remaining, &it->len);
4613 else
4615 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4616 it->len = 1;
4619 else
4621 /* Get the next character from a Lisp string that is not an
4622 overlay string. Such strings come from the mode line, for
4623 example. We may have to pad with spaces, or truncate the
4624 string. See also next_element_from_c_string. */
4625 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4627 it->what = IT_EOB;
4628 return 0;
4630 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4632 /* Pad with spaces. */
4633 it->c = ' ', it->len = 1;
4634 CHARPOS (position) = BYTEPOS (position) = -1;
4636 else if (STRING_MULTIBYTE (it->string))
4638 int maxlen = (STRING_BYTES (XSTRING (it->string))
4639 - IT_STRING_BYTEPOS (*it));
4640 unsigned char *s = (XSTRING (it->string)->data
4641 + IT_STRING_BYTEPOS (*it));
4642 it->c = string_char_and_length (s, maxlen, &it->len);
4644 else
4646 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4647 it->len = 1;
4651 /* Record what we have and where it came from. Note that we store a
4652 buffer position in IT->position although it could arguably be a
4653 string position. */
4654 it->what = IT_CHARACTER;
4655 it->object = it->string;
4656 it->position = position;
4657 return 1;
4661 /* Load IT with next display element from C string IT->s.
4662 IT->string_nchars is the maximum number of characters to return
4663 from the string. IT->end_charpos may be greater than
4664 IT->string_nchars when this function is called, in which case we
4665 may have to return padding spaces. Value is zero if end of string
4666 reached, including padding spaces. */
4668 static int
4669 next_element_from_c_string (it)
4670 struct it *it;
4672 int success_p = 1;
4674 xassert (it->s);
4675 it->what = IT_CHARACTER;
4676 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4677 it->object = Qnil;
4679 /* IT's position can be greater IT->string_nchars in case a field
4680 width or precision has been specified when the iterator was
4681 initialized. */
4682 if (IT_CHARPOS (*it) >= it->end_charpos)
4684 /* End of the game. */
4685 it->what = IT_EOB;
4686 success_p = 0;
4688 else if (IT_CHARPOS (*it) >= it->string_nchars)
4690 /* Pad with spaces. */
4691 it->c = ' ', it->len = 1;
4692 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4694 else if (it->multibyte_p)
4696 /* Implementation note: The calls to strlen apparently aren't a
4697 performance problem because there is no noticeable performance
4698 difference between Emacs running in unibyte or multibyte mode. */
4699 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4700 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4701 maxlen, &it->len);
4703 else
4704 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4706 return success_p;
4710 /* Set up IT to return characters from an ellipsis, if appropriate.
4711 The definition of the ellipsis glyphs may come from a display table
4712 entry. This function Fills IT with the first glyph from the
4713 ellipsis if an ellipsis is to be displayed. */
4715 static int
4716 next_element_from_ellipsis (it)
4717 struct it *it;
4719 if (it->selective_display_ellipsis_p)
4721 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4723 /* Use the display table definition for `...'. Invalid glyphs
4724 will be handled by the method returning elements from dpvec. */
4725 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4726 it->dpvec_char_len = it->len;
4727 it->dpvec = v->contents;
4728 it->dpend = v->contents + v->size;
4729 it->current.dpvec_index = 0;
4730 it->method = next_element_from_display_vector;
4732 else
4734 /* Use default `...' which is stored in default_invis_vector. */
4735 it->dpvec_char_len = it->len;
4736 it->dpvec = default_invis_vector;
4737 it->dpend = default_invis_vector + 3;
4738 it->current.dpvec_index = 0;
4739 it->method = next_element_from_display_vector;
4742 else
4744 /* The face at the current position may be different from the
4745 face we find after the invisible text. Remember what it
4746 was in IT->saved_face_id, and signal that it's there by
4747 setting face_before_selective_p. */
4748 it->saved_face_id = it->face_id;
4749 it->method = next_element_from_buffer;
4750 reseat_at_next_visible_line_start (it, 1);
4751 it->face_before_selective_p = 1;
4754 return get_next_display_element (it);
4758 /* Deliver an image display element. The iterator IT is already
4759 filled with image information (done in handle_display_prop). Value
4760 is always 1. */
4763 static int
4764 next_element_from_image (it)
4765 struct it *it;
4767 it->what = IT_IMAGE;
4768 return 1;
4772 /* Fill iterator IT with next display element from a stretch glyph
4773 property. IT->object is the value of the text property. Value is
4774 always 1. */
4776 static int
4777 next_element_from_stretch (it)
4778 struct it *it;
4780 it->what = IT_STRETCH;
4781 return 1;
4785 /* Load IT with the next display element from current_buffer. Value
4786 is zero if end of buffer reached. IT->stop_charpos is the next
4787 position at which to stop and check for text properties or buffer
4788 end. */
4790 static int
4791 next_element_from_buffer (it)
4792 struct it *it;
4794 int success_p = 1;
4796 /* Check this assumption, otherwise, we would never enter the
4797 if-statement, below. */
4798 xassert (IT_CHARPOS (*it) >= BEGV
4799 && IT_CHARPOS (*it) <= it->stop_charpos);
4801 if (IT_CHARPOS (*it) >= it->stop_charpos)
4803 if (IT_CHARPOS (*it) >= it->end_charpos)
4805 int overlay_strings_follow_p;
4807 /* End of the game, except when overlay strings follow that
4808 haven't been returned yet. */
4809 if (it->overlay_strings_at_end_processed_p)
4810 overlay_strings_follow_p = 0;
4811 else
4813 it->overlay_strings_at_end_processed_p = 1;
4814 overlay_strings_follow_p = get_overlay_strings (it, 0);
4817 if (overlay_strings_follow_p)
4818 success_p = get_next_display_element (it);
4819 else
4821 it->what = IT_EOB;
4822 it->position = it->current.pos;
4823 success_p = 0;
4826 else
4828 handle_stop (it);
4829 return get_next_display_element (it);
4832 else
4834 /* No face changes, overlays etc. in sight, so just return a
4835 character from current_buffer. */
4836 unsigned char *p;
4838 /* Maybe run the redisplay end trigger hook. Performance note:
4839 This doesn't seem to cost measurable time. */
4840 if (it->redisplay_end_trigger_charpos
4841 && it->glyph_row
4842 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4843 run_redisplay_end_trigger_hook (it);
4845 /* Get the next character, maybe multibyte. */
4846 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4847 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4849 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4850 - IT_BYTEPOS (*it));
4851 it->c = string_char_and_length (p, maxlen, &it->len);
4853 else
4854 it->c = *p, it->len = 1;
4856 /* Record what we have and where it came from. */
4857 it->what = IT_CHARACTER;;
4858 it->object = it->w->buffer;
4859 it->position = it->current.pos;
4861 /* Normally we return the character found above, except when we
4862 really want to return an ellipsis for selective display. */
4863 if (it->selective)
4865 if (it->c == '\n')
4867 /* A value of selective > 0 means hide lines indented more
4868 than that number of columns. */
4869 if (it->selective > 0
4870 && IT_CHARPOS (*it) + 1 < ZV
4871 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4872 IT_BYTEPOS (*it) + 1,
4873 it->selective))
4875 success_p = next_element_from_ellipsis (it);
4876 it->dpvec_char_len = -1;
4879 else if (it->c == '\r' && it->selective == -1)
4881 /* A value of selective == -1 means that everything from the
4882 CR to the end of the line is invisible, with maybe an
4883 ellipsis displayed for it. */
4884 success_p = next_element_from_ellipsis (it);
4885 it->dpvec_char_len = -1;
4890 /* Value is zero if end of buffer reached. */
4891 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4892 return success_p;
4896 /* Run the redisplay end trigger hook for IT. */
4898 static void
4899 run_redisplay_end_trigger_hook (it)
4900 struct it *it;
4902 Lisp_Object args[3];
4904 /* IT->glyph_row should be non-null, i.e. we should be actually
4905 displaying something, or otherwise we should not run the hook. */
4906 xassert (it->glyph_row);
4908 /* Set up hook arguments. */
4909 args[0] = Qredisplay_end_trigger_functions;
4910 args[1] = it->window;
4911 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4912 it->redisplay_end_trigger_charpos = 0;
4914 /* Since we are *trying* to run these functions, don't try to run
4915 them again, even if they get an error. */
4916 it->w->redisplay_end_trigger = Qnil;
4917 Frun_hook_with_args (3, args);
4919 /* Notice if it changed the face of the character we are on. */
4920 handle_face_prop (it);
4924 /* Deliver a composition display element. The iterator IT is already
4925 filled with composition information (done in
4926 handle_composition_prop). Value is always 1. */
4928 static int
4929 next_element_from_composition (it)
4930 struct it *it;
4932 it->what = IT_COMPOSITION;
4933 it->position = (STRINGP (it->string)
4934 ? it->current.string_pos
4935 : it->current.pos);
4936 return 1;
4941 /***********************************************************************
4942 Moving an iterator without producing glyphs
4943 ***********************************************************************/
4945 /* Move iterator IT to a specified buffer or X position within one
4946 line on the display without producing glyphs.
4948 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4949 whichever is reached first.
4951 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4953 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4954 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4955 TO_X includes the amount by which a window is horizontally
4956 scrolled.
4958 Value is
4960 MOVE_POS_MATCH_OR_ZV
4961 - when TO_POS or ZV was reached.
4963 MOVE_X_REACHED
4964 -when TO_X was reached before TO_POS or ZV were reached.
4966 MOVE_LINE_CONTINUED
4967 - when we reached the end of the display area and the line must
4968 be continued.
4970 MOVE_LINE_TRUNCATED
4971 - when we reached the end of the display area and the line is
4972 truncated.
4974 MOVE_NEWLINE_OR_CR
4975 - when we stopped at a line end, i.e. a newline or a CR and selective
4976 display is on. */
4978 static enum move_it_result
4979 move_it_in_display_line_to (it, to_charpos, to_x, op)
4980 struct it *it;
4981 int to_charpos, to_x, op;
4983 enum move_it_result result = MOVE_UNDEFINED;
4984 struct glyph_row *saved_glyph_row;
4986 /* Don't produce glyphs in produce_glyphs. */
4987 saved_glyph_row = it->glyph_row;
4988 it->glyph_row = NULL;
4990 while (1)
4992 int x, i, ascent = 0, descent = 0;
4994 /* Stop when ZV or TO_CHARPOS reached. */
4995 if (!get_next_display_element (it)
4996 || ((op & MOVE_TO_POS) != 0
4997 && BUFFERP (it->object)
4998 && IT_CHARPOS (*it) >= to_charpos))
5000 result = MOVE_POS_MATCH_OR_ZV;
5001 break;
5004 /* The call to produce_glyphs will get the metrics of the
5005 display element IT is loaded with. We record in x the
5006 x-position before this display element in case it does not
5007 fit on the line. */
5008 x = it->current_x;
5010 /* Remember the line height so far in case the next element doesn't
5011 fit on the line. */
5012 if (!it->truncate_lines_p)
5014 ascent = it->max_ascent;
5015 descent = it->max_descent;
5018 PRODUCE_GLYPHS (it);
5020 if (it->area != TEXT_AREA)
5022 set_iterator_to_next (it, 1);
5023 continue;
5026 /* The number of glyphs we get back in IT->nglyphs will normally
5027 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5028 character on a terminal frame, or (iii) a line end. For the
5029 second case, IT->nglyphs - 1 padding glyphs will be present
5030 (on X frames, there is only one glyph produced for a
5031 composite character.
5033 The behavior implemented below means, for continuation lines,
5034 that as many spaces of a TAB as fit on the current line are
5035 displayed there. For terminal frames, as many glyphs of a
5036 multi-glyph character are displayed in the current line, too.
5037 This is what the old redisplay code did, and we keep it that
5038 way. Under X, the whole shape of a complex character must
5039 fit on the line or it will be completely displayed in the
5040 next line.
5042 Note that both for tabs and padding glyphs, all glyphs have
5043 the same width. */
5044 if (it->nglyphs)
5046 /* More than one glyph or glyph doesn't fit on line. All
5047 glyphs have the same width. */
5048 int single_glyph_width = it->pixel_width / it->nglyphs;
5049 int new_x;
5051 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5053 new_x = x + single_glyph_width;
5055 /* We want to leave anything reaching TO_X to the caller. */
5056 if ((op & MOVE_TO_X) && new_x > to_x)
5058 it->current_x = x;
5059 result = MOVE_X_REACHED;
5060 break;
5062 else if (/* Lines are continued. */
5063 !it->truncate_lines_p
5064 && (/* And glyph doesn't fit on the line. */
5065 new_x > it->last_visible_x
5066 /* Or it fits exactly and we're on a window
5067 system frame. */
5068 || (new_x == it->last_visible_x
5069 && FRAME_WINDOW_P (it->f))))
5071 if (/* IT->hpos == 0 means the very first glyph
5072 doesn't fit on the line, e.g. a wide image. */
5073 it->hpos == 0
5074 || (new_x == it->last_visible_x
5075 && FRAME_WINDOW_P (it->f)))
5077 ++it->hpos;
5078 it->current_x = new_x;
5079 if (i == it->nglyphs - 1)
5080 set_iterator_to_next (it, 1);
5082 else
5084 it->current_x = x;
5085 it->max_ascent = ascent;
5086 it->max_descent = descent;
5089 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5090 IT_CHARPOS (*it)));
5091 result = MOVE_LINE_CONTINUED;
5092 break;
5094 else if (new_x > it->first_visible_x)
5096 /* Glyph is visible. Increment number of glyphs that
5097 would be displayed. */
5098 ++it->hpos;
5100 else
5102 /* Glyph is completely off the left margin of the display
5103 area. Nothing to do. */
5107 if (result != MOVE_UNDEFINED)
5108 break;
5110 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5112 /* Stop when TO_X specified and reached. This check is
5113 necessary here because of lines consisting of a line end,
5114 only. The line end will not produce any glyphs and we
5115 would never get MOVE_X_REACHED. */
5116 xassert (it->nglyphs == 0);
5117 result = MOVE_X_REACHED;
5118 break;
5121 /* Is this a line end? If yes, we're done. */
5122 if (ITERATOR_AT_END_OF_LINE_P (it))
5124 result = MOVE_NEWLINE_OR_CR;
5125 break;
5128 /* The current display element has been consumed. Advance
5129 to the next. */
5130 set_iterator_to_next (it, 1);
5132 /* Stop if lines are truncated and IT's current x-position is
5133 past the right edge of the window now. */
5134 if (it->truncate_lines_p
5135 && it->current_x >= it->last_visible_x)
5137 result = MOVE_LINE_TRUNCATED;
5138 break;
5142 /* Restore the iterator settings altered at the beginning of this
5143 function. */
5144 it->glyph_row = saved_glyph_row;
5145 return result;
5149 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5150 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5151 the description of enum move_operation_enum.
5153 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5154 screen line, this function will set IT to the next position >
5155 TO_CHARPOS. */
5157 void
5158 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5159 struct it *it;
5160 int to_charpos, to_x, to_y, to_vpos;
5161 int op;
5163 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5164 int line_height;
5165 int reached = 0;
5167 for (;;)
5169 if (op & MOVE_TO_VPOS)
5171 /* If no TO_CHARPOS and no TO_X specified, stop at the
5172 start of the line TO_VPOS. */
5173 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5175 if (it->vpos == to_vpos)
5177 reached = 1;
5178 break;
5180 else
5181 skip = move_it_in_display_line_to (it, -1, -1, 0);
5183 else
5185 /* TO_VPOS >= 0 means stop at TO_X in the line at
5186 TO_VPOS, or at TO_POS, whichever comes first. */
5187 if (it->vpos == to_vpos)
5189 reached = 2;
5190 break;
5193 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5195 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5197 reached = 3;
5198 break;
5200 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5202 /* We have reached TO_X but not in the line we want. */
5203 skip = move_it_in_display_line_to (it, to_charpos,
5204 -1, MOVE_TO_POS);
5205 if (skip == MOVE_POS_MATCH_OR_ZV)
5207 reached = 4;
5208 break;
5213 else if (op & MOVE_TO_Y)
5215 struct it it_backup;
5217 /* TO_Y specified means stop at TO_X in the line containing
5218 TO_Y---or at TO_CHARPOS if this is reached first. The
5219 problem is that we can't really tell whether the line
5220 contains TO_Y before we have completely scanned it, and
5221 this may skip past TO_X. What we do is to first scan to
5222 TO_X.
5224 If TO_X is not specified, use a TO_X of zero. The reason
5225 is to make the outcome of this function more predictable.
5226 If we didn't use TO_X == 0, we would stop at the end of
5227 the line which is probably not what a caller would expect
5228 to happen. */
5229 skip = move_it_in_display_line_to (it, to_charpos,
5230 ((op & MOVE_TO_X)
5231 ? to_x : 0),
5232 (MOVE_TO_X
5233 | (op & MOVE_TO_POS)));
5235 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5236 if (skip == MOVE_POS_MATCH_OR_ZV)
5238 reached = 5;
5239 break;
5242 /* If TO_X was reached, we would like to know whether TO_Y
5243 is in the line. This can only be said if we know the
5244 total line height which requires us to scan the rest of
5245 the line. */
5246 if (skip == MOVE_X_REACHED)
5248 it_backup = *it;
5249 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5250 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5251 op & MOVE_TO_POS);
5252 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5255 /* Now, decide whether TO_Y is in this line. */
5256 line_height = it->max_ascent + it->max_descent;
5257 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5259 if (to_y >= it->current_y
5260 && to_y < it->current_y + line_height)
5262 if (skip == MOVE_X_REACHED)
5263 /* If TO_Y is in this line and TO_X was reached above,
5264 we scanned too far. We have to restore IT's settings
5265 to the ones before skipping. */
5266 *it = it_backup;
5267 reached = 6;
5269 else if (skip == MOVE_X_REACHED)
5271 skip = skip2;
5272 if (skip == MOVE_POS_MATCH_OR_ZV)
5273 reached = 7;
5276 if (reached)
5277 break;
5279 else
5280 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5282 switch (skip)
5284 case MOVE_POS_MATCH_OR_ZV:
5285 reached = 8;
5286 goto out;
5288 case MOVE_NEWLINE_OR_CR:
5289 set_iterator_to_next (it, 1);
5290 it->continuation_lines_width = 0;
5291 break;
5293 case MOVE_LINE_TRUNCATED:
5294 it->continuation_lines_width = 0;
5295 reseat_at_next_visible_line_start (it, 0);
5296 if ((op & MOVE_TO_POS) != 0
5297 && IT_CHARPOS (*it) > to_charpos)
5299 reached = 9;
5300 goto out;
5302 break;
5304 case MOVE_LINE_CONTINUED:
5305 it->continuation_lines_width += it->current_x;
5306 break;
5308 default:
5309 abort ();
5312 /* Reset/increment for the next run. */
5313 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5314 it->current_x = it->hpos = 0;
5315 it->current_y += it->max_ascent + it->max_descent;
5316 ++it->vpos;
5317 last_height = it->max_ascent + it->max_descent;
5318 last_max_ascent = it->max_ascent;
5319 it->max_ascent = it->max_descent = 0;
5322 out:
5324 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5328 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5330 If DY > 0, move IT backward at least that many pixels. DY = 0
5331 means move IT backward to the preceding line start or BEGV. This
5332 function may move over more than DY pixels if IT->current_y - DY
5333 ends up in the middle of a line; in this case IT->current_y will be
5334 set to the top of the line moved to. */
5336 void
5337 move_it_vertically_backward (it, dy)
5338 struct it *it;
5339 int dy;
5341 int nlines, h;
5342 struct it it2, it3;
5343 int start_pos = IT_CHARPOS (*it);
5345 xassert (dy >= 0);
5347 /* Estimate how many newlines we must move back. */
5348 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5350 /* Set the iterator's position that many lines back. */
5351 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5352 back_to_previous_visible_line_start (it);
5354 /* Reseat the iterator here. When moving backward, we don't want
5355 reseat to skip forward over invisible text, set up the iterator
5356 to deliver from overlay strings at the new position etc. So,
5357 use reseat_1 here. */
5358 reseat_1 (it, it->current.pos, 1);
5360 /* We are now surely at a line start. */
5361 it->current_x = it->hpos = 0;
5363 /* Move forward and see what y-distance we moved. First move to the
5364 start of the next line so that we get its height. We need this
5365 height to be able to tell whether we reached the specified
5366 y-distance. */
5367 it2 = *it;
5368 it2.max_ascent = it2.max_descent = 0;
5369 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5370 MOVE_TO_POS | MOVE_TO_VPOS);
5371 xassert (IT_CHARPOS (*it) >= BEGV);
5372 it3 = it2;
5374 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5375 xassert (IT_CHARPOS (*it) >= BEGV);
5376 h = it2.current_y - it->current_y;
5377 nlines = it2.vpos - it->vpos;
5379 /* Correct IT's y and vpos position. */
5380 it->vpos -= nlines;
5381 it->current_y -= h;
5383 if (dy == 0)
5385 /* DY == 0 means move to the start of the screen line. The
5386 value of nlines is > 0 if continuation lines were involved. */
5387 if (nlines > 0)
5388 move_it_by_lines (it, nlines, 1);
5389 xassert (IT_CHARPOS (*it) <= start_pos);
5391 else if (nlines)
5393 /* The y-position we try to reach. Note that h has been
5394 subtracted in front of the if-statement. */
5395 int target_y = it->current_y + h - dy;
5396 int y0 = it3.current_y;
5397 int y1 = line_bottom_y (&it3);
5398 int line_height = y1 - y0;
5400 /* If we did not reach target_y, try to move further backward if
5401 we can. If we moved too far backward, try to move forward. */
5402 if (target_y < it->current_y
5403 /* This is heuristic. In a window that's 3 lines high, with
5404 a line height of 13 pixels each, recentering with point
5405 on the bottom line will try to move -39/2 = 19 pixels
5406 backward. Try to avoid moving into the first line. */
5407 && it->current_y - target_y > line_height / 3 * 2
5408 && IT_CHARPOS (*it) > BEGV)
5410 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5411 target_y - it->current_y));
5412 move_it_vertically (it, target_y - it->current_y);
5413 xassert (IT_CHARPOS (*it) >= BEGV);
5415 else if (target_y >= it->current_y + line_height
5416 && IT_CHARPOS (*it) < ZV)
5418 /* Should move forward by at least one line, maybe more. */
5421 move_it_by_lines (it, 1, 1);
5423 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5425 xassert (IT_CHARPOS (*it) >= BEGV);
5431 /* Move IT by a specified amount of pixel lines DY. DY negative means
5432 move backwards. DY = 0 means move to start of screen line. At the
5433 end, IT will be on the start of a screen line. */
5435 void
5436 move_it_vertically (it, dy)
5437 struct it *it;
5438 int dy;
5440 if (dy <= 0)
5441 move_it_vertically_backward (it, -dy);
5442 else if (dy > 0)
5444 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5445 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5446 MOVE_TO_POS | MOVE_TO_Y);
5447 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5449 /* If buffer ends in ZV without a newline, move to the start of
5450 the line to satisfy the post-condition. */
5451 if (IT_CHARPOS (*it) == ZV
5452 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5453 move_it_by_lines (it, 0, 0);
5458 /* Move iterator IT past the end of the text line it is in. */
5460 void
5461 move_it_past_eol (it)
5462 struct it *it;
5464 enum move_it_result rc;
5466 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5467 if (rc == MOVE_NEWLINE_OR_CR)
5468 set_iterator_to_next (it, 0);
5472 #if 0 /* Currently not used. */
5474 /* Return non-zero if some text between buffer positions START_CHARPOS
5475 and END_CHARPOS is invisible. IT->window is the window for text
5476 property lookup. */
5478 static int
5479 invisible_text_between_p (it, start_charpos, end_charpos)
5480 struct it *it;
5481 int start_charpos, end_charpos;
5483 Lisp_Object prop, limit;
5484 int invisible_found_p;
5486 xassert (it != NULL && start_charpos <= end_charpos);
5488 /* Is text at START invisible? */
5489 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5490 it->window);
5491 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5492 invisible_found_p = 1;
5493 else
5495 limit = Fnext_single_char_property_change (make_number (start_charpos),
5496 Qinvisible, Qnil,
5497 make_number (end_charpos));
5498 invisible_found_p = XFASTINT (limit) < end_charpos;
5501 return invisible_found_p;
5504 #endif /* 0 */
5507 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5508 negative means move up. DVPOS == 0 means move to the start of the
5509 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5510 NEED_Y_P is zero, IT->current_y will be left unchanged.
5512 Further optimization ideas: If we would know that IT->f doesn't use
5513 a face with proportional font, we could be faster for
5514 truncate-lines nil. */
5516 void
5517 move_it_by_lines (it, dvpos, need_y_p)
5518 struct it *it;
5519 int dvpos, need_y_p;
5521 struct position pos;
5523 if (!FRAME_WINDOW_P (it->f))
5525 struct text_pos textpos;
5527 /* We can use vmotion on frames without proportional fonts. */
5528 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5529 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5530 reseat (it, textpos, 1);
5531 it->vpos += pos.vpos;
5532 it->current_y += pos.vpos;
5534 else if (dvpos == 0)
5536 /* DVPOS == 0 means move to the start of the screen line. */
5537 move_it_vertically_backward (it, 0);
5538 xassert (it->current_x == 0 && it->hpos == 0);
5540 else if (dvpos > 0)
5541 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5542 else
5544 struct it it2;
5545 int start_charpos, i;
5547 /* Start at the beginning of the screen line containing IT's
5548 position. */
5549 move_it_vertically_backward (it, 0);
5551 /* Go back -DVPOS visible lines and reseat the iterator there. */
5552 start_charpos = IT_CHARPOS (*it);
5553 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5554 back_to_previous_visible_line_start (it);
5555 reseat (it, it->current.pos, 1);
5556 it->current_x = it->hpos = 0;
5558 /* Above call may have moved too far if continuation lines
5559 are involved. Scan forward and see if it did. */
5560 it2 = *it;
5561 it2.vpos = it2.current_y = 0;
5562 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5563 it->vpos -= it2.vpos;
5564 it->current_y -= it2.current_y;
5565 it->current_x = it->hpos = 0;
5567 /* If we moved too far, move IT some lines forward. */
5568 if (it2.vpos > -dvpos)
5570 int delta = it2.vpos + dvpos;
5571 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5578 /***********************************************************************
5579 Messages
5580 ***********************************************************************/
5583 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5584 to *Messages*. */
5586 void
5587 add_to_log (format, arg1, arg2)
5588 char *format;
5589 Lisp_Object arg1, arg2;
5591 Lisp_Object args[3];
5592 Lisp_Object msg, fmt;
5593 char *buffer;
5594 int len;
5595 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5597 /* Do nothing if called asynchronously. Inserting text into
5598 a buffer may call after-change-functions and alike and
5599 that would means running Lisp asynchronously. */
5600 if (handling_signal)
5601 return;
5603 fmt = msg = Qnil;
5604 GCPRO4 (fmt, msg, arg1, arg2);
5606 args[0] = fmt = build_string (format);
5607 args[1] = arg1;
5608 args[2] = arg2;
5609 msg = Fformat (3, args);
5611 len = STRING_BYTES (XSTRING (msg)) + 1;
5612 buffer = (char *) alloca (len);
5613 bcopy (XSTRING (msg)->data, buffer, len);
5615 message_dolog (buffer, len - 1, 1, 0);
5616 UNGCPRO;
5620 /* Output a newline in the *Messages* buffer if "needs" one. */
5622 void
5623 message_log_maybe_newline ()
5625 if (message_log_need_newline)
5626 message_dolog ("", 0, 1, 0);
5630 /* Add a string M of length NBYTES to the message log, optionally
5631 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5632 nonzero, means interpret the contents of M as multibyte. This
5633 function calls low-level routines in order to bypass text property
5634 hooks, etc. which might not be safe to run. */
5636 void
5637 message_dolog (m, nbytes, nlflag, multibyte)
5638 char *m;
5639 int nbytes, nlflag, multibyte;
5641 if (!NILP (Vmessage_log_max))
5643 struct buffer *oldbuf;
5644 Lisp_Object oldpoint, oldbegv, oldzv;
5645 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5646 int point_at_end = 0;
5647 int zv_at_end = 0;
5648 Lisp_Object old_deactivate_mark, tem;
5649 struct gcpro gcpro1;
5651 old_deactivate_mark = Vdeactivate_mark;
5652 oldbuf = current_buffer;
5653 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5654 current_buffer->undo_list = Qt;
5656 oldpoint = message_dolog_marker1;
5657 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5658 oldbegv = message_dolog_marker2;
5659 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5660 oldzv = message_dolog_marker3;
5661 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5662 GCPRO1 (old_deactivate_mark);
5664 if (PT == Z)
5665 point_at_end = 1;
5666 if (ZV == Z)
5667 zv_at_end = 1;
5669 BEGV = BEG;
5670 BEGV_BYTE = BEG_BYTE;
5671 ZV = Z;
5672 ZV_BYTE = Z_BYTE;
5673 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5675 /* Insert the string--maybe converting multibyte to single byte
5676 or vice versa, so that all the text fits the buffer. */
5677 if (multibyte
5678 && NILP (current_buffer->enable_multibyte_characters))
5680 int i, c, char_bytes;
5681 unsigned char work[1];
5683 /* Convert a multibyte string to single-byte
5684 for the *Message* buffer. */
5685 for (i = 0; i < nbytes; i += nbytes)
5687 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5688 work[0] = (SINGLE_BYTE_CHAR_P (c)
5690 : multibyte_char_to_unibyte (c, Qnil));
5691 insert_1_both (work, 1, 1, 1, 0, 0);
5694 else if (! multibyte
5695 && ! NILP (current_buffer->enable_multibyte_characters))
5697 int i, c, char_bytes;
5698 unsigned char *msg = (unsigned char *) m;
5699 unsigned char str[MAX_MULTIBYTE_LENGTH];
5700 /* Convert a single-byte string to multibyte
5701 for the *Message* buffer. */
5702 for (i = 0; i < nbytes; i++)
5704 c = unibyte_char_to_multibyte (msg[i]);
5705 char_bytes = CHAR_STRING (c, str);
5706 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5709 else if (nbytes)
5710 insert_1 (m, nbytes, 1, 0, 0);
5712 if (nlflag)
5714 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5715 insert_1 ("\n", 1, 1, 0, 0);
5717 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5718 this_bol = PT;
5719 this_bol_byte = PT_BYTE;
5721 /* See if this line duplicates the previous one.
5722 If so, combine duplicates. */
5723 if (this_bol > BEG)
5725 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5726 prev_bol = PT;
5727 prev_bol_byte = PT_BYTE;
5729 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5730 this_bol, this_bol_byte);
5731 if (dup)
5733 del_range_both (prev_bol, prev_bol_byte,
5734 this_bol, this_bol_byte, 0);
5735 if (dup > 1)
5737 char dupstr[40];
5738 int duplen;
5740 /* If you change this format, don't forget to also
5741 change message_log_check_duplicate. */
5742 sprintf (dupstr, " [%d times]", dup);
5743 duplen = strlen (dupstr);
5744 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5745 insert_1 (dupstr, duplen, 1, 0, 1);
5750 /* If we have more than the desired maximum number of lines
5751 in the *Messages* buffer now, delete the oldest ones.
5752 This is safe because we don't have undo in this buffer. */
5754 if (NATNUMP (Vmessage_log_max))
5756 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5757 -XFASTINT (Vmessage_log_max) - 1, 0);
5758 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5761 BEGV = XMARKER (oldbegv)->charpos;
5762 BEGV_BYTE = marker_byte_position (oldbegv);
5764 if (zv_at_end)
5766 ZV = Z;
5767 ZV_BYTE = Z_BYTE;
5769 else
5771 ZV = XMARKER (oldzv)->charpos;
5772 ZV_BYTE = marker_byte_position (oldzv);
5775 if (point_at_end)
5776 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5777 else
5778 /* We can't do Fgoto_char (oldpoint) because it will run some
5779 Lisp code. */
5780 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5781 XMARKER (oldpoint)->bytepos);
5783 UNGCPRO;
5784 unchain_marker (oldpoint);
5785 unchain_marker (oldbegv);
5786 unchain_marker (oldzv);
5788 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5789 set_buffer_internal (oldbuf);
5790 if (NILP (tem))
5791 windows_or_buffers_changed = old_windows_or_buffers_changed;
5792 message_log_need_newline = !nlflag;
5793 Vdeactivate_mark = old_deactivate_mark;
5798 /* We are at the end of the buffer after just having inserted a newline.
5799 (Note: We depend on the fact we won't be crossing the gap.)
5800 Check to see if the most recent message looks a lot like the previous one.
5801 Return 0 if different, 1 if the new one should just replace it, or a
5802 value N > 1 if we should also append " [N times]". */
5804 static int
5805 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5806 int prev_bol, this_bol;
5807 int prev_bol_byte, this_bol_byte;
5809 int i;
5810 int len = Z_BYTE - 1 - this_bol_byte;
5811 int seen_dots = 0;
5812 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5813 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5815 for (i = 0; i < len; i++)
5817 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5818 seen_dots = 1;
5819 if (p1[i] != p2[i])
5820 return seen_dots;
5822 p1 += len;
5823 if (*p1 == '\n')
5824 return 2;
5825 if (*p1++ == ' ' && *p1++ == '[')
5827 int n = 0;
5828 while (*p1 >= '0' && *p1 <= '9')
5829 n = n * 10 + *p1++ - '0';
5830 if (strncmp (p1, " times]\n", 8) == 0)
5831 return n+1;
5833 return 0;
5837 /* Display an echo area message M with a specified length of NBYTES
5838 bytes. The string may include null characters. If M is 0, clear
5839 out any existing message, and let the mini-buffer text show
5840 through.
5842 The buffer M must continue to exist until after the echo area gets
5843 cleared or some other message gets displayed there. This means do
5844 not pass text that is stored in a Lisp string; do not pass text in
5845 a buffer that was alloca'd. */
5847 void
5848 message2 (m, nbytes, multibyte)
5849 char *m;
5850 int nbytes;
5851 int multibyte;
5853 /* First flush out any partial line written with print. */
5854 message_log_maybe_newline ();
5855 if (m)
5856 message_dolog (m, nbytes, 1, multibyte);
5857 message2_nolog (m, nbytes, multibyte);
5861 /* The non-logging counterpart of message2. */
5863 void
5864 message2_nolog (m, nbytes, multibyte)
5865 char *m;
5866 int nbytes;
5868 struct frame *sf = SELECTED_FRAME ();
5869 message_enable_multibyte = multibyte;
5871 if (noninteractive)
5873 if (noninteractive_need_newline)
5874 putc ('\n', stderr);
5875 noninteractive_need_newline = 0;
5876 if (m)
5877 fwrite (m, nbytes, 1, stderr);
5878 if (cursor_in_echo_area == 0)
5879 fprintf (stderr, "\n");
5880 fflush (stderr);
5882 /* A null message buffer means that the frame hasn't really been
5883 initialized yet. Error messages get reported properly by
5884 cmd_error, so this must be just an informative message; toss it. */
5885 else if (INTERACTIVE
5886 && sf->glyphs_initialized_p
5887 && FRAME_MESSAGE_BUF (sf))
5889 Lisp_Object mini_window;
5890 struct frame *f;
5892 /* Get the frame containing the mini-buffer
5893 that the selected frame is using. */
5894 mini_window = FRAME_MINIBUF_WINDOW (sf);
5895 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5897 FRAME_SAMPLE_VISIBILITY (f);
5898 if (FRAME_VISIBLE_P (sf)
5899 && ! FRAME_VISIBLE_P (f))
5900 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5902 if (m)
5904 set_message (m, Qnil, nbytes, multibyte);
5905 if (minibuffer_auto_raise)
5906 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5908 else
5909 clear_message (1, 1);
5911 do_pending_window_change (0);
5912 echo_area_display (1);
5913 do_pending_window_change (0);
5914 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5915 (*frame_up_to_date_hook) (f);
5920 /* Display an echo area message M with a specified length of NBYTES
5921 bytes. The string may include null characters. If M is not a
5922 string, clear out any existing message, and let the mini-buffer
5923 text show through. */
5925 void
5926 message3 (m, nbytes, multibyte)
5927 Lisp_Object m;
5928 int nbytes;
5929 int multibyte;
5931 struct gcpro gcpro1;
5933 GCPRO1 (m);
5935 /* First flush out any partial line written with print. */
5936 message_log_maybe_newline ();
5937 if (STRINGP (m))
5938 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5939 message3_nolog (m, nbytes, multibyte);
5941 UNGCPRO;
5945 /* The non-logging version of message3. */
5947 void
5948 message3_nolog (m, nbytes, multibyte)
5949 Lisp_Object m;
5950 int nbytes, multibyte;
5952 struct frame *sf = SELECTED_FRAME ();
5953 message_enable_multibyte = multibyte;
5955 if (noninteractive)
5957 if (noninteractive_need_newline)
5958 putc ('\n', stderr);
5959 noninteractive_need_newline = 0;
5960 if (STRINGP (m))
5961 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5962 if (cursor_in_echo_area == 0)
5963 fprintf (stderr, "\n");
5964 fflush (stderr);
5966 /* A null message buffer means that the frame hasn't really been
5967 initialized yet. Error messages get reported properly by
5968 cmd_error, so this must be just an informative message; toss it. */
5969 else if (INTERACTIVE
5970 && sf->glyphs_initialized_p
5971 && FRAME_MESSAGE_BUF (sf))
5973 Lisp_Object mini_window;
5974 Lisp_Object frame;
5975 struct frame *f;
5977 /* Get the frame containing the mini-buffer
5978 that the selected frame is using. */
5979 mini_window = FRAME_MINIBUF_WINDOW (sf);
5980 frame = XWINDOW (mini_window)->frame;
5981 f = XFRAME (frame);
5983 FRAME_SAMPLE_VISIBILITY (f);
5984 if (FRAME_VISIBLE_P (sf)
5985 && !FRAME_VISIBLE_P (f))
5986 Fmake_frame_visible (frame);
5988 if (STRINGP (m) && XSTRING (m)->size)
5990 set_message (NULL, m, nbytes, multibyte);
5991 if (minibuffer_auto_raise)
5992 Fraise_frame (frame);
5994 else
5995 clear_message (1, 1);
5997 do_pending_window_change (0);
5998 echo_area_display (1);
5999 do_pending_window_change (0);
6000 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6001 (*frame_up_to_date_hook) (f);
6006 /* Display a null-terminated echo area message M. If M is 0, clear
6007 out any existing message, and let the mini-buffer text show through.
6009 The buffer M must continue to exist until after the echo area gets
6010 cleared or some other message gets displayed there. Do not pass
6011 text that is stored in a Lisp string. Do not pass text in a buffer
6012 that was alloca'd. */
6014 void
6015 message1 (m)
6016 char *m;
6018 message2 (m, (m ? strlen (m) : 0), 0);
6022 /* The non-logging counterpart of message1. */
6024 void
6025 message1_nolog (m)
6026 char *m;
6028 message2_nolog (m, (m ? strlen (m) : 0), 0);
6031 /* Display a message M which contains a single %s
6032 which gets replaced with STRING. */
6034 void
6035 message_with_string (m, string, log)
6036 char *m;
6037 Lisp_Object string;
6038 int log;
6040 if (noninteractive)
6042 if (m)
6044 if (noninteractive_need_newline)
6045 putc ('\n', stderr);
6046 noninteractive_need_newline = 0;
6047 fprintf (stderr, m, XSTRING (string)->data);
6048 if (cursor_in_echo_area == 0)
6049 fprintf (stderr, "\n");
6050 fflush (stderr);
6053 else if (INTERACTIVE)
6055 /* The frame whose minibuffer we're going to display the message on.
6056 It may be larger than the selected frame, so we need
6057 to use its buffer, not the selected frame's buffer. */
6058 Lisp_Object mini_window;
6059 struct frame *f, *sf = SELECTED_FRAME ();
6061 /* Get the frame containing the minibuffer
6062 that the selected frame is using. */
6063 mini_window = FRAME_MINIBUF_WINDOW (sf);
6064 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6066 /* A null message buffer means that the frame hasn't really been
6067 initialized yet. Error messages get reported properly by
6068 cmd_error, so this must be just an informative message; toss it. */
6069 if (FRAME_MESSAGE_BUF (f))
6071 int len;
6072 char *a[1];
6073 a[0] = (char *) XSTRING (string)->data;
6075 len = doprnt (FRAME_MESSAGE_BUF (f),
6076 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6078 if (log)
6079 message2 (FRAME_MESSAGE_BUF (f), len,
6080 STRING_MULTIBYTE (string));
6081 else
6082 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6083 STRING_MULTIBYTE (string));
6085 /* Print should start at the beginning of the message
6086 buffer next time. */
6087 message_buf_print = 0;
6093 /* Dump an informative message to the minibuf. If M is 0, clear out
6094 any existing message, and let the mini-buffer text show through. */
6096 /* VARARGS 1 */
6097 void
6098 message (m, a1, a2, a3)
6099 char *m;
6100 EMACS_INT a1, a2, a3;
6102 if (noninteractive)
6104 if (m)
6106 if (noninteractive_need_newline)
6107 putc ('\n', stderr);
6108 noninteractive_need_newline = 0;
6109 fprintf (stderr, m, a1, a2, a3);
6110 if (cursor_in_echo_area == 0)
6111 fprintf (stderr, "\n");
6112 fflush (stderr);
6115 else if (INTERACTIVE)
6117 /* The frame whose mini-buffer we're going to display the message
6118 on. It may be larger than the selected frame, so we need to
6119 use its buffer, not the selected frame's buffer. */
6120 Lisp_Object mini_window;
6121 struct frame *f, *sf = SELECTED_FRAME ();
6123 /* Get the frame containing the mini-buffer
6124 that the selected frame is using. */
6125 mini_window = FRAME_MINIBUF_WINDOW (sf);
6126 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6128 /* A null message buffer means that the frame hasn't really been
6129 initialized yet. Error messages get reported properly by
6130 cmd_error, so this must be just an informative message; toss
6131 it. */
6132 if (FRAME_MESSAGE_BUF (f))
6134 if (m)
6136 int len;
6137 #ifdef NO_ARG_ARRAY
6138 char *a[3];
6139 a[0] = (char *) a1;
6140 a[1] = (char *) a2;
6141 a[2] = (char *) a3;
6143 len = doprnt (FRAME_MESSAGE_BUF (f),
6144 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6145 #else
6146 len = doprnt (FRAME_MESSAGE_BUF (f),
6147 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6148 (char **) &a1);
6149 #endif /* NO_ARG_ARRAY */
6151 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6153 else
6154 message1 (0);
6156 /* Print should start at the beginning of the message
6157 buffer next time. */
6158 message_buf_print = 0;
6164 /* The non-logging version of message. */
6166 void
6167 message_nolog (m, a1, a2, a3)
6168 char *m;
6169 EMACS_INT a1, a2, a3;
6171 Lisp_Object old_log_max;
6172 old_log_max = Vmessage_log_max;
6173 Vmessage_log_max = Qnil;
6174 message (m, a1, a2, a3);
6175 Vmessage_log_max = old_log_max;
6179 /* Display the current message in the current mini-buffer. This is
6180 only called from error handlers in process.c, and is not time
6181 critical. */
6183 void
6184 update_echo_area ()
6186 if (!NILP (echo_area_buffer[0]))
6188 Lisp_Object string;
6189 string = Fcurrent_message ();
6190 message3 (string, XSTRING (string)->size,
6191 !NILP (current_buffer->enable_multibyte_characters));
6196 /* Make sure echo area buffers in echo_buffers[] are life. If they
6197 aren't, make new ones. */
6199 static void
6200 ensure_echo_area_buffers ()
6202 int i;
6204 for (i = 0; i < 2; ++i)
6205 if (!BUFFERP (echo_buffer[i])
6206 || NILP (XBUFFER (echo_buffer[i])->name))
6208 char name[30];
6209 Lisp_Object old_buffer;
6210 int j;
6212 old_buffer = echo_buffer[i];
6213 sprintf (name, " *Echo Area %d*", i);
6214 echo_buffer[i] = Fget_buffer_create (build_string (name));
6215 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6217 for (j = 0; j < 2; ++j)
6218 if (EQ (old_buffer, echo_area_buffer[j]))
6219 echo_area_buffer[j] = echo_buffer[i];
6224 /* Call FN with args A1..A4 with either the current or last displayed
6225 echo_area_buffer as current buffer.
6227 WHICH zero means use the current message buffer
6228 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6229 from echo_buffer[] and clear it.
6231 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6232 suitable buffer from echo_buffer[] and clear it.
6234 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6235 that the current message becomes the last displayed one, make
6236 choose a suitable buffer for echo_area_buffer[0], and clear it.
6238 Value is what FN returns. */
6240 static int
6241 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6242 struct window *w;
6243 int which;
6244 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6245 EMACS_INT a1;
6246 Lisp_Object a2;
6247 EMACS_INT a3, a4;
6249 Lisp_Object buffer;
6250 int this_one, the_other, clear_buffer_p, rc;
6251 int count = BINDING_STACK_SIZE ();
6253 /* If buffers aren't life, make new ones. */
6254 ensure_echo_area_buffers ();
6256 clear_buffer_p = 0;
6258 if (which == 0)
6259 this_one = 0, the_other = 1;
6260 else if (which > 0)
6261 this_one = 1, the_other = 0;
6262 else
6264 this_one = 0, the_other = 1;
6265 clear_buffer_p = 1;
6267 /* We need a fresh one in case the current echo buffer equals
6268 the one containing the last displayed echo area message. */
6269 if (!NILP (echo_area_buffer[this_one])
6270 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6271 echo_area_buffer[this_one] = Qnil;
6274 /* Choose a suitable buffer from echo_buffer[] is we don't
6275 have one. */
6276 if (NILP (echo_area_buffer[this_one]))
6278 echo_area_buffer[this_one]
6279 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6280 ? echo_buffer[the_other]
6281 : echo_buffer[this_one]);
6282 clear_buffer_p = 1;
6285 buffer = echo_area_buffer[this_one];
6287 /* Don't get confused by reusing the buffer used for echoing
6288 for a different purpose. */
6289 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6290 cancel_echoing ();
6292 record_unwind_protect (unwind_with_echo_area_buffer,
6293 with_echo_area_buffer_unwind_data (w));
6295 /* Make the echo area buffer current. Note that for display
6296 purposes, it is not necessary that the displayed window's buffer
6297 == current_buffer, except for text property lookup. So, let's
6298 only set that buffer temporarily here without doing a full
6299 Fset_window_buffer. We must also change w->pointm, though,
6300 because otherwise an assertions in unshow_buffer fails, and Emacs
6301 aborts. */
6302 set_buffer_internal_1 (XBUFFER (buffer));
6303 if (w)
6305 w->buffer = buffer;
6306 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6309 current_buffer->undo_list = Qt;
6310 current_buffer->read_only = Qnil;
6311 specbind (Qinhibit_read_only, Qt);
6312 specbind (Qinhibit_modification_hooks, Qt);
6314 if (clear_buffer_p && Z > BEG)
6315 del_range (BEG, Z);
6317 xassert (BEGV >= BEG);
6318 xassert (ZV <= Z && ZV >= BEGV);
6320 rc = fn (a1, a2, a3, a4);
6322 xassert (BEGV >= BEG);
6323 xassert (ZV <= Z && ZV >= BEGV);
6325 unbind_to (count, Qnil);
6326 return rc;
6330 /* Save state that should be preserved around the call to the function
6331 FN called in with_echo_area_buffer. */
6333 static Lisp_Object
6334 with_echo_area_buffer_unwind_data (w)
6335 struct window *w;
6337 int i = 0;
6338 Lisp_Object vector;
6340 /* Reduce consing by keeping one vector in
6341 Vwith_echo_area_save_vector. */
6342 vector = Vwith_echo_area_save_vector;
6343 Vwith_echo_area_save_vector = Qnil;
6345 if (NILP (vector))
6346 vector = Fmake_vector (make_number (7), Qnil);
6348 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6349 AREF (vector, i) = Vdeactivate_mark, ++i;
6350 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6352 if (w)
6354 XSETWINDOW (AREF (vector, i), w); ++i;
6355 AREF (vector, i) = w->buffer; ++i;
6356 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6357 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6359 else
6361 int end = i + 4;
6362 for (; i < end; ++i)
6363 AREF (vector, i) = Qnil;
6366 xassert (i == ASIZE (vector));
6367 return vector;
6371 /* Restore global state from VECTOR which was created by
6372 with_echo_area_buffer_unwind_data. */
6374 static Lisp_Object
6375 unwind_with_echo_area_buffer (vector)
6376 Lisp_Object vector;
6378 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6379 Vdeactivate_mark = AREF (vector, 1);
6380 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6382 if (WINDOWP (AREF (vector, 3)))
6384 struct window *w;
6385 Lisp_Object buffer, charpos, bytepos;
6387 w = XWINDOW (AREF (vector, 3));
6388 buffer = AREF (vector, 4);
6389 charpos = AREF (vector, 5);
6390 bytepos = AREF (vector, 6);
6392 w->buffer = buffer;
6393 set_marker_both (w->pointm, buffer,
6394 XFASTINT (charpos), XFASTINT (bytepos));
6397 Vwith_echo_area_save_vector = vector;
6398 return Qnil;
6402 /* Set up the echo area for use by print functions. MULTIBYTE_P
6403 non-zero means we will print multibyte. */
6405 void
6406 setup_echo_area_for_printing (multibyte_p)
6407 int multibyte_p;
6409 ensure_echo_area_buffers ();
6411 if (!message_buf_print)
6413 /* A message has been output since the last time we printed.
6414 Choose a fresh echo area buffer. */
6415 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6416 echo_area_buffer[0] = echo_buffer[1];
6417 else
6418 echo_area_buffer[0] = echo_buffer[0];
6420 /* Switch to that buffer and clear it. */
6421 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6422 current_buffer->truncate_lines = Qnil;
6424 if (Z > BEG)
6426 int count = BINDING_STACK_SIZE ();
6427 specbind (Qinhibit_read_only, Qt);
6428 del_range (BEG, Z);
6429 unbind_to (count, Qnil);
6431 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6433 /* Set up the buffer for the multibyteness we need. */
6434 if (multibyte_p
6435 != !NILP (current_buffer->enable_multibyte_characters))
6436 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6438 /* Raise the frame containing the echo area. */
6439 if (minibuffer_auto_raise)
6441 struct frame *sf = SELECTED_FRAME ();
6442 Lisp_Object mini_window;
6443 mini_window = FRAME_MINIBUF_WINDOW (sf);
6444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6447 message_log_maybe_newline ();
6448 message_buf_print = 1;
6450 else
6452 if (NILP (echo_area_buffer[0]))
6454 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6455 echo_area_buffer[0] = echo_buffer[1];
6456 else
6457 echo_area_buffer[0] = echo_buffer[0];
6460 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6462 /* Someone switched buffers between print requests. */
6463 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6464 current_buffer->truncate_lines = Qnil;
6470 /* Display an echo area message in window W. Value is non-zero if W's
6471 height is changed. If display_last_displayed_message_p is
6472 non-zero, display the message that was last displayed, otherwise
6473 display the current message. */
6475 static int
6476 display_echo_area (w)
6477 struct window *w;
6479 int i, no_message_p, window_height_changed_p, count;
6481 /* Temporarily disable garbage collections while displaying the echo
6482 area. This is done because a GC can print a message itself.
6483 That message would modify the echo area buffer's contents while a
6484 redisplay of the buffer is going on, and seriously confuse
6485 redisplay. */
6486 count = inhibit_garbage_collection ();
6488 /* If there is no message, we must call display_echo_area_1
6489 nevertheless because it resizes the window. But we will have to
6490 reset the echo_area_buffer in question to nil at the end because
6491 with_echo_area_buffer will sets it to an empty buffer. */
6492 i = display_last_displayed_message_p ? 1 : 0;
6493 no_message_p = NILP (echo_area_buffer[i]);
6495 window_height_changed_p
6496 = with_echo_area_buffer (w, display_last_displayed_message_p,
6497 display_echo_area_1,
6498 (EMACS_INT) w, Qnil, 0, 0);
6500 if (no_message_p)
6501 echo_area_buffer[i] = Qnil;
6503 unbind_to (count, Qnil);
6504 return window_height_changed_p;
6508 /* Helper for display_echo_area. Display the current buffer which
6509 contains the current echo area message in window W, a mini-window,
6510 a pointer to which is passed in A1. A2..A4 are currently not used.
6511 Change the height of W so that all of the message is displayed.
6512 Value is non-zero if height of W was changed. */
6514 static int
6515 display_echo_area_1 (a1, a2, a3, a4)
6516 EMACS_INT a1;
6517 Lisp_Object a2;
6518 EMACS_INT a3, a4;
6520 struct window *w = (struct window *) a1;
6521 Lisp_Object window;
6522 struct text_pos start;
6523 int window_height_changed_p = 0;
6525 /* Do this before displaying, so that we have a large enough glyph
6526 matrix for the display. */
6527 window_height_changed_p = resize_mini_window (w, 0);
6529 /* Display. */
6530 clear_glyph_matrix (w->desired_matrix);
6531 XSETWINDOW (window, w);
6532 SET_TEXT_POS (start, BEG, BEG_BYTE);
6533 try_window (window, start);
6535 return window_height_changed_p;
6539 /* Resize the echo area window to exactly the size needed for the
6540 currently displayed message, if there is one. If a mini-buffer
6541 is active, don't shrink it. */
6543 void
6544 resize_echo_area_exactly ()
6546 if (BUFFERP (echo_area_buffer[0])
6547 && WINDOWP (echo_area_window))
6549 struct window *w = XWINDOW (echo_area_window);
6550 int resized_p;
6551 Lisp_Object resize_exactly;
6553 if (minibuf_level == 0)
6554 resize_exactly = Qt;
6555 else
6556 resize_exactly = Qnil;
6558 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6559 (EMACS_INT) w, resize_exactly, 0, 0);
6560 if (resized_p)
6562 ++windows_or_buffers_changed;
6563 ++update_mode_lines;
6564 redisplay_internal (0);
6570 /* Callback function for with_echo_area_buffer, when used from
6571 resize_echo_area_exactly. A1 contains a pointer to the window to
6572 resize, EXACTLY non-nil means resize the mini-window exactly to the
6573 size of the text displayed. A3 and A4 are not used. Value is what
6574 resize_mini_window returns. */
6576 static int
6577 resize_mini_window_1 (a1, exactly, a3, a4)
6578 EMACS_INT a1;
6579 Lisp_Object exactly;
6580 EMACS_INT a3, a4;
6582 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6586 /* Resize mini-window W to fit the size of its contents. EXACT:P
6587 means size the window exactly to the size needed. Otherwise, it's
6588 only enlarged until W's buffer is empty. Value is non-zero if
6589 the window height has been changed. */
6592 resize_mini_window (w, exact_p)
6593 struct window *w;
6594 int exact_p;
6596 struct frame *f = XFRAME (w->frame);
6597 int window_height_changed_p = 0;
6599 xassert (MINI_WINDOW_P (w));
6601 /* Don't resize windows while redisplaying a window; it would
6602 confuse redisplay functions when the size of the window they are
6603 displaying changes from under them. Such a resizing can happen,
6604 for instance, when which-func prints a long message while
6605 we are running fontification-functions. We're running these
6606 functions with safe_call which binds inhibit-redisplay to t. */
6607 if (!NILP (Vinhibit_redisplay))
6608 return 0;
6610 /* Nil means don't try to resize. */
6611 if (NILP (Vresize_mini_windows)
6612 || (FRAME_X_P (f) && f->output_data.x == NULL))
6613 return 0;
6615 if (!FRAME_MINIBUF_ONLY_P (f))
6617 struct it it;
6618 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6619 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6620 int height, max_height;
6621 int unit = CANON_Y_UNIT (f);
6622 struct text_pos start;
6623 struct buffer *old_current_buffer = NULL;
6625 if (current_buffer != XBUFFER (w->buffer))
6627 old_current_buffer = current_buffer;
6628 set_buffer_internal (XBUFFER (w->buffer));
6631 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6633 /* Compute the max. number of lines specified by the user. */
6634 if (FLOATP (Vmax_mini_window_height))
6635 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6636 else if (INTEGERP (Vmax_mini_window_height))
6637 max_height = XINT (Vmax_mini_window_height);
6638 else
6639 max_height = total_height / 4;
6641 /* Correct that max. height if it's bogus. */
6642 max_height = max (1, max_height);
6643 max_height = min (total_height, max_height);
6645 /* Find out the height of the text in the window. */
6646 if (it.truncate_lines_p)
6647 height = 1;
6648 else
6650 last_height = 0;
6651 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6652 if (it.max_ascent == 0 && it.max_descent == 0)
6653 height = it.current_y + last_height;
6654 else
6655 height = it.current_y + it.max_ascent + it.max_descent;
6656 height -= it.extra_line_spacing;
6657 height = (height + unit - 1) / unit;
6660 /* Compute a suitable window start. */
6661 if (height > max_height)
6663 height = max_height;
6664 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6665 move_it_vertically_backward (&it, (height - 1) * unit);
6666 start = it.current.pos;
6668 else
6669 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6670 SET_MARKER_FROM_TEXT_POS (w->start, start);
6672 if (EQ (Vresize_mini_windows, Qgrow_only))
6674 /* Let it grow only, until we display an empty message, in which
6675 case the window shrinks again. */
6676 if (height > XFASTINT (w->height))
6678 int old_height = XFASTINT (w->height);
6679 freeze_window_starts (f, 1);
6680 grow_mini_window (w, height - XFASTINT (w->height));
6681 window_height_changed_p = XFASTINT (w->height) != old_height;
6683 else if (height < XFASTINT (w->height)
6684 && (exact_p || BEGV == ZV))
6686 int old_height = XFASTINT (w->height);
6687 freeze_window_starts (f, 0);
6688 shrink_mini_window (w);
6689 window_height_changed_p = XFASTINT (w->height) != old_height;
6692 else
6694 /* Always resize to exact size needed. */
6695 if (height > XFASTINT (w->height))
6697 int old_height = XFASTINT (w->height);
6698 freeze_window_starts (f, 1);
6699 grow_mini_window (w, height - XFASTINT (w->height));
6700 window_height_changed_p = XFASTINT (w->height) != old_height;
6702 else if (height < XFASTINT (w->height))
6704 int old_height = XFASTINT (w->height);
6705 freeze_window_starts (f, 0);
6706 shrink_mini_window (w);
6708 if (height)
6710 freeze_window_starts (f, 1);
6711 grow_mini_window (w, height - XFASTINT (w->height));
6714 window_height_changed_p = XFASTINT (w->height) != old_height;
6718 if (old_current_buffer)
6719 set_buffer_internal (old_current_buffer);
6722 return window_height_changed_p;
6726 /* Value is the current message, a string, or nil if there is no
6727 current message. */
6729 Lisp_Object
6730 current_message ()
6732 Lisp_Object msg;
6734 if (NILP (echo_area_buffer[0]))
6735 msg = Qnil;
6736 else
6738 with_echo_area_buffer (0, 0, current_message_1,
6739 (EMACS_INT) &msg, Qnil, 0, 0);
6740 if (NILP (msg))
6741 echo_area_buffer[0] = Qnil;
6744 return msg;
6748 static int
6749 current_message_1 (a1, a2, a3, a4)
6750 EMACS_INT a1;
6751 Lisp_Object a2;
6752 EMACS_INT a3, a4;
6754 Lisp_Object *msg = (Lisp_Object *) a1;
6756 if (Z > BEG)
6757 *msg = make_buffer_string (BEG, Z, 1);
6758 else
6759 *msg = Qnil;
6760 return 0;
6764 /* Push the current message on Vmessage_stack for later restauration
6765 by restore_message. Value is non-zero if the current message isn't
6766 empty. This is a relatively infrequent operation, so it's not
6767 worth optimizing. */
6770 push_message ()
6772 Lisp_Object msg;
6773 msg = current_message ();
6774 Vmessage_stack = Fcons (msg, Vmessage_stack);
6775 return STRINGP (msg);
6779 /* Handler for record_unwind_protect calling pop_message. */
6781 Lisp_Object
6782 push_message_unwind (dummy)
6783 Lisp_Object dummy;
6785 pop_message ();
6786 return Qnil;
6790 /* Restore message display from the top of Vmessage_stack. */
6792 void
6793 restore_message ()
6795 Lisp_Object msg;
6797 xassert (CONSP (Vmessage_stack));
6798 msg = XCAR (Vmessage_stack);
6799 if (STRINGP (msg))
6800 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6801 else
6802 message3_nolog (msg, 0, 0);
6806 /* Pop the top-most entry off Vmessage_stack. */
6808 void
6809 pop_message ()
6811 xassert (CONSP (Vmessage_stack));
6812 Vmessage_stack = XCDR (Vmessage_stack);
6816 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6817 exits. If the stack is not empty, we have a missing pop_message
6818 somewhere. */
6820 void
6821 check_message_stack ()
6823 if (!NILP (Vmessage_stack))
6824 abort ();
6828 /* Truncate to NCHARS what will be displayed in the echo area the next
6829 time we display it---but don't redisplay it now. */
6831 void
6832 truncate_echo_area (nchars)
6833 int nchars;
6835 if (nchars == 0)
6836 echo_area_buffer[0] = Qnil;
6837 /* A null message buffer means that the frame hasn't really been
6838 initialized yet. Error messages get reported properly by
6839 cmd_error, so this must be just an informative message; toss it. */
6840 else if (!noninteractive
6841 && INTERACTIVE
6842 && !NILP (echo_area_buffer[0]))
6844 struct frame *sf = SELECTED_FRAME ();
6845 if (FRAME_MESSAGE_BUF (sf))
6846 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6851 /* Helper function for truncate_echo_area. Truncate the current
6852 message to at most NCHARS characters. */
6854 static int
6855 truncate_message_1 (nchars, a2, a3, a4)
6856 EMACS_INT nchars;
6857 Lisp_Object a2;
6858 EMACS_INT a3, a4;
6860 if (BEG + nchars < Z)
6861 del_range (BEG + nchars, Z);
6862 if (Z == BEG)
6863 echo_area_buffer[0] = Qnil;
6864 return 0;
6868 /* Set the current message to a substring of S or STRING.
6870 If STRING is a Lisp string, set the message to the first NBYTES
6871 bytes from STRING. NBYTES zero means use the whole string. If
6872 STRING is multibyte, the message will be displayed multibyte.
6874 If S is not null, set the message to the first LEN bytes of S. LEN
6875 zero means use the whole string. MULTIBYTE_P non-zero means S is
6876 multibyte. Display the message multibyte in that case. */
6878 void
6879 set_message (s, string, nbytes, multibyte_p)
6880 char *s;
6881 Lisp_Object string;
6882 int nbytes;
6884 message_enable_multibyte
6885 = ((s && multibyte_p)
6886 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6888 with_echo_area_buffer (0, -1, set_message_1,
6889 (EMACS_INT) s, string, nbytes, multibyte_p);
6890 message_buf_print = 0;
6891 help_echo_showing_p = 0;
6895 /* Helper function for set_message. Arguments have the same meaning
6896 as there, with A1 corresponding to S and A2 corresponding to STRING
6897 This function is called with the echo area buffer being
6898 current. */
6900 static int
6901 set_message_1 (a1, a2, nbytes, multibyte_p)
6902 EMACS_INT a1;
6903 Lisp_Object a2;
6904 EMACS_INT nbytes, multibyte_p;
6906 char *s = (char *) a1;
6907 Lisp_Object string = a2;
6909 xassert (BEG == Z);
6911 /* Change multibyteness of the echo buffer appropriately. */
6912 if (message_enable_multibyte
6913 != !NILP (current_buffer->enable_multibyte_characters))
6914 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6916 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6918 /* Insert new message at BEG. */
6919 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6921 if (STRINGP (string))
6923 int nchars;
6925 if (nbytes == 0)
6926 nbytes = XSTRING (string)->size_byte;
6927 nchars = string_byte_to_char (string, nbytes);
6929 /* This function takes care of single/multibyte conversion. We
6930 just have to ensure that the echo area buffer has the right
6931 setting of enable_multibyte_characters. */
6932 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6934 else if (s)
6936 if (nbytes == 0)
6937 nbytes = strlen (s);
6939 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6941 /* Convert from multi-byte to single-byte. */
6942 int i, c, n;
6943 unsigned char work[1];
6945 /* Convert a multibyte string to single-byte. */
6946 for (i = 0; i < nbytes; i += n)
6948 c = string_char_and_length (s + i, nbytes - i, &n);
6949 work[0] = (SINGLE_BYTE_CHAR_P (c)
6951 : multibyte_char_to_unibyte (c, Qnil));
6952 insert_1_both (work, 1, 1, 1, 0, 0);
6955 else if (!multibyte_p
6956 && !NILP (current_buffer->enable_multibyte_characters))
6958 /* Convert from single-byte to multi-byte. */
6959 int i, c, n;
6960 unsigned char *msg = (unsigned char *) s;
6961 unsigned char str[MAX_MULTIBYTE_LENGTH];
6963 /* Convert a single-byte string to multibyte. */
6964 for (i = 0; i < nbytes; i++)
6966 c = unibyte_char_to_multibyte (msg[i]);
6967 n = CHAR_STRING (c, str);
6968 insert_1_both (str, 1, n, 1, 0, 0);
6971 else
6972 insert_1 (s, nbytes, 1, 0, 0);
6975 return 0;
6979 /* Clear messages. CURRENT_P non-zero means clear the current
6980 message. LAST_DISPLAYED_P non-zero means clear the message
6981 last displayed. */
6983 void
6984 clear_message (current_p, last_displayed_p)
6985 int current_p, last_displayed_p;
6987 if (current_p)
6989 echo_area_buffer[0] = Qnil;
6990 message_cleared_p = 1;
6993 if (last_displayed_p)
6994 echo_area_buffer[1] = Qnil;
6996 message_buf_print = 0;
6999 /* Clear garbaged frames.
7001 This function is used where the old redisplay called
7002 redraw_garbaged_frames which in turn called redraw_frame which in
7003 turn called clear_frame. The call to clear_frame was a source of
7004 flickering. I believe a clear_frame is not necessary. It should
7005 suffice in the new redisplay to invalidate all current matrices,
7006 and ensure a complete redisplay of all windows. */
7008 static void
7009 clear_garbaged_frames ()
7011 if (frame_garbaged)
7013 Lisp_Object tail, frame;
7015 FOR_EACH_FRAME (tail, frame)
7017 struct frame *f = XFRAME (frame);
7019 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7021 if (f->resized_p)
7022 Fredraw_frame (frame);
7023 clear_current_matrices (f);
7024 f->garbaged = 0;
7025 f->resized_p = 0;
7029 frame_garbaged = 0;
7030 ++windows_or_buffers_changed;
7035 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7036 is non-zero update selected_frame. Value is non-zero if the
7037 mini-windows height has been changed. */
7039 static int
7040 echo_area_display (update_frame_p)
7041 int update_frame_p;
7043 Lisp_Object mini_window;
7044 struct window *w;
7045 struct frame *f;
7046 int window_height_changed_p = 0;
7047 struct frame *sf = SELECTED_FRAME ();
7049 mini_window = FRAME_MINIBUF_WINDOW (sf);
7050 w = XWINDOW (mini_window);
7051 f = XFRAME (WINDOW_FRAME (w));
7053 /* Don't display if frame is invisible or not yet initialized. */
7054 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7055 return 0;
7057 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7058 #ifndef macintosh
7059 #ifdef HAVE_WINDOW_SYSTEM
7060 /* When Emacs starts, selected_frame may be a visible terminal
7061 frame, even if we run under a window system. If we let this
7062 through, a message would be displayed on the terminal. */
7063 if (EQ (selected_frame, Vterminal_frame)
7064 && !NILP (Vwindow_system))
7065 return 0;
7066 #endif /* HAVE_WINDOW_SYSTEM */
7067 #endif
7069 /* Redraw garbaged frames. */
7070 if (frame_garbaged)
7071 clear_garbaged_frames ();
7073 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7075 echo_area_window = mini_window;
7076 window_height_changed_p = display_echo_area (w);
7077 w->must_be_updated_p = 1;
7079 /* Update the display, unless called from redisplay_internal.
7080 Also don't update the screen during redisplay itself. The
7081 update will happen at the end of redisplay, and an update
7082 here could cause confusion. */
7083 if (update_frame_p && !redisplaying_p)
7085 int n = 0;
7087 /* If the display update has been interrupted by pending
7088 input, update mode lines in the frame. Due to the
7089 pending input, it might have been that redisplay hasn't
7090 been called, so that mode lines above the echo area are
7091 garbaged. This looks odd, so we prevent it here. */
7092 if (!display_completed)
7093 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7095 if (window_height_changed_p
7096 /* Don't do this if Emacs is shutting down. Redisplay
7097 needs to run hooks. */
7098 && !NILP (Vrun_hooks))
7100 /* Must update other windows. Likewise as in other
7101 cases, don't let this update be interrupted by
7102 pending input. */
7103 int count = BINDING_STACK_SIZE ();
7104 specbind (Qredisplay_dont_pause, Qt);
7105 windows_or_buffers_changed = 1;
7106 redisplay_internal (0);
7107 unbind_to (count, Qnil);
7109 else if (FRAME_WINDOW_P (f) && n == 0)
7111 /* Window configuration is the same as before.
7112 Can do with a display update of the echo area,
7113 unless we displayed some mode lines. */
7114 update_single_window (w, 1);
7115 rif->flush_display (f);
7117 else
7118 update_frame (f, 1, 1);
7120 /* If cursor is in the echo area, make sure that the next
7121 redisplay displays the minibuffer, so that the cursor will
7122 be replaced with what the minibuffer wants. */
7123 if (cursor_in_echo_area)
7124 ++windows_or_buffers_changed;
7127 else if (!EQ (mini_window, selected_window))
7128 windows_or_buffers_changed++;
7130 /* Last displayed message is now the current message. */
7131 echo_area_buffer[1] = echo_area_buffer[0];
7133 /* Prevent redisplay optimization in redisplay_internal by resetting
7134 this_line_start_pos. This is done because the mini-buffer now
7135 displays the message instead of its buffer text. */
7136 if (EQ (mini_window, selected_window))
7137 CHARPOS (this_line_start_pos) = 0;
7139 return window_height_changed_p;
7144 /***********************************************************************
7145 Frame Titles
7146 ***********************************************************************/
7149 #ifdef HAVE_WINDOW_SYSTEM
7151 /* A buffer for constructing frame titles in it; allocated from the
7152 heap in init_xdisp and resized as needed in store_frame_title_char. */
7154 static char *frame_title_buf;
7156 /* The buffer's end, and a current output position in it. */
7158 static char *frame_title_buf_end;
7159 static char *frame_title_ptr;
7162 /* Store a single character C for the frame title in frame_title_buf.
7163 Re-allocate frame_title_buf if necessary. */
7165 static void
7166 store_frame_title_char (c)
7167 char c;
7169 /* If output position has reached the end of the allocated buffer,
7170 double the buffer's size. */
7171 if (frame_title_ptr == frame_title_buf_end)
7173 int len = frame_title_ptr - frame_title_buf;
7174 int new_size = 2 * len * sizeof *frame_title_buf;
7175 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7176 frame_title_buf_end = frame_title_buf + new_size;
7177 frame_title_ptr = frame_title_buf + len;
7180 *frame_title_ptr++ = c;
7184 /* Store part of a frame title in frame_title_buf, beginning at
7185 frame_title_ptr. STR is the string to store. Do not copy
7186 characters that yield more columns than PRECISION; PRECISION <= 0
7187 means copy the whole string. Pad with spaces until FIELD_WIDTH
7188 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7189 pad. Called from display_mode_element when it is used to build a
7190 frame title. */
7192 static int
7193 store_frame_title (str, field_width, precision)
7194 unsigned char *str;
7195 int field_width, precision;
7197 int n = 0;
7198 int dummy, nbytes;
7200 /* Copy at most PRECISION chars from STR. */
7201 nbytes = strlen (str);
7202 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7203 while (nbytes--)
7204 store_frame_title_char (*str++);
7206 /* Fill up with spaces until FIELD_WIDTH reached. */
7207 while (field_width > 0
7208 && n < field_width)
7210 store_frame_title_char (' ');
7211 ++n;
7214 return n;
7218 /* Set the title of FRAME, if it has changed. The title format is
7219 Vicon_title_format if FRAME is iconified, otherwise it is
7220 frame_title_format. */
7222 static void
7223 x_consider_frame_title (frame)
7224 Lisp_Object frame;
7226 struct frame *f = XFRAME (frame);
7228 if (FRAME_WINDOW_P (f)
7229 || FRAME_MINIBUF_ONLY_P (f)
7230 || f->explicit_name)
7232 /* Do we have more than one visible frame on this X display? */
7233 Lisp_Object tail;
7234 Lisp_Object fmt;
7235 struct buffer *obuf;
7236 int len;
7237 struct it it;
7239 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7241 Lisp_Object other_frame = XCAR (tail);
7242 struct frame *tf = XFRAME (other_frame);
7244 if (tf != f
7245 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7246 && !FRAME_MINIBUF_ONLY_P (tf)
7247 && !EQ (other_frame, tip_frame)
7248 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7249 break;
7252 /* Set global variable indicating that multiple frames exist. */
7253 multiple_frames = CONSP (tail);
7255 /* Switch to the buffer of selected window of the frame. Set up
7256 frame_title_ptr so that display_mode_element will output into it;
7257 then display the title. */
7258 obuf = current_buffer;
7259 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7260 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7261 frame_title_ptr = frame_title_buf;
7262 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7263 NULL, DEFAULT_FACE_ID);
7264 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
7265 len = frame_title_ptr - frame_title_buf;
7266 frame_title_ptr = NULL;
7267 set_buffer_internal_1 (obuf);
7269 /* Set the title only if it's changed. This avoids consing in
7270 the common case where it hasn't. (If it turns out that we've
7271 already wasted too much time by walking through the list with
7272 display_mode_element, then we might need to optimize at a
7273 higher level than this.) */
7274 if (! STRINGP (f->name)
7275 || STRING_BYTES (XSTRING (f->name)) != len
7276 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7277 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7281 #else /* not HAVE_WINDOW_SYSTEM */
7283 #define frame_title_ptr ((char *)0)
7284 #define store_frame_title(str, mincol, maxcol) 0
7286 #endif /* not HAVE_WINDOW_SYSTEM */
7291 /***********************************************************************
7292 Menu Bars
7293 ***********************************************************************/
7296 /* Prepare for redisplay by updating menu-bar item lists when
7297 appropriate. This can call eval. */
7299 void
7300 prepare_menu_bars ()
7302 int all_windows;
7303 struct gcpro gcpro1, gcpro2;
7304 struct frame *f;
7305 Lisp_Object tooltip_frame;
7307 #ifdef HAVE_WINDOW_SYSTEM
7308 tooltip_frame = tip_frame;
7309 #else
7310 tooltip_frame = Qnil;
7311 #endif
7313 /* Update all frame titles based on their buffer names, etc. We do
7314 this before the menu bars so that the buffer-menu will show the
7315 up-to-date frame titles. */
7316 #ifdef HAVE_WINDOW_SYSTEM
7317 if (windows_or_buffers_changed || update_mode_lines)
7319 Lisp_Object tail, frame;
7321 FOR_EACH_FRAME (tail, frame)
7323 f = XFRAME (frame);
7324 if (!EQ (frame, tooltip_frame)
7325 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7326 x_consider_frame_title (frame);
7329 #endif /* HAVE_WINDOW_SYSTEM */
7331 /* Update the menu bar item lists, if appropriate. This has to be
7332 done before any actual redisplay or generation of display lines. */
7333 all_windows = (update_mode_lines
7334 || buffer_shared > 1
7335 || windows_or_buffers_changed);
7336 if (all_windows)
7338 Lisp_Object tail, frame;
7339 int count = BINDING_STACK_SIZE ();
7341 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7343 FOR_EACH_FRAME (tail, frame)
7345 f = XFRAME (frame);
7347 /* Ignore tooltip frame. */
7348 if (EQ (frame, tooltip_frame))
7349 continue;
7351 /* If a window on this frame changed size, report that to
7352 the user and clear the size-change flag. */
7353 if (FRAME_WINDOW_SIZES_CHANGED (f))
7355 Lisp_Object functions;
7357 /* Clear flag first in case we get an error below. */
7358 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7359 functions = Vwindow_size_change_functions;
7360 GCPRO2 (tail, functions);
7362 while (CONSP (functions))
7364 call1 (XCAR (functions), frame);
7365 functions = XCDR (functions);
7367 UNGCPRO;
7370 GCPRO1 (tail);
7371 update_menu_bar (f, 0);
7372 #ifdef HAVE_WINDOW_SYSTEM
7373 update_tool_bar (f, 0);
7374 #endif
7375 UNGCPRO;
7378 unbind_to (count, Qnil);
7380 else
7382 struct frame *sf = SELECTED_FRAME ();
7383 update_menu_bar (sf, 1);
7384 #ifdef HAVE_WINDOW_SYSTEM
7385 update_tool_bar (sf, 1);
7386 #endif
7389 /* Motif needs this. See comment in xmenu.c. Turn it off when
7390 pending_menu_activation is not defined. */
7391 #ifdef USE_X_TOOLKIT
7392 pending_menu_activation = 0;
7393 #endif
7397 /* Update the menu bar item list for frame F. This has to be done
7398 before we start to fill in any display lines, because it can call
7399 eval.
7401 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7403 static void
7404 update_menu_bar (f, save_match_data)
7405 struct frame *f;
7406 int save_match_data;
7408 Lisp_Object window;
7409 register struct window *w;
7411 /* If called recursively during a menu update, do nothing. This can
7412 happen when, for instance, an activate-menubar-hook causes a
7413 redisplay. */
7414 if (inhibit_menubar_update)
7415 return;
7417 window = FRAME_SELECTED_WINDOW (f);
7418 w = XWINDOW (window);
7420 if (update_mode_lines)
7421 w->update_mode_line = Qt;
7423 if (FRAME_WINDOW_P (f)
7425 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7426 FRAME_EXTERNAL_MENU_BAR (f)
7427 #else
7428 FRAME_MENU_BAR_LINES (f) > 0
7429 #endif
7430 : FRAME_MENU_BAR_LINES (f) > 0)
7432 /* If the user has switched buffers or windows, we need to
7433 recompute to reflect the new bindings. But we'll
7434 recompute when update_mode_lines is set too; that means
7435 that people can use force-mode-line-update to request
7436 that the menu bar be recomputed. The adverse effect on
7437 the rest of the redisplay algorithm is about the same as
7438 windows_or_buffers_changed anyway. */
7439 if (windows_or_buffers_changed
7440 || !NILP (w->update_mode_line)
7441 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7442 < BUF_MODIFF (XBUFFER (w->buffer)))
7443 != !NILP (w->last_had_star))
7444 || ((!NILP (Vtransient_mark_mode)
7445 && !NILP (XBUFFER (w->buffer)->mark_active))
7446 != !NILP (w->region_showing)))
7448 struct buffer *prev = current_buffer;
7449 int count = BINDING_STACK_SIZE ();
7451 specbind (Qinhibit_menubar_update, Qt);
7453 set_buffer_internal_1 (XBUFFER (w->buffer));
7454 if (save_match_data)
7455 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7456 if (NILP (Voverriding_local_map_menu_flag))
7458 specbind (Qoverriding_terminal_local_map, Qnil);
7459 specbind (Qoverriding_local_map, Qnil);
7462 /* Run the Lucid hook. */
7463 safe_run_hooks (Qactivate_menubar_hook);
7465 /* If it has changed current-menubar from previous value,
7466 really recompute the menu-bar from the value. */
7467 if (! NILP (Vlucid_menu_bar_dirty_flag))
7468 call0 (Qrecompute_lucid_menubar);
7470 safe_run_hooks (Qmenu_bar_update_hook);
7471 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7473 /* Redisplay the menu bar in case we changed it. */
7474 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7475 if (FRAME_WINDOW_P (f)
7476 #if defined (macintosh)
7477 /* All frames on Mac OS share the same menubar. So only the
7478 selected frame should be allowed to set it. */
7479 && f == SELECTED_FRAME ()
7480 #endif
7482 set_frame_menubar (f, 0, 0);
7483 else
7484 /* On a terminal screen, the menu bar is an ordinary screen
7485 line, and this makes it get updated. */
7486 w->update_mode_line = Qt;
7487 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7488 /* In the non-toolkit version, the menu bar is an ordinary screen
7489 line, and this makes it get updated. */
7490 w->update_mode_line = Qt;
7491 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7493 unbind_to (count, Qnil);
7494 set_buffer_internal_1 (prev);
7501 /***********************************************************************
7502 Tool-bars
7503 ***********************************************************************/
7505 #ifdef HAVE_WINDOW_SYSTEM
7507 /* Update the tool-bar item list for frame F. This has to be done
7508 before we start to fill in any display lines. Called from
7509 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7510 and restore it here. */
7512 static void
7513 update_tool_bar (f, save_match_data)
7514 struct frame *f;
7515 int save_match_data;
7517 if (WINDOWP (f->tool_bar_window)
7518 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7520 Lisp_Object window;
7521 struct window *w;
7523 window = FRAME_SELECTED_WINDOW (f);
7524 w = XWINDOW (window);
7526 /* If the user has switched buffers or windows, we need to
7527 recompute to reflect the new bindings. But we'll
7528 recompute when update_mode_lines is set too; that means
7529 that people can use force-mode-line-update to request
7530 that the menu bar be recomputed. The adverse effect on
7531 the rest of the redisplay algorithm is about the same as
7532 windows_or_buffers_changed anyway. */
7533 if (windows_or_buffers_changed
7534 || !NILP (w->update_mode_line)
7535 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7536 < BUF_MODIFF (XBUFFER (w->buffer)))
7537 != !NILP (w->last_had_star))
7538 || ((!NILP (Vtransient_mark_mode)
7539 && !NILP (XBUFFER (w->buffer)->mark_active))
7540 != !NILP (w->region_showing)))
7542 struct buffer *prev = current_buffer;
7543 int count = BINDING_STACK_SIZE ();
7545 /* Set current_buffer to the buffer of the selected
7546 window of the frame, so that we get the right local
7547 keymaps. */
7548 set_buffer_internal_1 (XBUFFER (w->buffer));
7550 /* Save match data, if we must. */
7551 if (save_match_data)
7552 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7554 /* Make sure that we don't accidentally use bogus keymaps. */
7555 if (NILP (Voverriding_local_map_menu_flag))
7557 specbind (Qoverriding_terminal_local_map, Qnil);
7558 specbind (Qoverriding_local_map, Qnil);
7561 /* Build desired tool-bar items from keymaps. */
7562 f->tool_bar_items
7563 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7565 /* Redisplay the tool-bar in case we changed it. */
7566 w->update_mode_line = Qt;
7568 unbind_to (count, Qnil);
7569 set_buffer_internal_1 (prev);
7575 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7576 F's desired tool-bar contents. F->tool_bar_items must have
7577 been set up previously by calling prepare_menu_bars. */
7579 static void
7580 build_desired_tool_bar_string (f)
7581 struct frame *f;
7583 int i, size, size_needed;
7584 struct gcpro gcpro1, gcpro2, gcpro3;
7585 Lisp_Object image, plist, props;
7587 image = plist = props = Qnil;
7588 GCPRO3 (image, plist, props);
7590 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7591 Otherwise, make a new string. */
7593 /* The size of the string we might be able to reuse. */
7594 size = (STRINGP (f->desired_tool_bar_string)
7595 ? XSTRING (f->desired_tool_bar_string)->size
7596 : 0);
7598 /* We need one space in the string for each image. */
7599 size_needed = f->n_tool_bar_items;
7601 /* Reuse f->desired_tool_bar_string, if possible. */
7602 if (size < size_needed || NILP (f->desired_tool_bar_string))
7603 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7604 make_number (' '));
7605 else
7607 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7608 Fremove_text_properties (make_number (0), make_number (size),
7609 props, f->desired_tool_bar_string);
7612 /* Put a `display' property on the string for the images to display,
7613 put a `menu_item' property on tool-bar items with a value that
7614 is the index of the item in F's tool-bar item vector. */
7615 for (i = 0; i < f->n_tool_bar_items; ++i)
7617 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7619 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7620 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7621 int hmargin, vmargin, relief, idx, end;
7622 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7624 /* If image is a vector, choose the image according to the
7625 button state. */
7626 image = PROP (TOOL_BAR_ITEM_IMAGES);
7627 if (VECTORP (image))
7629 if (enabled_p)
7630 idx = (selected_p
7631 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7632 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7633 else
7634 idx = (selected_p
7635 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7636 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7638 xassert (ASIZE (image) >= idx);
7639 image = AREF (image, idx);
7641 else
7642 idx = -1;
7644 /* Ignore invalid image specifications. */
7645 if (!valid_image_p (image))
7646 continue;
7648 /* Display the tool-bar button pressed, or depressed. */
7649 plist = Fcopy_sequence (XCDR (image));
7651 /* Compute margin and relief to draw. */
7652 relief = (tool_bar_button_relief >= 0
7653 ? tool_bar_button_relief
7654 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7655 hmargin = vmargin = relief;
7657 if (INTEGERP (Vtool_bar_button_margin)
7658 && XINT (Vtool_bar_button_margin) > 0)
7660 hmargin += XFASTINT (Vtool_bar_button_margin);
7661 vmargin += XFASTINT (Vtool_bar_button_margin);
7663 else if (CONSP (Vtool_bar_button_margin))
7665 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7666 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7667 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7669 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7670 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7671 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7674 if (auto_raise_tool_bar_buttons_p)
7676 /* Add a `:relief' property to the image spec if the item is
7677 selected. */
7678 if (selected_p)
7680 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7681 hmargin -= relief;
7682 vmargin -= relief;
7685 else
7687 /* If image is selected, display it pressed, i.e. with a
7688 negative relief. If it's not selected, display it with a
7689 raised relief. */
7690 plist = Fplist_put (plist, QCrelief,
7691 (selected_p
7692 ? make_number (-relief)
7693 : make_number (relief)));
7694 hmargin -= relief;
7695 vmargin -= relief;
7698 /* Put a margin around the image. */
7699 if (hmargin || vmargin)
7701 if (hmargin == vmargin)
7702 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7703 else
7704 plist = Fplist_put (plist, QCmargin,
7705 Fcons (make_number (hmargin),
7706 make_number (vmargin)));
7709 /* If button is not enabled, and we don't have special images
7710 for the disabled state, make the image appear disabled by
7711 applying an appropriate algorithm to it. */
7712 if (!enabled_p && idx < 0)
7713 plist = Fplist_put (plist, QCconversion, Qdisabled);
7715 /* Put a `display' text property on the string for the image to
7716 display. Put a `menu-item' property on the string that gives
7717 the start of this item's properties in the tool-bar items
7718 vector. */
7719 image = Fcons (Qimage, plist);
7720 props = list4 (Qdisplay, image,
7721 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7723 /* Let the last image hide all remaining spaces in the tool bar
7724 string. The string can be longer than needed when we reuse a
7725 previous string. */
7726 if (i + 1 == f->n_tool_bar_items)
7727 end = XSTRING (f->desired_tool_bar_string)->size;
7728 else
7729 end = i + 1;
7730 Fadd_text_properties (make_number (i), make_number (end),
7731 props, f->desired_tool_bar_string);
7732 #undef PROP
7735 UNGCPRO;
7739 /* Display one line of the tool-bar of frame IT->f. */
7741 static void
7742 display_tool_bar_line (it)
7743 struct it *it;
7745 struct glyph_row *row = it->glyph_row;
7746 int max_x = it->last_visible_x;
7747 struct glyph *last;
7749 prepare_desired_row (row);
7750 row->y = it->current_y;
7752 /* Note that this isn't made use of if the face hasn't a box,
7753 so there's no need to check the face here. */
7754 it->start_of_box_run_p = 1;
7756 while (it->current_x < max_x)
7758 int x_before, x, n_glyphs_before, i, nglyphs;
7760 /* Get the next display element. */
7761 if (!get_next_display_element (it))
7762 break;
7764 /* Produce glyphs. */
7765 x_before = it->current_x;
7766 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7767 PRODUCE_GLYPHS (it);
7769 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7770 i = 0;
7771 x = x_before;
7772 while (i < nglyphs)
7774 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7776 if (x + glyph->pixel_width > max_x)
7778 /* Glyph doesn't fit on line. */
7779 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7780 it->current_x = x;
7781 goto out;
7784 ++it->hpos;
7785 x += glyph->pixel_width;
7786 ++i;
7789 /* Stop at line ends. */
7790 if (ITERATOR_AT_END_OF_LINE_P (it))
7791 break;
7793 set_iterator_to_next (it, 1);
7796 out:;
7798 row->displays_text_p = row->used[TEXT_AREA] != 0;
7799 extend_face_to_end_of_line (it);
7800 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7801 last->right_box_line_p = 1;
7802 if (last == row->glyphs[TEXT_AREA])
7803 last->left_box_line_p = 1;
7804 compute_line_metrics (it);
7806 /* If line is empty, make it occupy the rest of the tool-bar. */
7807 if (!row->displays_text_p)
7809 row->height = row->phys_height = it->last_visible_y - row->y;
7810 row->ascent = row->phys_ascent = 0;
7813 row->full_width_p = 1;
7814 row->continued_p = 0;
7815 row->truncated_on_left_p = 0;
7816 row->truncated_on_right_p = 0;
7818 it->current_x = it->hpos = 0;
7819 it->current_y += row->height;
7820 ++it->vpos;
7821 ++it->glyph_row;
7825 /* Value is the number of screen lines needed to make all tool-bar
7826 items of frame F visible. */
7828 static int
7829 tool_bar_lines_needed (f)
7830 struct frame *f;
7832 struct window *w = XWINDOW (f->tool_bar_window);
7833 struct it it;
7835 /* Initialize an iterator for iteration over
7836 F->desired_tool_bar_string in the tool-bar window of frame F. */
7837 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7838 it.first_visible_x = 0;
7839 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7840 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7842 while (!ITERATOR_AT_END_P (&it))
7844 it.glyph_row = w->desired_matrix->rows;
7845 clear_glyph_row (it.glyph_row);
7846 display_tool_bar_line (&it);
7849 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7853 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7854 0, 1, 0,
7855 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7856 (frame)
7857 Lisp_Object frame;
7859 struct frame *f;
7860 struct window *w;
7861 int nlines = 0;
7863 if (NILP (frame))
7864 frame = selected_frame;
7865 else
7866 CHECK_FRAME (frame);
7867 f = XFRAME (frame);
7869 if (WINDOWP (f->tool_bar_window)
7870 || (w = XWINDOW (f->tool_bar_window),
7871 XFASTINT (w->height) > 0))
7873 update_tool_bar (f, 1);
7874 if (f->n_tool_bar_items)
7876 build_desired_tool_bar_string (f);
7877 nlines = tool_bar_lines_needed (f);
7881 return make_number (nlines);
7885 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7886 height should be changed. */
7888 static int
7889 redisplay_tool_bar (f)
7890 struct frame *f;
7892 struct window *w;
7893 struct it it;
7894 struct glyph_row *row;
7895 int change_height_p = 0;
7897 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7898 do anything. This means you must start with tool-bar-lines
7899 non-zero to get the auto-sizing effect. Or in other words, you
7900 can turn off tool-bars by specifying tool-bar-lines zero. */
7901 if (!WINDOWP (f->tool_bar_window)
7902 || (w = XWINDOW (f->tool_bar_window),
7903 XFASTINT (w->height) == 0))
7904 return 0;
7906 /* Set up an iterator for the tool-bar window. */
7907 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7908 it.first_visible_x = 0;
7909 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7910 row = it.glyph_row;
7912 /* Build a string that represents the contents of the tool-bar. */
7913 build_desired_tool_bar_string (f);
7914 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7916 /* Display as many lines as needed to display all tool-bar items. */
7917 while (it.current_y < it.last_visible_y)
7918 display_tool_bar_line (&it);
7920 /* It doesn't make much sense to try scrolling in the tool-bar
7921 window, so don't do it. */
7922 w->desired_matrix->no_scrolling_p = 1;
7923 w->must_be_updated_p = 1;
7925 if (auto_resize_tool_bars_p)
7927 int nlines;
7929 /* If we couldn't display everything, change the tool-bar's
7930 height. */
7931 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7932 change_height_p = 1;
7934 /* If there are blank lines at the end, except for a partially
7935 visible blank line at the end that is smaller than
7936 CANON_Y_UNIT, change the tool-bar's height. */
7937 row = it.glyph_row - 1;
7938 if (!row->displays_text_p
7939 && row->height >= CANON_Y_UNIT (f))
7940 change_height_p = 1;
7942 /* If row displays tool-bar items, but is partially visible,
7943 change the tool-bar's height. */
7944 if (row->displays_text_p
7945 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7946 change_height_p = 1;
7948 /* Resize windows as needed by changing the `tool-bar-lines'
7949 frame parameter. */
7950 if (change_height_p
7951 && (nlines = tool_bar_lines_needed (f),
7952 nlines != XFASTINT (w->height)))
7954 extern Lisp_Object Qtool_bar_lines;
7955 Lisp_Object frame;
7956 int old_height = XFASTINT (w->height);
7958 XSETFRAME (frame, f);
7959 clear_glyph_matrix (w->desired_matrix);
7960 Fmodify_frame_parameters (frame,
7961 Fcons (Fcons (Qtool_bar_lines,
7962 make_number (nlines)),
7963 Qnil));
7964 if (XFASTINT (w->height) != old_height)
7965 fonts_changed_p = 1;
7969 return change_height_p;
7973 /* Get information about the tool-bar item which is displayed in GLYPH
7974 on frame F. Return in *PROP_IDX the index where tool-bar item
7975 properties start in F->tool_bar_items. Value is zero if
7976 GLYPH doesn't display a tool-bar item. */
7979 tool_bar_item_info (f, glyph, prop_idx)
7980 struct frame *f;
7981 struct glyph *glyph;
7982 int *prop_idx;
7984 Lisp_Object prop;
7985 int success_p;
7986 int charpos;
7988 /* This function can be called asynchronously, which means we must
7989 exclude any possibility that Fget_text_property signals an
7990 error. */
7991 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7992 charpos = max (0, charpos);
7994 /* Get the text property `menu-item' at pos. The value of that
7995 property is the start index of this item's properties in
7996 F->tool_bar_items. */
7997 prop = Fget_text_property (make_number (charpos),
7998 Qmenu_item, f->current_tool_bar_string);
7999 if (INTEGERP (prop))
8001 *prop_idx = XINT (prop);
8002 success_p = 1;
8004 else
8005 success_p = 0;
8007 return success_p;
8010 #endif /* HAVE_WINDOW_SYSTEM */
8014 /************************************************************************
8015 Horizontal scrolling
8016 ************************************************************************/
8018 static int hscroll_window_tree P_ ((Lisp_Object));
8019 static int hscroll_windows P_ ((Lisp_Object));
8021 /* For all leaf windows in the window tree rooted at WINDOW, set their
8022 hscroll value so that PT is (i) visible in the window, and (ii) so
8023 that it is not within a certain margin at the window's left and
8024 right border. Value is non-zero if any window's hscroll has been
8025 changed. */
8027 static int
8028 hscroll_window_tree (window)
8029 Lisp_Object window;
8031 int hscrolled_p = 0;
8032 int hscroll_relative_p = FLOATP (Vautomatic_hscroll_step);
8033 int hscroll_step_abs = 0;
8034 double hscroll_step_rel = 0;
8036 if (hscroll_relative_p)
8038 hscroll_step_rel = XFLOAT_DATA (Vautomatic_hscroll_step);
8039 if (hscroll_step_rel < 0)
8041 hscroll_relative_p = 0;
8042 hscroll_step_abs = 0;
8045 else if (INTEGERP (Vautomatic_hscroll_step))
8047 hscroll_step_abs = XINT (Vautomatic_hscroll_step);
8048 if (hscroll_step_abs < 0)
8049 hscroll_step_abs = 0;
8051 else
8052 hscroll_step_abs = 0;
8054 while (WINDOWP (window))
8056 struct window *w = XWINDOW (window);
8058 if (WINDOWP (w->hchild))
8059 hscrolled_p |= hscroll_window_tree (w->hchild);
8060 else if (WINDOWP (w->vchild))
8061 hscrolled_p |= hscroll_window_tree (w->vchild);
8062 else if (w->cursor.vpos >= 0)
8064 int hscroll_margin, text_area_x, text_area_y;
8065 int text_area_width, text_area_height;
8066 struct glyph_row *current_cursor_row
8067 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8068 struct glyph_row *desired_cursor_row
8069 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8070 struct glyph_row *cursor_row
8071 = (desired_cursor_row->enabled_p
8072 ? desired_cursor_row
8073 : current_cursor_row);
8075 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8076 &text_area_width, &text_area_height);
8078 /* Scroll when cursor is inside this scroll margin. */
8079 hscroll_margin
8080 = automatic_hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8082 if ((XFASTINT (w->hscroll)
8083 && w->cursor.x <= hscroll_margin)
8084 || (cursor_row->enabled_p
8085 && cursor_row->truncated_on_right_p
8086 && (w->cursor.x >= text_area_width - hscroll_margin)))
8088 struct it it;
8089 int hscroll;
8090 struct buffer *saved_current_buffer;
8091 int pt;
8092 int wanted_x;
8094 /* Find point in a display of infinite width. */
8095 saved_current_buffer = current_buffer;
8096 current_buffer = XBUFFER (w->buffer);
8098 if (w == XWINDOW (selected_window))
8099 pt = BUF_PT (current_buffer);
8100 else
8102 pt = marker_position (w->pointm);
8103 pt = max (BEGV, pt);
8104 pt = min (ZV, pt);
8107 /* Move iterator to pt starting at cursor_row->start in
8108 a line with infinite width. */
8109 init_to_row_start (&it, w, cursor_row);
8110 it.last_visible_x = INFINITY;
8111 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8112 current_buffer = saved_current_buffer;
8114 /* Position cursor in window. */
8115 if (!hscroll_relative_p && hscroll_step_abs == 0)
8116 hscroll = max (0, it.current_x - text_area_width / 2)
8117 / CANON_X_UNIT (it.f);
8118 else if (w->cursor.x >= text_area_width - hscroll_margin)
8120 if (hscroll_relative_p)
8121 wanted_x = text_area_width * (1 - hscroll_step_rel)
8122 - hscroll_margin;
8123 else
8124 wanted_x = text_area_width
8125 - hscroll_step_abs * CANON_X_UNIT (it.f)
8126 - hscroll_margin;
8127 hscroll
8128 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8130 else
8132 if (hscroll_relative_p)
8133 wanted_x = text_area_width * hscroll_step_rel
8134 + hscroll_margin;
8135 else
8136 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8137 + hscroll_margin;
8138 hscroll
8139 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8141 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8143 /* Don't call Fset_window_hscroll if value hasn't
8144 changed because it will prevent redisplay
8145 optimizations. */
8146 if (XFASTINT (w->hscroll) != hscroll)
8148 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8149 w->hscroll = make_number (hscroll);
8150 hscrolled_p = 1;
8155 window = w->next;
8158 /* Value is non-zero if hscroll of any leaf window has been changed. */
8159 return hscrolled_p;
8163 /* Set hscroll so that cursor is visible and not inside horizontal
8164 scroll margins for all windows in the tree rooted at WINDOW. See
8165 also hscroll_window_tree above. Value is non-zero if any window's
8166 hscroll has been changed. If it has, desired matrices on the frame
8167 of WINDOW are cleared. */
8169 static int
8170 hscroll_windows (window)
8171 Lisp_Object window;
8173 int hscrolled_p;
8175 if (automatic_hscrolling_p)
8177 hscrolled_p = hscroll_window_tree (window);
8178 if (hscrolled_p)
8179 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8181 else
8182 hscrolled_p = 0;
8183 return hscrolled_p;
8188 /************************************************************************
8189 Redisplay
8190 ************************************************************************/
8192 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8193 to a non-zero value. This is sometimes handy to have in a debugger
8194 session. */
8196 #if GLYPH_DEBUG
8198 /* First and last unchanged row for try_window_id. */
8200 int debug_first_unchanged_at_end_vpos;
8201 int debug_last_unchanged_at_beg_vpos;
8203 /* Delta vpos and y. */
8205 int debug_dvpos, debug_dy;
8207 /* Delta in characters and bytes for try_window_id. */
8209 int debug_delta, debug_delta_bytes;
8211 /* Values of window_end_pos and window_end_vpos at the end of
8212 try_window_id. */
8214 int debug_end_pos, debug_end_vpos;
8216 /* Append a string to W->desired_matrix->method. FMT is a printf
8217 format string. A1...A9 are a supplement for a variable-length
8218 argument list. If trace_redisplay_p is non-zero also printf the
8219 resulting string to stderr. */
8221 static void
8222 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8223 struct window *w;
8224 char *fmt;
8225 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8227 char buffer[512];
8228 char *method = w->desired_matrix->method;
8229 int len = strlen (method);
8230 int size = sizeof w->desired_matrix->method;
8231 int remaining = size - len - 1;
8233 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8234 if (len && remaining)
8236 method[len] = '|';
8237 --remaining, ++len;
8240 strncpy (method + len, buffer, remaining);
8242 if (trace_redisplay_p)
8243 fprintf (stderr, "%p (%s): %s\n",
8245 ((BUFFERP (w->buffer)
8246 && STRINGP (XBUFFER (w->buffer)->name))
8247 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8248 : "no buffer"),
8249 buffer);
8252 #endif /* GLYPH_DEBUG */
8255 /* This counter is used to clear the face cache every once in a while
8256 in redisplay_internal. It is incremented for each redisplay.
8257 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8258 cleared. */
8260 #define CLEAR_FACE_CACHE_COUNT 500
8261 static int clear_face_cache_count;
8263 /* Record the previous terminal frame we displayed. */
8265 static struct frame *previous_terminal_frame;
8267 /* Non-zero while redisplay_internal is in progress. */
8269 int redisplaying_p;
8272 /* Value is non-zero if all changes in window W, which displays
8273 current_buffer, are in the text between START and END. START is a
8274 buffer position, END is given as a distance from Z. Used in
8275 redisplay_internal for display optimization. */
8277 static INLINE int
8278 text_outside_line_unchanged_p (w, start, end)
8279 struct window *w;
8280 int start, end;
8282 int unchanged_p = 1;
8284 /* If text or overlays have changed, see where. */
8285 if (XFASTINT (w->last_modified) < MODIFF
8286 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8288 /* Gap in the line? */
8289 if (GPT < start || Z - GPT < end)
8290 unchanged_p = 0;
8292 /* Changes start in front of the line, or end after it? */
8293 if (unchanged_p
8294 && (BEG_UNCHANGED < start - 1
8295 || END_UNCHANGED < end))
8296 unchanged_p = 0;
8298 /* If selective display, can't optimize if changes start at the
8299 beginning of the line. */
8300 if (unchanged_p
8301 && INTEGERP (current_buffer->selective_display)
8302 && XINT (current_buffer->selective_display) > 0
8303 && (BEG_UNCHANGED < start || GPT <= start))
8304 unchanged_p = 0;
8306 /* If there are overlays at the start or end of the line, these
8307 may have overlay strings with newlines in them. A change at
8308 START, for instance, may actually concern the display of such
8309 overlay strings as well, and they are displayed on different
8310 lines. So, quickly rule out this case. (For the future, it
8311 might be desirable to implement something more telling than
8312 just BEG/END_UNCHANGED.) */
8313 if (unchanged_p)
8315 if (BEG + BEG_UNCHANGED == start
8316 && overlay_touches_p (start))
8317 unchanged_p = 0;
8318 if (END_UNCHANGED == end
8319 && overlay_touches_p (Z - end))
8320 unchanged_p = 0;
8324 return unchanged_p;
8328 /* Do a frame update, taking possible shortcuts into account. This is
8329 the main external entry point for redisplay.
8331 If the last redisplay displayed an echo area message and that message
8332 is no longer requested, we clear the echo area or bring back the
8333 mini-buffer if that is in use. */
8335 void
8336 redisplay ()
8338 redisplay_internal (0);
8342 /* Return 1 if point moved out of or into a composition. Otherwise
8343 return 0. PREV_BUF and PREV_PT are the last point buffer and
8344 position. BUF and PT are the current point buffer and position. */
8347 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8348 struct buffer *prev_buf, *buf;
8349 int prev_pt, pt;
8351 int start, end;
8352 Lisp_Object prop;
8353 Lisp_Object buffer;
8355 XSETBUFFER (buffer, buf);
8356 /* Check a composition at the last point if point moved within the
8357 same buffer. */
8358 if (prev_buf == buf)
8360 if (prev_pt == pt)
8361 /* Point didn't move. */
8362 return 0;
8364 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8365 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8366 && COMPOSITION_VALID_P (start, end, prop)
8367 && start < prev_pt && end > prev_pt)
8368 /* The last point was within the composition. Return 1 iff
8369 point moved out of the composition. */
8370 return (pt <= start || pt >= end);
8373 /* Check a composition at the current point. */
8374 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8375 && find_composition (pt, -1, &start, &end, &prop, buffer)
8376 && COMPOSITION_VALID_P (start, end, prop)
8377 && start < pt && end > pt);
8381 /* Reconsider the setting of B->clip_changed which is displayed
8382 in window W. */
8384 static INLINE void
8385 reconsider_clip_changes (w, b)
8386 struct window *w;
8387 struct buffer *b;
8389 if (b->prevent_redisplay_optimizations_p)
8390 b->clip_changed = 1;
8391 else if (b->clip_changed
8392 && !NILP (w->window_end_valid)
8393 && w->current_matrix->buffer == b
8394 && w->current_matrix->zv == BUF_ZV (b)
8395 && w->current_matrix->begv == BUF_BEGV (b))
8396 b->clip_changed = 0;
8398 /* If display wasn't paused, and W is not a tool bar window, see if
8399 point has been moved into or out of a composition. In that case,
8400 we set b->clip_changed to 1 to force updating the screen. If
8401 b->clip_changed has already been set to 1, we can skip this
8402 check. */
8403 if (!b->clip_changed
8404 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8406 int pt;
8408 if (w == XWINDOW (selected_window))
8409 pt = BUF_PT (current_buffer);
8410 else
8411 pt = marker_position (w->pointm);
8413 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8414 || pt != XINT (w->last_point))
8415 && check_point_in_composition (w->current_matrix->buffer,
8416 XINT (w->last_point),
8417 XBUFFER (w->buffer), pt))
8418 b->clip_changed = 1;
8423 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8424 response to any user action; therefore, we should preserve the echo
8425 area. (Actually, our caller does that job.) Perhaps in the future
8426 avoid recentering windows if it is not necessary; currently that
8427 causes some problems. */
8429 static void
8430 redisplay_internal (preserve_echo_area)
8431 int preserve_echo_area;
8433 struct window *w = XWINDOW (selected_window);
8434 struct frame *f = XFRAME (w->frame);
8435 int pause;
8436 int must_finish = 0;
8437 struct text_pos tlbufpos, tlendpos;
8438 int number_of_visible_frames;
8439 int count;
8440 struct frame *sf = SELECTED_FRAME ();
8442 /* Non-zero means redisplay has to consider all windows on all
8443 frames. Zero means, only selected_window is considered. */
8444 int consider_all_windows_p;
8446 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8448 /* No redisplay if running in batch mode or frame is not yet fully
8449 initialized, or redisplay is explicitly turned off by setting
8450 Vinhibit_redisplay. */
8451 if (noninteractive
8452 || !NILP (Vinhibit_redisplay)
8453 || !f->glyphs_initialized_p)
8454 return;
8456 /* The flag redisplay_performed_directly_p is set by
8457 direct_output_for_insert when it already did the whole screen
8458 update necessary. */
8459 if (redisplay_performed_directly_p)
8461 redisplay_performed_directly_p = 0;
8462 if (!hscroll_windows (selected_window))
8463 return;
8466 #ifdef USE_X_TOOLKIT
8467 if (popup_activated ())
8468 return;
8469 #endif
8471 /* I don't think this happens but let's be paranoid. */
8472 if (redisplaying_p)
8473 return;
8475 /* Record a function that resets redisplaying_p to its old value
8476 when we leave this function. */
8477 count = BINDING_STACK_SIZE ();
8478 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8479 ++redisplaying_p;
8481 retry:
8482 pause = 0;
8483 reconsider_clip_changes (w, current_buffer);
8485 /* If new fonts have been loaded that make a glyph matrix adjustment
8486 necessary, do it. */
8487 if (fonts_changed_p)
8489 adjust_glyphs (NULL);
8490 ++windows_or_buffers_changed;
8491 fonts_changed_p = 0;
8494 /* If face_change_count is non-zero, init_iterator will free all
8495 realized faces, which includes the faces referenced from current
8496 matrices. So, we can't reuse current matrices in this case. */
8497 if (face_change_count)
8498 ++windows_or_buffers_changed;
8500 if (! FRAME_WINDOW_P (sf)
8501 && previous_terminal_frame != sf)
8503 /* Since frames on an ASCII terminal share the same display
8504 area, displaying a different frame means redisplay the whole
8505 thing. */
8506 windows_or_buffers_changed++;
8507 SET_FRAME_GARBAGED (sf);
8508 XSETFRAME (Vterminal_frame, sf);
8510 previous_terminal_frame = sf;
8512 /* Set the visible flags for all frames. Do this before checking
8513 for resized or garbaged frames; they want to know if their frames
8514 are visible. See the comment in frame.h for
8515 FRAME_SAMPLE_VISIBILITY. */
8517 Lisp_Object tail, frame;
8519 number_of_visible_frames = 0;
8521 FOR_EACH_FRAME (tail, frame)
8523 struct frame *f = XFRAME (frame);
8525 FRAME_SAMPLE_VISIBILITY (f);
8526 if (FRAME_VISIBLE_P (f))
8527 ++number_of_visible_frames;
8528 clear_desired_matrices (f);
8532 /* Notice any pending interrupt request to change frame size. */
8533 do_pending_window_change (1);
8535 /* Clear frames marked as garbaged. */
8536 if (frame_garbaged)
8537 clear_garbaged_frames ();
8539 /* Build menubar and tool-bar items. */
8540 prepare_menu_bars ();
8542 if (windows_or_buffers_changed)
8543 update_mode_lines++;
8545 /* Detect case that we need to write or remove a star in the mode line. */
8546 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8548 w->update_mode_line = Qt;
8549 if (buffer_shared > 1)
8550 update_mode_lines++;
8553 /* If %c is in the mode line, update it if needed. */
8554 if (!NILP (w->column_number_displayed)
8555 /* This alternative quickly identifies a common case
8556 where no change is needed. */
8557 && !(PT == XFASTINT (w->last_point)
8558 && XFASTINT (w->last_modified) >= MODIFF
8559 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8560 && XFASTINT (w->column_number_displayed) != current_column ())
8561 w->update_mode_line = Qt;
8563 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8565 /* The variable buffer_shared is set in redisplay_window and
8566 indicates that we redisplay a buffer in different windows. See
8567 there. */
8568 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8570 /* If specs for an arrow have changed, do thorough redisplay
8571 to ensure we remove any arrow that should no longer exist. */
8572 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8573 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8574 consider_all_windows_p = windows_or_buffers_changed = 1;
8576 /* Normally the message* functions will have already displayed and
8577 updated the echo area, but the frame may have been trashed, or
8578 the update may have been preempted, so display the echo area
8579 again here. Checking message_cleared_p captures the case that
8580 the echo area should be cleared. */
8581 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8582 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8583 || (message_cleared_p
8584 && minibuf_level == 0
8585 /* If the mini-window is currently selected, this means the
8586 echo-area doesn't show through. */
8587 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8589 int window_height_changed_p = echo_area_display (0);
8590 must_finish = 1;
8592 /* If we don't display the current message, don't clear the
8593 message_cleared_p flag, because, if we did, we wouldn't clear
8594 the echo area in the next redisplay which doesn't preserve
8595 the echo area. */
8596 if (!display_last_displayed_message_p)
8597 message_cleared_p = 0;
8599 if (fonts_changed_p)
8600 goto retry;
8601 else if (window_height_changed_p)
8603 consider_all_windows_p = 1;
8604 ++update_mode_lines;
8605 ++windows_or_buffers_changed;
8607 /* If window configuration was changed, frames may have been
8608 marked garbaged. Clear them or we will experience
8609 surprises wrt scrolling. */
8610 if (frame_garbaged)
8611 clear_garbaged_frames ();
8614 else if (EQ (selected_window, minibuf_window)
8615 && (current_buffer->clip_changed
8616 || XFASTINT (w->last_modified) < MODIFF
8617 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8618 && resize_mini_window (w, 0))
8620 /* Resized active mini-window to fit the size of what it is
8621 showing if its contents might have changed. */
8622 must_finish = 1;
8623 consider_all_windows_p = 1;
8624 ++windows_or_buffers_changed;
8625 ++update_mode_lines;
8627 /* If window configuration was changed, frames may have been
8628 marked garbaged. Clear them or we will experience
8629 surprises wrt scrolling. */
8630 if (frame_garbaged)
8631 clear_garbaged_frames ();
8635 /* If showing the region, and mark has changed, we must redisplay
8636 the whole window. The assignment to this_line_start_pos prevents
8637 the optimization directly below this if-statement. */
8638 if (((!NILP (Vtransient_mark_mode)
8639 && !NILP (XBUFFER (w->buffer)->mark_active))
8640 != !NILP (w->region_showing))
8641 || (!NILP (w->region_showing)
8642 && !EQ (w->region_showing,
8643 Fmarker_position (XBUFFER (w->buffer)->mark))))
8644 CHARPOS (this_line_start_pos) = 0;
8646 /* Optimize the case that only the line containing the cursor in the
8647 selected window has changed. Variables starting with this_ are
8648 set in display_line and record information about the line
8649 containing the cursor. */
8650 tlbufpos = this_line_start_pos;
8651 tlendpos = this_line_end_pos;
8652 if (!consider_all_windows_p
8653 && CHARPOS (tlbufpos) > 0
8654 && NILP (w->update_mode_line)
8655 && !current_buffer->clip_changed
8656 && FRAME_VISIBLE_P (XFRAME (w->frame))
8657 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8658 /* Make sure recorded data applies to current buffer, etc. */
8659 && this_line_buffer == current_buffer
8660 && current_buffer == XBUFFER (w->buffer)
8661 && NILP (w->force_start)
8662 /* Point must be on the line that we have info recorded about. */
8663 && PT >= CHARPOS (tlbufpos)
8664 && PT <= Z - CHARPOS (tlendpos)
8665 /* All text outside that line, including its final newline,
8666 must be unchanged */
8667 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8668 CHARPOS (tlendpos)))
8670 if (CHARPOS (tlbufpos) > BEGV
8671 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8672 && (CHARPOS (tlbufpos) == ZV
8673 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8674 /* Former continuation line has disappeared by becoming empty */
8675 goto cancel;
8676 else if (XFASTINT (w->last_modified) < MODIFF
8677 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8678 || MINI_WINDOW_P (w))
8680 /* We have to handle the case of continuation around a
8681 wide-column character (See the comment in indent.c around
8682 line 885).
8684 For instance, in the following case:
8686 -------- Insert --------
8687 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8688 J_I_ ==> J_I_ `^^' are cursors.
8689 ^^ ^^
8690 -------- --------
8692 As we have to redraw the line above, we should goto cancel. */
8694 struct it it;
8695 int line_height_before = this_line_pixel_height;
8697 /* Note that start_display will handle the case that the
8698 line starting at tlbufpos is a continuation lines. */
8699 start_display (&it, w, tlbufpos);
8701 /* Implementation note: It this still necessary? */
8702 if (it.current_x != this_line_start_x)
8703 goto cancel;
8705 TRACE ((stderr, "trying display optimization 1\n"));
8706 w->cursor.vpos = -1;
8707 overlay_arrow_seen = 0;
8708 it.vpos = this_line_vpos;
8709 it.current_y = this_line_y;
8710 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8711 display_line (&it);
8713 /* If line contains point, is not continued,
8714 and ends at same distance from eob as before, we win */
8715 if (w->cursor.vpos >= 0
8716 /* Line is not continued, otherwise this_line_start_pos
8717 would have been set to 0 in display_line. */
8718 && CHARPOS (this_line_start_pos)
8719 /* Line ends as before. */
8720 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8721 /* Line has same height as before. Otherwise other lines
8722 would have to be shifted up or down. */
8723 && this_line_pixel_height == line_height_before)
8725 /* If this is not the window's last line, we must adjust
8726 the charstarts of the lines below. */
8727 if (it.current_y < it.last_visible_y)
8729 struct glyph_row *row
8730 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8731 int delta, delta_bytes;
8733 if (Z - CHARPOS (tlendpos) == ZV)
8735 /* This line ends at end of (accessible part of)
8736 buffer. There is no newline to count. */
8737 delta = (Z
8738 - CHARPOS (tlendpos)
8739 - MATRIX_ROW_START_CHARPOS (row));
8740 delta_bytes = (Z_BYTE
8741 - BYTEPOS (tlendpos)
8742 - MATRIX_ROW_START_BYTEPOS (row));
8744 else
8746 /* This line ends in a newline. Must take
8747 account of the newline and the rest of the
8748 text that follows. */
8749 delta = (Z
8750 - CHARPOS (tlendpos)
8751 - MATRIX_ROW_START_CHARPOS (row));
8752 delta_bytes = (Z_BYTE
8753 - BYTEPOS (tlendpos)
8754 - MATRIX_ROW_START_BYTEPOS (row));
8757 increment_matrix_positions (w->current_matrix,
8758 this_line_vpos + 1,
8759 w->current_matrix->nrows,
8760 delta, delta_bytes);
8763 /* If this row displays text now but previously didn't,
8764 or vice versa, w->window_end_vpos may have to be
8765 adjusted. */
8766 if ((it.glyph_row - 1)->displays_text_p)
8768 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8769 XSETINT (w->window_end_vpos, this_line_vpos);
8771 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8772 && this_line_vpos > 0)
8773 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8774 w->window_end_valid = Qnil;
8776 /* Update hint: No need to try to scroll in update_window. */
8777 w->desired_matrix->no_scrolling_p = 1;
8779 #if GLYPH_DEBUG
8780 *w->desired_matrix->method = 0;
8781 debug_method_add (w, "optimization 1");
8782 #endif
8783 goto update;
8785 else
8786 goto cancel;
8788 else if (/* Cursor position hasn't changed. */
8789 PT == XFASTINT (w->last_point)
8790 /* Make sure the cursor was last displayed
8791 in this window. Otherwise we have to reposition it. */
8792 && 0 <= w->cursor.vpos
8793 && XINT (w->height) > w->cursor.vpos)
8795 if (!must_finish)
8797 do_pending_window_change (1);
8799 /* We used to always goto end_of_redisplay here, but this
8800 isn't enough if we have a blinking cursor. */
8801 if (w->cursor_off_p == w->last_cursor_off_p)
8802 goto end_of_redisplay;
8804 goto update;
8806 /* If highlighting the region, or if the cursor is in the echo area,
8807 then we can't just move the cursor. */
8808 else if (! (!NILP (Vtransient_mark_mode)
8809 && !NILP (current_buffer->mark_active))
8810 && (EQ (selected_window, current_buffer->last_selected_window)
8811 || highlight_nonselected_windows)
8812 && NILP (w->region_showing)
8813 && NILP (Vshow_trailing_whitespace)
8814 && !cursor_in_echo_area)
8816 struct it it;
8817 struct glyph_row *row;
8819 /* Skip from tlbufpos to PT and see where it is. Note that
8820 PT may be in invisible text. If so, we will end at the
8821 next visible position. */
8822 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8823 NULL, DEFAULT_FACE_ID);
8824 it.current_x = this_line_start_x;
8825 it.current_y = this_line_y;
8826 it.vpos = this_line_vpos;
8828 /* The call to move_it_to stops in front of PT, but
8829 moves over before-strings. */
8830 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8832 if (it.vpos == this_line_vpos
8833 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8834 row->enabled_p))
8836 xassert (this_line_vpos == it.vpos);
8837 xassert (this_line_y == it.current_y);
8838 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8839 #if GLYPH_DEBUG
8840 *w->desired_matrix->method = 0;
8841 debug_method_add (w, "optimization 3");
8842 #endif
8843 goto update;
8845 else
8846 goto cancel;
8849 cancel:
8850 /* Text changed drastically or point moved off of line. */
8851 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8854 CHARPOS (this_line_start_pos) = 0;
8855 consider_all_windows_p |= buffer_shared > 1;
8856 ++clear_face_cache_count;
8859 /* Build desired matrices, and update the display. If
8860 consider_all_windows_p is non-zero, do it for all windows on all
8861 frames. Otherwise do it for selected_window, only. */
8863 if (consider_all_windows_p)
8865 Lisp_Object tail, frame;
8866 int i, n = 0, size = 50;
8867 struct frame **updated
8868 = (struct frame **) alloca (size * sizeof *updated);
8870 /* Clear the face cache eventually. */
8871 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8873 clear_face_cache (0);
8874 clear_face_cache_count = 0;
8877 /* Recompute # windows showing selected buffer. This will be
8878 incremented each time such a window is displayed. */
8879 buffer_shared = 0;
8881 FOR_EACH_FRAME (tail, frame)
8883 struct frame *f = XFRAME (frame);
8885 if (FRAME_WINDOW_P (f) || f == sf)
8887 if (clear_face_cache_count % 50 == 0
8888 && FRAME_WINDOW_P (f))
8889 clear_image_cache (f, 0);
8891 /* Mark all the scroll bars to be removed; we'll redeem
8892 the ones we want when we redisplay their windows. */
8893 if (condemn_scroll_bars_hook)
8894 condemn_scroll_bars_hook (f);
8896 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8897 redisplay_windows (FRAME_ROOT_WINDOW (f));
8899 /* Any scroll bars which redisplay_windows should have
8900 nuked should now go away. */
8901 if (judge_scroll_bars_hook)
8902 judge_scroll_bars_hook (f);
8904 /* If fonts changed, display again. */
8905 if (fonts_changed_p)
8906 goto retry;
8908 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8910 /* See if we have to hscroll. */
8911 if (hscroll_windows (f->root_window))
8912 goto retry;
8914 /* Prevent various kinds of signals during display
8915 update. stdio is not robust about handling
8916 signals, which can cause an apparent I/O
8917 error. */
8918 if (interrupt_input)
8919 unrequest_sigio ();
8920 stop_polling ();
8922 /* Update the display. */
8923 set_window_update_flags (XWINDOW (f->root_window), 1);
8924 pause |= update_frame (f, 0, 0);
8925 if (pause)
8926 break;
8928 if (n == size)
8930 int nbytes = size * sizeof *updated;
8931 struct frame **p = (struct frame **) alloca (2 * nbytes);
8932 bcopy (updated, p, nbytes);
8933 size *= 2;
8936 updated[n++] = f;
8941 /* Do the mark_window_display_accurate after all windows have
8942 been redisplayed because this call resets flags in buffers
8943 which are needed for proper redisplay. */
8944 for (i = 0; i < n; ++i)
8946 struct frame *f = updated[i];
8947 mark_window_display_accurate (f->root_window, 1);
8948 if (frame_up_to_date_hook)
8949 frame_up_to_date_hook (f);
8952 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8954 Lisp_Object mini_window;
8955 struct frame *mini_frame;
8957 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8958 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8959 redisplay_window_error);
8961 /* Compare desired and current matrices, perform output. */
8962 update:
8964 /* If fonts changed, display again. */
8965 if (fonts_changed_p)
8966 goto retry;
8968 /* Prevent various kinds of signals during display update.
8969 stdio is not robust about handling signals,
8970 which can cause an apparent I/O error. */
8971 if (interrupt_input)
8972 unrequest_sigio ();
8973 stop_polling ();
8975 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8977 if (hscroll_windows (selected_window))
8978 goto retry;
8980 XWINDOW (selected_window)->must_be_updated_p = 1;
8981 pause = update_frame (sf, 0, 0);
8984 /* We may have called echo_area_display at the top of this
8985 function. If the echo area is on another frame, that may
8986 have put text on a frame other than the selected one, so the
8987 above call to update_frame would not have caught it. Catch
8988 it here. */
8989 mini_window = FRAME_MINIBUF_WINDOW (sf);
8990 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8992 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8994 XWINDOW (mini_window)->must_be_updated_p = 1;
8995 pause |= update_frame (mini_frame, 0, 0);
8996 if (!pause && hscroll_windows (mini_window))
8997 goto retry;
9001 /* If display was paused because of pending input, make sure we do a
9002 thorough update the next time. */
9003 if (pause)
9005 /* Prevent the optimization at the beginning of
9006 redisplay_internal that tries a single-line update of the
9007 line containing the cursor in the selected window. */
9008 CHARPOS (this_line_start_pos) = 0;
9010 /* Let the overlay arrow be updated the next time. */
9011 if (!NILP (last_arrow_position))
9013 last_arrow_position = Qt;
9014 last_arrow_string = Qt;
9017 /* If we pause after scrolling, some rows in the current
9018 matrices of some windows are not valid. */
9019 if (!WINDOW_FULL_WIDTH_P (w)
9020 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9021 update_mode_lines = 1;
9023 else
9025 if (!consider_all_windows_p)
9027 /* This has already been done above if
9028 consider_all_windows_p is set. */
9029 mark_window_display_accurate_1 (w, 1);
9031 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9032 last_arrow_string = Voverlay_arrow_string;
9034 if (frame_up_to_date_hook != 0)
9035 frame_up_to_date_hook (sf);
9038 update_mode_lines = 0;
9039 windows_or_buffers_changed = 0;
9042 /* Start SIGIO interrupts coming again. Having them off during the
9043 code above makes it less likely one will discard output, but not
9044 impossible, since there might be stuff in the system buffer here.
9045 But it is much hairier to try to do anything about that. */
9046 if (interrupt_input)
9047 request_sigio ();
9048 start_polling ();
9050 /* If a frame has become visible which was not before, redisplay
9051 again, so that we display it. Expose events for such a frame
9052 (which it gets when becoming visible) don't call the parts of
9053 redisplay constructing glyphs, so simply exposing a frame won't
9054 display anything in this case. So, we have to display these
9055 frames here explicitly. */
9056 if (!pause)
9058 Lisp_Object tail, frame;
9059 int new_count = 0;
9061 FOR_EACH_FRAME (tail, frame)
9063 int this_is_visible = 0;
9065 if (XFRAME (frame)->visible)
9066 this_is_visible = 1;
9067 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9068 if (XFRAME (frame)->visible)
9069 this_is_visible = 1;
9071 if (this_is_visible)
9072 new_count++;
9075 if (new_count != number_of_visible_frames)
9076 windows_or_buffers_changed++;
9079 /* Change frame size now if a change is pending. */
9080 do_pending_window_change (1);
9082 /* If we just did a pending size change, or have additional
9083 visible frames, redisplay again. */
9084 if (windows_or_buffers_changed && !pause)
9085 goto retry;
9087 end_of_redisplay:;
9089 unbind_to (count, Qnil);
9093 /* Redisplay, but leave alone any recent echo area message unless
9094 another message has been requested in its place.
9096 This is useful in situations where you need to redisplay but no
9097 user action has occurred, making it inappropriate for the message
9098 area to be cleared. See tracking_off and
9099 wait_reading_process_input for examples of these situations.
9101 FROM_WHERE is an integer saying from where this function was
9102 called. This is useful for debugging. */
9104 void
9105 redisplay_preserve_echo_area (from_where)
9106 int from_where;
9108 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9110 if (!NILP (echo_area_buffer[1]))
9112 /* We have a previously displayed message, but no current
9113 message. Redisplay the previous message. */
9114 display_last_displayed_message_p = 1;
9115 redisplay_internal (1);
9116 display_last_displayed_message_p = 0;
9118 else
9119 redisplay_internal (1);
9123 /* Function registered with record_unwind_protect in
9124 redisplay_internal. Clears the flag indicating that a redisplay is
9125 in progress. */
9127 static Lisp_Object
9128 unwind_redisplay (old_redisplaying_p)
9129 Lisp_Object old_redisplaying_p;
9131 redisplaying_p = XFASTINT (old_redisplaying_p);
9132 return Qnil;
9136 /* Mark the display of window W as accurate or inaccurate. If
9137 ACCURATE_P is non-zero mark display of W as accurate. If
9138 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9139 redisplay_internal is called. */
9141 static void
9142 mark_window_display_accurate_1 (w, accurate_p)
9143 struct window *w;
9144 int accurate_p;
9146 if (BUFFERP (w->buffer))
9148 struct buffer *b = XBUFFER (w->buffer);
9150 w->last_modified
9151 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9152 w->last_overlay_modified
9153 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9154 w->last_had_star
9155 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9157 if (accurate_p)
9159 b->clip_changed = 0;
9160 b->prevent_redisplay_optimizations_p = 0;
9162 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9163 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9164 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9165 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9167 w->current_matrix->buffer = b;
9168 w->current_matrix->begv = BUF_BEGV (b);
9169 w->current_matrix->zv = BUF_ZV (b);
9171 w->last_cursor = w->cursor;
9172 w->last_cursor_off_p = w->cursor_off_p;
9174 if (w == XWINDOW (selected_window))
9175 w->last_point = make_number (BUF_PT (b));
9176 else
9177 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9181 if (accurate_p)
9183 w->window_end_valid = w->buffer;
9184 #if 0 /* This is incorrect with variable-height lines. */
9185 xassert (XINT (w->window_end_vpos)
9186 < (XINT (w->height)
9187 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9188 #endif
9189 w->update_mode_line = Qnil;
9194 /* Mark the display of windows in the window tree rooted at WINDOW as
9195 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9196 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9197 be redisplayed the next time redisplay_internal is called. */
9199 void
9200 mark_window_display_accurate (window, accurate_p)
9201 Lisp_Object window;
9202 int accurate_p;
9204 struct window *w;
9206 for (; !NILP (window); window = w->next)
9208 w = XWINDOW (window);
9209 mark_window_display_accurate_1 (w, accurate_p);
9211 if (!NILP (w->vchild))
9212 mark_window_display_accurate (w->vchild, accurate_p);
9213 if (!NILP (w->hchild))
9214 mark_window_display_accurate (w->hchild, accurate_p);
9217 if (accurate_p)
9219 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9220 last_arrow_string = Voverlay_arrow_string;
9222 else
9224 /* Force a thorough redisplay the next time by setting
9225 last_arrow_position and last_arrow_string to t, which is
9226 unequal to any useful value of Voverlay_arrow_... */
9227 last_arrow_position = Qt;
9228 last_arrow_string = Qt;
9233 /* Return value in display table DP (Lisp_Char_Table *) for character
9234 C. Since a display table doesn't have any parent, we don't have to
9235 follow parent. Do not call this function directly but use the
9236 macro DISP_CHAR_VECTOR. */
9238 Lisp_Object
9239 disp_char_vector (dp, c)
9240 struct Lisp_Char_Table *dp;
9241 int c;
9243 Lisp_Object val;
9245 if (ASCII_CHAR_P (c))
9247 val = dp->ascii;
9248 if (SUB_CHAR_TABLE_P (val))
9249 val = XSUB_CHAR_TABLE (val)->contents[c];
9251 else
9253 Lisp_Object table;
9255 XSETCHAR_TABLE (table, dp);
9256 val = char_table_ref (table, c);
9258 if (NILP (val))
9259 val = dp->defalt;
9260 return val;
9265 /***********************************************************************
9266 Window Redisplay
9267 ***********************************************************************/
9269 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9271 static void
9272 redisplay_windows (window)
9273 Lisp_Object window;
9275 while (!NILP (window))
9277 struct window *w = XWINDOW (window);
9279 if (!NILP (w->hchild))
9280 redisplay_windows (w->hchild);
9281 else if (!NILP (w->vchild))
9282 redisplay_windows (w->vchild);
9283 else
9285 displayed_buffer = XBUFFER (w->buffer);
9286 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9287 redisplay_window_error);
9290 window = w->next;
9294 static Lisp_Object
9295 redisplay_window_error ()
9297 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9298 return Qnil;
9301 static Lisp_Object
9302 redisplay_window_0 (window)
9303 Lisp_Object window;
9305 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9306 redisplay_window (window, 0);
9307 return Qnil;
9310 static Lisp_Object
9311 redisplay_window_1 (window)
9312 Lisp_Object window;
9314 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9315 redisplay_window (window, 1);
9316 return Qnil;
9319 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9320 DELTA is the number of bytes by which positions recorded in ROW
9321 differ from current buffer positions. */
9323 void
9324 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9325 struct window *w;
9326 struct glyph_row *row;
9327 struct glyph_matrix *matrix;
9328 int delta, delta_bytes, dy, dvpos;
9330 struct glyph *glyph = row->glyphs[TEXT_AREA];
9331 struct glyph *end = glyph + row->used[TEXT_AREA];
9332 int x = row->x;
9333 int pt_old = PT - delta;
9335 /* Skip over glyphs not having an object at the start of the row.
9336 These are special glyphs like truncation marks on terminal
9337 frames. */
9338 if (row->displays_text_p)
9339 while (glyph < end
9340 && INTEGERP (glyph->object)
9341 && glyph->charpos < 0)
9343 x += glyph->pixel_width;
9344 ++glyph;
9347 while (glyph < end
9348 && !INTEGERP (glyph->object)
9349 && (!BUFFERP (glyph->object)
9350 || glyph->charpos < pt_old))
9352 x += glyph->pixel_width;
9353 ++glyph;
9356 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9357 w->cursor.x = x;
9358 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9359 w->cursor.y = row->y + dy;
9361 if (w == XWINDOW (selected_window))
9363 if (!row->continued_p
9364 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9365 && row->x == 0)
9367 this_line_buffer = XBUFFER (w->buffer);
9369 CHARPOS (this_line_start_pos)
9370 = MATRIX_ROW_START_CHARPOS (row) + delta;
9371 BYTEPOS (this_line_start_pos)
9372 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9374 CHARPOS (this_line_end_pos)
9375 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9376 BYTEPOS (this_line_end_pos)
9377 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9379 this_line_y = w->cursor.y;
9380 this_line_pixel_height = row->height;
9381 this_line_vpos = w->cursor.vpos;
9382 this_line_start_x = row->x;
9384 else
9385 CHARPOS (this_line_start_pos) = 0;
9390 /* Run window scroll functions, if any, for WINDOW with new window
9391 start STARTP. Sets the window start of WINDOW to that position.
9393 We assume that the window's buffer is really current. */
9395 static INLINE struct text_pos
9396 run_window_scroll_functions (window, startp)
9397 Lisp_Object window;
9398 struct text_pos startp;
9400 struct window *w = XWINDOW (window);
9401 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9403 if (current_buffer != XBUFFER (w->buffer))
9404 abort ();
9406 if (!NILP (Vwindow_scroll_functions))
9408 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9409 make_number (CHARPOS (startp)));
9410 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9411 /* In case the hook functions switch buffers. */
9412 if (current_buffer != XBUFFER (w->buffer))
9413 set_buffer_internal_1 (XBUFFER (w->buffer));
9416 return startp;
9420 /* Modify the desired matrix of window W and W->vscroll so that the
9421 line containing the cursor is fully visible. If this requires
9422 larger matrices than are allocated, set fonts_changed_p and return
9423 0. */
9425 static int
9426 make_cursor_line_fully_visible (w)
9427 struct window *w;
9429 struct glyph_matrix *matrix;
9430 struct glyph_row *row;
9431 int window_height;
9433 /* It's not always possible to find the cursor, e.g, when a window
9434 is full of overlay strings. Don't do anything in that case. */
9435 if (w->cursor.vpos < 0)
9436 return 1;
9438 matrix = w->desired_matrix;
9439 row = MATRIX_ROW (matrix, w->cursor.vpos);
9441 /* If the cursor row is not partially visible, there's nothing
9442 to do. */
9443 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9444 return 1;
9446 /* If the row the cursor is in is taller than the window's height,
9447 it's not clear what to do, so do nothing. */
9448 window_height = window_box_height (w);
9449 if (row->height >= window_height)
9450 return 1;
9452 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9454 int dy = row->height - row->visible_height;
9455 w->vscroll = 0;
9456 w->cursor.y += dy;
9457 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9459 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9461 int dy = - (row->height - row->visible_height);
9462 w->vscroll = dy;
9463 w->cursor.y += dy;
9464 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9467 /* When we change the cursor y-position of the selected window,
9468 change this_line_y as well so that the display optimization for
9469 the cursor line of the selected window in redisplay_internal uses
9470 the correct y-position. */
9471 if (w == XWINDOW (selected_window))
9472 this_line_y = w->cursor.y;
9474 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9475 redisplay with larger matrices. */
9476 if (matrix->nrows < required_matrix_height (w))
9478 fonts_changed_p = 1;
9479 return 0;
9482 return 1;
9486 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9487 non-zero means only WINDOW is redisplayed in redisplay_internal.
9488 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9489 in redisplay_window to bring a partially visible line into view in
9490 the case that only the cursor has moved.
9492 Value is
9494 1 if scrolling succeeded
9496 0 if scrolling didn't find point.
9498 -1 if new fonts have been loaded so that we must interrupt
9499 redisplay, adjust glyph matrices, and try again. */
9501 enum
9503 SCROLLING_SUCCESS,
9504 SCROLLING_FAILED,
9505 SCROLLING_NEED_LARGER_MATRICES
9508 static int
9509 try_scrolling (window, just_this_one_p, scroll_conservatively,
9510 scroll_step, temp_scroll_step)
9511 Lisp_Object window;
9512 int just_this_one_p;
9513 int scroll_conservatively, scroll_step;
9514 int temp_scroll_step;
9516 struct window *w = XWINDOW (window);
9517 struct frame *f = XFRAME (w->frame);
9518 struct text_pos scroll_margin_pos;
9519 struct text_pos pos;
9520 struct text_pos startp;
9521 struct it it;
9522 Lisp_Object window_end;
9523 int this_scroll_margin;
9524 int dy = 0;
9525 int scroll_max;
9526 int rc;
9527 int amount_to_scroll = 0;
9528 Lisp_Object aggressive;
9529 int height;
9531 #if GLYPH_DEBUG
9532 debug_method_add (w, "try_scrolling");
9533 #endif
9535 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9537 /* Compute scroll margin height in pixels. We scroll when point is
9538 within this distance from the top or bottom of the window. */
9539 if (scroll_margin > 0)
9541 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9542 this_scroll_margin *= CANON_Y_UNIT (f);
9544 else
9545 this_scroll_margin = 0;
9547 /* Compute how much we should try to scroll maximally to bring point
9548 into view. */
9549 if (scroll_step || scroll_conservatively || temp_scroll_step)
9550 scroll_max = max (scroll_step,
9551 max (scroll_conservatively, temp_scroll_step));
9552 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9553 || NUMBERP (current_buffer->scroll_up_aggressively))
9554 /* We're trying to scroll because of aggressive scrolling
9555 but no scroll_step is set. Choose an arbitrary one. Maybe
9556 there should be a variable for this. */
9557 scroll_max = 10;
9558 else
9559 scroll_max = 0;
9560 scroll_max *= CANON_Y_UNIT (f);
9562 /* Decide whether we have to scroll down. Start at the window end
9563 and move this_scroll_margin up to find the position of the scroll
9564 margin. */
9565 window_end = Fwindow_end (window, Qt);
9566 CHARPOS (scroll_margin_pos) = XINT (window_end);
9567 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9568 if (this_scroll_margin)
9570 start_display (&it, w, scroll_margin_pos);
9571 move_it_vertically (&it, - this_scroll_margin);
9572 scroll_margin_pos = it.current.pos;
9575 if (PT >= CHARPOS (scroll_margin_pos))
9577 int y0;
9579 /* Point is in the scroll margin at the bottom of the window, or
9580 below. Compute a new window start that makes point visible. */
9582 /* Compute the distance from the scroll margin to PT.
9583 Give up if the distance is greater than scroll_max. */
9584 start_display (&it, w, scroll_margin_pos);
9585 y0 = it.current_y;
9586 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9587 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9589 /* To make point visible, we have to move the window start
9590 down so that the line the cursor is in is visible, which
9591 means we have to add in the height of the cursor line. */
9592 dy = line_bottom_y (&it) - y0;
9594 if (dy > scroll_max)
9595 return SCROLLING_FAILED;
9597 /* Move the window start down. If scrolling conservatively,
9598 move it just enough down to make point visible. If
9599 scroll_step is set, move it down by scroll_step. */
9600 start_display (&it, w, startp);
9602 if (scroll_conservatively)
9603 amount_to_scroll
9604 = max (max (dy, CANON_Y_UNIT (f)),
9605 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9606 else if (scroll_step || temp_scroll_step)
9607 amount_to_scroll = scroll_max;
9608 else
9610 aggressive = current_buffer->scroll_up_aggressively;
9611 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9612 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9613 if (NUMBERP (aggressive))
9614 amount_to_scroll = XFLOATINT (aggressive) * height;
9617 if (amount_to_scroll <= 0)
9618 return SCROLLING_FAILED;
9620 move_it_vertically (&it, amount_to_scroll);
9621 startp = it.current.pos;
9623 else
9625 /* See if point is inside the scroll margin at the top of the
9626 window. */
9627 scroll_margin_pos = startp;
9628 if (this_scroll_margin)
9630 start_display (&it, w, startp);
9631 move_it_vertically (&it, this_scroll_margin);
9632 scroll_margin_pos = it.current.pos;
9635 if (PT < CHARPOS (scroll_margin_pos))
9637 /* Point is in the scroll margin at the top of the window or
9638 above what is displayed in the window. */
9639 int y0;
9641 /* Compute the vertical distance from PT to the scroll
9642 margin position. Give up if distance is greater than
9643 scroll_max. */
9644 SET_TEXT_POS (pos, PT, PT_BYTE);
9645 start_display (&it, w, pos);
9646 y0 = it.current_y;
9647 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9648 it.last_visible_y, -1,
9649 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9650 dy = it.current_y - y0;
9651 if (dy > scroll_max)
9652 return SCROLLING_FAILED;
9654 /* Compute new window start. */
9655 start_display (&it, w, startp);
9657 if (scroll_conservatively)
9658 amount_to_scroll =
9659 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9660 else if (scroll_step || temp_scroll_step)
9661 amount_to_scroll = scroll_max;
9662 else
9664 aggressive = current_buffer->scroll_down_aggressively;
9665 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9666 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9667 if (NUMBERP (aggressive))
9668 amount_to_scroll = XFLOATINT (aggressive) * height;
9671 if (amount_to_scroll <= 0)
9672 return SCROLLING_FAILED;
9674 move_it_vertically (&it, - amount_to_scroll);
9675 startp = it.current.pos;
9679 /* Run window scroll functions. */
9680 startp = run_window_scroll_functions (window, startp);
9682 /* Display the window. Give up if new fonts are loaded, or if point
9683 doesn't appear. */
9684 if (!try_window (window, startp))
9685 rc = SCROLLING_NEED_LARGER_MATRICES;
9686 else if (w->cursor.vpos < 0)
9688 clear_glyph_matrix (w->desired_matrix);
9689 rc = SCROLLING_FAILED;
9691 else
9693 /* Maybe forget recorded base line for line number display. */
9694 if (!just_this_one_p
9695 || current_buffer->clip_changed
9696 || BEG_UNCHANGED < CHARPOS (startp))
9697 w->base_line_number = Qnil;
9699 /* If cursor ends up on a partially visible line, shift display
9700 lines up or down. If that fails because we need larger
9701 matrices, give up. */
9702 if (!make_cursor_line_fully_visible (w))
9703 rc = SCROLLING_NEED_LARGER_MATRICES;
9704 else
9705 rc = SCROLLING_SUCCESS;
9708 return rc;
9712 /* Compute a suitable window start for window W if display of W starts
9713 on a continuation line. Value is non-zero if a new window start
9714 was computed.
9716 The new window start will be computed, based on W's width, starting
9717 from the start of the continued line. It is the start of the
9718 screen line with the minimum distance from the old start W->start. */
9720 static int
9721 compute_window_start_on_continuation_line (w)
9722 struct window *w;
9724 struct text_pos pos, start_pos;
9725 int window_start_changed_p = 0;
9727 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9729 /* If window start is on a continuation line... Window start may be
9730 < BEGV in case there's invisible text at the start of the
9731 buffer (M-x rmail, for example). */
9732 if (CHARPOS (start_pos) > BEGV
9733 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9735 struct it it;
9736 struct glyph_row *row;
9738 /* Handle the case that the window start is out of range. */
9739 if (CHARPOS (start_pos) < BEGV)
9740 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9741 else if (CHARPOS (start_pos) > ZV)
9742 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9744 /* Find the start of the continued line. This should be fast
9745 because scan_buffer is fast (newline cache). */
9746 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9747 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9748 row, DEFAULT_FACE_ID);
9749 reseat_at_previous_visible_line_start (&it);
9751 /* If the line start is "too far" away from the window start,
9752 say it takes too much time to compute a new window start. */
9753 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9754 < XFASTINT (w->height) * XFASTINT (w->width))
9756 int min_distance, distance;
9758 /* Move forward by display lines to find the new window
9759 start. If window width was enlarged, the new start can
9760 be expected to be > the old start. If window width was
9761 decreased, the new window start will be < the old start.
9762 So, we're looking for the display line start with the
9763 minimum distance from the old window start. */
9764 pos = it.current.pos;
9765 min_distance = INFINITY;
9766 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9767 distance < min_distance)
9769 min_distance = distance;
9770 pos = it.current.pos;
9771 move_it_by_lines (&it, 1, 0);
9774 /* Set the window start there. */
9775 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9776 window_start_changed_p = 1;
9780 return window_start_changed_p;
9784 /* Try cursor movement in case text has not changes in window WINDOW,
9785 with window start STARTP. Value is
9787 CURSOR_MOVEMENT_SUCCESS if successful
9789 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9791 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9792 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9793 we want to scroll as if scroll-step were set to 1. See the code.
9795 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9796 which case we have to abort this redisplay, and adjust matrices
9797 first. */
9799 enum
9801 CURSOR_MOVEMENT_SUCCESS,
9802 CURSOR_MOVEMENT_CANNOT_BE_USED,
9803 CURSOR_MOVEMENT_MUST_SCROLL,
9804 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9807 static int
9808 try_cursor_movement (window, startp, scroll_step)
9809 Lisp_Object window;
9810 struct text_pos startp;
9811 int *scroll_step;
9813 struct window *w = XWINDOW (window);
9814 struct frame *f = XFRAME (w->frame);
9815 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9817 #if GLYPH_DEBUG
9818 if (inhibit_try_cursor_movement)
9819 return rc;
9820 #endif
9822 /* Handle case where text has not changed, only point, and it has
9823 not moved off the frame. */
9824 if (/* Point may be in this window. */
9825 PT >= CHARPOS (startp)
9826 /* Selective display hasn't changed. */
9827 && !current_buffer->clip_changed
9828 /* Function force-mode-line-update is used to force a thorough
9829 redisplay. It sets either windows_or_buffers_changed or
9830 update_mode_lines. So don't take a shortcut here for these
9831 cases. */
9832 && !update_mode_lines
9833 && !windows_or_buffers_changed
9834 /* Can't use this case if highlighting a region. When a
9835 region exists, cursor movement has to do more than just
9836 set the cursor. */
9837 && !(!NILP (Vtransient_mark_mode)
9838 && !NILP (current_buffer->mark_active))
9839 && NILP (w->region_showing)
9840 && NILP (Vshow_trailing_whitespace)
9841 /* Right after splitting windows, last_point may be nil. */
9842 && INTEGERP (w->last_point)
9843 /* This code is not used for mini-buffer for the sake of the case
9844 of redisplaying to replace an echo area message; since in
9845 that case the mini-buffer contents per se are usually
9846 unchanged. This code is of no real use in the mini-buffer
9847 since the handling of this_line_start_pos, etc., in redisplay
9848 handles the same cases. */
9849 && !EQ (window, minibuf_window)
9850 /* When splitting windows or for new windows, it happens that
9851 redisplay is called with a nil window_end_vpos or one being
9852 larger than the window. This should really be fixed in
9853 window.c. I don't have this on my list, now, so we do
9854 approximately the same as the old redisplay code. --gerd. */
9855 && INTEGERP (w->window_end_vpos)
9856 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9857 && (FRAME_WINDOW_P (f)
9858 || !MARKERP (Voverlay_arrow_position)
9859 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9861 int this_scroll_margin;
9862 struct glyph_row *row = NULL;
9864 #if GLYPH_DEBUG
9865 debug_method_add (w, "cursor movement");
9866 #endif
9868 /* Scroll if point within this distance from the top or bottom
9869 of the window. This is a pixel value. */
9870 this_scroll_margin = max (0, scroll_margin);
9871 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9872 this_scroll_margin *= CANON_Y_UNIT (f);
9874 /* Start with the row the cursor was displayed during the last
9875 not paused redisplay. Give up if that row is not valid. */
9876 if (w->last_cursor.vpos < 0
9877 || w->last_cursor.vpos >= w->current_matrix->nrows)
9878 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9879 else
9881 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9882 if (row->mode_line_p)
9883 ++row;
9884 if (!row->enabled_p)
9885 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9888 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9890 int scroll_p = 0;
9891 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9893 if (PT > XFASTINT (w->last_point))
9895 /* Point has moved forward. */
9896 while (MATRIX_ROW_END_CHARPOS (row) < PT
9897 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9899 xassert (row->enabled_p);
9900 ++row;
9903 /* The end position of a row equals the start position
9904 of the next row. If PT is there, we would rather
9905 display it in the next line. */
9906 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9907 && MATRIX_ROW_END_CHARPOS (row) == PT
9908 && !cursor_row_p (w, row))
9909 ++row;
9911 /* If within the scroll margin, scroll. Note that
9912 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9913 the next line would be drawn, and that
9914 this_scroll_margin can be zero. */
9915 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9916 || PT > MATRIX_ROW_END_CHARPOS (row)
9917 /* Line is completely visible last line in window
9918 and PT is to be set in the next line. */
9919 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9920 && PT == MATRIX_ROW_END_CHARPOS (row)
9921 && !row->ends_at_zv_p
9922 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9923 scroll_p = 1;
9925 else if (PT < XFASTINT (w->last_point))
9927 /* Cursor has to be moved backward. Note that PT >=
9928 CHARPOS (startp) because of the outer
9929 if-statement. */
9930 while (!row->mode_line_p
9931 && (MATRIX_ROW_START_CHARPOS (row) > PT
9932 || (MATRIX_ROW_START_CHARPOS (row) == PT
9933 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9934 && (row->y > this_scroll_margin
9935 || CHARPOS (startp) == BEGV))
9937 xassert (row->enabled_p);
9938 --row;
9941 /* Consider the following case: Window starts at BEGV,
9942 there is invisible, intangible text at BEGV, so that
9943 display starts at some point START > BEGV. It can
9944 happen that we are called with PT somewhere between
9945 BEGV and START. Try to handle that case. */
9946 if (row < w->current_matrix->rows
9947 || row->mode_line_p)
9949 row = w->current_matrix->rows;
9950 if (row->mode_line_p)
9951 ++row;
9954 /* Due to newlines in overlay strings, we may have to
9955 skip forward over overlay strings. */
9956 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9957 && MATRIX_ROW_END_CHARPOS (row) == PT
9958 && !cursor_row_p (w, row))
9959 ++row;
9961 /* If within the scroll margin, scroll. */
9962 if (row->y < this_scroll_margin
9963 && CHARPOS (startp) != BEGV)
9964 scroll_p = 1;
9967 if (PT < MATRIX_ROW_START_CHARPOS (row)
9968 || PT > MATRIX_ROW_END_CHARPOS (row))
9970 /* if PT is not in the glyph row, give up. */
9971 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9973 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9975 if (PT == MATRIX_ROW_END_CHARPOS (row)
9976 && !row->ends_at_zv_p
9977 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9978 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9979 else if (row->height > window_box_height (w))
9981 /* If we end up in a partially visible line, let's
9982 make it fully visible, except when it's taller
9983 than the window, in which case we can't do much
9984 about it. */
9985 *scroll_step = 1;
9986 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9988 else
9990 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9991 try_window (window, startp);
9992 if (!make_cursor_line_fully_visible (w))
9993 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9994 else
9995 rc = CURSOR_MOVEMENT_SUCCESS;
9998 else if (scroll_p)
9999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10000 else
10002 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10003 rc = CURSOR_MOVEMENT_SUCCESS;
10008 return rc;
10012 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10013 selected_window is redisplayed. */
10015 static void
10016 redisplay_window (window, just_this_one_p)
10017 Lisp_Object window;
10018 int just_this_one_p;
10020 struct window *w = XWINDOW (window);
10021 struct frame *f = XFRAME (w->frame);
10022 struct buffer *buffer = XBUFFER (w->buffer);
10023 struct buffer *old = current_buffer;
10024 struct text_pos lpoint, opoint, startp;
10025 int update_mode_line;
10026 int tem;
10027 struct it it;
10028 /* Record it now because it's overwritten. */
10029 int current_matrix_up_to_date_p = 0;
10030 int temp_scroll_step = 0;
10031 int count = BINDING_STACK_SIZE ();
10032 int rc;
10034 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10035 opoint = lpoint;
10037 /* W must be a leaf window here. */
10038 xassert (!NILP (w->buffer));
10039 #if GLYPH_DEBUG
10040 *w->desired_matrix->method = 0;
10041 #endif
10043 specbind (Qinhibit_point_motion_hooks, Qt);
10045 reconsider_clip_changes (w, buffer);
10047 /* Has the mode line to be updated? */
10048 update_mode_line = (!NILP (w->update_mode_line)
10049 || update_mode_lines
10050 || buffer->clip_changed);
10052 if (MINI_WINDOW_P (w))
10054 if (w == XWINDOW (echo_area_window)
10055 && !NILP (echo_area_buffer[0]))
10057 if (update_mode_line)
10058 /* We may have to update a tty frame's menu bar or a
10059 tool-bar. Example `M-x C-h C-h C-g'. */
10060 goto finish_menu_bars;
10061 else
10062 /* We've already displayed the echo area glyphs in this window. */
10063 goto finish_scroll_bars;
10065 else if (w != XWINDOW (minibuf_window))
10067 /* W is a mini-buffer window, but it's not the currently
10068 active one, so clear it. */
10069 int yb = window_text_bottom_y (w);
10070 struct glyph_row *row;
10071 int y;
10073 for (y = 0, row = w->desired_matrix->rows;
10074 y < yb;
10075 y += row->height, ++row)
10076 blank_row (w, row, y);
10077 goto finish_scroll_bars;
10080 clear_glyph_matrix (w->desired_matrix);
10083 /* Otherwise set up data on this window; select its buffer and point
10084 value. */
10085 /* Really select the buffer, for the sake of buffer-local
10086 variables. */
10087 set_buffer_internal_1 (XBUFFER (w->buffer));
10088 SET_TEXT_POS (opoint, PT, PT_BYTE);
10090 current_matrix_up_to_date_p
10091 = (!NILP (w->window_end_valid)
10092 && !current_buffer->clip_changed
10093 && XFASTINT (w->last_modified) >= MODIFF
10094 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10096 /* When windows_or_buffers_changed is non-zero, we can't rely on
10097 the window end being valid, so set it to nil there. */
10098 if (windows_or_buffers_changed)
10100 /* If window starts on a continuation line, maybe adjust the
10101 window start in case the window's width changed. */
10102 if (XMARKER (w->start)->buffer == current_buffer)
10103 compute_window_start_on_continuation_line (w);
10105 w->window_end_valid = Qnil;
10108 /* Some sanity checks. */
10109 CHECK_WINDOW_END (w);
10110 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10111 abort ();
10112 if (BYTEPOS (opoint) < CHARPOS (opoint))
10113 abort ();
10115 /* If %c is in mode line, update it if needed. */
10116 if (!NILP (w->column_number_displayed)
10117 /* This alternative quickly identifies a common case
10118 where no change is needed. */
10119 && !(PT == XFASTINT (w->last_point)
10120 && XFASTINT (w->last_modified) >= MODIFF
10121 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10122 && XFASTINT (w->column_number_displayed) != current_column ())
10123 update_mode_line = 1;
10125 /* Count number of windows showing the selected buffer. An indirect
10126 buffer counts as its base buffer. */
10127 if (!just_this_one_p)
10129 struct buffer *current_base, *window_base;
10130 current_base = current_buffer;
10131 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10132 if (current_base->base_buffer)
10133 current_base = current_base->base_buffer;
10134 if (window_base->base_buffer)
10135 window_base = window_base->base_buffer;
10136 if (current_base == window_base)
10137 buffer_shared++;
10140 /* Point refers normally to the selected window. For any other
10141 window, set up appropriate value. */
10142 if (!EQ (window, selected_window))
10144 int new_pt = XMARKER (w->pointm)->charpos;
10145 int new_pt_byte = marker_byte_position (w->pointm);
10146 if (new_pt < BEGV)
10148 new_pt = BEGV;
10149 new_pt_byte = BEGV_BYTE;
10150 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10152 else if (new_pt > (ZV - 1))
10154 new_pt = ZV;
10155 new_pt_byte = ZV_BYTE;
10156 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10159 /* We don't use SET_PT so that the point-motion hooks don't run. */
10160 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10163 /* If any of the character widths specified in the display table
10164 have changed, invalidate the width run cache. It's true that
10165 this may be a bit late to catch such changes, but the rest of
10166 redisplay goes (non-fatally) haywire when the display table is
10167 changed, so why should we worry about doing any better? */
10168 if (current_buffer->width_run_cache)
10170 struct Lisp_Char_Table *disptab = buffer_display_table ();
10172 if (! disptab_matches_widthtab (disptab,
10173 XVECTOR (current_buffer->width_table)))
10175 invalidate_region_cache (current_buffer,
10176 current_buffer->width_run_cache,
10177 BEG, Z);
10178 recompute_width_table (current_buffer, disptab);
10182 /* If window-start is screwed up, choose a new one. */
10183 if (XMARKER (w->start)->buffer != current_buffer)
10184 goto recenter;
10186 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10188 /* If someone specified a new starting point but did not insist,
10189 check whether it can be used. */
10190 if (!NILP (w->optional_new_start)
10191 && CHARPOS (startp) >= BEGV
10192 && CHARPOS (startp) <= ZV)
10194 w->optional_new_start = Qnil;
10195 start_display (&it, w, startp);
10196 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10197 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10198 if (IT_CHARPOS (it) == PT)
10199 w->force_start = Qt;
10202 /* Handle case where place to start displaying has been specified,
10203 unless the specified location is outside the accessible range. */
10204 if (!NILP (w->force_start)
10205 || w->frozen_window_start_p)
10207 w->force_start = Qnil;
10208 w->vscroll = 0;
10209 w->window_end_valid = Qnil;
10211 /* Forget any recorded base line for line number display. */
10212 if (!current_matrix_up_to_date_p
10213 || current_buffer->clip_changed)
10214 w->base_line_number = Qnil;
10216 /* Redisplay the mode line. Select the buffer properly for that.
10217 Also, run the hook window-scroll-functions
10218 because we have scrolled. */
10219 /* Note, we do this after clearing force_start because
10220 if there's an error, it is better to forget about force_start
10221 than to get into an infinite loop calling the hook functions
10222 and having them get more errors. */
10223 if (!update_mode_line
10224 || ! NILP (Vwindow_scroll_functions))
10226 update_mode_line = 1;
10227 w->update_mode_line = Qt;
10228 startp = run_window_scroll_functions (window, startp);
10231 w->last_modified = make_number (0);
10232 w->last_overlay_modified = make_number (0);
10233 if (CHARPOS (startp) < BEGV)
10234 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10235 else if (CHARPOS (startp) > ZV)
10236 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10238 /* Redisplay, then check if cursor has been set during the
10239 redisplay. Give up if new fonts were loaded. */
10240 if (!try_window (window, startp))
10242 w->force_start = Qt;
10243 clear_glyph_matrix (w->desired_matrix);
10244 goto finish_scroll_bars;
10247 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10249 /* If point does not appear, try to move point so it does
10250 appear. The desired matrix has been built above, so we
10251 can use it here. */
10252 int window_height;
10253 struct glyph_row *row;
10255 window_height = window_box_height (w) / 2;
10256 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10257 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10258 ++row;
10260 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10261 MATRIX_ROW_START_BYTEPOS (row));
10263 if (w != XWINDOW (selected_window))
10264 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10265 else if (current_buffer == old)
10266 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10268 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10270 /* If we are highlighting the region, then we just changed
10271 the region, so redisplay to show it. */
10272 if (!NILP (Vtransient_mark_mode)
10273 && !NILP (current_buffer->mark_active))
10275 clear_glyph_matrix (w->desired_matrix);
10276 if (!try_window (window, startp))
10277 goto need_larger_matrices;
10281 if (!make_cursor_line_fully_visible (w))
10282 goto need_larger_matrices;
10283 #if GLYPH_DEBUG
10284 debug_method_add (w, "forced window start");
10285 #endif
10286 goto done;
10289 /* Handle case where text has not changed, only point, and it has
10290 not moved off the frame. */
10291 if (current_matrix_up_to_date_p
10292 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10293 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10295 switch (rc)
10297 case CURSOR_MOVEMENT_SUCCESS:
10298 goto done;
10300 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10301 goto need_larger_matrices;
10303 case CURSOR_MOVEMENT_MUST_SCROLL:
10304 goto try_to_scroll;
10306 default:
10307 abort ();
10310 /* If current starting point was originally the beginning of a line
10311 but no longer is, find a new starting point. */
10312 else if (!NILP (w->start_at_line_beg)
10313 && !(CHARPOS (startp) <= BEGV
10314 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10316 #if GLYPH_DEBUG
10317 debug_method_add (w, "recenter 1");
10318 #endif
10319 goto recenter;
10322 /* Try scrolling with try_window_id. Value is > 0 if update has
10323 been done, it is -1 if we know that the same window start will
10324 not work. It is 0 if unsuccessful for some other reason. */
10325 else if ((tem = try_window_id (w)) != 0)
10327 #if GLYPH_DEBUG
10328 debug_method_add (w, "try_window_id %d", tem);
10329 #endif
10331 if (fonts_changed_p)
10332 goto need_larger_matrices;
10333 if (tem > 0)
10334 goto done;
10336 /* Otherwise try_window_id has returned -1 which means that we
10337 don't want the alternative below this comment to execute. */
10339 else if (CHARPOS (startp) >= BEGV
10340 && CHARPOS (startp) <= ZV
10341 && PT >= CHARPOS (startp)
10342 && (CHARPOS (startp) < ZV
10343 /* Avoid starting at end of buffer. */
10344 || CHARPOS (startp) == BEGV
10345 || (XFASTINT (w->last_modified) >= MODIFF
10346 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10348 #if GLYPH_DEBUG
10349 debug_method_add (w, "same window start");
10350 #endif
10352 /* Try to redisplay starting at same place as before.
10353 If point has not moved off frame, accept the results. */
10354 if (!current_matrix_up_to_date_p
10355 /* Don't use try_window_reusing_current_matrix in this case
10356 because a window scroll function can have changed the
10357 buffer. */
10358 || !NILP (Vwindow_scroll_functions)
10359 || MINI_WINDOW_P (w)
10360 || !try_window_reusing_current_matrix (w))
10362 IF_DEBUG (debug_method_add (w, "1"));
10363 try_window (window, startp);
10366 if (fonts_changed_p)
10367 goto need_larger_matrices;
10369 if (w->cursor.vpos >= 0)
10371 if (!just_this_one_p
10372 || current_buffer->clip_changed
10373 || BEG_UNCHANGED < CHARPOS (startp))
10374 /* Forget any recorded base line for line number display. */
10375 w->base_line_number = Qnil;
10377 if (!make_cursor_line_fully_visible (w))
10378 goto need_larger_matrices;
10379 goto done;
10381 else
10382 clear_glyph_matrix (w->desired_matrix);
10385 try_to_scroll:
10387 w->last_modified = make_number (0);
10388 w->last_overlay_modified = make_number (0);
10390 /* Redisplay the mode line. Select the buffer properly for that. */
10391 if (!update_mode_line)
10393 update_mode_line = 1;
10394 w->update_mode_line = Qt;
10397 /* Try to scroll by specified few lines. */
10398 if ((scroll_conservatively
10399 || scroll_step
10400 || temp_scroll_step
10401 || NUMBERP (current_buffer->scroll_up_aggressively)
10402 || NUMBERP (current_buffer->scroll_down_aggressively))
10403 && !current_buffer->clip_changed
10404 && CHARPOS (startp) >= BEGV
10405 && CHARPOS (startp) <= ZV)
10407 /* The function returns -1 if new fonts were loaded, 1 if
10408 successful, 0 if not successful. */
10409 int rc = try_scrolling (window, just_this_one_p,
10410 scroll_conservatively,
10411 scroll_step,
10412 temp_scroll_step);
10413 switch (rc)
10415 case SCROLLING_SUCCESS:
10416 goto done;
10418 case SCROLLING_NEED_LARGER_MATRICES:
10419 goto need_larger_matrices;
10421 case SCROLLING_FAILED:
10422 break;
10424 default:
10425 abort ();
10429 /* Finally, just choose place to start which centers point */
10431 recenter:
10433 #if GLYPH_DEBUG
10434 debug_method_add (w, "recenter");
10435 #endif
10437 /* w->vscroll = 0; */
10439 /* Forget any previously recorded base line for line number display. */
10440 if (!current_matrix_up_to_date_p
10441 || current_buffer->clip_changed)
10442 w->base_line_number = Qnil;
10444 /* Move backward half the height of the window. */
10445 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10446 it.current_y = it.last_visible_y;
10447 move_it_vertically_backward (&it, window_box_height (w) / 2);
10448 xassert (IT_CHARPOS (it) >= BEGV);
10450 /* The function move_it_vertically_backward may move over more
10451 than the specified y-distance. If it->w is small, e.g. a
10452 mini-buffer window, we may end up in front of the window's
10453 display area. Start displaying at the start of the line
10454 containing PT in this case. */
10455 if (it.current_y <= 0)
10457 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10458 move_it_vertically (&it, 0);
10459 xassert (IT_CHARPOS (it) <= PT);
10460 it.current_y = 0;
10463 it.current_x = it.hpos = 0;
10465 /* Set startp here explicitly in case that helps avoid an infinite loop
10466 in case the window-scroll-functions functions get errors. */
10467 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10469 /* Run scroll hooks. */
10470 startp = run_window_scroll_functions (window, it.current.pos);
10472 /* Redisplay the window. */
10473 if (!current_matrix_up_to_date_p
10474 || windows_or_buffers_changed
10475 /* Don't use try_window_reusing_current_matrix in this case
10476 because it can have changed the buffer. */
10477 || !NILP (Vwindow_scroll_functions)
10478 || !just_this_one_p
10479 || MINI_WINDOW_P (w)
10480 || !try_window_reusing_current_matrix (w))
10481 try_window (window, startp);
10483 /* If new fonts have been loaded (due to fontsets), give up. We
10484 have to start a new redisplay since we need to re-adjust glyph
10485 matrices. */
10486 if (fonts_changed_p)
10487 goto need_larger_matrices;
10489 /* If cursor did not appear assume that the middle of the window is
10490 in the first line of the window. Do it again with the next line.
10491 (Imagine a window of height 100, displaying two lines of height
10492 60. Moving back 50 from it->last_visible_y will end in the first
10493 line.) */
10494 if (w->cursor.vpos < 0)
10496 if (!NILP (w->window_end_valid)
10497 && PT >= Z - XFASTINT (w->window_end_pos))
10499 clear_glyph_matrix (w->desired_matrix);
10500 move_it_by_lines (&it, 1, 0);
10501 try_window (window, it.current.pos);
10503 else if (PT < IT_CHARPOS (it))
10505 clear_glyph_matrix (w->desired_matrix);
10506 move_it_by_lines (&it, -1, 0);
10507 try_window (window, it.current.pos);
10509 else
10511 /* Not much we can do about it. */
10515 /* Consider the following case: Window starts at BEGV, there is
10516 invisible, intangible text at BEGV, so that display starts at
10517 some point START > BEGV. It can happen that we are called with
10518 PT somewhere between BEGV and START. Try to handle that case. */
10519 if (w->cursor.vpos < 0)
10521 struct glyph_row *row = w->current_matrix->rows;
10522 if (row->mode_line_p)
10523 ++row;
10524 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10527 if (!make_cursor_line_fully_visible (w))
10528 goto need_larger_matrices;
10530 done:
10532 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10533 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10534 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10535 ? Qt : Qnil);
10537 /* Display the mode line, if we must. */
10538 if ((update_mode_line
10539 /* If window not full width, must redo its mode line
10540 if (a) the window to its side is being redone and
10541 (b) we do a frame-based redisplay. This is a consequence
10542 of how inverted lines are drawn in frame-based redisplay. */
10543 || (!just_this_one_p
10544 && !FRAME_WINDOW_P (f)
10545 && !WINDOW_FULL_WIDTH_P (w))
10546 /* Line number to display. */
10547 || INTEGERP (w->base_line_pos)
10548 /* Column number is displayed and different from the one displayed. */
10549 || (!NILP (w->column_number_displayed)
10550 && XFASTINT (w->column_number_displayed) != current_column ()))
10551 /* This means that the window has a mode line. */
10552 && (WINDOW_WANTS_MODELINE_P (w)
10553 || WINDOW_WANTS_HEADER_LINE_P (w)))
10555 display_mode_lines (w);
10557 /* If mode line height has changed, arrange for a thorough
10558 immediate redisplay using the correct mode line height. */
10559 if (WINDOW_WANTS_MODELINE_P (w)
10560 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10562 fonts_changed_p = 1;
10563 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10564 = DESIRED_MODE_LINE_HEIGHT (w);
10567 /* If top line height has changed, arrange for a thorough
10568 immediate redisplay using the correct mode line height. */
10569 if (WINDOW_WANTS_HEADER_LINE_P (w)
10570 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10572 fonts_changed_p = 1;
10573 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10574 = DESIRED_HEADER_LINE_HEIGHT (w);
10577 if (fonts_changed_p)
10578 goto need_larger_matrices;
10581 if (!line_number_displayed
10582 && !BUFFERP (w->base_line_pos))
10584 w->base_line_pos = Qnil;
10585 w->base_line_number = Qnil;
10588 finish_menu_bars:
10590 /* When we reach a frame's selected window, redo the frame's menu bar. */
10591 if (update_mode_line
10592 && EQ (FRAME_SELECTED_WINDOW (f), window))
10594 int redisplay_menu_p = 0;
10596 if (FRAME_WINDOW_P (f))
10598 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10599 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10600 #else
10601 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10602 #endif
10604 else
10605 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10607 if (redisplay_menu_p)
10608 display_menu_bar (w);
10610 #ifdef HAVE_WINDOW_SYSTEM
10611 if (WINDOWP (f->tool_bar_window)
10612 && (FRAME_TOOL_BAR_LINES (f) > 0
10613 || auto_resize_tool_bars_p))
10614 redisplay_tool_bar (f);
10615 #endif
10618 need_larger_matrices:
10620 finish_scroll_bars:
10622 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10624 int start, end, whole;
10626 /* Calculate the start and end positions for the current window.
10627 At some point, it would be nice to choose between scrollbars
10628 which reflect the whole buffer size, with special markers
10629 indicating narrowing, and scrollbars which reflect only the
10630 visible region.
10632 Note that mini-buffers sometimes aren't displaying any text. */
10633 if (!MINI_WINDOW_P (w)
10634 || (w == XWINDOW (minibuf_window)
10635 && NILP (echo_area_buffer[0])))
10637 whole = ZV - BEGV;
10638 start = marker_position (w->start) - BEGV;
10639 /* I don't think this is guaranteed to be right. For the
10640 moment, we'll pretend it is. */
10641 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10643 if (end < start)
10644 end = start;
10645 if (whole < (end - start))
10646 whole = end - start;
10648 else
10649 start = end = whole = 0;
10651 /* Indicate what this scroll bar ought to be displaying now. */
10652 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10654 /* Note that we actually used the scroll bar attached to this
10655 window, so it shouldn't be deleted at the end of redisplay. */
10656 redeem_scroll_bar_hook (w);
10659 /* Restore current_buffer and value of point in it. */
10660 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10661 set_buffer_internal_1 (old);
10662 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10664 unbind_to (count, Qnil);
10668 /* Build the complete desired matrix of WINDOW with a window start
10669 buffer position POS. Value is non-zero if successful. It is zero
10670 if fonts were loaded during redisplay which makes re-adjusting
10671 glyph matrices necessary. */
10674 try_window (window, pos)
10675 Lisp_Object window;
10676 struct text_pos pos;
10678 struct window *w = XWINDOW (window);
10679 struct it it;
10680 struct glyph_row *last_text_row = NULL;
10682 /* Make POS the new window start. */
10683 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10685 /* Mark cursor position as unknown. No overlay arrow seen. */
10686 w->cursor.vpos = -1;
10687 overlay_arrow_seen = 0;
10689 /* Initialize iterator and info to start at POS. */
10690 start_display (&it, w, pos);
10692 /* Display all lines of W. */
10693 while (it.current_y < it.last_visible_y)
10695 if (display_line (&it))
10696 last_text_row = it.glyph_row - 1;
10697 if (fonts_changed_p)
10698 return 0;
10701 /* If bottom moved off end of frame, change mode line percentage. */
10702 if (XFASTINT (w->window_end_pos) <= 0
10703 && Z != IT_CHARPOS (it))
10704 w->update_mode_line = Qt;
10706 /* Set window_end_pos to the offset of the last character displayed
10707 on the window from the end of current_buffer. Set
10708 window_end_vpos to its row number. */
10709 if (last_text_row)
10711 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10712 w->window_end_bytepos
10713 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10714 w->window_end_pos
10715 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10716 w->window_end_vpos
10717 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10718 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10719 ->displays_text_p);
10721 else
10723 w->window_end_bytepos = 0;
10724 w->window_end_pos = w->window_end_vpos = make_number (0);
10727 /* But that is not valid info until redisplay finishes. */
10728 w->window_end_valid = Qnil;
10729 return 1;
10734 /************************************************************************
10735 Window redisplay reusing current matrix when buffer has not changed
10736 ************************************************************************/
10738 /* Try redisplay of window W showing an unchanged buffer with a
10739 different window start than the last time it was displayed by
10740 reusing its current matrix. Value is non-zero if successful.
10741 W->start is the new window start. */
10743 static int
10744 try_window_reusing_current_matrix (w)
10745 struct window *w;
10747 struct frame *f = XFRAME (w->frame);
10748 struct glyph_row *row, *bottom_row;
10749 struct it it;
10750 struct run run;
10751 struct text_pos start, new_start;
10752 int nrows_scrolled, i;
10753 struct glyph_row *last_text_row;
10754 struct glyph_row *last_reused_text_row;
10755 struct glyph_row *start_row;
10756 int start_vpos, min_y, max_y;
10758 #if GLYPH_DEBUG
10759 if (inhibit_try_window_reusing)
10760 return 0;
10761 #endif
10763 if (/* This function doesn't handle terminal frames. */
10764 !FRAME_WINDOW_P (f)
10765 /* Don't try to reuse the display if windows have been split
10766 or such. */
10767 || windows_or_buffers_changed)
10768 return 0;
10770 /* Can't do this if region may have changed. */
10771 if ((!NILP (Vtransient_mark_mode)
10772 && !NILP (current_buffer->mark_active))
10773 || !NILP (w->region_showing)
10774 || !NILP (Vshow_trailing_whitespace))
10775 return 0;
10777 /* If top-line visibility has changed, give up. */
10778 if (WINDOW_WANTS_HEADER_LINE_P (w)
10779 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10780 return 0;
10782 /* Give up if old or new display is scrolled vertically. We could
10783 make this function handle this, but right now it doesn't. */
10784 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10785 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10786 return 0;
10788 /* The variable new_start now holds the new window start. The old
10789 start `start' can be determined from the current matrix. */
10790 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10791 start = start_row->start.pos;
10792 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10794 /* Clear the desired matrix for the display below. */
10795 clear_glyph_matrix (w->desired_matrix);
10797 if (CHARPOS (new_start) <= CHARPOS (start))
10799 int first_row_y;
10801 /* Don't use this method if the display starts with an ellipsis
10802 displayed for invisible text. It's not easy to handle that case
10803 below, and it's certainly not worth the effort since this is
10804 not a frequent case. */
10805 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10806 return 0;
10808 IF_DEBUG (debug_method_add (w, "twu1"));
10810 /* Display up to a row that can be reused. The variable
10811 last_text_row is set to the last row displayed that displays
10812 text. Note that it.vpos == 0 if or if not there is a
10813 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10814 start_display (&it, w, new_start);
10815 first_row_y = it.current_y;
10816 w->cursor.vpos = -1;
10817 last_text_row = last_reused_text_row = NULL;
10819 while (it.current_y < it.last_visible_y
10820 && IT_CHARPOS (it) < CHARPOS (start)
10821 && !fonts_changed_p)
10822 if (display_line (&it))
10823 last_text_row = it.glyph_row - 1;
10825 /* A value of current_y < last_visible_y means that we stopped
10826 at the previous window start, which in turn means that we
10827 have at least one reusable row. */
10828 if (it.current_y < it.last_visible_y)
10830 /* IT.vpos always starts from 0; it counts text lines. */
10831 nrows_scrolled = it.vpos;
10833 /* Find PT if not already found in the lines displayed. */
10834 if (w->cursor.vpos < 0)
10836 int dy = it.current_y - first_row_y;
10838 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10839 row = row_containing_pos (w, PT, row, NULL, dy);
10840 if (row)
10841 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10842 dy, nrows_scrolled);
10843 else
10845 clear_glyph_matrix (w->desired_matrix);
10846 return 0;
10850 /* Scroll the display. Do it before the current matrix is
10851 changed. The problem here is that update has not yet
10852 run, i.e. part of the current matrix is not up to date.
10853 scroll_run_hook will clear the cursor, and use the
10854 current matrix to get the height of the row the cursor is
10855 in. */
10856 run.current_y = first_row_y;
10857 run.desired_y = it.current_y;
10858 run.height = it.last_visible_y - it.current_y;
10860 if (run.height > 0 && run.current_y != run.desired_y)
10862 update_begin (f);
10863 rif->update_window_begin_hook (w);
10864 rif->clear_mouse_face (w);
10865 rif->scroll_run_hook (w, &run);
10866 rif->update_window_end_hook (w, 0, 0);
10867 update_end (f);
10870 /* Shift current matrix down by nrows_scrolled lines. */
10871 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10872 rotate_matrix (w->current_matrix,
10873 start_vpos,
10874 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10875 nrows_scrolled);
10877 /* Disable lines that must be updated. */
10878 for (i = 0; i < it.vpos; ++i)
10879 (start_row + i)->enabled_p = 0;
10881 /* Re-compute Y positions. */
10882 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10883 max_y = it.last_visible_y;
10884 for (row = start_row + nrows_scrolled;
10885 row < bottom_row;
10886 ++row)
10888 row->y = it.current_y;
10889 row->visible_height = row->height;
10891 if (row->y < min_y)
10892 row->visible_height -= min_y - row->y;
10893 if (row->y + row->height > max_y)
10894 row->visible_height -= row->y + row->height - max_y;
10896 it.current_y += row->height;
10898 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10899 last_reused_text_row = row;
10900 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10901 break;
10904 /* Disable lines in the current matrix which are now
10905 below the window. */
10906 for (++row; row < bottom_row; ++row)
10907 row->enabled_p = 0;
10910 /* Update window_end_pos etc.; last_reused_text_row is the last
10911 reused row from the current matrix containing text, if any.
10912 The value of last_text_row is the last displayed line
10913 containing text. */
10914 if (last_reused_text_row)
10916 w->window_end_bytepos
10917 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10918 w->window_end_pos
10919 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10920 w->window_end_vpos
10921 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10922 w->current_matrix));
10924 else if (last_text_row)
10926 w->window_end_bytepos
10927 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10928 w->window_end_pos
10929 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10930 w->window_end_vpos
10931 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10933 else
10935 /* This window must be completely empty. */
10936 w->window_end_bytepos = 0;
10937 w->window_end_pos = w->window_end_vpos = make_number (0);
10939 w->window_end_valid = Qnil;
10941 /* Update hint: don't try scrolling again in update_window. */
10942 w->desired_matrix->no_scrolling_p = 1;
10944 #if GLYPH_DEBUG
10945 debug_method_add (w, "try_window_reusing_current_matrix 1");
10946 #endif
10947 return 1;
10949 else if (CHARPOS (new_start) > CHARPOS (start))
10951 struct glyph_row *pt_row, *row;
10952 struct glyph_row *first_reusable_row;
10953 struct glyph_row *first_row_to_display;
10954 int dy;
10955 int yb = window_text_bottom_y (w);
10957 /* Find the row starting at new_start, if there is one. Don't
10958 reuse a partially visible line at the end. */
10959 first_reusable_row = start_row;
10960 while (first_reusable_row->enabled_p
10961 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10962 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10963 < CHARPOS (new_start)))
10964 ++first_reusable_row;
10966 /* Give up if there is no row to reuse. */
10967 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10968 || !first_reusable_row->enabled_p
10969 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10970 != CHARPOS (new_start)))
10971 return 0;
10973 /* We can reuse fully visible rows beginning with
10974 first_reusable_row to the end of the window. Set
10975 first_row_to_display to the first row that cannot be reused.
10976 Set pt_row to the row containing point, if there is any. */
10977 pt_row = NULL;
10978 for (first_row_to_display = first_reusable_row;
10979 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10980 ++first_row_to_display)
10982 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10983 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10984 pt_row = first_row_to_display;
10987 /* Start displaying at the start of first_row_to_display. */
10988 xassert (first_row_to_display->y < yb);
10989 init_to_row_start (&it, w, first_row_to_display);
10991 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10992 - start_vpos);
10993 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10994 - nrows_scrolled);
10995 it.current_y = (first_row_to_display->y - first_reusable_row->y
10996 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10998 /* Display lines beginning with first_row_to_display in the
10999 desired matrix. Set last_text_row to the last row displayed
11000 that displays text. */
11001 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11002 if (pt_row == NULL)
11003 w->cursor.vpos = -1;
11004 last_text_row = NULL;
11005 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11006 if (display_line (&it))
11007 last_text_row = it.glyph_row - 1;
11009 /* Give up If point isn't in a row displayed or reused. */
11010 if (w->cursor.vpos < 0)
11012 clear_glyph_matrix (w->desired_matrix);
11013 return 0;
11016 /* If point is in a reused row, adjust y and vpos of the cursor
11017 position. */
11018 if (pt_row)
11020 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11021 w->current_matrix);
11022 w->cursor.y -= first_reusable_row->y;
11025 /* Scroll the display. */
11026 run.current_y = first_reusable_row->y;
11027 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11028 run.height = it.last_visible_y - run.current_y;
11029 dy = run.current_y - run.desired_y;
11031 if (run.height)
11033 struct frame *f = XFRAME (WINDOW_FRAME (w));
11034 update_begin (f);
11035 rif->update_window_begin_hook (w);
11036 rif->clear_mouse_face (w);
11037 rif->scroll_run_hook (w, &run);
11038 rif->update_window_end_hook (w, 0, 0);
11039 update_end (f);
11042 /* Adjust Y positions of reused rows. */
11043 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11044 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11045 max_y = it.last_visible_y;
11046 for (row = first_reusable_row; row < first_row_to_display; ++row)
11048 row->y -= dy;
11049 row->visible_height = row->height;
11050 if (row->y < min_y)
11051 row->visible_height -= min_y - row->y;
11052 if (row->y + row->height > max_y)
11053 row->visible_height -= row->y + row->height - max_y;
11056 /* Scroll the current matrix. */
11057 xassert (nrows_scrolled > 0);
11058 rotate_matrix (w->current_matrix,
11059 start_vpos,
11060 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11061 -nrows_scrolled);
11063 /* Disable rows not reused. */
11064 for (row -= nrows_scrolled; row < bottom_row; ++row)
11065 row->enabled_p = 0;
11067 /* Adjust window end. A null value of last_text_row means that
11068 the window end is in reused rows which in turn means that
11069 only its vpos can have changed. */
11070 if (last_text_row)
11072 w->window_end_bytepos
11073 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11074 w->window_end_pos
11075 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11076 w->window_end_vpos
11077 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11079 else
11081 w->window_end_vpos
11082 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11085 w->window_end_valid = Qnil;
11086 w->desired_matrix->no_scrolling_p = 1;
11088 #if GLYPH_DEBUG
11089 debug_method_add (w, "try_window_reusing_current_matrix 2");
11090 #endif
11091 return 1;
11094 return 0;
11099 /************************************************************************
11100 Window redisplay reusing current matrix when buffer has changed
11101 ************************************************************************/
11103 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11104 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11105 int *, int *));
11106 static struct glyph_row *
11107 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11108 struct glyph_row *));
11111 /* Return the last row in MATRIX displaying text. If row START is
11112 non-null, start searching with that row. IT gives the dimensions
11113 of the display. Value is null if matrix is empty; otherwise it is
11114 a pointer to the row found. */
11116 static struct glyph_row *
11117 find_last_row_displaying_text (matrix, it, start)
11118 struct glyph_matrix *matrix;
11119 struct it *it;
11120 struct glyph_row *start;
11122 struct glyph_row *row, *row_found;
11124 /* Set row_found to the last row in IT->w's current matrix
11125 displaying text. The loop looks funny but think of partially
11126 visible lines. */
11127 row_found = NULL;
11128 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11129 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11131 xassert (row->enabled_p);
11132 row_found = row;
11133 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11134 break;
11135 ++row;
11138 return row_found;
11142 /* Return the last row in the current matrix of W that is not affected
11143 by changes at the start of current_buffer that occurred since W's
11144 current matrix was built. Value is null if no such row exists.
11146 BEG_UNCHANGED us the number of characters unchanged at the start of
11147 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11148 first changed character in current_buffer. Characters at positions <
11149 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11150 when the current matrix was built. */
11152 static struct glyph_row *
11153 find_last_unchanged_at_beg_row (w)
11154 struct window *w;
11156 int first_changed_pos = BEG + BEG_UNCHANGED;
11157 struct glyph_row *row;
11158 struct glyph_row *row_found = NULL;
11159 int yb = window_text_bottom_y (w);
11161 /* Find the last row displaying unchanged text. */
11162 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11163 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11164 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11166 if (/* If row ends before first_changed_pos, it is unchanged,
11167 except in some case. */
11168 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11169 /* When row ends in ZV and we write at ZV it is not
11170 unchanged. */
11171 && !row->ends_at_zv_p
11172 /* When first_changed_pos is the end of a continued line,
11173 row is not unchanged because it may be no longer
11174 continued. */
11175 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11176 && row->continued_p))
11177 row_found = row;
11179 /* Stop if last visible row. */
11180 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11181 break;
11183 ++row;
11186 return row_found;
11190 /* Find the first glyph row in the current matrix of W that is not
11191 affected by changes at the end of current_buffer since the
11192 time W's current matrix was built.
11194 Return in *DELTA the number of chars by which buffer positions in
11195 unchanged text at the end of current_buffer must be adjusted.
11197 Return in *DELTA_BYTES the corresponding number of bytes.
11199 Value is null if no such row exists, i.e. all rows are affected by
11200 changes. */
11202 static struct glyph_row *
11203 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11204 struct window *w;
11205 int *delta, *delta_bytes;
11207 struct glyph_row *row;
11208 struct glyph_row *row_found = NULL;
11210 *delta = *delta_bytes = 0;
11212 /* Display must not have been paused, otherwise the current matrix
11213 is not up to date. */
11214 if (NILP (w->window_end_valid))
11215 abort ();
11217 /* A value of window_end_pos >= END_UNCHANGED means that the window
11218 end is in the range of changed text. If so, there is no
11219 unchanged row at the end of W's current matrix. */
11220 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11221 return NULL;
11223 /* Set row to the last row in W's current matrix displaying text. */
11224 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11226 /* If matrix is entirely empty, no unchanged row exists. */
11227 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11229 /* The value of row is the last glyph row in the matrix having a
11230 meaningful buffer position in it. The end position of row
11231 corresponds to window_end_pos. This allows us to translate
11232 buffer positions in the current matrix to current buffer
11233 positions for characters not in changed text. */
11234 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11235 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11236 int last_unchanged_pos, last_unchanged_pos_old;
11237 struct glyph_row *first_text_row
11238 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11240 *delta = Z - Z_old;
11241 *delta_bytes = Z_BYTE - Z_BYTE_old;
11243 /* Set last_unchanged_pos to the buffer position of the last
11244 character in the buffer that has not been changed. Z is the
11245 index + 1 of the last character in current_buffer, i.e. by
11246 subtracting END_UNCHANGED we get the index of the last
11247 unchanged character, and we have to add BEG to get its buffer
11248 position. */
11249 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11250 last_unchanged_pos_old = last_unchanged_pos - *delta;
11252 /* Search backward from ROW for a row displaying a line that
11253 starts at a minimum position >= last_unchanged_pos_old. */
11254 for (; row > first_text_row; --row)
11256 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11257 abort ();
11259 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11260 row_found = row;
11264 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11265 abort ();
11267 return row_found;
11271 /* Make sure that glyph rows in the current matrix of window W
11272 reference the same glyph memory as corresponding rows in the
11273 frame's frame matrix. This function is called after scrolling W's
11274 current matrix on a terminal frame in try_window_id and
11275 try_window_reusing_current_matrix. */
11277 static void
11278 sync_frame_with_window_matrix_rows (w)
11279 struct window *w;
11281 struct frame *f = XFRAME (w->frame);
11282 struct glyph_row *window_row, *window_row_end, *frame_row;
11284 /* Preconditions: W must be a leaf window and full-width. Its frame
11285 must have a frame matrix. */
11286 xassert (NILP (w->hchild) && NILP (w->vchild));
11287 xassert (WINDOW_FULL_WIDTH_P (w));
11288 xassert (!FRAME_WINDOW_P (f));
11290 /* If W is a full-width window, glyph pointers in W's current matrix
11291 have, by definition, to be the same as glyph pointers in the
11292 corresponding frame matrix. */
11293 window_row = w->current_matrix->rows;
11294 window_row_end = window_row + w->current_matrix->nrows;
11295 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11296 while (window_row < window_row_end)
11298 int area;
11300 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11301 frame_row->glyphs[area] = window_row->glyphs[area];
11303 /* Disable frame rows whose corresponding window rows have
11304 been disabled in try_window_id. */
11305 if (!window_row->enabled_p)
11306 frame_row->enabled_p = 0;
11308 ++window_row, ++frame_row;
11313 /* Find the glyph row in window W containing CHARPOS. Consider all
11314 rows between START and END (not inclusive). END null means search
11315 all rows to the end of the display area of W. Value is the row
11316 containing CHARPOS or null. */
11318 struct glyph_row *
11319 row_containing_pos (w, charpos, start, end, dy)
11320 struct window *w;
11321 int charpos;
11322 struct glyph_row *start, *end;
11323 int dy;
11325 struct glyph_row *row = start;
11326 int last_y;
11328 /* If we happen to start on a header-line, skip that. */
11329 if (row->mode_line_p)
11330 ++row;
11332 if ((end && row >= end) || !row->enabled_p)
11333 return NULL;
11335 last_y = window_text_bottom_y (w) - dy;
11337 while ((end == NULL || row < end)
11338 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11339 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11340 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11341 /* The end position of a row equals the start
11342 position of the next row. If CHARPOS is there, we
11343 would rather display it in the next line, except
11344 when this line ends in ZV. */
11345 && !row->ends_at_zv_p
11346 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11347 ++row;
11349 /* Give up if CHARPOS not found. */
11350 if ((end && row >= end)
11351 || charpos < MATRIX_ROW_START_CHARPOS (row)
11352 || charpos > MATRIX_ROW_END_CHARPOS (row))
11353 row = NULL;
11355 return row;
11359 /* Try to redisplay window W by reusing its existing display. W's
11360 current matrix must be up to date when this function is called,
11361 i.e. window_end_valid must not be nil.
11363 Value is
11365 1 if display has been updated
11366 0 if otherwise unsuccessful
11367 -1 if redisplay with same window start is known not to succeed
11369 The following steps are performed:
11371 1. Find the last row in the current matrix of W that is not
11372 affected by changes at the start of current_buffer. If no such row
11373 is found, give up.
11375 2. Find the first row in W's current matrix that is not affected by
11376 changes at the end of current_buffer. Maybe there is no such row.
11378 3. Display lines beginning with the row + 1 found in step 1 to the
11379 row found in step 2 or, if step 2 didn't find a row, to the end of
11380 the window.
11382 4. If cursor is not known to appear on the window, give up.
11384 5. If display stopped at the row found in step 2, scroll the
11385 display and current matrix as needed.
11387 6. Maybe display some lines at the end of W, if we must. This can
11388 happen under various circumstances, like a partially visible line
11389 becoming fully visible, or because newly displayed lines are displayed
11390 in smaller font sizes.
11392 7. Update W's window end information. */
11394 static int
11395 try_window_id (w)
11396 struct window *w;
11398 struct frame *f = XFRAME (w->frame);
11399 struct glyph_matrix *current_matrix = w->current_matrix;
11400 struct glyph_matrix *desired_matrix = w->desired_matrix;
11401 struct glyph_row *last_unchanged_at_beg_row;
11402 struct glyph_row *first_unchanged_at_end_row;
11403 struct glyph_row *row;
11404 struct glyph_row *bottom_row;
11405 int bottom_vpos;
11406 struct it it;
11407 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11408 struct text_pos start_pos;
11409 struct run run;
11410 int first_unchanged_at_end_vpos = 0;
11411 struct glyph_row *last_text_row, *last_text_row_at_end;
11412 struct text_pos start;
11413 int first_changed_charpos, last_changed_charpos;
11415 #if GLYPH_DEBUG
11416 if (inhibit_try_window_id)
11417 return 0;
11418 #endif
11420 /* This is handy for debugging. */
11421 #if 0
11422 #define GIVE_UP(X) \
11423 do { \
11424 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11425 return 0; \
11426 } while (0)
11427 #else
11428 #define GIVE_UP(X) return 0
11429 #endif
11431 SET_TEXT_POS_FROM_MARKER (start, w->start);
11433 /* Don't use this for mini-windows because these can show
11434 messages and mini-buffers, and we don't handle that here. */
11435 if (MINI_WINDOW_P (w))
11436 GIVE_UP (1);
11438 /* This flag is used to prevent redisplay optimizations. */
11439 if (windows_or_buffers_changed)
11440 GIVE_UP (2);
11442 /* Verify that narrowing has not changed. This flag is also set to prevent
11443 redisplay optimizations. It would be nice to further
11444 reduce the number of cases where this prevents try_window_id. */
11445 if (current_buffer->clip_changed)
11446 GIVE_UP (3);
11448 /* Window must either use window-based redisplay or be full width. */
11449 if (!FRAME_WINDOW_P (f)
11450 && (!line_ins_del_ok
11451 || !WINDOW_FULL_WIDTH_P (w)))
11452 GIVE_UP (4);
11454 /* Give up if point is not known NOT to appear in W. */
11455 if (PT < CHARPOS (start))
11456 GIVE_UP (5);
11458 /* Another way to prevent redisplay optimizations. */
11459 if (XFASTINT (w->last_modified) == 0)
11460 GIVE_UP (6);
11462 /* Verify that window is not hscrolled. */
11463 if (XFASTINT (w->hscroll) != 0)
11464 GIVE_UP (7);
11466 /* Verify that display wasn't paused. */
11467 if (NILP (w->window_end_valid))
11468 GIVE_UP (8);
11470 /* Can't use this if highlighting a region because a cursor movement
11471 will do more than just set the cursor. */
11472 if (!NILP (Vtransient_mark_mode)
11473 && !NILP (current_buffer->mark_active))
11474 GIVE_UP (9);
11476 /* Likewise if highlighting trailing whitespace. */
11477 if (!NILP (Vshow_trailing_whitespace))
11478 GIVE_UP (11);
11480 /* Likewise if showing a region. */
11481 if (!NILP (w->region_showing))
11482 GIVE_UP (10);
11484 /* Can use this if overlay arrow position and or string have changed. */
11485 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11486 || !EQ (last_arrow_string, Voverlay_arrow_string))
11487 GIVE_UP (12);
11490 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11491 only if buffer has really changed. The reason is that the gap is
11492 initially at Z for freshly visited files. The code below would
11493 set end_unchanged to 0 in that case. */
11494 if (MODIFF > SAVE_MODIFF
11495 /* This seems to happen sometimes after saving a buffer. */
11496 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11498 if (GPT - BEG < BEG_UNCHANGED)
11499 BEG_UNCHANGED = GPT - BEG;
11500 if (Z - GPT < END_UNCHANGED)
11501 END_UNCHANGED = Z - GPT;
11504 /* The position of the first and last character that has been changed. */
11505 first_changed_charpos = BEG + BEG_UNCHANGED;
11506 last_changed_charpos = Z - END_UNCHANGED;
11508 /* If window starts after a line end, and the last change is in
11509 front of that newline, then changes don't affect the display.
11510 This case happens with stealth-fontification. Note that although
11511 the display is unchanged, glyph positions in the matrix have to
11512 be adjusted, of course. */
11513 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11514 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11515 && ((last_changed_charpos < CHARPOS (start)
11516 && CHARPOS (start) == BEGV)
11517 || (last_changed_charpos < CHARPOS (start) - 1
11518 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11520 int Z_old, delta, Z_BYTE_old, delta_bytes;
11521 struct glyph_row *r0;
11523 /* Compute how many chars/bytes have been added to or removed
11524 from the buffer. */
11525 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11526 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11527 delta = Z - Z_old;
11528 delta_bytes = Z_BYTE - Z_BYTE_old;
11530 /* Give up if PT is not in the window. Note that it already has
11531 been checked at the start of try_window_id that PT is not in
11532 front of the window start. */
11533 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11534 GIVE_UP (13);
11536 /* If window start is unchanged, we can reuse the whole matrix
11537 as is, after adjusting glyph positions. No need to compute
11538 the window end again, since its offset from Z hasn't changed. */
11539 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11540 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11541 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11543 /* Adjust positions in the glyph matrix. */
11544 if (delta || delta_bytes)
11546 struct glyph_row *r1
11547 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11548 increment_matrix_positions (w->current_matrix,
11549 MATRIX_ROW_VPOS (r0, current_matrix),
11550 MATRIX_ROW_VPOS (r1, current_matrix),
11551 delta, delta_bytes);
11554 /* Set the cursor. */
11555 row = row_containing_pos (w, PT, r0, NULL, 0);
11556 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11557 return 1;
11561 /* Handle the case that changes are all below what is displayed in
11562 the window, and that PT is in the window. This shortcut cannot
11563 be taken if ZV is visible in the window, and text has been added
11564 there that is visible in the window. */
11565 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11566 /* ZV is not visible in the window, or there are no
11567 changes at ZV, actually. */
11568 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11569 || first_changed_charpos == last_changed_charpos))
11571 struct glyph_row *r0;
11573 /* Give up if PT is not in the window. Note that it already has
11574 been checked at the start of try_window_id that PT is not in
11575 front of the window start. */
11576 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11577 GIVE_UP (14);
11579 /* If window start is unchanged, we can reuse the whole matrix
11580 as is, without changing glyph positions since no text has
11581 been added/removed in front of the window end. */
11582 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11583 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11585 /* We have to compute the window end anew since text
11586 can have been added/removed after it. */
11587 w->window_end_pos
11588 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11589 w->window_end_bytepos
11590 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11592 /* Set the cursor. */
11593 row = row_containing_pos (w, PT, r0, NULL, 0);
11594 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11595 return 2;
11599 /* Give up if window start is in the changed area.
11601 The condition used to read
11603 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11605 but why that was tested escapes me at the moment. */
11606 if (CHARPOS (start) >= first_changed_charpos
11607 && CHARPOS (start) <= last_changed_charpos)
11608 GIVE_UP (15);
11610 /* Check that window start agrees with the start of the first glyph
11611 row in its current matrix. Check this after we know the window
11612 start is not in changed text, otherwise positions would not be
11613 comparable. */
11614 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11615 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11616 GIVE_UP (16);
11618 /* Give up if the window ends in strings. Overlay strings
11619 at the end are difficult to handle, so don't try. */
11620 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11621 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11622 GIVE_UP (20);
11624 /* Compute the position at which we have to start displaying new
11625 lines. Some of the lines at the top of the window might be
11626 reusable because they are not displaying changed text. Find the
11627 last row in W's current matrix not affected by changes at the
11628 start of current_buffer. Value is null if changes start in the
11629 first line of window. */
11630 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11631 if (last_unchanged_at_beg_row)
11633 /* Avoid starting to display in the moddle of a character, a TAB
11634 for instance. This is easier than to set up the iterator
11635 exactly, and it's not a frequent case, so the additional
11636 effort wouldn't really pay off. */
11637 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11638 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11639 && last_unchanged_at_beg_row > w->current_matrix->rows)
11640 --last_unchanged_at_beg_row;
11642 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11643 GIVE_UP (17);
11645 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11646 GIVE_UP (18);
11647 start_pos = it.current.pos;
11649 /* Start displaying new lines in the desired matrix at the same
11650 vpos we would use in the current matrix, i.e. below
11651 last_unchanged_at_beg_row. */
11652 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11653 current_matrix);
11654 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11655 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11657 xassert (it.hpos == 0 && it.current_x == 0);
11659 else
11661 /* There are no reusable lines at the start of the window.
11662 Start displaying in the first line. */
11663 start_display (&it, w, start);
11664 start_pos = it.current.pos;
11667 /* Find the first row that is not affected by changes at the end of
11668 the buffer. Value will be null if there is no unchanged row, in
11669 which case we must redisplay to the end of the window. delta
11670 will be set to the value by which buffer positions beginning with
11671 first_unchanged_at_end_row have to be adjusted due to text
11672 changes. */
11673 first_unchanged_at_end_row
11674 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11675 IF_DEBUG (debug_delta = delta);
11676 IF_DEBUG (debug_delta_bytes = delta_bytes);
11678 /* Set stop_pos to the buffer position up to which we will have to
11679 display new lines. If first_unchanged_at_end_row != NULL, this
11680 is the buffer position of the start of the line displayed in that
11681 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11682 that we don't stop at a buffer position. */
11683 stop_pos = 0;
11684 if (first_unchanged_at_end_row)
11686 xassert (last_unchanged_at_beg_row == NULL
11687 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11689 /* If this is a continuation line, move forward to the next one
11690 that isn't. Changes in lines above affect this line.
11691 Caution: this may move first_unchanged_at_end_row to a row
11692 not displaying text. */
11693 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11694 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11695 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11696 < it.last_visible_y))
11697 ++first_unchanged_at_end_row;
11699 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11700 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11701 >= it.last_visible_y))
11702 first_unchanged_at_end_row = NULL;
11703 else
11705 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11706 + delta);
11707 first_unchanged_at_end_vpos
11708 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11709 xassert (stop_pos >= Z - END_UNCHANGED);
11712 else if (last_unchanged_at_beg_row == NULL)
11713 GIVE_UP (19);
11716 #if GLYPH_DEBUG
11718 /* Either there is no unchanged row at the end, or the one we have
11719 now displays text. This is a necessary condition for the window
11720 end pos calculation at the end of this function. */
11721 xassert (first_unchanged_at_end_row == NULL
11722 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11724 debug_last_unchanged_at_beg_vpos
11725 = (last_unchanged_at_beg_row
11726 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11727 : -1);
11728 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11730 #endif /* GLYPH_DEBUG != 0 */
11733 /* Display new lines. Set last_text_row to the last new line
11734 displayed which has text on it, i.e. might end up as being the
11735 line where the window_end_vpos is. */
11736 w->cursor.vpos = -1;
11737 last_text_row = NULL;
11738 overlay_arrow_seen = 0;
11739 while (it.current_y < it.last_visible_y
11740 && !fonts_changed_p
11741 && (first_unchanged_at_end_row == NULL
11742 || IT_CHARPOS (it) < stop_pos))
11744 if (display_line (&it))
11745 last_text_row = it.glyph_row - 1;
11748 if (fonts_changed_p)
11749 return -1;
11752 /* Compute differences in buffer positions, y-positions etc. for
11753 lines reused at the bottom of the window. Compute what we can
11754 scroll. */
11755 if (first_unchanged_at_end_row
11756 /* No lines reused because we displayed everything up to the
11757 bottom of the window. */
11758 && it.current_y < it.last_visible_y)
11760 dvpos = (it.vpos
11761 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11762 current_matrix));
11763 dy = it.current_y - first_unchanged_at_end_row->y;
11764 run.current_y = first_unchanged_at_end_row->y;
11765 run.desired_y = run.current_y + dy;
11766 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11768 else
11770 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11771 first_unchanged_at_end_row = NULL;
11773 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11776 /* Find the cursor if not already found. We have to decide whether
11777 PT will appear on this window (it sometimes doesn't, but this is
11778 not a very frequent case.) This decision has to be made before
11779 the current matrix is altered. A value of cursor.vpos < 0 means
11780 that PT is either in one of the lines beginning at
11781 first_unchanged_at_end_row or below the window. Don't care for
11782 lines that might be displayed later at the window end; as
11783 mentioned, this is not a frequent case. */
11784 if (w->cursor.vpos < 0)
11786 /* Cursor in unchanged rows at the top? */
11787 if (PT < CHARPOS (start_pos)
11788 && last_unchanged_at_beg_row)
11790 row = row_containing_pos (w, PT,
11791 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11792 last_unchanged_at_beg_row + 1, 0);
11793 if (row)
11794 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11797 /* Start from first_unchanged_at_end_row looking for PT. */
11798 else if (first_unchanged_at_end_row)
11800 row = row_containing_pos (w, PT - delta,
11801 first_unchanged_at_end_row, NULL, 0);
11802 if (row)
11803 set_cursor_from_row (w, row, w->current_matrix, delta,
11804 delta_bytes, dy, dvpos);
11807 /* Give up if cursor was not found. */
11808 if (w->cursor.vpos < 0)
11810 clear_glyph_matrix (w->desired_matrix);
11811 return -1;
11815 /* Don't let the cursor end in the scroll margins. */
11817 int this_scroll_margin, cursor_height;
11819 this_scroll_margin = max (0, scroll_margin);
11820 this_scroll_margin = min (this_scroll_margin,
11821 XFASTINT (w->height) / 4);
11822 this_scroll_margin *= CANON_Y_UNIT (it.f);
11823 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11825 if ((w->cursor.y < this_scroll_margin
11826 && CHARPOS (start) > BEGV)
11827 /* Don't take scroll margin into account at the bottom because
11828 old redisplay didn't do it either. */
11829 || w->cursor.y + cursor_height > it.last_visible_y)
11831 w->cursor.vpos = -1;
11832 clear_glyph_matrix (w->desired_matrix);
11833 return -1;
11837 /* Scroll the display. Do it before changing the current matrix so
11838 that xterm.c doesn't get confused about where the cursor glyph is
11839 found. */
11840 if (dy && run.height)
11842 update_begin (f);
11844 if (FRAME_WINDOW_P (f))
11846 rif->update_window_begin_hook (w);
11847 rif->clear_mouse_face (w);
11848 rif->scroll_run_hook (w, &run);
11849 rif->update_window_end_hook (w, 0, 0);
11851 else
11853 /* Terminal frame. In this case, dvpos gives the number of
11854 lines to scroll by; dvpos < 0 means scroll up. */
11855 int first_unchanged_at_end_vpos
11856 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11857 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11858 int end = (XFASTINT (w->top)
11859 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11860 + window_internal_height (w));
11862 /* Perform the operation on the screen. */
11863 if (dvpos > 0)
11865 /* Scroll last_unchanged_at_beg_row to the end of the
11866 window down dvpos lines. */
11867 set_terminal_window (end);
11869 /* On dumb terminals delete dvpos lines at the end
11870 before inserting dvpos empty lines. */
11871 if (!scroll_region_ok)
11872 ins_del_lines (end - dvpos, -dvpos);
11874 /* Insert dvpos empty lines in front of
11875 last_unchanged_at_beg_row. */
11876 ins_del_lines (from, dvpos);
11878 else if (dvpos < 0)
11880 /* Scroll up last_unchanged_at_beg_vpos to the end of
11881 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11882 set_terminal_window (end);
11884 /* Delete dvpos lines in front of
11885 last_unchanged_at_beg_vpos. ins_del_lines will set
11886 the cursor to the given vpos and emit |dvpos| delete
11887 line sequences. */
11888 ins_del_lines (from + dvpos, dvpos);
11890 /* On a dumb terminal insert dvpos empty lines at the
11891 end. */
11892 if (!scroll_region_ok)
11893 ins_del_lines (end + dvpos, -dvpos);
11896 set_terminal_window (0);
11899 update_end (f);
11902 /* Shift reused rows of the current matrix to the right position.
11903 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11904 text. */
11905 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11906 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11907 if (dvpos < 0)
11909 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11910 bottom_vpos, dvpos);
11911 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11912 bottom_vpos, 0);
11914 else if (dvpos > 0)
11916 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11917 bottom_vpos, dvpos);
11918 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11919 first_unchanged_at_end_vpos + dvpos, 0);
11922 /* For frame-based redisplay, make sure that current frame and window
11923 matrix are in sync with respect to glyph memory. */
11924 if (!FRAME_WINDOW_P (f))
11925 sync_frame_with_window_matrix_rows (w);
11927 /* Adjust buffer positions in reused rows. */
11928 if (delta)
11929 increment_matrix_positions (current_matrix,
11930 first_unchanged_at_end_vpos + dvpos,
11931 bottom_vpos, delta, delta_bytes);
11933 /* Adjust Y positions. */
11934 if (dy)
11935 shift_glyph_matrix (w, current_matrix,
11936 first_unchanged_at_end_vpos + dvpos,
11937 bottom_vpos, dy);
11939 if (first_unchanged_at_end_row)
11940 first_unchanged_at_end_row += dvpos;
11942 /* If scrolling up, there may be some lines to display at the end of
11943 the window. */
11944 last_text_row_at_end = NULL;
11945 if (dy < 0)
11947 /* Scrolling up can leave for example a partially visible line
11948 at the end of the window to be redisplayed. */
11949 /* Set last_row to the glyph row in the current matrix where the
11950 window end line is found. It has been moved up or down in
11951 the matrix by dvpos. */
11952 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11953 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11955 /* If last_row is the window end line, it should display text. */
11956 xassert (last_row->displays_text_p);
11958 /* If window end line was partially visible before, begin
11959 displaying at that line. Otherwise begin displaying with the
11960 line following it. */
11961 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11963 init_to_row_start (&it, w, last_row);
11964 it.vpos = last_vpos;
11965 it.current_y = last_row->y;
11967 else
11969 init_to_row_end (&it, w, last_row);
11970 it.vpos = 1 + last_vpos;
11971 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11972 ++last_row;
11975 /* We may start in a continuation line. If so, we have to
11976 get the right continuation_lines_width and current_x. */
11977 it.continuation_lines_width = last_row->continuation_lines_width;
11978 it.hpos = it.current_x = 0;
11980 /* Display the rest of the lines at the window end. */
11981 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11982 while (it.current_y < it.last_visible_y
11983 && !fonts_changed_p)
11985 /* Is it always sure that the display agrees with lines in
11986 the current matrix? I don't think so, so we mark rows
11987 displayed invalid in the current matrix by setting their
11988 enabled_p flag to zero. */
11989 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11990 if (display_line (&it))
11991 last_text_row_at_end = it.glyph_row - 1;
11995 /* Update window_end_pos and window_end_vpos. */
11996 if (first_unchanged_at_end_row
11997 && first_unchanged_at_end_row->y < it.last_visible_y
11998 && !last_text_row_at_end)
12000 /* Window end line if one of the preserved rows from the current
12001 matrix. Set row to the last row displaying text in current
12002 matrix starting at first_unchanged_at_end_row, after
12003 scrolling. */
12004 xassert (first_unchanged_at_end_row->displays_text_p);
12005 row = find_last_row_displaying_text (w->current_matrix, &it,
12006 first_unchanged_at_end_row);
12007 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12009 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12010 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12011 w->window_end_vpos
12012 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12013 xassert (w->window_end_bytepos >= 0);
12014 IF_DEBUG (debug_method_add (w, "A"));
12016 else if (last_text_row_at_end)
12018 w->window_end_pos
12019 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12020 w->window_end_bytepos
12021 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12022 w->window_end_vpos
12023 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12024 xassert (w->window_end_bytepos >= 0);
12025 IF_DEBUG (debug_method_add (w, "B"));
12027 else if (last_text_row)
12029 /* We have displayed either to the end of the window or at the
12030 end of the window, i.e. the last row with text is to be found
12031 in the desired matrix. */
12032 w->window_end_pos
12033 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12034 w->window_end_bytepos
12035 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12036 w->window_end_vpos
12037 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12038 xassert (w->window_end_bytepos >= 0);
12040 else if (first_unchanged_at_end_row == NULL
12041 && last_text_row == NULL
12042 && last_text_row_at_end == NULL)
12044 /* Displayed to end of window, but no line containing text was
12045 displayed. Lines were deleted at the end of the window. */
12046 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12047 int vpos = XFASTINT (w->window_end_vpos);
12048 struct glyph_row *current_row = current_matrix->rows + vpos;
12049 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12051 for (row = NULL;
12052 row == NULL && vpos >= first_vpos;
12053 --vpos, --current_row, --desired_row)
12055 if (desired_row->enabled_p)
12057 if (desired_row->displays_text_p)
12058 row = desired_row;
12060 else if (current_row->displays_text_p)
12061 row = current_row;
12064 xassert (row != NULL);
12065 w->window_end_vpos = make_number (vpos + 1);
12066 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12067 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12068 xassert (w->window_end_bytepos >= 0);
12069 IF_DEBUG (debug_method_add (w, "C"));
12071 else
12072 abort ();
12074 #if 0 /* This leads to problems, for instance when the cursor is
12075 at ZV, and the cursor line displays no text. */
12076 /* Disable rows below what's displayed in the window. This makes
12077 debugging easier. */
12078 enable_glyph_matrix_rows (current_matrix,
12079 XFASTINT (w->window_end_vpos) + 1,
12080 bottom_vpos, 0);
12081 #endif
12083 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12084 debug_end_vpos = XFASTINT (w->window_end_vpos));
12086 /* Record that display has not been completed. */
12087 w->window_end_valid = Qnil;
12088 w->desired_matrix->no_scrolling_p = 1;
12089 return 3;
12091 #undef GIVE_UP
12096 /***********************************************************************
12097 More debugging support
12098 ***********************************************************************/
12100 #if GLYPH_DEBUG
12102 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12103 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12104 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12107 /* Dump the contents of glyph matrix MATRIX on stderr.
12109 GLYPHS 0 means don't show glyph contents.
12110 GLYPHS 1 means show glyphs in short form
12111 GLYPHS > 1 means show glyphs in long form. */
12113 void
12114 dump_glyph_matrix (matrix, glyphs)
12115 struct glyph_matrix *matrix;
12116 int glyphs;
12118 int i;
12119 for (i = 0; i < matrix->nrows; ++i)
12120 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12124 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12125 the glyph row and area where the glyph comes from. */
12127 void
12128 dump_glyph (row, glyph, area)
12129 struct glyph_row *row;
12130 struct glyph *glyph;
12131 int area;
12133 if (glyph->type == CHAR_GLYPH)
12135 fprintf (stderr,
12136 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12137 glyph - row->glyphs[TEXT_AREA],
12138 'C',
12139 glyph->charpos,
12140 (BUFFERP (glyph->object)
12141 ? 'B'
12142 : (STRINGP (glyph->object)
12143 ? 'S'
12144 : '-')),
12145 glyph->pixel_width,
12146 glyph->u.ch,
12147 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12148 ? glyph->u.ch
12149 : '.'),
12150 glyph->face_id,
12151 glyph->left_box_line_p,
12152 glyph->right_box_line_p);
12154 else if (glyph->type == STRETCH_GLYPH)
12156 fprintf (stderr,
12157 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12158 glyph - row->glyphs[TEXT_AREA],
12159 'S',
12160 glyph->charpos,
12161 (BUFFERP (glyph->object)
12162 ? 'B'
12163 : (STRINGP (glyph->object)
12164 ? 'S'
12165 : '-')),
12166 glyph->pixel_width,
12168 '.',
12169 glyph->face_id,
12170 glyph->left_box_line_p,
12171 glyph->right_box_line_p);
12173 else if (glyph->type == IMAGE_GLYPH)
12175 fprintf (stderr,
12176 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12177 glyph - row->glyphs[TEXT_AREA],
12178 'I',
12179 glyph->charpos,
12180 (BUFFERP (glyph->object)
12181 ? 'B'
12182 : (STRINGP (glyph->object)
12183 ? 'S'
12184 : '-')),
12185 glyph->pixel_width,
12186 glyph->u.img_id,
12187 '.',
12188 glyph->face_id,
12189 glyph->left_box_line_p,
12190 glyph->right_box_line_p);
12195 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12196 GLYPHS 0 means don't show glyph contents.
12197 GLYPHS 1 means show glyphs in short form
12198 GLYPHS > 1 means show glyphs in long form. */
12200 void
12201 dump_glyph_row (row, vpos, glyphs)
12202 struct glyph_row *row;
12203 int vpos, glyphs;
12205 if (glyphs != 1)
12207 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12208 fprintf (stderr, "=======================================================================\n");
12210 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12211 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12212 vpos,
12213 MATRIX_ROW_START_CHARPOS (row),
12214 MATRIX_ROW_END_CHARPOS (row),
12215 row->used[TEXT_AREA],
12216 row->contains_overlapping_glyphs_p,
12217 row->enabled_p,
12218 row->truncated_on_left_p,
12219 row->truncated_on_right_p,
12220 row->overlay_arrow_p,
12221 row->continued_p,
12222 MATRIX_ROW_CONTINUATION_LINE_P (row),
12223 row->displays_text_p,
12224 row->ends_at_zv_p,
12225 row->fill_line_p,
12226 row->ends_in_middle_of_char_p,
12227 row->starts_in_middle_of_char_p,
12228 row->mouse_face_p,
12229 row->x,
12230 row->y,
12231 row->pixel_width,
12232 row->height,
12233 row->visible_height,
12234 row->ascent,
12235 row->phys_ascent);
12236 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12237 row->end.overlay_string_index,
12238 row->continuation_lines_width);
12239 fprintf (stderr, "%9d %5d\n",
12240 CHARPOS (row->start.string_pos),
12241 CHARPOS (row->end.string_pos));
12242 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12243 row->end.dpvec_index);
12246 if (glyphs > 1)
12248 int area;
12250 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12252 struct glyph *glyph = row->glyphs[area];
12253 struct glyph *glyph_end = glyph + row->used[area];
12255 /* Glyph for a line end in text. */
12256 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12257 ++glyph_end;
12259 if (glyph < glyph_end)
12260 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12262 for (; glyph < glyph_end; ++glyph)
12263 dump_glyph (row, glyph, area);
12266 else if (glyphs == 1)
12268 int area;
12270 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12272 char *s = (char *) alloca (row->used[area] + 1);
12273 int i;
12275 for (i = 0; i < row->used[area]; ++i)
12277 struct glyph *glyph = row->glyphs[area] + i;
12278 if (glyph->type == CHAR_GLYPH
12279 && glyph->u.ch < 0x80
12280 && glyph->u.ch >= ' ')
12281 s[i] = glyph->u.ch;
12282 else
12283 s[i] = '.';
12286 s[i] = '\0';
12287 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12293 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12294 Sdump_glyph_matrix, 0, 1, "p",
12295 doc: /* Dump the current matrix of the selected window to stderr.
12296 Shows contents of glyph row structures. With non-nil
12297 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12298 glyphs in short form, otherwise show glyphs in long form. */)
12299 (glyphs)
12300 Lisp_Object glyphs;
12302 struct window *w = XWINDOW (selected_window);
12303 struct buffer *buffer = XBUFFER (w->buffer);
12305 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12306 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12307 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12308 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12309 fprintf (stderr, "=============================================\n");
12310 dump_glyph_matrix (w->current_matrix,
12311 NILP (glyphs) ? 0 : XINT (glyphs));
12312 return Qnil;
12316 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12317 doc: /* Dump glyph row ROW to stderr.
12318 GLYPH 0 means don't dump glyphs.
12319 GLYPH 1 means dump glyphs in short form.
12320 GLYPH > 1 or omitted means dump glyphs in long form. */)
12321 (row, glyphs)
12322 Lisp_Object row, glyphs;
12324 struct glyph_matrix *matrix;
12325 int vpos;
12327 CHECK_NUMBER (row);
12328 matrix = XWINDOW (selected_window)->current_matrix;
12329 vpos = XINT (row);
12330 if (vpos >= 0 && vpos < matrix->nrows)
12331 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12332 vpos,
12333 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12334 return Qnil;
12338 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12339 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12340 GLYPH 0 means don't dump glyphs.
12341 GLYPH 1 means dump glyphs in short form.
12342 GLYPH > 1 or omitted means dump glyphs in long form. */)
12343 (row, glyphs)
12344 Lisp_Object row, glyphs;
12346 struct frame *sf = SELECTED_FRAME ();
12347 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12348 int vpos;
12350 CHECK_NUMBER (row);
12351 vpos = XINT (row);
12352 if (vpos >= 0 && vpos < m->nrows)
12353 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12354 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12355 return Qnil;
12359 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12360 doc: /* Toggle tracing of redisplay.
12361 With ARG, turn tracing on if and only if ARG is positive. */)
12362 (arg)
12363 Lisp_Object arg;
12365 if (NILP (arg))
12366 trace_redisplay_p = !trace_redisplay_p;
12367 else
12369 arg = Fprefix_numeric_value (arg);
12370 trace_redisplay_p = XINT (arg) > 0;
12373 return Qnil;
12377 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12378 doc: /* Like `format', but print result to stderr. */)
12379 (nargs, args)
12380 int nargs;
12381 Lisp_Object *args;
12383 Lisp_Object s = Fformat (nargs, args);
12384 fprintf (stderr, "%s", XSTRING (s)->data);
12385 return Qnil;
12388 #endif /* GLYPH_DEBUG */
12392 /***********************************************************************
12393 Building Desired Matrix Rows
12394 ***********************************************************************/
12396 /* Return a temporary glyph row holding the glyphs of an overlay
12397 arrow. Only used for non-window-redisplay windows. */
12399 static struct glyph_row *
12400 get_overlay_arrow_glyph_row (w)
12401 struct window *w;
12403 struct frame *f = XFRAME (WINDOW_FRAME (w));
12404 struct buffer *buffer = XBUFFER (w->buffer);
12405 struct buffer *old = current_buffer;
12406 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12407 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12408 unsigned char *arrow_end = arrow_string + arrow_len;
12409 unsigned char *p;
12410 struct it it;
12411 int multibyte_p;
12412 int n_glyphs_before;
12414 set_buffer_temp (buffer);
12415 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12416 it.glyph_row->used[TEXT_AREA] = 0;
12417 SET_TEXT_POS (it.position, 0, 0);
12419 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12420 p = arrow_string;
12421 while (p < arrow_end)
12423 Lisp_Object face, ilisp;
12425 /* Get the next character. */
12426 if (multibyte_p)
12427 it.c = string_char_and_length (p, arrow_len, &it.len);
12428 else
12429 it.c = *p, it.len = 1;
12430 p += it.len;
12432 /* Get its face. */
12433 ilisp = make_number (p - arrow_string);
12434 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12435 it.face_id = compute_char_face (f, it.c, face);
12437 /* Compute its width, get its glyphs. */
12438 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12439 SET_TEXT_POS (it.position, -1, -1);
12440 PRODUCE_GLYPHS (&it);
12442 /* If this character doesn't fit any more in the line, we have
12443 to remove some glyphs. */
12444 if (it.current_x > it.last_visible_x)
12446 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12447 break;
12451 set_buffer_temp (old);
12452 return it.glyph_row;
12456 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12457 glyphs are only inserted for terminal frames since we can't really
12458 win with truncation glyphs when partially visible glyphs are
12459 involved. Which glyphs to insert is determined by
12460 produce_special_glyphs. */
12462 static void
12463 insert_left_trunc_glyphs (it)
12464 struct it *it;
12466 struct it truncate_it;
12467 struct glyph *from, *end, *to, *toend;
12469 xassert (!FRAME_WINDOW_P (it->f));
12471 /* Get the truncation glyphs. */
12472 truncate_it = *it;
12473 truncate_it.current_x = 0;
12474 truncate_it.face_id = DEFAULT_FACE_ID;
12475 truncate_it.glyph_row = &scratch_glyph_row;
12476 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12477 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12478 truncate_it.object = make_number (0);
12479 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12481 /* Overwrite glyphs from IT with truncation glyphs. */
12482 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12483 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12484 to = it->glyph_row->glyphs[TEXT_AREA];
12485 toend = to + it->glyph_row->used[TEXT_AREA];
12487 while (from < end)
12488 *to++ = *from++;
12490 /* There may be padding glyphs left over. Overwrite them too. */
12491 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12493 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12494 while (from < end)
12495 *to++ = *from++;
12498 if (to > toend)
12499 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12503 /* Compute the pixel height and width of IT->glyph_row.
12505 Most of the time, ascent and height of a display line will be equal
12506 to the max_ascent and max_height values of the display iterator
12507 structure. This is not the case if
12509 1. We hit ZV without displaying anything. In this case, max_ascent
12510 and max_height will be zero.
12512 2. We have some glyphs that don't contribute to the line height.
12513 (The glyph row flag contributes_to_line_height_p is for future
12514 pixmap extensions).
12516 The first case is easily covered by using default values because in
12517 these cases, the line height does not really matter, except that it
12518 must not be zero. */
12520 static void
12521 compute_line_metrics (it)
12522 struct it *it;
12524 struct glyph_row *row = it->glyph_row;
12525 int area, i;
12527 if (FRAME_WINDOW_P (it->f))
12529 int i, min_y, max_y;
12531 /* The line may consist of one space only, that was added to
12532 place the cursor on it. If so, the row's height hasn't been
12533 computed yet. */
12534 if (row->height == 0)
12536 if (it->max_ascent + it->max_descent == 0)
12537 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12538 row->ascent = it->max_ascent;
12539 row->height = it->max_ascent + it->max_descent;
12540 row->phys_ascent = it->max_phys_ascent;
12541 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12544 /* Compute the width of this line. */
12545 row->pixel_width = row->x;
12546 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12547 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12549 xassert (row->pixel_width >= 0);
12550 xassert (row->ascent >= 0 && row->height > 0);
12552 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12553 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12555 /* If first line's physical ascent is larger than its logical
12556 ascent, use the physical ascent, and make the row taller.
12557 This makes accented characters fully visible. */
12558 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12559 && row->phys_ascent > row->ascent)
12561 row->height += row->phys_ascent - row->ascent;
12562 row->ascent = row->phys_ascent;
12565 /* Compute how much of the line is visible. */
12566 row->visible_height = row->height;
12568 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12569 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12571 if (row->y < min_y)
12572 row->visible_height -= min_y - row->y;
12573 if (row->y + row->height > max_y)
12574 row->visible_height -= row->y + row->height - max_y;
12576 else
12578 row->pixel_width = row->used[TEXT_AREA];
12579 if (row->continued_p)
12580 row->pixel_width -= it->continuation_pixel_width;
12581 else if (row->truncated_on_right_p)
12582 row->pixel_width -= it->truncation_pixel_width;
12583 row->ascent = row->phys_ascent = 0;
12584 row->height = row->phys_height = row->visible_height = 1;
12587 /* Compute a hash code for this row. */
12588 row->hash = 0;
12589 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12590 for (i = 0; i < row->used[area]; ++i)
12591 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12592 + row->glyphs[area][i].u.val
12593 + row->glyphs[area][i].face_id
12594 + row->glyphs[area][i].padding_p
12595 + (row->glyphs[area][i].type << 2));
12597 it->max_ascent = it->max_descent = 0;
12598 it->max_phys_ascent = it->max_phys_descent = 0;
12602 /* Append one space to the glyph row of iterator IT if doing a
12603 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12604 space have the default face, otherwise let it have the same face as
12605 IT->face_id. Value is non-zero if a space was added.
12607 This function is called to make sure that there is always one glyph
12608 at the end of a glyph row that the cursor can be set on under
12609 window-systems. (If there weren't such a glyph we would not know
12610 how wide and tall a box cursor should be displayed).
12612 At the same time this space let's a nicely handle clearing to the
12613 end of the line if the row ends in italic text. */
12615 static int
12616 append_space (it, default_face_p)
12617 struct it *it;
12618 int default_face_p;
12620 if (FRAME_WINDOW_P (it->f))
12622 int n = it->glyph_row->used[TEXT_AREA];
12624 if (it->glyph_row->glyphs[TEXT_AREA] + n
12625 < it->glyph_row->glyphs[1 + TEXT_AREA])
12627 /* Save some values that must not be changed.
12628 Must save IT->c and IT->len because otherwise
12629 ITERATOR_AT_END_P wouldn't work anymore after
12630 append_space has been called. */
12631 enum display_element_type saved_what = it->what;
12632 int saved_c = it->c, saved_len = it->len;
12633 int saved_x = it->current_x;
12634 int saved_face_id = it->face_id;
12635 struct text_pos saved_pos;
12636 Lisp_Object saved_object;
12637 struct face *face;
12639 saved_object = it->object;
12640 saved_pos = it->position;
12642 it->what = IT_CHARACTER;
12643 bzero (&it->position, sizeof it->position);
12644 it->object = make_number (0);
12645 it->c = ' ';
12646 it->len = 1;
12648 if (default_face_p)
12649 it->face_id = DEFAULT_FACE_ID;
12650 else if (it->face_before_selective_p)
12651 it->face_id = it->saved_face_id;
12652 face = FACE_FROM_ID (it->f, it->face_id);
12653 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12655 PRODUCE_GLYPHS (it);
12657 it->current_x = saved_x;
12658 it->object = saved_object;
12659 it->position = saved_pos;
12660 it->what = saved_what;
12661 it->face_id = saved_face_id;
12662 it->len = saved_len;
12663 it->c = saved_c;
12664 return 1;
12668 return 0;
12672 /* Extend the face of the last glyph in the text area of IT->glyph_row
12673 to the end of the display line. Called from display_line.
12674 If the glyph row is empty, add a space glyph to it so that we
12675 know the face to draw. Set the glyph row flag fill_line_p. */
12677 static void
12678 extend_face_to_end_of_line (it)
12679 struct it *it;
12681 struct face *face;
12682 struct frame *f = it->f;
12684 /* If line is already filled, do nothing. */
12685 if (it->current_x >= it->last_visible_x)
12686 return;
12688 /* Face extension extends the background and box of IT->face_id
12689 to the end of the line. If the background equals the background
12690 of the frame, we don't have to do anything. */
12691 if (it->face_before_selective_p)
12692 face = FACE_FROM_ID (it->f, it->saved_face_id);
12693 else
12694 face = FACE_FROM_ID (f, it->face_id);
12696 if (FRAME_WINDOW_P (f)
12697 && face->box == FACE_NO_BOX
12698 && face->background == FRAME_BACKGROUND_PIXEL (f)
12699 && !face->stipple)
12700 return;
12702 /* Set the glyph row flag indicating that the face of the last glyph
12703 in the text area has to be drawn to the end of the text area. */
12704 it->glyph_row->fill_line_p = 1;
12706 /* If current character of IT is not ASCII, make sure we have the
12707 ASCII face. This will be automatically undone the next time
12708 get_next_display_element returns a multibyte character. Note
12709 that the character will always be single byte in unibyte text. */
12710 if (!SINGLE_BYTE_CHAR_P (it->c))
12712 it->face_id = FACE_FOR_CHAR (f, face, 0);
12715 if (FRAME_WINDOW_P (f))
12717 /* If the row is empty, add a space with the current face of IT,
12718 so that we know which face to draw. */
12719 if (it->glyph_row->used[TEXT_AREA] == 0)
12721 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12722 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12723 it->glyph_row->used[TEXT_AREA] = 1;
12726 else
12728 /* Save some values that must not be changed. */
12729 int saved_x = it->current_x;
12730 struct text_pos saved_pos;
12731 Lisp_Object saved_object;
12732 enum display_element_type saved_what = it->what;
12733 int saved_face_id = it->face_id;
12735 saved_object = it->object;
12736 saved_pos = it->position;
12738 it->what = IT_CHARACTER;
12739 bzero (&it->position, sizeof it->position);
12740 it->object = make_number (0);
12741 it->c = ' ';
12742 it->len = 1;
12743 it->face_id = face->id;
12745 PRODUCE_GLYPHS (it);
12747 while (it->current_x <= it->last_visible_x)
12748 PRODUCE_GLYPHS (it);
12750 /* Don't count these blanks really. It would let us insert a left
12751 truncation glyph below and make us set the cursor on them, maybe. */
12752 it->current_x = saved_x;
12753 it->object = saved_object;
12754 it->position = saved_pos;
12755 it->what = saved_what;
12756 it->face_id = saved_face_id;
12761 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12762 trailing whitespace. */
12764 static int
12765 trailing_whitespace_p (charpos)
12766 int charpos;
12768 int bytepos = CHAR_TO_BYTE (charpos);
12769 int c = 0;
12771 while (bytepos < ZV_BYTE
12772 && (c = FETCH_CHAR (bytepos),
12773 c == ' ' || c == '\t'))
12774 ++bytepos;
12776 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12778 if (bytepos != PT_BYTE)
12779 return 1;
12781 return 0;
12785 /* Highlight trailing whitespace, if any, in ROW. */
12787 void
12788 highlight_trailing_whitespace (f, row)
12789 struct frame *f;
12790 struct glyph_row *row;
12792 int used = row->used[TEXT_AREA];
12794 if (used)
12796 struct glyph *start = row->glyphs[TEXT_AREA];
12797 struct glyph *glyph = start + used - 1;
12799 /* Skip over glyphs inserted to display the cursor at the
12800 end of a line, for extending the face of the last glyph
12801 to the end of the line on terminals, and for truncation
12802 and continuation glyphs. */
12803 while (glyph >= start
12804 && glyph->type == CHAR_GLYPH
12805 && INTEGERP (glyph->object))
12806 --glyph;
12808 /* If last glyph is a space or stretch, and it's trailing
12809 whitespace, set the face of all trailing whitespace glyphs in
12810 IT->glyph_row to `trailing-whitespace'. */
12811 if (glyph >= start
12812 && BUFFERP (glyph->object)
12813 && (glyph->type == STRETCH_GLYPH
12814 || (glyph->type == CHAR_GLYPH
12815 && glyph->u.ch == ' '))
12816 && trailing_whitespace_p (glyph->charpos))
12818 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12820 while (glyph >= start
12821 && BUFFERP (glyph->object)
12822 && (glyph->type == STRETCH_GLYPH
12823 || (glyph->type == CHAR_GLYPH
12824 && glyph->u.ch == ' ')))
12825 (glyph--)->face_id = face_id;
12831 /* Value is non-zero if glyph row ROW in window W should be
12832 used to hold the cursor. */
12834 static int
12835 cursor_row_p (w, row)
12836 struct window *w;
12837 struct glyph_row *row;
12839 int cursor_row_p = 1;
12841 if (PT == MATRIX_ROW_END_CHARPOS (row))
12843 /* If the row ends with a newline from a string, we don't want
12844 the cursor there (if the row is continued it doesn't end in a
12845 newline). */
12846 if (CHARPOS (row->end.string_pos) >= 0
12847 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12848 cursor_row_p = row->continued_p;
12850 /* If the row ends at ZV, display the cursor at the end of that
12851 row instead of at the start of the row below. */
12852 else if (row->ends_at_zv_p)
12853 cursor_row_p = 1;
12854 else
12855 cursor_row_p = 0;
12858 return cursor_row_p;
12862 /* Construct the glyph row IT->glyph_row in the desired matrix of
12863 IT->w from text at the current position of IT. See dispextern.h
12864 for an overview of struct it. Value is non-zero if
12865 IT->glyph_row displays text, as opposed to a line displaying ZV
12866 only. */
12868 static int
12869 display_line (it)
12870 struct it *it;
12872 struct glyph_row *row = it->glyph_row;
12874 /* We always start displaying at hpos zero even if hscrolled. */
12875 xassert (it->hpos == 0 && it->current_x == 0);
12877 /* We must not display in a row that's not a text row. */
12878 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12879 < it->w->desired_matrix->nrows);
12881 /* Is IT->w showing the region? */
12882 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12884 /* Clear the result glyph row and enable it. */
12885 prepare_desired_row (row);
12887 row->y = it->current_y;
12888 row->start = it->current;
12889 row->continuation_lines_width = it->continuation_lines_width;
12890 row->displays_text_p = 1;
12891 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12892 it->starts_in_middle_of_char_p = 0;
12894 /* Arrange the overlays nicely for our purposes. Usually, we call
12895 display_line on only one line at a time, in which case this
12896 can't really hurt too much, or we call it on lines which appear
12897 one after another in the buffer, in which case all calls to
12898 recenter_overlay_lists but the first will be pretty cheap. */
12899 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12901 /* Move over display elements that are not visible because we are
12902 hscrolled. This may stop at an x-position < IT->first_visible_x
12903 if the first glyph is partially visible or if we hit a line end. */
12904 if (it->current_x < it->first_visible_x)
12905 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12906 MOVE_TO_POS | MOVE_TO_X);
12908 /* Get the initial row height. This is either the height of the
12909 text hscrolled, if there is any, or zero. */
12910 row->ascent = it->max_ascent;
12911 row->height = it->max_ascent + it->max_descent;
12912 row->phys_ascent = it->max_phys_ascent;
12913 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12915 /* Loop generating characters. The loop is left with IT on the next
12916 character to display. */
12917 while (1)
12919 int n_glyphs_before, hpos_before, x_before;
12920 int x, i, nglyphs;
12921 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12923 /* Retrieve the next thing to display. Value is zero if end of
12924 buffer reached. */
12925 if (!get_next_display_element (it))
12927 /* Maybe add a space at the end of this line that is used to
12928 display the cursor there under X. Set the charpos of the
12929 first glyph of blank lines not corresponding to any text
12930 to -1. */
12931 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12932 || row->used[TEXT_AREA] == 0)
12934 row->glyphs[TEXT_AREA]->charpos = -1;
12935 row->displays_text_p = 0;
12937 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12938 && (!MINI_WINDOW_P (it->w)
12939 || (minibuf_level && EQ (it->window, minibuf_window))))
12940 row->indicate_empty_line_p = 1;
12943 it->continuation_lines_width = 0;
12944 row->ends_at_zv_p = 1;
12945 break;
12948 /* Now, get the metrics of what we want to display. This also
12949 generates glyphs in `row' (which is IT->glyph_row). */
12950 n_glyphs_before = row->used[TEXT_AREA];
12951 x = it->current_x;
12953 /* Remember the line height so far in case the next element doesn't
12954 fit on the line. */
12955 if (!it->truncate_lines_p)
12957 ascent = it->max_ascent;
12958 descent = it->max_descent;
12959 phys_ascent = it->max_phys_ascent;
12960 phys_descent = it->max_phys_descent;
12963 PRODUCE_GLYPHS (it);
12965 /* If this display element was in marginal areas, continue with
12966 the next one. */
12967 if (it->area != TEXT_AREA)
12969 row->ascent = max (row->ascent, it->max_ascent);
12970 row->height = max (row->height, it->max_ascent + it->max_descent);
12971 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12972 row->phys_height = max (row->phys_height,
12973 it->max_phys_ascent + it->max_phys_descent);
12974 set_iterator_to_next (it, 1);
12975 continue;
12978 /* Does the display element fit on the line? If we truncate
12979 lines, we should draw past the right edge of the window. If
12980 we don't truncate, we want to stop so that we can display the
12981 continuation glyph before the right margin. If lines are
12982 continued, there are two possible strategies for characters
12983 resulting in more than 1 glyph (e.g. tabs): Display as many
12984 glyphs as possible in this line and leave the rest for the
12985 continuation line, or display the whole element in the next
12986 line. Original redisplay did the former, so we do it also. */
12987 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12988 hpos_before = it->hpos;
12989 x_before = x;
12991 if (/* Not a newline. */
12992 nglyphs > 0
12993 /* Glyphs produced fit entirely in the line. */
12994 && it->current_x < it->last_visible_x)
12996 it->hpos += nglyphs;
12997 row->ascent = max (row->ascent, it->max_ascent);
12998 row->height = max (row->height, it->max_ascent + it->max_descent);
12999 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13000 row->phys_height = max (row->phys_height,
13001 it->max_phys_ascent + it->max_phys_descent);
13002 if (it->current_x - it->pixel_width < it->first_visible_x)
13003 row->x = x - it->first_visible_x;
13005 else
13007 int new_x;
13008 struct glyph *glyph;
13010 for (i = 0; i < nglyphs; ++i, x = new_x)
13012 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13013 new_x = x + glyph->pixel_width;
13015 if (/* Lines are continued. */
13016 !it->truncate_lines_p
13017 && (/* Glyph doesn't fit on the line. */
13018 new_x > it->last_visible_x
13019 /* Or it fits exactly on a window system frame. */
13020 || (new_x == it->last_visible_x
13021 && FRAME_WINDOW_P (it->f))))
13023 /* End of a continued line. */
13025 if (it->hpos == 0
13026 || (new_x == it->last_visible_x
13027 && FRAME_WINDOW_P (it->f)))
13029 /* Current glyph is the only one on the line or
13030 fits exactly on the line. We must continue
13031 the line because we can't draw the cursor
13032 after the glyph. */
13033 row->continued_p = 1;
13034 it->current_x = new_x;
13035 it->continuation_lines_width += new_x;
13036 ++it->hpos;
13037 if (i == nglyphs - 1)
13038 set_iterator_to_next (it, 1);
13040 else if (CHAR_GLYPH_PADDING_P (*glyph)
13041 && !FRAME_WINDOW_P (it->f))
13043 /* A padding glyph that doesn't fit on this line.
13044 This means the whole character doesn't fit
13045 on the line. */
13046 row->used[TEXT_AREA] = n_glyphs_before;
13048 /* Fill the rest of the row with continuation
13049 glyphs like in 20.x. */
13050 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13051 < row->glyphs[1 + TEXT_AREA])
13052 produce_special_glyphs (it, IT_CONTINUATION);
13054 row->continued_p = 1;
13055 it->current_x = x_before;
13056 it->continuation_lines_width += x_before;
13058 /* Restore the height to what it was before the
13059 element not fitting on the line. */
13060 it->max_ascent = ascent;
13061 it->max_descent = descent;
13062 it->max_phys_ascent = phys_ascent;
13063 it->max_phys_descent = phys_descent;
13065 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13067 /* A TAB that extends past the right edge of the
13068 window. This produces a single glyph on
13069 window system frames. We leave the glyph in
13070 this row and let it fill the row, but don't
13071 consume the TAB. */
13072 it->continuation_lines_width += it->last_visible_x;
13073 row->ends_in_middle_of_char_p = 1;
13074 row->continued_p = 1;
13075 glyph->pixel_width = it->last_visible_x - x;
13076 it->starts_in_middle_of_char_p = 1;
13078 else
13080 /* Something other than a TAB that draws past
13081 the right edge of the window. Restore
13082 positions to values before the element. */
13083 row->used[TEXT_AREA] = n_glyphs_before + i;
13085 /* Display continuation glyphs. */
13086 if (!FRAME_WINDOW_P (it->f))
13087 produce_special_glyphs (it, IT_CONTINUATION);
13088 row->continued_p = 1;
13090 it->continuation_lines_width += x;
13092 if (nglyphs > 1 && i > 0)
13094 row->ends_in_middle_of_char_p = 1;
13095 it->starts_in_middle_of_char_p = 1;
13098 /* Restore the height to what it was before the
13099 element not fitting on the line. */
13100 it->max_ascent = ascent;
13101 it->max_descent = descent;
13102 it->max_phys_ascent = phys_ascent;
13103 it->max_phys_descent = phys_descent;
13106 break;
13108 else if (new_x > it->first_visible_x)
13110 /* Increment number of glyphs actually displayed. */
13111 ++it->hpos;
13113 if (x < it->first_visible_x)
13114 /* Glyph is partially visible, i.e. row starts at
13115 negative X position. */
13116 row->x = x - it->first_visible_x;
13118 else
13120 /* Glyph is completely off the left margin of the
13121 window. This should not happen because of the
13122 move_it_in_display_line at the start of
13123 this function. */
13124 abort ();
13128 row->ascent = max (row->ascent, it->max_ascent);
13129 row->height = max (row->height, it->max_ascent + it->max_descent);
13130 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13131 row->phys_height = max (row->phys_height,
13132 it->max_phys_ascent + it->max_phys_descent);
13134 /* End of this display line if row is continued. */
13135 if (row->continued_p)
13136 break;
13139 /* Is this a line end? If yes, we're also done, after making
13140 sure that a non-default face is extended up to the right
13141 margin of the window. */
13142 if (ITERATOR_AT_END_OF_LINE_P (it))
13144 int used_before = row->used[TEXT_AREA];
13146 row->ends_in_newline_from_string_p = STRINGP (it->object);
13148 /* Add a space at the end of the line that is used to
13149 display the cursor there. */
13150 append_space (it, 0);
13152 /* Extend the face to the end of the line. */
13153 extend_face_to_end_of_line (it);
13155 /* Make sure we have the position. */
13156 if (used_before == 0)
13157 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13159 /* Consume the line end. This skips over invisible lines. */
13160 set_iterator_to_next (it, 1);
13161 it->continuation_lines_width = 0;
13162 break;
13165 /* Proceed with next display element. Note that this skips
13166 over lines invisible because of selective display. */
13167 set_iterator_to_next (it, 1);
13169 /* If we truncate lines, we are done when the last displayed
13170 glyphs reach past the right margin of the window. */
13171 if (it->truncate_lines_p
13172 && (FRAME_WINDOW_P (it->f)
13173 ? (it->current_x >= it->last_visible_x)
13174 : (it->current_x > it->last_visible_x)))
13176 /* Maybe add truncation glyphs. */
13177 if (!FRAME_WINDOW_P (it->f))
13179 int i, n;
13181 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13182 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13183 break;
13185 for (n = row->used[TEXT_AREA]; i < n; ++i)
13187 row->used[TEXT_AREA] = i;
13188 produce_special_glyphs (it, IT_TRUNCATION);
13192 row->truncated_on_right_p = 1;
13193 it->continuation_lines_width = 0;
13194 reseat_at_next_visible_line_start (it, 0);
13195 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13196 it->hpos = hpos_before;
13197 it->current_x = x_before;
13198 break;
13202 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13203 at the left window margin. */
13204 if (it->first_visible_x
13205 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13207 if (!FRAME_WINDOW_P (it->f))
13208 insert_left_trunc_glyphs (it);
13209 row->truncated_on_left_p = 1;
13212 /* If the start of this line is the overlay arrow-position, then
13213 mark this glyph row as the one containing the overlay arrow.
13214 This is clearly a mess with variable size fonts. It would be
13215 better to let it be displayed like cursors under X. */
13216 if (MARKERP (Voverlay_arrow_position)
13217 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13218 && (MATRIX_ROW_START_CHARPOS (row)
13219 == marker_position (Voverlay_arrow_position))
13220 && STRINGP (Voverlay_arrow_string)
13221 && ! overlay_arrow_seen)
13223 /* Overlay arrow in window redisplay is a fringe bitmap. */
13224 if (!FRAME_WINDOW_P (it->f))
13226 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13227 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13228 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13229 struct glyph *p = row->glyphs[TEXT_AREA];
13230 struct glyph *p2, *end;
13232 /* Copy the arrow glyphs. */
13233 while (glyph < arrow_end)
13234 *p++ = *glyph++;
13236 /* Throw away padding glyphs. */
13237 p2 = p;
13238 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13239 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13240 ++p2;
13241 if (p2 > p)
13243 while (p2 < end)
13244 *p++ = *p2++;
13245 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13249 overlay_arrow_seen = 1;
13250 row->overlay_arrow_p = 1;
13253 /* Compute pixel dimensions of this line. */
13254 compute_line_metrics (it);
13256 /* Remember the position at which this line ends. */
13257 row->end = it->current;
13259 /* Maybe set the cursor. */
13260 if (it->w->cursor.vpos < 0
13261 && PT >= MATRIX_ROW_START_CHARPOS (row)
13262 && PT <= MATRIX_ROW_END_CHARPOS (row)
13263 && cursor_row_p (it->w, row))
13264 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13266 /* Highlight trailing whitespace. */
13267 if (!NILP (Vshow_trailing_whitespace))
13268 highlight_trailing_whitespace (it->f, it->glyph_row);
13270 /* Prepare for the next line. This line starts horizontally at (X
13271 HPOS) = (0 0). Vertical positions are incremented. As a
13272 convenience for the caller, IT->glyph_row is set to the next
13273 row to be used. */
13274 it->current_x = it->hpos = 0;
13275 it->current_y += row->height;
13276 ++it->vpos;
13277 ++it->glyph_row;
13278 return row->displays_text_p;
13283 /***********************************************************************
13284 Menu Bar
13285 ***********************************************************************/
13287 /* Redisplay the menu bar in the frame for window W.
13289 The menu bar of X frames that don't have X toolkit support is
13290 displayed in a special window W->frame->menu_bar_window.
13292 The menu bar of terminal frames is treated specially as far as
13293 glyph matrices are concerned. Menu bar lines are not part of
13294 windows, so the update is done directly on the frame matrix rows
13295 for the menu bar. */
13297 static void
13298 display_menu_bar (w)
13299 struct window *w;
13301 struct frame *f = XFRAME (WINDOW_FRAME (w));
13302 struct it it;
13303 Lisp_Object items;
13304 int i;
13306 /* Don't do all this for graphical frames. */
13307 #ifdef HAVE_NTGUI
13308 if (!NILP (Vwindow_system))
13309 return;
13310 #endif
13311 #ifdef USE_X_TOOLKIT
13312 if (FRAME_X_P (f))
13313 return;
13314 #endif
13315 #ifdef macintosh
13316 if (FRAME_MAC_P (f))
13317 return;
13318 #endif
13320 #ifdef USE_X_TOOLKIT
13321 xassert (!FRAME_WINDOW_P (f));
13322 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13323 it.first_visible_x = 0;
13324 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13325 #else /* not USE_X_TOOLKIT */
13326 if (FRAME_WINDOW_P (f))
13328 /* Menu bar lines are displayed in the desired matrix of the
13329 dummy window menu_bar_window. */
13330 struct window *menu_w;
13331 xassert (WINDOWP (f->menu_bar_window));
13332 menu_w = XWINDOW (f->menu_bar_window);
13333 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13334 MENU_FACE_ID);
13335 it.first_visible_x = 0;
13336 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13338 else
13340 /* This is a TTY frame, i.e. character hpos/vpos are used as
13341 pixel x/y. */
13342 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13343 MENU_FACE_ID);
13344 it.first_visible_x = 0;
13345 it.last_visible_x = FRAME_WIDTH (f);
13347 #endif /* not USE_X_TOOLKIT */
13349 if (! mode_line_inverse_video)
13350 /* Force the menu-bar to be displayed in the default face. */
13351 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13353 /* Clear all rows of the menu bar. */
13354 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13356 struct glyph_row *row = it.glyph_row + i;
13357 clear_glyph_row (row);
13358 row->enabled_p = 1;
13359 row->full_width_p = 1;
13362 /* Display all items of the menu bar. */
13363 items = FRAME_MENU_BAR_ITEMS (it.f);
13364 for (i = 0; i < XVECTOR (items)->size; i += 4)
13366 Lisp_Object string;
13368 /* Stop at nil string. */
13369 string = AREF (items, i + 1);
13370 if (NILP (string))
13371 break;
13373 /* Remember where item was displayed. */
13374 AREF (items, i + 3) = make_number (it.hpos);
13376 /* Display the item, pad with one space. */
13377 if (it.current_x < it.last_visible_x)
13378 display_string (NULL, string, Qnil, 0, 0, &it,
13379 XSTRING (string)->size + 1, 0, 0, -1);
13382 /* Fill out the line with spaces. */
13383 if (it.current_x < it.last_visible_x)
13384 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13386 /* Compute the total height of the lines. */
13387 compute_line_metrics (&it);
13392 /***********************************************************************
13393 Mode Line
13394 ***********************************************************************/
13396 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13397 FORCE is non-zero, redisplay mode lines unconditionally.
13398 Otherwise, redisplay only mode lines that are garbaged. Value is
13399 the number of windows whose mode lines were redisplayed. */
13401 static int
13402 redisplay_mode_lines (window, force)
13403 Lisp_Object window;
13404 int force;
13406 int nwindows = 0;
13408 while (!NILP (window))
13410 struct window *w = XWINDOW (window);
13412 if (WINDOWP (w->hchild))
13413 nwindows += redisplay_mode_lines (w->hchild, force);
13414 else if (WINDOWP (w->vchild))
13415 nwindows += redisplay_mode_lines (w->vchild, force);
13416 else if (force
13417 || FRAME_GARBAGED_P (XFRAME (w->frame))
13418 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13420 struct text_pos lpoint;
13421 struct buffer *old = current_buffer;
13423 /* Set the window's buffer for the mode line display. */
13424 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13425 set_buffer_internal_1 (XBUFFER (w->buffer));
13427 /* Point refers normally to the selected window. For any
13428 other window, set up appropriate value. */
13429 if (!EQ (window, selected_window))
13431 struct text_pos pt;
13433 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13434 if (CHARPOS (pt) < BEGV)
13435 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13436 else if (CHARPOS (pt) > (ZV - 1))
13437 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13438 else
13439 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13442 /* Display mode lines. */
13443 clear_glyph_matrix (w->desired_matrix);
13444 if (display_mode_lines (w))
13446 ++nwindows;
13447 w->must_be_updated_p = 1;
13450 /* Restore old settings. */
13451 set_buffer_internal_1 (old);
13452 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13455 window = w->next;
13458 return nwindows;
13462 /* Display the mode and/or top line of window W. Value is the number
13463 of mode lines displayed. */
13465 static int
13466 display_mode_lines (w)
13467 struct window *w;
13469 Lisp_Object old_selected_window, old_selected_frame;
13470 int n = 0;
13472 old_selected_frame = selected_frame;
13473 selected_frame = w->frame;
13474 old_selected_window = selected_window;
13475 XSETWINDOW (selected_window, w);
13477 /* These will be set while the mode line specs are processed. */
13478 line_number_displayed = 0;
13479 w->column_number_displayed = Qnil;
13481 if (WINDOW_WANTS_MODELINE_P (w))
13483 struct window *sel_w = XWINDOW (old_selected_window);
13485 /* Select mode line face based on the real selected window. */
13486 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13487 current_buffer->mode_line_format);
13488 ++n;
13491 if (WINDOW_WANTS_HEADER_LINE_P (w))
13493 display_mode_line (w, HEADER_LINE_FACE_ID,
13494 current_buffer->header_line_format);
13495 ++n;
13498 selected_frame = old_selected_frame;
13499 selected_window = old_selected_window;
13500 return n;
13504 /* Display mode or top line of window W. FACE_ID specifies which line
13505 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13506 FORMAT is the mode line format to display. Value is the pixel
13507 height of the mode line displayed. */
13509 static int
13510 display_mode_line (w, face_id, format)
13511 struct window *w;
13512 enum face_id face_id;
13513 Lisp_Object format;
13515 struct it it;
13516 struct face *face;
13518 init_iterator (&it, w, -1, -1, NULL, face_id);
13519 prepare_desired_row (it.glyph_row);
13521 if (! mode_line_inverse_video)
13522 /* Force the mode-line to be displayed in the default face. */
13523 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13525 /* Temporarily make frame's keyboard the current kboard so that
13526 kboard-local variables in the mode_line_format will get the right
13527 values. */
13528 push_frame_kboard (it.f);
13529 display_mode_element (&it, 0, 0, 0, format, Qnil);
13530 pop_frame_kboard ();
13532 /* Fill up with spaces. */
13533 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13535 compute_line_metrics (&it);
13536 it.glyph_row->full_width_p = 1;
13537 it.glyph_row->mode_line_p = 1;
13538 it.glyph_row->continued_p = 0;
13539 it.glyph_row->truncated_on_left_p = 0;
13540 it.glyph_row->truncated_on_right_p = 0;
13542 /* Make a 3D mode-line have a shadow at its right end. */
13543 face = FACE_FROM_ID (it.f, face_id);
13544 extend_face_to_end_of_line (&it);
13545 if (face->box != FACE_NO_BOX)
13547 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13548 + it.glyph_row->used[TEXT_AREA] - 1);
13549 last->right_box_line_p = 1;
13552 return it.glyph_row->height;
13555 /* Alist that caches the results of :propertize.
13556 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13557 Lisp_Object mode_line_proptrans_alist;
13559 /* Contribute ELT to the mode line for window IT->w. How it
13560 translates into text depends on its data type.
13562 IT describes the display environment in which we display, as usual.
13564 DEPTH is the depth in recursion. It is used to prevent
13565 infinite recursion here.
13567 FIELD_WIDTH is the number of characters the display of ELT should
13568 occupy in the mode line, and PRECISION is the maximum number of
13569 characters to display from ELT's representation. See
13570 display_string for details.
13572 Returns the hpos of the end of the text generated by ELT. */
13574 static int
13575 display_mode_element (it, depth, field_width, precision, elt, props)
13576 struct it *it;
13577 int depth;
13578 int field_width, precision;
13579 Lisp_Object elt, props;
13581 int n = 0, field, prec;
13582 int literal = 0;
13584 tail_recurse:
13585 if (depth > 10)
13586 goto invalid;
13588 depth++;
13590 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13592 case Lisp_String:
13594 /* A string: output it and check for %-constructs within it. */
13595 unsigned char c;
13596 unsigned char *this = XSTRING (elt)->data;
13597 unsigned char *lisp_string = this;
13599 if (!NILP (props))
13601 Lisp_Object oprops, aelt;
13602 oprops = Ftext_properties_at (make_number (0), elt);
13603 if (NILP (Fequal (props, oprops)))
13605 aelt = Fassoc (elt, mode_line_proptrans_alist);
13606 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13607 elt = XCAR (aelt);
13608 else
13610 elt = Fcopy_sequence (elt);
13611 Fset_text_properties (0, Flength (elt), props, elt);
13612 mode_line_proptrans_alist
13613 = Fcons (Fcons (elt, props),
13614 mode_line_proptrans_alist);
13617 this = XSTRING (elt)->data;
13618 lisp_string = this;
13621 if (literal)
13623 prec = precision - n;
13624 if (frame_title_ptr)
13625 n += store_frame_title (XSTRING (elt)->data, -1, prec);
13626 else
13627 n += display_string (NULL, elt, Qnil, 0, 0, it,
13628 0, prec, 0, STRING_MULTIBYTE (elt));
13630 break;
13633 while ((precision <= 0 || n < precision)
13634 && *this
13635 && (frame_title_ptr
13636 || it->current_x < it->last_visible_x))
13638 unsigned char *last = this;
13640 /* Advance to end of string or next format specifier. */
13641 while ((c = *this++) != '\0' && c != '%')
13644 if (this - 1 != last)
13646 /* Output to end of string or up to '%'. Field width
13647 is length of string. Don't output more than
13648 PRECISION allows us. */
13649 --this;
13651 prec = chars_in_text (last, this - last);
13652 if (precision > 0 && prec > precision - n)
13653 prec = precision - n;
13655 if (frame_title_ptr)
13656 n += store_frame_title (last, 0, prec);
13657 else
13659 int bytepos = last - lisp_string;
13660 int charpos = string_byte_to_char (elt, bytepos);
13661 n += display_string (NULL, elt, Qnil, 0, charpos,
13662 it, 0, prec, 0,
13663 STRING_MULTIBYTE (elt));
13666 else /* c == '%' */
13668 unsigned char *percent_position = this;
13670 /* Get the specified minimum width. Zero means
13671 don't pad. */
13672 field = 0;
13673 while ((c = *this++) >= '0' && c <= '9')
13674 field = field * 10 + c - '0';
13676 /* Don't pad beyond the total padding allowed. */
13677 if (field_width - n > 0 && field > field_width - n)
13678 field = field_width - n;
13680 /* Note that either PRECISION <= 0 or N < PRECISION. */
13681 prec = precision - n;
13683 if (c == 'M')
13684 n += display_mode_element (it, depth, field, prec,
13685 Vglobal_mode_string, props);
13686 else if (c != 0)
13688 int multibyte;
13689 unsigned char *spec
13690 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13692 if (frame_title_ptr)
13693 n += store_frame_title (spec, field, prec);
13694 else
13696 int nglyphs_before, bytepos, charpos, nwritten;
13698 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13699 bytepos = percent_position - lisp_string;
13700 charpos = (STRING_MULTIBYTE (elt)
13701 ? string_byte_to_char (elt, bytepos)
13702 : bytepos);
13703 nwritten = display_string (spec, Qnil, elt,
13704 charpos, 0, it,
13705 field, prec, 0,
13706 multibyte);
13708 /* Assign to the glyphs written above the
13709 string where the `%x' came from, position
13710 of the `%'. */
13711 if (nwritten > 0)
13713 struct glyph *glyph
13714 = (it->glyph_row->glyphs[TEXT_AREA]
13715 + nglyphs_before);
13716 int i;
13718 for (i = 0; i < nwritten; ++i)
13720 glyph[i].object = elt;
13721 glyph[i].charpos = charpos;
13724 n += nwritten;
13728 else /* c == 0 */
13729 break;
13733 break;
13735 case Lisp_Symbol:
13736 /* A symbol: process the value of the symbol recursively
13737 as if it appeared here directly. Avoid error if symbol void.
13738 Special case: if value of symbol is a string, output the string
13739 literally. */
13741 register Lisp_Object tem;
13742 tem = Fboundp (elt);
13743 if (!NILP (tem))
13745 tem = Fsymbol_value (elt);
13746 /* If value is a string, output that string literally:
13747 don't check for % within it. */
13748 if (STRINGP (tem))
13749 literal = 1;
13751 if (!EQ (tem, elt))
13753 /* Give up right away for nil or t. */
13754 elt = tem;
13755 goto tail_recurse;
13759 break;
13761 case Lisp_Cons:
13763 register Lisp_Object car, tem;
13765 /* A cons cell: five distinct cases.
13766 If first element is :eval or :propertize, do something special.
13767 If first element is a string or a cons, process all the elements
13768 and effectively concatenate them.
13769 If first element is a negative number, truncate displaying cdr to
13770 at most that many characters. If positive, pad (with spaces)
13771 to at least that many characters.
13772 If first element is a symbol, process the cadr or caddr recursively
13773 according to whether the symbol's value is non-nil or nil. */
13774 car = XCAR (elt);
13775 if (EQ (car, QCeval))
13777 /* An element of the form (:eval FORM) means evaluate FORM
13778 and use the result as mode line elements. */
13780 if (CONSP (XCDR (elt)))
13782 Lisp_Object spec;
13783 spec = safe_eval (XCAR (XCDR (elt)));
13784 n += display_mode_element (it, depth, field_width - n,
13785 precision - n, spec, props);
13788 else if (EQ (car, QCpropertize))
13790 if (CONSP (XCDR (elt)))
13792 /* An element of the form (:propertize ELT PROPS...)
13793 means display ELT but applying properties PROPS. */
13794 n += display_mode_element (it, depth, field_width - n,
13795 precision - n, XCAR (XCDR (elt)),
13796 XCDR (XCDR (elt)));
13799 else if (SYMBOLP (car))
13801 tem = Fboundp (car);
13802 elt = XCDR (elt);
13803 if (!CONSP (elt))
13804 goto invalid;
13805 /* elt is now the cdr, and we know it is a cons cell.
13806 Use its car if CAR has a non-nil value. */
13807 if (!NILP (tem))
13809 tem = Fsymbol_value (car);
13810 if (!NILP (tem))
13812 elt = XCAR (elt);
13813 goto tail_recurse;
13816 /* Symbol's value is nil (or symbol is unbound)
13817 Get the cddr of the original list
13818 and if possible find the caddr and use that. */
13819 elt = XCDR (elt);
13820 if (NILP (elt))
13821 break;
13822 else if (!CONSP (elt))
13823 goto invalid;
13824 elt = XCAR (elt);
13825 goto tail_recurse;
13827 else if (INTEGERP (car))
13829 register int lim = XINT (car);
13830 elt = XCDR (elt);
13831 if (lim < 0)
13833 /* Negative int means reduce maximum width. */
13834 if (precision <= 0)
13835 precision = -lim;
13836 else
13837 precision = min (precision, -lim);
13839 else if (lim > 0)
13841 /* Padding specified. Don't let it be more than
13842 current maximum. */
13843 if (precision > 0)
13844 lim = min (precision, lim);
13846 /* If that's more padding than already wanted, queue it.
13847 But don't reduce padding already specified even if
13848 that is beyond the current truncation point. */
13849 field_width = max (lim, field_width);
13851 goto tail_recurse;
13853 else if (STRINGP (car) || CONSP (car))
13855 register int limit = 50;
13856 /* Limit is to protect against circular lists. */
13857 while (CONSP (elt)
13858 && --limit > 0
13859 && (precision <= 0 || n < precision))
13861 n += display_mode_element (it, depth, field_width - n,
13862 precision - n, XCAR (elt), props);
13863 elt = XCDR (elt);
13867 break;
13869 default:
13870 invalid:
13871 if (frame_title_ptr)
13872 n += store_frame_title ("*invalid*", 0, precision - n);
13873 else
13874 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13875 precision - n, 0, 0);
13876 return n;
13879 /* Pad to FIELD_WIDTH. */
13880 if (field_width > 0 && n < field_width)
13882 if (frame_title_ptr)
13883 n += store_frame_title ("", field_width - n, 0);
13884 else
13885 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13886 0, 0, 0);
13889 return n;
13893 /* Write a null-terminated, right justified decimal representation of
13894 the positive integer D to BUF using a minimal field width WIDTH. */
13896 static void
13897 pint2str (buf, width, d)
13898 register char *buf;
13899 register int width;
13900 register int d;
13902 register char *p = buf;
13904 if (d <= 0)
13905 *p++ = '0';
13906 else
13908 while (d > 0)
13910 *p++ = d % 10 + '0';
13911 d /= 10;
13915 for (width -= (int) (p - buf); width > 0; --width)
13916 *p++ = ' ';
13917 *p-- = '\0';
13918 while (p > buf)
13920 d = *buf;
13921 *buf++ = *p;
13922 *p-- = d;
13926 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13927 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13928 type of CODING_SYSTEM. Return updated pointer into BUF. */
13930 static unsigned char invalid_eol_type[] = "(*invalid*)";
13932 static char *
13933 decode_mode_spec_coding (coding_system, buf, eol_flag)
13934 Lisp_Object coding_system;
13935 register char *buf;
13936 int eol_flag;
13938 Lisp_Object val;
13939 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13940 unsigned char *eol_str;
13941 int eol_str_len;
13942 /* The EOL conversion we are using. */
13943 Lisp_Object eoltype;
13945 val = CODING_SYSTEM_SPEC (coding_system);
13946 eoltype = Qnil;
13948 if (!VECTORP (val)) /* Not yet decided. */
13950 if (multibyte)
13951 *buf++ = '-';
13952 if (eol_flag)
13953 eoltype = eol_mnemonic_undecided;
13954 /* Don't mention EOL conversion if it isn't decided. */
13956 else
13958 Lisp_Object attrs;
13959 Lisp_Object eolvalue;
13961 attrs = AREF (val, 0);
13962 eolvalue = AREF (val, 2);
13964 if (multibyte)
13965 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
13967 if (eol_flag)
13969 /* The EOL conversion that is normal on this system. */
13971 if (NILP (eolvalue)) /* Not yet decided. */
13972 eoltype = eol_mnemonic_undecided;
13973 else if (VECTORP (eolvalue)) /* Not yet decided. */
13974 eoltype = eol_mnemonic_undecided;
13975 else /* eolvalue is Qunix, Qdos, or Qmac. */
13976 eoltype = (EQ (eolvalue, Qunix)
13977 ? eol_mnemonic_unix
13978 : (EQ (eolvalue, Qdos) == 1
13979 ? eol_mnemonic_dos : eol_mnemonic_mac));
13983 if (eol_flag)
13985 /* Mention the EOL conversion if it is not the usual one. */
13986 if (STRINGP (eoltype))
13988 eol_str = XSTRING (eoltype)->data;
13989 eol_str_len = XSTRING (eoltype)->size;
13991 else if (INTEGERP (eoltype)
13992 && CHAR_VALID_P (XINT (eoltype), 0))
13994 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13995 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13997 else
13999 eol_str = invalid_eol_type;
14000 eol_str_len = sizeof (invalid_eol_type) - 1;
14002 bcopy (eol_str, buf, eol_str_len);
14003 buf += eol_str_len;
14006 return buf;
14009 /* Return a string for the output of a mode line %-spec for window W,
14010 generated by character C. PRECISION >= 0 means don't return a
14011 string longer than that value. FIELD_WIDTH > 0 means pad the
14012 string returned with spaces to that value. Return 1 in *MULTIBYTE
14013 if the result is multibyte text. */
14015 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14017 static char *
14018 decode_mode_spec (w, c, field_width, precision, multibyte)
14019 struct window *w;
14020 register int c;
14021 int field_width, precision;
14022 int *multibyte;
14024 Lisp_Object obj;
14025 struct frame *f = XFRAME (WINDOW_FRAME (w));
14026 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14027 struct buffer *b = XBUFFER (w->buffer);
14029 obj = Qnil;
14030 *multibyte = 0;
14032 switch (c)
14034 case '*':
14035 if (!NILP (b->read_only))
14036 return "%";
14037 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14038 return "*";
14039 return "-";
14041 case '+':
14042 /* This differs from %* only for a modified read-only buffer. */
14043 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14044 return "*";
14045 if (!NILP (b->read_only))
14046 return "%";
14047 return "-";
14049 case '&':
14050 /* This differs from %* in ignoring read-only-ness. */
14051 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14052 return "*";
14053 return "-";
14055 case '%':
14056 return "%";
14058 case '[':
14060 int i;
14061 char *p;
14063 if (command_loop_level > 5)
14064 return "[[[... ";
14065 p = decode_mode_spec_buf;
14066 for (i = 0; i < command_loop_level; i++)
14067 *p++ = '[';
14068 *p = 0;
14069 return decode_mode_spec_buf;
14072 case ']':
14074 int i;
14075 char *p;
14077 if (command_loop_level > 5)
14078 return " ...]]]";
14079 p = decode_mode_spec_buf;
14080 for (i = 0; i < command_loop_level; i++)
14081 *p++ = ']';
14082 *p = 0;
14083 return decode_mode_spec_buf;
14086 case '-':
14088 register int i;
14090 /* Let lots_of_dashes be a string of infinite length. */
14091 if (field_width <= 0
14092 || field_width > sizeof (lots_of_dashes))
14094 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14095 decode_mode_spec_buf[i] = '-';
14096 decode_mode_spec_buf[i] = '\0';
14097 return decode_mode_spec_buf;
14099 else
14100 return lots_of_dashes;
14103 case 'b':
14104 obj = b->name;
14105 break;
14107 case 'c':
14109 int col = current_column ();
14110 w->column_number_displayed = make_number (col);
14111 pint2str (decode_mode_spec_buf, field_width, col);
14112 return decode_mode_spec_buf;
14115 case 'F':
14116 /* %F displays the frame name. */
14117 if (!NILP (f->title))
14118 return (char *) XSTRING (f->title)->data;
14119 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14120 return (char *) XSTRING (f->name)->data;
14121 return "Emacs";
14123 case 'f':
14124 obj = b->filename;
14125 break;
14127 case 'l':
14129 int startpos = XMARKER (w->start)->charpos;
14130 int startpos_byte = marker_byte_position (w->start);
14131 int line, linepos, linepos_byte, topline;
14132 int nlines, junk;
14133 int height = XFASTINT (w->height);
14135 /* If we decided that this buffer isn't suitable for line numbers,
14136 don't forget that too fast. */
14137 if (EQ (w->base_line_pos, w->buffer))
14138 goto no_value;
14139 /* But do forget it, if the window shows a different buffer now. */
14140 else if (BUFFERP (w->base_line_pos))
14141 w->base_line_pos = Qnil;
14143 /* If the buffer is very big, don't waste time. */
14144 if (INTEGERP (Vline_number_display_limit)
14145 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14147 w->base_line_pos = Qnil;
14148 w->base_line_number = Qnil;
14149 goto no_value;
14152 if (!NILP (w->base_line_number)
14153 && !NILP (w->base_line_pos)
14154 && XFASTINT (w->base_line_pos) <= startpos)
14156 line = XFASTINT (w->base_line_number);
14157 linepos = XFASTINT (w->base_line_pos);
14158 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14160 else
14162 line = 1;
14163 linepos = BUF_BEGV (b);
14164 linepos_byte = BUF_BEGV_BYTE (b);
14167 /* Count lines from base line to window start position. */
14168 nlines = display_count_lines (linepos, linepos_byte,
14169 startpos_byte,
14170 startpos, &junk);
14172 topline = nlines + line;
14174 /* Determine a new base line, if the old one is too close
14175 or too far away, or if we did not have one.
14176 "Too close" means it's plausible a scroll-down would
14177 go back past it. */
14178 if (startpos == BUF_BEGV (b))
14180 w->base_line_number = make_number (topline);
14181 w->base_line_pos = make_number (BUF_BEGV (b));
14183 else if (nlines < height + 25 || nlines > height * 3 + 50
14184 || linepos == BUF_BEGV (b))
14186 int limit = BUF_BEGV (b);
14187 int limit_byte = BUF_BEGV_BYTE (b);
14188 int position;
14189 int distance = (height * 2 + 30) * line_number_display_limit_width;
14191 if (startpos - distance > limit)
14193 limit = startpos - distance;
14194 limit_byte = CHAR_TO_BYTE (limit);
14197 nlines = display_count_lines (startpos, startpos_byte,
14198 limit_byte,
14199 - (height * 2 + 30),
14200 &position);
14201 /* If we couldn't find the lines we wanted within
14202 line_number_display_limit_width chars per line,
14203 give up on line numbers for this window. */
14204 if (position == limit_byte && limit == startpos - distance)
14206 w->base_line_pos = w->buffer;
14207 w->base_line_number = Qnil;
14208 goto no_value;
14211 w->base_line_number = make_number (topline - nlines);
14212 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14215 /* Now count lines from the start pos to point. */
14216 nlines = display_count_lines (startpos, startpos_byte,
14217 PT_BYTE, PT, &junk);
14219 /* Record that we did display the line number. */
14220 line_number_displayed = 1;
14222 /* Make the string to show. */
14223 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14224 return decode_mode_spec_buf;
14225 no_value:
14227 char* p = decode_mode_spec_buf;
14228 int pad = field_width - 2;
14229 while (pad-- > 0)
14230 *p++ = ' ';
14231 *p++ = '?';
14232 *p++ = '?';
14233 *p = '\0';
14234 return decode_mode_spec_buf;
14237 break;
14239 case 'm':
14240 obj = b->mode_name;
14241 break;
14243 case 'n':
14244 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14245 return " Narrow";
14246 break;
14248 case 'p':
14250 int pos = marker_position (w->start);
14251 int total = BUF_ZV (b) - BUF_BEGV (b);
14253 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14255 if (pos <= BUF_BEGV (b))
14256 return "All";
14257 else
14258 return "Bottom";
14260 else if (pos <= BUF_BEGV (b))
14261 return "Top";
14262 else
14264 if (total > 1000000)
14265 /* Do it differently for a large value, to avoid overflow. */
14266 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14267 else
14268 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14269 /* We can't normally display a 3-digit number,
14270 so get us a 2-digit number that is close. */
14271 if (total == 100)
14272 total = 99;
14273 sprintf (decode_mode_spec_buf, "%2d%%", total);
14274 return decode_mode_spec_buf;
14278 /* Display percentage of size above the bottom of the screen. */
14279 case 'P':
14281 int toppos = marker_position (w->start);
14282 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14283 int total = BUF_ZV (b) - BUF_BEGV (b);
14285 if (botpos >= BUF_ZV (b))
14287 if (toppos <= BUF_BEGV (b))
14288 return "All";
14289 else
14290 return "Bottom";
14292 else
14294 if (total > 1000000)
14295 /* Do it differently for a large value, to avoid overflow. */
14296 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14297 else
14298 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14299 /* We can't normally display a 3-digit number,
14300 so get us a 2-digit number that is close. */
14301 if (total == 100)
14302 total = 99;
14303 if (toppos <= BUF_BEGV (b))
14304 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14305 else
14306 sprintf (decode_mode_spec_buf, "%2d%%", total);
14307 return decode_mode_spec_buf;
14311 case 's':
14312 /* status of process */
14313 obj = Fget_buffer_process (w->buffer);
14314 if (NILP (obj))
14315 return "no process";
14316 #ifdef subprocesses
14317 obj = Fsymbol_name (Fprocess_status (obj));
14318 #endif
14319 break;
14321 case 't': /* indicate TEXT or BINARY */
14322 #ifdef MODE_LINE_BINARY_TEXT
14323 return MODE_LINE_BINARY_TEXT (b);
14324 #else
14325 return "T";
14326 #endif
14328 case 'z':
14329 /* coding-system (not including end-of-line format) */
14330 case 'Z':
14331 /* coding-system (including end-of-line type) */
14333 int eol_flag = (c == 'Z');
14334 char *p = decode_mode_spec_buf;
14336 if (! FRAME_WINDOW_P (f))
14338 /* No need to mention EOL here--the terminal never needs
14339 to do EOL conversion. */
14340 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
14341 p, 0);
14342 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
14343 p, 0);
14345 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14346 p, eol_flag);
14348 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14349 #ifdef subprocesses
14350 obj = Fget_buffer_process (Fcurrent_buffer ());
14351 if (PROCESSP (obj))
14353 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14354 p, eol_flag);
14355 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14356 p, eol_flag);
14358 #endif /* subprocesses */
14359 #endif /* 0 */
14360 *p = 0;
14361 return decode_mode_spec_buf;
14365 if (STRINGP (obj))
14367 *multibyte = STRING_MULTIBYTE (obj);
14368 return (char *) XSTRING (obj)->data;
14370 else
14371 return "";
14375 /* Count up to COUNT lines starting from START / START_BYTE.
14376 But don't go beyond LIMIT_BYTE.
14377 Return the number of lines thus found (always nonnegative).
14379 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14381 static int
14382 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14383 int start, start_byte, limit_byte, count;
14384 int *byte_pos_ptr;
14386 register unsigned char *cursor;
14387 unsigned char *base;
14389 register int ceiling;
14390 register unsigned char *ceiling_addr;
14391 int orig_count = count;
14393 /* If we are not in selective display mode,
14394 check only for newlines. */
14395 int selective_display = (!NILP (current_buffer->selective_display)
14396 && !INTEGERP (current_buffer->selective_display));
14398 if (count > 0)
14400 while (start_byte < limit_byte)
14402 ceiling = BUFFER_CEILING_OF (start_byte);
14403 ceiling = min (limit_byte - 1, ceiling);
14404 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14405 base = (cursor = BYTE_POS_ADDR (start_byte));
14406 while (1)
14408 if (selective_display)
14409 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14411 else
14412 while (*cursor != '\n' && ++cursor != ceiling_addr)
14415 if (cursor != ceiling_addr)
14417 if (--count == 0)
14419 start_byte += cursor - base + 1;
14420 *byte_pos_ptr = start_byte;
14421 return orig_count;
14423 else
14424 if (++cursor == ceiling_addr)
14425 break;
14427 else
14428 break;
14430 start_byte += cursor - base;
14433 else
14435 while (start_byte > limit_byte)
14437 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14438 ceiling = max (limit_byte, ceiling);
14439 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14440 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14441 while (1)
14443 if (selective_display)
14444 while (--cursor != ceiling_addr
14445 && *cursor != '\n' && *cursor != 015)
14447 else
14448 while (--cursor != ceiling_addr && *cursor != '\n')
14451 if (cursor != ceiling_addr)
14453 if (++count == 0)
14455 start_byte += cursor - base + 1;
14456 *byte_pos_ptr = start_byte;
14457 /* When scanning backwards, we should
14458 not count the newline posterior to which we stop. */
14459 return - orig_count - 1;
14462 else
14463 break;
14465 /* Here we add 1 to compensate for the last decrement
14466 of CURSOR, which took it past the valid range. */
14467 start_byte += cursor - base + 1;
14471 *byte_pos_ptr = limit_byte;
14473 if (count < 0)
14474 return - orig_count + count;
14475 return orig_count - count;
14481 /***********************************************************************
14482 Displaying strings
14483 ***********************************************************************/
14485 /* Display a NUL-terminated string, starting with index START.
14487 If STRING is non-null, display that C string. Otherwise, the Lisp
14488 string LISP_STRING is displayed.
14490 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14491 FACE_STRING. Display STRING or LISP_STRING with the face at
14492 FACE_STRING_POS in FACE_STRING:
14494 Display the string in the environment given by IT, but use the
14495 standard display table, temporarily.
14497 FIELD_WIDTH is the minimum number of output glyphs to produce.
14498 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14499 with spaces. If STRING has more characters, more than FIELD_WIDTH
14500 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14502 PRECISION is the maximum number of characters to output from
14503 STRING. PRECISION < 0 means don't truncate the string.
14505 This is roughly equivalent to printf format specifiers:
14507 FIELD_WIDTH PRECISION PRINTF
14508 ----------------------------------------
14509 -1 -1 %s
14510 -1 10 %.10s
14511 10 -1 %10s
14512 20 10 %20.10s
14514 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14515 display them, and < 0 means obey the current buffer's value of
14516 enable_multibyte_characters.
14518 Value is the number of glyphs produced. */
14520 static int
14521 display_string (string, lisp_string, face_string, face_string_pos,
14522 start, it, field_width, precision, max_x, multibyte)
14523 unsigned char *string;
14524 Lisp_Object lisp_string;
14525 Lisp_Object face_string;
14526 int face_string_pos;
14527 int start;
14528 struct it *it;
14529 int field_width, precision, max_x;
14530 int multibyte;
14532 int hpos_at_start = it->hpos;
14533 int saved_face_id = it->face_id;
14534 struct glyph_row *row = it->glyph_row;
14536 /* Initialize the iterator IT for iteration over STRING beginning
14537 with index START. */
14538 reseat_to_string (it, string, lisp_string, start,
14539 precision, field_width, multibyte);
14541 /* If displaying STRING, set up the face of the iterator
14542 from LISP_STRING, if that's given. */
14543 if (STRINGP (face_string))
14545 int endptr;
14546 struct face *face;
14548 it->face_id
14549 = face_at_string_position (it->w, face_string, face_string_pos,
14550 0, it->region_beg_charpos,
14551 it->region_end_charpos,
14552 &endptr, it->base_face_id, 0);
14553 face = FACE_FROM_ID (it->f, it->face_id);
14554 it->face_box_p = face->box != FACE_NO_BOX;
14557 /* Set max_x to the maximum allowed X position. Don't let it go
14558 beyond the right edge of the window. */
14559 if (max_x <= 0)
14560 max_x = it->last_visible_x;
14561 else
14562 max_x = min (max_x, it->last_visible_x);
14564 /* Skip over display elements that are not visible. because IT->w is
14565 hscrolled. */
14566 if (it->current_x < it->first_visible_x)
14567 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14568 MOVE_TO_POS | MOVE_TO_X);
14570 row->ascent = it->max_ascent;
14571 row->height = it->max_ascent + it->max_descent;
14572 row->phys_ascent = it->max_phys_ascent;
14573 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14575 /* This condition is for the case that we are called with current_x
14576 past last_visible_x. */
14577 while (it->current_x < max_x)
14579 int x_before, x, n_glyphs_before, i, nglyphs;
14581 /* Get the next display element. */
14582 if (!get_next_display_element (it))
14583 break;
14585 /* Produce glyphs. */
14586 x_before = it->current_x;
14587 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14588 PRODUCE_GLYPHS (it);
14590 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14591 i = 0;
14592 x = x_before;
14593 while (i < nglyphs)
14595 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14597 if (!it->truncate_lines_p
14598 && x + glyph->pixel_width > max_x)
14600 /* End of continued line or max_x reached. */
14601 if (CHAR_GLYPH_PADDING_P (*glyph))
14603 /* A wide character is unbreakable. */
14604 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14605 it->current_x = x_before;
14607 else
14609 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14610 it->current_x = x;
14612 break;
14614 else if (x + glyph->pixel_width > it->first_visible_x)
14616 /* Glyph is at least partially visible. */
14617 ++it->hpos;
14618 if (x < it->first_visible_x)
14619 it->glyph_row->x = x - it->first_visible_x;
14621 else
14623 /* Glyph is off the left margin of the display area.
14624 Should not happen. */
14625 abort ();
14628 row->ascent = max (row->ascent, it->max_ascent);
14629 row->height = max (row->height, it->max_ascent + it->max_descent);
14630 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14631 row->phys_height = max (row->phys_height,
14632 it->max_phys_ascent + it->max_phys_descent);
14633 x += glyph->pixel_width;
14634 ++i;
14637 /* Stop if max_x reached. */
14638 if (i < nglyphs)
14639 break;
14641 /* Stop at line ends. */
14642 if (ITERATOR_AT_END_OF_LINE_P (it))
14644 it->continuation_lines_width = 0;
14645 break;
14648 set_iterator_to_next (it, 1);
14650 /* Stop if truncating at the right edge. */
14651 if (it->truncate_lines_p
14652 && it->current_x >= it->last_visible_x)
14654 /* Add truncation mark, but don't do it if the line is
14655 truncated at a padding space. */
14656 if (IT_CHARPOS (*it) < it->string_nchars)
14658 if (!FRAME_WINDOW_P (it->f))
14660 int i, n;
14662 if (it->current_x > it->last_visible_x)
14664 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14665 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14666 break;
14667 for (n = row->used[TEXT_AREA]; i < n; ++i)
14669 row->used[TEXT_AREA] = i;
14670 produce_special_glyphs (it, IT_TRUNCATION);
14673 produce_special_glyphs (it, IT_TRUNCATION);
14675 it->glyph_row->truncated_on_right_p = 1;
14677 break;
14681 /* Maybe insert a truncation at the left. */
14682 if (it->first_visible_x
14683 && IT_CHARPOS (*it) > 0)
14685 if (!FRAME_WINDOW_P (it->f))
14686 insert_left_trunc_glyphs (it);
14687 it->glyph_row->truncated_on_left_p = 1;
14690 it->face_id = saved_face_id;
14692 /* Value is number of columns displayed. */
14693 return it->hpos - hpos_at_start;
14698 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14699 appears as an element of LIST or as the car of an element of LIST.
14700 If PROPVAL is a list, compare each element against LIST in that
14701 way, and return 1/2 if any element of PROPVAL is found in LIST.
14702 Otherwise return 0. This function cannot quit.
14703 The return value is 2 if the text is invisible but with an ellipsis
14704 and 1 if it's invisible and without an ellipsis. */
14707 invisible_p (propval, list)
14708 register Lisp_Object propval;
14709 Lisp_Object list;
14711 register Lisp_Object tail, proptail;
14713 for (tail = list; CONSP (tail); tail = XCDR (tail))
14715 register Lisp_Object tem;
14716 tem = XCAR (tail);
14717 if (EQ (propval, tem))
14718 return 1;
14719 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14720 return NILP (XCDR (tem)) ? 1 : 2;
14723 if (CONSP (propval))
14725 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14727 Lisp_Object propelt;
14728 propelt = XCAR (proptail);
14729 for (tail = list; CONSP (tail); tail = XCDR (tail))
14731 register Lisp_Object tem;
14732 tem = XCAR (tail);
14733 if (EQ (propelt, tem))
14734 return 1;
14735 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14736 return NILP (XCDR (tem)) ? 1 : 2;
14741 return 0;
14745 /***********************************************************************
14746 Initialization
14747 ***********************************************************************/
14749 void
14750 syms_of_xdisp ()
14752 Vwith_echo_area_save_vector = Qnil;
14753 staticpro (&Vwith_echo_area_save_vector);
14755 Vmessage_stack = Qnil;
14756 staticpro (&Vmessage_stack);
14758 Qinhibit_redisplay = intern ("inhibit-redisplay");
14759 staticpro (&Qinhibit_redisplay);
14761 message_dolog_marker1 = Fmake_marker ();
14762 staticpro (&message_dolog_marker1);
14763 message_dolog_marker2 = Fmake_marker ();
14764 staticpro (&message_dolog_marker2);
14765 message_dolog_marker3 = Fmake_marker ();
14766 staticpro (&message_dolog_marker3);
14768 #if GLYPH_DEBUG
14769 defsubr (&Sdump_glyph_matrix);
14770 defsubr (&Sdump_glyph_row);
14771 defsubr (&Sdump_tool_bar_row);
14772 defsubr (&Strace_redisplay);
14773 defsubr (&Strace_to_stderr);
14774 #endif
14775 #ifdef HAVE_WINDOW_SYSTEM
14776 defsubr (&Stool_bar_lines_needed);
14777 #endif
14779 staticpro (&Qmenu_bar_update_hook);
14780 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14782 staticpro (&Qoverriding_terminal_local_map);
14783 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14785 staticpro (&Qoverriding_local_map);
14786 Qoverriding_local_map = intern ("overriding-local-map");
14788 staticpro (&Qwindow_scroll_functions);
14789 Qwindow_scroll_functions = intern ("window-scroll-functions");
14791 staticpro (&Qredisplay_end_trigger_functions);
14792 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14794 staticpro (&Qinhibit_point_motion_hooks);
14795 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14797 QCdata = intern (":data");
14798 staticpro (&QCdata);
14799 Qdisplay = intern ("display");
14800 staticpro (&Qdisplay);
14801 Qspace_width = intern ("space-width");
14802 staticpro (&Qspace_width);
14803 Qraise = intern ("raise");
14804 staticpro (&Qraise);
14805 Qspace = intern ("space");
14806 staticpro (&Qspace);
14807 Qmargin = intern ("margin");
14808 staticpro (&Qmargin);
14809 Qleft_margin = intern ("left-margin");
14810 staticpro (&Qleft_margin);
14811 Qright_margin = intern ("right-margin");
14812 staticpro (&Qright_margin);
14813 Qalign_to = intern ("align-to");
14814 staticpro (&Qalign_to);
14815 QCalign_to = intern (":align-to");
14816 staticpro (&QCalign_to);
14817 Qrelative_width = intern ("relative-width");
14818 staticpro (&Qrelative_width);
14819 QCrelative_width = intern (":relative-width");
14820 staticpro (&QCrelative_width);
14821 QCrelative_height = intern (":relative-height");
14822 staticpro (&QCrelative_height);
14823 QCeval = intern (":eval");
14824 staticpro (&QCeval);
14825 QCpropertize = intern (":propertize");
14826 staticpro (&QCpropertize);
14827 Qwhen = intern ("when");
14828 staticpro (&Qwhen);
14829 QCfile = intern (":file");
14830 staticpro (&QCfile);
14831 Qfontified = intern ("fontified");
14832 staticpro (&Qfontified);
14833 Qfontification_functions = intern ("fontification-functions");
14834 staticpro (&Qfontification_functions);
14835 Qtrailing_whitespace = intern ("trailing-whitespace");
14836 staticpro (&Qtrailing_whitespace);
14837 Qimage = intern ("image");
14838 staticpro (&Qimage);
14839 Qmessage_truncate_lines = intern ("message-truncate-lines");
14840 staticpro (&Qmessage_truncate_lines);
14841 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14842 staticpro (&Qcursor_in_non_selected_windows);
14843 Qgrow_only = intern ("grow-only");
14844 staticpro (&Qgrow_only);
14845 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14846 staticpro (&Qinhibit_menubar_update);
14847 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14848 staticpro (&Qinhibit_eval_during_redisplay);
14849 Qposition = intern ("position");
14850 staticpro (&Qposition);
14851 Qbuffer_position = intern ("buffer-position");
14852 staticpro (&Qbuffer_position);
14853 Qobject = intern ("object");
14854 staticpro (&Qobject);
14856 last_arrow_position = Qnil;
14857 last_arrow_string = Qnil;
14858 staticpro (&last_arrow_position);
14859 staticpro (&last_arrow_string);
14861 echo_buffer[0] = echo_buffer[1] = Qnil;
14862 staticpro (&echo_buffer[0]);
14863 staticpro (&echo_buffer[1]);
14865 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14866 staticpro (&echo_area_buffer[0]);
14867 staticpro (&echo_area_buffer[1]);
14869 Vmessages_buffer_name = build_string ("*Messages*");
14870 staticpro (&Vmessages_buffer_name);
14872 mode_line_proptrans_alist = Qnil;
14873 staticpro (&mode_line_proptrans_alist);
14875 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14876 doc: /* Non-nil means highlight trailing whitespace.
14877 The face used for trailing whitespace is `trailing-whitespace'. */);
14878 Vshow_trailing_whitespace = Qnil;
14880 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14881 doc: /* Non-nil means don't actually do any redisplay.
14882 This is used for internal purposes. */);
14883 Vinhibit_redisplay = Qnil;
14885 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14886 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14887 Vglobal_mode_string = Qnil;
14889 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14890 doc: /* Marker for where to display an arrow on top of the buffer text.
14891 This must be the beginning of a line in order to work.
14892 See also `overlay-arrow-string'. */);
14893 Voverlay_arrow_position = Qnil;
14895 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14896 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14897 Voverlay_arrow_string = Qnil;
14899 DEFVAR_INT ("scroll-step", &scroll_step,
14900 doc: /* *The number of lines to try scrolling a window by when point moves out.
14901 If that fails to bring point back on frame, point is centered instead.
14902 If this is zero, point is always centered after it moves off frame.
14903 If you want scrolling to always be a line at a time, you should set
14904 `scroll-conservatively' to a large value rather than set this to 1. */);
14906 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14907 doc: /* *Scroll up to this many lines, to bring point back on screen.
14908 A value of zero means to scroll the text to center point vertically
14909 in the window. */);
14910 scroll_conservatively = 0;
14912 DEFVAR_INT ("scroll-margin", &scroll_margin,
14913 doc: /* *Number of lines of margin at the top and bottom of a window.
14914 Recenter the window whenever point gets within this many lines
14915 of the top or bottom of the window. */);
14916 scroll_margin = 0;
14918 #if GLYPH_DEBUG
14919 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14920 #endif
14922 DEFVAR_BOOL ("truncate-partial-width-windows",
14923 &truncate_partial_width_windows,
14924 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14925 truncate_partial_width_windows = 1;
14927 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14928 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14929 Any other value means to use the appropriate face, `mode-line',
14930 `header-line', or `menu' respectively.
14932 This variable is deprecated; please change the above faces instead. */);
14933 mode_line_inverse_video = 1;
14935 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14936 doc: /* *Maximum buffer size for which line number should be displayed.
14937 If the buffer is bigger than this, the line number does not appear
14938 in the mode line. A value of nil means no limit. */);
14939 Vline_number_display_limit = Qnil;
14941 DEFVAR_INT ("line-number-display-limit-width",
14942 &line_number_display_limit_width,
14943 doc: /* *Maximum line width (in characters) for line number display.
14944 If the average length of the lines near point is bigger than this, then the
14945 line number may be omitted from the mode line. */);
14946 line_number_display_limit_width = 200;
14948 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14949 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14950 highlight_nonselected_windows = 0;
14952 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14953 doc: /* Non-nil if more than one frame is visible on this display.
14954 Minibuffer-only frames don't count, but iconified frames do.
14955 This variable is not guaranteed to be accurate except while processing
14956 `frame-title-format' and `icon-title-format'. */);
14958 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14959 doc: /* Template for displaying the title bar of visible frames.
14960 \(Assuming the window manager supports this feature.)
14961 This variable has the same structure as `mode-line-format' (which see),
14962 and is used only on frames for which no explicit name has been set
14963 \(see `modify-frame-parameters'). */);
14964 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14965 doc: /* Template for displaying the title bar of an iconified frame.
14966 \(Assuming the window manager supports this feature.)
14967 This variable has the same structure as `mode-line-format' (which see),
14968 and is used only on frames for which no explicit name has been set
14969 \(see `modify-frame-parameters'). */);
14970 Vicon_title_format
14971 = Vframe_title_format
14972 = Fcons (intern ("multiple-frames"),
14973 Fcons (build_string ("%b"),
14974 Fcons (Fcons (empty_string,
14975 Fcons (intern ("invocation-name"),
14976 Fcons (build_string ("@"),
14977 Fcons (intern ("system-name"),
14978 Qnil)))),
14979 Qnil)));
14981 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14982 doc: /* Maximum number of lines to keep in the message log buffer.
14983 If nil, disable message logging. If t, log messages but don't truncate
14984 the buffer when it becomes large. */);
14985 Vmessage_log_max = make_number (50);
14987 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14988 doc: /* Functions called before redisplay, if window sizes have changed.
14989 The value should be a list of functions that take one argument.
14990 Just before redisplay, for each frame, if any of its windows have changed
14991 size since the last redisplay, or have been split or deleted,
14992 all the functions in the list are called, with the frame as argument. */);
14993 Vwindow_size_change_functions = Qnil;
14995 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14996 doc: /* List of Functions to call before redisplaying a window with scrolling.
14997 Each function is called with two arguments, the window
14998 and its new display-start position. Note that the value of `window-end'
14999 is not valid when these functions are called. */);
15000 Vwindow_scroll_functions = Qnil;
15002 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15003 doc: /* *Non-nil means automatically resize tool-bars.
15004 This increases a tool-bar's height if not all tool-bar items are visible.
15005 It decreases a tool-bar's height when it would display blank lines
15006 otherwise. */);
15007 auto_resize_tool_bars_p = 1;
15009 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15010 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15011 auto_raise_tool_bar_buttons_p = 1;
15013 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15014 doc: /* *Margin around tool-bar buttons in pixels.
15015 If an integer, use that for both horizontal and vertical margins.
15016 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15017 HORZ specifying the horizontal margin, and VERT specifying the
15018 vertical margin. */);
15019 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15021 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15022 doc: /* *Relief thickness of tool-bar buttons. */);
15023 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15025 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15026 doc: /* List of functions to call to fontify regions of text.
15027 Each function is called with one argument POS. Functions must
15028 fontify a region starting at POS in the current buffer, and give
15029 fontified regions the property `fontified'. */);
15030 Vfontification_functions = Qnil;
15031 Fmake_variable_buffer_local (Qfontification_functions);
15033 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15034 &unibyte_display_via_language_environment,
15035 doc: /* *Non-nil means display unibyte text according to language environment.
15036 Specifically this means that unibyte non-ASCII characters
15037 are displayed by converting them to the equivalent multibyte characters
15038 according to the current language environment. As a result, they are
15039 displayed according to the current fontset. */);
15040 unibyte_display_via_language_environment = 0;
15042 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15043 doc: /* *Maximum height for resizing mini-windows.
15044 If a float, it specifies a fraction of the mini-window frame's height.
15045 If an integer, it specifies a number of lines. */);
15046 Vmax_mini_window_height = make_float (0.25);
15048 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15049 doc: /* *How to resize mini-windows.
15050 A value of nil means don't automatically resize mini-windows.
15051 A value of t means resize them to fit the text displayed in them.
15052 A value of `grow-only', the default, means let mini-windows grow
15053 only, until their display becomes empty, at which point the windows
15054 go back to their normal size. */);
15055 Vresize_mini_windows = Qgrow_only;
15057 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15058 &cursor_in_non_selected_windows,
15059 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15060 nil means don't display a cursor there. */);
15061 cursor_in_non_selected_windows = 1;
15063 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
15064 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15065 automatic_hscrolling_p = 1;
15067 DEFVAR_INT ("automatic-hscroll-margin", &automatic_hscroll_margin,
15068 doc: /* *How many columns away from the window edge point is allowed to get
15069 before automatic hscrolling will horizontally scroll the window. */);
15070 automatic_hscroll_margin = 5;
15072 DEFVAR_LISP ("automatic-hscroll-step", &Vautomatic_hscroll_step,
15073 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15074 When point is less than `automatic-hscroll-margin' columns from the window
15075 edge, automatic hscrolling will scroll the window by the amount of columns
15076 determined by this variable. If its value is a positive integer, scroll that
15077 many columns. If it's a positive floating-point number, it specifies the
15078 fraction of the window's width to scroll. If it's nil or zero, point will be
15079 centered horizontally after the scroll. Any other value, including negative
15080 numbers, are treated as if the value were zero.
15082 Automatic hscrolling always moves point outside the scroll margin, so if
15083 point was more than scroll step columns inside the margin, the window will
15084 scroll more than the value given by the scroll step.
15086 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15087 and `scroll-right' overrides this variable's effect. */);
15088 Vautomatic_hscroll_step = make_number (0);
15090 DEFVAR_LISP ("image-types", &Vimage_types,
15091 doc: /* List of supported image types.
15092 Each element of the list is a symbol for a supported image type. */);
15093 Vimage_types = Qnil;
15095 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15096 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15097 Bind this around calls to `message' to let it take effect. */);
15098 message_truncate_lines = 0;
15100 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15101 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15102 Can be used to update submenus whose contents should vary. */);
15103 Vmenu_bar_update_hook = Qnil;
15105 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15106 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15107 inhibit_menubar_update = 0;
15109 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15110 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15111 inhibit_eval_during_redisplay = 0;
15113 #if GLYPH_DEBUG
15114 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15115 doc: /* Inhibit try_window_id display optimization. */);
15116 inhibit_try_window_id = 0;
15118 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15119 doc: /* Inhibit try_window_reusing display optimization. */);
15120 inhibit_try_window_reusing = 0;
15122 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15123 doc: /* Inhibit try_cursor_movement display optimization. */);
15124 inhibit_try_cursor_movement = 0;
15125 #endif /* GLYPH_DEBUG */
15129 /* Initialize this module when Emacs starts. */
15131 void
15132 init_xdisp ()
15134 Lisp_Object root_window;
15135 struct window *mini_w;
15137 current_header_line_height = current_mode_line_height = -1;
15139 CHARPOS (this_line_start_pos) = 0;
15141 mini_w = XWINDOW (minibuf_window);
15142 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15144 if (!noninteractive)
15146 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15147 int i;
15149 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15150 set_window_height (root_window,
15151 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15153 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15154 set_window_height (minibuf_window, 1, 0);
15156 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15157 mini_w->width = make_number (FRAME_WIDTH (f));
15159 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15160 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15161 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15163 /* The default ellipsis glyphs `...'. */
15164 for (i = 0; i < 3; ++i)
15165 default_invis_vector[i] = make_number ('.');
15168 #ifdef HAVE_WINDOW_SYSTEM
15170 /* Allocate the buffer for frame titles. */
15171 int size = 100;
15172 frame_title_buf = (char *) xmalloc (size);
15173 frame_title_buf_end = frame_title_buf + size;
15174 frame_title_ptr = NULL;
15176 #endif /* HAVE_WINDOW_SYSTEM */
15178 help_echo_showing_p = 0;