(archive-unixdate): Corrected the date field string.
[emacs.git] / src / xdisp.c
blob009074ede9dc53ac2fb0c3cdb1c989be16d03c0b
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "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 MAC_OS
198 #include "macterm.h"
199 #endif
201 #define INFINITY 10000000
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
204 || defined (USE_GTK)
205 extern void set_frame_menubar P_ ((struct frame *f, int, int));
206 extern int pending_menu_activation;
207 #endif
209 extern int interrupt_input;
210 extern int command_loop_level;
212 extern int minibuffer_auto_raise;
213 extern Lisp_Object Vminibuffer_list;
215 extern Lisp_Object Qface;
216 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
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, QCpropertize;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229 Lisp_Object Qinhibit_eval_during_redisplay;
230 Lisp_Object Qbuffer_position, Qposition, Qobject;
232 /* Cursor shapes */
233 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
235 Lisp_Object Qrisky_local_variable;
237 /* Holds the list (error). */
238 Lisp_Object list_of_error;
240 /* Functions called to fontify regions of text. */
242 Lisp_Object Vfontification_functions;
243 Lisp_Object Qfontification_functions;
245 /* Non-zero means draw tool bar buttons raised when the mouse moves
246 over them. */
248 int auto_raise_tool_bar_buttons_p;
250 /* Margin around tool bar buttons in pixels. */
252 Lisp_Object Vtool_bar_button_margin;
254 /* Thickness of shadow to draw around tool bar buttons. */
256 EMACS_INT tool_bar_button_relief;
258 /* Non-zero means automatically resize tool-bars so that all tool-bar
259 items are visible, and no blank lines remain. */
261 int auto_resize_tool_bars_p;
263 /* Non-nil means don't actually do any redisplay. */
265 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
267 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
269 int inhibit_eval_during_redisplay;
271 /* Names of text properties relevant for redisplay. */
273 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
274 extern Lisp_Object Qface, Qinvisible, Qwidth;
276 /* Symbols used in text property values. */
278 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
279 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
280 Lisp_Object Qmargin;
281 extern Lisp_Object Qheight;
283 /* Non-nil means highlight trailing whitespace. */
285 Lisp_Object Vshow_trailing_whitespace;
287 /* Name of the face used to highlight trailing whitespace. */
289 Lisp_Object Qtrailing_whitespace;
291 /* The symbol `image' which is the car of the lists used to represent
292 images in Lisp. */
294 Lisp_Object Qimage;
296 /* Non-zero means print newline to stdout before next mini-buffer
297 message. */
299 int noninteractive_need_newline;
301 /* Non-zero means print newline to message log before next message. */
303 static int message_log_need_newline;
305 /* Three markers that message_dolog uses.
306 It could allocate them itself, but that causes trouble
307 in handling memory-full errors. */
308 static Lisp_Object message_dolog_marker1;
309 static Lisp_Object message_dolog_marker2;
310 static Lisp_Object message_dolog_marker3;
312 /* The buffer position of the first character appearing entirely or
313 partially on the line of the selected window which contains the
314 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
315 redisplay optimization in redisplay_internal. */
317 static struct text_pos this_line_start_pos;
319 /* Number of characters past the end of the line above, including the
320 terminating newline. */
322 static struct text_pos this_line_end_pos;
324 /* The vertical positions and the height of this line. */
326 static int this_line_vpos;
327 static int this_line_y;
328 static int this_line_pixel_height;
330 /* X position at which this display line starts. Usually zero;
331 negative if first character is partially visible. */
333 static int this_line_start_x;
335 /* Buffer that this_line_.* variables are referring to. */
337 static struct buffer *this_line_buffer;
339 /* Nonzero means truncate lines in all windows less wide than the
340 frame. */
342 int truncate_partial_width_windows;
344 /* A flag to control how to display unibyte 8-bit character. */
346 int unibyte_display_via_language_environment;
348 /* Nonzero means we have more than one non-mini-buffer-only frame.
349 Not guaranteed to be accurate except while parsing
350 frame-title-format. */
352 int multiple_frames;
354 Lisp_Object Vglobal_mode_string;
356 /* Marker for where to display an arrow on top of the buffer text. */
358 Lisp_Object Voverlay_arrow_position;
360 /* String to display for the arrow. Only used on terminal frames. */
362 Lisp_Object Voverlay_arrow_string;
364 /* Values of those variables at last redisplay. However, if
365 Voverlay_arrow_position is a marker, last_arrow_position is its
366 numerical position. */
368 static Lisp_Object last_arrow_position, last_arrow_string;
370 /* Like mode-line-format, but for the title bar on a visible frame. */
372 Lisp_Object Vframe_title_format;
374 /* Like mode-line-format, but for the title bar on an iconified frame. */
376 Lisp_Object Vicon_title_format;
378 /* List of functions to call when a window's size changes. These
379 functions get one arg, a frame on which one or more windows' sizes
380 have changed. */
382 static Lisp_Object Vwindow_size_change_functions;
384 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
386 /* Nonzero if overlay arrow has been displayed once in this window. */
388 static int overlay_arrow_seen;
390 /* Nonzero means highlight the region even in nonselected windows. */
392 int highlight_nonselected_windows;
394 /* If cursor motion alone moves point off frame, try scrolling this
395 many lines up or down if that will bring it back. */
397 static EMACS_INT scroll_step;
399 /* Nonzero means scroll just far enough to bring point back on the
400 screen, when appropriate. */
402 static EMACS_INT scroll_conservatively;
404 /* Recenter the window whenever point gets within this many lines of
405 the top or bottom of the window. This value is translated into a
406 pixel value by multiplying it with CANON_Y_UNIT, which means that
407 there is really a fixed pixel height scroll margin. */
409 EMACS_INT scroll_margin;
411 /* Number of windows showing the buffer of the selected window (or
412 another buffer with the same base buffer). keyboard.c refers to
413 this. */
415 int buffer_shared;
417 /* Vector containing glyphs for an ellipsis `...'. */
419 static Lisp_Object default_invis_vector[3];
421 /* Zero means display the mode-line/header-line/menu-bar in the default face
422 (this slightly odd definition is for compatibility with previous versions
423 of emacs), non-zero means display them using their respective faces.
425 This variable is deprecated. */
427 int mode_line_inverse_video;
429 /* Prompt to display in front of the mini-buffer contents. */
431 Lisp_Object minibuf_prompt;
433 /* Width of current mini-buffer prompt. Only set after display_line
434 of the line that contains the prompt. */
436 int minibuf_prompt_width;
438 /* This is the window where the echo area message was displayed. It
439 is always a mini-buffer window, but it may not be the same window
440 currently active as a mini-buffer. */
442 Lisp_Object echo_area_window;
444 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
445 pushes the current message and the value of
446 message_enable_multibyte on the stack, the function restore_message
447 pops the stack and displays MESSAGE again. */
449 Lisp_Object Vmessage_stack;
451 /* Nonzero means multibyte characters were enabled when the echo area
452 message was specified. */
454 int message_enable_multibyte;
456 /* Nonzero if we should redraw the mode lines on the next redisplay. */
458 int update_mode_lines;
460 /* Nonzero if window sizes or contents have changed since last
461 redisplay that finished. */
463 int windows_or_buffers_changed;
465 /* Nonzero means a frame's cursor type has been changed. */
467 int cursor_type_changed;
469 /* Nonzero after display_mode_line if %l was used and it displayed a
470 line number. */
472 int line_number_displayed;
474 /* Maximum buffer size for which to display line numbers. */
476 Lisp_Object Vline_number_display_limit;
478 /* Line width to consider when repositioning for line number display. */
480 static EMACS_INT line_number_display_limit_width;
482 /* Number of lines to keep in the message log buffer. t means
483 infinite. nil means don't log at all. */
485 Lisp_Object Vmessage_log_max;
487 /* The name of the *Messages* buffer, a string. */
489 static Lisp_Object Vmessages_buffer_name;
491 /* Current, index 0, and last displayed echo area message. Either
492 buffers from echo_buffers, or nil to indicate no message. */
494 Lisp_Object echo_area_buffer[2];
496 /* The buffers referenced from echo_area_buffer. */
498 static Lisp_Object echo_buffer[2];
500 /* A vector saved used in with_area_buffer to reduce consing. */
502 static Lisp_Object Vwith_echo_area_save_vector;
504 /* Non-zero means display_echo_area should display the last echo area
505 message again. Set by redisplay_preserve_echo_area. */
507 static int display_last_displayed_message_p;
509 /* Nonzero if echo area is being used by print; zero if being used by
510 message. */
512 int message_buf_print;
514 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
516 Lisp_Object Qinhibit_menubar_update;
517 int inhibit_menubar_update;
519 /* Maximum height for resizing mini-windows. Either a float
520 specifying a fraction of the available height, or an integer
521 specifying a number of lines. */
523 Lisp_Object Vmax_mini_window_height;
525 /* Non-zero means messages should be displayed with truncated
526 lines instead of being continued. */
528 int message_truncate_lines;
529 Lisp_Object Qmessage_truncate_lines;
531 /* Set to 1 in clear_message to make redisplay_internal aware
532 of an emptied echo area. */
534 static int message_cleared_p;
536 /* Non-zero means we want a hollow cursor in windows that are not
537 selected. Zero means there's no cursor in such windows. */
539 Lisp_Object Vcursor_in_non_selected_windows;
540 Lisp_Object Qcursor_in_non_selected_windows;
542 /* How to blink the default frame cursor off. */
543 Lisp_Object Vblink_cursor_alist;
545 /* A scratch glyph row with contents used for generating truncation
546 glyphs. Also used in direct_output_for_insert. */
548 #define MAX_SCRATCH_GLYPHS 100
549 struct glyph_row scratch_glyph_row;
550 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
552 /* Ascent and height of the last line processed by move_it_to. */
554 static int last_max_ascent, last_height;
556 /* Non-zero if there's a help-echo in the echo area. */
558 int help_echo_showing_p;
560 /* If >= 0, computed, exact values of mode-line and header-line height
561 to use in the macros CURRENT_MODE_LINE_HEIGHT and
562 CURRENT_HEADER_LINE_HEIGHT. */
564 int current_mode_line_height, current_header_line_height;
566 /* The maximum distance to look ahead for text properties. Values
567 that are too small let us call compute_char_face and similar
568 functions too often which is expensive. Values that are too large
569 let us call compute_char_face and alike too often because we
570 might not be interested in text properties that far away. */
572 #define TEXT_PROP_DISTANCE_LIMIT 100
574 #if GLYPH_DEBUG
576 /* Variables to turn off display optimizations from Lisp. */
578 int inhibit_try_window_id, inhibit_try_window_reusing;
579 int inhibit_try_cursor_movement;
581 /* Non-zero means print traces of redisplay if compiled with
582 GLYPH_DEBUG != 0. */
584 int trace_redisplay_p;
586 #endif /* GLYPH_DEBUG */
588 #ifdef DEBUG_TRACE_MOVE
589 /* Non-zero means trace with TRACE_MOVE to stderr. */
590 int trace_move;
592 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
593 #else
594 #define TRACE_MOVE(x) (void) 0
595 #endif
597 /* Non-zero means automatically scroll windows horizontally to make
598 point visible. */
600 int automatic_hscrolling_p;
602 /* How close to the margin can point get before the window is scrolled
603 horizontally. */
604 EMACS_INT hscroll_margin;
606 /* How much to scroll horizontally when point is inside the above margin. */
607 Lisp_Object Vhscroll_step;
609 /* A list of symbols, one for each supported image type. */
611 Lisp_Object Vimage_types;
613 /* The variable `resize-mini-windows'. If nil, don't resize
614 mini-windows. If t, always resize them to fit the text they
615 display. If `grow-only', let mini-windows grow only until they
616 become empty. */
618 Lisp_Object Vresize_mini_windows;
620 /* Buffer being redisplayed -- for redisplay_window_error. */
622 struct buffer *displayed_buffer;
624 /* Value returned from text property handlers (see below). */
626 enum prop_handled
628 HANDLED_NORMALLY,
629 HANDLED_RECOMPUTE_PROPS,
630 HANDLED_OVERLAY_STRING_CONSUMED,
631 HANDLED_RETURN
634 /* A description of text properties that redisplay is interested
635 in. */
637 struct props
639 /* The name of the property. */
640 Lisp_Object *name;
642 /* A unique index for the property. */
643 enum prop_idx idx;
645 /* A handler function called to set up iterator IT from the property
646 at IT's current position. Value is used to steer handle_stop. */
647 enum prop_handled (*handler) P_ ((struct it *it));
650 static enum prop_handled handle_face_prop P_ ((struct it *));
651 static enum prop_handled handle_invisible_prop P_ ((struct it *));
652 static enum prop_handled handle_display_prop P_ ((struct it *));
653 static enum prop_handled handle_composition_prop P_ ((struct it *));
654 static enum prop_handled handle_overlay_change P_ ((struct it *));
655 static enum prop_handled handle_fontified_prop P_ ((struct it *));
657 /* Properties handled by iterators. */
659 static struct props it_props[] =
661 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
662 /* Handle `face' before `display' because some sub-properties of
663 `display' need to know the face. */
664 {&Qface, FACE_PROP_IDX, handle_face_prop},
665 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
666 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
667 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
668 {NULL, 0, NULL}
671 /* Value is the position described by X. If X is a marker, value is
672 the marker_position of X. Otherwise, value is X. */
674 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
676 /* Enumeration returned by some move_it_.* functions internally. */
678 enum move_it_result
680 /* Not used. Undefined value. */
681 MOVE_UNDEFINED,
683 /* Move ended at the requested buffer position or ZV. */
684 MOVE_POS_MATCH_OR_ZV,
686 /* Move ended at the requested X pixel position. */
687 MOVE_X_REACHED,
689 /* Move within a line ended at the end of a line that must be
690 continued. */
691 MOVE_LINE_CONTINUED,
693 /* Move within a line ended at the end of a line that would
694 be displayed truncated. */
695 MOVE_LINE_TRUNCATED,
697 /* Move within a line ended at a line end. */
698 MOVE_NEWLINE_OR_CR
701 /* This counter is used to clear the face cache every once in a while
702 in redisplay_internal. It is incremented for each redisplay.
703 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
704 cleared. */
706 #define CLEAR_FACE_CACHE_COUNT 500
707 static int clear_face_cache_count;
709 /* Record the previous terminal frame we displayed. */
711 static struct frame *previous_terminal_frame;
713 /* Non-zero while redisplay_internal is in progress. */
715 int redisplaying_p;
717 /* Non-zero means don't free realized faces. Bound while freeing
718 realized faces is dangerous because glyph matrices might still
719 reference them. */
721 int inhibit_free_realized_faces;
722 Lisp_Object Qinhibit_free_realized_faces;
725 /* Function prototypes. */
727 static void setup_for_ellipsis P_ ((struct it *));
728 static void mark_window_display_accurate_1 P_ ((struct window *, int));
729 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
730 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
731 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
732 static int redisplay_mode_lines P_ ((Lisp_Object, int));
733 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
735 #if 0
736 static int invisible_text_between_p P_ ((struct it *, int, int));
737 #endif
739 static int next_element_from_ellipsis P_ ((struct it *));
740 static void pint2str P_ ((char *, int, int));
741 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
742 struct text_pos));
743 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
744 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
745 static void store_frame_title_char P_ ((char));
746 static int store_frame_title P_ ((const unsigned char *, int, int));
747 static void x_consider_frame_title P_ ((Lisp_Object));
748 static void handle_stop P_ ((struct it *));
749 static int tool_bar_lines_needed P_ ((struct frame *));
750 static int single_display_prop_intangible_p P_ ((Lisp_Object));
751 static void ensure_echo_area_buffers P_ ((void));
752 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
753 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
754 static int with_echo_area_buffer P_ ((struct window *, int,
755 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
756 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
757 static void clear_garbaged_frames P_ ((void));
758 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
759 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
760 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
761 static int display_echo_area P_ ((struct window *));
762 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
763 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
764 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
765 static int string_char_and_length P_ ((const unsigned char *, int, int *));
766 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
767 struct text_pos));
768 static int compute_window_start_on_continuation_line P_ ((struct window *));
769 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
770 static void insert_left_trunc_glyphs P_ ((struct it *));
771 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
772 static void extend_face_to_end_of_line P_ ((struct it *));
773 static int append_space P_ ((struct it *, int));
774 static int make_cursor_line_fully_visible P_ ((struct window *));
775 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
776 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
777 static int trailing_whitespace_p P_ ((int));
778 static int message_log_check_duplicate P_ ((int, int, int, int));
779 static void push_it P_ ((struct it *));
780 static void pop_it P_ ((struct it *));
781 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
782 static void redisplay_internal P_ ((int));
783 static int echo_area_display P_ ((int));
784 static void redisplay_windows P_ ((Lisp_Object));
785 static void redisplay_window P_ ((Lisp_Object, int));
786 static Lisp_Object redisplay_window_error ();
787 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
788 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
789 static void update_menu_bar P_ ((struct frame *, int));
790 static int try_window_reusing_current_matrix P_ ((struct window *));
791 static int try_window_id P_ ((struct window *));
792 static int display_line P_ ((struct it *));
793 static int display_mode_lines P_ ((struct window *));
794 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
795 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
796 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
797 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
798 static void display_menu_bar P_ ((struct window *));
799 static int display_count_lines P_ ((int, int, int, int, int *));
800 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
801 int, int, struct it *, int, int, int, int));
802 static void compute_line_metrics P_ ((struct it *));
803 static void run_redisplay_end_trigger_hook P_ ((struct it *));
804 static int get_overlay_strings P_ ((struct it *, int));
805 static void next_overlay_string P_ ((struct it *));
806 static void reseat P_ ((struct it *, struct text_pos, int));
807 static void reseat_1 P_ ((struct it *, struct text_pos, int));
808 static void back_to_previous_visible_line_start P_ ((struct it *));
809 static void reseat_at_previous_visible_line_start P_ ((struct it *));
810 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
811 static int next_element_from_display_vector P_ ((struct it *));
812 static int next_element_from_string P_ ((struct it *));
813 static int next_element_from_c_string P_ ((struct it *));
814 static int next_element_from_buffer P_ ((struct it *));
815 static int next_element_from_composition P_ ((struct it *));
816 static int next_element_from_image P_ ((struct it *));
817 static int next_element_from_stretch P_ ((struct it *));
818 static void load_overlay_strings P_ ((struct it *, int));
819 static int init_from_display_pos P_ ((struct it *, struct window *,
820 struct display_pos *));
821 static void reseat_to_string P_ ((struct it *, unsigned char *,
822 Lisp_Object, int, int, int, int));
823 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
824 int, int, int));
825 void move_it_vertically_backward P_ ((struct it *, int));
826 static void init_to_row_start P_ ((struct it *, struct window *,
827 struct glyph_row *));
828 static int init_to_row_end P_ ((struct it *, struct window *,
829 struct glyph_row *));
830 static void back_to_previous_line_start P_ ((struct it *));
831 static int forward_to_next_line_start P_ ((struct it *, int *));
832 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
833 Lisp_Object, int));
834 static struct text_pos string_pos P_ ((int, Lisp_Object));
835 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
836 static int number_of_chars P_ ((unsigned char *, int));
837 static void compute_stop_pos P_ ((struct it *));
838 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
839 Lisp_Object));
840 static int face_before_or_after_it_pos P_ ((struct it *, int));
841 static int next_overlay_change P_ ((int));
842 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
843 Lisp_Object, struct text_pos *,
844 int));
845 static int underlying_face_id P_ ((struct it *));
846 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
847 struct window *));
849 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
850 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
852 #ifdef HAVE_WINDOW_SYSTEM
854 static void update_tool_bar P_ ((struct frame *, int));
855 static void build_desired_tool_bar_string P_ ((struct frame *f));
856 static int redisplay_tool_bar P_ ((struct frame *));
857 static void display_tool_bar_line P_ ((struct it *));
859 #endif /* HAVE_WINDOW_SYSTEM */
862 /***********************************************************************
863 Window display dimensions
864 ***********************************************************************/
866 /* Return the bottom boundary y-position for text lines in window W.
867 This is the first y position at which a line cannot start.
868 It is relative to the top of the window.
870 This is the height of W minus the height of a mode line, if any. */
872 INLINE int
873 window_text_bottom_y (w)
874 struct window *w;
876 struct frame *f = XFRAME (w->frame);
877 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
879 if (WINDOW_WANTS_MODELINE_P (w))
880 height -= CURRENT_MODE_LINE_HEIGHT (w);
881 return height;
885 /* Return the pixel width of display area AREA of window W. AREA < 0
886 means return the total width of W, not including fringes to
887 the left and right of the window. */
889 INLINE int
890 window_box_width (w, area)
891 struct window *w;
892 int area;
894 struct frame *f = XFRAME (w->frame);
895 int width = XFASTINT (w->width);
897 if (!w->pseudo_window_p)
899 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
901 if (area == TEXT_AREA)
903 if (INTEGERP (w->left_margin_width))
904 width -= XFASTINT (w->left_margin_width);
905 if (INTEGERP (w->right_margin_width))
906 width -= XFASTINT (w->right_margin_width);
908 else if (area == LEFT_MARGIN_AREA)
909 width = (INTEGERP (w->left_margin_width)
910 ? XFASTINT (w->left_margin_width) : 0);
911 else if (area == RIGHT_MARGIN_AREA)
912 width = (INTEGERP (w->right_margin_width)
913 ? XFASTINT (w->right_margin_width) : 0);
916 return width * CANON_X_UNIT (f);
920 /* Return the pixel height of the display area of window W, not
921 including mode lines of W, if any. */
923 INLINE int
924 window_box_height (w)
925 struct window *w;
927 struct frame *f = XFRAME (w->frame);
928 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
930 xassert (height >= 0);
932 /* Note: the code below that determines the mode-line/header-line
933 height is essentially the same as that contained in the macro
934 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
935 the appropriate glyph row has its `mode_line_p' flag set,
936 and if it doesn't, uses estimate_mode_line_height instead. */
938 if (WINDOW_WANTS_MODELINE_P (w))
940 struct glyph_row *ml_row
941 = (w->current_matrix && w->current_matrix->rows
942 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
943 : 0);
944 if (ml_row && ml_row->mode_line_p)
945 height -= ml_row->height;
946 else
947 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
950 if (WINDOW_WANTS_HEADER_LINE_P (w))
952 struct glyph_row *hl_row
953 = (w->current_matrix && w->current_matrix->rows
954 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
955 : 0);
956 if (hl_row && hl_row->mode_line_p)
957 height -= hl_row->height;
958 else
959 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
962 /* With a very small font and a mode-line that's taller than
963 default, we might end up with a negative height. */
964 return max (0, height);
968 /* Return the frame-relative coordinate of the left edge of display
969 area AREA of window W. AREA < 0 means return the left edge of the
970 whole window, to the right of the left fringe of W. */
972 INLINE int
973 window_box_left (w, area)
974 struct window *w;
975 int area;
977 struct frame *f = XFRAME (w->frame);
978 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
980 if (!w->pseudo_window_p)
982 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
983 + FRAME_LEFT_FRINGE_WIDTH (f));
985 if (area == TEXT_AREA)
986 x += window_box_width (w, LEFT_MARGIN_AREA);
987 else if (area == RIGHT_MARGIN_AREA)
988 x += (window_box_width (w, LEFT_MARGIN_AREA)
989 + window_box_width (w, TEXT_AREA));
992 return x;
996 /* Return the frame-relative coordinate of the right edge of display
997 area AREA of window W. AREA < 0 means return the left edge of the
998 whole window, to the left of the right fringe of W. */
1000 INLINE int
1001 window_box_right (w, area)
1002 struct window *w;
1003 int area;
1005 return window_box_left (w, area) + window_box_width (w, area);
1009 /* Get the bounding box of the display area AREA of window W, without
1010 mode lines, in frame-relative coordinates. AREA < 0 means the
1011 whole window, not including the left and right fringes of
1012 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1013 coordinates of the upper-left corner of the box. Return in
1014 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1016 INLINE void
1017 window_box (w, area, box_x, box_y, box_width, box_height)
1018 struct window *w;
1019 int area;
1020 int *box_x, *box_y, *box_width, *box_height;
1022 struct frame *f = XFRAME (w->frame);
1024 *box_width = window_box_width (w, area);
1025 *box_height = window_box_height (w);
1026 *box_x = window_box_left (w, area);
1027 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1028 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1029 if (WINDOW_WANTS_HEADER_LINE_P (w))
1030 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1034 /* Get the bounding box of the display area AREA of window W, without
1035 mode lines. AREA < 0 means the whole window, not including the
1036 left and right fringe of the window. Return in *TOP_LEFT_X
1037 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1038 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1039 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1040 box. */
1042 INLINE void
1043 window_box_edges (w, area, top_left_x, top_left_y,
1044 bottom_right_x, bottom_right_y)
1045 struct window *w;
1046 int area;
1047 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1049 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1050 bottom_right_y);
1051 *bottom_right_x += *top_left_x;
1052 *bottom_right_y += *top_left_y;
1057 /***********************************************************************
1058 Utilities
1059 ***********************************************************************/
1061 /* Return the bottom y-position of the line the iterator IT is in.
1062 This can modify IT's settings. */
1065 line_bottom_y (it)
1066 struct it *it;
1068 int line_height = it->max_ascent + it->max_descent;
1069 int line_top_y = it->current_y;
1071 if (line_height == 0)
1073 if (last_height)
1074 line_height = last_height;
1075 else if (IT_CHARPOS (*it) < ZV)
1077 move_it_by_lines (it, 1, 1);
1078 line_height = (it->max_ascent || it->max_descent
1079 ? it->max_ascent + it->max_descent
1080 : last_height);
1082 else
1084 struct glyph_row *row = it->glyph_row;
1086 /* Use the default character height. */
1087 it->glyph_row = NULL;
1088 it->what = IT_CHARACTER;
1089 it->c = ' ';
1090 it->len = 1;
1091 PRODUCE_GLYPHS (it);
1092 line_height = it->ascent + it->descent;
1093 it->glyph_row = row;
1097 return line_top_y + line_height;
1101 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1102 1 if POS is visible and the line containing POS is fully visible.
1103 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1104 and header-lines heights. */
1107 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1108 struct window *w;
1109 int charpos, *fully, exact_mode_line_heights_p;
1111 struct it it;
1112 struct text_pos top;
1113 int visible_p;
1114 struct buffer *old_buffer = NULL;
1116 if (XBUFFER (w->buffer) != current_buffer)
1118 old_buffer = current_buffer;
1119 set_buffer_internal_1 (XBUFFER (w->buffer));
1122 *fully = visible_p = 0;
1123 SET_TEXT_POS_FROM_MARKER (top, w->start);
1125 /* Compute exact mode line heights, if requested. */
1126 if (exact_mode_line_heights_p)
1128 if (WINDOW_WANTS_MODELINE_P (w))
1129 current_mode_line_height
1130 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1131 current_buffer->mode_line_format);
1133 if (WINDOW_WANTS_HEADER_LINE_P (w))
1134 current_header_line_height
1135 = display_mode_line (w, HEADER_LINE_FACE_ID,
1136 current_buffer->header_line_format);
1139 start_display (&it, w, top);
1140 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1141 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1143 /* Note that we may overshoot because of invisible text. */
1144 if (IT_CHARPOS (it) >= charpos)
1146 int top_y = it.current_y;
1147 int bottom_y = line_bottom_y (&it);
1148 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1150 if (top_y < window_top_y)
1151 visible_p = bottom_y > window_top_y;
1152 else if (top_y < it.last_visible_y)
1154 visible_p = 1;
1155 *fully = bottom_y <= it.last_visible_y;
1158 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1160 move_it_by_lines (&it, 1, 0);
1161 if (charpos < IT_CHARPOS (it))
1163 visible_p = 1;
1164 *fully = 0;
1168 if (old_buffer)
1169 set_buffer_internal_1 (old_buffer);
1171 current_header_line_height = current_mode_line_height = -1;
1172 return visible_p;
1176 /* Return the next character from STR which is MAXLEN bytes long.
1177 Return in *LEN the length of the character. This is like
1178 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1179 we find one, we return a `?', but with the length of the invalid
1180 character. */
1182 static INLINE int
1183 string_char_and_length (str, maxlen, len)
1184 const unsigned char *str;
1185 int maxlen, *len;
1187 int c;
1189 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1190 if (!CHAR_VALID_P (c, 1))
1191 /* We may not change the length here because other places in Emacs
1192 don't use this function, i.e. they silently accept invalid
1193 characters. */
1194 c = '?';
1196 return c;
1201 /* Given a position POS containing a valid character and byte position
1202 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1204 static struct text_pos
1205 string_pos_nchars_ahead (pos, string, nchars)
1206 struct text_pos pos;
1207 Lisp_Object string;
1208 int nchars;
1210 xassert (STRINGP (string) && nchars >= 0);
1212 if (STRING_MULTIBYTE (string))
1214 int rest = SBYTES (string) - BYTEPOS (pos);
1215 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1216 int len;
1218 while (nchars--)
1220 string_char_and_length (p, rest, &len);
1221 p += len, rest -= len;
1222 xassert (rest >= 0);
1223 CHARPOS (pos) += 1;
1224 BYTEPOS (pos) += len;
1227 else
1228 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1230 return pos;
1234 /* Value is the text position, i.e. character and byte position,
1235 for character position CHARPOS in STRING. */
1237 static INLINE struct text_pos
1238 string_pos (charpos, string)
1239 int charpos;
1240 Lisp_Object string;
1242 struct text_pos pos;
1243 xassert (STRINGP (string));
1244 xassert (charpos >= 0);
1245 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1246 return pos;
1250 /* Value is a text position, i.e. character and byte position, for
1251 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1252 means recognize multibyte characters. */
1254 static struct text_pos
1255 c_string_pos (charpos, s, multibyte_p)
1256 int charpos;
1257 unsigned char *s;
1258 int multibyte_p;
1260 struct text_pos pos;
1262 xassert (s != NULL);
1263 xassert (charpos >= 0);
1265 if (multibyte_p)
1267 int rest = strlen (s), len;
1269 SET_TEXT_POS (pos, 0, 0);
1270 while (charpos--)
1272 string_char_and_length (s, rest, &len);
1273 s += len, rest -= len;
1274 xassert (rest >= 0);
1275 CHARPOS (pos) += 1;
1276 BYTEPOS (pos) += len;
1279 else
1280 SET_TEXT_POS (pos, charpos, charpos);
1282 return pos;
1286 /* Value is the number of characters in C string S. MULTIBYTE_P
1287 non-zero means recognize multibyte characters. */
1289 static int
1290 number_of_chars (s, multibyte_p)
1291 unsigned char *s;
1292 int multibyte_p;
1294 int nchars;
1296 if (multibyte_p)
1298 int rest = strlen (s), len;
1299 unsigned char *p = (unsigned char *) s;
1301 for (nchars = 0; rest > 0; ++nchars)
1303 string_char_and_length (p, rest, &len);
1304 rest -= len, p += len;
1307 else
1308 nchars = strlen (s);
1310 return nchars;
1314 /* Compute byte position NEWPOS->bytepos corresponding to
1315 NEWPOS->charpos. POS is a known position in string STRING.
1316 NEWPOS->charpos must be >= POS.charpos. */
1318 static void
1319 compute_string_pos (newpos, pos, string)
1320 struct text_pos *newpos, pos;
1321 Lisp_Object string;
1323 xassert (STRINGP (string));
1324 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1326 if (STRING_MULTIBYTE (string))
1327 *newpos = string_pos_nchars_ahead (pos, string,
1328 CHARPOS (*newpos) - CHARPOS (pos));
1329 else
1330 BYTEPOS (*newpos) = CHARPOS (*newpos);
1335 /***********************************************************************
1336 Lisp form evaluation
1337 ***********************************************************************/
1339 /* Error handler for safe_eval and safe_call. */
1341 static Lisp_Object
1342 safe_eval_handler (arg)
1343 Lisp_Object arg;
1345 add_to_log ("Error during redisplay: %s", arg, Qnil);
1346 return Qnil;
1350 /* Evaluate SEXPR and return the result, or nil if something went
1351 wrong. Prevent redisplay during the evaluation. */
1353 Lisp_Object
1354 safe_eval (sexpr)
1355 Lisp_Object sexpr;
1357 Lisp_Object val;
1359 if (inhibit_eval_during_redisplay)
1360 val = Qnil;
1361 else
1363 int count = SPECPDL_INDEX ();
1364 struct gcpro gcpro1;
1366 GCPRO1 (sexpr);
1367 specbind (Qinhibit_redisplay, Qt);
1368 /* Use Qt to ensure debugger does not run,
1369 so there is no possibility of wanting to redisplay. */
1370 val = internal_condition_case_1 (Feval, sexpr, Qt,
1371 safe_eval_handler);
1372 UNGCPRO;
1373 val = unbind_to (count, val);
1376 return val;
1380 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1381 Return the result, or nil if something went wrong. Prevent
1382 redisplay during the evaluation. */
1384 Lisp_Object
1385 safe_call (nargs, args)
1386 int nargs;
1387 Lisp_Object *args;
1389 Lisp_Object val;
1391 if (inhibit_eval_during_redisplay)
1392 val = Qnil;
1393 else
1395 int count = SPECPDL_INDEX ();
1396 struct gcpro gcpro1;
1398 GCPRO1 (args[0]);
1399 gcpro1.nvars = nargs;
1400 specbind (Qinhibit_redisplay, Qt);
1401 /* Use Qt to ensure debugger does not run,
1402 so there is no possibility of wanting to redisplay. */
1403 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1404 safe_eval_handler);
1405 UNGCPRO;
1406 val = unbind_to (count, val);
1409 return val;
1413 /* Call function FN with one argument ARG.
1414 Return the result, or nil if something went wrong. */
1416 Lisp_Object
1417 safe_call1 (fn, arg)
1418 Lisp_Object fn, arg;
1420 Lisp_Object args[2];
1421 args[0] = fn;
1422 args[1] = arg;
1423 return safe_call (2, args);
1428 /***********************************************************************
1429 Debugging
1430 ***********************************************************************/
1432 #if 0
1434 /* Define CHECK_IT to perform sanity checks on iterators.
1435 This is for debugging. It is too slow to do unconditionally. */
1437 static void
1438 check_it (it)
1439 struct it *it;
1441 if (it->method == next_element_from_string)
1443 xassert (STRINGP (it->string));
1444 xassert (IT_STRING_CHARPOS (*it) >= 0);
1446 else if (it->method == next_element_from_buffer)
1448 /* Check that character and byte positions agree. */
1449 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1452 if (it->dpvec)
1453 xassert (it->current.dpvec_index >= 0);
1454 else
1455 xassert (it->current.dpvec_index < 0);
1458 #define CHECK_IT(IT) check_it ((IT))
1460 #else /* not 0 */
1462 #define CHECK_IT(IT) (void) 0
1464 #endif /* not 0 */
1467 #if GLYPH_DEBUG
1469 /* Check that the window end of window W is what we expect it
1470 to be---the last row in the current matrix displaying text. */
1472 static void
1473 check_window_end (w)
1474 struct window *w;
1476 if (!MINI_WINDOW_P (w)
1477 && !NILP (w->window_end_valid))
1479 struct glyph_row *row;
1480 xassert ((row = MATRIX_ROW (w->current_matrix,
1481 XFASTINT (w->window_end_vpos)),
1482 !row->enabled_p
1483 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1484 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1488 #define CHECK_WINDOW_END(W) check_window_end ((W))
1490 #else /* not GLYPH_DEBUG */
1492 #define CHECK_WINDOW_END(W) (void) 0
1494 #endif /* not GLYPH_DEBUG */
1498 /***********************************************************************
1499 Iterator initialization
1500 ***********************************************************************/
1502 /* Initialize IT for displaying current_buffer in window W, starting
1503 at character position CHARPOS. CHARPOS < 0 means that no buffer
1504 position is specified which is useful when the iterator is assigned
1505 a position later. BYTEPOS is the byte position corresponding to
1506 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1508 If ROW is not null, calls to produce_glyphs with IT as parameter
1509 will produce glyphs in that row.
1511 BASE_FACE_ID is the id of a base face to use. It must be one of
1512 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1513 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1514 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1516 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1517 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1518 will be initialized to use the corresponding mode line glyph row of
1519 the desired matrix of W. */
1521 void
1522 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1523 struct it *it;
1524 struct window *w;
1525 int charpos, bytepos;
1526 struct glyph_row *row;
1527 enum face_id base_face_id;
1529 int highlight_region_p;
1531 /* Some precondition checks. */
1532 xassert (w != NULL && it != NULL);
1533 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1534 && charpos <= ZV));
1536 /* If face attributes have been changed since the last redisplay,
1537 free realized faces now because they depend on face definitions
1538 that might have changed. Don't free faces while there might be
1539 desired matrices pending which reference these faces. */
1540 if (face_change_count && !inhibit_free_realized_faces)
1542 face_change_count = 0;
1543 free_all_realized_faces (Qnil);
1546 /* Use one of the mode line rows of W's desired matrix if
1547 appropriate. */
1548 if (row == NULL)
1550 if (base_face_id == MODE_LINE_FACE_ID
1551 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1552 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1553 else if (base_face_id == HEADER_LINE_FACE_ID)
1554 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1557 /* Clear IT. */
1558 bzero (it, sizeof *it);
1559 it->current.overlay_string_index = -1;
1560 it->current.dpvec_index = -1;
1561 it->base_face_id = base_face_id;
1563 /* The window in which we iterate over current_buffer: */
1564 XSETWINDOW (it->window, w);
1565 it->w = w;
1566 it->f = XFRAME (w->frame);
1568 /* Extra space between lines (on window systems only). */
1569 if (base_face_id == DEFAULT_FACE_ID
1570 && FRAME_WINDOW_P (it->f))
1572 if (NATNUMP (current_buffer->extra_line_spacing))
1573 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1574 else if (it->f->extra_line_spacing > 0)
1575 it->extra_line_spacing = it->f->extra_line_spacing;
1578 /* If realized faces have been removed, e.g. because of face
1579 attribute changes of named faces, recompute them. When running
1580 in batch mode, the face cache of Vterminal_frame is null. If
1581 we happen to get called, make a dummy face cache. */
1582 if (
1583 #ifndef WINDOWSNT
1584 noninteractive &&
1585 #endif
1586 FRAME_FACE_CACHE (it->f) == NULL)
1587 init_frame_faces (it->f);
1588 if (FRAME_FACE_CACHE (it->f)->used == 0)
1589 recompute_basic_faces (it->f);
1591 /* Current value of the `space-width', and 'height' properties. */
1592 it->space_width = Qnil;
1593 it->font_height = Qnil;
1595 /* Are control characters displayed as `^C'? */
1596 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1598 /* -1 means everything between a CR and the following line end
1599 is invisible. >0 means lines indented more than this value are
1600 invisible. */
1601 it->selective = (INTEGERP (current_buffer->selective_display)
1602 ? XFASTINT (current_buffer->selective_display)
1603 : (!NILP (current_buffer->selective_display)
1604 ? -1 : 0));
1605 it->selective_display_ellipsis_p
1606 = !NILP (current_buffer->selective_display_ellipses);
1608 /* Display table to use. */
1609 it->dp = window_display_table (w);
1611 /* Are multibyte characters enabled in current_buffer? */
1612 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1614 /* Non-zero if we should highlight the region. */
1615 highlight_region_p
1616 = (!NILP (Vtransient_mark_mode)
1617 && !NILP (current_buffer->mark_active)
1618 && XMARKER (current_buffer->mark)->buffer != 0);
1620 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1621 start and end of a visible region in window IT->w. Set both to
1622 -1 to indicate no region. */
1623 if (highlight_region_p
1624 /* Maybe highlight only in selected window. */
1625 && (/* Either show region everywhere. */
1626 highlight_nonselected_windows
1627 /* Or show region in the selected window. */
1628 || w == XWINDOW (selected_window)
1629 /* Or show the region if we are in the mini-buffer and W is
1630 the window the mini-buffer refers to. */
1631 || (MINI_WINDOW_P (XWINDOW (selected_window))
1632 && WINDOWP (minibuf_selected_window)
1633 && w == XWINDOW (minibuf_selected_window))))
1635 int charpos = marker_position (current_buffer->mark);
1636 it->region_beg_charpos = min (PT, charpos);
1637 it->region_end_charpos = max (PT, charpos);
1639 else
1640 it->region_beg_charpos = it->region_end_charpos = -1;
1642 /* Get the position at which the redisplay_end_trigger hook should
1643 be run, if it is to be run at all. */
1644 if (MARKERP (w->redisplay_end_trigger)
1645 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1646 it->redisplay_end_trigger_charpos
1647 = marker_position (w->redisplay_end_trigger);
1648 else if (INTEGERP (w->redisplay_end_trigger))
1649 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1651 /* Correct bogus values of tab_width. */
1652 it->tab_width = XINT (current_buffer->tab_width);
1653 if (it->tab_width <= 0 || it->tab_width > 1000)
1654 it->tab_width = 8;
1656 /* Are lines in the display truncated? */
1657 it->truncate_lines_p
1658 = (base_face_id != DEFAULT_FACE_ID
1659 || XINT (it->w->hscroll)
1660 || (truncate_partial_width_windows
1661 && !WINDOW_FULL_WIDTH_P (it->w))
1662 || !NILP (current_buffer->truncate_lines));
1664 /* Get dimensions of truncation and continuation glyphs. These are
1665 displayed as fringe bitmaps under X, so we don't need them for such
1666 frames. */
1667 if (!FRAME_WINDOW_P (it->f))
1669 if (it->truncate_lines_p)
1671 /* We will need the truncation glyph. */
1672 xassert (it->glyph_row == NULL);
1673 produce_special_glyphs (it, IT_TRUNCATION);
1674 it->truncation_pixel_width = it->pixel_width;
1676 else
1678 /* We will need the continuation glyph. */
1679 xassert (it->glyph_row == NULL);
1680 produce_special_glyphs (it, IT_CONTINUATION);
1681 it->continuation_pixel_width = it->pixel_width;
1684 /* Reset these values to zero because the produce_special_glyphs
1685 above has changed them. */
1686 it->pixel_width = it->ascent = it->descent = 0;
1687 it->phys_ascent = it->phys_descent = 0;
1690 /* Set this after getting the dimensions of truncation and
1691 continuation glyphs, so that we don't produce glyphs when calling
1692 produce_special_glyphs, above. */
1693 it->glyph_row = row;
1694 it->area = TEXT_AREA;
1696 /* Get the dimensions of the display area. The display area
1697 consists of the visible window area plus a horizontally scrolled
1698 part to the left of the window. All x-values are relative to the
1699 start of this total display area. */
1700 if (base_face_id != DEFAULT_FACE_ID)
1702 /* Mode lines, menu bar in terminal frames. */
1703 it->first_visible_x = 0;
1704 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1706 else
1708 it->first_visible_x
1709 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1710 it->last_visible_x = (it->first_visible_x
1711 + window_box_width (w, TEXT_AREA));
1713 /* If we truncate lines, leave room for the truncator glyph(s) at
1714 the right margin. Otherwise, leave room for the continuation
1715 glyph(s). Truncation and continuation glyphs are not inserted
1716 for window-based redisplay. */
1717 if (!FRAME_WINDOW_P (it->f))
1719 if (it->truncate_lines_p)
1720 it->last_visible_x -= it->truncation_pixel_width;
1721 else
1722 it->last_visible_x -= it->continuation_pixel_width;
1725 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1726 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1729 /* Leave room for a border glyph. */
1730 if (!FRAME_WINDOW_P (it->f)
1731 && !WINDOW_RIGHTMOST_P (it->w))
1732 it->last_visible_x -= 1;
1734 it->last_visible_y = window_text_bottom_y (w);
1736 /* For mode lines and alike, arrange for the first glyph having a
1737 left box line if the face specifies a box. */
1738 if (base_face_id != DEFAULT_FACE_ID)
1740 struct face *face;
1742 it->face_id = base_face_id;
1744 /* If we have a boxed mode line, make the first character appear
1745 with a left box line. */
1746 face = FACE_FROM_ID (it->f, base_face_id);
1747 if (face->box != FACE_NO_BOX)
1748 it->start_of_box_run_p = 1;
1751 /* If a buffer position was specified, set the iterator there,
1752 getting overlays and face properties from that position. */
1753 if (charpos >= BUF_BEG (current_buffer))
1755 it->end_charpos = ZV;
1756 it->face_id = -1;
1757 IT_CHARPOS (*it) = charpos;
1759 /* Compute byte position if not specified. */
1760 if (bytepos < charpos)
1761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1762 else
1763 IT_BYTEPOS (*it) = bytepos;
1765 /* Compute faces etc. */
1766 reseat (it, it->current.pos, 1);
1769 CHECK_IT (it);
1773 /* Initialize IT for the display of window W with window start POS. */
1775 void
1776 start_display (it, w, pos)
1777 struct it *it;
1778 struct window *w;
1779 struct text_pos pos;
1781 struct glyph_row *row;
1782 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1784 row = w->desired_matrix->rows + first_vpos;
1785 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1787 if (!it->truncate_lines_p)
1789 int start_at_line_beg_p;
1790 int first_y = it->current_y;
1792 /* If window start is not at a line start, skip forward to POS to
1793 get the correct continuation lines width. */
1794 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1795 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1796 if (!start_at_line_beg_p)
1798 int new_x;
1800 reseat_at_previous_visible_line_start (it);
1801 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1803 new_x = it->current_x + it->pixel_width;
1805 /* If lines are continued, this line may end in the middle
1806 of a multi-glyph character (e.g. a control character
1807 displayed as \003, or in the middle of an overlay
1808 string). In this case move_it_to above will not have
1809 taken us to the start of the continuation line but to the
1810 end of the continued line. */
1811 if (it->current_x > 0
1812 && !it->truncate_lines_p /* Lines are continued. */
1813 && (/* And glyph doesn't fit on the line. */
1814 new_x > it->last_visible_x
1815 /* Or it fits exactly and we're on a window
1816 system frame. */
1817 || (new_x == it->last_visible_x
1818 && FRAME_WINDOW_P (it->f))))
1820 if (it->current.dpvec_index >= 0
1821 || it->current.overlay_string_index >= 0)
1823 set_iterator_to_next (it, 1);
1824 move_it_in_display_line_to (it, -1, -1, 0);
1827 it->continuation_lines_width += it->current_x;
1830 /* We're starting a new display line, not affected by the
1831 height of the continued line, so clear the appropriate
1832 fields in the iterator structure. */
1833 it->max_ascent = it->max_descent = 0;
1834 it->max_phys_ascent = it->max_phys_descent = 0;
1836 it->current_y = first_y;
1837 it->vpos = 0;
1838 it->current_x = it->hpos = 0;
1842 #if 0 /* Don't assert the following because start_display is sometimes
1843 called intentionally with a window start that is not at a
1844 line start. Please leave this code in as a comment. */
1846 /* Window start should be on a line start, now. */
1847 xassert (it->continuation_lines_width
1848 || IT_CHARPOS (it) == BEGV
1849 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1850 #endif /* 0 */
1854 /* Return 1 if POS is a position in ellipses displayed for invisible
1855 text. W is the window we display, for text property lookup. */
1857 static int
1858 in_ellipses_for_invisible_text_p (pos, w)
1859 struct display_pos *pos;
1860 struct window *w;
1862 Lisp_Object prop, window;
1863 int ellipses_p = 0;
1864 int charpos = CHARPOS (pos->pos);
1866 /* If POS specifies a position in a display vector, this might
1867 be for an ellipsis displayed for invisible text. We won't
1868 get the iterator set up for delivering that ellipsis unless
1869 we make sure that it gets aware of the invisible text. */
1870 if (pos->dpvec_index >= 0
1871 && pos->overlay_string_index < 0
1872 && CHARPOS (pos->string_pos) < 0
1873 && charpos > BEGV
1874 && (XSETWINDOW (window, w),
1875 prop = Fget_char_property (make_number (charpos),
1876 Qinvisible, window),
1877 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1879 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1880 window);
1881 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1884 return ellipses_p;
1888 /* Initialize IT for stepping through current_buffer in window W,
1889 starting at position POS that includes overlay string and display
1890 vector/ control character translation position information. Value
1891 is zero if there are overlay strings with newlines at POS. */
1893 static int
1894 init_from_display_pos (it, w, pos)
1895 struct it *it;
1896 struct window *w;
1897 struct display_pos *pos;
1899 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1900 int i, overlay_strings_with_newlines = 0;
1902 /* If POS specifies a position in a display vector, this might
1903 be for an ellipsis displayed for invisible text. We won't
1904 get the iterator set up for delivering that ellipsis unless
1905 we make sure that it gets aware of the invisible text. */
1906 if (in_ellipses_for_invisible_text_p (pos, w))
1908 --charpos;
1909 bytepos = 0;
1912 /* Keep in mind: the call to reseat in init_iterator skips invisible
1913 text, so we might end up at a position different from POS. This
1914 is only a problem when POS is a row start after a newline and an
1915 overlay starts there with an after-string, and the overlay has an
1916 invisible property. Since we don't skip invisible text in
1917 display_line and elsewhere immediately after consuming the
1918 newline before the row start, such a POS will not be in a string,
1919 but the call to init_iterator below will move us to the
1920 after-string. */
1921 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1923 for (i = 0; i < it->n_overlay_strings; ++i)
1925 const char *s = SDATA (it->overlay_strings[i]);
1926 const char *e = s + SBYTES (it->overlay_strings[i]);
1928 while (s < e && *s != '\n')
1929 ++s;
1931 if (s < e)
1933 overlay_strings_with_newlines = 1;
1934 break;
1938 /* If position is within an overlay string, set up IT to the right
1939 overlay string. */
1940 if (pos->overlay_string_index >= 0)
1942 int relative_index;
1944 /* If the first overlay string happens to have a `display'
1945 property for an image, the iterator will be set up for that
1946 image, and we have to undo that setup first before we can
1947 correct the overlay string index. */
1948 if (it->method == next_element_from_image)
1949 pop_it (it);
1951 /* We already have the first chunk of overlay strings in
1952 IT->overlay_strings. Load more until the one for
1953 pos->overlay_string_index is in IT->overlay_strings. */
1954 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1956 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1957 it->current.overlay_string_index = 0;
1958 while (n--)
1960 load_overlay_strings (it, 0);
1961 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1965 it->current.overlay_string_index = pos->overlay_string_index;
1966 relative_index = (it->current.overlay_string_index
1967 % OVERLAY_STRING_CHUNK_SIZE);
1968 it->string = it->overlay_strings[relative_index];
1969 xassert (STRINGP (it->string));
1970 it->current.string_pos = pos->string_pos;
1971 it->method = next_element_from_string;
1974 #if 0 /* This is bogus because POS not having an overlay string
1975 position does not mean it's after the string. Example: A
1976 line starting with a before-string and initialization of IT
1977 to the previous row's end position. */
1978 else if (it->current.overlay_string_index >= 0)
1980 /* If POS says we're already after an overlay string ending at
1981 POS, make sure to pop the iterator because it will be in
1982 front of that overlay string. When POS is ZV, we've thereby
1983 also ``processed'' overlay strings at ZV. */
1984 while (it->sp)
1985 pop_it (it);
1986 it->current.overlay_string_index = -1;
1987 it->method = next_element_from_buffer;
1988 if (CHARPOS (pos->pos) == ZV)
1989 it->overlay_strings_at_end_processed_p = 1;
1991 #endif /* 0 */
1993 if (CHARPOS (pos->string_pos) >= 0)
1995 /* Recorded position is not in an overlay string, but in another
1996 string. This can only be a string from a `display' property.
1997 IT should already be filled with that string. */
1998 it->current.string_pos = pos->string_pos;
1999 xassert (STRINGP (it->string));
2002 /* Restore position in display vector translations, control
2003 character translations or ellipses. */
2004 if (pos->dpvec_index >= 0)
2006 if (it->dpvec == NULL)
2007 get_next_display_element (it);
2008 xassert (it->dpvec && it->current.dpvec_index == 0);
2009 it->current.dpvec_index = pos->dpvec_index;
2012 CHECK_IT (it);
2013 return !overlay_strings_with_newlines;
2017 /* Initialize IT for stepping through current_buffer in window W
2018 starting at ROW->start. */
2020 static void
2021 init_to_row_start (it, w, row)
2022 struct it *it;
2023 struct window *w;
2024 struct glyph_row *row;
2026 init_from_display_pos (it, w, &row->start);
2027 it->continuation_lines_width = row->continuation_lines_width;
2028 CHECK_IT (it);
2032 /* Initialize IT for stepping through current_buffer in window W
2033 starting in the line following ROW, i.e. starting at ROW->end.
2034 Value is zero if there are overlay strings with newlines at ROW's
2035 end position. */
2037 static int
2038 init_to_row_end (it, w, row)
2039 struct it *it;
2040 struct window *w;
2041 struct glyph_row *row;
2043 int success = 0;
2045 if (init_from_display_pos (it, w, &row->end))
2047 if (row->continued_p)
2048 it->continuation_lines_width
2049 = row->continuation_lines_width + row->pixel_width;
2050 CHECK_IT (it);
2051 success = 1;
2054 return success;
2060 /***********************************************************************
2061 Text properties
2062 ***********************************************************************/
2064 /* Called when IT reaches IT->stop_charpos. Handle text property and
2065 overlay changes. Set IT->stop_charpos to the next position where
2066 to stop. */
2068 static void
2069 handle_stop (it)
2070 struct it *it;
2072 enum prop_handled handled;
2073 int handle_overlay_change_p = 1;
2074 struct props *p;
2076 it->dpvec = NULL;
2077 it->current.dpvec_index = -1;
2081 handled = HANDLED_NORMALLY;
2083 /* Call text property handlers. */
2084 for (p = it_props; p->handler; ++p)
2086 handled = p->handler (it);
2088 if (handled == HANDLED_RECOMPUTE_PROPS)
2089 break;
2090 else if (handled == HANDLED_RETURN)
2091 return;
2092 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2093 handle_overlay_change_p = 0;
2096 if (handled != HANDLED_RECOMPUTE_PROPS)
2098 /* Don't check for overlay strings below when set to deliver
2099 characters from a display vector. */
2100 if (it->method == next_element_from_display_vector)
2101 handle_overlay_change_p = 0;
2103 /* Handle overlay changes. */
2104 if (handle_overlay_change_p)
2105 handled = handle_overlay_change (it);
2107 /* Determine where to stop next. */
2108 if (handled == HANDLED_NORMALLY)
2109 compute_stop_pos (it);
2112 while (handled == HANDLED_RECOMPUTE_PROPS);
2116 /* Compute IT->stop_charpos from text property and overlay change
2117 information for IT's current position. */
2119 static void
2120 compute_stop_pos (it)
2121 struct it *it;
2123 register INTERVAL iv, next_iv;
2124 Lisp_Object object, limit, position;
2126 /* If nowhere else, stop at the end. */
2127 it->stop_charpos = it->end_charpos;
2129 if (STRINGP (it->string))
2131 /* Strings are usually short, so don't limit the search for
2132 properties. */
2133 object = it->string;
2134 limit = Qnil;
2135 position = make_number (IT_STRING_CHARPOS (*it));
2137 else
2139 int charpos;
2141 /* If next overlay change is in front of the current stop pos
2142 (which is IT->end_charpos), stop there. Note: value of
2143 next_overlay_change is point-max if no overlay change
2144 follows. */
2145 charpos = next_overlay_change (IT_CHARPOS (*it));
2146 if (charpos < it->stop_charpos)
2147 it->stop_charpos = charpos;
2149 /* If showing the region, we have to stop at the region
2150 start or end because the face might change there. */
2151 if (it->region_beg_charpos > 0)
2153 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2154 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2155 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2156 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2159 /* Set up variables for computing the stop position from text
2160 property changes. */
2161 XSETBUFFER (object, current_buffer);
2162 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2163 position = make_number (IT_CHARPOS (*it));
2167 /* Get the interval containing IT's position. Value is a null
2168 interval if there isn't such an interval. */
2169 iv = validate_interval_range (object, &position, &position, 0);
2170 if (!NULL_INTERVAL_P (iv))
2172 Lisp_Object values_here[LAST_PROP_IDX];
2173 struct props *p;
2175 /* Get properties here. */
2176 for (p = it_props; p->handler; ++p)
2177 values_here[p->idx] = textget (iv->plist, *p->name);
2179 /* Look for an interval following iv that has different
2180 properties. */
2181 for (next_iv = next_interval (iv);
2182 (!NULL_INTERVAL_P (next_iv)
2183 && (NILP (limit)
2184 || XFASTINT (limit) > next_iv->position));
2185 next_iv = next_interval (next_iv))
2187 for (p = it_props; p->handler; ++p)
2189 Lisp_Object new_value;
2191 new_value = textget (next_iv->plist, *p->name);
2192 if (!EQ (values_here[p->idx], new_value))
2193 break;
2196 if (p->handler)
2197 break;
2200 if (!NULL_INTERVAL_P (next_iv))
2202 if (INTEGERP (limit)
2203 && next_iv->position >= XFASTINT (limit))
2204 /* No text property change up to limit. */
2205 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2206 else
2207 /* Text properties change in next_iv. */
2208 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2212 xassert (STRINGP (it->string)
2213 || (it->stop_charpos >= BEGV
2214 && it->stop_charpos >= IT_CHARPOS (*it)));
2218 /* Return the position of the next overlay change after POS in
2219 current_buffer. Value is point-max if no overlay change
2220 follows. This is like `next-overlay-change' but doesn't use
2221 xmalloc. */
2223 static int
2224 next_overlay_change (pos)
2225 int pos;
2227 int noverlays;
2228 int endpos;
2229 Lisp_Object *overlays;
2230 int len;
2231 int i;
2233 /* Get all overlays at the given position. */
2234 len = 10;
2235 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2236 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2237 if (noverlays > len)
2239 len = noverlays;
2240 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2241 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2244 /* If any of these overlays ends before endpos,
2245 use its ending point instead. */
2246 for (i = 0; i < noverlays; ++i)
2248 Lisp_Object oend;
2249 int oendpos;
2251 oend = OVERLAY_END (overlays[i]);
2252 oendpos = OVERLAY_POSITION (oend);
2253 endpos = min (endpos, oendpos);
2256 return endpos;
2261 /***********************************************************************
2262 Fontification
2263 ***********************************************************************/
2265 /* Handle changes in the `fontified' property of the current buffer by
2266 calling hook functions from Qfontification_functions to fontify
2267 regions of text. */
2269 static enum prop_handled
2270 handle_fontified_prop (it)
2271 struct it *it;
2273 Lisp_Object prop, pos;
2274 enum prop_handled handled = HANDLED_NORMALLY;
2276 /* Get the value of the `fontified' property at IT's current buffer
2277 position. (The `fontified' property doesn't have a special
2278 meaning in strings.) If the value is nil, call functions from
2279 Qfontification_functions. */
2280 if (!STRINGP (it->string)
2281 && it->s == NULL
2282 && !NILP (Vfontification_functions)
2283 && !NILP (Vrun_hooks)
2284 && (pos = make_number (IT_CHARPOS (*it)),
2285 prop = Fget_char_property (pos, Qfontified, Qnil),
2286 NILP (prop)))
2288 int count = SPECPDL_INDEX ();
2289 Lisp_Object val;
2291 val = Vfontification_functions;
2292 specbind (Qfontification_functions, Qnil);
2294 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2295 safe_call1 (val, pos);
2296 else
2298 Lisp_Object globals, fn;
2299 struct gcpro gcpro1, gcpro2;
2301 globals = Qnil;
2302 GCPRO2 (val, globals);
2304 for (; CONSP (val); val = XCDR (val))
2306 fn = XCAR (val);
2308 if (EQ (fn, Qt))
2310 /* A value of t indicates this hook has a local
2311 binding; it means to run the global binding too.
2312 In a global value, t should not occur. If it
2313 does, we must ignore it to avoid an endless
2314 loop. */
2315 for (globals = Fdefault_value (Qfontification_functions);
2316 CONSP (globals);
2317 globals = XCDR (globals))
2319 fn = XCAR (globals);
2320 if (!EQ (fn, Qt))
2321 safe_call1 (fn, pos);
2324 else
2325 safe_call1 (fn, pos);
2328 UNGCPRO;
2331 unbind_to (count, Qnil);
2333 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2334 something. This avoids an endless loop if they failed to
2335 fontify the text for which reason ever. */
2336 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2337 handled = HANDLED_RECOMPUTE_PROPS;
2340 return handled;
2345 /***********************************************************************
2346 Faces
2347 ***********************************************************************/
2349 /* Set up iterator IT from face properties at its current position.
2350 Called from handle_stop. */
2352 static enum prop_handled
2353 handle_face_prop (it)
2354 struct it *it;
2356 int new_face_id, next_stop;
2358 if (!STRINGP (it->string))
2360 new_face_id
2361 = face_at_buffer_position (it->w,
2362 IT_CHARPOS (*it),
2363 it->region_beg_charpos,
2364 it->region_end_charpos,
2365 &next_stop,
2366 (IT_CHARPOS (*it)
2367 + TEXT_PROP_DISTANCE_LIMIT),
2370 /* Is this a start of a run of characters with box face?
2371 Caveat: this can be called for a freshly initialized
2372 iterator; face_id is -1 in this case. We know that the new
2373 face will not change until limit, i.e. if the new face has a
2374 box, all characters up to limit will have one. But, as
2375 usual, we don't know whether limit is really the end. */
2376 if (new_face_id != it->face_id)
2378 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2380 /* If new face has a box but old face has not, this is
2381 the start of a run of characters with box, i.e. it has
2382 a shadow on the left side. The value of face_id of the
2383 iterator will be -1 if this is the initial call that gets
2384 the face. In this case, we have to look in front of IT's
2385 position and see whether there is a face != new_face_id. */
2386 it->start_of_box_run_p
2387 = (new_face->box != FACE_NO_BOX
2388 && (it->face_id >= 0
2389 || IT_CHARPOS (*it) == BEG
2390 || new_face_id != face_before_it_pos (it)));
2391 it->face_box_p = new_face->box != FACE_NO_BOX;
2394 else
2396 int base_face_id, bufpos;
2398 if (it->current.overlay_string_index >= 0)
2399 bufpos = IT_CHARPOS (*it);
2400 else
2401 bufpos = 0;
2403 /* For strings from a buffer, i.e. overlay strings or strings
2404 from a `display' property, use the face at IT's current
2405 buffer position as the base face to merge with, so that
2406 overlay strings appear in the same face as surrounding
2407 text, unless they specify their own faces. */
2408 base_face_id = underlying_face_id (it);
2410 new_face_id = face_at_string_position (it->w,
2411 it->string,
2412 IT_STRING_CHARPOS (*it),
2413 bufpos,
2414 it->region_beg_charpos,
2415 it->region_end_charpos,
2416 &next_stop,
2417 base_face_id, 0);
2419 #if 0 /* This shouldn't be neccessary. Let's check it. */
2420 /* If IT is used to display a mode line we would really like to
2421 use the mode line face instead of the frame's default face. */
2422 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2423 && new_face_id == DEFAULT_FACE_ID)
2424 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2425 #endif
2427 /* Is this a start of a run of characters with box? Caveat:
2428 this can be called for a freshly allocated iterator; face_id
2429 is -1 is this case. We know that the new face will not
2430 change until the next check pos, i.e. if the new face has a
2431 box, all characters up to that position will have a
2432 box. But, as usual, we don't know whether that position
2433 is really the end. */
2434 if (new_face_id != it->face_id)
2436 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2437 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2439 /* If new face has a box but old face hasn't, this is the
2440 start of a run of characters with box, i.e. it has a
2441 shadow on the left side. */
2442 it->start_of_box_run_p
2443 = new_face->box && (old_face == NULL || !old_face->box);
2444 it->face_box_p = new_face->box != FACE_NO_BOX;
2448 it->face_id = new_face_id;
2449 return HANDLED_NORMALLY;
2453 /* Return the ID of the face ``underlying'' IT's current position,
2454 which is in a string. If the iterator is associated with a
2455 buffer, return the face at IT's current buffer position.
2456 Otherwise, use the iterator's base_face_id. */
2458 static int
2459 underlying_face_id (it)
2460 struct it *it;
2462 int face_id = it->base_face_id, i;
2464 xassert (STRINGP (it->string));
2466 for (i = it->sp - 1; i >= 0; --i)
2467 if (NILP (it->stack[i].string))
2468 face_id = it->stack[i].face_id;
2470 return face_id;
2474 /* Compute the face one character before or after the current position
2475 of IT. BEFORE_P non-zero means get the face in front of IT's
2476 position. Value is the id of the face. */
2478 static int
2479 face_before_or_after_it_pos (it, before_p)
2480 struct it *it;
2481 int before_p;
2483 int face_id, limit;
2484 int next_check_charpos;
2485 struct text_pos pos;
2487 xassert (it->s == NULL);
2489 if (STRINGP (it->string))
2491 int bufpos, base_face_id;
2493 /* No face change past the end of the string (for the case
2494 we are padding with spaces). No face change before the
2495 string start. */
2496 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2497 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2498 return it->face_id;
2500 /* Set pos to the position before or after IT's current position. */
2501 if (before_p)
2502 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2503 else
2504 /* For composition, we must check the character after the
2505 composition. */
2506 pos = (it->what == IT_COMPOSITION
2507 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2508 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2510 if (it->current.overlay_string_index >= 0)
2511 bufpos = IT_CHARPOS (*it);
2512 else
2513 bufpos = 0;
2515 base_face_id = underlying_face_id (it);
2517 /* Get the face for ASCII, or unibyte. */
2518 face_id = face_at_string_position (it->w,
2519 it->string,
2520 CHARPOS (pos),
2521 bufpos,
2522 it->region_beg_charpos,
2523 it->region_end_charpos,
2524 &next_check_charpos,
2525 base_face_id, 0);
2527 /* Correct the face for charsets different from ASCII. Do it
2528 for the multibyte case only. The face returned above is
2529 suitable for unibyte text if IT->string is unibyte. */
2530 if (STRING_MULTIBYTE (it->string))
2532 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2533 int rest = SBYTES (it->string) - BYTEPOS (pos);
2534 int c, len;
2535 struct face *face = FACE_FROM_ID (it->f, face_id);
2537 c = string_char_and_length (p, rest, &len);
2538 face_id = FACE_FOR_CHAR (it->f, face, c);
2541 else
2543 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2544 || (IT_CHARPOS (*it) <= BEGV && before_p))
2545 return it->face_id;
2547 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2548 pos = it->current.pos;
2550 if (before_p)
2551 DEC_TEXT_POS (pos, it->multibyte_p);
2552 else
2554 if (it->what == IT_COMPOSITION)
2555 /* For composition, we must check the position after the
2556 composition. */
2557 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2558 else
2559 INC_TEXT_POS (pos, it->multibyte_p);
2562 /* Determine face for CHARSET_ASCII, or unibyte. */
2563 face_id = face_at_buffer_position (it->w,
2564 CHARPOS (pos),
2565 it->region_beg_charpos,
2566 it->region_end_charpos,
2567 &next_check_charpos,
2568 limit, 0);
2570 /* Correct the face for charsets different from ASCII. Do it
2571 for the multibyte case only. The face returned above is
2572 suitable for unibyte text if current_buffer is unibyte. */
2573 if (it->multibyte_p)
2575 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2576 struct face *face = FACE_FROM_ID (it->f, face_id);
2577 face_id = FACE_FOR_CHAR (it->f, face, c);
2581 return face_id;
2586 /***********************************************************************
2587 Invisible text
2588 ***********************************************************************/
2590 /* Set up iterator IT from invisible properties at its current
2591 position. Called from handle_stop. */
2593 static enum prop_handled
2594 handle_invisible_prop (it)
2595 struct it *it;
2597 enum prop_handled handled = HANDLED_NORMALLY;
2599 if (STRINGP (it->string))
2601 extern Lisp_Object Qinvisible;
2602 Lisp_Object prop, end_charpos, limit, charpos;
2604 /* Get the value of the invisible text property at the
2605 current position. Value will be nil if there is no such
2606 property. */
2607 charpos = make_number (IT_STRING_CHARPOS (*it));
2608 prop = Fget_text_property (charpos, Qinvisible, it->string);
2610 if (!NILP (prop)
2611 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2613 handled = HANDLED_RECOMPUTE_PROPS;
2615 /* Get the position at which the next change of the
2616 invisible text property can be found in IT->string.
2617 Value will be nil if the property value is the same for
2618 all the rest of IT->string. */
2619 XSETINT (limit, SCHARS (it->string));
2620 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2621 it->string, limit);
2623 /* Text at current position is invisible. The next
2624 change in the property is at position end_charpos.
2625 Move IT's current position to that position. */
2626 if (INTEGERP (end_charpos)
2627 && XFASTINT (end_charpos) < XFASTINT (limit))
2629 struct text_pos old;
2630 old = it->current.string_pos;
2631 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2632 compute_string_pos (&it->current.string_pos, old, it->string);
2634 else
2636 /* The rest of the string is invisible. If this is an
2637 overlay string, proceed with the next overlay string
2638 or whatever comes and return a character from there. */
2639 if (it->current.overlay_string_index >= 0)
2641 next_overlay_string (it);
2642 /* Don't check for overlay strings when we just
2643 finished processing them. */
2644 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2646 else
2648 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2649 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2654 else
2656 int invis_p, newpos, next_stop, start_charpos;
2657 Lisp_Object pos, prop, overlay;
2659 /* First of all, is there invisible text at this position? */
2660 start_charpos = IT_CHARPOS (*it);
2661 pos = make_number (IT_CHARPOS (*it));
2662 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2663 &overlay);
2664 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2666 /* If we are on invisible text, skip over it. */
2667 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2669 /* Record whether we have to display an ellipsis for the
2670 invisible text. */
2671 int display_ellipsis_p = invis_p == 2;
2673 handled = HANDLED_RECOMPUTE_PROPS;
2675 /* Loop skipping over invisible text. The loop is left at
2676 ZV or with IT on the first char being visible again. */
2679 /* Try to skip some invisible text. Return value is the
2680 position reached which can be equal to IT's position
2681 if there is nothing invisible here. This skips both
2682 over invisible text properties and overlays with
2683 invisible property. */
2684 newpos = skip_invisible (IT_CHARPOS (*it),
2685 &next_stop, ZV, it->window);
2687 /* If we skipped nothing at all we weren't at invisible
2688 text in the first place. If everything to the end of
2689 the buffer was skipped, end the loop. */
2690 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2691 invis_p = 0;
2692 else
2694 /* We skipped some characters but not necessarily
2695 all there are. Check if we ended up on visible
2696 text. Fget_char_property returns the property of
2697 the char before the given position, i.e. if we
2698 get invis_p = 0, this means that the char at
2699 newpos is visible. */
2700 pos = make_number (newpos);
2701 prop = Fget_char_property (pos, Qinvisible, it->window);
2702 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2705 /* If we ended up on invisible text, proceed to
2706 skip starting with next_stop. */
2707 if (invis_p)
2708 IT_CHARPOS (*it) = next_stop;
2710 while (invis_p);
2712 /* The position newpos is now either ZV or on visible text. */
2713 IT_CHARPOS (*it) = newpos;
2714 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2716 /* If there are before-strings at the start of invisible
2717 text, and the text is invisible because of a text
2718 property, arrange to show before-strings because 20.x did
2719 it that way. (If the text is invisible because of an
2720 overlay property instead of a text property, this is
2721 already handled in the overlay code.) */
2722 if (NILP (overlay)
2723 && get_overlay_strings (it, start_charpos))
2725 handled = HANDLED_RECOMPUTE_PROPS;
2726 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2728 else if (display_ellipsis_p)
2729 setup_for_ellipsis (it);
2733 return handled;
2737 /* Make iterator IT return `...' next. */
2739 static void
2740 setup_for_ellipsis (it)
2741 struct it *it;
2743 if (it->dp
2744 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2746 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2747 it->dpvec = v->contents;
2748 it->dpend = v->contents + v->size;
2750 else
2752 /* Default `...'. */
2753 it->dpvec = default_invis_vector;
2754 it->dpend = default_invis_vector + 3;
2757 /* The ellipsis display does not replace the display of the
2758 character at the new position. Indicate this by setting
2759 IT->dpvec_char_len to zero. */
2760 it->dpvec_char_len = 0;
2762 it->current.dpvec_index = 0;
2763 it->method = next_element_from_display_vector;
2768 /***********************************************************************
2769 'display' property
2770 ***********************************************************************/
2772 /* Set up iterator IT from `display' property at its current position.
2773 Called from handle_stop. */
2775 static enum prop_handled
2776 handle_display_prop (it)
2777 struct it *it;
2779 Lisp_Object prop, object;
2780 struct text_pos *position;
2781 int display_replaced_p = 0;
2783 if (STRINGP (it->string))
2785 object = it->string;
2786 position = &it->current.string_pos;
2788 else
2790 object = it->w->buffer;
2791 position = &it->current.pos;
2794 /* Reset those iterator values set from display property values. */
2795 it->font_height = Qnil;
2796 it->space_width = Qnil;
2797 it->voffset = 0;
2799 /* We don't support recursive `display' properties, i.e. string
2800 values that have a string `display' property, that have a string
2801 `display' property etc. */
2802 if (!it->string_from_display_prop_p)
2803 it->area = TEXT_AREA;
2805 prop = Fget_char_property (make_number (position->charpos),
2806 Qdisplay, object);
2807 if (NILP (prop))
2808 return HANDLED_NORMALLY;
2810 if (CONSP (prop)
2811 /* Simple properties. */
2812 && !EQ (XCAR (prop), Qimage)
2813 && !EQ (XCAR (prop), Qspace)
2814 && !EQ (XCAR (prop), Qwhen)
2815 && !EQ (XCAR (prop), Qspace_width)
2816 && !EQ (XCAR (prop), Qheight)
2817 && !EQ (XCAR (prop), Qraise)
2818 /* Marginal area specifications. */
2819 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2820 && !NILP (XCAR (prop)))
2822 for (; CONSP (prop); prop = XCDR (prop))
2824 if (handle_single_display_prop (it, XCAR (prop), object,
2825 position, display_replaced_p))
2826 display_replaced_p = 1;
2829 else if (VECTORP (prop))
2831 int i;
2832 for (i = 0; i < ASIZE (prop); ++i)
2833 if (handle_single_display_prop (it, AREF (prop, i), object,
2834 position, display_replaced_p))
2835 display_replaced_p = 1;
2837 else
2839 if (handle_single_display_prop (it, prop, object, position, 0))
2840 display_replaced_p = 1;
2843 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2847 /* Value is the position of the end of the `display' property starting
2848 at START_POS in OBJECT. */
2850 static struct text_pos
2851 display_prop_end (it, object, start_pos)
2852 struct it *it;
2853 Lisp_Object object;
2854 struct text_pos start_pos;
2856 Lisp_Object end;
2857 struct text_pos end_pos;
2859 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2860 Qdisplay, object, Qnil);
2861 CHARPOS (end_pos) = XFASTINT (end);
2862 if (STRINGP (object))
2863 compute_string_pos (&end_pos, start_pos, it->string);
2864 else
2865 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2867 return end_pos;
2871 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2872 is the object in which the `display' property was found. *POSITION
2873 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2874 means that we previously saw a display sub-property which already
2875 replaced text display with something else, for example an image;
2876 ignore such properties after the first one has been processed.
2878 If PROP is a `space' or `image' sub-property, set *POSITION to the
2879 end position of the `display' property.
2881 Value is non-zero if something was found which replaces the display
2882 of buffer or string text. */
2884 static int
2885 handle_single_display_prop (it, prop, object, position,
2886 display_replaced_before_p)
2887 struct it *it;
2888 Lisp_Object prop;
2889 Lisp_Object object;
2890 struct text_pos *position;
2891 int display_replaced_before_p;
2893 Lisp_Object value;
2894 int replaces_text_display_p = 0;
2895 Lisp_Object form;
2897 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2898 evaluated. If the result is nil, VALUE is ignored. */
2899 form = Qt;
2900 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2902 prop = XCDR (prop);
2903 if (!CONSP (prop))
2904 return 0;
2905 form = XCAR (prop);
2906 prop = XCDR (prop);
2909 if (!NILP (form) && !EQ (form, Qt))
2911 int count = SPECPDL_INDEX ();
2912 struct gcpro gcpro1;
2914 /* Bind `object' to the object having the `display' property, a
2915 buffer or string. Bind `position' to the position in the
2916 object where the property was found, and `buffer-position'
2917 to the current position in the buffer. */
2918 specbind (Qobject, object);
2919 specbind (Qposition, make_number (CHARPOS (*position)));
2920 specbind (Qbuffer_position,
2921 make_number (STRINGP (object)
2922 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2923 GCPRO1 (form);
2924 form = safe_eval (form);
2925 UNGCPRO;
2926 unbind_to (count, Qnil);
2929 if (NILP (form))
2930 return 0;
2932 if (CONSP (prop)
2933 && EQ (XCAR (prop), Qheight)
2934 && CONSP (XCDR (prop)))
2936 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2937 return 0;
2939 /* `(height HEIGHT)'. */
2940 it->font_height = XCAR (XCDR (prop));
2941 if (!NILP (it->font_height))
2943 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2944 int new_height = -1;
2946 if (CONSP (it->font_height)
2947 && (EQ (XCAR (it->font_height), Qplus)
2948 || EQ (XCAR (it->font_height), Qminus))
2949 && CONSP (XCDR (it->font_height))
2950 && INTEGERP (XCAR (XCDR (it->font_height))))
2952 /* `(+ N)' or `(- N)' where N is an integer. */
2953 int steps = XINT (XCAR (XCDR (it->font_height)));
2954 if (EQ (XCAR (it->font_height), Qplus))
2955 steps = - steps;
2956 it->face_id = smaller_face (it->f, it->face_id, steps);
2958 else if (FUNCTIONP (it->font_height))
2960 /* Call function with current height as argument.
2961 Value is the new height. */
2962 Lisp_Object height;
2963 height = safe_call1 (it->font_height,
2964 face->lface[LFACE_HEIGHT_INDEX]);
2965 if (NUMBERP (height))
2966 new_height = XFLOATINT (height);
2968 else if (NUMBERP (it->font_height))
2970 /* Value is a multiple of the canonical char height. */
2971 struct face *face;
2973 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2974 new_height = (XFLOATINT (it->font_height)
2975 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2977 else
2979 /* Evaluate IT->font_height with `height' bound to the
2980 current specified height to get the new height. */
2981 Lisp_Object value;
2982 int count = SPECPDL_INDEX ();
2984 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2985 value = safe_eval (it->font_height);
2986 unbind_to (count, Qnil);
2988 if (NUMBERP (value))
2989 new_height = XFLOATINT (value);
2992 if (new_height > 0)
2993 it->face_id = face_with_height (it->f, it->face_id, new_height);
2996 else if (CONSP (prop)
2997 && EQ (XCAR (prop), Qspace_width)
2998 && CONSP (XCDR (prop)))
3000 /* `(space_width WIDTH)'. */
3001 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3002 return 0;
3004 value = XCAR (XCDR (prop));
3005 if (NUMBERP (value) && XFLOATINT (value) > 0)
3006 it->space_width = value;
3008 else if (CONSP (prop)
3009 && EQ (XCAR (prop), Qraise)
3010 && CONSP (XCDR (prop)))
3012 /* `(raise FACTOR)'. */
3013 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3014 return 0;
3016 #ifdef HAVE_WINDOW_SYSTEM
3017 value = XCAR (XCDR (prop));
3018 if (NUMBERP (value))
3020 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3021 it->voffset = - (XFLOATINT (value)
3022 * (FONT_HEIGHT (face->font)));
3024 #endif /* HAVE_WINDOW_SYSTEM */
3026 else if (!it->string_from_display_prop_p)
3028 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3029 VALUE) or `((margin nil) VALUE)' or VALUE. */
3030 Lisp_Object location, value;
3031 struct text_pos start_pos;
3032 int valid_p;
3034 /* Characters having this form of property are not displayed, so
3035 we have to find the end of the property. */
3036 start_pos = *position;
3037 *position = display_prop_end (it, object, start_pos);
3038 value = Qnil;
3040 /* Let's stop at the new position and assume that all
3041 text properties change there. */
3042 it->stop_charpos = position->charpos;
3044 location = Qunbound;
3045 if (CONSP (prop) && CONSP (XCAR (prop)))
3047 Lisp_Object tem;
3049 value = XCDR (prop);
3050 if (CONSP (value))
3051 value = XCAR (value);
3053 tem = XCAR (prop);
3054 if (EQ (XCAR (tem), Qmargin)
3055 && (tem = XCDR (tem),
3056 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3057 (NILP (tem)
3058 || EQ (tem, Qleft_margin)
3059 || EQ (tem, Qright_margin))))
3060 location = tem;
3063 if (EQ (location, Qunbound))
3065 location = Qnil;
3066 value = prop;
3069 #ifdef HAVE_WINDOW_SYSTEM
3070 if (FRAME_TERMCAP_P (it->f))
3071 valid_p = STRINGP (value);
3072 else
3073 valid_p = (STRINGP (value)
3074 || (CONSP (value) && EQ (XCAR (value), Qspace))
3075 || valid_image_p (value));
3076 #else /* not HAVE_WINDOW_SYSTEM */
3077 valid_p = STRINGP (value);
3078 #endif /* not HAVE_WINDOW_SYSTEM */
3080 if ((EQ (location, Qleft_margin)
3081 || EQ (location, Qright_margin)
3082 || NILP (location))
3083 && valid_p
3084 && !display_replaced_before_p)
3086 replaces_text_display_p = 1;
3088 /* Save current settings of IT so that we can restore them
3089 when we are finished with the glyph property value. */
3090 push_it (it);
3092 if (NILP (location))
3093 it->area = TEXT_AREA;
3094 else if (EQ (location, Qleft_margin))
3095 it->area = LEFT_MARGIN_AREA;
3096 else
3097 it->area = RIGHT_MARGIN_AREA;
3099 if (STRINGP (value))
3101 it->string = value;
3102 it->multibyte_p = STRING_MULTIBYTE (it->string);
3103 it->current.overlay_string_index = -1;
3104 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3105 it->end_charpos = it->string_nchars = SCHARS (it->string);
3106 it->method = next_element_from_string;
3107 it->stop_charpos = 0;
3108 it->string_from_display_prop_p = 1;
3109 /* Say that we haven't consumed the characters with
3110 `display' property yet. The call to pop_it in
3111 set_iterator_to_next will clean this up. */
3112 *position = start_pos;
3114 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3116 it->method = next_element_from_stretch;
3117 it->object = value;
3118 it->current.pos = it->position = start_pos;
3120 #ifdef HAVE_WINDOW_SYSTEM
3121 else
3123 it->what = IT_IMAGE;
3124 it->image_id = lookup_image (it->f, value);
3125 it->position = start_pos;
3126 it->object = NILP (object) ? it->w->buffer : object;
3127 it->method = next_element_from_image;
3129 /* Say that we haven't consumed the characters with
3130 `display' property yet. The call to pop_it in
3131 set_iterator_to_next will clean this up. */
3132 *position = start_pos;
3134 #endif /* HAVE_WINDOW_SYSTEM */
3136 else
3137 /* Invalid property or property not supported. Restore
3138 the position to what it was before. */
3139 *position = start_pos;
3142 return replaces_text_display_p;
3146 /* Check if PROP is a display sub-property value whose text should be
3147 treated as intangible. */
3149 static int
3150 single_display_prop_intangible_p (prop)
3151 Lisp_Object prop;
3153 /* Skip over `when FORM'. */
3154 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3156 prop = XCDR (prop);
3157 if (!CONSP (prop))
3158 return 0;
3159 prop = XCDR (prop);
3162 if (STRINGP (prop))
3163 return 1;
3165 if (!CONSP (prop))
3166 return 0;
3168 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3169 we don't need to treat text as intangible. */
3170 if (EQ (XCAR (prop), Qmargin))
3172 prop = XCDR (prop);
3173 if (!CONSP (prop))
3174 return 0;
3176 prop = XCDR (prop);
3177 if (!CONSP (prop)
3178 || EQ (XCAR (prop), Qleft_margin)
3179 || EQ (XCAR (prop), Qright_margin))
3180 return 0;
3183 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3187 /* Check if PROP is a display property value whose text should be
3188 treated as intangible. */
3191 display_prop_intangible_p (prop)
3192 Lisp_Object prop;
3194 if (CONSP (prop)
3195 && CONSP (XCAR (prop))
3196 && !EQ (Qmargin, XCAR (XCAR (prop))))
3198 /* A list of sub-properties. */
3199 while (CONSP (prop))
3201 if (single_display_prop_intangible_p (XCAR (prop)))
3202 return 1;
3203 prop = XCDR (prop);
3206 else if (VECTORP (prop))
3208 /* A vector of sub-properties. */
3209 int i;
3210 for (i = 0; i < ASIZE (prop); ++i)
3211 if (single_display_prop_intangible_p (AREF (prop, i)))
3212 return 1;
3214 else
3215 return single_display_prop_intangible_p (prop);
3217 return 0;
3221 /* Return 1 if PROP is a display sub-property value containing STRING. */
3223 static int
3224 single_display_prop_string_p (prop, string)
3225 Lisp_Object prop, string;
3227 if (EQ (string, prop))
3228 return 1;
3230 /* Skip over `when FORM'. */
3231 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3233 prop = XCDR (prop);
3234 if (!CONSP (prop))
3235 return 0;
3236 prop = XCDR (prop);
3239 if (CONSP (prop))
3240 /* Skip over `margin LOCATION'. */
3241 if (EQ (XCAR (prop), Qmargin))
3243 prop = XCDR (prop);
3244 if (!CONSP (prop))
3245 return 0;
3247 prop = XCDR (prop);
3248 if (!CONSP (prop))
3249 return 0;
3252 return CONSP (prop) && EQ (XCAR (prop), string);
3256 /* Return 1 if STRING appears in the `display' property PROP. */
3258 static int
3259 display_prop_string_p (prop, string)
3260 Lisp_Object prop, string;
3262 if (CONSP (prop)
3263 && CONSP (XCAR (prop))
3264 && !EQ (Qmargin, XCAR (XCAR (prop))))
3266 /* A list of sub-properties. */
3267 while (CONSP (prop))
3269 if (single_display_prop_string_p (XCAR (prop), string))
3270 return 1;
3271 prop = XCDR (prop);
3274 else if (VECTORP (prop))
3276 /* A vector of sub-properties. */
3277 int i;
3278 for (i = 0; i < ASIZE (prop); ++i)
3279 if (single_display_prop_string_p (AREF (prop, i), string))
3280 return 1;
3282 else
3283 return single_display_prop_string_p (prop, string);
3285 return 0;
3289 /* Determine from which buffer position in W's buffer STRING comes
3290 from. AROUND_CHARPOS is an approximate position where it could
3291 be from. Value is the buffer position or 0 if it couldn't be
3292 determined.
3294 W's buffer must be current.
3296 This function is necessary because we don't record buffer positions
3297 in glyphs generated from strings (to keep struct glyph small).
3298 This function may only use code that doesn't eval because it is
3299 called asynchronously from note_mouse_highlight. */
3302 string_buffer_position (w, string, around_charpos)
3303 struct window *w;
3304 Lisp_Object string;
3305 int around_charpos;
3307 Lisp_Object limit, prop, pos;
3308 const int MAX_DISTANCE = 1000;
3309 int found = 0;
3311 pos = make_number (around_charpos);
3312 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3313 while (!found && !EQ (pos, limit))
3315 prop = Fget_char_property (pos, Qdisplay, Qnil);
3316 if (!NILP (prop) && display_prop_string_p (prop, string))
3317 found = 1;
3318 else
3319 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3322 if (!found)
3324 pos = make_number (around_charpos);
3325 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3326 while (!found && !EQ (pos, limit))
3328 prop = Fget_char_property (pos, Qdisplay, Qnil);
3329 if (!NILP (prop) && display_prop_string_p (prop, string))
3330 found = 1;
3331 else
3332 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3333 limit);
3337 return found ? XINT (pos) : 0;
3342 /***********************************************************************
3343 `composition' property
3344 ***********************************************************************/
3346 /* Set up iterator IT from `composition' property at its current
3347 position. Called from handle_stop. */
3349 static enum prop_handled
3350 handle_composition_prop (it)
3351 struct it *it;
3353 Lisp_Object prop, string;
3354 int pos, pos_byte, end;
3355 enum prop_handled handled = HANDLED_NORMALLY;
3357 if (STRINGP (it->string))
3359 pos = IT_STRING_CHARPOS (*it);
3360 pos_byte = IT_STRING_BYTEPOS (*it);
3361 string = it->string;
3363 else
3365 pos = IT_CHARPOS (*it);
3366 pos_byte = IT_BYTEPOS (*it);
3367 string = Qnil;
3370 /* If there's a valid composition and point is not inside of the
3371 composition (in the case that the composition is from the current
3372 buffer), draw a glyph composed from the composition components. */
3373 if (find_composition (pos, -1, &pos, &end, &prop, string)
3374 && COMPOSITION_VALID_P (pos, end, prop)
3375 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3377 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3379 if (id >= 0)
3381 it->method = next_element_from_composition;
3382 it->cmp_id = id;
3383 it->cmp_len = COMPOSITION_LENGTH (prop);
3384 /* For a terminal, draw only the first character of the
3385 components. */
3386 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3387 it->len = (STRINGP (it->string)
3388 ? string_char_to_byte (it->string, end)
3389 : CHAR_TO_BYTE (end)) - pos_byte;
3390 it->stop_charpos = end;
3391 handled = HANDLED_RETURN;
3395 return handled;
3400 /***********************************************************************
3401 Overlay strings
3402 ***********************************************************************/
3404 /* The following structure is used to record overlay strings for
3405 later sorting in load_overlay_strings. */
3407 struct overlay_entry
3409 Lisp_Object overlay;
3410 Lisp_Object string;
3411 int priority;
3412 int after_string_p;
3416 /* Set up iterator IT from overlay strings at its current position.
3417 Called from handle_stop. */
3419 static enum prop_handled
3420 handle_overlay_change (it)
3421 struct it *it;
3423 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3424 return HANDLED_RECOMPUTE_PROPS;
3425 else
3426 return HANDLED_NORMALLY;
3430 /* Set up the next overlay string for delivery by IT, if there is an
3431 overlay string to deliver. Called by set_iterator_to_next when the
3432 end of the current overlay string is reached. If there are more
3433 overlay strings to display, IT->string and
3434 IT->current.overlay_string_index are set appropriately here.
3435 Otherwise IT->string is set to nil. */
3437 static void
3438 next_overlay_string (it)
3439 struct it *it;
3441 ++it->current.overlay_string_index;
3442 if (it->current.overlay_string_index == it->n_overlay_strings)
3444 /* No more overlay strings. Restore IT's settings to what
3445 they were before overlay strings were processed, and
3446 continue to deliver from current_buffer. */
3447 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3449 pop_it (it);
3450 xassert (it->stop_charpos >= BEGV
3451 && it->stop_charpos <= it->end_charpos);
3452 it->string = Qnil;
3453 it->current.overlay_string_index = -1;
3454 SET_TEXT_POS (it->current.string_pos, -1, -1);
3455 it->n_overlay_strings = 0;
3456 it->method = next_element_from_buffer;
3458 /* If we're at the end of the buffer, record that we have
3459 processed the overlay strings there already, so that
3460 next_element_from_buffer doesn't try it again. */
3461 if (IT_CHARPOS (*it) >= it->end_charpos)
3462 it->overlay_strings_at_end_processed_p = 1;
3464 /* If we have to display `...' for invisible text, set
3465 the iterator up for that. */
3466 if (display_ellipsis_p)
3467 setup_for_ellipsis (it);
3469 else
3471 /* There are more overlay strings to process. If
3472 IT->current.overlay_string_index has advanced to a position
3473 where we must load IT->overlay_strings with more strings, do
3474 it. */
3475 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3477 if (it->current.overlay_string_index && i == 0)
3478 load_overlay_strings (it, 0);
3480 /* Initialize IT to deliver display elements from the overlay
3481 string. */
3482 it->string = it->overlay_strings[i];
3483 it->multibyte_p = STRING_MULTIBYTE (it->string);
3484 SET_TEXT_POS (it->current.string_pos, 0, 0);
3485 it->method = next_element_from_string;
3486 it->stop_charpos = 0;
3489 CHECK_IT (it);
3493 /* Compare two overlay_entry structures E1 and E2. Used as a
3494 comparison function for qsort in load_overlay_strings. Overlay
3495 strings for the same position are sorted so that
3497 1. All after-strings come in front of before-strings, except
3498 when they come from the same overlay.
3500 2. Within after-strings, strings are sorted so that overlay strings
3501 from overlays with higher priorities come first.
3503 2. Within before-strings, strings are sorted so that overlay
3504 strings from overlays with higher priorities come last.
3506 Value is analogous to strcmp. */
3509 static int
3510 compare_overlay_entries (e1, e2)
3511 void *e1, *e2;
3513 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3514 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3515 int result;
3517 if (entry1->after_string_p != entry2->after_string_p)
3519 /* Let after-strings appear in front of before-strings if
3520 they come from different overlays. */
3521 if (EQ (entry1->overlay, entry2->overlay))
3522 result = entry1->after_string_p ? 1 : -1;
3523 else
3524 result = entry1->after_string_p ? -1 : 1;
3526 else if (entry1->after_string_p)
3527 /* After-strings sorted in order of decreasing priority. */
3528 result = entry2->priority - entry1->priority;
3529 else
3530 /* Before-strings sorted in order of increasing priority. */
3531 result = entry1->priority - entry2->priority;
3533 return result;
3537 /* Load the vector IT->overlay_strings with overlay strings from IT's
3538 current buffer position, or from CHARPOS if that is > 0. Set
3539 IT->n_overlays to the total number of overlay strings found.
3541 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3542 a time. On entry into load_overlay_strings,
3543 IT->current.overlay_string_index gives the number of overlay
3544 strings that have already been loaded by previous calls to this
3545 function.
3547 IT->add_overlay_start contains an additional overlay start
3548 position to consider for taking overlay strings from, if non-zero.
3549 This position comes into play when the overlay has an `invisible'
3550 property, and both before and after-strings. When we've skipped to
3551 the end of the overlay, because of its `invisible' property, we
3552 nevertheless want its before-string to appear.
3553 IT->add_overlay_start will contain the overlay start position
3554 in this case.
3556 Overlay strings are sorted so that after-string strings come in
3557 front of before-string strings. Within before and after-strings,
3558 strings are sorted by overlay priority. See also function
3559 compare_overlay_entries. */
3561 static void
3562 load_overlay_strings (it, charpos)
3563 struct it *it;
3564 int charpos;
3566 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3567 Lisp_Object ov, overlay, window, str, invisible;
3568 int start, end;
3569 int size = 20;
3570 int n = 0, i, j, invis_p;
3571 struct overlay_entry *entries
3572 = (struct overlay_entry *) alloca (size * sizeof *entries);
3574 if (charpos <= 0)
3575 charpos = IT_CHARPOS (*it);
3577 /* Append the overlay string STRING of overlay OVERLAY to vector
3578 `entries' which has size `size' and currently contains `n'
3579 elements. AFTER_P non-zero means STRING is an after-string of
3580 OVERLAY. */
3581 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3582 do \
3584 Lisp_Object priority; \
3586 if (n == size) \
3588 int new_size = 2 * size; \
3589 struct overlay_entry *old = entries; \
3590 entries = \
3591 (struct overlay_entry *) alloca (new_size \
3592 * sizeof *entries); \
3593 bcopy (old, entries, size * sizeof *entries); \
3594 size = new_size; \
3597 entries[n].string = (STRING); \
3598 entries[n].overlay = (OVERLAY); \
3599 priority = Foverlay_get ((OVERLAY), Qpriority); \
3600 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3601 entries[n].after_string_p = (AFTER_P); \
3602 ++n; \
3604 while (0)
3606 /* Process overlay before the overlay center. */
3607 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3609 overlay = XCAR (ov);
3610 xassert (OVERLAYP (overlay));
3611 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3612 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3614 if (end < charpos)
3615 break;
3617 /* Skip this overlay if it doesn't start or end at IT's current
3618 position. */
3619 if (end != charpos && start != charpos)
3620 continue;
3622 /* Skip this overlay if it doesn't apply to IT->w. */
3623 window = Foverlay_get (overlay, Qwindow);
3624 if (WINDOWP (window) && XWINDOW (window) != it->w)
3625 continue;
3627 /* If the text ``under'' the overlay is invisible, both before-
3628 and after-strings from this overlay are visible; start and
3629 end position are indistinguishable. */
3630 invisible = Foverlay_get (overlay, Qinvisible);
3631 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3633 /* If overlay has a non-empty before-string, record it. */
3634 if ((start == charpos || (end == charpos && invis_p))
3635 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3636 && SCHARS (str))
3637 RECORD_OVERLAY_STRING (overlay, str, 0);
3639 /* If overlay has a non-empty after-string, record it. */
3640 if ((end == charpos || (start == charpos && invis_p))
3641 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3642 && SCHARS (str))
3643 RECORD_OVERLAY_STRING (overlay, str, 1);
3646 /* Process overlays after the overlay center. */
3647 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3649 overlay = XCAR (ov);
3650 xassert (OVERLAYP (overlay));
3651 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3652 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3654 if (start > charpos)
3655 break;
3657 /* Skip this overlay if it doesn't start or end at IT's current
3658 position. */
3659 if (end != charpos && start != charpos)
3660 continue;
3662 /* Skip this overlay if it doesn't apply to IT->w. */
3663 window = Foverlay_get (overlay, Qwindow);
3664 if (WINDOWP (window) && XWINDOW (window) != it->w)
3665 continue;
3667 /* If the text ``under'' the overlay is invisible, it has a zero
3668 dimension, and both before- and after-strings apply. */
3669 invisible = Foverlay_get (overlay, Qinvisible);
3670 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3672 /* If overlay has a non-empty before-string, record it. */
3673 if ((start == charpos || (end == charpos && invis_p))
3674 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3675 && SCHARS (str))
3676 RECORD_OVERLAY_STRING (overlay, str, 0);
3678 /* If overlay has a non-empty after-string, record it. */
3679 if ((end == charpos || (start == charpos && invis_p))
3680 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3681 && SCHARS (str))
3682 RECORD_OVERLAY_STRING (overlay, str, 1);
3685 #undef RECORD_OVERLAY_STRING
3687 /* Sort entries. */
3688 if (n > 1)
3689 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3691 /* Record the total number of strings to process. */
3692 it->n_overlay_strings = n;
3694 /* IT->current.overlay_string_index is the number of overlay strings
3695 that have already been consumed by IT. Copy some of the
3696 remaining overlay strings to IT->overlay_strings. */
3697 i = 0;
3698 j = it->current.overlay_string_index;
3699 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3700 it->overlay_strings[i++] = entries[j++].string;
3702 CHECK_IT (it);
3706 /* Get the first chunk of overlay strings at IT's current buffer
3707 position, or at CHARPOS if that is > 0. Value is non-zero if at
3708 least one overlay string was found. */
3710 static int
3711 get_overlay_strings (it, charpos)
3712 struct it *it;
3713 int charpos;
3715 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3716 process. This fills IT->overlay_strings with strings, and sets
3717 IT->n_overlay_strings to the total number of strings to process.
3718 IT->pos.overlay_string_index has to be set temporarily to zero
3719 because load_overlay_strings needs this; it must be set to -1
3720 when no overlay strings are found because a zero value would
3721 indicate a position in the first overlay string. */
3722 it->current.overlay_string_index = 0;
3723 load_overlay_strings (it, charpos);
3725 /* If we found overlay strings, set up IT to deliver display
3726 elements from the first one. Otherwise set up IT to deliver
3727 from current_buffer. */
3728 if (it->n_overlay_strings)
3730 /* Make sure we know settings in current_buffer, so that we can
3731 restore meaningful values when we're done with the overlay
3732 strings. */
3733 compute_stop_pos (it);
3734 xassert (it->face_id >= 0);
3736 /* Save IT's settings. They are restored after all overlay
3737 strings have been processed. */
3738 xassert (it->sp == 0);
3739 push_it (it);
3741 /* Set up IT to deliver display elements from the first overlay
3742 string. */
3743 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3744 it->string = it->overlay_strings[0];
3745 it->stop_charpos = 0;
3746 xassert (STRINGP (it->string));
3747 it->end_charpos = SCHARS (it->string);
3748 it->multibyte_p = STRING_MULTIBYTE (it->string);
3749 it->method = next_element_from_string;
3751 else
3753 it->string = Qnil;
3754 it->current.overlay_string_index = -1;
3755 it->method = next_element_from_buffer;
3758 CHECK_IT (it);
3760 /* Value is non-zero if we found at least one overlay string. */
3761 return STRINGP (it->string);
3766 /***********************************************************************
3767 Saving and restoring state
3768 ***********************************************************************/
3770 /* Save current settings of IT on IT->stack. Called, for example,
3771 before setting up IT for an overlay string, to be able to restore
3772 IT's settings to what they were after the overlay string has been
3773 processed. */
3775 static void
3776 push_it (it)
3777 struct it *it;
3779 struct iterator_stack_entry *p;
3781 xassert (it->sp < 2);
3782 p = it->stack + it->sp;
3784 p->stop_charpos = it->stop_charpos;
3785 xassert (it->face_id >= 0);
3786 p->face_id = it->face_id;
3787 p->string = it->string;
3788 p->pos = it->current;
3789 p->end_charpos = it->end_charpos;
3790 p->string_nchars = it->string_nchars;
3791 p->area = it->area;
3792 p->multibyte_p = it->multibyte_p;
3793 p->space_width = it->space_width;
3794 p->font_height = it->font_height;
3795 p->voffset = it->voffset;
3796 p->string_from_display_prop_p = it->string_from_display_prop_p;
3797 p->display_ellipsis_p = 0;
3798 ++it->sp;
3802 /* Restore IT's settings from IT->stack. Called, for example, when no
3803 more overlay strings must be processed, and we return to delivering
3804 display elements from a buffer, or when the end of a string from a
3805 `display' property is reached and we return to delivering display
3806 elements from an overlay string, or from a buffer. */
3808 static void
3809 pop_it (it)
3810 struct it *it;
3812 struct iterator_stack_entry *p;
3814 xassert (it->sp > 0);
3815 --it->sp;
3816 p = it->stack + it->sp;
3817 it->stop_charpos = p->stop_charpos;
3818 it->face_id = p->face_id;
3819 it->string = p->string;
3820 it->current = p->pos;
3821 it->end_charpos = p->end_charpos;
3822 it->string_nchars = p->string_nchars;
3823 it->area = p->area;
3824 it->multibyte_p = p->multibyte_p;
3825 it->space_width = p->space_width;
3826 it->font_height = p->font_height;
3827 it->voffset = p->voffset;
3828 it->string_from_display_prop_p = p->string_from_display_prop_p;
3833 /***********************************************************************
3834 Moving over lines
3835 ***********************************************************************/
3837 /* Set IT's current position to the previous line start. */
3839 static void
3840 back_to_previous_line_start (it)
3841 struct it *it;
3843 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3844 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3848 /* Move IT to the next line start.
3850 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3851 we skipped over part of the text (as opposed to moving the iterator
3852 continuously over the text). Otherwise, don't change the value
3853 of *SKIPPED_P.
3855 Newlines may come from buffer text, overlay strings, or strings
3856 displayed via the `display' property. That's the reason we can't
3857 simply use find_next_newline_no_quit.
3859 Note that this function may not skip over invisible text that is so
3860 because of text properties and immediately follows a newline. If
3861 it would, function reseat_at_next_visible_line_start, when called
3862 from set_iterator_to_next, would effectively make invisible
3863 characters following a newline part of the wrong glyph row, which
3864 leads to wrong cursor motion. */
3866 static int
3867 forward_to_next_line_start (it, skipped_p)
3868 struct it *it;
3869 int *skipped_p;
3871 int old_selective, newline_found_p, n;
3872 const int MAX_NEWLINE_DISTANCE = 500;
3874 /* If already on a newline, just consume it to avoid unintended
3875 skipping over invisible text below. */
3876 if (it->what == IT_CHARACTER
3877 && it->c == '\n'
3878 && CHARPOS (it->position) == IT_CHARPOS (*it))
3880 set_iterator_to_next (it, 0);
3881 it->c = 0;
3882 return 1;
3885 /* Don't handle selective display in the following. It's (a)
3886 unnecessary because it's done by the caller, and (b) leads to an
3887 infinite recursion because next_element_from_ellipsis indirectly
3888 calls this function. */
3889 old_selective = it->selective;
3890 it->selective = 0;
3892 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3893 from buffer text. */
3894 for (n = newline_found_p = 0;
3895 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3896 n += STRINGP (it->string) ? 0 : 1)
3898 if (!get_next_display_element (it))
3899 return 0;
3900 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3901 set_iterator_to_next (it, 0);
3904 /* If we didn't find a newline near enough, see if we can use a
3905 short-cut. */
3906 if (!newline_found_p)
3908 int start = IT_CHARPOS (*it);
3909 int limit = find_next_newline_no_quit (start, 1);
3910 Lisp_Object pos;
3912 xassert (!STRINGP (it->string));
3914 /* If there isn't any `display' property in sight, and no
3915 overlays, we can just use the position of the newline in
3916 buffer text. */
3917 if (it->stop_charpos >= limit
3918 || ((pos = Fnext_single_property_change (make_number (start),
3919 Qdisplay,
3920 Qnil, make_number (limit)),
3921 NILP (pos))
3922 && next_overlay_change (start) == ZV))
3924 IT_CHARPOS (*it) = limit;
3925 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3926 *skipped_p = newline_found_p = 1;
3928 else
3930 while (get_next_display_element (it)
3931 && !newline_found_p)
3933 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3934 set_iterator_to_next (it, 0);
3939 it->selective = old_selective;
3940 return newline_found_p;
3944 /* Set IT's current position to the previous visible line start. Skip
3945 invisible text that is so either due to text properties or due to
3946 selective display. Caution: this does not change IT->current_x and
3947 IT->hpos. */
3949 static void
3950 back_to_previous_visible_line_start (it)
3951 struct it *it;
3953 int visible_p = 0;
3955 /* Go back one newline if not on BEGV already. */
3956 if (IT_CHARPOS (*it) > BEGV)
3957 back_to_previous_line_start (it);
3959 /* Move over lines that are invisible because of selective display
3960 or text properties. */
3961 while (IT_CHARPOS (*it) > BEGV
3962 && !visible_p)
3964 visible_p = 1;
3966 /* If selective > 0, then lines indented more than that values
3967 are invisible. */
3968 if (it->selective > 0
3969 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3970 (double) it->selective)) /* iftc */
3971 visible_p = 0;
3972 else
3974 Lisp_Object prop;
3976 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3977 Qinvisible, it->window);
3978 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3979 visible_p = 0;
3982 /* Back one more newline if the current one is invisible. */
3983 if (!visible_p)
3984 back_to_previous_line_start (it);
3987 xassert (IT_CHARPOS (*it) >= BEGV);
3988 xassert (IT_CHARPOS (*it) == BEGV
3989 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3990 CHECK_IT (it);
3994 /* Reseat iterator IT at the previous visible line start. Skip
3995 invisible text that is so either due to text properties or due to
3996 selective display. At the end, update IT's overlay information,
3997 face information etc. */
3999 static void
4000 reseat_at_previous_visible_line_start (it)
4001 struct it *it;
4003 back_to_previous_visible_line_start (it);
4004 reseat (it, it->current.pos, 1);
4005 CHECK_IT (it);
4009 /* Reseat iterator IT on the next visible line start in the current
4010 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4011 preceding the line start. Skip over invisible text that is so
4012 because of selective display. Compute faces, overlays etc at the
4013 new position. Note that this function does not skip over text that
4014 is invisible because of text properties. */
4016 static void
4017 reseat_at_next_visible_line_start (it, on_newline_p)
4018 struct it *it;
4019 int on_newline_p;
4021 int newline_found_p, skipped_p = 0;
4023 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4025 /* Skip over lines that are invisible because they are indented
4026 more than the value of IT->selective. */
4027 if (it->selective > 0)
4028 while (IT_CHARPOS (*it) < ZV
4029 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4030 (double) it->selective)) /* iftc */
4032 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4033 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4036 /* Position on the newline if that's what's requested. */
4037 if (on_newline_p && newline_found_p)
4039 if (STRINGP (it->string))
4041 if (IT_STRING_CHARPOS (*it) > 0)
4043 --IT_STRING_CHARPOS (*it);
4044 --IT_STRING_BYTEPOS (*it);
4047 else if (IT_CHARPOS (*it) > BEGV)
4049 --IT_CHARPOS (*it);
4050 --IT_BYTEPOS (*it);
4051 reseat (it, it->current.pos, 0);
4054 else if (skipped_p)
4055 reseat (it, it->current.pos, 0);
4057 CHECK_IT (it);
4062 /***********************************************************************
4063 Changing an iterator's position
4064 ***********************************************************************/
4066 /* Change IT's current position to POS in current_buffer. If FORCE_P
4067 is non-zero, always check for text properties at the new position.
4068 Otherwise, text properties are only looked up if POS >=
4069 IT->check_charpos of a property. */
4071 static void
4072 reseat (it, pos, force_p)
4073 struct it *it;
4074 struct text_pos pos;
4075 int force_p;
4077 int original_pos = IT_CHARPOS (*it);
4079 reseat_1 (it, pos, 0);
4081 /* Determine where to check text properties. Avoid doing it
4082 where possible because text property lookup is very expensive. */
4083 if (force_p
4084 || CHARPOS (pos) > it->stop_charpos
4085 || CHARPOS (pos) < original_pos)
4086 handle_stop (it);
4088 CHECK_IT (it);
4092 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4093 IT->stop_pos to POS, also. */
4095 static void
4096 reseat_1 (it, pos, set_stop_p)
4097 struct it *it;
4098 struct text_pos pos;
4099 int set_stop_p;
4101 /* Don't call this function when scanning a C string. */
4102 xassert (it->s == NULL);
4104 /* POS must be a reasonable value. */
4105 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4107 it->current.pos = it->position = pos;
4108 XSETBUFFER (it->object, current_buffer);
4109 it->end_charpos = ZV;
4110 it->dpvec = NULL;
4111 it->current.dpvec_index = -1;
4112 it->current.overlay_string_index = -1;
4113 IT_STRING_CHARPOS (*it) = -1;
4114 IT_STRING_BYTEPOS (*it) = -1;
4115 it->string = Qnil;
4116 it->method = next_element_from_buffer;
4117 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4118 it->sp = 0;
4119 it->face_before_selective_p = 0;
4121 if (set_stop_p)
4122 it->stop_charpos = CHARPOS (pos);
4126 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4127 If S is non-null, it is a C string to iterate over. Otherwise,
4128 STRING gives a Lisp string to iterate over.
4130 If PRECISION > 0, don't return more then PRECISION number of
4131 characters from the string.
4133 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4134 characters have been returned. FIELD_WIDTH < 0 means an infinite
4135 field width.
4137 MULTIBYTE = 0 means disable processing of multibyte characters,
4138 MULTIBYTE > 0 means enable it,
4139 MULTIBYTE < 0 means use IT->multibyte_p.
4141 IT must be initialized via a prior call to init_iterator before
4142 calling this function. */
4144 static void
4145 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4146 struct it *it;
4147 unsigned char *s;
4148 Lisp_Object string;
4149 int charpos;
4150 int precision, field_width, multibyte;
4152 /* No region in strings. */
4153 it->region_beg_charpos = it->region_end_charpos = -1;
4155 /* No text property checks performed by default, but see below. */
4156 it->stop_charpos = -1;
4158 /* Set iterator position and end position. */
4159 bzero (&it->current, sizeof it->current);
4160 it->current.overlay_string_index = -1;
4161 it->current.dpvec_index = -1;
4162 xassert (charpos >= 0);
4164 /* If STRING is specified, use its multibyteness, otherwise use the
4165 setting of MULTIBYTE, if specified. */
4166 if (multibyte >= 0)
4167 it->multibyte_p = multibyte > 0;
4169 if (s == NULL)
4171 xassert (STRINGP (string));
4172 it->string = string;
4173 it->s = NULL;
4174 it->end_charpos = it->string_nchars = SCHARS (string);
4175 it->method = next_element_from_string;
4176 it->current.string_pos = string_pos (charpos, string);
4178 else
4180 it->s = s;
4181 it->string = Qnil;
4183 /* Note that we use IT->current.pos, not it->current.string_pos,
4184 for displaying C strings. */
4185 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4186 if (it->multibyte_p)
4188 it->current.pos = c_string_pos (charpos, s, 1);
4189 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4191 else
4193 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4194 it->end_charpos = it->string_nchars = strlen (s);
4197 it->method = next_element_from_c_string;
4200 /* PRECISION > 0 means don't return more than PRECISION characters
4201 from the string. */
4202 if (precision > 0 && it->end_charpos - charpos > precision)
4203 it->end_charpos = it->string_nchars = charpos + precision;
4205 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4206 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4207 FIELD_WIDTH < 0 means infinite field width. This is useful for
4208 padding with `-' at the end of a mode line. */
4209 if (field_width < 0)
4210 field_width = INFINITY;
4211 if (field_width > it->end_charpos - charpos)
4212 it->end_charpos = charpos + field_width;
4214 /* Use the standard display table for displaying strings. */
4215 if (DISP_TABLE_P (Vstandard_display_table))
4216 it->dp = XCHAR_TABLE (Vstandard_display_table);
4218 it->stop_charpos = charpos;
4219 CHECK_IT (it);
4224 /***********************************************************************
4225 Iteration
4226 ***********************************************************************/
4228 /* Load IT's display element fields with information about the next
4229 display element from the current position of IT. Value is zero if
4230 end of buffer (or C string) is reached. */
4233 get_next_display_element (it)
4234 struct it *it;
4236 /* Non-zero means that we found a display element. Zero means that
4237 we hit the end of what we iterate over. Performance note: the
4238 function pointer `method' used here turns out to be faster than
4239 using a sequence of if-statements. */
4240 int success_p = (*it->method) (it);
4242 if (it->what == IT_CHARACTER)
4244 /* Map via display table or translate control characters.
4245 IT->c, IT->len etc. have been set to the next character by
4246 the function call above. If we have a display table, and it
4247 contains an entry for IT->c, translate it. Don't do this if
4248 IT->c itself comes from a display table, otherwise we could
4249 end up in an infinite recursion. (An alternative could be to
4250 count the recursion depth of this function and signal an
4251 error when a certain maximum depth is reached.) Is it worth
4252 it? */
4253 if (success_p && it->dpvec == NULL)
4255 Lisp_Object dv;
4257 if (it->dp
4258 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4259 VECTORP (dv)))
4261 struct Lisp_Vector *v = XVECTOR (dv);
4263 /* Return the first character from the display table
4264 entry, if not empty. If empty, don't display the
4265 current character. */
4266 if (v->size)
4268 it->dpvec_char_len = it->len;
4269 it->dpvec = v->contents;
4270 it->dpend = v->contents + v->size;
4271 it->current.dpvec_index = 0;
4272 it->method = next_element_from_display_vector;
4273 success_p = get_next_display_element (it);
4275 else
4277 set_iterator_to_next (it, 0);
4278 success_p = get_next_display_element (it);
4282 /* Translate control characters into `\003' or `^C' form.
4283 Control characters coming from a display table entry are
4284 currently not translated because we use IT->dpvec to hold
4285 the translation. This could easily be changed but I
4286 don't believe that it is worth doing.
4288 If it->multibyte_p is nonzero, eight-bit characters and
4289 non-printable multibyte characters are also translated to
4290 octal form.
4292 If it->multibyte_p is zero, eight-bit characters that
4293 don't have corresponding multibyte char code are also
4294 translated to octal form. */
4295 else if ((it->c < ' '
4296 && (it->area != TEXT_AREA
4297 || (it->c != '\n' && it->c != '\t')))
4298 || (it->multibyte_p
4299 ? ((it->c >= 127
4300 && it->len == 1)
4301 || !CHAR_PRINTABLE_P (it->c))
4302 : (it->c >= 127
4303 && it->c == unibyte_char_to_multibyte (it->c))))
4305 /* IT->c is a control character which must be displayed
4306 either as '\003' or as `^C' where the '\\' and '^'
4307 can be defined in the display table. Fill
4308 IT->ctl_chars with glyphs for what we have to
4309 display. Then, set IT->dpvec to these glyphs. */
4310 GLYPH g;
4312 if (it->c < 128 && it->ctl_arrow_p)
4314 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4315 if (it->dp
4316 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4317 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4318 g = XINT (DISP_CTRL_GLYPH (it->dp));
4319 else
4320 g = FAST_MAKE_GLYPH ('^', 0);
4321 XSETINT (it->ctl_chars[0], g);
4323 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4324 XSETINT (it->ctl_chars[1], g);
4326 /* Set up IT->dpvec and return first character from it. */
4327 it->dpvec_char_len = it->len;
4328 it->dpvec = it->ctl_chars;
4329 it->dpend = it->dpvec + 2;
4330 it->current.dpvec_index = 0;
4331 it->method = next_element_from_display_vector;
4332 get_next_display_element (it);
4334 else
4336 unsigned char str[MAX_MULTIBYTE_LENGTH];
4337 int len;
4338 int i;
4339 GLYPH escape_glyph;
4341 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4342 if (it->dp
4343 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4344 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4345 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4346 else
4347 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4349 if (SINGLE_BYTE_CHAR_P (it->c))
4350 str[0] = it->c, len = 1;
4351 else
4353 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4354 if (len < 0)
4356 /* It's an invalid character, which
4357 shouldn't happen actually, but due to
4358 bugs it may happen. Let's print the char
4359 as is, there's not much meaningful we can
4360 do with it. */
4361 str[0] = it->c;
4362 str[1] = it->c >> 8;
4363 str[2] = it->c >> 16;
4364 str[3] = it->c >> 24;
4365 len = 4;
4369 for (i = 0; i < len; i++)
4371 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4372 /* Insert three more glyphs into IT->ctl_chars for
4373 the octal display of the character. */
4374 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4375 XSETINT (it->ctl_chars[i * 4 + 1], g);
4376 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4377 XSETINT (it->ctl_chars[i * 4 + 2], g);
4378 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4379 XSETINT (it->ctl_chars[i * 4 + 3], g);
4382 /* Set up IT->dpvec and return the first character
4383 from it. */
4384 it->dpvec_char_len = it->len;
4385 it->dpvec = it->ctl_chars;
4386 it->dpend = it->dpvec + len * 4;
4387 it->current.dpvec_index = 0;
4388 it->method = next_element_from_display_vector;
4389 get_next_display_element (it);
4394 /* Adjust face id for a multibyte character. There are no
4395 multibyte character in unibyte text. */
4396 if (it->multibyte_p
4397 && success_p
4398 && FRAME_WINDOW_P (it->f))
4400 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4401 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4405 /* Is this character the last one of a run of characters with
4406 box? If yes, set IT->end_of_box_run_p to 1. */
4407 if (it->face_box_p
4408 && it->s == NULL)
4410 int face_id;
4411 struct face *face;
4413 it->end_of_box_run_p
4414 = ((face_id = face_after_it_pos (it),
4415 face_id != it->face_id)
4416 && (face = FACE_FROM_ID (it->f, face_id),
4417 face->box == FACE_NO_BOX));
4420 /* Value is 0 if end of buffer or string reached. */
4421 return success_p;
4425 /* Move IT to the next display element.
4427 RESEAT_P non-zero means if called on a newline in buffer text,
4428 skip to the next visible line start.
4430 Functions get_next_display_element and set_iterator_to_next are
4431 separate because I find this arrangement easier to handle than a
4432 get_next_display_element function that also increments IT's
4433 position. The way it is we can first look at an iterator's current
4434 display element, decide whether it fits on a line, and if it does,
4435 increment the iterator position. The other way around we probably
4436 would either need a flag indicating whether the iterator has to be
4437 incremented the next time, or we would have to implement a
4438 decrement position function which would not be easy to write. */
4440 void
4441 set_iterator_to_next (it, reseat_p)
4442 struct it *it;
4443 int reseat_p;
4445 /* Reset flags indicating start and end of a sequence of characters
4446 with box. Reset them at the start of this function because
4447 moving the iterator to a new position might set them. */
4448 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4450 if (it->method == next_element_from_buffer)
4452 /* The current display element of IT is a character from
4453 current_buffer. Advance in the buffer, and maybe skip over
4454 invisible lines that are so because of selective display. */
4455 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4456 reseat_at_next_visible_line_start (it, 0);
4457 else
4459 xassert (it->len != 0);
4460 IT_BYTEPOS (*it) += it->len;
4461 IT_CHARPOS (*it) += 1;
4462 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4465 else if (it->method == next_element_from_composition)
4467 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4468 if (STRINGP (it->string))
4470 IT_STRING_BYTEPOS (*it) += it->len;
4471 IT_STRING_CHARPOS (*it) += it->cmp_len;
4472 it->method = next_element_from_string;
4473 goto consider_string_end;
4475 else
4477 IT_BYTEPOS (*it) += it->len;
4478 IT_CHARPOS (*it) += it->cmp_len;
4479 it->method = next_element_from_buffer;
4482 else if (it->method == next_element_from_c_string)
4484 /* Current display element of IT is from a C string. */
4485 IT_BYTEPOS (*it) += it->len;
4486 IT_CHARPOS (*it) += 1;
4488 else if (it->method == next_element_from_display_vector)
4490 /* Current display element of IT is from a display table entry.
4491 Advance in the display table definition. Reset it to null if
4492 end reached, and continue with characters from buffers/
4493 strings. */
4494 ++it->current.dpvec_index;
4496 /* Restore face of the iterator to what they were before the
4497 display vector entry (these entries may contain faces). */
4498 it->face_id = it->saved_face_id;
4500 if (it->dpvec + it->current.dpvec_index == it->dpend)
4502 if (it->s)
4503 it->method = next_element_from_c_string;
4504 else if (STRINGP (it->string))
4505 it->method = next_element_from_string;
4506 else
4507 it->method = next_element_from_buffer;
4509 it->dpvec = NULL;
4510 it->current.dpvec_index = -1;
4512 /* Skip over characters which were displayed via IT->dpvec. */
4513 if (it->dpvec_char_len < 0)
4514 reseat_at_next_visible_line_start (it, 1);
4515 else if (it->dpvec_char_len > 0)
4517 it->len = it->dpvec_char_len;
4518 set_iterator_to_next (it, reseat_p);
4522 else if (it->method == next_element_from_string)
4524 /* Current display element is a character from a Lisp string. */
4525 xassert (it->s == NULL && STRINGP (it->string));
4526 IT_STRING_BYTEPOS (*it) += it->len;
4527 IT_STRING_CHARPOS (*it) += 1;
4529 consider_string_end:
4531 if (it->current.overlay_string_index >= 0)
4533 /* IT->string is an overlay string. Advance to the
4534 next, if there is one. */
4535 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4536 next_overlay_string (it);
4538 else
4540 /* IT->string is not an overlay string. If we reached
4541 its end, and there is something on IT->stack, proceed
4542 with what is on the stack. This can be either another
4543 string, this time an overlay string, or a buffer. */
4544 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4545 && it->sp > 0)
4547 pop_it (it);
4548 if (!STRINGP (it->string))
4549 it->method = next_element_from_buffer;
4550 else
4551 goto consider_string_end;
4555 else if (it->method == next_element_from_image
4556 || it->method == next_element_from_stretch)
4558 /* The position etc with which we have to proceed are on
4559 the stack. The position may be at the end of a string,
4560 if the `display' property takes up the whole string. */
4561 pop_it (it);
4562 it->image_id = 0;
4563 if (STRINGP (it->string))
4565 it->method = next_element_from_string;
4566 goto consider_string_end;
4568 else
4569 it->method = next_element_from_buffer;
4571 else
4572 /* There are no other methods defined, so this should be a bug. */
4573 abort ();
4575 xassert (it->method != next_element_from_string
4576 || (STRINGP (it->string)
4577 && IT_STRING_CHARPOS (*it) >= 0));
4581 /* Load IT's display element fields with information about the next
4582 display element which comes from a display table entry or from the
4583 result of translating a control character to one of the forms `^C'
4584 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4586 static int
4587 next_element_from_display_vector (it)
4588 struct it *it;
4590 /* Precondition. */
4591 xassert (it->dpvec && it->current.dpvec_index >= 0);
4593 /* Remember the current face id in case glyphs specify faces.
4594 IT's face is restored in set_iterator_to_next. */
4595 it->saved_face_id = it->face_id;
4597 if (INTEGERP (*it->dpvec)
4598 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4600 int lface_id;
4601 GLYPH g;
4603 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4604 it->c = FAST_GLYPH_CHAR (g);
4605 it->len = CHAR_BYTES (it->c);
4607 /* The entry may contain a face id to use. Such a face id is
4608 the id of a Lisp face, not a realized face. A face id of
4609 zero means no face is specified. */
4610 lface_id = FAST_GLYPH_FACE (g);
4611 if (lface_id)
4613 /* The function returns -1 if lface_id is invalid. */
4614 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4615 if (face_id >= 0)
4616 it->face_id = face_id;
4619 else
4620 /* Display table entry is invalid. Return a space. */
4621 it->c = ' ', it->len = 1;
4623 /* Don't change position and object of the iterator here. They are
4624 still the values of the character that had this display table
4625 entry or was translated, and that's what we want. */
4626 it->what = IT_CHARACTER;
4627 return 1;
4631 /* Load IT with the next display element from Lisp string IT->string.
4632 IT->current.string_pos is the current position within the string.
4633 If IT->current.overlay_string_index >= 0, the Lisp string is an
4634 overlay string. */
4636 static int
4637 next_element_from_string (it)
4638 struct it *it;
4640 struct text_pos position;
4642 xassert (STRINGP (it->string));
4643 xassert (IT_STRING_CHARPOS (*it) >= 0);
4644 position = it->current.string_pos;
4646 /* Time to check for invisible text? */
4647 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4648 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4650 handle_stop (it);
4652 /* Since a handler may have changed IT->method, we must
4653 recurse here. */
4654 return get_next_display_element (it);
4657 if (it->current.overlay_string_index >= 0)
4659 /* Get the next character from an overlay string. In overlay
4660 strings, There is no field width or padding with spaces to
4661 do. */
4662 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4664 it->what = IT_EOB;
4665 return 0;
4667 else if (STRING_MULTIBYTE (it->string))
4669 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4670 const unsigned char *s = (SDATA (it->string)
4671 + IT_STRING_BYTEPOS (*it));
4672 it->c = string_char_and_length (s, remaining, &it->len);
4674 else
4676 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4677 it->len = 1;
4680 else
4682 /* Get the next character from a Lisp string that is not an
4683 overlay string. Such strings come from the mode line, for
4684 example. We may have to pad with spaces, or truncate the
4685 string. See also next_element_from_c_string. */
4686 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4688 it->what = IT_EOB;
4689 return 0;
4691 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4693 /* Pad with spaces. */
4694 it->c = ' ', it->len = 1;
4695 CHARPOS (position) = BYTEPOS (position) = -1;
4697 else if (STRING_MULTIBYTE (it->string))
4699 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4700 const unsigned char *s = (SDATA (it->string)
4701 + IT_STRING_BYTEPOS (*it));
4702 it->c = string_char_and_length (s, maxlen, &it->len);
4704 else
4706 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4707 it->len = 1;
4711 /* Record what we have and where it came from. Note that we store a
4712 buffer position in IT->position although it could arguably be a
4713 string position. */
4714 it->what = IT_CHARACTER;
4715 it->object = it->string;
4716 it->position = position;
4717 return 1;
4721 /* Load IT with next display element from C string IT->s.
4722 IT->string_nchars is the maximum number of characters to return
4723 from the string. IT->end_charpos may be greater than
4724 IT->string_nchars when this function is called, in which case we
4725 may have to return padding spaces. Value is zero if end of string
4726 reached, including padding spaces. */
4728 static int
4729 next_element_from_c_string (it)
4730 struct it *it;
4732 int success_p = 1;
4734 xassert (it->s);
4735 it->what = IT_CHARACTER;
4736 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4737 it->object = Qnil;
4739 /* IT's position can be greater IT->string_nchars in case a field
4740 width or precision has been specified when the iterator was
4741 initialized. */
4742 if (IT_CHARPOS (*it) >= it->end_charpos)
4744 /* End of the game. */
4745 it->what = IT_EOB;
4746 success_p = 0;
4748 else if (IT_CHARPOS (*it) >= it->string_nchars)
4750 /* Pad with spaces. */
4751 it->c = ' ', it->len = 1;
4752 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4754 else if (it->multibyte_p)
4756 /* Implementation note: The calls to strlen apparently aren't a
4757 performance problem because there is no noticeable performance
4758 difference between Emacs running in unibyte or multibyte mode. */
4759 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4760 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4761 maxlen, &it->len);
4763 else
4764 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4766 return success_p;
4770 /* Set up IT to return characters from an ellipsis, if appropriate.
4771 The definition of the ellipsis glyphs may come from a display table
4772 entry. This function Fills IT with the first glyph from the
4773 ellipsis if an ellipsis is to be displayed. */
4775 static int
4776 next_element_from_ellipsis (it)
4777 struct it *it;
4779 if (it->selective_display_ellipsis_p)
4781 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4783 /* Use the display table definition for `...'. Invalid glyphs
4784 will be handled by the method returning elements from dpvec. */
4785 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4786 it->dpvec_char_len = it->len;
4787 it->dpvec = v->contents;
4788 it->dpend = v->contents + v->size;
4789 it->current.dpvec_index = 0;
4790 it->method = next_element_from_display_vector;
4792 else
4794 /* Use default `...' which is stored in default_invis_vector. */
4795 it->dpvec_char_len = it->len;
4796 it->dpvec = default_invis_vector;
4797 it->dpend = default_invis_vector + 3;
4798 it->current.dpvec_index = 0;
4799 it->method = next_element_from_display_vector;
4802 else
4804 /* The face at the current position may be different from the
4805 face we find after the invisible text. Remember what it
4806 was in IT->saved_face_id, and signal that it's there by
4807 setting face_before_selective_p. */
4808 it->saved_face_id = it->face_id;
4809 it->method = next_element_from_buffer;
4810 reseat_at_next_visible_line_start (it, 1);
4811 it->face_before_selective_p = 1;
4814 return get_next_display_element (it);
4818 /* Deliver an image display element. The iterator IT is already
4819 filled with image information (done in handle_display_prop). Value
4820 is always 1. */
4823 static int
4824 next_element_from_image (it)
4825 struct it *it;
4827 it->what = IT_IMAGE;
4828 return 1;
4832 /* Fill iterator IT with next display element from a stretch glyph
4833 property. IT->object is the value of the text property. Value is
4834 always 1. */
4836 static int
4837 next_element_from_stretch (it)
4838 struct it *it;
4840 it->what = IT_STRETCH;
4841 return 1;
4845 /* Load IT with the next display element from current_buffer. Value
4846 is zero if end of buffer reached. IT->stop_charpos is the next
4847 position at which to stop and check for text properties or buffer
4848 end. */
4850 static int
4851 next_element_from_buffer (it)
4852 struct it *it;
4854 int success_p = 1;
4856 /* Check this assumption, otherwise, we would never enter the
4857 if-statement, below. */
4858 xassert (IT_CHARPOS (*it) >= BEGV
4859 && IT_CHARPOS (*it) <= it->stop_charpos);
4861 if (IT_CHARPOS (*it) >= it->stop_charpos)
4863 if (IT_CHARPOS (*it) >= it->end_charpos)
4865 int overlay_strings_follow_p;
4867 /* End of the game, except when overlay strings follow that
4868 haven't been returned yet. */
4869 if (it->overlay_strings_at_end_processed_p)
4870 overlay_strings_follow_p = 0;
4871 else
4873 it->overlay_strings_at_end_processed_p = 1;
4874 overlay_strings_follow_p = get_overlay_strings (it, 0);
4877 if (overlay_strings_follow_p)
4878 success_p = get_next_display_element (it);
4879 else
4881 it->what = IT_EOB;
4882 it->position = it->current.pos;
4883 success_p = 0;
4886 else
4888 handle_stop (it);
4889 return get_next_display_element (it);
4892 else
4894 /* No face changes, overlays etc. in sight, so just return a
4895 character from current_buffer. */
4896 unsigned char *p;
4898 /* Maybe run the redisplay end trigger hook. Performance note:
4899 This doesn't seem to cost measurable time. */
4900 if (it->redisplay_end_trigger_charpos
4901 && it->glyph_row
4902 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4903 run_redisplay_end_trigger_hook (it);
4905 /* Get the next character, maybe multibyte. */
4906 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4907 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4909 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4910 - IT_BYTEPOS (*it));
4911 it->c = string_char_and_length (p, maxlen, &it->len);
4913 else
4914 it->c = *p, it->len = 1;
4916 /* Record what we have and where it came from. */
4917 it->what = IT_CHARACTER;;
4918 it->object = it->w->buffer;
4919 it->position = it->current.pos;
4921 /* Normally we return the character found above, except when we
4922 really want to return an ellipsis for selective display. */
4923 if (it->selective)
4925 if (it->c == '\n')
4927 /* A value of selective > 0 means hide lines indented more
4928 than that number of columns. */
4929 if (it->selective > 0
4930 && IT_CHARPOS (*it) + 1 < ZV
4931 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4932 IT_BYTEPOS (*it) + 1,
4933 (double) it->selective)) /* iftc */
4935 success_p = next_element_from_ellipsis (it);
4936 it->dpvec_char_len = -1;
4939 else if (it->c == '\r' && it->selective == -1)
4941 /* A value of selective == -1 means that everything from the
4942 CR to the end of the line is invisible, with maybe an
4943 ellipsis displayed for it. */
4944 success_p = next_element_from_ellipsis (it);
4945 it->dpvec_char_len = -1;
4950 /* Value is zero if end of buffer reached. */
4951 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4952 return success_p;
4956 /* Run the redisplay end trigger hook for IT. */
4958 static void
4959 run_redisplay_end_trigger_hook (it)
4960 struct it *it;
4962 Lisp_Object args[3];
4964 /* IT->glyph_row should be non-null, i.e. we should be actually
4965 displaying something, or otherwise we should not run the hook. */
4966 xassert (it->glyph_row);
4968 /* Set up hook arguments. */
4969 args[0] = Qredisplay_end_trigger_functions;
4970 args[1] = it->window;
4971 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4972 it->redisplay_end_trigger_charpos = 0;
4974 /* Since we are *trying* to run these functions, don't try to run
4975 them again, even if they get an error. */
4976 it->w->redisplay_end_trigger = Qnil;
4977 Frun_hook_with_args (3, args);
4979 /* Notice if it changed the face of the character we are on. */
4980 handle_face_prop (it);
4984 /* Deliver a composition display element. The iterator IT is already
4985 filled with composition information (done in
4986 handle_composition_prop). Value is always 1. */
4988 static int
4989 next_element_from_composition (it)
4990 struct it *it;
4992 it->what = IT_COMPOSITION;
4993 it->position = (STRINGP (it->string)
4994 ? it->current.string_pos
4995 : it->current.pos);
4996 return 1;
5001 /***********************************************************************
5002 Moving an iterator without producing glyphs
5003 ***********************************************************************/
5005 /* Move iterator IT to a specified buffer or X position within one
5006 line on the display without producing glyphs.
5008 OP should be a bit mask including some or all of these bits:
5009 MOVE_TO_X: Stop on reaching x-position TO_X.
5010 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5011 Regardless of OP's value, stop in reaching the end of the display line.
5013 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5014 This means, in particular, that TO_X includes window's horizontal
5015 scroll amount.
5017 The return value has several possible values that
5018 say what condition caused the scan to stop:
5020 MOVE_POS_MATCH_OR_ZV
5021 - when TO_POS or ZV was reached.
5023 MOVE_X_REACHED
5024 -when TO_X was reached before TO_POS or ZV were reached.
5026 MOVE_LINE_CONTINUED
5027 - when we reached the end of the display area and the line must
5028 be continued.
5030 MOVE_LINE_TRUNCATED
5031 - when we reached the end of the display area and the line is
5032 truncated.
5034 MOVE_NEWLINE_OR_CR
5035 - when we stopped at a line end, i.e. a newline or a CR and selective
5036 display is on. */
5038 static enum move_it_result
5039 move_it_in_display_line_to (it, to_charpos, to_x, op)
5040 struct it *it;
5041 int to_charpos, to_x, op;
5043 enum move_it_result result = MOVE_UNDEFINED;
5044 struct glyph_row *saved_glyph_row;
5046 /* Don't produce glyphs in produce_glyphs. */
5047 saved_glyph_row = it->glyph_row;
5048 it->glyph_row = NULL;
5050 while (1)
5052 int x, i, ascent = 0, descent = 0;
5054 /* Stop when ZV or TO_CHARPOS reached. */
5055 if (!get_next_display_element (it)
5056 || ((op & MOVE_TO_POS) != 0
5057 && BUFFERP (it->object)
5058 && IT_CHARPOS (*it) >= to_charpos))
5060 result = MOVE_POS_MATCH_OR_ZV;
5061 break;
5064 /* The call to produce_glyphs will get the metrics of the
5065 display element IT is loaded with. We record in x the
5066 x-position before this display element in case it does not
5067 fit on the line. */
5068 x = it->current_x;
5070 /* Remember the line height so far in case the next element doesn't
5071 fit on the line. */
5072 if (!it->truncate_lines_p)
5074 ascent = it->max_ascent;
5075 descent = it->max_descent;
5078 PRODUCE_GLYPHS (it);
5080 if (it->area != TEXT_AREA)
5082 set_iterator_to_next (it, 1);
5083 continue;
5086 /* The number of glyphs we get back in IT->nglyphs will normally
5087 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5088 character on a terminal frame, or (iii) a line end. For the
5089 second case, IT->nglyphs - 1 padding glyphs will be present
5090 (on X frames, there is only one glyph produced for a
5091 composite character.
5093 The behavior implemented below means, for continuation lines,
5094 that as many spaces of a TAB as fit on the current line are
5095 displayed there. For terminal frames, as many glyphs of a
5096 multi-glyph character are displayed in the current line, too.
5097 This is what the old redisplay code did, and we keep it that
5098 way. Under X, the whole shape of a complex character must
5099 fit on the line or it will be completely displayed in the
5100 next line.
5102 Note that both for tabs and padding glyphs, all glyphs have
5103 the same width. */
5104 if (it->nglyphs)
5106 /* More than one glyph or glyph doesn't fit on line. All
5107 glyphs have the same width. */
5108 int single_glyph_width = it->pixel_width / it->nglyphs;
5109 int new_x;
5111 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5113 new_x = x + single_glyph_width;
5115 /* We want to leave anything reaching TO_X to the caller. */
5116 if ((op & MOVE_TO_X) && new_x > to_x)
5118 it->current_x = x;
5119 result = MOVE_X_REACHED;
5120 break;
5122 else if (/* Lines are continued. */
5123 !it->truncate_lines_p
5124 && (/* And glyph doesn't fit on the line. */
5125 new_x > it->last_visible_x
5126 /* Or it fits exactly and we're on a window
5127 system frame. */
5128 || (new_x == it->last_visible_x
5129 && FRAME_WINDOW_P (it->f))))
5131 if (/* IT->hpos == 0 means the very first glyph
5132 doesn't fit on the line, e.g. a wide image. */
5133 it->hpos == 0
5134 || (new_x == it->last_visible_x
5135 && FRAME_WINDOW_P (it->f)))
5137 ++it->hpos;
5138 it->current_x = new_x;
5139 if (i == it->nglyphs - 1)
5140 set_iterator_to_next (it, 1);
5142 else
5144 it->current_x = x;
5145 it->max_ascent = ascent;
5146 it->max_descent = descent;
5149 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5150 IT_CHARPOS (*it)));
5151 result = MOVE_LINE_CONTINUED;
5152 break;
5154 else if (new_x > it->first_visible_x)
5156 /* Glyph is visible. Increment number of glyphs that
5157 would be displayed. */
5158 ++it->hpos;
5160 else
5162 /* Glyph is completely off the left margin of the display
5163 area. Nothing to do. */
5167 if (result != MOVE_UNDEFINED)
5168 break;
5170 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5172 /* Stop when TO_X specified and reached. This check is
5173 necessary here because of lines consisting of a line end,
5174 only. The line end will not produce any glyphs and we
5175 would never get MOVE_X_REACHED. */
5176 xassert (it->nglyphs == 0);
5177 result = MOVE_X_REACHED;
5178 break;
5181 /* Is this a line end? If yes, we're done. */
5182 if (ITERATOR_AT_END_OF_LINE_P (it))
5184 result = MOVE_NEWLINE_OR_CR;
5185 break;
5188 /* The current display element has been consumed. Advance
5189 to the next. */
5190 set_iterator_to_next (it, 1);
5192 /* Stop if lines are truncated and IT's current x-position is
5193 past the right edge of the window now. */
5194 if (it->truncate_lines_p
5195 && it->current_x >= it->last_visible_x)
5197 result = MOVE_LINE_TRUNCATED;
5198 break;
5202 /* Restore the iterator settings altered at the beginning of this
5203 function. */
5204 it->glyph_row = saved_glyph_row;
5205 return result;
5209 /* Move IT forward until it satisfies one or more of the criteria in
5210 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5212 OP is a bit-mask that specifies where to stop, and in particular,
5213 which of those four position arguments makes a difference. See the
5214 description of enum move_operation_enum.
5216 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5217 screen line, this function will set IT to the next position >
5218 TO_CHARPOS. */
5220 void
5221 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5222 struct it *it;
5223 int to_charpos, to_x, to_y, to_vpos;
5224 int op;
5226 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5227 int line_height;
5228 int reached = 0;
5230 for (;;)
5232 if (op & MOVE_TO_VPOS)
5234 /* If no TO_CHARPOS and no TO_X specified, stop at the
5235 start of the line TO_VPOS. */
5236 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5238 if (it->vpos == to_vpos)
5240 reached = 1;
5241 break;
5243 else
5244 skip = move_it_in_display_line_to (it, -1, -1, 0);
5246 else
5248 /* TO_VPOS >= 0 means stop at TO_X in the line at
5249 TO_VPOS, or at TO_POS, whichever comes first. */
5250 if (it->vpos == to_vpos)
5252 reached = 2;
5253 break;
5256 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5258 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5260 reached = 3;
5261 break;
5263 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5265 /* We have reached TO_X but not in the line we want. */
5266 skip = move_it_in_display_line_to (it, to_charpos,
5267 -1, MOVE_TO_POS);
5268 if (skip == MOVE_POS_MATCH_OR_ZV)
5270 reached = 4;
5271 break;
5276 else if (op & MOVE_TO_Y)
5278 struct it it_backup;
5280 /* TO_Y specified means stop at TO_X in the line containing
5281 TO_Y---or at TO_CHARPOS if this is reached first. The
5282 problem is that we can't really tell whether the line
5283 contains TO_Y before we have completely scanned it, and
5284 this may skip past TO_X. What we do is to first scan to
5285 TO_X.
5287 If TO_X is not specified, use a TO_X of zero. The reason
5288 is to make the outcome of this function more predictable.
5289 If we didn't use TO_X == 0, we would stop at the end of
5290 the line which is probably not what a caller would expect
5291 to happen. */
5292 skip = move_it_in_display_line_to (it, to_charpos,
5293 ((op & MOVE_TO_X)
5294 ? to_x : 0),
5295 (MOVE_TO_X
5296 | (op & MOVE_TO_POS)));
5298 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5299 if (skip == MOVE_POS_MATCH_OR_ZV)
5301 reached = 5;
5302 break;
5305 /* If TO_X was reached, we would like to know whether TO_Y
5306 is in the line. This can only be said if we know the
5307 total line height which requires us to scan the rest of
5308 the line. */
5309 if (skip == MOVE_X_REACHED)
5311 it_backup = *it;
5312 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5313 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5314 op & MOVE_TO_POS);
5315 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5318 /* Now, decide whether TO_Y is in this line. */
5319 line_height = it->max_ascent + it->max_descent;
5320 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5322 if (to_y >= it->current_y
5323 && to_y < it->current_y + line_height)
5325 if (skip == MOVE_X_REACHED)
5326 /* If TO_Y is in this line and TO_X was reached above,
5327 we scanned too far. We have to restore IT's settings
5328 to the ones before skipping. */
5329 *it = it_backup;
5330 reached = 6;
5332 else if (skip == MOVE_X_REACHED)
5334 skip = skip2;
5335 if (skip == MOVE_POS_MATCH_OR_ZV)
5336 reached = 7;
5339 if (reached)
5340 break;
5342 else
5343 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5345 switch (skip)
5347 case MOVE_POS_MATCH_OR_ZV:
5348 reached = 8;
5349 goto out;
5351 case MOVE_NEWLINE_OR_CR:
5352 set_iterator_to_next (it, 1);
5353 it->continuation_lines_width = 0;
5354 break;
5356 case MOVE_LINE_TRUNCATED:
5357 it->continuation_lines_width = 0;
5358 reseat_at_next_visible_line_start (it, 0);
5359 if ((op & MOVE_TO_POS) != 0
5360 && IT_CHARPOS (*it) > to_charpos)
5362 reached = 9;
5363 goto out;
5365 break;
5367 case MOVE_LINE_CONTINUED:
5368 it->continuation_lines_width += it->current_x;
5369 break;
5371 default:
5372 abort ();
5375 /* Reset/increment for the next run. */
5376 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5377 it->current_x = it->hpos = 0;
5378 it->current_y += it->max_ascent + it->max_descent;
5379 ++it->vpos;
5380 last_height = it->max_ascent + it->max_descent;
5381 last_max_ascent = it->max_ascent;
5382 it->max_ascent = it->max_descent = 0;
5385 out:
5387 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5391 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5393 If DY > 0, move IT backward at least that many pixels. DY = 0
5394 means move IT backward to the preceding line start or BEGV. This
5395 function may move over more than DY pixels if IT->current_y - DY
5396 ends up in the middle of a line; in this case IT->current_y will be
5397 set to the top of the line moved to. */
5399 void
5400 move_it_vertically_backward (it, dy)
5401 struct it *it;
5402 int dy;
5404 int nlines, h;
5405 struct it it2, it3;
5406 int start_pos = IT_CHARPOS (*it);
5408 xassert (dy >= 0);
5410 /* Estimate how many newlines we must move back. */
5411 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5413 /* Set the iterator's position that many lines back. */
5414 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5415 back_to_previous_visible_line_start (it);
5417 /* Reseat the iterator here. When moving backward, we don't want
5418 reseat to skip forward over invisible text, set up the iterator
5419 to deliver from overlay strings at the new position etc. So,
5420 use reseat_1 here. */
5421 reseat_1 (it, it->current.pos, 1);
5423 /* We are now surely at a line start. */
5424 it->current_x = it->hpos = 0;
5425 it->continuation_lines_width = 0;
5427 /* Move forward and see what y-distance we moved. First move to the
5428 start of the next line so that we get its height. We need this
5429 height to be able to tell whether we reached the specified
5430 y-distance. */
5431 it2 = *it;
5432 it2.max_ascent = it2.max_descent = 0;
5433 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5434 MOVE_TO_POS | MOVE_TO_VPOS);
5435 xassert (IT_CHARPOS (*it) >= BEGV);
5436 it3 = it2;
5438 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5439 xassert (IT_CHARPOS (*it) >= BEGV);
5440 h = it2.current_y - it->current_y;
5441 nlines = it2.vpos - it->vpos;
5443 /* Correct IT's y and vpos position. */
5444 it->vpos -= nlines;
5445 it->current_y -= h;
5447 if (dy == 0)
5449 /* DY == 0 means move to the start of the screen line. The
5450 value of nlines is > 0 if continuation lines were involved. */
5451 if (nlines > 0)
5452 move_it_by_lines (it, nlines, 1);
5453 xassert (IT_CHARPOS (*it) <= start_pos);
5455 else if (nlines)
5457 /* The y-position we try to reach. Note that h has been
5458 subtracted in front of the if-statement. */
5459 int target_y = it->current_y + h - dy;
5460 int y0 = it3.current_y;
5461 int y1 = line_bottom_y (&it3);
5462 int line_height = y1 - y0;
5464 /* If we did not reach target_y, try to move further backward if
5465 we can. If we moved too far backward, try to move forward. */
5466 if (target_y < it->current_y
5467 /* This is heuristic. In a window that's 3 lines high, with
5468 a line height of 13 pixels each, recentering with point
5469 on the bottom line will try to move -39/2 = 19 pixels
5470 backward. Try to avoid moving into the first line. */
5471 && it->current_y - target_y > line_height / 3 * 2
5472 && IT_CHARPOS (*it) > BEGV)
5474 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5475 target_y - it->current_y));
5476 move_it_vertically (it, target_y - it->current_y);
5477 xassert (IT_CHARPOS (*it) >= BEGV);
5479 else if (target_y >= it->current_y + line_height
5480 && IT_CHARPOS (*it) < ZV)
5482 /* Should move forward by at least one line, maybe more.
5484 Note: Calling move_it_by_lines can be expensive on
5485 terminal frames, where compute_motion is used (via
5486 vmotion) to do the job, when there are very long lines
5487 and truncate-lines is nil. That's the reason for
5488 treating terminal frames specially here. */
5490 if (!FRAME_WINDOW_P (it->f))
5491 move_it_vertically (it, target_y - (it->current_y + line_height));
5492 else
5496 move_it_by_lines (it, 1, 1);
5498 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5501 xassert (IT_CHARPOS (*it) >= BEGV);
5507 /* Move IT by a specified amount of pixel lines DY. DY negative means
5508 move backwards. DY = 0 means move to start of screen line. At the
5509 end, IT will be on the start of a screen line. */
5511 void
5512 move_it_vertically (it, dy)
5513 struct it *it;
5514 int dy;
5516 if (dy <= 0)
5517 move_it_vertically_backward (it, -dy);
5518 else if (dy > 0)
5520 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5521 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5522 MOVE_TO_POS | MOVE_TO_Y);
5523 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5525 /* If buffer ends in ZV without a newline, move to the start of
5526 the line to satisfy the post-condition. */
5527 if (IT_CHARPOS (*it) == ZV
5528 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5529 move_it_by_lines (it, 0, 0);
5534 /* Move iterator IT past the end of the text line it is in. */
5536 void
5537 move_it_past_eol (it)
5538 struct it *it;
5540 enum move_it_result rc;
5542 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5543 if (rc == MOVE_NEWLINE_OR_CR)
5544 set_iterator_to_next (it, 0);
5548 #if 0 /* Currently not used. */
5550 /* Return non-zero if some text between buffer positions START_CHARPOS
5551 and END_CHARPOS is invisible. IT->window is the window for text
5552 property lookup. */
5554 static int
5555 invisible_text_between_p (it, start_charpos, end_charpos)
5556 struct it *it;
5557 int start_charpos, end_charpos;
5559 Lisp_Object prop, limit;
5560 int invisible_found_p;
5562 xassert (it != NULL && start_charpos <= end_charpos);
5564 /* Is text at START invisible? */
5565 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5566 it->window);
5567 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5568 invisible_found_p = 1;
5569 else
5571 limit = Fnext_single_char_property_change (make_number (start_charpos),
5572 Qinvisible, Qnil,
5573 make_number (end_charpos));
5574 invisible_found_p = XFASTINT (limit) < end_charpos;
5577 return invisible_found_p;
5580 #endif /* 0 */
5583 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5584 negative means move up. DVPOS == 0 means move to the start of the
5585 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5586 NEED_Y_P is zero, IT->current_y will be left unchanged.
5588 Further optimization ideas: If we would know that IT->f doesn't use
5589 a face with proportional font, we could be faster for
5590 truncate-lines nil. */
5592 void
5593 move_it_by_lines (it, dvpos, need_y_p)
5594 struct it *it;
5595 int dvpos, need_y_p;
5597 struct position pos;
5599 if (!FRAME_WINDOW_P (it->f))
5601 struct text_pos textpos;
5603 /* We can use vmotion on frames without proportional fonts. */
5604 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5605 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5606 reseat (it, textpos, 1);
5607 it->vpos += pos.vpos;
5608 it->current_y += pos.vpos;
5610 else if (dvpos == 0)
5612 /* DVPOS == 0 means move to the start of the screen line. */
5613 move_it_vertically_backward (it, 0);
5614 xassert (it->current_x == 0 && it->hpos == 0);
5616 else if (dvpos > 0)
5617 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5618 else
5620 struct it it2;
5621 int start_charpos, i;
5623 /* Start at the beginning of the screen line containing IT's
5624 position. */
5625 move_it_vertically_backward (it, 0);
5627 /* Go back -DVPOS visible lines and reseat the iterator there. */
5628 start_charpos = IT_CHARPOS (*it);
5629 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5630 back_to_previous_visible_line_start (it);
5631 reseat (it, it->current.pos, 1);
5632 it->current_x = it->hpos = 0;
5634 /* Above call may have moved too far if continuation lines
5635 are involved. Scan forward and see if it did. */
5636 it2 = *it;
5637 it2.vpos = it2.current_y = 0;
5638 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5639 it->vpos -= it2.vpos;
5640 it->current_y -= it2.current_y;
5641 it->current_x = it->hpos = 0;
5643 /* If we moved too far, move IT some lines forward. */
5644 if (it2.vpos > -dvpos)
5646 int delta = it2.vpos + dvpos;
5647 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5654 /***********************************************************************
5655 Messages
5656 ***********************************************************************/
5659 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5660 to *Messages*. */
5662 void
5663 add_to_log (format, arg1, arg2)
5664 char *format;
5665 Lisp_Object arg1, arg2;
5667 Lisp_Object args[3];
5668 Lisp_Object msg, fmt;
5669 char *buffer;
5670 int len;
5671 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5673 /* Do nothing if called asynchronously. Inserting text into
5674 a buffer may call after-change-functions and alike and
5675 that would means running Lisp asynchronously. */
5676 if (handling_signal)
5677 return;
5679 fmt = msg = Qnil;
5680 GCPRO4 (fmt, msg, arg1, arg2);
5682 args[0] = fmt = build_string (format);
5683 args[1] = arg1;
5684 args[2] = arg2;
5685 msg = Fformat (3, args);
5687 len = SBYTES (msg) + 1;
5688 buffer = (char *) alloca (len);
5689 bcopy (SDATA (msg), buffer, len);
5691 message_dolog (buffer, len - 1, 1, 0);
5692 UNGCPRO;
5696 /* Output a newline in the *Messages* buffer if "needs" one. */
5698 void
5699 message_log_maybe_newline ()
5701 if (message_log_need_newline)
5702 message_dolog ("", 0, 1, 0);
5706 /* Add a string M of length NBYTES to the message log, optionally
5707 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5708 nonzero, means interpret the contents of M as multibyte. This
5709 function calls low-level routines in order to bypass text property
5710 hooks, etc. which might not be safe to run. */
5712 void
5713 message_dolog (m, nbytes, nlflag, multibyte)
5714 const char *m;
5715 int nbytes, nlflag, multibyte;
5717 if (!NILP (Vmemory_full))
5718 return;
5720 if (!NILP (Vmessage_log_max))
5722 struct buffer *oldbuf;
5723 Lisp_Object oldpoint, oldbegv, oldzv;
5724 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5725 int point_at_end = 0;
5726 int zv_at_end = 0;
5727 Lisp_Object old_deactivate_mark, tem;
5728 struct gcpro gcpro1;
5730 old_deactivate_mark = Vdeactivate_mark;
5731 oldbuf = current_buffer;
5732 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5733 current_buffer->undo_list = Qt;
5735 oldpoint = message_dolog_marker1;
5736 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5737 oldbegv = message_dolog_marker2;
5738 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5739 oldzv = message_dolog_marker3;
5740 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5741 GCPRO1 (old_deactivate_mark);
5743 if (PT == Z)
5744 point_at_end = 1;
5745 if (ZV == Z)
5746 zv_at_end = 1;
5748 BEGV = BEG;
5749 BEGV_BYTE = BEG_BYTE;
5750 ZV = Z;
5751 ZV_BYTE = Z_BYTE;
5752 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5754 /* Insert the string--maybe converting multibyte to single byte
5755 or vice versa, so that all the text fits the buffer. */
5756 if (multibyte
5757 && NILP (current_buffer->enable_multibyte_characters))
5759 int i, c, char_bytes;
5760 unsigned char work[1];
5762 /* Convert a multibyte string to single-byte
5763 for the *Message* buffer. */
5764 for (i = 0; i < nbytes; i += char_bytes)
5766 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5767 work[0] = (SINGLE_BYTE_CHAR_P (c)
5769 : multibyte_char_to_unibyte (c, Qnil));
5770 insert_1_both (work, 1, 1, 1, 0, 0);
5773 else if (! multibyte
5774 && ! NILP (current_buffer->enable_multibyte_characters))
5776 int i, c, char_bytes;
5777 unsigned char *msg = (unsigned char *) m;
5778 unsigned char str[MAX_MULTIBYTE_LENGTH];
5779 /* Convert a single-byte string to multibyte
5780 for the *Message* buffer. */
5781 for (i = 0; i < nbytes; i++)
5783 c = unibyte_char_to_multibyte (msg[i]);
5784 char_bytes = CHAR_STRING (c, str);
5785 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5788 else if (nbytes)
5789 insert_1 (m, nbytes, 1, 0, 0);
5791 if (nlflag)
5793 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5794 insert_1 ("\n", 1, 1, 0, 0);
5796 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5797 this_bol = PT;
5798 this_bol_byte = PT_BYTE;
5800 /* See if this line duplicates the previous one.
5801 If so, combine duplicates. */
5802 if (this_bol > BEG)
5804 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5805 prev_bol = PT;
5806 prev_bol_byte = PT_BYTE;
5808 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5809 this_bol, this_bol_byte);
5810 if (dup)
5812 del_range_both (prev_bol, prev_bol_byte,
5813 this_bol, this_bol_byte, 0);
5814 if (dup > 1)
5816 char dupstr[40];
5817 int duplen;
5819 /* If you change this format, don't forget to also
5820 change message_log_check_duplicate. */
5821 sprintf (dupstr, " [%d times]", dup);
5822 duplen = strlen (dupstr);
5823 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5824 insert_1 (dupstr, duplen, 1, 0, 1);
5829 /* If we have more than the desired maximum number of lines
5830 in the *Messages* buffer now, delete the oldest ones.
5831 This is safe because we don't have undo in this buffer. */
5833 if (NATNUMP (Vmessage_log_max))
5835 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5836 -XFASTINT (Vmessage_log_max) - 1, 0);
5837 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5840 BEGV = XMARKER (oldbegv)->charpos;
5841 BEGV_BYTE = marker_byte_position (oldbegv);
5843 if (zv_at_end)
5845 ZV = Z;
5846 ZV_BYTE = Z_BYTE;
5848 else
5850 ZV = XMARKER (oldzv)->charpos;
5851 ZV_BYTE = marker_byte_position (oldzv);
5854 if (point_at_end)
5855 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5856 else
5857 /* We can't do Fgoto_char (oldpoint) because it will run some
5858 Lisp code. */
5859 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5860 XMARKER (oldpoint)->bytepos);
5862 UNGCPRO;
5863 unchain_marker (oldpoint);
5864 unchain_marker (oldbegv);
5865 unchain_marker (oldzv);
5867 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5868 set_buffer_internal (oldbuf);
5869 if (NILP (tem))
5870 windows_or_buffers_changed = old_windows_or_buffers_changed;
5871 message_log_need_newline = !nlflag;
5872 Vdeactivate_mark = old_deactivate_mark;
5877 /* We are at the end of the buffer after just having inserted a newline.
5878 (Note: We depend on the fact we won't be crossing the gap.)
5879 Check to see if the most recent message looks a lot like the previous one.
5880 Return 0 if different, 1 if the new one should just replace it, or a
5881 value N > 1 if we should also append " [N times]". */
5883 static int
5884 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5885 int prev_bol, this_bol;
5886 int prev_bol_byte, this_bol_byte;
5888 int i;
5889 int len = Z_BYTE - 1 - this_bol_byte;
5890 int seen_dots = 0;
5891 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5892 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5894 for (i = 0; i < len; i++)
5896 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5897 seen_dots = 1;
5898 if (p1[i] != p2[i])
5899 return seen_dots;
5901 p1 += len;
5902 if (*p1 == '\n')
5903 return 2;
5904 if (*p1++ == ' ' && *p1++ == '[')
5906 int n = 0;
5907 while (*p1 >= '0' && *p1 <= '9')
5908 n = n * 10 + *p1++ - '0';
5909 if (strncmp (p1, " times]\n", 8) == 0)
5910 return n+1;
5912 return 0;
5916 /* Display an echo area message M with a specified length of NBYTES
5917 bytes. The string may include null characters. If M is 0, clear
5918 out any existing message, and let the mini-buffer text show
5919 through.
5921 The buffer M must continue to exist until after the echo area gets
5922 cleared or some other message gets displayed there. This means do
5923 not pass text that is stored in a Lisp string; do not pass text in
5924 a buffer that was alloca'd. */
5926 void
5927 message2 (m, nbytes, multibyte)
5928 const char *m;
5929 int nbytes;
5930 int multibyte;
5932 /* First flush out any partial line written with print. */
5933 message_log_maybe_newline ();
5934 if (m)
5935 message_dolog (m, nbytes, 1, multibyte);
5936 message2_nolog (m, nbytes, multibyte);
5940 /* The non-logging counterpart of message2. */
5942 void
5943 message2_nolog (m, nbytes, multibyte)
5944 const char *m;
5945 int nbytes, multibyte;
5947 struct frame *sf = SELECTED_FRAME ();
5948 message_enable_multibyte = multibyte;
5950 if (noninteractive)
5952 if (noninteractive_need_newline)
5953 putc ('\n', stderr);
5954 noninteractive_need_newline = 0;
5955 if (m)
5956 fwrite (m, nbytes, 1, stderr);
5957 if (cursor_in_echo_area == 0)
5958 fprintf (stderr, "\n");
5959 fflush (stderr);
5961 /* A null message buffer means that the frame hasn't really been
5962 initialized yet. Error messages get reported properly by
5963 cmd_error, so this must be just an informative message; toss it. */
5964 else if (INTERACTIVE
5965 && sf->glyphs_initialized_p
5966 && FRAME_MESSAGE_BUF (sf))
5968 Lisp_Object mini_window;
5969 struct frame *f;
5971 /* Get the frame containing the mini-buffer
5972 that the selected frame is using. */
5973 mini_window = FRAME_MINIBUF_WINDOW (sf);
5974 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5976 FRAME_SAMPLE_VISIBILITY (f);
5977 if (FRAME_VISIBLE_P (sf)
5978 && ! FRAME_VISIBLE_P (f))
5979 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5981 if (m)
5983 set_message (m, Qnil, nbytes, multibyte);
5984 if (minibuffer_auto_raise)
5985 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5987 else
5988 clear_message (1, 1);
5990 do_pending_window_change (0);
5991 echo_area_display (1);
5992 do_pending_window_change (0);
5993 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5994 (*frame_up_to_date_hook) (f);
5999 /* Display an echo area message M with a specified length of NBYTES
6000 bytes. The string may include null characters. If M is not a
6001 string, clear out any existing message, and let the mini-buffer
6002 text show through. */
6004 void
6005 message3 (m, nbytes, multibyte)
6006 Lisp_Object m;
6007 int nbytes;
6008 int multibyte;
6010 struct gcpro gcpro1;
6012 GCPRO1 (m);
6014 /* First flush out any partial line written with print. */
6015 message_log_maybe_newline ();
6016 if (STRINGP (m))
6017 message_dolog (SDATA (m), nbytes, 1, multibyte);
6018 message3_nolog (m, nbytes, multibyte);
6020 UNGCPRO;
6024 /* The non-logging version of message3. */
6026 void
6027 message3_nolog (m, nbytes, multibyte)
6028 Lisp_Object m;
6029 int nbytes, multibyte;
6031 struct frame *sf = SELECTED_FRAME ();
6032 message_enable_multibyte = multibyte;
6034 if (noninteractive)
6036 if (noninteractive_need_newline)
6037 putc ('\n', stderr);
6038 noninteractive_need_newline = 0;
6039 if (STRINGP (m))
6040 fwrite (SDATA (m), nbytes, 1, stderr);
6041 if (cursor_in_echo_area == 0)
6042 fprintf (stderr, "\n");
6043 fflush (stderr);
6045 /* A null message buffer means that the frame hasn't really been
6046 initialized yet. Error messages get reported properly by
6047 cmd_error, so this must be just an informative message; toss it. */
6048 else if (INTERACTIVE
6049 && sf->glyphs_initialized_p
6050 && FRAME_MESSAGE_BUF (sf))
6052 Lisp_Object mini_window;
6053 Lisp_Object frame;
6054 struct frame *f;
6056 /* Get the frame containing the mini-buffer
6057 that the selected frame is using. */
6058 mini_window = FRAME_MINIBUF_WINDOW (sf);
6059 frame = XWINDOW (mini_window)->frame;
6060 f = XFRAME (frame);
6062 FRAME_SAMPLE_VISIBILITY (f);
6063 if (FRAME_VISIBLE_P (sf)
6064 && !FRAME_VISIBLE_P (f))
6065 Fmake_frame_visible (frame);
6067 if (STRINGP (m) && SCHARS (m) > 0)
6069 set_message (NULL, m, nbytes, multibyte);
6070 if (minibuffer_auto_raise)
6071 Fraise_frame (frame);
6073 else
6074 clear_message (1, 1);
6076 do_pending_window_change (0);
6077 echo_area_display (1);
6078 do_pending_window_change (0);
6079 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6080 (*frame_up_to_date_hook) (f);
6085 /* Display a null-terminated echo area message M. If M is 0, clear
6086 out any existing message, and let the mini-buffer text show through.
6088 The buffer M must continue to exist until after the echo area gets
6089 cleared or some other message gets displayed there. Do not pass
6090 text that is stored in a Lisp string. Do not pass text in a buffer
6091 that was alloca'd. */
6093 void
6094 message1 (m)
6095 char *m;
6097 message2 (m, (m ? strlen (m) : 0), 0);
6101 /* The non-logging counterpart of message1. */
6103 void
6104 message1_nolog (m)
6105 char *m;
6107 message2_nolog (m, (m ? strlen (m) : 0), 0);
6110 /* Display a message M which contains a single %s
6111 which gets replaced with STRING. */
6113 void
6114 message_with_string (m, string, log)
6115 char *m;
6116 Lisp_Object string;
6117 int log;
6119 CHECK_STRING (string);
6121 if (noninteractive)
6123 if (m)
6125 if (noninteractive_need_newline)
6126 putc ('\n', stderr);
6127 noninteractive_need_newline = 0;
6128 fprintf (stderr, m, SDATA (string));
6129 if (cursor_in_echo_area == 0)
6130 fprintf (stderr, "\n");
6131 fflush (stderr);
6134 else if (INTERACTIVE)
6136 /* The frame whose minibuffer we're going to display the message on.
6137 It may be larger than the selected frame, so we need
6138 to use its buffer, not the selected frame's buffer. */
6139 Lisp_Object mini_window;
6140 struct frame *f, *sf = SELECTED_FRAME ();
6142 /* Get the frame containing the minibuffer
6143 that the selected frame is using. */
6144 mini_window = FRAME_MINIBUF_WINDOW (sf);
6145 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6147 /* A null message buffer means that the frame hasn't really been
6148 initialized yet. Error messages get reported properly by
6149 cmd_error, so this must be just an informative message; toss it. */
6150 if (FRAME_MESSAGE_BUF (f))
6152 Lisp_Object args[2], message;
6153 struct gcpro gcpro1, gcpro2;
6155 args[0] = build_string (m);
6156 args[1] = message = string;
6157 GCPRO2 (args[0], message);
6158 gcpro1.nvars = 2;
6160 message = Fformat (2, args);
6162 if (log)
6163 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6164 else
6165 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6167 UNGCPRO;
6169 /* Print should start at the beginning of the message
6170 buffer next time. */
6171 message_buf_print = 0;
6177 /* Dump an informative message to the minibuf. If M is 0, clear out
6178 any existing message, and let the mini-buffer text show through. */
6180 /* VARARGS 1 */
6181 void
6182 message (m, a1, a2, a3)
6183 char *m;
6184 EMACS_INT a1, a2, a3;
6186 if (noninteractive)
6188 if (m)
6190 if (noninteractive_need_newline)
6191 putc ('\n', stderr);
6192 noninteractive_need_newline = 0;
6193 fprintf (stderr, m, a1, a2, a3);
6194 if (cursor_in_echo_area == 0)
6195 fprintf (stderr, "\n");
6196 fflush (stderr);
6199 else if (INTERACTIVE)
6201 /* The frame whose mini-buffer we're going to display the message
6202 on. It may be larger than the selected frame, so we need to
6203 use its buffer, not the selected frame's buffer. */
6204 Lisp_Object mini_window;
6205 struct frame *f, *sf = SELECTED_FRAME ();
6207 /* Get the frame containing the mini-buffer
6208 that the selected frame is using. */
6209 mini_window = FRAME_MINIBUF_WINDOW (sf);
6210 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6212 /* A null message buffer means that the frame hasn't really been
6213 initialized yet. Error messages get reported properly by
6214 cmd_error, so this must be just an informative message; toss
6215 it. */
6216 if (FRAME_MESSAGE_BUF (f))
6218 if (m)
6220 int len;
6221 #ifdef NO_ARG_ARRAY
6222 char *a[3];
6223 a[0] = (char *) a1;
6224 a[1] = (char *) a2;
6225 a[2] = (char *) a3;
6227 len = doprnt (FRAME_MESSAGE_BUF (f),
6228 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6229 #else
6230 len = doprnt (FRAME_MESSAGE_BUF (f),
6231 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6232 (char **) &a1);
6233 #endif /* NO_ARG_ARRAY */
6235 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6237 else
6238 message1 (0);
6240 /* Print should start at the beginning of the message
6241 buffer next time. */
6242 message_buf_print = 0;
6248 /* The non-logging version of message. */
6250 void
6251 message_nolog (m, a1, a2, a3)
6252 char *m;
6253 EMACS_INT a1, a2, a3;
6255 Lisp_Object old_log_max;
6256 old_log_max = Vmessage_log_max;
6257 Vmessage_log_max = Qnil;
6258 message (m, a1, a2, a3);
6259 Vmessage_log_max = old_log_max;
6263 /* Display the current message in the current mini-buffer. This is
6264 only called from error handlers in process.c, and is not time
6265 critical. */
6267 void
6268 update_echo_area ()
6270 if (!NILP (echo_area_buffer[0]))
6272 Lisp_Object string;
6273 string = Fcurrent_message ();
6274 message3 (string, SBYTES (string),
6275 !NILP (current_buffer->enable_multibyte_characters));
6280 /* Make sure echo area buffers in `echo_buffers' are live.
6281 If they aren't, make new ones. */
6283 static void
6284 ensure_echo_area_buffers ()
6286 int i;
6288 for (i = 0; i < 2; ++i)
6289 if (!BUFFERP (echo_buffer[i])
6290 || NILP (XBUFFER (echo_buffer[i])->name))
6292 char name[30];
6293 Lisp_Object old_buffer;
6294 int j;
6296 old_buffer = echo_buffer[i];
6297 sprintf (name, " *Echo Area %d*", i);
6298 echo_buffer[i] = Fget_buffer_create (build_string (name));
6299 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6301 for (j = 0; j < 2; ++j)
6302 if (EQ (old_buffer, echo_area_buffer[j]))
6303 echo_area_buffer[j] = echo_buffer[i];
6308 /* Call FN with args A1..A4 with either the current or last displayed
6309 echo_area_buffer as current buffer.
6311 WHICH zero means use the current message buffer
6312 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6313 from echo_buffer[] and clear it.
6315 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6316 suitable buffer from echo_buffer[] and clear it.
6318 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6319 that the current message becomes the last displayed one, make
6320 choose a suitable buffer for echo_area_buffer[0], and clear it.
6322 Value is what FN returns. */
6324 static int
6325 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6326 struct window *w;
6327 int which;
6328 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6329 EMACS_INT a1;
6330 Lisp_Object a2;
6331 EMACS_INT a3, a4;
6333 Lisp_Object buffer;
6334 int this_one, the_other, clear_buffer_p, rc;
6335 int count = SPECPDL_INDEX ();
6337 /* If buffers aren't live, make new ones. */
6338 ensure_echo_area_buffers ();
6340 clear_buffer_p = 0;
6342 if (which == 0)
6343 this_one = 0, the_other = 1;
6344 else if (which > 0)
6345 this_one = 1, the_other = 0;
6346 else
6348 this_one = 0, the_other = 1;
6349 clear_buffer_p = 1;
6351 /* We need a fresh one in case the current echo buffer equals
6352 the one containing the last displayed echo area message. */
6353 if (!NILP (echo_area_buffer[this_one])
6354 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6355 echo_area_buffer[this_one] = Qnil;
6358 /* Choose a suitable buffer from echo_buffer[] is we don't
6359 have one. */
6360 if (NILP (echo_area_buffer[this_one]))
6362 echo_area_buffer[this_one]
6363 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6364 ? echo_buffer[the_other]
6365 : echo_buffer[this_one]);
6366 clear_buffer_p = 1;
6369 buffer = echo_area_buffer[this_one];
6371 /* Don't get confused by reusing the buffer used for echoing
6372 for a different purpose. */
6373 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6374 cancel_echoing ();
6376 record_unwind_protect (unwind_with_echo_area_buffer,
6377 with_echo_area_buffer_unwind_data (w));
6379 /* Make the echo area buffer current. Note that for display
6380 purposes, it is not necessary that the displayed window's buffer
6381 == current_buffer, except for text property lookup. So, let's
6382 only set that buffer temporarily here without doing a full
6383 Fset_window_buffer. We must also change w->pointm, though,
6384 because otherwise an assertions in unshow_buffer fails, and Emacs
6385 aborts. */
6386 set_buffer_internal_1 (XBUFFER (buffer));
6387 if (w)
6389 w->buffer = buffer;
6390 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6393 current_buffer->undo_list = Qt;
6394 current_buffer->read_only = Qnil;
6395 specbind (Qinhibit_read_only, Qt);
6396 specbind (Qinhibit_modification_hooks, Qt);
6398 if (clear_buffer_p && Z > BEG)
6399 del_range (BEG, Z);
6401 xassert (BEGV >= BEG);
6402 xassert (ZV <= Z && ZV >= BEGV);
6404 rc = fn (a1, a2, a3, a4);
6406 xassert (BEGV >= BEG);
6407 xassert (ZV <= Z && ZV >= BEGV);
6409 unbind_to (count, Qnil);
6410 return rc;
6414 /* Save state that should be preserved around the call to the function
6415 FN called in with_echo_area_buffer. */
6417 static Lisp_Object
6418 with_echo_area_buffer_unwind_data (w)
6419 struct window *w;
6421 int i = 0;
6422 Lisp_Object vector;
6424 /* Reduce consing by keeping one vector in
6425 Vwith_echo_area_save_vector. */
6426 vector = Vwith_echo_area_save_vector;
6427 Vwith_echo_area_save_vector = Qnil;
6429 if (NILP (vector))
6430 vector = Fmake_vector (make_number (7), Qnil);
6432 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6433 AREF (vector, i) = Vdeactivate_mark, ++i;
6434 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6436 if (w)
6438 XSETWINDOW (AREF (vector, i), w); ++i;
6439 AREF (vector, i) = w->buffer; ++i;
6440 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6441 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6443 else
6445 int end = i + 4;
6446 for (; i < end; ++i)
6447 AREF (vector, i) = Qnil;
6450 xassert (i == ASIZE (vector));
6451 return vector;
6455 /* Restore global state from VECTOR which was created by
6456 with_echo_area_buffer_unwind_data. */
6458 static Lisp_Object
6459 unwind_with_echo_area_buffer (vector)
6460 Lisp_Object vector;
6462 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6463 Vdeactivate_mark = AREF (vector, 1);
6464 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6466 if (WINDOWP (AREF (vector, 3)))
6468 struct window *w;
6469 Lisp_Object buffer, charpos, bytepos;
6471 w = XWINDOW (AREF (vector, 3));
6472 buffer = AREF (vector, 4);
6473 charpos = AREF (vector, 5);
6474 bytepos = AREF (vector, 6);
6476 w->buffer = buffer;
6477 set_marker_both (w->pointm, buffer,
6478 XFASTINT (charpos), XFASTINT (bytepos));
6481 Vwith_echo_area_save_vector = vector;
6482 return Qnil;
6486 /* Set up the echo area for use by print functions. MULTIBYTE_P
6487 non-zero means we will print multibyte. */
6489 void
6490 setup_echo_area_for_printing (multibyte_p)
6491 int multibyte_p;
6493 /* If we can't find an echo area any more, exit. */
6494 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6495 Fkill_emacs (Qnil);
6497 ensure_echo_area_buffers ();
6499 if (!message_buf_print)
6501 /* A message has been output since the last time we printed.
6502 Choose a fresh echo area buffer. */
6503 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6504 echo_area_buffer[0] = echo_buffer[1];
6505 else
6506 echo_area_buffer[0] = echo_buffer[0];
6508 /* Switch to that buffer and clear it. */
6509 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6510 current_buffer->truncate_lines = Qnil;
6512 if (Z > BEG)
6514 int count = SPECPDL_INDEX ();
6515 specbind (Qinhibit_read_only, Qt);
6516 /* Note that undo recording is always disabled. */
6517 del_range (BEG, Z);
6518 unbind_to (count, Qnil);
6520 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6522 /* Set up the buffer for the multibyteness we need. */
6523 if (multibyte_p
6524 != !NILP (current_buffer->enable_multibyte_characters))
6525 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6527 /* Raise the frame containing the echo area. */
6528 if (minibuffer_auto_raise)
6530 struct frame *sf = SELECTED_FRAME ();
6531 Lisp_Object mini_window;
6532 mini_window = FRAME_MINIBUF_WINDOW (sf);
6533 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6536 message_log_maybe_newline ();
6537 message_buf_print = 1;
6539 else
6541 if (NILP (echo_area_buffer[0]))
6543 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6544 echo_area_buffer[0] = echo_buffer[1];
6545 else
6546 echo_area_buffer[0] = echo_buffer[0];
6549 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6551 /* Someone switched buffers between print requests. */
6552 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6553 current_buffer->truncate_lines = Qnil;
6559 /* Display an echo area message in window W. Value is non-zero if W's
6560 height is changed. If display_last_displayed_message_p is
6561 non-zero, display the message that was last displayed, otherwise
6562 display the current message. */
6564 static int
6565 display_echo_area (w)
6566 struct window *w;
6568 int i, no_message_p, window_height_changed_p, count;
6570 /* Temporarily disable garbage collections while displaying the echo
6571 area. This is done because a GC can print a message itself.
6572 That message would modify the echo area buffer's contents while a
6573 redisplay of the buffer is going on, and seriously confuse
6574 redisplay. */
6575 count = inhibit_garbage_collection ();
6577 /* If there is no message, we must call display_echo_area_1
6578 nevertheless because it resizes the window. But we will have to
6579 reset the echo_area_buffer in question to nil at the end because
6580 with_echo_area_buffer will sets it to an empty buffer. */
6581 i = display_last_displayed_message_p ? 1 : 0;
6582 no_message_p = NILP (echo_area_buffer[i]);
6584 window_height_changed_p
6585 = with_echo_area_buffer (w, display_last_displayed_message_p,
6586 display_echo_area_1,
6587 (EMACS_INT) w, Qnil, 0, 0);
6589 if (no_message_p)
6590 echo_area_buffer[i] = Qnil;
6592 unbind_to (count, Qnil);
6593 return window_height_changed_p;
6597 /* Helper for display_echo_area. Display the current buffer which
6598 contains the current echo area message in window W, a mini-window,
6599 a pointer to which is passed in A1. A2..A4 are currently not used.
6600 Change the height of W so that all of the message is displayed.
6601 Value is non-zero if height of W was changed. */
6603 static int
6604 display_echo_area_1 (a1, a2, a3, a4)
6605 EMACS_INT a1;
6606 Lisp_Object a2;
6607 EMACS_INT a3, a4;
6609 struct window *w = (struct window *) a1;
6610 Lisp_Object window;
6611 struct text_pos start;
6612 int window_height_changed_p = 0;
6614 /* Do this before displaying, so that we have a large enough glyph
6615 matrix for the display. */
6616 window_height_changed_p = resize_mini_window (w, 0);
6618 /* Display. */
6619 clear_glyph_matrix (w->desired_matrix);
6620 XSETWINDOW (window, w);
6621 SET_TEXT_POS (start, BEG, BEG_BYTE);
6622 try_window (window, start);
6624 return window_height_changed_p;
6628 /* Resize the echo area window to exactly the size needed for the
6629 currently displayed message, if there is one. If a mini-buffer
6630 is active, don't shrink it. */
6632 void
6633 resize_echo_area_exactly ()
6635 if (BUFFERP (echo_area_buffer[0])
6636 && WINDOWP (echo_area_window))
6638 struct window *w = XWINDOW (echo_area_window);
6639 int resized_p;
6640 Lisp_Object resize_exactly;
6642 if (minibuf_level == 0)
6643 resize_exactly = Qt;
6644 else
6645 resize_exactly = Qnil;
6647 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6648 (EMACS_INT) w, resize_exactly, 0, 0);
6649 if (resized_p)
6651 ++windows_or_buffers_changed;
6652 ++update_mode_lines;
6653 redisplay_internal (0);
6659 /* Callback function for with_echo_area_buffer, when used from
6660 resize_echo_area_exactly. A1 contains a pointer to the window to
6661 resize, EXACTLY non-nil means resize the mini-window exactly to the
6662 size of the text displayed. A3 and A4 are not used. Value is what
6663 resize_mini_window returns. */
6665 static int
6666 resize_mini_window_1 (a1, exactly, a3, a4)
6667 EMACS_INT a1;
6668 Lisp_Object exactly;
6669 EMACS_INT a3, a4;
6671 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6675 /* Resize mini-window W to fit the size of its contents. EXACT:P
6676 means size the window exactly to the size needed. Otherwise, it's
6677 only enlarged until W's buffer is empty. Value is non-zero if
6678 the window height has been changed. */
6681 resize_mini_window (w, exact_p)
6682 struct window *w;
6683 int exact_p;
6685 struct frame *f = XFRAME (w->frame);
6686 int window_height_changed_p = 0;
6688 xassert (MINI_WINDOW_P (w));
6690 /* Don't resize windows while redisplaying a window; it would
6691 confuse redisplay functions when the size of the window they are
6692 displaying changes from under them. Such a resizing can happen,
6693 for instance, when which-func prints a long message while
6694 we are running fontification-functions. We're running these
6695 functions with safe_call which binds inhibit-redisplay to t. */
6696 if (!NILP (Vinhibit_redisplay))
6697 return 0;
6699 /* Nil means don't try to resize. */
6700 if (NILP (Vresize_mini_windows)
6701 || (FRAME_X_P (f) && f->output_data.x == NULL))
6702 return 0;
6704 if (!FRAME_MINIBUF_ONLY_P (f))
6706 struct it it;
6707 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6708 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6709 int height, max_height;
6710 int unit = CANON_Y_UNIT (f);
6711 struct text_pos start;
6712 struct buffer *old_current_buffer = NULL;
6714 if (current_buffer != XBUFFER (w->buffer))
6716 old_current_buffer = current_buffer;
6717 set_buffer_internal (XBUFFER (w->buffer));
6720 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6722 /* Compute the max. number of lines specified by the user. */
6723 if (FLOATP (Vmax_mini_window_height))
6724 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6725 else if (INTEGERP (Vmax_mini_window_height))
6726 max_height = XINT (Vmax_mini_window_height);
6727 else
6728 max_height = total_height / 4;
6730 /* Correct that max. height if it's bogus. */
6731 max_height = max (1, max_height);
6732 max_height = min (total_height, max_height);
6734 /* Find out the height of the text in the window. */
6735 if (it.truncate_lines_p)
6736 height = 1;
6737 else
6739 last_height = 0;
6740 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6741 if (it.max_ascent == 0 && it.max_descent == 0)
6742 height = it.current_y + last_height;
6743 else
6744 height = it.current_y + it.max_ascent + it.max_descent;
6745 height -= it.extra_line_spacing;
6746 height = (height + unit - 1) / unit;
6749 /* Compute a suitable window start. */
6750 if (height > max_height)
6752 height = max_height;
6753 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6754 move_it_vertically_backward (&it, (height - 1) * unit);
6755 start = it.current.pos;
6757 else
6758 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6759 SET_MARKER_FROM_TEXT_POS (w->start, start);
6761 if (EQ (Vresize_mini_windows, Qgrow_only))
6763 /* Let it grow only, until we display an empty message, in which
6764 case the window shrinks again. */
6765 if (height > XFASTINT (w->height))
6767 int old_height = XFASTINT (w->height);
6768 freeze_window_starts (f, 1);
6769 grow_mini_window (w, height - XFASTINT (w->height));
6770 window_height_changed_p = XFASTINT (w->height) != old_height;
6772 else if (height < XFASTINT (w->height)
6773 && (exact_p || BEGV == ZV))
6775 int old_height = XFASTINT (w->height);
6776 freeze_window_starts (f, 0);
6777 shrink_mini_window (w);
6778 window_height_changed_p = XFASTINT (w->height) != old_height;
6781 else
6783 /* Always resize to exact size needed. */
6784 if (height > XFASTINT (w->height))
6786 int old_height = XFASTINT (w->height);
6787 freeze_window_starts (f, 1);
6788 grow_mini_window (w, height - XFASTINT (w->height));
6789 window_height_changed_p = XFASTINT (w->height) != old_height;
6791 else if (height < XFASTINT (w->height))
6793 int old_height = XFASTINT (w->height);
6794 freeze_window_starts (f, 0);
6795 shrink_mini_window (w);
6797 if (height)
6799 freeze_window_starts (f, 1);
6800 grow_mini_window (w, height - XFASTINT (w->height));
6803 window_height_changed_p = XFASTINT (w->height) != old_height;
6807 if (old_current_buffer)
6808 set_buffer_internal (old_current_buffer);
6811 return window_height_changed_p;
6815 /* Value is the current message, a string, or nil if there is no
6816 current message. */
6818 Lisp_Object
6819 current_message ()
6821 Lisp_Object msg;
6823 if (NILP (echo_area_buffer[0]))
6824 msg = Qnil;
6825 else
6827 with_echo_area_buffer (0, 0, current_message_1,
6828 (EMACS_INT) &msg, Qnil, 0, 0);
6829 if (NILP (msg))
6830 echo_area_buffer[0] = Qnil;
6833 return msg;
6837 static int
6838 current_message_1 (a1, a2, a3, a4)
6839 EMACS_INT a1;
6840 Lisp_Object a2;
6841 EMACS_INT a3, a4;
6843 Lisp_Object *msg = (Lisp_Object *) a1;
6845 if (Z > BEG)
6846 *msg = make_buffer_string (BEG, Z, 1);
6847 else
6848 *msg = Qnil;
6849 return 0;
6853 /* Push the current message on Vmessage_stack for later restauration
6854 by restore_message. Value is non-zero if the current message isn't
6855 empty. This is a relatively infrequent operation, so it's not
6856 worth optimizing. */
6859 push_message ()
6861 Lisp_Object msg;
6862 msg = current_message ();
6863 Vmessage_stack = Fcons (msg, Vmessage_stack);
6864 return STRINGP (msg);
6868 /* Restore message display from the top of Vmessage_stack. */
6870 void
6871 restore_message ()
6873 Lisp_Object msg;
6875 xassert (CONSP (Vmessage_stack));
6876 msg = XCAR (Vmessage_stack);
6877 if (STRINGP (msg))
6878 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6879 else
6880 message3_nolog (msg, 0, 0);
6884 /* Handler for record_unwind_protect calling pop_message. */
6886 Lisp_Object
6887 pop_message_unwind (dummy)
6888 Lisp_Object dummy;
6890 pop_message ();
6891 return Qnil;
6894 /* Pop the top-most entry off Vmessage_stack. */
6896 void
6897 pop_message ()
6899 xassert (CONSP (Vmessage_stack));
6900 Vmessage_stack = XCDR (Vmessage_stack);
6904 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6905 exits. If the stack is not empty, we have a missing pop_message
6906 somewhere. */
6908 void
6909 check_message_stack ()
6911 if (!NILP (Vmessage_stack))
6912 abort ();
6916 /* Truncate to NCHARS what will be displayed in the echo area the next
6917 time we display it---but don't redisplay it now. */
6919 void
6920 truncate_echo_area (nchars)
6921 int nchars;
6923 if (nchars == 0)
6924 echo_area_buffer[0] = Qnil;
6925 /* A null message buffer means that the frame hasn't really been
6926 initialized yet. Error messages get reported properly by
6927 cmd_error, so this must be just an informative message; toss it. */
6928 else if (!noninteractive
6929 && INTERACTIVE
6930 && !NILP (echo_area_buffer[0]))
6932 struct frame *sf = SELECTED_FRAME ();
6933 if (FRAME_MESSAGE_BUF (sf))
6934 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6939 /* Helper function for truncate_echo_area. Truncate the current
6940 message to at most NCHARS characters. */
6942 static int
6943 truncate_message_1 (nchars, a2, a3, a4)
6944 EMACS_INT nchars;
6945 Lisp_Object a2;
6946 EMACS_INT a3, a4;
6948 if (BEG + nchars < Z)
6949 del_range (BEG + nchars, Z);
6950 if (Z == BEG)
6951 echo_area_buffer[0] = Qnil;
6952 return 0;
6956 /* Set the current message to a substring of S or STRING.
6958 If STRING is a Lisp string, set the message to the first NBYTES
6959 bytes from STRING. NBYTES zero means use the whole string. If
6960 STRING is multibyte, the message will be displayed multibyte.
6962 If S is not null, set the message to the first LEN bytes of S. LEN
6963 zero means use the whole string. MULTIBYTE_P non-zero means S is
6964 multibyte. Display the message multibyte in that case. */
6966 void
6967 set_message (s, string, nbytes, multibyte_p)
6968 const char *s;
6969 Lisp_Object string;
6970 int nbytes, multibyte_p;
6972 message_enable_multibyte
6973 = ((s && multibyte_p)
6974 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6976 with_echo_area_buffer (0, -1, set_message_1,
6977 (EMACS_INT) s, string, nbytes, multibyte_p);
6978 message_buf_print = 0;
6979 help_echo_showing_p = 0;
6983 /* Helper function for set_message. Arguments have the same meaning
6984 as there, with A1 corresponding to S and A2 corresponding to STRING
6985 This function is called with the echo area buffer being
6986 current. */
6988 static int
6989 set_message_1 (a1, a2, nbytes, multibyte_p)
6990 EMACS_INT a1;
6991 Lisp_Object a2;
6992 EMACS_INT nbytes, multibyte_p;
6994 const char *s = (const char *) a1;
6995 Lisp_Object string = a2;
6997 xassert (BEG == Z);
6999 /* Change multibyteness of the echo buffer appropriately. */
7000 if (message_enable_multibyte
7001 != !NILP (current_buffer->enable_multibyte_characters))
7002 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7004 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7006 /* Insert new message at BEG. */
7007 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7009 if (STRINGP (string))
7011 int nchars;
7013 if (nbytes == 0)
7014 nbytes = SBYTES (string);
7015 nchars = string_byte_to_char (string, nbytes);
7017 /* This function takes care of single/multibyte conversion. We
7018 just have to ensure that the echo area buffer has the right
7019 setting of enable_multibyte_characters. */
7020 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7022 else if (s)
7024 if (nbytes == 0)
7025 nbytes = strlen (s);
7027 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7029 /* Convert from multi-byte to single-byte. */
7030 int i, c, n;
7031 unsigned char work[1];
7033 /* Convert a multibyte string to single-byte. */
7034 for (i = 0; i < nbytes; i += n)
7036 c = string_char_and_length (s + i, nbytes - i, &n);
7037 work[0] = (SINGLE_BYTE_CHAR_P (c)
7039 : multibyte_char_to_unibyte (c, Qnil));
7040 insert_1_both (work, 1, 1, 1, 0, 0);
7043 else if (!multibyte_p
7044 && !NILP (current_buffer->enable_multibyte_characters))
7046 /* Convert from single-byte to multi-byte. */
7047 int i, c, n;
7048 const unsigned char *msg = (const unsigned char *) s;
7049 unsigned char str[MAX_MULTIBYTE_LENGTH];
7051 /* Convert a single-byte string to multibyte. */
7052 for (i = 0; i < nbytes; i++)
7054 c = unibyte_char_to_multibyte (msg[i]);
7055 n = CHAR_STRING (c, str);
7056 insert_1_both (str, 1, n, 1, 0, 0);
7059 else
7060 insert_1 (s, nbytes, 1, 0, 0);
7063 return 0;
7067 /* Clear messages. CURRENT_P non-zero means clear the current
7068 message. LAST_DISPLAYED_P non-zero means clear the message
7069 last displayed. */
7071 void
7072 clear_message (current_p, last_displayed_p)
7073 int current_p, last_displayed_p;
7075 if (current_p)
7077 echo_area_buffer[0] = Qnil;
7078 message_cleared_p = 1;
7081 if (last_displayed_p)
7082 echo_area_buffer[1] = Qnil;
7084 message_buf_print = 0;
7087 /* Clear garbaged frames.
7089 This function is used where the old redisplay called
7090 redraw_garbaged_frames which in turn called redraw_frame which in
7091 turn called clear_frame. The call to clear_frame was a source of
7092 flickering. I believe a clear_frame is not necessary. It should
7093 suffice in the new redisplay to invalidate all current matrices,
7094 and ensure a complete redisplay of all windows. */
7096 static void
7097 clear_garbaged_frames ()
7099 if (frame_garbaged)
7101 Lisp_Object tail, frame;
7102 int changed_count = 0;
7104 FOR_EACH_FRAME (tail, frame)
7106 struct frame *f = XFRAME (frame);
7108 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7110 if (f->resized_p)
7111 Fredraw_frame (frame);
7112 clear_current_matrices (f);
7113 changed_count++;
7114 f->garbaged = 0;
7115 f->resized_p = 0;
7119 frame_garbaged = 0;
7120 if (changed_count)
7121 ++windows_or_buffers_changed;
7126 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7127 is non-zero update selected_frame. Value is non-zero if the
7128 mini-windows height has been changed. */
7130 static int
7131 echo_area_display (update_frame_p)
7132 int update_frame_p;
7134 Lisp_Object mini_window;
7135 struct window *w;
7136 struct frame *f;
7137 int window_height_changed_p = 0;
7138 struct frame *sf = SELECTED_FRAME ();
7140 mini_window = FRAME_MINIBUF_WINDOW (sf);
7141 w = XWINDOW (mini_window);
7142 f = XFRAME (WINDOW_FRAME (w));
7144 /* Don't display if frame is invisible or not yet initialized. */
7145 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7146 return 0;
7148 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7149 #ifndef MAC_OS8
7150 #ifdef HAVE_WINDOW_SYSTEM
7151 /* When Emacs starts, selected_frame may be a visible terminal
7152 frame, even if we run under a window system. If we let this
7153 through, a message would be displayed on the terminal. */
7154 if (EQ (selected_frame, Vterminal_frame)
7155 && !NILP (Vwindow_system))
7156 return 0;
7157 #endif /* HAVE_WINDOW_SYSTEM */
7158 #endif
7160 /* Redraw garbaged frames. */
7161 if (frame_garbaged)
7162 clear_garbaged_frames ();
7164 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7166 echo_area_window = mini_window;
7167 window_height_changed_p = display_echo_area (w);
7168 w->must_be_updated_p = 1;
7170 /* Update the display, unless called from redisplay_internal.
7171 Also don't update the screen during redisplay itself. The
7172 update will happen at the end of redisplay, and an update
7173 here could cause confusion. */
7174 if (update_frame_p && !redisplaying_p)
7176 int n = 0;
7178 /* If the display update has been interrupted by pending
7179 input, update mode lines in the frame. Due to the
7180 pending input, it might have been that redisplay hasn't
7181 been called, so that mode lines above the echo area are
7182 garbaged. This looks odd, so we prevent it here. */
7183 if (!display_completed)
7184 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7186 if (window_height_changed_p
7187 /* Don't do this if Emacs is shutting down. Redisplay
7188 needs to run hooks. */
7189 && !NILP (Vrun_hooks))
7191 /* Must update other windows. Likewise as in other
7192 cases, don't let this update be interrupted by
7193 pending input. */
7194 int count = SPECPDL_INDEX ();
7195 specbind (Qredisplay_dont_pause, Qt);
7196 windows_or_buffers_changed = 1;
7197 redisplay_internal (0);
7198 unbind_to (count, Qnil);
7200 else if (FRAME_WINDOW_P (f) && n == 0)
7202 /* Window configuration is the same as before.
7203 Can do with a display update of the echo area,
7204 unless we displayed some mode lines. */
7205 update_single_window (w, 1);
7206 rif->flush_display (f);
7208 else
7209 update_frame (f, 1, 1);
7211 /* If cursor is in the echo area, make sure that the next
7212 redisplay displays the minibuffer, so that the cursor will
7213 be replaced with what the minibuffer wants. */
7214 if (cursor_in_echo_area)
7215 ++windows_or_buffers_changed;
7218 else if (!EQ (mini_window, selected_window))
7219 windows_or_buffers_changed++;
7221 /* Last displayed message is now the current message. */
7222 echo_area_buffer[1] = echo_area_buffer[0];
7224 /* Prevent redisplay optimization in redisplay_internal by resetting
7225 this_line_start_pos. This is done because the mini-buffer now
7226 displays the message instead of its buffer text. */
7227 if (EQ (mini_window, selected_window))
7228 CHARPOS (this_line_start_pos) = 0;
7230 return window_height_changed_p;
7235 /***********************************************************************
7236 Frame Titles
7237 ***********************************************************************/
7240 /* The frame title buffering code is also used by Fformat_mode_line.
7241 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7243 /* A buffer for constructing frame titles in it; allocated from the
7244 heap in init_xdisp and resized as needed in store_frame_title_char. */
7246 static char *frame_title_buf;
7248 /* The buffer's end, and a current output position in it. */
7250 static char *frame_title_buf_end;
7251 static char *frame_title_ptr;
7254 /* Store a single character C for the frame title in frame_title_buf.
7255 Re-allocate frame_title_buf if necessary. */
7257 static void
7258 #ifdef PROTOTYPES
7259 store_frame_title_char (char c)
7260 #else
7261 store_frame_title_char (c)
7262 char c;
7263 #endif
7265 /* If output position has reached the end of the allocated buffer,
7266 double the buffer's size. */
7267 if (frame_title_ptr == frame_title_buf_end)
7269 int len = frame_title_ptr - frame_title_buf;
7270 int new_size = 2 * len * sizeof *frame_title_buf;
7271 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7272 frame_title_buf_end = frame_title_buf + new_size;
7273 frame_title_ptr = frame_title_buf + len;
7276 *frame_title_ptr++ = c;
7280 /* Store part of a frame title in frame_title_buf, beginning at
7281 frame_title_ptr. STR is the string to store. Do not copy
7282 characters that yield more columns than PRECISION; PRECISION <= 0
7283 means copy the whole string. Pad with spaces until FIELD_WIDTH
7284 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7285 pad. Called from display_mode_element when it is used to build a
7286 frame title. */
7288 static int
7289 store_frame_title (str, field_width, precision)
7290 const unsigned char *str;
7291 int field_width, precision;
7293 int n = 0;
7294 int dummy, nbytes;
7296 /* Copy at most PRECISION chars from STR. */
7297 nbytes = strlen (str);
7298 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7299 while (nbytes--)
7300 store_frame_title_char (*str++);
7302 /* Fill up with spaces until FIELD_WIDTH reached. */
7303 while (field_width > 0
7304 && n < field_width)
7306 store_frame_title_char (' ');
7307 ++n;
7310 return n;
7313 #ifdef HAVE_WINDOW_SYSTEM
7315 /* Set the title of FRAME, if it has changed. The title format is
7316 Vicon_title_format if FRAME is iconified, otherwise it is
7317 frame_title_format. */
7319 static void
7320 x_consider_frame_title (frame)
7321 Lisp_Object frame;
7323 struct frame *f = XFRAME (frame);
7325 if (FRAME_WINDOW_P (f)
7326 || FRAME_MINIBUF_ONLY_P (f)
7327 || f->explicit_name)
7329 /* Do we have more than one visible frame on this X display? */
7330 Lisp_Object tail;
7331 Lisp_Object fmt;
7332 struct buffer *obuf;
7333 int len;
7334 struct it it;
7336 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7338 Lisp_Object other_frame = XCAR (tail);
7339 struct frame *tf = XFRAME (other_frame);
7341 if (tf != f
7342 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7343 && !FRAME_MINIBUF_ONLY_P (tf)
7344 && !EQ (other_frame, tip_frame)
7345 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7346 break;
7349 /* Set global variable indicating that multiple frames exist. */
7350 multiple_frames = CONSP (tail);
7352 /* Switch to the buffer of selected window of the frame. Set up
7353 frame_title_ptr so that display_mode_element will output into it;
7354 then display the title. */
7355 obuf = current_buffer;
7356 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7357 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7358 frame_title_ptr = frame_title_buf;
7359 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7360 NULL, DEFAULT_FACE_ID);
7361 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7362 len = frame_title_ptr - frame_title_buf;
7363 frame_title_ptr = NULL;
7364 set_buffer_internal_1 (obuf);
7366 /* Set the title only if it's changed. This avoids consing in
7367 the common case where it hasn't. (If it turns out that we've
7368 already wasted too much time by walking through the list with
7369 display_mode_element, then we might need to optimize at a
7370 higher level than this.) */
7371 if (! STRINGP (f->name)
7372 || SBYTES (f->name) != len
7373 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7374 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7378 #endif /* not HAVE_WINDOW_SYSTEM */
7383 /***********************************************************************
7384 Menu Bars
7385 ***********************************************************************/
7388 /* Prepare for redisplay by updating menu-bar item lists when
7389 appropriate. This can call eval. */
7391 void
7392 prepare_menu_bars ()
7394 int all_windows;
7395 struct gcpro gcpro1, gcpro2;
7396 struct frame *f;
7397 Lisp_Object tooltip_frame;
7399 #ifdef HAVE_WINDOW_SYSTEM
7400 tooltip_frame = tip_frame;
7401 #else
7402 tooltip_frame = Qnil;
7403 #endif
7405 /* Update all frame titles based on their buffer names, etc. We do
7406 this before the menu bars so that the buffer-menu will show the
7407 up-to-date frame titles. */
7408 #ifdef HAVE_WINDOW_SYSTEM
7409 if (windows_or_buffers_changed || update_mode_lines)
7411 Lisp_Object tail, frame;
7413 FOR_EACH_FRAME (tail, frame)
7415 f = XFRAME (frame);
7416 if (!EQ (frame, tooltip_frame)
7417 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7418 x_consider_frame_title (frame);
7421 #endif /* HAVE_WINDOW_SYSTEM */
7423 /* Update the menu bar item lists, if appropriate. This has to be
7424 done before any actual redisplay or generation of display lines. */
7425 all_windows = (update_mode_lines
7426 || buffer_shared > 1
7427 || windows_or_buffers_changed);
7428 if (all_windows)
7430 Lisp_Object tail, frame;
7431 int count = SPECPDL_INDEX ();
7433 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7435 FOR_EACH_FRAME (tail, frame)
7437 f = XFRAME (frame);
7439 /* Ignore tooltip frame. */
7440 if (EQ (frame, tooltip_frame))
7441 continue;
7443 /* If a window on this frame changed size, report that to
7444 the user and clear the size-change flag. */
7445 if (FRAME_WINDOW_SIZES_CHANGED (f))
7447 Lisp_Object functions;
7449 /* Clear flag first in case we get an error below. */
7450 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7451 functions = Vwindow_size_change_functions;
7452 GCPRO2 (tail, functions);
7454 while (CONSP (functions))
7456 call1 (XCAR (functions), frame);
7457 functions = XCDR (functions);
7459 UNGCPRO;
7462 GCPRO1 (tail);
7463 update_menu_bar (f, 0);
7464 #ifdef HAVE_WINDOW_SYSTEM
7465 update_tool_bar (f, 0);
7466 #endif
7467 UNGCPRO;
7470 unbind_to (count, Qnil);
7472 else
7474 struct frame *sf = SELECTED_FRAME ();
7475 update_menu_bar (sf, 1);
7476 #ifdef HAVE_WINDOW_SYSTEM
7477 update_tool_bar (sf, 1);
7478 #endif
7481 /* Motif needs this. See comment in xmenu.c. Turn it off when
7482 pending_menu_activation is not defined. */
7483 #ifdef USE_X_TOOLKIT
7484 pending_menu_activation = 0;
7485 #endif
7489 /* Update the menu bar item list for frame F. This has to be done
7490 before we start to fill in any display lines, because it can call
7491 eval.
7493 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7495 static void
7496 update_menu_bar (f, save_match_data)
7497 struct frame *f;
7498 int save_match_data;
7500 Lisp_Object window;
7501 register struct window *w;
7503 /* If called recursively during a menu update, do nothing. This can
7504 happen when, for instance, an activate-menubar-hook causes a
7505 redisplay. */
7506 if (inhibit_menubar_update)
7507 return;
7509 window = FRAME_SELECTED_WINDOW (f);
7510 w = XWINDOW (window);
7512 #if 0 /* The if statement below this if statement used to include the
7513 condition !NILP (w->update_mode_line), rather than using
7514 update_mode_lines directly, and this if statement may have
7515 been added to make that condition work. Now the if
7516 statement below matches its comment, this isn't needed. */
7517 if (update_mode_lines)
7518 w->update_mode_line = Qt;
7519 #endif
7521 if (FRAME_WINDOW_P (f)
7523 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7524 || defined (USE_GTK)
7525 FRAME_EXTERNAL_MENU_BAR (f)
7526 #else
7527 FRAME_MENU_BAR_LINES (f) > 0
7528 #endif
7529 : FRAME_MENU_BAR_LINES (f) > 0)
7531 /* If the user has switched buffers or windows, we need to
7532 recompute to reflect the new bindings. But we'll
7533 recompute when update_mode_lines is set too; that means
7534 that people can use force-mode-line-update to request
7535 that the menu bar be recomputed. The adverse effect on
7536 the rest of the redisplay algorithm is about the same as
7537 windows_or_buffers_changed anyway. */
7538 if (windows_or_buffers_changed
7539 /* This used to test w->update_mode_line, but we believe
7540 there is no need to recompute the menu in that case. */
7541 || update_mode_lines
7542 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7543 < BUF_MODIFF (XBUFFER (w->buffer)))
7544 != !NILP (w->last_had_star))
7545 || ((!NILP (Vtransient_mark_mode)
7546 && !NILP (XBUFFER (w->buffer)->mark_active))
7547 != !NILP (w->region_showing)))
7549 struct buffer *prev = current_buffer;
7550 int count = SPECPDL_INDEX ();
7552 specbind (Qinhibit_menubar_update, Qt);
7554 set_buffer_internal_1 (XBUFFER (w->buffer));
7555 if (save_match_data)
7556 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7557 if (NILP (Voverriding_local_map_menu_flag))
7559 specbind (Qoverriding_terminal_local_map, Qnil);
7560 specbind (Qoverriding_local_map, Qnil);
7563 /* Run the Lucid hook. */
7564 safe_run_hooks (Qactivate_menubar_hook);
7566 /* If it has changed current-menubar from previous value,
7567 really recompute the menu-bar from the value. */
7568 if (! NILP (Vlucid_menu_bar_dirty_flag))
7569 call0 (Qrecompute_lucid_menubar);
7571 safe_run_hooks (Qmenu_bar_update_hook);
7572 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7574 /* Redisplay the menu bar in case we changed it. */
7575 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7576 || defined (USE_GTK)
7577 if (FRAME_WINDOW_P (f)
7578 #if defined (MAC_OS)
7579 /* All frames on Mac OS share the same menubar. So only the
7580 selected frame should be allowed to set it. */
7581 && f == SELECTED_FRAME ()
7582 #endif
7584 set_frame_menubar (f, 0, 0);
7585 else
7586 /* On a terminal screen, the menu bar is an ordinary screen
7587 line, and this makes it get updated. */
7588 w->update_mode_line = Qt;
7589 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7590 /* In the non-toolkit version, the menu bar is an ordinary screen
7591 line, and this makes it get updated. */
7592 w->update_mode_line = Qt;
7593 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7595 unbind_to (count, Qnil);
7596 set_buffer_internal_1 (prev);
7603 /***********************************************************************
7604 Tool-bars
7605 ***********************************************************************/
7607 #ifdef HAVE_WINDOW_SYSTEM
7609 /* Update the tool-bar item list for frame F. This has to be done
7610 before we start to fill in any display lines. Called from
7611 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7612 and restore it here. */
7614 static void
7615 update_tool_bar (f, save_match_data)
7616 struct frame *f;
7617 int save_match_data;
7619 #ifdef USE_GTK
7620 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
7621 #else
7622 int do_update = WINDOWP (f->tool_bar_window)
7623 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
7624 #endif
7626 if (do_update)
7628 Lisp_Object window;
7629 struct window *w;
7631 window = FRAME_SELECTED_WINDOW (f);
7632 w = XWINDOW (window);
7634 /* If the user has switched buffers or windows, we need to
7635 recompute to reflect the new bindings. But we'll
7636 recompute when update_mode_lines is set too; that means
7637 that people can use force-mode-line-update to request
7638 that the menu bar be recomputed. The adverse effect on
7639 the rest of the redisplay algorithm is about the same as
7640 windows_or_buffers_changed anyway. */
7641 if (windows_or_buffers_changed
7642 || !NILP (w->update_mode_line)
7643 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7644 < BUF_MODIFF (XBUFFER (w->buffer)))
7645 != !NILP (w->last_had_star))
7646 || ((!NILP (Vtransient_mark_mode)
7647 && !NILP (XBUFFER (w->buffer)->mark_active))
7648 != !NILP (w->region_showing)))
7650 struct buffer *prev = current_buffer;
7651 int count = SPECPDL_INDEX ();
7653 /* Set current_buffer to the buffer of the selected
7654 window of the frame, so that we get the right local
7655 keymaps. */
7656 set_buffer_internal_1 (XBUFFER (w->buffer));
7658 /* Save match data, if we must. */
7659 if (save_match_data)
7660 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7662 /* Make sure that we don't accidentally use bogus keymaps. */
7663 if (NILP (Voverriding_local_map_menu_flag))
7665 specbind (Qoverriding_terminal_local_map, Qnil);
7666 specbind (Qoverriding_local_map, Qnil);
7669 /* Build desired tool-bar items from keymaps. */
7670 f->tool_bar_items
7671 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7673 /* Redisplay the tool-bar in case we changed it. */
7674 w->update_mode_line = Qt;
7676 unbind_to (count, Qnil);
7677 set_buffer_internal_1 (prev);
7683 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7684 F's desired tool-bar contents. F->tool_bar_items must have
7685 been set up previously by calling prepare_menu_bars. */
7687 static void
7688 build_desired_tool_bar_string (f)
7689 struct frame *f;
7691 int i, size, size_needed;
7692 struct gcpro gcpro1, gcpro2, gcpro3;
7693 Lisp_Object image, plist, props;
7695 image = plist = props = Qnil;
7696 GCPRO3 (image, plist, props);
7698 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7699 Otherwise, make a new string. */
7701 /* The size of the string we might be able to reuse. */
7702 size = (STRINGP (f->desired_tool_bar_string)
7703 ? SCHARS (f->desired_tool_bar_string)
7704 : 0);
7706 /* We need one space in the string for each image. */
7707 size_needed = f->n_tool_bar_items;
7709 /* Reuse f->desired_tool_bar_string, if possible. */
7710 if (size < size_needed || NILP (f->desired_tool_bar_string))
7711 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7712 make_number (' '));
7713 else
7715 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7716 Fremove_text_properties (make_number (0), make_number (size),
7717 props, f->desired_tool_bar_string);
7720 /* Put a `display' property on the string for the images to display,
7721 put a `menu_item' property on tool-bar items with a value that
7722 is the index of the item in F's tool-bar item vector. */
7723 for (i = 0; i < f->n_tool_bar_items; ++i)
7725 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7727 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7728 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7729 int hmargin, vmargin, relief, idx, end;
7730 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7732 /* If image is a vector, choose the image according to the
7733 button state. */
7734 image = PROP (TOOL_BAR_ITEM_IMAGES);
7735 if (VECTORP (image))
7737 if (enabled_p)
7738 idx = (selected_p
7739 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7740 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7741 else
7742 idx = (selected_p
7743 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7744 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7746 xassert (ASIZE (image) >= idx);
7747 image = AREF (image, idx);
7749 else
7750 idx = -1;
7752 /* Ignore invalid image specifications. */
7753 if (!valid_image_p (image))
7754 continue;
7756 /* Display the tool-bar button pressed, or depressed. */
7757 plist = Fcopy_sequence (XCDR (image));
7759 /* Compute margin and relief to draw. */
7760 relief = (tool_bar_button_relief >= 0
7761 ? tool_bar_button_relief
7762 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7763 hmargin = vmargin = relief;
7765 if (INTEGERP (Vtool_bar_button_margin)
7766 && XINT (Vtool_bar_button_margin) > 0)
7768 hmargin += XFASTINT (Vtool_bar_button_margin);
7769 vmargin += XFASTINT (Vtool_bar_button_margin);
7771 else if (CONSP (Vtool_bar_button_margin))
7773 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7774 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7775 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7777 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7778 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7779 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7782 if (auto_raise_tool_bar_buttons_p)
7784 /* Add a `:relief' property to the image spec if the item is
7785 selected. */
7786 if (selected_p)
7788 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7789 hmargin -= relief;
7790 vmargin -= relief;
7793 else
7795 /* If image is selected, display it pressed, i.e. with a
7796 negative relief. If it's not selected, display it with a
7797 raised relief. */
7798 plist = Fplist_put (plist, QCrelief,
7799 (selected_p
7800 ? make_number (-relief)
7801 : make_number (relief)));
7802 hmargin -= relief;
7803 vmargin -= relief;
7806 /* Put a margin around the image. */
7807 if (hmargin || vmargin)
7809 if (hmargin == vmargin)
7810 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7811 else
7812 plist = Fplist_put (plist, QCmargin,
7813 Fcons (make_number (hmargin),
7814 make_number (vmargin)));
7817 /* If button is not enabled, and we don't have special images
7818 for the disabled state, make the image appear disabled by
7819 applying an appropriate algorithm to it. */
7820 if (!enabled_p && idx < 0)
7821 plist = Fplist_put (plist, QCconversion, Qdisabled);
7823 /* Put a `display' text property on the string for the image to
7824 display. Put a `menu-item' property on the string that gives
7825 the start of this item's properties in the tool-bar items
7826 vector. */
7827 image = Fcons (Qimage, plist);
7828 props = list4 (Qdisplay, image,
7829 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7831 /* Let the last image hide all remaining spaces in the tool bar
7832 string. The string can be longer than needed when we reuse a
7833 previous string. */
7834 if (i + 1 == f->n_tool_bar_items)
7835 end = SCHARS (f->desired_tool_bar_string);
7836 else
7837 end = i + 1;
7838 Fadd_text_properties (make_number (i), make_number (end),
7839 props, f->desired_tool_bar_string);
7840 #undef PROP
7843 UNGCPRO;
7847 /* Display one line of the tool-bar of frame IT->f. */
7849 static void
7850 display_tool_bar_line (it)
7851 struct it *it;
7853 struct glyph_row *row = it->glyph_row;
7854 int max_x = it->last_visible_x;
7855 struct glyph *last;
7857 prepare_desired_row (row);
7858 row->y = it->current_y;
7860 /* Note that this isn't made use of if the face hasn't a box,
7861 so there's no need to check the face here. */
7862 it->start_of_box_run_p = 1;
7864 while (it->current_x < max_x)
7866 int x_before, x, n_glyphs_before, i, nglyphs;
7868 /* Get the next display element. */
7869 if (!get_next_display_element (it))
7870 break;
7872 /* Produce glyphs. */
7873 x_before = it->current_x;
7874 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7875 PRODUCE_GLYPHS (it);
7877 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7878 i = 0;
7879 x = x_before;
7880 while (i < nglyphs)
7882 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7884 if (x + glyph->pixel_width > max_x)
7886 /* Glyph doesn't fit on line. */
7887 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7888 it->current_x = x;
7889 goto out;
7892 ++it->hpos;
7893 x += glyph->pixel_width;
7894 ++i;
7897 /* Stop at line ends. */
7898 if (ITERATOR_AT_END_OF_LINE_P (it))
7899 break;
7901 set_iterator_to_next (it, 1);
7904 out:;
7906 row->displays_text_p = row->used[TEXT_AREA] != 0;
7907 extend_face_to_end_of_line (it);
7908 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7909 last->right_box_line_p = 1;
7910 if (last == row->glyphs[TEXT_AREA])
7911 last->left_box_line_p = 1;
7912 compute_line_metrics (it);
7914 /* If line is empty, make it occupy the rest of the tool-bar. */
7915 if (!row->displays_text_p)
7917 row->height = row->phys_height = it->last_visible_y - row->y;
7918 row->ascent = row->phys_ascent = 0;
7921 row->full_width_p = 1;
7922 row->continued_p = 0;
7923 row->truncated_on_left_p = 0;
7924 row->truncated_on_right_p = 0;
7926 it->current_x = it->hpos = 0;
7927 it->current_y += row->height;
7928 ++it->vpos;
7929 ++it->glyph_row;
7933 /* Value is the number of screen lines needed to make all tool-bar
7934 items of frame F visible. */
7936 static int
7937 tool_bar_lines_needed (f)
7938 struct frame *f;
7940 struct window *w = XWINDOW (f->tool_bar_window);
7941 struct it it;
7943 /* Initialize an iterator for iteration over
7944 F->desired_tool_bar_string in the tool-bar window of frame F. */
7945 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7946 it.first_visible_x = 0;
7947 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7948 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7950 while (!ITERATOR_AT_END_P (&it))
7952 it.glyph_row = w->desired_matrix->rows;
7953 clear_glyph_row (it.glyph_row);
7954 display_tool_bar_line (&it);
7957 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7961 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7962 0, 1, 0,
7963 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7964 (frame)
7965 Lisp_Object frame;
7967 struct frame *f;
7968 struct window *w;
7969 int nlines = 0;
7971 if (NILP (frame))
7972 frame = selected_frame;
7973 else
7974 CHECK_FRAME (frame);
7975 f = XFRAME (frame);
7977 if (WINDOWP (f->tool_bar_window)
7978 || (w = XWINDOW (f->tool_bar_window),
7979 XFASTINT (w->height) > 0))
7981 update_tool_bar (f, 1);
7982 if (f->n_tool_bar_items)
7984 build_desired_tool_bar_string (f);
7985 nlines = tool_bar_lines_needed (f);
7989 return make_number (nlines);
7993 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7994 height should be changed. */
7996 static int
7997 redisplay_tool_bar (f)
7998 struct frame *f;
8000 struct window *w;
8001 struct it it;
8002 struct glyph_row *row;
8003 int change_height_p = 0;
8005 #ifdef USE_GTK
8006 if (FRAME_EXTERNAL_TOOL_BAR(f))
8007 update_frame_tool_bar (f);
8008 return 0;
8009 #endif
8011 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8012 do anything. This means you must start with tool-bar-lines
8013 non-zero to get the auto-sizing effect. Or in other words, you
8014 can turn off tool-bars by specifying tool-bar-lines zero. */
8015 if (!WINDOWP (f->tool_bar_window)
8016 || (w = XWINDOW (f->tool_bar_window),
8017 XFASTINT (w->height) == 0))
8018 return 0;
8020 /* Set up an iterator for the tool-bar window. */
8021 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8022 it.first_visible_x = 0;
8023 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8024 row = it.glyph_row;
8026 /* Build a string that represents the contents of the tool-bar. */
8027 build_desired_tool_bar_string (f);
8028 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8030 /* Display as many lines as needed to display all tool-bar items. */
8031 while (it.current_y < it.last_visible_y)
8032 display_tool_bar_line (&it);
8034 /* It doesn't make much sense to try scrolling in the tool-bar
8035 window, so don't do it. */
8036 w->desired_matrix->no_scrolling_p = 1;
8037 w->must_be_updated_p = 1;
8039 if (auto_resize_tool_bars_p)
8041 int nlines;
8043 /* If we couldn't display everything, change the tool-bar's
8044 height. */
8045 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8046 change_height_p = 1;
8048 /* If there are blank lines at the end, except for a partially
8049 visible blank line at the end that is smaller than
8050 CANON_Y_UNIT, change the tool-bar's height. */
8051 row = it.glyph_row - 1;
8052 if (!row->displays_text_p
8053 && row->height >= CANON_Y_UNIT (f))
8054 change_height_p = 1;
8056 /* If row displays tool-bar items, but is partially visible,
8057 change the tool-bar's height. */
8058 if (row->displays_text_p
8059 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8060 change_height_p = 1;
8062 /* Resize windows as needed by changing the `tool-bar-lines'
8063 frame parameter. */
8064 if (change_height_p
8065 && (nlines = tool_bar_lines_needed (f),
8066 nlines != XFASTINT (w->height)))
8068 extern Lisp_Object Qtool_bar_lines;
8069 Lisp_Object frame;
8070 int old_height = XFASTINT (w->height);
8072 XSETFRAME (frame, f);
8073 clear_glyph_matrix (w->desired_matrix);
8074 Fmodify_frame_parameters (frame,
8075 Fcons (Fcons (Qtool_bar_lines,
8076 make_number (nlines)),
8077 Qnil));
8078 if (XFASTINT (w->height) != old_height)
8079 fonts_changed_p = 1;
8083 return change_height_p;
8087 /* Get information about the tool-bar item which is displayed in GLYPH
8088 on frame F. Return in *PROP_IDX the index where tool-bar item
8089 properties start in F->tool_bar_items. Value is zero if
8090 GLYPH doesn't display a tool-bar item. */
8093 tool_bar_item_info (f, glyph, prop_idx)
8094 struct frame *f;
8095 struct glyph *glyph;
8096 int *prop_idx;
8098 Lisp_Object prop;
8099 int success_p;
8100 int charpos;
8102 /* This function can be called asynchronously, which means we must
8103 exclude any possibility that Fget_text_property signals an
8104 error. */
8105 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8106 charpos = max (0, charpos);
8108 /* Get the text property `menu-item' at pos. The value of that
8109 property is the start index of this item's properties in
8110 F->tool_bar_items. */
8111 prop = Fget_text_property (make_number (charpos),
8112 Qmenu_item, f->current_tool_bar_string);
8113 if (INTEGERP (prop))
8115 *prop_idx = XINT (prop);
8116 success_p = 1;
8118 else
8119 success_p = 0;
8121 return success_p;
8124 #endif /* HAVE_WINDOW_SYSTEM */
8128 /************************************************************************
8129 Horizontal scrolling
8130 ************************************************************************/
8132 static int hscroll_window_tree P_ ((Lisp_Object));
8133 static int hscroll_windows P_ ((Lisp_Object));
8135 /* For all leaf windows in the window tree rooted at WINDOW, set their
8136 hscroll value so that PT is (i) visible in the window, and (ii) so
8137 that it is not within a certain margin at the window's left and
8138 right border. Value is non-zero if any window's hscroll has been
8139 changed. */
8141 static int
8142 hscroll_window_tree (window)
8143 Lisp_Object window;
8145 int hscrolled_p = 0;
8146 int hscroll_relative_p = FLOATP (Vhscroll_step);
8147 int hscroll_step_abs = 0;
8148 double hscroll_step_rel = 0;
8150 if (hscroll_relative_p)
8152 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8153 if (hscroll_step_rel < 0)
8155 hscroll_relative_p = 0;
8156 hscroll_step_abs = 0;
8159 else if (INTEGERP (Vhscroll_step))
8161 hscroll_step_abs = XINT (Vhscroll_step);
8162 if (hscroll_step_abs < 0)
8163 hscroll_step_abs = 0;
8165 else
8166 hscroll_step_abs = 0;
8168 while (WINDOWP (window))
8170 struct window *w = XWINDOW (window);
8172 if (WINDOWP (w->hchild))
8173 hscrolled_p |= hscroll_window_tree (w->hchild);
8174 else if (WINDOWP (w->vchild))
8175 hscrolled_p |= hscroll_window_tree (w->vchild);
8176 else if (w->cursor.vpos >= 0)
8178 int h_margin, text_area_x, text_area_y;
8179 int text_area_width, text_area_height;
8180 struct glyph_row *current_cursor_row
8181 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8182 struct glyph_row *desired_cursor_row
8183 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8184 struct glyph_row *cursor_row
8185 = (desired_cursor_row->enabled_p
8186 ? desired_cursor_row
8187 : current_cursor_row);
8189 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8190 &text_area_width, &text_area_height);
8192 /* Scroll when cursor is inside this scroll margin. */
8193 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8195 if ((XFASTINT (w->hscroll)
8196 && w->cursor.x <= h_margin)
8197 || (cursor_row->enabled_p
8198 && cursor_row->truncated_on_right_p
8199 && (w->cursor.x >= text_area_width - h_margin)))
8201 struct it it;
8202 int hscroll;
8203 struct buffer *saved_current_buffer;
8204 int pt;
8205 int wanted_x;
8207 /* Find point in a display of infinite width. */
8208 saved_current_buffer = current_buffer;
8209 current_buffer = XBUFFER (w->buffer);
8211 if (w == XWINDOW (selected_window))
8212 pt = BUF_PT (current_buffer);
8213 else
8215 pt = marker_position (w->pointm);
8216 pt = max (BEGV, pt);
8217 pt = min (ZV, pt);
8220 /* Move iterator to pt starting at cursor_row->start in
8221 a line with infinite width. */
8222 init_to_row_start (&it, w, cursor_row);
8223 it.last_visible_x = INFINITY;
8224 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8225 current_buffer = saved_current_buffer;
8227 /* Position cursor in window. */
8228 if (!hscroll_relative_p && hscroll_step_abs == 0)
8229 hscroll = max (0, it.current_x - text_area_width / 2)
8230 / CANON_X_UNIT (it.f);
8231 else if (w->cursor.x >= text_area_width - h_margin)
8233 if (hscroll_relative_p)
8234 wanted_x = text_area_width * (1 - hscroll_step_rel)
8235 - h_margin;
8236 else
8237 wanted_x = text_area_width
8238 - hscroll_step_abs * CANON_X_UNIT (it.f)
8239 - h_margin;
8240 hscroll
8241 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8243 else
8245 if (hscroll_relative_p)
8246 wanted_x = text_area_width * hscroll_step_rel
8247 + h_margin;
8248 else
8249 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8250 + h_margin;
8251 hscroll
8252 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8254 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8256 /* Don't call Fset_window_hscroll if value hasn't
8257 changed because it will prevent redisplay
8258 optimizations. */
8259 if (XFASTINT (w->hscroll) != hscroll)
8261 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8262 w->hscroll = make_number (hscroll);
8263 hscrolled_p = 1;
8268 window = w->next;
8271 /* Value is non-zero if hscroll of any leaf window has been changed. */
8272 return hscrolled_p;
8276 /* Set hscroll so that cursor is visible and not inside horizontal
8277 scroll margins for all windows in the tree rooted at WINDOW. See
8278 also hscroll_window_tree above. Value is non-zero if any window's
8279 hscroll has been changed. If it has, desired matrices on the frame
8280 of WINDOW are cleared. */
8282 static int
8283 hscroll_windows (window)
8284 Lisp_Object window;
8286 int hscrolled_p;
8288 if (automatic_hscrolling_p)
8290 hscrolled_p = hscroll_window_tree (window);
8291 if (hscrolled_p)
8292 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8294 else
8295 hscrolled_p = 0;
8296 return hscrolled_p;
8301 /************************************************************************
8302 Redisplay
8303 ************************************************************************/
8305 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8306 to a non-zero value. This is sometimes handy to have in a debugger
8307 session. */
8309 #if GLYPH_DEBUG
8311 /* First and last unchanged row for try_window_id. */
8313 int debug_first_unchanged_at_end_vpos;
8314 int debug_last_unchanged_at_beg_vpos;
8316 /* Delta vpos and y. */
8318 int debug_dvpos, debug_dy;
8320 /* Delta in characters and bytes for try_window_id. */
8322 int debug_delta, debug_delta_bytes;
8324 /* Values of window_end_pos and window_end_vpos at the end of
8325 try_window_id. */
8327 EMACS_INT debug_end_pos, debug_end_vpos;
8329 /* Append a string to W->desired_matrix->method. FMT is a printf
8330 format string. A1...A9 are a supplement for a variable-length
8331 argument list. If trace_redisplay_p is non-zero also printf the
8332 resulting string to stderr. */
8334 static void
8335 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8336 struct window *w;
8337 char *fmt;
8338 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8340 char buffer[512];
8341 char *method = w->desired_matrix->method;
8342 int len = strlen (method);
8343 int size = sizeof w->desired_matrix->method;
8344 int remaining = size - len - 1;
8346 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8347 if (len && remaining)
8349 method[len] = '|';
8350 --remaining, ++len;
8353 strncpy (method + len, buffer, remaining);
8355 if (trace_redisplay_p)
8356 fprintf (stderr, "%p (%s): %s\n",
8358 ((BUFFERP (w->buffer)
8359 && STRINGP (XBUFFER (w->buffer)->name))
8360 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8361 : "no buffer"),
8362 buffer);
8365 #endif /* GLYPH_DEBUG */
8368 /* Value is non-zero if all changes in window W, which displays
8369 current_buffer, are in the text between START and END. START is a
8370 buffer position, END is given as a distance from Z. Used in
8371 redisplay_internal for display optimization. */
8373 static INLINE int
8374 text_outside_line_unchanged_p (w, start, end)
8375 struct window *w;
8376 int start, end;
8378 int unchanged_p = 1;
8380 /* If text or overlays have changed, see where. */
8381 if (XFASTINT (w->last_modified) < MODIFF
8382 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8384 /* Gap in the line? */
8385 if (GPT < start || Z - GPT < end)
8386 unchanged_p = 0;
8388 /* Changes start in front of the line, or end after it? */
8389 if (unchanged_p
8390 && (BEG_UNCHANGED < start - 1
8391 || END_UNCHANGED < end))
8392 unchanged_p = 0;
8394 /* If selective display, can't optimize if changes start at the
8395 beginning of the line. */
8396 if (unchanged_p
8397 && INTEGERP (current_buffer->selective_display)
8398 && XINT (current_buffer->selective_display) > 0
8399 && (BEG_UNCHANGED < start || GPT <= start))
8400 unchanged_p = 0;
8402 /* If there are overlays at the start or end of the line, these
8403 may have overlay strings with newlines in them. A change at
8404 START, for instance, may actually concern the display of such
8405 overlay strings as well, and they are displayed on different
8406 lines. So, quickly rule out this case. (For the future, it
8407 might be desirable to implement something more telling than
8408 just BEG/END_UNCHANGED.) */
8409 if (unchanged_p)
8411 if (BEG + BEG_UNCHANGED == start
8412 && overlay_touches_p (start))
8413 unchanged_p = 0;
8414 if (END_UNCHANGED == end
8415 && overlay_touches_p (Z - end))
8416 unchanged_p = 0;
8420 return unchanged_p;
8424 /* Do a frame update, taking possible shortcuts into account. This is
8425 the main external entry point for redisplay.
8427 If the last redisplay displayed an echo area message and that message
8428 is no longer requested, we clear the echo area or bring back the
8429 mini-buffer if that is in use. */
8431 void
8432 redisplay ()
8434 redisplay_internal (0);
8438 /* Return 1 if point moved out of or into a composition. Otherwise
8439 return 0. PREV_BUF and PREV_PT are the last point buffer and
8440 position. BUF and PT are the current point buffer and position. */
8443 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8444 struct buffer *prev_buf, *buf;
8445 int prev_pt, pt;
8447 int start, end;
8448 Lisp_Object prop;
8449 Lisp_Object buffer;
8451 XSETBUFFER (buffer, buf);
8452 /* Check a composition at the last point if point moved within the
8453 same buffer. */
8454 if (prev_buf == buf)
8456 if (prev_pt == pt)
8457 /* Point didn't move. */
8458 return 0;
8460 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8461 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8462 && COMPOSITION_VALID_P (start, end, prop)
8463 && start < prev_pt && end > prev_pt)
8464 /* The last point was within the composition. Return 1 iff
8465 point moved out of the composition. */
8466 return (pt <= start || pt >= end);
8469 /* Check a composition at the current point. */
8470 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8471 && find_composition (pt, -1, &start, &end, &prop, buffer)
8472 && COMPOSITION_VALID_P (start, end, prop)
8473 && start < pt && end > pt);
8477 /* Reconsider the setting of B->clip_changed which is displayed
8478 in window W. */
8480 static INLINE void
8481 reconsider_clip_changes (w, b)
8482 struct window *w;
8483 struct buffer *b;
8485 if (b->clip_changed
8486 && !NILP (w->window_end_valid)
8487 && w->current_matrix->buffer == b
8488 && w->current_matrix->zv == BUF_ZV (b)
8489 && w->current_matrix->begv == BUF_BEGV (b))
8490 b->clip_changed = 0;
8492 /* If display wasn't paused, and W is not a tool bar window, see if
8493 point has been moved into or out of a composition. In that case,
8494 we set b->clip_changed to 1 to force updating the screen. If
8495 b->clip_changed has already been set to 1, we can skip this
8496 check. */
8497 if (!b->clip_changed
8498 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8500 int pt;
8502 if (w == XWINDOW (selected_window))
8503 pt = BUF_PT (current_buffer);
8504 else
8505 pt = marker_position (w->pointm);
8507 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8508 || pt != XINT (w->last_point))
8509 && check_point_in_composition (w->current_matrix->buffer,
8510 XINT (w->last_point),
8511 XBUFFER (w->buffer), pt))
8512 b->clip_changed = 1;
8516 #define STOP_POLLING \
8517 do { if (! polling_stopped_here) stop_polling (); \
8518 polling_stopped_here = 1; } while (0)
8520 #define RESUME_POLLING \
8521 do { if (polling_stopped_here) start_polling (); \
8522 polling_stopped_here = 0; } while (0)
8525 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8526 response to any user action; therefore, we should preserve the echo
8527 area. (Actually, our caller does that job.) Perhaps in the future
8528 avoid recentering windows if it is not necessary; currently that
8529 causes some problems. */
8531 static void
8532 redisplay_internal (preserve_echo_area)
8533 int preserve_echo_area;
8535 struct window *w = XWINDOW (selected_window);
8536 struct frame *f = XFRAME (w->frame);
8537 int pause;
8538 int must_finish = 0;
8539 struct text_pos tlbufpos, tlendpos;
8540 int number_of_visible_frames;
8541 int count;
8542 struct frame *sf = SELECTED_FRAME ();
8543 int polling_stopped_here = 0;
8545 /* Non-zero means redisplay has to consider all windows on all
8546 frames. Zero means, only selected_window is considered. */
8547 int consider_all_windows_p;
8549 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8551 /* No redisplay if running in batch mode or frame is not yet fully
8552 initialized, or redisplay is explicitly turned off by setting
8553 Vinhibit_redisplay. */
8554 if (noninteractive
8555 || !NILP (Vinhibit_redisplay)
8556 || !f->glyphs_initialized_p)
8557 return;
8559 /* The flag redisplay_performed_directly_p is set by
8560 direct_output_for_insert when it already did the whole screen
8561 update necessary. */
8562 if (redisplay_performed_directly_p)
8564 redisplay_performed_directly_p = 0;
8565 if (!hscroll_windows (selected_window))
8566 return;
8569 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8570 if (popup_activated ())
8571 return;
8572 #endif
8574 /* I don't think this happens but let's be paranoid. */
8575 if (redisplaying_p)
8576 return;
8578 /* Record a function that resets redisplaying_p to its old value
8579 when we leave this function. */
8580 count = SPECPDL_INDEX ();
8581 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8582 ++redisplaying_p;
8583 specbind (Qinhibit_free_realized_faces, Qnil);
8585 retry:
8586 pause = 0;
8587 reconsider_clip_changes (w, current_buffer);
8589 /* If new fonts have been loaded that make a glyph matrix adjustment
8590 necessary, do it. */
8591 if (fonts_changed_p)
8593 adjust_glyphs (NULL);
8594 ++windows_or_buffers_changed;
8595 fonts_changed_p = 0;
8598 /* If face_change_count is non-zero, init_iterator will free all
8599 realized faces, which includes the faces referenced from current
8600 matrices. So, we can't reuse current matrices in this case. */
8601 if (face_change_count)
8602 ++windows_or_buffers_changed;
8604 if (! FRAME_WINDOW_P (sf)
8605 && previous_terminal_frame != sf)
8607 /* Since frames on an ASCII terminal share the same display
8608 area, displaying a different frame means redisplay the whole
8609 thing. */
8610 windows_or_buffers_changed++;
8611 SET_FRAME_GARBAGED (sf);
8612 XSETFRAME (Vterminal_frame, sf);
8614 previous_terminal_frame = sf;
8616 /* Set the visible flags for all frames. Do this before checking
8617 for resized or garbaged frames; they want to know if their frames
8618 are visible. See the comment in frame.h for
8619 FRAME_SAMPLE_VISIBILITY. */
8621 Lisp_Object tail, frame;
8623 number_of_visible_frames = 0;
8625 FOR_EACH_FRAME (tail, frame)
8627 struct frame *f = XFRAME (frame);
8629 FRAME_SAMPLE_VISIBILITY (f);
8630 if (FRAME_VISIBLE_P (f))
8631 ++number_of_visible_frames;
8632 clear_desired_matrices (f);
8636 /* Notice any pending interrupt request to change frame size. */
8637 do_pending_window_change (1);
8639 /* Clear frames marked as garbaged. */
8640 if (frame_garbaged)
8641 clear_garbaged_frames ();
8643 /* Build menubar and tool-bar items. */
8644 prepare_menu_bars ();
8646 if (windows_or_buffers_changed)
8647 update_mode_lines++;
8649 /* Detect case that we need to write or remove a star in the mode line. */
8650 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8652 w->update_mode_line = Qt;
8653 if (buffer_shared > 1)
8654 update_mode_lines++;
8657 /* If %c is in the mode line, update it if needed. */
8658 if (!NILP (w->column_number_displayed)
8659 /* This alternative quickly identifies a common case
8660 where no change is needed. */
8661 && !(PT == XFASTINT (w->last_point)
8662 && XFASTINT (w->last_modified) >= MODIFF
8663 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8664 && (XFASTINT (w->column_number_displayed)
8665 != (int) current_column ())) /* iftc */
8666 w->update_mode_line = Qt;
8668 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8670 /* The variable buffer_shared is set in redisplay_window and
8671 indicates that we redisplay a buffer in different windows. See
8672 there. */
8673 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8674 || cursor_type_changed);
8676 /* If specs for an arrow have changed, do thorough redisplay
8677 to ensure we remove any arrow that should no longer exist. */
8678 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8679 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8680 consider_all_windows_p = windows_or_buffers_changed = 1;
8682 /* Normally the message* functions will have already displayed and
8683 updated the echo area, but the frame may have been trashed, or
8684 the update may have been preempted, so display the echo area
8685 again here. Checking message_cleared_p captures the case that
8686 the echo area should be cleared. */
8687 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8688 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8689 || (message_cleared_p
8690 && minibuf_level == 0
8691 /* If the mini-window is currently selected, this means the
8692 echo-area doesn't show through. */
8693 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8695 int window_height_changed_p = echo_area_display (0);
8696 must_finish = 1;
8698 /* If we don't display the current message, don't clear the
8699 message_cleared_p flag, because, if we did, we wouldn't clear
8700 the echo area in the next redisplay which doesn't preserve
8701 the echo area. */
8702 if (!display_last_displayed_message_p)
8703 message_cleared_p = 0;
8705 if (fonts_changed_p)
8706 goto retry;
8707 else if (window_height_changed_p)
8709 consider_all_windows_p = 1;
8710 ++update_mode_lines;
8711 ++windows_or_buffers_changed;
8713 /* If window configuration was changed, frames may have been
8714 marked garbaged. Clear them or we will experience
8715 surprises wrt scrolling. */
8716 if (frame_garbaged)
8717 clear_garbaged_frames ();
8720 else if (EQ (selected_window, minibuf_window)
8721 && (current_buffer->clip_changed
8722 || XFASTINT (w->last_modified) < MODIFF
8723 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8724 && resize_mini_window (w, 0))
8726 /* Resized active mini-window to fit the size of what it is
8727 showing if its contents might have changed. */
8728 must_finish = 1;
8729 consider_all_windows_p = 1;
8730 ++windows_or_buffers_changed;
8731 ++update_mode_lines;
8733 /* If window configuration was changed, frames may have been
8734 marked garbaged. Clear them or we will experience
8735 surprises wrt scrolling. */
8736 if (frame_garbaged)
8737 clear_garbaged_frames ();
8741 /* If showing the region, and mark has changed, we must redisplay
8742 the whole window. The assignment to this_line_start_pos prevents
8743 the optimization directly below this if-statement. */
8744 if (((!NILP (Vtransient_mark_mode)
8745 && !NILP (XBUFFER (w->buffer)->mark_active))
8746 != !NILP (w->region_showing))
8747 || (!NILP (w->region_showing)
8748 && !EQ (w->region_showing,
8749 Fmarker_position (XBUFFER (w->buffer)->mark))))
8750 CHARPOS (this_line_start_pos) = 0;
8752 /* Optimize the case that only the line containing the cursor in the
8753 selected window has changed. Variables starting with this_ are
8754 set in display_line and record information about the line
8755 containing the cursor. */
8756 tlbufpos = this_line_start_pos;
8757 tlendpos = this_line_end_pos;
8758 if (!consider_all_windows_p
8759 && CHARPOS (tlbufpos) > 0
8760 && NILP (w->update_mode_line)
8761 && !current_buffer->clip_changed
8762 && !current_buffer->prevent_redisplay_optimizations_p
8763 && FRAME_VISIBLE_P (XFRAME (w->frame))
8764 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8765 /* Make sure recorded data applies to current buffer, etc. */
8766 && this_line_buffer == current_buffer
8767 && current_buffer == XBUFFER (w->buffer)
8768 && NILP (w->force_start)
8769 && NILP (w->optional_new_start)
8770 /* Point must be on the line that we have info recorded about. */
8771 && PT >= CHARPOS (tlbufpos)
8772 && PT <= Z - CHARPOS (tlendpos)
8773 /* All text outside that line, including its final newline,
8774 must be unchanged */
8775 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8776 CHARPOS (tlendpos)))
8778 if (CHARPOS (tlbufpos) > BEGV
8779 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8780 && (CHARPOS (tlbufpos) == ZV
8781 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8782 /* Former continuation line has disappeared by becoming empty */
8783 goto cancel;
8784 else if (XFASTINT (w->last_modified) < MODIFF
8785 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8786 || MINI_WINDOW_P (w))
8788 /* We have to handle the case of continuation around a
8789 wide-column character (See the comment in indent.c around
8790 line 885).
8792 For instance, in the following case:
8794 -------- Insert --------
8795 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8796 J_I_ ==> J_I_ `^^' are cursors.
8797 ^^ ^^
8798 -------- --------
8800 As we have to redraw the line above, we should goto cancel. */
8802 struct it it;
8803 int line_height_before = this_line_pixel_height;
8805 /* Note that start_display will handle the case that the
8806 line starting at tlbufpos is a continuation lines. */
8807 start_display (&it, w, tlbufpos);
8809 /* Implementation note: It this still necessary? */
8810 if (it.current_x != this_line_start_x)
8811 goto cancel;
8813 TRACE ((stderr, "trying display optimization 1\n"));
8814 w->cursor.vpos = -1;
8815 overlay_arrow_seen = 0;
8816 it.vpos = this_line_vpos;
8817 it.current_y = this_line_y;
8818 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8819 display_line (&it);
8821 /* If line contains point, is not continued,
8822 and ends at same distance from eob as before, we win */
8823 if (w->cursor.vpos >= 0
8824 /* Line is not continued, otherwise this_line_start_pos
8825 would have been set to 0 in display_line. */
8826 && CHARPOS (this_line_start_pos)
8827 /* Line ends as before. */
8828 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8829 /* Line has same height as before. Otherwise other lines
8830 would have to be shifted up or down. */
8831 && this_line_pixel_height == line_height_before)
8833 /* If this is not the window's last line, we must adjust
8834 the charstarts of the lines below. */
8835 if (it.current_y < it.last_visible_y)
8837 struct glyph_row *row
8838 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8839 int delta, delta_bytes;
8841 if (Z - CHARPOS (tlendpos) == ZV)
8843 /* This line ends at end of (accessible part of)
8844 buffer. There is no newline to count. */
8845 delta = (Z
8846 - CHARPOS (tlendpos)
8847 - MATRIX_ROW_START_CHARPOS (row));
8848 delta_bytes = (Z_BYTE
8849 - BYTEPOS (tlendpos)
8850 - MATRIX_ROW_START_BYTEPOS (row));
8852 else
8854 /* This line ends in a newline. Must take
8855 account of the newline and the rest of the
8856 text that follows. */
8857 delta = (Z
8858 - CHARPOS (tlendpos)
8859 - MATRIX_ROW_START_CHARPOS (row));
8860 delta_bytes = (Z_BYTE
8861 - BYTEPOS (tlendpos)
8862 - MATRIX_ROW_START_BYTEPOS (row));
8865 increment_matrix_positions (w->current_matrix,
8866 this_line_vpos + 1,
8867 w->current_matrix->nrows,
8868 delta, delta_bytes);
8871 /* If this row displays text now but previously didn't,
8872 or vice versa, w->window_end_vpos may have to be
8873 adjusted. */
8874 if ((it.glyph_row - 1)->displays_text_p)
8876 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8877 XSETINT (w->window_end_vpos, this_line_vpos);
8879 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8880 && this_line_vpos > 0)
8881 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8882 w->window_end_valid = Qnil;
8884 /* Update hint: No need to try to scroll in update_window. */
8885 w->desired_matrix->no_scrolling_p = 1;
8887 #if GLYPH_DEBUG
8888 *w->desired_matrix->method = 0;
8889 debug_method_add (w, "optimization 1");
8890 #endif
8891 goto update;
8893 else
8894 goto cancel;
8896 else if (/* Cursor position hasn't changed. */
8897 PT == XFASTINT (w->last_point)
8898 /* Make sure the cursor was last displayed
8899 in this window. Otherwise we have to reposition it. */
8900 && 0 <= w->cursor.vpos
8901 && XINT (w->height) > w->cursor.vpos)
8903 if (!must_finish)
8905 do_pending_window_change (1);
8907 /* We used to always goto end_of_redisplay here, but this
8908 isn't enough if we have a blinking cursor. */
8909 if (w->cursor_off_p == w->last_cursor_off_p)
8910 goto end_of_redisplay;
8912 goto update;
8914 /* If highlighting the region, or if the cursor is in the echo area,
8915 then we can't just move the cursor. */
8916 else if (! (!NILP (Vtransient_mark_mode)
8917 && !NILP (current_buffer->mark_active))
8918 && (EQ (selected_window, current_buffer->last_selected_window)
8919 || highlight_nonselected_windows)
8920 && NILP (w->region_showing)
8921 && NILP (Vshow_trailing_whitespace)
8922 && !cursor_in_echo_area)
8924 struct it it;
8925 struct glyph_row *row;
8927 /* Skip from tlbufpos to PT and see where it is. Note that
8928 PT may be in invisible text. If so, we will end at the
8929 next visible position. */
8930 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8931 NULL, DEFAULT_FACE_ID);
8932 it.current_x = this_line_start_x;
8933 it.current_y = this_line_y;
8934 it.vpos = this_line_vpos;
8936 /* The call to move_it_to stops in front of PT, but
8937 moves over before-strings. */
8938 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8940 if (it.vpos == this_line_vpos
8941 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8942 row->enabled_p))
8944 xassert (this_line_vpos == it.vpos);
8945 xassert (this_line_y == it.current_y);
8946 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8947 #if GLYPH_DEBUG
8948 *w->desired_matrix->method = 0;
8949 debug_method_add (w, "optimization 3");
8950 #endif
8951 goto update;
8953 else
8954 goto cancel;
8957 cancel:
8958 /* Text changed drastically or point moved off of line. */
8959 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8962 CHARPOS (this_line_start_pos) = 0;
8963 consider_all_windows_p |= buffer_shared > 1;
8964 ++clear_face_cache_count;
8967 /* Build desired matrices, and update the display. If
8968 consider_all_windows_p is non-zero, do it for all windows on all
8969 frames. Otherwise do it for selected_window, only. */
8971 if (consider_all_windows_p)
8973 Lisp_Object tail, frame;
8974 int i, n = 0, size = 50;
8975 struct frame **updated
8976 = (struct frame **) alloca (size * sizeof *updated);
8978 /* Clear the face cache eventually. */
8979 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8981 clear_face_cache (0);
8982 clear_face_cache_count = 0;
8985 /* Recompute # windows showing selected buffer. This will be
8986 incremented each time such a window is displayed. */
8987 buffer_shared = 0;
8989 FOR_EACH_FRAME (tail, frame)
8991 struct frame *f = XFRAME (frame);
8993 if (FRAME_WINDOW_P (f) || f == sf)
8995 #ifdef HAVE_WINDOW_SYSTEM
8996 if (clear_face_cache_count % 50 == 0
8997 && FRAME_WINDOW_P (f))
8998 clear_image_cache (f, 0);
8999 #endif /* HAVE_WINDOW_SYSTEM */
9001 /* Mark all the scroll bars to be removed; we'll redeem
9002 the ones we want when we redisplay their windows. */
9003 if (condemn_scroll_bars_hook)
9004 condemn_scroll_bars_hook (f);
9006 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9007 redisplay_windows (FRAME_ROOT_WINDOW (f));
9009 /* Any scroll bars which redisplay_windows should have
9010 nuked should now go away. */
9011 if (judge_scroll_bars_hook)
9012 judge_scroll_bars_hook (f);
9014 /* If fonts changed, display again. */
9015 /* ??? rms: I suspect it is a mistake to jump all the way
9016 back to retry here. It should just retry this frame. */
9017 if (fonts_changed_p)
9018 goto retry;
9020 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9022 /* See if we have to hscroll. */
9023 if (hscroll_windows (f->root_window))
9024 goto retry;
9026 /* Prevent various kinds of signals during display
9027 update. stdio is not robust about handling
9028 signals, which can cause an apparent I/O
9029 error. */
9030 if (interrupt_input)
9031 unrequest_sigio ();
9032 STOP_POLLING;
9034 /* Update the display. */
9035 set_window_update_flags (XWINDOW (f->root_window), 1);
9036 pause |= update_frame (f, 0, 0);
9037 if (pause)
9038 break;
9040 if (n == size)
9042 int nbytes = size * sizeof *updated;
9043 struct frame **p = (struct frame **) alloca (2 * nbytes);
9044 bcopy (updated, p, nbytes);
9045 size *= 2;
9048 updated[n++] = f;
9053 /* Do the mark_window_display_accurate after all windows have
9054 been redisplayed because this call resets flags in buffers
9055 which are needed for proper redisplay. */
9056 for (i = 0; i < n; ++i)
9058 struct frame *f = updated[i];
9059 mark_window_display_accurate (f->root_window, 1);
9060 if (frame_up_to_date_hook)
9061 frame_up_to_date_hook (f);
9064 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9066 Lisp_Object mini_window;
9067 struct frame *mini_frame;
9069 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9070 /* Use list_of_error, not Qerror, so that
9071 we catch only errors and don't run the debugger. */
9072 internal_condition_case_1 (redisplay_window_1, selected_window,
9073 list_of_error,
9074 redisplay_window_error);
9076 /* Compare desired and current matrices, perform output. */
9078 update:
9079 /* If fonts changed, display again. */
9080 if (fonts_changed_p)
9081 goto retry;
9083 /* Prevent various kinds of signals during display update.
9084 stdio is not robust about handling signals,
9085 which can cause an apparent I/O error. */
9086 if (interrupt_input)
9087 unrequest_sigio ();
9088 STOP_POLLING;
9090 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9092 if (hscroll_windows (selected_window))
9093 goto retry;
9095 XWINDOW (selected_window)->must_be_updated_p = 1;
9096 pause = update_frame (sf, 0, 0);
9099 /* We may have called echo_area_display at the top of this
9100 function. If the echo area is on another frame, that may
9101 have put text on a frame other than the selected one, so the
9102 above call to update_frame would not have caught it. Catch
9103 it here. */
9104 mini_window = FRAME_MINIBUF_WINDOW (sf);
9105 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9107 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9109 XWINDOW (mini_window)->must_be_updated_p = 1;
9110 pause |= update_frame (mini_frame, 0, 0);
9111 if (!pause && hscroll_windows (mini_window))
9112 goto retry;
9116 /* If display was paused because of pending input, make sure we do a
9117 thorough update the next time. */
9118 if (pause)
9120 /* Prevent the optimization at the beginning of
9121 redisplay_internal that tries a single-line update of the
9122 line containing the cursor in the selected window. */
9123 CHARPOS (this_line_start_pos) = 0;
9125 /* Let the overlay arrow be updated the next time. */
9126 if (!NILP (last_arrow_position))
9128 last_arrow_position = Qt;
9129 last_arrow_string = Qt;
9132 /* If we pause after scrolling, some rows in the current
9133 matrices of some windows are not valid. */
9134 if (!WINDOW_FULL_WIDTH_P (w)
9135 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9136 update_mode_lines = 1;
9138 else
9140 if (!consider_all_windows_p)
9142 /* This has already been done above if
9143 consider_all_windows_p is set. */
9144 mark_window_display_accurate_1 (w, 1);
9146 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9147 last_arrow_string = Voverlay_arrow_string;
9149 if (frame_up_to_date_hook != 0)
9150 frame_up_to_date_hook (sf);
9153 update_mode_lines = 0;
9154 windows_or_buffers_changed = 0;
9155 cursor_type_changed = 0;
9158 /* Start SIGIO interrupts coming again. Having them off during the
9159 code above makes it less likely one will discard output, but not
9160 impossible, since there might be stuff in the system buffer here.
9161 But it is much hairier to try to do anything about that. */
9162 if (interrupt_input)
9163 request_sigio ();
9164 RESUME_POLLING;
9166 /* If a frame has become visible which was not before, redisplay
9167 again, so that we display it. Expose events for such a frame
9168 (which it gets when becoming visible) don't call the parts of
9169 redisplay constructing glyphs, so simply exposing a frame won't
9170 display anything in this case. So, we have to display these
9171 frames here explicitly. */
9172 if (!pause)
9174 Lisp_Object tail, frame;
9175 int new_count = 0;
9177 FOR_EACH_FRAME (tail, frame)
9179 int this_is_visible = 0;
9181 if (XFRAME (frame)->visible)
9182 this_is_visible = 1;
9183 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9184 if (XFRAME (frame)->visible)
9185 this_is_visible = 1;
9187 if (this_is_visible)
9188 new_count++;
9191 if (new_count != number_of_visible_frames)
9192 windows_or_buffers_changed++;
9195 /* Change frame size now if a change is pending. */
9196 do_pending_window_change (1);
9198 /* If we just did a pending size change, or have additional
9199 visible frames, redisplay again. */
9200 if (windows_or_buffers_changed && !pause)
9201 goto retry;
9203 end_of_redisplay:
9204 unbind_to (count, Qnil);
9205 RESUME_POLLING;
9209 /* Redisplay, but leave alone any recent echo area message unless
9210 another message has been requested in its place.
9212 This is useful in situations where you need to redisplay but no
9213 user action has occurred, making it inappropriate for the message
9214 area to be cleared. See tracking_off and
9215 wait_reading_process_input for examples of these situations.
9217 FROM_WHERE is an integer saying from where this function was
9218 called. This is useful for debugging. */
9220 void
9221 redisplay_preserve_echo_area (from_where)
9222 int from_where;
9224 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9226 if (!NILP (echo_area_buffer[1]))
9228 /* We have a previously displayed message, but no current
9229 message. Redisplay the previous message. */
9230 display_last_displayed_message_p = 1;
9231 redisplay_internal (1);
9232 display_last_displayed_message_p = 0;
9234 else
9235 redisplay_internal (1);
9239 /* Function registered with record_unwind_protect in
9240 redisplay_internal. Reset redisplaying_p to the value it had
9241 before redisplay_internal was called, and clear
9242 prevent_freeing_realized_faces_p. */
9244 static Lisp_Object
9245 unwind_redisplay (old_redisplaying_p)
9246 Lisp_Object old_redisplaying_p;
9248 redisplaying_p = XFASTINT (old_redisplaying_p);
9249 return Qnil;
9253 /* Mark the display of window W as accurate or inaccurate. If
9254 ACCURATE_P is non-zero mark display of W as accurate. If
9255 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9256 redisplay_internal is called. */
9258 static void
9259 mark_window_display_accurate_1 (w, accurate_p)
9260 struct window *w;
9261 int accurate_p;
9263 if (BUFFERP (w->buffer))
9265 struct buffer *b = XBUFFER (w->buffer);
9267 w->last_modified
9268 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9269 w->last_overlay_modified
9270 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9271 w->last_had_star
9272 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9274 if (accurate_p)
9276 b->clip_changed = 0;
9277 b->prevent_redisplay_optimizations_p = 0;
9279 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9280 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9281 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9282 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9284 w->current_matrix->buffer = b;
9285 w->current_matrix->begv = BUF_BEGV (b);
9286 w->current_matrix->zv = BUF_ZV (b);
9288 w->last_cursor = w->cursor;
9289 w->last_cursor_off_p = w->cursor_off_p;
9291 if (w == XWINDOW (selected_window))
9292 w->last_point = make_number (BUF_PT (b));
9293 else
9294 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9298 if (accurate_p)
9300 w->window_end_valid = w->buffer;
9301 #if 0 /* This is incorrect with variable-height lines. */
9302 xassert (XINT (w->window_end_vpos)
9303 < (XINT (w->height)
9304 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9305 #endif
9306 w->update_mode_line = Qnil;
9311 /* Mark the display of windows in the window tree rooted at WINDOW as
9312 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9313 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9314 be redisplayed the next time redisplay_internal is called. */
9316 void
9317 mark_window_display_accurate (window, accurate_p)
9318 Lisp_Object window;
9319 int accurate_p;
9321 struct window *w;
9323 for (; !NILP (window); window = w->next)
9325 w = XWINDOW (window);
9326 mark_window_display_accurate_1 (w, accurate_p);
9328 if (!NILP (w->vchild))
9329 mark_window_display_accurate (w->vchild, accurate_p);
9330 if (!NILP (w->hchild))
9331 mark_window_display_accurate (w->hchild, accurate_p);
9334 if (accurate_p)
9336 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9337 last_arrow_string = Voverlay_arrow_string;
9339 else
9341 /* Force a thorough redisplay the next time by setting
9342 last_arrow_position and last_arrow_string to t, which is
9343 unequal to any useful value of Voverlay_arrow_... */
9344 last_arrow_position = Qt;
9345 last_arrow_string = Qt;
9350 /* Return value in display table DP (Lisp_Char_Table *) for character
9351 C. Since a display table doesn't have any parent, we don't have to
9352 follow parent. Do not call this function directly but use the
9353 macro DISP_CHAR_VECTOR. */
9355 Lisp_Object
9356 disp_char_vector (dp, c)
9357 struct Lisp_Char_Table *dp;
9358 int c;
9360 int code[4], i;
9361 Lisp_Object val;
9363 if (SINGLE_BYTE_CHAR_P (c))
9364 return (dp->contents[c]);
9366 SPLIT_CHAR (c, code[0], code[1], code[2]);
9367 if (code[1] < 32)
9368 code[1] = -1;
9369 else if (code[2] < 32)
9370 code[2] = -1;
9372 /* Here, the possible range of code[0] (== charset ID) is
9373 128..max_charset. Since the top level char table contains data
9374 for multibyte characters after 256th element, we must increment
9375 code[0] by 128 to get a correct index. */
9376 code[0] += 128;
9377 code[3] = -1; /* anchor */
9379 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9381 val = dp->contents[code[i]];
9382 if (!SUB_CHAR_TABLE_P (val))
9383 return (NILP (val) ? dp->defalt : val);
9386 /* Here, val is a sub char table. We return the default value of
9387 it. */
9388 return (dp->defalt);
9393 /***********************************************************************
9394 Window Redisplay
9395 ***********************************************************************/
9397 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9399 static void
9400 redisplay_windows (window)
9401 Lisp_Object window;
9403 while (!NILP (window))
9405 struct window *w = XWINDOW (window);
9407 if (!NILP (w->hchild))
9408 redisplay_windows (w->hchild);
9409 else if (!NILP (w->vchild))
9410 redisplay_windows (w->vchild);
9411 else
9413 displayed_buffer = XBUFFER (w->buffer);
9414 /* Use list_of_error, not Qerror, so that
9415 we catch only errors and don't run the debugger. */
9416 internal_condition_case_1 (redisplay_window_0, window,
9417 list_of_error,
9418 redisplay_window_error);
9421 window = w->next;
9425 static Lisp_Object
9426 redisplay_window_error ()
9428 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9429 return Qnil;
9432 static Lisp_Object
9433 redisplay_window_0 (window)
9434 Lisp_Object window;
9436 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9437 redisplay_window (window, 0);
9438 return Qnil;
9441 static Lisp_Object
9442 redisplay_window_1 (window)
9443 Lisp_Object window;
9445 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9446 redisplay_window (window, 1);
9447 return Qnil;
9450 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9451 DELTA is the number of bytes by which positions recorded in ROW
9452 differ from current buffer positions. */
9454 void
9455 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9456 struct window *w;
9457 struct glyph_row *row;
9458 struct glyph_matrix *matrix;
9459 int delta, delta_bytes, dy, dvpos;
9461 struct glyph *glyph = row->glyphs[TEXT_AREA];
9462 struct glyph *end = glyph + row->used[TEXT_AREA];
9463 int x = row->x;
9464 int pt_old = PT - delta;
9466 /* Skip over glyphs not having an object at the start of the row.
9467 These are special glyphs like truncation marks on terminal
9468 frames. */
9469 if (row->displays_text_p)
9470 while (glyph < end
9471 && INTEGERP (glyph->object)
9472 && glyph->charpos < 0)
9474 x += glyph->pixel_width;
9475 ++glyph;
9478 while (glyph < end
9479 && !INTEGERP (glyph->object)
9480 && (!BUFFERP (glyph->object)
9481 || glyph->charpos < pt_old))
9483 x += glyph->pixel_width;
9484 ++glyph;
9487 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9488 w->cursor.x = x;
9489 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9490 w->cursor.y = row->y + dy;
9492 if (w == XWINDOW (selected_window))
9494 if (!row->continued_p
9495 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9496 && row->x == 0)
9498 this_line_buffer = XBUFFER (w->buffer);
9500 CHARPOS (this_line_start_pos)
9501 = MATRIX_ROW_START_CHARPOS (row) + delta;
9502 BYTEPOS (this_line_start_pos)
9503 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9505 CHARPOS (this_line_end_pos)
9506 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9507 BYTEPOS (this_line_end_pos)
9508 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9510 this_line_y = w->cursor.y;
9511 this_line_pixel_height = row->height;
9512 this_line_vpos = w->cursor.vpos;
9513 this_line_start_x = row->x;
9515 else
9516 CHARPOS (this_line_start_pos) = 0;
9521 /* Run window scroll functions, if any, for WINDOW with new window
9522 start STARTP. Sets the window start of WINDOW to that position.
9524 We assume that the window's buffer is really current. */
9526 static INLINE struct text_pos
9527 run_window_scroll_functions (window, startp)
9528 Lisp_Object window;
9529 struct text_pos startp;
9531 struct window *w = XWINDOW (window);
9532 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9534 if (current_buffer != XBUFFER (w->buffer))
9535 abort ();
9537 if (!NILP (Vwindow_scroll_functions))
9539 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9540 make_number (CHARPOS (startp)));
9541 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9542 /* In case the hook functions switch buffers. */
9543 if (current_buffer != XBUFFER (w->buffer))
9544 set_buffer_internal_1 (XBUFFER (w->buffer));
9547 return startp;
9551 /* Make sure the line containing the cursor is fully visible.
9552 A value of 1 means there is nothing to be done.
9553 (Either the line is fully visible, or it cannot be made so,
9554 or we cannot tell.)
9555 A value of 0 means the caller should do scrolling
9556 as if point had gone off the screen. */
9558 static int
9559 make_cursor_line_fully_visible (w)
9560 struct window *w;
9562 struct glyph_matrix *matrix;
9563 struct glyph_row *row;
9564 int window_height;
9566 /* It's not always possible to find the cursor, e.g, when a window
9567 is full of overlay strings. Don't do anything in that case. */
9568 if (w->cursor.vpos < 0)
9569 return 1;
9571 matrix = w->desired_matrix;
9572 row = MATRIX_ROW (matrix, w->cursor.vpos);
9574 /* If the cursor row is not partially visible, there's nothing to do. */
9575 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9576 return 1;
9578 /* If the row the cursor is in is taller than the window's height,
9579 it's not clear what to do, so do nothing. */
9580 window_height = window_box_height (w);
9581 if (row->height >= window_height)
9582 return 1;
9584 return 0;
9586 #if 0
9587 /* This code used to try to scroll the window just enough to make
9588 the line visible. It returned 0 to say that the caller should
9589 allocate larger glyph matrices. */
9591 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9593 int dy = row->height - row->visible_height;
9594 w->vscroll = 0;
9595 w->cursor.y += dy;
9596 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9598 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9600 int dy = - (row->height - row->visible_height);
9601 w->vscroll = dy;
9602 w->cursor.y += dy;
9603 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9606 /* When we change the cursor y-position of the selected window,
9607 change this_line_y as well so that the display optimization for
9608 the cursor line of the selected window in redisplay_internal uses
9609 the correct y-position. */
9610 if (w == XWINDOW (selected_window))
9611 this_line_y = w->cursor.y;
9613 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9614 redisplay with larger matrices. */
9615 if (matrix->nrows < required_matrix_height (w))
9617 fonts_changed_p = 1;
9618 return 0;
9621 return 1;
9622 #endif /* 0 */
9626 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9627 non-zero means only WINDOW is redisplayed in redisplay_internal.
9628 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9629 in redisplay_window to bring a partially visible line into view in
9630 the case that only the cursor has moved.
9632 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
9633 last screen line's vertical height extends past the end of the screen.
9635 Value is
9637 1 if scrolling succeeded
9639 0 if scrolling didn't find point.
9641 -1 if new fonts have been loaded so that we must interrupt
9642 redisplay, adjust glyph matrices, and try again. */
9644 enum
9646 SCROLLING_SUCCESS,
9647 SCROLLING_FAILED,
9648 SCROLLING_NEED_LARGER_MATRICES
9651 static int
9652 try_scrolling (window, just_this_one_p, scroll_conservatively,
9653 scroll_step, temp_scroll_step, last_line_misfit)
9654 Lisp_Object window;
9655 int just_this_one_p;
9656 EMACS_INT scroll_conservatively, scroll_step;
9657 int temp_scroll_step;
9658 int last_line_misfit;
9660 struct window *w = XWINDOW (window);
9661 struct frame *f = XFRAME (w->frame);
9662 struct text_pos scroll_margin_pos;
9663 struct text_pos pos;
9664 struct text_pos startp;
9665 struct it it;
9666 Lisp_Object window_end;
9667 int this_scroll_margin;
9668 int dy = 0;
9669 int scroll_max;
9670 int rc;
9671 int amount_to_scroll = 0;
9672 Lisp_Object aggressive;
9673 int height;
9674 int end_scroll_margin;
9676 #if GLYPH_DEBUG
9677 debug_method_add (w, "try_scrolling");
9678 #endif
9680 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9682 /* Compute scroll margin height in pixels. We scroll when point is
9683 within this distance from the top or bottom of the window. */
9684 if (scroll_margin > 0)
9686 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9687 this_scroll_margin *= CANON_Y_UNIT (f);
9689 else
9690 this_scroll_margin = 0;
9692 /* Compute how much we should try to scroll maximally to bring point
9693 into view. */
9694 if (scroll_step || scroll_conservatively || temp_scroll_step)
9695 scroll_max = max (scroll_step,
9696 max (scroll_conservatively, temp_scroll_step));
9697 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9698 || NUMBERP (current_buffer->scroll_up_aggressively))
9699 /* We're trying to scroll because of aggressive scrolling
9700 but no scroll_step is set. Choose an arbitrary one. Maybe
9701 there should be a variable for this. */
9702 scroll_max = 10;
9703 else
9704 scroll_max = 0;
9705 scroll_max *= CANON_Y_UNIT (f);
9707 /* Decide whether we have to scroll down. Start at the window end
9708 and move this_scroll_margin up to find the position of the scroll
9709 margin. */
9710 window_end = Fwindow_end (window, Qt);
9712 too_near_end:
9714 CHARPOS (scroll_margin_pos) = XINT (window_end);
9715 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9717 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
9718 if (end_scroll_margin)
9720 start_display (&it, w, scroll_margin_pos);
9721 move_it_vertically (&it, - end_scroll_margin);
9722 scroll_margin_pos = it.current.pos;
9725 if (PT >= CHARPOS (scroll_margin_pos))
9727 int y0;
9729 /* Point is in the scroll margin at the bottom of the window, or
9730 below. Compute a new window start that makes point visible. */
9732 /* Compute the distance from the scroll margin to PT.
9733 Give up if the distance is greater than scroll_max. */
9734 start_display (&it, w, scroll_margin_pos);
9735 y0 = it.current_y;
9736 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9737 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9739 /* To make point visible, we have to move the window start
9740 down so that the line the cursor is in is visible, which
9741 means we have to add in the height of the cursor line. */
9742 dy = line_bottom_y (&it) - y0;
9744 if (dy > scroll_max)
9745 return SCROLLING_FAILED;
9747 /* Move the window start down. If scrolling conservatively,
9748 move it just enough down to make point visible. If
9749 scroll_step is set, move it down by scroll_step. */
9750 start_display (&it, w, startp);
9752 if (scroll_conservatively)
9753 /* Set AMOUNT_TO_SCROLL to at least one line,
9754 and at most scroll_conservatively lines. */
9755 amount_to_scroll
9756 = min (max (dy, CANON_Y_UNIT (f)),
9757 CANON_Y_UNIT (f) * scroll_conservatively);
9758 else if (scroll_step || temp_scroll_step)
9759 amount_to_scroll = scroll_max;
9760 else
9762 aggressive = current_buffer->scroll_up_aggressively;
9763 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9764 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9765 if (NUMBERP (aggressive))
9766 amount_to_scroll = XFLOATINT (aggressive) * height;
9769 if (amount_to_scroll <= 0)
9770 return SCROLLING_FAILED;
9772 /* If moving by amount_to_scroll leaves STARTP unchanged,
9773 move it down one screen line. */
9775 move_it_vertically (&it, amount_to_scroll);
9776 if (CHARPOS (it.current.pos) == CHARPOS (startp))
9777 move_it_by_lines (&it, 1, 1);
9778 startp = it.current.pos;
9780 else
9782 /* See if point is inside the scroll margin at the top of the
9783 window. */
9784 scroll_margin_pos = startp;
9785 if (this_scroll_margin)
9787 start_display (&it, w, startp);
9788 move_it_vertically (&it, this_scroll_margin);
9789 scroll_margin_pos = it.current.pos;
9792 if (PT < CHARPOS (scroll_margin_pos))
9794 /* Point is in the scroll margin at the top of the window or
9795 above what is displayed in the window. */
9796 int y0;
9798 /* Compute the vertical distance from PT to the scroll
9799 margin position. Give up if distance is greater than
9800 scroll_max. */
9801 SET_TEXT_POS (pos, PT, PT_BYTE);
9802 start_display (&it, w, pos);
9803 y0 = it.current_y;
9804 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9805 it.last_visible_y, -1,
9806 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9807 dy = it.current_y - y0;
9808 if (dy > scroll_max)
9809 return SCROLLING_FAILED;
9811 /* Compute new window start. */
9812 start_display (&it, w, startp);
9814 if (scroll_conservatively)
9815 amount_to_scroll =
9816 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9817 else if (scroll_step || temp_scroll_step)
9818 amount_to_scroll = scroll_max;
9819 else
9821 aggressive = current_buffer->scroll_down_aggressively;
9822 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9823 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9824 if (NUMBERP (aggressive))
9825 amount_to_scroll = XFLOATINT (aggressive) * height;
9828 if (amount_to_scroll <= 0)
9829 return SCROLLING_FAILED;
9831 move_it_vertically (&it, - amount_to_scroll);
9832 startp = it.current.pos;
9836 /* Run window scroll functions. */
9837 startp = run_window_scroll_functions (window, startp);
9839 /* Display the window. Give up if new fonts are loaded, or if point
9840 doesn't appear. */
9841 if (!try_window (window, startp))
9842 rc = SCROLLING_NEED_LARGER_MATRICES;
9843 else if (w->cursor.vpos < 0)
9845 clear_glyph_matrix (w->desired_matrix);
9846 rc = SCROLLING_FAILED;
9848 else
9850 /* Maybe forget recorded base line for line number display. */
9851 if (!just_this_one_p
9852 || current_buffer->clip_changed
9853 || BEG_UNCHANGED < CHARPOS (startp))
9854 w->base_line_number = Qnil;
9856 /* If cursor ends up on a partially visible line,
9857 treat that as being off the bottom of the screen. */
9858 if (! make_cursor_line_fully_visible (w))
9860 clear_glyph_matrix (w->desired_matrix);
9861 last_line_misfit = 1;
9862 goto too_near_end;
9864 rc = SCROLLING_SUCCESS;
9867 return rc;
9871 /* Compute a suitable window start for window W if display of W starts
9872 on a continuation line. Value is non-zero if a new window start
9873 was computed.
9875 The new window start will be computed, based on W's width, starting
9876 from the start of the continued line. It is the start of the
9877 screen line with the minimum distance from the old start W->start. */
9879 static int
9880 compute_window_start_on_continuation_line (w)
9881 struct window *w;
9883 struct text_pos pos, start_pos;
9884 int window_start_changed_p = 0;
9886 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9888 /* If window start is on a continuation line... Window start may be
9889 < BEGV in case there's invisible text at the start of the
9890 buffer (M-x rmail, for example). */
9891 if (CHARPOS (start_pos) > BEGV
9892 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9894 struct it it;
9895 struct glyph_row *row;
9897 /* Handle the case that the window start is out of range. */
9898 if (CHARPOS (start_pos) < BEGV)
9899 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9900 else if (CHARPOS (start_pos) > ZV)
9901 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9903 /* Find the start of the continued line. This should be fast
9904 because scan_buffer is fast (newline cache). */
9905 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9906 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9907 row, DEFAULT_FACE_ID);
9908 reseat_at_previous_visible_line_start (&it);
9910 /* If the line start is "too far" away from the window start,
9911 say it takes too much time to compute a new window start. */
9912 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9913 < XFASTINT (w->height) * XFASTINT (w->width))
9915 int min_distance, distance;
9917 /* Move forward by display lines to find the new window
9918 start. If window width was enlarged, the new start can
9919 be expected to be > the old start. If window width was
9920 decreased, the new window start will be < the old start.
9921 So, we're looking for the display line start with the
9922 minimum distance from the old window start. */
9923 pos = it.current.pos;
9924 min_distance = INFINITY;
9925 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9926 distance < min_distance)
9928 min_distance = distance;
9929 pos = it.current.pos;
9930 move_it_by_lines (&it, 1, 0);
9933 /* Set the window start there. */
9934 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9935 window_start_changed_p = 1;
9939 return window_start_changed_p;
9943 /* Try cursor movement in case text has not changed in window WINDOW,
9944 with window start STARTP. Value is
9946 CURSOR_MOVEMENT_SUCCESS if successful
9948 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9950 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9951 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9952 we want to scroll as if scroll-step were set to 1. See the code.
9954 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9955 which case we have to abort this redisplay, and adjust matrices
9956 first. */
9958 enum
9960 CURSOR_MOVEMENT_SUCCESS,
9961 CURSOR_MOVEMENT_CANNOT_BE_USED,
9962 CURSOR_MOVEMENT_MUST_SCROLL,
9963 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9966 static int
9967 try_cursor_movement (window, startp, scroll_step)
9968 Lisp_Object window;
9969 struct text_pos startp;
9970 int *scroll_step;
9972 struct window *w = XWINDOW (window);
9973 struct frame *f = XFRAME (w->frame);
9974 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9976 #if GLYPH_DEBUG
9977 if (inhibit_try_cursor_movement)
9978 return rc;
9979 #endif
9981 /* Handle case where text has not changed, only point, and it has
9982 not moved off the frame. */
9983 if (/* Point may be in this window. */
9984 PT >= CHARPOS (startp)
9985 /* Selective display hasn't changed. */
9986 && !current_buffer->clip_changed
9987 /* Function force-mode-line-update is used to force a thorough
9988 redisplay. It sets either windows_or_buffers_changed or
9989 update_mode_lines. So don't take a shortcut here for these
9990 cases. */
9991 && !update_mode_lines
9992 && !windows_or_buffers_changed
9993 && !cursor_type_changed
9994 /* Can't use this case if highlighting a region. When a
9995 region exists, cursor movement has to do more than just
9996 set the cursor. */
9997 && !(!NILP (Vtransient_mark_mode)
9998 && !NILP (current_buffer->mark_active))
9999 && NILP (w->region_showing)
10000 && NILP (Vshow_trailing_whitespace)
10001 /* Right after splitting windows, last_point may be nil. */
10002 && INTEGERP (w->last_point)
10003 /* This code is not used for mini-buffer for the sake of the case
10004 of redisplaying to replace an echo area message; since in
10005 that case the mini-buffer contents per se are usually
10006 unchanged. This code is of no real use in the mini-buffer
10007 since the handling of this_line_start_pos, etc., in redisplay
10008 handles the same cases. */
10009 && !EQ (window, minibuf_window)
10010 /* When splitting windows or for new windows, it happens that
10011 redisplay is called with a nil window_end_vpos or one being
10012 larger than the window. This should really be fixed in
10013 window.c. I don't have this on my list, now, so we do
10014 approximately the same as the old redisplay code. --gerd. */
10015 && INTEGERP (w->window_end_vpos)
10016 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
10017 && (FRAME_WINDOW_P (f)
10018 || !MARKERP (Voverlay_arrow_position)
10019 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
10021 int this_scroll_margin;
10022 struct glyph_row *row = NULL;
10024 #if GLYPH_DEBUG
10025 debug_method_add (w, "cursor movement");
10026 #endif
10028 /* Scroll if point within this distance from the top or bottom
10029 of the window. This is a pixel value. */
10030 this_scroll_margin = max (0, scroll_margin);
10031 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
10032 this_scroll_margin *= CANON_Y_UNIT (f);
10034 /* Start with the row the cursor was displayed during the last
10035 not paused redisplay. Give up if that row is not valid. */
10036 if (w->last_cursor.vpos < 0
10037 || w->last_cursor.vpos >= w->current_matrix->nrows)
10038 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10039 else
10041 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
10042 if (row->mode_line_p)
10043 ++row;
10044 if (!row->enabled_p)
10045 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10048 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
10050 int scroll_p = 0;
10051 int last_y = window_text_bottom_y (w) - this_scroll_margin;
10053 if (PT > XFASTINT (w->last_point))
10055 /* Point has moved forward. */
10056 while (MATRIX_ROW_END_CHARPOS (row) < PT
10057 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10059 xassert (row->enabled_p);
10060 ++row;
10063 /* The end position of a row equals the start position
10064 of the next row. If PT is there, we would rather
10065 display it in the next line. */
10066 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10067 && MATRIX_ROW_END_CHARPOS (row) == PT
10068 && !cursor_row_p (w, row))
10069 ++row;
10071 /* If within the scroll margin, scroll. Note that
10072 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10073 the next line would be drawn, and that
10074 this_scroll_margin can be zero. */
10075 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10076 || PT > MATRIX_ROW_END_CHARPOS (row)
10077 /* Line is completely visible last line in window
10078 and PT is to be set in the next line. */
10079 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10080 && PT == MATRIX_ROW_END_CHARPOS (row)
10081 && !row->ends_at_zv_p
10082 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10083 scroll_p = 1;
10085 else if (PT < XFASTINT (w->last_point))
10087 /* Cursor has to be moved backward. Note that PT >=
10088 CHARPOS (startp) because of the outer
10089 if-statement. */
10090 while (!row->mode_line_p
10091 && (MATRIX_ROW_START_CHARPOS (row) > PT
10092 || (MATRIX_ROW_START_CHARPOS (row) == PT
10093 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10094 && (row->y > this_scroll_margin
10095 || CHARPOS (startp) == BEGV))
10097 xassert (row->enabled_p);
10098 --row;
10101 /* Consider the following case: Window starts at BEGV,
10102 there is invisible, intangible text at BEGV, so that
10103 display starts at some point START > BEGV. It can
10104 happen that we are called with PT somewhere between
10105 BEGV and START. Try to handle that case. */
10106 if (row < w->current_matrix->rows
10107 || row->mode_line_p)
10109 row = w->current_matrix->rows;
10110 if (row->mode_line_p)
10111 ++row;
10114 /* Due to newlines in overlay strings, we may have to
10115 skip forward over overlay strings. */
10116 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10117 && MATRIX_ROW_END_CHARPOS (row) == PT
10118 && !cursor_row_p (w, row))
10119 ++row;
10121 /* If within the scroll margin, scroll. */
10122 if (row->y < this_scroll_margin
10123 && CHARPOS (startp) != BEGV)
10124 scroll_p = 1;
10127 if (PT < MATRIX_ROW_START_CHARPOS (row)
10128 || PT > MATRIX_ROW_END_CHARPOS (row))
10130 /* if PT is not in the glyph row, give up. */
10131 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10133 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10135 if (PT == MATRIX_ROW_END_CHARPOS (row)
10136 && !row->ends_at_zv_p
10137 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10138 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10139 else if (row->height > window_box_height (w))
10141 /* If we end up in a partially visible line, let's
10142 make it fully visible, except when it's taller
10143 than the window, in which case we can't do much
10144 about it. */
10145 *scroll_step = 1;
10146 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10148 else
10150 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10151 if (!make_cursor_line_fully_visible (w))
10152 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10153 else
10154 rc = CURSOR_MOVEMENT_SUCCESS;
10157 else if (scroll_p)
10158 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10159 else
10161 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10162 rc = CURSOR_MOVEMENT_SUCCESS;
10167 return rc;
10171 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10172 selected_window is redisplayed.
10174 We can return without actually redisplaying the window if
10175 fonts_changed_p is nonzero. In that case, redisplay_internal will
10176 retry. */
10178 static void
10179 redisplay_window (window, just_this_one_p)
10180 Lisp_Object window;
10181 int just_this_one_p;
10183 struct window *w = XWINDOW (window);
10184 struct frame *f = XFRAME (w->frame);
10185 struct buffer *buffer = XBUFFER (w->buffer);
10186 struct buffer *old = current_buffer;
10187 struct text_pos lpoint, opoint, startp;
10188 int update_mode_line;
10189 int tem;
10190 struct it it;
10191 /* Record it now because it's overwritten. */
10192 int current_matrix_up_to_date_p = 0;
10193 /* This is less strict than current_matrix_up_to_date_p.
10194 It indictes that the buffer contents and narrowing are unchanged. */
10195 int buffer_unchanged_p = 0;
10196 int temp_scroll_step = 0;
10197 int count = SPECPDL_INDEX ();
10198 int rc;
10199 int centering_position;
10200 int last_line_misfit = 0;
10202 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10203 opoint = lpoint;
10205 /* W must be a leaf window here. */
10206 xassert (!NILP (w->buffer));
10207 #if GLYPH_DEBUG
10208 *w->desired_matrix->method = 0;
10209 #endif
10211 specbind (Qinhibit_point_motion_hooks, Qt);
10213 reconsider_clip_changes (w, buffer);
10215 /* Has the mode line to be updated? */
10216 update_mode_line = (!NILP (w->update_mode_line)
10217 || update_mode_lines
10218 || buffer->clip_changed
10219 || buffer->prevent_redisplay_optimizations_p);
10221 if (MINI_WINDOW_P (w))
10223 if (w == XWINDOW (echo_area_window)
10224 && !NILP (echo_area_buffer[0]))
10226 if (update_mode_line)
10227 /* We may have to update a tty frame's menu bar or a
10228 tool-bar. Example `M-x C-h C-h C-g'. */
10229 goto finish_menu_bars;
10230 else
10231 /* We've already displayed the echo area glyphs in this window. */
10232 goto finish_scroll_bars;
10234 else if ((w != XWINDOW (minibuf_window)
10235 || minibuf_level == 0)
10236 /* Quail displays non-mini buffers in minibuffer window.
10237 In that case, redisplay the window normally. */
10238 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
10240 /* W is a mini-buffer window, but it's not active, so clear
10241 it. */
10242 int yb = window_text_bottom_y (w);
10243 struct glyph_row *row;
10244 int y;
10246 for (y = 0, row = w->desired_matrix->rows;
10247 y < yb;
10248 y += row->height, ++row)
10249 blank_row (w, row, y);
10250 goto finish_scroll_bars;
10253 clear_glyph_matrix (w->desired_matrix);
10256 /* Otherwise set up data on this window; select its buffer and point
10257 value. */
10258 /* Really select the buffer, for the sake of buffer-local
10259 variables. */
10260 set_buffer_internal_1 (XBUFFER (w->buffer));
10261 SET_TEXT_POS (opoint, PT, PT_BYTE);
10263 current_matrix_up_to_date_p
10264 = (!NILP (w->window_end_valid)
10265 && !current_buffer->clip_changed
10266 && !current_buffer->prevent_redisplay_optimizations_p
10267 && XFASTINT (w->last_modified) >= MODIFF
10268 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10270 buffer_unchanged_p
10271 = (!NILP (w->window_end_valid)
10272 && !current_buffer->clip_changed
10273 && XFASTINT (w->last_modified) >= MODIFF
10274 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10276 /* When windows_or_buffers_changed is non-zero, we can't rely on
10277 the window end being valid, so set it to nil there. */
10278 if (windows_or_buffers_changed)
10280 /* If window starts on a continuation line, maybe adjust the
10281 window start in case the window's width changed. */
10282 if (XMARKER (w->start)->buffer == current_buffer)
10283 compute_window_start_on_continuation_line (w);
10285 w->window_end_valid = Qnil;
10288 /* Some sanity checks. */
10289 CHECK_WINDOW_END (w);
10290 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10291 abort ();
10292 if (BYTEPOS (opoint) < CHARPOS (opoint))
10293 abort ();
10295 /* If %c is in mode line, update it if needed. */
10296 if (!NILP (w->column_number_displayed)
10297 /* This alternative quickly identifies a common case
10298 where no change is needed. */
10299 && !(PT == XFASTINT (w->last_point)
10300 && XFASTINT (w->last_modified) >= MODIFF
10301 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10302 && (XFASTINT (w->column_number_displayed)
10303 != (int) current_column ())) /* iftc */
10304 update_mode_line = 1;
10306 /* Count number of windows showing the selected buffer. An indirect
10307 buffer counts as its base buffer. */
10308 if (!just_this_one_p)
10310 struct buffer *current_base, *window_base;
10311 current_base = current_buffer;
10312 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10313 if (current_base->base_buffer)
10314 current_base = current_base->base_buffer;
10315 if (window_base->base_buffer)
10316 window_base = window_base->base_buffer;
10317 if (current_base == window_base)
10318 buffer_shared++;
10321 /* Point refers normally to the selected window. For any other
10322 window, set up appropriate value. */
10323 if (!EQ (window, selected_window))
10325 int new_pt = XMARKER (w->pointm)->charpos;
10326 int new_pt_byte = marker_byte_position (w->pointm);
10327 if (new_pt < BEGV)
10329 new_pt = BEGV;
10330 new_pt_byte = BEGV_BYTE;
10331 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10333 else if (new_pt > (ZV - 1))
10335 new_pt = ZV;
10336 new_pt_byte = ZV_BYTE;
10337 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10340 /* We don't use SET_PT so that the point-motion hooks don't run. */
10341 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10344 /* If any of the character widths specified in the display table
10345 have changed, invalidate the width run cache. It's true that
10346 this may be a bit late to catch such changes, but the rest of
10347 redisplay goes (non-fatally) haywire when the display table is
10348 changed, so why should we worry about doing any better? */
10349 if (current_buffer->width_run_cache)
10351 struct Lisp_Char_Table *disptab = buffer_display_table ();
10353 if (! disptab_matches_widthtab (disptab,
10354 XVECTOR (current_buffer->width_table)))
10356 invalidate_region_cache (current_buffer,
10357 current_buffer->width_run_cache,
10358 BEG, Z);
10359 recompute_width_table (current_buffer, disptab);
10363 /* If window-start is screwed up, choose a new one. */
10364 if (XMARKER (w->start)->buffer != current_buffer)
10365 goto recenter;
10367 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10369 /* If someone specified a new starting point but did not insist,
10370 check whether it can be used. */
10371 if (!NILP (w->optional_new_start)
10372 && CHARPOS (startp) >= BEGV
10373 && CHARPOS (startp) <= ZV)
10375 w->optional_new_start = Qnil;
10376 start_display (&it, w, startp);
10377 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10378 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10379 if (IT_CHARPOS (it) == PT)
10380 w->force_start = Qt;
10383 /* Handle case where place to start displaying has been specified,
10384 unless the specified location is outside the accessible range. */
10385 if (!NILP (w->force_start)
10386 || w->frozen_window_start_p)
10388 /* We set this later on if we have to adjust point. */
10389 int new_vpos = -1;
10391 w->force_start = Qnil;
10392 w->vscroll = 0;
10393 w->window_end_valid = Qnil;
10395 /* Forget any recorded base line for line number display. */
10396 if (!buffer_unchanged_p)
10397 w->base_line_number = Qnil;
10399 /* Redisplay the mode line. Select the buffer properly for that.
10400 Also, run the hook window-scroll-functions
10401 because we have scrolled. */
10402 /* Note, we do this after clearing force_start because
10403 if there's an error, it is better to forget about force_start
10404 than to get into an infinite loop calling the hook functions
10405 and having them get more errors. */
10406 if (!update_mode_line
10407 || ! NILP (Vwindow_scroll_functions))
10409 update_mode_line = 1;
10410 w->update_mode_line = Qt;
10411 startp = run_window_scroll_functions (window, startp);
10414 w->last_modified = make_number (0);
10415 w->last_overlay_modified = make_number (0);
10416 if (CHARPOS (startp) < BEGV)
10417 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10418 else if (CHARPOS (startp) > ZV)
10419 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10421 /* Redisplay, then check if cursor has been set during the
10422 redisplay. Give up if new fonts were loaded. */
10423 if (!try_window (window, startp))
10425 w->force_start = Qt;
10426 clear_glyph_matrix (w->desired_matrix);
10427 goto need_larger_matrices;
10430 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10432 /* If point does not appear, try to move point so it does
10433 appear. The desired matrix has been built above, so we
10434 can use it here. */
10435 new_vpos = window_box_height (w) / 2;
10438 if (!make_cursor_line_fully_visible (w))
10440 /* Point does appear, but on a line partly visible at end of window.
10441 Move it back to a fully-visible line. */
10442 new_vpos = window_box_height (w);
10445 /* If we need to move point for either of the above reasons,
10446 now actually do it. */
10447 if (new_vpos >= 0)
10449 struct glyph_row *row;
10451 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10452 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
10453 ++row;
10455 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10456 MATRIX_ROW_START_BYTEPOS (row));
10458 if (w != XWINDOW (selected_window))
10459 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10460 else if (current_buffer == old)
10461 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10463 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10465 /* If we are highlighting the region, then we just changed
10466 the region, so redisplay to show it. */
10467 if (!NILP (Vtransient_mark_mode)
10468 && !NILP (current_buffer->mark_active))
10470 clear_glyph_matrix (w->desired_matrix);
10471 if (!try_window (window, startp))
10472 goto need_larger_matrices;
10476 #if GLYPH_DEBUG
10477 debug_method_add (w, "forced window start");
10478 #endif
10479 goto done;
10482 /* Handle case where text has not changed, only point, and it has
10483 not moved off the frame, and we are not retrying after hscroll.
10484 (current_matrix_up_to_date_p is nonzero when retrying.) */
10485 if (current_matrix_up_to_date_p
10486 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10487 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10489 switch (rc)
10491 case CURSOR_MOVEMENT_SUCCESS:
10492 goto done;
10494 #if 0 /* try_cursor_movement never returns this value. */
10495 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10496 goto need_larger_matrices;
10497 #endif
10499 case CURSOR_MOVEMENT_MUST_SCROLL:
10500 goto try_to_scroll;
10502 default:
10503 abort ();
10506 /* If current starting point was originally the beginning of a line
10507 but no longer is, find a new starting point. */
10508 else if (!NILP (w->start_at_line_beg)
10509 && !(CHARPOS (startp) <= BEGV
10510 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10512 #if GLYPH_DEBUG
10513 debug_method_add (w, "recenter 1");
10514 #endif
10515 goto recenter;
10518 /* Try scrolling with try_window_id. Value is > 0 if update has
10519 been done, it is -1 if we know that the same window start will
10520 not work. It is 0 if unsuccessful for some other reason. */
10521 else if ((tem = try_window_id (w)) != 0)
10523 #if GLYPH_DEBUG
10524 debug_method_add (w, "try_window_id %d", tem);
10525 #endif
10527 if (fonts_changed_p)
10528 goto need_larger_matrices;
10529 if (tem > 0)
10530 goto done;
10532 /* Otherwise try_window_id has returned -1 which means that we
10533 don't want the alternative below this comment to execute. */
10535 else if (CHARPOS (startp) >= BEGV
10536 && CHARPOS (startp) <= ZV
10537 && PT >= CHARPOS (startp)
10538 && (CHARPOS (startp) < ZV
10539 /* Avoid starting at end of buffer. */
10540 || CHARPOS (startp) == BEGV
10541 || (XFASTINT (w->last_modified) >= MODIFF
10542 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10544 #if GLYPH_DEBUG
10545 debug_method_add (w, "same window start");
10546 #endif
10548 /* Try to redisplay starting at same place as before.
10549 If point has not moved off frame, accept the results. */
10550 if (!current_matrix_up_to_date_p
10551 /* Don't use try_window_reusing_current_matrix in this case
10552 because a window scroll function can have changed the
10553 buffer. */
10554 || !NILP (Vwindow_scroll_functions)
10555 || MINI_WINDOW_P (w)
10556 || !try_window_reusing_current_matrix (w))
10558 IF_DEBUG (debug_method_add (w, "1"));
10559 try_window (window, startp);
10562 if (fonts_changed_p)
10563 goto need_larger_matrices;
10565 if (w->cursor.vpos >= 0)
10567 if (!just_this_one_p
10568 || current_buffer->clip_changed
10569 || BEG_UNCHANGED < CHARPOS (startp))
10570 /* Forget any recorded base line for line number display. */
10571 w->base_line_number = Qnil;
10573 if (!make_cursor_line_fully_visible (w))
10575 clear_glyph_matrix (w->desired_matrix);
10576 last_line_misfit = 1;
10578 /* Drop through and scroll. */
10579 else
10580 goto done;
10582 else
10583 clear_glyph_matrix (w->desired_matrix);
10586 try_to_scroll:
10588 w->last_modified = make_number (0);
10589 w->last_overlay_modified = make_number (0);
10591 /* Redisplay the mode line. Select the buffer properly for that. */
10592 if (!update_mode_line)
10594 update_mode_line = 1;
10595 w->update_mode_line = Qt;
10598 /* Try to scroll by specified few lines. */
10599 if ((scroll_conservatively
10600 || scroll_step
10601 || temp_scroll_step
10602 || NUMBERP (current_buffer->scroll_up_aggressively)
10603 || NUMBERP (current_buffer->scroll_down_aggressively))
10604 && !current_buffer->clip_changed
10605 && CHARPOS (startp) >= BEGV
10606 && CHARPOS (startp) <= ZV)
10608 /* The function returns -1 if new fonts were loaded, 1 if
10609 successful, 0 if not successful. */
10610 int rc = try_scrolling (window, just_this_one_p,
10611 scroll_conservatively,
10612 scroll_step,
10613 temp_scroll_step, last_line_misfit);
10614 switch (rc)
10616 case SCROLLING_SUCCESS:
10617 goto done;
10619 case SCROLLING_NEED_LARGER_MATRICES:
10620 goto need_larger_matrices;
10622 case SCROLLING_FAILED:
10623 break;
10625 default:
10626 abort ();
10630 /* Finally, just choose place to start which centers point */
10632 recenter:
10633 centering_position = window_box_height (w) / 2;
10635 point_at_top:
10636 /* Jump here with centering_position already set to 0. */
10638 #if GLYPH_DEBUG
10639 debug_method_add (w, "recenter");
10640 #endif
10642 /* w->vscroll = 0; */
10644 /* Forget any previously recorded base line for line number display. */
10645 if (!buffer_unchanged_p)
10646 w->base_line_number = Qnil;
10648 /* Move backward half the height of the window. */
10649 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10650 it.current_y = it.last_visible_y;
10651 move_it_vertically_backward (&it, centering_position);
10652 xassert (IT_CHARPOS (it) >= BEGV);
10654 /* The function move_it_vertically_backward may move over more
10655 than the specified y-distance. If it->w is small, e.g. a
10656 mini-buffer window, we may end up in front of the window's
10657 display area. Start displaying at the start of the line
10658 containing PT in this case. */
10659 if (it.current_y <= 0)
10661 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10662 move_it_vertically (&it, 0);
10663 xassert (IT_CHARPOS (it) <= PT);
10664 it.current_y = 0;
10667 it.current_x = it.hpos = 0;
10669 /* Set startp here explicitly in case that helps avoid an infinite loop
10670 in case the window-scroll-functions functions get errors. */
10671 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10673 /* Run scroll hooks. */
10674 startp = run_window_scroll_functions (window, it.current.pos);
10676 /* Redisplay the window. */
10677 if (!current_matrix_up_to_date_p
10678 || windows_or_buffers_changed
10679 || cursor_type_changed
10680 /* Don't use try_window_reusing_current_matrix in this case
10681 because it can have changed the buffer. */
10682 || !NILP (Vwindow_scroll_functions)
10683 || !just_this_one_p
10684 || MINI_WINDOW_P (w)
10685 || !try_window_reusing_current_matrix (w))
10686 try_window (window, startp);
10688 /* If new fonts have been loaded (due to fontsets), give up. We
10689 have to start a new redisplay since we need to re-adjust glyph
10690 matrices. */
10691 if (fonts_changed_p)
10692 goto need_larger_matrices;
10694 /* If cursor did not appear assume that the middle of the window is
10695 in the first line of the window. Do it again with the next line.
10696 (Imagine a window of height 100, displaying two lines of height
10697 60. Moving back 50 from it->last_visible_y will end in the first
10698 line.) */
10699 if (w->cursor.vpos < 0)
10701 if (!NILP (w->window_end_valid)
10702 && PT >= Z - XFASTINT (w->window_end_pos))
10704 clear_glyph_matrix (w->desired_matrix);
10705 move_it_by_lines (&it, 1, 0);
10706 try_window (window, it.current.pos);
10708 else if (PT < IT_CHARPOS (it))
10710 clear_glyph_matrix (w->desired_matrix);
10711 move_it_by_lines (&it, -1, 0);
10712 try_window (window, it.current.pos);
10714 else
10716 /* Not much we can do about it. */
10720 /* Consider the following case: Window starts at BEGV, there is
10721 invisible, intangible text at BEGV, so that display starts at
10722 some point START > BEGV. It can happen that we are called with
10723 PT somewhere between BEGV and START. Try to handle that case. */
10724 if (w->cursor.vpos < 0)
10726 struct glyph_row *row = w->current_matrix->rows;
10727 if (row->mode_line_p)
10728 ++row;
10729 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10732 if (!make_cursor_line_fully_visible (w))
10734 /* If centering point failed to make the whole line visible,
10735 put point at the top instead. That has to make the whole line
10736 visible, if it can be done. */
10737 centering_position = 0;
10738 goto point_at_top;
10741 done:
10743 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10744 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10745 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10746 ? Qt : Qnil);
10748 /* Display the mode line, if we must. */
10749 if ((update_mode_line
10750 /* If window not full width, must redo its mode line
10751 if (a) the window to its side is being redone and
10752 (b) we do a frame-based redisplay. This is a consequence
10753 of how inverted lines are drawn in frame-based redisplay. */
10754 || (!just_this_one_p
10755 && !FRAME_WINDOW_P (f)
10756 && !WINDOW_FULL_WIDTH_P (w))
10757 /* Line number to display. */
10758 || INTEGERP (w->base_line_pos)
10759 /* Column number is displayed and different from the one displayed. */
10760 || (!NILP (w->column_number_displayed)
10761 && (XFASTINT (w->column_number_displayed)
10762 != (int) current_column ()))) /* iftc */
10763 /* This means that the window has a mode line. */
10764 && (WINDOW_WANTS_MODELINE_P (w)
10765 || WINDOW_WANTS_HEADER_LINE_P (w)))
10767 display_mode_lines (w);
10769 /* If mode line height has changed, arrange for a thorough
10770 immediate redisplay using the correct mode line height. */
10771 if (WINDOW_WANTS_MODELINE_P (w)
10772 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10774 fonts_changed_p = 1;
10775 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10776 = DESIRED_MODE_LINE_HEIGHT (w);
10779 /* If top line height has changed, arrange for a thorough
10780 immediate redisplay using the correct mode line height. */
10781 if (WINDOW_WANTS_HEADER_LINE_P (w)
10782 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10784 fonts_changed_p = 1;
10785 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10786 = DESIRED_HEADER_LINE_HEIGHT (w);
10789 if (fonts_changed_p)
10790 goto need_larger_matrices;
10793 if (!line_number_displayed
10794 && !BUFFERP (w->base_line_pos))
10796 w->base_line_pos = Qnil;
10797 w->base_line_number = Qnil;
10800 finish_menu_bars:
10802 /* When we reach a frame's selected window, redo the frame's menu bar. */
10803 if (update_mode_line
10804 && EQ (FRAME_SELECTED_WINDOW (f), window))
10806 int redisplay_menu_p = 0;
10807 int redisplay_tool_bar_p = 0;
10809 if (FRAME_WINDOW_P (f))
10811 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
10812 || defined (USE_GTK)
10813 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10814 #else
10815 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10816 #endif
10818 else
10819 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10821 if (redisplay_menu_p)
10822 display_menu_bar (w);
10824 #ifdef HAVE_WINDOW_SYSTEM
10825 #ifdef USE_GTK
10826 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
10827 #else
10828 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
10829 && (FRAME_TOOL_BAR_LINES (f) > 0
10830 || auto_resize_tool_bars_p);
10832 #endif
10834 if (redisplay_tool_bar_p)
10835 redisplay_tool_bar (f);
10836 #endif
10839 /* We go to this label, with fonts_changed_p nonzero,
10840 if it is necessary to try again using larger glyph matrices.
10841 We have to redeem the scroll bar even in this case,
10842 because the loop in redisplay_internal expects that. */
10843 need_larger_matrices:
10845 finish_scroll_bars:
10847 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10849 int start, end, whole;
10851 /* Calculate the start and end positions for the current window.
10852 At some point, it would be nice to choose between scrollbars
10853 which reflect the whole buffer size, with special markers
10854 indicating narrowing, and scrollbars which reflect only the
10855 visible region.
10857 Note that mini-buffers sometimes aren't displaying any text. */
10858 if (!MINI_WINDOW_P (w)
10859 || (w == XWINDOW (minibuf_window)
10860 && NILP (echo_area_buffer[0])))
10862 whole = ZV - BEGV;
10863 start = marker_position (w->start) - BEGV;
10864 /* I don't think this is guaranteed to be right. For the
10865 moment, we'll pretend it is. */
10866 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10868 if (end < start)
10869 end = start;
10870 if (whole < (end - start))
10871 whole = end - start;
10873 else
10874 start = end = whole = 0;
10876 /* Indicate what this scroll bar ought to be displaying now. */
10877 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10879 /* Note that we actually used the scroll bar attached to this
10880 window, so it shouldn't be deleted at the end of redisplay. */
10881 redeem_scroll_bar_hook (w);
10884 /* Restore current_buffer and value of point in it. */
10885 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10886 set_buffer_internal_1 (old);
10887 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10889 unbind_to (count, Qnil);
10893 /* Build the complete desired matrix of WINDOW with a window start
10894 buffer position POS. Value is non-zero if successful. It is zero
10895 if fonts were loaded during redisplay which makes re-adjusting
10896 glyph matrices necessary. */
10899 try_window (window, pos)
10900 Lisp_Object window;
10901 struct text_pos pos;
10903 struct window *w = XWINDOW (window);
10904 struct it it;
10905 struct glyph_row *last_text_row = NULL;
10907 /* Make POS the new window start. */
10908 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10910 /* Mark cursor position as unknown. No overlay arrow seen. */
10911 w->cursor.vpos = -1;
10912 overlay_arrow_seen = 0;
10914 /* Initialize iterator and info to start at POS. */
10915 start_display (&it, w, pos);
10917 /* Display all lines of W. */
10918 while (it.current_y < it.last_visible_y)
10920 if (display_line (&it))
10921 last_text_row = it.glyph_row - 1;
10922 if (fonts_changed_p)
10923 return 0;
10926 /* If bottom moved off end of frame, change mode line percentage. */
10927 if (XFASTINT (w->window_end_pos) <= 0
10928 && Z != IT_CHARPOS (it))
10929 w->update_mode_line = Qt;
10931 /* Set window_end_pos to the offset of the last character displayed
10932 on the window from the end of current_buffer. Set
10933 window_end_vpos to its row number. */
10934 if (last_text_row)
10936 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10937 w->window_end_bytepos
10938 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10939 w->window_end_pos
10940 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10941 w->window_end_vpos
10942 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10943 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10944 ->displays_text_p);
10946 else
10948 w->window_end_bytepos = 0;
10949 w->window_end_pos = w->window_end_vpos = make_number (0);
10952 /* But that is not valid info until redisplay finishes. */
10953 w->window_end_valid = Qnil;
10954 return 1;
10959 /************************************************************************
10960 Window redisplay reusing current matrix when buffer has not changed
10961 ************************************************************************/
10963 /* Try redisplay of window W showing an unchanged buffer with a
10964 different window start than the last time it was displayed by
10965 reusing its current matrix. Value is non-zero if successful.
10966 W->start is the new window start. */
10968 static int
10969 try_window_reusing_current_matrix (w)
10970 struct window *w;
10972 struct frame *f = XFRAME (w->frame);
10973 struct glyph_row *row, *bottom_row;
10974 struct it it;
10975 struct run run;
10976 struct text_pos start, new_start;
10977 int nrows_scrolled, i;
10978 struct glyph_row *last_text_row;
10979 struct glyph_row *last_reused_text_row;
10980 struct glyph_row *start_row;
10981 int start_vpos, min_y, max_y;
10983 #if GLYPH_DEBUG
10984 if (inhibit_try_window_reusing)
10985 return 0;
10986 #endif
10988 if (/* This function doesn't handle terminal frames. */
10989 !FRAME_WINDOW_P (f)
10990 /* Don't try to reuse the display if windows have been split
10991 or such. */
10992 || windows_or_buffers_changed
10993 || cursor_type_changed)
10994 return 0;
10996 /* Can't do this if region may have changed. */
10997 if ((!NILP (Vtransient_mark_mode)
10998 && !NILP (current_buffer->mark_active))
10999 || !NILP (w->region_showing)
11000 || !NILP (Vshow_trailing_whitespace))
11001 return 0;
11003 /* If top-line visibility has changed, give up. */
11004 if (WINDOW_WANTS_HEADER_LINE_P (w)
11005 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
11006 return 0;
11008 /* Give up if old or new display is scrolled vertically. We could
11009 make this function handle this, but right now it doesn't. */
11010 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11011 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
11012 return 0;
11014 /* The variable new_start now holds the new window start. The old
11015 start `start' can be determined from the current matrix. */
11016 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
11017 start = start_row->start.pos;
11018 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
11020 /* Clear the desired matrix for the display below. */
11021 clear_glyph_matrix (w->desired_matrix);
11023 if (CHARPOS (new_start) <= CHARPOS (start))
11025 int first_row_y;
11027 /* Don't use this method if the display starts with an ellipsis
11028 displayed for invisible text. It's not easy to handle that case
11029 below, and it's certainly not worth the effort since this is
11030 not a frequent case. */
11031 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
11032 return 0;
11034 IF_DEBUG (debug_method_add (w, "twu1"));
11036 /* Display up to a row that can be reused. The variable
11037 last_text_row is set to the last row displayed that displays
11038 text. Note that it.vpos == 0 if or if not there is a
11039 header-line; it's not the same as the MATRIX_ROW_VPOS! */
11040 start_display (&it, w, new_start);
11041 first_row_y = it.current_y;
11042 w->cursor.vpos = -1;
11043 last_text_row = last_reused_text_row = NULL;
11045 while (it.current_y < it.last_visible_y
11046 && IT_CHARPOS (it) < CHARPOS (start)
11047 && !fonts_changed_p)
11048 if (display_line (&it))
11049 last_text_row = it.glyph_row - 1;
11051 /* A value of current_y < last_visible_y means that we stopped
11052 at the previous window start, which in turn means that we
11053 have at least one reusable row. */
11054 if (it.current_y < it.last_visible_y)
11056 /* IT.vpos always starts from 0; it counts text lines. */
11057 nrows_scrolled = it.vpos;
11059 /* Find PT if not already found in the lines displayed. */
11060 if (w->cursor.vpos < 0)
11062 int dy = it.current_y - first_row_y;
11064 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11065 row = row_containing_pos (w, PT, row, NULL, dy);
11066 if (row)
11067 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11068 dy, nrows_scrolled);
11069 else
11071 clear_glyph_matrix (w->desired_matrix);
11072 return 0;
11076 /* Scroll the display. Do it before the current matrix is
11077 changed. The problem here is that update has not yet
11078 run, i.e. part of the current matrix is not up to date.
11079 scroll_run_hook will clear the cursor, and use the
11080 current matrix to get the height of the row the cursor is
11081 in. */
11082 run.current_y = first_row_y;
11083 run.desired_y = it.current_y;
11084 run.height = it.last_visible_y - it.current_y;
11086 if (run.height > 0 && run.current_y != run.desired_y)
11088 update_begin (f);
11089 rif->update_window_begin_hook (w);
11090 rif->clear_mouse_face (w);
11091 rif->scroll_run_hook (w, &run);
11092 rif->update_window_end_hook (w, 0, 0);
11093 update_end (f);
11096 /* Shift current matrix down by nrows_scrolled lines. */
11097 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11098 rotate_matrix (w->current_matrix,
11099 start_vpos,
11100 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11101 nrows_scrolled);
11103 /* Disable lines that must be updated. */
11104 for (i = 0; i < it.vpos; ++i)
11105 (start_row + i)->enabled_p = 0;
11107 /* Re-compute Y positions. */
11108 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11109 max_y = it.last_visible_y;
11110 for (row = start_row + nrows_scrolled;
11111 row < bottom_row;
11112 ++row)
11114 row->y = it.current_y;
11115 row->visible_height = row->height;
11117 if (row->y < min_y)
11118 row->visible_height -= min_y - row->y;
11119 if (row->y + row->height > max_y)
11120 row->visible_height -= row->y + row->height - max_y;
11122 it.current_y += row->height;
11124 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11125 last_reused_text_row = row;
11126 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11127 break;
11130 /* Disable lines in the current matrix which are now
11131 below the window. */
11132 for (++row; row < bottom_row; ++row)
11133 row->enabled_p = 0;
11136 /* Update window_end_pos etc.; last_reused_text_row is the last
11137 reused row from the current matrix containing text, if any.
11138 The value of last_text_row is the last displayed line
11139 containing text. */
11140 if (last_reused_text_row)
11142 w->window_end_bytepos
11143 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11144 w->window_end_pos
11145 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11146 w->window_end_vpos
11147 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11148 w->current_matrix));
11150 else if (last_text_row)
11152 w->window_end_bytepos
11153 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11154 w->window_end_pos
11155 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11156 w->window_end_vpos
11157 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11159 else
11161 /* This window must be completely empty. */
11162 w->window_end_bytepos = 0;
11163 w->window_end_pos = w->window_end_vpos = make_number (0);
11165 w->window_end_valid = Qnil;
11167 /* Update hint: don't try scrolling again in update_window. */
11168 w->desired_matrix->no_scrolling_p = 1;
11170 #if GLYPH_DEBUG
11171 debug_method_add (w, "try_window_reusing_current_matrix 1");
11172 #endif
11173 return 1;
11175 else if (CHARPOS (new_start) > CHARPOS (start))
11177 struct glyph_row *pt_row, *row;
11178 struct glyph_row *first_reusable_row;
11179 struct glyph_row *first_row_to_display;
11180 int dy;
11181 int yb = window_text_bottom_y (w);
11183 /* Find the row starting at new_start, if there is one. Don't
11184 reuse a partially visible line at the end. */
11185 first_reusable_row = start_row;
11186 while (first_reusable_row->enabled_p
11187 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11188 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11189 < CHARPOS (new_start)))
11190 ++first_reusable_row;
11192 /* Give up if there is no row to reuse. */
11193 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11194 || !first_reusable_row->enabled_p
11195 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11196 != CHARPOS (new_start)))
11197 return 0;
11199 /* We can reuse fully visible rows beginning with
11200 first_reusable_row to the end of the window. Set
11201 first_row_to_display to the first row that cannot be reused.
11202 Set pt_row to the row containing point, if there is any. */
11203 pt_row = NULL;
11204 for (first_row_to_display = first_reusable_row;
11205 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11206 ++first_row_to_display)
11208 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11209 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11210 pt_row = first_row_to_display;
11213 /* Start displaying at the start of first_row_to_display. */
11214 xassert (first_row_to_display->y < yb);
11215 init_to_row_start (&it, w, first_row_to_display);
11217 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11218 - start_vpos);
11219 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11220 - nrows_scrolled);
11221 it.current_y = (first_row_to_display->y - first_reusable_row->y
11222 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11224 /* Display lines beginning with first_row_to_display in the
11225 desired matrix. Set last_text_row to the last row displayed
11226 that displays text. */
11227 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11228 if (pt_row == NULL)
11229 w->cursor.vpos = -1;
11230 last_text_row = NULL;
11231 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11232 if (display_line (&it))
11233 last_text_row = it.glyph_row - 1;
11235 /* Give up If point isn't in a row displayed or reused. */
11236 if (w->cursor.vpos < 0)
11238 clear_glyph_matrix (w->desired_matrix);
11239 return 0;
11242 /* If point is in a reused row, adjust y and vpos of the cursor
11243 position. */
11244 if (pt_row)
11246 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11247 w->current_matrix);
11248 w->cursor.y -= first_reusable_row->y;
11251 /* Scroll the display. */
11252 run.current_y = first_reusable_row->y;
11253 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11254 run.height = it.last_visible_y - run.current_y;
11255 dy = run.current_y - run.desired_y;
11257 if (run.height)
11259 struct frame *f = XFRAME (WINDOW_FRAME (w));
11260 update_begin (f);
11261 rif->update_window_begin_hook (w);
11262 rif->clear_mouse_face (w);
11263 rif->scroll_run_hook (w, &run);
11264 rif->update_window_end_hook (w, 0, 0);
11265 update_end (f);
11268 /* Adjust Y positions of reused rows. */
11269 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11270 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11271 max_y = it.last_visible_y;
11272 for (row = first_reusable_row; row < first_row_to_display; ++row)
11274 row->y -= dy;
11275 row->visible_height = row->height;
11276 if (row->y < min_y)
11277 row->visible_height -= min_y - row->y;
11278 if (row->y + row->height > max_y)
11279 row->visible_height -= row->y + row->height - max_y;
11282 /* Scroll the current matrix. */
11283 xassert (nrows_scrolled > 0);
11284 rotate_matrix (w->current_matrix,
11285 start_vpos,
11286 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11287 -nrows_scrolled);
11289 /* Disable rows not reused. */
11290 for (row -= nrows_scrolled; row < bottom_row; ++row)
11291 row->enabled_p = 0;
11293 /* Adjust window end. A null value of last_text_row means that
11294 the window end is in reused rows which in turn means that
11295 only its vpos can have changed. */
11296 if (last_text_row)
11298 w->window_end_bytepos
11299 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11300 w->window_end_pos
11301 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11302 w->window_end_vpos
11303 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11305 else
11307 w->window_end_vpos
11308 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11311 w->window_end_valid = Qnil;
11312 w->desired_matrix->no_scrolling_p = 1;
11314 #if GLYPH_DEBUG
11315 debug_method_add (w, "try_window_reusing_current_matrix 2");
11316 #endif
11317 return 1;
11320 return 0;
11325 /************************************************************************
11326 Window redisplay reusing current matrix when buffer has changed
11327 ************************************************************************/
11329 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11330 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11331 int *, int *));
11332 static struct glyph_row *
11333 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11334 struct glyph_row *));
11337 /* Return the last row in MATRIX displaying text. If row START is
11338 non-null, start searching with that row. IT gives the dimensions
11339 of the display. Value is null if matrix is empty; otherwise it is
11340 a pointer to the row found. */
11342 static struct glyph_row *
11343 find_last_row_displaying_text (matrix, it, start)
11344 struct glyph_matrix *matrix;
11345 struct it *it;
11346 struct glyph_row *start;
11348 struct glyph_row *row, *row_found;
11350 /* Set row_found to the last row in IT->w's current matrix
11351 displaying text. The loop looks funny but think of partially
11352 visible lines. */
11353 row_found = NULL;
11354 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11355 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11357 xassert (row->enabled_p);
11358 row_found = row;
11359 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11360 break;
11361 ++row;
11364 return row_found;
11368 /* Return the last row in the current matrix of W that is not affected
11369 by changes at the start of current_buffer that occurred since W's
11370 current matrix was built. Value is null if no such row exists.
11372 BEG_UNCHANGED us the number of characters unchanged at the start of
11373 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11374 first changed character in current_buffer. Characters at positions <
11375 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11376 when the current matrix was built. */
11378 static struct glyph_row *
11379 find_last_unchanged_at_beg_row (w)
11380 struct window *w;
11382 int first_changed_pos = BEG + BEG_UNCHANGED;
11383 struct glyph_row *row;
11384 struct glyph_row *row_found = NULL;
11385 int yb = window_text_bottom_y (w);
11387 /* Find the last row displaying unchanged text. */
11388 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11389 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11390 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11392 if (/* If row ends before first_changed_pos, it is unchanged,
11393 except in some case. */
11394 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11395 /* When row ends in ZV and we write at ZV it is not
11396 unchanged. */
11397 && !row->ends_at_zv_p
11398 /* When first_changed_pos is the end of a continued line,
11399 row is not unchanged because it may be no longer
11400 continued. */
11401 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11402 && row->continued_p))
11403 row_found = row;
11405 /* Stop if last visible row. */
11406 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11407 break;
11409 ++row;
11412 return row_found;
11416 /* Find the first glyph row in the current matrix of W that is not
11417 affected by changes at the end of current_buffer since the
11418 time W's current matrix was built.
11420 Return in *DELTA the number of chars by which buffer positions in
11421 unchanged text at the end of current_buffer must be adjusted.
11423 Return in *DELTA_BYTES the corresponding number of bytes.
11425 Value is null if no such row exists, i.e. all rows are affected by
11426 changes. */
11428 static struct glyph_row *
11429 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11430 struct window *w;
11431 int *delta, *delta_bytes;
11433 struct glyph_row *row;
11434 struct glyph_row *row_found = NULL;
11436 *delta = *delta_bytes = 0;
11438 /* Display must not have been paused, otherwise the current matrix
11439 is not up to date. */
11440 if (NILP (w->window_end_valid))
11441 abort ();
11443 /* A value of window_end_pos >= END_UNCHANGED means that the window
11444 end is in the range of changed text. If so, there is no
11445 unchanged row at the end of W's current matrix. */
11446 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11447 return NULL;
11449 /* Set row to the last row in W's current matrix displaying text. */
11450 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11452 /* If matrix is entirely empty, no unchanged row exists. */
11453 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11455 /* The value of row is the last glyph row in the matrix having a
11456 meaningful buffer position in it. The end position of row
11457 corresponds to window_end_pos. This allows us to translate
11458 buffer positions in the current matrix to current buffer
11459 positions for characters not in changed text. */
11460 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11461 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11462 int last_unchanged_pos, last_unchanged_pos_old;
11463 struct glyph_row *first_text_row
11464 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11466 *delta = Z - Z_old;
11467 *delta_bytes = Z_BYTE - Z_BYTE_old;
11469 /* Set last_unchanged_pos to the buffer position of the last
11470 character in the buffer that has not been changed. Z is the
11471 index + 1 of the last character in current_buffer, i.e. by
11472 subtracting END_UNCHANGED we get the index of the last
11473 unchanged character, and we have to add BEG to get its buffer
11474 position. */
11475 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11476 last_unchanged_pos_old = last_unchanged_pos - *delta;
11478 /* Search backward from ROW for a row displaying a line that
11479 starts at a minimum position >= last_unchanged_pos_old. */
11480 for (; row > first_text_row; --row)
11482 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11483 abort ();
11485 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11486 row_found = row;
11490 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11491 abort ();
11493 return row_found;
11497 /* Make sure that glyph rows in the current matrix of window W
11498 reference the same glyph memory as corresponding rows in the
11499 frame's frame matrix. This function is called after scrolling W's
11500 current matrix on a terminal frame in try_window_id and
11501 try_window_reusing_current_matrix. */
11503 static void
11504 sync_frame_with_window_matrix_rows (w)
11505 struct window *w;
11507 struct frame *f = XFRAME (w->frame);
11508 struct glyph_row *window_row, *window_row_end, *frame_row;
11510 /* Preconditions: W must be a leaf window and full-width. Its frame
11511 must have a frame matrix. */
11512 xassert (NILP (w->hchild) && NILP (w->vchild));
11513 xassert (WINDOW_FULL_WIDTH_P (w));
11514 xassert (!FRAME_WINDOW_P (f));
11516 /* If W is a full-width window, glyph pointers in W's current matrix
11517 have, by definition, to be the same as glyph pointers in the
11518 corresponding frame matrix. Note that frame matrices have no
11519 marginal areas (see build_frame_matrix). */
11520 window_row = w->current_matrix->rows;
11521 window_row_end = window_row + w->current_matrix->nrows;
11522 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11523 while (window_row < window_row_end)
11525 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11526 struct glyph *end = window_row->glyphs[LAST_AREA];
11528 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11529 frame_row->glyphs[TEXT_AREA] = start;
11530 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11531 frame_row->glyphs[LAST_AREA] = end;
11533 /* Disable frame rows whose corresponding window rows have
11534 been disabled in try_window_id. */
11535 if (!window_row->enabled_p)
11536 frame_row->enabled_p = 0;
11538 ++window_row, ++frame_row;
11543 /* Find the glyph row in window W containing CHARPOS. Consider all
11544 rows between START and END (not inclusive). END null means search
11545 all rows to the end of the display area of W. Value is the row
11546 containing CHARPOS or null. */
11548 struct glyph_row *
11549 row_containing_pos (w, charpos, start, end, dy)
11550 struct window *w;
11551 int charpos;
11552 struct glyph_row *start, *end;
11553 int dy;
11555 struct glyph_row *row = start;
11556 int last_y;
11558 /* If we happen to start on a header-line, skip that. */
11559 if (row->mode_line_p)
11560 ++row;
11562 if ((end && row >= end) || !row->enabled_p)
11563 return NULL;
11565 last_y = window_text_bottom_y (w) - dy;
11567 while (1)
11569 /* Give up if we have gone too far. */
11570 if (end && row >= end)
11571 return NULL;
11572 /* This formerly returned if they were equal.
11573 I think that both quantities are of a "last plus one" type;
11574 if so, when they are equal, the row is within the screen. -- rms. */
11575 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
11576 return NULL;
11578 /* If it is in this row, return this row. */
11579 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
11580 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11581 /* The end position of a row equals the start
11582 position of the next row. If CHARPOS is there, we
11583 would rather display it in the next line, except
11584 when this line ends in ZV. */
11585 && !row->ends_at_zv_p
11586 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11587 && charpos >= MATRIX_ROW_START_CHARPOS (row))
11588 return row;
11589 ++row;
11594 /* Try to redisplay window W by reusing its existing display. W's
11595 current matrix must be up to date when this function is called,
11596 i.e. window_end_valid must not be nil.
11598 Value is
11600 1 if display has been updated
11601 0 if otherwise unsuccessful
11602 -1 if redisplay with same window start is known not to succeed
11604 The following steps are performed:
11606 1. Find the last row in the current matrix of W that is not
11607 affected by changes at the start of current_buffer. If no such row
11608 is found, give up.
11610 2. Find the first row in W's current matrix that is not affected by
11611 changes at the end of current_buffer. Maybe there is no such row.
11613 3. Display lines beginning with the row + 1 found in step 1 to the
11614 row found in step 2 or, if step 2 didn't find a row, to the end of
11615 the window.
11617 4. If cursor is not known to appear on the window, give up.
11619 5. If display stopped at the row found in step 2, scroll the
11620 display and current matrix as needed.
11622 6. Maybe display some lines at the end of W, if we must. This can
11623 happen under various circumstances, like a partially visible line
11624 becoming fully visible, or because newly displayed lines are displayed
11625 in smaller font sizes.
11627 7. Update W's window end information. */
11629 static int
11630 try_window_id (w)
11631 struct window *w;
11633 struct frame *f = XFRAME (w->frame);
11634 struct glyph_matrix *current_matrix = w->current_matrix;
11635 struct glyph_matrix *desired_matrix = w->desired_matrix;
11636 struct glyph_row *last_unchanged_at_beg_row;
11637 struct glyph_row *first_unchanged_at_end_row;
11638 struct glyph_row *row;
11639 struct glyph_row *bottom_row;
11640 int bottom_vpos;
11641 struct it it;
11642 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11643 struct text_pos start_pos;
11644 struct run run;
11645 int first_unchanged_at_end_vpos = 0;
11646 struct glyph_row *last_text_row, *last_text_row_at_end;
11647 struct text_pos start;
11648 int first_changed_charpos, last_changed_charpos;
11650 #if GLYPH_DEBUG
11651 if (inhibit_try_window_id)
11652 return 0;
11653 #endif
11655 /* This is handy for debugging. */
11656 #if 0
11657 #define GIVE_UP(X) \
11658 do { \
11659 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11660 return 0; \
11661 } while (0)
11662 #else
11663 #define GIVE_UP(X) return 0
11664 #endif
11666 SET_TEXT_POS_FROM_MARKER (start, w->start);
11668 /* Don't use this for mini-windows because these can show
11669 messages and mini-buffers, and we don't handle that here. */
11670 if (MINI_WINDOW_P (w))
11671 GIVE_UP (1);
11673 /* This flag is used to prevent redisplay optimizations. */
11674 if (windows_or_buffers_changed || cursor_type_changed)
11675 GIVE_UP (2);
11677 /* Verify that narrowing has not changed.
11678 Also verify that we were not told to prevent redisplay optimizations.
11679 It would be nice to further
11680 reduce the number of cases where this prevents try_window_id. */
11681 if (current_buffer->clip_changed
11682 || current_buffer->prevent_redisplay_optimizations_p)
11683 GIVE_UP (3);
11685 /* Window must either use window-based redisplay or be full width. */
11686 if (!FRAME_WINDOW_P (f)
11687 && (!line_ins_del_ok
11688 || !WINDOW_FULL_WIDTH_P (w)))
11689 GIVE_UP (4);
11691 /* Give up if point is not known NOT to appear in W. */
11692 if (PT < CHARPOS (start))
11693 GIVE_UP (5);
11695 /* Another way to prevent redisplay optimizations. */
11696 if (XFASTINT (w->last_modified) == 0)
11697 GIVE_UP (6);
11699 /* Verify that window is not hscrolled. */
11700 if (XFASTINT (w->hscroll) != 0)
11701 GIVE_UP (7);
11703 /* Verify that display wasn't paused. */
11704 if (NILP (w->window_end_valid))
11705 GIVE_UP (8);
11707 /* Can't use this if highlighting a region because a cursor movement
11708 will do more than just set the cursor. */
11709 if (!NILP (Vtransient_mark_mode)
11710 && !NILP (current_buffer->mark_active))
11711 GIVE_UP (9);
11713 /* Likewise if highlighting trailing whitespace. */
11714 if (!NILP (Vshow_trailing_whitespace))
11715 GIVE_UP (11);
11717 /* Likewise if showing a region. */
11718 if (!NILP (w->region_showing))
11719 GIVE_UP (10);
11721 /* Can use this if overlay arrow position and or string have changed. */
11722 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11723 || !EQ (last_arrow_string, Voverlay_arrow_string))
11724 GIVE_UP (12);
11727 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11728 only if buffer has really changed. The reason is that the gap is
11729 initially at Z for freshly visited files. The code below would
11730 set end_unchanged to 0 in that case. */
11731 if (MODIFF > SAVE_MODIFF
11732 /* This seems to happen sometimes after saving a buffer. */
11733 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11735 if (GPT - BEG < BEG_UNCHANGED)
11736 BEG_UNCHANGED = GPT - BEG;
11737 if (Z - GPT < END_UNCHANGED)
11738 END_UNCHANGED = Z - GPT;
11741 /* The position of the first and last character that has been changed. */
11742 first_changed_charpos = BEG + BEG_UNCHANGED;
11743 last_changed_charpos = Z - END_UNCHANGED;
11745 /* If window starts after a line end, and the last change is in
11746 front of that newline, then changes don't affect the display.
11747 This case happens with stealth-fontification. Note that although
11748 the display is unchanged, glyph positions in the matrix have to
11749 be adjusted, of course. */
11750 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11751 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11752 && ((last_changed_charpos < CHARPOS (start)
11753 && CHARPOS (start) == BEGV)
11754 || (last_changed_charpos < CHARPOS (start) - 1
11755 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11757 int Z_old, delta, Z_BYTE_old, delta_bytes;
11758 struct glyph_row *r0;
11760 /* Compute how many chars/bytes have been added to or removed
11761 from the buffer. */
11762 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11763 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11764 delta = Z - Z_old;
11765 delta_bytes = Z_BYTE - Z_BYTE_old;
11767 /* Give up if PT is not in the window. Note that it already has
11768 been checked at the start of try_window_id that PT is not in
11769 front of the window start. */
11770 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11771 GIVE_UP (13);
11773 /* If window start is unchanged, we can reuse the whole matrix
11774 as is, after adjusting glyph positions. No need to compute
11775 the window end again, since its offset from Z hasn't changed. */
11776 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11777 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11778 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11780 /* Adjust positions in the glyph matrix. */
11781 if (delta || delta_bytes)
11783 struct glyph_row *r1
11784 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11785 increment_matrix_positions (w->current_matrix,
11786 MATRIX_ROW_VPOS (r0, current_matrix),
11787 MATRIX_ROW_VPOS (r1, current_matrix),
11788 delta, delta_bytes);
11791 /* Set the cursor. */
11792 row = row_containing_pos (w, PT, r0, NULL, 0);
11793 if (row)
11794 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11795 else
11796 abort ();
11797 return 1;
11801 /* Handle the case that changes are all below what is displayed in
11802 the window, and that PT is in the window. This shortcut cannot
11803 be taken if ZV is visible in the window, and text has been added
11804 there that is visible in the window. */
11805 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11806 /* ZV is not visible in the window, or there are no
11807 changes at ZV, actually. */
11808 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11809 || first_changed_charpos == last_changed_charpos))
11811 struct glyph_row *r0;
11813 /* Give up if PT is not in the window. Note that it already has
11814 been checked at the start of try_window_id that PT is not in
11815 front of the window start. */
11816 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11817 GIVE_UP (14);
11819 /* If window start is unchanged, we can reuse the whole matrix
11820 as is, without changing glyph positions since no text has
11821 been added/removed in front of the window end. */
11822 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11823 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11825 /* We have to compute the window end anew since text
11826 can have been added/removed after it. */
11827 w->window_end_pos
11828 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11829 w->window_end_bytepos
11830 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11832 /* Set the cursor. */
11833 row = row_containing_pos (w, PT, r0, NULL, 0);
11834 if (row)
11835 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11836 else
11837 abort ();
11838 return 2;
11842 /* Give up if window start is in the changed area.
11844 The condition used to read
11846 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11848 but why that was tested escapes me at the moment. */
11849 if (CHARPOS (start) >= first_changed_charpos
11850 && CHARPOS (start) <= last_changed_charpos)
11851 GIVE_UP (15);
11853 /* Check that window start agrees with the start of the first glyph
11854 row in its current matrix. Check this after we know the window
11855 start is not in changed text, otherwise positions would not be
11856 comparable. */
11857 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11858 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11859 GIVE_UP (16);
11861 /* Give up if the window ends in strings. Overlay strings
11862 at the end are difficult to handle, so don't try. */
11863 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11864 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11865 GIVE_UP (20);
11867 /* Compute the position at which we have to start displaying new
11868 lines. Some of the lines at the top of the window might be
11869 reusable because they are not displaying changed text. Find the
11870 last row in W's current matrix not affected by changes at the
11871 start of current_buffer. Value is null if changes start in the
11872 first line of window. */
11873 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11874 if (last_unchanged_at_beg_row)
11876 /* Avoid starting to display in the moddle of a character, a TAB
11877 for instance. This is easier than to set up the iterator
11878 exactly, and it's not a frequent case, so the additional
11879 effort wouldn't really pay off. */
11880 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11881 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11882 && last_unchanged_at_beg_row > w->current_matrix->rows)
11883 --last_unchanged_at_beg_row;
11885 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11886 GIVE_UP (17);
11888 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11889 GIVE_UP (18);
11890 start_pos = it.current.pos;
11892 /* Start displaying new lines in the desired matrix at the same
11893 vpos we would use in the current matrix, i.e. below
11894 last_unchanged_at_beg_row. */
11895 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11896 current_matrix);
11897 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11898 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11900 xassert (it.hpos == 0 && it.current_x == 0);
11902 else
11904 /* There are no reusable lines at the start of the window.
11905 Start displaying in the first line. */
11906 start_display (&it, w, start);
11907 start_pos = it.current.pos;
11910 /* Find the first row that is not affected by changes at the end of
11911 the buffer. Value will be null if there is no unchanged row, in
11912 which case we must redisplay to the end of the window. delta
11913 will be set to the value by which buffer positions beginning with
11914 first_unchanged_at_end_row have to be adjusted due to text
11915 changes. */
11916 first_unchanged_at_end_row
11917 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11918 IF_DEBUG (debug_delta = delta);
11919 IF_DEBUG (debug_delta_bytes = delta_bytes);
11921 /* Set stop_pos to the buffer position up to which we will have to
11922 display new lines. If first_unchanged_at_end_row != NULL, this
11923 is the buffer position of the start of the line displayed in that
11924 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11925 that we don't stop at a buffer position. */
11926 stop_pos = 0;
11927 if (first_unchanged_at_end_row)
11929 xassert (last_unchanged_at_beg_row == NULL
11930 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11932 /* If this is a continuation line, move forward to the next one
11933 that isn't. Changes in lines above affect this line.
11934 Caution: this may move first_unchanged_at_end_row to a row
11935 not displaying text. */
11936 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11937 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11938 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11939 < it.last_visible_y))
11940 ++first_unchanged_at_end_row;
11942 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11943 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11944 >= it.last_visible_y))
11945 first_unchanged_at_end_row = NULL;
11946 else
11948 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11949 + delta);
11950 first_unchanged_at_end_vpos
11951 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11952 xassert (stop_pos >= Z - END_UNCHANGED);
11955 else if (last_unchanged_at_beg_row == NULL)
11956 GIVE_UP (19);
11959 #if GLYPH_DEBUG
11961 /* Either there is no unchanged row at the end, or the one we have
11962 now displays text. This is a necessary condition for the window
11963 end pos calculation at the end of this function. */
11964 xassert (first_unchanged_at_end_row == NULL
11965 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11967 debug_last_unchanged_at_beg_vpos
11968 = (last_unchanged_at_beg_row
11969 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11970 : -1);
11971 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11973 #endif /* GLYPH_DEBUG != 0 */
11976 /* Display new lines. Set last_text_row to the last new line
11977 displayed which has text on it, i.e. might end up as being the
11978 line where the window_end_vpos is. */
11979 w->cursor.vpos = -1;
11980 last_text_row = NULL;
11981 overlay_arrow_seen = 0;
11982 while (it.current_y < it.last_visible_y
11983 && !fonts_changed_p
11984 && (first_unchanged_at_end_row == NULL
11985 || IT_CHARPOS (it) < stop_pos))
11987 if (display_line (&it))
11988 last_text_row = it.glyph_row - 1;
11991 if (fonts_changed_p)
11992 return -1;
11995 /* Compute differences in buffer positions, y-positions etc. for
11996 lines reused at the bottom of the window. Compute what we can
11997 scroll. */
11998 if (first_unchanged_at_end_row
11999 /* No lines reused because we displayed everything up to the
12000 bottom of the window. */
12001 && it.current_y < it.last_visible_y)
12003 dvpos = (it.vpos
12004 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
12005 current_matrix));
12006 dy = it.current_y - first_unchanged_at_end_row->y;
12007 run.current_y = first_unchanged_at_end_row->y;
12008 run.desired_y = run.current_y + dy;
12009 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
12011 else
12013 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
12014 first_unchanged_at_end_row = NULL;
12016 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
12019 /* Find the cursor if not already found. We have to decide whether
12020 PT will appear on this window (it sometimes doesn't, but this is
12021 not a very frequent case.) This decision has to be made before
12022 the current matrix is altered. A value of cursor.vpos < 0 means
12023 that PT is either in one of the lines beginning at
12024 first_unchanged_at_end_row or below the window. Don't care for
12025 lines that might be displayed later at the window end; as
12026 mentioned, this is not a frequent case. */
12027 if (w->cursor.vpos < 0)
12029 /* Cursor in unchanged rows at the top? */
12030 if (PT < CHARPOS (start_pos)
12031 && last_unchanged_at_beg_row)
12033 row = row_containing_pos (w, PT,
12034 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
12035 last_unchanged_at_beg_row + 1, 0);
12036 if (row)
12037 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12040 /* Start from first_unchanged_at_end_row looking for PT. */
12041 else if (first_unchanged_at_end_row)
12043 row = row_containing_pos (w, PT - delta,
12044 first_unchanged_at_end_row, NULL, 0);
12045 if (row)
12046 set_cursor_from_row (w, row, w->current_matrix, delta,
12047 delta_bytes, dy, dvpos);
12050 /* Give up if cursor was not found. */
12051 if (w->cursor.vpos < 0)
12053 clear_glyph_matrix (w->desired_matrix);
12054 return -1;
12058 /* Don't let the cursor end in the scroll margins. */
12060 int this_scroll_margin, cursor_height;
12062 this_scroll_margin = max (0, scroll_margin);
12063 this_scroll_margin = min (this_scroll_margin,
12064 XFASTINT (w->height) / 4);
12065 this_scroll_margin *= CANON_Y_UNIT (it.f);
12066 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12068 if ((w->cursor.y < this_scroll_margin
12069 && CHARPOS (start) > BEGV)
12070 /* Don't take scroll margin into account at the bottom because
12071 old redisplay didn't do it either. */
12072 || w->cursor.y + cursor_height > it.last_visible_y)
12074 w->cursor.vpos = -1;
12075 clear_glyph_matrix (w->desired_matrix);
12076 return -1;
12080 /* Scroll the display. Do it before changing the current matrix so
12081 that xterm.c doesn't get confused about where the cursor glyph is
12082 found. */
12083 if (dy && run.height)
12085 update_begin (f);
12087 if (FRAME_WINDOW_P (f))
12089 rif->update_window_begin_hook (w);
12090 rif->clear_mouse_face (w);
12091 rif->scroll_run_hook (w, &run);
12092 rif->update_window_end_hook (w, 0, 0);
12094 else
12096 /* Terminal frame. In this case, dvpos gives the number of
12097 lines to scroll by; dvpos < 0 means scroll up. */
12098 int first_unchanged_at_end_vpos
12099 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12100 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
12101 int end = (XFASTINT (w->top)
12102 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12103 + window_internal_height (w));
12105 /* Perform the operation on the screen. */
12106 if (dvpos > 0)
12108 /* Scroll last_unchanged_at_beg_row to the end of the
12109 window down dvpos lines. */
12110 set_terminal_window (end);
12112 /* On dumb terminals delete dvpos lines at the end
12113 before inserting dvpos empty lines. */
12114 if (!scroll_region_ok)
12115 ins_del_lines (end - dvpos, -dvpos);
12117 /* Insert dvpos empty lines in front of
12118 last_unchanged_at_beg_row. */
12119 ins_del_lines (from, dvpos);
12121 else if (dvpos < 0)
12123 /* Scroll up last_unchanged_at_beg_vpos to the end of
12124 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12125 set_terminal_window (end);
12127 /* Delete dvpos lines in front of
12128 last_unchanged_at_beg_vpos. ins_del_lines will set
12129 the cursor to the given vpos and emit |dvpos| delete
12130 line sequences. */
12131 ins_del_lines (from + dvpos, dvpos);
12133 /* On a dumb terminal insert dvpos empty lines at the
12134 end. */
12135 if (!scroll_region_ok)
12136 ins_del_lines (end + dvpos, -dvpos);
12139 set_terminal_window (0);
12142 update_end (f);
12145 /* Shift reused rows of the current matrix to the right position.
12146 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12147 text. */
12148 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12149 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12150 if (dvpos < 0)
12152 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12153 bottom_vpos, dvpos);
12154 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12155 bottom_vpos, 0);
12157 else if (dvpos > 0)
12159 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12160 bottom_vpos, dvpos);
12161 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12162 first_unchanged_at_end_vpos + dvpos, 0);
12165 /* For frame-based redisplay, make sure that current frame and window
12166 matrix are in sync with respect to glyph memory. */
12167 if (!FRAME_WINDOW_P (f))
12168 sync_frame_with_window_matrix_rows (w);
12170 /* Adjust buffer positions in reused rows. */
12171 if (delta)
12172 increment_matrix_positions (current_matrix,
12173 first_unchanged_at_end_vpos + dvpos,
12174 bottom_vpos, delta, delta_bytes);
12176 /* Adjust Y positions. */
12177 if (dy)
12178 shift_glyph_matrix (w, current_matrix,
12179 first_unchanged_at_end_vpos + dvpos,
12180 bottom_vpos, dy);
12182 if (first_unchanged_at_end_row)
12183 first_unchanged_at_end_row += dvpos;
12185 /* If scrolling up, there may be some lines to display at the end of
12186 the window. */
12187 last_text_row_at_end = NULL;
12188 if (dy < 0)
12190 /* Scrolling up can leave for example a partially visible line
12191 at the end of the window to be redisplayed. */
12192 /* Set last_row to the glyph row in the current matrix where the
12193 window end line is found. It has been moved up or down in
12194 the matrix by dvpos. */
12195 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12196 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12198 /* If last_row is the window end line, it should display text. */
12199 xassert (last_row->displays_text_p);
12201 /* If window end line was partially visible before, begin
12202 displaying at that line. Otherwise begin displaying with the
12203 line following it. */
12204 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12206 init_to_row_start (&it, w, last_row);
12207 it.vpos = last_vpos;
12208 it.current_y = last_row->y;
12210 else
12212 init_to_row_end (&it, w, last_row);
12213 it.vpos = 1 + last_vpos;
12214 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12215 ++last_row;
12218 /* We may start in a continuation line. If so, we have to
12219 get the right continuation_lines_width and current_x. */
12220 it.continuation_lines_width = last_row->continuation_lines_width;
12221 it.hpos = it.current_x = 0;
12223 /* Display the rest of the lines at the window end. */
12224 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12225 while (it.current_y < it.last_visible_y
12226 && !fonts_changed_p)
12228 /* Is it always sure that the display agrees with lines in
12229 the current matrix? I don't think so, so we mark rows
12230 displayed invalid in the current matrix by setting their
12231 enabled_p flag to zero. */
12232 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12233 if (display_line (&it))
12234 last_text_row_at_end = it.glyph_row - 1;
12238 /* Update window_end_pos and window_end_vpos. */
12239 if (first_unchanged_at_end_row
12240 && first_unchanged_at_end_row->y < it.last_visible_y
12241 && !last_text_row_at_end)
12243 /* Window end line if one of the preserved rows from the current
12244 matrix. Set row to the last row displaying text in current
12245 matrix starting at first_unchanged_at_end_row, after
12246 scrolling. */
12247 xassert (first_unchanged_at_end_row->displays_text_p);
12248 row = find_last_row_displaying_text (w->current_matrix, &it,
12249 first_unchanged_at_end_row);
12250 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12252 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12253 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12254 w->window_end_vpos
12255 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12256 xassert (w->window_end_bytepos >= 0);
12257 IF_DEBUG (debug_method_add (w, "A"));
12259 else if (last_text_row_at_end)
12261 w->window_end_pos
12262 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12263 w->window_end_bytepos
12264 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12265 w->window_end_vpos
12266 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12267 xassert (w->window_end_bytepos >= 0);
12268 IF_DEBUG (debug_method_add (w, "B"));
12270 else if (last_text_row)
12272 /* We have displayed either to the end of the window or at the
12273 end of the window, i.e. the last row with text is to be found
12274 in the desired matrix. */
12275 w->window_end_pos
12276 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12277 w->window_end_bytepos
12278 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12279 w->window_end_vpos
12280 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12281 xassert (w->window_end_bytepos >= 0);
12283 else if (first_unchanged_at_end_row == NULL
12284 && last_text_row == NULL
12285 && last_text_row_at_end == NULL)
12287 /* Displayed to end of window, but no line containing text was
12288 displayed. Lines were deleted at the end of the window. */
12289 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12290 int vpos = XFASTINT (w->window_end_vpos);
12291 struct glyph_row *current_row = current_matrix->rows + vpos;
12292 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12294 for (row = NULL;
12295 row == NULL && vpos >= first_vpos;
12296 --vpos, --current_row, --desired_row)
12298 if (desired_row->enabled_p)
12300 if (desired_row->displays_text_p)
12301 row = desired_row;
12303 else if (current_row->displays_text_p)
12304 row = current_row;
12307 xassert (row != NULL);
12308 w->window_end_vpos = make_number (vpos + 1);
12309 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12310 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12311 xassert (w->window_end_bytepos >= 0);
12312 IF_DEBUG (debug_method_add (w, "C"));
12314 else
12315 abort ();
12317 #if 0 /* This leads to problems, for instance when the cursor is
12318 at ZV, and the cursor line displays no text. */
12319 /* Disable rows below what's displayed in the window. This makes
12320 debugging easier. */
12321 enable_glyph_matrix_rows (current_matrix,
12322 XFASTINT (w->window_end_vpos) + 1,
12323 bottom_vpos, 0);
12324 #endif
12326 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12327 debug_end_vpos = XFASTINT (w->window_end_vpos));
12329 /* Record that display has not been completed. */
12330 w->window_end_valid = Qnil;
12331 w->desired_matrix->no_scrolling_p = 1;
12332 return 3;
12334 #undef GIVE_UP
12339 /***********************************************************************
12340 More debugging support
12341 ***********************************************************************/
12343 #if GLYPH_DEBUG
12345 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12346 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12347 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12350 /* Dump the contents of glyph matrix MATRIX on stderr.
12352 GLYPHS 0 means don't show glyph contents.
12353 GLYPHS 1 means show glyphs in short form
12354 GLYPHS > 1 means show glyphs in long form. */
12356 void
12357 dump_glyph_matrix (matrix, glyphs)
12358 struct glyph_matrix *matrix;
12359 int glyphs;
12361 int i;
12362 for (i = 0; i < matrix->nrows; ++i)
12363 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12367 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12368 the glyph row and area where the glyph comes from. */
12370 void
12371 dump_glyph (row, glyph, area)
12372 struct glyph_row *row;
12373 struct glyph *glyph;
12374 int area;
12376 if (glyph->type == CHAR_GLYPH)
12378 fprintf (stderr,
12379 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12380 glyph - row->glyphs[TEXT_AREA],
12381 'C',
12382 glyph->charpos,
12383 (BUFFERP (glyph->object)
12384 ? 'B'
12385 : (STRINGP (glyph->object)
12386 ? 'S'
12387 : '-')),
12388 glyph->pixel_width,
12389 glyph->u.ch,
12390 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12391 ? glyph->u.ch
12392 : '.'),
12393 glyph->face_id,
12394 glyph->left_box_line_p,
12395 glyph->right_box_line_p);
12397 else if (glyph->type == STRETCH_GLYPH)
12399 fprintf (stderr,
12400 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12401 glyph - row->glyphs[TEXT_AREA],
12402 'S',
12403 glyph->charpos,
12404 (BUFFERP (glyph->object)
12405 ? 'B'
12406 : (STRINGP (glyph->object)
12407 ? 'S'
12408 : '-')),
12409 glyph->pixel_width,
12411 '.',
12412 glyph->face_id,
12413 glyph->left_box_line_p,
12414 glyph->right_box_line_p);
12416 else if (glyph->type == IMAGE_GLYPH)
12418 fprintf (stderr,
12419 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12420 glyph - row->glyphs[TEXT_AREA],
12421 'I',
12422 glyph->charpos,
12423 (BUFFERP (glyph->object)
12424 ? 'B'
12425 : (STRINGP (glyph->object)
12426 ? 'S'
12427 : '-')),
12428 glyph->pixel_width,
12429 glyph->u.img_id,
12430 '.',
12431 glyph->face_id,
12432 glyph->left_box_line_p,
12433 glyph->right_box_line_p);
12438 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12439 GLYPHS 0 means don't show glyph contents.
12440 GLYPHS 1 means show glyphs in short form
12441 GLYPHS > 1 means show glyphs in long form. */
12443 void
12444 dump_glyph_row (row, vpos, glyphs)
12445 struct glyph_row *row;
12446 int vpos, glyphs;
12448 if (glyphs != 1)
12450 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12451 fprintf (stderr, "=======================================================================\n");
12453 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12454 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12455 vpos,
12456 MATRIX_ROW_START_CHARPOS (row),
12457 MATRIX_ROW_END_CHARPOS (row),
12458 row->used[TEXT_AREA],
12459 row->contains_overlapping_glyphs_p,
12460 row->enabled_p,
12461 row->truncated_on_left_p,
12462 row->truncated_on_right_p,
12463 row->overlay_arrow_p,
12464 row->continued_p,
12465 MATRIX_ROW_CONTINUATION_LINE_P (row),
12466 row->displays_text_p,
12467 row->ends_at_zv_p,
12468 row->fill_line_p,
12469 row->ends_in_middle_of_char_p,
12470 row->starts_in_middle_of_char_p,
12471 row->mouse_face_p,
12472 row->x,
12473 row->y,
12474 row->pixel_width,
12475 row->height,
12476 row->visible_height,
12477 row->ascent,
12478 row->phys_ascent);
12479 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12480 row->end.overlay_string_index,
12481 row->continuation_lines_width);
12482 fprintf (stderr, "%9d %5d\n",
12483 CHARPOS (row->start.string_pos),
12484 CHARPOS (row->end.string_pos));
12485 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12486 row->end.dpvec_index);
12489 if (glyphs > 1)
12491 int area;
12493 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12495 struct glyph *glyph = row->glyphs[area];
12496 struct glyph *glyph_end = glyph + row->used[area];
12498 /* Glyph for a line end in text. */
12499 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12500 ++glyph_end;
12502 if (glyph < glyph_end)
12503 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12505 for (; glyph < glyph_end; ++glyph)
12506 dump_glyph (row, glyph, area);
12509 else if (glyphs == 1)
12511 int area;
12513 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12515 char *s = (char *) alloca (row->used[area] + 1);
12516 int i;
12518 for (i = 0; i < row->used[area]; ++i)
12520 struct glyph *glyph = row->glyphs[area] + i;
12521 if (glyph->type == CHAR_GLYPH
12522 && glyph->u.ch < 0x80
12523 && glyph->u.ch >= ' ')
12524 s[i] = glyph->u.ch;
12525 else
12526 s[i] = '.';
12529 s[i] = '\0';
12530 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12536 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12537 Sdump_glyph_matrix, 0, 1, "p",
12538 doc: /* Dump the current matrix of the selected window to stderr.
12539 Shows contents of glyph row structures. With non-nil
12540 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12541 glyphs in short form, otherwise show glyphs in long form. */)
12542 (glyphs)
12543 Lisp_Object glyphs;
12545 struct window *w = XWINDOW (selected_window);
12546 struct buffer *buffer = XBUFFER (w->buffer);
12548 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12549 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12550 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12551 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12552 fprintf (stderr, "=============================================\n");
12553 dump_glyph_matrix (w->current_matrix,
12554 NILP (glyphs) ? 0 : XINT (glyphs));
12555 return Qnil;
12559 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12560 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12563 struct frame *f = XFRAME (selected_frame);
12564 dump_glyph_matrix (f->current_matrix, 1);
12565 return Qnil;
12569 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12570 doc: /* Dump glyph row ROW to stderr.
12571 GLYPH 0 means don't dump glyphs.
12572 GLYPH 1 means dump glyphs in short form.
12573 GLYPH > 1 or omitted means dump glyphs in long form. */)
12574 (row, glyphs)
12575 Lisp_Object row, glyphs;
12577 struct glyph_matrix *matrix;
12578 int vpos;
12580 CHECK_NUMBER (row);
12581 matrix = XWINDOW (selected_window)->current_matrix;
12582 vpos = XINT (row);
12583 if (vpos >= 0 && vpos < matrix->nrows)
12584 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12585 vpos,
12586 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12587 return Qnil;
12591 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12592 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12593 GLYPH 0 means don't dump glyphs.
12594 GLYPH 1 means dump glyphs in short form.
12595 GLYPH > 1 or omitted means dump glyphs in long form. */)
12596 (row, glyphs)
12597 Lisp_Object row, glyphs;
12599 struct frame *sf = SELECTED_FRAME ();
12600 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12601 int vpos;
12603 CHECK_NUMBER (row);
12604 vpos = XINT (row);
12605 if (vpos >= 0 && vpos < m->nrows)
12606 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12607 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12608 return Qnil;
12612 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12613 doc: /* Toggle tracing of redisplay.
12614 With ARG, turn tracing on if and only if ARG is positive. */)
12615 (arg)
12616 Lisp_Object arg;
12618 if (NILP (arg))
12619 trace_redisplay_p = !trace_redisplay_p;
12620 else
12622 arg = Fprefix_numeric_value (arg);
12623 trace_redisplay_p = XINT (arg) > 0;
12626 return Qnil;
12630 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12631 doc: /* Like `format', but print result to stderr.
12632 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12633 (nargs, args)
12634 int nargs;
12635 Lisp_Object *args;
12637 Lisp_Object s = Fformat (nargs, args);
12638 fprintf (stderr, "%s", SDATA (s));
12639 return Qnil;
12642 #endif /* GLYPH_DEBUG */
12646 /***********************************************************************
12647 Building Desired Matrix Rows
12648 ***********************************************************************/
12650 /* Return a temporary glyph row holding the glyphs of an overlay
12651 arrow. Only used for non-window-redisplay windows. */
12653 static struct glyph_row *
12654 get_overlay_arrow_glyph_row (w)
12655 struct window *w;
12657 struct frame *f = XFRAME (WINDOW_FRAME (w));
12658 struct buffer *buffer = XBUFFER (w->buffer);
12659 struct buffer *old = current_buffer;
12660 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12661 int arrow_len = SCHARS (Voverlay_arrow_string);
12662 const unsigned char *arrow_end = arrow_string + arrow_len;
12663 const unsigned char *p;
12664 struct it it;
12665 int multibyte_p;
12666 int n_glyphs_before;
12668 set_buffer_temp (buffer);
12669 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12670 it.glyph_row->used[TEXT_AREA] = 0;
12671 SET_TEXT_POS (it.position, 0, 0);
12673 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12674 p = arrow_string;
12675 while (p < arrow_end)
12677 Lisp_Object face, ilisp;
12679 /* Get the next character. */
12680 if (multibyte_p)
12681 it.c = string_char_and_length (p, arrow_len, &it.len);
12682 else
12683 it.c = *p, it.len = 1;
12684 p += it.len;
12686 /* Get its face. */
12687 ilisp = make_number (p - arrow_string);
12688 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12689 it.face_id = compute_char_face (f, it.c, face);
12691 /* Compute its width, get its glyphs. */
12692 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12693 SET_TEXT_POS (it.position, -1, -1);
12694 PRODUCE_GLYPHS (&it);
12696 /* If this character doesn't fit any more in the line, we have
12697 to remove some glyphs. */
12698 if (it.current_x > it.last_visible_x)
12700 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12701 break;
12705 set_buffer_temp (old);
12706 return it.glyph_row;
12710 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12711 glyphs are only inserted for terminal frames since we can't really
12712 win with truncation glyphs when partially visible glyphs are
12713 involved. Which glyphs to insert is determined by
12714 produce_special_glyphs. */
12716 static void
12717 insert_left_trunc_glyphs (it)
12718 struct it *it;
12720 struct it truncate_it;
12721 struct glyph *from, *end, *to, *toend;
12723 xassert (!FRAME_WINDOW_P (it->f));
12725 /* Get the truncation glyphs. */
12726 truncate_it = *it;
12727 truncate_it.current_x = 0;
12728 truncate_it.face_id = DEFAULT_FACE_ID;
12729 truncate_it.glyph_row = &scratch_glyph_row;
12730 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12731 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12732 truncate_it.object = make_number (0);
12733 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12735 /* Overwrite glyphs from IT with truncation glyphs. */
12736 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12737 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12738 to = it->glyph_row->glyphs[TEXT_AREA];
12739 toend = to + it->glyph_row->used[TEXT_AREA];
12741 while (from < end)
12742 *to++ = *from++;
12744 /* There may be padding glyphs left over. Overwrite them too. */
12745 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12747 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12748 while (from < end)
12749 *to++ = *from++;
12752 if (to > toend)
12753 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12757 /* Compute the pixel height and width of IT->glyph_row.
12759 Most of the time, ascent and height of a display line will be equal
12760 to the max_ascent and max_height values of the display iterator
12761 structure. This is not the case if
12763 1. We hit ZV without displaying anything. In this case, max_ascent
12764 and max_height will be zero.
12766 2. We have some glyphs that don't contribute to the line height.
12767 (The glyph row flag contributes_to_line_height_p is for future
12768 pixmap extensions).
12770 The first case is easily covered by using default values because in
12771 these cases, the line height does not really matter, except that it
12772 must not be zero. */
12774 static void
12775 compute_line_metrics (it)
12776 struct it *it;
12778 struct glyph_row *row = it->glyph_row;
12779 int area, i;
12781 if (FRAME_WINDOW_P (it->f))
12783 int i, min_y, max_y;
12785 /* The line may consist of one space only, that was added to
12786 place the cursor on it. If so, the row's height hasn't been
12787 computed yet. */
12788 if (row->height == 0)
12790 if (it->max_ascent + it->max_descent == 0)
12791 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12792 row->ascent = it->max_ascent;
12793 row->height = it->max_ascent + it->max_descent;
12794 row->phys_ascent = it->max_phys_ascent;
12795 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12798 /* Compute the width of this line. */
12799 row->pixel_width = row->x;
12800 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12801 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12803 xassert (row->pixel_width >= 0);
12804 xassert (row->ascent >= 0 && row->height > 0);
12806 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12807 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12809 /* If first line's physical ascent is larger than its logical
12810 ascent, use the physical ascent, and make the row taller.
12811 This makes accented characters fully visible. */
12812 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12813 && row->phys_ascent > row->ascent)
12815 row->height += row->phys_ascent - row->ascent;
12816 row->ascent = row->phys_ascent;
12819 /* Compute how much of the line is visible. */
12820 row->visible_height = row->height;
12822 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12823 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12825 if (row->y < min_y)
12826 row->visible_height -= min_y - row->y;
12827 if (row->y + row->height > max_y)
12828 row->visible_height -= row->y + row->height - max_y;
12830 else
12832 row->pixel_width = row->used[TEXT_AREA];
12833 if (row->continued_p)
12834 row->pixel_width -= it->continuation_pixel_width;
12835 else if (row->truncated_on_right_p)
12836 row->pixel_width -= it->truncation_pixel_width;
12837 row->ascent = row->phys_ascent = 0;
12838 row->height = row->phys_height = row->visible_height = 1;
12841 /* Compute a hash code for this row. */
12842 row->hash = 0;
12843 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12844 for (i = 0; i < row->used[area]; ++i)
12845 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12846 + row->glyphs[area][i].u.val
12847 + row->glyphs[area][i].face_id
12848 + row->glyphs[area][i].padding_p
12849 + (row->glyphs[area][i].type << 2));
12851 it->max_ascent = it->max_descent = 0;
12852 it->max_phys_ascent = it->max_phys_descent = 0;
12856 /* Append one space to the glyph row of iterator IT if doing a
12857 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12858 space have the default face, otherwise let it have the same face as
12859 IT->face_id. Value is non-zero if a space was added.
12861 This function is called to make sure that there is always one glyph
12862 at the end of a glyph row that the cursor can be set on under
12863 window-systems. (If there weren't such a glyph we would not know
12864 how wide and tall a box cursor should be displayed).
12866 At the same time this space let's a nicely handle clearing to the
12867 end of the line if the row ends in italic text. */
12869 static int
12870 append_space (it, default_face_p)
12871 struct it *it;
12872 int default_face_p;
12874 if (FRAME_WINDOW_P (it->f))
12876 int n = it->glyph_row->used[TEXT_AREA];
12878 if (it->glyph_row->glyphs[TEXT_AREA] + n
12879 < it->glyph_row->glyphs[1 + TEXT_AREA])
12881 /* Save some values that must not be changed.
12882 Must save IT->c and IT->len because otherwise
12883 ITERATOR_AT_END_P wouldn't work anymore after
12884 append_space has been called. */
12885 enum display_element_type saved_what = it->what;
12886 int saved_c = it->c, saved_len = it->len;
12887 int saved_x = it->current_x;
12888 int saved_face_id = it->face_id;
12889 struct text_pos saved_pos;
12890 Lisp_Object saved_object;
12891 struct face *face;
12893 saved_object = it->object;
12894 saved_pos = it->position;
12896 it->what = IT_CHARACTER;
12897 bzero (&it->position, sizeof it->position);
12898 it->object = make_number (0);
12899 it->c = ' ';
12900 it->len = 1;
12902 if (default_face_p)
12903 it->face_id = DEFAULT_FACE_ID;
12904 else if (it->face_before_selective_p)
12905 it->face_id = it->saved_face_id;
12906 face = FACE_FROM_ID (it->f, it->face_id);
12907 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12909 PRODUCE_GLYPHS (it);
12911 it->current_x = saved_x;
12912 it->object = saved_object;
12913 it->position = saved_pos;
12914 it->what = saved_what;
12915 it->face_id = saved_face_id;
12916 it->len = saved_len;
12917 it->c = saved_c;
12918 return 1;
12922 return 0;
12926 /* Extend the face of the last glyph in the text area of IT->glyph_row
12927 to the end of the display line. Called from display_line.
12928 If the glyph row is empty, add a space glyph to it so that we
12929 know the face to draw. Set the glyph row flag fill_line_p. */
12931 static void
12932 extend_face_to_end_of_line (it)
12933 struct it *it;
12935 struct face *face;
12936 struct frame *f = it->f;
12938 /* If line is already filled, do nothing. */
12939 if (it->current_x >= it->last_visible_x)
12940 return;
12942 /* Face extension extends the background and box of IT->face_id
12943 to the end of the line. If the background equals the background
12944 of the frame, we don't have to do anything. */
12945 if (it->face_before_selective_p)
12946 face = FACE_FROM_ID (it->f, it->saved_face_id);
12947 else
12948 face = FACE_FROM_ID (f, it->face_id);
12950 if (FRAME_WINDOW_P (f)
12951 && face->box == FACE_NO_BOX
12952 && face->background == FRAME_BACKGROUND_PIXEL (f)
12953 && !face->stipple)
12954 return;
12956 /* Set the glyph row flag indicating that the face of the last glyph
12957 in the text area has to be drawn to the end of the text area. */
12958 it->glyph_row->fill_line_p = 1;
12960 /* If current character of IT is not ASCII, make sure we have the
12961 ASCII face. This will be automatically undone the next time
12962 get_next_display_element returns a multibyte character. Note
12963 that the character will always be single byte in unibyte text. */
12964 if (!SINGLE_BYTE_CHAR_P (it->c))
12966 it->face_id = FACE_FOR_CHAR (f, face, 0);
12969 if (FRAME_WINDOW_P (f))
12971 /* If the row is empty, add a space with the current face of IT,
12972 so that we know which face to draw. */
12973 if (it->glyph_row->used[TEXT_AREA] == 0)
12975 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12976 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12977 it->glyph_row->used[TEXT_AREA] = 1;
12980 else
12982 /* Save some values that must not be changed. */
12983 int saved_x = it->current_x;
12984 struct text_pos saved_pos;
12985 Lisp_Object saved_object;
12986 enum display_element_type saved_what = it->what;
12987 int saved_face_id = it->face_id;
12989 saved_object = it->object;
12990 saved_pos = it->position;
12992 it->what = IT_CHARACTER;
12993 bzero (&it->position, sizeof it->position);
12994 it->object = make_number (0);
12995 it->c = ' ';
12996 it->len = 1;
12997 it->face_id = face->id;
12999 PRODUCE_GLYPHS (it);
13001 while (it->current_x <= it->last_visible_x)
13002 PRODUCE_GLYPHS (it);
13004 /* Don't count these blanks really. It would let us insert a left
13005 truncation glyph below and make us set the cursor on them, maybe. */
13006 it->current_x = saved_x;
13007 it->object = saved_object;
13008 it->position = saved_pos;
13009 it->what = saved_what;
13010 it->face_id = saved_face_id;
13015 /* Value is non-zero if text starting at CHARPOS in current_buffer is
13016 trailing whitespace. */
13018 static int
13019 trailing_whitespace_p (charpos)
13020 int charpos;
13022 int bytepos = CHAR_TO_BYTE (charpos);
13023 int c = 0;
13025 while (bytepos < ZV_BYTE
13026 && (c = FETCH_CHAR (bytepos),
13027 c == ' ' || c == '\t'))
13028 ++bytepos;
13030 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
13032 if (bytepos != PT_BYTE)
13033 return 1;
13035 return 0;
13039 /* Highlight trailing whitespace, if any, in ROW. */
13041 void
13042 highlight_trailing_whitespace (f, row)
13043 struct frame *f;
13044 struct glyph_row *row;
13046 int used = row->used[TEXT_AREA];
13048 if (used)
13050 struct glyph *start = row->glyphs[TEXT_AREA];
13051 struct glyph *glyph = start + used - 1;
13053 /* Skip over glyphs inserted to display the cursor at the
13054 end of a line, for extending the face of the last glyph
13055 to the end of the line on terminals, and for truncation
13056 and continuation glyphs. */
13057 while (glyph >= start
13058 && glyph->type == CHAR_GLYPH
13059 && INTEGERP (glyph->object))
13060 --glyph;
13062 /* If last glyph is a space or stretch, and it's trailing
13063 whitespace, set the face of all trailing whitespace glyphs in
13064 IT->glyph_row to `trailing-whitespace'. */
13065 if (glyph >= start
13066 && BUFFERP (glyph->object)
13067 && (glyph->type == STRETCH_GLYPH
13068 || (glyph->type == CHAR_GLYPH
13069 && glyph->u.ch == ' '))
13070 && trailing_whitespace_p (glyph->charpos))
13072 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
13074 while (glyph >= start
13075 && BUFFERP (glyph->object)
13076 && (glyph->type == STRETCH_GLYPH
13077 || (glyph->type == CHAR_GLYPH
13078 && glyph->u.ch == ' ')))
13079 (glyph--)->face_id = face_id;
13085 /* Value is non-zero if glyph row ROW in window W should be
13086 used to hold the cursor. */
13088 static int
13089 cursor_row_p (w, row)
13090 struct window *w;
13091 struct glyph_row *row;
13093 int cursor_row_p = 1;
13095 if (PT == MATRIX_ROW_END_CHARPOS (row))
13097 /* If the row ends with a newline from a string, we don't want
13098 the cursor there (if the row is continued it doesn't end in a
13099 newline). */
13100 if (CHARPOS (row->end.string_pos) >= 0
13101 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13102 cursor_row_p = row->continued_p;
13104 /* If the row ends at ZV, display the cursor at the end of that
13105 row instead of at the start of the row below. */
13106 else if (row->ends_at_zv_p)
13107 cursor_row_p = 1;
13108 else
13109 cursor_row_p = 0;
13112 return cursor_row_p;
13116 /* Construct the glyph row IT->glyph_row in the desired matrix of
13117 IT->w from text at the current position of IT. See dispextern.h
13118 for an overview of struct it. Value is non-zero if
13119 IT->glyph_row displays text, as opposed to a line displaying ZV
13120 only. */
13122 static int
13123 display_line (it)
13124 struct it *it;
13126 struct glyph_row *row = it->glyph_row;
13128 /* We always start displaying at hpos zero even if hscrolled. */
13129 xassert (it->hpos == 0 && it->current_x == 0);
13131 /* We must not display in a row that's not a text row. */
13132 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13133 < it->w->desired_matrix->nrows);
13135 /* Is IT->w showing the region? */
13136 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13138 /* Clear the result glyph row and enable it. */
13139 prepare_desired_row (row);
13141 row->y = it->current_y;
13142 row->start = it->current;
13143 row->continuation_lines_width = it->continuation_lines_width;
13144 row->displays_text_p = 1;
13145 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13146 it->starts_in_middle_of_char_p = 0;
13148 /* Arrange the overlays nicely for our purposes. Usually, we call
13149 display_line on only one line at a time, in which case this
13150 can't really hurt too much, or we call it on lines which appear
13151 one after another in the buffer, in which case all calls to
13152 recenter_overlay_lists but the first will be pretty cheap. */
13153 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13155 /* Move over display elements that are not visible because we are
13156 hscrolled. This may stop at an x-position < IT->first_visible_x
13157 if the first glyph is partially visible or if we hit a line end. */
13158 if (it->current_x < it->first_visible_x)
13159 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13160 MOVE_TO_POS | MOVE_TO_X);
13162 /* Get the initial row height. This is either the height of the
13163 text hscrolled, if there is any, or zero. */
13164 row->ascent = it->max_ascent;
13165 row->height = it->max_ascent + it->max_descent;
13166 row->phys_ascent = it->max_phys_ascent;
13167 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13169 /* Loop generating characters. The loop is left with IT on the next
13170 character to display. */
13171 while (1)
13173 int n_glyphs_before, hpos_before, x_before;
13174 int x, i, nglyphs;
13175 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13177 /* Retrieve the next thing to display. Value is zero if end of
13178 buffer reached. */
13179 if (!get_next_display_element (it))
13181 /* Maybe add a space at the end of this line that is used to
13182 display the cursor there under X. Set the charpos of the
13183 first glyph of blank lines not corresponding to any text
13184 to -1. */
13185 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13186 || row->used[TEXT_AREA] == 0)
13188 row->glyphs[TEXT_AREA]->charpos = -1;
13189 row->displays_text_p = 0;
13191 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13192 && (!MINI_WINDOW_P (it->w)
13193 || (minibuf_level && EQ (it->window, minibuf_window))))
13194 row->indicate_empty_line_p = 1;
13197 it->continuation_lines_width = 0;
13198 row->ends_at_zv_p = 1;
13199 break;
13202 /* Now, get the metrics of what we want to display. This also
13203 generates glyphs in `row' (which is IT->glyph_row). */
13204 n_glyphs_before = row->used[TEXT_AREA];
13205 x = it->current_x;
13207 /* Remember the line height so far in case the next element doesn't
13208 fit on the line. */
13209 if (!it->truncate_lines_p)
13211 ascent = it->max_ascent;
13212 descent = it->max_descent;
13213 phys_ascent = it->max_phys_ascent;
13214 phys_descent = it->max_phys_descent;
13217 PRODUCE_GLYPHS (it);
13219 /* If this display element was in marginal areas, continue with
13220 the next one. */
13221 if (it->area != TEXT_AREA)
13223 row->ascent = max (row->ascent, it->max_ascent);
13224 row->height = max (row->height, it->max_ascent + it->max_descent);
13225 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13226 row->phys_height = max (row->phys_height,
13227 it->max_phys_ascent + it->max_phys_descent);
13228 set_iterator_to_next (it, 1);
13229 continue;
13232 /* Does the display element fit on the line? If we truncate
13233 lines, we should draw past the right edge of the window. If
13234 we don't truncate, we want to stop so that we can display the
13235 continuation glyph before the right margin. If lines are
13236 continued, there are two possible strategies for characters
13237 resulting in more than 1 glyph (e.g. tabs): Display as many
13238 glyphs as possible in this line and leave the rest for the
13239 continuation line, or display the whole element in the next
13240 line. Original redisplay did the former, so we do it also. */
13241 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13242 hpos_before = it->hpos;
13243 x_before = x;
13245 if (/* Not a newline. */
13246 nglyphs > 0
13247 /* Glyphs produced fit entirely in the line. */
13248 && it->current_x < it->last_visible_x)
13250 it->hpos += nglyphs;
13251 row->ascent = max (row->ascent, it->max_ascent);
13252 row->height = max (row->height, it->max_ascent + it->max_descent);
13253 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13254 row->phys_height = max (row->phys_height,
13255 it->max_phys_ascent + it->max_phys_descent);
13256 if (it->current_x - it->pixel_width < it->first_visible_x)
13257 row->x = x - it->first_visible_x;
13259 else
13261 int new_x;
13262 struct glyph *glyph;
13264 for (i = 0; i < nglyphs; ++i, x = new_x)
13266 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13267 new_x = x + glyph->pixel_width;
13269 if (/* Lines are continued. */
13270 !it->truncate_lines_p
13271 && (/* Glyph doesn't fit on the line. */
13272 new_x > it->last_visible_x
13273 /* Or it fits exactly on a window system frame. */
13274 || (new_x == it->last_visible_x
13275 && FRAME_WINDOW_P (it->f))))
13277 /* End of a continued line. */
13279 if (it->hpos == 0
13280 || (new_x == it->last_visible_x
13281 && FRAME_WINDOW_P (it->f)))
13283 /* Current glyph is the only one on the line or
13284 fits exactly on the line. We must continue
13285 the line because we can't draw the cursor
13286 after the glyph. */
13287 row->continued_p = 1;
13288 it->current_x = new_x;
13289 it->continuation_lines_width += new_x;
13290 ++it->hpos;
13291 if (i == nglyphs - 1)
13292 set_iterator_to_next (it, 1);
13294 else if (CHAR_GLYPH_PADDING_P (*glyph)
13295 && !FRAME_WINDOW_P (it->f))
13297 /* A padding glyph that doesn't fit on this line.
13298 This means the whole character doesn't fit
13299 on the line. */
13300 row->used[TEXT_AREA] = n_glyphs_before;
13302 /* Fill the rest of the row with continuation
13303 glyphs like in 20.x. */
13304 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13305 < row->glyphs[1 + TEXT_AREA])
13306 produce_special_glyphs (it, IT_CONTINUATION);
13308 row->continued_p = 1;
13309 it->current_x = x_before;
13310 it->continuation_lines_width += x_before;
13312 /* Restore the height to what it was before the
13313 element not fitting on the line. */
13314 it->max_ascent = ascent;
13315 it->max_descent = descent;
13316 it->max_phys_ascent = phys_ascent;
13317 it->max_phys_descent = phys_descent;
13319 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13321 /* A TAB that extends past the right edge of the
13322 window. This produces a single glyph on
13323 window system frames. We leave the glyph in
13324 this row and let it fill the row, but don't
13325 consume the TAB. */
13326 it->continuation_lines_width += it->last_visible_x;
13327 row->ends_in_middle_of_char_p = 1;
13328 row->continued_p = 1;
13329 glyph->pixel_width = it->last_visible_x - x;
13330 it->starts_in_middle_of_char_p = 1;
13332 else
13334 /* Something other than a TAB that draws past
13335 the right edge of the window. Restore
13336 positions to values before the element. */
13337 row->used[TEXT_AREA] = n_glyphs_before + i;
13339 /* Display continuation glyphs. */
13340 if (!FRAME_WINDOW_P (it->f))
13341 produce_special_glyphs (it, IT_CONTINUATION);
13342 row->continued_p = 1;
13344 it->continuation_lines_width += x;
13346 if (nglyphs > 1 && i > 0)
13348 row->ends_in_middle_of_char_p = 1;
13349 it->starts_in_middle_of_char_p = 1;
13352 /* Restore the height to what it was before the
13353 element not fitting on the line. */
13354 it->max_ascent = ascent;
13355 it->max_descent = descent;
13356 it->max_phys_ascent = phys_ascent;
13357 it->max_phys_descent = phys_descent;
13360 break;
13362 else if (new_x > it->first_visible_x)
13364 /* Increment number of glyphs actually displayed. */
13365 ++it->hpos;
13367 if (x < it->first_visible_x)
13368 /* Glyph is partially visible, i.e. row starts at
13369 negative X position. */
13370 row->x = x - it->first_visible_x;
13372 else
13374 /* Glyph is completely off the left margin of the
13375 window. This should not happen because of the
13376 move_it_in_display_line at the start of this
13377 function, unless the text display area of the
13378 window is empty. */
13379 xassert (it->first_visible_x <= it->last_visible_x);
13383 row->ascent = max (row->ascent, it->max_ascent);
13384 row->height = max (row->height, it->max_ascent + it->max_descent);
13385 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13386 row->phys_height = max (row->phys_height,
13387 it->max_phys_ascent + it->max_phys_descent);
13389 /* End of this display line if row is continued. */
13390 if (row->continued_p)
13391 break;
13394 /* Is this a line end? If yes, we're also done, after making
13395 sure that a non-default face is extended up to the right
13396 margin of the window. */
13397 if (ITERATOR_AT_END_OF_LINE_P (it))
13399 int used_before = row->used[TEXT_AREA];
13401 row->ends_in_newline_from_string_p = STRINGP (it->object);
13403 /* Add a space at the end of the line that is used to
13404 display the cursor there. */
13405 append_space (it, 0);
13407 /* Extend the face to the end of the line. */
13408 extend_face_to_end_of_line (it);
13410 /* Make sure we have the position. */
13411 if (used_before == 0)
13412 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13414 /* Consume the line end. This skips over invisible lines. */
13415 set_iterator_to_next (it, 1);
13416 it->continuation_lines_width = 0;
13417 break;
13420 /* Proceed with next display element. Note that this skips
13421 over lines invisible because of selective display. */
13422 set_iterator_to_next (it, 1);
13424 /* If we truncate lines, we are done when the last displayed
13425 glyphs reach past the right margin of the window. */
13426 if (it->truncate_lines_p
13427 && (FRAME_WINDOW_P (it->f)
13428 ? (it->current_x >= it->last_visible_x)
13429 : (it->current_x > it->last_visible_x)))
13431 /* Maybe add truncation glyphs. */
13432 if (!FRAME_WINDOW_P (it->f))
13434 int i, n;
13436 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13437 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13438 break;
13440 for (n = row->used[TEXT_AREA]; i < n; ++i)
13442 row->used[TEXT_AREA] = i;
13443 produce_special_glyphs (it, IT_TRUNCATION);
13447 row->truncated_on_right_p = 1;
13448 it->continuation_lines_width = 0;
13449 reseat_at_next_visible_line_start (it, 0);
13450 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13451 it->hpos = hpos_before;
13452 it->current_x = x_before;
13453 break;
13457 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13458 at the left window margin. */
13459 if (it->first_visible_x
13460 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13462 if (!FRAME_WINDOW_P (it->f))
13463 insert_left_trunc_glyphs (it);
13464 row->truncated_on_left_p = 1;
13467 /* If the start of this line is the overlay arrow-position, then
13468 mark this glyph row as the one containing the overlay arrow.
13469 This is clearly a mess with variable size fonts. It would be
13470 better to let it be displayed like cursors under X. */
13471 if (MARKERP (Voverlay_arrow_position)
13472 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13473 && (MATRIX_ROW_START_CHARPOS (row)
13474 == marker_position (Voverlay_arrow_position))
13475 && STRINGP (Voverlay_arrow_string)
13476 && ! overlay_arrow_seen)
13478 /* Overlay arrow in window redisplay is a fringe bitmap. */
13479 if (!FRAME_WINDOW_P (it->f))
13481 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13482 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13483 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13484 struct glyph *p = row->glyphs[TEXT_AREA];
13485 struct glyph *p2, *end;
13487 /* Copy the arrow glyphs. */
13488 while (glyph < arrow_end)
13489 *p++ = *glyph++;
13491 /* Throw away padding glyphs. */
13492 p2 = p;
13493 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13494 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13495 ++p2;
13496 if (p2 > p)
13498 while (p2 < end)
13499 *p++ = *p2++;
13500 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13504 overlay_arrow_seen = 1;
13505 row->overlay_arrow_p = 1;
13508 /* Compute pixel dimensions of this line. */
13509 compute_line_metrics (it);
13511 /* Remember the position at which this line ends. */
13512 row->end = it->current;
13514 /* Maybe set the cursor. */
13515 if (it->w->cursor.vpos < 0
13516 && PT >= MATRIX_ROW_START_CHARPOS (row)
13517 && PT <= MATRIX_ROW_END_CHARPOS (row)
13518 && cursor_row_p (it->w, row))
13519 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13521 /* Highlight trailing whitespace. */
13522 if (!NILP (Vshow_trailing_whitespace))
13523 highlight_trailing_whitespace (it->f, it->glyph_row);
13525 /* Prepare for the next line. This line starts horizontally at (X
13526 HPOS) = (0 0). Vertical positions are incremented. As a
13527 convenience for the caller, IT->glyph_row is set to the next
13528 row to be used. */
13529 it->current_x = it->hpos = 0;
13530 it->current_y += row->height;
13531 ++it->vpos;
13532 ++it->glyph_row;
13533 return row->displays_text_p;
13538 /***********************************************************************
13539 Menu Bar
13540 ***********************************************************************/
13542 /* Redisplay the menu bar in the frame for window W.
13544 The menu bar of X frames that don't have X toolkit support is
13545 displayed in a special window W->frame->menu_bar_window.
13547 The menu bar of terminal frames is treated specially as far as
13548 glyph matrices are concerned. Menu bar lines are not part of
13549 windows, so the update is done directly on the frame matrix rows
13550 for the menu bar. */
13552 static void
13553 display_menu_bar (w)
13554 struct window *w;
13556 struct frame *f = XFRAME (WINDOW_FRAME (w));
13557 struct it it;
13558 Lisp_Object items;
13559 int i;
13561 /* Don't do all this for graphical frames. */
13562 #ifdef HAVE_NTGUI
13563 if (!NILP (Vwindow_system))
13564 return;
13565 #endif
13566 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
13567 if (FRAME_X_P (f))
13568 return;
13569 #endif
13570 #ifdef MAC_OS
13571 if (FRAME_MAC_P (f))
13572 return;
13573 #endif
13575 #ifdef USE_X_TOOLKIT
13576 xassert (!FRAME_WINDOW_P (f));
13577 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13578 it.first_visible_x = 0;
13579 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13580 #else /* not USE_X_TOOLKIT */
13581 if (FRAME_WINDOW_P (f))
13583 /* Menu bar lines are displayed in the desired matrix of the
13584 dummy window menu_bar_window. */
13585 struct window *menu_w;
13586 xassert (WINDOWP (f->menu_bar_window));
13587 menu_w = XWINDOW (f->menu_bar_window);
13588 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13589 MENU_FACE_ID);
13590 it.first_visible_x = 0;
13591 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13593 else
13595 /* This is a TTY frame, i.e. character hpos/vpos are used as
13596 pixel x/y. */
13597 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13598 MENU_FACE_ID);
13599 it.first_visible_x = 0;
13600 it.last_visible_x = FRAME_WIDTH (f);
13602 #endif /* not USE_X_TOOLKIT */
13604 if (! mode_line_inverse_video)
13605 /* Force the menu-bar to be displayed in the default face. */
13606 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13608 /* Clear all rows of the menu bar. */
13609 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13611 struct glyph_row *row = it.glyph_row + i;
13612 clear_glyph_row (row);
13613 row->enabled_p = 1;
13614 row->full_width_p = 1;
13617 /* Display all items of the menu bar. */
13618 items = FRAME_MENU_BAR_ITEMS (it.f);
13619 for (i = 0; i < XVECTOR (items)->size; i += 4)
13621 Lisp_Object string;
13623 /* Stop at nil string. */
13624 string = AREF (items, i + 1);
13625 if (NILP (string))
13626 break;
13628 /* Remember where item was displayed. */
13629 AREF (items, i + 3) = make_number (it.hpos);
13631 /* Display the item, pad with one space. */
13632 if (it.current_x < it.last_visible_x)
13633 display_string (NULL, string, Qnil, 0, 0, &it,
13634 SCHARS (string) + 1, 0, 0, -1);
13637 /* Fill out the line with spaces. */
13638 if (it.current_x < it.last_visible_x)
13639 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13641 /* Compute the total height of the lines. */
13642 compute_line_metrics (&it);
13647 /***********************************************************************
13648 Mode Line
13649 ***********************************************************************/
13651 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13652 FORCE is non-zero, redisplay mode lines unconditionally.
13653 Otherwise, redisplay only mode lines that are garbaged. Value is
13654 the number of windows whose mode lines were redisplayed. */
13656 static int
13657 redisplay_mode_lines (window, force)
13658 Lisp_Object window;
13659 int force;
13661 int nwindows = 0;
13663 while (!NILP (window))
13665 struct window *w = XWINDOW (window);
13667 if (WINDOWP (w->hchild))
13668 nwindows += redisplay_mode_lines (w->hchild, force);
13669 else if (WINDOWP (w->vchild))
13670 nwindows += redisplay_mode_lines (w->vchild, force);
13671 else if (force
13672 || FRAME_GARBAGED_P (XFRAME (w->frame))
13673 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13675 struct text_pos lpoint;
13676 struct buffer *old = current_buffer;
13678 /* Set the window's buffer for the mode line display. */
13679 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13680 set_buffer_internal_1 (XBUFFER (w->buffer));
13682 /* Point refers normally to the selected window. For any
13683 other window, set up appropriate value. */
13684 if (!EQ (window, selected_window))
13686 struct text_pos pt;
13688 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13689 if (CHARPOS (pt) < BEGV)
13690 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13691 else if (CHARPOS (pt) > (ZV - 1))
13692 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13693 else
13694 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13697 /* Display mode lines. */
13698 clear_glyph_matrix (w->desired_matrix);
13699 if (display_mode_lines (w))
13701 ++nwindows;
13702 w->must_be_updated_p = 1;
13705 /* Restore old settings. */
13706 set_buffer_internal_1 (old);
13707 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13710 window = w->next;
13713 return nwindows;
13717 /* Display the mode and/or top line of window W. Value is the number
13718 of mode lines displayed. */
13720 static int
13721 display_mode_lines (w)
13722 struct window *w;
13724 Lisp_Object old_selected_window, old_selected_frame;
13725 int n = 0;
13727 old_selected_frame = selected_frame;
13728 selected_frame = w->frame;
13729 old_selected_window = selected_window;
13730 XSETWINDOW (selected_window, w);
13732 /* These will be set while the mode line specs are processed. */
13733 line_number_displayed = 0;
13734 w->column_number_displayed = Qnil;
13736 if (WINDOW_WANTS_MODELINE_P (w))
13738 struct window *sel_w = XWINDOW (old_selected_window);
13740 /* Select mode line face based on the real selected window. */
13741 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13742 current_buffer->mode_line_format);
13743 ++n;
13746 if (WINDOW_WANTS_HEADER_LINE_P (w))
13748 display_mode_line (w, HEADER_LINE_FACE_ID,
13749 current_buffer->header_line_format);
13750 ++n;
13753 selected_frame = old_selected_frame;
13754 selected_window = old_selected_window;
13755 return n;
13759 /* Display mode or top line of window W. FACE_ID specifies which line
13760 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13761 FORMAT is the mode line format to display. Value is the pixel
13762 height of the mode line displayed. */
13764 static int
13765 display_mode_line (w, face_id, format)
13766 struct window *w;
13767 enum face_id face_id;
13768 Lisp_Object format;
13770 struct it it;
13771 struct face *face;
13773 init_iterator (&it, w, -1, -1, NULL, face_id);
13774 prepare_desired_row (it.glyph_row);
13776 if (! mode_line_inverse_video)
13777 /* Force the mode-line to be displayed in the default face. */
13778 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13780 /* Temporarily make frame's keyboard the current kboard so that
13781 kboard-local variables in the mode_line_format will get the right
13782 values. */
13783 push_frame_kboard (it.f);
13784 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13785 pop_frame_kboard ();
13787 /* Fill up with spaces. */
13788 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13790 compute_line_metrics (&it);
13791 it.glyph_row->full_width_p = 1;
13792 it.glyph_row->mode_line_p = 1;
13793 it.glyph_row->continued_p = 0;
13794 it.glyph_row->truncated_on_left_p = 0;
13795 it.glyph_row->truncated_on_right_p = 0;
13797 /* Make a 3D mode-line have a shadow at its right end. */
13798 face = FACE_FROM_ID (it.f, face_id);
13799 extend_face_to_end_of_line (&it);
13800 if (face->box != FACE_NO_BOX)
13802 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13803 + it.glyph_row->used[TEXT_AREA] - 1);
13804 last->right_box_line_p = 1;
13807 return it.glyph_row->height;
13810 /* Alist that caches the results of :propertize.
13811 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13812 Lisp_Object mode_line_proptrans_alist;
13814 /* List of strings making up the mode-line. */
13815 Lisp_Object mode_line_string_list;
13817 /* Base face property when building propertized mode line string. */
13818 static Lisp_Object mode_line_string_face;
13819 static Lisp_Object mode_line_string_face_prop;
13822 /* Contribute ELT to the mode line for window IT->w. How it
13823 translates into text depends on its data type.
13825 IT describes the display environment in which we display, as usual.
13827 DEPTH is the depth in recursion. It is used to prevent
13828 infinite recursion here.
13830 FIELD_WIDTH is the number of characters the display of ELT should
13831 occupy in the mode line, and PRECISION is the maximum number of
13832 characters to display from ELT's representation. See
13833 display_string for details.
13835 Returns the hpos of the end of the text generated by ELT.
13837 PROPS is a property list to add to any string we encounter.
13839 If RISKY is nonzero, remove (disregard) any properties in any string
13840 we encounter, and ignore :eval and :propertize.
13842 If the global variable `frame_title_ptr' is non-NULL, then the output
13843 is passed to `store_frame_title' instead of `display_string'. */
13845 static int
13846 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13847 struct it *it;
13848 int depth;
13849 int field_width, precision;
13850 Lisp_Object elt, props;
13851 int risky;
13853 int n = 0, field, prec;
13854 int literal = 0;
13856 tail_recurse:
13857 if (depth > 10)
13858 goto invalid;
13860 depth++;
13862 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13864 case Lisp_String:
13866 /* A string: output it and check for %-constructs within it. */
13867 unsigned char c;
13868 const unsigned char *this, *lisp_string;
13870 if (!NILP (props) || risky)
13872 Lisp_Object oprops, aelt;
13873 oprops = Ftext_properties_at (make_number (0), elt);
13875 if (NILP (Fequal (props, oprops)) || risky)
13877 /* If the starting string has properties,
13878 merge the specified ones onto the existing ones. */
13879 if (! NILP (oprops) && !risky)
13881 Lisp_Object tem;
13883 oprops = Fcopy_sequence (oprops);
13884 tem = props;
13885 while (CONSP (tem))
13887 oprops = Fplist_put (oprops, XCAR (tem),
13888 XCAR (XCDR (tem)));
13889 tem = XCDR (XCDR (tem));
13891 props = oprops;
13894 aelt = Fassoc (elt, mode_line_proptrans_alist);
13895 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13897 mode_line_proptrans_alist
13898 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13899 elt = XCAR (aelt);
13901 else
13903 Lisp_Object tem;
13905 elt = Fcopy_sequence (elt);
13906 Fset_text_properties (make_number (0), Flength (elt),
13907 props, elt);
13908 /* Add this item to mode_line_proptrans_alist. */
13909 mode_line_proptrans_alist
13910 = Fcons (Fcons (elt, props),
13911 mode_line_proptrans_alist);
13912 /* Truncate mode_line_proptrans_alist
13913 to at most 50 elements. */
13914 tem = Fnthcdr (make_number (50),
13915 mode_line_proptrans_alist);
13916 if (! NILP (tem))
13917 XSETCDR (tem, Qnil);
13922 this = SDATA (elt);
13923 lisp_string = this;
13925 if (literal)
13927 prec = precision - n;
13928 if (frame_title_ptr)
13929 n += store_frame_title (SDATA (elt), -1, prec);
13930 else if (!NILP (mode_line_string_list))
13931 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13932 else
13933 n += display_string (NULL, elt, Qnil, 0, 0, it,
13934 0, prec, 0, STRING_MULTIBYTE (elt));
13936 break;
13939 while ((precision <= 0 || n < precision)
13940 && *this
13941 && (frame_title_ptr
13942 || !NILP (mode_line_string_list)
13943 || it->current_x < it->last_visible_x))
13945 const unsigned char *last = this;
13947 /* Advance to end of string or next format specifier. */
13948 while ((c = *this++) != '\0' && c != '%')
13951 if (this - 1 != last)
13953 /* Output to end of string or up to '%'. Field width
13954 is length of string. Don't output more than
13955 PRECISION allows us. */
13956 --this;
13958 prec = chars_in_text (last, this - last);
13959 if (precision > 0 && prec > precision - n)
13960 prec = precision - n;
13962 if (frame_title_ptr)
13963 n += store_frame_title (last, 0, prec);
13964 else if (!NILP (mode_line_string_list))
13966 int bytepos = last - lisp_string;
13967 int charpos = string_byte_to_char (elt, bytepos);
13968 n += store_mode_line_string (NULL,
13969 Fsubstring (elt, make_number (charpos),
13970 make_number (charpos + prec)),
13971 0, 0, 0, Qnil);
13973 else
13975 int bytepos = last - lisp_string;
13976 int charpos = string_byte_to_char (elt, bytepos);
13977 n += display_string (NULL, elt, Qnil, 0, charpos,
13978 it, 0, prec, 0,
13979 STRING_MULTIBYTE (elt));
13982 else /* c == '%' */
13984 const unsigned char *percent_position = this;
13986 /* Get the specified minimum width. Zero means
13987 don't pad. */
13988 field = 0;
13989 while ((c = *this++) >= '0' && c <= '9')
13990 field = field * 10 + c - '0';
13992 /* Don't pad beyond the total padding allowed. */
13993 if (field_width - n > 0 && field > field_width - n)
13994 field = field_width - n;
13996 /* Note that either PRECISION <= 0 or N < PRECISION. */
13997 prec = precision - n;
13999 if (c == 'M')
14000 n += display_mode_element (it, depth, field, prec,
14001 Vglobal_mode_string, props,
14002 risky);
14003 else if (c != 0)
14005 int multibyte;
14006 int bytepos, charpos;
14007 unsigned char *spec;
14009 bytepos = percent_position - lisp_string;
14010 charpos = (STRING_MULTIBYTE (elt)
14011 ? string_byte_to_char (elt, bytepos)
14012 : bytepos);
14014 spec
14015 = decode_mode_spec (it->w, c, field, prec, &multibyte);
14017 if (frame_title_ptr)
14018 n += store_frame_title (spec, field, prec);
14019 else if (!NILP (mode_line_string_list))
14021 int len = strlen (spec);
14022 Lisp_Object tem = make_string (spec, len);
14023 props = Ftext_properties_at (make_number (charpos), elt);
14024 /* Should only keep face property in props */
14025 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
14027 else
14029 int nglyphs_before, nwritten;
14031 nglyphs_before = it->glyph_row->used[TEXT_AREA];
14032 nwritten = display_string (spec, Qnil, elt,
14033 charpos, 0, it,
14034 field, prec, 0,
14035 multibyte);
14037 /* Assign to the glyphs written above the
14038 string where the `%x' came from, position
14039 of the `%'. */
14040 if (nwritten > 0)
14042 struct glyph *glyph
14043 = (it->glyph_row->glyphs[TEXT_AREA]
14044 + nglyphs_before);
14045 int i;
14047 for (i = 0; i < nwritten; ++i)
14049 glyph[i].object = elt;
14050 glyph[i].charpos = charpos;
14053 n += nwritten;
14057 else /* c == 0 */
14058 break;
14062 break;
14064 case Lisp_Symbol:
14065 /* A symbol: process the value of the symbol recursively
14066 as if it appeared here directly. Avoid error if symbol void.
14067 Special case: if value of symbol is a string, output the string
14068 literally. */
14070 register Lisp_Object tem;
14072 /* If the variable is not marked as risky to set
14073 then its contents are risky to use. */
14074 if (NILP (Fget (elt, Qrisky_local_variable)))
14075 risky = 1;
14077 tem = Fboundp (elt);
14078 if (!NILP (tem))
14080 tem = Fsymbol_value (elt);
14081 /* If value is a string, output that string literally:
14082 don't check for % within it. */
14083 if (STRINGP (tem))
14084 literal = 1;
14086 if (!EQ (tem, elt))
14088 /* Give up right away for nil or t. */
14089 elt = tem;
14090 goto tail_recurse;
14094 break;
14096 case Lisp_Cons:
14098 register Lisp_Object car, tem;
14100 /* A cons cell: five distinct cases.
14101 If first element is :eval or :propertize, do something special.
14102 If first element is a string or a cons, process all the elements
14103 and effectively concatenate them.
14104 If first element is a negative number, truncate displaying cdr to
14105 at most that many characters. If positive, pad (with spaces)
14106 to at least that many characters.
14107 If first element is a symbol, process the cadr or caddr recursively
14108 according to whether the symbol's value is non-nil or nil. */
14109 car = XCAR (elt);
14110 if (EQ (car, QCeval))
14112 /* An element of the form (:eval FORM) means evaluate FORM
14113 and use the result as mode line elements. */
14115 if (risky)
14116 break;
14118 if (CONSP (XCDR (elt)))
14120 Lisp_Object spec;
14121 spec = safe_eval (XCAR (XCDR (elt)));
14122 n += display_mode_element (it, depth, field_width - n,
14123 precision - n, spec, props,
14124 risky);
14127 else if (EQ (car, QCpropertize))
14129 /* An element of the form (:propertize ELT PROPS...)
14130 means display ELT but applying properties PROPS. */
14132 if (risky)
14133 break;
14135 if (CONSP (XCDR (elt)))
14136 n += display_mode_element (it, depth, field_width - n,
14137 precision - n, XCAR (XCDR (elt)),
14138 XCDR (XCDR (elt)), risky);
14140 else if (SYMBOLP (car))
14142 tem = Fboundp (car);
14143 elt = XCDR (elt);
14144 if (!CONSP (elt))
14145 goto invalid;
14146 /* elt is now the cdr, and we know it is a cons cell.
14147 Use its car if CAR has a non-nil value. */
14148 if (!NILP (tem))
14150 tem = Fsymbol_value (car);
14151 if (!NILP (tem))
14153 elt = XCAR (elt);
14154 goto tail_recurse;
14157 /* Symbol's value is nil (or symbol is unbound)
14158 Get the cddr of the original list
14159 and if possible find the caddr and use that. */
14160 elt = XCDR (elt);
14161 if (NILP (elt))
14162 break;
14163 else if (!CONSP (elt))
14164 goto invalid;
14165 elt = XCAR (elt);
14166 goto tail_recurse;
14168 else if (INTEGERP (car))
14170 register int lim = XINT (car);
14171 elt = XCDR (elt);
14172 if (lim < 0)
14174 /* Negative int means reduce maximum width. */
14175 if (precision <= 0)
14176 precision = -lim;
14177 else
14178 precision = min (precision, -lim);
14180 else if (lim > 0)
14182 /* Padding specified. Don't let it be more than
14183 current maximum. */
14184 if (precision > 0)
14185 lim = min (precision, lim);
14187 /* If that's more padding than already wanted, queue it.
14188 But don't reduce padding already specified even if
14189 that is beyond the current truncation point. */
14190 field_width = max (lim, field_width);
14192 goto tail_recurse;
14194 else if (STRINGP (car) || CONSP (car))
14196 register int limit = 50;
14197 /* Limit is to protect against circular lists. */
14198 while (CONSP (elt)
14199 && --limit > 0
14200 && (precision <= 0 || n < precision))
14202 n += display_mode_element (it, depth, field_width - n,
14203 precision - n, XCAR (elt),
14204 props, risky);
14205 elt = XCDR (elt);
14209 break;
14211 default:
14212 invalid:
14213 if (frame_title_ptr)
14214 n += store_frame_title ("*invalid*", 0, precision - n);
14215 else if (!NILP (mode_line_string_list))
14216 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14217 else
14218 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14219 precision - n, 0, 0);
14220 return n;
14223 /* Pad to FIELD_WIDTH. */
14224 if (field_width > 0 && n < field_width)
14226 if (frame_title_ptr)
14227 n += store_frame_title ("", field_width - n, 0);
14228 else if (!NILP (mode_line_string_list))
14229 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14230 else
14231 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14232 0, 0, 0);
14235 return n;
14238 /* Store a mode-line string element in mode_line_string_list.
14240 If STRING is non-null, display that C string. Otherwise, the Lisp
14241 string LISP_STRING is displayed.
14243 FIELD_WIDTH is the minimum number of output glyphs to produce.
14244 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14245 with spaces. FIELD_WIDTH <= 0 means don't pad.
14247 PRECISION is the maximum number of characters to output from
14248 STRING. PRECISION <= 0 means don't truncate the string.
14250 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14251 properties to the string.
14253 PROPS are the properties to add to the string.
14254 The mode_line_string_face face property is always added to the string.
14257 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14258 char *string;
14259 Lisp_Object lisp_string;
14260 int copy_string;
14261 int field_width;
14262 int precision;
14263 Lisp_Object props;
14265 int len;
14266 int n = 0;
14268 if (string != NULL)
14270 len = strlen (string);
14271 if (precision > 0 && len > precision)
14272 len = precision;
14273 lisp_string = make_string (string, len);
14274 if (NILP (props))
14275 props = mode_line_string_face_prop;
14276 else if (!NILP (mode_line_string_face))
14278 Lisp_Object face = Fplist_get (props, Qface);
14279 props = Fcopy_sequence (props);
14280 if (NILP (face))
14281 face = mode_line_string_face;
14282 else
14283 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14284 props = Fplist_put (props, Qface, face);
14286 Fadd_text_properties (make_number (0), make_number (len),
14287 props, lisp_string);
14289 else
14291 len = XFASTINT (Flength (lisp_string));
14292 if (precision > 0 && len > precision)
14294 len = precision;
14295 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14296 precision = -1;
14298 if (!NILP (mode_line_string_face))
14300 Lisp_Object face;
14301 if (NILP (props))
14302 props = Ftext_properties_at (make_number (0), lisp_string);
14303 face = Fplist_get (props, Qface);
14304 if (NILP (face))
14305 face = mode_line_string_face;
14306 else
14307 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14308 props = Fcons (Qface, Fcons (face, Qnil));
14309 if (copy_string)
14310 lisp_string = Fcopy_sequence (lisp_string);
14312 if (!NILP (props))
14313 Fadd_text_properties (make_number (0), make_number (len),
14314 props, lisp_string);
14317 if (len > 0)
14319 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14320 n += len;
14323 if (field_width > len)
14325 field_width -= len;
14326 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14327 if (!NILP (props))
14328 Fadd_text_properties (make_number (0), make_number (field_width),
14329 props, lisp_string);
14330 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14331 n += field_width;
14334 return n;
14338 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14339 0, 3, 0,
14340 doc: /* Return the mode-line of selected window as a string.
14341 First optional arg FORMAT specifies a different format string (see
14342 `mode-line-format' for details) to use. If FORMAT is t, return
14343 the buffer's header-line. Second optional arg WINDOW specifies a
14344 different window to use as the context for the formatting.
14345 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14346 (format, window, no_props)
14347 Lisp_Object format, window, no_props;
14349 struct it it;
14350 int len;
14351 struct window *w;
14352 struct buffer *old_buffer = NULL;
14353 enum face_id face_id = DEFAULT_FACE_ID;
14355 if (NILP (window))
14356 window = selected_window;
14357 CHECK_WINDOW (window);
14358 w = XWINDOW (window);
14359 CHECK_BUFFER (w->buffer);
14361 if (XBUFFER (w->buffer) != current_buffer)
14363 old_buffer = current_buffer;
14364 set_buffer_internal_1 (XBUFFER (w->buffer));
14367 if (NILP (format) || EQ (format, Qt))
14369 face_id = NILP (format)
14370 ? CURRENT_MODE_LINE_FACE_ID (w) :
14371 HEADER_LINE_FACE_ID;
14372 format = NILP (format)
14373 ? current_buffer->mode_line_format
14374 : current_buffer->header_line_format;
14377 init_iterator (&it, w, -1, -1, NULL, face_id);
14379 if (NILP (no_props))
14381 mode_line_string_face =
14382 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14383 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14384 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14386 mode_line_string_face_prop =
14387 NILP (mode_line_string_face) ? Qnil :
14388 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14390 /* We need a dummy last element in mode_line_string_list to
14391 indicate we are building the propertized mode-line string.
14392 Using mode_line_string_face_prop here GC protects it. */
14393 mode_line_string_list =
14394 Fcons (mode_line_string_face_prop, Qnil);
14395 frame_title_ptr = NULL;
14397 else
14399 mode_line_string_face_prop = Qnil;
14400 mode_line_string_list = Qnil;
14401 frame_title_ptr = frame_title_buf;
14404 push_frame_kboard (it.f);
14405 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14406 pop_frame_kboard ();
14408 if (old_buffer)
14409 set_buffer_internal_1 (old_buffer);
14411 if (NILP (no_props))
14413 Lisp_Object str;
14414 mode_line_string_list = Fnreverse (mode_line_string_list);
14415 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14416 make_string ("", 0));
14417 mode_line_string_face_prop = Qnil;
14418 mode_line_string_list = Qnil;
14419 return str;
14422 len = frame_title_ptr - frame_title_buf;
14423 if (len > 0 && frame_title_ptr[-1] == '-')
14425 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14426 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14428 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14429 if (len > frame_title_ptr - frame_title_buf)
14430 len = frame_title_ptr - frame_title_buf;
14433 frame_title_ptr = NULL;
14434 return make_string (frame_title_buf, len);
14437 /* Write a null-terminated, right justified decimal representation of
14438 the positive integer D to BUF using a minimal field width WIDTH. */
14440 static void
14441 pint2str (buf, width, d)
14442 register char *buf;
14443 register int width;
14444 register int d;
14446 register char *p = buf;
14448 if (d <= 0)
14449 *p++ = '0';
14450 else
14452 while (d > 0)
14454 *p++ = d % 10 + '0';
14455 d /= 10;
14459 for (width -= (int) (p - buf); width > 0; --width)
14460 *p++ = ' ';
14461 *p-- = '\0';
14462 while (p > buf)
14464 d = *buf;
14465 *buf++ = *p;
14466 *p-- = d;
14470 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14471 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14472 type of CODING_SYSTEM. Return updated pointer into BUF. */
14474 static unsigned char invalid_eol_type[] = "(*invalid*)";
14476 static char *
14477 decode_mode_spec_coding (coding_system, buf, eol_flag)
14478 Lisp_Object coding_system;
14479 register char *buf;
14480 int eol_flag;
14482 Lisp_Object val;
14483 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14484 const unsigned char *eol_str;
14485 int eol_str_len;
14486 /* The EOL conversion we are using. */
14487 Lisp_Object eoltype;
14489 val = Fget (coding_system, Qcoding_system);
14490 eoltype = Qnil;
14492 if (!VECTORP (val)) /* Not yet decided. */
14494 if (multibyte)
14495 *buf++ = '-';
14496 if (eol_flag)
14497 eoltype = eol_mnemonic_undecided;
14498 /* Don't mention EOL conversion if it isn't decided. */
14500 else
14502 Lisp_Object eolvalue;
14504 eolvalue = Fget (coding_system, Qeol_type);
14506 if (multibyte)
14507 *buf++ = XFASTINT (AREF (val, 1));
14509 if (eol_flag)
14511 /* The EOL conversion that is normal on this system. */
14513 if (NILP (eolvalue)) /* Not yet decided. */
14514 eoltype = eol_mnemonic_undecided;
14515 else if (VECTORP (eolvalue)) /* Not yet decided. */
14516 eoltype = eol_mnemonic_undecided;
14517 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14518 eoltype = (XFASTINT (eolvalue) == 0
14519 ? eol_mnemonic_unix
14520 : (XFASTINT (eolvalue) == 1
14521 ? eol_mnemonic_dos : eol_mnemonic_mac));
14525 if (eol_flag)
14527 /* Mention the EOL conversion if it is not the usual one. */
14528 if (STRINGP (eoltype))
14530 eol_str = SDATA (eoltype);
14531 eol_str_len = SBYTES (eoltype);
14533 else if (INTEGERP (eoltype)
14534 && CHAR_VALID_P (XINT (eoltype), 0))
14536 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14537 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14538 eol_str = tmp;
14540 else
14542 eol_str = invalid_eol_type;
14543 eol_str_len = sizeof (invalid_eol_type) - 1;
14545 bcopy (eol_str, buf, eol_str_len);
14546 buf += eol_str_len;
14549 return buf;
14552 /* Return a string for the output of a mode line %-spec for window W,
14553 generated by character C. PRECISION >= 0 means don't return a
14554 string longer than that value. FIELD_WIDTH > 0 means pad the
14555 string returned with spaces to that value. Return 1 in *MULTIBYTE
14556 if the result is multibyte text. */
14558 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14560 static char *
14561 decode_mode_spec (w, c, field_width, precision, multibyte)
14562 struct window *w;
14563 register int c;
14564 int field_width, precision;
14565 int *multibyte;
14567 Lisp_Object obj;
14568 struct frame *f = XFRAME (WINDOW_FRAME (w));
14569 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14570 struct buffer *b = XBUFFER (w->buffer);
14572 obj = Qnil;
14573 *multibyte = 0;
14575 switch (c)
14577 case '*':
14578 if (!NILP (b->read_only))
14579 return "%";
14580 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14581 return "*";
14582 return "-";
14584 case '+':
14585 /* This differs from %* only for a modified read-only buffer. */
14586 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14587 return "*";
14588 if (!NILP (b->read_only))
14589 return "%";
14590 return "-";
14592 case '&':
14593 /* This differs from %* in ignoring read-only-ness. */
14594 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14595 return "*";
14596 return "-";
14598 case '%':
14599 return "%";
14601 case '[':
14603 int i;
14604 char *p;
14606 if (command_loop_level > 5)
14607 return "[[[... ";
14608 p = decode_mode_spec_buf;
14609 for (i = 0; i < command_loop_level; i++)
14610 *p++ = '[';
14611 *p = 0;
14612 return decode_mode_spec_buf;
14615 case ']':
14617 int i;
14618 char *p;
14620 if (command_loop_level > 5)
14621 return " ...]]]";
14622 p = decode_mode_spec_buf;
14623 for (i = 0; i < command_loop_level; i++)
14624 *p++ = ']';
14625 *p = 0;
14626 return decode_mode_spec_buf;
14629 case '-':
14631 register int i;
14633 /* Let lots_of_dashes be a string of infinite length. */
14634 if (!NILP (mode_line_string_list))
14635 return "--";
14636 if (field_width <= 0
14637 || field_width > sizeof (lots_of_dashes))
14639 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14640 decode_mode_spec_buf[i] = '-';
14641 decode_mode_spec_buf[i] = '\0';
14642 return decode_mode_spec_buf;
14644 else
14645 return lots_of_dashes;
14648 case 'b':
14649 obj = b->name;
14650 break;
14652 case 'c':
14654 int col = (int) current_column (); /* iftc */
14655 w->column_number_displayed = make_number (col);
14656 pint2str (decode_mode_spec_buf, field_width, col);
14657 return decode_mode_spec_buf;
14660 case 'F':
14661 /* %F displays the frame name. */
14662 if (!NILP (f->title))
14663 return (char *) SDATA (f->title);
14664 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14665 return (char *) SDATA (f->name);
14666 return "Emacs";
14668 case 'f':
14669 obj = b->filename;
14670 break;
14672 case 'l':
14674 int startpos = XMARKER (w->start)->charpos;
14675 int startpos_byte = marker_byte_position (w->start);
14676 int line, linepos, linepos_byte, topline;
14677 int nlines, junk;
14678 int height = XFASTINT (w->height);
14680 /* If we decided that this buffer isn't suitable for line numbers,
14681 don't forget that too fast. */
14682 if (EQ (w->base_line_pos, w->buffer))
14683 goto no_value;
14684 /* But do forget it, if the window shows a different buffer now. */
14685 else if (BUFFERP (w->base_line_pos))
14686 w->base_line_pos = Qnil;
14688 /* If the buffer is very big, don't waste time. */
14689 if (INTEGERP (Vline_number_display_limit)
14690 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14692 w->base_line_pos = Qnil;
14693 w->base_line_number = Qnil;
14694 goto no_value;
14697 if (!NILP (w->base_line_number)
14698 && !NILP (w->base_line_pos)
14699 && XFASTINT (w->base_line_pos) <= startpos)
14701 line = XFASTINT (w->base_line_number);
14702 linepos = XFASTINT (w->base_line_pos);
14703 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14705 else
14707 line = 1;
14708 linepos = BUF_BEGV (b);
14709 linepos_byte = BUF_BEGV_BYTE (b);
14712 /* Count lines from base line to window start position. */
14713 nlines = display_count_lines (linepos, linepos_byte,
14714 startpos_byte,
14715 startpos, &junk);
14717 topline = nlines + line;
14719 /* Determine a new base line, if the old one is too close
14720 or too far away, or if we did not have one.
14721 "Too close" means it's plausible a scroll-down would
14722 go back past it. */
14723 if (startpos == BUF_BEGV (b))
14725 w->base_line_number = make_number (topline);
14726 w->base_line_pos = make_number (BUF_BEGV (b));
14728 else if (nlines < height + 25 || nlines > height * 3 + 50
14729 || linepos == BUF_BEGV (b))
14731 int limit = BUF_BEGV (b);
14732 int limit_byte = BUF_BEGV_BYTE (b);
14733 int position;
14734 int distance = (height * 2 + 30) * line_number_display_limit_width;
14736 if (startpos - distance > limit)
14738 limit = startpos - distance;
14739 limit_byte = CHAR_TO_BYTE (limit);
14742 nlines = display_count_lines (startpos, startpos_byte,
14743 limit_byte,
14744 - (height * 2 + 30),
14745 &position);
14746 /* If we couldn't find the lines we wanted within
14747 line_number_display_limit_width chars per line,
14748 give up on line numbers for this window. */
14749 if (position == limit_byte && limit == startpos - distance)
14751 w->base_line_pos = w->buffer;
14752 w->base_line_number = Qnil;
14753 goto no_value;
14756 w->base_line_number = make_number (topline - nlines);
14757 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14760 /* Now count lines from the start pos to point. */
14761 nlines = display_count_lines (startpos, startpos_byte,
14762 PT_BYTE, PT, &junk);
14764 /* Record that we did display the line number. */
14765 line_number_displayed = 1;
14767 /* Make the string to show. */
14768 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14769 return decode_mode_spec_buf;
14770 no_value:
14772 char* p = decode_mode_spec_buf;
14773 int pad = field_width - 2;
14774 while (pad-- > 0)
14775 *p++ = ' ';
14776 *p++ = '?';
14777 *p++ = '?';
14778 *p = '\0';
14779 return decode_mode_spec_buf;
14782 break;
14784 case 'm':
14785 obj = b->mode_name;
14786 break;
14788 case 'n':
14789 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14790 return " Narrow";
14791 break;
14793 case 'p':
14795 int pos = marker_position (w->start);
14796 int total = BUF_ZV (b) - BUF_BEGV (b);
14798 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14800 if (pos <= BUF_BEGV (b))
14801 return "All";
14802 else
14803 return "Bottom";
14805 else if (pos <= BUF_BEGV (b))
14806 return "Top";
14807 else
14809 if (total > 1000000)
14810 /* Do it differently for a large value, to avoid overflow. */
14811 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14812 else
14813 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14814 /* We can't normally display a 3-digit number,
14815 so get us a 2-digit number that is close. */
14816 if (total == 100)
14817 total = 99;
14818 sprintf (decode_mode_spec_buf, "%2d%%", total);
14819 return decode_mode_spec_buf;
14823 /* Display percentage of size above the bottom of the screen. */
14824 case 'P':
14826 int toppos = marker_position (w->start);
14827 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14828 int total = BUF_ZV (b) - BUF_BEGV (b);
14830 if (botpos >= BUF_ZV (b))
14832 if (toppos <= BUF_BEGV (b))
14833 return "All";
14834 else
14835 return "Bottom";
14837 else
14839 if (total > 1000000)
14840 /* Do it differently for a large value, to avoid overflow. */
14841 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14842 else
14843 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14844 /* We can't normally display a 3-digit number,
14845 so get us a 2-digit number that is close. */
14846 if (total == 100)
14847 total = 99;
14848 if (toppos <= BUF_BEGV (b))
14849 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14850 else
14851 sprintf (decode_mode_spec_buf, "%2d%%", total);
14852 return decode_mode_spec_buf;
14856 case 's':
14857 /* status of process */
14858 obj = Fget_buffer_process (w->buffer);
14859 if (NILP (obj))
14860 return "no process";
14861 #ifdef subprocesses
14862 obj = Fsymbol_name (Fprocess_status (obj));
14863 #endif
14864 break;
14866 case 't': /* indicate TEXT or BINARY */
14867 #ifdef MODE_LINE_BINARY_TEXT
14868 return MODE_LINE_BINARY_TEXT (b);
14869 #else
14870 return "T";
14871 #endif
14873 case 'z':
14874 /* coding-system (not including end-of-line format) */
14875 case 'Z':
14876 /* coding-system (including end-of-line type) */
14878 int eol_flag = (c == 'Z');
14879 char *p = decode_mode_spec_buf;
14881 if (! FRAME_WINDOW_P (f))
14883 /* No need to mention EOL here--the terminal never needs
14884 to do EOL conversion. */
14885 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14886 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14888 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14889 p, eol_flag);
14891 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14892 #ifdef subprocesses
14893 obj = Fget_buffer_process (Fcurrent_buffer ());
14894 if (PROCESSP (obj))
14896 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14897 p, eol_flag);
14898 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14899 p, eol_flag);
14901 #endif /* subprocesses */
14902 #endif /* 0 */
14903 *p = 0;
14904 return decode_mode_spec_buf;
14908 if (STRINGP (obj))
14910 *multibyte = STRING_MULTIBYTE (obj);
14911 return (char *) SDATA (obj);
14913 else
14914 return "";
14918 /* Count up to COUNT lines starting from START / START_BYTE.
14919 But don't go beyond LIMIT_BYTE.
14920 Return the number of lines thus found (always nonnegative).
14922 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14924 static int
14925 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14926 int start, start_byte, limit_byte, count;
14927 int *byte_pos_ptr;
14929 register unsigned char *cursor;
14930 unsigned char *base;
14932 register int ceiling;
14933 register unsigned char *ceiling_addr;
14934 int orig_count = count;
14936 /* If we are not in selective display mode,
14937 check only for newlines. */
14938 int selective_display = (!NILP (current_buffer->selective_display)
14939 && !INTEGERP (current_buffer->selective_display));
14941 if (count > 0)
14943 while (start_byte < limit_byte)
14945 ceiling = BUFFER_CEILING_OF (start_byte);
14946 ceiling = min (limit_byte - 1, ceiling);
14947 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14948 base = (cursor = BYTE_POS_ADDR (start_byte));
14949 while (1)
14951 if (selective_display)
14952 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14954 else
14955 while (*cursor != '\n' && ++cursor != ceiling_addr)
14958 if (cursor != ceiling_addr)
14960 if (--count == 0)
14962 start_byte += cursor - base + 1;
14963 *byte_pos_ptr = start_byte;
14964 return orig_count;
14966 else
14967 if (++cursor == ceiling_addr)
14968 break;
14970 else
14971 break;
14973 start_byte += cursor - base;
14976 else
14978 while (start_byte > limit_byte)
14980 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14981 ceiling = max (limit_byte, ceiling);
14982 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14983 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14984 while (1)
14986 if (selective_display)
14987 while (--cursor != ceiling_addr
14988 && *cursor != '\n' && *cursor != 015)
14990 else
14991 while (--cursor != ceiling_addr && *cursor != '\n')
14994 if (cursor != ceiling_addr)
14996 if (++count == 0)
14998 start_byte += cursor - base + 1;
14999 *byte_pos_ptr = start_byte;
15000 /* When scanning backwards, we should
15001 not count the newline posterior to which we stop. */
15002 return - orig_count - 1;
15005 else
15006 break;
15008 /* Here we add 1 to compensate for the last decrement
15009 of CURSOR, which took it past the valid range. */
15010 start_byte += cursor - base + 1;
15014 *byte_pos_ptr = limit_byte;
15016 if (count < 0)
15017 return - orig_count + count;
15018 return orig_count - count;
15024 /***********************************************************************
15025 Displaying strings
15026 ***********************************************************************/
15028 /* Display a NUL-terminated string, starting with index START.
15030 If STRING is non-null, display that C string. Otherwise, the Lisp
15031 string LISP_STRING is displayed.
15033 If FACE_STRING is not nil, FACE_STRING_POS is a position in
15034 FACE_STRING. Display STRING or LISP_STRING with the face at
15035 FACE_STRING_POS in FACE_STRING:
15037 Display the string in the environment given by IT, but use the
15038 standard display table, temporarily.
15040 FIELD_WIDTH is the minimum number of output glyphs to produce.
15041 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15042 with spaces. If STRING has more characters, more than FIELD_WIDTH
15043 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
15045 PRECISION is the maximum number of characters to output from
15046 STRING. PRECISION < 0 means don't truncate the string.
15048 This is roughly equivalent to printf format specifiers:
15050 FIELD_WIDTH PRECISION PRINTF
15051 ----------------------------------------
15052 -1 -1 %s
15053 -1 10 %.10s
15054 10 -1 %10s
15055 20 10 %20.10s
15057 MULTIBYTE zero means do not display multibyte chars, > 0 means do
15058 display them, and < 0 means obey the current buffer's value of
15059 enable_multibyte_characters.
15061 Value is the number of glyphs produced. */
15063 static int
15064 display_string (string, lisp_string, face_string, face_string_pos,
15065 start, it, field_width, precision, max_x, multibyte)
15066 unsigned char *string;
15067 Lisp_Object lisp_string;
15068 Lisp_Object face_string;
15069 int face_string_pos;
15070 int start;
15071 struct it *it;
15072 int field_width, precision, max_x;
15073 int multibyte;
15075 int hpos_at_start = it->hpos;
15076 int saved_face_id = it->face_id;
15077 struct glyph_row *row = it->glyph_row;
15079 /* Initialize the iterator IT for iteration over STRING beginning
15080 with index START. */
15081 reseat_to_string (it, string, lisp_string, start,
15082 precision, field_width, multibyte);
15084 /* If displaying STRING, set up the face of the iterator
15085 from LISP_STRING, if that's given. */
15086 if (STRINGP (face_string))
15088 int endptr;
15089 struct face *face;
15091 it->face_id
15092 = face_at_string_position (it->w, face_string, face_string_pos,
15093 0, it->region_beg_charpos,
15094 it->region_end_charpos,
15095 &endptr, it->base_face_id, 0);
15096 face = FACE_FROM_ID (it->f, it->face_id);
15097 it->face_box_p = face->box != FACE_NO_BOX;
15100 /* Set max_x to the maximum allowed X position. Don't let it go
15101 beyond the right edge of the window. */
15102 if (max_x <= 0)
15103 max_x = it->last_visible_x;
15104 else
15105 max_x = min (max_x, it->last_visible_x);
15107 /* Skip over display elements that are not visible. because IT->w is
15108 hscrolled. */
15109 if (it->current_x < it->first_visible_x)
15110 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15111 MOVE_TO_POS | MOVE_TO_X);
15113 row->ascent = it->max_ascent;
15114 row->height = it->max_ascent + it->max_descent;
15115 row->phys_ascent = it->max_phys_ascent;
15116 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15118 /* This condition is for the case that we are called with current_x
15119 past last_visible_x. */
15120 while (it->current_x < max_x)
15122 int x_before, x, n_glyphs_before, i, nglyphs;
15124 /* Get the next display element. */
15125 if (!get_next_display_element (it))
15126 break;
15128 /* Produce glyphs. */
15129 x_before = it->current_x;
15130 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15131 PRODUCE_GLYPHS (it);
15133 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15134 i = 0;
15135 x = x_before;
15136 while (i < nglyphs)
15138 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15140 if (!it->truncate_lines_p
15141 && x + glyph->pixel_width > max_x)
15143 /* End of continued line or max_x reached. */
15144 if (CHAR_GLYPH_PADDING_P (*glyph))
15146 /* A wide character is unbreakable. */
15147 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15148 it->current_x = x_before;
15150 else
15152 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15153 it->current_x = x;
15155 break;
15157 else if (x + glyph->pixel_width > it->first_visible_x)
15159 /* Glyph is at least partially visible. */
15160 ++it->hpos;
15161 if (x < it->first_visible_x)
15162 it->glyph_row->x = x - it->first_visible_x;
15164 else
15166 /* Glyph is off the left margin of the display area.
15167 Should not happen. */
15168 abort ();
15171 row->ascent = max (row->ascent, it->max_ascent);
15172 row->height = max (row->height, it->max_ascent + it->max_descent);
15173 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15174 row->phys_height = max (row->phys_height,
15175 it->max_phys_ascent + it->max_phys_descent);
15176 x += glyph->pixel_width;
15177 ++i;
15180 /* Stop if max_x reached. */
15181 if (i < nglyphs)
15182 break;
15184 /* Stop at line ends. */
15185 if (ITERATOR_AT_END_OF_LINE_P (it))
15187 it->continuation_lines_width = 0;
15188 break;
15191 set_iterator_to_next (it, 1);
15193 /* Stop if truncating at the right edge. */
15194 if (it->truncate_lines_p
15195 && it->current_x >= it->last_visible_x)
15197 /* Add truncation mark, but don't do it if the line is
15198 truncated at a padding space. */
15199 if (IT_CHARPOS (*it) < it->string_nchars)
15201 if (!FRAME_WINDOW_P (it->f))
15203 int i, n;
15205 if (it->current_x > it->last_visible_x)
15207 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15208 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15209 break;
15210 for (n = row->used[TEXT_AREA]; i < n; ++i)
15212 row->used[TEXT_AREA] = i;
15213 produce_special_glyphs (it, IT_TRUNCATION);
15216 produce_special_glyphs (it, IT_TRUNCATION);
15218 it->glyph_row->truncated_on_right_p = 1;
15220 break;
15224 /* Maybe insert a truncation at the left. */
15225 if (it->first_visible_x
15226 && IT_CHARPOS (*it) > 0)
15228 if (!FRAME_WINDOW_P (it->f))
15229 insert_left_trunc_glyphs (it);
15230 it->glyph_row->truncated_on_left_p = 1;
15233 it->face_id = saved_face_id;
15235 /* Value is number of columns displayed. */
15236 return it->hpos - hpos_at_start;
15241 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15242 appears as an element of LIST or as the car of an element of LIST.
15243 If PROPVAL is a list, compare each element against LIST in that
15244 way, and return 1/2 if any element of PROPVAL is found in LIST.
15245 Otherwise return 0. This function cannot quit.
15246 The return value is 2 if the text is invisible but with an ellipsis
15247 and 1 if it's invisible and without an ellipsis. */
15250 invisible_p (propval, list)
15251 register Lisp_Object propval;
15252 Lisp_Object list;
15254 register Lisp_Object tail, proptail;
15256 for (tail = list; CONSP (tail); tail = XCDR (tail))
15258 register Lisp_Object tem;
15259 tem = XCAR (tail);
15260 if (EQ (propval, tem))
15261 return 1;
15262 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15263 return NILP (XCDR (tem)) ? 1 : 2;
15266 if (CONSP (propval))
15268 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15270 Lisp_Object propelt;
15271 propelt = XCAR (proptail);
15272 for (tail = list; CONSP (tail); tail = XCDR (tail))
15274 register Lisp_Object tem;
15275 tem = XCAR (tail);
15276 if (EQ (propelt, tem))
15277 return 1;
15278 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15279 return NILP (XCDR (tem)) ? 1 : 2;
15284 return 0;
15288 /***********************************************************************
15289 Cursor types
15290 ***********************************************************************/
15292 /* Value is the internal representation of the specified cursor type
15293 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
15294 of the bar cursor. */
15296 enum text_cursor_kinds
15297 get_specified_cursor_type (arg, width)
15298 Lisp_Object arg;
15299 int *width;
15301 enum text_cursor_kinds type;
15303 if (NILP (arg))
15304 return NO_CURSOR;
15306 if (EQ (arg, Qbox))
15307 return FILLED_BOX_CURSOR;
15309 if (EQ (arg, Qhollow))
15310 return HOLLOW_BOX_CURSOR;
15312 if (EQ (arg, Qbar))
15314 *width = 2;
15315 return BAR_CURSOR;
15318 if (CONSP (arg)
15319 && EQ (XCAR (arg), Qbar)
15320 && INTEGERP (XCDR (arg))
15321 && XINT (XCDR (arg)) >= 0)
15323 *width = XINT (XCDR (arg));
15324 return BAR_CURSOR;
15327 if (EQ (arg, Qhbar))
15329 *width = 2;
15330 return HBAR_CURSOR;
15333 if (CONSP (arg)
15334 && EQ (XCAR (arg), Qhbar)
15335 && INTEGERP (XCDR (arg))
15336 && XINT (XCDR (arg)) >= 0)
15338 *width = XINT (XCDR (arg));
15339 return HBAR_CURSOR;
15342 /* Treat anything unknown as "hollow box cursor".
15343 It was bad to signal an error; people have trouble fixing
15344 .Xdefaults with Emacs, when it has something bad in it. */
15345 type = HOLLOW_BOX_CURSOR;
15347 return type;
15350 /* Set the default cursor types for specified frame. */
15351 void
15352 set_frame_cursor_types (f, arg)
15353 struct frame *f;
15354 Lisp_Object arg;
15356 int width;
15357 Lisp_Object tem;
15359 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
15360 FRAME_CURSOR_WIDTH (f) = width;
15362 /* By default, set up the blink-off state depending on the on-state. */
15364 tem = Fassoc (arg, Vblink_cursor_alist);
15365 if (!NILP (tem))
15367 FRAME_BLINK_OFF_CURSOR (f)
15368 = get_specified_cursor_type (XCDR (tem), &width);
15369 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
15371 else
15372 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
15376 /* Return the cursor we want to be displayed in window W. Return
15377 width of bar/hbar cursor through WIDTH arg. Return with
15378 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
15379 (i.e. if the `system caret' should track this cursor).
15381 In a mini-buffer window, we want the cursor only to appear if we
15382 are reading input from this window. For the selected window, we
15383 want the cursor type given by the frame parameter or buffer local
15384 setting of cursor-type. If explicitly marked off, draw no cursor.
15385 In all other cases, we want a hollow box cursor. */
15387 enum text_cursor_kinds
15388 get_window_cursor_type (w, width, active_cursor)
15389 struct window *w;
15390 int *width;
15391 int *active_cursor;
15393 struct frame *f = XFRAME (w->frame);
15394 struct buffer *b = XBUFFER (w->buffer);
15395 int cursor_type = DEFAULT_CURSOR;
15396 Lisp_Object alt_cursor;
15397 int non_selected = 0;
15399 *active_cursor = 1;
15401 /* Echo area */
15402 if (cursor_in_echo_area
15403 && FRAME_HAS_MINIBUF_P (f)
15404 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
15406 if (w == XWINDOW (echo_area_window))
15408 *width = FRAME_CURSOR_WIDTH (f);
15409 return FRAME_DESIRED_CURSOR (f);
15412 *active_cursor = 0;
15413 non_selected = 1;
15416 /* Nonselected window or nonselected frame. */
15417 else if (w != XWINDOW (f->selected_window)
15418 #ifdef HAVE_WINDOW_SYSTEM
15419 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
15420 #endif
15423 *active_cursor = 0;
15425 if (MINI_WINDOW_P (w) && minibuf_level == 0)
15426 return NO_CURSOR;
15428 non_selected = 1;
15431 /* Never display a cursor in a window in which cursor-type is nil. */
15432 if (NILP (b->cursor_type))
15433 return NO_CURSOR;
15435 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
15436 if (non_selected)
15438 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
15439 return get_specified_cursor_type (alt_cursor, width);
15442 /* Get the normal cursor type for this window. */
15443 if (EQ (b->cursor_type, Qt))
15445 cursor_type = FRAME_DESIRED_CURSOR (f);
15446 *width = FRAME_CURSOR_WIDTH (f);
15448 else
15449 cursor_type = get_specified_cursor_type (b->cursor_type, width);
15451 /* Use normal cursor if not blinked off. */
15452 if (!w->cursor_off_p)
15453 return cursor_type;
15455 /* Cursor is blinked off, so determine how to "toggle" it. */
15457 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
15458 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
15459 return get_specified_cursor_type (XCDR (alt_cursor), width);
15461 /* Then see if frame has specified a specific blink off cursor type. */
15462 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
15464 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
15465 return FRAME_BLINK_OFF_CURSOR (f);
15468 /* Finally perform built-in cursor blinking:
15469 filled box <-> hollow box
15470 wide [h]bar <-> narrow [h]bar
15471 narrow [h]bar <-> no cursor
15472 other type <-> no cursor */
15474 if (cursor_type == FILLED_BOX_CURSOR)
15475 return HOLLOW_BOX_CURSOR;
15477 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
15479 *width = 1;
15480 return cursor_type;
15483 return NO_CURSOR;
15487 /***********************************************************************
15488 Initialization
15489 ***********************************************************************/
15491 void
15492 syms_of_xdisp ()
15494 Vwith_echo_area_save_vector = Qnil;
15495 staticpro (&Vwith_echo_area_save_vector);
15497 Vmessage_stack = Qnil;
15498 staticpro (&Vmessage_stack);
15500 Qinhibit_redisplay = intern ("inhibit-redisplay");
15501 staticpro (&Qinhibit_redisplay);
15503 message_dolog_marker1 = Fmake_marker ();
15504 staticpro (&message_dolog_marker1);
15505 message_dolog_marker2 = Fmake_marker ();
15506 staticpro (&message_dolog_marker2);
15507 message_dolog_marker3 = Fmake_marker ();
15508 staticpro (&message_dolog_marker3);
15510 #if GLYPH_DEBUG
15511 defsubr (&Sdump_frame_glyph_matrix);
15512 defsubr (&Sdump_glyph_matrix);
15513 defsubr (&Sdump_glyph_row);
15514 defsubr (&Sdump_tool_bar_row);
15515 defsubr (&Strace_redisplay);
15516 defsubr (&Strace_to_stderr);
15517 #endif
15518 #ifdef HAVE_WINDOW_SYSTEM
15519 defsubr (&Stool_bar_lines_needed);
15520 #endif
15521 defsubr (&Sformat_mode_line);
15523 staticpro (&Qmenu_bar_update_hook);
15524 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15526 staticpro (&Qoverriding_terminal_local_map);
15527 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15529 staticpro (&Qoverriding_local_map);
15530 Qoverriding_local_map = intern ("overriding-local-map");
15532 staticpro (&Qwindow_scroll_functions);
15533 Qwindow_scroll_functions = intern ("window-scroll-functions");
15535 staticpro (&Qredisplay_end_trigger_functions);
15536 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15538 staticpro (&Qinhibit_point_motion_hooks);
15539 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15541 QCdata = intern (":data");
15542 staticpro (&QCdata);
15543 Qdisplay = intern ("display");
15544 staticpro (&Qdisplay);
15545 Qspace_width = intern ("space-width");
15546 staticpro (&Qspace_width);
15547 Qraise = intern ("raise");
15548 staticpro (&Qraise);
15549 Qspace = intern ("space");
15550 staticpro (&Qspace);
15551 Qmargin = intern ("margin");
15552 staticpro (&Qmargin);
15553 Qleft_margin = intern ("left-margin");
15554 staticpro (&Qleft_margin);
15555 Qright_margin = intern ("right-margin");
15556 staticpro (&Qright_margin);
15557 Qalign_to = intern ("align-to");
15558 staticpro (&Qalign_to);
15559 QCalign_to = intern (":align-to");
15560 staticpro (&QCalign_to);
15561 Qrelative_width = intern ("relative-width");
15562 staticpro (&Qrelative_width);
15563 QCrelative_width = intern (":relative-width");
15564 staticpro (&QCrelative_width);
15565 QCrelative_height = intern (":relative-height");
15566 staticpro (&QCrelative_height);
15567 QCeval = intern (":eval");
15568 staticpro (&QCeval);
15569 QCpropertize = intern (":propertize");
15570 staticpro (&QCpropertize);
15571 Qwhen = intern ("when");
15572 staticpro (&Qwhen);
15573 QCfile = intern (":file");
15574 staticpro (&QCfile);
15575 Qfontified = intern ("fontified");
15576 staticpro (&Qfontified);
15577 Qfontification_functions = intern ("fontification-functions");
15578 staticpro (&Qfontification_functions);
15579 Qtrailing_whitespace = intern ("trailing-whitespace");
15580 staticpro (&Qtrailing_whitespace);
15581 Qimage = intern ("image");
15582 staticpro (&Qimage);
15583 Qmessage_truncate_lines = intern ("message-truncate-lines");
15584 staticpro (&Qmessage_truncate_lines);
15585 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15586 staticpro (&Qcursor_in_non_selected_windows);
15587 Qgrow_only = intern ("grow-only");
15588 staticpro (&Qgrow_only);
15589 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15590 staticpro (&Qinhibit_menubar_update);
15591 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15592 staticpro (&Qinhibit_eval_during_redisplay);
15593 Qposition = intern ("position");
15594 staticpro (&Qposition);
15595 Qbuffer_position = intern ("buffer-position");
15596 staticpro (&Qbuffer_position);
15597 Qobject = intern ("object");
15598 staticpro (&Qobject);
15599 Qbar = intern ("bar");
15600 staticpro (&Qbar);
15601 Qhbar = intern ("hbar");
15602 staticpro (&Qhbar);
15603 Qbox = intern ("box");
15604 staticpro (&Qbox);
15605 Qhollow = intern ("hollow");
15606 staticpro (&Qhollow);
15607 Qrisky_local_variable = intern ("risky-local-variable");
15608 staticpro (&Qrisky_local_variable);
15609 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
15610 staticpro (&Qinhibit_free_realized_faces);
15612 list_of_error = Fcons (intern ("error"), Qnil);
15613 staticpro (&list_of_error);
15615 last_arrow_position = Qnil;
15616 last_arrow_string = Qnil;
15617 staticpro (&last_arrow_position);
15618 staticpro (&last_arrow_string);
15620 echo_buffer[0] = echo_buffer[1] = Qnil;
15621 staticpro (&echo_buffer[0]);
15622 staticpro (&echo_buffer[1]);
15624 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15625 staticpro (&echo_area_buffer[0]);
15626 staticpro (&echo_area_buffer[1]);
15628 Vmessages_buffer_name = build_string ("*Messages*");
15629 staticpro (&Vmessages_buffer_name);
15631 mode_line_proptrans_alist = Qnil;
15632 staticpro (&mode_line_proptrans_alist);
15634 mode_line_string_list = Qnil;
15635 staticpro (&mode_line_string_list);
15637 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15638 doc: /* Non-nil means highlight trailing whitespace.
15639 The face used for trailing whitespace is `trailing-whitespace'. */);
15640 Vshow_trailing_whitespace = Qnil;
15642 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15643 doc: /* Non-nil means don't actually do any redisplay.
15644 This is used for internal purposes. */);
15645 Vinhibit_redisplay = Qnil;
15647 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15648 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15649 Vglobal_mode_string = Qnil;
15651 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15652 doc: /* Marker for where to display an arrow on top of the buffer text.
15653 This must be the beginning of a line in order to work.
15654 See also `overlay-arrow-string'. */);
15655 Voverlay_arrow_position = Qnil;
15657 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15658 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15659 Voverlay_arrow_string = Qnil;
15661 DEFVAR_INT ("scroll-step", &scroll_step,
15662 doc: /* *The number of lines to try scrolling a window by when point moves out.
15663 If that fails to bring point back on frame, point is centered instead.
15664 If this is zero, point is always centered after it moves off frame.
15665 If you want scrolling to always be a line at a time, you should set
15666 `scroll-conservatively' to a large value rather than set this to 1. */);
15668 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15669 doc: /* *Scroll up to this many lines, to bring point back on screen.
15670 A value of zero means to scroll the text to center point vertically
15671 in the window. */);
15672 scroll_conservatively = 0;
15674 DEFVAR_INT ("scroll-margin", &scroll_margin,
15675 doc: /* *Number of lines of margin at the top and bottom of a window.
15676 Recenter the window whenever point gets within this many lines
15677 of the top or bottom of the window. */);
15678 scroll_margin = 0;
15680 #if GLYPH_DEBUG
15681 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15682 #endif
15684 DEFVAR_BOOL ("truncate-partial-width-windows",
15685 &truncate_partial_width_windows,
15686 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15687 truncate_partial_width_windows = 1;
15689 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15690 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15691 Any other value means to use the appropriate face, `mode-line',
15692 `header-line', or `menu' respectively. */);
15693 mode_line_inverse_video = 1;
15695 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15696 doc: /* *Maximum buffer size for which line number should be displayed.
15697 If the buffer is bigger than this, the line number does not appear
15698 in the mode line. A value of nil means no limit. */);
15699 Vline_number_display_limit = Qnil;
15701 DEFVAR_INT ("line-number-display-limit-width",
15702 &line_number_display_limit_width,
15703 doc: /* *Maximum line width (in characters) for line number display.
15704 If the average length of the lines near point is bigger than this, then the
15705 line number may be omitted from the mode line. */);
15706 line_number_display_limit_width = 200;
15708 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15709 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15710 highlight_nonselected_windows = 0;
15712 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15713 doc: /* Non-nil if more than one frame is visible on this display.
15714 Minibuffer-only frames don't count, but iconified frames do.
15715 This variable is not guaranteed to be accurate except while processing
15716 `frame-title-format' and `icon-title-format'. */);
15718 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15719 doc: /* Template for displaying the title bar of visible frames.
15720 \(Assuming the window manager supports this feature.)
15721 This variable has the same structure as `mode-line-format' (which see),
15722 and is used only on frames for which no explicit name has been set
15723 \(see `modify-frame-parameters'). */);
15724 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15725 doc: /* Template for displaying the title bar of an iconified frame.
15726 \(Assuming the window manager supports this feature.)
15727 This variable has the same structure as `mode-line-format' (which see),
15728 and is used only on frames for which no explicit name has been set
15729 \(see `modify-frame-parameters'). */);
15730 Vicon_title_format
15731 = Vframe_title_format
15732 = Fcons (intern ("multiple-frames"),
15733 Fcons (build_string ("%b"),
15734 Fcons (Fcons (empty_string,
15735 Fcons (intern ("invocation-name"),
15736 Fcons (build_string ("@"),
15737 Fcons (intern ("system-name"),
15738 Qnil)))),
15739 Qnil)));
15741 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15742 doc: /* Maximum number of lines to keep in the message log buffer.
15743 If nil, disable message logging. If t, log messages but don't truncate
15744 the buffer when it becomes large. */);
15745 Vmessage_log_max = make_number (50);
15747 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15748 doc: /* Functions called before redisplay, if window sizes have changed.
15749 The value should be a list of functions that take one argument.
15750 Just before redisplay, for each frame, if any of its windows have changed
15751 size since the last redisplay, or have been split or deleted,
15752 all the functions in the list are called, with the frame as argument. */);
15753 Vwindow_size_change_functions = Qnil;
15755 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15756 doc: /* List of Functions to call before redisplaying a window with scrolling.
15757 Each function is called with two arguments, the window
15758 and its new display-start position. Note that the value of `window-end'
15759 is not valid when these functions are called. */);
15760 Vwindow_scroll_functions = Qnil;
15762 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15763 doc: /* *Non-nil means automatically resize tool-bars.
15764 This increases a tool-bar's height if not all tool-bar items are visible.
15765 It decreases a tool-bar's height when it would display blank lines
15766 otherwise. */);
15767 auto_resize_tool_bars_p = 1;
15769 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15770 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15771 auto_raise_tool_bar_buttons_p = 1;
15773 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15774 doc: /* *Margin around tool-bar buttons in pixels.
15775 If an integer, use that for both horizontal and vertical margins.
15776 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15777 HORZ specifying the horizontal margin, and VERT specifying the
15778 vertical margin. */);
15779 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15781 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15782 doc: /* *Relief thickness of tool-bar buttons. */);
15783 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15785 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15786 doc: /* List of functions to call to fontify regions of text.
15787 Each function is called with one argument POS. Functions must
15788 fontify a region starting at POS in the current buffer, and give
15789 fontified regions the property `fontified'. */);
15790 Vfontification_functions = Qnil;
15791 Fmake_variable_buffer_local (Qfontification_functions);
15793 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15794 &unibyte_display_via_language_environment,
15795 doc: /* *Non-nil means display unibyte text according to language environment.
15796 Specifically this means that unibyte non-ASCII characters
15797 are displayed by converting them to the equivalent multibyte characters
15798 according to the current language environment. As a result, they are
15799 displayed according to the current fontset. */);
15800 unibyte_display_via_language_environment = 0;
15802 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15803 doc: /* *Maximum height for resizing mini-windows.
15804 If a float, it specifies a fraction of the mini-window frame's height.
15805 If an integer, it specifies a number of lines. */);
15806 Vmax_mini_window_height = make_float (0.25);
15808 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15809 doc: /* *How to resize mini-windows.
15810 A value of nil means don't automatically resize mini-windows.
15811 A value of t means resize them to fit the text displayed in them.
15812 A value of `grow-only', the default, means let mini-windows grow
15813 only, until their display becomes empty, at which point the windows
15814 go back to their normal size. */);
15815 Vresize_mini_windows = Qgrow_only;
15817 DEFVAR_LISP ("cursor-in-non-selected-windows",
15818 &Vcursor_in_non_selected_windows,
15819 doc: /* *Cursor type to display in non-selected windows.
15820 t means to use hollow box cursor. See `cursor-type' for other values. */);
15821 Vcursor_in_non_selected_windows = Qt;
15823 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
15824 doc: /* Alist specifying how to blink the cursor off.
15825 Each element has the form (ON-STATE . OFF-STATE). Whenever the
15826 `cursor-type' frame-parameter or variable equals ON-STATE,
15827 comparing using `equal', Emacs uses OFF-STATE to specify
15828 how to blink it off. */);
15829 Vblink_cursor_alist = Qnil;
15831 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15832 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15833 automatic_hscrolling_p = 1;
15835 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15836 doc: /* *How many columns away from the window edge point is allowed to get
15837 before automatic hscrolling will horizontally scroll the window. */);
15838 hscroll_margin = 5;
15840 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15841 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15842 When point is less than `automatic-hscroll-margin' columns from the window
15843 edge, automatic hscrolling will scroll the window by the amount of columns
15844 determined by this variable. If its value is a positive integer, scroll that
15845 many columns. If it's a positive floating-point number, it specifies the
15846 fraction of the window's width to scroll. If it's nil or zero, point will be
15847 centered horizontally after the scroll. Any other value, including negative
15848 numbers, are treated as if the value were zero.
15850 Automatic hscrolling always moves point outside the scroll margin, so if
15851 point was more than scroll step columns inside the margin, the window will
15852 scroll more than the value given by the scroll step.
15854 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15855 and `scroll-right' overrides this variable's effect. */);
15856 Vhscroll_step = make_number (0);
15858 DEFVAR_LISP ("image-types", &Vimage_types,
15859 doc: /* List of supported image types.
15860 Each element of the list is a symbol for a supported image type. */);
15861 Vimage_types = Qnil;
15863 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15864 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15865 Bind this around calls to `message' to let it take effect. */);
15866 message_truncate_lines = 0;
15868 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15869 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15870 Can be used to update submenus whose contents should vary. */);
15871 Vmenu_bar_update_hook = Qnil;
15873 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15874 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15875 inhibit_menubar_update = 0;
15877 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15878 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15879 inhibit_eval_during_redisplay = 0;
15881 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
15882 doc: /* Non-nil means don't free realized faces. Internal use only. */);
15883 inhibit_free_realized_faces = 0;
15885 #if GLYPH_DEBUG
15886 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15887 doc: /* Inhibit try_window_id display optimization. */);
15888 inhibit_try_window_id = 0;
15890 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15891 doc: /* Inhibit try_window_reusing display optimization. */);
15892 inhibit_try_window_reusing = 0;
15894 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15895 doc: /* Inhibit try_cursor_movement display optimization. */);
15896 inhibit_try_cursor_movement = 0;
15897 #endif /* GLYPH_DEBUG */
15901 /* Initialize this module when Emacs starts. */
15903 void
15904 init_xdisp ()
15906 Lisp_Object root_window;
15907 struct window *mini_w;
15909 current_header_line_height = current_mode_line_height = -1;
15911 CHARPOS (this_line_start_pos) = 0;
15913 mini_w = XWINDOW (minibuf_window);
15914 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15916 if (!noninteractive)
15918 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15919 int i;
15921 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15922 set_window_height (root_window,
15923 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15925 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15926 set_window_height (minibuf_window, 1, 0);
15928 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15929 mini_w->width = make_number (FRAME_WIDTH (f));
15931 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15932 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15933 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15935 /* The default ellipsis glyphs `...'. */
15936 for (i = 0; i < 3; ++i)
15937 default_invis_vector[i] = make_number ('.');
15941 /* Allocate the buffer for frame titles.
15942 Also used for `format-mode-line'. */
15943 int size = 100;
15944 frame_title_buf = (char *) xmalloc (size);
15945 frame_title_buf_end = frame_title_buf + size;
15946 frame_title_ptr = NULL;
15949 help_echo_showing_p = 0;