*** empty log message ***
[emacs.git] / src / xdisp.c
blobf17467f63c77b5b285912890a00e5e8237218613
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 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 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 be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #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 "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
204 #define INFINITY 10000000
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
211 extern int interrupt_input;
212 extern int command_loop_level;
214 extern int minibuffer_auto_raise;
216 extern Lisp_Object Qface;
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
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, Qinhibit_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, Qimage, 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;
296 /* The buffer position of the first character appearing entirely or
297 partially on the line of the selected window which contains the
298 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
299 redisplay optimization in redisplay_internal. */
301 static struct text_pos this_line_start_pos;
303 /* Number of characters past the end of the line above, including the
304 terminating newline. */
306 static struct text_pos this_line_end_pos;
308 /* The vertical positions and the height of this line. */
310 static int this_line_vpos;
311 static int this_line_y;
312 static int this_line_pixel_height;
314 /* X position at which this display line starts. Usually zero;
315 negative if first character is partially visible. */
317 static int this_line_start_x;
319 /* Buffer that this_line_.* variables are referring to. */
321 static struct buffer *this_line_buffer;
323 /* Nonzero means truncate lines in all windows less wide than the
324 frame. */
326 int truncate_partial_width_windows;
328 /* A flag to control how to display unibyte 8-bit character. */
330 int unibyte_display_via_language_environment;
332 /* Nonzero means we have more than one non-mini-buffer-only frame.
333 Not guaranteed to be accurate except while parsing
334 frame-title-format. */
336 int multiple_frames;
338 Lisp_Object Vglobal_mode_string;
340 /* Marker for where to display an arrow on top of the buffer text. */
342 Lisp_Object Voverlay_arrow_position;
344 /* String to display for the arrow. Only used on terminal frames. */
346 Lisp_Object Voverlay_arrow_string;
348 /* Values of those variables at last redisplay. However, if
349 Voverlay_arrow_position is a marker, last_arrow_position is its
350 numerical position. */
352 static Lisp_Object last_arrow_position, last_arrow_string;
354 /* Like mode-line-format, but for the title bar on a visible frame. */
356 Lisp_Object Vframe_title_format;
358 /* Like mode-line-format, but for the title bar on an iconified frame. */
360 Lisp_Object Vicon_title_format;
362 /* List of functions to call when a window's size changes. These
363 functions get one arg, a frame on which one or more windows' sizes
364 have changed. */
366 static Lisp_Object Vwindow_size_change_functions;
368 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
370 /* Nonzero if overlay arrow has been displayed once in this window. */
372 static int overlay_arrow_seen;
374 /* Nonzero means highlight the region even in nonselected windows. */
376 int highlight_nonselected_windows;
378 /* If cursor motion alone moves point off frame, try scrolling this
379 many lines up or down if that will bring it back. */
381 static int scroll_step;
383 /* Non-0 means scroll just far enough to bring point back on the
384 screen, when appropriate. */
386 static int scroll_conservatively;
388 /* Recenter the window whenever point gets within this many lines of
389 the top or bottom of the window. This value is translated into a
390 pixel value by multiplying it with CANON_Y_UNIT, which means that
391 there is really a fixed pixel height scroll margin. */
393 int scroll_margin;
395 /* Number of windows showing the buffer of the selected window (or
396 another buffer with the same base buffer). keyboard.c refers to
397 this. */
399 int buffer_shared;
401 /* Vector containing glyphs for an ellipsis `...'. */
403 static Lisp_Object default_invis_vector[3];
405 /* Zero means display the mode-line/header-line/menu-bar in the default face
406 (this slightly odd definition is for compatibility with previous versions
407 of emacs), non-zero means display them using their respective faces.
409 This variable is deprecated. */
411 int mode_line_inverse_video;
413 /* Prompt to display in front of the mini-buffer contents. */
415 Lisp_Object minibuf_prompt;
417 /* Width of current mini-buffer prompt. Only set after display_line
418 of the line that contains the prompt. */
420 int minibuf_prompt_width;
421 int minibuf_prompt_pixel_width;
423 /* This is the window where the echo area message was displayed. It
424 is always a mini-buffer window, but it may not be the same window
425 currently active as a mini-buffer. */
427 Lisp_Object echo_area_window;
429 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
430 pushes the current message and the value of
431 message_enable_multibyte on the stack, the function restore_message
432 pops the stack and displays MESSAGE again. */
434 Lisp_Object Vmessage_stack;
436 /* Nonzero means multibyte characters were enabled when the echo area
437 message was specified. */
439 int message_enable_multibyte;
441 /* True if we should redraw the mode lines on the next redisplay. */
443 int update_mode_lines;
445 /* Nonzero if window sizes or contents have changed since last
446 redisplay that finished */
448 int windows_or_buffers_changed;
450 /* Nonzero after display_mode_line if %l was used and it displayed a
451 line number. */
453 int line_number_displayed;
455 /* Maximum buffer size for which to display line numbers. */
457 Lisp_Object Vline_number_display_limit;
459 /* line width to consider when repostioning for line number display */
461 static int line_number_display_limit_width;
463 /* Number of lines to keep in the message log buffer. t means
464 infinite. nil means don't log at all. */
466 Lisp_Object Vmessage_log_max;
468 /* The name of the *Messages* buffer, a string. */
470 static Lisp_Object Vmessages_buffer_name;
472 /* Current, index 0, and last displayed echo area message. Either
473 buffers from echo_buffers, or nil to indicate no message. */
475 Lisp_Object echo_area_buffer[2];
477 /* The buffers referenced from echo_area_buffer. */
479 static Lisp_Object echo_buffer[2];
481 /* A vector saved used in with_area_buffer to reduce consing. */
483 static Lisp_Object Vwith_echo_area_save_vector;
485 /* Non-zero means display_echo_area should display the last echo area
486 message again. Set by redisplay_preserve_echo_area. */
488 static int display_last_displayed_message_p;
490 /* Nonzero if echo area is being used by print; zero if being used by
491 message. */
493 int message_buf_print;
495 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
497 Lisp_Object Qinhibit_menubar_update;
498 int inhibit_menubar_update;
500 /* Maximum height for resizing mini-windows. Either a float
501 specifying a fraction of the available height, or an integer
502 specifying a number of lines. */
504 Lisp_Object Vmax_mini_window_height;
506 /* Non-zero means messages should be displayed with truncated
507 lines instead of being continued. */
509 int message_truncate_lines;
510 Lisp_Object Qmessage_truncate_lines;
512 /* Non-zero means we want a hollow cursor in windows that are not
513 selected. Zero means there's no cursor in such windows. */
515 int cursor_in_non_selected_windows;
517 /* A scratch glyph row with contents used for generating truncation
518 glyphs. Also used in direct_output_for_insert. */
520 #define MAX_SCRATCH_GLYPHS 100
521 struct glyph_row scratch_glyph_row;
522 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
524 /* Ascent and height of the last line processed by move_it_to. */
526 static int last_max_ascent, last_height;
528 /* Non-zero if there's a help-echo in the echo area. */
530 int help_echo_showing_p;
532 /* If >= 0, computed, exact values of mode-line and header-line height
533 to use in the macros CURRENT_MODE_LINE_HEIGHT and
534 CURRENT_HEADER_LINE_HEIGHT. */
536 int current_mode_line_height, current_header_line_height;
538 /* The maximum distance to look ahead for text properties. Values
539 that are too small let us call compute_char_face and similar
540 functions too often which is expensive. Values that are too large
541 let us call compute_char_face and alike too often because we
542 might not be interested in text properties that far away. */
544 #define TEXT_PROP_DISTANCE_LIMIT 100
546 #if GLYPH_DEBUG
548 /* Non-zero means print traces of redisplay if compiled with
549 GLYPH_DEBUG != 0. */
551 int trace_redisplay_p;
553 #endif /* GLYPH_DEBUG */
555 #ifdef DEBUG_TRACE_MOVE
556 /* Non-zero means trace with TRACE_MOVE to stderr. */
557 int trace_move;
559 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
560 #else
561 #define TRACE_MOVE(x) (void) 0
562 #endif
564 /* Non-zero means automatically scroll windows horizontally to make
565 point visible. */
567 int automatic_hscrolling_p;
569 /* A list of symbols, one for each supported image type. */
571 Lisp_Object Vimage_types;
573 /* The variable `resize-mini-windows'. If nil, don't resize
574 mini-windows. If t, always resize them to fit the text they
575 display. If `grow-only', let mini-windows grow only until they
576 become empty. */
578 Lisp_Object Vresize_mini_windows;
580 /* Value returned from text property handlers (see below). */
582 enum prop_handled
584 HANDLED_NORMALLY,
585 HANDLED_RECOMPUTE_PROPS,
586 HANDLED_OVERLAY_STRING_CONSUMED,
587 HANDLED_RETURN
590 /* A description of text properties that redisplay is interested
591 in. */
593 struct props
595 /* The name of the property. */
596 Lisp_Object *name;
598 /* A unique index for the property. */
599 enum prop_idx idx;
601 /* A handler function called to set up iterator IT from the property
602 at IT's current position. Value is used to steer handle_stop. */
603 enum prop_handled (*handler) P_ ((struct it *it));
606 static enum prop_handled handle_face_prop P_ ((struct it *));
607 static enum prop_handled handle_invisible_prop P_ ((struct it *));
608 static enum prop_handled handle_display_prop P_ ((struct it *));
609 static enum prop_handled handle_composition_prop P_ ((struct it *));
610 static enum prop_handled handle_overlay_change P_ ((struct it *));
611 static enum prop_handled handle_fontified_prop P_ ((struct it *));
613 /* Properties handled by iterators. */
615 static struct props it_props[] =
617 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
618 /* Handle `face' before `display' because some sub-properties of
619 `display' need to know the face. */
620 {&Qface, FACE_PROP_IDX, handle_face_prop},
621 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
622 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
623 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
624 {NULL, 0, NULL}
627 /* Value is the position described by X. If X is a marker, value is
628 the marker_position of X. Otherwise, value is X. */
630 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
632 /* Enumeration returned by some move_it_.* functions internally. */
634 enum move_it_result
636 /* Not used. Undefined value. */
637 MOVE_UNDEFINED,
639 /* Move ended at the requested buffer position or ZV. */
640 MOVE_POS_MATCH_OR_ZV,
642 /* Move ended at the requested X pixel position. */
643 MOVE_X_REACHED,
645 /* Move within a line ended at the end of a line that must be
646 continued. */
647 MOVE_LINE_CONTINUED,
649 /* Move within a line ended at the end of a line that would
650 be displayed truncated. */
651 MOVE_LINE_TRUNCATED,
653 /* Move within a line ended at a line end. */
654 MOVE_NEWLINE_OR_CR
659 /* Function prototypes. */
661 static void mark_window_display_accurate_1 P_ ((struct window *, int));
662 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
663 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
664 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
665 static int redisplay_mode_lines P_ ((Lisp_Object, int));
666 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
667 static int invisible_text_between_p P_ ((struct it *, int, int));
668 static int next_element_from_ellipsis P_ ((struct it *));
669 static void pint2str P_ ((char *, int, int));
670 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
671 struct text_pos));
672 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
673 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
674 static void store_frame_title_char P_ ((char));
675 static int store_frame_title P_ ((unsigned char *, int, int));
676 static void x_consider_frame_title P_ ((Lisp_Object));
677 static void handle_stop P_ ((struct it *));
678 static int tool_bar_lines_needed P_ ((struct frame *));
679 static int single_display_prop_intangible_p P_ ((Lisp_Object));
680 static void ensure_echo_area_buffers P_ ((void));
681 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
682 struct glyph_row *,
683 struct glyph_row *));
684 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
685 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
686 static int with_echo_area_buffer P_ ((struct window *, int,
687 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
688 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
689 static void clear_garbaged_frames P_ ((void));
690 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
693 static int display_echo_area P_ ((struct window *));
694 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
695 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
696 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
697 static int string_char_and_length P_ ((unsigned char *, int, int *));
698 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
699 struct text_pos));
700 static int compute_window_start_on_continuation_line P_ ((struct window *));
701 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
702 static void insert_left_trunc_glyphs P_ ((struct it *));
703 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
704 static void extend_face_to_end_of_line P_ ((struct it *));
705 static int append_space P_ ((struct it *, int));
706 static void make_cursor_line_fully_visible P_ ((struct window *));
707 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
708 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
709 static int trailing_whitespace_p P_ ((int));
710 static int message_log_check_duplicate P_ ((int, int, int, int));
711 int invisible_p P_ ((Lisp_Object, Lisp_Object));
712 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
713 static void push_it P_ ((struct it *));
714 static void pop_it P_ ((struct it *));
715 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
716 static void redisplay_internal P_ ((int));
717 static int echo_area_display P_ ((int));
718 static void redisplay_windows P_ ((Lisp_Object));
719 static void redisplay_window P_ ((Lisp_Object, int));
720 static void update_menu_bar P_ ((struct frame *, int));
721 static int try_window_reusing_current_matrix P_ ((struct window *));
722 static int try_window_id P_ ((struct window *));
723 static int display_line P_ ((struct it *));
724 static int display_mode_lines P_ ((struct window *));
725 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
726 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
727 static char *decode_mode_spec P_ ((struct window *, int, int, int));
728 static void display_menu_bar P_ ((struct window *));
729 static int display_count_lines P_ ((int, int, int, int, int *));
730 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
731 int, int, struct it *, int, int, int, int));
732 static void compute_line_metrics P_ ((struct it *));
733 static void run_redisplay_end_trigger_hook P_ ((struct it *));
734 static int get_overlay_strings P_ ((struct it *));
735 static void next_overlay_string P_ ((struct it *));
736 static void reseat P_ ((struct it *, struct text_pos, int));
737 static void reseat_1 P_ ((struct it *, struct text_pos, int));
738 static void back_to_previous_visible_line_start P_ ((struct it *));
739 static void reseat_at_previous_visible_line_start P_ ((struct it *));
740 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
741 static int next_element_from_display_vector P_ ((struct it *));
742 static int next_element_from_string P_ ((struct it *));
743 static int next_element_from_c_string P_ ((struct it *));
744 static int next_element_from_buffer P_ ((struct it *));
745 static int next_element_from_composition P_ ((struct it *));
746 static int next_element_from_image P_ ((struct it *));
747 static int next_element_from_stretch P_ ((struct it *));
748 static void load_overlay_strings P_ ((struct it *));
749 static void init_from_display_pos P_ ((struct it *, struct window *,
750 struct display_pos *));
751 static void reseat_to_string P_ ((struct it *, unsigned char *,
752 Lisp_Object, int, int, int, int));
753 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
754 int, int, int));
755 void move_it_vertically_backward P_ ((struct it *, int));
756 static void init_to_row_start P_ ((struct it *, struct window *,
757 struct glyph_row *));
758 static void init_to_row_end P_ ((struct it *, struct window *,
759 struct glyph_row *));
760 static void back_to_previous_line_start P_ ((struct it *));
761 static int forward_to_next_line_start P_ ((struct it *, int *));
762 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
763 Lisp_Object, int));
764 static struct text_pos string_pos P_ ((int, Lisp_Object));
765 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
766 static int number_of_chars P_ ((unsigned char *, int));
767 static void compute_stop_pos P_ ((struct it *));
768 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
769 Lisp_Object));
770 static int face_before_or_after_it_pos P_ ((struct it *, int));
771 static int next_overlay_change P_ ((int));
772 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
773 Lisp_Object, struct text_pos *,
774 int));
775 static int underlying_face_id P_ ((struct it *));
776 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
777 struct window *));
779 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
780 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
782 #ifdef HAVE_WINDOW_SYSTEM
784 static void update_tool_bar P_ ((struct frame *, int));
785 static void build_desired_tool_bar_string P_ ((struct frame *f));
786 static int redisplay_tool_bar P_ ((struct frame *));
787 static void display_tool_bar_line P_ ((struct it *));
789 #endif /* HAVE_WINDOW_SYSTEM */
792 /***********************************************************************
793 Window display dimensions
794 ***********************************************************************/
796 /* Return the window-relative maximum y + 1 for glyph rows displaying
797 text in window W. This is the height of W minus the height of a
798 mode line, if any. */
800 INLINE int
801 window_text_bottom_y (w)
802 struct window *w;
804 struct frame *f = XFRAME (w->frame);
805 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
807 if (WINDOW_WANTS_MODELINE_P (w))
808 height -= CURRENT_MODE_LINE_HEIGHT (w);
809 return height;
813 /* Return the pixel width of display area AREA of window W. AREA < 0
814 means return the total width of W, not including bitmap areas to
815 the left and right of the window. */
817 INLINE int
818 window_box_width (w, area)
819 struct window *w;
820 int area;
822 struct frame *f = XFRAME (w->frame);
823 int width = XFASTINT (w->width);
825 if (!w->pseudo_window_p)
827 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
829 if (area == TEXT_AREA)
831 if (INTEGERP (w->left_margin_width))
832 width -= XFASTINT (w->left_margin_width);
833 if (INTEGERP (w->right_margin_width))
834 width -= XFASTINT (w->right_margin_width);
836 else if (area == LEFT_MARGIN_AREA)
837 width = (INTEGERP (w->left_margin_width)
838 ? XFASTINT (w->left_margin_width) : 0);
839 else if (area == RIGHT_MARGIN_AREA)
840 width = (INTEGERP (w->right_margin_width)
841 ? XFASTINT (w->right_margin_width) : 0);
844 return width * CANON_X_UNIT (f);
848 /* Return the pixel height of the display area of window W, not
849 including mode lines of W, if any.. */
851 INLINE int
852 window_box_height (w)
853 struct window *w;
855 struct frame *f = XFRAME (w->frame);
856 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
858 xassert (height >= 0);
860 /* Note: the code below that determines the mode-line/header-line
861 height is essentially the same as that contained in the macro
862 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
863 the appropriate glyph row has its `mode_line_p' flag set,
864 and if it doesn't, uses estimate_mode_line_height instead. */
866 if (WINDOW_WANTS_MODELINE_P (w))
868 struct glyph_row *ml_row
869 = (w->current_matrix && w->current_matrix->rows
870 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
871 : 0);
872 if (ml_row && ml_row->mode_line_p)
873 height -= ml_row->height;
874 else
875 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
878 if (WINDOW_WANTS_HEADER_LINE_P (w))
880 struct glyph_row *hl_row
881 = (w->current_matrix && w->current_matrix->rows
882 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
883 : 0);
884 if (hl_row && hl_row->mode_line_p)
885 height -= hl_row->height;
886 else
887 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
890 return height;
894 /* Return the frame-relative coordinate of the left edge of display
895 area AREA of window W. AREA < 0 means return the left edge of the
896 whole window, to the right of any bitmap area at the left side of
897 W. */
899 INLINE int
900 window_box_left (w, area)
901 struct window *w;
902 int area;
904 struct frame *f = XFRAME (w->frame);
905 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
907 if (!w->pseudo_window_p)
909 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
910 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
912 if (area == TEXT_AREA)
913 x += window_box_width (w, LEFT_MARGIN_AREA);
914 else if (area == RIGHT_MARGIN_AREA)
915 x += (window_box_width (w, LEFT_MARGIN_AREA)
916 + window_box_width (w, TEXT_AREA));
919 return x;
923 /* Return the frame-relative coordinate of the right edge of display
924 area AREA of window W. AREA < 0 means return the left edge of the
925 whole window, to the left of any bitmap area at the right side of
926 W. */
928 INLINE int
929 window_box_right (w, area)
930 struct window *w;
931 int area;
933 return window_box_left (w, area) + window_box_width (w, area);
937 /* Get the bounding box of the display area AREA of window W, without
938 mode lines, in frame-relative coordinates. AREA < 0 means the
939 whole window, not including bitmap areas to the left and right of
940 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
941 coordinates of the upper-left corner of the box. Return in
942 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
944 INLINE void
945 window_box (w, area, box_x, box_y, box_width, box_height)
946 struct window *w;
947 int area;
948 int *box_x, *box_y, *box_width, *box_height;
950 struct frame *f = XFRAME (w->frame);
952 *box_width = window_box_width (w, area);
953 *box_height = window_box_height (w);
954 *box_x = window_box_left (w, area);
955 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
956 + XFASTINT (w->top) * CANON_Y_UNIT (f));
957 if (WINDOW_WANTS_HEADER_LINE_P (w))
958 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
962 /* Get the bounding box of the display area AREA of window W, without
963 mode lines. AREA < 0 means the whole window, not including bitmap
964 areas to the left and right of the window. Return in *TOP_LEFT_X
965 and TOP_LEFT_Y the frame-relative pixel coordinates of the
966 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
967 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
968 box. */
970 INLINE void
971 window_box_edges (w, area, top_left_x, top_left_y,
972 bottom_right_x, bottom_right_y)
973 struct window *w;
974 int area;
975 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
977 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
978 bottom_right_y);
979 *bottom_right_x += *top_left_x;
980 *bottom_right_y += *top_left_y;
985 /***********************************************************************
986 Utilities
987 ***********************************************************************/
989 /* Return the bottom y-position of the line the iterator IT is in.
990 This can modify IT's settings. */
993 line_bottom_y (it)
994 struct it *it;
996 int line_height = it->max_ascent + it->max_descent;
997 int line_top_y = it->current_y;
999 if (line_height == 0)
1001 if (last_height)
1002 line_height = last_height;
1003 else if (IT_CHARPOS (*it) < ZV)
1005 move_it_by_lines (it, 1, 1);
1006 line_height = (it->max_ascent || it->max_descent
1007 ? it->max_ascent + it->max_descent
1008 : last_height);
1010 else
1012 struct glyph_row *row = it->glyph_row;
1014 /* Use the default character height. */
1015 it->glyph_row = NULL;
1016 it->what = IT_CHARACTER;
1017 it->c = ' ';
1018 it->len = 1;
1019 PRODUCE_GLYPHS (it);
1020 line_height = it->ascent + it->descent;
1021 it->glyph_row = row;
1025 return line_top_y + line_height;
1029 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1030 1 if POS is visible and the line containing POS is fully visible.
1031 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1032 and header-lines heights. */
1035 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1036 struct window *w;
1037 int charpos, *fully, exact_mode_line_heights_p;
1039 struct it it;
1040 struct text_pos top;
1041 int visible_p;
1042 struct buffer *old_buffer = NULL;
1044 if (XBUFFER (w->buffer) != current_buffer)
1046 old_buffer = current_buffer;
1047 set_buffer_internal_1 (XBUFFER (w->buffer));
1050 *fully = visible_p = 0;
1051 SET_TEXT_POS_FROM_MARKER (top, w->start);
1053 /* Compute exact mode line heights, if requested. */
1054 if (exact_mode_line_heights_p)
1056 if (WINDOW_WANTS_MODELINE_P (w))
1057 current_mode_line_height
1058 = display_mode_line (w, MODE_LINE_FACE_ID,
1059 current_buffer->mode_line_format);
1061 if (WINDOW_WANTS_HEADER_LINE_P (w))
1062 current_header_line_height
1063 = display_mode_line (w, HEADER_LINE_FACE_ID,
1064 current_buffer->header_line_format);
1067 start_display (&it, w, top);
1068 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1069 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1071 /* Note that we may overshoot because of invisible text. */
1072 if (IT_CHARPOS (it) >= charpos)
1074 int top_y = it.current_y;
1075 int bottom_y = line_bottom_y (&it);
1076 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1078 if (top_y < window_top_y)
1079 visible_p = bottom_y > window_top_y;
1080 else if (top_y < it.last_visible_y)
1082 visible_p = 1;
1083 *fully = bottom_y <= it.last_visible_y;
1086 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1088 move_it_by_lines (&it, 1, 0);
1089 if (charpos < IT_CHARPOS (it))
1091 visible_p = 1;
1092 *fully = 0;
1096 if (old_buffer)
1097 set_buffer_internal_1 (old_buffer);
1099 current_header_line_height = current_mode_line_height = -1;
1100 return visible_p;
1104 /* Return the next character from STR which is MAXLEN bytes long.
1105 Return in *LEN the length of the character. This is like
1106 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1107 we find one, we return a `?', but with the length of the invalid
1108 character. */
1110 static INLINE int
1111 string_char_and_length (str, maxlen, len)
1112 unsigned char *str;
1113 int maxlen, *len;
1115 int c;
1117 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1118 if (!CHAR_VALID_P (c, 1))
1119 /* We may not change the length here because other places in Emacs
1120 don't use this function, i.e. they silently accept invalid
1121 characters. */
1122 c = '?';
1124 return c;
1129 /* Given a position POS containing a valid character and byte position
1130 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1132 static struct text_pos
1133 string_pos_nchars_ahead (pos, string, nchars)
1134 struct text_pos pos;
1135 Lisp_Object string;
1136 int nchars;
1138 xassert (STRINGP (string) && nchars >= 0);
1140 if (STRING_MULTIBYTE (string))
1142 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1143 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1144 int len;
1146 while (nchars--)
1148 string_char_and_length (p, rest, &len);
1149 p += len, rest -= len;
1150 xassert (rest >= 0);
1151 CHARPOS (pos) += 1;
1152 BYTEPOS (pos) += len;
1155 else
1156 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1158 return pos;
1162 /* Value is the text position, i.e. character and byte position,
1163 for character position CHARPOS in STRING. */
1165 static INLINE struct text_pos
1166 string_pos (charpos, string)
1167 int charpos;
1168 Lisp_Object string;
1170 struct text_pos pos;
1171 xassert (STRINGP (string));
1172 xassert (charpos >= 0);
1173 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1174 return pos;
1178 /* Value is a text position, i.e. character and byte position, for
1179 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1180 means recognize multibyte characters. */
1182 static struct text_pos
1183 c_string_pos (charpos, s, multibyte_p)
1184 int charpos;
1185 unsigned char *s;
1186 int multibyte_p;
1188 struct text_pos pos;
1190 xassert (s != NULL);
1191 xassert (charpos >= 0);
1193 if (multibyte_p)
1195 int rest = strlen (s), len;
1197 SET_TEXT_POS (pos, 0, 0);
1198 while (charpos--)
1200 string_char_and_length (s, rest, &len);
1201 s += len, rest -= len;
1202 xassert (rest >= 0);
1203 CHARPOS (pos) += 1;
1204 BYTEPOS (pos) += len;
1207 else
1208 SET_TEXT_POS (pos, charpos, charpos);
1210 return pos;
1214 /* Value is the number of characters in C string S. MULTIBYTE_P
1215 non-zero means recognize multibyte characters. */
1217 static int
1218 number_of_chars (s, multibyte_p)
1219 unsigned char *s;
1220 int multibyte_p;
1222 int nchars;
1224 if (multibyte_p)
1226 int rest = strlen (s), len;
1227 unsigned char *p = (unsigned char *) s;
1229 for (nchars = 0; rest > 0; ++nchars)
1231 string_char_and_length (p, rest, &len);
1232 rest -= len, p += len;
1235 else
1236 nchars = strlen (s);
1238 return nchars;
1242 /* Compute byte position NEWPOS->bytepos corresponding to
1243 NEWPOS->charpos. POS is a known position in string STRING.
1244 NEWPOS->charpos must be >= POS.charpos. */
1246 static void
1247 compute_string_pos (newpos, pos, string)
1248 struct text_pos *newpos, pos;
1249 Lisp_Object string;
1251 xassert (STRINGP (string));
1252 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1254 if (STRING_MULTIBYTE (string))
1255 *newpos = string_pos_nchars_ahead (pos, string,
1256 CHARPOS (*newpos) - CHARPOS (pos));
1257 else
1258 BYTEPOS (*newpos) = CHARPOS (*newpos);
1263 /***********************************************************************
1264 Lisp form evaluation
1265 ***********************************************************************/
1267 /* Error handler for safe_eval and safe_call. */
1269 static Lisp_Object
1270 safe_eval_handler (arg)
1271 Lisp_Object arg;
1273 add_to_log ("Error during redisplay: %s", arg, Qnil);
1274 return Qnil;
1278 /* Evaluate SEXPR and return the result, or nil if something went
1279 wrong. */
1281 Lisp_Object
1282 safe_eval (sexpr)
1283 Lisp_Object sexpr;
1285 Lisp_Object val;
1287 if (inhibit_eval_during_redisplay)
1288 val = Qnil;
1289 else
1291 int count = BINDING_STACK_SIZE ();
1292 struct gcpro gcpro1;
1294 GCPRO1 (sexpr);
1295 specbind (Qinhibit_redisplay, Qt);
1296 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1297 safe_eval_handler);
1298 UNGCPRO;
1299 val = unbind_to (count, val);
1302 return val;
1306 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1307 Return the result, or nil if something went wrong. */
1309 Lisp_Object
1310 safe_call (nargs, args)
1311 int nargs;
1312 Lisp_Object *args;
1314 Lisp_Object val;
1316 if (inhibit_eval_during_redisplay)
1317 val = Qnil;
1318 else
1320 int count = BINDING_STACK_SIZE ();
1321 struct gcpro gcpro1;
1323 GCPRO1 (args[0]);
1324 gcpro1.nvars = nargs;
1325 specbind (Qinhibit_redisplay, Qt);
1326 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1327 safe_eval_handler);
1328 UNGCPRO;
1329 val = unbind_to (count, val);
1332 return val;
1336 /* Call function FN with one argument ARG.
1337 Return the result, or nil if something went wrong. */
1339 Lisp_Object
1340 safe_call1 (fn, arg)
1341 Lisp_Object fn, arg;
1343 Lisp_Object args[2];
1344 args[0] = fn;
1345 args[1] = arg;
1346 return safe_call (2, args);
1351 /***********************************************************************
1352 Debugging
1353 ***********************************************************************/
1355 #if 0
1357 /* Define CHECK_IT to perform sanity checks on iterators.
1358 This is for debugging. It is too slow to do unconditionally. */
1360 static void
1361 check_it (it)
1362 struct it *it;
1364 if (it->method == next_element_from_string)
1366 xassert (STRINGP (it->string));
1367 xassert (IT_STRING_CHARPOS (*it) >= 0);
1369 else if (it->method == next_element_from_buffer)
1371 /* Check that character and byte positions agree. */
1372 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1375 if (it->dpvec)
1376 xassert (it->current.dpvec_index >= 0);
1377 else
1378 xassert (it->current.dpvec_index < 0);
1381 #define CHECK_IT(IT) check_it ((IT))
1383 #else /* not 0 */
1385 #define CHECK_IT(IT) (void) 0
1387 #endif /* not 0 */
1390 #if GLYPH_DEBUG
1392 /* Check that the window end of window W is what we expect it
1393 to be---the last row in the current matrix displaying text. */
1395 static void
1396 check_window_end (w)
1397 struct window *w;
1399 if (!MINI_WINDOW_P (w)
1400 && !NILP (w->window_end_valid))
1402 struct glyph_row *row;
1403 xassert ((row = MATRIX_ROW (w->current_matrix,
1404 XFASTINT (w->window_end_vpos)),
1405 !row->enabled_p
1406 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1407 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1411 #define CHECK_WINDOW_END(W) check_window_end ((W))
1413 #else /* not GLYPH_DEBUG */
1415 #define CHECK_WINDOW_END(W) (void) 0
1417 #endif /* not GLYPH_DEBUG */
1421 /***********************************************************************
1422 Iterator initialization
1423 ***********************************************************************/
1425 /* Initialize IT for displaying current_buffer in window W, starting
1426 at character position CHARPOS. CHARPOS < 0 means that no buffer
1427 position is specified which is useful when the iterator is assigned
1428 a position later. BYTEPOS is the byte position corresponding to
1429 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1431 If ROW is not null, calls to produce_glyphs with IT as parameter
1432 will produce glyphs in that row.
1434 BASE_FACE_ID is the id of a base face to use. It must be one of
1435 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1436 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1437 displaying the tool-bar.
1439 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1440 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1441 corresponding mode line glyph row of the desired matrix of W. */
1443 void
1444 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1445 struct it *it;
1446 struct window *w;
1447 int charpos, bytepos;
1448 struct glyph_row *row;
1449 enum face_id base_face_id;
1451 int highlight_region_p;
1453 /* Some precondition checks. */
1454 xassert (w != NULL && it != NULL);
1455 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1457 /* If face attributes have been changed since the last redisplay,
1458 free realized faces now because they depend on face definitions
1459 that might have changed. */
1460 if (face_change_count)
1462 face_change_count = 0;
1463 free_all_realized_faces (Qnil);
1466 /* Use one of the mode line rows of W's desired matrix if
1467 appropriate. */
1468 if (row == NULL)
1470 if (base_face_id == MODE_LINE_FACE_ID)
1471 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1472 else if (base_face_id == HEADER_LINE_FACE_ID)
1473 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1476 /* Clear IT. */
1477 bzero (it, sizeof *it);
1478 it->current.overlay_string_index = -1;
1479 it->current.dpvec_index = -1;
1480 it->base_face_id = base_face_id;
1482 /* The window in which we iterate over current_buffer: */
1483 XSETWINDOW (it->window, w);
1484 it->w = w;
1485 it->f = XFRAME (w->frame);
1487 /* Extra space between lines (on window systems only). */
1488 if (base_face_id == DEFAULT_FACE_ID
1489 && FRAME_WINDOW_P (it->f))
1491 if (NATNUMP (current_buffer->extra_line_spacing))
1492 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1493 else if (it->f->extra_line_spacing > 0)
1494 it->extra_line_spacing = it->f->extra_line_spacing;
1497 /* If realized faces have been removed, e.g. because of face
1498 attribute changes of named faces, recompute them. When running
1499 in batch mode, the face cache of Vterminal_frame is null. If
1500 we happen to get called, make a dummy face cache. */
1501 if (
1502 #ifndef WINDOWSNT
1503 noninteractive &&
1504 #endif
1505 FRAME_FACE_CACHE (it->f) == NULL)
1506 init_frame_faces (it->f);
1507 if (FRAME_FACE_CACHE (it->f)->used == 0)
1508 recompute_basic_faces (it->f);
1510 /* Current value of the `space-width', and 'height' properties. */
1511 it->space_width = Qnil;
1512 it->font_height = Qnil;
1514 /* Are control characters displayed as `^C'? */
1515 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1517 /* -1 means everything between a CR and the following line end
1518 is invisible. >0 means lines indented more than this value are
1519 invisible. */
1520 it->selective = (INTEGERP (current_buffer->selective_display)
1521 ? XFASTINT (current_buffer->selective_display)
1522 : (!NILP (current_buffer->selective_display)
1523 ? -1 : 0));
1524 it->selective_display_ellipsis_p
1525 = !NILP (current_buffer->selective_display_ellipses);
1527 /* Display table to use. */
1528 it->dp = window_display_table (w);
1530 /* Are multibyte characters enabled in current_buffer? */
1531 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1533 /* Non-zero if we should highlight the region. */
1534 highlight_region_p
1535 = (!NILP (Vtransient_mark_mode)
1536 && !NILP (current_buffer->mark_active)
1537 && XMARKER (current_buffer->mark)->buffer != 0);
1539 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1540 start and end of a visible region in window IT->w. Set both to
1541 -1 to indicate no region. */
1542 if (highlight_region_p
1543 /* Maybe highlight only in selected window. */
1544 && (/* Either show region everywhere. */
1545 highlight_nonselected_windows
1546 /* Or show region in the selected window. */
1547 || w == XWINDOW (selected_window)
1548 /* Or show the region if we are in the mini-buffer and W is
1549 the window the mini-buffer refers to. */
1550 || (MINI_WINDOW_P (XWINDOW (selected_window))
1551 && WINDOWP (Vminibuf_scroll_window)
1552 && w == XWINDOW (Vminibuf_scroll_window))))
1554 int charpos = marker_position (current_buffer->mark);
1555 it->region_beg_charpos = min (PT, charpos);
1556 it->region_end_charpos = max (PT, charpos);
1558 else
1559 it->region_beg_charpos = it->region_end_charpos = -1;
1561 /* Get the position at which the redisplay_end_trigger hook should
1562 be run, if it is to be run at all. */
1563 if (MARKERP (w->redisplay_end_trigger)
1564 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1565 it->redisplay_end_trigger_charpos
1566 = marker_position (w->redisplay_end_trigger);
1567 else if (INTEGERP (w->redisplay_end_trigger))
1568 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1570 /* Correct bogus values of tab_width. */
1571 it->tab_width = XINT (current_buffer->tab_width);
1572 if (it->tab_width <= 0 || it->tab_width > 1000)
1573 it->tab_width = 8;
1575 /* Are lines in the display truncated? */
1576 it->truncate_lines_p
1577 = (base_face_id != DEFAULT_FACE_ID
1578 || XINT (it->w->hscroll)
1579 || (truncate_partial_width_windows
1580 && !WINDOW_FULL_WIDTH_P (it->w))
1581 || !NILP (current_buffer->truncate_lines));
1583 /* Get dimensions of truncation and continuation glyphs. These are
1584 displayed as bitmaps under X, so we don't need them for such
1585 frames. */
1586 if (!FRAME_WINDOW_P (it->f))
1588 if (it->truncate_lines_p)
1590 /* We will need the truncation glyph. */
1591 xassert (it->glyph_row == NULL);
1592 produce_special_glyphs (it, IT_TRUNCATION);
1593 it->truncation_pixel_width = it->pixel_width;
1595 else
1597 /* We will need the continuation glyph. */
1598 xassert (it->glyph_row == NULL);
1599 produce_special_glyphs (it, IT_CONTINUATION);
1600 it->continuation_pixel_width = it->pixel_width;
1603 /* Reset these values to zero becaue the produce_special_glyphs
1604 above has changed them. */
1605 it->pixel_width = it->ascent = it->descent = 0;
1606 it->phys_ascent = it->phys_descent = 0;
1609 /* Set this after getting the dimensions of truncation and
1610 continuation glyphs, so that we don't produce glyphs when calling
1611 produce_special_glyphs, above. */
1612 it->glyph_row = row;
1613 it->area = TEXT_AREA;
1615 /* Get the dimensions of the display area. The display area
1616 consists of the visible window area plus a horizontally scrolled
1617 part to the left of the window. All x-values are relative to the
1618 start of this total display area. */
1619 if (base_face_id != DEFAULT_FACE_ID)
1621 /* Mode lines, menu bar in terminal frames. */
1622 it->first_visible_x = 0;
1623 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1625 else
1627 it->first_visible_x
1628 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1629 it->last_visible_x = (it->first_visible_x
1630 + window_box_width (w, TEXT_AREA));
1632 /* If we truncate lines, leave room for the truncator glyph(s) at
1633 the right margin. Otherwise, leave room for the continuation
1634 glyph(s). Truncation and continuation glyphs are not inserted
1635 for window-based redisplay. */
1636 if (!FRAME_WINDOW_P (it->f))
1638 if (it->truncate_lines_p)
1639 it->last_visible_x -= it->truncation_pixel_width;
1640 else
1641 it->last_visible_x -= it->continuation_pixel_width;
1644 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1645 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1648 /* Leave room for a border glyph. */
1649 if (!FRAME_WINDOW_P (it->f)
1650 && !WINDOW_RIGHTMOST_P (it->w))
1651 it->last_visible_x -= 1;
1653 it->last_visible_y = window_text_bottom_y (w);
1655 /* For mode lines and alike, arrange for the first glyph having a
1656 left box line if the face specifies a box. */
1657 if (base_face_id != DEFAULT_FACE_ID)
1659 struct face *face;
1661 it->face_id = base_face_id;
1663 /* If we have a boxed mode line, make the first character appear
1664 with a left box line. */
1665 face = FACE_FROM_ID (it->f, base_face_id);
1666 if (face->box != FACE_NO_BOX)
1667 it->start_of_box_run_p = 1;
1670 /* If a buffer position was specified, set the iterator there,
1671 getting overlays and face properties from that position. */
1672 if (charpos > 0)
1674 it->end_charpos = ZV;
1675 it->face_id = -1;
1676 IT_CHARPOS (*it) = charpos;
1678 /* Compute byte position if not specified. */
1679 if (bytepos <= 0)
1680 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1681 else
1682 IT_BYTEPOS (*it) = bytepos;
1684 /* Compute faces etc. */
1685 reseat (it, it->current.pos, 1);
1688 CHECK_IT (it);
1692 /* Initialize IT for the display of window W with window start POS. */
1694 void
1695 start_display (it, w, pos)
1696 struct it *it;
1697 struct window *w;
1698 struct text_pos pos;
1700 int start_at_line_beg_p;
1701 struct glyph_row *row;
1702 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1703 int first_y;
1705 row = w->desired_matrix->rows + first_vpos;
1706 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1707 first_y = it->current_y;
1709 /* If window start is not at a line start, move back to the line
1710 start. This makes sure that we take continuation lines into
1711 account. */
1712 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1713 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1714 if (!start_at_line_beg_p)
1715 reseat_at_previous_visible_line_start (it);
1717 /* If window start is not at a line start, skip forward to POS to
1718 get the correct continuation_lines_width and current_x. */
1719 if (!start_at_line_beg_p)
1721 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1723 /* If lines are continued, this line may end in the middle of a
1724 multi-glyph character (e.g. a control character displayed as
1725 \003, or in the middle of an overlay string). In this case
1726 move_it_to above will not have taken us to the start of
1727 the continuation line but to the end of the continued line. */
1728 if (!it->truncate_lines_p)
1730 if (it->current_x > 0)
1732 if (it->current.dpvec_index >= 0
1733 || it->current.overlay_string_index >= 0)
1735 set_iterator_to_next (it, 1);
1736 move_it_in_display_line_to (it, -1, -1, 0);
1739 it->continuation_lines_width += it->current_x;
1742 /* We're starting a new display line, not affected by the
1743 height of the continued line, so clear the appropriate
1744 fields in the iterator structure. */
1745 it->max_ascent = it->max_descent = 0;
1746 it->max_phys_ascent = it->max_phys_descent = 0;
1749 it->current_y = first_y;
1750 it->vpos = 0;
1751 it->current_x = it->hpos = 0;
1754 #if 0 /* Don't assert the following because start_display is sometimes
1755 called intentionally with a window start that is not at a
1756 line start. Please leave this code in as a comment. */
1758 /* Window start should be on a line start, now. */
1759 xassert (it->continuation_lines_width
1760 || IT_CHARPOS (it) == BEGV
1761 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1762 #endif /* 0 */
1766 /* Return 1 if POS is a position in ellipses displayed for invisible
1767 text. W is the window we display, for text property lookup. */
1769 static int
1770 in_ellipses_for_invisible_text_p (pos, w)
1771 struct display_pos *pos;
1772 struct window *w;
1774 Lisp_Object prop, window;
1775 int ellipses_p = 0;
1776 int charpos = CHARPOS (pos->pos);
1778 /* If POS specifies a position in a display vector, this might
1779 be for an ellipsis displayed for invisible text. We won't
1780 get the iterator set up for delivering that ellipsis unless
1781 we make sure that it gets aware of the invisible text. */
1782 if (pos->dpvec_index >= 0
1783 && pos->overlay_string_index < 0
1784 && CHARPOS (pos->string_pos) < 0
1785 && charpos > BEGV
1786 && (XSETWINDOW (window, w),
1787 prop = Fget_char_property (make_number (charpos),
1788 Qinvisible, window),
1789 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1791 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1792 window);
1793 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1794 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1795 ellipses_p = 1;
1798 return ellipses_p;
1802 /* Initialize IT for stepping through current_buffer in window W,
1803 starting at position POS that includes overlay string and display
1804 vector/ control character translation position information. */
1806 static void
1807 init_from_display_pos (it, w, pos)
1808 struct it *it;
1809 struct window *w;
1810 struct display_pos *pos;
1812 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1814 /* If POS specifies a position in a display vector, this might
1815 be for an ellipsis displayed for invisible text. We won't
1816 get the iterator set up for delivering that ellipsis unless
1817 we make sure that it gets aware of the invisible text. */
1818 if (in_ellipses_for_invisible_text_p (pos, w))
1820 --charpos;
1821 bytepos = 0;
1824 /* Keep in mind: the call to reseat in init_iterator skips invisible
1825 text, so we might end up at a position different from POS. This
1826 is only a problem when POS is a row start after a newline and an
1827 overlay starts there with an after-string, and the overlay has an
1828 invisible property. Since we don't skip invisible text in
1829 display_line and elsewhere immediately after consuming the
1830 newline before the row start, such a POS will not be in a string,
1831 but the call to init_iterator below will move us to the
1832 after-string. */
1833 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1835 /* If position is within an overlay string, set up IT to
1836 the right overlay string. */
1837 if (pos->overlay_string_index >= 0)
1839 int relative_index;
1841 /* We already have the first chunk of overlay strings in
1842 IT->overlay_strings. Load more until the one for
1843 pos->overlay_string_index is in IT->overlay_strings. */
1844 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1846 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1847 it->current.overlay_string_index = 0;
1848 while (n--)
1850 load_overlay_strings (it);
1851 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1855 it->current.overlay_string_index = pos->overlay_string_index;
1856 relative_index = (it->current.overlay_string_index
1857 % OVERLAY_STRING_CHUNK_SIZE);
1858 it->string = it->overlay_strings[relative_index];
1859 xassert (STRINGP (it->string));
1860 it->current.string_pos = pos->string_pos;
1861 it->method = next_element_from_string;
1863 else if (it->current.overlay_string_index >= 0)
1865 /* If POS says we're already after an overlay string ending at
1866 POS, make sure to pop the iterator because it will be in
1867 front of that overlay string. When POS is ZV, we've thereby
1868 also ``processed'' overlay strings at ZV. */
1869 while (it->sp)
1870 pop_it (it);
1871 it->current.overlay_string_index = -1;
1872 it->method = next_element_from_buffer;
1873 if (CHARPOS (pos->pos) == ZV)
1874 it->overlay_strings_at_end_processed_p = 1;
1877 if (CHARPOS (pos->string_pos) >= 0)
1879 /* Recorded position is not in an overlay string, but in another
1880 string. This can only be a string from a `display' property.
1881 IT should already be filled with that string. */
1882 it->current.string_pos = pos->string_pos;
1883 xassert (STRINGP (it->string));
1886 /* Restore position in display vector translations, control
1887 character translations or ellipses. */
1888 if (pos->dpvec_index >= 0)
1890 if (it->dpvec == NULL)
1891 get_next_display_element (it);
1892 xassert (it->dpvec && it->current.dpvec_index == 0);
1893 it->current.dpvec_index = pos->dpvec_index;
1896 CHECK_IT (it);
1900 /* Initialize IT for stepping through current_buffer in window W
1901 starting at ROW->start. */
1903 static void
1904 init_to_row_start (it, w, row)
1905 struct it *it;
1906 struct window *w;
1907 struct glyph_row *row;
1909 init_from_display_pos (it, w, &row->start);
1910 it->continuation_lines_width = row->continuation_lines_width;
1911 CHECK_IT (it);
1915 /* Initialize IT for stepping through current_buffer in window W
1916 starting in the line following ROW, i.e. starting at ROW->end. */
1918 static void
1919 init_to_row_end (it, w, row)
1920 struct it *it;
1921 struct window *w;
1922 struct glyph_row *row;
1924 init_from_display_pos (it, w, &row->end);
1926 if (row->continued_p)
1927 it->continuation_lines_width = (row->continuation_lines_width
1928 + row->pixel_width);
1929 CHECK_IT (it);
1935 /***********************************************************************
1936 Text properties
1937 ***********************************************************************/
1939 /* Called when IT reaches IT->stop_charpos. Handle text property and
1940 overlay changes. Set IT->stop_charpos to the next position where
1941 to stop. */
1943 static void
1944 handle_stop (it)
1945 struct it *it;
1947 enum prop_handled handled;
1948 int handle_overlay_change_p = 1;
1949 struct props *p;
1951 it->dpvec = NULL;
1952 it->current.dpvec_index = -1;
1956 handled = HANDLED_NORMALLY;
1958 /* Call text property handlers. */
1959 for (p = it_props; p->handler; ++p)
1961 handled = p->handler (it);
1963 if (handled == HANDLED_RECOMPUTE_PROPS)
1964 break;
1965 else if (handled == HANDLED_RETURN)
1966 return;
1967 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1968 handle_overlay_change_p = 0;
1971 if (handled != HANDLED_RECOMPUTE_PROPS)
1973 /* Don't check for overlay strings below when set to deliver
1974 characters from a display vector. */
1975 if (it->method == next_element_from_display_vector)
1976 handle_overlay_change_p = 0;
1978 /* Handle overlay changes. */
1979 if (handle_overlay_change_p)
1980 handled = handle_overlay_change (it);
1982 /* Determine where to stop next. */
1983 if (handled == HANDLED_NORMALLY)
1984 compute_stop_pos (it);
1987 while (handled == HANDLED_RECOMPUTE_PROPS);
1991 /* Compute IT->stop_charpos from text property and overlay change
1992 information for IT's current position. */
1994 static void
1995 compute_stop_pos (it)
1996 struct it *it;
1998 register INTERVAL iv, next_iv;
1999 Lisp_Object object, limit, position;
2001 /* If nowhere else, stop at the end. */
2002 it->stop_charpos = it->end_charpos;
2004 if (STRINGP (it->string))
2006 /* Strings are usually short, so don't limit the search for
2007 properties. */
2008 object = it->string;
2009 limit = Qnil;
2010 position = make_number (IT_STRING_CHARPOS (*it));
2012 else
2014 int charpos;
2016 /* If next overlay change is in front of the current stop pos
2017 (which is IT->end_charpos), stop there. Note: value of
2018 next_overlay_change is point-max if no overlay change
2019 follows. */
2020 charpos = next_overlay_change (IT_CHARPOS (*it));
2021 if (charpos < it->stop_charpos)
2022 it->stop_charpos = charpos;
2024 /* If showing the region, we have to stop at the region
2025 start or end because the face might change there. */
2026 if (it->region_beg_charpos > 0)
2028 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2029 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2030 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2031 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2034 /* Set up variables for computing the stop position from text
2035 property changes. */
2036 XSETBUFFER (object, current_buffer);
2037 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2038 position = make_number (IT_CHARPOS (*it));
2042 /* Get the interval containing IT's position. Value is a null
2043 interval if there isn't such an interval. */
2044 iv = validate_interval_range (object, &position, &position, 0);
2045 if (!NULL_INTERVAL_P (iv))
2047 Lisp_Object values_here[LAST_PROP_IDX];
2048 struct props *p;
2050 /* Get properties here. */
2051 for (p = it_props; p->handler; ++p)
2052 values_here[p->idx] = textget (iv->plist, *p->name);
2054 /* Look for an interval following iv that has different
2055 properties. */
2056 for (next_iv = next_interval (iv);
2057 (!NULL_INTERVAL_P (next_iv)
2058 && (NILP (limit)
2059 || XFASTINT (limit) > next_iv->position));
2060 next_iv = next_interval (next_iv))
2062 for (p = it_props; p->handler; ++p)
2064 Lisp_Object new_value;
2066 new_value = textget (next_iv->plist, *p->name);
2067 if (!EQ (values_here[p->idx], new_value))
2068 break;
2071 if (p->handler)
2072 break;
2075 if (!NULL_INTERVAL_P (next_iv))
2077 if (INTEGERP (limit)
2078 && next_iv->position >= XFASTINT (limit))
2079 /* No text property change up to limit. */
2080 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2081 else
2082 /* Text properties change in next_iv. */
2083 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2087 xassert (STRINGP (it->string)
2088 || (it->stop_charpos >= BEGV
2089 && it->stop_charpos >= IT_CHARPOS (*it)));
2093 /* Return the position of the next overlay change after POS in
2094 current_buffer. Value is point-max if no overlay change
2095 follows. This is like `next-overlay-change' but doesn't use
2096 xmalloc. */
2098 static int
2099 next_overlay_change (pos)
2100 int pos;
2102 int noverlays;
2103 int endpos;
2104 Lisp_Object *overlays;
2105 int len;
2106 int i;
2108 /* Get all overlays at the given position. */
2109 len = 10;
2110 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2111 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2112 if (noverlays > len)
2114 len = noverlays;
2115 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2116 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2119 /* If any of these overlays ends before endpos,
2120 use its ending point instead. */
2121 for (i = 0; i < noverlays; ++i)
2123 Lisp_Object oend;
2124 int oendpos;
2126 oend = OVERLAY_END (overlays[i]);
2127 oendpos = OVERLAY_POSITION (oend);
2128 endpos = min (endpos, oendpos);
2131 return endpos;
2136 /***********************************************************************
2137 Fontification
2138 ***********************************************************************/
2140 /* Handle changes in the `fontified' property of the current buffer by
2141 calling hook functions from Qfontification_functions to fontify
2142 regions of text. */
2144 static enum prop_handled
2145 handle_fontified_prop (it)
2146 struct it *it;
2148 Lisp_Object prop, pos;
2149 enum prop_handled handled = HANDLED_NORMALLY;
2151 /* Get the value of the `fontified' property at IT's current buffer
2152 position. (The `fontified' property doesn't have a special
2153 meaning in strings.) If the value is nil, call functions from
2154 Qfontification_functions. */
2155 if (!STRINGP (it->string)
2156 && it->s == NULL
2157 && !NILP (Vfontification_functions)
2158 && !NILP (Vrun_hooks)
2159 && (pos = make_number (IT_CHARPOS (*it)),
2160 prop = Fget_char_property (pos, Qfontified, Qnil),
2161 NILP (prop)))
2163 int count = BINDING_STACK_SIZE ();
2164 Lisp_Object val;
2166 val = Vfontification_functions;
2167 specbind (Qfontification_functions, Qnil);
2168 specbind (Qafter_change_functions, Qnil);
2170 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2171 safe_call1 (val, pos);
2172 else
2174 Lisp_Object globals, fn;
2175 struct gcpro gcpro1, gcpro2;
2177 globals = Qnil;
2178 GCPRO2 (val, globals);
2180 for (; CONSP (val); val = XCDR (val))
2182 fn = XCAR (val);
2184 if (EQ (fn, Qt))
2186 /* A value of t indicates this hook has a local
2187 binding; it means to run the global binding too.
2188 In a global value, t should not occur. If it
2189 does, we must ignore it to avoid an endless
2190 loop. */
2191 for (globals = Fdefault_value (Qfontification_functions);
2192 CONSP (globals);
2193 globals = XCDR (globals))
2195 fn = XCAR (globals);
2196 if (!EQ (fn, Qt))
2197 safe_call1 (fn, pos);
2200 else
2201 safe_call1 (fn, pos);
2204 UNGCPRO;
2207 unbind_to (count, Qnil);
2209 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2210 something. This avoids an endless loop if they failed to
2211 fontify the text for which reason ever. */
2212 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2213 handled = HANDLED_RECOMPUTE_PROPS;
2216 return handled;
2221 /***********************************************************************
2222 Faces
2223 ***********************************************************************/
2225 /* Set up iterator IT from face properties at its current position.
2226 Called from handle_stop. */
2228 static enum prop_handled
2229 handle_face_prop (it)
2230 struct it *it;
2232 int new_face_id, next_stop;
2234 if (!STRINGP (it->string))
2236 new_face_id
2237 = face_at_buffer_position (it->w,
2238 IT_CHARPOS (*it),
2239 it->region_beg_charpos,
2240 it->region_end_charpos,
2241 &next_stop,
2242 (IT_CHARPOS (*it)
2243 + TEXT_PROP_DISTANCE_LIMIT),
2246 /* Is this a start of a run of characters with box face?
2247 Caveat: this can be called for a freshly initialized
2248 iterator; face_id is -1 is this case. We know that the new
2249 face will not change until limit, i.e. if the new face has a
2250 box, all characters up to limit will have one. But, as
2251 usual, we don't know whether limit is really the end. */
2252 if (new_face_id != it->face_id)
2254 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2256 /* If new face has a box but old face has not, this is
2257 the start of a run of characters with box, i.e. it has
2258 a shadow on the left side. The value of face_id of the
2259 iterator will be -1 if this is the initial call that gets
2260 the face. In this case, we have to look in front of IT's
2261 position and see whether there is a face != new_face_id. */
2262 it->start_of_box_run_p
2263 = (new_face->box != FACE_NO_BOX
2264 && (it->face_id >= 0
2265 || IT_CHARPOS (*it) == BEG
2266 || new_face_id != face_before_it_pos (it)));
2267 it->face_box_p = new_face->box != FACE_NO_BOX;
2270 else
2272 int base_face_id, bufpos;
2274 if (it->current.overlay_string_index >= 0)
2275 bufpos = IT_CHARPOS (*it);
2276 else
2277 bufpos = 0;
2279 /* For strings from a buffer, i.e. overlay strings or strings
2280 from a `display' property, use the face at IT's current
2281 buffer position as the base face to merge with, so that
2282 overlay strings appear in the same face as surrounding
2283 text, unless they specify their own faces. */
2284 base_face_id = underlying_face_id (it);
2286 new_face_id = face_at_string_position (it->w,
2287 it->string,
2288 IT_STRING_CHARPOS (*it),
2289 bufpos,
2290 it->region_beg_charpos,
2291 it->region_end_charpos,
2292 &next_stop,
2293 base_face_id, 0);
2295 #if 0 /* This shouldn't be neccessary. Let's check it. */
2296 /* If IT is used to display a mode line we would really like to
2297 use the mode line face instead of the frame's default face. */
2298 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2299 && new_face_id == DEFAULT_FACE_ID)
2300 new_face_id = MODE_LINE_FACE_ID;
2301 #endif
2303 /* Is this a start of a run of characters with box? Caveat:
2304 this can be called for a freshly allocated iterator; face_id
2305 is -1 is this case. We know that the new face will not
2306 change until the next check pos, i.e. if the new face has a
2307 box, all characters up to that position will have a
2308 box. But, as usual, we don't know whether that position
2309 is really the end. */
2310 if (new_face_id != it->face_id)
2312 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2313 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2315 /* If new face has a box but old face hasn't, this is the
2316 start of a run of characters with box, i.e. it has a
2317 shadow on the left side. */
2318 it->start_of_box_run_p
2319 = new_face->box && (old_face == NULL || !old_face->box);
2320 it->face_box_p = new_face->box != FACE_NO_BOX;
2324 it->face_id = new_face_id;
2325 return HANDLED_NORMALLY;
2329 /* Return the ID of the face ``underlying'' IT's current position,
2330 which is in a string. If the iterator is associated with a
2331 buffer, return the face at IT's current buffer position.
2332 Otherwise, use the iterator's base_face_id. */
2334 static int
2335 underlying_face_id (it)
2336 struct it *it;
2338 int face_id = it->base_face_id, i;
2340 xassert (STRINGP (it->string));
2342 for (i = it->sp - 1; i >= 0; --i)
2343 if (NILP (it->stack[i].string))
2344 face_id = it->stack[i].face_id;
2346 return face_id;
2350 /* Compute the face one character before or after the current position
2351 of IT. BEFORE_P non-zero means get the face in front of IT's
2352 position. Value is the id of the face. */
2354 static int
2355 face_before_or_after_it_pos (it, before_p)
2356 struct it *it;
2357 int before_p;
2359 int face_id, limit;
2360 int next_check_charpos;
2361 struct text_pos pos;
2363 xassert (it->s == NULL);
2365 if (STRINGP (it->string))
2367 int bufpos, base_face_id;
2369 /* No face change past the end of the string (for the case
2370 we are padding with spaces). No face change before the
2371 string start. */
2372 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2373 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2374 return it->face_id;
2376 /* Set pos to the position before or after IT's current position. */
2377 if (before_p)
2378 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2379 else
2380 /* For composition, we must check the character after the
2381 composition. */
2382 pos = (it->what == IT_COMPOSITION
2383 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2384 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2386 if (it->current.overlay_string_index >= 0)
2387 bufpos = IT_CHARPOS (*it);
2388 else
2389 bufpos = 0;
2391 base_face_id = underlying_face_id (it);
2393 /* Get the face for ASCII, or unibyte. */
2394 face_id = face_at_string_position (it->w,
2395 it->string,
2396 CHARPOS (pos),
2397 bufpos,
2398 it->region_beg_charpos,
2399 it->region_end_charpos,
2400 &next_check_charpos,
2401 base_face_id, 0);
2403 /* Correct the face for charsets different from ASCII. Do it
2404 for the multibyte case only. The face returned above is
2405 suitable for unibyte text if IT->string is unibyte. */
2406 if (STRING_MULTIBYTE (it->string))
2408 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2409 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2410 int c, len;
2411 struct face *face = FACE_FROM_ID (it->f, face_id);
2413 c = string_char_and_length (p, rest, &len);
2414 face_id = FACE_FOR_CHAR (it->f, face, c);
2417 else
2419 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2420 || (IT_CHARPOS (*it) <= BEGV && before_p))
2421 return it->face_id;
2423 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2424 pos = it->current.pos;
2426 if (before_p)
2427 DEC_TEXT_POS (pos, it->multibyte_p);
2428 else
2430 if (it->what == IT_COMPOSITION)
2431 /* For composition, we must check the position after the
2432 composition. */
2433 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2434 else
2435 INC_TEXT_POS (pos, it->multibyte_p);
2438 /* Determine face for CHARSET_ASCII, or unibyte. */
2439 face_id = face_at_buffer_position (it->w,
2440 CHARPOS (pos),
2441 it->region_beg_charpos,
2442 it->region_end_charpos,
2443 &next_check_charpos,
2444 limit, 0);
2446 /* Correct the face for charsets different from ASCII. Do it
2447 for the multibyte case only. The face returned above is
2448 suitable for unibyte text if current_buffer is unibyte. */
2449 if (it->multibyte_p)
2451 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2452 struct face *face = FACE_FROM_ID (it->f, face_id);
2453 face_id = FACE_FOR_CHAR (it->f, face, c);
2457 return face_id;
2462 /***********************************************************************
2463 Invisible text
2464 ***********************************************************************/
2466 /* Set up iterator IT from invisible properties at its current
2467 position. Called from handle_stop. */
2469 static enum prop_handled
2470 handle_invisible_prop (it)
2471 struct it *it;
2473 enum prop_handled handled = HANDLED_NORMALLY;
2475 if (STRINGP (it->string))
2477 extern Lisp_Object Qinvisible;
2478 Lisp_Object prop, end_charpos, limit, charpos;
2480 /* Get the value of the invisible text property at the
2481 current position. Value will be nil if there is no such
2482 property. */
2483 charpos = make_number (IT_STRING_CHARPOS (*it));
2484 prop = Fget_text_property (charpos, Qinvisible, it->string);
2486 if (!NILP (prop)
2487 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2489 handled = HANDLED_RECOMPUTE_PROPS;
2491 /* Get the position at which the next change of the
2492 invisible text property can be found in IT->string.
2493 Value will be nil if the property value is the same for
2494 all the rest of IT->string. */
2495 XSETINT (limit, XSTRING (it->string)->size);
2496 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2497 it->string, limit);
2499 /* Text at current position is invisible. The next
2500 change in the property is at position end_charpos.
2501 Move IT's current position to that position. */
2502 if (INTEGERP (end_charpos)
2503 && XFASTINT (end_charpos) < XFASTINT (limit))
2505 struct text_pos old;
2506 old = it->current.string_pos;
2507 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2508 compute_string_pos (&it->current.string_pos, old, it->string);
2510 else
2512 /* The rest of the string is invisible. If this is an
2513 overlay string, proceed with the next overlay string
2514 or whatever comes and return a character from there. */
2515 if (it->current.overlay_string_index >= 0)
2517 next_overlay_string (it);
2518 /* Don't check for overlay strings when we just
2519 finished processing them. */
2520 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2522 else
2524 struct Lisp_String *s = XSTRING (it->string);
2525 IT_STRING_CHARPOS (*it) = s->size;
2526 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2531 else
2533 int visible_p, newpos, next_stop;
2534 Lisp_Object pos, prop;
2536 /* First of all, is there invisible text at this position? */
2537 pos = make_number (IT_CHARPOS (*it));
2538 prop = Fget_char_property (pos, Qinvisible, it->window);
2540 /* If we are on invisible text, skip over it. */
2541 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2542 && IT_CHARPOS (*it) < it->end_charpos)
2544 /* Record whether we have to display an ellipsis for the
2545 invisible text. */
2546 int display_ellipsis_p
2547 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2549 handled = HANDLED_RECOMPUTE_PROPS;
2551 /* Loop skipping over invisible text. The loop is left at
2552 ZV or with IT on the first char being visible again. */
2555 /* Try to skip some invisible text. Return value is the
2556 position reached which can be equal to IT's position
2557 if there is nothing invisible here. This skips both
2558 over invisible text properties and overlays with
2559 invisible property. */
2560 newpos = skip_invisible (IT_CHARPOS (*it),
2561 &next_stop, ZV, it->window);
2563 /* If we skipped nothing at all we weren't at invisible
2564 text in the first place. If everything to the end of
2565 the buffer was skipped, end the loop. */
2566 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2567 visible_p = 1;
2568 else
2570 /* We skipped some characters but not necessarily
2571 all there are. Check if we ended up on visible
2572 text. Fget_char_property returns the property of
2573 the char before the given position, i.e. if we
2574 get visible_p = 1, this means that the char at
2575 newpos is visible. */
2576 pos = make_number (newpos);
2577 prop = Fget_char_property (pos, Qinvisible, it->window);
2578 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2581 /* If we ended up on invisible text, proceed to
2582 skip starting with next_stop. */
2583 if (!visible_p)
2584 IT_CHARPOS (*it) = next_stop;
2586 while (!visible_p);
2588 /* The position newpos is now either ZV or on visible text. */
2589 IT_CHARPOS (*it) = newpos;
2590 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2592 /* Maybe return `...' next for the end of the invisible text. */
2593 if (display_ellipsis_p)
2595 if (it->dp
2596 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2598 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2599 it->dpvec = v->contents;
2600 it->dpend = v->contents + v->size;
2602 else
2604 /* Default `...'. */
2605 it->dpvec = default_invis_vector;
2606 it->dpend = default_invis_vector + 3;
2609 /* The ellipsis display does not replace the display of
2610 the character at the new position. Indicate this by
2611 setting IT->dpvec_char_len to zero. */
2612 it->dpvec_char_len = 0;
2614 it->current.dpvec_index = 0;
2615 it->method = next_element_from_display_vector;
2620 return handled;
2625 /***********************************************************************
2626 'display' property
2627 ***********************************************************************/
2629 /* Set up iterator IT from `display' property at its current position.
2630 Called from handle_stop. */
2632 static enum prop_handled
2633 handle_display_prop (it)
2634 struct it *it;
2636 Lisp_Object prop, object;
2637 struct text_pos *position;
2638 int display_replaced_p = 0;
2640 if (STRINGP (it->string))
2642 object = it->string;
2643 position = &it->current.string_pos;
2645 else
2647 object = it->w->buffer;
2648 position = &it->current.pos;
2651 /* Reset those iterator values set from display property values. */
2652 it->font_height = Qnil;
2653 it->space_width = Qnil;
2654 it->voffset = 0;
2656 /* We don't support recursive `display' properties, i.e. string
2657 values that have a string `display' property, that have a string
2658 `display' property etc. */
2659 if (!it->string_from_display_prop_p)
2660 it->area = TEXT_AREA;
2662 prop = Fget_char_property (make_number (position->charpos),
2663 Qdisplay, object);
2664 if (NILP (prop))
2665 return HANDLED_NORMALLY;
2667 if (CONSP (prop)
2668 && CONSP (XCAR (prop))
2669 && !EQ (Qmargin, XCAR (XCAR (prop))))
2671 /* A list of sub-properties. */
2672 for (; CONSP (prop); prop = XCDR (prop))
2674 if (handle_single_display_prop (it, XCAR (prop), object,
2675 position, display_replaced_p))
2676 display_replaced_p = 1;
2679 else if (VECTORP (prop))
2681 int i;
2682 for (i = 0; i < ASIZE (prop); ++i)
2683 if (handle_single_display_prop (it, AREF (prop, i), object,
2684 position, display_replaced_p))
2685 display_replaced_p = 1;
2687 else
2689 if (handle_single_display_prop (it, prop, object, position, 0))
2690 display_replaced_p = 1;
2693 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2697 /* Value is the position of the end of the `display' property starting
2698 at START_POS in OBJECT. */
2700 static struct text_pos
2701 display_prop_end (it, object, start_pos)
2702 struct it *it;
2703 Lisp_Object object;
2704 struct text_pos start_pos;
2706 Lisp_Object end;
2707 struct text_pos end_pos;
2709 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2710 Qdisplay, object, Qnil);
2711 CHARPOS (end_pos) = XFASTINT (end);
2712 if (STRINGP (object))
2713 compute_string_pos (&end_pos, start_pos, it->string);
2714 else
2715 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2717 return end_pos;
2721 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2722 is the object in which the `display' property was found. *POSITION
2723 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2724 means that we previously saw a display sub-property which already
2725 replaced text display with something else, for example an image;
2726 ignore such properties after the first one has been processed.
2728 If PROP is a `space' or `image' sub-property, set *POSITION to the
2729 end position of the `display' property.
2731 Value is non-zero something was found which replaces the display
2732 of buffer or string text. */
2734 static int
2735 handle_single_display_prop (it, prop, object, position,
2736 display_replaced_before_p)
2737 struct it *it;
2738 Lisp_Object prop;
2739 Lisp_Object object;
2740 struct text_pos *position;
2741 int display_replaced_before_p;
2743 Lisp_Object value;
2744 int replaces_text_display_p = 0;
2745 Lisp_Object form;
2747 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2748 evaluated. If the result is nil, VALUE is ignored. */
2749 form = Qt;
2750 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2752 prop = XCDR (prop);
2753 if (!CONSP (prop))
2754 return 0;
2755 form = XCAR (prop);
2756 prop = XCDR (prop);
2759 if (!NILP (form) && !EQ (form, Qt))
2761 struct gcpro gcpro1;
2762 struct text_pos end_pos, pt;
2764 GCPRO1 (form);
2765 end_pos = display_prop_end (it, object, *position);
2767 /* Temporarily set point to the end position, and then evaluate
2768 the form. This makes `(eolp)' work as FORM. */
2769 if (BUFFERP (object))
2771 CHARPOS (pt) = PT;
2772 BYTEPOS (pt) = PT_BYTE;
2773 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2776 form = safe_eval (form);
2778 if (BUFFERP (object))
2779 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2780 UNGCPRO;
2783 if (NILP (form))
2784 return 0;
2786 if (CONSP (prop)
2787 && EQ (XCAR (prop), Qheight)
2788 && CONSP (XCDR (prop)))
2790 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2791 return 0;
2793 /* `(height HEIGHT)'. */
2794 it->font_height = XCAR (XCDR (prop));
2795 if (!NILP (it->font_height))
2797 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2798 int new_height = -1;
2800 if (CONSP (it->font_height)
2801 && (EQ (XCAR (it->font_height), Qplus)
2802 || EQ (XCAR (it->font_height), Qminus))
2803 && CONSP (XCDR (it->font_height))
2804 && INTEGERP (XCAR (XCDR (it->font_height))))
2806 /* `(+ N)' or `(- N)' where N is an integer. */
2807 int steps = XINT (XCAR (XCDR (it->font_height)));
2808 if (EQ (XCAR (it->font_height), Qplus))
2809 steps = - steps;
2810 it->face_id = smaller_face (it->f, it->face_id, steps);
2812 else if (FUNCTIONP (it->font_height))
2814 /* Call function with current height as argument.
2815 Value is the new height. */
2816 Lisp_Object height;
2817 height = safe_call1 (it->font_height,
2818 face->lface[LFACE_HEIGHT_INDEX]);
2819 if (NUMBERP (height))
2820 new_height = XFLOATINT (height);
2822 else if (NUMBERP (it->font_height))
2824 /* Value is a multiple of the canonical char height. */
2825 struct face *face;
2827 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2828 new_height = (XFLOATINT (it->font_height)
2829 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2831 else
2833 /* Evaluate IT->font_height with `height' bound to the
2834 current specified height to get the new height. */
2835 Lisp_Object value;
2836 int count = BINDING_STACK_SIZE ();
2838 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2839 value = safe_eval (it->font_height);
2840 unbind_to (count, Qnil);
2842 if (NUMBERP (value))
2843 new_height = XFLOATINT (value);
2846 if (new_height > 0)
2847 it->face_id = face_with_height (it->f, it->face_id, new_height);
2850 else if (CONSP (prop)
2851 && EQ (XCAR (prop), Qspace_width)
2852 && CONSP (XCDR (prop)))
2854 /* `(space_width WIDTH)'. */
2855 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2856 return 0;
2858 value = XCAR (XCDR (prop));
2859 if (NUMBERP (value) && XFLOATINT (value) > 0)
2860 it->space_width = value;
2862 else if (CONSP (prop)
2863 && EQ (XCAR (prop), Qraise)
2864 && CONSP (XCDR (prop)))
2866 /* `(raise FACTOR)'. */
2867 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2868 return 0;
2870 #ifdef HAVE_WINDOW_SYSTEM
2871 value = XCAR (XCDR (prop));
2872 if (NUMBERP (value))
2874 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2875 it->voffset = - (XFLOATINT (value)
2876 * (FONT_HEIGHT (face->font)));
2878 #endif /* HAVE_WINDOW_SYSTEM */
2880 else if (!it->string_from_display_prop_p)
2882 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2883 VALUE) or `((margin nil) VALUE)' or VALUE. */
2884 Lisp_Object location, value;
2885 struct text_pos start_pos;
2886 int valid_p;
2888 /* Characters having this form of property are not displayed, so
2889 we have to find the end of the property. */
2890 start_pos = *position;
2891 *position = display_prop_end (it, object, start_pos);
2892 value = Qnil;
2894 /* Let's stop at the new position and assume that all
2895 text properties change there. */
2896 it->stop_charpos = position->charpos;
2898 location = Qunbound;
2899 if (CONSP (prop) && CONSP (XCAR (prop)))
2901 Lisp_Object tem;
2903 value = XCDR (prop);
2904 if (CONSP (value))
2905 value = XCAR (value);
2907 tem = XCAR (prop);
2908 if (EQ (XCAR (tem), Qmargin)
2909 && (tem = XCDR (tem),
2910 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2911 (NILP (tem)
2912 || EQ (tem, Qleft_margin)
2913 || EQ (tem, Qright_margin))))
2914 location = tem;
2917 if (EQ (location, Qunbound))
2919 location = Qnil;
2920 value = prop;
2923 #ifdef HAVE_WINDOW_SYSTEM
2924 if (FRAME_TERMCAP_P (it->f))
2925 valid_p = STRINGP (value);
2926 else
2927 valid_p = (STRINGP (value)
2928 || (CONSP (value) && EQ (XCAR (value), Qspace))
2929 || valid_image_p (value));
2930 #else /* not HAVE_WINDOW_SYSTEM */
2931 valid_p = STRINGP (value);
2932 #endif /* not HAVE_WINDOW_SYSTEM */
2934 if ((EQ (location, Qleft_margin)
2935 || EQ (location, Qright_margin)
2936 || NILP (location))
2937 && valid_p
2938 && !display_replaced_before_p)
2940 replaces_text_display_p = 1;
2942 /* Save current settings of IT so that we can restore them
2943 when we are finished with the glyph property value. */
2944 push_it (it);
2946 if (NILP (location))
2947 it->area = TEXT_AREA;
2948 else if (EQ (location, Qleft_margin))
2949 it->area = LEFT_MARGIN_AREA;
2950 else
2951 it->area = RIGHT_MARGIN_AREA;
2953 if (STRINGP (value))
2955 it->string = value;
2956 it->multibyte_p = STRING_MULTIBYTE (it->string);
2957 it->current.overlay_string_index = -1;
2958 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2959 it->end_charpos = it->string_nchars
2960 = XSTRING (it->string)->size;
2961 it->method = next_element_from_string;
2962 it->stop_charpos = 0;
2963 it->string_from_display_prop_p = 1;
2964 /* Say that we haven't consumed the characters with
2965 `display' property yet. The call to pop_it in
2966 set_iterator_to_next will clean this up. */
2967 *position = start_pos;
2969 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2971 it->method = next_element_from_stretch;
2972 it->object = value;
2973 it->current.pos = it->position = start_pos;
2975 #ifdef HAVE_WINDOW_SYSTEM
2976 else
2978 it->what = IT_IMAGE;
2979 it->image_id = lookup_image (it->f, value);
2980 it->position = start_pos;
2981 it->object = NILP (object) ? it->w->buffer : object;
2982 it->method = next_element_from_image;
2984 /* Say that we haven't consumed the characters with
2985 `display' property yet. The call to pop_it in
2986 set_iterator_to_next will clean this up. */
2987 *position = start_pos;
2989 #endif /* HAVE_WINDOW_SYSTEM */
2991 else
2992 /* Invalid property or property not supported. Restore
2993 the position to what it was before. */
2994 *position = start_pos;
2997 return replaces_text_display_p;
3001 /* Check if PROP is a display sub-property value whose text should be
3002 treated as intangible. */
3004 static int
3005 single_display_prop_intangible_p (prop)
3006 Lisp_Object prop;
3008 /* Skip over `when FORM'. */
3009 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3011 prop = XCDR (prop);
3012 if (!CONSP (prop))
3013 return 0;
3014 prop = XCDR (prop);
3017 if (!CONSP (prop))
3018 return 0;
3020 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3021 we don't need to treat text as intangible. */
3022 if (EQ (XCAR (prop), Qmargin))
3024 prop = XCDR (prop);
3025 if (!CONSP (prop))
3026 return 0;
3028 prop = XCDR (prop);
3029 if (!CONSP (prop)
3030 || EQ (XCAR (prop), Qleft_margin)
3031 || EQ (XCAR (prop), Qright_margin))
3032 return 0;
3035 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3039 /* Check if PROP is a display property value whose text should be
3040 treated as intangible. */
3043 display_prop_intangible_p (prop)
3044 Lisp_Object prop;
3046 if (CONSP (prop)
3047 && CONSP (XCAR (prop))
3048 && !EQ (Qmargin, XCAR (XCAR (prop))))
3050 /* A list of sub-properties. */
3051 while (CONSP (prop))
3053 if (single_display_prop_intangible_p (XCAR (prop)))
3054 return 1;
3055 prop = XCDR (prop);
3058 else if (VECTORP (prop))
3060 /* A vector of sub-properties. */
3061 int i;
3062 for (i = 0; i < ASIZE (prop); ++i)
3063 if (single_display_prop_intangible_p (AREF (prop, i)))
3064 return 1;
3066 else
3067 return single_display_prop_intangible_p (prop);
3069 return 0;
3073 /* Return 1 if PROP is a display sub-property value containing STRING. */
3075 static int
3076 single_display_prop_string_p (prop, string)
3077 Lisp_Object prop, string;
3079 extern Lisp_Object Qwhen, Qmargin;
3081 if (EQ (string, prop))
3082 return 1;
3084 /* Skip over `when FORM'. */
3085 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3087 prop = XCDR (prop);
3088 if (!CONSP (prop))
3089 return 0;
3090 prop = XCDR (prop);
3093 if (CONSP (prop))
3094 /* Skip over `margin LOCATION'. */
3095 if (EQ (XCAR (prop), Qmargin))
3097 prop = XCDR (prop);
3098 if (!CONSP (prop))
3099 return 0;
3101 prop = XCDR (prop);
3102 if (!CONSP (prop))
3103 return 0;
3106 return CONSP (prop) && EQ (XCAR (prop), string);
3110 /* Return 1 if STRING appears in the `display' property PROP. */
3112 static int
3113 display_prop_string_p (prop, string)
3114 Lisp_Object prop, string;
3116 extern Lisp_Object Qwhen, Qmargin;
3118 if (CONSP (prop)
3119 && CONSP (XCAR (prop))
3120 && !EQ (Qmargin, XCAR (XCAR (prop))))
3122 /* A list of sub-properties. */
3123 while (CONSP (prop))
3125 if (single_display_prop_string_p (XCAR (prop), string))
3126 return 1;
3127 prop = XCDR (prop);
3130 else if (VECTORP (prop))
3132 /* A vector of sub-properties. */
3133 int i;
3134 for (i = 0; i < ASIZE (prop); ++i)
3135 if (single_display_prop_string_p (AREF (prop, i), string))
3136 return 1;
3138 else
3139 return single_display_prop_string_p (prop, string);
3141 return 0;
3145 /* Determine from which buffer position in W's buffer STRING comes
3146 from. AROUND_CHARPOS is an approximate position where it could
3147 be from. Value is the buffer position or 0 if it couldn't be
3148 determined.
3150 W's buffer must be current.
3152 This function is necessary because we don't record buffer positions
3153 in glyphs generated from strings (to keep struct glyph small).
3154 This function may only use code that doesn't eval because it is
3155 called asynchronously from note_mouse_highlight. */
3158 string_buffer_position (w, string, around_charpos)
3159 struct window *w;
3160 Lisp_Object string;
3161 int around_charpos;
3163 Lisp_Object around = make_number (around_charpos);
3164 Lisp_Object limit, prop, pos;
3165 const int MAX_DISTANCE = 1000;
3166 int found = 0;
3168 pos = make_number (around_charpos);
3169 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3170 while (!found && !EQ (pos, limit))
3172 prop = Fget_char_property (pos, Qdisplay, Qnil);
3173 if (!NILP (prop) && display_prop_string_p (prop, string))
3174 found = 1;
3175 else
3176 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3179 if (!found)
3181 pos = make_number (around_charpos);
3182 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3183 while (!found && !EQ (pos, limit))
3185 prop = Fget_char_property (pos, Qdisplay, Qnil);
3186 if (!NILP (prop) && display_prop_string_p (prop, string))
3187 found = 1;
3188 else
3189 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3190 limit);
3194 return found ? XINT (pos) : 0;
3199 /***********************************************************************
3200 `composition' property
3201 ***********************************************************************/
3203 /* Set up iterator IT from `composition' property at its current
3204 position. Called from handle_stop. */
3206 static enum prop_handled
3207 handle_composition_prop (it)
3208 struct it *it;
3210 Lisp_Object prop, string;
3211 int pos, pos_byte, end;
3212 enum prop_handled handled = HANDLED_NORMALLY;
3214 if (STRINGP (it->string))
3216 pos = IT_STRING_CHARPOS (*it);
3217 pos_byte = IT_STRING_BYTEPOS (*it);
3218 string = it->string;
3220 else
3222 pos = IT_CHARPOS (*it);
3223 pos_byte = IT_BYTEPOS (*it);
3224 string = Qnil;
3227 /* If there's a valid composition and point is not inside of the
3228 composition (in the case that the composition is from the current
3229 buffer), draw a glyph composed from the composition components. */
3230 if (find_composition (pos, -1, &pos, &end, &prop, string)
3231 && COMPOSITION_VALID_P (pos, end, prop)
3232 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3234 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3236 if (id >= 0)
3238 it->method = next_element_from_composition;
3239 it->cmp_id = id;
3240 it->cmp_len = COMPOSITION_LENGTH (prop);
3241 /* For a terminal, draw only the first character of the
3242 components. */
3243 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3244 it->len = (STRINGP (it->string)
3245 ? string_char_to_byte (it->string, end)
3246 : CHAR_TO_BYTE (end)) - pos_byte;
3247 it->stop_charpos = end;
3248 handled = HANDLED_RETURN;
3252 return handled;
3257 /***********************************************************************
3258 Overlay strings
3259 ***********************************************************************/
3261 /* The following structure is used to record overlay strings for
3262 later sorting in load_overlay_strings. */
3264 struct overlay_entry
3266 Lisp_Object overlay;
3267 Lisp_Object string;
3268 int priority;
3269 int after_string_p;
3273 /* Set up iterator IT from overlay strings at its current position.
3274 Called from handle_stop. */
3276 static enum prop_handled
3277 handle_overlay_change (it)
3278 struct it *it;
3280 if (!STRINGP (it->string) && get_overlay_strings (it))
3281 return HANDLED_RECOMPUTE_PROPS;
3282 else
3283 return HANDLED_NORMALLY;
3287 /* Set up the next overlay string for delivery by IT, if there is an
3288 overlay string to deliver. Called by set_iterator_to_next when the
3289 end of the current overlay string is reached. If there are more
3290 overlay strings to display, IT->string and
3291 IT->current.overlay_string_index are set appropriately here.
3292 Otherwise IT->string is set to nil. */
3294 static void
3295 next_overlay_string (it)
3296 struct it *it;
3298 ++it->current.overlay_string_index;
3299 if (it->current.overlay_string_index == it->n_overlay_strings)
3301 /* No more overlay strings. Restore IT's settings to what
3302 they were before overlay strings were processed, and
3303 continue to deliver from current_buffer. */
3304 pop_it (it);
3305 xassert (it->stop_charpos >= BEGV
3306 && it->stop_charpos <= it->end_charpos);
3307 it->string = Qnil;
3308 it->current.overlay_string_index = -1;
3309 SET_TEXT_POS (it->current.string_pos, -1, -1);
3310 it->n_overlay_strings = 0;
3311 it->method = next_element_from_buffer;
3313 /* If we're at the end of the buffer, record that we have
3314 processed the overlay strings there already, so that
3315 next_element_from_buffer doesn't try it again. */
3316 if (IT_CHARPOS (*it) >= it->end_charpos)
3317 it->overlay_strings_at_end_processed_p = 1;
3319 else
3321 /* There are more overlay strings to process. If
3322 IT->current.overlay_string_index has advanced to a position
3323 where we must load IT->overlay_strings with more strings, do
3324 it. */
3325 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3327 if (it->current.overlay_string_index && i == 0)
3328 load_overlay_strings (it);
3330 /* Initialize IT to deliver display elements from the overlay
3331 string. */
3332 it->string = it->overlay_strings[i];
3333 it->multibyte_p = STRING_MULTIBYTE (it->string);
3334 SET_TEXT_POS (it->current.string_pos, 0, 0);
3335 it->method = next_element_from_string;
3336 it->stop_charpos = 0;
3339 CHECK_IT (it);
3343 /* Compare two overlay_entry structures E1 and E2. Used as a
3344 comparison function for qsort in load_overlay_strings. Overlay
3345 strings for the same position are sorted so that
3347 1. All after-strings come in front of before-strings, except
3348 when they come from the same overlay.
3350 2. Within after-strings, strings are sorted so that overlay strings
3351 from overlays with higher priorities come first.
3353 2. Within before-strings, strings are sorted so that overlay
3354 strings from overlays with higher priorities come last.
3356 Value is analogous to strcmp. */
3359 static int
3360 compare_overlay_entries (e1, e2)
3361 void *e1, *e2;
3363 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3364 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3365 int result;
3367 if (entry1->after_string_p != entry2->after_string_p)
3369 /* Let after-strings appear in front of before-strings if
3370 they come from different overlays. */
3371 if (EQ (entry1->overlay, entry2->overlay))
3372 result = entry1->after_string_p ? 1 : -1;
3373 else
3374 result = entry1->after_string_p ? -1 : 1;
3376 else if (entry1->after_string_p)
3377 /* After-strings sorted in order of decreasing priority. */
3378 result = entry2->priority - entry1->priority;
3379 else
3380 /* Before-strings sorted in order of increasing priority. */
3381 result = entry1->priority - entry2->priority;
3383 return result;
3387 /* Load the vector IT->overlay_strings with overlay strings from IT's
3388 current buffer position. Set IT->n_overlays to the total number of
3389 overlay strings found.
3391 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3392 a time. On entry into load_overlay_strings,
3393 IT->current.overlay_string_index gives the number of overlay
3394 strings that have already been loaded by previous calls to this
3395 function.
3397 IT->add_overlay_start contains an additional overlay start
3398 position to consider for taking overlay strings from, if non-zero.
3399 This position comes into play when the overlay has an `invisible'
3400 property, and both before and after-strings. When we've skipped to
3401 the end of the overlay, because of its `invisible' property, we
3402 nevertheless want its before-string to appear.
3403 IT->add_overlay_start will contain the overlay start position
3404 in this case.
3406 Overlay strings are sorted so that after-string strings come in
3407 front of before-string strings. Within before and after-strings,
3408 strings are sorted by overlay priority. See also function
3409 compare_overlay_entries. */
3411 static void
3412 load_overlay_strings (it)
3413 struct it *it;
3415 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3416 Lisp_Object ov, overlay, window, str, invisible;
3417 int start, end;
3418 int size = 20;
3419 int n = 0, i, j, invis_p;
3420 struct overlay_entry *entries
3421 = (struct overlay_entry *) alloca (size * sizeof *entries);
3422 int charpos = IT_CHARPOS (*it);
3424 /* Append the overlay string STRING of overlay OVERLAY to vector
3425 `entries' which has size `size' and currently contains `n'
3426 elements. AFTER_P non-zero means STRING is an after-string of
3427 OVERLAY. */
3428 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3429 do \
3431 Lisp_Object priority; \
3433 if (n == size) \
3435 int new_size = 2 * size; \
3436 struct overlay_entry *old = entries; \
3437 entries = \
3438 (struct overlay_entry *) alloca (new_size \
3439 * sizeof *entries); \
3440 bcopy (old, entries, size * sizeof *entries); \
3441 size = new_size; \
3444 entries[n].string = (STRING); \
3445 entries[n].overlay = (OVERLAY); \
3446 priority = Foverlay_get ((OVERLAY), Qpriority); \
3447 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3448 entries[n].after_string_p = (AFTER_P); \
3449 ++n; \
3451 while (0)
3453 /* Process overlay before the overlay center. */
3454 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3456 overlay = XCAR (ov);
3457 xassert (OVERLAYP (overlay));
3458 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3459 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3461 if (end < charpos)
3462 break;
3464 /* Skip this overlay if it doesn't start or end at IT's current
3465 position. */
3466 if (end != charpos && start != charpos)
3467 continue;
3469 /* Skip this overlay if it doesn't apply to IT->w. */
3470 window = Foverlay_get (overlay, Qwindow);
3471 if (WINDOWP (window) && XWINDOW (window) != it->w)
3472 continue;
3474 /* If the text ``under'' the overlay is invisible, both before-
3475 and after-strings from this overlay are visible; start and
3476 end position are indistinguishable. */
3477 invisible = Foverlay_get (overlay, Qinvisible);
3478 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3480 /* If overlay has a non-empty before-string, record it. */
3481 if ((start == charpos || (end == charpos && invis_p))
3482 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3483 && XSTRING (str)->size)
3484 RECORD_OVERLAY_STRING (overlay, str, 0);
3486 /* If overlay has a non-empty after-string, record it. */
3487 if ((end == charpos || (start == charpos && invis_p))
3488 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3489 && XSTRING (str)->size)
3490 RECORD_OVERLAY_STRING (overlay, str, 1);
3493 /* Process overlays after the overlay center. */
3494 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3496 overlay = XCAR (ov);
3497 xassert (OVERLAYP (overlay));
3498 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3499 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3501 if (start > charpos)
3502 break;
3504 /* Skip this overlay if it doesn't start or end at IT's current
3505 position. */
3506 if (end != charpos && start != charpos)
3507 continue;
3509 /* Skip this overlay if it doesn't apply to IT->w. */
3510 window = Foverlay_get (overlay, Qwindow);
3511 if (WINDOWP (window) && XWINDOW (window) != it->w)
3512 continue;
3514 /* If the text ``under'' the overlay is invisible, it has a zero
3515 dimension, and both before- and after-strings apply. */
3516 invisible = Foverlay_get (overlay, Qinvisible);
3517 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3519 /* If overlay has a non-empty before-string, record it. */
3520 if ((start == charpos || (end == charpos && invis_p))
3521 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3522 && XSTRING (str)->size)
3523 RECORD_OVERLAY_STRING (overlay, str, 0);
3525 /* If overlay has a non-empty after-string, record it. */
3526 if ((end == charpos || (start == charpos && invis_p))
3527 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3528 && XSTRING (str)->size)
3529 RECORD_OVERLAY_STRING (overlay, str, 1);
3532 #undef RECORD_OVERLAY_STRING
3534 /* Sort entries. */
3535 if (n > 1)
3536 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3538 /* Record the total number of strings to process. */
3539 it->n_overlay_strings = n;
3541 /* IT->current.overlay_string_index is the number of overlay strings
3542 that have already been consumed by IT. Copy some of the
3543 remaining overlay strings to IT->overlay_strings. */
3544 i = 0;
3545 j = it->current.overlay_string_index;
3546 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3547 it->overlay_strings[i++] = entries[j++].string;
3549 CHECK_IT (it);
3553 /* Get the first chunk of overlay strings at IT's current buffer
3554 position. Value is non-zero if at least one overlay string was
3555 found. */
3557 static int
3558 get_overlay_strings (it)
3559 struct it *it;
3561 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3562 process. This fills IT->overlay_strings with strings, and sets
3563 IT->n_overlay_strings to the total number of strings to process.
3564 IT->pos.overlay_string_index has to be set temporarily to zero
3565 because load_overlay_strings needs this; it must be set to -1
3566 when no overlay strings are found because a zero value would
3567 indicate a position in the first overlay string. */
3568 it->current.overlay_string_index = 0;
3569 load_overlay_strings (it);
3571 /* If we found overlay strings, set up IT to deliver display
3572 elements from the first one. Otherwise set up IT to deliver
3573 from current_buffer. */
3574 if (it->n_overlay_strings)
3576 /* Make sure we know settings in current_buffer, so that we can
3577 restore meaningful values when we're done with the overlay
3578 strings. */
3579 compute_stop_pos (it);
3580 xassert (it->face_id >= 0);
3582 /* Save IT's settings. They are restored after all overlay
3583 strings have been processed. */
3584 xassert (it->sp == 0);
3585 push_it (it);
3587 /* Set up IT to deliver display elements from the first overlay
3588 string. */
3589 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3590 it->stop_charpos = 0;
3591 it->string = it->overlay_strings[0];
3592 it->multibyte_p = STRING_MULTIBYTE (it->string);
3593 xassert (STRINGP (it->string));
3594 it->method = next_element_from_string;
3596 else
3598 it->string = Qnil;
3599 it->current.overlay_string_index = -1;
3600 it->method = next_element_from_buffer;
3603 CHECK_IT (it);
3605 /* Value is non-zero if we found at least one overlay string. */
3606 return STRINGP (it->string);
3611 /***********************************************************************
3612 Saving and restoring state
3613 ***********************************************************************/
3615 /* Save current settings of IT on IT->stack. Called, for example,
3616 before setting up IT for an overlay string, to be able to restore
3617 IT's settings to what they were after the overlay string has been
3618 processed. */
3620 static void
3621 push_it (it)
3622 struct it *it;
3624 struct iterator_stack_entry *p;
3626 xassert (it->sp < 2);
3627 p = it->stack + it->sp;
3629 p->stop_charpos = it->stop_charpos;
3630 xassert (it->face_id >= 0);
3631 p->face_id = it->face_id;
3632 p->string = it->string;
3633 p->pos = it->current;
3634 p->end_charpos = it->end_charpos;
3635 p->string_nchars = it->string_nchars;
3636 p->area = it->area;
3637 p->multibyte_p = it->multibyte_p;
3638 p->space_width = it->space_width;
3639 p->font_height = it->font_height;
3640 p->voffset = it->voffset;
3641 p->string_from_display_prop_p = it->string_from_display_prop_p;
3642 ++it->sp;
3646 /* Restore IT's settings from IT->stack. Called, for example, when no
3647 more overlay strings must be processed, and we return to delivering
3648 display elements from a buffer, or when the end of a string from a
3649 `display' property is reached and we return to delivering display
3650 elements from an overlay string, or from a buffer. */
3652 static void
3653 pop_it (it)
3654 struct it *it;
3656 struct iterator_stack_entry *p;
3658 xassert (it->sp > 0);
3659 --it->sp;
3660 p = it->stack + it->sp;
3661 it->stop_charpos = p->stop_charpos;
3662 it->face_id = p->face_id;
3663 it->string = p->string;
3664 it->current = p->pos;
3665 it->end_charpos = p->end_charpos;
3666 it->string_nchars = p->string_nchars;
3667 it->area = p->area;
3668 it->multibyte_p = p->multibyte_p;
3669 it->space_width = p->space_width;
3670 it->font_height = p->font_height;
3671 it->voffset = p->voffset;
3672 it->string_from_display_prop_p = p->string_from_display_prop_p;
3677 /***********************************************************************
3678 Moving over lines
3679 ***********************************************************************/
3681 /* Set IT's current position to the previous line start. */
3683 static void
3684 back_to_previous_line_start (it)
3685 struct it *it;
3687 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3688 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3692 /* Move IT to the next line start.
3694 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3695 we skipped over part of the text (as opposed to moving the iterator
3696 continuously over the text). Otherwise, don't change the value
3697 of *SKIPPED_P.
3699 Newlines may come from buffer text, overlay strings, or strings
3700 displayed via the `display' property. That's the reason we can't
3701 simply use find_next_newline_no_quit.
3703 Note that this function may not skip over invisible text that is so
3704 because of text properties and immediately follows a newline. If
3705 it would, function reseat_at_next_visible_line_start, when called
3706 from set_iterator_to_next, would effectively make invisible
3707 characters following a newline part of the wrong glyph row, which
3708 leads to wrong cursor motion. */
3710 static int
3711 forward_to_next_line_start (it, skipped_p)
3712 struct it *it;
3713 int *skipped_p;
3715 int old_selective, newline_found_p, n;
3716 const int MAX_NEWLINE_DISTANCE = 500;
3718 /* If already on a newline, just consume it to avoid unintended
3719 skipping over invisible text below. */
3720 if (it->what == IT_CHARACTER
3721 && it->c == '\n'
3722 && CHARPOS (it->position) == IT_CHARPOS (*it))
3724 set_iterator_to_next (it, 0);
3725 it->c = 0;
3726 return 1;
3729 /* Don't handle selective display in the following. It's (a)
3730 unnecessary because it's done by the caller, and (b) leads to an
3731 infinite recursion because next_element_from_ellipsis indirectly
3732 calls this function. */
3733 old_selective = it->selective;
3734 it->selective = 0;
3736 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3737 from buffer text. */
3738 for (n = newline_found_p = 0;
3739 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3740 n += STRINGP (it->string) ? 0 : 1)
3742 if (!get_next_display_element (it))
3743 break;
3744 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3745 set_iterator_to_next (it, 0);
3748 /* If we didn't find a newline near enough, see if we can use a
3749 short-cut. */
3750 if (n == MAX_NEWLINE_DISTANCE)
3752 int start = IT_CHARPOS (*it);
3753 int limit = find_next_newline_no_quit (start, 1);
3754 Lisp_Object pos;
3756 xassert (!STRINGP (it->string));
3758 /* If there isn't any `display' property in sight, and no
3759 overlays, we can just use the position of the newline in
3760 buffer text. */
3761 if (it->stop_charpos >= limit
3762 || ((pos = Fnext_single_property_change (make_number (start),
3763 Qdisplay,
3764 Qnil, make_number (limit)),
3765 NILP (pos))
3766 && next_overlay_change (start) == ZV))
3768 IT_CHARPOS (*it) = limit;
3769 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3770 *skipped_p = newline_found_p = 1;
3772 else
3774 while (get_next_display_element (it)
3775 && !newline_found_p)
3777 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3778 set_iterator_to_next (it, 0);
3783 it->selective = old_selective;
3784 return newline_found_p;
3788 /* Set IT's current position to the previous visible line start. Skip
3789 invisible text that is so either due to text properties or due to
3790 selective display. Caution: this does not change IT->current_x and
3791 IT->hpos. */
3793 static void
3794 back_to_previous_visible_line_start (it)
3795 struct it *it;
3797 int visible_p = 0;
3799 /* Go back one newline if not on BEGV already. */
3800 if (IT_CHARPOS (*it) > BEGV)
3801 back_to_previous_line_start (it);
3803 /* Move over lines that are invisible because of selective display
3804 or text properties. */
3805 while (IT_CHARPOS (*it) > BEGV
3806 && !visible_p)
3808 visible_p = 1;
3810 /* If selective > 0, then lines indented more than that values
3811 are invisible. */
3812 if (it->selective > 0
3813 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3814 it->selective))
3815 visible_p = 0;
3816 else
3818 Lisp_Object prop;
3820 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3821 Qinvisible, it->window);
3822 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3823 visible_p = 0;
3826 /* Back one more newline if the current one is invisible. */
3827 if (!visible_p)
3828 back_to_previous_line_start (it);
3831 xassert (IT_CHARPOS (*it) >= BEGV);
3832 xassert (IT_CHARPOS (*it) == BEGV
3833 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3834 CHECK_IT (it);
3838 /* Reseat iterator IT at the previous visible line start. Skip
3839 invisible text that is so either due to text properties or due to
3840 selective display. At the end, update IT's overlay information,
3841 face information etc. */
3843 static void
3844 reseat_at_previous_visible_line_start (it)
3845 struct it *it;
3847 back_to_previous_visible_line_start (it);
3848 reseat (it, it->current.pos, 1);
3849 CHECK_IT (it);
3853 /* Reseat iterator IT on the next visible line start in the current
3854 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3855 preceding the line start. Skip over invisible text that is so
3856 because of selective display. Compute faces, overlays etc at the
3857 new position. Note that this function does not skip over text that
3858 is invisible because of text properties. */
3860 static void
3861 reseat_at_next_visible_line_start (it, on_newline_p)
3862 struct it *it;
3863 int on_newline_p;
3865 int newline_found_p, skipped_p = 0;
3867 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3869 /* Skip over lines that are invisible because they are indented
3870 more than the value of IT->selective. */
3871 if (it->selective > 0)
3872 while (IT_CHARPOS (*it) < ZV
3873 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3874 it->selective))
3876 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3877 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3880 /* Position on the newline if that's what's requested. */
3881 if (on_newline_p && newline_found_p)
3883 if (STRINGP (it->string))
3885 if (IT_STRING_CHARPOS (*it) > 0)
3887 --IT_STRING_CHARPOS (*it);
3888 --IT_STRING_BYTEPOS (*it);
3891 else if (IT_CHARPOS (*it) > BEGV)
3893 --IT_CHARPOS (*it);
3894 --IT_BYTEPOS (*it);
3895 reseat (it, it->current.pos, 0);
3898 else if (skipped_p)
3899 reseat (it, it->current.pos, 0);
3901 CHECK_IT (it);
3906 /***********************************************************************
3907 Changing an iterator's position
3908 ***********************************************************************/
3910 /* Change IT's current position to POS in current_buffer. If FORCE_P
3911 is non-zero, always check for text properties at the new position.
3912 Otherwise, text properties are only looked up if POS >=
3913 IT->check_charpos of a property. */
3915 static void
3916 reseat (it, pos, force_p)
3917 struct it *it;
3918 struct text_pos pos;
3919 int force_p;
3921 int original_pos = IT_CHARPOS (*it);
3923 reseat_1 (it, pos, 0);
3925 /* Determine where to check text properties. Avoid doing it
3926 where possible because text property lookup is very expensive. */
3927 if (force_p
3928 || CHARPOS (pos) > it->stop_charpos
3929 || CHARPOS (pos) < original_pos)
3930 handle_stop (it);
3932 CHECK_IT (it);
3936 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3937 IT->stop_pos to POS, also. */
3939 static void
3940 reseat_1 (it, pos, set_stop_p)
3941 struct it *it;
3942 struct text_pos pos;
3943 int set_stop_p;
3945 /* Don't call this function when scanning a C string. */
3946 xassert (it->s == NULL);
3948 /* POS must be a reasonable value. */
3949 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3951 it->current.pos = it->position = pos;
3952 XSETBUFFER (it->object, current_buffer);
3953 it->end_charpos = ZV;
3954 it->dpvec = NULL;
3955 it->current.dpvec_index = -1;
3956 it->current.overlay_string_index = -1;
3957 IT_STRING_CHARPOS (*it) = -1;
3958 IT_STRING_BYTEPOS (*it) = -1;
3959 it->string = Qnil;
3960 it->method = next_element_from_buffer;
3961 it->sp = 0;
3962 it->face_before_selective_p = 0;
3964 if (set_stop_p)
3965 it->stop_charpos = CHARPOS (pos);
3969 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3970 If S is non-null, it is a C string to iterate over. Otherwise,
3971 STRING gives a Lisp string to iterate over.
3973 If PRECISION > 0, don't return more then PRECISION number of
3974 characters from the string.
3976 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3977 characters have been returned. FIELD_WIDTH < 0 means an infinite
3978 field width.
3980 MULTIBYTE = 0 means disable processing of multibyte characters,
3981 MULTIBYTE > 0 means enable it,
3982 MULTIBYTE < 0 means use IT->multibyte_p.
3984 IT must be initialized via a prior call to init_iterator before
3985 calling this function. */
3987 static void
3988 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3989 struct it *it;
3990 unsigned char *s;
3991 Lisp_Object string;
3992 int charpos;
3993 int precision, field_width, multibyte;
3995 /* No region in strings. */
3996 it->region_beg_charpos = it->region_end_charpos = -1;
3998 /* No text property checks performed by default, but see below. */
3999 it->stop_charpos = -1;
4001 /* Set iterator position and end position. */
4002 bzero (&it->current, sizeof it->current);
4003 it->current.overlay_string_index = -1;
4004 it->current.dpvec_index = -1;
4005 xassert (charpos >= 0);
4007 /* If STRING is specified, use its multibyteness, otherwise use the
4008 setting of MULTIBYTE, if specified. */
4009 if (STRINGP (string))
4010 it->multibyte_p = STRING_MULTIBYTE (string);
4011 else if (multibyte >= 0)
4012 it->multibyte_p = multibyte > 0;
4014 if (s == NULL)
4016 xassert (STRINGP (string));
4017 it->string = string;
4018 it->s = NULL;
4019 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4020 it->method = next_element_from_string;
4021 it->current.string_pos = string_pos (charpos, string);
4023 else
4025 it->s = s;
4026 it->string = Qnil;
4028 /* Note that we use IT->current.pos, not it->current.string_pos,
4029 for displaying C strings. */
4030 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4031 if (it->multibyte_p)
4033 it->current.pos = c_string_pos (charpos, s, 1);
4034 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4036 else
4038 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4039 it->end_charpos = it->string_nchars = strlen (s);
4042 it->method = next_element_from_c_string;
4045 /* PRECISION > 0 means don't return more than PRECISION characters
4046 from the string. */
4047 if (precision > 0 && it->end_charpos - charpos > precision)
4048 it->end_charpos = it->string_nchars = charpos + precision;
4050 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4051 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4052 FIELD_WIDTH < 0 means infinite field width. This is useful for
4053 padding with `-' at the end of a mode line. */
4054 if (field_width < 0)
4055 field_width = INFINITY;
4056 if (field_width > it->end_charpos - charpos)
4057 it->end_charpos = charpos + field_width;
4059 /* Use the standard display table for displaying strings. */
4060 if (DISP_TABLE_P (Vstandard_display_table))
4061 it->dp = XCHAR_TABLE (Vstandard_display_table);
4063 it->stop_charpos = charpos;
4064 CHECK_IT (it);
4069 /***********************************************************************
4070 Iteration
4071 ***********************************************************************/
4073 /* Load IT's display element fields with information about the next
4074 display element from the current position of IT. Value is zero if
4075 end of buffer (or C string) is reached. */
4078 get_next_display_element (it)
4079 struct it *it;
4081 /* Non-zero means that we found an display element. Zero means that
4082 we hit the end of what we iterate over. Performance note: the
4083 function pointer `method' used here turns out to be faster than
4084 using a sequence of if-statements. */
4085 int success_p = (*it->method) (it);
4087 if (it->what == IT_CHARACTER)
4089 /* Map via display table or translate control characters.
4090 IT->c, IT->len etc. have been set to the next character by
4091 the function call above. If we have a display table, and it
4092 contains an entry for IT->c, translate it. Don't do this if
4093 IT->c itself comes from a display table, otherwise we could
4094 end up in an infinite recursion. (An alternative could be to
4095 count the recursion depth of this function and signal an
4096 error when a certain maximum depth is reached.) Is it worth
4097 it? */
4098 if (success_p && it->dpvec == NULL)
4100 Lisp_Object dv;
4102 if (it->dp
4103 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4104 VECTORP (dv)))
4106 struct Lisp_Vector *v = XVECTOR (dv);
4108 /* Return the first character from the display table
4109 entry, if not empty. If empty, don't display the
4110 current character. */
4111 if (v->size)
4113 it->dpvec_char_len = it->len;
4114 it->dpvec = v->contents;
4115 it->dpend = v->contents + v->size;
4116 it->current.dpvec_index = 0;
4117 it->method = next_element_from_display_vector;
4118 success_p = get_next_display_element (it);
4120 else
4122 set_iterator_to_next (it, 0);
4123 success_p = get_next_display_element (it);
4127 /* Translate control characters into `\003' or `^C' form.
4128 Control characters coming from a display table entry are
4129 currently not translated because we use IT->dpvec to hold
4130 the translation. This could easily be changed but I
4131 don't believe that it is worth doing.
4133 Non-printable multibyte characters are also translated
4134 octal form. */
4135 else if ((it->c < ' '
4136 && (it->area != TEXT_AREA
4137 || (it->c != '\n' && it->c != '\t')))
4138 || (it->c >= 127
4139 && it->len == 1)
4140 || !CHAR_PRINTABLE_P (it->c))
4142 /* IT->c is a control character which must be displayed
4143 either as '\003' or as `^C' where the '\\' and '^'
4144 can be defined in the display table. Fill
4145 IT->ctl_chars with glyphs for what we have to
4146 display. Then, set IT->dpvec to these glyphs. */
4147 GLYPH g;
4149 if (it->c < 128 && it->ctl_arrow_p)
4151 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4152 if (it->dp
4153 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4154 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4155 g = XINT (DISP_CTRL_GLYPH (it->dp));
4156 else
4157 g = FAST_MAKE_GLYPH ('^', 0);
4158 XSETINT (it->ctl_chars[0], g);
4160 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4161 XSETINT (it->ctl_chars[1], g);
4163 /* Set up IT->dpvec and return first character from it. */
4164 it->dpvec_char_len = it->len;
4165 it->dpvec = it->ctl_chars;
4166 it->dpend = it->dpvec + 2;
4167 it->current.dpvec_index = 0;
4168 it->method = next_element_from_display_vector;
4169 get_next_display_element (it);
4171 else
4173 unsigned char str[MAX_MULTIBYTE_LENGTH];
4174 int len;
4175 int i;
4176 GLYPH escape_glyph;
4178 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4179 if (it->dp
4180 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4181 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4182 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4183 else
4184 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4186 if (SINGLE_BYTE_CHAR_P (it->c))
4187 str[0] = it->c, len = 1;
4188 else
4189 len = CHAR_STRING (it->c, str);
4191 for (i = 0; i < len; i++)
4193 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4194 /* Insert three more glyphs into IT->ctl_chars for
4195 the octal display of the character. */
4196 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4197 XSETINT (it->ctl_chars[i * 4 + 1], g);
4198 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4199 XSETINT (it->ctl_chars[i * 4 + 2], g);
4200 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4201 XSETINT (it->ctl_chars[i * 4 + 3], g);
4204 /* Set up IT->dpvec and return the first character
4205 from it. */
4206 it->dpvec_char_len = it->len;
4207 it->dpvec = it->ctl_chars;
4208 it->dpend = it->dpvec + len * 4;
4209 it->current.dpvec_index = 0;
4210 it->method = next_element_from_display_vector;
4211 get_next_display_element (it);
4216 /* Adjust face id for a multibyte character. There are no
4217 multibyte character in unibyte text. */
4218 if (it->multibyte_p
4219 && success_p
4220 && FRAME_WINDOW_P (it->f))
4222 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4223 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4227 /* Is this character the last one of a run of characters with
4228 box? If yes, set IT->end_of_box_run_p to 1. */
4229 if (it->face_box_p
4230 && it->s == NULL)
4232 int face_id;
4233 struct face *face;
4235 it->end_of_box_run_p
4236 = ((face_id = face_after_it_pos (it),
4237 face_id != it->face_id)
4238 && (face = FACE_FROM_ID (it->f, face_id),
4239 face->box == FACE_NO_BOX));
4242 /* Value is 0 if end of buffer or string reached. */
4243 return success_p;
4247 /* Move IT to the next display element.
4249 RESEAT_P non-zero means if called on a newline in buffer text,
4250 skip to the next visible line start.
4252 Functions get_next_display_element and set_iterator_to_next are
4253 separate because I find this arrangement easier to handle than a
4254 get_next_display_element function that also increments IT's
4255 position. The way it is we can first look at an iterator's current
4256 display element, decide whether it fits on a line, and if it does,
4257 increment the iterator position. The other way around we probably
4258 would either need a flag indicating whether the iterator has to be
4259 incremented the next time, or we would have to implement a
4260 decrement position function which would not be easy to write. */
4262 void
4263 set_iterator_to_next (it, reseat_p)
4264 struct it *it;
4265 int reseat_p;
4267 /* Reset flags indicating start and end of a sequence of characters
4268 with box. Reset them at the start of this function because
4269 moving the iterator to a new position might set them. */
4270 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4272 if (it->method == next_element_from_buffer)
4274 /* The current display element of IT is a character from
4275 current_buffer. Advance in the buffer, and maybe skip over
4276 invisible lines that are so because of selective display. */
4277 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4278 reseat_at_next_visible_line_start (it, 0);
4279 else
4281 xassert (it->len != 0);
4282 IT_BYTEPOS (*it) += it->len;
4283 IT_CHARPOS (*it) += 1;
4284 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4287 else if (it->method == next_element_from_composition)
4289 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4290 if (STRINGP (it->string))
4292 IT_STRING_BYTEPOS (*it) += it->len;
4293 IT_STRING_CHARPOS (*it) += it->cmp_len;
4294 it->method = next_element_from_string;
4295 goto consider_string_end;
4297 else
4299 IT_BYTEPOS (*it) += it->len;
4300 IT_CHARPOS (*it) += it->cmp_len;
4301 it->method = next_element_from_buffer;
4304 else if (it->method == next_element_from_c_string)
4306 /* Current display element of IT is from a C string. */
4307 IT_BYTEPOS (*it) += it->len;
4308 IT_CHARPOS (*it) += 1;
4310 else if (it->method == next_element_from_display_vector)
4312 /* Current display element of IT is from a display table entry.
4313 Advance in the display table definition. Reset it to null if
4314 end reached, and continue with characters from buffers/
4315 strings. */
4316 ++it->current.dpvec_index;
4318 /* Restore face of the iterator to what they were before the
4319 display vector entry (these entries may contain faces). */
4320 it->face_id = it->saved_face_id;
4322 if (it->dpvec + it->current.dpvec_index == it->dpend)
4324 if (it->s)
4325 it->method = next_element_from_c_string;
4326 else if (STRINGP (it->string))
4327 it->method = next_element_from_string;
4328 else
4329 it->method = next_element_from_buffer;
4331 it->dpvec = NULL;
4332 it->current.dpvec_index = -1;
4334 /* Skip over characters which were displayed via IT->dpvec. */
4335 if (it->dpvec_char_len < 0)
4336 reseat_at_next_visible_line_start (it, 1);
4337 else if (it->dpvec_char_len > 0)
4339 it->len = it->dpvec_char_len;
4340 set_iterator_to_next (it, reseat_p);
4344 else if (it->method == next_element_from_string)
4346 /* Current display element is a character from a Lisp string. */
4347 xassert (it->s == NULL && STRINGP (it->string));
4348 IT_STRING_BYTEPOS (*it) += it->len;
4349 IT_STRING_CHARPOS (*it) += 1;
4351 consider_string_end:
4353 if (it->current.overlay_string_index >= 0)
4355 /* IT->string is an overlay string. Advance to the
4356 next, if there is one. */
4357 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4358 next_overlay_string (it);
4360 else
4362 /* IT->string is not an overlay string. If we reached
4363 its end, and there is something on IT->stack, proceed
4364 with what is on the stack. This can be either another
4365 string, this time an overlay string, or a buffer. */
4366 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4367 && it->sp > 0)
4369 pop_it (it);
4370 if (!STRINGP (it->string))
4371 it->method = next_element_from_buffer;
4375 else if (it->method == next_element_from_image
4376 || it->method == next_element_from_stretch)
4378 /* The position etc with which we have to proceed are on
4379 the stack. The position may be at the end of a string,
4380 if the `display' property takes up the whole string. */
4381 pop_it (it);
4382 it->image_id = 0;
4383 if (STRINGP (it->string))
4385 it->method = next_element_from_string;
4386 goto consider_string_end;
4388 else
4389 it->method = next_element_from_buffer;
4391 else
4392 /* There are no other methods defined, so this should be a bug. */
4393 abort ();
4395 xassert (it->method != next_element_from_string
4396 || (STRINGP (it->string)
4397 && IT_STRING_CHARPOS (*it) >= 0));
4401 /* Load IT's display element fields with information about the next
4402 display element which comes from a display table entry or from the
4403 result of translating a control character to one of the forms `^C'
4404 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4406 static int
4407 next_element_from_display_vector (it)
4408 struct it *it;
4410 /* Precondition. */
4411 xassert (it->dpvec && it->current.dpvec_index >= 0);
4413 /* Remember the current face id in case glyphs specify faces.
4414 IT's face is restored in set_iterator_to_next. */
4415 it->saved_face_id = it->face_id;
4417 if (INTEGERP (*it->dpvec)
4418 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4420 int lface_id;
4421 GLYPH g;
4423 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4424 it->c = FAST_GLYPH_CHAR (g);
4425 it->len = CHAR_BYTES (it->c);
4427 /* The entry may contain a face id to use. Such a face id is
4428 the id of a Lisp face, not a realized face. A face id of
4429 zero means no face is specified. */
4430 lface_id = FAST_GLYPH_FACE (g);
4431 if (lface_id)
4433 /* The function returns -1 if lface_id is invalid. */
4434 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4435 if (face_id >= 0)
4436 it->face_id = face_id;
4439 else
4440 /* Display table entry is invalid. Return a space. */
4441 it->c = ' ', it->len = 1;
4443 /* Don't change position and object of the iterator here. They are
4444 still the values of the character that had this display table
4445 entry or was translated, and that's what we want. */
4446 it->what = IT_CHARACTER;
4447 return 1;
4451 /* Load IT with the next display element from Lisp string IT->string.
4452 IT->current.string_pos is the current position within the string.
4453 If IT->current.overlay_string_index >= 0, the Lisp string is an
4454 overlay string. */
4456 static int
4457 next_element_from_string (it)
4458 struct it *it;
4460 struct text_pos position;
4462 xassert (STRINGP (it->string));
4463 xassert (IT_STRING_CHARPOS (*it) >= 0);
4464 position = it->current.string_pos;
4466 /* Time to check for invisible text? */
4467 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4468 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4470 handle_stop (it);
4472 /* Since a handler may have changed IT->method, we must
4473 recurse here. */
4474 return get_next_display_element (it);
4477 if (it->current.overlay_string_index >= 0)
4479 /* Get the next character from an overlay string. In overlay
4480 strings, There is no field width or padding with spaces to
4481 do. */
4482 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4484 it->what = IT_EOB;
4485 return 0;
4487 else if (STRING_MULTIBYTE (it->string))
4489 int remaining = (STRING_BYTES (XSTRING (it->string))
4490 - IT_STRING_BYTEPOS (*it));
4491 unsigned char *s = (XSTRING (it->string)->data
4492 + IT_STRING_BYTEPOS (*it));
4493 it->c = string_char_and_length (s, remaining, &it->len);
4495 else
4497 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4498 it->len = 1;
4501 else
4503 /* Get the next character from a Lisp string that is not an
4504 overlay string. Such strings come from the mode line, for
4505 example. We may have to pad with spaces, or truncate the
4506 string. See also next_element_from_c_string. */
4507 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4509 it->what = IT_EOB;
4510 return 0;
4512 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4514 /* Pad with spaces. */
4515 it->c = ' ', it->len = 1;
4516 CHARPOS (position) = BYTEPOS (position) = -1;
4518 else if (STRING_MULTIBYTE (it->string))
4520 int maxlen = (STRING_BYTES (XSTRING (it->string))
4521 - IT_STRING_BYTEPOS (*it));
4522 unsigned char *s = (XSTRING (it->string)->data
4523 + IT_STRING_BYTEPOS (*it));
4524 it->c = string_char_and_length (s, maxlen, &it->len);
4526 else
4528 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4529 it->len = 1;
4533 /* Record what we have and where it came from. Note that we store a
4534 buffer position in IT->position although it could arguably be a
4535 string position. */
4536 it->what = IT_CHARACTER;
4537 it->object = it->string;
4538 it->position = position;
4539 return 1;
4543 /* Load IT with next display element from C string IT->s.
4544 IT->string_nchars is the maximum number of characters to return
4545 from the string. IT->end_charpos may be greater than
4546 IT->string_nchars when this function is called, in which case we
4547 may have to return padding spaces. Value is zero if end of string
4548 reached, including padding spaces. */
4550 static int
4551 next_element_from_c_string (it)
4552 struct it *it;
4554 int success_p = 1;
4556 xassert (it->s);
4557 it->what = IT_CHARACTER;
4558 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4559 it->object = Qnil;
4561 /* IT's position can be greater IT->string_nchars in case a field
4562 width or precision has been specified when the iterator was
4563 initialized. */
4564 if (IT_CHARPOS (*it) >= it->end_charpos)
4566 /* End of the game. */
4567 it->what = IT_EOB;
4568 success_p = 0;
4570 else if (IT_CHARPOS (*it) >= it->string_nchars)
4572 /* Pad with spaces. */
4573 it->c = ' ', it->len = 1;
4574 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4576 else if (it->multibyte_p)
4578 /* Implementation note: The calls to strlen apparently aren't a
4579 performance problem because there is no noticeable performance
4580 difference between Emacs running in unibyte or multibyte mode. */
4581 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4582 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4583 maxlen, &it->len);
4585 else
4586 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4588 return success_p;
4592 /* Set up IT to return characters from an ellipsis, if appropriate.
4593 The definition of the ellipsis glyphs may come from a display table
4594 entry. This function Fills IT with the first glyph from the
4595 ellipsis if an ellipsis is to be displayed. */
4597 static int
4598 next_element_from_ellipsis (it)
4599 struct it *it;
4601 if (it->selective_display_ellipsis_p)
4603 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4605 /* Use the display table definition for `...'. Invalid glyphs
4606 will be handled by the method returning elements from dpvec. */
4607 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4608 it->dpvec_char_len = it->len;
4609 it->dpvec = v->contents;
4610 it->dpend = v->contents + v->size;
4611 it->current.dpvec_index = 0;
4612 it->method = next_element_from_display_vector;
4614 else
4616 /* Use default `...' which is stored in default_invis_vector. */
4617 it->dpvec_char_len = it->len;
4618 it->dpvec = default_invis_vector;
4619 it->dpend = default_invis_vector + 3;
4620 it->current.dpvec_index = 0;
4621 it->method = next_element_from_display_vector;
4624 else
4626 /* The face at the current position may be different from the
4627 face we find after the invisible text. Remember what it
4628 was in IT->saved_face_id, and signal that it's there by
4629 setting face_before_selective_p. */
4630 it->saved_face_id = it->face_id;
4631 it->method = next_element_from_buffer;
4632 reseat_at_next_visible_line_start (it, 1);
4633 it->face_before_selective_p = 1;
4636 return get_next_display_element (it);
4640 /* Deliver an image display element. The iterator IT is already
4641 filled with image information (done in handle_display_prop). Value
4642 is always 1. */
4645 static int
4646 next_element_from_image (it)
4647 struct it *it;
4649 it->what = IT_IMAGE;
4650 return 1;
4654 /* Fill iterator IT with next display element from a stretch glyph
4655 property. IT->object is the value of the text property. Value is
4656 always 1. */
4658 static int
4659 next_element_from_stretch (it)
4660 struct it *it;
4662 it->what = IT_STRETCH;
4663 return 1;
4667 /* Load IT with the next display element from current_buffer. Value
4668 is zero if end of buffer reached. IT->stop_charpos is the next
4669 position at which to stop and check for text properties or buffer
4670 end. */
4672 static int
4673 next_element_from_buffer (it)
4674 struct it *it;
4676 int success_p = 1;
4678 /* Check this assumption, otherwise, we would never enter the
4679 if-statement, below. */
4680 xassert (IT_CHARPOS (*it) >= BEGV
4681 && IT_CHARPOS (*it) <= it->stop_charpos);
4683 if (IT_CHARPOS (*it) >= it->stop_charpos)
4685 if (IT_CHARPOS (*it) >= it->end_charpos)
4687 int overlay_strings_follow_p;
4689 /* End of the game, except when overlay strings follow that
4690 haven't been returned yet. */
4691 if (it->overlay_strings_at_end_processed_p)
4692 overlay_strings_follow_p = 0;
4693 else
4695 it->overlay_strings_at_end_processed_p = 1;
4696 overlay_strings_follow_p = get_overlay_strings (it);
4699 if (overlay_strings_follow_p)
4700 success_p = get_next_display_element (it);
4701 else
4703 it->what = IT_EOB;
4704 it->position = it->current.pos;
4705 success_p = 0;
4708 else
4710 handle_stop (it);
4711 return get_next_display_element (it);
4714 else
4716 /* No face changes, overlays etc. in sight, so just return a
4717 character from current_buffer. */
4718 unsigned char *p;
4720 /* Maybe run the redisplay end trigger hook. Performance note:
4721 This doesn't seem to cost measurable time. */
4722 if (it->redisplay_end_trigger_charpos
4723 && it->glyph_row
4724 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4725 run_redisplay_end_trigger_hook (it);
4727 /* Get the next character, maybe multibyte. */
4728 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4729 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4731 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4732 - IT_BYTEPOS (*it));
4733 it->c = string_char_and_length (p, maxlen, &it->len);
4735 else
4736 it->c = *p, it->len = 1;
4738 /* Record what we have and where it came from. */
4739 it->what = IT_CHARACTER;;
4740 it->object = it->w->buffer;
4741 it->position = it->current.pos;
4743 /* Normally we return the character found above, except when we
4744 really want to return an ellipsis for selective display. */
4745 if (it->selective)
4747 if (it->c == '\n')
4749 /* A value of selective > 0 means hide lines indented more
4750 than that number of columns. */
4751 if (it->selective > 0
4752 && IT_CHARPOS (*it) + 1 < ZV
4753 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4754 IT_BYTEPOS (*it) + 1,
4755 it->selective))
4757 success_p = next_element_from_ellipsis (it);
4758 it->dpvec_char_len = -1;
4761 else if (it->c == '\r' && it->selective == -1)
4763 /* A value of selective == -1 means that everything from the
4764 CR to the end of the line is invisible, with maybe an
4765 ellipsis displayed for it. */
4766 success_p = next_element_from_ellipsis (it);
4767 it->dpvec_char_len = -1;
4772 /* Value is zero if end of buffer reached. */
4773 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4774 return success_p;
4778 /* Run the redisplay end trigger hook for IT. */
4780 static void
4781 run_redisplay_end_trigger_hook (it)
4782 struct it *it;
4784 Lisp_Object args[3];
4786 /* IT->glyph_row should be non-null, i.e. we should be actually
4787 displaying something, or otherwise we should not run the hook. */
4788 xassert (it->glyph_row);
4790 /* Set up hook arguments. */
4791 args[0] = Qredisplay_end_trigger_functions;
4792 args[1] = it->window;
4793 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4794 it->redisplay_end_trigger_charpos = 0;
4796 /* Since we are *trying* to run these functions, don't try to run
4797 them again, even if they get an error. */
4798 it->w->redisplay_end_trigger = Qnil;
4799 Frun_hook_with_args (3, args);
4801 /* Notice if it changed the face of the character we are on. */
4802 handle_face_prop (it);
4806 /* Deliver a composition display element. The iterator IT is already
4807 filled with composition information (done in
4808 handle_composition_prop). Value is always 1. */
4810 static int
4811 next_element_from_composition (it)
4812 struct it *it;
4814 it->what = IT_COMPOSITION;
4815 it->position = (STRINGP (it->string)
4816 ? it->current.string_pos
4817 : it->current.pos);
4818 return 1;
4823 /***********************************************************************
4824 Moving an iterator without producing glyphs
4825 ***********************************************************************/
4827 /* Move iterator IT to a specified buffer or X position within one
4828 line on the display without producing glyphs.
4830 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4831 whichever is reached first.
4833 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4835 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4836 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4837 TO_X includes the amount by which a window is horizontally
4838 scrolled.
4840 Value is
4842 MOVE_POS_MATCH_OR_ZV
4843 - when TO_POS or ZV was reached.
4845 MOVE_X_REACHED
4846 -when TO_X was reached before TO_POS or ZV were reached.
4848 MOVE_LINE_CONTINUED
4849 - when we reached the end of the display area and the line must
4850 be continued.
4852 MOVE_LINE_TRUNCATED
4853 - when we reached the end of the display area and the line is
4854 truncated.
4856 MOVE_NEWLINE_OR_CR
4857 - when we stopped at a line end, i.e. a newline or a CR and selective
4858 display is on. */
4860 static enum move_it_result
4861 move_it_in_display_line_to (it, to_charpos, to_x, op)
4862 struct it *it;
4863 int to_charpos, to_x, op;
4865 enum move_it_result result = MOVE_UNDEFINED;
4866 struct glyph_row *saved_glyph_row;
4868 /* Don't produce glyphs in produce_glyphs. */
4869 saved_glyph_row = it->glyph_row;
4870 it->glyph_row = NULL;
4872 while (1)
4874 int x, i, ascent = 0, descent = 0;
4876 /* Stop when ZV or TO_CHARPOS reached. */
4877 if (!get_next_display_element (it)
4878 || ((op & MOVE_TO_POS) != 0
4879 && BUFFERP (it->object)
4880 && IT_CHARPOS (*it) >= to_charpos))
4882 result = MOVE_POS_MATCH_OR_ZV;
4883 break;
4886 /* The call to produce_glyphs will get the metrics of the
4887 display element IT is loaded with. We record in x the
4888 x-position before this display element in case it does not
4889 fit on the line. */
4890 x = it->current_x;
4892 /* Remember the line height so far in case the next element doesn't
4893 fit on the line. */
4894 if (!it->truncate_lines_p)
4896 ascent = it->max_ascent;
4897 descent = it->max_descent;
4900 PRODUCE_GLYPHS (it);
4902 if (it->area != TEXT_AREA)
4904 set_iterator_to_next (it, 1);
4905 continue;
4908 /* The number of glyphs we get back in IT->nglyphs will normally
4909 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4910 character on a terminal frame, or (iii) a line end. For the
4911 second case, IT->nglyphs - 1 padding glyphs will be present
4912 (on X frames, there is only one glyph produced for a
4913 composite character.
4915 The behavior implemented below means, for continuation lines,
4916 that as many spaces of a TAB as fit on the current line are
4917 displayed there. For terminal frames, as many glyphs of a
4918 multi-glyph character are displayed in the current line, too.
4919 This is what the old redisplay code did, and we keep it that
4920 way. Under X, the whole shape of a complex character must
4921 fit on the line or it will be completely displayed in the
4922 next line.
4924 Note that both for tabs and padding glyphs, all glyphs have
4925 the same width. */
4926 if (it->nglyphs)
4928 /* More than one glyph or glyph doesn't fit on line. All
4929 glyphs have the same width. */
4930 int single_glyph_width = it->pixel_width / it->nglyphs;
4931 int new_x;
4933 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4935 new_x = x + single_glyph_width;
4937 /* We want to leave anything reaching TO_X to the caller. */
4938 if ((op & MOVE_TO_X) && new_x > to_x)
4940 it->current_x = x;
4941 result = MOVE_X_REACHED;
4942 break;
4944 else if (/* Lines are continued. */
4945 !it->truncate_lines_p
4946 && (/* And glyph doesn't fit on the line. */
4947 new_x > it->last_visible_x
4948 /* Or it fits exactly and we're on a window
4949 system frame. */
4950 || (new_x == it->last_visible_x
4951 && FRAME_WINDOW_P (it->f))))
4953 if (/* IT->hpos == 0 means the very first glyph
4954 doesn't fit on the line, e.g. a wide image. */
4955 it->hpos == 0
4956 || (new_x == it->last_visible_x
4957 && FRAME_WINDOW_P (it->f)))
4959 ++it->hpos;
4960 it->current_x = new_x;
4961 if (i == it->nglyphs - 1)
4962 set_iterator_to_next (it, 1);
4964 else
4966 it->current_x = x;
4967 it->max_ascent = ascent;
4968 it->max_descent = descent;
4971 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4972 IT_CHARPOS (*it)));
4973 result = MOVE_LINE_CONTINUED;
4974 break;
4976 else if (new_x > it->first_visible_x)
4978 /* Glyph is visible. Increment number of glyphs that
4979 would be displayed. */
4980 ++it->hpos;
4982 else
4984 /* Glyph is completely off the left margin of the display
4985 area. Nothing to do. */
4989 if (result != MOVE_UNDEFINED)
4990 break;
4992 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4994 /* Stop when TO_X specified and reached. This check is
4995 necessary here because of lines consisting of a line end,
4996 only. The line end will not produce any glyphs and we
4997 would never get MOVE_X_REACHED. */
4998 xassert (it->nglyphs == 0);
4999 result = MOVE_X_REACHED;
5000 break;
5003 /* Is this a line end? If yes, we're done. */
5004 if (ITERATOR_AT_END_OF_LINE_P (it))
5006 result = MOVE_NEWLINE_OR_CR;
5007 break;
5010 /* The current display element has been consumed. Advance
5011 to the next. */
5012 set_iterator_to_next (it, 1);
5014 /* Stop if lines are truncated and IT's current x-position is
5015 past the right edge of the window now. */
5016 if (it->truncate_lines_p
5017 && it->current_x >= it->last_visible_x)
5019 result = MOVE_LINE_TRUNCATED;
5020 break;
5024 /* Restore the iterator settings altered at the beginning of this
5025 function. */
5026 it->glyph_row = saved_glyph_row;
5027 return result;
5031 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5032 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5033 the description of enum move_operation_enum.
5035 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5036 screen line, this function will set IT to the next position >
5037 TO_CHARPOS. */
5039 void
5040 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5041 struct it *it;
5042 int to_charpos, to_x, to_y, to_vpos;
5043 int op;
5045 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5046 int line_height;
5047 int reached = 0;
5049 for (;;)
5051 if (op & MOVE_TO_VPOS)
5053 /* If no TO_CHARPOS and no TO_X specified, stop at the
5054 start of the line TO_VPOS. */
5055 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5057 if (it->vpos == to_vpos)
5059 reached = 1;
5060 break;
5062 else
5063 skip = move_it_in_display_line_to (it, -1, -1, 0);
5065 else
5067 /* TO_VPOS >= 0 means stop at TO_X in the line at
5068 TO_VPOS, or at TO_POS, whichever comes first. */
5069 if (it->vpos == to_vpos)
5071 reached = 2;
5072 break;
5075 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5077 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5079 reached = 3;
5080 break;
5082 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5084 /* We have reached TO_X but not in the line we want. */
5085 skip = move_it_in_display_line_to (it, to_charpos,
5086 -1, MOVE_TO_POS);
5087 if (skip == MOVE_POS_MATCH_OR_ZV)
5089 reached = 4;
5090 break;
5095 else if (op & MOVE_TO_Y)
5097 struct it it_backup;
5099 /* TO_Y specified means stop at TO_X in the line containing
5100 TO_Y---or at TO_CHARPOS if this is reached first. The
5101 problem is that we can't really tell whether the line
5102 contains TO_Y before we have completely scanned it, and
5103 this may skip past TO_X. What we do is to first scan to
5104 TO_X.
5106 If TO_X is not specified, use a TO_X of zero. The reason
5107 is to make the outcome of this function more predictable.
5108 If we didn't use TO_X == 0, we would stop at the end of
5109 the line which is probably not what a caller would expect
5110 to happen. */
5111 skip = move_it_in_display_line_to (it, to_charpos,
5112 ((op & MOVE_TO_X)
5113 ? to_x : 0),
5114 (MOVE_TO_X
5115 | (op & MOVE_TO_POS)));
5117 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5118 if (skip == MOVE_POS_MATCH_OR_ZV)
5120 reached = 5;
5121 break;
5124 /* If TO_X was reached, we would like to know whether TO_Y
5125 is in the line. This can only be said if we know the
5126 total line height which requires us to scan the rest of
5127 the line. */
5128 if (skip == MOVE_X_REACHED)
5130 it_backup = *it;
5131 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5132 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5133 op & MOVE_TO_POS);
5134 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5137 /* Now, decide whether TO_Y is in this line. */
5138 line_height = it->max_ascent + it->max_descent;
5139 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5141 if (to_y >= it->current_y
5142 && to_y < it->current_y + line_height)
5144 if (skip == MOVE_X_REACHED)
5145 /* If TO_Y is in this line and TO_X was reached above,
5146 we scanned too far. We have to restore IT's settings
5147 to the ones before skipping. */
5148 *it = it_backup;
5149 reached = 6;
5151 else if (skip == MOVE_X_REACHED)
5153 skip = skip2;
5154 if (skip == MOVE_POS_MATCH_OR_ZV)
5155 reached = 7;
5158 if (reached)
5159 break;
5161 else
5162 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5164 switch (skip)
5166 case MOVE_POS_MATCH_OR_ZV:
5167 reached = 8;
5168 goto out;
5170 case MOVE_NEWLINE_OR_CR:
5171 set_iterator_to_next (it, 1);
5172 it->continuation_lines_width = 0;
5173 break;
5175 case MOVE_LINE_TRUNCATED:
5176 it->continuation_lines_width = 0;
5177 reseat_at_next_visible_line_start (it, 0);
5178 if ((op & MOVE_TO_POS) != 0
5179 && IT_CHARPOS (*it) > to_charpos)
5181 reached = 9;
5182 goto out;
5184 break;
5186 case MOVE_LINE_CONTINUED:
5187 it->continuation_lines_width += it->current_x;
5188 break;
5190 default:
5191 abort ();
5194 /* Reset/increment for the next run. */
5195 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5196 it->current_x = it->hpos = 0;
5197 it->current_y += it->max_ascent + it->max_descent;
5198 ++it->vpos;
5199 last_height = it->max_ascent + it->max_descent;
5200 last_max_ascent = it->max_ascent;
5201 it->max_ascent = it->max_descent = 0;
5204 out:
5206 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5210 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5212 If DY > 0, move IT backward at least that many pixels. DY = 0
5213 means move IT backward to the preceding line start or BEGV. This
5214 function may move over more than DY pixels if IT->current_y - DY
5215 ends up in the middle of a line; in this case IT->current_y will be
5216 set to the top of the line moved to. */
5218 void
5219 move_it_vertically_backward (it, dy)
5220 struct it *it;
5221 int dy;
5223 int nlines, h, line_height;
5224 struct it it2;
5225 int start_pos = IT_CHARPOS (*it);
5227 xassert (dy >= 0);
5229 /* Estimate how many newlines we must move back. */
5230 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5232 /* Set the iterator's position that many lines back. */
5233 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5234 back_to_previous_visible_line_start (it);
5236 /* Reseat the iterator here. When moving backward, we don't want
5237 reseat to skip forward over invisible text, set up the iterator
5238 to deliver from overlay strings at the new position etc. So,
5239 use reseat_1 here. */
5240 reseat_1 (it, it->current.pos, 1);
5242 /* We are now surely at a line start. */
5243 it->current_x = it->hpos = 0;
5245 /* Move forward and see what y-distance we moved. First move to the
5246 start of the next line so that we get its height. We need this
5247 height to be able to tell whether we reached the specified
5248 y-distance. */
5249 it2 = *it;
5250 it2.max_ascent = it2.max_descent = 0;
5251 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5252 MOVE_TO_POS | MOVE_TO_VPOS);
5253 xassert (IT_CHARPOS (*it) >= BEGV);
5254 line_height = it2.max_ascent + it2.max_descent;
5256 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5257 xassert (IT_CHARPOS (*it) >= BEGV);
5258 h = it2.current_y - it->current_y;
5259 nlines = it2.vpos - it->vpos;
5261 /* Correct IT's y and vpos position. */
5262 it->vpos -= nlines;
5263 it->current_y -= h;
5265 if (dy == 0)
5267 /* DY == 0 means move to the start of the screen line. The
5268 value of nlines is > 0 if continuation lines were involved. */
5269 if (nlines > 0)
5270 move_it_by_lines (it, nlines, 1);
5271 xassert (IT_CHARPOS (*it) <= start_pos);
5273 else if (nlines)
5275 /* The y-position we try to reach. Note that h has been
5276 subtracted in front of the if-statement. */
5277 int target_y = it->current_y + h - dy;
5279 /* If we did not reach target_y, try to move further backward if
5280 we can. If we moved too far backward, try to move forward. */
5281 if (target_y < it->current_y
5282 && IT_CHARPOS (*it) > BEGV)
5284 move_it_vertically (it, target_y - it->current_y);
5285 xassert (IT_CHARPOS (*it) >= BEGV);
5287 else if (target_y >= it->current_y + line_height
5288 && IT_CHARPOS (*it) < ZV)
5290 move_it_vertically (it, target_y - (it->current_y + line_height));
5291 xassert (IT_CHARPOS (*it) >= BEGV);
5297 /* Move IT by a specified amount of pixel lines DY. DY negative means
5298 move backwards. DY = 0 means move to start of screen line. At the
5299 end, IT will be on the start of a screen line. */
5301 void
5302 move_it_vertically (it, dy)
5303 struct it *it;
5304 int dy;
5306 if (dy <= 0)
5307 move_it_vertically_backward (it, -dy);
5308 else if (dy > 0)
5310 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5311 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5312 MOVE_TO_POS | MOVE_TO_Y);
5313 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5315 /* If buffer ends in ZV without a newline, move to the start of
5316 the line to satisfy the post-condition. */
5317 if (IT_CHARPOS (*it) == ZV
5318 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5319 move_it_by_lines (it, 0, 0);
5324 /* Move iterator IT past the end of the text line it is in. */
5326 void
5327 move_it_past_eol (it)
5328 struct it *it;
5330 enum move_it_result rc;
5332 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5333 if (rc == MOVE_NEWLINE_OR_CR)
5334 set_iterator_to_next (it, 0);
5338 #if 0 /* Currently not used. */
5340 /* Return non-zero if some text between buffer positions START_CHARPOS
5341 and END_CHARPOS is invisible. IT->window is the window for text
5342 property lookup. */
5344 static int
5345 invisible_text_between_p (it, start_charpos, end_charpos)
5346 struct it *it;
5347 int start_charpos, end_charpos;
5349 Lisp_Object prop, limit;
5350 int invisible_found_p;
5352 xassert (it != NULL && start_charpos <= end_charpos);
5354 /* Is text at START invisible? */
5355 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5356 it->window);
5357 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5358 invisible_found_p = 1;
5359 else
5361 limit = Fnext_single_char_property_change (make_number (start_charpos),
5362 Qinvisible, Qnil,
5363 make_number (end_charpos));
5364 invisible_found_p = XFASTINT (limit) < end_charpos;
5367 return invisible_found_p;
5370 #endif /* 0 */
5373 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5374 negative means move up. DVPOS == 0 means move to the start of the
5375 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5376 NEED_Y_P is zero, IT->current_y will be left unchanged.
5378 Further optimization ideas: If we would know that IT->f doesn't use
5379 a face with proportional font, we could be faster for
5380 truncate-lines nil. */
5382 void
5383 move_it_by_lines (it, dvpos, need_y_p)
5384 struct it *it;
5385 int dvpos, need_y_p;
5387 struct position pos;
5389 if (!FRAME_WINDOW_P (it->f))
5391 struct text_pos textpos;
5393 /* We can use vmotion on frames without proportional fonts. */
5394 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5395 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5396 reseat (it, textpos, 1);
5397 it->vpos += pos.vpos;
5398 it->current_y += pos.vpos;
5400 else if (dvpos == 0)
5402 /* DVPOS == 0 means move to the start of the screen line. */
5403 move_it_vertically_backward (it, 0);
5404 xassert (it->current_x == 0 && it->hpos == 0);
5406 else if (dvpos > 0)
5407 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5408 else
5410 struct it it2;
5411 int start_charpos, i;
5413 /* Go back -DVPOS visible lines and reseat the iterator there. */
5414 start_charpos = IT_CHARPOS (*it);
5415 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5416 back_to_previous_visible_line_start (it);
5417 reseat (it, it->current.pos, 1);
5418 it->current_x = it->hpos = 0;
5420 /* Above call may have moved too far if continuation lines
5421 are involved. Scan forward and see if it did. */
5422 it2 = *it;
5423 it2.vpos = it2.current_y = 0;
5424 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5425 it->vpos -= it2.vpos;
5426 it->current_y -= it2.current_y;
5427 it->current_x = it->hpos = 0;
5429 /* If we moved too far, move IT some lines forward. */
5430 if (it2.vpos > -dvpos)
5432 int delta = it2.vpos + dvpos;
5433 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5440 /***********************************************************************
5441 Messages
5442 ***********************************************************************/
5445 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5446 to *Messages*. */
5448 void
5449 add_to_log (format, arg1, arg2)
5450 char *format;
5451 Lisp_Object arg1, arg2;
5453 Lisp_Object args[3];
5454 Lisp_Object msg, fmt;
5455 char *buffer;
5456 int len;
5457 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5459 fmt = msg = Qnil;
5460 GCPRO4 (fmt, msg, arg1, arg2);
5462 args[0] = fmt = build_string (format);
5463 args[1] = arg1;
5464 args[2] = arg2;
5465 msg = Fformat (3, args);
5467 len = STRING_BYTES (XSTRING (msg)) + 1;
5468 buffer = (char *) alloca (len);
5469 strcpy (buffer, XSTRING (msg)->data);
5471 message_dolog (buffer, len - 1, 1, 0);
5472 UNGCPRO;
5476 /* Output a newline in the *Messages* buffer if "needs" one. */
5478 void
5479 message_log_maybe_newline ()
5481 if (message_log_need_newline)
5482 message_dolog ("", 0, 1, 0);
5486 /* Add a string M of length NBYTES to the message log, optionally
5487 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5488 nonzero, means interpret the contents of M as multibyte. This
5489 function calls low-level routines in order to bypass text property
5490 hooks, etc. which might not be safe to run. */
5492 void
5493 message_dolog (m, nbytes, nlflag, multibyte)
5494 char *m;
5495 int nbytes, nlflag, multibyte;
5497 if (!NILP (Vmessage_log_max))
5499 struct buffer *oldbuf;
5500 Lisp_Object oldpoint, oldbegv, oldzv;
5501 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5502 int point_at_end = 0;
5503 int zv_at_end = 0;
5504 Lisp_Object old_deactivate_mark, tem;
5505 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5507 old_deactivate_mark = Vdeactivate_mark;
5508 oldbuf = current_buffer;
5509 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5510 current_buffer->undo_list = Qt;
5512 oldpoint = Fpoint_marker ();
5513 oldbegv = Fpoint_min_marker ();
5514 oldzv = Fpoint_max_marker ();
5515 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5517 if (PT == Z)
5518 point_at_end = 1;
5519 if (ZV == Z)
5520 zv_at_end = 1;
5522 BEGV = BEG;
5523 BEGV_BYTE = BEG_BYTE;
5524 ZV = Z;
5525 ZV_BYTE = Z_BYTE;
5526 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5528 /* Insert the string--maybe converting multibyte to single byte
5529 or vice versa, so that all the text fits the buffer. */
5530 if (multibyte
5531 && NILP (current_buffer->enable_multibyte_characters))
5533 int i, c, char_bytes;
5534 unsigned char work[1];
5536 /* Convert a multibyte string to single-byte
5537 for the *Message* buffer. */
5538 for (i = 0; i < nbytes; i += nbytes)
5540 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5541 work[0] = (SINGLE_BYTE_CHAR_P (c)
5543 : multibyte_char_to_unibyte (c, Qnil));
5544 insert_1_both (work, 1, 1, 1, 0, 0);
5547 else if (! multibyte
5548 && ! NILP (current_buffer->enable_multibyte_characters))
5550 int i, c, char_bytes;
5551 unsigned char *msg = (unsigned char *) m;
5552 unsigned char str[MAX_MULTIBYTE_LENGTH];
5553 /* Convert a single-byte string to multibyte
5554 for the *Message* buffer. */
5555 for (i = 0; i < nbytes; i++)
5557 c = unibyte_char_to_multibyte (msg[i]);
5558 char_bytes = CHAR_STRING (c, str);
5559 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5562 else if (nbytes)
5563 insert_1 (m, nbytes, 1, 0, 0);
5565 if (nlflag)
5567 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5568 insert_1 ("\n", 1, 1, 0, 0);
5570 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5571 this_bol = PT;
5572 this_bol_byte = PT_BYTE;
5574 if (this_bol > BEG)
5576 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5577 prev_bol = PT;
5578 prev_bol_byte = PT_BYTE;
5580 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5581 this_bol, this_bol_byte);
5582 if (dup)
5584 del_range_both (prev_bol, prev_bol_byte,
5585 this_bol, this_bol_byte, 0);
5586 if (dup > 1)
5588 char dupstr[40];
5589 int duplen;
5591 /* If you change this format, don't forget to also
5592 change message_log_check_duplicate. */
5593 sprintf (dupstr, " [%d times]", dup);
5594 duplen = strlen (dupstr);
5595 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5596 insert_1 (dupstr, duplen, 1, 0, 1);
5601 if (NATNUMP (Vmessage_log_max))
5603 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5604 -XFASTINT (Vmessage_log_max) - 1, 0);
5605 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5608 BEGV = XMARKER (oldbegv)->charpos;
5609 BEGV_BYTE = marker_byte_position (oldbegv);
5611 if (zv_at_end)
5613 ZV = Z;
5614 ZV_BYTE = Z_BYTE;
5616 else
5618 ZV = XMARKER (oldzv)->charpos;
5619 ZV_BYTE = marker_byte_position (oldzv);
5622 if (point_at_end)
5623 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5624 else
5625 /* We can't do Fgoto_char (oldpoint) because it will run some
5626 Lisp code. */
5627 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5628 XMARKER (oldpoint)->bytepos);
5630 UNGCPRO;
5631 free_marker (oldpoint);
5632 free_marker (oldbegv);
5633 free_marker (oldzv);
5635 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5636 set_buffer_internal (oldbuf);
5637 if (NILP (tem))
5638 windows_or_buffers_changed = old_windows_or_buffers_changed;
5639 message_log_need_newline = !nlflag;
5640 Vdeactivate_mark = old_deactivate_mark;
5645 /* We are at the end of the buffer after just having inserted a newline.
5646 (Note: We depend on the fact we won't be crossing the gap.)
5647 Check to see if the most recent message looks a lot like the previous one.
5648 Return 0 if different, 1 if the new one should just replace it, or a
5649 value N > 1 if we should also append " [N times]". */
5651 static int
5652 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5653 int prev_bol, this_bol;
5654 int prev_bol_byte, this_bol_byte;
5656 int i;
5657 int len = Z_BYTE - 1 - this_bol_byte;
5658 int seen_dots = 0;
5659 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5660 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5662 for (i = 0; i < len; i++)
5664 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5665 seen_dots = 1;
5666 if (p1[i] != p2[i])
5667 return seen_dots;
5669 p1 += len;
5670 if (*p1 == '\n')
5671 return 2;
5672 if (*p1++ == ' ' && *p1++ == '[')
5674 int n = 0;
5675 while (*p1 >= '0' && *p1 <= '9')
5676 n = n * 10 + *p1++ - '0';
5677 if (strncmp (p1, " times]\n", 8) == 0)
5678 return n+1;
5680 return 0;
5684 /* Display an echo area message M with a specified length of NBYTES
5685 bytes. The string may include null characters. If M is 0, clear
5686 out any existing message, and let the mini-buffer text show
5687 through.
5689 The buffer M must continue to exist until after the echo area gets
5690 cleared or some other message gets displayed there. This means do
5691 not pass text that is stored in a Lisp string; do not pass text in
5692 a buffer that was alloca'd. */
5694 void
5695 message2 (m, nbytes, multibyte)
5696 char *m;
5697 int nbytes;
5698 int multibyte;
5700 /* First flush out any partial line written with print. */
5701 message_log_maybe_newline ();
5702 if (m)
5703 message_dolog (m, nbytes, 1, multibyte);
5704 message2_nolog (m, nbytes, multibyte);
5708 /* The non-logging counterpart of message2. */
5710 void
5711 message2_nolog (m, nbytes, multibyte)
5712 char *m;
5713 int nbytes;
5715 struct frame *sf = SELECTED_FRAME ();
5716 message_enable_multibyte = multibyte;
5718 if (noninteractive)
5720 if (noninteractive_need_newline)
5721 putc ('\n', stderr);
5722 noninteractive_need_newline = 0;
5723 if (m)
5724 fwrite (m, nbytes, 1, stderr);
5725 if (cursor_in_echo_area == 0)
5726 fprintf (stderr, "\n");
5727 fflush (stderr);
5729 /* A null message buffer means that the frame hasn't really been
5730 initialized yet. Error messages get reported properly by
5731 cmd_error, so this must be just an informative message; toss it. */
5732 else if (INTERACTIVE
5733 && sf->glyphs_initialized_p
5734 && FRAME_MESSAGE_BUF (sf))
5736 Lisp_Object mini_window;
5737 struct frame *f;
5739 /* Get the frame containing the mini-buffer
5740 that the selected frame is using. */
5741 mini_window = FRAME_MINIBUF_WINDOW (sf);
5742 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5744 FRAME_SAMPLE_VISIBILITY (f);
5745 if (FRAME_VISIBLE_P (sf)
5746 && ! FRAME_VISIBLE_P (f))
5747 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5749 if (m)
5751 set_message (m, Qnil, nbytes, multibyte);
5752 if (minibuffer_auto_raise)
5753 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5755 else
5756 clear_message (1, 1);
5758 do_pending_window_change (0);
5759 echo_area_display (1);
5760 do_pending_window_change (0);
5761 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5762 (*frame_up_to_date_hook) (f);
5767 /* Display an echo area message M with a specified length of NBYTES
5768 bytes. The string may include null characters. If M is not a
5769 string, clear out any existing message, and let the mini-buffer
5770 text show through. */
5772 void
5773 message3 (m, nbytes, multibyte)
5774 Lisp_Object m;
5775 int nbytes;
5776 int multibyte;
5778 struct gcpro gcpro1;
5780 GCPRO1 (m);
5782 /* First flush out any partial line written with print. */
5783 message_log_maybe_newline ();
5784 if (STRINGP (m))
5785 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5786 message3_nolog (m, nbytes, multibyte);
5788 UNGCPRO;
5792 /* The non-logging version of message3. */
5794 void
5795 message3_nolog (m, nbytes, multibyte)
5796 Lisp_Object m;
5797 int nbytes, multibyte;
5799 struct frame *sf = SELECTED_FRAME ();
5800 message_enable_multibyte = multibyte;
5802 if (noninteractive)
5804 if (noninteractive_need_newline)
5805 putc ('\n', stderr);
5806 noninteractive_need_newline = 0;
5807 if (STRINGP (m))
5808 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5809 if (cursor_in_echo_area == 0)
5810 fprintf (stderr, "\n");
5811 fflush (stderr);
5813 /* A null message buffer means that the frame hasn't really been
5814 initialized yet. Error messages get reported properly by
5815 cmd_error, so this must be just an informative message; toss it. */
5816 else if (INTERACTIVE
5817 && sf->glyphs_initialized_p
5818 && FRAME_MESSAGE_BUF (sf))
5820 Lisp_Object mini_window;
5821 Lisp_Object frame;
5822 struct frame *f;
5824 /* Get the frame containing the mini-buffer
5825 that the selected frame is using. */
5826 mini_window = FRAME_MINIBUF_WINDOW (sf);
5827 frame = XWINDOW (mini_window)->frame;
5828 f = XFRAME (frame);
5830 FRAME_SAMPLE_VISIBILITY (f);
5831 if (FRAME_VISIBLE_P (sf)
5832 && !FRAME_VISIBLE_P (f))
5833 Fmake_frame_visible (frame);
5835 if (STRINGP (m) && XSTRING (m)->size)
5837 set_message (NULL, m, nbytes, multibyte);
5838 if (minibuffer_auto_raise)
5839 Fraise_frame (frame);
5841 else
5842 clear_message (1, 1);
5844 do_pending_window_change (0);
5845 echo_area_display (1);
5846 do_pending_window_change (0);
5847 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5848 (*frame_up_to_date_hook) (f);
5853 /* Display a null-terminated echo area message M. If M is 0, clear
5854 out any existing message, and let the mini-buffer text show through.
5856 The buffer M must continue to exist until after the echo area gets
5857 cleared or some other message gets displayed there. Do not pass
5858 text that is stored in a Lisp string. Do not pass text in a buffer
5859 that was alloca'd. */
5861 void
5862 message1 (m)
5863 char *m;
5865 message2 (m, (m ? strlen (m) : 0), 0);
5869 /* The non-logging counterpart of message1. */
5871 void
5872 message1_nolog (m)
5873 char *m;
5875 message2_nolog (m, (m ? strlen (m) : 0), 0);
5878 /* Display a message M which contains a single %s
5879 which gets replaced with STRING. */
5881 void
5882 message_with_string (m, string, log)
5883 char *m;
5884 Lisp_Object string;
5885 int log;
5887 if (noninteractive)
5889 if (m)
5891 if (noninteractive_need_newline)
5892 putc ('\n', stderr);
5893 noninteractive_need_newline = 0;
5894 fprintf (stderr, m, XSTRING (string)->data);
5895 if (cursor_in_echo_area == 0)
5896 fprintf (stderr, "\n");
5897 fflush (stderr);
5900 else if (INTERACTIVE)
5902 /* The frame whose minibuffer we're going to display the message on.
5903 It may be larger than the selected frame, so we need
5904 to use its buffer, not the selected frame's buffer. */
5905 Lisp_Object mini_window;
5906 struct frame *f, *sf = SELECTED_FRAME ();
5908 /* Get the frame containing the minibuffer
5909 that the selected frame is using. */
5910 mini_window = FRAME_MINIBUF_WINDOW (sf);
5911 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5913 /* A null message buffer means that the frame hasn't really been
5914 initialized yet. Error messages get reported properly by
5915 cmd_error, so this must be just an informative message; toss it. */
5916 if (FRAME_MESSAGE_BUF (f))
5918 int len;
5919 char *a[1];
5920 a[0] = (char *) XSTRING (string)->data;
5922 len = doprnt (FRAME_MESSAGE_BUF (f),
5923 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5925 if (log)
5926 message2 (FRAME_MESSAGE_BUF (f), len,
5927 STRING_MULTIBYTE (string));
5928 else
5929 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5930 STRING_MULTIBYTE (string));
5932 /* Print should start at the beginning of the message
5933 buffer next time. */
5934 message_buf_print = 0;
5940 /* Dump an informative message to the minibuf. If M is 0, clear out
5941 any existing message, and let the mini-buffer text show through. */
5943 /* VARARGS 1 */
5944 void
5945 message (m, a1, a2, a3)
5946 char *m;
5947 EMACS_INT a1, a2, a3;
5949 if (noninteractive)
5951 if (m)
5953 if (noninteractive_need_newline)
5954 putc ('\n', stderr);
5955 noninteractive_need_newline = 0;
5956 fprintf (stderr, m, a1, a2, a3);
5957 if (cursor_in_echo_area == 0)
5958 fprintf (stderr, "\n");
5959 fflush (stderr);
5962 else if (INTERACTIVE)
5964 /* The frame whose mini-buffer we're going to display the message
5965 on. It may be larger than the selected frame, so we need to
5966 use its buffer, not the selected frame's buffer. */
5967 Lisp_Object mini_window;
5968 struct frame *f, *sf = SELECTED_FRAME ();
5970 /* Get the frame containing the mini-buffer
5971 that the selected frame is using. */
5972 mini_window = FRAME_MINIBUF_WINDOW (sf);
5973 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5975 /* A null message buffer means that the frame hasn't really been
5976 initialized yet. Error messages get reported properly by
5977 cmd_error, so this must be just an informative message; toss
5978 it. */
5979 if (FRAME_MESSAGE_BUF (f))
5981 if (m)
5983 int len;
5984 #ifdef NO_ARG_ARRAY
5985 char *a[3];
5986 a[0] = (char *) a1;
5987 a[1] = (char *) a2;
5988 a[2] = (char *) a3;
5990 len = doprnt (FRAME_MESSAGE_BUF (f),
5991 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5992 #else
5993 len = doprnt (FRAME_MESSAGE_BUF (f),
5994 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5995 (char **) &a1);
5996 #endif /* NO_ARG_ARRAY */
5998 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6000 else
6001 message1 (0);
6003 /* Print should start at the beginning of the message
6004 buffer next time. */
6005 message_buf_print = 0;
6011 /* The non-logging version of message. */
6013 void
6014 message_nolog (m, a1, a2, a3)
6015 char *m;
6016 EMACS_INT a1, a2, a3;
6018 Lisp_Object old_log_max;
6019 old_log_max = Vmessage_log_max;
6020 Vmessage_log_max = Qnil;
6021 message (m, a1, a2, a3);
6022 Vmessage_log_max = old_log_max;
6026 /* Display the current message in the current mini-buffer. This is
6027 only called from error handlers in process.c, and is not time
6028 critical. */
6030 void
6031 update_echo_area ()
6033 if (!NILP (echo_area_buffer[0]))
6035 Lisp_Object string;
6036 string = Fcurrent_message ();
6037 message3 (string, XSTRING (string)->size,
6038 !NILP (current_buffer->enable_multibyte_characters));
6043 /* Make sure echo area buffers in echo_buffers[] are life. If they
6044 aren't, make new ones. */
6046 static void
6047 ensure_echo_area_buffers ()
6049 int i;
6051 for (i = 0; i < 2; ++i)
6052 if (!BUFFERP (echo_buffer[i])
6053 || NILP (XBUFFER (echo_buffer[i])->name))
6055 char name[30];
6056 Lisp_Object old_buffer;
6057 int j;
6059 old_buffer = echo_buffer[i];
6060 sprintf (name, " *Echo Area %d*", i);
6061 echo_buffer[i] = Fget_buffer_create (build_string (name));
6062 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6064 for (j = 0; j < 2; ++j)
6065 if (EQ (old_buffer, echo_area_buffer[j]))
6066 echo_area_buffer[j] = echo_buffer[i];
6071 /* Call FN with args A1..A4 with either the current or last displayed
6072 echo_area_buffer as current buffer.
6074 WHICH zero means use the current message buffer
6075 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6076 from echo_buffer[] and clear it.
6078 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6079 suitable buffer from echo_buffer[] and clear it.
6081 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6082 that the current message becomes the last displayed one, make
6083 choose a suitable buffer for echo_area_buffer[0], and clear it.
6085 Value is what FN returns. */
6087 static int
6088 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6089 struct window *w;
6090 int which;
6091 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6092 EMACS_INT a1;
6093 Lisp_Object a2;
6094 EMACS_INT a3, a4;
6096 Lisp_Object buffer;
6097 int this_one, the_other, clear_buffer_p, rc;
6098 int count = BINDING_STACK_SIZE ();
6100 /* If buffers aren't life, make new ones. */
6101 ensure_echo_area_buffers ();
6103 clear_buffer_p = 0;
6105 if (which == 0)
6106 this_one = 0, the_other = 1;
6107 else if (which > 0)
6108 this_one = 1, the_other = 0;
6109 else
6111 this_one = 0, the_other = 1;
6112 clear_buffer_p = 1;
6114 /* We need a fresh one in case the current echo buffer equals
6115 the one containing the last displayed echo area message. */
6116 if (!NILP (echo_area_buffer[this_one])
6117 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6118 echo_area_buffer[this_one] = Qnil;
6121 /* Choose a suitable buffer from echo_buffer[] is we don't
6122 have one. */
6123 if (NILP (echo_area_buffer[this_one]))
6125 echo_area_buffer[this_one]
6126 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6127 ? echo_buffer[the_other]
6128 : echo_buffer[this_one]);
6129 clear_buffer_p = 1;
6132 buffer = echo_area_buffer[this_one];
6134 record_unwind_protect (unwind_with_echo_area_buffer,
6135 with_echo_area_buffer_unwind_data (w));
6137 /* Make the echo area buffer current. Note that for display
6138 purposes, it is not necessary that the displayed window's buffer
6139 == current_buffer, except for text property lookup. So, let's
6140 only set that buffer temporarily here without doing a full
6141 Fset_window_buffer. We must also change w->pointm, though,
6142 because otherwise an assertions in unshow_buffer fails, and Emacs
6143 aborts. */
6144 set_buffer_internal_1 (XBUFFER (buffer));
6145 if (w)
6147 w->buffer = buffer;
6148 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6151 current_buffer->undo_list = Qt;
6152 current_buffer->read_only = Qnil;
6153 specbind (Qinhibit_read_only, Qt);
6155 if (clear_buffer_p && Z > BEG)
6156 del_range (BEG, Z);
6158 xassert (BEGV >= BEG);
6159 xassert (ZV <= Z && ZV >= BEGV);
6161 rc = fn (a1, a2, a3, a4);
6163 xassert (BEGV >= BEG);
6164 xassert (ZV <= Z && ZV >= BEGV);
6166 unbind_to (count, Qnil);
6167 return rc;
6171 /* Save state that should be preserved around the call to the function
6172 FN called in with_echo_area_buffer. */
6174 static Lisp_Object
6175 with_echo_area_buffer_unwind_data (w)
6176 struct window *w;
6178 int i = 0;
6179 Lisp_Object vector;
6181 /* Reduce consing by keeping one vector in
6182 Vwith_echo_area_save_vector. */
6183 vector = Vwith_echo_area_save_vector;
6184 Vwith_echo_area_save_vector = Qnil;
6186 if (NILP (vector))
6187 vector = Fmake_vector (make_number (7), Qnil);
6189 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6190 AREF (vector, i) = Vdeactivate_mark, ++i;
6191 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6193 if (w)
6195 XSETWINDOW (AREF (vector, i), w); ++i;
6196 AREF (vector, i) = w->buffer; ++i;
6197 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6198 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6200 else
6202 int end = i + 4;
6203 for (; i < end; ++i)
6204 AREF (vector, i) = Qnil;
6207 xassert (i == ASIZE (vector));
6208 return vector;
6212 /* Restore global state from VECTOR which was created by
6213 with_echo_area_buffer_unwind_data. */
6215 static Lisp_Object
6216 unwind_with_echo_area_buffer (vector)
6217 Lisp_Object vector;
6219 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6220 Vdeactivate_mark = AREF (vector, 1);
6221 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6223 if (WINDOWP (AREF (vector, 3)))
6225 struct window *w;
6226 Lisp_Object buffer, charpos, bytepos;
6228 w = XWINDOW (AREF (vector, 3));
6229 buffer = AREF (vector, 4);
6230 charpos = AREF (vector, 5);
6231 bytepos = AREF (vector, 6);
6233 w->buffer = buffer;
6234 set_marker_both (w->pointm, buffer,
6235 XFASTINT (charpos), XFASTINT (bytepos));
6238 Vwith_echo_area_save_vector = vector;
6239 return Qnil;
6243 /* Set up the echo area for use by print functions. MULTIBYTE_P
6244 non-zero means we will print multibyte. */
6246 void
6247 setup_echo_area_for_printing (multibyte_p)
6248 int multibyte_p;
6250 ensure_echo_area_buffers ();
6252 if (!message_buf_print)
6254 /* A message has been output since the last time we printed.
6255 Choose a fresh echo area buffer. */
6256 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6257 echo_area_buffer[0] = echo_buffer[1];
6258 else
6259 echo_area_buffer[0] = echo_buffer[0];
6261 /* Switch to that buffer and clear it. */
6262 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6263 current_buffer->truncate_lines = Qnil;
6265 if (Z > BEG)
6267 int count = BINDING_STACK_SIZE ();
6268 specbind (Qinhibit_read_only, Qt);
6269 del_range (BEG, Z);
6270 unbind_to (count, Qnil);
6272 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6274 /* Set up the buffer for the multibyteness we need. */
6275 if (multibyte_p
6276 != !NILP (current_buffer->enable_multibyte_characters))
6277 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6279 /* Raise the frame containing the echo area. */
6280 if (minibuffer_auto_raise)
6282 struct frame *sf = SELECTED_FRAME ();
6283 Lisp_Object mini_window;
6284 mini_window = FRAME_MINIBUF_WINDOW (sf);
6285 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6288 message_log_maybe_newline ();
6289 message_buf_print = 1;
6291 else
6293 if (NILP (echo_area_buffer[0]))
6295 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6296 echo_area_buffer[0] = echo_buffer[1];
6297 else
6298 echo_area_buffer[0] = echo_buffer[0];
6301 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6303 /* Someone switched buffers between print requests. */
6304 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6305 current_buffer->truncate_lines = Qnil;
6311 /* Display an echo area message in window W. Value is non-zero if W's
6312 height is changed. If display_last_displayed_message_p is
6313 non-zero, display the message that was last displayed, otherwise
6314 display the current message. */
6316 static int
6317 display_echo_area (w)
6318 struct window *w;
6320 int i, no_message_p, window_height_changed_p, count;
6322 /* Temporarily disable garbage collections while displaying the echo
6323 area. This is done because a GC can print a message itself.
6324 That message would modify the echo area buffer's contents while a
6325 redisplay of the buffer is going on, and seriously confuse
6326 redisplay. */
6327 count = inhibit_garbage_collection ();
6329 /* If there is no message, we must call display_echo_area_1
6330 nevertheless because it resizes the window. But we will have to
6331 reset the echo_area_buffer in question to nil at the end because
6332 with_echo_area_buffer will sets it to an empty buffer. */
6333 i = display_last_displayed_message_p ? 1 : 0;
6334 no_message_p = NILP (echo_area_buffer[i]);
6336 window_height_changed_p
6337 = with_echo_area_buffer (w, display_last_displayed_message_p,
6338 display_echo_area_1,
6339 (EMACS_INT) w, Qnil, 0, 0);
6341 if (no_message_p)
6342 echo_area_buffer[i] = Qnil;
6344 unbind_to (count, Qnil);
6345 return window_height_changed_p;
6349 /* Helper for display_echo_area. Display the current buffer which
6350 contains the current echo area message in window W, a mini-window,
6351 a pointer to which is passed in A1. A2..A4 are currently not used.
6352 Change the height of W so that all of the message is displayed.
6353 Value is non-zero if height of W was changed. */
6355 static int
6356 display_echo_area_1 (a1, a2, a3, a4)
6357 EMACS_INT a1;
6358 Lisp_Object a2;
6359 EMACS_INT a3, a4;
6361 struct window *w = (struct window *) a1;
6362 Lisp_Object window;
6363 struct text_pos start;
6364 int window_height_changed_p = 0;
6366 /* Do this before displaying, so that we have a large enough glyph
6367 matrix for the display. */
6368 window_height_changed_p = resize_mini_window (w, 0);
6370 /* Display. */
6371 clear_glyph_matrix (w->desired_matrix);
6372 XSETWINDOW (window, w);
6373 SET_TEXT_POS (start, BEG, BEG_BYTE);
6374 try_window (window, start);
6376 return window_height_changed_p;
6380 /* Resize the echo area window to exactly the size needed for the
6381 currently displayed message, if there is one. */
6383 void
6384 resize_echo_area_axactly ()
6386 if (BUFFERP (echo_area_buffer[0])
6387 && WINDOWP (echo_area_window))
6389 struct window *w = XWINDOW (echo_area_window);
6390 int resized_p;
6392 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6393 (EMACS_INT) w, Qnil, 0, 0);
6394 if (resized_p)
6396 ++windows_or_buffers_changed;
6397 ++update_mode_lines;
6398 redisplay_internal (0);
6404 /* Callback function for with_echo_area_buffer, when used from
6405 resize_echo_area_axactly. A1 contains a pointer to the window to
6406 resize, A2 to A4 are not used. Value is what resize_mini_window
6407 returns. */
6409 static int
6410 resize_mini_window_1 (a1, a2, a3, a4)
6411 EMACS_INT a1;
6412 Lisp_Object a2;
6413 EMACS_INT a3, a4;
6415 return resize_mini_window ((struct window *) a1, 1);
6419 /* Resize mini-window W to fit the size of its contents. EXACT:P
6420 means size the window exactly to the size needed. Otherwise, it's
6421 only enlarged until W's buffer is empty. Value is non-zero if
6422 the window height has been changed. */
6425 resize_mini_window (w, exact_p)
6426 struct window *w;
6427 int exact_p;
6429 struct frame *f = XFRAME (w->frame);
6430 int window_height_changed_p = 0;
6432 xassert (MINI_WINDOW_P (w));
6434 /* Nil means don't try to resize. */
6435 if (NILP (Vresize_mini_windows)
6436 || (FRAME_X_P (f) && f->output_data.x == NULL))
6437 return 0;
6439 if (!FRAME_MINIBUF_ONLY_P (f))
6441 struct it it;
6442 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6443 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6444 int height, max_height;
6445 int unit = CANON_Y_UNIT (f);
6446 struct text_pos start;
6447 struct buffer *old_current_buffer = NULL;
6449 if (current_buffer != XBUFFER (w->buffer))
6451 old_current_buffer = current_buffer;
6452 set_buffer_internal (XBUFFER (w->buffer));
6455 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6457 /* Compute the max. number of lines specified by the user. */
6458 if (FLOATP (Vmax_mini_window_height))
6459 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6460 else if (INTEGERP (Vmax_mini_window_height))
6461 max_height = XINT (Vmax_mini_window_height);
6462 else
6463 max_height = total_height / 4;
6465 /* Correct that max. height if it's bogus. */
6466 max_height = max (1, max_height);
6467 max_height = min (total_height, max_height);
6469 /* Find out the height of the text in the window. */
6470 if (it.truncate_lines_p)
6471 height = 1;
6472 else
6474 last_height = 0;
6475 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6476 if (it.max_ascent == 0 && it.max_descent == 0)
6477 height = it.current_y + last_height;
6478 else
6479 height = it.current_y + it.max_ascent + it.max_descent;
6480 height -= it.extra_line_spacing;
6481 height = (height + unit - 1) / unit;
6484 /* Compute a suitable window start. */
6485 if (height > max_height)
6487 height = max_height;
6488 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6489 move_it_vertically_backward (&it, (height - 1) * unit);
6490 start = it.current.pos;
6492 else
6493 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6494 SET_MARKER_FROM_TEXT_POS (w->start, start);
6496 if (EQ (Vresize_mini_windows, Qgrow_only))
6498 /* Let it grow only, until we display an empty message, in which
6499 case the window shrinks again. */
6500 if (height > XFASTINT (w->height))
6502 int old_height = XFASTINT (w->height);
6503 freeze_window_starts (f, 1);
6504 grow_mini_window (w, height - XFASTINT (w->height));
6505 window_height_changed_p = XFASTINT (w->height) != old_height;
6507 else if (height < XFASTINT (w->height)
6508 && (exact_p || BEGV == ZV))
6510 int old_height = XFASTINT (w->height);
6511 freeze_window_starts (f, 0);
6512 shrink_mini_window (w);
6513 window_height_changed_p = XFASTINT (w->height) != old_height;
6516 else
6518 /* Always resize to exact size needed. */
6519 if (height > XFASTINT (w->height))
6521 int old_height = XFASTINT (w->height);
6522 freeze_window_starts (f, 1);
6523 grow_mini_window (w, height - XFASTINT (w->height));
6524 window_height_changed_p = XFASTINT (w->height) != old_height;
6526 else if (height < XFASTINT (w->height))
6528 int old_height = XFASTINT (w->height);
6529 freeze_window_starts (f, 0);
6530 shrink_mini_window (w);
6532 if (height)
6534 freeze_window_starts (f, 1);
6535 grow_mini_window (w, height - XFASTINT (w->height));
6538 window_height_changed_p = XFASTINT (w->height) != old_height;
6542 if (old_current_buffer)
6543 set_buffer_internal (old_current_buffer);
6546 return window_height_changed_p;
6550 /* Value is the current message, a string, or nil if there is no
6551 current message. */
6553 Lisp_Object
6554 current_message ()
6556 Lisp_Object msg;
6558 if (NILP (echo_area_buffer[0]))
6559 msg = Qnil;
6560 else
6562 with_echo_area_buffer (0, 0, current_message_1,
6563 (EMACS_INT) &msg, Qnil, 0, 0);
6564 if (NILP (msg))
6565 echo_area_buffer[0] = Qnil;
6568 return msg;
6572 static int
6573 current_message_1 (a1, a2, a3, a4)
6574 EMACS_INT a1;
6575 Lisp_Object a2;
6576 EMACS_INT a3, a4;
6578 Lisp_Object *msg = (Lisp_Object *) a1;
6580 if (Z > BEG)
6581 *msg = make_buffer_string (BEG, Z, 1);
6582 else
6583 *msg = Qnil;
6584 return 0;
6588 /* Push the current message on Vmessage_stack for later restauration
6589 by restore_message. Value is non-zero if the current message isn't
6590 empty. This is a relatively infrequent operation, so it's not
6591 worth optimizing. */
6594 push_message ()
6596 Lisp_Object msg;
6597 msg = current_message ();
6598 Vmessage_stack = Fcons (msg, Vmessage_stack);
6599 return STRINGP (msg);
6603 /* Handler for record_unwind_protect calling pop_message. */
6605 Lisp_Object
6606 push_message_unwind (dummy)
6607 Lisp_Object dummy;
6609 pop_message ();
6610 return Qnil;
6614 /* Restore message display from the top of Vmessage_stack. */
6616 void
6617 restore_message ()
6619 Lisp_Object msg;
6621 xassert (CONSP (Vmessage_stack));
6622 msg = XCAR (Vmessage_stack);
6623 if (STRINGP (msg))
6624 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6625 else
6626 message3_nolog (msg, 0, 0);
6630 /* Pop the top-most entry off Vmessage_stack. */
6632 void
6633 pop_message ()
6635 xassert (CONSP (Vmessage_stack));
6636 Vmessage_stack = XCDR (Vmessage_stack);
6640 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6641 exits. If the stack is not empty, we have a missing pop_message
6642 somewhere. */
6644 void
6645 check_message_stack ()
6647 if (!NILP (Vmessage_stack))
6648 abort ();
6652 /* Truncate to NCHARS what will be displayed in the echo area the next
6653 time we display it---but don't redisplay it now. */
6655 void
6656 truncate_echo_area (nchars)
6657 int nchars;
6659 if (nchars == 0)
6660 echo_area_buffer[0] = Qnil;
6661 /* A null message buffer means that the frame hasn't really been
6662 initialized yet. Error messages get reported properly by
6663 cmd_error, so this must be just an informative message; toss it. */
6664 else if (!noninteractive
6665 && INTERACTIVE
6666 && !NILP (echo_area_buffer[0]))
6668 struct frame *sf = SELECTED_FRAME ();
6669 if (FRAME_MESSAGE_BUF (sf))
6670 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6675 /* Helper function for truncate_echo_area. Truncate the current
6676 message to at most NCHARS characters. */
6678 static int
6679 truncate_message_1 (nchars, a2, a3, a4)
6680 EMACS_INT nchars;
6681 Lisp_Object a2;
6682 EMACS_INT a3, a4;
6684 if (BEG + nchars < Z)
6685 del_range (BEG + nchars, Z);
6686 if (Z == BEG)
6687 echo_area_buffer[0] = Qnil;
6688 return 0;
6692 /* Set the current message to a substring of S or STRING.
6694 If STRING is a Lisp string, set the message to the first NBYTES
6695 bytes from STRING. NBYTES zero means use the whole string. If
6696 STRING is multibyte, the message will be displayed multibyte.
6698 If S is not null, set the message to the first LEN bytes of S. LEN
6699 zero means use the whole string. MULTIBYTE_P non-zero means S is
6700 multibyte. Display the message multibyte in that case. */
6702 void
6703 set_message (s, string, nbytes, multibyte_p)
6704 char *s;
6705 Lisp_Object string;
6706 int nbytes;
6708 message_enable_multibyte
6709 = ((s && multibyte_p)
6710 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6712 with_echo_area_buffer (0, -1, set_message_1,
6713 (EMACS_INT) s, string, nbytes, multibyte_p);
6714 message_buf_print = 0;
6715 help_echo_showing_p = 0;
6719 /* Helper function for set_message. Arguments have the same meaning
6720 as there, with A1 corresponding to S and A2 corresponding to STRING
6721 This function is called with the echo area buffer being
6722 current. */
6724 static int
6725 set_message_1 (a1, a2, nbytes, multibyte_p)
6726 EMACS_INT a1;
6727 Lisp_Object a2;
6728 EMACS_INT nbytes, multibyte_p;
6730 char *s = (char *) a1;
6731 Lisp_Object string = a2;
6733 xassert (BEG == Z);
6735 /* Change multibyteness of the echo buffer appropriately. */
6736 if (message_enable_multibyte
6737 != !NILP (current_buffer->enable_multibyte_characters))
6738 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6740 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6742 /* Insert new message at BEG. */
6743 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6745 if (STRINGP (string))
6747 int nchars;
6749 if (nbytes == 0)
6750 nbytes = XSTRING (string)->size_byte;
6751 nchars = string_byte_to_char (string, nbytes);
6753 /* This function takes care of single/multibyte conversion. We
6754 just have to ensure that the echo area buffer has the right
6755 setting of enable_multibyte_characters. */
6756 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6758 else if (s)
6760 if (nbytes == 0)
6761 nbytes = strlen (s);
6763 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6765 /* Convert from multi-byte to single-byte. */
6766 int i, c, n;
6767 unsigned char work[1];
6769 /* Convert a multibyte string to single-byte. */
6770 for (i = 0; i < nbytes; i += n)
6772 c = string_char_and_length (s + i, nbytes - i, &n);
6773 work[0] = (SINGLE_BYTE_CHAR_P (c)
6775 : multibyte_char_to_unibyte (c, Qnil));
6776 insert_1_both (work, 1, 1, 1, 0, 0);
6779 else if (!multibyte_p
6780 && !NILP (current_buffer->enable_multibyte_characters))
6782 /* Convert from single-byte to multi-byte. */
6783 int i, c, n;
6784 unsigned char *msg = (unsigned char *) s;
6785 unsigned char str[MAX_MULTIBYTE_LENGTH];
6787 /* Convert a single-byte string to multibyte. */
6788 for (i = 0; i < nbytes; i++)
6790 c = unibyte_char_to_multibyte (msg[i]);
6791 n = CHAR_STRING (c, str);
6792 insert_1_both (str, 1, n, 1, 0, 0);
6795 else
6796 insert_1 (s, nbytes, 1, 0, 0);
6799 return 0;
6803 /* Clear messages. CURRENT_P non-zero means clear the current
6804 message. LAST_DISPLAYED_P non-zero means clear the message
6805 last displayed. */
6807 void
6808 clear_message (current_p, last_displayed_p)
6809 int current_p, last_displayed_p;
6811 if (current_p)
6812 echo_area_buffer[0] = Qnil;
6814 if (last_displayed_p)
6815 echo_area_buffer[1] = Qnil;
6817 message_buf_print = 0;
6820 /* Clear garbaged frames.
6822 This function is used where the old redisplay called
6823 redraw_garbaged_frames which in turn called redraw_frame which in
6824 turn called clear_frame. The call to clear_frame was a source of
6825 flickering. I believe a clear_frame is not necessary. It should
6826 suffice in the new redisplay to invalidate all current matrices,
6827 and ensure a complete redisplay of all windows. */
6829 static void
6830 clear_garbaged_frames ()
6832 if (frame_garbaged)
6834 Lisp_Object tail, frame;
6836 FOR_EACH_FRAME (tail, frame)
6838 struct frame *f = XFRAME (frame);
6840 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6842 clear_current_matrices (f);
6843 f->garbaged = 0;
6847 frame_garbaged = 0;
6848 ++windows_or_buffers_changed;
6853 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6854 is non-zero update selected_frame. Value is non-zero if the
6855 mini-windows height has been changed. */
6857 static int
6858 echo_area_display (update_frame_p)
6859 int update_frame_p;
6861 Lisp_Object mini_window;
6862 struct window *w;
6863 struct frame *f;
6864 int window_height_changed_p = 0;
6865 struct frame *sf = SELECTED_FRAME ();
6867 mini_window = FRAME_MINIBUF_WINDOW (sf);
6868 w = XWINDOW (mini_window);
6869 f = XFRAME (WINDOW_FRAME (w));
6871 /* Don't display if frame is invisible or not yet initialized. */
6872 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6873 return 0;
6875 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6876 #ifndef macintosh
6877 #ifdef HAVE_WINDOW_SYSTEM
6878 /* When Emacs starts, selected_frame may be a visible terminal
6879 frame, even if we run under a window system. If we let this
6880 through, a message would be displayed on the terminal. */
6881 if (EQ (selected_frame, Vterminal_frame)
6882 && !NILP (Vwindow_system))
6883 return 0;
6884 #endif /* HAVE_WINDOW_SYSTEM */
6885 #endif
6887 /* Redraw garbaged frames. */
6888 if (frame_garbaged)
6889 clear_garbaged_frames ();
6891 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6893 echo_area_window = mini_window;
6894 window_height_changed_p = display_echo_area (w);
6895 w->must_be_updated_p = 1;
6897 /* Update the display, unless called from redisplay_internal.
6898 Also don't update the screen during redisplay itself. The
6899 update will happen at the end of redisplay, and an update
6900 here could cause confusion. */
6901 if (update_frame_p && !redisplaying_p)
6903 int n = 0;
6905 /* If the display update has been interrupted by pending
6906 input, update mode lines in the frame. Due to the
6907 pending input, it might have been that redisplay hasn't
6908 been called, so that mode lines above the echo area are
6909 garbaged. This looks odd, so we prevent it here. */
6910 if (!display_completed)
6911 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6913 if (window_height_changed_p
6914 /* Don't do this if Emacs is shutting down. Redisplay
6915 needs to run hooks. */
6916 && !NILP (Vrun_hooks))
6918 /* Must update other windows. Likewise as in other
6919 cases, don't let this update be interrupted by
6920 pending input. */
6921 int count = BINDING_STACK_SIZE ();
6922 specbind (Qredisplay_dont_pause, Qt);
6923 windows_or_buffers_changed = 1;
6924 redisplay_internal (0);
6925 unbind_to (count, Qnil);
6927 else if (FRAME_WINDOW_P (f) && n == 0)
6929 /* Window configuration is the same as before.
6930 Can do with a display update of the echo area,
6931 unless we displayed some mode lines. */
6932 update_single_window (w, 1);
6933 rif->flush_display (f);
6935 else
6936 update_frame (f, 1, 1);
6938 /* If cursor is in the echo area, make sure that the next
6939 redisplay displays the minibuffer, so that the cursor will
6940 be replaced with what the minibuffer wants. */
6941 if (cursor_in_echo_area)
6942 ++windows_or_buffers_changed;
6945 else if (!EQ (mini_window, selected_window))
6946 windows_or_buffers_changed++;
6948 /* Last displayed message is now the current message. */
6949 echo_area_buffer[1] = echo_area_buffer[0];
6951 /* Prevent redisplay optimization in redisplay_internal by resetting
6952 this_line_start_pos. This is done because the mini-buffer now
6953 displays the message instead of its buffer text. */
6954 if (EQ (mini_window, selected_window))
6955 CHARPOS (this_line_start_pos) = 0;
6957 return window_height_changed_p;
6962 /***********************************************************************
6963 Frame Titles
6964 ***********************************************************************/
6967 #ifdef HAVE_WINDOW_SYSTEM
6969 /* A buffer for constructing frame titles in it; allocated from the
6970 heap in init_xdisp and resized as needed in store_frame_title_char. */
6972 static char *frame_title_buf;
6974 /* The buffer's end, and a current output position in it. */
6976 static char *frame_title_buf_end;
6977 static char *frame_title_ptr;
6980 /* Store a single character C for the frame title in frame_title_buf.
6981 Re-allocate frame_title_buf if necessary. */
6983 static void
6984 store_frame_title_char (c)
6985 char c;
6987 /* If output position has reached the end of the allocated buffer,
6988 double the buffer's size. */
6989 if (frame_title_ptr == frame_title_buf_end)
6991 int len = frame_title_ptr - frame_title_buf;
6992 int new_size = 2 * len * sizeof *frame_title_buf;
6993 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6994 frame_title_buf_end = frame_title_buf + new_size;
6995 frame_title_ptr = frame_title_buf + len;
6998 *frame_title_ptr++ = c;
7002 /* Store part of a frame title in frame_title_buf, beginning at
7003 frame_title_ptr. STR is the string to store. Do not copy
7004 characters that yield more columns than PRECISION; PRECISION <= 0
7005 means copy the whole string. Pad with spaces until FIELD_WIDTH
7006 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7007 pad. Called from display_mode_element when it is used to build a
7008 frame title. */
7010 static int
7011 store_frame_title (str, field_width, precision)
7012 unsigned char *str;
7013 int field_width, precision;
7015 int n = 0;
7016 int dummy, nbytes, width;
7018 /* Copy at most PRECISION chars from STR. */
7019 nbytes = strlen (str);
7020 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7021 while (nbytes--)
7022 store_frame_title_char (*str++);
7024 /* Fill up with spaces until FIELD_WIDTH reached. */
7025 while (field_width > 0
7026 && n < field_width)
7028 store_frame_title_char (' ');
7029 ++n;
7032 return n;
7036 /* Set the title of FRAME, if it has changed. The title format is
7037 Vicon_title_format if FRAME is iconified, otherwise it is
7038 frame_title_format. */
7040 static void
7041 x_consider_frame_title (frame)
7042 Lisp_Object frame;
7044 struct frame *f = XFRAME (frame);
7046 if (FRAME_WINDOW_P (f)
7047 || FRAME_MINIBUF_ONLY_P (f)
7048 || f->explicit_name)
7050 /* Do we have more than one visible frame on this X display? */
7051 Lisp_Object tail;
7052 Lisp_Object fmt;
7053 struct buffer *obuf;
7054 int len;
7055 struct it it;
7057 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7059 struct frame *tf = XFRAME (XCAR (tail));
7061 if (tf != f
7062 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7063 && !FRAME_MINIBUF_ONLY_P (tf)
7064 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7065 break;
7068 /* Set global variable indicating that multiple frames exist. */
7069 multiple_frames = CONSP (tail);
7071 /* Switch to the buffer of selected window of the frame. Set up
7072 frame_title_ptr so that display_mode_element will output into it;
7073 then display the title. */
7074 obuf = current_buffer;
7075 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7076 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7077 frame_title_ptr = frame_title_buf;
7078 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7079 NULL, DEFAULT_FACE_ID);
7080 display_mode_element (&it, 0, -1, -1, fmt);
7081 len = frame_title_ptr - frame_title_buf;
7082 frame_title_ptr = NULL;
7083 set_buffer_internal (obuf);
7085 /* Set the title only if it's changed. This avoids consing in
7086 the common case where it hasn't. (If it turns out that we've
7087 already wasted too much time by walking through the list with
7088 display_mode_element, then we might need to optimize at a
7089 higher level than this.) */
7090 if (! STRINGP (f->name)
7091 || STRING_BYTES (XSTRING (f->name)) != len
7092 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7093 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7097 #else /* not HAVE_WINDOW_SYSTEM */
7099 #define frame_title_ptr ((char *)0)
7100 #define store_frame_title(str, mincol, maxcol) 0
7102 #endif /* not HAVE_WINDOW_SYSTEM */
7107 /***********************************************************************
7108 Menu Bars
7109 ***********************************************************************/
7112 /* Prepare for redisplay by updating menu-bar item lists when
7113 appropriate. This can call eval. */
7115 void
7116 prepare_menu_bars ()
7118 int all_windows;
7119 struct gcpro gcpro1, gcpro2;
7120 struct frame *f;
7121 Lisp_Object tooltip_frame;
7123 #ifdef HAVE_X_WINDOWS
7124 tooltip_frame = tip_frame;
7125 #else
7126 tooltip_frame = Qnil;
7127 #endif
7129 /* Update all frame titles based on their buffer names, etc. We do
7130 this before the menu bars so that the buffer-menu will show the
7131 up-to-date frame titles. */
7132 #ifdef HAVE_WINDOW_SYSTEM
7133 if (windows_or_buffers_changed || update_mode_lines)
7135 Lisp_Object tail, frame;
7137 FOR_EACH_FRAME (tail, frame)
7139 f = XFRAME (frame);
7140 if (!EQ (frame, tooltip_frame)
7141 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7142 x_consider_frame_title (frame);
7145 #endif /* HAVE_WINDOW_SYSTEM */
7147 /* Update the menu bar item lists, if appropriate. This has to be
7148 done before any actual redisplay or generation of display lines. */
7149 all_windows = (update_mode_lines
7150 || buffer_shared > 1
7151 || windows_or_buffers_changed);
7152 if (all_windows)
7154 Lisp_Object tail, frame;
7155 int count = BINDING_STACK_SIZE ();
7157 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7159 FOR_EACH_FRAME (tail, frame)
7161 f = XFRAME (frame);
7163 /* Ignore tooltip frame. */
7164 if (EQ (frame, tooltip_frame))
7165 continue;
7167 /* If a window on this frame changed size, report that to
7168 the user and clear the size-change flag. */
7169 if (FRAME_WINDOW_SIZES_CHANGED (f))
7171 Lisp_Object functions;
7173 /* Clear flag first in case we get an error below. */
7174 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7175 functions = Vwindow_size_change_functions;
7176 GCPRO2 (tail, functions);
7178 while (CONSP (functions))
7180 call1 (XCAR (functions), frame);
7181 functions = XCDR (functions);
7183 UNGCPRO;
7186 GCPRO1 (tail);
7187 update_menu_bar (f, 0);
7188 #ifdef HAVE_WINDOW_SYSTEM
7189 update_tool_bar (f, 0);
7190 #endif
7191 UNGCPRO;
7194 unbind_to (count, Qnil);
7196 else
7198 struct frame *sf = SELECTED_FRAME ();
7199 update_menu_bar (sf, 1);
7200 #ifdef HAVE_WINDOW_SYSTEM
7201 update_tool_bar (sf, 1);
7202 #endif
7205 /* Motif needs this. See comment in xmenu.c. Turn it off when
7206 pending_menu_activation is not defined. */
7207 #ifdef USE_X_TOOLKIT
7208 pending_menu_activation = 0;
7209 #endif
7213 /* Update the menu bar item list for frame F. This has to be done
7214 before we start to fill in any display lines, because it can call
7215 eval.
7217 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7219 static void
7220 update_menu_bar (f, save_match_data)
7221 struct frame *f;
7222 int save_match_data;
7224 Lisp_Object window;
7225 register struct window *w;
7227 /* If called recursively during a menu update, do nothing. This can
7228 happen when, for instance, an activate-menubar-hook causes a
7229 redisplay. */
7230 if (inhibit_menubar_update)
7231 return;
7233 window = FRAME_SELECTED_WINDOW (f);
7234 w = XWINDOW (window);
7236 if (update_mode_lines)
7237 w->update_mode_line = Qt;
7239 if (FRAME_WINDOW_P (f)
7241 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7242 FRAME_EXTERNAL_MENU_BAR (f)
7243 #else
7244 FRAME_MENU_BAR_LINES (f) > 0
7245 #endif
7246 : FRAME_MENU_BAR_LINES (f) > 0)
7248 /* If the user has switched buffers or windows, we need to
7249 recompute to reflect the new bindings. But we'll
7250 recompute when update_mode_lines is set too; that means
7251 that people can use force-mode-line-update to request
7252 that the menu bar be recomputed. The adverse effect on
7253 the rest of the redisplay algorithm is about the same as
7254 windows_or_buffers_changed anyway. */
7255 if (windows_or_buffers_changed
7256 || !NILP (w->update_mode_line)
7257 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7258 < BUF_MODIFF (XBUFFER (w->buffer)))
7259 != !NILP (w->last_had_star))
7260 || ((!NILP (Vtransient_mark_mode)
7261 && !NILP (XBUFFER (w->buffer)->mark_active))
7262 != !NILP (w->region_showing)))
7264 struct buffer *prev = current_buffer;
7265 int count = BINDING_STACK_SIZE ();
7267 specbind (Qinhibit_menubar_update, Qt);
7269 set_buffer_internal_1 (XBUFFER (w->buffer));
7270 if (save_match_data)
7271 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7272 if (NILP (Voverriding_local_map_menu_flag))
7274 specbind (Qoverriding_terminal_local_map, Qnil);
7275 specbind (Qoverriding_local_map, Qnil);
7278 /* Run the Lucid hook. */
7279 safe_run_hooks (Qactivate_menubar_hook);
7281 /* If it has changed current-menubar from previous value,
7282 really recompute the menu-bar from the value. */
7283 if (! NILP (Vlucid_menu_bar_dirty_flag))
7284 call0 (Qrecompute_lucid_menubar);
7286 safe_run_hooks (Qmenu_bar_update_hook);
7287 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7289 /* Redisplay the menu bar in case we changed it. */
7290 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7291 if (FRAME_WINDOW_P (f)
7292 #if defined (macintosh)
7293 /* All frames on Mac OS share the same menubar. So only the
7294 selected frame should be allowed to set it. */
7295 && f == SELECTED_FRAME ()
7296 #endif
7298 set_frame_menubar (f, 0, 0);
7299 else
7300 /* On a terminal screen, the menu bar is an ordinary screen
7301 line, and this makes it get updated. */
7302 w->update_mode_line = Qt;
7303 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7304 /* In the non-toolkit version, the menu bar is an ordinary screen
7305 line, and this makes it get updated. */
7306 w->update_mode_line = Qt;
7307 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7309 unbind_to (count, Qnil);
7310 set_buffer_internal_1 (prev);
7317 /***********************************************************************
7318 Tool-bars
7319 ***********************************************************************/
7321 #ifdef HAVE_WINDOW_SYSTEM
7323 /* Update the tool-bar item list for frame F. This has to be done
7324 before we start to fill in any display lines. Called from
7325 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7326 and restore it here. */
7328 static void
7329 update_tool_bar (f, save_match_data)
7330 struct frame *f;
7331 int save_match_data;
7333 if (WINDOWP (f->tool_bar_window)
7334 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7336 Lisp_Object window;
7337 struct window *w;
7339 window = FRAME_SELECTED_WINDOW (f);
7340 w = XWINDOW (window);
7342 /* If the user has switched buffers or windows, we need to
7343 recompute to reflect the new bindings. But we'll
7344 recompute when update_mode_lines is set too; that means
7345 that people can use force-mode-line-update to request
7346 that the menu bar be recomputed. The adverse effect on
7347 the rest of the redisplay algorithm is about the same as
7348 windows_or_buffers_changed anyway. */
7349 if (windows_or_buffers_changed
7350 || !NILP (w->update_mode_line)
7351 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7352 < BUF_MODIFF (XBUFFER (w->buffer)))
7353 != !NILP (w->last_had_star))
7354 || ((!NILP (Vtransient_mark_mode)
7355 && !NILP (XBUFFER (w->buffer)->mark_active))
7356 != !NILP (w->region_showing)))
7358 struct buffer *prev = current_buffer;
7359 int count = BINDING_STACK_SIZE ();
7361 /* Set current_buffer to the buffer of the selected
7362 window of the frame, so that we get the right local
7363 keymaps. */
7364 set_buffer_internal_1 (XBUFFER (w->buffer));
7366 /* Save match data, if we must. */
7367 if (save_match_data)
7368 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7370 /* Make sure that we don't accidentally use bogus keymaps. */
7371 if (NILP (Voverriding_local_map_menu_flag))
7373 specbind (Qoverriding_terminal_local_map, Qnil);
7374 specbind (Qoverriding_local_map, Qnil);
7377 /* Build desired tool-bar items from keymaps. */
7378 f->tool_bar_items
7379 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7381 /* Redisplay the tool-bar in case we changed it. */
7382 w->update_mode_line = Qt;
7384 unbind_to (count, Qnil);
7385 set_buffer_internal_1 (prev);
7391 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7392 F's desired tool-bar contents. F->tool_bar_items must have
7393 been set up previously by calling prepare_menu_bars. */
7395 static void
7396 build_desired_tool_bar_string (f)
7397 struct frame *f;
7399 int i, size, size_needed;
7400 struct gcpro gcpro1, gcpro2, gcpro3;
7401 Lisp_Object image, plist, props;
7403 image = plist = props = Qnil;
7404 GCPRO3 (image, plist, props);
7406 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7407 Otherwise, make a new string. */
7409 /* The size of the string we might be able to reuse. */
7410 size = (STRINGP (f->desired_tool_bar_string)
7411 ? XSTRING (f->desired_tool_bar_string)->size
7412 : 0);
7414 /* We need one space in the string for each image. */
7415 size_needed = f->n_tool_bar_items;
7417 /* Reuse f->desired_tool_bar_string, if possible. */
7418 if (size < size_needed || NILP (f->desired_tool_bar_string))
7419 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7420 make_number (' '));
7421 else
7423 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7424 Fremove_text_properties (make_number (0), make_number (size),
7425 props, f->desired_tool_bar_string);
7428 /* Put a `display' property on the string for the images to display,
7429 put a `menu_item' property on tool-bar items with a value that
7430 is the index of the item in F's tool-bar item vector. */
7431 for (i = 0; i < f->n_tool_bar_items; ++i)
7433 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7435 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7436 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7437 int hmargin, vmargin, relief, idx, end;
7438 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7439 extern Lisp_Object Qlaplace;
7441 /* If image is a vector, choose the image according to the
7442 button state. */
7443 image = PROP (TOOL_BAR_ITEM_IMAGES);
7444 if (VECTORP (image))
7446 if (enabled_p)
7447 idx = (selected_p
7448 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7449 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7450 else
7451 idx = (selected_p
7452 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7453 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7455 xassert (ASIZE (image) >= idx);
7456 image = AREF (image, idx);
7458 else
7459 idx = -1;
7461 /* Ignore invalid image specifications. */
7462 if (!valid_image_p (image))
7463 continue;
7465 /* Display the tool-bar button pressed, or depressed. */
7466 plist = Fcopy_sequence (XCDR (image));
7468 /* Compute margin and relief to draw. */
7469 relief = (tool_bar_button_relief > 0
7470 ? tool_bar_button_relief
7471 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7472 hmargin = vmargin = relief;
7474 if (INTEGERP (Vtool_bar_button_margin)
7475 && XINT (Vtool_bar_button_margin) > 0)
7477 hmargin += XFASTINT (Vtool_bar_button_margin);
7478 vmargin += XFASTINT (Vtool_bar_button_margin);
7480 else if (CONSP (Vtool_bar_button_margin))
7482 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7483 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7484 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7486 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7487 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7488 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7491 if (auto_raise_tool_bar_buttons_p)
7493 /* Add a `:relief' property to the image spec if the item is
7494 selected. */
7495 if (selected_p)
7497 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7498 hmargin -= relief;
7499 vmargin -= relief;
7502 else
7504 /* If image is selected, display it pressed, i.e. with a
7505 negative relief. If it's not selected, display it with a
7506 raised relief. */
7507 plist = Fplist_put (plist, QCrelief,
7508 (selected_p
7509 ? make_number (-relief)
7510 : make_number (relief)));
7511 hmargin -= relief;
7512 vmargin -= relief;
7515 /* Put a margin around the image. */
7516 if (hmargin || vmargin)
7518 if (hmargin == vmargin)
7519 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7520 else
7521 plist = Fplist_put (plist, QCmargin,
7522 Fcons (make_number (hmargin),
7523 make_number (vmargin)));
7526 /* If button is not enabled, and we don't have special images
7527 for the disabled state, make the image appear disabled by
7528 applying an appropriate algorithm to it. */
7529 if (!enabled_p && idx < 0)
7530 plist = Fplist_put (plist, QCconversion, Qdisabled);
7532 /* Put a `display' text property on the string for the image to
7533 display. Put a `menu-item' property on the string that gives
7534 the start of this item's properties in the tool-bar items
7535 vector. */
7536 image = Fcons (Qimage, plist);
7537 props = list4 (Qdisplay, image,
7538 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7540 /* Let the last image hide all remaining spaces in the tool bar
7541 string. The string can be longer than needed when we reuse a
7542 previous string. */
7543 if (i + 1 == f->n_tool_bar_items)
7544 end = XSTRING (f->desired_tool_bar_string)->size;
7545 else
7546 end = i + 1;
7547 Fadd_text_properties (make_number (i), make_number (end),
7548 props, f->desired_tool_bar_string);
7549 #undef PROP
7552 UNGCPRO;
7556 /* Display one line of the tool-bar of frame IT->f. */
7558 static void
7559 display_tool_bar_line (it)
7560 struct it *it;
7562 struct glyph_row *row = it->glyph_row;
7563 int max_x = it->last_visible_x;
7564 struct glyph *last;
7566 prepare_desired_row (row);
7567 row->y = it->current_y;
7569 /* Note that this isn't made use of if the face hasn't a box,
7570 so there's no need to check the face here. */
7571 it->start_of_box_run_p = 1;
7573 while (it->current_x < max_x)
7575 int x_before, x, n_glyphs_before, i, nglyphs;
7577 /* Get the next display element. */
7578 if (!get_next_display_element (it))
7579 break;
7581 /* Produce glyphs. */
7582 x_before = it->current_x;
7583 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7584 PRODUCE_GLYPHS (it);
7586 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7587 i = 0;
7588 x = x_before;
7589 while (i < nglyphs)
7591 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7593 if (x + glyph->pixel_width > max_x)
7595 /* Glyph doesn't fit on line. */
7596 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7597 it->current_x = x;
7598 goto out;
7601 ++it->hpos;
7602 x += glyph->pixel_width;
7603 ++i;
7606 /* Stop at line ends. */
7607 if (ITERATOR_AT_END_OF_LINE_P (it))
7608 break;
7610 set_iterator_to_next (it, 1);
7613 out:;
7615 row->displays_text_p = row->used[TEXT_AREA] != 0;
7616 extend_face_to_end_of_line (it);
7617 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7618 last->right_box_line_p = 1;
7619 if (last == row->glyphs[TEXT_AREA])
7620 last->left_box_line_p = 1;
7621 compute_line_metrics (it);
7623 /* If line is empty, make it occupy the rest of the tool-bar. */
7624 if (!row->displays_text_p)
7626 row->height = row->phys_height = it->last_visible_y - row->y;
7627 row->ascent = row->phys_ascent = 0;
7630 row->full_width_p = 1;
7631 row->continued_p = 0;
7632 row->truncated_on_left_p = 0;
7633 row->truncated_on_right_p = 0;
7635 it->current_x = it->hpos = 0;
7636 it->current_y += row->height;
7637 ++it->vpos;
7638 ++it->glyph_row;
7642 /* Value is the number of screen lines needed to make all tool-bar
7643 items of frame F visible. */
7645 static int
7646 tool_bar_lines_needed (f)
7647 struct frame *f;
7649 struct window *w = XWINDOW (f->tool_bar_window);
7650 struct it it;
7652 /* Initialize an iterator for iteration over
7653 F->desired_tool_bar_string in the tool-bar window of frame F. */
7654 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7655 it.first_visible_x = 0;
7656 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7657 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7659 while (!ITERATOR_AT_END_P (&it))
7661 it.glyph_row = w->desired_matrix->rows;
7662 clear_glyph_row (it.glyph_row);
7663 display_tool_bar_line (&it);
7666 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7670 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7671 0, 1, 0,
7672 "Return the number of lines occupied by the tool bar of FRAME.")
7673 (frame)
7674 Lisp_Object frame;
7676 struct frame *f;
7677 struct window *w;
7678 int nlines = 0;
7680 if (NILP (frame))
7681 frame = selected_frame;
7682 else
7683 CHECK_FRAME (frame, 0);
7684 f = XFRAME (frame);
7686 if (WINDOWP (f->tool_bar_window)
7687 || (w = XWINDOW (f->tool_bar_window),
7688 XFASTINT (w->height) > 0))
7690 update_tool_bar (f, 1);
7691 if (f->n_tool_bar_items)
7693 build_desired_tool_bar_string (f);
7694 nlines = tool_bar_lines_needed (f);
7698 return make_number (nlines);
7702 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7703 height should be changed. */
7705 static int
7706 redisplay_tool_bar (f)
7707 struct frame *f;
7709 struct window *w;
7710 struct it it;
7711 struct glyph_row *row;
7712 int change_height_p = 0;
7714 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7715 do anything. This means you must start with tool-bar-lines
7716 non-zero to get the auto-sizing effect. Or in other words, you
7717 can turn off tool-bars by specifying tool-bar-lines zero. */
7718 if (!WINDOWP (f->tool_bar_window)
7719 || (w = XWINDOW (f->tool_bar_window),
7720 XFASTINT (w->height) == 0))
7721 return 0;
7723 /* Set up an iterator for the tool-bar window. */
7724 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7725 it.first_visible_x = 0;
7726 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7727 row = it.glyph_row;
7729 /* Build a string that represents the contents of the tool-bar. */
7730 build_desired_tool_bar_string (f);
7731 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7733 /* Display as many lines as needed to display all tool-bar items. */
7734 while (it.current_y < it.last_visible_y)
7735 display_tool_bar_line (&it);
7737 /* It doesn't make much sense to try scrolling in the tool-bar
7738 window, so don't do it. */
7739 w->desired_matrix->no_scrolling_p = 1;
7740 w->must_be_updated_p = 1;
7742 if (auto_resize_tool_bars_p)
7744 int nlines;
7746 /* If we couldn't display everything, change the tool-bar's
7747 height. */
7748 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7749 change_height_p = 1;
7751 /* If there are blank lines at the end, except for a partially
7752 visible blank line at the end that is smaller than
7753 CANON_Y_UNIT, change the tool-bar's height. */
7754 row = it.glyph_row - 1;
7755 if (!row->displays_text_p
7756 && row->height >= CANON_Y_UNIT (f))
7757 change_height_p = 1;
7759 /* If row displays tool-bar items, but is partially visible,
7760 change the tool-bar's height. */
7761 if (row->displays_text_p
7762 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7763 change_height_p = 1;
7765 /* Resize windows as needed by changing the `tool-bar-lines'
7766 frame parameter. */
7767 if (change_height_p
7768 && (nlines = tool_bar_lines_needed (f),
7769 nlines != XFASTINT (w->height)))
7771 extern Lisp_Object Qtool_bar_lines;
7772 Lisp_Object frame;
7773 int old_height = XFASTINT (w->height);
7775 XSETFRAME (frame, f);
7776 clear_glyph_matrix (w->desired_matrix);
7777 Fmodify_frame_parameters (frame,
7778 Fcons (Fcons (Qtool_bar_lines,
7779 make_number (nlines)),
7780 Qnil));
7781 if (XFASTINT (w->height) != old_height)
7782 fonts_changed_p = 1;
7786 return change_height_p;
7790 /* Get information about the tool-bar item which is displayed in GLYPH
7791 on frame F. Return in *PROP_IDX the index where tool-bar item
7792 properties start in F->tool_bar_items. Value is zero if
7793 GLYPH doesn't display a tool-bar item. */
7796 tool_bar_item_info (f, glyph, prop_idx)
7797 struct frame *f;
7798 struct glyph *glyph;
7799 int *prop_idx;
7801 Lisp_Object prop;
7802 int success_p;
7804 /* Get the text property `menu-item' at pos. The value of that
7805 property is the start index of this item's properties in
7806 F->tool_bar_items. */
7807 prop = Fget_text_property (make_number (glyph->charpos),
7808 Qmenu_item, f->current_tool_bar_string);
7809 if (INTEGERP (prop))
7811 *prop_idx = XINT (prop);
7812 success_p = 1;
7814 else
7815 success_p = 0;
7817 return success_p;
7820 #endif /* HAVE_WINDOW_SYSTEM */
7824 /************************************************************************
7825 Horizontal scrolling
7826 ************************************************************************/
7828 static int hscroll_window_tree P_ ((Lisp_Object));
7829 static int hscroll_windows P_ ((Lisp_Object));
7831 /* For all leaf windows in the window tree rooted at WINDOW, set their
7832 hscroll value so that PT is (i) visible in the window, and (ii) so
7833 that it is not within a certain margin at the window's left and
7834 right border. Value is non-zero if any window's hscroll has been
7835 changed. */
7837 static int
7838 hscroll_window_tree (window)
7839 Lisp_Object window;
7841 int hscrolled_p = 0;
7843 while (WINDOWP (window))
7845 struct window *w = XWINDOW (window);
7847 if (WINDOWP (w->hchild))
7848 hscrolled_p |= hscroll_window_tree (w->hchild);
7849 else if (WINDOWP (w->vchild))
7850 hscrolled_p |= hscroll_window_tree (w->vchild);
7851 else if (w->cursor.vpos >= 0)
7853 int hscroll_margin, text_area_x, text_area_y;
7854 int text_area_width, text_area_height;
7855 struct glyph_row *current_cursor_row
7856 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7857 struct glyph_row *desired_cursor_row
7858 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7859 struct glyph_row *cursor_row
7860 = (desired_cursor_row->enabled_p
7861 ? desired_cursor_row
7862 : current_cursor_row);
7864 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7865 &text_area_width, &text_area_height);
7867 /* Scroll when cursor is inside this scroll margin. */
7868 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7870 if ((XFASTINT (w->hscroll)
7871 && w->cursor.x < hscroll_margin)
7872 || (cursor_row->enabled_p
7873 && cursor_row->truncated_on_right_p
7874 && (w->cursor.x > text_area_width - hscroll_margin)))
7876 struct it it;
7877 int hscroll;
7878 struct buffer *saved_current_buffer;
7879 int pt;
7881 /* Find point in a display of infinite width. */
7882 saved_current_buffer = current_buffer;
7883 current_buffer = XBUFFER (w->buffer);
7885 if (w == XWINDOW (selected_window))
7886 pt = BUF_PT (current_buffer);
7887 else
7889 pt = marker_position (w->pointm);
7890 pt = max (BEGV, pt);
7891 pt = min (ZV, pt);
7894 /* Move iterator to pt starting at cursor_row->start in
7895 a line with infinite width. */
7896 init_to_row_start (&it, w, cursor_row);
7897 it.last_visible_x = INFINITY;
7898 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7899 current_buffer = saved_current_buffer;
7901 /* Center cursor in window. */
7902 hscroll = (max (0, it.current_x - text_area_width / 2)
7903 / CANON_X_UNIT (it.f));
7904 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7906 /* Don't call Fset_window_hscroll if value hasn't
7907 changed because it will prevent redisplay
7908 optimizations. */
7909 if (XFASTINT (w->hscroll) != hscroll)
7911 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7912 w->hscroll = make_number (hscroll);
7913 hscrolled_p = 1;
7918 window = w->next;
7921 /* Value is non-zero if hscroll of any leaf window has been changed. */
7922 return hscrolled_p;
7926 /* Set hscroll so that cursor is visible and not inside horizontal
7927 scroll margins for all windows in the tree rooted at WINDOW. See
7928 also hscroll_window_tree above. Value is non-zero if any window's
7929 hscroll has been changed. If it has, desired matrices on the frame
7930 of WINDOW are cleared. */
7932 static int
7933 hscroll_windows (window)
7934 Lisp_Object window;
7936 int hscrolled_p;
7938 if (automatic_hscrolling_p)
7940 hscrolled_p = hscroll_window_tree (window);
7941 if (hscrolled_p)
7942 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7944 else
7945 hscrolled_p = 0;
7946 return hscrolled_p;
7951 /************************************************************************
7952 Redisplay
7953 ************************************************************************/
7955 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7956 to a non-zero value. This is sometimes handy to have in a debugger
7957 session. */
7959 #if GLYPH_DEBUG
7961 /* First and last unchanged row for try_window_id. */
7963 int debug_first_unchanged_at_end_vpos;
7964 int debug_last_unchanged_at_beg_vpos;
7966 /* Delta vpos and y. */
7968 int debug_dvpos, debug_dy;
7970 /* Delta in characters and bytes for try_window_id. */
7972 int debug_delta, debug_delta_bytes;
7974 /* Values of window_end_pos and window_end_vpos at the end of
7975 try_window_id. */
7977 int debug_end_pos, debug_end_vpos;
7979 /* Append a string to W->desired_matrix->method. FMT is a printf
7980 format string. A1...A9 are a supplement for a variable-length
7981 argument list. If trace_redisplay_p is non-zero also printf the
7982 resulting string to stderr. */
7984 static void
7985 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7986 struct window *w;
7987 char *fmt;
7988 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7990 char buffer[512];
7991 char *method = w->desired_matrix->method;
7992 int len = strlen (method);
7993 int size = sizeof w->desired_matrix->method;
7994 int remaining = size - len - 1;
7996 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7997 if (len && remaining)
7999 method[len] = '|';
8000 --remaining, ++len;
8003 strncpy (method + len, buffer, remaining);
8005 if (trace_redisplay_p)
8006 fprintf (stderr, "%p (%s): %s\n",
8008 ((BUFFERP (w->buffer)
8009 && STRINGP (XBUFFER (w->buffer)->name))
8010 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8011 : "no buffer"),
8012 buffer);
8015 #endif /* GLYPH_DEBUG */
8018 /* This counter is used to clear the face cache every once in a while
8019 in redisplay_internal. It is incremented for each redisplay.
8020 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8021 cleared. */
8023 #define CLEAR_FACE_CACHE_COUNT 10000
8024 static int clear_face_cache_count;
8026 /* Record the previous terminal frame we displayed. */
8028 static struct frame *previous_terminal_frame;
8030 /* Non-zero while redisplay_internal is in progress. */
8032 int redisplaying_p;
8035 /* Value is non-zero if all changes in window W, which displays
8036 current_buffer, are in the text between START and END. START is a
8037 buffer position, END is given as a distance from Z. Used in
8038 redisplay_internal for display optimization. */
8040 static INLINE int
8041 text_outside_line_unchanged_p (w, start, end)
8042 struct window *w;
8043 int start, end;
8045 int unchanged_p = 1;
8047 /* If text or overlays have changed, see where. */
8048 if (XFASTINT (w->last_modified) < MODIFF
8049 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8051 /* Gap in the line? */
8052 if (GPT < start || Z - GPT < end)
8053 unchanged_p = 0;
8055 /* Changes start in front of the line, or end after it? */
8056 if (unchanged_p
8057 && (BEG_UNCHANGED < start - 1
8058 || END_UNCHANGED < end))
8059 unchanged_p = 0;
8061 /* If selective display, can't optimize if changes start at the
8062 beginning of the line. */
8063 if (unchanged_p
8064 && INTEGERP (current_buffer->selective_display)
8065 && XINT (current_buffer->selective_display) > 0
8066 && (BEG_UNCHANGED < start || GPT <= start))
8067 unchanged_p = 0;
8070 return unchanged_p;
8074 /* Do a frame update, taking possible shortcuts into account. This is
8075 the main external entry point for redisplay.
8077 If the last redisplay displayed an echo area message and that message
8078 is no longer requested, we clear the echo area or bring back the
8079 mini-buffer if that is in use. */
8081 void
8082 redisplay ()
8084 redisplay_internal (0);
8087 /* Return 1 if point moved out of or into a composition. Otherwise
8088 return 0. PREV_BUF and PREV_PT are the last point buffer and
8089 position. BUF and PT are the current point buffer and position. */
8092 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8093 struct buffer *prev_buf, *buf;
8094 int prev_pt, pt;
8096 int start, end;
8097 Lisp_Object prop;
8098 Lisp_Object buffer;
8100 XSETBUFFER (buffer, buf);
8101 /* Check a composition at the last point if point moved within the
8102 same buffer. */
8103 if (prev_buf == buf)
8105 if (prev_pt == pt)
8106 /* Point didn't move. */
8107 return 0;
8109 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8110 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8111 && COMPOSITION_VALID_P (start, end, prop)
8112 && start < prev_pt && end > prev_pt)
8113 /* The last point was within the composition. Return 1 iff
8114 point moved out of the composition. */
8115 return (pt <= start || pt >= end);
8118 /* Check a composition at the current point. */
8119 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8120 && find_composition (pt, -1, &start, &end, &prop, buffer)
8121 && COMPOSITION_VALID_P (start, end, prop)
8122 && start < pt && end > pt);
8125 /* Reconsider the setting of B->clip_changed which is displayed
8126 in window W. */
8128 static INLINE void
8129 reconsider_clip_changes (w, b)
8130 struct window *w;
8131 struct buffer *b;
8133 if (b->prevent_redisplay_optimizations_p)
8134 b->clip_changed = 1;
8135 else if (b->clip_changed
8136 && !NILP (w->window_end_valid)
8137 && w->current_matrix->buffer == b
8138 && w->current_matrix->zv == BUF_ZV (b)
8139 && w->current_matrix->begv == BUF_BEGV (b))
8140 b->clip_changed = 0;
8142 /* If display wasn't paused, and W is not a tool bar window, see if
8143 point has been moved into or out of a composition. In that case,
8144 we set b->clip_changed to 1 to force updating the screen. If
8145 b->clip_changed has already been set to 1, we can skip this
8146 check. */
8147 if (!b->clip_changed
8148 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8150 int pt;
8152 if (w == XWINDOW (selected_window))
8153 pt = BUF_PT (current_buffer);
8154 else
8155 pt = marker_position (w->pointm);
8157 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8158 || pt != XINT (w->last_point))
8159 && check_point_in_composition (w->current_matrix->buffer,
8160 XINT (w->last_point),
8161 XBUFFER (w->buffer), pt))
8162 b->clip_changed = 1;
8167 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8168 response to any user action; therefore, we should preserve the echo
8169 area. (Actually, our caller does that job.) Perhaps in the future
8170 avoid recentering windows if it is not necessary; currently that
8171 causes some problems. */
8173 static void
8174 redisplay_internal (preserve_echo_area)
8175 int preserve_echo_area;
8177 struct window *w = XWINDOW (selected_window);
8178 struct frame *f = XFRAME (w->frame);
8179 int pause;
8180 int must_finish = 0;
8181 struct text_pos tlbufpos, tlendpos;
8182 int number_of_visible_frames;
8183 int count;
8184 struct frame *sf = SELECTED_FRAME ();
8186 /* Non-zero means redisplay has to consider all windows on all
8187 frames. Zero means, only selected_window is considered. */
8188 int consider_all_windows_p;
8190 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8192 /* No redisplay if running in batch mode or frame is not yet fully
8193 initialized, or redisplay is explicitly turned off by setting
8194 Vinhibit_redisplay. */
8195 if (noninteractive
8196 || !NILP (Vinhibit_redisplay)
8197 || !f->glyphs_initialized_p)
8198 return;
8200 /* The flag redisplay_performed_directly_p is set by
8201 direct_output_for_insert when it already did the whole screen
8202 update necessary. */
8203 if (redisplay_performed_directly_p)
8205 redisplay_performed_directly_p = 0;
8206 if (!hscroll_windows (selected_window))
8207 return;
8210 #ifdef USE_X_TOOLKIT
8211 if (popup_activated ())
8212 return;
8213 #endif
8215 /* I don't think this happens but let's be paranoid. */
8216 if (redisplaying_p)
8217 return;
8219 /* Record a function that resets redisplaying_p to its old value
8220 when we leave this function. */
8221 count = BINDING_STACK_SIZE ();
8222 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8223 ++redisplaying_p;
8225 retry:
8226 pause = 0;
8227 reconsider_clip_changes (w, current_buffer);
8229 /* If new fonts have been loaded that make a glyph matrix adjustment
8230 necessary, do it. */
8231 if (fonts_changed_p)
8233 adjust_glyphs (NULL);
8234 ++windows_or_buffers_changed;
8235 fonts_changed_p = 0;
8238 /* If face_change_count is non-zero, init_iterator will free all
8239 realized faces, which includes the faces referenced from current
8240 matrices. So, we can't reuse current matrices in this case. */
8241 if (face_change_count)
8242 ++windows_or_buffers_changed;
8244 if (! FRAME_WINDOW_P (sf)
8245 && previous_terminal_frame != sf)
8247 /* Since frames on an ASCII terminal share the same display
8248 area, displaying a different frame means redisplay the whole
8249 thing. */
8250 windows_or_buffers_changed++;
8251 SET_FRAME_GARBAGED (sf);
8252 XSETFRAME (Vterminal_frame, sf);
8254 previous_terminal_frame = sf;
8256 /* Set the visible flags for all frames. Do this before checking
8257 for resized or garbaged frames; they want to know if their frames
8258 are visible. See the comment in frame.h for
8259 FRAME_SAMPLE_VISIBILITY. */
8261 Lisp_Object tail, frame;
8263 number_of_visible_frames = 0;
8265 FOR_EACH_FRAME (tail, frame)
8267 struct frame *f = XFRAME (frame);
8269 FRAME_SAMPLE_VISIBILITY (f);
8270 if (FRAME_VISIBLE_P (f))
8271 ++number_of_visible_frames;
8272 clear_desired_matrices (f);
8276 /* Notice any pending interrupt request to change frame size. */
8277 do_pending_window_change (1);
8279 /* Clear frames marked as garbaged. */
8280 if (frame_garbaged)
8281 clear_garbaged_frames ();
8283 /* Build menubar and tool-bar items. */
8284 prepare_menu_bars ();
8286 if (windows_or_buffers_changed)
8287 update_mode_lines++;
8289 /* Detect case that we need to write or remove a star in the mode line. */
8290 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8292 w->update_mode_line = Qt;
8293 if (buffer_shared > 1)
8294 update_mode_lines++;
8297 /* If %c is in the mode line, update it if needed. */
8298 if (!NILP (w->column_number_displayed)
8299 /* This alternative quickly identifies a common case
8300 where no change is needed. */
8301 && !(PT == XFASTINT (w->last_point)
8302 && XFASTINT (w->last_modified) >= MODIFF
8303 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8304 && XFASTINT (w->column_number_displayed) != current_column ())
8305 w->update_mode_line = Qt;
8307 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8309 /* The variable buffer_shared is set in redisplay_window and
8310 indicates that we redisplay a buffer in different windows. See
8311 there. */
8312 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8314 /* If specs for an arrow have changed, do thorough redisplay
8315 to ensure we remove any arrow that should no longer exist. */
8316 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8317 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8318 consider_all_windows_p = windows_or_buffers_changed = 1;
8320 /* Normally the message* functions will have already displayed and
8321 updated the echo area, but the frame may have been trashed, or
8322 the update may have been preempted, so display the echo area
8323 again here. Checking both message buffers captures the case that
8324 the echo area should be cleared. */
8325 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8327 int window_height_changed_p = echo_area_display (0);
8328 must_finish = 1;
8330 if (fonts_changed_p)
8331 goto retry;
8332 else if (window_height_changed_p)
8334 consider_all_windows_p = 1;
8335 ++update_mode_lines;
8336 ++windows_or_buffers_changed;
8338 /* If window configuration was changed, frames may have been
8339 marked garbaged. Clear them or we will experience
8340 surprises wrt scrolling. */
8341 if (frame_garbaged)
8342 clear_garbaged_frames ();
8345 else if (EQ (selected_window, minibuf_window)
8346 && (current_buffer->clip_changed
8347 || XFASTINT (w->last_modified) < MODIFF
8348 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8349 && resize_mini_window (w, 0))
8351 /* Resized active mini-window to fit the size of what it is
8352 showing if its contents might have changed. */
8353 must_finish = 1;
8354 consider_all_windows_p = 1;
8355 ++windows_or_buffers_changed;
8356 ++update_mode_lines;
8358 /* If window configuration was changed, frames may have been
8359 marked garbaged. Clear them or we will experience
8360 surprises wrt scrolling. */
8361 if (frame_garbaged)
8362 clear_garbaged_frames ();
8366 /* If showing the region, and mark has changed, we must redisplay
8367 the whole window. The assignment to this_line_start_pos prevents
8368 the optimization directly below this if-statement. */
8369 if (((!NILP (Vtransient_mark_mode)
8370 && !NILP (XBUFFER (w->buffer)->mark_active))
8371 != !NILP (w->region_showing))
8372 || (!NILP (w->region_showing)
8373 && !EQ (w->region_showing,
8374 Fmarker_position (XBUFFER (w->buffer)->mark))))
8375 CHARPOS (this_line_start_pos) = 0;
8377 /* Optimize the case that only the line containing the cursor in the
8378 selected window has changed. Variables starting with this_ are
8379 set in display_line and record information about the line
8380 containing the cursor. */
8381 tlbufpos = this_line_start_pos;
8382 tlendpos = this_line_end_pos;
8383 if (!consider_all_windows_p
8384 && CHARPOS (tlbufpos) > 0
8385 && NILP (w->update_mode_line)
8386 && !current_buffer->clip_changed
8387 && FRAME_VISIBLE_P (XFRAME (w->frame))
8388 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8389 /* Make sure recorded data applies to current buffer, etc. */
8390 && this_line_buffer == current_buffer
8391 && current_buffer == XBUFFER (w->buffer)
8392 && NILP (w->force_start)
8393 /* Point must be on the line that we have info recorded about. */
8394 && PT >= CHARPOS (tlbufpos)
8395 && PT <= Z - CHARPOS (tlendpos)
8396 /* All text outside that line, including its final newline,
8397 must be unchanged */
8398 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8399 CHARPOS (tlendpos)))
8401 if (CHARPOS (tlbufpos) > BEGV
8402 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8403 && (CHARPOS (tlbufpos) == ZV
8404 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8405 /* Former continuation line has disappeared by becoming empty */
8406 goto cancel;
8407 else if (XFASTINT (w->last_modified) < MODIFF
8408 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8409 || MINI_WINDOW_P (w))
8411 /* We have to handle the case of continuation around a
8412 wide-column character (See the comment in indent.c around
8413 line 885).
8415 For instance, in the following case:
8417 -------- Insert --------
8418 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8419 J_I_ ==> J_I_ `^^' are cursors.
8420 ^^ ^^
8421 -------- --------
8423 As we have to redraw the line above, we should goto cancel. */
8425 struct it it;
8426 int line_height_before = this_line_pixel_height;
8428 /* Note that start_display will handle the case that the
8429 line starting at tlbufpos is a continuation lines. */
8430 start_display (&it, w, tlbufpos);
8432 /* Implementation note: It this still necessary? */
8433 if (it.current_x != this_line_start_x)
8434 goto cancel;
8436 TRACE ((stderr, "trying display optimization 1\n"));
8437 w->cursor.vpos = -1;
8438 overlay_arrow_seen = 0;
8439 it.vpos = this_line_vpos;
8440 it.current_y = this_line_y;
8441 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8442 display_line (&it);
8444 /* If line contains point, is not continued,
8445 and ends at same distance from eob as before, we win */
8446 if (w->cursor.vpos >= 0
8447 /* Line is not continued, otherwise this_line_start_pos
8448 would have been set to 0 in display_line. */
8449 && CHARPOS (this_line_start_pos)
8450 /* Line ends as before. */
8451 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8452 /* Line has same height as before. Otherwise other lines
8453 would have to be shifted up or down. */
8454 && this_line_pixel_height == line_height_before)
8456 /* If this is not the window's last line, we must adjust
8457 the charstarts of the lines below. */
8458 if (it.current_y < it.last_visible_y)
8460 struct glyph_row *row
8461 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8462 int delta, delta_bytes;
8464 if (Z - CHARPOS (tlendpos) == ZV)
8466 /* This line ends at end of (accessible part of)
8467 buffer. There is no newline to count. */
8468 delta = (Z
8469 - CHARPOS (tlendpos)
8470 - MATRIX_ROW_START_CHARPOS (row));
8471 delta_bytes = (Z_BYTE
8472 - BYTEPOS (tlendpos)
8473 - MATRIX_ROW_START_BYTEPOS (row));
8475 else
8477 /* This line ends in a newline. Must take
8478 account of the newline and the rest of the
8479 text that follows. */
8480 delta = (Z
8481 - CHARPOS (tlendpos)
8482 - MATRIX_ROW_START_CHARPOS (row));
8483 delta_bytes = (Z_BYTE
8484 - BYTEPOS (tlendpos)
8485 - MATRIX_ROW_START_BYTEPOS (row));
8488 increment_matrix_positions (w->current_matrix,
8489 this_line_vpos + 1,
8490 w->current_matrix->nrows,
8491 delta, delta_bytes);
8494 /* If this row displays text now but previously didn't,
8495 or vice versa, w->window_end_vpos may have to be
8496 adjusted. */
8497 if ((it.glyph_row - 1)->displays_text_p)
8499 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8500 XSETINT (w->window_end_vpos, this_line_vpos);
8502 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8503 && this_line_vpos > 0)
8504 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8505 w->window_end_valid = Qnil;
8507 /* Update hint: No need to try to scroll in update_window. */
8508 w->desired_matrix->no_scrolling_p = 1;
8510 #if GLYPH_DEBUG
8511 *w->desired_matrix->method = 0;
8512 debug_method_add (w, "optimization 1");
8513 #endif
8514 goto update;
8516 else
8517 goto cancel;
8519 else if (/* Cursor position hasn't changed. */
8520 PT == XFASTINT (w->last_point)
8521 /* Make sure the cursor was last displayed
8522 in this window. Otherwise we have to reposition it. */
8523 && 0 <= w->cursor.vpos
8524 && XINT (w->height) > w->cursor.vpos)
8526 if (!must_finish)
8528 do_pending_window_change (1);
8530 /* We used to always goto end_of_redisplay here, but this
8531 isn't enough if we have a blinking cursor. */
8532 if (w->cursor_off_p == w->last_cursor_off_p)
8533 goto end_of_redisplay;
8535 goto update;
8537 /* If highlighting the region, or if the cursor is in the echo area,
8538 then we can't just move the cursor. */
8539 else if (! (!NILP (Vtransient_mark_mode)
8540 && !NILP (current_buffer->mark_active))
8541 && (EQ (selected_window, current_buffer->last_selected_window)
8542 || highlight_nonselected_windows)
8543 && NILP (w->region_showing)
8544 && NILP (Vshow_trailing_whitespace)
8545 && !cursor_in_echo_area)
8547 struct it it;
8548 struct glyph_row *row;
8550 /* Skip from tlbufpos to PT and see where it is. Note that
8551 PT may be in invisible text. If so, we will end at the
8552 next visible position. */
8553 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8554 NULL, DEFAULT_FACE_ID);
8555 it.current_x = this_line_start_x;
8556 it.current_y = this_line_y;
8557 it.vpos = this_line_vpos;
8559 /* The call to move_it_to stops in front of PT, but
8560 moves over before-strings. */
8561 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8563 if (it.vpos == this_line_vpos
8564 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8565 row->enabled_p))
8567 xassert (this_line_vpos == it.vpos);
8568 xassert (this_line_y == it.current_y);
8569 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8570 #if GLYPH_DEBUG
8571 *w->desired_matrix->method = 0;
8572 debug_method_add (w, "optimization 3");
8573 #endif
8574 goto update;
8576 else
8577 goto cancel;
8580 cancel:
8581 /* Text changed drastically or point moved off of line. */
8582 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8585 CHARPOS (this_line_start_pos) = 0;
8586 consider_all_windows_p |= buffer_shared > 1;
8587 ++clear_face_cache_count;
8590 /* Build desired matrices, and update the display. If
8591 consider_all_windows_p is non-zero, do it for all windows on all
8592 frames. Otherwise do it for selected_window, only. */
8594 if (consider_all_windows_p)
8596 Lisp_Object tail, frame;
8597 int i, n = 0, size = 50;
8598 struct frame **updated
8599 = (struct frame **) alloca (size * sizeof *updated);
8601 /* Clear the face cache eventually. */
8602 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8604 clear_face_cache (0);
8605 clear_face_cache_count = 0;
8608 /* Recompute # windows showing selected buffer. This will be
8609 incremented each time such a window is displayed. */
8610 buffer_shared = 0;
8612 FOR_EACH_FRAME (tail, frame)
8614 struct frame *f = XFRAME (frame);
8616 if (FRAME_WINDOW_P (f) || f == sf)
8618 /* Mark all the scroll bars to be removed; we'll redeem
8619 the ones we want when we redisplay their windows. */
8620 if (condemn_scroll_bars_hook)
8621 condemn_scroll_bars_hook (f);
8623 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8624 redisplay_windows (FRAME_ROOT_WINDOW (f));
8626 /* Any scroll bars which redisplay_windows should have
8627 nuked should now go away. */
8628 if (judge_scroll_bars_hook)
8629 judge_scroll_bars_hook (f);
8631 /* If fonts changed, display again. */
8632 if (fonts_changed_p)
8633 goto retry;
8635 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8637 /* See if we have to hscroll. */
8638 if (hscroll_windows (f->root_window))
8639 goto retry;
8641 /* Prevent various kinds of signals during display
8642 update. stdio is not robust about handling
8643 signals, which can cause an apparent I/O
8644 error. */
8645 if (interrupt_input)
8646 unrequest_sigio ();
8647 stop_polling ();
8649 /* Update the display. */
8650 set_window_update_flags (XWINDOW (f->root_window), 1);
8651 pause |= update_frame (f, 0, 0);
8652 if (pause)
8653 break;
8655 if (n == size)
8657 int nbytes = size * sizeof *updated;
8658 struct frame **p = (struct frame **) alloca (2 * nbytes);
8659 bcopy (updated, p, nbytes);
8660 size *= 2;
8663 updated[n++] = f;
8668 /* Do the mark_window_display_accurate after all windows have
8669 been redisplayed because this call resets flags in buffers
8670 which are needed for proper redisplay. */
8671 for (i = 0; i < n; ++i)
8673 struct frame *f = updated[i];
8674 mark_window_display_accurate (f->root_window, 1);
8675 if (frame_up_to_date_hook)
8676 frame_up_to_date_hook (f);
8679 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8681 Lisp_Object mini_window;
8682 struct frame *mini_frame;
8684 redisplay_window (selected_window, 1);
8686 /* Compare desired and current matrices, perform output. */
8687 update:
8689 /* If fonts changed, display again. */
8690 if (fonts_changed_p)
8691 goto retry;
8693 /* Prevent various kinds of signals during display update.
8694 stdio is not robust about handling signals,
8695 which can cause an apparent I/O error. */
8696 if (interrupt_input)
8697 unrequest_sigio ();
8698 stop_polling ();
8700 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8702 if (hscroll_windows (selected_window))
8703 goto retry;
8705 XWINDOW (selected_window)->must_be_updated_p = 1;
8706 pause = update_frame (sf, 0, 0);
8709 /* We may have called echo_area_display at the top of this
8710 function. If the echo area is on another frame, that may
8711 have put text on a frame other than the selected one, so the
8712 above call to update_frame would not have caught it. Catch
8713 it here. */
8714 mini_window = FRAME_MINIBUF_WINDOW (sf);
8715 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8717 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8719 XWINDOW (mini_window)->must_be_updated_p = 1;
8720 pause |= update_frame (mini_frame, 0, 0);
8721 if (!pause && hscroll_windows (mini_window))
8722 goto retry;
8726 /* If display was paused because of pending input, make sure we do a
8727 thorough update the next time. */
8728 if (pause)
8730 /* Prevent the optimization at the beginning of
8731 redisplay_internal that tries a single-line update of the
8732 line containing the cursor in the selected window. */
8733 CHARPOS (this_line_start_pos) = 0;
8735 /* Let the overlay arrow be updated the next time. */
8736 if (!NILP (last_arrow_position))
8738 last_arrow_position = Qt;
8739 last_arrow_string = Qt;
8742 /* If we pause after scrolling, some rows in the current
8743 matrices of some windows are not valid. */
8744 if (!WINDOW_FULL_WIDTH_P (w)
8745 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8746 update_mode_lines = 1;
8748 else
8750 if (!consider_all_windows_p)
8752 /* This has already been done above if
8753 consider_all_windows_p is set. */
8754 mark_window_display_accurate_1 (w, 1);
8756 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8757 last_arrow_string = Voverlay_arrow_string;
8759 if (frame_up_to_date_hook != 0)
8760 frame_up_to_date_hook (sf);
8763 update_mode_lines = 0;
8764 windows_or_buffers_changed = 0;
8767 /* Start SIGIO interrupts coming again. Having them off during the
8768 code above makes it less likely one will discard output, but not
8769 impossible, since there might be stuff in the system buffer here.
8770 But it is much hairier to try to do anything about that. */
8771 if (interrupt_input)
8772 request_sigio ();
8773 start_polling ();
8775 /* If a frame has become visible which was not before, redisplay
8776 again, so that we display it. Expose events for such a frame
8777 (which it gets when becoming visible) don't call the parts of
8778 redisplay constructing glyphs, so simply exposing a frame won't
8779 display anything in this case. So, we have to display these
8780 frames here explicitly. */
8781 if (!pause)
8783 Lisp_Object tail, frame;
8784 int new_count = 0;
8786 FOR_EACH_FRAME (tail, frame)
8788 int this_is_visible = 0;
8790 if (XFRAME (frame)->visible)
8791 this_is_visible = 1;
8792 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8793 if (XFRAME (frame)->visible)
8794 this_is_visible = 1;
8796 if (this_is_visible)
8797 new_count++;
8800 if (new_count != number_of_visible_frames)
8801 windows_or_buffers_changed++;
8804 /* Change frame size now if a change is pending. */
8805 do_pending_window_change (1);
8807 /* If we just did a pending size change, or have additional
8808 visible frames, redisplay again. */
8809 if (windows_or_buffers_changed && !pause)
8810 goto retry;
8812 end_of_redisplay:;
8814 unbind_to (count, Qnil);
8818 /* Redisplay, but leave alone any recent echo area message unless
8819 another message has been requested in its place.
8821 This is useful in situations where you need to redisplay but no
8822 user action has occurred, making it inappropriate for the message
8823 area to be cleared. See tracking_off and
8824 wait_reading_process_input for examples of these situations.
8826 FROM_WHERE is an integer saying from where this function was
8827 called. This is useful for debugging. */
8829 void
8830 redisplay_preserve_echo_area (from_where)
8831 int from_where;
8833 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8835 if (!NILP (echo_area_buffer[1]))
8837 /* We have a previously displayed message, but no current
8838 message. Redisplay the previous message. */
8839 display_last_displayed_message_p = 1;
8840 redisplay_internal (1);
8841 display_last_displayed_message_p = 0;
8843 else
8844 redisplay_internal (1);
8848 /* Function registered with record_unwind_protect in
8849 redisplay_internal. Clears the flag indicating that a redisplay is
8850 in progress. */
8852 static Lisp_Object
8853 unwind_redisplay (old_redisplaying_p)
8854 Lisp_Object old_redisplaying_p;
8856 redisplaying_p = XFASTINT (old_redisplaying_p);
8857 return Qnil;
8861 /* Mark the display of window W as accurate or inaccurate. If
8862 ACCURATE_P is non-zero mark display of W as accurate. If
8863 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8864 redisplay_internal is called. */
8866 static void
8867 mark_window_display_accurate_1 (w, accurate_p)
8868 struct window *w;
8869 int accurate_p;
8871 if (BUFFERP (w->buffer))
8873 struct buffer *b = XBUFFER (w->buffer);
8875 w->last_modified
8876 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8877 w->last_overlay_modified
8878 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8879 w->last_had_star
8880 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8882 if (accurate_p)
8884 b->clip_changed = 0;
8885 b->prevent_redisplay_optimizations_p = 0;
8887 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8888 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8889 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8890 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8892 w->current_matrix->buffer = b;
8893 w->current_matrix->begv = BUF_BEGV (b);
8894 w->current_matrix->zv = BUF_ZV (b);
8896 w->last_cursor = w->cursor;
8897 w->last_cursor_off_p = w->cursor_off_p;
8899 if (w == XWINDOW (selected_window))
8900 w->last_point = make_number (BUF_PT (b));
8901 else
8902 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8906 if (accurate_p)
8908 w->window_end_valid = w->buffer;
8909 w->update_mode_line = Qnil;
8914 /* Mark the display of windows in the window tree rooted at WINDOW as
8915 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8916 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8917 be redisplayed the next time redisplay_internal is called. */
8919 void
8920 mark_window_display_accurate (window, accurate_p)
8921 Lisp_Object window;
8922 int accurate_p;
8924 struct window *w;
8926 for (; !NILP (window); window = w->next)
8928 w = XWINDOW (window);
8929 mark_window_display_accurate_1 (w, accurate_p);
8931 if (!NILP (w->vchild))
8932 mark_window_display_accurate (w->vchild, accurate_p);
8933 if (!NILP (w->hchild))
8934 mark_window_display_accurate (w->hchild, accurate_p);
8937 if (accurate_p)
8939 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8940 last_arrow_string = Voverlay_arrow_string;
8942 else
8944 /* Force a thorough redisplay the next time by setting
8945 last_arrow_position and last_arrow_string to t, which is
8946 unequal to any useful value of Voverlay_arrow_... */
8947 last_arrow_position = Qt;
8948 last_arrow_string = Qt;
8953 /* Return value in display table DP (Lisp_Char_Table *) for character
8954 C. Since a display table doesn't have any parent, we don't have to
8955 follow parent. Do not call this function directly but use the
8956 macro DISP_CHAR_VECTOR. */
8958 Lisp_Object
8959 disp_char_vector (dp, c)
8960 struct Lisp_Char_Table *dp;
8961 int c;
8963 int code[4], i;
8964 Lisp_Object val;
8966 if (SINGLE_BYTE_CHAR_P (c))
8967 return (dp->contents[c]);
8969 SPLIT_CHAR (c, code[0], code[1], code[2]);
8970 if (code[1] < 32)
8971 code[1] = -1;
8972 else if (code[2] < 32)
8973 code[2] = -1;
8975 /* Here, the possible range of code[0] (== charset ID) is
8976 128..max_charset. Since the top level char table contains data
8977 for multibyte characters after 256th element, we must increment
8978 code[0] by 128 to get a correct index. */
8979 code[0] += 128;
8980 code[3] = -1; /* anchor */
8982 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8984 val = dp->contents[code[i]];
8985 if (!SUB_CHAR_TABLE_P (val))
8986 return (NILP (val) ? dp->defalt : val);
8989 /* Here, val is a sub char table. We return the default value of
8990 it. */
8991 return (dp->defalt);
8996 /***********************************************************************
8997 Window Redisplay
8998 ***********************************************************************/
9000 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9002 static void
9003 redisplay_windows (window)
9004 Lisp_Object window;
9006 while (!NILP (window))
9008 struct window *w = XWINDOW (window);
9010 if (!NILP (w->hchild))
9011 redisplay_windows (w->hchild);
9012 else if (!NILP (w->vchild))
9013 redisplay_windows (w->vchild);
9014 else
9015 redisplay_window (window, 0);
9017 window = w->next;
9022 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9023 DELTA is the number of bytes by which positions recorded in ROW
9024 differ from current buffer positions. */
9026 void
9027 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9028 struct window *w;
9029 struct glyph_row *row;
9030 struct glyph_matrix *matrix;
9031 int delta, delta_bytes, dy, dvpos;
9033 struct glyph *glyph = row->glyphs[TEXT_AREA];
9034 struct glyph *end = glyph + row->used[TEXT_AREA];
9035 int x = row->x;
9036 int pt_old = PT - delta;
9038 /* Skip over glyphs not having an object at the start of the row.
9039 These are special glyphs like truncation marks on terminal
9040 frames. */
9041 if (row->displays_text_p)
9042 while (glyph < end
9043 && INTEGERP (glyph->object)
9044 && glyph->charpos < 0)
9046 x += glyph->pixel_width;
9047 ++glyph;
9050 while (glyph < end
9051 && !INTEGERP (glyph->object)
9052 && (!BUFFERP (glyph->object)
9053 || glyph->charpos < pt_old))
9055 x += glyph->pixel_width;
9056 ++glyph;
9059 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9060 w->cursor.x = x;
9061 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9062 w->cursor.y = row->y + dy;
9064 if (w == XWINDOW (selected_window))
9066 if (!row->continued_p
9067 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9068 && row->x == 0)
9070 this_line_buffer = XBUFFER (w->buffer);
9072 CHARPOS (this_line_start_pos)
9073 = MATRIX_ROW_START_CHARPOS (row) + delta;
9074 BYTEPOS (this_line_start_pos)
9075 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9077 CHARPOS (this_line_end_pos)
9078 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9079 BYTEPOS (this_line_end_pos)
9080 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9082 this_line_y = w->cursor.y;
9083 this_line_pixel_height = row->height;
9084 this_line_vpos = w->cursor.vpos;
9085 this_line_start_x = row->x;
9087 else
9088 CHARPOS (this_line_start_pos) = 0;
9093 /* Run window scroll functions, if any, for WINDOW with new window
9094 start STARTP. Sets the window start of WINDOW to that position.
9096 We assume that the window's buffer is really current. */
9098 static INLINE struct text_pos
9099 run_window_scroll_functions (window, startp)
9100 Lisp_Object window;
9101 struct text_pos startp;
9103 struct window *w = XWINDOW (window);
9104 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9106 if (current_buffer != XBUFFER (w->buffer))
9107 abort ();
9109 if (!NILP (Vwindow_scroll_functions))
9111 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9112 make_number (CHARPOS (startp)));
9113 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9114 /* In case the hook functions switch buffers. */
9115 if (current_buffer != XBUFFER (w->buffer))
9116 set_buffer_internal_1 (XBUFFER (w->buffer));
9119 return startp;
9123 /* Modify the desired matrix of window W and W->vscroll so that the
9124 line containing the cursor is fully visible. */
9126 static void
9127 make_cursor_line_fully_visible (w)
9128 struct window *w;
9130 struct glyph_matrix *matrix;
9131 struct glyph_row *row;
9132 int window_height;
9134 /* It's not always possible to find the cursor, e.g, when a window
9135 is full of overlay strings. Don't do anything in that case. */
9136 if (w->cursor.vpos < 0)
9137 return;
9139 matrix = w->desired_matrix;
9140 row = MATRIX_ROW (matrix, w->cursor.vpos);
9142 /* If the cursor row is not partially visible, there's nothing
9143 to do. */
9144 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9145 return;
9147 /* If the row the cursor is in is taller than the window's height,
9148 it's not clear what to do, so do nothing. */
9149 window_height = window_box_height (w);
9150 if (row->height >= window_height)
9151 return;
9153 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9155 int dy = row->height - row->visible_height;
9156 w->vscroll = 0;
9157 w->cursor.y += dy;
9158 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9160 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9162 int dy = - (row->height - row->visible_height);
9163 w->vscroll = dy;
9164 w->cursor.y += dy;
9165 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9168 /* When we change the cursor y-position of the selected window,
9169 change this_line_y as well so that the display optimization for
9170 the cursor line of the selected window in redisplay_internal uses
9171 the correct y-position. */
9172 if (w == XWINDOW (selected_window))
9173 this_line_y = w->cursor.y;
9177 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9178 non-zero means only WINDOW is redisplayed in redisplay_internal.
9179 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9180 in redisplay_window to bring a partially visible line into view in
9181 the case that only the cursor has moved.
9183 Value is
9185 1 if scrolling succeeded
9187 0 if scrolling didn't find point.
9189 -1 if new fonts have been loaded so that we must interrupt
9190 redisplay, adjust glyph matrices, and try again. */
9192 static int
9193 try_scrolling (window, just_this_one_p, scroll_conservatively,
9194 scroll_step, temp_scroll_step)
9195 Lisp_Object window;
9196 int just_this_one_p;
9197 int scroll_conservatively, scroll_step;
9198 int temp_scroll_step;
9200 struct window *w = XWINDOW (window);
9201 struct frame *f = XFRAME (w->frame);
9202 struct text_pos scroll_margin_pos;
9203 struct text_pos pos;
9204 struct text_pos startp;
9205 struct it it;
9206 Lisp_Object window_end;
9207 int this_scroll_margin;
9208 int dy = 0;
9209 int scroll_max;
9210 int rc;
9211 int amount_to_scroll = 0;
9212 Lisp_Object aggressive;
9213 int height;
9215 #if GLYPH_DEBUG
9216 debug_method_add (w, "try_scrolling");
9217 #endif
9219 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9221 /* Compute scroll margin height in pixels. We scroll when point is
9222 within this distance from the top or bottom of the window. */
9223 if (scroll_margin > 0)
9225 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9226 this_scroll_margin *= CANON_Y_UNIT (f);
9228 else
9229 this_scroll_margin = 0;
9231 /* Compute how much we should try to scroll maximally to bring point
9232 into view. */
9233 if (scroll_step || scroll_conservatively || temp_scroll_step)
9234 scroll_max = max (scroll_step,
9235 max (scroll_conservatively, temp_scroll_step));
9236 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9237 || NUMBERP (current_buffer->scroll_up_aggressively))
9238 /* We're trying to scroll because of aggressive scrolling
9239 but no scroll_step is set. Choose an arbitrary one. Maybe
9240 there should be a variable for this. */
9241 scroll_max = 10;
9242 else
9243 scroll_max = 0;
9244 scroll_max *= CANON_Y_UNIT (f);
9246 /* Decide whether we have to scroll down. Start at the window end
9247 and move this_scroll_margin up to find the position of the scroll
9248 margin. */
9249 window_end = Fwindow_end (window, Qt);
9250 CHARPOS (scroll_margin_pos) = XINT (window_end);
9251 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9252 if (this_scroll_margin)
9254 start_display (&it, w, scroll_margin_pos);
9255 move_it_vertically (&it, - this_scroll_margin);
9256 scroll_margin_pos = it.current.pos;
9259 if (PT >= CHARPOS (scroll_margin_pos))
9261 int y0;
9263 /* Point is in the scroll margin at the bottom of the window, or
9264 below. Compute a new window start that makes point visible. */
9266 /* Compute the distance from the scroll margin to PT.
9267 Give up if the distance is greater than scroll_max. */
9268 start_display (&it, w, scroll_margin_pos);
9269 y0 = it.current_y;
9270 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9271 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9273 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9274 end, which is one line below the window. The iterator's
9275 current_y will be same as y0 in that case, but we have to
9276 scroll a line to make PT visible. That's the reason why 1 is
9277 added below. */
9278 dy = 1 + it.current_y - y0;
9280 if (dy > scroll_max)
9281 return 0;
9283 /* Move the window start down. If scrolling conservatively,
9284 move it just enough down to make point visible. If
9285 scroll_step is set, move it down by scroll_step. */
9286 start_display (&it, w, startp);
9288 if (scroll_conservatively)
9289 amount_to_scroll
9290 = max (max (dy, CANON_Y_UNIT (f)),
9291 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9292 else if (scroll_step || temp_scroll_step)
9293 amount_to_scroll = scroll_max;
9294 else
9296 aggressive = current_buffer->scroll_down_aggressively;
9297 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9298 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9299 if (NUMBERP (aggressive))
9300 amount_to_scroll = XFLOATINT (aggressive) * height;
9303 if (amount_to_scroll <= 0)
9304 return 0;
9306 move_it_vertically (&it, amount_to_scroll);
9307 startp = it.current.pos;
9309 else
9311 /* See if point is inside the scroll margin at the top of the
9312 window. */
9313 scroll_margin_pos = startp;
9314 if (this_scroll_margin)
9316 start_display (&it, w, startp);
9317 move_it_vertically (&it, this_scroll_margin);
9318 scroll_margin_pos = it.current.pos;
9321 if (PT < CHARPOS (scroll_margin_pos))
9323 /* Point is in the scroll margin at the top of the window or
9324 above what is displayed in the window. */
9325 int y0;
9327 /* Compute the vertical distance from PT to the scroll
9328 margin position. Give up if distance is greater than
9329 scroll_max. */
9330 SET_TEXT_POS (pos, PT, PT_BYTE);
9331 start_display (&it, w, pos);
9332 y0 = it.current_y;
9333 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9334 it.last_visible_y, -1,
9335 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9336 dy = it.current_y - y0;
9337 if (dy > scroll_max)
9338 return 0;
9340 /* Compute new window start. */
9341 start_display (&it, w, startp);
9343 if (scroll_conservatively)
9344 amount_to_scroll =
9345 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9346 else if (scroll_step || temp_scroll_step)
9347 amount_to_scroll = scroll_max;
9348 else
9350 aggressive = current_buffer->scroll_up_aggressively;
9351 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9352 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9353 if (NUMBERP (aggressive))
9354 amount_to_scroll = XFLOATINT (aggressive) * height;
9357 if (amount_to_scroll <= 0)
9358 return 0;
9360 move_it_vertically (&it, - amount_to_scroll);
9361 startp = it.current.pos;
9365 /* Run window scroll functions. */
9366 startp = run_window_scroll_functions (window, startp);
9368 /* Display the window. Give up if new fonts are loaded, or if point
9369 doesn't appear. */
9370 if (!try_window (window, startp))
9371 rc = -1;
9372 else if (w->cursor.vpos < 0)
9374 clear_glyph_matrix (w->desired_matrix);
9375 rc = 0;
9377 else
9379 /* Maybe forget recorded base line for line number display. */
9380 if (!just_this_one_p
9381 || current_buffer->clip_changed
9382 || BEG_UNCHANGED < CHARPOS (startp))
9383 w->base_line_number = Qnil;
9385 /* If cursor ends up on a partially visible line, shift display
9386 lines up or down. */
9387 make_cursor_line_fully_visible (w);
9388 rc = 1;
9391 return rc;
9395 /* Compute a suitable window start for window W if display of W starts
9396 on a continuation line. Value is non-zero if a new window start
9397 was computed.
9399 The new window start will be computed, based on W's width, starting
9400 from the start of the continued line. It is the start of the
9401 screen line with the minimum distance from the old start W->start. */
9403 static int
9404 compute_window_start_on_continuation_line (w)
9405 struct window *w;
9407 struct text_pos pos, start_pos;
9408 int window_start_changed_p = 0;
9410 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9412 /* If window start is on a continuation line... Window start may be
9413 < BEGV in case there's invisible text at the start of the
9414 buffer (M-x rmail, for example). */
9415 if (CHARPOS (start_pos) > BEGV
9416 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9418 struct it it;
9419 struct glyph_row *row;
9421 /* Handle the case that the window start is out of range. */
9422 if (CHARPOS (start_pos) < BEGV)
9423 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9424 else if (CHARPOS (start_pos) > ZV)
9425 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9427 /* Find the start of the continued line. This should be fast
9428 because scan_buffer is fast (newline cache). */
9429 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9430 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9431 row, DEFAULT_FACE_ID);
9432 reseat_at_previous_visible_line_start (&it);
9434 /* If the line start is "too far" away from the window start,
9435 say it takes too much time to compute a new window start. */
9436 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9437 < XFASTINT (w->height) * XFASTINT (w->width))
9439 int min_distance, distance;
9441 /* Move forward by display lines to find the new window
9442 start. If window width was enlarged, the new start can
9443 be expected to be > the old start. If window width was
9444 decreased, the new window start will be < the old start.
9445 So, we're looking for the display line start with the
9446 minimum distance from the old window start. */
9447 pos = it.current.pos;
9448 min_distance = INFINITY;
9449 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9450 distance < min_distance)
9452 min_distance = distance;
9453 pos = it.current.pos;
9454 move_it_by_lines (&it, 1, 0);
9457 /* Set the window start there. */
9458 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9459 window_start_changed_p = 1;
9463 return window_start_changed_p;
9467 /* Try cursor movement in case text has not changes in window WINDOW,
9468 with window start STARTP. Value is
9470 1 if successful
9472 0 if this method cannot be used
9474 -1 if we know we have to scroll the display. *SCROLL_STEP is
9475 set to 1, under certain circumstances, if we want to scroll as
9476 if scroll-step were set to 1. See the code. */
9478 static int
9479 try_cursor_movement (window, startp, scroll_step)
9480 Lisp_Object window;
9481 struct text_pos startp;
9482 int *scroll_step;
9484 struct window *w = XWINDOW (window);
9485 struct frame *f = XFRAME (w->frame);
9486 int rc = 0;
9488 /* Handle case where text has not changed, only point, and it has
9489 not moved off the frame. */
9490 if (/* Point may be in this window. */
9491 PT >= CHARPOS (startp)
9492 /* Selective display hasn't changed. */
9493 && !current_buffer->clip_changed
9494 /* Function force-mode-line-update is used to force a thorough
9495 redisplay. It sets either windows_or_buffers_changed or
9496 update_mode_lines. So don't take a shortcut here for these
9497 cases. */
9498 && !update_mode_lines
9499 && !windows_or_buffers_changed
9500 /* Can't use this case if highlighting a region. When a
9501 region exists, cursor movement has to do more than just
9502 set the cursor. */
9503 && !(!NILP (Vtransient_mark_mode)
9504 && !NILP (current_buffer->mark_active))
9505 && NILP (w->region_showing)
9506 && NILP (Vshow_trailing_whitespace)
9507 /* Right after splitting windows, last_point may be nil. */
9508 && INTEGERP (w->last_point)
9509 /* This code is not used for mini-buffer for the sake of the case
9510 of redisplaying to replace an echo area message; since in
9511 that case the mini-buffer contents per se are usually
9512 unchanged. This code is of no real use in the mini-buffer
9513 since the handling of this_line_start_pos, etc., in redisplay
9514 handles the same cases. */
9515 && !EQ (window, minibuf_window)
9516 /* When splitting windows or for new windows, it happens that
9517 redisplay is called with a nil window_end_vpos or one being
9518 larger than the window. This should really be fixed in
9519 window.c. I don't have this on my list, now, so we do
9520 approximately the same as the old redisplay code. --gerd. */
9521 && INTEGERP (w->window_end_vpos)
9522 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9523 && (FRAME_WINDOW_P (f)
9524 || !MARKERP (Voverlay_arrow_position)
9525 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9527 int this_scroll_margin;
9528 struct glyph_row *row;
9530 #if GLYPH_DEBUG
9531 debug_method_add (w, "cursor movement");
9532 #endif
9534 /* Scroll if point within this distance from the top or bottom
9535 of the window. This is a pixel value. */
9536 this_scroll_margin = max (0, scroll_margin);
9537 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9538 this_scroll_margin *= CANON_Y_UNIT (f);
9540 /* Start with the row the cursor was displayed during the last
9541 not paused redisplay. Give up if that row is not valid. */
9542 if (w->last_cursor.vpos < 0
9543 || w->last_cursor.vpos >= w->current_matrix->nrows)
9544 rc = -1;
9545 else
9547 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9548 if (row->mode_line_p)
9549 ++row;
9550 if (!row->enabled_p)
9551 rc = -1;
9554 if (rc == 0)
9556 int scroll_p = 0;
9557 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9559 if (PT > XFASTINT (w->last_point))
9561 /* Point has moved forward. */
9562 while (MATRIX_ROW_END_CHARPOS (row) < PT
9563 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9565 xassert (row->enabled_p);
9566 ++row;
9569 /* The end position of a row equals the start position
9570 of the next row. If PT is there, we would rather
9571 display it in the next line. */
9572 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9573 && MATRIX_ROW_END_CHARPOS (row) == PT
9574 && !cursor_row_p (w, row))
9575 ++row;
9577 /* If within the scroll margin, scroll. Note that
9578 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9579 the next line would be drawn, and that
9580 this_scroll_margin can be zero. */
9581 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9582 || PT > MATRIX_ROW_END_CHARPOS (row)
9583 /* Line is completely visible last line in window
9584 and PT is to be set in the next line. */
9585 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9586 && PT == MATRIX_ROW_END_CHARPOS (row)
9587 && !row->ends_at_zv_p
9588 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9589 scroll_p = 1;
9591 else if (PT < XFASTINT (w->last_point))
9593 /* Cursor has to be moved backward. Note that PT >=
9594 CHARPOS (startp) because of the outer
9595 if-statement. */
9596 while (!row->mode_line_p
9597 && (MATRIX_ROW_START_CHARPOS (row) > PT
9598 || (MATRIX_ROW_START_CHARPOS (row) == PT
9599 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9600 && (row->y > this_scroll_margin
9601 || CHARPOS (startp) == BEGV))
9603 xassert (row->enabled_p);
9604 --row;
9607 /* Consider the following case: Window starts at BEGV,
9608 there is invisible, intangible text at BEGV, so that
9609 display starts at some point START > BEGV. It can
9610 happen that we are called with PT somewhere between
9611 BEGV and START. Try to handle that case. */
9612 if (row < w->current_matrix->rows
9613 || row->mode_line_p)
9615 row = w->current_matrix->rows;
9616 if (row->mode_line_p)
9617 ++row;
9620 /* Due to newlines in overlay strings, we may have to
9621 skip forward over overlay strings. */
9622 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9623 && MATRIX_ROW_END_CHARPOS (row) == PT
9624 && !cursor_row_p (w, row))
9625 ++row;
9627 /* If within the scroll margin, scroll. */
9628 if (row->y < this_scroll_margin
9629 && CHARPOS (startp) != BEGV)
9630 scroll_p = 1;
9633 if (PT < MATRIX_ROW_START_CHARPOS (row)
9634 || PT > MATRIX_ROW_END_CHARPOS (row))
9636 /* if PT is not in the glyph row, give up. */
9637 rc = -1;
9639 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9641 if (PT == MATRIX_ROW_END_CHARPOS (row)
9642 && !row->ends_at_zv_p
9643 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9644 rc = -1;
9645 else if (row->height > window_box_height (w))
9647 /* If we end up in a partially visible line, let's
9648 make it fully visible, except when it's taller
9649 than the window, in which case we can't do much
9650 about it. */
9651 *scroll_step = 1;
9652 rc = -1;
9654 else
9656 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9657 try_window (window, startp);
9658 make_cursor_line_fully_visible (w);
9659 rc = 1;
9662 else if (scroll_p)
9663 rc = -1;
9664 else
9666 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9667 rc = 1;
9672 return rc;
9676 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9677 selected_window is redisplayed. */
9679 static void
9680 redisplay_window (window, just_this_one_p)
9681 Lisp_Object window;
9682 int just_this_one_p;
9684 struct window *w = XWINDOW (window);
9685 struct frame *f = XFRAME (w->frame);
9686 struct buffer *buffer = XBUFFER (w->buffer);
9687 struct buffer *old = current_buffer;
9688 struct text_pos lpoint, opoint, startp;
9689 int update_mode_line;
9690 int tem;
9691 struct it it;
9692 /* Record it now because it's overwritten. */
9693 int current_matrix_up_to_date_p = 0;
9694 int temp_scroll_step = 0;
9695 int count = BINDING_STACK_SIZE ();
9696 int rc;
9698 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9699 opoint = lpoint;
9701 /* W must be a leaf window here. */
9702 xassert (!NILP (w->buffer));
9703 #if GLYPH_DEBUG
9704 *w->desired_matrix->method = 0;
9705 #endif
9707 specbind (Qinhibit_point_motion_hooks, Qt);
9709 reconsider_clip_changes (w, buffer);
9711 /* Has the mode line to be updated? */
9712 update_mode_line = (!NILP (w->update_mode_line)
9713 || update_mode_lines
9714 || buffer->clip_changed);
9716 if (MINI_WINDOW_P (w))
9718 if (w == XWINDOW (echo_area_window)
9719 && !NILP (echo_area_buffer[0]))
9721 if (update_mode_line)
9722 /* We may have to update a tty frame's menu bar or a
9723 tool-bar. Example `M-x C-h C-h C-g'. */
9724 goto finish_menu_bars;
9725 else
9726 /* We've already displayed the echo area glyphs in this window. */
9727 goto finish_scroll_bars;
9729 else if (w != XWINDOW (minibuf_window))
9731 /* W is a mini-buffer window, but it's not the currently
9732 active one, so clear it. */
9733 int yb = window_text_bottom_y (w);
9734 struct glyph_row *row;
9735 int y;
9737 for (y = 0, row = w->desired_matrix->rows;
9738 y < yb;
9739 y += row->height, ++row)
9740 blank_row (w, row, y);
9741 goto finish_scroll_bars;
9745 /* Otherwise set up data on this window; select its buffer and point
9746 value. */
9747 /* Really select the buffer, for the sake of buffer-local
9748 variables. */
9749 set_buffer_internal_1 (XBUFFER (w->buffer));
9750 SET_TEXT_POS (opoint, PT, PT_BYTE);
9752 current_matrix_up_to_date_p
9753 = (!NILP (w->window_end_valid)
9754 && !current_buffer->clip_changed
9755 && XFASTINT (w->last_modified) >= MODIFF
9756 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9758 /* When windows_or_buffers_changed is non-zero, we can't rely on
9759 the window end being valid, so set it to nil there. */
9760 if (windows_or_buffers_changed)
9762 /* If window starts on a continuation line, maybe adjust the
9763 window start in case the window's width changed. */
9764 if (XMARKER (w->start)->buffer == current_buffer)
9765 compute_window_start_on_continuation_line (w);
9767 w->window_end_valid = Qnil;
9770 /* Some sanity checks. */
9771 CHECK_WINDOW_END (w);
9772 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9773 abort ();
9774 if (BYTEPOS (opoint) < CHARPOS (opoint))
9775 abort ();
9777 /* If %c is in mode line, update it if needed. */
9778 if (!NILP (w->column_number_displayed)
9779 /* This alternative quickly identifies a common case
9780 where no change is needed. */
9781 && !(PT == XFASTINT (w->last_point)
9782 && XFASTINT (w->last_modified) >= MODIFF
9783 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9784 && XFASTINT (w->column_number_displayed) != current_column ())
9785 update_mode_line = 1;
9787 /* Count number of windows showing the selected buffer. An indirect
9788 buffer counts as its base buffer. */
9789 if (!just_this_one_p)
9791 struct buffer *current_base, *window_base;
9792 current_base = current_buffer;
9793 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9794 if (current_base->base_buffer)
9795 current_base = current_base->base_buffer;
9796 if (window_base->base_buffer)
9797 window_base = window_base->base_buffer;
9798 if (current_base == window_base)
9799 buffer_shared++;
9802 /* Point refers normally to the selected window. For any other
9803 window, set up appropriate value. */
9804 if (!EQ (window, selected_window))
9806 int new_pt = XMARKER (w->pointm)->charpos;
9807 int new_pt_byte = marker_byte_position (w->pointm);
9808 if (new_pt < BEGV)
9810 new_pt = BEGV;
9811 new_pt_byte = BEGV_BYTE;
9812 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9814 else if (new_pt > (ZV - 1))
9816 new_pt = ZV;
9817 new_pt_byte = ZV_BYTE;
9818 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9821 /* We don't use SET_PT so that the point-motion hooks don't run. */
9822 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9825 /* If any of the character widths specified in the display table
9826 have changed, invalidate the width run cache. It's true that
9827 this may be a bit late to catch such changes, but the rest of
9828 redisplay goes (non-fatally) haywire when the display table is
9829 changed, so why should we worry about doing any better? */
9830 if (current_buffer->width_run_cache)
9832 struct Lisp_Char_Table *disptab = buffer_display_table ();
9834 if (! disptab_matches_widthtab (disptab,
9835 XVECTOR (current_buffer->width_table)))
9837 invalidate_region_cache (current_buffer,
9838 current_buffer->width_run_cache,
9839 BEG, Z);
9840 recompute_width_table (current_buffer, disptab);
9844 /* If window-start is screwed up, choose a new one. */
9845 if (XMARKER (w->start)->buffer != current_buffer)
9846 goto recenter;
9848 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9850 /* If someone specified a new starting point but did not insist,
9851 check whether it can be used. */
9852 if (!NILP (w->optional_new_start)
9853 && CHARPOS (startp) >= BEGV
9854 && CHARPOS (startp) <= ZV)
9856 w->optional_new_start = Qnil;
9857 start_display (&it, w, startp);
9858 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9859 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9860 if (IT_CHARPOS (it) == PT)
9861 w->force_start = Qt;
9864 /* Handle case where place to start displaying has been specified,
9865 unless the specified location is outside the accessible range. */
9866 if (!NILP (w->force_start)
9867 || w->frozen_window_start_p)
9869 w->force_start = Qnil;
9870 w->vscroll = 0;
9871 w->window_end_valid = Qnil;
9873 /* Forget any recorded base line for line number display. */
9874 if (!current_matrix_up_to_date_p
9875 || current_buffer->clip_changed)
9876 w->base_line_number = Qnil;
9878 /* Redisplay the mode line. Select the buffer properly for that.
9879 Also, run the hook window-scroll-functions
9880 because we have scrolled. */
9881 /* Note, we do this after clearing force_start because
9882 if there's an error, it is better to forget about force_start
9883 than to get into an infinite loop calling the hook functions
9884 and having them get more errors. */
9885 if (!update_mode_line
9886 || ! NILP (Vwindow_scroll_functions))
9888 update_mode_line = 1;
9889 w->update_mode_line = Qt;
9890 startp = run_window_scroll_functions (window, startp);
9893 w->last_modified = make_number (0);
9894 w->last_overlay_modified = make_number (0);
9895 if (CHARPOS (startp) < BEGV)
9896 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9897 else if (CHARPOS (startp) > ZV)
9898 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9900 /* Redisplay, then check if cursor has been set during the
9901 redisplay. Give up if new fonts were loaded. */
9902 if (!try_window (window, startp))
9904 w->force_start = Qt;
9905 clear_glyph_matrix (w->desired_matrix);
9906 goto finish_scroll_bars;
9909 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9911 /* If point does not appear, try to move point so it does
9912 appear. The desired matrix has been built above, so we
9913 can use it here. */
9914 int window_height;
9915 struct glyph_row *row;
9917 window_height = window_box_height (w) / 2;
9918 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9919 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9920 ++row;
9922 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9923 MATRIX_ROW_START_BYTEPOS (row));
9925 if (w != XWINDOW (selected_window))
9926 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9927 else if (current_buffer == old)
9928 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9930 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9932 /* If we are highlighting the region, then we just changed
9933 the region, so redisplay to show it. */
9934 if (!NILP (Vtransient_mark_mode)
9935 && !NILP (current_buffer->mark_active))
9937 clear_glyph_matrix (w->desired_matrix);
9938 if (!try_window (window, startp))
9939 goto finish_scroll_bars;
9943 make_cursor_line_fully_visible (w);
9944 #if GLYPH_DEBUG
9945 debug_method_add (w, "forced window start");
9946 #endif
9947 goto done;
9950 /* Handle case where text has not changed, only point, and it has
9951 not moved off the frame. */
9952 if (current_matrix_up_to_date_p
9953 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9954 rc != 0))
9956 if (rc == -1)
9957 goto try_to_scroll;
9958 else
9959 goto done;
9961 /* If current starting point was originally the beginning of a line
9962 but no longer is, find a new starting point. */
9963 else if (!NILP (w->start_at_line_beg)
9964 && !(CHARPOS (startp) <= BEGV
9965 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9967 #if GLYPH_DEBUG
9968 debug_method_add (w, "recenter 1");
9969 #endif
9970 goto recenter;
9973 /* Try scrolling with try_window_id. */
9974 else if (/* Windows and buffers haven't changed. */
9975 !windows_or_buffers_changed
9976 /* Window must be either use window-based redisplay or
9977 be full width. */
9978 && (FRAME_WINDOW_P (f)
9979 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9980 && !MINI_WINDOW_P (w)
9981 /* Point is not known NOT to appear in window. */
9982 && PT >= CHARPOS (startp)
9983 && XFASTINT (w->last_modified)
9984 /* Window is not hscrolled. */
9985 && XFASTINT (w->hscroll) == 0
9986 /* Selective display has not changed. */
9987 && !current_buffer->clip_changed
9988 /* Current matrix is up to date. */
9989 && !NILP (w->window_end_valid)
9990 /* Can't use this case if highlighting a region because
9991 a cursor movement will do more than just set the cursor. */
9992 && !(!NILP (Vtransient_mark_mode)
9993 && !NILP (current_buffer->mark_active))
9994 && NILP (w->region_showing)
9995 && NILP (Vshow_trailing_whitespace)
9996 /* Overlay arrow position and string not changed. */
9997 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9998 && EQ (last_arrow_string, Voverlay_arrow_string)
9999 /* Value is > 0 if update has been done, it is -1 if we
10000 know that the same window start will not work. It is 0
10001 if unsuccessful for some other reason. */
10002 && (tem = try_window_id (w)) != 0)
10004 #if GLYPH_DEBUG
10005 debug_method_add (w, "try_window_id %d", tem);
10006 #endif
10008 if (fonts_changed_p)
10009 goto finish_scroll_bars;
10010 if (tem > 0)
10011 goto done;
10013 /* Otherwise try_window_id has returned -1 which means that we
10014 don't want the alternative below this comment to execute. */
10016 else if (CHARPOS (startp) >= BEGV
10017 && CHARPOS (startp) <= ZV
10018 && PT >= CHARPOS (startp)
10019 && (CHARPOS (startp) < ZV
10020 /* Avoid starting at end of buffer. */
10021 || CHARPOS (startp) == BEGV
10022 || (XFASTINT (w->last_modified) >= MODIFF
10023 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10025 #if GLYPH_DEBUG
10026 debug_method_add (w, "same window start");
10027 #endif
10029 /* Try to redisplay starting at same place as before.
10030 If point has not moved off frame, accept the results. */
10031 if (!current_matrix_up_to_date_p
10032 /* Don't use try_window_reusing_current_matrix in this case
10033 because a window scroll function can have changed the
10034 buffer. */
10035 || !NILP (Vwindow_scroll_functions)
10036 || MINI_WINDOW_P (w)
10037 || !try_window_reusing_current_matrix (w))
10039 IF_DEBUG (debug_method_add (w, "1"));
10040 try_window (window, startp);
10043 if (fonts_changed_p)
10044 goto finish_scroll_bars;
10046 if (w->cursor.vpos >= 0)
10048 if (!just_this_one_p
10049 || current_buffer->clip_changed
10050 || BEG_UNCHANGED < CHARPOS (startp))
10051 /* Forget any recorded base line for line number display. */
10052 w->base_line_number = Qnil;
10054 make_cursor_line_fully_visible (w);
10055 goto done;
10057 else
10058 clear_glyph_matrix (w->desired_matrix);
10061 try_to_scroll:
10063 w->last_modified = make_number (0);
10064 w->last_overlay_modified = make_number (0);
10066 /* Redisplay the mode line. Select the buffer properly for that. */
10067 if (!update_mode_line)
10069 update_mode_line = 1;
10070 w->update_mode_line = Qt;
10073 /* Try to scroll by specified few lines. */
10074 if ((scroll_conservatively
10075 || scroll_step
10076 || temp_scroll_step
10077 || NUMBERP (current_buffer->scroll_up_aggressively)
10078 || NUMBERP (current_buffer->scroll_down_aggressively))
10079 && !current_buffer->clip_changed
10080 && CHARPOS (startp) >= BEGV
10081 && CHARPOS (startp) <= ZV)
10083 /* The function returns -1 if new fonts were loaded, 1 if
10084 successful, 0 if not successful. */
10085 int rc = try_scrolling (window, just_this_one_p,
10086 scroll_conservatively,
10087 scroll_step,
10088 temp_scroll_step);
10089 if (rc > 0)
10090 goto done;
10091 else if (rc < 0)
10092 goto finish_scroll_bars;
10095 /* Finally, just choose place to start which centers point */
10097 recenter:
10099 #if GLYPH_DEBUG
10100 debug_method_add (w, "recenter");
10101 #endif
10103 /* w->vscroll = 0; */
10105 /* Forget any previously recorded base line for line number display. */
10106 if (!current_matrix_up_to_date_p
10107 || current_buffer->clip_changed)
10108 w->base_line_number = Qnil;
10110 /* Move backward half the height of the window. */
10111 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10112 it.current_y = it.last_visible_y;
10113 move_it_vertically_backward (&it, it.last_visible_y / 2);
10114 xassert (IT_CHARPOS (it) >= BEGV);
10116 /* The function move_it_vertically_backward may move over more
10117 than the specified y-distance. If it->w is small, e.g. a
10118 mini-buffer window, we may end up in front of the window's
10119 display area. Start displaying at the start of the line
10120 containing PT in this case. */
10121 if (it.current_y <= 0)
10123 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10124 move_it_vertically (&it, 0);
10125 xassert (IT_CHARPOS (it) <= PT);
10126 it.current_y = 0;
10129 it.current_x = it.hpos = 0;
10131 /* Set startp here explicitly in case that helps avoid an infinite loop
10132 in case the window-scroll-functions functions get errors. */
10133 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10135 /* Run scroll hooks. */
10136 startp = run_window_scroll_functions (window, it.current.pos);
10138 /* Redisplay the window. */
10139 if (!current_matrix_up_to_date_p
10140 || windows_or_buffers_changed
10141 /* Don't use try_window_reusing_current_matrix in this case
10142 because it can have changed the buffer. */
10143 || !NILP (Vwindow_scroll_functions)
10144 || !just_this_one_p
10145 || MINI_WINDOW_P (w)
10146 || !try_window_reusing_current_matrix (w))
10147 try_window (window, startp);
10149 /* If new fonts have been loaded (due to fontsets), give up. We
10150 have to start a new redisplay since we need to re-adjust glyph
10151 matrices. */
10152 if (fonts_changed_p)
10153 goto finish_scroll_bars;
10155 /* If cursor did not appear assume that the middle of the window is
10156 in the first line of the window. Do it again with the next line.
10157 (Imagine a window of height 100, displaying two lines of height
10158 60. Moving back 50 from it->last_visible_y will end in the first
10159 line.) */
10160 if (w->cursor.vpos < 0)
10162 if (!NILP (w->window_end_valid)
10163 && PT >= Z - XFASTINT (w->window_end_pos))
10165 clear_glyph_matrix (w->desired_matrix);
10166 move_it_by_lines (&it, 1, 0);
10167 try_window (window, it.current.pos);
10169 else if (PT < IT_CHARPOS (it))
10171 clear_glyph_matrix (w->desired_matrix);
10172 move_it_by_lines (&it, -1, 0);
10173 try_window (window, it.current.pos);
10175 else
10177 /* Not much we can do about it. */
10181 /* Consider the following case: Window starts at BEGV, there is
10182 invisible, intangible text at BEGV, so that display starts at
10183 some point START > BEGV. It can happen that we are called with
10184 PT somewhere between BEGV and START. Try to handle that case. */
10185 if (w->cursor.vpos < 0)
10187 struct glyph_row *row = w->current_matrix->rows;
10188 if (row->mode_line_p)
10189 ++row;
10190 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10193 make_cursor_line_fully_visible (w);
10195 done:
10197 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10198 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10199 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10200 ? Qt : Qnil);
10202 /* Display the mode line, if we must. */
10203 if ((update_mode_line
10204 /* If window not full width, must redo its mode line
10205 if (a) the window to its side is being redone and
10206 (b) we do a frame-based redisplay. This is a consequence
10207 of how inverted lines are drawn in frame-based redisplay. */
10208 || (!just_this_one_p
10209 && !FRAME_WINDOW_P (f)
10210 && !WINDOW_FULL_WIDTH_P (w))
10211 /* Line number to display. */
10212 || INTEGERP (w->base_line_pos)
10213 /* Column number is displayed and different from the one displayed. */
10214 || (!NILP (w->column_number_displayed)
10215 && XFASTINT (w->column_number_displayed) != current_column ()))
10216 /* This means that the window has a mode line. */
10217 && (WINDOW_WANTS_MODELINE_P (w)
10218 || WINDOW_WANTS_HEADER_LINE_P (w)))
10220 Lisp_Object old_selected_frame;
10222 old_selected_frame = selected_frame;
10224 XSETFRAME (selected_frame, f);
10225 display_mode_lines (w);
10226 selected_frame = old_selected_frame;
10228 /* If mode line height has changed, arrange for a thorough
10229 immediate redisplay using the correct mode line height. */
10230 if (WINDOW_WANTS_MODELINE_P (w)
10231 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10233 fonts_changed_p = 1;
10234 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10235 = DESIRED_MODE_LINE_HEIGHT (w);
10238 /* If top line height has changed, arrange for a thorough
10239 immediate redisplay using the correct mode line height. */
10240 if (WINDOW_WANTS_HEADER_LINE_P (w)
10241 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10243 fonts_changed_p = 1;
10244 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10245 = DESIRED_HEADER_LINE_HEIGHT (w);
10248 if (fonts_changed_p)
10249 goto finish_scroll_bars;
10252 if (!line_number_displayed
10253 && !BUFFERP (w->base_line_pos))
10255 w->base_line_pos = Qnil;
10256 w->base_line_number = Qnil;
10259 finish_menu_bars:
10261 /* When we reach a frame's selected window, redo the frame's menu bar. */
10262 if (update_mode_line
10263 && EQ (FRAME_SELECTED_WINDOW (f), window))
10265 int redisplay_menu_p = 0;
10267 if (FRAME_WINDOW_P (f))
10269 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10270 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10271 #else
10272 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10273 #endif
10275 else
10276 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10278 if (redisplay_menu_p)
10279 display_menu_bar (w);
10281 #ifdef HAVE_WINDOW_SYSTEM
10282 if (WINDOWP (f->tool_bar_window)
10283 && (FRAME_TOOL_BAR_LINES (f) > 0
10284 || auto_resize_tool_bars_p))
10285 redisplay_tool_bar (f);
10286 #endif
10289 finish_scroll_bars:
10291 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10293 int start, end, whole;
10295 /* Calculate the start and end positions for the current window.
10296 At some point, it would be nice to choose between scrollbars
10297 which reflect the whole buffer size, with special markers
10298 indicating narrowing, and scrollbars which reflect only the
10299 visible region.
10301 Note that mini-buffers sometimes aren't displaying any text. */
10302 if (!MINI_WINDOW_P (w)
10303 || (w == XWINDOW (minibuf_window)
10304 && NILP (echo_area_buffer[0])))
10306 whole = ZV - BEGV;
10307 start = marker_position (w->start) - BEGV;
10308 /* I don't think this is guaranteed to be right. For the
10309 moment, we'll pretend it is. */
10310 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10312 if (end < start)
10313 end = start;
10314 if (whole < (end - start))
10315 whole = end - start;
10317 else
10318 start = end = whole = 0;
10320 /* Indicate what this scroll bar ought to be displaying now. */
10321 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10323 /* Note that we actually used the scroll bar attached to this
10324 window, so it shouldn't be deleted at the end of redisplay. */
10325 redeem_scroll_bar_hook (w);
10328 /* Restore current_buffer and value of point in it. */
10329 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10330 set_buffer_internal_1 (old);
10331 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10333 unbind_to (count, Qnil);
10337 /* Build the complete desired matrix of WINDOW with a window start
10338 buffer position POS. Value is non-zero if successful. It is zero
10339 if fonts were loaded during redisplay which makes re-adjusting
10340 glyph matrices necessary. */
10343 try_window (window, pos)
10344 Lisp_Object window;
10345 struct text_pos pos;
10347 struct window *w = XWINDOW (window);
10348 struct it it;
10349 struct glyph_row *last_text_row = NULL;
10351 /* Make POS the new window start. */
10352 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10354 /* Mark cursor position as unknown. No overlay arrow seen. */
10355 w->cursor.vpos = -1;
10356 overlay_arrow_seen = 0;
10358 /* Initialize iterator and info to start at POS. */
10359 start_display (&it, w, pos);
10361 /* Display all lines of W. */
10362 while (it.current_y < it.last_visible_y)
10364 if (display_line (&it))
10365 last_text_row = it.glyph_row - 1;
10366 if (fonts_changed_p)
10367 return 0;
10370 /* If bottom moved off end of frame, change mode line percentage. */
10371 if (XFASTINT (w->window_end_pos) <= 0
10372 && Z != IT_CHARPOS (it))
10373 w->update_mode_line = Qt;
10375 /* Set window_end_pos to the offset of the last character displayed
10376 on the window from the end of current_buffer. Set
10377 window_end_vpos to its row number. */
10378 if (last_text_row)
10380 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10381 w->window_end_bytepos
10382 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10383 w->window_end_pos
10384 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10385 w->window_end_vpos
10386 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10387 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10388 ->displays_text_p);
10390 else
10392 w->window_end_bytepos = 0;
10393 w->window_end_pos = w->window_end_vpos = make_number (0);
10396 /* But that is not valid info until redisplay finishes. */
10397 w->window_end_valid = Qnil;
10398 return 1;
10403 /************************************************************************
10404 Window redisplay reusing current matrix when buffer has not changed
10405 ************************************************************************/
10407 /* Try redisplay of window W showing an unchanged buffer with a
10408 different window start than the last time it was displayed by
10409 reusing its current matrix. Value is non-zero if successful.
10410 W->start is the new window start. */
10412 static int
10413 try_window_reusing_current_matrix (w)
10414 struct window *w;
10416 struct frame *f = XFRAME (w->frame);
10417 struct glyph_row *row, *bottom_row;
10418 struct it it;
10419 struct run run;
10420 struct text_pos start, new_start;
10421 int nrows_scrolled, i;
10422 struct glyph_row *last_text_row;
10423 struct glyph_row *last_reused_text_row;
10424 struct glyph_row *start_row;
10425 int start_vpos, min_y, max_y;
10427 if (/* This function doesn't handle terminal frames. */
10428 !FRAME_WINDOW_P (f)
10429 /* Don't try to reuse the display if windows have been split
10430 or such. */
10431 || windows_or_buffers_changed)
10432 return 0;
10434 /* Can't do this if region may have changed. */
10435 if ((!NILP (Vtransient_mark_mode)
10436 && !NILP (current_buffer->mark_active))
10437 || !NILP (w->region_showing)
10438 || !NILP (Vshow_trailing_whitespace))
10439 return 0;
10441 /* If top-line visibility has changed, give up. */
10442 if (WINDOW_WANTS_HEADER_LINE_P (w)
10443 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10444 return 0;
10446 /* Give up if old or new display is scrolled vertically. We could
10447 make this function handle this, but right now it doesn't. */
10448 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10449 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10450 return 0;
10452 /* The variable new_start now holds the new window start. The old
10453 start `start' can be determined from the current matrix. */
10454 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10455 start = start_row->start.pos;
10456 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10458 /* Clear the desired matrix for the display below. */
10459 clear_glyph_matrix (w->desired_matrix);
10461 if (CHARPOS (new_start) <= CHARPOS (start))
10463 int first_row_y;
10465 /* Don't use this method if the display starts with an ellipsis
10466 displayed for invisible text. It's not easy to handle that case
10467 below, and it's certainly not worth the effort since this is
10468 not a frequent case. */
10469 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10470 return 0;
10472 IF_DEBUG (debug_method_add (w, "twu1"));
10474 /* Display up to a row that can be reused. The variable
10475 last_text_row is set to the last row displayed that displays
10476 text. Note that it.vpos == 0 if or if not there is a
10477 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10478 start_display (&it, w, new_start);
10479 first_row_y = it.current_y;
10480 w->cursor.vpos = -1;
10481 last_text_row = last_reused_text_row = NULL;
10483 while (it.current_y < it.last_visible_y
10484 && IT_CHARPOS (it) < CHARPOS (start)
10485 && !fonts_changed_p)
10486 if (display_line (&it))
10487 last_text_row = it.glyph_row - 1;
10489 /* A value of current_y < last_visible_y means that we stopped
10490 at the previous window start, which in turn means that we
10491 have at least one reusable row. */
10492 if (it.current_y < it.last_visible_y)
10494 /* IT.vpos always starts from 0; it counts text lines. */
10495 nrows_scrolled = it.vpos;
10497 /* Find PT if not already found in the lines displayed. */
10498 if (w->cursor.vpos < 0)
10500 int dy = it.current_y - first_row_y;
10502 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10503 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10505 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10506 && PT < MATRIX_ROW_END_CHARPOS (row))
10508 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10509 dy, nrows_scrolled);
10510 break;
10513 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10514 break;
10516 ++row;
10519 /* Give up if point was not found. This shouldn't
10520 happen often; not more often than with try_window
10521 itself. */
10522 if (w->cursor.vpos < 0)
10524 clear_glyph_matrix (w->desired_matrix);
10525 return 0;
10529 /* Scroll the display. Do it before the current matrix is
10530 changed. The problem here is that update has not yet
10531 run, i.e. part of the current matrix is not up to date.
10532 scroll_run_hook will clear the cursor, and use the
10533 current matrix to get the height of the row the cursor is
10534 in. */
10535 run.current_y = first_row_y;
10536 run.desired_y = it.current_y;
10537 run.height = it.last_visible_y - it.current_y;
10539 if (run.height > 0 && run.current_y != run.desired_y)
10541 update_begin (f);
10542 rif->update_window_begin_hook (w);
10543 rif->clear_mouse_face (w);
10544 rif->scroll_run_hook (w, &run);
10545 rif->update_window_end_hook (w, 0, 0);
10546 update_end (f);
10549 /* Shift current matrix down by nrows_scrolled lines. */
10550 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10551 rotate_matrix (w->current_matrix,
10552 start_vpos,
10553 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10554 nrows_scrolled);
10556 /* Disable lines that must be updated. */
10557 for (i = 0; i < it.vpos; ++i)
10558 (start_row + i)->enabled_p = 0;
10560 /* Re-compute Y positions. */
10561 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10562 max_y = it.last_visible_y;
10563 for (row = start_row + nrows_scrolled;
10564 row < bottom_row;
10565 ++row)
10567 row->y = it.current_y;
10569 if (row->y < min_y)
10570 row->visible_height = row->height - (min_y - row->y);
10571 else if (row->y + row->height > max_y)
10572 row->visible_height
10573 = row->height - (row->y + row->height - max_y);
10574 else
10575 row->visible_height = row->height;
10577 it.current_y += row->height;
10579 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10580 last_reused_text_row = row;
10581 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10582 break;
10585 /* Disable lines in the current matrix which are now
10586 below the window. */
10587 for (++row; row < bottom_row; ++row)
10588 row->enabled_p = 0;
10591 /* Update window_end_pos etc.; last_reused_text_row is the last
10592 reused row from the current matrix containing text, if any.
10593 The value of last_text_row is the last displayed line
10594 containing text. */
10595 if (last_reused_text_row)
10597 w->window_end_bytepos
10598 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10599 w->window_end_pos
10600 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10601 w->window_end_vpos
10602 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10603 w->current_matrix));
10605 else if (last_text_row)
10607 w->window_end_bytepos
10608 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10609 w->window_end_pos
10610 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10611 w->window_end_vpos
10612 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10614 else
10616 /* This window must be completely empty. */
10617 w->window_end_bytepos = 0;
10618 w->window_end_pos = w->window_end_vpos = make_number (0);
10620 w->window_end_valid = Qnil;
10622 /* Update hint: don't try scrolling again in update_window. */
10623 w->desired_matrix->no_scrolling_p = 1;
10625 #if GLYPH_DEBUG
10626 debug_method_add (w, "try_window_reusing_current_matrix 1");
10627 #endif
10628 return 1;
10630 else if (CHARPOS (new_start) > CHARPOS (start))
10632 struct glyph_row *pt_row, *row;
10633 struct glyph_row *first_reusable_row;
10634 struct glyph_row *first_row_to_display;
10635 int dy;
10636 int yb = window_text_bottom_y (w);
10638 /* Find the row starting at new_start, if there is one. Don't
10639 reuse a partially visible line at the end. */
10640 first_reusable_row = start_row;
10641 while (first_reusable_row->enabled_p
10642 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10643 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10644 < CHARPOS (new_start)))
10645 ++first_reusable_row;
10647 /* Give up if there is no row to reuse. */
10648 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10649 || !first_reusable_row->enabled_p
10650 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10651 != CHARPOS (new_start)))
10652 return 0;
10654 /* We can reuse fully visible rows beginning with
10655 first_reusable_row to the end of the window. Set
10656 first_row_to_display to the first row that cannot be reused.
10657 Set pt_row to the row containing point, if there is any. */
10658 pt_row = NULL;
10659 for (first_row_to_display = first_reusable_row;
10660 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10661 ++first_row_to_display)
10663 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10664 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10665 pt_row = first_row_to_display;
10668 /* Start displaying at the start of first_row_to_display. */
10669 xassert (first_row_to_display->y < yb);
10670 init_to_row_start (&it, w, first_row_to_display);
10672 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10673 - start_vpos);
10674 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10675 - nrows_scrolled);
10676 it.current_y = (first_row_to_display->y - first_reusable_row->y
10677 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10679 /* Display lines beginning with first_row_to_display in the
10680 desired matrix. Set last_text_row to the last row displayed
10681 that displays text. */
10682 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10683 if (pt_row == NULL)
10684 w->cursor.vpos = -1;
10685 last_text_row = NULL;
10686 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10687 if (display_line (&it))
10688 last_text_row = it.glyph_row - 1;
10690 /* Give up If point isn't in a row displayed or reused. */
10691 if (w->cursor.vpos < 0)
10693 clear_glyph_matrix (w->desired_matrix);
10694 return 0;
10697 /* If point is in a reused row, adjust y and vpos of the cursor
10698 position. */
10699 if (pt_row)
10701 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10702 w->current_matrix);
10703 w->cursor.y -= first_reusable_row->y;
10706 /* Scroll the display. */
10707 run.current_y = first_reusable_row->y;
10708 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10709 run.height = it.last_visible_y - run.current_y;
10710 dy = run.current_y - run.desired_y;
10712 if (run.height)
10714 struct frame *f = XFRAME (WINDOW_FRAME (w));
10715 update_begin (f);
10716 rif->update_window_begin_hook (w);
10717 rif->clear_mouse_face (w);
10718 rif->scroll_run_hook (w, &run);
10719 rif->update_window_end_hook (w, 0, 0);
10720 update_end (f);
10723 /* Adjust Y positions of reused rows. */
10724 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10725 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10726 max_y = it.last_visible_y;
10727 for (row = first_reusable_row; row < first_row_to_display; ++row)
10729 row->y -= dy;
10730 if (row->y < min_y)
10731 row->visible_height = row->height - (min_y - row->y);
10732 else if (row->y + row->height > max_y)
10733 row->visible_height
10734 = row->height - (row->y + row->height - max_y);
10735 else
10736 row->visible_height = row->height;
10739 /* Scroll the current matrix. */
10740 xassert (nrows_scrolled > 0);
10741 rotate_matrix (w->current_matrix,
10742 start_vpos,
10743 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10744 -nrows_scrolled);
10746 /* Disable rows not reused. */
10747 for (row -= nrows_scrolled; row < bottom_row; ++row)
10748 row->enabled_p = 0;
10750 /* Adjust window end. A null value of last_text_row means that
10751 the window end is in reused rows which in turn means that
10752 only its vpos can have changed. */
10753 if (last_text_row)
10755 w->window_end_bytepos
10756 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10757 w->window_end_pos
10758 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10759 w->window_end_vpos
10760 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10762 else
10764 w->window_end_vpos
10765 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10768 w->window_end_valid = Qnil;
10769 w->desired_matrix->no_scrolling_p = 1;
10771 #if GLYPH_DEBUG
10772 debug_method_add (w, "try_window_reusing_current_matrix 2");
10773 #endif
10774 return 1;
10777 return 0;
10782 /************************************************************************
10783 Window redisplay reusing current matrix when buffer has changed
10784 ************************************************************************/
10786 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10787 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10788 int *, int *));
10789 static struct glyph_row *
10790 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10791 struct glyph_row *));
10794 /* Return the last row in MATRIX displaying text. If row START is
10795 non-null, start searching with that row. IT gives the dimensions
10796 of the display. Value is null if matrix is empty; otherwise it is
10797 a pointer to the row found. */
10799 static struct glyph_row *
10800 find_last_row_displaying_text (matrix, it, start)
10801 struct glyph_matrix *matrix;
10802 struct it *it;
10803 struct glyph_row *start;
10805 struct glyph_row *row, *row_found;
10807 /* Set row_found to the last row in IT->w's current matrix
10808 displaying text. The loop looks funny but think of partially
10809 visible lines. */
10810 row_found = NULL;
10811 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10812 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10814 xassert (row->enabled_p);
10815 row_found = row;
10816 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10817 break;
10818 ++row;
10821 return row_found;
10825 /* Return the last row in the current matrix of W that is not affected
10826 by changes at the start of current_buffer that occurred since the
10827 last time W was redisplayed. Value is null if no such row exists.
10829 The global variable beg_unchanged has to contain the number of
10830 bytes unchanged at the start of current_buffer. BEG +
10831 beg_unchanged is the buffer position of the first changed byte in
10832 current_buffer. Characters at positions < BEG + beg_unchanged are
10833 at the same buffer positions as they were when the current matrix
10834 was built. */
10836 static struct glyph_row *
10837 find_last_unchanged_at_beg_row (w)
10838 struct window *w;
10840 int first_changed_pos = BEG + BEG_UNCHANGED;
10841 struct glyph_row *row;
10842 struct glyph_row *row_found = NULL;
10843 int yb = window_text_bottom_y (w);
10845 /* Find the last row displaying unchanged text. */
10846 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10847 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10848 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10850 if (/* If row ends before first_changed_pos, it is unchanged,
10851 except in some case. */
10852 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10853 /* When row ends in ZV and we write at ZV it is not
10854 unchanged. */
10855 && !row->ends_at_zv_p
10856 /* When first_changed_pos is the end of a continued line,
10857 row is not unchanged because it may be no longer
10858 continued. */
10859 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10860 && row->continued_p))
10861 row_found = row;
10863 /* Stop if last visible row. */
10864 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10865 break;
10867 ++row;
10870 return row_found;
10874 /* Find the first glyph row in the current matrix of W that is not
10875 affected by changes at the end of current_buffer since the last
10876 time the window was redisplayed. Return in *DELTA the number of
10877 chars by which buffer positions in unchanged text at the end of
10878 current_buffer must be adjusted. Return in *DELTA_BYTES the
10879 corresponding number of bytes. Value is null if no such row
10880 exists, i.e. all rows are affected by changes. */
10882 static struct glyph_row *
10883 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10884 struct window *w;
10885 int *delta, *delta_bytes;
10887 struct glyph_row *row;
10888 struct glyph_row *row_found = NULL;
10890 *delta = *delta_bytes = 0;
10892 /* Display must not have been paused, otherwise the current matrix
10893 is not up to date. */
10894 if (NILP (w->window_end_valid))
10895 abort ();
10897 /* A value of window_end_pos >= END_UNCHANGED means that the window
10898 end is in the range of changed text. If so, there is no
10899 unchanged row at the end of W's current matrix. */
10900 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10901 return NULL;
10903 /* Set row to the last row in W's current matrix displaying text. */
10904 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10906 /* If matrix is entirely empty, no unchanged row exists. */
10907 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10909 /* The value of row is the last glyph row in the matrix having a
10910 meaningful buffer position in it. The end position of row
10911 corresponds to window_end_pos. This allows us to translate
10912 buffer positions in the current matrix to current buffer
10913 positions for characters not in changed text. */
10914 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10915 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10916 int last_unchanged_pos, last_unchanged_pos_old;
10917 struct glyph_row *first_text_row
10918 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10920 *delta = Z - Z_old;
10921 *delta_bytes = Z_BYTE - Z_BYTE_old;
10923 /* Set last_unchanged_pos to the buffer position of the last
10924 character in the buffer that has not been changed. Z is the
10925 index + 1 of the last byte in current_buffer, i.e. by
10926 subtracting end_unchanged we get the index of the last
10927 unchanged character, and we have to add BEG to get its buffer
10928 position. */
10929 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10930 last_unchanged_pos_old = last_unchanged_pos - *delta;
10932 /* Search backward from ROW for a row displaying a line that
10933 starts at a minimum position >= last_unchanged_pos_old. */
10934 for (; row > first_text_row; --row)
10936 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10937 abort ();
10939 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10940 row_found = row;
10944 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10945 abort ();
10947 return row_found;
10951 /* Make sure that glyph rows in the current matrix of window W
10952 reference the same glyph memory as corresponding rows in the
10953 frame's frame matrix. This function is called after scrolling W's
10954 current matrix on a terminal frame in try_window_id and
10955 try_window_reusing_current_matrix. */
10957 static void
10958 sync_frame_with_window_matrix_rows (w)
10959 struct window *w;
10961 struct frame *f = XFRAME (w->frame);
10962 struct glyph_row *window_row, *window_row_end, *frame_row;
10964 /* Preconditions: W must be a leaf window and full-width. Its frame
10965 must have a frame matrix. */
10966 xassert (NILP (w->hchild) && NILP (w->vchild));
10967 xassert (WINDOW_FULL_WIDTH_P (w));
10968 xassert (!FRAME_WINDOW_P (f));
10970 /* If W is a full-width window, glyph pointers in W's current matrix
10971 have, by definition, to be the same as glyph pointers in the
10972 corresponding frame matrix. */
10973 window_row = w->current_matrix->rows;
10974 window_row_end = window_row + w->current_matrix->nrows;
10975 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10976 while (window_row < window_row_end)
10978 int area;
10980 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10981 frame_row->glyphs[area] = window_row->glyphs[area];
10983 /* Disable frame rows whose corresponding window rows have
10984 been disabled in try_window_id. */
10985 if (!window_row->enabled_p)
10986 frame_row->enabled_p = 0;
10988 ++window_row, ++frame_row;
10993 /* Find the glyph row in window W containing CHARPOS. Consider all
10994 rows between START and END (not inclusive). END null means search
10995 all rows to the end of the display area of W. Value is the row
10996 containing CHARPOS or null. */
10998 static struct glyph_row *
10999 row_containing_pos (w, charpos, start, end)
11000 struct window *w;
11001 int charpos;
11002 struct glyph_row *start, *end;
11004 struct glyph_row *row = start;
11005 int last_y;
11007 /* If we happen to start on a header-line, skip that. */
11008 if (row->mode_line_p)
11009 ++row;
11011 if ((end && row >= end) || !row->enabled_p)
11012 return NULL;
11014 last_y = window_text_bottom_y (w);
11016 while ((end == NULL || row < end)
11017 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11018 /* The end position of a row equals the start
11019 position of the next row. If CHARPOS is there, we
11020 would rather display it in the next line, except
11021 when this line ends in ZV. */
11022 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11023 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11024 || !row->ends_at_zv_p)))
11025 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11026 ++row;
11028 /* Give up if CHARPOS not found. */
11029 if ((end && row >= end)
11030 || charpos < MATRIX_ROW_START_CHARPOS (row)
11031 || charpos > MATRIX_ROW_END_CHARPOS (row))
11032 row = NULL;
11034 return row;
11038 /* Try to redisplay window W by reusing its existing display. W's
11039 current matrix must be up to date when this function is called,
11040 i.e. window_end_valid must not be nil.
11042 Value is
11044 1 if display has been updated
11045 0 if otherwise unsuccessful
11046 -1 if redisplay with same window start is known not to succeed
11048 The following steps are performed:
11050 1. Find the last row in the current matrix of W that is not
11051 affected by changes at the start of current_buffer. If no such row
11052 is found, give up.
11054 2. Find the first row in W's current matrix that is not affected by
11055 changes at the end of current_buffer. Maybe there is no such row.
11057 3. Display lines beginning with the row + 1 found in step 1 to the
11058 row found in step 2 or, if step 2 didn't find a row, to the end of
11059 the window.
11061 4. If cursor is not known to appear on the window, give up.
11063 5. If display stopped at the row found in step 2, scroll the
11064 display and current matrix as needed.
11066 6. Maybe display some lines at the end of W, if we must. This can
11067 happen under various circumstances, like a partially visible line
11068 becoming fully visible, or because newly displayed lines are displayed
11069 in smaller font sizes.
11071 7. Update W's window end information. */
11073 /* Check that window end is what we expect it to be. */
11075 static int
11076 try_window_id (w)
11077 struct window *w;
11079 struct frame *f = XFRAME (w->frame);
11080 struct glyph_matrix *current_matrix = w->current_matrix;
11081 struct glyph_matrix *desired_matrix = w->desired_matrix;
11082 struct glyph_row *last_unchanged_at_beg_row;
11083 struct glyph_row *first_unchanged_at_end_row;
11084 struct glyph_row *row;
11085 struct glyph_row *bottom_row;
11086 int bottom_vpos;
11087 struct it it;
11088 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11089 struct text_pos start_pos;
11090 struct run run;
11091 int first_unchanged_at_end_vpos = 0;
11092 struct glyph_row *last_text_row, *last_text_row_at_end;
11093 struct text_pos start;
11095 SET_TEXT_POS_FROM_MARKER (start, w->start);
11097 /* Check pre-conditions. Window end must be valid, otherwise
11098 the current matrix would not be up to date. */
11099 xassert (!NILP (w->window_end_valid));
11100 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11101 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11103 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11104 only if buffer has really changed. The reason is that the gap is
11105 initially at Z for freshly visited files. The code below would
11106 set end_unchanged to 0 in that case. */
11107 if (MODIFF > SAVE_MODIFF
11108 /* This seems to happen sometimes after saving a buffer. */
11109 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11111 if (GPT - BEG < BEG_UNCHANGED)
11112 BEG_UNCHANGED = GPT - BEG;
11113 if (Z - GPT < END_UNCHANGED)
11114 END_UNCHANGED = Z - GPT;
11117 /* If window starts after a line end, and the last change is in
11118 front of that newline, then changes don't affect the display.
11119 This case happens with stealth-fontification. Note that although
11120 the display is unchanged, glyph positions in the matrix have to
11121 be adjusted, of course. */
11122 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11123 if (CHARPOS (start) > BEGV
11124 && Z - END_UNCHANGED < CHARPOS (start) - 1
11125 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11126 && PT < MATRIX_ROW_END_CHARPOS (row))
11128 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11129 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11131 if (delta)
11133 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11134 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11136 increment_matrix_positions (w->current_matrix,
11137 MATRIX_ROW_VPOS (r0, current_matrix),
11138 MATRIX_ROW_VPOS (r1, current_matrix),
11139 delta, delta_bytes);
11142 #if 0 /* If changes are all in front of the window start, the
11143 distance of the last displayed glyph from Z hasn't
11144 changed. */
11145 w->window_end_pos
11146 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11147 w->window_end_bytepos
11148 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11149 #endif
11151 row = row_containing_pos (w, PT, r0, NULL);
11152 if (row == NULL)
11153 return 0;
11155 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11156 return 1;
11159 /* Return quickly if changes are all below what is displayed in the
11160 window, and if PT is in the window. */
11161 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
11162 && PT < MATRIX_ROW_END_CHARPOS (row))
11164 /* We have to update window end positions because the buffer's
11165 size has changed. */
11166 w->window_end_pos
11167 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11168 w->window_end_bytepos
11169 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11171 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11172 row = row_containing_pos (w, PT, row, NULL);
11173 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11174 return 2;
11177 /* Check that window start agrees with the start of the first glyph
11178 row in its current matrix. Check this after we know the window
11179 start is not in changed text, otherwise positions would not be
11180 comparable. */
11181 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11182 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11183 return 0;
11185 /* Compute the position at which we have to start displaying new
11186 lines. Some of the lines at the top of the window might be
11187 reusable because they are not displaying changed text. Find the
11188 last row in W's current matrix not affected by changes at the
11189 start of current_buffer. Value is null if changes start in the
11190 first line of window. */
11191 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11192 if (last_unchanged_at_beg_row)
11194 /* Avoid starting to display in the moddle of a character, a TAB
11195 for instance. This is easier than to set up the iterator
11196 exactly, and it's not a frequent case, so the additional
11197 effort wouldn't really pay off. */
11198 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11199 && last_unchanged_at_beg_row > w->current_matrix->rows)
11200 --last_unchanged_at_beg_row;
11202 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11203 return 0;
11205 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11206 start_pos = it.current.pos;
11208 /* Start displaying new lines in the desired matrix at the same
11209 vpos we would use in the current matrix, i.e. below
11210 last_unchanged_at_beg_row. */
11211 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11212 current_matrix);
11213 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11214 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11216 xassert (it.hpos == 0 && it.current_x == 0);
11218 else
11220 /* There are no reusable lines at the start of the window.
11221 Start displaying in the first line. */
11222 start_display (&it, w, start);
11223 start_pos = it.current.pos;
11226 /* Find the first row that is not affected by changes at the end of
11227 the buffer. Value will be null if there is no unchanged row, in
11228 which case we must redisplay to the end of the window. delta
11229 will be set to the value by which buffer positions beginning with
11230 first_unchanged_at_end_row have to be adjusted due to text
11231 changes. */
11232 first_unchanged_at_end_row
11233 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11234 IF_DEBUG (debug_delta = delta);
11235 IF_DEBUG (debug_delta_bytes = delta_bytes);
11237 /* Set stop_pos to the buffer position up to which we will have to
11238 display new lines. If first_unchanged_at_end_row != NULL, this
11239 is the buffer position of the start of the line displayed in that
11240 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11241 that we don't stop at a buffer position. */
11242 stop_pos = 0;
11243 if (first_unchanged_at_end_row)
11245 xassert (last_unchanged_at_beg_row == NULL
11246 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11248 /* If this is a continuation line, move forward to the next one
11249 that isn't. Changes in lines above affect this line.
11250 Caution: this may move first_unchanged_at_end_row to a row
11251 not displaying text. */
11252 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11253 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11254 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11255 < it.last_visible_y))
11256 ++first_unchanged_at_end_row;
11258 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11259 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11260 >= it.last_visible_y))
11261 first_unchanged_at_end_row = NULL;
11262 else
11264 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11265 + delta);
11266 first_unchanged_at_end_vpos
11267 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11268 xassert (stop_pos >= Z - END_UNCHANGED);
11271 else if (last_unchanged_at_beg_row == NULL)
11272 return 0;
11275 #if GLYPH_DEBUG
11277 /* Either there is no unchanged row at the end, or the one we have
11278 now displays text. This is a necessary condition for the window
11279 end pos calculation at the end of this function. */
11280 xassert (first_unchanged_at_end_row == NULL
11281 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11283 debug_last_unchanged_at_beg_vpos
11284 = (last_unchanged_at_beg_row
11285 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11286 : -1);
11287 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11289 #endif /* GLYPH_DEBUG != 0 */
11292 /* Display new lines. Set last_text_row to the last new line
11293 displayed which has text on it, i.e. might end up as being the
11294 line where the window_end_vpos is. */
11295 w->cursor.vpos = -1;
11296 last_text_row = NULL;
11297 overlay_arrow_seen = 0;
11298 while (it.current_y < it.last_visible_y
11299 && !fonts_changed_p
11300 && (first_unchanged_at_end_row == NULL
11301 || IT_CHARPOS (it) < stop_pos))
11303 if (display_line (&it))
11304 last_text_row = it.glyph_row - 1;
11307 if (fonts_changed_p)
11308 return -1;
11311 /* Compute differences in buffer positions, y-positions etc. for
11312 lines reused at the bottom of the window. Compute what we can
11313 scroll. */
11314 if (first_unchanged_at_end_row
11315 /* No lines reused because we displayed everything up to the
11316 bottom of the window. */
11317 && it.current_y < it.last_visible_y)
11319 dvpos = (it.vpos
11320 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11321 current_matrix));
11322 dy = it.current_y - first_unchanged_at_end_row->y;
11323 run.current_y = first_unchanged_at_end_row->y;
11324 run.desired_y = run.current_y + dy;
11325 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11327 else
11329 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11330 first_unchanged_at_end_row = NULL;
11332 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11335 /* Find the cursor if not already found. We have to decide whether
11336 PT will appear on this window (it sometimes doesn't, but this is
11337 not a very frequent case.) This decision has to be made before
11338 the current matrix is altered. A value of cursor.vpos < 0 means
11339 that PT is either in one of the lines beginning at
11340 first_unchanged_at_end_row or below the window. Don't care for
11341 lines that might be displayed later at the window end; as
11342 mentioned, this is not a frequent case. */
11343 if (w->cursor.vpos < 0)
11345 /* Cursor in unchanged rows at the top? */
11346 if (PT < CHARPOS (start_pos)
11347 && last_unchanged_at_beg_row)
11349 row = row_containing_pos (w, PT,
11350 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11351 last_unchanged_at_beg_row + 1);
11352 if (row)
11353 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11356 /* Start from first_unchanged_at_end_row looking for PT. */
11357 else if (first_unchanged_at_end_row)
11359 row = row_containing_pos (w, PT - delta,
11360 first_unchanged_at_end_row, NULL);
11361 if (row)
11362 set_cursor_from_row (w, row, w->current_matrix, delta,
11363 delta_bytes, dy, dvpos);
11366 /* Give up if cursor was not found. */
11367 if (w->cursor.vpos < 0)
11369 clear_glyph_matrix (w->desired_matrix);
11370 return -1;
11374 /* Don't let the cursor end in the scroll margins. */
11376 int this_scroll_margin, cursor_height;
11378 this_scroll_margin = max (0, scroll_margin);
11379 this_scroll_margin = min (this_scroll_margin,
11380 XFASTINT (w->height) / 4);
11381 this_scroll_margin *= CANON_Y_UNIT (it.f);
11382 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11384 if ((w->cursor.y < this_scroll_margin
11385 && CHARPOS (start) > BEGV)
11386 /* Don't take scroll margin into account at the bottom because
11387 old redisplay didn't do it either. */
11388 || w->cursor.y + cursor_height > it.last_visible_y)
11390 w->cursor.vpos = -1;
11391 clear_glyph_matrix (w->desired_matrix);
11392 return -1;
11396 /* Scroll the display. Do it before changing the current matrix so
11397 that xterm.c doesn't get confused about where the cursor glyph is
11398 found. */
11399 if (dy && run.height)
11401 update_begin (f);
11403 if (FRAME_WINDOW_P (f))
11405 rif->update_window_begin_hook (w);
11406 rif->clear_mouse_face (w);
11407 rif->scroll_run_hook (w, &run);
11408 rif->update_window_end_hook (w, 0, 0);
11410 else
11412 /* Terminal frame. In this case, dvpos gives the number of
11413 lines to scroll by; dvpos < 0 means scroll up. */
11414 int first_unchanged_at_end_vpos
11415 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11416 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11417 int end = XFASTINT (w->top) + window_internal_height (w);
11419 /* Perform the operation on the screen. */
11420 if (dvpos > 0)
11422 /* Scroll last_unchanged_at_beg_row to the end of the
11423 window down dvpos lines. */
11424 set_terminal_window (end);
11426 /* On dumb terminals delete dvpos lines at the end
11427 before inserting dvpos empty lines. */
11428 if (!scroll_region_ok)
11429 ins_del_lines (end - dvpos, -dvpos);
11431 /* Insert dvpos empty lines in front of
11432 last_unchanged_at_beg_row. */
11433 ins_del_lines (from, dvpos);
11435 else if (dvpos < 0)
11437 /* Scroll up last_unchanged_at_beg_vpos to the end of
11438 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11439 set_terminal_window (end);
11441 /* Delete dvpos lines in front of
11442 last_unchanged_at_beg_vpos. ins_del_lines will set
11443 the cursor to the given vpos and emit |dvpos| delete
11444 line sequences. */
11445 ins_del_lines (from + dvpos, dvpos);
11447 /* On a dumb terminal insert dvpos empty lines at the
11448 end. */
11449 if (!scroll_region_ok)
11450 ins_del_lines (end + dvpos, -dvpos);
11453 set_terminal_window (0);
11456 update_end (f);
11459 /* Shift reused rows of the current matrix to the right position.
11460 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11461 text. */
11462 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11463 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11464 if (dvpos < 0)
11466 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11467 bottom_vpos, dvpos);
11468 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11469 bottom_vpos, 0);
11471 else if (dvpos > 0)
11473 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11474 bottom_vpos, dvpos);
11475 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11476 first_unchanged_at_end_vpos + dvpos, 0);
11479 /* For frame-based redisplay, make sure that current frame and window
11480 matrix are in sync with respect to glyph memory. */
11481 if (!FRAME_WINDOW_P (f))
11482 sync_frame_with_window_matrix_rows (w);
11484 /* Adjust buffer positions in reused rows. */
11485 if (delta)
11486 increment_matrix_positions (current_matrix,
11487 first_unchanged_at_end_vpos + dvpos,
11488 bottom_vpos, delta, delta_bytes);
11490 /* Adjust Y positions. */
11491 if (dy)
11492 shift_glyph_matrix (w, current_matrix,
11493 first_unchanged_at_end_vpos + dvpos,
11494 bottom_vpos, dy);
11496 if (first_unchanged_at_end_row)
11497 first_unchanged_at_end_row += dvpos;
11499 /* If scrolling up, there may be some lines to display at the end of
11500 the window. */
11501 last_text_row_at_end = NULL;
11502 if (dy < 0)
11504 /* Set last_row to the glyph row in the current matrix where the
11505 window end line is found. It has been moved up or down in
11506 the matrix by dvpos. */
11507 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11508 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11510 /* If last_row is the window end line, it should display text. */
11511 xassert (last_row->displays_text_p);
11513 /* If window end line was partially visible before, begin
11514 displaying at that line. Otherwise begin displaying with the
11515 line following it. */
11516 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11518 init_to_row_start (&it, w, last_row);
11519 it.vpos = last_vpos;
11520 it.current_y = last_row->y;
11522 else
11524 init_to_row_end (&it, w, last_row);
11525 it.vpos = 1 + last_vpos;
11526 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11527 ++last_row;
11530 /* We may start in a continuation line. If so, we have to get
11531 the right continuation_lines_width and current_x. */
11532 it.continuation_lines_width = last_row->continuation_lines_width;
11533 it.hpos = it.current_x = 0;
11535 /* Display the rest of the lines at the window end. */
11536 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11537 while (it.current_y < it.last_visible_y
11538 && !fonts_changed_p)
11540 /* Is it always sure that the display agrees with lines in
11541 the current matrix? I don't think so, so we mark rows
11542 displayed invalid in the current matrix by setting their
11543 enabled_p flag to zero. */
11544 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11545 if (display_line (&it))
11546 last_text_row_at_end = it.glyph_row - 1;
11550 /* Update window_end_pos and window_end_vpos. */
11551 if (first_unchanged_at_end_row
11552 && first_unchanged_at_end_row->y < it.last_visible_y
11553 && !last_text_row_at_end)
11555 /* Window end line if one of the preserved rows from the current
11556 matrix. Set row to the last row displaying text in current
11557 matrix starting at first_unchanged_at_end_row, after
11558 scrolling. */
11559 xassert (first_unchanged_at_end_row->displays_text_p);
11560 row = find_last_row_displaying_text (w->current_matrix, &it,
11561 first_unchanged_at_end_row);
11562 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11564 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11565 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11566 w->window_end_vpos
11567 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11569 else if (last_text_row_at_end)
11571 w->window_end_pos
11572 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11573 w->window_end_bytepos
11574 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11575 w->window_end_vpos
11576 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11578 else if (last_text_row)
11580 /* We have displayed either to the end of the window or at the
11581 end of the window, i.e. the last row with text is to be found
11582 in the desired matrix. */
11583 w->window_end_pos
11584 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11585 w->window_end_bytepos
11586 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11587 w->window_end_vpos
11588 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11590 else if (first_unchanged_at_end_row == NULL
11591 && last_text_row == NULL
11592 && last_text_row_at_end == NULL)
11594 /* Displayed to end of window, but no line containing text was
11595 displayed. Lines were deleted at the end of the window. */
11596 int vpos;
11597 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11599 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11600 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11601 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11602 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11603 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11604 break;
11606 w->window_end_vpos = make_number (vpos);
11608 else
11609 abort ();
11611 #if 0 /* This leads to problems, for instance when the cursor is
11612 at ZV, and the cursor line displays no text. */
11613 /* Disable rows below what's displayed in the window. This makes
11614 debugging easier. */
11615 enable_glyph_matrix_rows (current_matrix,
11616 XFASTINT (w->window_end_vpos) + 1,
11617 bottom_vpos, 0);
11618 #endif
11620 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11621 debug_end_vpos = XFASTINT (w->window_end_vpos));
11623 /* Record that display has not been completed. */
11624 w->window_end_valid = Qnil;
11625 w->desired_matrix->no_scrolling_p = 1;
11626 return 3;
11631 /***********************************************************************
11632 More debugging support
11633 ***********************************************************************/
11635 #if GLYPH_DEBUG
11637 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11638 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11639 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11642 /* Dump the contents of glyph matrix MATRIX on stderr.
11644 GLYPHS 0 means don't show glyph contents.
11645 GLYPHS 1 means show glyphs in short form
11646 GLYPHS > 1 means show glyphs in long form. */
11648 void
11649 dump_glyph_matrix (matrix, glyphs)
11650 struct glyph_matrix *matrix;
11651 int glyphs;
11653 int i;
11654 for (i = 0; i < matrix->nrows; ++i)
11655 dump_glyph_row (matrix, i, glyphs);
11659 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11660 the glyph row and area where the glyph comes from. */
11662 void
11663 dump_glyph (row, glyph, area)
11664 struct glyph_row *row;
11665 struct glyph *glyph;
11666 int area;
11668 if (glyph->type == CHAR_GLYPH)
11670 fprintf (stderr,
11671 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11672 glyph - row->glyphs[TEXT_AREA],
11673 'C',
11674 glyph->charpos,
11675 (BUFFERP (glyph->object)
11676 ? 'B'
11677 : (STRINGP (glyph->object)
11678 ? 'S'
11679 : '-')),
11680 glyph->pixel_width,
11681 glyph->u.ch,
11682 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11683 ? glyph->u.ch
11684 : '.'),
11685 glyph->face_id,
11686 glyph->left_box_line_p,
11687 glyph->right_box_line_p);
11689 else if (glyph->type == STRETCH_GLYPH)
11691 fprintf (stderr,
11692 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11693 glyph - row->glyphs[TEXT_AREA],
11694 'S',
11695 glyph->charpos,
11696 (BUFFERP (glyph->object)
11697 ? 'B'
11698 : (STRINGP (glyph->object)
11699 ? 'S'
11700 : '-')),
11701 glyph->pixel_width,
11703 '.',
11704 glyph->face_id,
11705 glyph->left_box_line_p,
11706 glyph->right_box_line_p);
11708 else if (glyph->type == IMAGE_GLYPH)
11710 fprintf (stderr,
11711 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11712 glyph - row->glyphs[TEXT_AREA],
11713 'I',
11714 glyph->charpos,
11715 (BUFFERP (glyph->object)
11716 ? 'B'
11717 : (STRINGP (glyph->object)
11718 ? 'S'
11719 : '-')),
11720 glyph->pixel_width,
11721 glyph->u.img_id,
11722 '.',
11723 glyph->face_id,
11724 glyph->left_box_line_p,
11725 glyph->right_box_line_p);
11730 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11731 GLYPHS 0 means don't show glyph contents.
11732 GLYPHS 1 means show glyphs in short form
11733 GLYPHS > 1 means show glyphs in long form. */
11735 void
11736 dump_glyph_row (matrix, vpos, glyphs)
11737 struct glyph_matrix *matrix;
11738 int vpos, glyphs;
11740 struct glyph_row *row;
11742 if (vpos < 0 || vpos >= matrix->nrows)
11743 return;
11745 row = MATRIX_ROW (matrix, vpos);
11747 if (glyphs != 1)
11749 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11750 fprintf (stderr, "=======================================================================\n");
11752 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11753 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11754 row - matrix->rows,
11755 MATRIX_ROW_START_CHARPOS (row),
11756 MATRIX_ROW_END_CHARPOS (row),
11757 row->used[TEXT_AREA],
11758 row->contains_overlapping_glyphs_p,
11759 row->enabled_p,
11760 row->inverse_p,
11761 row->truncated_on_left_p,
11762 row->truncated_on_right_p,
11763 row->overlay_arrow_p,
11764 row->continued_p,
11765 MATRIX_ROW_CONTINUATION_LINE_P (row),
11766 row->displays_text_p,
11767 row->ends_at_zv_p,
11768 row->fill_line_p,
11769 row->ends_in_middle_of_char_p,
11770 row->starts_in_middle_of_char_p,
11771 row->mouse_face_p,
11772 row->x,
11773 row->y,
11774 row->pixel_width,
11775 row->height,
11776 row->visible_height,
11777 row->ascent,
11778 row->phys_ascent);
11779 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11780 row->end.overlay_string_index);
11781 fprintf (stderr, "%9d %5d\n",
11782 CHARPOS (row->start.string_pos),
11783 CHARPOS (row->end.string_pos));
11784 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11785 row->end.dpvec_index);
11788 if (glyphs > 1)
11790 int area;
11792 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11794 struct glyph *glyph = row->glyphs[area];
11795 struct glyph *glyph_end = glyph + row->used[area];
11797 /* Glyph for a line end in text. */
11798 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11799 ++glyph_end;
11801 if (glyph < glyph_end)
11802 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11804 for (; glyph < glyph_end; ++glyph)
11805 dump_glyph (row, glyph, area);
11808 else if (glyphs == 1)
11810 int area;
11812 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11814 char *s = (char *) alloca (row->used[area] + 1);
11815 int i;
11817 for (i = 0; i < row->used[area]; ++i)
11819 struct glyph *glyph = row->glyphs[area] + i;
11820 if (glyph->type == CHAR_GLYPH
11821 && glyph->u.ch < 0x80
11822 && glyph->u.ch >= ' ')
11823 s[i] = glyph->u.ch;
11824 else
11825 s[i] = '.';
11828 s[i] = '\0';
11829 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11835 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11836 Sdump_glyph_matrix, 0, 1, "p",
11837 "Dump the current matrix of the selected window to stderr.\n\
11838 Shows contents of glyph row structures. With non-nil\n\
11839 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11840 glyphs in short form, otherwise show glyphs in long form.")
11841 (glyphs)
11842 Lisp_Object glyphs;
11844 struct window *w = XWINDOW (selected_window);
11845 struct buffer *buffer = XBUFFER (w->buffer);
11847 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11848 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11849 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11850 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11851 fprintf (stderr, "=============================================\n");
11852 dump_glyph_matrix (w->current_matrix,
11853 NILP (glyphs) ? 0 : XINT (glyphs));
11854 return Qnil;
11858 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11859 "Dump glyph row ROW to stderr.\n\
11860 GLYPH 0 means don't dump glyphs.\n\
11861 GLYPH 1 means dump glyphs in short form.\n\
11862 GLYPH > 1 or omitted means dump glyphs in long form.")
11863 (row, glyphs)
11864 Lisp_Object row, glyphs;
11866 CHECK_NUMBER (row, 0);
11867 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11868 XINT (row),
11869 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11870 return Qnil;
11874 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11875 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11876 GLYPH 0 means don't dump glyphs.\n\
11877 GLYPH 1 means dump glyphs in short form.\n\
11878 GLYPH > 1 or omitted means dump glyphs in long form.")
11879 (row, glyphs)
11880 Lisp_Object row, glyphs;
11882 struct frame *sf = SELECTED_FRAME ();
11883 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11884 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11885 return Qnil;
11889 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11890 Strace_redisplay_toggle, 0, 0, "",
11891 "Toggle tracing of redisplay.")
11894 trace_redisplay_p = !trace_redisplay_p;
11895 return Qnil;
11899 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11900 "Print STRING to stderr.")
11901 (string)
11902 Lisp_Object string;
11904 CHECK_STRING (string, 0);
11905 fprintf (stderr, "%s", XSTRING (string)->data);
11906 return Qnil;
11909 #endif /* GLYPH_DEBUG */
11913 /***********************************************************************
11914 Building Desired Matrix Rows
11915 ***********************************************************************/
11917 /* Return a temporary glyph row holding the glyphs of an overlay
11918 arrow. Only used for non-window-redisplay windows. */
11920 static struct glyph_row *
11921 get_overlay_arrow_glyph_row (w)
11922 struct window *w;
11924 struct frame *f = XFRAME (WINDOW_FRAME (w));
11925 struct buffer *buffer = XBUFFER (w->buffer);
11926 struct buffer *old = current_buffer;
11927 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11928 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11929 unsigned char *arrow_end = arrow_string + arrow_len;
11930 unsigned char *p;
11931 struct it it;
11932 int multibyte_p;
11933 int n_glyphs_before;
11935 set_buffer_temp (buffer);
11936 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11937 it.glyph_row->used[TEXT_AREA] = 0;
11938 SET_TEXT_POS (it.position, 0, 0);
11940 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11941 p = arrow_string;
11942 while (p < arrow_end)
11944 Lisp_Object face, ilisp;
11946 /* Get the next character. */
11947 if (multibyte_p)
11948 it.c = string_char_and_length (p, arrow_len, &it.len);
11949 else
11950 it.c = *p, it.len = 1;
11951 p += it.len;
11953 /* Get its face. */
11954 ilisp = make_number (p - arrow_string);
11955 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11956 it.face_id = compute_char_face (f, it.c, face);
11958 /* Compute its width, get its glyphs. */
11959 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11960 SET_TEXT_POS (it.position, -1, -1);
11961 PRODUCE_GLYPHS (&it);
11963 /* If this character doesn't fit any more in the line, we have
11964 to remove some glyphs. */
11965 if (it.current_x > it.last_visible_x)
11967 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11968 break;
11972 set_buffer_temp (old);
11973 return it.glyph_row;
11977 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11978 glyphs are only inserted for terminal frames since we can't really
11979 win with truncation glyphs when partially visible glyphs are
11980 involved. Which glyphs to insert is determined by
11981 produce_special_glyphs. */
11983 static void
11984 insert_left_trunc_glyphs (it)
11985 struct it *it;
11987 struct it truncate_it;
11988 struct glyph *from, *end, *to, *toend;
11990 xassert (!FRAME_WINDOW_P (it->f));
11992 /* Get the truncation glyphs. */
11993 truncate_it = *it;
11994 truncate_it.current_x = 0;
11995 truncate_it.face_id = DEFAULT_FACE_ID;
11996 truncate_it.glyph_row = &scratch_glyph_row;
11997 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11998 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11999 truncate_it.object = make_number (0);
12000 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12002 /* Overwrite glyphs from IT with truncation glyphs. */
12003 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12004 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12005 to = it->glyph_row->glyphs[TEXT_AREA];
12006 toend = to + it->glyph_row->used[TEXT_AREA];
12008 while (from < end)
12009 *to++ = *from++;
12011 /* There may be padding glyphs left over. Overwrite them too. */
12012 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12014 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12015 while (from < end)
12016 *to++ = *from++;
12019 if (to > toend)
12020 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12024 /* Compute the pixel height and width of IT->glyph_row.
12026 Most of the time, ascent and height of a display line will be equal
12027 to the max_ascent and max_height values of the display iterator
12028 structure. This is not the case if
12030 1. We hit ZV without displaying anything. In this case, max_ascent
12031 and max_height will be zero.
12033 2. We have some glyphs that don't contribute to the line height.
12034 (The glyph row flag contributes_to_line_height_p is for future
12035 pixmap extensions).
12037 The first case is easily covered by using default values because in
12038 these cases, the line height does not really matter, except that it
12039 must not be zero. */
12041 static void
12042 compute_line_metrics (it)
12043 struct it *it;
12045 struct glyph_row *row = it->glyph_row;
12046 int area, i;
12048 if (FRAME_WINDOW_P (it->f))
12050 int i, header_line_height;
12052 /* The line may consist of one space only, that was added to
12053 place the cursor on it. If so, the row's height hasn't been
12054 computed yet. */
12055 if (row->height == 0)
12057 if (it->max_ascent + it->max_descent == 0)
12058 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12059 row->ascent = it->max_ascent;
12060 row->height = it->max_ascent + it->max_descent;
12061 row->phys_ascent = it->max_phys_ascent;
12062 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12065 /* Compute the width of this line. */
12066 row->pixel_width = row->x;
12067 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12068 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12070 xassert (row->pixel_width >= 0);
12071 xassert (row->ascent >= 0 && row->height > 0);
12073 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12074 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12076 /* If first line's physical ascent is larger than its logical
12077 ascent, use the physical ascent, and make the row taller.
12078 This makes accented characters fully visible. */
12079 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12080 && row->phys_ascent > row->ascent)
12082 row->height += row->phys_ascent - row->ascent;
12083 row->ascent = row->phys_ascent;
12086 /* Compute how much of the line is visible. */
12087 row->visible_height = row->height;
12089 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12090 if (row->y < header_line_height)
12091 row->visible_height -= header_line_height - row->y;
12092 else
12094 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12095 if (row->y + row->height > max_y)
12096 row->visible_height -= row->y + row->height - max_y;
12099 else
12101 row->pixel_width = row->used[TEXT_AREA];
12102 row->ascent = row->phys_ascent = 0;
12103 row->height = row->phys_height = row->visible_height = 1;
12106 /* Compute a hash code for this row. */
12107 row->hash = 0;
12108 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12109 for (i = 0; i < row->used[area]; ++i)
12110 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12111 + row->glyphs[area][i].u.val
12112 + row->glyphs[area][i].face_id
12113 + row->glyphs[area][i].padding_p
12114 + (row->glyphs[area][i].type << 2));
12116 it->max_ascent = it->max_descent = 0;
12117 it->max_phys_ascent = it->max_phys_descent = 0;
12121 /* Append one space to the glyph row of iterator IT if doing a
12122 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12123 space have the default face, otherwise let it have the same face as
12124 IT->face_id. Value is non-zero if a space was added.
12126 This function is called to make sure that there is always one glyph
12127 at the end of a glyph row that the cursor can be set on under
12128 window-systems. (If there weren't such a glyph we would not know
12129 how wide and tall a box cursor should be displayed).
12131 At the same time this space let's a nicely handle clearing to the
12132 end of the line if the row ends in italic text. */
12134 static int
12135 append_space (it, default_face_p)
12136 struct it *it;
12137 int default_face_p;
12139 if (FRAME_WINDOW_P (it->f))
12141 int n = it->glyph_row->used[TEXT_AREA];
12143 if (it->glyph_row->glyphs[TEXT_AREA] + n
12144 < it->glyph_row->glyphs[1 + TEXT_AREA])
12146 /* Save some values that must not be changed.
12147 Must save IT->c and IT->len because otherwise
12148 ITERATOR_AT_END_P wouldn't work anymore after
12149 append_space has been called. */
12150 enum display_element_type saved_what = it->what;
12151 int saved_c = it->c, saved_len = it->len;
12152 int saved_x = it->current_x;
12153 int saved_face_id = it->face_id;
12154 struct text_pos saved_pos;
12155 Lisp_Object saved_object;
12156 struct face *face;
12158 saved_object = it->object;
12159 saved_pos = it->position;
12161 it->what = IT_CHARACTER;
12162 bzero (&it->position, sizeof it->position);
12163 it->object = make_number (0);
12164 it->c = ' ';
12165 it->len = 1;
12167 if (default_face_p)
12168 it->face_id = DEFAULT_FACE_ID;
12169 else if (it->face_before_selective_p)
12170 it->face_id = it->saved_face_id;
12171 face = FACE_FROM_ID (it->f, it->face_id);
12172 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12174 PRODUCE_GLYPHS (it);
12176 it->current_x = saved_x;
12177 it->object = saved_object;
12178 it->position = saved_pos;
12179 it->what = saved_what;
12180 it->face_id = saved_face_id;
12181 it->len = saved_len;
12182 it->c = saved_c;
12183 return 1;
12187 return 0;
12191 /* Extend the face of the last glyph in the text area of IT->glyph_row
12192 to the end of the display line. Called from display_line.
12193 If the glyph row is empty, add a space glyph to it so that we
12194 know the face to draw. Set the glyph row flag fill_line_p. */
12196 static void
12197 extend_face_to_end_of_line (it)
12198 struct it *it;
12200 struct face *face;
12201 struct frame *f = it->f;
12203 /* If line is already filled, do nothing. */
12204 if (it->current_x >= it->last_visible_x)
12205 return;
12207 /* Face extension extends the background and box of IT->face_id
12208 to the end of the line. If the background equals the background
12209 of the frame, we don't have to do anything. */
12210 if (it->face_before_selective_p)
12211 face = FACE_FROM_ID (it->f, it->saved_face_id);
12212 else
12213 face = FACE_FROM_ID (f, it->face_id);
12215 if (FRAME_WINDOW_P (f)
12216 && face->box == FACE_NO_BOX
12217 && face->background == FRAME_BACKGROUND_PIXEL (f)
12218 && !face->stipple)
12219 return;
12221 /* Set the glyph row flag indicating that the face of the last glyph
12222 in the text area has to be drawn to the end of the text area. */
12223 it->glyph_row->fill_line_p = 1;
12225 /* If current character of IT is not ASCII, make sure we have the
12226 ASCII face. This will be automatically undone the next time
12227 get_next_display_element returns a multibyte character. Note
12228 that the character will always be single byte in unibyte text. */
12229 if (!SINGLE_BYTE_CHAR_P (it->c))
12231 it->face_id = FACE_FOR_CHAR (f, face, 0);
12234 if (FRAME_WINDOW_P (f))
12236 /* If the row is empty, add a space with the current face of IT,
12237 so that we know which face to draw. */
12238 if (it->glyph_row->used[TEXT_AREA] == 0)
12240 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12241 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12242 it->glyph_row->used[TEXT_AREA] = 1;
12245 else
12247 /* Save some values that must not be changed. */
12248 int saved_x = it->current_x;
12249 struct text_pos saved_pos;
12250 Lisp_Object saved_object;
12251 enum display_element_type saved_what = it->what;
12252 int saved_face_id = it->face_id;
12254 saved_object = it->object;
12255 saved_pos = it->position;
12257 it->what = IT_CHARACTER;
12258 bzero (&it->position, sizeof it->position);
12259 it->object = make_number (0);
12260 it->c = ' ';
12261 it->len = 1;
12262 it->face_id = face->id;
12264 PRODUCE_GLYPHS (it);
12266 while (it->current_x <= it->last_visible_x)
12267 PRODUCE_GLYPHS (it);
12269 /* Don't count these blanks really. It would let us insert a left
12270 truncation glyph below and make us set the cursor on them, maybe. */
12271 it->current_x = saved_x;
12272 it->object = saved_object;
12273 it->position = saved_pos;
12274 it->what = saved_what;
12275 it->face_id = saved_face_id;
12280 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12281 trailing whitespace. */
12283 static int
12284 trailing_whitespace_p (charpos)
12285 int charpos;
12287 int bytepos = CHAR_TO_BYTE (charpos);
12288 int c = 0;
12290 while (bytepos < ZV_BYTE
12291 && (c = FETCH_CHAR (bytepos),
12292 c == ' ' || c == '\t'))
12293 ++bytepos;
12295 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12297 if (bytepos != PT_BYTE)
12298 return 1;
12300 return 0;
12304 /* Highlight trailing whitespace, if any, in ROW. */
12306 void
12307 highlight_trailing_whitespace (f, row)
12308 struct frame *f;
12309 struct glyph_row *row;
12311 int used = row->used[TEXT_AREA];
12313 if (used)
12315 struct glyph *start = row->glyphs[TEXT_AREA];
12316 struct glyph *glyph = start + used - 1;
12318 /* Skip over glyphs inserted to display the cursor at the
12319 end of a line, for extending the face of the last glyph
12320 to the end of the line on terminals, and for truncation
12321 and continuation glyphs. */
12322 while (glyph >= start
12323 && glyph->type == CHAR_GLYPH
12324 && INTEGERP (glyph->object))
12325 --glyph;
12327 /* If last glyph is a space or stretch, and it's trailing
12328 whitespace, set the face of all trailing whitespace glyphs in
12329 IT->glyph_row to `trailing-whitespace'. */
12330 if (glyph >= start
12331 && BUFFERP (glyph->object)
12332 && (glyph->type == STRETCH_GLYPH
12333 || (glyph->type == CHAR_GLYPH
12334 && glyph->u.ch == ' '))
12335 && trailing_whitespace_p (glyph->charpos))
12337 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12339 while (glyph >= start
12340 && BUFFERP (glyph->object)
12341 && (glyph->type == STRETCH_GLYPH
12342 || (glyph->type == CHAR_GLYPH
12343 && glyph->u.ch == ' ')))
12344 (glyph--)->face_id = face_id;
12350 /* Value is non-zero if glyph row ROW in window W should be
12351 used to hold the cursor. */
12353 static int
12354 cursor_row_p (w, row)
12355 struct window *w;
12356 struct glyph_row *row;
12358 int cursor_row_p = 1;
12360 if (PT == MATRIX_ROW_END_CHARPOS (row))
12362 /* If the row ends with a newline from a string, we don't want
12363 the cursor there (if the row is continued it doesn't end in a
12364 newline). */
12365 if (CHARPOS (row->end.string_pos) >= 0
12366 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12367 cursor_row_p = row->continued_p;
12369 /* If the row ends at ZV, display the cursor at the end of that
12370 row instead of at the start of the row below. */
12371 else if (row->ends_at_zv_p)
12372 cursor_row_p = 1;
12373 else
12374 cursor_row_p = 0;
12377 return cursor_row_p;
12381 /* Construct the glyph row IT->glyph_row in the desired matrix of
12382 IT->w from text at the current position of IT. See dispextern.h
12383 for an overview of struct it. Value is non-zero if
12384 IT->glyph_row displays text, as opposed to a line displaying ZV
12385 only. */
12387 static int
12388 display_line (it)
12389 struct it *it;
12391 struct glyph_row *row = it->glyph_row;
12393 /* We always start displaying at hpos zero even if hscrolled. */
12394 xassert (it->hpos == 0 && it->current_x == 0);
12396 /* We must not display in a row that's not a text row. */
12397 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12398 < it->w->desired_matrix->nrows);
12400 /* Is IT->w showing the region? */
12401 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12403 /* Clear the result glyph row and enable it. */
12404 prepare_desired_row (row);
12406 row->y = it->current_y;
12407 row->start = it->current;
12408 row->continuation_lines_width = it->continuation_lines_width;
12409 row->displays_text_p = 1;
12410 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12411 it->starts_in_middle_of_char_p = 0;
12413 /* Arrange the overlays nicely for our purposes. Usually, we call
12414 display_line on only one line at a time, in which case this
12415 can't really hurt too much, or we call it on lines which appear
12416 one after another in the buffer, in which case all calls to
12417 recenter_overlay_lists but the first will be pretty cheap. */
12418 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12420 /* Move over display elements that are not visible because we are
12421 hscrolled. This may stop at an x-position < IT->first_visible_x
12422 if the first glyph is partially visible or if we hit a line end. */
12423 if (it->current_x < it->first_visible_x)
12424 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12425 MOVE_TO_POS | MOVE_TO_X);
12427 /* Get the initial row height. This is either the height of the
12428 text hscrolled, if there is any, or zero. */
12429 row->ascent = it->max_ascent;
12430 row->height = it->max_ascent + it->max_descent;
12431 row->phys_ascent = it->max_phys_ascent;
12432 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12434 /* Loop generating characters. The loop is left with IT on the next
12435 character to display. */
12436 while (1)
12438 int n_glyphs_before, hpos_before, x_before;
12439 int x, i, nglyphs;
12440 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12442 /* Retrieve the next thing to display. Value is zero if end of
12443 buffer reached. */
12444 if (!get_next_display_element (it))
12446 /* Maybe add a space at the end of this line that is used to
12447 display the cursor there under X. Set the charpos of the
12448 first glyph of blank lines not corresponding to any text
12449 to -1. */
12450 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12451 || row->used[TEXT_AREA] == 0)
12453 row->glyphs[TEXT_AREA]->charpos = -1;
12454 row->displays_text_p = 0;
12456 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12457 row->indicate_empty_line_p = 1;
12460 it->continuation_lines_width = 0;
12461 row->ends_at_zv_p = 1;
12462 break;
12465 /* Now, get the metrics of what we want to display. This also
12466 generates glyphs in `row' (which is IT->glyph_row). */
12467 n_glyphs_before = row->used[TEXT_AREA];
12468 x = it->current_x;
12470 /* Remember the line height so far in case the next element doesn't
12471 fit on the line. */
12472 if (!it->truncate_lines_p)
12474 ascent = it->max_ascent;
12475 descent = it->max_descent;
12476 phys_ascent = it->max_phys_ascent;
12477 phys_descent = it->max_phys_descent;
12480 PRODUCE_GLYPHS (it);
12482 /* If this display element was in marginal areas, continue with
12483 the next one. */
12484 if (it->area != TEXT_AREA)
12486 row->ascent = max (row->ascent, it->max_ascent);
12487 row->height = max (row->height, it->max_ascent + it->max_descent);
12488 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12489 row->phys_height = max (row->phys_height,
12490 it->max_phys_ascent + it->max_phys_descent);
12491 set_iterator_to_next (it, 1);
12492 continue;
12495 /* Does the display element fit on the line? If we truncate
12496 lines, we should draw past the right edge of the window. If
12497 we don't truncate, we want to stop so that we can display the
12498 continuation glyph before the right margin. If lines are
12499 continued, there are two possible strategies for characters
12500 resulting in more than 1 glyph (e.g. tabs): Display as many
12501 glyphs as possible in this line and leave the rest for the
12502 continuation line, or display the whole element in the next
12503 line. Original redisplay did the former, so we do it also. */
12504 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12505 hpos_before = it->hpos;
12506 x_before = x;
12508 if (/* Not a newline. */
12509 nglyphs > 0
12510 /* Glyphs produced fit entirely in the line. */
12511 && it->current_x < it->last_visible_x)
12513 it->hpos += nglyphs;
12514 row->ascent = max (row->ascent, it->max_ascent);
12515 row->height = max (row->height, it->max_ascent + it->max_descent);
12516 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12517 row->phys_height = max (row->phys_height,
12518 it->max_phys_ascent + it->max_phys_descent);
12519 if (it->current_x - it->pixel_width < it->first_visible_x)
12520 row->x = x - it->first_visible_x;
12522 else
12524 int new_x;
12525 struct glyph *glyph;
12527 for (i = 0; i < nglyphs; ++i, x = new_x)
12529 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12530 new_x = x + glyph->pixel_width;
12532 if (/* Lines are continued. */
12533 !it->truncate_lines_p
12534 && (/* Glyph doesn't fit on the line. */
12535 new_x > it->last_visible_x
12536 /* Or it fits exactly on a window system frame. */
12537 || (new_x == it->last_visible_x
12538 && FRAME_WINDOW_P (it->f))))
12540 /* End of a continued line. */
12542 if (it->hpos == 0
12543 || (new_x == it->last_visible_x
12544 && FRAME_WINDOW_P (it->f)))
12546 /* Current glyph is the only one on the line or
12547 fits exactly on the line. We must continue
12548 the line because we can't draw the cursor
12549 after the glyph. */
12550 row->continued_p = 1;
12551 it->current_x = new_x;
12552 it->continuation_lines_width += new_x;
12553 ++it->hpos;
12554 if (i == nglyphs - 1)
12555 set_iterator_to_next (it, 1);
12557 else if (CHAR_GLYPH_PADDING_P (*glyph)
12558 && !FRAME_WINDOW_P (it->f))
12560 /* A padding glyph that doesn't fit on this line.
12561 This means the whole character doesn't fit
12562 on the line. */
12563 row->used[TEXT_AREA] = n_glyphs_before;
12565 /* Fill the rest of the row with continuation
12566 glyphs like in 20.x. */
12567 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12568 < row->glyphs[1 + TEXT_AREA])
12569 produce_special_glyphs (it, IT_CONTINUATION);
12571 row->continued_p = 1;
12572 it->current_x = x_before;
12573 it->continuation_lines_width += x_before;
12575 /* Restore the height to what it was before the
12576 element not fitting on the line. */
12577 it->max_ascent = ascent;
12578 it->max_descent = descent;
12579 it->max_phys_ascent = phys_ascent;
12580 it->max_phys_descent = phys_descent;
12582 else
12584 /* Display element draws past the right edge of
12585 the window. Restore positions to values
12586 before the element. The next line starts
12587 with current_x before the glyph that could
12588 not be displayed, so that TAB works right. */
12589 row->used[TEXT_AREA] = n_glyphs_before + i;
12591 /* Display continuation glyphs. */
12592 if (!FRAME_WINDOW_P (it->f))
12593 produce_special_glyphs (it, IT_CONTINUATION);
12594 row->continued_p = 1;
12596 it->current_x = x;
12597 it->continuation_lines_width += x;
12598 if (nglyphs > 1 && i > 0)
12600 row->ends_in_middle_of_char_p = 1;
12601 it->starts_in_middle_of_char_p = 1;
12604 /* Restore the height to what it was before the
12605 element not fitting on the line. */
12606 it->max_ascent = ascent;
12607 it->max_descent = descent;
12608 it->max_phys_ascent = phys_ascent;
12609 it->max_phys_descent = phys_descent;
12612 break;
12614 else if (new_x > it->first_visible_x)
12616 /* Increment number of glyphs actually displayed. */
12617 ++it->hpos;
12619 if (x < it->first_visible_x)
12620 /* Glyph is partially visible, i.e. row starts at
12621 negative X position. */
12622 row->x = x - it->first_visible_x;
12624 else
12626 /* Glyph is completely off the left margin of the
12627 window. This should not happen because of the
12628 move_it_in_display_line at the start of
12629 this function. */
12630 abort ();
12634 row->ascent = max (row->ascent, it->max_ascent);
12635 row->height = max (row->height, it->max_ascent + it->max_descent);
12636 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12637 row->phys_height = max (row->phys_height,
12638 it->max_phys_ascent + it->max_phys_descent);
12640 /* End of this display line if row is continued. */
12641 if (row->continued_p)
12642 break;
12645 /* Is this a line end? If yes, we're also done, after making
12646 sure that a non-default face is extended up to the right
12647 margin of the window. */
12648 if (ITERATOR_AT_END_OF_LINE_P (it))
12650 int used_before = row->used[TEXT_AREA];
12652 /* Add a space at the end of the line that is used to
12653 display the cursor there. */
12654 append_space (it, 0);
12656 /* Extend the face to the end of the line. */
12657 extend_face_to_end_of_line (it);
12659 /* Make sure we have the position. */
12660 if (used_before == 0)
12661 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12663 /* Consume the line end. This skips over invisible lines. */
12664 set_iterator_to_next (it, 1);
12665 it->continuation_lines_width = 0;
12666 break;
12669 /* Proceed with next display element. Note that this skips
12670 over lines invisible because of selective display. */
12671 set_iterator_to_next (it, 1);
12673 /* If we truncate lines, we are done when the last displayed
12674 glyphs reach past the right margin of the window. */
12675 if (it->truncate_lines_p
12676 && (FRAME_WINDOW_P (it->f)
12677 ? (it->current_x >= it->last_visible_x)
12678 : (it->current_x > it->last_visible_x)))
12680 /* Maybe add truncation glyphs. */
12681 if (!FRAME_WINDOW_P (it->f))
12683 int i, n;
12685 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12686 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12687 break;
12689 for (n = row->used[TEXT_AREA]; i < n; ++i)
12691 row->used[TEXT_AREA] = i;
12692 produce_special_glyphs (it, IT_TRUNCATION);
12696 row->truncated_on_right_p = 1;
12697 it->continuation_lines_width = 0;
12698 reseat_at_next_visible_line_start (it, 0);
12699 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12700 it->hpos = hpos_before;
12701 it->current_x = x_before;
12702 break;
12706 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12707 at the left window margin. */
12708 if (it->first_visible_x
12709 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12711 if (!FRAME_WINDOW_P (it->f))
12712 insert_left_trunc_glyphs (it);
12713 row->truncated_on_left_p = 1;
12716 /* If the start of this line is the overlay arrow-position, then
12717 mark this glyph row as the one containing the overlay arrow.
12718 This is clearly a mess with variable size fonts. It would be
12719 better to let it be displayed like cursors under X. */
12720 if (MARKERP (Voverlay_arrow_position)
12721 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12722 && (MATRIX_ROW_START_CHARPOS (row)
12723 == marker_position (Voverlay_arrow_position))
12724 && STRINGP (Voverlay_arrow_string)
12725 && ! overlay_arrow_seen)
12727 /* Overlay arrow in window redisplay is a bitmap. */
12728 if (!FRAME_WINDOW_P (it->f))
12730 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12731 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12732 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12733 struct glyph *p = row->glyphs[TEXT_AREA];
12734 struct glyph *p2, *end;
12736 /* Copy the arrow glyphs. */
12737 while (glyph < arrow_end)
12738 *p++ = *glyph++;
12740 /* Throw away padding glyphs. */
12741 p2 = p;
12742 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12743 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12744 ++p2;
12745 if (p2 > p)
12747 while (p2 < end)
12748 *p++ = *p2++;
12749 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12753 overlay_arrow_seen = 1;
12754 row->overlay_arrow_p = 1;
12757 /* Compute pixel dimensions of this line. */
12758 compute_line_metrics (it);
12760 /* Remember the position at which this line ends. */
12761 row->end = it->current;
12763 /* Maybe set the cursor. */
12764 if (it->w->cursor.vpos < 0
12765 && PT >= MATRIX_ROW_START_CHARPOS (row)
12766 && PT <= MATRIX_ROW_END_CHARPOS (row)
12767 && cursor_row_p (it->w, row))
12768 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12770 /* Highlight trailing whitespace. */
12771 if (!NILP (Vshow_trailing_whitespace))
12772 highlight_trailing_whitespace (it->f, it->glyph_row);
12774 /* Prepare for the next line. This line starts horizontally at (X
12775 HPOS) = (0 0). Vertical positions are incremented. As a
12776 convenience for the caller, IT->glyph_row is set to the next
12777 row to be used. */
12778 it->current_x = it->hpos = 0;
12779 it->current_y += row->height;
12780 ++it->vpos;
12781 ++it->glyph_row;
12782 return row->displays_text_p;
12787 /***********************************************************************
12788 Menu Bar
12789 ***********************************************************************/
12791 /* Redisplay the menu bar in the frame for window W.
12793 The menu bar of X frames that don't have X toolkit support is
12794 displayed in a special window W->frame->menu_bar_window.
12796 The menu bar of terminal frames is treated specially as far as
12797 glyph matrices are concerned. Menu bar lines are not part of
12798 windows, so the update is done directly on the frame matrix rows
12799 for the menu bar. */
12801 static void
12802 display_menu_bar (w)
12803 struct window *w;
12805 struct frame *f = XFRAME (WINDOW_FRAME (w));
12806 struct it it;
12807 Lisp_Object items;
12808 int i;
12810 /* Don't do all this for graphical frames. */
12811 #ifdef HAVE_NTGUI
12812 if (!NILP (Vwindow_system))
12813 return;
12814 #endif
12815 #ifdef USE_X_TOOLKIT
12816 if (FRAME_X_P (f))
12817 return;
12818 #endif
12819 #ifdef macintosh
12820 if (FRAME_MAC_P (f))
12821 return;
12822 #endif
12824 #ifdef USE_X_TOOLKIT
12825 xassert (!FRAME_WINDOW_P (f));
12826 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12827 it.first_visible_x = 0;
12828 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12829 #else /* not USE_X_TOOLKIT */
12830 if (FRAME_WINDOW_P (f))
12832 /* Menu bar lines are displayed in the desired matrix of the
12833 dummy window menu_bar_window. */
12834 struct window *menu_w;
12835 xassert (WINDOWP (f->menu_bar_window));
12836 menu_w = XWINDOW (f->menu_bar_window);
12837 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12838 MENU_FACE_ID);
12839 it.first_visible_x = 0;
12840 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12842 else
12844 /* This is a TTY frame, i.e. character hpos/vpos are used as
12845 pixel x/y. */
12846 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12847 MENU_FACE_ID);
12848 it.first_visible_x = 0;
12849 it.last_visible_x = FRAME_WIDTH (f);
12851 #endif /* not USE_X_TOOLKIT */
12853 if (! mode_line_inverse_video)
12854 /* Force the menu-bar to be displayed in the default face. */
12855 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12857 /* Clear all rows of the menu bar. */
12858 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12860 struct glyph_row *row = it.glyph_row + i;
12861 clear_glyph_row (row);
12862 row->enabled_p = 1;
12863 row->full_width_p = 1;
12866 /* Display all items of the menu bar. */
12867 items = FRAME_MENU_BAR_ITEMS (it.f);
12868 for (i = 0; i < XVECTOR (items)->size; i += 4)
12870 Lisp_Object string;
12872 /* Stop at nil string. */
12873 string = AREF (items, i + 1);
12874 if (NILP (string))
12875 break;
12877 /* Remember where item was displayed. */
12878 AREF (items, i + 3) = make_number (it.hpos);
12880 /* Display the item, pad with one space. */
12881 if (it.current_x < it.last_visible_x)
12882 display_string (NULL, string, Qnil, 0, 0, &it,
12883 XSTRING (string)->size + 1, 0, 0, -1);
12886 /* Fill out the line with spaces. */
12887 if (it.current_x < it.last_visible_x)
12888 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12890 /* Compute the total height of the lines. */
12891 compute_line_metrics (&it);
12896 /***********************************************************************
12897 Mode Line
12898 ***********************************************************************/
12900 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12901 FORCE is non-zero, redisplay mode lines unconditionally.
12902 Otherwise, redisplay only mode lines that are garbaged. Value is
12903 the number of windows whose mode lines were redisplayed. */
12905 static int
12906 redisplay_mode_lines (window, force)
12907 Lisp_Object window;
12908 int force;
12910 int nwindows = 0;
12912 while (!NILP (window))
12914 struct window *w = XWINDOW (window);
12916 if (WINDOWP (w->hchild))
12917 nwindows += redisplay_mode_lines (w->hchild, force);
12918 else if (WINDOWP (w->vchild))
12919 nwindows += redisplay_mode_lines (w->vchild, force);
12920 else if (force
12921 || FRAME_GARBAGED_P (XFRAME (w->frame))
12922 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12924 Lisp_Object old_selected_frame;
12925 struct text_pos lpoint;
12926 struct buffer *old = current_buffer;
12928 /* Set the window's buffer for the mode line display. */
12929 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12930 set_buffer_internal_1 (XBUFFER (w->buffer));
12932 /* Point refers normally to the selected window. For any
12933 other window, set up appropriate value. */
12934 if (!EQ (window, selected_window))
12936 struct text_pos pt;
12938 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12939 if (CHARPOS (pt) < BEGV)
12940 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12941 else if (CHARPOS (pt) > (ZV - 1))
12942 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12943 else
12944 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12947 /* Temporarily set up the selected frame. */
12948 old_selected_frame = selected_frame;
12949 selected_frame = w->frame;
12951 /* Display mode lines. */
12952 clear_glyph_matrix (w->desired_matrix);
12953 if (display_mode_lines (w))
12955 ++nwindows;
12956 w->must_be_updated_p = 1;
12959 /* Restore old settings. */
12960 selected_frame = old_selected_frame;
12961 set_buffer_internal_1 (old);
12962 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12965 window = w->next;
12968 return nwindows;
12972 /* Display the mode and/or top line of window W. Value is the number
12973 of mode lines displayed. */
12975 static int
12976 display_mode_lines (w)
12977 struct window *w;
12979 int n = 0;
12981 /* These will be set while the mode line specs are processed. */
12982 line_number_displayed = 0;
12983 w->column_number_displayed = Qnil;
12985 if (WINDOW_WANTS_MODELINE_P (w))
12987 display_mode_line (w, MODE_LINE_FACE_ID,
12988 current_buffer->mode_line_format);
12989 ++n;
12992 if (WINDOW_WANTS_HEADER_LINE_P (w))
12994 display_mode_line (w, HEADER_LINE_FACE_ID,
12995 current_buffer->header_line_format);
12996 ++n;
12999 return n;
13003 /* Display mode or top line of window W. FACE_ID specifies which line
13004 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13005 FORMAT is the mode line format to display. Value is the pixel
13006 height of the mode line displayed. */
13008 static int
13009 display_mode_line (w, face_id, format)
13010 struct window *w;
13011 enum face_id face_id;
13012 Lisp_Object format;
13014 struct it it;
13015 struct face *face;
13017 init_iterator (&it, w, -1, -1, NULL, face_id);
13018 prepare_desired_row (it.glyph_row);
13020 if (! mode_line_inverse_video)
13021 /* Force the mode-line to be displayed in the default face. */
13022 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13024 /* Temporarily make frame's keyboard the current kboard so that
13025 kboard-local variables in the mode_line_format will get the right
13026 values. */
13027 push_frame_kboard (it.f);
13028 display_mode_element (&it, 0, 0, 0, format);
13029 pop_frame_kboard ();
13031 /* Fill up with spaces. */
13032 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13034 compute_line_metrics (&it);
13035 it.glyph_row->full_width_p = 1;
13036 it.glyph_row->mode_line_p = 1;
13037 it.glyph_row->inverse_p = 0;
13038 it.glyph_row->continued_p = 0;
13039 it.glyph_row->truncated_on_left_p = 0;
13040 it.glyph_row->truncated_on_right_p = 0;
13042 /* Make a 3D mode-line have a shadow at its right end. */
13043 face = FACE_FROM_ID (it.f, face_id);
13044 extend_face_to_end_of_line (&it);
13045 if (face->box != FACE_NO_BOX)
13047 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13048 + it.glyph_row->used[TEXT_AREA] - 1);
13049 last->right_box_line_p = 1;
13052 return it.glyph_row->height;
13056 /* Contribute ELT to the mode line for window IT->w. How it
13057 translates into text depends on its data type.
13059 IT describes the display environment in which we display, as usual.
13061 DEPTH is the depth in recursion. It is used to prevent
13062 infinite recursion here.
13064 FIELD_WIDTH is the number of characters the display of ELT should
13065 occupy in the mode line, and PRECISION is the maximum number of
13066 characters to display from ELT's representation. See
13067 display_string for details. *
13069 Returns the hpos of the end of the text generated by ELT. */
13071 static int
13072 display_mode_element (it, depth, field_width, precision, elt)
13073 struct it *it;
13074 int depth;
13075 int field_width, precision;
13076 Lisp_Object elt;
13078 int n = 0, field, prec;
13080 tail_recurse:
13081 if (depth > 10)
13082 goto invalid;
13084 depth++;
13086 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13088 case Lisp_String:
13090 /* A string: output it and check for %-constructs within it. */
13091 unsigned char c;
13092 unsigned char *this = XSTRING (elt)->data;
13093 unsigned char *lisp_string = this;
13095 while ((precision <= 0 || n < precision)
13096 && *this
13097 && (frame_title_ptr
13098 || it->current_x < it->last_visible_x))
13100 unsigned char *last = this;
13102 /* Advance to end of string or next format specifier. */
13103 while ((c = *this++) != '\0' && c != '%')
13106 if (this - 1 != last)
13108 /* Output to end of string or up to '%'. Field width
13109 is length of string. Don't output more than
13110 PRECISION allows us. */
13111 --this;
13112 prec = strwidth (last, this - last);
13113 if (precision > 0 && prec > precision - n)
13114 prec = precision - n;
13116 if (frame_title_ptr)
13117 n += store_frame_title (last, 0, prec);
13118 else
13119 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13120 it, 0, prec, 0, -1);
13122 else /* c == '%' */
13124 unsigned char *percent_position = this;
13126 /* Get the specified minimum width. Zero means
13127 don't pad. */
13128 field = 0;
13129 while ((c = *this++) >= '0' && c <= '9')
13130 field = field * 10 + c - '0';
13132 /* Don't pad beyond the total padding allowed. */
13133 if (field_width - n > 0 && field > field_width - n)
13134 field = field_width - n;
13136 /* Note that either PRECISION <= 0 or N < PRECISION. */
13137 prec = precision - n;
13139 if (c == 'M')
13140 n += display_mode_element (it, depth, field, prec,
13141 Vglobal_mode_string);
13142 else if (c != 0)
13144 unsigned char *spec
13145 = decode_mode_spec (it->w, c, field, prec);
13147 if (frame_title_ptr)
13148 n += store_frame_title (spec, field, prec);
13149 else
13151 int nglyphs_before
13152 = it->glyph_row->used[TEXT_AREA];
13153 int charpos
13154 = percent_position - XSTRING (elt)->data;
13155 int nwritten
13156 = display_string (spec, Qnil, elt, charpos, 0, it,
13157 field, prec, 0, -1);
13159 /* Assign to the glyphs written above the
13160 string where the `%x' came from, position
13161 of the `%'. */
13162 if (nwritten > 0)
13164 struct glyph *glyph
13165 = (it->glyph_row->glyphs[TEXT_AREA]
13166 + nglyphs_before);
13167 int i;
13169 for (i = 0; i < nwritten; ++i)
13171 glyph[i].object = elt;
13172 glyph[i].charpos = charpos;
13175 n += nwritten;
13182 break;
13184 case Lisp_Symbol:
13185 /* A symbol: process the value of the symbol recursively
13186 as if it appeared here directly. Avoid error if symbol void.
13187 Special case: if value of symbol is a string, output the string
13188 literally. */
13190 register Lisp_Object tem;
13191 tem = Fboundp (elt);
13192 if (!NILP (tem))
13194 tem = Fsymbol_value (elt);
13195 /* If value is a string, output that string literally:
13196 don't check for % within it. */
13197 if (STRINGP (tem))
13199 prec = precision - n;
13200 if (frame_title_ptr)
13201 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13202 else
13203 n += display_string (NULL, tem, Qnil, 0, 0, it,
13204 0, prec, 0, -1);
13206 else if (!EQ (tem, elt))
13208 /* Give up right away for nil or t. */
13209 elt = tem;
13210 goto tail_recurse;
13214 break;
13216 case Lisp_Cons:
13218 register Lisp_Object car, tem;
13220 /* A cons cell: three distinct cases.
13221 If first element is a string or a cons, process all the elements
13222 and effectively concatenate them.
13223 If first element is a negative number, truncate displaying cdr to
13224 at most that many characters. If positive, pad (with spaces)
13225 to at least that many characters.
13226 If first element is a symbol, process the cadr or caddr recursively
13227 according to whether the symbol's value is non-nil or nil. */
13228 car = XCAR (elt);
13229 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13231 /* An element of the form (:eval FORM) means evaluate FORM
13232 and use the result as mode line elements. */
13233 struct gcpro gcpro1;
13234 Lisp_Object spec;
13236 spec = safe_eval (XCAR (XCDR (elt)));
13237 GCPRO1 (spec);
13238 n += display_mode_element (it, depth, field_width - n,
13239 precision - n, spec);
13240 UNGCPRO;
13242 else if (SYMBOLP (car))
13244 tem = Fboundp (car);
13245 elt = XCDR (elt);
13246 if (!CONSP (elt))
13247 goto invalid;
13248 /* elt is now the cdr, and we know it is a cons cell.
13249 Use its car if CAR has a non-nil value. */
13250 if (!NILP (tem))
13252 tem = Fsymbol_value (car);
13253 if (!NILP (tem))
13255 elt = XCAR (elt);
13256 goto tail_recurse;
13259 /* Symbol's value is nil (or symbol is unbound)
13260 Get the cddr of the original list
13261 and if possible find the caddr and use that. */
13262 elt = XCDR (elt);
13263 if (NILP (elt))
13264 break;
13265 else if (!CONSP (elt))
13266 goto invalid;
13267 elt = XCAR (elt);
13268 goto tail_recurse;
13270 else if (INTEGERP (car))
13272 register int lim = XINT (car);
13273 elt = XCDR (elt);
13274 if (lim < 0)
13276 /* Negative int means reduce maximum width. */
13277 if (precision <= 0)
13278 precision = -lim;
13279 else
13280 precision = min (precision, -lim);
13282 else if (lim > 0)
13284 /* Padding specified. Don't let it be more than
13285 current maximum. */
13286 if (precision > 0)
13287 lim = min (precision, lim);
13289 /* If that's more padding than already wanted, queue it.
13290 But don't reduce padding already specified even if
13291 that is beyond the current truncation point. */
13292 field_width = max (lim, field_width);
13294 goto tail_recurse;
13296 else if (STRINGP (car) || CONSP (car))
13298 register int limit = 50;
13299 /* Limit is to protect against circular lists. */
13300 while (CONSP (elt)
13301 && --limit > 0
13302 && (precision <= 0 || n < precision))
13304 n += display_mode_element (it, depth, field_width - n,
13305 precision - n, XCAR (elt));
13306 elt = XCDR (elt);
13310 break;
13312 default:
13313 invalid:
13314 if (frame_title_ptr)
13315 n += store_frame_title ("*invalid*", 0, precision - n);
13316 else
13317 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13318 precision - n, 0, 0);
13319 return n;
13322 /* Pad to FIELD_WIDTH. */
13323 if (field_width > 0 && n < field_width)
13325 if (frame_title_ptr)
13326 n += store_frame_title ("", field_width - n, 0);
13327 else
13328 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13329 0, 0, 0);
13332 return n;
13336 /* Write a null-terminated, right justified decimal representation of
13337 the positive integer D to BUF using a minimal field width WIDTH. */
13339 static void
13340 pint2str (buf, width, d)
13341 register char *buf;
13342 register int width;
13343 register int d;
13345 register char *p = buf;
13347 if (d <= 0)
13348 *p++ = '0';
13349 else
13351 while (d > 0)
13353 *p++ = d % 10 + '0';
13354 d /= 10;
13358 for (width -= (int) (p - buf); width > 0; --width)
13359 *p++ = ' ';
13360 *p-- = '\0';
13361 while (p > buf)
13363 d = *buf;
13364 *buf++ = *p;
13365 *p-- = d;
13369 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13370 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13371 type of CODING_SYSTEM. Return updated pointer into BUF. */
13373 static unsigned char invalid_eol_type[] = "(*invalid*)";
13375 static char *
13376 decode_mode_spec_coding (coding_system, buf, eol_flag)
13377 Lisp_Object coding_system;
13378 register char *buf;
13379 int eol_flag;
13381 Lisp_Object val;
13382 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13383 unsigned char *eol_str;
13384 int eol_str_len;
13385 /* The EOL conversion we are using. */
13386 Lisp_Object eoltype;
13388 val = Fget (coding_system, Qcoding_system);
13389 eoltype = Qnil;
13391 if (!VECTORP (val)) /* Not yet decided. */
13393 if (multibyte)
13394 *buf++ = '-';
13395 if (eol_flag)
13396 eoltype = eol_mnemonic_undecided;
13397 /* Don't mention EOL conversion if it isn't decided. */
13399 else
13401 Lisp_Object eolvalue;
13403 eolvalue = Fget (coding_system, Qeol_type);
13405 if (multibyte)
13406 *buf++ = XFASTINT (AREF (val, 1));
13408 if (eol_flag)
13410 /* The EOL conversion that is normal on this system. */
13412 if (NILP (eolvalue)) /* Not yet decided. */
13413 eoltype = eol_mnemonic_undecided;
13414 else if (VECTORP (eolvalue)) /* Not yet decided. */
13415 eoltype = eol_mnemonic_undecided;
13416 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13417 eoltype = (XFASTINT (eolvalue) == 0
13418 ? eol_mnemonic_unix
13419 : (XFASTINT (eolvalue) == 1
13420 ? eol_mnemonic_dos : eol_mnemonic_mac));
13424 if (eol_flag)
13426 /* Mention the EOL conversion if it is not the usual one. */
13427 if (STRINGP (eoltype))
13429 eol_str = XSTRING (eoltype)->data;
13430 eol_str_len = XSTRING (eoltype)->size;
13432 else if (INTEGERP (eoltype)
13433 && CHAR_VALID_P (XINT (eoltype), 0))
13435 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13436 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13438 else
13440 eol_str = invalid_eol_type;
13441 eol_str_len = sizeof (invalid_eol_type) - 1;
13443 bcopy (eol_str, buf, eol_str_len);
13444 buf += eol_str_len;
13447 return buf;
13450 /* Return a string for the output of a mode line %-spec for window W,
13451 generated by character C. PRECISION >= 0 means don't return a
13452 string longer than that value. FIELD_WIDTH > 0 means pad the
13453 string returned with spaces to that value. */
13455 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13457 static char *
13458 decode_mode_spec (w, c, field_width, precision)
13459 struct window *w;
13460 register int c;
13461 int field_width, precision;
13463 Lisp_Object obj;
13464 struct frame *f = XFRAME (WINDOW_FRAME (w));
13465 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13466 struct buffer *b = XBUFFER (w->buffer);
13468 obj = Qnil;
13470 switch (c)
13472 case '*':
13473 if (!NILP (b->read_only))
13474 return "%";
13475 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13476 return "*";
13477 return "-";
13479 case '+':
13480 /* This differs from %* only for a modified read-only buffer. */
13481 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13482 return "*";
13483 if (!NILP (b->read_only))
13484 return "%";
13485 return "-";
13487 case '&':
13488 /* This differs from %* in ignoring read-only-ness. */
13489 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13490 return "*";
13491 return "-";
13493 case '%':
13494 return "%";
13496 case '[':
13498 int i;
13499 char *p;
13501 if (command_loop_level > 5)
13502 return "[[[... ";
13503 p = decode_mode_spec_buf;
13504 for (i = 0; i < command_loop_level; i++)
13505 *p++ = '[';
13506 *p = 0;
13507 return decode_mode_spec_buf;
13510 case ']':
13512 int i;
13513 char *p;
13515 if (command_loop_level > 5)
13516 return " ...]]]";
13517 p = decode_mode_spec_buf;
13518 for (i = 0; i < command_loop_level; i++)
13519 *p++ = ']';
13520 *p = 0;
13521 return decode_mode_spec_buf;
13524 case '-':
13526 register int i;
13528 /* Let lots_of_dashes be a string of infinite length. */
13529 if (field_width <= 0
13530 || field_width > sizeof (lots_of_dashes))
13532 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13533 decode_mode_spec_buf[i] = '-';
13534 decode_mode_spec_buf[i] = '\0';
13535 return decode_mode_spec_buf;
13537 else
13538 return lots_of_dashes;
13541 case 'b':
13542 obj = b->name;
13543 break;
13545 case 'c':
13547 int col = current_column ();
13548 w->column_number_displayed = make_number (col);
13549 pint2str (decode_mode_spec_buf, field_width, col);
13550 return decode_mode_spec_buf;
13553 case 'F':
13554 /* %F displays the frame name. */
13555 if (!NILP (f->title))
13556 return (char *) XSTRING (f->title)->data;
13557 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13558 return (char *) XSTRING (f->name)->data;
13559 return "Emacs";
13561 case 'f':
13562 obj = b->filename;
13563 break;
13565 case 'l':
13567 int startpos = XMARKER (w->start)->charpos;
13568 int startpos_byte = marker_byte_position (w->start);
13569 int line, linepos, linepos_byte, topline;
13570 int nlines, junk;
13571 int height = XFASTINT (w->height);
13573 /* If we decided that this buffer isn't suitable for line numbers,
13574 don't forget that too fast. */
13575 if (EQ (w->base_line_pos, w->buffer))
13576 goto no_value;
13577 /* But do forget it, if the window shows a different buffer now. */
13578 else if (BUFFERP (w->base_line_pos))
13579 w->base_line_pos = Qnil;
13581 /* If the buffer is very big, don't waste time. */
13582 if (INTEGERP (Vline_number_display_limit)
13583 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13585 w->base_line_pos = Qnil;
13586 w->base_line_number = Qnil;
13587 goto no_value;
13590 if (!NILP (w->base_line_number)
13591 && !NILP (w->base_line_pos)
13592 && XFASTINT (w->base_line_pos) <= startpos)
13594 line = XFASTINT (w->base_line_number);
13595 linepos = XFASTINT (w->base_line_pos);
13596 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13598 else
13600 line = 1;
13601 linepos = BUF_BEGV (b);
13602 linepos_byte = BUF_BEGV_BYTE (b);
13605 /* Count lines from base line to window start position. */
13606 nlines = display_count_lines (linepos, linepos_byte,
13607 startpos_byte,
13608 startpos, &junk);
13610 topline = nlines + line;
13612 /* Determine a new base line, if the old one is too close
13613 or too far away, or if we did not have one.
13614 "Too close" means it's plausible a scroll-down would
13615 go back past it. */
13616 if (startpos == BUF_BEGV (b))
13618 w->base_line_number = make_number (topline);
13619 w->base_line_pos = make_number (BUF_BEGV (b));
13621 else if (nlines < height + 25 || nlines > height * 3 + 50
13622 || linepos == BUF_BEGV (b))
13624 int limit = BUF_BEGV (b);
13625 int limit_byte = BUF_BEGV_BYTE (b);
13626 int position;
13627 int distance = (height * 2 + 30) * line_number_display_limit_width;
13629 if (startpos - distance > limit)
13631 limit = startpos - distance;
13632 limit_byte = CHAR_TO_BYTE (limit);
13635 nlines = display_count_lines (startpos, startpos_byte,
13636 limit_byte,
13637 - (height * 2 + 30),
13638 &position);
13639 /* If we couldn't find the lines we wanted within
13640 line_number_display_limit_width chars per line,
13641 give up on line numbers for this window. */
13642 if (position == limit_byte && limit == startpos - distance)
13644 w->base_line_pos = w->buffer;
13645 w->base_line_number = Qnil;
13646 goto no_value;
13649 w->base_line_number = make_number (topline - nlines);
13650 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13653 /* Now count lines from the start pos to point. */
13654 nlines = display_count_lines (startpos, startpos_byte,
13655 PT_BYTE, PT, &junk);
13657 /* Record that we did display the line number. */
13658 line_number_displayed = 1;
13660 /* Make the string to show. */
13661 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13662 return decode_mode_spec_buf;
13663 no_value:
13665 char* p = decode_mode_spec_buf;
13666 int pad = field_width - 2;
13667 while (pad-- > 0)
13668 *p++ = ' ';
13669 *p++ = '?';
13670 *p++ = '?';
13671 *p = '\0';
13672 return decode_mode_spec_buf;
13675 break;
13677 case 'm':
13678 obj = b->mode_name;
13679 break;
13681 case 'n':
13682 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13683 return " Narrow";
13684 break;
13686 case 'p':
13688 int pos = marker_position (w->start);
13689 int total = BUF_ZV (b) - BUF_BEGV (b);
13691 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13693 if (pos <= BUF_BEGV (b))
13694 return "All";
13695 else
13696 return "Bottom";
13698 else if (pos <= BUF_BEGV (b))
13699 return "Top";
13700 else
13702 if (total > 1000000)
13703 /* Do it differently for a large value, to avoid overflow. */
13704 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13705 else
13706 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13707 /* We can't normally display a 3-digit number,
13708 so get us a 2-digit number that is close. */
13709 if (total == 100)
13710 total = 99;
13711 sprintf (decode_mode_spec_buf, "%2d%%", total);
13712 return decode_mode_spec_buf;
13716 /* Display percentage of size above the bottom of the screen. */
13717 case 'P':
13719 int toppos = marker_position (w->start);
13720 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13721 int total = BUF_ZV (b) - BUF_BEGV (b);
13723 if (botpos >= BUF_ZV (b))
13725 if (toppos <= BUF_BEGV (b))
13726 return "All";
13727 else
13728 return "Bottom";
13730 else
13732 if (total > 1000000)
13733 /* Do it differently for a large value, to avoid overflow. */
13734 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13735 else
13736 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13737 /* We can't normally display a 3-digit number,
13738 so get us a 2-digit number that is close. */
13739 if (total == 100)
13740 total = 99;
13741 if (toppos <= BUF_BEGV (b))
13742 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13743 else
13744 sprintf (decode_mode_spec_buf, "%2d%%", total);
13745 return decode_mode_spec_buf;
13749 case 's':
13750 /* status of process */
13751 obj = Fget_buffer_process (w->buffer);
13752 if (NILP (obj))
13753 return "no process";
13754 #ifdef subprocesses
13755 obj = Fsymbol_name (Fprocess_status (obj));
13756 #endif
13757 break;
13759 case 't': /* indicate TEXT or BINARY */
13760 #ifdef MODE_LINE_BINARY_TEXT
13761 return MODE_LINE_BINARY_TEXT (b);
13762 #else
13763 return "T";
13764 #endif
13766 case 'z':
13767 /* coding-system (not including end-of-line format) */
13768 case 'Z':
13769 /* coding-system (including end-of-line type) */
13771 int eol_flag = (c == 'Z');
13772 char *p = decode_mode_spec_buf;
13774 if (! FRAME_WINDOW_P (f))
13776 /* No need to mention EOL here--the terminal never needs
13777 to do EOL conversion. */
13778 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13779 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13781 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13782 p, eol_flag);
13784 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13785 #ifdef subprocesses
13786 obj = Fget_buffer_process (Fcurrent_buffer ());
13787 if (PROCESSP (obj))
13789 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13790 p, eol_flag);
13791 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13792 p, eol_flag);
13794 #endif /* subprocesses */
13795 #endif /* 0 */
13796 *p = 0;
13797 return decode_mode_spec_buf;
13801 if (STRINGP (obj))
13802 return (char *) XSTRING (obj)->data;
13803 else
13804 return "";
13808 /* Count up to COUNT lines starting from START / START_BYTE.
13809 But don't go beyond LIMIT_BYTE.
13810 Return the number of lines thus found (always nonnegative).
13812 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13814 static int
13815 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13816 int start, start_byte, limit_byte, count;
13817 int *byte_pos_ptr;
13819 register unsigned char *cursor;
13820 unsigned char *base;
13822 register int ceiling;
13823 register unsigned char *ceiling_addr;
13824 int orig_count = count;
13826 /* If we are not in selective display mode,
13827 check only for newlines. */
13828 int selective_display = (!NILP (current_buffer->selective_display)
13829 && !INTEGERP (current_buffer->selective_display));
13831 if (count > 0)
13833 while (start_byte < limit_byte)
13835 ceiling = BUFFER_CEILING_OF (start_byte);
13836 ceiling = min (limit_byte - 1, ceiling);
13837 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13838 base = (cursor = BYTE_POS_ADDR (start_byte));
13839 while (1)
13841 if (selective_display)
13842 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13844 else
13845 while (*cursor != '\n' && ++cursor != ceiling_addr)
13848 if (cursor != ceiling_addr)
13850 if (--count == 0)
13852 start_byte += cursor - base + 1;
13853 *byte_pos_ptr = start_byte;
13854 return orig_count;
13856 else
13857 if (++cursor == ceiling_addr)
13858 break;
13860 else
13861 break;
13863 start_byte += cursor - base;
13866 else
13868 while (start_byte > limit_byte)
13870 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13871 ceiling = max (limit_byte, ceiling);
13872 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13873 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13874 while (1)
13876 if (selective_display)
13877 while (--cursor != ceiling_addr
13878 && *cursor != '\n' && *cursor != 015)
13880 else
13881 while (--cursor != ceiling_addr && *cursor != '\n')
13884 if (cursor != ceiling_addr)
13886 if (++count == 0)
13888 start_byte += cursor - base + 1;
13889 *byte_pos_ptr = start_byte;
13890 /* When scanning backwards, we should
13891 not count the newline posterior to which we stop. */
13892 return - orig_count - 1;
13895 else
13896 break;
13898 /* Here we add 1 to compensate for the last decrement
13899 of CURSOR, which took it past the valid range. */
13900 start_byte += cursor - base + 1;
13904 *byte_pos_ptr = limit_byte;
13906 if (count < 0)
13907 return - orig_count + count;
13908 return orig_count - count;
13914 /***********************************************************************
13915 Displaying strings
13916 ***********************************************************************/
13918 /* Display a NUL-terminated string, starting with index START.
13920 If STRING is non-null, display that C string. Otherwise, the Lisp
13921 string LISP_STRING is displayed.
13923 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13924 FACE_STRING. Display STRING or LISP_STRING with the face at
13925 FACE_STRING_POS in FACE_STRING:
13927 Display the string in the environment given by IT, but use the
13928 standard display table, temporarily.
13930 FIELD_WIDTH is the minimum number of output glyphs to produce.
13931 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13932 with spaces. If STRING has more characters, more than FIELD_WIDTH
13933 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13935 PRECISION is the maximum number of characters to output from
13936 STRING. PRECISION < 0 means don't truncate the string.
13938 This is roughly equivalent to printf format specifiers:
13940 FIELD_WIDTH PRECISION PRINTF
13941 ----------------------------------------
13942 -1 -1 %s
13943 -1 10 %.10s
13944 10 -1 %10s
13945 20 10 %20.10s
13947 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13948 display them, and < 0 means obey the current buffer's value of
13949 enable_multibyte_characters.
13951 Value is the number of glyphs produced. */
13953 static int
13954 display_string (string, lisp_string, face_string, face_string_pos,
13955 start, it, field_width, precision, max_x, multibyte)
13956 unsigned char *string;
13957 Lisp_Object lisp_string;
13958 Lisp_Object face_string;
13959 int face_string_pos;
13960 int start;
13961 struct it *it;
13962 int field_width, precision, max_x;
13963 int multibyte;
13965 int hpos_at_start = it->hpos;
13966 int saved_face_id = it->face_id;
13967 struct glyph_row *row = it->glyph_row;
13969 /* Initialize the iterator IT for iteration over STRING beginning
13970 with index START. */
13971 reseat_to_string (it, string, lisp_string, start,
13972 precision, field_width, multibyte);
13974 /* If displaying STRING, set up the face of the iterator
13975 from LISP_STRING, if that's given. */
13976 if (STRINGP (face_string))
13978 int endptr;
13979 struct face *face;
13981 it->face_id
13982 = face_at_string_position (it->w, face_string, face_string_pos,
13983 0, it->region_beg_charpos,
13984 it->region_end_charpos,
13985 &endptr, it->base_face_id, 0);
13986 face = FACE_FROM_ID (it->f, it->face_id);
13987 it->face_box_p = face->box != FACE_NO_BOX;
13990 /* Set max_x to the maximum allowed X position. Don't let it go
13991 beyond the right edge of the window. */
13992 if (max_x <= 0)
13993 max_x = it->last_visible_x;
13994 else
13995 max_x = min (max_x, it->last_visible_x);
13997 /* Skip over display elements that are not visible. because IT->w is
13998 hscrolled. */
13999 if (it->current_x < it->first_visible_x)
14000 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14001 MOVE_TO_POS | MOVE_TO_X);
14003 row->ascent = it->max_ascent;
14004 row->height = it->max_ascent + it->max_descent;
14005 row->phys_ascent = it->max_phys_ascent;
14006 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14008 /* This condition is for the case that we are called with current_x
14009 past last_visible_x. */
14010 while (it->current_x < max_x)
14012 int x_before, x, n_glyphs_before, i, nglyphs;
14014 /* Get the next display element. */
14015 if (!get_next_display_element (it))
14016 break;
14018 /* Produce glyphs. */
14019 x_before = it->current_x;
14020 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14021 PRODUCE_GLYPHS (it);
14023 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14024 i = 0;
14025 x = x_before;
14026 while (i < nglyphs)
14028 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14030 if (!it->truncate_lines_p
14031 && x + glyph->pixel_width > max_x)
14033 /* End of continued line or max_x reached. */
14034 if (CHAR_GLYPH_PADDING_P (*glyph))
14036 /* A wide character is unbreakable. */
14037 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14038 it->current_x = x_before;
14040 else
14042 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14043 it->current_x = x;
14045 break;
14047 else if (x + glyph->pixel_width > it->first_visible_x)
14049 /* Glyph is at least partially visible. */
14050 ++it->hpos;
14051 if (x < it->first_visible_x)
14052 it->glyph_row->x = x - it->first_visible_x;
14054 else
14056 /* Glyph is off the left margin of the display area.
14057 Should not happen. */
14058 abort ();
14061 row->ascent = max (row->ascent, it->max_ascent);
14062 row->height = max (row->height, it->max_ascent + it->max_descent);
14063 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14064 row->phys_height = max (row->phys_height,
14065 it->max_phys_ascent + it->max_phys_descent);
14066 x += glyph->pixel_width;
14067 ++i;
14070 /* Stop if max_x reached. */
14071 if (i < nglyphs)
14072 break;
14074 /* Stop at line ends. */
14075 if (ITERATOR_AT_END_OF_LINE_P (it))
14077 it->continuation_lines_width = 0;
14078 break;
14081 set_iterator_to_next (it, 1);
14083 /* Stop if truncating at the right edge. */
14084 if (it->truncate_lines_p
14085 && it->current_x >= it->last_visible_x)
14087 /* Add truncation mark, but don't do it if the line is
14088 truncated at a padding space. */
14089 if (IT_CHARPOS (*it) < it->string_nchars)
14091 if (!FRAME_WINDOW_P (it->f))
14093 int i, n;
14095 if (it->current_x > it->last_visible_x)
14097 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14098 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14099 break;
14100 for (n = row->used[TEXT_AREA]; i < n; ++i)
14102 row->used[TEXT_AREA] = i;
14103 produce_special_glyphs (it, IT_TRUNCATION);
14106 produce_special_glyphs (it, IT_TRUNCATION);
14108 it->glyph_row->truncated_on_right_p = 1;
14110 break;
14114 /* Maybe insert a truncation at the left. */
14115 if (it->first_visible_x
14116 && IT_CHARPOS (*it) > 0)
14118 if (!FRAME_WINDOW_P (it->f))
14119 insert_left_trunc_glyphs (it);
14120 it->glyph_row->truncated_on_left_p = 1;
14123 it->face_id = saved_face_id;
14125 /* Value is number of columns displayed. */
14126 return it->hpos - hpos_at_start;
14131 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14132 appears as an element of LIST or as the car of an element of LIST.
14133 If PROPVAL is a list, compare each element against LIST in that
14134 way, and return 1 if any element of PROPVAL is found in LIST.
14135 Otherwise return 0. This function cannot quit. */
14138 invisible_p (propval, list)
14139 register Lisp_Object propval;
14140 Lisp_Object list;
14142 register Lisp_Object tail, proptail;
14144 for (tail = list; CONSP (tail); tail = XCDR (tail))
14146 register Lisp_Object tem;
14147 tem = XCAR (tail);
14148 if (EQ (propval, tem))
14149 return 1;
14150 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14151 return 1;
14154 if (CONSP (propval))
14156 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14158 Lisp_Object propelt;
14159 propelt = XCAR (proptail);
14160 for (tail = list; CONSP (tail); tail = XCDR (tail))
14162 register Lisp_Object tem;
14163 tem = XCAR (tail);
14164 if (EQ (propelt, tem))
14165 return 1;
14166 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14167 return 1;
14172 return 0;
14176 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14177 the cdr of that element is non-nil. If PROPVAL is a list, check
14178 each element of PROPVAL in that way, and the first time some
14179 element is found, return 1 if the cdr of that element is non-nil.
14180 Otherwise return 0. This function cannot quit. */
14183 invisible_ellipsis_p (propval, list)
14184 register Lisp_Object propval;
14185 Lisp_Object list;
14187 register Lisp_Object tail, proptail;
14189 for (tail = list; CONSP (tail); tail = XCDR (tail))
14191 register Lisp_Object tem;
14192 tem = XCAR (tail);
14193 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14194 return ! NILP (XCDR (tem));
14197 if (CONSP (propval))
14198 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14200 Lisp_Object propelt;
14201 propelt = XCAR (proptail);
14202 for (tail = list; CONSP (tail); tail = XCDR (tail))
14204 register Lisp_Object tem;
14205 tem = XCAR (tail);
14206 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14207 return ! NILP (XCDR (tem));
14211 return 0;
14216 /***********************************************************************
14217 Initialization
14218 ***********************************************************************/
14220 void
14221 syms_of_xdisp ()
14223 Vwith_echo_area_save_vector = Qnil;
14224 staticpro (&Vwith_echo_area_save_vector);
14226 Vmessage_stack = Qnil;
14227 staticpro (&Vmessage_stack);
14229 Qinhibit_redisplay = intern ("inhibit-redisplay");
14230 staticpro (&Qinhibit_redisplay);
14232 #if GLYPH_DEBUG
14233 defsubr (&Sdump_glyph_matrix);
14234 defsubr (&Sdump_glyph_row);
14235 defsubr (&Sdump_tool_bar_row);
14236 defsubr (&Strace_redisplay_toggle);
14237 defsubr (&Strace_to_stderr);
14238 #endif
14239 #ifdef HAVE_WINDOW_SYSTEM
14240 defsubr (&Stool_bar_lines_needed);
14241 #endif
14243 staticpro (&Qmenu_bar_update_hook);
14244 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14246 staticpro (&Qoverriding_terminal_local_map);
14247 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14249 staticpro (&Qoverriding_local_map);
14250 Qoverriding_local_map = intern ("overriding-local-map");
14252 staticpro (&Qwindow_scroll_functions);
14253 Qwindow_scroll_functions = intern ("window-scroll-functions");
14255 staticpro (&Qredisplay_end_trigger_functions);
14256 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14258 staticpro (&Qinhibit_point_motion_hooks);
14259 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14261 QCdata = intern (":data");
14262 staticpro (&QCdata);
14263 Qdisplay = intern ("display");
14264 staticpro (&Qdisplay);
14265 Qspace_width = intern ("space-width");
14266 staticpro (&Qspace_width);
14267 Qraise = intern ("raise");
14268 staticpro (&Qraise);
14269 Qspace = intern ("space");
14270 staticpro (&Qspace);
14271 Qmargin = intern ("margin");
14272 staticpro (&Qmargin);
14273 Qleft_margin = intern ("left-margin");
14274 staticpro (&Qleft_margin);
14275 Qright_margin = intern ("right-margin");
14276 staticpro (&Qright_margin);
14277 Qalign_to = intern ("align-to");
14278 staticpro (&Qalign_to);
14279 QCalign_to = intern (":align-to");
14280 staticpro (&QCalign_to);
14281 Qrelative_width = intern ("relative-width");
14282 staticpro (&Qrelative_width);
14283 QCrelative_width = intern (":relative-width");
14284 staticpro (&QCrelative_width);
14285 QCrelative_height = intern (":relative-height");
14286 staticpro (&QCrelative_height);
14287 QCeval = intern (":eval");
14288 staticpro (&QCeval);
14289 Qwhen = intern ("when");
14290 staticpro (&Qwhen);
14291 QCfile = intern (":file");
14292 staticpro (&QCfile);
14293 Qfontified = intern ("fontified");
14294 staticpro (&Qfontified);
14295 Qfontification_functions = intern ("fontification-functions");
14296 staticpro (&Qfontification_functions);
14297 Qtrailing_whitespace = intern ("trailing-whitespace");
14298 staticpro (&Qtrailing_whitespace);
14299 Qimage = intern ("image");
14300 staticpro (&Qimage);
14301 Qmessage_truncate_lines = intern ("message-truncate-lines");
14302 staticpro (&Qmessage_truncate_lines);
14303 Qgrow_only = intern ("grow-only");
14304 staticpro (&Qgrow_only);
14305 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14306 staticpro (&Qinhibit_menubar_update);
14307 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14308 staticpro (&Qinhibit_eval_during_redisplay);
14310 last_arrow_position = Qnil;
14311 last_arrow_string = Qnil;
14312 staticpro (&last_arrow_position);
14313 staticpro (&last_arrow_string);
14315 echo_buffer[0] = echo_buffer[1] = Qnil;
14316 staticpro (&echo_buffer[0]);
14317 staticpro (&echo_buffer[1]);
14319 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14320 staticpro (&echo_area_buffer[0]);
14321 staticpro (&echo_area_buffer[1]);
14323 Vmessages_buffer_name = build_string ("*Messages*");
14324 staticpro (&Vmessages_buffer_name);
14326 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14327 "Non-nil means highlight trailing whitespace.\n\
14328 The face used for trailing whitespace is `trailing-whitespace'.");
14329 Vshow_trailing_whitespace = Qnil;
14331 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14332 "Non-nil means don't actually do any redisplay.\n\
14333 This is used for internal purposes.");
14334 Vinhibit_redisplay = Qnil;
14336 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14337 "String (or mode line construct) included (normally) in `mode-line-format'.");
14338 Vglobal_mode_string = Qnil;
14340 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14341 "Marker for where to display an arrow on top of the buffer text.\n\
14342 This must be the beginning of a line in order to work.\n\
14343 See also `overlay-arrow-string'.");
14344 Voverlay_arrow_position = Qnil;
14346 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14347 "String to display as an arrow. See also `overlay-arrow-position'.");
14348 Voverlay_arrow_string = Qnil;
14350 DEFVAR_INT ("scroll-step", &scroll_step,
14351 "*The number of lines to try scrolling a window by when point moves out.\n\
14352 If that fails to bring point back on frame, point is centered instead.\n\
14353 If this is zero, point is always centered after it moves off frame.\n\
14354 If you want scrolling to always be a line at a time, you should set\n\
14355 `scroll-conservatively' to a large value rather than set this to 1.");
14357 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14358 "*Scroll up to this many lines, to bring point back on screen.\n\
14359 A value of zero means to scroll the text to center point vertically\n\
14360 in the window.");
14361 scroll_conservatively = 0;
14363 DEFVAR_INT ("scroll-margin", &scroll_margin,
14364 "*Number of lines of margin at the top and bottom of a window.\n\
14365 Recenter the window whenever point gets within this many lines\n\
14366 of the top or bottom of the window.");
14367 scroll_margin = 0;
14369 #if GLYPH_DEBUG
14370 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14371 #endif
14373 DEFVAR_BOOL ("truncate-partial-width-windows",
14374 &truncate_partial_width_windows,
14375 "*Non-nil means truncate lines in all windows less than full frame wide.");
14376 truncate_partial_width_windows = 1;
14378 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14379 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14380 Any other value means to use the appropriate face, `mode-line',\n\
14381 `header-line', or `menu' respectively.\n\
14383 This variable is deprecated; please change the above faces instead.");
14384 mode_line_inverse_video = 1;
14386 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14387 "*Maximum buffer size for which line number should be displayed.\n\
14388 If the buffer is bigger than this, the line number does not appear\n\
14389 in the mode line. A value of nil means no limit.");
14390 Vline_number_display_limit = Qnil;
14392 DEFVAR_INT ("line-number-display-limit-width",
14393 &line_number_display_limit_width,
14394 "*Maximum line width (in characters) for line number display.\n\
14395 If the average length of the lines near point is bigger than this, then the\n\
14396 line number may be omitted from the mode line.");
14397 line_number_display_limit_width = 200;
14399 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14400 "*Non-nil means highlight region even in nonselected windows.");
14401 highlight_nonselected_windows = 0;
14403 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14404 "Non-nil if more than one frame is visible on this display.\n\
14405 Minibuffer-only frames don't count, but iconified frames do.\n\
14406 This variable is not guaranteed to be accurate except while processing\n\
14407 `frame-title-format' and `icon-title-format'.");
14409 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14410 "Template for displaying the title bar of visible frames.\n\
14411 \(Assuming the window manager supports this feature.)\n\
14412 This variable has the same structure as `mode-line-format' (which see),\n\
14413 and is used only on frames for which no explicit name has been set\n\
14414 \(see `modify-frame-parameters').");
14415 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14416 "Template for displaying the title bar of an iconified frame.\n\
14417 \(Assuming the window manager supports this feature.)\n\
14418 This variable has the same structure as `mode-line-format' (which see),\n\
14419 and is used only on frames for which no explicit name has been set\n\
14420 \(see `modify-frame-parameters').");
14421 Vicon_title_format
14422 = Vframe_title_format
14423 = Fcons (intern ("multiple-frames"),
14424 Fcons (build_string ("%b"),
14425 Fcons (Fcons (build_string (""),
14426 Fcons (intern ("invocation-name"),
14427 Fcons (build_string ("@"),
14428 Fcons (intern ("system-name"),
14429 Qnil)))),
14430 Qnil)));
14432 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14433 "Maximum number of lines to keep in the message log buffer.\n\
14434 If nil, disable message logging. If t, log messages but don't truncate\n\
14435 the buffer when it becomes large.");
14436 Vmessage_log_max = make_number (50);
14438 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14439 "Functions called before redisplay, if window sizes have changed.\n\
14440 The value should be a list of functions that take one argument.\n\
14441 Just before redisplay, for each frame, if any of its windows have changed\n\
14442 size since the last redisplay, or have been split or deleted,\n\
14443 all the functions in the list are called, with the frame as argument.");
14444 Vwindow_size_change_functions = Qnil;
14446 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14447 "List of Functions to call before redisplaying a window with scrolling.\n\
14448 Each function is called with two arguments, the window\n\
14449 and its new display-start position. Note that the value of `window-end'\n\
14450 is not valid when these functions are called.");
14451 Vwindow_scroll_functions = Qnil;
14453 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14454 "*Non-nil means automatically resize tool-bars.\n\
14455 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14456 It decreases a tool-bar's height when it would display blank lines\n\
14457 otherwise.");
14458 auto_resize_tool_bars_p = 1;
14460 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14461 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14462 auto_raise_tool_bar_buttons_p = 1;
14464 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14465 "*Margin around tool-bar buttons in pixels.\n\
14466 If an integer, use that for both horizontal and vertical margins.\n\
14467 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14468 HORZ specifying the horizontal margin, and VERT specifying the\n\
14469 vertical margin.");
14470 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14472 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14473 "Relief thickness of tool-bar buttons.");
14474 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14476 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14477 "List of functions to call to fontify regions of text.\n\
14478 Each function is called with one argument POS. Functions must\n\
14479 fontify a region starting at POS in the current buffer, and give\n\
14480 fontified regions the property `fontified'.\n\
14481 This variable automatically becomes buffer-local when set.");
14482 Vfontification_functions = Qnil;
14483 Fmake_variable_buffer_local (Qfontification_functions);
14485 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14486 &unibyte_display_via_language_environment,
14487 "*Non-nil means display unibyte text according to language environment.\n\
14488 Specifically this means that unibyte non-ASCII characters\n\
14489 are displayed by converting them to the equivalent multibyte characters\n\
14490 according to the current language environment. As a result, they are\n\
14491 displayed according to the current fontset.");
14492 unibyte_display_via_language_environment = 0;
14494 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14495 "*Maximum height for resizing mini-windows.\n\
14496 If a float, it specifies a fraction of the mini-window frame's height.\n\
14497 If an integer, it specifies a number of lines.");
14498 Vmax_mini_window_height = make_float (0.25);
14500 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14501 "*How to resize mini-windows.\n\
14502 A value of nil means don't automatically resize mini-windows.\n\
14503 A value of t means resize them to fit the text displayed in them.\n\
14504 A value of `grow-only', the default, means let mini-windows grow\n\
14505 only, until their display becomes empty, at which point the windows\n\
14506 go back to their normal size.");
14507 Vresize_mini_windows = Qgrow_only;
14509 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14510 &cursor_in_non_selected_windows,
14511 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14512 Nil means don't display a cursor there.");
14513 cursor_in_non_selected_windows = 1;
14515 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14516 "*Non-nil means scroll the display automatically to make point visible.");
14517 automatic_hscrolling_p = 1;
14519 DEFVAR_LISP ("image-types", &Vimage_types,
14520 "List of supported image types.\n\
14521 Each element of the list is a symbol for a supported image type.");
14522 Vimage_types = Qnil;
14524 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14525 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14526 Bind this around calls to `message' to let it take effect.");
14527 message_truncate_lines = 0;
14529 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14530 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14531 Can be used to update submenus whose contents should vary.");
14532 Vmenu_bar_update_hook = Qnil;
14534 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14535 "Non-nil means don't update menu bars. Internal use only.");
14536 inhibit_menubar_update = 0;
14538 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14539 "Non-nil means don't eval Lisp during redisplay.");
14540 inhibit_eval_during_redisplay = 0;
14544 /* Initialize this module when Emacs starts. */
14546 void
14547 init_xdisp ()
14549 Lisp_Object root_window;
14550 struct window *mini_w;
14552 current_header_line_height = current_mode_line_height = -1;
14554 CHARPOS (this_line_start_pos) = 0;
14556 mini_w = XWINDOW (minibuf_window);
14557 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14559 if (!noninteractive)
14561 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14562 int i;
14564 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14565 set_window_height (root_window,
14566 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14568 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14569 set_window_height (minibuf_window, 1, 0);
14571 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14572 mini_w->width = make_number (FRAME_WIDTH (f));
14574 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14575 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14576 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14578 /* The default ellipsis glyphs `...'. */
14579 for (i = 0; i < 3; ++i)
14580 default_invis_vector[i] = make_number ('.');
14583 #ifdef HAVE_WINDOW_SYSTEM
14585 /* Allocate the buffer for frame titles. */
14586 int size = 100;
14587 frame_title_buf = (char *) xmalloc (size);
14588 frame_title_buf_end = frame_title_buf + size;
14589 frame_title_ptr = NULL;
14591 #endif /* HAVE_WINDOW_SYSTEM */
14593 help_echo_showing_p = 0;