Update commentary to include new function iswitchb-exclude-nonmatching.
[emacs.git] / src / xdisp.c
blob107218b928ba23e727b90202836d8741f8467d81
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? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
201 #define INFINITY 10000000
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
208 extern int interrupt_input;
209 extern int command_loop_level;
211 extern int minibuffer_auto_raise;
213 extern Lisp_Object Qface;
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
224 Lisp_Object Qfontified;
225 Lisp_Object Qgrow_only;
226 Lisp_Object Qinhibit_eval_during_redisplay;
227 Lisp_Object Qbuffer_position, Qposition, Qobject;
229 Lisp_Object Qrisky_local_variable;
231 /* Holds the list (error). */
232 Lisp_Object list_of_error;
234 /* Functions called to fontify regions of text. */
236 Lisp_Object Vfontification_functions;
237 Lisp_Object Qfontification_functions;
239 /* Non-zero means draw tool bar buttons raised when the mouse moves
240 over them. */
242 int auto_raise_tool_bar_buttons_p;
244 /* Margin around tool bar buttons in pixels. */
246 Lisp_Object Vtool_bar_button_margin;
248 /* Thickness of shadow to draw around tool bar buttons. */
250 EMACS_INT tool_bar_button_relief;
252 /* Non-zero means automatically resize tool-bars so that all tool-bar
253 items are visible, and no blank lines remain. */
255 int auto_resize_tool_bars_p;
257 /* Non-nil means don't actually do any redisplay. */
259 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
261 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
263 int inhibit_eval_during_redisplay;
265 /* Names of text properties relevant for redisplay. */
267 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
268 extern Lisp_Object Qface, Qinvisible, Qwidth;
270 /* Symbols used in text property values. */
272 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
273 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
274 Lisp_Object Qmargin;
275 extern Lisp_Object Qheight;
277 /* Non-nil means highlight trailing whitespace. */
279 Lisp_Object Vshow_trailing_whitespace;
281 /* Name of the face used to highlight trailing whitespace. */
283 Lisp_Object Qtrailing_whitespace;
285 /* The symbol `image' which is the car of the lists used to represent
286 images in Lisp. */
288 Lisp_Object Qimage;
290 /* Non-zero means print newline to stdout before next mini-buffer
291 message. */
293 int noninteractive_need_newline;
295 /* Non-zero means print newline to message log before next message. */
297 static int message_log_need_newline;
299 /* Three markers that message_dolog uses.
300 It could allocate them itself, but that causes trouble
301 in handling memory-full errors. */
302 static Lisp_Object message_dolog_marker1;
303 static Lisp_Object message_dolog_marker2;
304 static Lisp_Object message_dolog_marker3;
306 /* The buffer position of the first character appearing entirely or
307 partially on the line of the selected window which contains the
308 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
309 redisplay optimization in redisplay_internal. */
311 static struct text_pos this_line_start_pos;
313 /* Number of characters past the end of the line above, including the
314 terminating newline. */
316 static struct text_pos this_line_end_pos;
318 /* The vertical positions and the height of this line. */
320 static int this_line_vpos;
321 static int this_line_y;
322 static int this_line_pixel_height;
324 /* X position at which this display line starts. Usually zero;
325 negative if first character is partially visible. */
327 static int this_line_start_x;
329 /* Buffer that this_line_.* variables are referring to. */
331 static struct buffer *this_line_buffer;
333 /* Nonzero means truncate lines in all windows less wide than the
334 frame. */
336 int truncate_partial_width_windows;
338 /* A flag to control how to display unibyte 8-bit character. */
340 int unibyte_display_via_language_environment;
342 /* Nonzero means we have more than one non-mini-buffer-only frame.
343 Not guaranteed to be accurate except while parsing
344 frame-title-format. */
346 int multiple_frames;
348 Lisp_Object Vglobal_mode_string;
350 /* Marker for where to display an arrow on top of the buffer text. */
352 Lisp_Object Voverlay_arrow_position;
354 /* String to display for the arrow. Only used on terminal frames. */
356 Lisp_Object Voverlay_arrow_string;
358 /* Values of those variables at last redisplay. However, if
359 Voverlay_arrow_position is a marker, last_arrow_position is its
360 numerical position. */
362 static Lisp_Object last_arrow_position, last_arrow_string;
364 /* Like mode-line-format, but for the title bar on a visible frame. */
366 Lisp_Object Vframe_title_format;
368 /* Like mode-line-format, but for the title bar on an iconified frame. */
370 Lisp_Object Vicon_title_format;
372 /* List of functions to call when a window's size changes. These
373 functions get one arg, a frame on which one or more windows' sizes
374 have changed. */
376 static Lisp_Object Vwindow_size_change_functions;
378 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
380 /* Nonzero if overlay arrow has been displayed once in this window. */
382 static int overlay_arrow_seen;
384 /* Nonzero means highlight the region even in nonselected windows. */
386 int highlight_nonselected_windows;
388 /* If cursor motion alone moves point off frame, try scrolling this
389 many lines up or down if that will bring it back. */
391 static EMACS_INT scroll_step;
393 /* Nonzero means scroll just far enough to bring point back on the
394 screen, when appropriate. */
396 static EMACS_INT scroll_conservatively;
398 /* Recenter the window whenever point gets within this many lines of
399 the top or bottom of the window. This value is translated into a
400 pixel value by multiplying it with CANON_Y_UNIT, which means that
401 there is really a fixed pixel height scroll margin. */
403 EMACS_INT scroll_margin;
405 /* Number of windows showing the buffer of the selected window (or
406 another buffer with the same base buffer). keyboard.c refers to
407 this. */
409 int buffer_shared;
411 /* Vector containing glyphs for an ellipsis `...'. */
413 static Lisp_Object default_invis_vector[3];
415 /* Zero means display the mode-line/header-line/menu-bar in the default face
416 (this slightly odd definition is for compatibility with previous versions
417 of emacs), non-zero means display them using their respective faces.
419 This variable is deprecated. */
421 int mode_line_inverse_video;
423 /* Prompt to display in front of the mini-buffer contents. */
425 Lisp_Object minibuf_prompt;
427 /* Width of current mini-buffer prompt. Only set after display_line
428 of the line that contains the prompt. */
430 int minibuf_prompt_width;
431 int minibuf_prompt_pixel_width;
433 /* This is the window where the echo area message was displayed. It
434 is always a mini-buffer window, but it may not be the same window
435 currently active as a mini-buffer. */
437 Lisp_Object echo_area_window;
439 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
440 pushes the current message and the value of
441 message_enable_multibyte on the stack, the function restore_message
442 pops the stack and displays MESSAGE again. */
444 Lisp_Object Vmessage_stack;
446 /* Nonzero means multibyte characters were enabled when the echo area
447 message was specified. */
449 int message_enable_multibyte;
451 /* Nonzero if we should redraw the mode lines on the next redisplay. */
453 int update_mode_lines;
455 /* Nonzero if window sizes or contents have changed since last
456 redisplay that finished. */
458 int windows_or_buffers_changed;
460 /* Nonzero means a frame's cursor type has been changed. */
462 int cursor_type_changed;
464 /* Nonzero after display_mode_line if %l was used and it displayed a
465 line number. */
467 int line_number_displayed;
469 /* Maximum buffer size for which to display line numbers. */
471 Lisp_Object Vline_number_display_limit;
473 /* Line width to consider when repositioning for line number display. */
475 static EMACS_INT line_number_display_limit_width;
477 /* Number of lines to keep in the message log buffer. t means
478 infinite. nil means don't log at all. */
480 Lisp_Object Vmessage_log_max;
482 /* The name of the *Messages* buffer, a string. */
484 static Lisp_Object Vmessages_buffer_name;
486 /* Current, index 0, and last displayed echo area message. Either
487 buffers from echo_buffers, or nil to indicate no message. */
489 Lisp_Object echo_area_buffer[2];
491 /* The buffers referenced from echo_area_buffer. */
493 static Lisp_Object echo_buffer[2];
495 /* A vector saved used in with_area_buffer to reduce consing. */
497 static Lisp_Object Vwith_echo_area_save_vector;
499 /* Non-zero means display_echo_area should display the last echo area
500 message again. Set by redisplay_preserve_echo_area. */
502 static int display_last_displayed_message_p;
504 /* Nonzero if echo area is being used by print; zero if being used by
505 message. */
507 int message_buf_print;
509 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
511 Lisp_Object Qinhibit_menubar_update;
512 int inhibit_menubar_update;
514 /* Maximum height for resizing mini-windows. Either a float
515 specifying a fraction of the available height, or an integer
516 specifying a number of lines. */
518 Lisp_Object Vmax_mini_window_height;
520 /* Non-zero means messages should be displayed with truncated
521 lines instead of being continued. */
523 int message_truncate_lines;
524 Lisp_Object Qmessage_truncate_lines;
526 /* Set to 1 in clear_message to make redisplay_internal aware
527 of an emptied echo area. */
529 static int message_cleared_p;
531 /* Non-zero means we want a hollow cursor in windows that are not
532 selected. Zero means there's no cursor in such windows. */
534 int cursor_in_non_selected_windows;
535 Lisp_Object Qcursor_in_non_selected_windows;
537 /* A scratch glyph row with contents used for generating truncation
538 glyphs. Also used in direct_output_for_insert. */
540 #define MAX_SCRATCH_GLYPHS 100
541 struct glyph_row scratch_glyph_row;
542 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
544 /* Ascent and height of the last line processed by move_it_to. */
546 static int last_max_ascent, last_height;
548 /* Non-zero if there's a help-echo in the echo area. */
550 int help_echo_showing_p;
552 /* If >= 0, computed, exact values of mode-line and header-line height
553 to use in the macros CURRENT_MODE_LINE_HEIGHT and
554 CURRENT_HEADER_LINE_HEIGHT. */
556 int current_mode_line_height, current_header_line_height;
558 /* The maximum distance to look ahead for text properties. Values
559 that are too small let us call compute_char_face and similar
560 functions too often which is expensive. Values that are too large
561 let us call compute_char_face and alike too often because we
562 might not be interested in text properties that far away. */
564 #define TEXT_PROP_DISTANCE_LIMIT 100
566 #if GLYPH_DEBUG
568 /* Variables to turn off display optimizations from Lisp. */
570 int inhibit_try_window_id, inhibit_try_window_reusing;
571 int inhibit_try_cursor_movement;
573 /* Non-zero means print traces of redisplay if compiled with
574 GLYPH_DEBUG != 0. */
576 int trace_redisplay_p;
578 #endif /* GLYPH_DEBUG */
580 #ifdef DEBUG_TRACE_MOVE
581 /* Non-zero means trace with TRACE_MOVE to stderr. */
582 int trace_move;
584 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
585 #else
586 #define TRACE_MOVE(x) (void) 0
587 #endif
589 /* Non-zero means automatically scroll windows horizontally to make
590 point visible. */
592 int automatic_hscrolling_p;
594 /* How close to the margin can point get before the window is scrolled
595 horizontally. */
596 EMACS_INT hscroll_margin;
598 /* How much to scroll horizontally when point is inside the above margin. */
599 Lisp_Object Vhscroll_step;
601 /* A list of symbols, one for each supported image type. */
603 Lisp_Object Vimage_types;
605 /* The variable `resize-mini-windows'. If nil, don't resize
606 mini-windows. If t, always resize them to fit the text they
607 display. If `grow-only', let mini-windows grow only until they
608 become empty. */
610 Lisp_Object Vresize_mini_windows;
612 /* Buffer being redisplayed -- for redisplay_window_error. */
614 struct buffer *displayed_buffer;
616 /* Value returned from text property handlers (see below). */
618 enum prop_handled
620 HANDLED_NORMALLY,
621 HANDLED_RECOMPUTE_PROPS,
622 HANDLED_OVERLAY_STRING_CONSUMED,
623 HANDLED_RETURN
626 /* A description of text properties that redisplay is interested
627 in. */
629 struct props
631 /* The name of the property. */
632 Lisp_Object *name;
634 /* A unique index for the property. */
635 enum prop_idx idx;
637 /* A handler function called to set up iterator IT from the property
638 at IT's current position. Value is used to steer handle_stop. */
639 enum prop_handled (*handler) P_ ((struct it *it));
642 static enum prop_handled handle_face_prop P_ ((struct it *));
643 static enum prop_handled handle_invisible_prop P_ ((struct it *));
644 static enum prop_handled handle_display_prop P_ ((struct it *));
645 static enum prop_handled handle_composition_prop P_ ((struct it *));
646 static enum prop_handled handle_overlay_change P_ ((struct it *));
647 static enum prop_handled handle_fontified_prop P_ ((struct it *));
649 /* Properties handled by iterators. */
651 static struct props it_props[] =
653 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
654 /* Handle `face' before `display' because some sub-properties of
655 `display' need to know the face. */
656 {&Qface, FACE_PROP_IDX, handle_face_prop},
657 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
658 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
659 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
660 {NULL, 0, NULL}
663 /* Value is the position described by X. If X is a marker, value is
664 the marker_position of X. Otherwise, value is X. */
666 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
668 /* Enumeration returned by some move_it_.* functions internally. */
670 enum move_it_result
672 /* Not used. Undefined value. */
673 MOVE_UNDEFINED,
675 /* Move ended at the requested buffer position or ZV. */
676 MOVE_POS_MATCH_OR_ZV,
678 /* Move ended at the requested X pixel position. */
679 MOVE_X_REACHED,
681 /* Move within a line ended at the end of a line that must be
682 continued. */
683 MOVE_LINE_CONTINUED,
685 /* Move within a line ended at the end of a line that would
686 be displayed truncated. */
687 MOVE_LINE_TRUNCATED,
689 /* Move within a line ended at a line end. */
690 MOVE_NEWLINE_OR_CR
695 /* Function prototypes. */
697 static void setup_for_ellipsis P_ ((struct it *));
698 static void mark_window_display_accurate_1 P_ ((struct window *, int));
699 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
700 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
701 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
702 static int redisplay_mode_lines P_ ((Lisp_Object, int));
703 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
705 #if 0
706 static int invisible_text_between_p P_ ((struct it *, int, int));
707 #endif
709 static int next_element_from_ellipsis P_ ((struct it *));
710 static void pint2str P_ ((char *, int, int));
711 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
712 struct text_pos));
713 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
714 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
715 static void store_frame_title_char P_ ((char));
716 static int store_frame_title P_ ((unsigned char *, int, int));
717 static void x_consider_frame_title P_ ((Lisp_Object));
718 static void handle_stop P_ ((struct it *));
719 static int tool_bar_lines_needed P_ ((struct frame *));
720 static int single_display_prop_intangible_p P_ ((Lisp_Object));
721 static void ensure_echo_area_buffers P_ ((void));
722 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
723 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
724 static int with_echo_area_buffer P_ ((struct window *, int,
725 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
726 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
727 static void clear_garbaged_frames P_ ((void));
728 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
729 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
730 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
731 static int display_echo_area P_ ((struct window *));
732 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
733 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
734 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
735 static int string_char_and_length P_ ((unsigned char *, int, int *));
736 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
737 struct text_pos));
738 static int compute_window_start_on_continuation_line P_ ((struct window *));
739 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
740 static void insert_left_trunc_glyphs P_ ((struct it *));
741 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
742 static void extend_face_to_end_of_line P_ ((struct it *));
743 static int append_space P_ ((struct it *, int));
744 static int make_cursor_line_fully_visible P_ ((struct window *));
745 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
746 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
747 static int trailing_whitespace_p P_ ((int));
748 static int message_log_check_duplicate P_ ((int, int, int, int));
749 static void push_it P_ ((struct it *));
750 static void pop_it P_ ((struct it *));
751 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
752 static void redisplay_internal P_ ((int));
753 static int echo_area_display P_ ((int));
754 static void redisplay_windows P_ ((Lisp_Object));
755 static void redisplay_window P_ ((Lisp_Object, int));
756 static Lisp_Object redisplay_window_error ();
757 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
758 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
759 static void update_menu_bar P_ ((struct frame *, int));
760 static int try_window_reusing_current_matrix P_ ((struct window *));
761 static int try_window_id P_ ((struct window *));
762 static int display_line P_ ((struct it *));
763 static int display_mode_lines P_ ((struct window *));
764 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
765 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
766 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
767 static void display_menu_bar P_ ((struct window *));
768 static int display_count_lines P_ ((int, int, int, int, int *));
769 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
770 int, int, struct it *, int, int, int, int));
771 static void compute_line_metrics P_ ((struct it *));
772 static void run_redisplay_end_trigger_hook P_ ((struct it *));
773 static int get_overlay_strings P_ ((struct it *, int));
774 static void next_overlay_string P_ ((struct it *));
775 static void reseat P_ ((struct it *, struct text_pos, int));
776 static void reseat_1 P_ ((struct it *, struct text_pos, int));
777 static void back_to_previous_visible_line_start P_ ((struct it *));
778 static void reseat_at_previous_visible_line_start P_ ((struct it *));
779 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
780 static int next_element_from_display_vector P_ ((struct it *));
781 static int next_element_from_string P_ ((struct it *));
782 static int next_element_from_c_string P_ ((struct it *));
783 static int next_element_from_buffer P_ ((struct it *));
784 static int next_element_from_composition P_ ((struct it *));
785 static int next_element_from_image P_ ((struct it *));
786 static int next_element_from_stretch P_ ((struct it *));
787 static void load_overlay_strings P_ ((struct it *, int));
788 static int init_from_display_pos P_ ((struct it *, struct window *,
789 struct display_pos *));
790 static void reseat_to_string P_ ((struct it *, unsigned char *,
791 Lisp_Object, int, int, int, int));
792 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
793 int, int, int));
794 void move_it_vertically_backward P_ ((struct it *, int));
795 static void init_to_row_start P_ ((struct it *, struct window *,
796 struct glyph_row *));
797 static int init_to_row_end P_ ((struct it *, struct window *,
798 struct glyph_row *));
799 static void back_to_previous_line_start P_ ((struct it *));
800 static int forward_to_next_line_start P_ ((struct it *, int *));
801 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
802 Lisp_Object, int));
803 static struct text_pos string_pos P_ ((int, Lisp_Object));
804 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
805 static int number_of_chars P_ ((unsigned char *, int));
806 static void compute_stop_pos P_ ((struct it *));
807 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
808 Lisp_Object));
809 static int face_before_or_after_it_pos P_ ((struct it *, int));
810 static int next_overlay_change P_ ((int));
811 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
812 Lisp_Object, struct text_pos *,
813 int));
814 static int underlying_face_id P_ ((struct it *));
815 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
816 struct window *));
818 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
819 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
821 #ifdef HAVE_WINDOW_SYSTEM
823 static void update_tool_bar P_ ((struct frame *, int));
824 static void build_desired_tool_bar_string P_ ((struct frame *f));
825 static int redisplay_tool_bar P_ ((struct frame *));
826 static void display_tool_bar_line P_ ((struct it *));
828 #endif /* HAVE_WINDOW_SYSTEM */
831 /***********************************************************************
832 Window display dimensions
833 ***********************************************************************/
835 /* Return the window-relative maximum y + 1 for glyph rows displaying
836 text in window W. This is the height of W minus the height of a
837 mode line, if any. */
839 INLINE int
840 window_text_bottom_y (w)
841 struct window *w;
843 struct frame *f = XFRAME (w->frame);
844 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
846 if (WINDOW_WANTS_MODELINE_P (w))
847 height -= CURRENT_MODE_LINE_HEIGHT (w);
848 return height;
852 /* Return the pixel width of display area AREA of window W. AREA < 0
853 means return the total width of W, not including fringes to
854 the left and right of the window. */
856 INLINE int
857 window_box_width (w, area)
858 struct window *w;
859 int area;
861 struct frame *f = XFRAME (w->frame);
862 int width = XFASTINT (w->width);
864 if (!w->pseudo_window_p)
866 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
868 if (area == TEXT_AREA)
870 if (INTEGERP (w->left_margin_width))
871 width -= XFASTINT (w->left_margin_width);
872 if (INTEGERP (w->right_margin_width))
873 width -= XFASTINT (w->right_margin_width);
875 else if (area == LEFT_MARGIN_AREA)
876 width = (INTEGERP (w->left_margin_width)
877 ? XFASTINT (w->left_margin_width) : 0);
878 else if (area == RIGHT_MARGIN_AREA)
879 width = (INTEGERP (w->right_margin_width)
880 ? XFASTINT (w->right_margin_width) : 0);
883 return width * CANON_X_UNIT (f);
887 /* Return the pixel height of the display area of window W, not
888 including mode lines of W, if any. */
890 INLINE int
891 window_box_height (w)
892 struct window *w;
894 struct frame *f = XFRAME (w->frame);
895 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
897 xassert (height >= 0);
899 /* Note: the code below that determines the mode-line/header-line
900 height is essentially the same as that contained in the macro
901 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
902 the appropriate glyph row has its `mode_line_p' flag set,
903 and if it doesn't, uses estimate_mode_line_height instead. */
905 if (WINDOW_WANTS_MODELINE_P (w))
907 struct glyph_row *ml_row
908 = (w->current_matrix && w->current_matrix->rows
909 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
910 : 0);
911 if (ml_row && ml_row->mode_line_p)
912 height -= ml_row->height;
913 else
914 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
917 if (WINDOW_WANTS_HEADER_LINE_P (w))
919 struct glyph_row *hl_row
920 = (w->current_matrix && w->current_matrix->rows
921 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
922 : 0);
923 if (hl_row && hl_row->mode_line_p)
924 height -= hl_row->height;
925 else
926 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
929 /* With a very small font and a mode-line that's taller than
930 default, we might end up with a negative height. */
931 return max (0, height);
935 /* Return the frame-relative coordinate of the left edge of display
936 area AREA of window W. AREA < 0 means return the left edge of the
937 whole window, to the right of the left fringe of W. */
939 INLINE int
940 window_box_left (w, area)
941 struct window *w;
942 int area;
944 struct frame *f = XFRAME (w->frame);
945 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
947 if (!w->pseudo_window_p)
949 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
950 + FRAME_LEFT_FRINGE_WIDTH (f));
952 if (area == TEXT_AREA)
953 x += window_box_width (w, LEFT_MARGIN_AREA);
954 else if (area == RIGHT_MARGIN_AREA)
955 x += (window_box_width (w, LEFT_MARGIN_AREA)
956 + window_box_width (w, TEXT_AREA));
959 return x;
963 /* Return the frame-relative coordinate of the right edge of display
964 area AREA of window W. AREA < 0 means return the left edge of the
965 whole window, to the left of the right fringe of W. */
967 INLINE int
968 window_box_right (w, area)
969 struct window *w;
970 int area;
972 return window_box_left (w, area) + window_box_width (w, area);
976 /* Get the bounding box of the display area AREA of window W, without
977 mode lines, in frame-relative coordinates. AREA < 0 means the
978 whole window, not including the left and right fringes of
979 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
980 coordinates of the upper-left corner of the box. Return in
981 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
983 INLINE void
984 window_box (w, area, box_x, box_y, box_width, box_height)
985 struct window *w;
986 int area;
987 int *box_x, *box_y, *box_width, *box_height;
989 struct frame *f = XFRAME (w->frame);
991 *box_width = window_box_width (w, area);
992 *box_height = window_box_height (w);
993 *box_x = window_box_left (w, area);
994 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
995 + XFASTINT (w->top) * CANON_Y_UNIT (f));
996 if (WINDOW_WANTS_HEADER_LINE_P (w))
997 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1001 /* Get the bounding box of the display area AREA of window W, without
1002 mode lines. AREA < 0 means the whole window, not including the
1003 left and right fringe of the window. Return in *TOP_LEFT_X
1004 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1005 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1006 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1007 box. */
1009 INLINE void
1010 window_box_edges (w, area, top_left_x, top_left_y,
1011 bottom_right_x, bottom_right_y)
1012 struct window *w;
1013 int area;
1014 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1016 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1017 bottom_right_y);
1018 *bottom_right_x += *top_left_x;
1019 *bottom_right_y += *top_left_y;
1024 /***********************************************************************
1025 Utilities
1026 ***********************************************************************/
1028 /* Return the bottom y-position of the line the iterator IT is in.
1029 This can modify IT's settings. */
1032 line_bottom_y (it)
1033 struct it *it;
1035 int line_height = it->max_ascent + it->max_descent;
1036 int line_top_y = it->current_y;
1038 if (line_height == 0)
1040 if (last_height)
1041 line_height = last_height;
1042 else if (IT_CHARPOS (*it) < ZV)
1044 move_it_by_lines (it, 1, 1);
1045 line_height = (it->max_ascent || it->max_descent
1046 ? it->max_ascent + it->max_descent
1047 : last_height);
1049 else
1051 struct glyph_row *row = it->glyph_row;
1053 /* Use the default character height. */
1054 it->glyph_row = NULL;
1055 it->what = IT_CHARACTER;
1056 it->c = ' ';
1057 it->len = 1;
1058 PRODUCE_GLYPHS (it);
1059 line_height = it->ascent + it->descent;
1060 it->glyph_row = row;
1064 return line_top_y + line_height;
1068 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1069 1 if POS is visible and the line containing POS is fully visible.
1070 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1071 and header-lines heights. */
1074 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1075 struct window *w;
1076 int charpos, *fully, exact_mode_line_heights_p;
1078 struct it it;
1079 struct text_pos top;
1080 int visible_p;
1081 struct buffer *old_buffer = NULL;
1083 if (XBUFFER (w->buffer) != current_buffer)
1085 old_buffer = current_buffer;
1086 set_buffer_internal_1 (XBUFFER (w->buffer));
1089 *fully = visible_p = 0;
1090 SET_TEXT_POS_FROM_MARKER (top, w->start);
1092 /* Compute exact mode line heights, if requested. */
1093 if (exact_mode_line_heights_p)
1095 if (WINDOW_WANTS_MODELINE_P (w))
1096 current_mode_line_height
1097 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1098 current_buffer->mode_line_format);
1100 if (WINDOW_WANTS_HEADER_LINE_P (w))
1101 current_header_line_height
1102 = display_mode_line (w, HEADER_LINE_FACE_ID,
1103 current_buffer->header_line_format);
1106 start_display (&it, w, top);
1107 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1108 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1110 /* Note that we may overshoot because of invisible text. */
1111 if (IT_CHARPOS (it) >= charpos)
1113 int top_y = it.current_y;
1114 int bottom_y = line_bottom_y (&it);
1115 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1117 if (top_y < window_top_y)
1118 visible_p = bottom_y > window_top_y;
1119 else if (top_y < it.last_visible_y)
1121 visible_p = 1;
1122 *fully = bottom_y <= it.last_visible_y;
1125 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1127 move_it_by_lines (&it, 1, 0);
1128 if (charpos < IT_CHARPOS (it))
1130 visible_p = 1;
1131 *fully = 0;
1135 if (old_buffer)
1136 set_buffer_internal_1 (old_buffer);
1138 current_header_line_height = current_mode_line_height = -1;
1139 return visible_p;
1143 /* Return the next character from STR which is MAXLEN bytes long.
1144 Return in *LEN the length of the character. This is like
1145 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1146 we find one, we return a `?', but with the length of the invalid
1147 character. */
1149 static INLINE int
1150 string_char_and_length (str, maxlen, len)
1151 unsigned char *str;
1152 int maxlen, *len;
1154 int c;
1156 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1157 if (!CHAR_VALID_P (c, 1))
1158 /* We may not change the length here because other places in Emacs
1159 don't use this function, i.e. they silently accept invalid
1160 characters. */
1161 c = '?';
1163 return c;
1168 /* Given a position POS containing a valid character and byte position
1169 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1171 static struct text_pos
1172 string_pos_nchars_ahead (pos, string, nchars)
1173 struct text_pos pos;
1174 Lisp_Object string;
1175 int nchars;
1177 xassert (STRINGP (string) && nchars >= 0);
1179 if (STRING_MULTIBYTE (string))
1181 int rest = SBYTES (string) - BYTEPOS (pos);
1182 unsigned char *p = SDATA (string) + BYTEPOS (pos);
1183 int len;
1185 while (nchars--)
1187 string_char_and_length (p, rest, &len);
1188 p += len, rest -= len;
1189 xassert (rest >= 0);
1190 CHARPOS (pos) += 1;
1191 BYTEPOS (pos) += len;
1194 else
1195 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1197 return pos;
1201 /* Value is the text position, i.e. character and byte position,
1202 for character position CHARPOS in STRING. */
1204 static INLINE struct text_pos
1205 string_pos (charpos, string)
1206 int charpos;
1207 Lisp_Object string;
1209 struct text_pos pos;
1210 xassert (STRINGP (string));
1211 xassert (charpos >= 0);
1212 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1213 return pos;
1217 /* Value is a text position, i.e. character and byte position, for
1218 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1219 means recognize multibyte characters. */
1221 static struct text_pos
1222 c_string_pos (charpos, s, multibyte_p)
1223 int charpos;
1224 unsigned char *s;
1225 int multibyte_p;
1227 struct text_pos pos;
1229 xassert (s != NULL);
1230 xassert (charpos >= 0);
1232 if (multibyte_p)
1234 int rest = strlen (s), len;
1236 SET_TEXT_POS (pos, 0, 0);
1237 while (charpos--)
1239 string_char_and_length (s, rest, &len);
1240 s += len, rest -= len;
1241 xassert (rest >= 0);
1242 CHARPOS (pos) += 1;
1243 BYTEPOS (pos) += len;
1246 else
1247 SET_TEXT_POS (pos, charpos, charpos);
1249 return pos;
1253 /* Value is the number of characters in C string S. MULTIBYTE_P
1254 non-zero means recognize multibyte characters. */
1256 static int
1257 number_of_chars (s, multibyte_p)
1258 unsigned char *s;
1259 int multibyte_p;
1261 int nchars;
1263 if (multibyte_p)
1265 int rest = strlen (s), len;
1266 unsigned char *p = (unsigned char *) s;
1268 for (nchars = 0; rest > 0; ++nchars)
1270 string_char_and_length (p, rest, &len);
1271 rest -= len, p += len;
1274 else
1275 nchars = strlen (s);
1277 return nchars;
1281 /* Compute byte position NEWPOS->bytepos corresponding to
1282 NEWPOS->charpos. POS is a known position in string STRING.
1283 NEWPOS->charpos must be >= POS.charpos. */
1285 static void
1286 compute_string_pos (newpos, pos, string)
1287 struct text_pos *newpos, pos;
1288 Lisp_Object string;
1290 xassert (STRINGP (string));
1291 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1293 if (STRING_MULTIBYTE (string))
1294 *newpos = string_pos_nchars_ahead (pos, string,
1295 CHARPOS (*newpos) - CHARPOS (pos));
1296 else
1297 BYTEPOS (*newpos) = CHARPOS (*newpos);
1302 /***********************************************************************
1303 Lisp form evaluation
1304 ***********************************************************************/
1306 /* Error handler for safe_eval and safe_call. */
1308 static Lisp_Object
1309 safe_eval_handler (arg)
1310 Lisp_Object arg;
1312 add_to_log ("Error during redisplay: %s", arg, Qnil);
1313 return Qnil;
1317 /* Evaluate SEXPR and return the result, or nil if something went
1318 wrong. Prevent redisplay during the evaluation. */
1320 Lisp_Object
1321 safe_eval (sexpr)
1322 Lisp_Object sexpr;
1324 Lisp_Object val;
1326 if (inhibit_eval_during_redisplay)
1327 val = Qnil;
1328 else
1330 int count = BINDING_STACK_SIZE ();
1331 struct gcpro gcpro1;
1333 GCPRO1 (sexpr);
1334 specbind (Qinhibit_redisplay, Qt);
1335 /* Use Qt to ensure debugger does not run,
1336 so there is no possibility of wanting to redisplay. */
1337 val = internal_condition_case_1 (Feval, sexpr, Qt,
1338 safe_eval_handler);
1339 UNGCPRO;
1340 val = unbind_to (count, val);
1343 return val;
1347 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1348 Return the result, or nil if something went wrong. Prevent
1349 redisplay during the evaluation. */
1351 Lisp_Object
1352 safe_call (nargs, args)
1353 int nargs;
1354 Lisp_Object *args;
1356 Lisp_Object val;
1358 if (inhibit_eval_during_redisplay)
1359 val = Qnil;
1360 else
1362 int count = BINDING_STACK_SIZE ();
1363 struct gcpro gcpro1;
1365 GCPRO1 (args[0]);
1366 gcpro1.nvars = nargs;
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_2 (Ffuncall, nargs, args, Qt,
1371 safe_eval_handler);
1372 UNGCPRO;
1373 val = unbind_to (count, val);
1376 return val;
1380 /* Call function FN with one argument ARG.
1381 Return the result, or nil if something went wrong. */
1383 Lisp_Object
1384 safe_call1 (fn, arg)
1385 Lisp_Object fn, arg;
1387 Lisp_Object args[2];
1388 args[0] = fn;
1389 args[1] = arg;
1390 return safe_call (2, args);
1395 /***********************************************************************
1396 Debugging
1397 ***********************************************************************/
1399 #if 0
1401 /* Define CHECK_IT to perform sanity checks on iterators.
1402 This is for debugging. It is too slow to do unconditionally. */
1404 static void
1405 check_it (it)
1406 struct it *it;
1408 if (it->method == next_element_from_string)
1410 xassert (STRINGP (it->string));
1411 xassert (IT_STRING_CHARPOS (*it) >= 0);
1413 else if (it->method == next_element_from_buffer)
1415 /* Check that character and byte positions agree. */
1416 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1419 if (it->dpvec)
1420 xassert (it->current.dpvec_index >= 0);
1421 else
1422 xassert (it->current.dpvec_index < 0);
1425 #define CHECK_IT(IT) check_it ((IT))
1427 #else /* not 0 */
1429 #define CHECK_IT(IT) (void) 0
1431 #endif /* not 0 */
1434 #if GLYPH_DEBUG
1436 /* Check that the window end of window W is what we expect it
1437 to be---the last row in the current matrix displaying text. */
1439 static void
1440 check_window_end (w)
1441 struct window *w;
1443 if (!MINI_WINDOW_P (w)
1444 && !NILP (w->window_end_valid))
1446 struct glyph_row *row;
1447 xassert ((row = MATRIX_ROW (w->current_matrix,
1448 XFASTINT (w->window_end_vpos)),
1449 !row->enabled_p
1450 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1451 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1455 #define CHECK_WINDOW_END(W) check_window_end ((W))
1457 #else /* not GLYPH_DEBUG */
1459 #define CHECK_WINDOW_END(W) (void) 0
1461 #endif /* not GLYPH_DEBUG */
1465 /***********************************************************************
1466 Iterator initialization
1467 ***********************************************************************/
1469 /* Initialize IT for displaying current_buffer in window W, starting
1470 at character position CHARPOS. CHARPOS < 0 means that no buffer
1471 position is specified which is useful when the iterator is assigned
1472 a position later. BYTEPOS is the byte position corresponding to
1473 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1475 If ROW is not null, calls to produce_glyphs with IT as parameter
1476 will produce glyphs in that row.
1478 BASE_FACE_ID is the id of a base face to use. It must be one of
1479 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1480 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1481 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1483 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1484 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1485 will be initialized to use the corresponding mode line glyph row of
1486 the desired matrix of W. */
1488 void
1489 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1490 struct it *it;
1491 struct window *w;
1492 int charpos, bytepos;
1493 struct glyph_row *row;
1494 enum face_id base_face_id;
1496 int highlight_region_p;
1498 /* Some precondition checks. */
1499 xassert (w != NULL && it != NULL);
1500 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1501 && charpos <= ZV));
1503 /* If face attributes have been changed since the last redisplay,
1504 free realized faces now because they depend on face definitions
1505 that might have changed. */
1506 if (face_change_count)
1508 face_change_count = 0;
1509 free_all_realized_faces (Qnil);
1512 /* Use one of the mode line rows of W's desired matrix if
1513 appropriate. */
1514 if (row == NULL)
1516 if (base_face_id == MODE_LINE_FACE_ID
1517 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1518 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1519 else if (base_face_id == HEADER_LINE_FACE_ID)
1520 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1523 /* Clear IT. */
1524 bzero (it, sizeof *it);
1525 it->current.overlay_string_index = -1;
1526 it->current.dpvec_index = -1;
1527 it->base_face_id = base_face_id;
1529 /* The window in which we iterate over current_buffer: */
1530 XSETWINDOW (it->window, w);
1531 it->w = w;
1532 it->f = XFRAME (w->frame);
1534 /* Extra space between lines (on window systems only). */
1535 if (base_face_id == DEFAULT_FACE_ID
1536 && FRAME_WINDOW_P (it->f))
1538 if (NATNUMP (current_buffer->extra_line_spacing))
1539 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1540 else if (it->f->extra_line_spacing > 0)
1541 it->extra_line_spacing = it->f->extra_line_spacing;
1544 /* If realized faces have been removed, e.g. because of face
1545 attribute changes of named faces, recompute them. When running
1546 in batch mode, the face cache of Vterminal_frame is null. If
1547 we happen to get called, make a dummy face cache. */
1548 if (
1549 #ifndef WINDOWSNT
1550 noninteractive &&
1551 #endif
1552 FRAME_FACE_CACHE (it->f) == NULL)
1553 init_frame_faces (it->f);
1554 if (FRAME_FACE_CACHE (it->f)->used == 0)
1555 recompute_basic_faces (it->f);
1557 /* Current value of the `space-width', and 'height' properties. */
1558 it->space_width = Qnil;
1559 it->font_height = Qnil;
1561 /* Are control characters displayed as `^C'? */
1562 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1564 /* -1 means everything between a CR and the following line end
1565 is invisible. >0 means lines indented more than this value are
1566 invisible. */
1567 it->selective = (INTEGERP (current_buffer->selective_display)
1568 ? XFASTINT (current_buffer->selective_display)
1569 : (!NILP (current_buffer->selective_display)
1570 ? -1 : 0));
1571 it->selective_display_ellipsis_p
1572 = !NILP (current_buffer->selective_display_ellipses);
1574 /* Display table to use. */
1575 it->dp = window_display_table (w);
1577 /* Are multibyte characters enabled in current_buffer? */
1578 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1580 /* Non-zero if we should highlight the region. */
1581 highlight_region_p
1582 = (!NILP (Vtransient_mark_mode)
1583 && !NILP (current_buffer->mark_active)
1584 && XMARKER (current_buffer->mark)->buffer != 0);
1586 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1587 start and end of a visible region in window IT->w. Set both to
1588 -1 to indicate no region. */
1589 if (highlight_region_p
1590 /* Maybe highlight only in selected window. */
1591 && (/* Either show region everywhere. */
1592 highlight_nonselected_windows
1593 /* Or show region in the selected window. */
1594 || w == XWINDOW (selected_window)
1595 /* Or show the region if we are in the mini-buffer and W is
1596 the window the mini-buffer refers to. */
1597 || (MINI_WINDOW_P (XWINDOW (selected_window))
1598 && WINDOWP (minibuf_selected_window)
1599 && w == XWINDOW (minibuf_selected_window))))
1601 int charpos = marker_position (current_buffer->mark);
1602 it->region_beg_charpos = min (PT, charpos);
1603 it->region_end_charpos = max (PT, charpos);
1605 else
1606 it->region_beg_charpos = it->region_end_charpos = -1;
1608 /* Get the position at which the redisplay_end_trigger hook should
1609 be run, if it is to be run at all. */
1610 if (MARKERP (w->redisplay_end_trigger)
1611 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1612 it->redisplay_end_trigger_charpos
1613 = marker_position (w->redisplay_end_trigger);
1614 else if (INTEGERP (w->redisplay_end_trigger))
1615 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1617 /* Correct bogus values of tab_width. */
1618 it->tab_width = XINT (current_buffer->tab_width);
1619 if (it->tab_width <= 0 || it->tab_width > 1000)
1620 it->tab_width = 8;
1622 /* Are lines in the display truncated? */
1623 it->truncate_lines_p
1624 = (base_face_id != DEFAULT_FACE_ID
1625 || XINT (it->w->hscroll)
1626 || (truncate_partial_width_windows
1627 && !WINDOW_FULL_WIDTH_P (it->w))
1628 || !NILP (current_buffer->truncate_lines));
1630 /* Get dimensions of truncation and continuation glyphs. These are
1631 displayed as fringe bitmaps under X, so we don't need them for such
1632 frames. */
1633 if (!FRAME_WINDOW_P (it->f))
1635 if (it->truncate_lines_p)
1637 /* We will need the truncation glyph. */
1638 xassert (it->glyph_row == NULL);
1639 produce_special_glyphs (it, IT_TRUNCATION);
1640 it->truncation_pixel_width = it->pixel_width;
1642 else
1644 /* We will need the continuation glyph. */
1645 xassert (it->glyph_row == NULL);
1646 produce_special_glyphs (it, IT_CONTINUATION);
1647 it->continuation_pixel_width = it->pixel_width;
1650 /* Reset these values to zero because the produce_special_glyphs
1651 above has changed them. */
1652 it->pixel_width = it->ascent = it->descent = 0;
1653 it->phys_ascent = it->phys_descent = 0;
1656 /* Set this after getting the dimensions of truncation and
1657 continuation glyphs, so that we don't produce glyphs when calling
1658 produce_special_glyphs, above. */
1659 it->glyph_row = row;
1660 it->area = TEXT_AREA;
1662 /* Get the dimensions of the display area. The display area
1663 consists of the visible window area plus a horizontally scrolled
1664 part to the left of the window. All x-values are relative to the
1665 start of this total display area. */
1666 if (base_face_id != DEFAULT_FACE_ID)
1668 /* Mode lines, menu bar in terminal frames. */
1669 it->first_visible_x = 0;
1670 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1672 else
1674 it->first_visible_x
1675 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1676 it->last_visible_x = (it->first_visible_x
1677 + window_box_width (w, TEXT_AREA));
1679 /* If we truncate lines, leave room for the truncator glyph(s) at
1680 the right margin. Otherwise, leave room for the continuation
1681 glyph(s). Truncation and continuation glyphs are not inserted
1682 for window-based redisplay. */
1683 if (!FRAME_WINDOW_P (it->f))
1685 if (it->truncate_lines_p)
1686 it->last_visible_x -= it->truncation_pixel_width;
1687 else
1688 it->last_visible_x -= it->continuation_pixel_width;
1691 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1692 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1695 /* Leave room for a border glyph. */
1696 if (!FRAME_WINDOW_P (it->f)
1697 && !WINDOW_RIGHTMOST_P (it->w))
1698 it->last_visible_x -= 1;
1700 it->last_visible_y = window_text_bottom_y (w);
1702 /* For mode lines and alike, arrange for the first glyph having a
1703 left box line if the face specifies a box. */
1704 if (base_face_id != DEFAULT_FACE_ID)
1706 struct face *face;
1708 it->face_id = base_face_id;
1710 /* If we have a boxed mode line, make the first character appear
1711 with a left box line. */
1712 face = FACE_FROM_ID (it->f, base_face_id);
1713 if (face->box != FACE_NO_BOX)
1714 it->start_of_box_run_p = 1;
1717 /* If a buffer position was specified, set the iterator there,
1718 getting overlays and face properties from that position. */
1719 if (charpos >= BUF_BEG (current_buffer))
1721 it->end_charpos = ZV;
1722 it->face_id = -1;
1723 IT_CHARPOS (*it) = charpos;
1725 /* Compute byte position if not specified. */
1726 if (bytepos < charpos)
1727 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1728 else
1729 IT_BYTEPOS (*it) = bytepos;
1731 /* Compute faces etc. */
1732 reseat (it, it->current.pos, 1);
1735 CHECK_IT (it);
1739 /* Initialize IT for the display of window W with window start POS. */
1741 void
1742 start_display (it, w, pos)
1743 struct it *it;
1744 struct window *w;
1745 struct text_pos pos;
1747 struct glyph_row *row;
1748 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1750 row = w->desired_matrix->rows + first_vpos;
1751 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1753 if (!it->truncate_lines_p)
1755 int start_at_line_beg_p;
1756 int first_y = it->current_y;
1758 /* If window start is not at a line start, skip forward to POS to
1759 get the correct continuation lines width. */
1760 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1761 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1762 if (!start_at_line_beg_p)
1764 reseat_at_previous_visible_line_start (it);
1765 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1767 /* If lines are continued, this line may end in the middle
1768 of a multi-glyph character (e.g. a control character
1769 displayed as \003, or in the middle of an overlay
1770 string). In this case move_it_to above will not have
1771 taken us to the start of the continuation line but to the
1772 end of the continued line. */
1773 if (it->current_x > 0)
1775 if (it->current.dpvec_index >= 0
1776 || it->current.overlay_string_index >= 0)
1778 set_iterator_to_next (it, 1);
1779 move_it_in_display_line_to (it, -1, -1, 0);
1782 it->continuation_lines_width += it->current_x;
1785 /* We're starting a new display line, not affected by the
1786 height of the continued line, so clear the appropriate
1787 fields in the iterator structure. */
1788 it->max_ascent = it->max_descent = 0;
1789 it->max_phys_ascent = it->max_phys_descent = 0;
1791 it->current_y = first_y;
1792 it->vpos = 0;
1793 it->current_x = it->hpos = 0;
1797 #if 0 /* Don't assert the following because start_display is sometimes
1798 called intentionally with a window start that is not at a
1799 line start. Please leave this code in as a comment. */
1801 /* Window start should be on a line start, now. */
1802 xassert (it->continuation_lines_width
1803 || IT_CHARPOS (it) == BEGV
1804 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1805 #endif /* 0 */
1809 /* Return 1 if POS is a position in ellipses displayed for invisible
1810 text. W is the window we display, for text property lookup. */
1812 static int
1813 in_ellipses_for_invisible_text_p (pos, w)
1814 struct display_pos *pos;
1815 struct window *w;
1817 Lisp_Object prop, window;
1818 int ellipses_p = 0;
1819 int charpos = CHARPOS (pos->pos);
1821 /* If POS specifies a position in a display vector, this might
1822 be for an ellipsis displayed for invisible text. We won't
1823 get the iterator set up for delivering that ellipsis unless
1824 we make sure that it gets aware of the invisible text. */
1825 if (pos->dpvec_index >= 0
1826 && pos->overlay_string_index < 0
1827 && CHARPOS (pos->string_pos) < 0
1828 && charpos > BEGV
1829 && (XSETWINDOW (window, w),
1830 prop = Fget_char_property (make_number (charpos),
1831 Qinvisible, window),
1832 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1834 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1835 window);
1836 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1839 return ellipses_p;
1843 /* Initialize IT for stepping through current_buffer in window W,
1844 starting at position POS that includes overlay string and display
1845 vector/ control character translation position information. Value
1846 is zero if there are overlay strings with newlines at POS. */
1848 static int
1849 init_from_display_pos (it, w, pos)
1850 struct it *it;
1851 struct window *w;
1852 struct display_pos *pos;
1854 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1855 int i, overlay_strings_with_newlines = 0;
1857 /* If POS specifies a position in a display vector, this might
1858 be for an ellipsis displayed for invisible text. We won't
1859 get the iterator set up for delivering that ellipsis unless
1860 we make sure that it gets aware of the invisible text. */
1861 if (in_ellipses_for_invisible_text_p (pos, w))
1863 --charpos;
1864 bytepos = 0;
1867 /* Keep in mind: the call to reseat in init_iterator skips invisible
1868 text, so we might end up at a position different from POS. This
1869 is only a problem when POS is a row start after a newline and an
1870 overlay starts there with an after-string, and the overlay has an
1871 invisible property. Since we don't skip invisible text in
1872 display_line and elsewhere immediately after consuming the
1873 newline before the row start, such a POS will not be in a string,
1874 but the call to init_iterator below will move us to the
1875 after-string. */
1876 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1878 for (i = 0; i < it->n_overlay_strings; ++i)
1880 char *s = SDATA (it->overlay_strings[i]);
1881 char *e = s + SBYTES (it->overlay_strings[i]);
1883 while (s < e && *s != '\n')
1884 ++s;
1886 if (s < e)
1888 overlay_strings_with_newlines = 1;
1889 break;
1893 /* If position is within an overlay string, set up IT to the right
1894 overlay string. */
1895 if (pos->overlay_string_index >= 0)
1897 int relative_index;
1899 /* If the first overlay string happens to have a `display'
1900 property for an image, the iterator will be set up for that
1901 image, and we have to undo that setup first before we can
1902 correct the overlay string index. */
1903 if (it->method == next_element_from_image)
1904 pop_it (it);
1906 /* We already have the first chunk of overlay strings in
1907 IT->overlay_strings. Load more until the one for
1908 pos->overlay_string_index is in IT->overlay_strings. */
1909 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1911 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1912 it->current.overlay_string_index = 0;
1913 while (n--)
1915 load_overlay_strings (it, 0);
1916 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1920 it->current.overlay_string_index = pos->overlay_string_index;
1921 relative_index = (it->current.overlay_string_index
1922 % OVERLAY_STRING_CHUNK_SIZE);
1923 it->string = it->overlay_strings[relative_index];
1924 xassert (STRINGP (it->string));
1925 it->current.string_pos = pos->string_pos;
1926 it->method = next_element_from_string;
1929 #if 0 /* This is bogus because POS not having an overlay string
1930 position does not mean it's after the string. Example: A
1931 line starting with a before-string and initialization of IT
1932 to the previous row's end position. */
1933 else if (it->current.overlay_string_index >= 0)
1935 /* If POS says we're already after an overlay string ending at
1936 POS, make sure to pop the iterator because it will be in
1937 front of that overlay string. When POS is ZV, we've thereby
1938 also ``processed'' overlay strings at ZV. */
1939 while (it->sp)
1940 pop_it (it);
1941 it->current.overlay_string_index = -1;
1942 it->method = next_element_from_buffer;
1943 if (CHARPOS (pos->pos) == ZV)
1944 it->overlay_strings_at_end_processed_p = 1;
1946 #endif /* 0 */
1948 if (CHARPOS (pos->string_pos) >= 0)
1950 /* Recorded position is not in an overlay string, but in another
1951 string. This can only be a string from a `display' property.
1952 IT should already be filled with that string. */
1953 it->current.string_pos = pos->string_pos;
1954 xassert (STRINGP (it->string));
1957 /* Restore position in display vector translations, control
1958 character translations or ellipses. */
1959 if (pos->dpvec_index >= 0)
1961 if (it->dpvec == NULL)
1962 get_next_display_element (it);
1963 xassert (it->dpvec && it->current.dpvec_index == 0);
1964 it->current.dpvec_index = pos->dpvec_index;
1967 CHECK_IT (it);
1968 return !overlay_strings_with_newlines;
1972 /* Initialize IT for stepping through current_buffer in window W
1973 starting at ROW->start. */
1975 static void
1976 init_to_row_start (it, w, row)
1977 struct it *it;
1978 struct window *w;
1979 struct glyph_row *row;
1981 init_from_display_pos (it, w, &row->start);
1982 it->continuation_lines_width = row->continuation_lines_width;
1983 CHECK_IT (it);
1987 /* Initialize IT for stepping through current_buffer in window W
1988 starting in the line following ROW, i.e. starting at ROW->end.
1989 Value is zero if there are overlay strings with newlines at ROW's
1990 end position. */
1992 static int
1993 init_to_row_end (it, w, row)
1994 struct it *it;
1995 struct window *w;
1996 struct glyph_row *row;
1998 int success = 0;
2000 if (init_from_display_pos (it, w, &row->end))
2002 if (row->continued_p)
2003 it->continuation_lines_width
2004 = row->continuation_lines_width + row->pixel_width;
2005 CHECK_IT (it);
2006 success = 1;
2009 return success;
2015 /***********************************************************************
2016 Text properties
2017 ***********************************************************************/
2019 /* Called when IT reaches IT->stop_charpos. Handle text property and
2020 overlay changes. Set IT->stop_charpos to the next position where
2021 to stop. */
2023 static void
2024 handle_stop (it)
2025 struct it *it;
2027 enum prop_handled handled;
2028 int handle_overlay_change_p = 1;
2029 struct props *p;
2031 it->dpvec = NULL;
2032 it->current.dpvec_index = -1;
2036 handled = HANDLED_NORMALLY;
2038 /* Call text property handlers. */
2039 for (p = it_props; p->handler; ++p)
2041 handled = p->handler (it);
2043 if (handled == HANDLED_RECOMPUTE_PROPS)
2044 break;
2045 else if (handled == HANDLED_RETURN)
2046 return;
2047 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2048 handle_overlay_change_p = 0;
2051 if (handled != HANDLED_RECOMPUTE_PROPS)
2053 /* Don't check for overlay strings below when set to deliver
2054 characters from a display vector. */
2055 if (it->method == next_element_from_display_vector)
2056 handle_overlay_change_p = 0;
2058 /* Handle overlay changes. */
2059 if (handle_overlay_change_p)
2060 handled = handle_overlay_change (it);
2062 /* Determine where to stop next. */
2063 if (handled == HANDLED_NORMALLY)
2064 compute_stop_pos (it);
2067 while (handled == HANDLED_RECOMPUTE_PROPS);
2071 /* Compute IT->stop_charpos from text property and overlay change
2072 information for IT's current position. */
2074 static void
2075 compute_stop_pos (it)
2076 struct it *it;
2078 register INTERVAL iv, next_iv;
2079 Lisp_Object object, limit, position;
2081 /* If nowhere else, stop at the end. */
2082 it->stop_charpos = it->end_charpos;
2084 if (STRINGP (it->string))
2086 /* Strings are usually short, so don't limit the search for
2087 properties. */
2088 object = it->string;
2089 limit = Qnil;
2090 position = make_number (IT_STRING_CHARPOS (*it));
2092 else
2094 int charpos;
2096 /* If next overlay change is in front of the current stop pos
2097 (which is IT->end_charpos), stop there. Note: value of
2098 next_overlay_change is point-max if no overlay change
2099 follows. */
2100 charpos = next_overlay_change (IT_CHARPOS (*it));
2101 if (charpos < it->stop_charpos)
2102 it->stop_charpos = charpos;
2104 /* If showing the region, we have to stop at the region
2105 start or end because the face might change there. */
2106 if (it->region_beg_charpos > 0)
2108 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2109 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2110 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2111 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2114 /* Set up variables for computing the stop position from text
2115 property changes. */
2116 XSETBUFFER (object, current_buffer);
2117 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2118 position = make_number (IT_CHARPOS (*it));
2122 /* Get the interval containing IT's position. Value is a null
2123 interval if there isn't such an interval. */
2124 iv = validate_interval_range (object, &position, &position, 0);
2125 if (!NULL_INTERVAL_P (iv))
2127 Lisp_Object values_here[LAST_PROP_IDX];
2128 struct props *p;
2130 /* Get properties here. */
2131 for (p = it_props; p->handler; ++p)
2132 values_here[p->idx] = textget (iv->plist, *p->name);
2134 /* Look for an interval following iv that has different
2135 properties. */
2136 for (next_iv = next_interval (iv);
2137 (!NULL_INTERVAL_P (next_iv)
2138 && (NILP (limit)
2139 || XFASTINT (limit) > next_iv->position));
2140 next_iv = next_interval (next_iv))
2142 for (p = it_props; p->handler; ++p)
2144 Lisp_Object new_value;
2146 new_value = textget (next_iv->plist, *p->name);
2147 if (!EQ (values_here[p->idx], new_value))
2148 break;
2151 if (p->handler)
2152 break;
2155 if (!NULL_INTERVAL_P (next_iv))
2157 if (INTEGERP (limit)
2158 && next_iv->position >= XFASTINT (limit))
2159 /* No text property change up to limit. */
2160 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2161 else
2162 /* Text properties change in next_iv. */
2163 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2167 xassert (STRINGP (it->string)
2168 || (it->stop_charpos >= BEGV
2169 && it->stop_charpos >= IT_CHARPOS (*it)));
2173 /* Return the position of the next overlay change after POS in
2174 current_buffer. Value is point-max if no overlay change
2175 follows. This is like `next-overlay-change' but doesn't use
2176 xmalloc. */
2178 static int
2179 next_overlay_change (pos)
2180 int pos;
2182 int noverlays;
2183 int endpos;
2184 Lisp_Object *overlays;
2185 int len;
2186 int i;
2188 /* Get all overlays at the given position. */
2189 len = 10;
2190 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2191 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2192 if (noverlays > len)
2194 len = noverlays;
2195 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2196 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2199 /* If any of these overlays ends before endpos,
2200 use its ending point instead. */
2201 for (i = 0; i < noverlays; ++i)
2203 Lisp_Object oend;
2204 int oendpos;
2206 oend = OVERLAY_END (overlays[i]);
2207 oendpos = OVERLAY_POSITION (oend);
2208 endpos = min (endpos, oendpos);
2211 return endpos;
2216 /***********************************************************************
2217 Fontification
2218 ***********************************************************************/
2220 /* Handle changes in the `fontified' property of the current buffer by
2221 calling hook functions from Qfontification_functions to fontify
2222 regions of text. */
2224 static enum prop_handled
2225 handle_fontified_prop (it)
2226 struct it *it;
2228 Lisp_Object prop, pos;
2229 enum prop_handled handled = HANDLED_NORMALLY;
2231 /* Get the value of the `fontified' property at IT's current buffer
2232 position. (The `fontified' property doesn't have a special
2233 meaning in strings.) If the value is nil, call functions from
2234 Qfontification_functions. */
2235 if (!STRINGP (it->string)
2236 && it->s == NULL
2237 && !NILP (Vfontification_functions)
2238 && !NILP (Vrun_hooks)
2239 && (pos = make_number (IT_CHARPOS (*it)),
2240 prop = Fget_char_property (pos, Qfontified, Qnil),
2241 NILP (prop)))
2243 int count = BINDING_STACK_SIZE ();
2244 Lisp_Object val;
2246 val = Vfontification_functions;
2247 specbind (Qfontification_functions, Qnil);
2249 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2250 safe_call1 (val, pos);
2251 else
2253 Lisp_Object globals, fn;
2254 struct gcpro gcpro1, gcpro2;
2256 globals = Qnil;
2257 GCPRO2 (val, globals);
2259 for (; CONSP (val); val = XCDR (val))
2261 fn = XCAR (val);
2263 if (EQ (fn, Qt))
2265 /* A value of t indicates this hook has a local
2266 binding; it means to run the global binding too.
2267 In a global value, t should not occur. If it
2268 does, we must ignore it to avoid an endless
2269 loop. */
2270 for (globals = Fdefault_value (Qfontification_functions);
2271 CONSP (globals);
2272 globals = XCDR (globals))
2274 fn = XCAR (globals);
2275 if (!EQ (fn, Qt))
2276 safe_call1 (fn, pos);
2279 else
2280 safe_call1 (fn, pos);
2283 UNGCPRO;
2286 unbind_to (count, Qnil);
2288 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2289 something. This avoids an endless loop if they failed to
2290 fontify the text for which reason ever. */
2291 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2292 handled = HANDLED_RECOMPUTE_PROPS;
2295 return handled;
2300 /***********************************************************************
2301 Faces
2302 ***********************************************************************/
2304 /* Set up iterator IT from face properties at its current position.
2305 Called from handle_stop. */
2307 static enum prop_handled
2308 handle_face_prop (it)
2309 struct it *it;
2311 int new_face_id, next_stop;
2313 if (!STRINGP (it->string))
2315 new_face_id
2316 = face_at_buffer_position (it->w,
2317 IT_CHARPOS (*it),
2318 it->region_beg_charpos,
2319 it->region_end_charpos,
2320 &next_stop,
2321 (IT_CHARPOS (*it)
2322 + TEXT_PROP_DISTANCE_LIMIT),
2325 /* Is this a start of a run of characters with box face?
2326 Caveat: this can be called for a freshly initialized
2327 iterator; face_id is -1 in this case. We know that the new
2328 face will not change until limit, i.e. if the new face has a
2329 box, all characters up to limit will have one. But, as
2330 usual, we don't know whether limit is really the end. */
2331 if (new_face_id != it->face_id)
2333 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2335 /* If new face has a box but old face has not, this is
2336 the start of a run of characters with box, i.e. it has
2337 a shadow on the left side. The value of face_id of the
2338 iterator will be -1 if this is the initial call that gets
2339 the face. In this case, we have to look in front of IT's
2340 position and see whether there is a face != new_face_id. */
2341 it->start_of_box_run_p
2342 = (new_face->box != FACE_NO_BOX
2343 && (it->face_id >= 0
2344 || IT_CHARPOS (*it) == BEG
2345 || new_face_id != face_before_it_pos (it)));
2346 it->face_box_p = new_face->box != FACE_NO_BOX;
2349 else
2351 int base_face_id, bufpos;
2353 if (it->current.overlay_string_index >= 0)
2354 bufpos = IT_CHARPOS (*it);
2355 else
2356 bufpos = 0;
2358 /* For strings from a buffer, i.e. overlay strings or strings
2359 from a `display' property, use the face at IT's current
2360 buffer position as the base face to merge with, so that
2361 overlay strings appear in the same face as surrounding
2362 text, unless they specify their own faces. */
2363 base_face_id = underlying_face_id (it);
2365 new_face_id = face_at_string_position (it->w,
2366 it->string,
2367 IT_STRING_CHARPOS (*it),
2368 bufpos,
2369 it->region_beg_charpos,
2370 it->region_end_charpos,
2371 &next_stop,
2372 base_face_id, 0);
2374 #if 0 /* This shouldn't be neccessary. Let's check it. */
2375 /* If IT is used to display a mode line we would really like to
2376 use the mode line face instead of the frame's default face. */
2377 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2378 && new_face_id == DEFAULT_FACE_ID)
2379 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2380 #endif
2382 /* Is this a start of a run of characters with box? Caveat:
2383 this can be called for a freshly allocated iterator; face_id
2384 is -1 is this case. We know that the new face will not
2385 change until the next check pos, i.e. if the new face has a
2386 box, all characters up to that position will have a
2387 box. But, as usual, we don't know whether that position
2388 is really the end. */
2389 if (new_face_id != it->face_id)
2391 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2392 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2394 /* If new face has a box but old face hasn't, this is the
2395 start of a run of characters with box, i.e. it has a
2396 shadow on the left side. */
2397 it->start_of_box_run_p
2398 = new_face->box && (old_face == NULL || !old_face->box);
2399 it->face_box_p = new_face->box != FACE_NO_BOX;
2403 it->face_id = new_face_id;
2404 return HANDLED_NORMALLY;
2408 /* Return the ID of the face ``underlying'' IT's current position,
2409 which is in a string. If the iterator is associated with a
2410 buffer, return the face at IT's current buffer position.
2411 Otherwise, use the iterator's base_face_id. */
2413 static int
2414 underlying_face_id (it)
2415 struct it *it;
2417 int face_id = it->base_face_id, i;
2419 xassert (STRINGP (it->string));
2421 for (i = it->sp - 1; i >= 0; --i)
2422 if (NILP (it->stack[i].string))
2423 face_id = it->stack[i].face_id;
2425 return face_id;
2429 /* Compute the face one character before or after the current position
2430 of IT. BEFORE_P non-zero means get the face in front of IT's
2431 position. Value is the id of the face. */
2433 static int
2434 face_before_or_after_it_pos (it, before_p)
2435 struct it *it;
2436 int before_p;
2438 int face_id, limit;
2439 int next_check_charpos;
2440 struct text_pos pos;
2442 xassert (it->s == NULL);
2444 if (STRINGP (it->string))
2446 int bufpos, base_face_id;
2448 /* No face change past the end of the string (for the case
2449 we are padding with spaces). No face change before the
2450 string start. */
2451 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2452 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2453 return it->face_id;
2455 /* Set pos to the position before or after IT's current position. */
2456 if (before_p)
2457 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2458 else
2459 /* For composition, we must check the character after the
2460 composition. */
2461 pos = (it->what == IT_COMPOSITION
2462 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2463 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2465 if (it->current.overlay_string_index >= 0)
2466 bufpos = IT_CHARPOS (*it);
2467 else
2468 bufpos = 0;
2470 base_face_id = underlying_face_id (it);
2472 /* Get the face for ASCII, or unibyte. */
2473 face_id = face_at_string_position (it->w,
2474 it->string,
2475 CHARPOS (pos),
2476 bufpos,
2477 it->region_beg_charpos,
2478 it->region_end_charpos,
2479 &next_check_charpos,
2480 base_face_id, 0);
2482 /* Correct the face for charsets different from ASCII. Do it
2483 for the multibyte case only. The face returned above is
2484 suitable for unibyte text if IT->string is unibyte. */
2485 if (STRING_MULTIBYTE (it->string))
2487 unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2488 int rest = SBYTES (it->string) - BYTEPOS (pos);
2489 int c, len;
2490 struct face *face = FACE_FROM_ID (it->f, face_id);
2492 c = string_char_and_length (p, rest, &len);
2493 face_id = FACE_FOR_CHAR (it->f, face, c);
2496 else
2498 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2499 || (IT_CHARPOS (*it) <= BEGV && before_p))
2500 return it->face_id;
2502 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2503 pos = it->current.pos;
2505 if (before_p)
2506 DEC_TEXT_POS (pos, it->multibyte_p);
2507 else
2509 if (it->what == IT_COMPOSITION)
2510 /* For composition, we must check the position after the
2511 composition. */
2512 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2513 else
2514 INC_TEXT_POS (pos, it->multibyte_p);
2517 /* Determine face for CHARSET_ASCII, or unibyte. */
2518 face_id = face_at_buffer_position (it->w,
2519 CHARPOS (pos),
2520 it->region_beg_charpos,
2521 it->region_end_charpos,
2522 &next_check_charpos,
2523 limit, 0);
2525 /* Correct the face for charsets different from ASCII. Do it
2526 for the multibyte case only. The face returned above is
2527 suitable for unibyte text if current_buffer is unibyte. */
2528 if (it->multibyte_p)
2530 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2531 struct face *face = FACE_FROM_ID (it->f, face_id);
2532 face_id = FACE_FOR_CHAR (it->f, face, c);
2536 return face_id;
2541 /***********************************************************************
2542 Invisible text
2543 ***********************************************************************/
2545 /* Set up iterator IT from invisible properties at its current
2546 position. Called from handle_stop. */
2548 static enum prop_handled
2549 handle_invisible_prop (it)
2550 struct it *it;
2552 enum prop_handled handled = HANDLED_NORMALLY;
2554 if (STRINGP (it->string))
2556 extern Lisp_Object Qinvisible;
2557 Lisp_Object prop, end_charpos, limit, charpos;
2559 /* Get the value of the invisible text property at the
2560 current position. Value will be nil if there is no such
2561 property. */
2562 charpos = make_number (IT_STRING_CHARPOS (*it));
2563 prop = Fget_text_property (charpos, Qinvisible, it->string);
2565 if (!NILP (prop)
2566 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2568 handled = HANDLED_RECOMPUTE_PROPS;
2570 /* Get the position at which the next change of the
2571 invisible text property can be found in IT->string.
2572 Value will be nil if the property value is the same for
2573 all the rest of IT->string. */
2574 XSETINT (limit, SCHARS (it->string));
2575 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2576 it->string, limit);
2578 /* Text at current position is invisible. The next
2579 change in the property is at position end_charpos.
2580 Move IT's current position to that position. */
2581 if (INTEGERP (end_charpos)
2582 && XFASTINT (end_charpos) < XFASTINT (limit))
2584 struct text_pos old;
2585 old = it->current.string_pos;
2586 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2587 compute_string_pos (&it->current.string_pos, old, it->string);
2589 else
2591 /* The rest of the string is invisible. If this is an
2592 overlay string, proceed with the next overlay string
2593 or whatever comes and return a character from there. */
2594 if (it->current.overlay_string_index >= 0)
2596 next_overlay_string (it);
2597 /* Don't check for overlay strings when we just
2598 finished processing them. */
2599 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2601 else
2603 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2604 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2609 else
2611 int invis_p, newpos, next_stop, start_charpos;
2612 Lisp_Object pos, prop, overlay;
2614 /* First of all, is there invisible text at this position? */
2615 start_charpos = IT_CHARPOS (*it);
2616 pos = make_number (IT_CHARPOS (*it));
2617 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2618 &overlay);
2619 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2621 /* If we are on invisible text, skip over it. */
2622 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2624 /* Record whether we have to display an ellipsis for the
2625 invisible text. */
2626 int display_ellipsis_p = invis_p == 2;
2628 handled = HANDLED_RECOMPUTE_PROPS;
2630 /* Loop skipping over invisible text. The loop is left at
2631 ZV or with IT on the first char being visible again. */
2634 /* Try to skip some invisible text. Return value is the
2635 position reached which can be equal to IT's position
2636 if there is nothing invisible here. This skips both
2637 over invisible text properties and overlays with
2638 invisible property. */
2639 newpos = skip_invisible (IT_CHARPOS (*it),
2640 &next_stop, ZV, it->window);
2642 /* If we skipped nothing at all we weren't at invisible
2643 text in the first place. If everything to the end of
2644 the buffer was skipped, end the loop. */
2645 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2646 invis_p = 0;
2647 else
2649 /* We skipped some characters but not necessarily
2650 all there are. Check if we ended up on visible
2651 text. Fget_char_property returns the property of
2652 the char before the given position, i.e. if we
2653 get invis_p = 0, this means that the char at
2654 newpos is visible. */
2655 pos = make_number (newpos);
2656 prop = Fget_char_property (pos, Qinvisible, it->window);
2657 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2660 /* If we ended up on invisible text, proceed to
2661 skip starting with next_stop. */
2662 if (invis_p)
2663 IT_CHARPOS (*it) = next_stop;
2665 while (invis_p);
2667 /* The position newpos is now either ZV or on visible text. */
2668 IT_CHARPOS (*it) = newpos;
2669 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2671 /* If there are before-strings at the start of invisible
2672 text, and the text is invisible because of a text
2673 property, arrange to show before-strings because 20.x did
2674 it that way. (If the text is invisible because of an
2675 overlay property instead of a text property, this is
2676 already handled in the overlay code.) */
2677 if (NILP (overlay)
2678 && get_overlay_strings (it, start_charpos))
2680 handled = HANDLED_RECOMPUTE_PROPS;
2681 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2683 else if (display_ellipsis_p)
2684 setup_for_ellipsis (it);
2688 return handled;
2692 /* Make iterator IT return `...' next. */
2694 static void
2695 setup_for_ellipsis (it)
2696 struct it *it;
2698 if (it->dp
2699 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2701 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2702 it->dpvec = v->contents;
2703 it->dpend = v->contents + v->size;
2705 else
2707 /* Default `...'. */
2708 it->dpvec = default_invis_vector;
2709 it->dpend = default_invis_vector + 3;
2712 /* The ellipsis display does not replace the display of the
2713 character at the new position. Indicate this by setting
2714 IT->dpvec_char_len to zero. */
2715 it->dpvec_char_len = 0;
2717 it->current.dpvec_index = 0;
2718 it->method = next_element_from_display_vector;
2723 /***********************************************************************
2724 'display' property
2725 ***********************************************************************/
2727 /* Set up iterator IT from `display' property at its current position.
2728 Called from handle_stop. */
2730 static enum prop_handled
2731 handle_display_prop (it)
2732 struct it *it;
2734 Lisp_Object prop, object;
2735 struct text_pos *position;
2736 int display_replaced_p = 0;
2738 if (STRINGP (it->string))
2740 object = it->string;
2741 position = &it->current.string_pos;
2743 else
2745 object = it->w->buffer;
2746 position = &it->current.pos;
2749 /* Reset those iterator values set from display property values. */
2750 it->font_height = Qnil;
2751 it->space_width = Qnil;
2752 it->voffset = 0;
2754 /* We don't support recursive `display' properties, i.e. string
2755 values that have a string `display' property, that have a string
2756 `display' property etc. */
2757 if (!it->string_from_display_prop_p)
2758 it->area = TEXT_AREA;
2760 prop = Fget_char_property (make_number (position->charpos),
2761 Qdisplay, object);
2762 if (NILP (prop))
2763 return HANDLED_NORMALLY;
2765 if (CONSP (prop)
2766 /* Simple properties. */
2767 && !EQ (XCAR (prop), Qimage)
2768 && !EQ (XCAR (prop), Qspace)
2769 && !EQ (XCAR (prop), Qwhen)
2770 && !EQ (XCAR (prop), Qspace_width)
2771 && !EQ (XCAR (prop), Qheight)
2772 && !EQ (XCAR (prop), Qraise)
2773 /* Marginal area specifications. */
2774 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2775 && !NILP (XCAR (prop)))
2777 for (; CONSP (prop); prop = XCDR (prop))
2779 if (handle_single_display_prop (it, XCAR (prop), object,
2780 position, display_replaced_p))
2781 display_replaced_p = 1;
2784 else if (VECTORP (prop))
2786 int i;
2787 for (i = 0; i < ASIZE (prop); ++i)
2788 if (handle_single_display_prop (it, AREF (prop, i), object,
2789 position, display_replaced_p))
2790 display_replaced_p = 1;
2792 else
2794 if (handle_single_display_prop (it, prop, object, position, 0))
2795 display_replaced_p = 1;
2798 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2802 /* Value is the position of the end of the `display' property starting
2803 at START_POS in OBJECT. */
2805 static struct text_pos
2806 display_prop_end (it, object, start_pos)
2807 struct it *it;
2808 Lisp_Object object;
2809 struct text_pos start_pos;
2811 Lisp_Object end;
2812 struct text_pos end_pos;
2814 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2815 Qdisplay, object, Qnil);
2816 CHARPOS (end_pos) = XFASTINT (end);
2817 if (STRINGP (object))
2818 compute_string_pos (&end_pos, start_pos, it->string);
2819 else
2820 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2822 return end_pos;
2826 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2827 is the object in which the `display' property was found. *POSITION
2828 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2829 means that we previously saw a display sub-property which already
2830 replaced text display with something else, for example an image;
2831 ignore such properties after the first one has been processed.
2833 If PROP is a `space' or `image' sub-property, set *POSITION to the
2834 end position of the `display' property.
2836 Value is non-zero if something was found which replaces the display
2837 of buffer or string text. */
2839 static int
2840 handle_single_display_prop (it, prop, object, position,
2841 display_replaced_before_p)
2842 struct it *it;
2843 Lisp_Object prop;
2844 Lisp_Object object;
2845 struct text_pos *position;
2846 int display_replaced_before_p;
2848 Lisp_Object value;
2849 int replaces_text_display_p = 0;
2850 Lisp_Object form;
2852 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2853 evaluated. If the result is nil, VALUE is ignored. */
2854 form = Qt;
2855 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2857 prop = XCDR (prop);
2858 if (!CONSP (prop))
2859 return 0;
2860 form = XCAR (prop);
2861 prop = XCDR (prop);
2864 if (!NILP (form) && !EQ (form, Qt))
2866 int count = BINDING_STACK_SIZE ();
2867 struct gcpro gcpro1;
2869 /* Bind `object' to the object having the `display' property, a
2870 buffer or string. Bind `position' to the position in the
2871 object where the property was found, and `buffer-position'
2872 to the current position in the buffer. */
2873 specbind (Qobject, object);
2874 specbind (Qposition, make_number (CHARPOS (*position)));
2875 specbind (Qbuffer_position,
2876 make_number (STRINGP (object)
2877 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2878 GCPRO1 (form);
2879 form = safe_eval (form);
2880 UNGCPRO;
2881 unbind_to (count, Qnil);
2884 if (NILP (form))
2885 return 0;
2887 if (CONSP (prop)
2888 && EQ (XCAR (prop), Qheight)
2889 && CONSP (XCDR (prop)))
2891 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2892 return 0;
2894 /* `(height HEIGHT)'. */
2895 it->font_height = XCAR (XCDR (prop));
2896 if (!NILP (it->font_height))
2898 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2899 int new_height = -1;
2901 if (CONSP (it->font_height)
2902 && (EQ (XCAR (it->font_height), Qplus)
2903 || EQ (XCAR (it->font_height), Qminus))
2904 && CONSP (XCDR (it->font_height))
2905 && INTEGERP (XCAR (XCDR (it->font_height))))
2907 /* `(+ N)' or `(- N)' where N is an integer. */
2908 int steps = XINT (XCAR (XCDR (it->font_height)));
2909 if (EQ (XCAR (it->font_height), Qplus))
2910 steps = - steps;
2911 it->face_id = smaller_face (it->f, it->face_id, steps);
2913 else if (FUNCTIONP (it->font_height))
2915 /* Call function with current height as argument.
2916 Value is the new height. */
2917 Lisp_Object height;
2918 height = safe_call1 (it->font_height,
2919 face->lface[LFACE_HEIGHT_INDEX]);
2920 if (NUMBERP (height))
2921 new_height = XFLOATINT (height);
2923 else if (NUMBERP (it->font_height))
2925 /* Value is a multiple of the canonical char height. */
2926 struct face *face;
2928 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2929 new_height = (XFLOATINT (it->font_height)
2930 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2932 else
2934 /* Evaluate IT->font_height with `height' bound to the
2935 current specified height to get the new height. */
2936 Lisp_Object value;
2937 int count = BINDING_STACK_SIZE ();
2939 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2940 value = safe_eval (it->font_height);
2941 unbind_to (count, Qnil);
2943 if (NUMBERP (value))
2944 new_height = XFLOATINT (value);
2947 if (new_height > 0)
2948 it->face_id = face_with_height (it->f, it->face_id, new_height);
2951 else if (CONSP (prop)
2952 && EQ (XCAR (prop), Qspace_width)
2953 && CONSP (XCDR (prop)))
2955 /* `(space_width WIDTH)'. */
2956 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2957 return 0;
2959 value = XCAR (XCDR (prop));
2960 if (NUMBERP (value) && XFLOATINT (value) > 0)
2961 it->space_width = value;
2963 else if (CONSP (prop)
2964 && EQ (XCAR (prop), Qraise)
2965 && CONSP (XCDR (prop)))
2967 /* `(raise FACTOR)'. */
2968 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2969 return 0;
2971 #ifdef HAVE_WINDOW_SYSTEM
2972 value = XCAR (XCDR (prop));
2973 if (NUMBERP (value))
2975 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2976 it->voffset = - (XFLOATINT (value)
2977 * (FONT_HEIGHT (face->font)));
2979 #endif /* HAVE_WINDOW_SYSTEM */
2981 else if (!it->string_from_display_prop_p)
2983 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2984 VALUE) or `((margin nil) VALUE)' or VALUE. */
2985 Lisp_Object location, value;
2986 struct text_pos start_pos;
2987 int valid_p;
2989 /* Characters having this form of property are not displayed, so
2990 we have to find the end of the property. */
2991 start_pos = *position;
2992 *position = display_prop_end (it, object, start_pos);
2993 value = Qnil;
2995 /* Let's stop at the new position and assume that all
2996 text properties change there. */
2997 it->stop_charpos = position->charpos;
2999 location = Qunbound;
3000 if (CONSP (prop) && CONSP (XCAR (prop)))
3002 Lisp_Object tem;
3004 value = XCDR (prop);
3005 if (CONSP (value))
3006 value = XCAR (value);
3008 tem = XCAR (prop);
3009 if (EQ (XCAR (tem), Qmargin)
3010 && (tem = XCDR (tem),
3011 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3012 (NILP (tem)
3013 || EQ (tem, Qleft_margin)
3014 || EQ (tem, Qright_margin))))
3015 location = tem;
3018 if (EQ (location, Qunbound))
3020 location = Qnil;
3021 value = prop;
3024 #ifdef HAVE_WINDOW_SYSTEM
3025 if (FRAME_TERMCAP_P (it->f))
3026 valid_p = STRINGP (value);
3027 else
3028 valid_p = (STRINGP (value)
3029 || (CONSP (value) && EQ (XCAR (value), Qspace))
3030 || valid_image_p (value));
3031 #else /* not HAVE_WINDOW_SYSTEM */
3032 valid_p = STRINGP (value);
3033 #endif /* not HAVE_WINDOW_SYSTEM */
3035 if ((EQ (location, Qleft_margin)
3036 || EQ (location, Qright_margin)
3037 || NILP (location))
3038 && valid_p
3039 && !display_replaced_before_p)
3041 replaces_text_display_p = 1;
3043 /* Save current settings of IT so that we can restore them
3044 when we are finished with the glyph property value. */
3045 push_it (it);
3047 if (NILP (location))
3048 it->area = TEXT_AREA;
3049 else if (EQ (location, Qleft_margin))
3050 it->area = LEFT_MARGIN_AREA;
3051 else
3052 it->area = RIGHT_MARGIN_AREA;
3054 if (STRINGP (value))
3056 it->string = value;
3057 it->multibyte_p = STRING_MULTIBYTE (it->string);
3058 it->current.overlay_string_index = -1;
3059 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3060 it->end_charpos = it->string_nchars = SCHARS (it->string);
3061 it->method = next_element_from_string;
3062 it->stop_charpos = 0;
3063 it->string_from_display_prop_p = 1;
3064 /* Say that we haven't consumed the characters with
3065 `display' property yet. The call to pop_it in
3066 set_iterator_to_next will clean this up. */
3067 *position = start_pos;
3069 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3071 it->method = next_element_from_stretch;
3072 it->object = value;
3073 it->current.pos = it->position = start_pos;
3075 #ifdef HAVE_WINDOW_SYSTEM
3076 else
3078 it->what = IT_IMAGE;
3079 it->image_id = lookup_image (it->f, value);
3080 it->position = start_pos;
3081 it->object = NILP (object) ? it->w->buffer : object;
3082 it->method = next_element_from_image;
3084 /* Say that we haven't consumed the characters with
3085 `display' property yet. The call to pop_it in
3086 set_iterator_to_next will clean this up. */
3087 *position = start_pos;
3089 #endif /* HAVE_WINDOW_SYSTEM */
3091 else
3092 /* Invalid property or property not supported. Restore
3093 the position to what it was before. */
3094 *position = start_pos;
3097 return replaces_text_display_p;
3101 /* Check if PROP is a display sub-property value whose text should be
3102 treated as intangible. */
3104 static int
3105 single_display_prop_intangible_p (prop)
3106 Lisp_Object prop;
3108 /* Skip over `when FORM'. */
3109 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3111 prop = XCDR (prop);
3112 if (!CONSP (prop))
3113 return 0;
3114 prop = XCDR (prop);
3117 if (!CONSP (prop))
3118 return 0;
3120 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3121 we don't need to treat text as intangible. */
3122 if (EQ (XCAR (prop), Qmargin))
3124 prop = XCDR (prop);
3125 if (!CONSP (prop))
3126 return 0;
3128 prop = XCDR (prop);
3129 if (!CONSP (prop)
3130 || EQ (XCAR (prop), Qleft_margin)
3131 || EQ (XCAR (prop), Qright_margin))
3132 return 0;
3135 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3139 /* Check if PROP is a display property value whose text should be
3140 treated as intangible. */
3143 display_prop_intangible_p (prop)
3144 Lisp_Object prop;
3146 if (CONSP (prop)
3147 && CONSP (XCAR (prop))
3148 && !EQ (Qmargin, XCAR (XCAR (prop))))
3150 /* A list of sub-properties. */
3151 while (CONSP (prop))
3153 if (single_display_prop_intangible_p (XCAR (prop)))
3154 return 1;
3155 prop = XCDR (prop);
3158 else if (VECTORP (prop))
3160 /* A vector of sub-properties. */
3161 int i;
3162 for (i = 0; i < ASIZE (prop); ++i)
3163 if (single_display_prop_intangible_p (AREF (prop, i)))
3164 return 1;
3166 else
3167 return single_display_prop_intangible_p (prop);
3169 return 0;
3173 /* Return 1 if PROP is a display sub-property value containing STRING. */
3175 static int
3176 single_display_prop_string_p (prop, string)
3177 Lisp_Object prop, string;
3179 if (EQ (string, prop))
3180 return 1;
3182 /* Skip over `when FORM'. */
3183 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3185 prop = XCDR (prop);
3186 if (!CONSP (prop))
3187 return 0;
3188 prop = XCDR (prop);
3191 if (CONSP (prop))
3192 /* Skip over `margin LOCATION'. */
3193 if (EQ (XCAR (prop), Qmargin))
3195 prop = XCDR (prop);
3196 if (!CONSP (prop))
3197 return 0;
3199 prop = XCDR (prop);
3200 if (!CONSP (prop))
3201 return 0;
3204 return CONSP (prop) && EQ (XCAR (prop), string);
3208 /* Return 1 if STRING appears in the `display' property PROP. */
3210 static int
3211 display_prop_string_p (prop, string)
3212 Lisp_Object prop, string;
3214 if (CONSP (prop)
3215 && CONSP (XCAR (prop))
3216 && !EQ (Qmargin, XCAR (XCAR (prop))))
3218 /* A list of sub-properties. */
3219 while (CONSP (prop))
3221 if (single_display_prop_string_p (XCAR (prop), string))
3222 return 1;
3223 prop = XCDR (prop);
3226 else if (VECTORP (prop))
3228 /* A vector of sub-properties. */
3229 int i;
3230 for (i = 0; i < ASIZE (prop); ++i)
3231 if (single_display_prop_string_p (AREF (prop, i), string))
3232 return 1;
3234 else
3235 return single_display_prop_string_p (prop, string);
3237 return 0;
3241 /* Determine from which buffer position in W's buffer STRING comes
3242 from. AROUND_CHARPOS is an approximate position where it could
3243 be from. Value is the buffer position or 0 if it couldn't be
3244 determined.
3246 W's buffer must be current.
3248 This function is necessary because we don't record buffer positions
3249 in glyphs generated from strings (to keep struct glyph small).
3250 This function may only use code that doesn't eval because it is
3251 called asynchronously from note_mouse_highlight. */
3254 string_buffer_position (w, string, around_charpos)
3255 struct window *w;
3256 Lisp_Object string;
3257 int around_charpos;
3259 Lisp_Object limit, prop, pos;
3260 const int MAX_DISTANCE = 1000;
3261 int found = 0;
3263 pos = make_number (around_charpos);
3264 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3265 while (!found && !EQ (pos, limit))
3267 prop = Fget_char_property (pos, Qdisplay, Qnil);
3268 if (!NILP (prop) && display_prop_string_p (prop, string))
3269 found = 1;
3270 else
3271 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3274 if (!found)
3276 pos = make_number (around_charpos);
3277 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3278 while (!found && !EQ (pos, limit))
3280 prop = Fget_char_property (pos, Qdisplay, Qnil);
3281 if (!NILP (prop) && display_prop_string_p (prop, string))
3282 found = 1;
3283 else
3284 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3285 limit);
3289 return found ? XINT (pos) : 0;
3294 /***********************************************************************
3295 `composition' property
3296 ***********************************************************************/
3298 /* Set up iterator IT from `composition' property at its current
3299 position. Called from handle_stop. */
3301 static enum prop_handled
3302 handle_composition_prop (it)
3303 struct it *it;
3305 Lisp_Object prop, string;
3306 int pos, pos_byte, end;
3307 enum prop_handled handled = HANDLED_NORMALLY;
3309 if (STRINGP (it->string))
3311 pos = IT_STRING_CHARPOS (*it);
3312 pos_byte = IT_STRING_BYTEPOS (*it);
3313 string = it->string;
3315 else
3317 pos = IT_CHARPOS (*it);
3318 pos_byte = IT_BYTEPOS (*it);
3319 string = Qnil;
3322 /* If there's a valid composition and point is not inside of the
3323 composition (in the case that the composition is from the current
3324 buffer), draw a glyph composed from the composition components. */
3325 if (find_composition (pos, -1, &pos, &end, &prop, string)
3326 && COMPOSITION_VALID_P (pos, end, prop)
3327 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3329 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3331 if (id >= 0)
3333 it->method = next_element_from_composition;
3334 it->cmp_id = id;
3335 it->cmp_len = COMPOSITION_LENGTH (prop);
3336 /* For a terminal, draw only the first character of the
3337 components. */
3338 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3339 it->len = (STRINGP (it->string)
3340 ? string_char_to_byte (it->string, end)
3341 : CHAR_TO_BYTE (end)) - pos_byte;
3342 it->stop_charpos = end;
3343 handled = HANDLED_RETURN;
3347 return handled;
3352 /***********************************************************************
3353 Overlay strings
3354 ***********************************************************************/
3356 /* The following structure is used to record overlay strings for
3357 later sorting in load_overlay_strings. */
3359 struct overlay_entry
3361 Lisp_Object overlay;
3362 Lisp_Object string;
3363 int priority;
3364 int after_string_p;
3368 /* Set up iterator IT from overlay strings at its current position.
3369 Called from handle_stop. */
3371 static enum prop_handled
3372 handle_overlay_change (it)
3373 struct it *it;
3375 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3376 return HANDLED_RECOMPUTE_PROPS;
3377 else
3378 return HANDLED_NORMALLY;
3382 /* Set up the next overlay string for delivery by IT, if there is an
3383 overlay string to deliver. Called by set_iterator_to_next when the
3384 end of the current overlay string is reached. If there are more
3385 overlay strings to display, IT->string and
3386 IT->current.overlay_string_index are set appropriately here.
3387 Otherwise IT->string is set to nil. */
3389 static void
3390 next_overlay_string (it)
3391 struct it *it;
3393 ++it->current.overlay_string_index;
3394 if (it->current.overlay_string_index == it->n_overlay_strings)
3396 /* No more overlay strings. Restore IT's settings to what
3397 they were before overlay strings were processed, and
3398 continue to deliver from current_buffer. */
3399 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3401 pop_it (it);
3402 xassert (it->stop_charpos >= BEGV
3403 && it->stop_charpos <= it->end_charpos);
3404 it->string = Qnil;
3405 it->current.overlay_string_index = -1;
3406 SET_TEXT_POS (it->current.string_pos, -1, -1);
3407 it->n_overlay_strings = 0;
3408 it->method = next_element_from_buffer;
3410 /* If we're at the end of the buffer, record that we have
3411 processed the overlay strings there already, so that
3412 next_element_from_buffer doesn't try it again. */
3413 if (IT_CHARPOS (*it) >= it->end_charpos)
3414 it->overlay_strings_at_end_processed_p = 1;
3416 /* If we have to display `...' for invisible text, set
3417 the iterator up for that. */
3418 if (display_ellipsis_p)
3419 setup_for_ellipsis (it);
3421 else
3423 /* There are more overlay strings to process. If
3424 IT->current.overlay_string_index has advanced to a position
3425 where we must load IT->overlay_strings with more strings, do
3426 it. */
3427 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3429 if (it->current.overlay_string_index && i == 0)
3430 load_overlay_strings (it, 0);
3432 /* Initialize IT to deliver display elements from the overlay
3433 string. */
3434 it->string = it->overlay_strings[i];
3435 it->multibyte_p = STRING_MULTIBYTE (it->string);
3436 SET_TEXT_POS (it->current.string_pos, 0, 0);
3437 it->method = next_element_from_string;
3438 it->stop_charpos = 0;
3441 CHECK_IT (it);
3445 /* Compare two overlay_entry structures E1 and E2. Used as a
3446 comparison function for qsort in load_overlay_strings. Overlay
3447 strings for the same position are sorted so that
3449 1. All after-strings come in front of before-strings, except
3450 when they come from the same overlay.
3452 2. Within after-strings, strings are sorted so that overlay strings
3453 from overlays with higher priorities come first.
3455 2. Within before-strings, strings are sorted so that overlay
3456 strings from overlays with higher priorities come last.
3458 Value is analogous to strcmp. */
3461 static int
3462 compare_overlay_entries (e1, e2)
3463 void *e1, *e2;
3465 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3466 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3467 int result;
3469 if (entry1->after_string_p != entry2->after_string_p)
3471 /* Let after-strings appear in front of before-strings if
3472 they come from different overlays. */
3473 if (EQ (entry1->overlay, entry2->overlay))
3474 result = entry1->after_string_p ? 1 : -1;
3475 else
3476 result = entry1->after_string_p ? -1 : 1;
3478 else if (entry1->after_string_p)
3479 /* After-strings sorted in order of decreasing priority. */
3480 result = entry2->priority - entry1->priority;
3481 else
3482 /* Before-strings sorted in order of increasing priority. */
3483 result = entry1->priority - entry2->priority;
3485 return result;
3489 /* Load the vector IT->overlay_strings with overlay strings from IT's
3490 current buffer position, or from CHARPOS if that is > 0. Set
3491 IT->n_overlays to the total number of overlay strings found.
3493 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3494 a time. On entry into load_overlay_strings,
3495 IT->current.overlay_string_index gives the number of overlay
3496 strings that have already been loaded by previous calls to this
3497 function.
3499 IT->add_overlay_start contains an additional overlay start
3500 position to consider for taking overlay strings from, if non-zero.
3501 This position comes into play when the overlay has an `invisible'
3502 property, and both before and after-strings. When we've skipped to
3503 the end of the overlay, because of its `invisible' property, we
3504 nevertheless want its before-string to appear.
3505 IT->add_overlay_start will contain the overlay start position
3506 in this case.
3508 Overlay strings are sorted so that after-string strings come in
3509 front of before-string strings. Within before and after-strings,
3510 strings are sorted by overlay priority. See also function
3511 compare_overlay_entries. */
3513 static void
3514 load_overlay_strings (it, charpos)
3515 struct it *it;
3516 int charpos;
3518 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3519 Lisp_Object ov, overlay, window, str, invisible;
3520 int start, end;
3521 int size = 20;
3522 int n = 0, i, j, invis_p;
3523 struct overlay_entry *entries
3524 = (struct overlay_entry *) alloca (size * sizeof *entries);
3526 if (charpos <= 0)
3527 charpos = IT_CHARPOS (*it);
3529 /* Append the overlay string STRING of overlay OVERLAY to vector
3530 `entries' which has size `size' and currently contains `n'
3531 elements. AFTER_P non-zero means STRING is an after-string of
3532 OVERLAY. */
3533 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3534 do \
3536 Lisp_Object priority; \
3538 if (n == size) \
3540 int new_size = 2 * size; \
3541 struct overlay_entry *old = entries; \
3542 entries = \
3543 (struct overlay_entry *) alloca (new_size \
3544 * sizeof *entries); \
3545 bcopy (old, entries, size * sizeof *entries); \
3546 size = new_size; \
3549 entries[n].string = (STRING); \
3550 entries[n].overlay = (OVERLAY); \
3551 priority = Foverlay_get ((OVERLAY), Qpriority); \
3552 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3553 entries[n].after_string_p = (AFTER_P); \
3554 ++n; \
3556 while (0)
3558 /* Process overlay before the overlay center. */
3559 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3561 overlay = XCAR (ov);
3562 xassert (OVERLAYP (overlay));
3563 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3564 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3566 if (end < charpos)
3567 break;
3569 /* Skip this overlay if it doesn't start or end at IT's current
3570 position. */
3571 if (end != charpos && start != charpos)
3572 continue;
3574 /* Skip this overlay if it doesn't apply to IT->w. */
3575 window = Foverlay_get (overlay, Qwindow);
3576 if (WINDOWP (window) && XWINDOW (window) != it->w)
3577 continue;
3579 /* If the text ``under'' the overlay is invisible, both before-
3580 and after-strings from this overlay are visible; start and
3581 end position are indistinguishable. */
3582 invisible = Foverlay_get (overlay, Qinvisible);
3583 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3585 /* If overlay has a non-empty before-string, record it. */
3586 if ((start == charpos || (end == charpos && invis_p))
3587 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3588 && SCHARS (str))
3589 RECORD_OVERLAY_STRING (overlay, str, 0);
3591 /* If overlay has a non-empty after-string, record it. */
3592 if ((end == charpos || (start == charpos && invis_p))
3593 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3594 && SCHARS (str))
3595 RECORD_OVERLAY_STRING (overlay, str, 1);
3598 /* Process overlays after the overlay center. */
3599 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3601 overlay = XCAR (ov);
3602 xassert (OVERLAYP (overlay));
3603 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3604 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3606 if (start > charpos)
3607 break;
3609 /* Skip this overlay if it doesn't start or end at IT's current
3610 position. */
3611 if (end != charpos && start != charpos)
3612 continue;
3614 /* Skip this overlay if it doesn't apply to IT->w. */
3615 window = Foverlay_get (overlay, Qwindow);
3616 if (WINDOWP (window) && XWINDOW (window) != it->w)
3617 continue;
3619 /* If the text ``under'' the overlay is invisible, it has a zero
3620 dimension, and both before- and after-strings apply. */
3621 invisible = Foverlay_get (overlay, Qinvisible);
3622 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3624 /* If overlay has a non-empty before-string, record it. */
3625 if ((start == charpos || (end == charpos && invis_p))
3626 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3627 && SCHARS (str))
3628 RECORD_OVERLAY_STRING (overlay, str, 0);
3630 /* If overlay has a non-empty after-string, record it. */
3631 if ((end == charpos || (start == charpos && invis_p))
3632 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3633 && SCHARS (str))
3634 RECORD_OVERLAY_STRING (overlay, str, 1);
3637 #undef RECORD_OVERLAY_STRING
3639 /* Sort entries. */
3640 if (n > 1)
3641 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3643 /* Record the total number of strings to process. */
3644 it->n_overlay_strings = n;
3646 /* IT->current.overlay_string_index is the number of overlay strings
3647 that have already been consumed by IT. Copy some of the
3648 remaining overlay strings to IT->overlay_strings. */
3649 i = 0;
3650 j = it->current.overlay_string_index;
3651 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3652 it->overlay_strings[i++] = entries[j++].string;
3654 CHECK_IT (it);
3658 /* Get the first chunk of overlay strings at IT's current buffer
3659 position, or at CHARPOS if that is > 0. Value is non-zero if at
3660 least one overlay string was found. */
3662 static int
3663 get_overlay_strings (it, charpos)
3664 struct it *it;
3665 int charpos;
3667 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3668 process. This fills IT->overlay_strings with strings, and sets
3669 IT->n_overlay_strings to the total number of strings to process.
3670 IT->pos.overlay_string_index has to be set temporarily to zero
3671 because load_overlay_strings needs this; it must be set to -1
3672 when no overlay strings are found because a zero value would
3673 indicate a position in the first overlay string. */
3674 it->current.overlay_string_index = 0;
3675 load_overlay_strings (it, charpos);
3677 /* If we found overlay strings, set up IT to deliver display
3678 elements from the first one. Otherwise set up IT to deliver
3679 from current_buffer. */
3680 if (it->n_overlay_strings)
3682 /* Make sure we know settings in current_buffer, so that we can
3683 restore meaningful values when we're done with the overlay
3684 strings. */
3685 compute_stop_pos (it);
3686 xassert (it->face_id >= 0);
3688 /* Save IT's settings. They are restored after all overlay
3689 strings have been processed. */
3690 xassert (it->sp == 0);
3691 push_it (it);
3693 /* Set up IT to deliver display elements from the first overlay
3694 string. */
3695 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3696 it->string = it->overlay_strings[0];
3697 it->stop_charpos = 0;
3698 xassert (STRINGP (it->string));
3699 it->end_charpos = SCHARS (it->string);
3700 it->multibyte_p = SMBP (it->string);
3701 it->method = next_element_from_string;
3703 else
3705 it->string = Qnil;
3706 it->current.overlay_string_index = -1;
3707 it->method = next_element_from_buffer;
3710 CHECK_IT (it);
3712 /* Value is non-zero if we found at least one overlay string. */
3713 return STRINGP (it->string);
3718 /***********************************************************************
3719 Saving and restoring state
3720 ***********************************************************************/
3722 /* Save current settings of IT on IT->stack. Called, for example,
3723 before setting up IT for an overlay string, to be able to restore
3724 IT's settings to what they were after the overlay string has been
3725 processed. */
3727 static void
3728 push_it (it)
3729 struct it *it;
3731 struct iterator_stack_entry *p;
3733 xassert (it->sp < 2);
3734 p = it->stack + it->sp;
3736 p->stop_charpos = it->stop_charpos;
3737 xassert (it->face_id >= 0);
3738 p->face_id = it->face_id;
3739 p->string = it->string;
3740 p->pos = it->current;
3741 p->end_charpos = it->end_charpos;
3742 p->string_nchars = it->string_nchars;
3743 p->area = it->area;
3744 p->multibyte_p = it->multibyte_p;
3745 p->space_width = it->space_width;
3746 p->font_height = it->font_height;
3747 p->voffset = it->voffset;
3748 p->string_from_display_prop_p = it->string_from_display_prop_p;
3749 p->display_ellipsis_p = 0;
3750 ++it->sp;
3754 /* Restore IT's settings from IT->stack. Called, for example, when no
3755 more overlay strings must be processed, and we return to delivering
3756 display elements from a buffer, or when the end of a string from a
3757 `display' property is reached and we return to delivering display
3758 elements from an overlay string, or from a buffer. */
3760 static void
3761 pop_it (it)
3762 struct it *it;
3764 struct iterator_stack_entry *p;
3766 xassert (it->sp > 0);
3767 --it->sp;
3768 p = it->stack + it->sp;
3769 it->stop_charpos = p->stop_charpos;
3770 it->face_id = p->face_id;
3771 it->string = p->string;
3772 it->current = p->pos;
3773 it->end_charpos = p->end_charpos;
3774 it->string_nchars = p->string_nchars;
3775 it->area = p->area;
3776 it->multibyte_p = p->multibyte_p;
3777 it->space_width = p->space_width;
3778 it->font_height = p->font_height;
3779 it->voffset = p->voffset;
3780 it->string_from_display_prop_p = p->string_from_display_prop_p;
3785 /***********************************************************************
3786 Moving over lines
3787 ***********************************************************************/
3789 /* Set IT's current position to the previous line start. */
3791 static void
3792 back_to_previous_line_start (it)
3793 struct it *it;
3795 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3796 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3800 /* Move IT to the next line start.
3802 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3803 we skipped over part of the text (as opposed to moving the iterator
3804 continuously over the text). Otherwise, don't change the value
3805 of *SKIPPED_P.
3807 Newlines may come from buffer text, overlay strings, or strings
3808 displayed via the `display' property. That's the reason we can't
3809 simply use find_next_newline_no_quit.
3811 Note that this function may not skip over invisible text that is so
3812 because of text properties and immediately follows a newline. If
3813 it would, function reseat_at_next_visible_line_start, when called
3814 from set_iterator_to_next, would effectively make invisible
3815 characters following a newline part of the wrong glyph row, which
3816 leads to wrong cursor motion. */
3818 static int
3819 forward_to_next_line_start (it, skipped_p)
3820 struct it *it;
3821 int *skipped_p;
3823 int old_selective, newline_found_p, n;
3824 const int MAX_NEWLINE_DISTANCE = 500;
3826 /* If already on a newline, just consume it to avoid unintended
3827 skipping over invisible text below. */
3828 if (it->what == IT_CHARACTER
3829 && it->c == '\n'
3830 && CHARPOS (it->position) == IT_CHARPOS (*it))
3832 set_iterator_to_next (it, 0);
3833 it->c = 0;
3834 return 1;
3837 /* Don't handle selective display in the following. It's (a)
3838 unnecessary because it's done by the caller, and (b) leads to an
3839 infinite recursion because next_element_from_ellipsis indirectly
3840 calls this function. */
3841 old_selective = it->selective;
3842 it->selective = 0;
3844 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3845 from buffer text. */
3846 for (n = newline_found_p = 0;
3847 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3848 n += STRINGP (it->string) ? 0 : 1)
3850 if (!get_next_display_element (it))
3851 break;
3852 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3853 set_iterator_to_next (it, 0);
3856 /* If we didn't find a newline near enough, see if we can use a
3857 short-cut. */
3858 if (n == MAX_NEWLINE_DISTANCE)
3860 int start = IT_CHARPOS (*it);
3861 int limit = find_next_newline_no_quit (start, 1);
3862 Lisp_Object pos;
3864 xassert (!STRINGP (it->string));
3866 /* If there isn't any `display' property in sight, and no
3867 overlays, we can just use the position of the newline in
3868 buffer text. */
3869 if (it->stop_charpos >= limit
3870 || ((pos = Fnext_single_property_change (make_number (start),
3871 Qdisplay,
3872 Qnil, make_number (limit)),
3873 NILP (pos))
3874 && next_overlay_change (start) == ZV))
3876 IT_CHARPOS (*it) = limit;
3877 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3878 *skipped_p = newline_found_p = 1;
3880 else
3882 while (get_next_display_element (it)
3883 && !newline_found_p)
3885 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3886 set_iterator_to_next (it, 0);
3891 it->selective = old_selective;
3892 return newline_found_p;
3896 /* Set IT's current position to the previous visible line start. Skip
3897 invisible text that is so either due to text properties or due to
3898 selective display. Caution: this does not change IT->current_x and
3899 IT->hpos. */
3901 static void
3902 back_to_previous_visible_line_start (it)
3903 struct it *it;
3905 int visible_p = 0;
3907 /* Go back one newline if not on BEGV already. */
3908 if (IT_CHARPOS (*it) > BEGV)
3909 back_to_previous_line_start (it);
3911 /* Move over lines that are invisible because of selective display
3912 or text properties. */
3913 while (IT_CHARPOS (*it) > BEGV
3914 && !visible_p)
3916 visible_p = 1;
3918 /* If selective > 0, then lines indented more than that values
3919 are invisible. */
3920 if (it->selective > 0
3921 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3922 it->selective))
3923 visible_p = 0;
3924 else
3926 Lisp_Object prop;
3928 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3929 Qinvisible, it->window);
3930 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3931 visible_p = 0;
3934 /* Back one more newline if the current one is invisible. */
3935 if (!visible_p)
3936 back_to_previous_line_start (it);
3939 xassert (IT_CHARPOS (*it) >= BEGV);
3940 xassert (IT_CHARPOS (*it) == BEGV
3941 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3942 CHECK_IT (it);
3946 /* Reseat iterator IT at the previous visible line start. Skip
3947 invisible text that is so either due to text properties or due to
3948 selective display. At the end, update IT's overlay information,
3949 face information etc. */
3951 static void
3952 reseat_at_previous_visible_line_start (it)
3953 struct it *it;
3955 back_to_previous_visible_line_start (it);
3956 reseat (it, it->current.pos, 1);
3957 CHECK_IT (it);
3961 /* Reseat iterator IT on the next visible line start in the current
3962 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3963 preceding the line start. Skip over invisible text that is so
3964 because of selective display. Compute faces, overlays etc at the
3965 new position. Note that this function does not skip over text that
3966 is invisible because of text properties. */
3968 static void
3969 reseat_at_next_visible_line_start (it, on_newline_p)
3970 struct it *it;
3971 int on_newline_p;
3973 int newline_found_p, skipped_p = 0;
3975 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3977 /* Skip over lines that are invisible because they are indented
3978 more than the value of IT->selective. */
3979 if (it->selective > 0)
3980 while (IT_CHARPOS (*it) < ZV
3981 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3982 it->selective))
3984 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3985 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3988 /* Position on the newline if that's what's requested. */
3989 if (on_newline_p && newline_found_p)
3991 if (STRINGP (it->string))
3993 if (IT_STRING_CHARPOS (*it) > 0)
3995 --IT_STRING_CHARPOS (*it);
3996 --IT_STRING_BYTEPOS (*it);
3999 else if (IT_CHARPOS (*it) > BEGV)
4001 --IT_CHARPOS (*it);
4002 --IT_BYTEPOS (*it);
4003 reseat (it, it->current.pos, 0);
4006 else if (skipped_p)
4007 reseat (it, it->current.pos, 0);
4009 CHECK_IT (it);
4014 /***********************************************************************
4015 Changing an iterator's position
4016 ***********************************************************************/
4018 /* Change IT's current position to POS in current_buffer. If FORCE_P
4019 is non-zero, always check for text properties at the new position.
4020 Otherwise, text properties are only looked up if POS >=
4021 IT->check_charpos of a property. */
4023 static void
4024 reseat (it, pos, force_p)
4025 struct it *it;
4026 struct text_pos pos;
4027 int force_p;
4029 int original_pos = IT_CHARPOS (*it);
4031 reseat_1 (it, pos, 0);
4033 /* Determine where to check text properties. Avoid doing it
4034 where possible because text property lookup is very expensive. */
4035 if (force_p
4036 || CHARPOS (pos) > it->stop_charpos
4037 || CHARPOS (pos) < original_pos)
4038 handle_stop (it);
4040 CHECK_IT (it);
4044 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4045 IT->stop_pos to POS, also. */
4047 static void
4048 reseat_1 (it, pos, set_stop_p)
4049 struct it *it;
4050 struct text_pos pos;
4051 int set_stop_p;
4053 /* Don't call this function when scanning a C string. */
4054 xassert (it->s == NULL);
4056 /* POS must be a reasonable value. */
4057 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4059 it->current.pos = it->position = pos;
4060 XSETBUFFER (it->object, current_buffer);
4061 it->end_charpos = ZV;
4062 it->dpvec = NULL;
4063 it->current.dpvec_index = -1;
4064 it->current.overlay_string_index = -1;
4065 IT_STRING_CHARPOS (*it) = -1;
4066 IT_STRING_BYTEPOS (*it) = -1;
4067 it->string = Qnil;
4068 it->method = next_element_from_buffer;
4069 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4070 it->sp = 0;
4071 it->face_before_selective_p = 0;
4073 if (set_stop_p)
4074 it->stop_charpos = CHARPOS (pos);
4078 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4079 If S is non-null, it is a C string to iterate over. Otherwise,
4080 STRING gives a Lisp string to iterate over.
4082 If PRECISION > 0, don't return more then PRECISION number of
4083 characters from the string.
4085 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4086 characters have been returned. FIELD_WIDTH < 0 means an infinite
4087 field width.
4089 MULTIBYTE = 0 means disable processing of multibyte characters,
4090 MULTIBYTE > 0 means enable it,
4091 MULTIBYTE < 0 means use IT->multibyte_p.
4093 IT must be initialized via a prior call to init_iterator before
4094 calling this function. */
4096 static void
4097 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4098 struct it *it;
4099 unsigned char *s;
4100 Lisp_Object string;
4101 int charpos;
4102 int precision, field_width, multibyte;
4104 /* No region in strings. */
4105 it->region_beg_charpos = it->region_end_charpos = -1;
4107 /* No text property checks performed by default, but see below. */
4108 it->stop_charpos = -1;
4110 /* Set iterator position and end position. */
4111 bzero (&it->current, sizeof it->current);
4112 it->current.overlay_string_index = -1;
4113 it->current.dpvec_index = -1;
4114 xassert (charpos >= 0);
4116 /* If STRING is specified, use its multibyteness, otherwise use the
4117 setting of MULTIBYTE, if specified. */
4118 if (multibyte >= 0)
4119 it->multibyte_p = multibyte > 0;
4121 if (s == NULL)
4123 xassert (STRINGP (string));
4124 it->string = string;
4125 it->s = NULL;
4126 it->end_charpos = it->string_nchars = SCHARS (string);
4127 it->method = next_element_from_string;
4128 it->current.string_pos = string_pos (charpos, string);
4130 else
4132 it->s = s;
4133 it->string = Qnil;
4135 /* Note that we use IT->current.pos, not it->current.string_pos,
4136 for displaying C strings. */
4137 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4138 if (it->multibyte_p)
4140 it->current.pos = c_string_pos (charpos, s, 1);
4141 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4143 else
4145 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4146 it->end_charpos = it->string_nchars = strlen (s);
4149 it->method = next_element_from_c_string;
4152 /* PRECISION > 0 means don't return more than PRECISION characters
4153 from the string. */
4154 if (precision > 0 && it->end_charpos - charpos > precision)
4155 it->end_charpos = it->string_nchars = charpos + precision;
4157 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4158 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4159 FIELD_WIDTH < 0 means infinite field width. This is useful for
4160 padding with `-' at the end of a mode line. */
4161 if (field_width < 0)
4162 field_width = INFINITY;
4163 if (field_width > it->end_charpos - charpos)
4164 it->end_charpos = charpos + field_width;
4166 /* Use the standard display table for displaying strings. */
4167 if (DISP_TABLE_P (Vstandard_display_table))
4168 it->dp = XCHAR_TABLE (Vstandard_display_table);
4170 it->stop_charpos = charpos;
4171 CHECK_IT (it);
4176 /***********************************************************************
4177 Iteration
4178 ***********************************************************************/
4180 /* Load IT's display element fields with information about the next
4181 display element from the current position of IT. Value is zero if
4182 end of buffer (or C string) is reached. */
4185 get_next_display_element (it)
4186 struct it *it;
4188 /* Non-zero means that we found an display element. Zero means that
4189 we hit the end of what we iterate over. Performance note: the
4190 function pointer `method' used here turns out to be faster than
4191 using a sequence of if-statements. */
4192 int success_p = (*it->method) (it);
4194 if (it->what == IT_CHARACTER)
4196 /* Map via display table or translate control characters.
4197 IT->c, IT->len etc. have been set to the next character by
4198 the function call above. If we have a display table, and it
4199 contains an entry for IT->c, translate it. Don't do this if
4200 IT->c itself comes from a display table, otherwise we could
4201 end up in an infinite recursion. (An alternative could be to
4202 count the recursion depth of this function and signal an
4203 error when a certain maximum depth is reached.) Is it worth
4204 it? */
4205 if (success_p && it->dpvec == NULL)
4207 Lisp_Object dv;
4209 if (it->dp
4210 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4211 VECTORP (dv)))
4213 struct Lisp_Vector *v = XVECTOR (dv);
4215 /* Return the first character from the display table
4216 entry, if not empty. If empty, don't display the
4217 current character. */
4218 if (v->size)
4220 it->dpvec_char_len = it->len;
4221 it->dpvec = v->contents;
4222 it->dpend = v->contents + v->size;
4223 it->current.dpvec_index = 0;
4224 it->method = next_element_from_display_vector;
4225 success_p = get_next_display_element (it);
4227 else
4229 set_iterator_to_next (it, 0);
4230 success_p = get_next_display_element (it);
4234 /* Translate control characters into `\003' or `^C' form.
4235 Control characters coming from a display table entry are
4236 currently not translated because we use IT->dpvec to hold
4237 the translation. This could easily be changed but I
4238 don't believe that it is worth doing.
4240 Non-printable multibyte characters are also translated
4241 octal form. */
4242 else if ((it->c < ' '
4243 && (it->area != TEXT_AREA
4244 || (it->c != '\n' && it->c != '\t')))
4245 || (it->c >= 127
4246 && it->len == 1)
4247 || !CHAR_PRINTABLE_P (it->c))
4249 /* IT->c is a control character which must be displayed
4250 either as '\003' or as `^C' where the '\\' and '^'
4251 can be defined in the display table. Fill
4252 IT->ctl_chars with glyphs for what we have to
4253 display. Then, set IT->dpvec to these glyphs. */
4254 GLYPH g;
4256 if (it->c < 128 && it->ctl_arrow_p)
4258 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4259 if (it->dp
4260 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4261 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4262 g = XINT (DISP_CTRL_GLYPH (it->dp));
4263 else
4264 g = FAST_MAKE_GLYPH ('^', 0);
4265 XSETINT (it->ctl_chars[0], g);
4267 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4268 XSETINT (it->ctl_chars[1], g);
4270 /* Set up IT->dpvec and return first character from it. */
4271 it->dpvec_char_len = it->len;
4272 it->dpvec = it->ctl_chars;
4273 it->dpend = it->dpvec + 2;
4274 it->current.dpvec_index = 0;
4275 it->method = next_element_from_display_vector;
4276 get_next_display_element (it);
4278 else
4280 unsigned char str[MAX_MULTIBYTE_LENGTH];
4281 int len;
4282 int i;
4283 GLYPH escape_glyph;
4285 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4286 if (it->dp
4287 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4288 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4289 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4290 else
4291 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4293 if (SINGLE_BYTE_CHAR_P (it->c))
4294 str[0] = it->c, len = 1;
4295 else
4297 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4298 if (len < 0)
4300 /* It's an invalid character, which
4301 shouldn't happen actually, but due to
4302 bugs it may happen. Let's print the char
4303 as is, there's not much meaningful we can
4304 do with it. */
4305 str[0] = it->c;
4306 str[1] = it->c >> 8;
4307 str[2] = it->c >> 16;
4308 str[3] = it->c >> 24;
4309 len = 4;
4313 for (i = 0; i < len; i++)
4315 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4316 /* Insert three more glyphs into IT->ctl_chars for
4317 the octal display of the character. */
4318 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4319 XSETINT (it->ctl_chars[i * 4 + 1], g);
4320 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4321 XSETINT (it->ctl_chars[i * 4 + 2], g);
4322 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4323 XSETINT (it->ctl_chars[i * 4 + 3], g);
4326 /* Set up IT->dpvec and return the first character
4327 from it. */
4328 it->dpvec_char_len = it->len;
4329 it->dpvec = it->ctl_chars;
4330 it->dpend = it->dpvec + len * 4;
4331 it->current.dpvec_index = 0;
4332 it->method = next_element_from_display_vector;
4333 get_next_display_element (it);
4338 /* Adjust face id for a multibyte character. There are no
4339 multibyte character in unibyte text. */
4340 if (it->multibyte_p
4341 && success_p
4342 && FRAME_WINDOW_P (it->f))
4344 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4345 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4349 /* Is this character the last one of a run of characters with
4350 box? If yes, set IT->end_of_box_run_p to 1. */
4351 if (it->face_box_p
4352 && it->s == NULL)
4354 int face_id;
4355 struct face *face;
4357 it->end_of_box_run_p
4358 = ((face_id = face_after_it_pos (it),
4359 face_id != it->face_id)
4360 && (face = FACE_FROM_ID (it->f, face_id),
4361 face->box == FACE_NO_BOX));
4364 /* Value is 0 if end of buffer or string reached. */
4365 return success_p;
4369 /* Move IT to the next display element.
4371 RESEAT_P non-zero means if called on a newline in buffer text,
4372 skip to the next visible line start.
4374 Functions get_next_display_element and set_iterator_to_next are
4375 separate because I find this arrangement easier to handle than a
4376 get_next_display_element function that also increments IT's
4377 position. The way it is we can first look at an iterator's current
4378 display element, decide whether it fits on a line, and if it does,
4379 increment the iterator position. The other way around we probably
4380 would either need a flag indicating whether the iterator has to be
4381 incremented the next time, or we would have to implement a
4382 decrement position function which would not be easy to write. */
4384 void
4385 set_iterator_to_next (it, reseat_p)
4386 struct it *it;
4387 int reseat_p;
4389 /* Reset flags indicating start and end of a sequence of characters
4390 with box. Reset them at the start of this function because
4391 moving the iterator to a new position might set them. */
4392 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4394 if (it->method == next_element_from_buffer)
4396 /* The current display element of IT is a character from
4397 current_buffer. Advance in the buffer, and maybe skip over
4398 invisible lines that are so because of selective display. */
4399 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4400 reseat_at_next_visible_line_start (it, 0);
4401 else
4403 xassert (it->len != 0);
4404 IT_BYTEPOS (*it) += it->len;
4405 IT_CHARPOS (*it) += 1;
4406 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4409 else if (it->method == next_element_from_composition)
4411 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4412 if (STRINGP (it->string))
4414 IT_STRING_BYTEPOS (*it) += it->len;
4415 IT_STRING_CHARPOS (*it) += it->cmp_len;
4416 it->method = next_element_from_string;
4417 goto consider_string_end;
4419 else
4421 IT_BYTEPOS (*it) += it->len;
4422 IT_CHARPOS (*it) += it->cmp_len;
4423 it->method = next_element_from_buffer;
4426 else if (it->method == next_element_from_c_string)
4428 /* Current display element of IT is from a C string. */
4429 IT_BYTEPOS (*it) += it->len;
4430 IT_CHARPOS (*it) += 1;
4432 else if (it->method == next_element_from_display_vector)
4434 /* Current display element of IT is from a display table entry.
4435 Advance in the display table definition. Reset it to null if
4436 end reached, and continue with characters from buffers/
4437 strings. */
4438 ++it->current.dpvec_index;
4440 /* Restore face of the iterator to what they were before the
4441 display vector entry (these entries may contain faces). */
4442 it->face_id = it->saved_face_id;
4444 if (it->dpvec + it->current.dpvec_index == it->dpend)
4446 if (it->s)
4447 it->method = next_element_from_c_string;
4448 else if (STRINGP (it->string))
4449 it->method = next_element_from_string;
4450 else
4451 it->method = next_element_from_buffer;
4453 it->dpvec = NULL;
4454 it->current.dpvec_index = -1;
4456 /* Skip over characters which were displayed via IT->dpvec. */
4457 if (it->dpvec_char_len < 0)
4458 reseat_at_next_visible_line_start (it, 1);
4459 else if (it->dpvec_char_len > 0)
4461 it->len = it->dpvec_char_len;
4462 set_iterator_to_next (it, reseat_p);
4466 else if (it->method == next_element_from_string)
4468 /* Current display element is a character from a Lisp string. */
4469 xassert (it->s == NULL && STRINGP (it->string));
4470 IT_STRING_BYTEPOS (*it) += it->len;
4471 IT_STRING_CHARPOS (*it) += 1;
4473 consider_string_end:
4475 if (it->current.overlay_string_index >= 0)
4477 /* IT->string is an overlay string. Advance to the
4478 next, if there is one. */
4479 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4480 next_overlay_string (it);
4482 else
4484 /* IT->string is not an overlay string. If we reached
4485 its end, and there is something on IT->stack, proceed
4486 with what is on the stack. This can be either another
4487 string, this time an overlay string, or a buffer. */
4488 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4489 && it->sp > 0)
4491 pop_it (it);
4492 if (!STRINGP (it->string))
4493 it->method = next_element_from_buffer;
4494 else
4495 goto consider_string_end;
4499 else if (it->method == next_element_from_image
4500 || it->method == next_element_from_stretch)
4502 /* The position etc with which we have to proceed are on
4503 the stack. The position may be at the end of a string,
4504 if the `display' property takes up the whole string. */
4505 pop_it (it);
4506 it->image_id = 0;
4507 if (STRINGP (it->string))
4509 it->method = next_element_from_string;
4510 goto consider_string_end;
4512 else
4513 it->method = next_element_from_buffer;
4515 else
4516 /* There are no other methods defined, so this should be a bug. */
4517 abort ();
4519 xassert (it->method != next_element_from_string
4520 || (STRINGP (it->string)
4521 && IT_STRING_CHARPOS (*it) >= 0));
4525 /* Load IT's display element fields with information about the next
4526 display element which comes from a display table entry or from the
4527 result of translating a control character to one of the forms `^C'
4528 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4530 static int
4531 next_element_from_display_vector (it)
4532 struct it *it;
4534 /* Precondition. */
4535 xassert (it->dpvec && it->current.dpvec_index >= 0);
4537 /* Remember the current face id in case glyphs specify faces.
4538 IT's face is restored in set_iterator_to_next. */
4539 it->saved_face_id = it->face_id;
4541 if (INTEGERP (*it->dpvec)
4542 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4544 int lface_id;
4545 GLYPH g;
4547 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4548 it->c = FAST_GLYPH_CHAR (g);
4549 it->len = CHAR_BYTES (it->c);
4551 /* The entry may contain a face id to use. Such a face id is
4552 the id of a Lisp face, not a realized face. A face id of
4553 zero means no face is specified. */
4554 lface_id = FAST_GLYPH_FACE (g);
4555 if (lface_id)
4557 /* The function returns -1 if lface_id is invalid. */
4558 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4559 if (face_id >= 0)
4560 it->face_id = face_id;
4563 else
4564 /* Display table entry is invalid. Return a space. */
4565 it->c = ' ', it->len = 1;
4567 /* Don't change position and object of the iterator here. They are
4568 still the values of the character that had this display table
4569 entry or was translated, and that's what we want. */
4570 it->what = IT_CHARACTER;
4571 return 1;
4575 /* Load IT with the next display element from Lisp string IT->string.
4576 IT->current.string_pos is the current position within the string.
4577 If IT->current.overlay_string_index >= 0, the Lisp string is an
4578 overlay string. */
4580 static int
4581 next_element_from_string (it)
4582 struct it *it;
4584 struct text_pos position;
4586 xassert (STRINGP (it->string));
4587 xassert (IT_STRING_CHARPOS (*it) >= 0);
4588 position = it->current.string_pos;
4590 /* Time to check for invisible text? */
4591 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4592 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4594 handle_stop (it);
4596 /* Since a handler may have changed IT->method, we must
4597 recurse here. */
4598 return get_next_display_element (it);
4601 if (it->current.overlay_string_index >= 0)
4603 /* Get the next character from an overlay string. In overlay
4604 strings, There is no field width or padding with spaces to
4605 do. */
4606 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4608 it->what = IT_EOB;
4609 return 0;
4611 else if (STRING_MULTIBYTE (it->string))
4613 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4614 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4615 it->c = string_char_and_length (s, remaining, &it->len);
4617 else
4619 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4620 it->len = 1;
4623 else
4625 /* Get the next character from a Lisp string that is not an
4626 overlay string. Such strings come from the mode line, for
4627 example. We may have to pad with spaces, or truncate the
4628 string. See also next_element_from_c_string. */
4629 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4631 it->what = IT_EOB;
4632 return 0;
4634 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4636 /* Pad with spaces. */
4637 it->c = ' ', it->len = 1;
4638 CHARPOS (position) = BYTEPOS (position) = -1;
4640 else if (STRING_MULTIBYTE (it->string))
4642 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4643 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4644 it->c = string_char_and_length (s, maxlen, &it->len);
4646 else
4648 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4649 it->len = 1;
4653 /* Record what we have and where it came from. Note that we store a
4654 buffer position in IT->position although it could arguably be a
4655 string position. */
4656 it->what = IT_CHARACTER;
4657 it->object = it->string;
4658 it->position = position;
4659 return 1;
4663 /* Load IT with next display element from C string IT->s.
4664 IT->string_nchars is the maximum number of characters to return
4665 from the string. IT->end_charpos may be greater than
4666 IT->string_nchars when this function is called, in which case we
4667 may have to return padding spaces. Value is zero if end of string
4668 reached, including padding spaces. */
4670 static int
4671 next_element_from_c_string (it)
4672 struct it *it;
4674 int success_p = 1;
4676 xassert (it->s);
4677 it->what = IT_CHARACTER;
4678 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4679 it->object = Qnil;
4681 /* IT's position can be greater IT->string_nchars in case a field
4682 width or precision has been specified when the iterator was
4683 initialized. */
4684 if (IT_CHARPOS (*it) >= it->end_charpos)
4686 /* End of the game. */
4687 it->what = IT_EOB;
4688 success_p = 0;
4690 else if (IT_CHARPOS (*it) >= it->string_nchars)
4692 /* Pad with spaces. */
4693 it->c = ' ', it->len = 1;
4694 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4696 else if (it->multibyte_p)
4698 /* Implementation note: The calls to strlen apparently aren't a
4699 performance problem because there is no noticeable performance
4700 difference between Emacs running in unibyte or multibyte mode. */
4701 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4702 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4703 maxlen, &it->len);
4705 else
4706 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4708 return success_p;
4712 /* Set up IT to return characters from an ellipsis, if appropriate.
4713 The definition of the ellipsis glyphs may come from a display table
4714 entry. This function Fills IT with the first glyph from the
4715 ellipsis if an ellipsis is to be displayed. */
4717 static int
4718 next_element_from_ellipsis (it)
4719 struct it *it;
4721 if (it->selective_display_ellipsis_p)
4723 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4725 /* Use the display table definition for `...'. Invalid glyphs
4726 will be handled by the method returning elements from dpvec. */
4727 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4728 it->dpvec_char_len = it->len;
4729 it->dpvec = v->contents;
4730 it->dpend = v->contents + v->size;
4731 it->current.dpvec_index = 0;
4732 it->method = next_element_from_display_vector;
4734 else
4736 /* Use default `...' which is stored in default_invis_vector. */
4737 it->dpvec_char_len = it->len;
4738 it->dpvec = default_invis_vector;
4739 it->dpend = default_invis_vector + 3;
4740 it->current.dpvec_index = 0;
4741 it->method = next_element_from_display_vector;
4744 else
4746 /* The face at the current position may be different from the
4747 face we find after the invisible text. Remember what it
4748 was in IT->saved_face_id, and signal that it's there by
4749 setting face_before_selective_p. */
4750 it->saved_face_id = it->face_id;
4751 it->method = next_element_from_buffer;
4752 reseat_at_next_visible_line_start (it, 1);
4753 it->face_before_selective_p = 1;
4756 return get_next_display_element (it);
4760 /* Deliver an image display element. The iterator IT is already
4761 filled with image information (done in handle_display_prop). Value
4762 is always 1. */
4765 static int
4766 next_element_from_image (it)
4767 struct it *it;
4769 it->what = IT_IMAGE;
4770 return 1;
4774 /* Fill iterator IT with next display element from a stretch glyph
4775 property. IT->object is the value of the text property. Value is
4776 always 1. */
4778 static int
4779 next_element_from_stretch (it)
4780 struct it *it;
4782 it->what = IT_STRETCH;
4783 return 1;
4787 /* Load IT with the next display element from current_buffer. Value
4788 is zero if end of buffer reached. IT->stop_charpos is the next
4789 position at which to stop and check for text properties or buffer
4790 end. */
4792 static int
4793 next_element_from_buffer (it)
4794 struct it *it;
4796 int success_p = 1;
4798 /* Check this assumption, otherwise, we would never enter the
4799 if-statement, below. */
4800 xassert (IT_CHARPOS (*it) >= BEGV
4801 && IT_CHARPOS (*it) <= it->stop_charpos);
4803 if (IT_CHARPOS (*it) >= it->stop_charpos)
4805 if (IT_CHARPOS (*it) >= it->end_charpos)
4807 int overlay_strings_follow_p;
4809 /* End of the game, except when overlay strings follow that
4810 haven't been returned yet. */
4811 if (it->overlay_strings_at_end_processed_p)
4812 overlay_strings_follow_p = 0;
4813 else
4815 it->overlay_strings_at_end_processed_p = 1;
4816 overlay_strings_follow_p = get_overlay_strings (it, 0);
4819 if (overlay_strings_follow_p)
4820 success_p = get_next_display_element (it);
4821 else
4823 it->what = IT_EOB;
4824 it->position = it->current.pos;
4825 success_p = 0;
4828 else
4830 handle_stop (it);
4831 return get_next_display_element (it);
4834 else
4836 /* No face changes, overlays etc. in sight, so just return a
4837 character from current_buffer. */
4838 unsigned char *p;
4840 /* Maybe run the redisplay end trigger hook. Performance note:
4841 This doesn't seem to cost measurable time. */
4842 if (it->redisplay_end_trigger_charpos
4843 && it->glyph_row
4844 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4845 run_redisplay_end_trigger_hook (it);
4847 /* Get the next character, maybe multibyte. */
4848 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4849 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4851 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4852 - IT_BYTEPOS (*it));
4853 it->c = string_char_and_length (p, maxlen, &it->len);
4855 else
4856 it->c = *p, it->len = 1;
4858 /* Record what we have and where it came from. */
4859 it->what = IT_CHARACTER;;
4860 it->object = it->w->buffer;
4861 it->position = it->current.pos;
4863 /* Normally we return the character found above, except when we
4864 really want to return an ellipsis for selective display. */
4865 if (it->selective)
4867 if (it->c == '\n')
4869 /* A value of selective > 0 means hide lines indented more
4870 than that number of columns. */
4871 if (it->selective > 0
4872 && IT_CHARPOS (*it) + 1 < ZV
4873 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4874 IT_BYTEPOS (*it) + 1,
4875 it->selective))
4877 success_p = next_element_from_ellipsis (it);
4878 it->dpvec_char_len = -1;
4881 else if (it->c == '\r' && it->selective == -1)
4883 /* A value of selective == -1 means that everything from the
4884 CR to the end of the line is invisible, with maybe an
4885 ellipsis displayed for it. */
4886 success_p = next_element_from_ellipsis (it);
4887 it->dpvec_char_len = -1;
4892 /* Value is zero if end of buffer reached. */
4893 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4894 return success_p;
4898 /* Run the redisplay end trigger hook for IT. */
4900 static void
4901 run_redisplay_end_trigger_hook (it)
4902 struct it *it;
4904 Lisp_Object args[3];
4906 /* IT->glyph_row should be non-null, i.e. we should be actually
4907 displaying something, or otherwise we should not run the hook. */
4908 xassert (it->glyph_row);
4910 /* Set up hook arguments. */
4911 args[0] = Qredisplay_end_trigger_functions;
4912 args[1] = it->window;
4913 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4914 it->redisplay_end_trigger_charpos = 0;
4916 /* Since we are *trying* to run these functions, don't try to run
4917 them again, even if they get an error. */
4918 it->w->redisplay_end_trigger = Qnil;
4919 Frun_hook_with_args (3, args);
4921 /* Notice if it changed the face of the character we are on. */
4922 handle_face_prop (it);
4926 /* Deliver a composition display element. The iterator IT is already
4927 filled with composition information (done in
4928 handle_composition_prop). Value is always 1. */
4930 static int
4931 next_element_from_composition (it)
4932 struct it *it;
4934 it->what = IT_COMPOSITION;
4935 it->position = (STRINGP (it->string)
4936 ? it->current.string_pos
4937 : it->current.pos);
4938 return 1;
4943 /***********************************************************************
4944 Moving an iterator without producing glyphs
4945 ***********************************************************************/
4947 /* Move iterator IT to a specified buffer or X position within one
4948 line on the display without producing glyphs.
4950 OP should be a bit mask including some or all of these bits:
4951 MOVE_TO_X: Stop on reaching x-position TO_X.
4952 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4953 Regardless of OP's value, stop in reaching the end of the display line.
4955 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4956 This means, in particular, that TO_X includes window's horizontal
4957 scroll amount.
4959 The return value has several possible values that
4960 say what condition caused the scan to stop:
4962 MOVE_POS_MATCH_OR_ZV
4963 - when TO_POS or ZV was reached.
4965 MOVE_X_REACHED
4966 -when TO_X was reached before TO_POS or ZV were reached.
4968 MOVE_LINE_CONTINUED
4969 - when we reached the end of the display area and the line must
4970 be continued.
4972 MOVE_LINE_TRUNCATED
4973 - when we reached the end of the display area and the line is
4974 truncated.
4976 MOVE_NEWLINE_OR_CR
4977 - when we stopped at a line end, i.e. a newline or a CR and selective
4978 display is on. */
4980 static enum move_it_result
4981 move_it_in_display_line_to (it, to_charpos, to_x, op)
4982 struct it *it;
4983 int to_charpos, to_x, op;
4985 enum move_it_result result = MOVE_UNDEFINED;
4986 struct glyph_row *saved_glyph_row;
4988 /* Don't produce glyphs in produce_glyphs. */
4989 saved_glyph_row = it->glyph_row;
4990 it->glyph_row = NULL;
4992 while (1)
4994 int x, i, ascent = 0, descent = 0;
4996 /* Stop when ZV or TO_CHARPOS reached. */
4997 if (!get_next_display_element (it)
4998 || ((op & MOVE_TO_POS) != 0
4999 && BUFFERP (it->object)
5000 && IT_CHARPOS (*it) >= to_charpos))
5002 result = MOVE_POS_MATCH_OR_ZV;
5003 break;
5006 /* The call to produce_glyphs will get the metrics of the
5007 display element IT is loaded with. We record in x the
5008 x-position before this display element in case it does not
5009 fit on the line. */
5010 x = it->current_x;
5012 /* Remember the line height so far in case the next element doesn't
5013 fit on the line. */
5014 if (!it->truncate_lines_p)
5016 ascent = it->max_ascent;
5017 descent = it->max_descent;
5020 PRODUCE_GLYPHS (it);
5022 if (it->area != TEXT_AREA)
5024 set_iterator_to_next (it, 1);
5025 continue;
5028 /* The number of glyphs we get back in IT->nglyphs will normally
5029 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5030 character on a terminal frame, or (iii) a line end. For the
5031 second case, IT->nglyphs - 1 padding glyphs will be present
5032 (on X frames, there is only one glyph produced for a
5033 composite character.
5035 The behavior implemented below means, for continuation lines,
5036 that as many spaces of a TAB as fit on the current line are
5037 displayed there. For terminal frames, as many glyphs of a
5038 multi-glyph character are displayed in the current line, too.
5039 This is what the old redisplay code did, and we keep it that
5040 way. Under X, the whole shape of a complex character must
5041 fit on the line or it will be completely displayed in the
5042 next line.
5044 Note that both for tabs and padding glyphs, all glyphs have
5045 the same width. */
5046 if (it->nglyphs)
5048 /* More than one glyph or glyph doesn't fit on line. All
5049 glyphs have the same width. */
5050 int single_glyph_width = it->pixel_width / it->nglyphs;
5051 int new_x;
5053 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5055 new_x = x + single_glyph_width;
5057 /* We want to leave anything reaching TO_X to the caller. */
5058 if ((op & MOVE_TO_X) && new_x > to_x)
5060 it->current_x = x;
5061 result = MOVE_X_REACHED;
5062 break;
5064 else if (/* Lines are continued. */
5065 !it->truncate_lines_p
5066 && (/* And glyph doesn't fit on the line. */
5067 new_x > it->last_visible_x
5068 /* Or it fits exactly and we're on a window
5069 system frame. */
5070 || (new_x == it->last_visible_x
5071 && FRAME_WINDOW_P (it->f))))
5073 if (/* IT->hpos == 0 means the very first glyph
5074 doesn't fit on the line, e.g. a wide image. */
5075 it->hpos == 0
5076 || (new_x == it->last_visible_x
5077 && FRAME_WINDOW_P (it->f)))
5079 ++it->hpos;
5080 it->current_x = new_x;
5081 if (i == it->nglyphs - 1)
5082 set_iterator_to_next (it, 1);
5084 else
5086 it->current_x = x;
5087 it->max_ascent = ascent;
5088 it->max_descent = descent;
5091 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5092 IT_CHARPOS (*it)));
5093 result = MOVE_LINE_CONTINUED;
5094 break;
5096 else if (new_x > it->first_visible_x)
5098 /* Glyph is visible. Increment number of glyphs that
5099 would be displayed. */
5100 ++it->hpos;
5102 else
5104 /* Glyph is completely off the left margin of the display
5105 area. Nothing to do. */
5109 if (result != MOVE_UNDEFINED)
5110 break;
5112 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5114 /* Stop when TO_X specified and reached. This check is
5115 necessary here because of lines consisting of a line end,
5116 only. The line end will not produce any glyphs and we
5117 would never get MOVE_X_REACHED. */
5118 xassert (it->nglyphs == 0);
5119 result = MOVE_X_REACHED;
5120 break;
5123 /* Is this a line end? If yes, we're done. */
5124 if (ITERATOR_AT_END_OF_LINE_P (it))
5126 result = MOVE_NEWLINE_OR_CR;
5127 break;
5130 /* The current display element has been consumed. Advance
5131 to the next. */
5132 set_iterator_to_next (it, 1);
5134 /* Stop if lines are truncated and IT's current x-position is
5135 past the right edge of the window now. */
5136 if (it->truncate_lines_p
5137 && it->current_x >= it->last_visible_x)
5139 result = MOVE_LINE_TRUNCATED;
5140 break;
5144 /* Restore the iterator settings altered at the beginning of this
5145 function. */
5146 it->glyph_row = saved_glyph_row;
5147 return result;
5151 /* Move IT forward until it satisfies one or more of the criteria in
5152 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5154 OP is a bit-mask that specifies where to stop, and in particular,
5155 which of those four position arguments makes a difference. See the
5156 description of enum move_operation_enum.
5158 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5159 screen line, this function will set IT to the next position >
5160 TO_CHARPOS. */
5162 void
5163 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5164 struct it *it;
5165 int to_charpos, to_x, to_y, to_vpos;
5166 int op;
5168 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5169 int line_height;
5170 int reached = 0;
5172 for (;;)
5174 if (op & MOVE_TO_VPOS)
5176 /* If no TO_CHARPOS and no TO_X specified, stop at the
5177 start of the line TO_VPOS. */
5178 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5180 if (it->vpos == to_vpos)
5182 reached = 1;
5183 break;
5185 else
5186 skip = move_it_in_display_line_to (it, -1, -1, 0);
5188 else
5190 /* TO_VPOS >= 0 means stop at TO_X in the line at
5191 TO_VPOS, or at TO_POS, whichever comes first. */
5192 if (it->vpos == to_vpos)
5194 reached = 2;
5195 break;
5198 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5200 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5202 reached = 3;
5203 break;
5205 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5207 /* We have reached TO_X but not in the line we want. */
5208 skip = move_it_in_display_line_to (it, to_charpos,
5209 -1, MOVE_TO_POS);
5210 if (skip == MOVE_POS_MATCH_OR_ZV)
5212 reached = 4;
5213 break;
5218 else if (op & MOVE_TO_Y)
5220 struct it it_backup;
5222 /* TO_Y specified means stop at TO_X in the line containing
5223 TO_Y---or at TO_CHARPOS if this is reached first. The
5224 problem is that we can't really tell whether the line
5225 contains TO_Y before we have completely scanned it, and
5226 this may skip past TO_X. What we do is to first scan to
5227 TO_X.
5229 If TO_X is not specified, use a TO_X of zero. The reason
5230 is to make the outcome of this function more predictable.
5231 If we didn't use TO_X == 0, we would stop at the end of
5232 the line which is probably not what a caller would expect
5233 to happen. */
5234 skip = move_it_in_display_line_to (it, to_charpos,
5235 ((op & MOVE_TO_X)
5236 ? to_x : 0),
5237 (MOVE_TO_X
5238 | (op & MOVE_TO_POS)));
5240 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5241 if (skip == MOVE_POS_MATCH_OR_ZV)
5243 reached = 5;
5244 break;
5247 /* If TO_X was reached, we would like to know whether TO_Y
5248 is in the line. This can only be said if we know the
5249 total line height which requires us to scan the rest of
5250 the line. */
5251 if (skip == MOVE_X_REACHED)
5253 it_backup = *it;
5254 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5255 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5256 op & MOVE_TO_POS);
5257 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5260 /* Now, decide whether TO_Y is in this line. */
5261 line_height = it->max_ascent + it->max_descent;
5262 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5264 if (to_y >= it->current_y
5265 && to_y < it->current_y + line_height)
5267 if (skip == MOVE_X_REACHED)
5268 /* If TO_Y is in this line and TO_X was reached above,
5269 we scanned too far. We have to restore IT's settings
5270 to the ones before skipping. */
5271 *it = it_backup;
5272 reached = 6;
5274 else if (skip == MOVE_X_REACHED)
5276 skip = skip2;
5277 if (skip == MOVE_POS_MATCH_OR_ZV)
5278 reached = 7;
5281 if (reached)
5282 break;
5284 else
5285 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5287 switch (skip)
5289 case MOVE_POS_MATCH_OR_ZV:
5290 reached = 8;
5291 goto out;
5293 case MOVE_NEWLINE_OR_CR:
5294 set_iterator_to_next (it, 1);
5295 it->continuation_lines_width = 0;
5296 break;
5298 case MOVE_LINE_TRUNCATED:
5299 it->continuation_lines_width = 0;
5300 reseat_at_next_visible_line_start (it, 0);
5301 if ((op & MOVE_TO_POS) != 0
5302 && IT_CHARPOS (*it) > to_charpos)
5304 reached = 9;
5305 goto out;
5307 break;
5309 case MOVE_LINE_CONTINUED:
5310 it->continuation_lines_width += it->current_x;
5311 break;
5313 default:
5314 abort ();
5317 /* Reset/increment for the next run. */
5318 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5319 it->current_x = it->hpos = 0;
5320 it->current_y += it->max_ascent + it->max_descent;
5321 ++it->vpos;
5322 last_height = it->max_ascent + it->max_descent;
5323 last_max_ascent = it->max_ascent;
5324 it->max_ascent = it->max_descent = 0;
5327 out:
5329 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5333 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5335 If DY > 0, move IT backward at least that many pixels. DY = 0
5336 means move IT backward to the preceding line start or BEGV. This
5337 function may move over more than DY pixels if IT->current_y - DY
5338 ends up in the middle of a line; in this case IT->current_y will be
5339 set to the top of the line moved to. */
5341 void
5342 move_it_vertically_backward (it, dy)
5343 struct it *it;
5344 int dy;
5346 int nlines, h;
5347 struct it it2, it3;
5348 int start_pos = IT_CHARPOS (*it);
5350 xassert (dy >= 0);
5352 /* Estimate how many newlines we must move back. */
5353 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5355 /* Set the iterator's position that many lines back. */
5356 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5357 back_to_previous_visible_line_start (it);
5359 /* Reseat the iterator here. When moving backward, we don't want
5360 reseat to skip forward over invisible text, set up the iterator
5361 to deliver from overlay strings at the new position etc. So,
5362 use reseat_1 here. */
5363 reseat_1 (it, it->current.pos, 1);
5365 /* We are now surely at a line start. */
5366 it->current_x = it->hpos = 0;
5368 /* Move forward and see what y-distance we moved. First move to the
5369 start of the next line so that we get its height. We need this
5370 height to be able to tell whether we reached the specified
5371 y-distance. */
5372 it2 = *it;
5373 it2.max_ascent = it2.max_descent = 0;
5374 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5375 MOVE_TO_POS | MOVE_TO_VPOS);
5376 xassert (IT_CHARPOS (*it) >= BEGV);
5377 it3 = it2;
5379 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5380 xassert (IT_CHARPOS (*it) >= BEGV);
5381 h = it2.current_y - it->current_y;
5382 nlines = it2.vpos - it->vpos;
5384 /* Correct IT's y and vpos position. */
5385 it->vpos -= nlines;
5386 it->current_y -= h;
5388 if (dy == 0)
5390 /* DY == 0 means move to the start of the screen line. The
5391 value of nlines is > 0 if continuation lines were involved. */
5392 if (nlines > 0)
5393 move_it_by_lines (it, nlines, 1);
5394 xassert (IT_CHARPOS (*it) <= start_pos);
5396 else if (nlines)
5398 /* The y-position we try to reach. Note that h has been
5399 subtracted in front of the if-statement. */
5400 int target_y = it->current_y + h - dy;
5401 int y0 = it3.current_y;
5402 int y1 = line_bottom_y (&it3);
5403 int line_height = y1 - y0;
5405 /* If we did not reach target_y, try to move further backward if
5406 we can. If we moved too far backward, try to move forward. */
5407 if (target_y < it->current_y
5408 /* This is heuristic. In a window that's 3 lines high, with
5409 a line height of 13 pixels each, recentering with point
5410 on the bottom line will try to move -39/2 = 19 pixels
5411 backward. Try to avoid moving into the first line. */
5412 && it->current_y - target_y > line_height / 3 * 2
5413 && IT_CHARPOS (*it) > BEGV)
5415 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5416 target_y - it->current_y));
5417 move_it_vertically (it, target_y - it->current_y);
5418 xassert (IT_CHARPOS (*it) >= BEGV);
5420 else if (target_y >= it->current_y + line_height
5421 && IT_CHARPOS (*it) < ZV)
5423 /* Should move forward by at least one line, maybe more.
5425 Note: Calling move_it_by_lines can be expensive on
5426 terminal frames, where compute_motion is used (via
5427 vmotion) to do the job, when there are very long lines
5428 and truncate-lines is nil. That's the reason for
5429 treating terminal frames specially here. */
5431 if (!FRAME_WINDOW_P (it->f))
5432 move_it_vertically (it, target_y - (it->current_y + line_height));
5433 else
5437 move_it_by_lines (it, 1, 1);
5439 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5442 xassert (IT_CHARPOS (*it) >= BEGV);
5448 /* Move IT by a specified amount of pixel lines DY. DY negative means
5449 move backwards. DY = 0 means move to start of screen line. At the
5450 end, IT will be on the start of a screen line. */
5452 void
5453 move_it_vertically (it, dy)
5454 struct it *it;
5455 int dy;
5457 if (dy <= 0)
5458 move_it_vertically_backward (it, -dy);
5459 else if (dy > 0)
5461 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5462 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5463 MOVE_TO_POS | MOVE_TO_Y);
5464 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5466 /* If buffer ends in ZV without a newline, move to the start of
5467 the line to satisfy the post-condition. */
5468 if (IT_CHARPOS (*it) == ZV
5469 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5470 move_it_by_lines (it, 0, 0);
5475 /* Move iterator IT past the end of the text line it is in. */
5477 void
5478 move_it_past_eol (it)
5479 struct it *it;
5481 enum move_it_result rc;
5483 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5484 if (rc == MOVE_NEWLINE_OR_CR)
5485 set_iterator_to_next (it, 0);
5489 #if 0 /* Currently not used. */
5491 /* Return non-zero if some text between buffer positions START_CHARPOS
5492 and END_CHARPOS is invisible. IT->window is the window for text
5493 property lookup. */
5495 static int
5496 invisible_text_between_p (it, start_charpos, end_charpos)
5497 struct it *it;
5498 int start_charpos, end_charpos;
5500 Lisp_Object prop, limit;
5501 int invisible_found_p;
5503 xassert (it != NULL && start_charpos <= end_charpos);
5505 /* Is text at START invisible? */
5506 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5507 it->window);
5508 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5509 invisible_found_p = 1;
5510 else
5512 limit = Fnext_single_char_property_change (make_number (start_charpos),
5513 Qinvisible, Qnil,
5514 make_number (end_charpos));
5515 invisible_found_p = XFASTINT (limit) < end_charpos;
5518 return invisible_found_p;
5521 #endif /* 0 */
5524 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5525 negative means move up. DVPOS == 0 means move to the start of the
5526 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5527 NEED_Y_P is zero, IT->current_y will be left unchanged.
5529 Further optimization ideas: If we would know that IT->f doesn't use
5530 a face with proportional font, we could be faster for
5531 truncate-lines nil. */
5533 void
5534 move_it_by_lines (it, dvpos, need_y_p)
5535 struct it *it;
5536 int dvpos, need_y_p;
5538 struct position pos;
5540 if (!FRAME_WINDOW_P (it->f))
5542 struct text_pos textpos;
5544 /* We can use vmotion on frames without proportional fonts. */
5545 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5546 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5547 reseat (it, textpos, 1);
5548 it->vpos += pos.vpos;
5549 it->current_y += pos.vpos;
5551 else if (dvpos == 0)
5553 /* DVPOS == 0 means move to the start of the screen line. */
5554 move_it_vertically_backward (it, 0);
5555 xassert (it->current_x == 0 && it->hpos == 0);
5557 else if (dvpos > 0)
5558 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5559 else
5561 struct it it2;
5562 int start_charpos, i;
5564 /* Start at the beginning of the screen line containing IT's
5565 position. */
5566 move_it_vertically_backward (it, 0);
5568 /* Go back -DVPOS visible lines and reseat the iterator there. */
5569 start_charpos = IT_CHARPOS (*it);
5570 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5571 back_to_previous_visible_line_start (it);
5572 reseat (it, it->current.pos, 1);
5573 it->current_x = it->hpos = 0;
5575 /* Above call may have moved too far if continuation lines
5576 are involved. Scan forward and see if it did. */
5577 it2 = *it;
5578 it2.vpos = it2.current_y = 0;
5579 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5580 it->vpos -= it2.vpos;
5581 it->current_y -= it2.current_y;
5582 it->current_x = it->hpos = 0;
5584 /* If we moved too far, move IT some lines forward. */
5585 if (it2.vpos > -dvpos)
5587 int delta = it2.vpos + dvpos;
5588 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5595 /***********************************************************************
5596 Messages
5597 ***********************************************************************/
5600 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5601 to *Messages*. */
5603 void
5604 add_to_log (format, arg1, arg2)
5605 char *format;
5606 Lisp_Object arg1, arg2;
5608 Lisp_Object args[3];
5609 Lisp_Object msg, fmt;
5610 char *buffer;
5611 int len;
5612 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5614 /* Do nothing if called asynchronously. Inserting text into
5615 a buffer may call after-change-functions and alike and
5616 that would means running Lisp asynchronously. */
5617 if (handling_signal)
5618 return;
5620 fmt = msg = Qnil;
5621 GCPRO4 (fmt, msg, arg1, arg2);
5623 args[0] = fmt = build_string (format);
5624 args[1] = arg1;
5625 args[2] = arg2;
5626 msg = Fformat (3, args);
5628 len = SBYTES (msg) + 1;
5629 buffer = (char *) alloca (len);
5630 bcopy (SDATA (msg), buffer, len);
5632 message_dolog (buffer, len - 1, 1, 0);
5633 UNGCPRO;
5637 /* Output a newline in the *Messages* buffer if "needs" one. */
5639 void
5640 message_log_maybe_newline ()
5642 if (message_log_need_newline)
5643 message_dolog ("", 0, 1, 0);
5647 /* Add a string M of length NBYTES to the message log, optionally
5648 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5649 nonzero, means interpret the contents of M as multibyte. This
5650 function calls low-level routines in order to bypass text property
5651 hooks, etc. which might not be safe to run. */
5653 void
5654 message_dolog (m, nbytes, nlflag, multibyte)
5655 char *m;
5656 int nbytes, nlflag, multibyte;
5658 if (!NILP (Vmessage_log_max))
5660 struct buffer *oldbuf;
5661 Lisp_Object oldpoint, oldbegv, oldzv;
5662 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5663 int point_at_end = 0;
5664 int zv_at_end = 0;
5665 Lisp_Object old_deactivate_mark, tem;
5666 struct gcpro gcpro1;
5668 old_deactivate_mark = Vdeactivate_mark;
5669 oldbuf = current_buffer;
5670 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5671 current_buffer->undo_list = Qt;
5673 oldpoint = message_dolog_marker1;
5674 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5675 oldbegv = message_dolog_marker2;
5676 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5677 oldzv = message_dolog_marker3;
5678 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5679 GCPRO1 (old_deactivate_mark);
5681 if (PT == Z)
5682 point_at_end = 1;
5683 if (ZV == Z)
5684 zv_at_end = 1;
5686 BEGV = BEG;
5687 BEGV_BYTE = BEG_BYTE;
5688 ZV = Z;
5689 ZV_BYTE = Z_BYTE;
5690 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5692 /* Insert the string--maybe converting multibyte to single byte
5693 or vice versa, so that all the text fits the buffer. */
5694 if (multibyte
5695 && NILP (current_buffer->enable_multibyte_characters))
5697 int i, c, char_bytes;
5698 unsigned char work[1];
5700 /* Convert a multibyte string to single-byte
5701 for the *Message* buffer. */
5702 for (i = 0; i < nbytes; i += nbytes)
5704 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5705 work[0] = (SINGLE_BYTE_CHAR_P (c)
5707 : multibyte_char_to_unibyte (c, Qnil));
5708 insert_1_both (work, 1, 1, 1, 0, 0);
5711 else if (! multibyte
5712 && ! NILP (current_buffer->enable_multibyte_characters))
5714 int i, c, char_bytes;
5715 unsigned char *msg = (unsigned char *) m;
5716 unsigned char str[MAX_MULTIBYTE_LENGTH];
5717 /* Convert a single-byte string to multibyte
5718 for the *Message* buffer. */
5719 for (i = 0; i < nbytes; i++)
5721 c = unibyte_char_to_multibyte (msg[i]);
5722 char_bytes = CHAR_STRING (c, str);
5723 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5726 else if (nbytes)
5727 insert_1 (m, nbytes, 1, 0, 0);
5729 if (nlflag)
5731 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5732 insert_1 ("\n", 1, 1, 0, 0);
5734 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5735 this_bol = PT;
5736 this_bol_byte = PT_BYTE;
5738 /* See if this line duplicates the previous one.
5739 If so, combine duplicates. */
5740 if (this_bol > BEG)
5742 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5743 prev_bol = PT;
5744 prev_bol_byte = PT_BYTE;
5746 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5747 this_bol, this_bol_byte);
5748 if (dup)
5750 del_range_both (prev_bol, prev_bol_byte,
5751 this_bol, this_bol_byte, 0);
5752 if (dup > 1)
5754 char dupstr[40];
5755 int duplen;
5757 /* If you change this format, don't forget to also
5758 change message_log_check_duplicate. */
5759 sprintf (dupstr, " [%d times]", dup);
5760 duplen = strlen (dupstr);
5761 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5762 insert_1 (dupstr, duplen, 1, 0, 1);
5767 /* If we have more than the desired maximum number of lines
5768 in the *Messages* buffer now, delete the oldest ones.
5769 This is safe because we don't have undo in this buffer. */
5771 if (NATNUMP (Vmessage_log_max))
5773 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5774 -XFASTINT (Vmessage_log_max) - 1, 0);
5775 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5778 BEGV = XMARKER (oldbegv)->charpos;
5779 BEGV_BYTE = marker_byte_position (oldbegv);
5781 if (zv_at_end)
5783 ZV = Z;
5784 ZV_BYTE = Z_BYTE;
5786 else
5788 ZV = XMARKER (oldzv)->charpos;
5789 ZV_BYTE = marker_byte_position (oldzv);
5792 if (point_at_end)
5793 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5794 else
5795 /* We can't do Fgoto_char (oldpoint) because it will run some
5796 Lisp code. */
5797 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5798 XMARKER (oldpoint)->bytepos);
5800 UNGCPRO;
5801 unchain_marker (oldpoint);
5802 unchain_marker (oldbegv);
5803 unchain_marker (oldzv);
5805 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5806 set_buffer_internal (oldbuf);
5807 if (NILP (tem))
5808 windows_or_buffers_changed = old_windows_or_buffers_changed;
5809 message_log_need_newline = !nlflag;
5810 Vdeactivate_mark = old_deactivate_mark;
5815 /* We are at the end of the buffer after just having inserted a newline.
5816 (Note: We depend on the fact we won't be crossing the gap.)
5817 Check to see if the most recent message looks a lot like the previous one.
5818 Return 0 if different, 1 if the new one should just replace it, or a
5819 value N > 1 if we should also append " [N times]". */
5821 static int
5822 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5823 int prev_bol, this_bol;
5824 int prev_bol_byte, this_bol_byte;
5826 int i;
5827 int len = Z_BYTE - 1 - this_bol_byte;
5828 int seen_dots = 0;
5829 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5830 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5832 for (i = 0; i < len; i++)
5834 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5835 seen_dots = 1;
5836 if (p1[i] != p2[i])
5837 return seen_dots;
5839 p1 += len;
5840 if (*p1 == '\n')
5841 return 2;
5842 if (*p1++ == ' ' && *p1++ == '[')
5844 int n = 0;
5845 while (*p1 >= '0' && *p1 <= '9')
5846 n = n * 10 + *p1++ - '0';
5847 if (strncmp (p1, " times]\n", 8) == 0)
5848 return n+1;
5850 return 0;
5854 /* Display an echo area message M with a specified length of NBYTES
5855 bytes. The string may include null characters. If M is 0, clear
5856 out any existing message, and let the mini-buffer text show
5857 through.
5859 The buffer M must continue to exist until after the echo area gets
5860 cleared or some other message gets displayed there. This means do
5861 not pass text that is stored in a Lisp string; do not pass text in
5862 a buffer that was alloca'd. */
5864 void
5865 message2 (m, nbytes, multibyte)
5866 char *m;
5867 int nbytes;
5868 int multibyte;
5870 /* First flush out any partial line written with print. */
5871 message_log_maybe_newline ();
5872 if (m)
5873 message_dolog (m, nbytes, 1, multibyte);
5874 message2_nolog (m, nbytes, multibyte);
5878 /* The non-logging counterpart of message2. */
5880 void
5881 message2_nolog (m, nbytes, multibyte)
5882 char *m;
5883 int nbytes;
5885 struct frame *sf = SELECTED_FRAME ();
5886 message_enable_multibyte = multibyte;
5888 if (noninteractive)
5890 if (noninteractive_need_newline)
5891 putc ('\n', stderr);
5892 noninteractive_need_newline = 0;
5893 if (m)
5894 fwrite (m, nbytes, 1, stderr);
5895 if (cursor_in_echo_area == 0)
5896 fprintf (stderr, "\n");
5897 fflush (stderr);
5899 /* A null message buffer means that the frame hasn't really been
5900 initialized yet. Error messages get reported properly by
5901 cmd_error, so this must be just an informative message; toss it. */
5902 else if (INTERACTIVE
5903 && sf->glyphs_initialized_p
5904 && FRAME_MESSAGE_BUF (sf))
5906 Lisp_Object mini_window;
5907 struct frame *f;
5909 /* Get the frame containing the mini-buffer
5910 that the selected frame is using. */
5911 mini_window = FRAME_MINIBUF_WINDOW (sf);
5912 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5914 FRAME_SAMPLE_VISIBILITY (f);
5915 if (FRAME_VISIBLE_P (sf)
5916 && ! FRAME_VISIBLE_P (f))
5917 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5919 if (m)
5921 set_message (m, Qnil, nbytes, multibyte);
5922 if (minibuffer_auto_raise)
5923 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5925 else
5926 clear_message (1, 1);
5928 do_pending_window_change (0);
5929 echo_area_display (1);
5930 do_pending_window_change (0);
5931 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5932 (*frame_up_to_date_hook) (f);
5937 /* Display an echo area message M with a specified length of NBYTES
5938 bytes. The string may include null characters. If M is not a
5939 string, clear out any existing message, and let the mini-buffer
5940 text show through. */
5942 void
5943 message3 (m, nbytes, multibyte)
5944 Lisp_Object m;
5945 int nbytes;
5946 int multibyte;
5948 struct gcpro gcpro1;
5950 GCPRO1 (m);
5952 /* First flush out any partial line written with print. */
5953 message_log_maybe_newline ();
5954 if (STRINGP (m))
5955 message_dolog (SDATA (m), nbytes, 1, multibyte);
5956 message3_nolog (m, nbytes, multibyte);
5958 UNGCPRO;
5962 /* The non-logging version of message3. */
5964 void
5965 message3_nolog (m, nbytes, multibyte)
5966 Lisp_Object m;
5967 int nbytes, multibyte;
5969 struct frame *sf = SELECTED_FRAME ();
5970 message_enable_multibyte = multibyte;
5972 if (noninteractive)
5974 if (noninteractive_need_newline)
5975 putc ('\n', stderr);
5976 noninteractive_need_newline = 0;
5977 if (STRINGP (m))
5978 fwrite (SDATA (m), nbytes, 1, stderr);
5979 if (cursor_in_echo_area == 0)
5980 fprintf (stderr, "\n");
5981 fflush (stderr);
5983 /* A null message buffer means that the frame hasn't really been
5984 initialized yet. Error messages get reported properly by
5985 cmd_error, so this must be just an informative message; toss it. */
5986 else if (INTERACTIVE
5987 && sf->glyphs_initialized_p
5988 && FRAME_MESSAGE_BUF (sf))
5990 Lisp_Object mini_window;
5991 Lisp_Object frame;
5992 struct frame *f;
5994 /* Get the frame containing the mini-buffer
5995 that the selected frame is using. */
5996 mini_window = FRAME_MINIBUF_WINDOW (sf);
5997 frame = XWINDOW (mini_window)->frame;
5998 f = XFRAME (frame);
6000 FRAME_SAMPLE_VISIBILITY (f);
6001 if (FRAME_VISIBLE_P (sf)
6002 && !FRAME_VISIBLE_P (f))
6003 Fmake_frame_visible (frame);
6005 if (STRINGP (m) && SCHARS (m) > 0)
6007 set_message (NULL, m, nbytes, multibyte);
6008 if (minibuffer_auto_raise)
6009 Fraise_frame (frame);
6011 else
6012 clear_message (1, 1);
6014 do_pending_window_change (0);
6015 echo_area_display (1);
6016 do_pending_window_change (0);
6017 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6018 (*frame_up_to_date_hook) (f);
6023 /* Display a null-terminated echo area message M. If M is 0, clear
6024 out any existing message, and let the mini-buffer text show through.
6026 The buffer M must continue to exist until after the echo area gets
6027 cleared or some other message gets displayed there. Do not pass
6028 text that is stored in a Lisp string. Do not pass text in a buffer
6029 that was alloca'd. */
6031 void
6032 message1 (m)
6033 char *m;
6035 message2 (m, (m ? strlen (m) : 0), 0);
6039 /* The non-logging counterpart of message1. */
6041 void
6042 message1_nolog (m)
6043 char *m;
6045 message2_nolog (m, (m ? strlen (m) : 0), 0);
6048 /* Display a message M which contains a single %s
6049 which gets replaced with STRING. */
6051 void
6052 message_with_string (m, string, log)
6053 char *m;
6054 Lisp_Object string;
6055 int log;
6057 if (noninteractive)
6059 if (m)
6061 if (noninteractive_need_newline)
6062 putc ('\n', stderr);
6063 noninteractive_need_newline = 0;
6064 fprintf (stderr, m, SDATA (string));
6065 if (cursor_in_echo_area == 0)
6066 fprintf (stderr, "\n");
6067 fflush (stderr);
6070 else if (INTERACTIVE)
6072 /* The frame whose minibuffer we're going to display the message on.
6073 It may be larger than the selected frame, so we need
6074 to use its buffer, not the selected frame's buffer. */
6075 Lisp_Object mini_window;
6076 struct frame *f, *sf = SELECTED_FRAME ();
6078 /* Get the frame containing the minibuffer
6079 that the selected frame is using. */
6080 mini_window = FRAME_MINIBUF_WINDOW (sf);
6081 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6083 /* A null message buffer means that the frame hasn't really been
6084 initialized yet. Error messages get reported properly by
6085 cmd_error, so this must be just an informative message; toss it. */
6086 if (FRAME_MESSAGE_BUF (f))
6088 Lisp_Object args[2], message;
6089 struct gcpro gcpro1, gcpro2;
6091 args[0] = build_string (m);
6092 args[1] = message = string;
6093 GCPRO2 (args[0], message);
6094 gcpro1.nvars = 2;
6096 message = Fformat (2, args);
6098 if (log)
6099 message3 (message, SBYTES (message), SMBP (message));
6100 else
6101 message3_nolog (message, SBYTES (message), SMBP (message));
6103 UNGCPRO;
6105 /* Print should start at the beginning of the message
6106 buffer next time. */
6107 message_buf_print = 0;
6113 /* Dump an informative message to the minibuf. If M is 0, clear out
6114 any existing message, and let the mini-buffer text show through. */
6116 /* VARARGS 1 */
6117 void
6118 message (m, a1, a2, a3)
6119 char *m;
6120 EMACS_INT a1, a2, a3;
6122 if (noninteractive)
6124 if (m)
6126 if (noninteractive_need_newline)
6127 putc ('\n', stderr);
6128 noninteractive_need_newline = 0;
6129 fprintf (stderr, m, a1, a2, a3);
6130 if (cursor_in_echo_area == 0)
6131 fprintf (stderr, "\n");
6132 fflush (stderr);
6135 else if (INTERACTIVE)
6137 /* The frame whose mini-buffer we're going to display the message
6138 on. It may be larger than the selected frame, so we need to
6139 use its buffer, not the selected frame's buffer. */
6140 Lisp_Object mini_window;
6141 struct frame *f, *sf = SELECTED_FRAME ();
6143 /* Get the frame containing the mini-buffer
6144 that the selected frame is using. */
6145 mini_window = FRAME_MINIBUF_WINDOW (sf);
6146 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6148 /* A null message buffer means that the frame hasn't really been
6149 initialized yet. Error messages get reported properly by
6150 cmd_error, so this must be just an informative message; toss
6151 it. */
6152 if (FRAME_MESSAGE_BUF (f))
6154 if (m)
6156 int len;
6157 #ifdef NO_ARG_ARRAY
6158 char *a[3];
6159 a[0] = (char *) a1;
6160 a[1] = (char *) a2;
6161 a[2] = (char *) a3;
6163 len = doprnt (FRAME_MESSAGE_BUF (f),
6164 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6165 #else
6166 len = doprnt (FRAME_MESSAGE_BUF (f),
6167 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6168 (char **) &a1);
6169 #endif /* NO_ARG_ARRAY */
6171 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6173 else
6174 message1 (0);
6176 /* Print should start at the beginning of the message
6177 buffer next time. */
6178 message_buf_print = 0;
6184 /* The non-logging version of message. */
6186 void
6187 message_nolog (m, a1, a2, a3)
6188 char *m;
6189 EMACS_INT a1, a2, a3;
6191 Lisp_Object old_log_max;
6192 old_log_max = Vmessage_log_max;
6193 Vmessage_log_max = Qnil;
6194 message (m, a1, a2, a3);
6195 Vmessage_log_max = old_log_max;
6199 /* Display the current message in the current mini-buffer. This is
6200 only called from error handlers in process.c, and is not time
6201 critical. */
6203 void
6204 update_echo_area ()
6206 if (!NILP (echo_area_buffer[0]))
6208 Lisp_Object string;
6209 string = Fcurrent_message ();
6210 message3 (string, SBYTES (string),
6211 !NILP (current_buffer->enable_multibyte_characters));
6216 /* Make sure echo area buffers in echo_buffers[] are life. If they
6217 aren't, make new ones. */
6219 static void
6220 ensure_echo_area_buffers ()
6222 int i;
6224 for (i = 0; i < 2; ++i)
6225 if (!BUFFERP (echo_buffer[i])
6226 || NILP (XBUFFER (echo_buffer[i])->name))
6228 char name[30];
6229 Lisp_Object old_buffer;
6230 int j;
6232 old_buffer = echo_buffer[i];
6233 sprintf (name, " *Echo Area %d*", i);
6234 echo_buffer[i] = Fget_buffer_create (build_string (name));
6235 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6237 for (j = 0; j < 2; ++j)
6238 if (EQ (old_buffer, echo_area_buffer[j]))
6239 echo_area_buffer[j] = echo_buffer[i];
6244 /* Call FN with args A1..A4 with either the current or last displayed
6245 echo_area_buffer as current buffer.
6247 WHICH zero means use the current message buffer
6248 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6249 from echo_buffer[] and clear it.
6251 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6252 suitable buffer from echo_buffer[] and clear it.
6254 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6255 that the current message becomes the last displayed one, make
6256 choose a suitable buffer for echo_area_buffer[0], and clear it.
6258 Value is what FN returns. */
6260 static int
6261 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6262 struct window *w;
6263 int which;
6264 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6265 EMACS_INT a1;
6266 Lisp_Object a2;
6267 EMACS_INT a3, a4;
6269 Lisp_Object buffer;
6270 int this_one, the_other, clear_buffer_p, rc;
6271 int count = BINDING_STACK_SIZE ();
6273 /* If buffers aren't life, make new ones. */
6274 ensure_echo_area_buffers ();
6276 clear_buffer_p = 0;
6278 if (which == 0)
6279 this_one = 0, the_other = 1;
6280 else if (which > 0)
6281 this_one = 1, the_other = 0;
6282 else
6284 this_one = 0, the_other = 1;
6285 clear_buffer_p = 1;
6287 /* We need a fresh one in case the current echo buffer equals
6288 the one containing the last displayed echo area message. */
6289 if (!NILP (echo_area_buffer[this_one])
6290 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6291 echo_area_buffer[this_one] = Qnil;
6294 /* Choose a suitable buffer from echo_buffer[] is we don't
6295 have one. */
6296 if (NILP (echo_area_buffer[this_one]))
6298 echo_area_buffer[this_one]
6299 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6300 ? echo_buffer[the_other]
6301 : echo_buffer[this_one]);
6302 clear_buffer_p = 1;
6305 buffer = echo_area_buffer[this_one];
6307 /* Don't get confused by reusing the buffer used for echoing
6308 for a different purpose. */
6309 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6310 cancel_echoing ();
6312 record_unwind_protect (unwind_with_echo_area_buffer,
6313 with_echo_area_buffer_unwind_data (w));
6315 /* Make the echo area buffer current. Note that for display
6316 purposes, it is not necessary that the displayed window's buffer
6317 == current_buffer, except for text property lookup. So, let's
6318 only set that buffer temporarily here without doing a full
6319 Fset_window_buffer. We must also change w->pointm, though,
6320 because otherwise an assertions in unshow_buffer fails, and Emacs
6321 aborts. */
6322 set_buffer_internal_1 (XBUFFER (buffer));
6323 if (w)
6325 w->buffer = buffer;
6326 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6329 current_buffer->undo_list = Qt;
6330 current_buffer->read_only = Qnil;
6331 specbind (Qinhibit_read_only, Qt);
6332 specbind (Qinhibit_modification_hooks, Qt);
6334 if (clear_buffer_p && Z > BEG)
6335 del_range (BEG, Z);
6337 xassert (BEGV >= BEG);
6338 xassert (ZV <= Z && ZV >= BEGV);
6340 rc = fn (a1, a2, a3, a4);
6342 xassert (BEGV >= BEG);
6343 xassert (ZV <= Z && ZV >= BEGV);
6345 unbind_to (count, Qnil);
6346 return rc;
6350 /* Save state that should be preserved around the call to the function
6351 FN called in with_echo_area_buffer. */
6353 static Lisp_Object
6354 with_echo_area_buffer_unwind_data (w)
6355 struct window *w;
6357 int i = 0;
6358 Lisp_Object vector;
6360 /* Reduce consing by keeping one vector in
6361 Vwith_echo_area_save_vector. */
6362 vector = Vwith_echo_area_save_vector;
6363 Vwith_echo_area_save_vector = Qnil;
6365 if (NILP (vector))
6366 vector = Fmake_vector (make_number (7), Qnil);
6368 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6369 AREF (vector, i) = Vdeactivate_mark, ++i;
6370 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6372 if (w)
6374 XSETWINDOW (AREF (vector, i), w); ++i;
6375 AREF (vector, i) = w->buffer; ++i;
6376 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6377 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6379 else
6381 int end = i + 4;
6382 for (; i < end; ++i)
6383 AREF (vector, i) = Qnil;
6386 xassert (i == ASIZE (vector));
6387 return vector;
6391 /* Restore global state from VECTOR which was created by
6392 with_echo_area_buffer_unwind_data. */
6394 static Lisp_Object
6395 unwind_with_echo_area_buffer (vector)
6396 Lisp_Object vector;
6398 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6399 Vdeactivate_mark = AREF (vector, 1);
6400 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6402 if (WINDOWP (AREF (vector, 3)))
6404 struct window *w;
6405 Lisp_Object buffer, charpos, bytepos;
6407 w = XWINDOW (AREF (vector, 3));
6408 buffer = AREF (vector, 4);
6409 charpos = AREF (vector, 5);
6410 bytepos = AREF (vector, 6);
6412 w->buffer = buffer;
6413 set_marker_both (w->pointm, buffer,
6414 XFASTINT (charpos), XFASTINT (bytepos));
6417 Vwith_echo_area_save_vector = vector;
6418 return Qnil;
6422 /* Set up the echo area for use by print functions. MULTIBYTE_P
6423 non-zero means we will print multibyte. */
6425 void
6426 setup_echo_area_for_printing (multibyte_p)
6427 int multibyte_p;
6429 ensure_echo_area_buffers ();
6431 if (!message_buf_print)
6433 /* A message has been output since the last time we printed.
6434 Choose a fresh echo area buffer. */
6435 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6436 echo_area_buffer[0] = echo_buffer[1];
6437 else
6438 echo_area_buffer[0] = echo_buffer[0];
6440 /* Switch to that buffer and clear it. */
6441 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6442 current_buffer->truncate_lines = Qnil;
6444 if (Z > BEG)
6446 int count = BINDING_STACK_SIZE ();
6447 specbind (Qinhibit_read_only, Qt);
6448 del_range (BEG, Z);
6449 unbind_to (count, Qnil);
6451 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6453 /* Set up the buffer for the multibyteness we need. */
6454 if (multibyte_p
6455 != !NILP (current_buffer->enable_multibyte_characters))
6456 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6458 /* Raise the frame containing the echo area. */
6459 if (minibuffer_auto_raise)
6461 struct frame *sf = SELECTED_FRAME ();
6462 Lisp_Object mini_window;
6463 mini_window = FRAME_MINIBUF_WINDOW (sf);
6464 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6467 message_log_maybe_newline ();
6468 message_buf_print = 1;
6470 else
6472 if (NILP (echo_area_buffer[0]))
6474 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6475 echo_area_buffer[0] = echo_buffer[1];
6476 else
6477 echo_area_buffer[0] = echo_buffer[0];
6480 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6482 /* Someone switched buffers between print requests. */
6483 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6484 current_buffer->truncate_lines = Qnil;
6490 /* Display an echo area message in window W. Value is non-zero if W's
6491 height is changed. If display_last_displayed_message_p is
6492 non-zero, display the message that was last displayed, otherwise
6493 display the current message. */
6495 static int
6496 display_echo_area (w)
6497 struct window *w;
6499 int i, no_message_p, window_height_changed_p, count;
6501 /* Temporarily disable garbage collections while displaying the echo
6502 area. This is done because a GC can print a message itself.
6503 That message would modify the echo area buffer's contents while a
6504 redisplay of the buffer is going on, and seriously confuse
6505 redisplay. */
6506 count = inhibit_garbage_collection ();
6508 /* If there is no message, we must call display_echo_area_1
6509 nevertheless because it resizes the window. But we will have to
6510 reset the echo_area_buffer in question to nil at the end because
6511 with_echo_area_buffer will sets it to an empty buffer. */
6512 i = display_last_displayed_message_p ? 1 : 0;
6513 no_message_p = NILP (echo_area_buffer[i]);
6515 window_height_changed_p
6516 = with_echo_area_buffer (w, display_last_displayed_message_p,
6517 display_echo_area_1,
6518 (EMACS_INT) w, Qnil, 0, 0);
6520 if (no_message_p)
6521 echo_area_buffer[i] = Qnil;
6523 unbind_to (count, Qnil);
6524 return window_height_changed_p;
6528 /* Helper for display_echo_area. Display the current buffer which
6529 contains the current echo area message in window W, a mini-window,
6530 a pointer to which is passed in A1. A2..A4 are currently not used.
6531 Change the height of W so that all of the message is displayed.
6532 Value is non-zero if height of W was changed. */
6534 static int
6535 display_echo_area_1 (a1, a2, a3, a4)
6536 EMACS_INT a1;
6537 Lisp_Object a2;
6538 EMACS_INT a3, a4;
6540 struct window *w = (struct window *) a1;
6541 Lisp_Object window;
6542 struct text_pos start;
6543 int window_height_changed_p = 0;
6545 /* Do this before displaying, so that we have a large enough glyph
6546 matrix for the display. */
6547 window_height_changed_p = resize_mini_window (w, 0);
6549 /* Display. */
6550 clear_glyph_matrix (w->desired_matrix);
6551 XSETWINDOW (window, w);
6552 SET_TEXT_POS (start, BEG, BEG_BYTE);
6553 try_window (window, start);
6555 return window_height_changed_p;
6559 /* Resize the echo area window to exactly the size needed for the
6560 currently displayed message, if there is one. If a mini-buffer
6561 is active, don't shrink it. */
6563 void
6564 resize_echo_area_exactly ()
6566 if (BUFFERP (echo_area_buffer[0])
6567 && WINDOWP (echo_area_window))
6569 struct window *w = XWINDOW (echo_area_window);
6570 int resized_p;
6571 Lisp_Object resize_exactly;
6573 if (minibuf_level == 0)
6574 resize_exactly = Qt;
6575 else
6576 resize_exactly = Qnil;
6578 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6579 (EMACS_INT) w, resize_exactly, 0, 0);
6580 if (resized_p)
6582 ++windows_or_buffers_changed;
6583 ++update_mode_lines;
6584 redisplay_internal (0);
6590 /* Callback function for with_echo_area_buffer, when used from
6591 resize_echo_area_exactly. A1 contains a pointer to the window to
6592 resize, EXACTLY non-nil means resize the mini-window exactly to the
6593 size of the text displayed. A3 and A4 are not used. Value is what
6594 resize_mini_window returns. */
6596 static int
6597 resize_mini_window_1 (a1, exactly, a3, a4)
6598 EMACS_INT a1;
6599 Lisp_Object exactly;
6600 EMACS_INT a3, a4;
6602 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6606 /* Resize mini-window W to fit the size of its contents. EXACT:P
6607 means size the window exactly to the size needed. Otherwise, it's
6608 only enlarged until W's buffer is empty. Value is non-zero if
6609 the window height has been changed. */
6612 resize_mini_window (w, exact_p)
6613 struct window *w;
6614 int exact_p;
6616 struct frame *f = XFRAME (w->frame);
6617 int window_height_changed_p = 0;
6619 xassert (MINI_WINDOW_P (w));
6621 /* Don't resize windows while redisplaying a window; it would
6622 confuse redisplay functions when the size of the window they are
6623 displaying changes from under them. Such a resizing can happen,
6624 for instance, when which-func prints a long message while
6625 we are running fontification-functions. We're running these
6626 functions with safe_call which binds inhibit-redisplay to t. */
6627 if (!NILP (Vinhibit_redisplay))
6628 return 0;
6630 /* Nil means don't try to resize. */
6631 if (NILP (Vresize_mini_windows)
6632 || (FRAME_X_P (f) && f->output_data.x == NULL))
6633 return 0;
6635 if (!FRAME_MINIBUF_ONLY_P (f))
6637 struct it it;
6638 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6639 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6640 int height, max_height;
6641 int unit = CANON_Y_UNIT (f);
6642 struct text_pos start;
6643 struct buffer *old_current_buffer = NULL;
6645 if (current_buffer != XBUFFER (w->buffer))
6647 old_current_buffer = current_buffer;
6648 set_buffer_internal (XBUFFER (w->buffer));
6651 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6653 /* Compute the max. number of lines specified by the user. */
6654 if (FLOATP (Vmax_mini_window_height))
6655 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6656 else if (INTEGERP (Vmax_mini_window_height))
6657 max_height = XINT (Vmax_mini_window_height);
6658 else
6659 max_height = total_height / 4;
6661 /* Correct that max. height if it's bogus. */
6662 max_height = max (1, max_height);
6663 max_height = min (total_height, max_height);
6665 /* Find out the height of the text in the window. */
6666 if (it.truncate_lines_p)
6667 height = 1;
6668 else
6670 last_height = 0;
6671 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6672 if (it.max_ascent == 0 && it.max_descent == 0)
6673 height = it.current_y + last_height;
6674 else
6675 height = it.current_y + it.max_ascent + it.max_descent;
6676 height -= it.extra_line_spacing;
6677 height = (height + unit - 1) / unit;
6680 /* Compute a suitable window start. */
6681 if (height > max_height)
6683 height = max_height;
6684 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6685 move_it_vertically_backward (&it, (height - 1) * unit);
6686 start = it.current.pos;
6688 else
6689 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6690 SET_MARKER_FROM_TEXT_POS (w->start, start);
6692 if (EQ (Vresize_mini_windows, Qgrow_only))
6694 /* Let it grow only, until we display an empty message, in which
6695 case the window shrinks again. */
6696 if (height > XFASTINT (w->height))
6698 int old_height = XFASTINT (w->height);
6699 freeze_window_starts (f, 1);
6700 grow_mini_window (w, height - XFASTINT (w->height));
6701 window_height_changed_p = XFASTINT (w->height) != old_height;
6703 else if (height < XFASTINT (w->height)
6704 && (exact_p || BEGV == ZV))
6706 int old_height = XFASTINT (w->height);
6707 freeze_window_starts (f, 0);
6708 shrink_mini_window (w);
6709 window_height_changed_p = XFASTINT (w->height) != old_height;
6712 else
6714 /* Always resize to exact size needed. */
6715 if (height > XFASTINT (w->height))
6717 int old_height = XFASTINT (w->height);
6718 freeze_window_starts (f, 1);
6719 grow_mini_window (w, height - XFASTINT (w->height));
6720 window_height_changed_p = XFASTINT (w->height) != old_height;
6722 else if (height < XFASTINT (w->height))
6724 int old_height = XFASTINT (w->height);
6725 freeze_window_starts (f, 0);
6726 shrink_mini_window (w);
6728 if (height)
6730 freeze_window_starts (f, 1);
6731 grow_mini_window (w, height - XFASTINT (w->height));
6734 window_height_changed_p = XFASTINT (w->height) != old_height;
6738 if (old_current_buffer)
6739 set_buffer_internal (old_current_buffer);
6742 return window_height_changed_p;
6746 /* Value is the current message, a string, or nil if there is no
6747 current message. */
6749 Lisp_Object
6750 current_message ()
6752 Lisp_Object msg;
6754 if (NILP (echo_area_buffer[0]))
6755 msg = Qnil;
6756 else
6758 with_echo_area_buffer (0, 0, current_message_1,
6759 (EMACS_INT) &msg, Qnil, 0, 0);
6760 if (NILP (msg))
6761 echo_area_buffer[0] = Qnil;
6764 return msg;
6768 static int
6769 current_message_1 (a1, a2, a3, a4)
6770 EMACS_INT a1;
6771 Lisp_Object a2;
6772 EMACS_INT a3, a4;
6774 Lisp_Object *msg = (Lisp_Object *) a1;
6776 if (Z > BEG)
6777 *msg = make_buffer_string (BEG, Z, 1);
6778 else
6779 *msg = Qnil;
6780 return 0;
6784 /* Push the current message on Vmessage_stack for later restauration
6785 by restore_message. Value is non-zero if the current message isn't
6786 empty. This is a relatively infrequent operation, so it's not
6787 worth optimizing. */
6790 push_message ()
6792 Lisp_Object msg;
6793 msg = current_message ();
6794 Vmessage_stack = Fcons (msg, Vmessage_stack);
6795 return STRINGP (msg);
6799 /* Handler for record_unwind_protect calling pop_message. */
6801 Lisp_Object
6802 push_message_unwind (dummy)
6803 Lisp_Object dummy;
6805 pop_message ();
6806 return Qnil;
6810 /* Restore message display from the top of Vmessage_stack. */
6812 void
6813 restore_message ()
6815 Lisp_Object msg;
6817 xassert (CONSP (Vmessage_stack));
6818 msg = XCAR (Vmessage_stack);
6819 if (STRINGP (msg))
6820 message3_nolog (msg, SBYTES (msg), SMBP (msg));
6821 else
6822 message3_nolog (msg, 0, 0);
6826 /* Pop the top-most entry off Vmessage_stack. */
6828 void
6829 pop_message ()
6831 xassert (CONSP (Vmessage_stack));
6832 Vmessage_stack = XCDR (Vmessage_stack);
6836 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6837 exits. If the stack is not empty, we have a missing pop_message
6838 somewhere. */
6840 void
6841 check_message_stack ()
6843 if (!NILP (Vmessage_stack))
6844 abort ();
6848 /* Truncate to NCHARS what will be displayed in the echo area the next
6849 time we display it---but don't redisplay it now. */
6851 void
6852 truncate_echo_area (nchars)
6853 int nchars;
6855 if (nchars == 0)
6856 echo_area_buffer[0] = Qnil;
6857 /* A null message buffer means that the frame hasn't really been
6858 initialized yet. Error messages get reported properly by
6859 cmd_error, so this must be just an informative message; toss it. */
6860 else if (!noninteractive
6861 && INTERACTIVE
6862 && !NILP (echo_area_buffer[0]))
6864 struct frame *sf = SELECTED_FRAME ();
6865 if (FRAME_MESSAGE_BUF (sf))
6866 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6871 /* Helper function for truncate_echo_area. Truncate the current
6872 message to at most NCHARS characters. */
6874 static int
6875 truncate_message_1 (nchars, a2, a3, a4)
6876 EMACS_INT nchars;
6877 Lisp_Object a2;
6878 EMACS_INT a3, a4;
6880 if (BEG + nchars < Z)
6881 del_range (BEG + nchars, Z);
6882 if (Z == BEG)
6883 echo_area_buffer[0] = Qnil;
6884 return 0;
6888 /* Set the current message to a substring of S or STRING.
6890 If STRING is a Lisp string, set the message to the first NBYTES
6891 bytes from STRING. NBYTES zero means use the whole string. If
6892 STRING is multibyte, the message will be displayed multibyte.
6894 If S is not null, set the message to the first LEN bytes of S. LEN
6895 zero means use the whole string. MULTIBYTE_P non-zero means S is
6896 multibyte. Display the message multibyte in that case. */
6898 void
6899 set_message (s, string, nbytes, multibyte_p)
6900 char *s;
6901 Lisp_Object string;
6902 int nbytes;
6904 message_enable_multibyte
6905 = ((s && multibyte_p)
6906 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6908 with_echo_area_buffer (0, -1, set_message_1,
6909 (EMACS_INT) s, string, nbytes, multibyte_p);
6910 message_buf_print = 0;
6911 help_echo_showing_p = 0;
6915 /* Helper function for set_message. Arguments have the same meaning
6916 as there, with A1 corresponding to S and A2 corresponding to STRING
6917 This function is called with the echo area buffer being
6918 current. */
6920 static int
6921 set_message_1 (a1, a2, nbytes, multibyte_p)
6922 EMACS_INT a1;
6923 Lisp_Object a2;
6924 EMACS_INT nbytes, multibyte_p;
6926 char *s = (char *) a1;
6927 Lisp_Object string = a2;
6929 xassert (BEG == Z);
6931 /* Change multibyteness of the echo buffer appropriately. */
6932 if (message_enable_multibyte
6933 != !NILP (current_buffer->enable_multibyte_characters))
6934 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6936 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6938 /* Insert new message at BEG. */
6939 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6941 if (STRINGP (string))
6943 int nchars;
6945 if (nbytes == 0)
6946 nbytes = SBYTES (string);
6947 nchars = string_byte_to_char (string, nbytes);
6949 /* This function takes care of single/multibyte conversion. We
6950 just have to ensure that the echo area buffer has the right
6951 setting of enable_multibyte_characters. */
6952 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6954 else if (s)
6956 if (nbytes == 0)
6957 nbytes = strlen (s);
6959 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6961 /* Convert from multi-byte to single-byte. */
6962 int i, c, n;
6963 unsigned char work[1];
6965 /* Convert a multibyte string to single-byte. */
6966 for (i = 0; i < nbytes; i += n)
6968 c = string_char_and_length (s + i, nbytes - i, &n);
6969 work[0] = (SINGLE_BYTE_CHAR_P (c)
6971 : multibyte_char_to_unibyte (c, Qnil));
6972 insert_1_both (work, 1, 1, 1, 0, 0);
6975 else if (!multibyte_p
6976 && !NILP (current_buffer->enable_multibyte_characters))
6978 /* Convert from single-byte to multi-byte. */
6979 int i, c, n;
6980 unsigned char *msg = (unsigned char *) s;
6981 unsigned char str[MAX_MULTIBYTE_LENGTH];
6983 /* Convert a single-byte string to multibyte. */
6984 for (i = 0; i < nbytes; i++)
6986 c = unibyte_char_to_multibyte (msg[i]);
6987 n = CHAR_STRING (c, str);
6988 insert_1_both (str, 1, n, 1, 0, 0);
6991 else
6992 insert_1 (s, nbytes, 1, 0, 0);
6995 return 0;
6999 /* Clear messages. CURRENT_P non-zero means clear the current
7000 message. LAST_DISPLAYED_P non-zero means clear the message
7001 last displayed. */
7003 void
7004 clear_message (current_p, last_displayed_p)
7005 int current_p, last_displayed_p;
7007 if (current_p)
7009 echo_area_buffer[0] = Qnil;
7010 message_cleared_p = 1;
7013 if (last_displayed_p)
7014 echo_area_buffer[1] = Qnil;
7016 message_buf_print = 0;
7019 /* Clear garbaged frames.
7021 This function is used where the old redisplay called
7022 redraw_garbaged_frames which in turn called redraw_frame which in
7023 turn called clear_frame. The call to clear_frame was a source of
7024 flickering. I believe a clear_frame is not necessary. It should
7025 suffice in the new redisplay to invalidate all current matrices,
7026 and ensure a complete redisplay of all windows. */
7028 static void
7029 clear_garbaged_frames ()
7031 if (frame_garbaged)
7033 Lisp_Object tail, frame;
7034 int changed_count = 0;
7036 FOR_EACH_FRAME (tail, frame)
7038 struct frame *f = XFRAME (frame);
7040 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7042 if (f->resized_p)
7043 Fredraw_frame (frame);
7044 clear_current_matrices (f);
7045 changed_count++;
7046 f->garbaged = 0;
7047 f->resized_p = 0;
7051 frame_garbaged = 0;
7052 if (changed_count)
7053 ++windows_or_buffers_changed;
7058 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7059 is non-zero update selected_frame. Value is non-zero if the
7060 mini-windows height has been changed. */
7062 static int
7063 echo_area_display (update_frame_p)
7064 int update_frame_p;
7066 Lisp_Object mini_window;
7067 struct window *w;
7068 struct frame *f;
7069 int window_height_changed_p = 0;
7070 struct frame *sf = SELECTED_FRAME ();
7072 mini_window = FRAME_MINIBUF_WINDOW (sf);
7073 w = XWINDOW (mini_window);
7074 f = XFRAME (WINDOW_FRAME (w));
7076 /* Don't display if frame is invisible or not yet initialized. */
7077 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7078 return 0;
7080 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7081 #ifndef macintosh
7082 #ifdef HAVE_WINDOW_SYSTEM
7083 /* When Emacs starts, selected_frame may be a visible terminal
7084 frame, even if we run under a window system. If we let this
7085 through, a message would be displayed on the terminal. */
7086 if (EQ (selected_frame, Vterminal_frame)
7087 && !NILP (Vwindow_system))
7088 return 0;
7089 #endif /* HAVE_WINDOW_SYSTEM */
7090 #endif
7092 /* Redraw garbaged frames. */
7093 if (frame_garbaged)
7094 clear_garbaged_frames ();
7096 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7098 echo_area_window = mini_window;
7099 window_height_changed_p = display_echo_area (w);
7100 w->must_be_updated_p = 1;
7102 /* Update the display, unless called from redisplay_internal.
7103 Also don't update the screen during redisplay itself. The
7104 update will happen at the end of redisplay, and an update
7105 here could cause confusion. */
7106 if (update_frame_p && !redisplaying_p)
7108 int n = 0;
7110 /* If the display update has been interrupted by pending
7111 input, update mode lines in the frame. Due to the
7112 pending input, it might have been that redisplay hasn't
7113 been called, so that mode lines above the echo area are
7114 garbaged. This looks odd, so we prevent it here. */
7115 if (!display_completed)
7116 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7118 if (window_height_changed_p
7119 /* Don't do this if Emacs is shutting down. Redisplay
7120 needs to run hooks. */
7121 && !NILP (Vrun_hooks))
7123 /* Must update other windows. Likewise as in other
7124 cases, don't let this update be interrupted by
7125 pending input. */
7126 int count = BINDING_STACK_SIZE ();
7127 specbind (Qredisplay_dont_pause, Qt);
7128 windows_or_buffers_changed = 1;
7129 redisplay_internal (0);
7130 unbind_to (count, Qnil);
7132 else if (FRAME_WINDOW_P (f) && n == 0)
7134 /* Window configuration is the same as before.
7135 Can do with a display update of the echo area,
7136 unless we displayed some mode lines. */
7137 update_single_window (w, 1);
7138 rif->flush_display (f);
7140 else
7141 update_frame (f, 1, 1);
7143 /* If cursor is in the echo area, make sure that the next
7144 redisplay displays the minibuffer, so that the cursor will
7145 be replaced with what the minibuffer wants. */
7146 if (cursor_in_echo_area)
7147 ++windows_or_buffers_changed;
7150 else if (!EQ (mini_window, selected_window))
7151 windows_or_buffers_changed++;
7153 /* Last displayed message is now the current message. */
7154 echo_area_buffer[1] = echo_area_buffer[0];
7156 /* Prevent redisplay optimization in redisplay_internal by resetting
7157 this_line_start_pos. This is done because the mini-buffer now
7158 displays the message instead of its buffer text. */
7159 if (EQ (mini_window, selected_window))
7160 CHARPOS (this_line_start_pos) = 0;
7162 return window_height_changed_p;
7167 /***********************************************************************
7168 Frame Titles
7169 ***********************************************************************/
7172 #ifdef HAVE_WINDOW_SYSTEM
7174 /* A buffer for constructing frame titles in it; allocated from the
7175 heap in init_xdisp and resized as needed in store_frame_title_char. */
7177 static char *frame_title_buf;
7179 /* The buffer's end, and a current output position in it. */
7181 static char *frame_title_buf_end;
7182 static char *frame_title_ptr;
7185 /* Store a single character C for the frame title in frame_title_buf.
7186 Re-allocate frame_title_buf if necessary. */
7188 static void
7189 store_frame_title_char (c)
7190 char c;
7192 /* If output position has reached the end of the allocated buffer,
7193 double the buffer's size. */
7194 if (frame_title_ptr == frame_title_buf_end)
7196 int len = frame_title_ptr - frame_title_buf;
7197 int new_size = 2 * len * sizeof *frame_title_buf;
7198 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7199 frame_title_buf_end = frame_title_buf + new_size;
7200 frame_title_ptr = frame_title_buf + len;
7203 *frame_title_ptr++ = c;
7207 /* Store part of a frame title in frame_title_buf, beginning at
7208 frame_title_ptr. STR is the string to store. Do not copy
7209 characters that yield more columns than PRECISION; PRECISION <= 0
7210 means copy the whole string. Pad with spaces until FIELD_WIDTH
7211 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7212 pad. Called from display_mode_element when it is used to build a
7213 frame title. */
7215 static int
7216 store_frame_title (str, field_width, precision)
7217 unsigned char *str;
7218 int field_width, precision;
7220 int n = 0;
7221 int dummy, nbytes;
7223 /* Copy at most PRECISION chars from STR. */
7224 nbytes = strlen (str);
7225 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7226 while (nbytes--)
7227 store_frame_title_char (*str++);
7229 /* Fill up with spaces until FIELD_WIDTH reached. */
7230 while (field_width > 0
7231 && n < field_width)
7233 store_frame_title_char (' ');
7234 ++n;
7237 return n;
7241 /* Set the title of FRAME, if it has changed. The title format is
7242 Vicon_title_format if FRAME is iconified, otherwise it is
7243 frame_title_format. */
7245 static void
7246 x_consider_frame_title (frame)
7247 Lisp_Object frame;
7249 struct frame *f = XFRAME (frame);
7251 if (FRAME_WINDOW_P (f)
7252 || FRAME_MINIBUF_ONLY_P (f)
7253 || f->explicit_name)
7255 /* Do we have more than one visible frame on this X display? */
7256 Lisp_Object tail;
7257 Lisp_Object fmt;
7258 struct buffer *obuf;
7259 int len;
7260 struct it it;
7262 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7264 Lisp_Object other_frame = XCAR (tail);
7265 struct frame *tf = XFRAME (other_frame);
7267 if (tf != f
7268 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7269 && !FRAME_MINIBUF_ONLY_P (tf)
7270 && !EQ (other_frame, tip_frame)
7271 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7272 break;
7275 /* Set global variable indicating that multiple frames exist. */
7276 multiple_frames = CONSP (tail);
7278 /* Switch to the buffer of selected window of the frame. Set up
7279 frame_title_ptr so that display_mode_element will output into it;
7280 then display the title. */
7281 obuf = current_buffer;
7282 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7283 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7284 frame_title_ptr = frame_title_buf;
7285 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7286 NULL, DEFAULT_FACE_ID);
7287 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7288 len = frame_title_ptr - frame_title_buf;
7289 frame_title_ptr = NULL;
7290 set_buffer_internal_1 (obuf);
7292 /* Set the title only if it's changed. This avoids consing in
7293 the common case where it hasn't. (If it turns out that we've
7294 already wasted too much time by walking through the list with
7295 display_mode_element, then we might need to optimize at a
7296 higher level than this.) */
7297 if (! STRINGP (f->name)
7298 || SBYTES (f->name) != len
7299 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7300 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7304 #else /* not HAVE_WINDOW_SYSTEM */
7306 #define frame_title_ptr ((char *)0)
7307 #define store_frame_title(str, mincol, maxcol) 0
7309 #endif /* not HAVE_WINDOW_SYSTEM */
7314 /***********************************************************************
7315 Menu Bars
7316 ***********************************************************************/
7319 /* Prepare for redisplay by updating menu-bar item lists when
7320 appropriate. This can call eval. */
7322 void
7323 prepare_menu_bars ()
7325 int all_windows;
7326 struct gcpro gcpro1, gcpro2;
7327 struct frame *f;
7328 Lisp_Object tooltip_frame;
7330 #ifdef HAVE_WINDOW_SYSTEM
7331 tooltip_frame = tip_frame;
7332 #else
7333 tooltip_frame = Qnil;
7334 #endif
7336 /* Update all frame titles based on their buffer names, etc. We do
7337 this before the menu bars so that the buffer-menu will show the
7338 up-to-date frame titles. */
7339 #ifdef HAVE_WINDOW_SYSTEM
7340 if (windows_or_buffers_changed || update_mode_lines)
7342 Lisp_Object tail, frame;
7344 FOR_EACH_FRAME (tail, frame)
7346 f = XFRAME (frame);
7347 if (!EQ (frame, tooltip_frame)
7348 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7349 x_consider_frame_title (frame);
7352 #endif /* HAVE_WINDOW_SYSTEM */
7354 /* Update the menu bar item lists, if appropriate. This has to be
7355 done before any actual redisplay or generation of display lines. */
7356 all_windows = (update_mode_lines
7357 || buffer_shared > 1
7358 || windows_or_buffers_changed);
7359 if (all_windows)
7361 Lisp_Object tail, frame;
7362 int count = BINDING_STACK_SIZE ();
7364 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7366 FOR_EACH_FRAME (tail, frame)
7368 f = XFRAME (frame);
7370 /* Ignore tooltip frame. */
7371 if (EQ (frame, tooltip_frame))
7372 continue;
7374 /* If a window on this frame changed size, report that to
7375 the user and clear the size-change flag. */
7376 if (FRAME_WINDOW_SIZES_CHANGED (f))
7378 Lisp_Object functions;
7380 /* Clear flag first in case we get an error below. */
7381 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7382 functions = Vwindow_size_change_functions;
7383 GCPRO2 (tail, functions);
7385 while (CONSP (functions))
7387 call1 (XCAR (functions), frame);
7388 functions = XCDR (functions);
7390 UNGCPRO;
7393 GCPRO1 (tail);
7394 update_menu_bar (f, 0);
7395 #ifdef HAVE_WINDOW_SYSTEM
7396 update_tool_bar (f, 0);
7397 #endif
7398 UNGCPRO;
7401 unbind_to (count, Qnil);
7403 else
7405 struct frame *sf = SELECTED_FRAME ();
7406 update_menu_bar (sf, 1);
7407 #ifdef HAVE_WINDOW_SYSTEM
7408 update_tool_bar (sf, 1);
7409 #endif
7412 /* Motif needs this. See comment in xmenu.c. Turn it off when
7413 pending_menu_activation is not defined. */
7414 #ifdef USE_X_TOOLKIT
7415 pending_menu_activation = 0;
7416 #endif
7420 /* Update the menu bar item list for frame F. This has to be done
7421 before we start to fill in any display lines, because it can call
7422 eval.
7424 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7426 static void
7427 update_menu_bar (f, save_match_data)
7428 struct frame *f;
7429 int save_match_data;
7431 Lisp_Object window;
7432 register struct window *w;
7434 /* If called recursively during a menu update, do nothing. This can
7435 happen when, for instance, an activate-menubar-hook causes a
7436 redisplay. */
7437 if (inhibit_menubar_update)
7438 return;
7440 window = FRAME_SELECTED_WINDOW (f);
7441 w = XWINDOW (window);
7443 #if 0 /* The if statement below this if statement used to include the
7444 condition !NILP (w->update_mode_line), rather than using
7445 update_mode_lines directly, and this if statement may have
7446 been added to make that condition work. Now the if
7447 statement below matches its comment, this isn't needed. */
7448 if (update_mode_lines)
7449 w->update_mode_line = Qt;
7450 #endif
7452 if (FRAME_WINDOW_P (f)
7454 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7455 FRAME_EXTERNAL_MENU_BAR (f)
7456 #else
7457 FRAME_MENU_BAR_LINES (f) > 0
7458 #endif
7459 : FRAME_MENU_BAR_LINES (f) > 0)
7461 /* If the user has switched buffers or windows, we need to
7462 recompute to reflect the new bindings. But we'll
7463 recompute when update_mode_lines is set too; that means
7464 that people can use force-mode-line-update to request
7465 that the menu bar be recomputed. The adverse effect on
7466 the rest of the redisplay algorithm is about the same as
7467 windows_or_buffers_changed anyway. */
7468 if (windows_or_buffers_changed
7469 /* This used to test w->update_mode_line, but we believe
7470 there is no need to recompute the menu in that case. */
7471 || update_mode_lines
7472 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7473 < BUF_MODIFF (XBUFFER (w->buffer)))
7474 != !NILP (w->last_had_star))
7475 || ((!NILP (Vtransient_mark_mode)
7476 && !NILP (XBUFFER (w->buffer)->mark_active))
7477 != !NILP (w->region_showing)))
7479 struct buffer *prev = current_buffer;
7480 int count = BINDING_STACK_SIZE ();
7482 specbind (Qinhibit_menubar_update, Qt);
7484 set_buffer_internal_1 (XBUFFER (w->buffer));
7485 if (save_match_data)
7486 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7487 if (NILP (Voverriding_local_map_menu_flag))
7489 specbind (Qoverriding_terminal_local_map, Qnil);
7490 specbind (Qoverriding_local_map, Qnil);
7493 /* Run the Lucid hook. */
7494 safe_run_hooks (Qactivate_menubar_hook);
7496 /* If it has changed current-menubar from previous value,
7497 really recompute the menu-bar from the value. */
7498 if (! NILP (Vlucid_menu_bar_dirty_flag))
7499 call0 (Qrecompute_lucid_menubar);
7501 safe_run_hooks (Qmenu_bar_update_hook);
7502 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7504 /* Redisplay the menu bar in case we changed it. */
7505 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7506 if (FRAME_WINDOW_P (f)
7507 #if defined (macintosh)
7508 /* All frames on Mac OS share the same menubar. So only the
7509 selected frame should be allowed to set it. */
7510 && f == SELECTED_FRAME ()
7511 #endif
7513 set_frame_menubar (f, 0, 0);
7514 else
7515 /* On a terminal screen, the menu bar is an ordinary screen
7516 line, and this makes it get updated. */
7517 w->update_mode_line = Qt;
7518 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7519 /* In the non-toolkit version, the menu bar is an ordinary screen
7520 line, and this makes it get updated. */
7521 w->update_mode_line = Qt;
7522 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7524 unbind_to (count, Qnil);
7525 set_buffer_internal_1 (prev);
7532 /***********************************************************************
7533 Tool-bars
7534 ***********************************************************************/
7536 #ifdef HAVE_WINDOW_SYSTEM
7538 /* Update the tool-bar item list for frame F. This has to be done
7539 before we start to fill in any display lines. Called from
7540 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7541 and restore it here. */
7543 static void
7544 update_tool_bar (f, save_match_data)
7545 struct frame *f;
7546 int save_match_data;
7548 if (WINDOWP (f->tool_bar_window)
7549 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7551 Lisp_Object window;
7552 struct window *w;
7554 window = FRAME_SELECTED_WINDOW (f);
7555 w = XWINDOW (window);
7557 /* If the user has switched buffers or windows, we need to
7558 recompute to reflect the new bindings. But we'll
7559 recompute when update_mode_lines is set too; that means
7560 that people can use force-mode-line-update to request
7561 that the menu bar be recomputed. The adverse effect on
7562 the rest of the redisplay algorithm is about the same as
7563 windows_or_buffers_changed anyway. */
7564 if (windows_or_buffers_changed
7565 || !NILP (w->update_mode_line)
7566 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7567 < BUF_MODIFF (XBUFFER (w->buffer)))
7568 != !NILP (w->last_had_star))
7569 || ((!NILP (Vtransient_mark_mode)
7570 && !NILP (XBUFFER (w->buffer)->mark_active))
7571 != !NILP (w->region_showing)))
7573 struct buffer *prev = current_buffer;
7574 int count = BINDING_STACK_SIZE ();
7576 /* Set current_buffer to the buffer of the selected
7577 window of the frame, so that we get the right local
7578 keymaps. */
7579 set_buffer_internal_1 (XBUFFER (w->buffer));
7581 /* Save match data, if we must. */
7582 if (save_match_data)
7583 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7585 /* Make sure that we don't accidentally use bogus keymaps. */
7586 if (NILP (Voverriding_local_map_menu_flag))
7588 specbind (Qoverriding_terminal_local_map, Qnil);
7589 specbind (Qoverriding_local_map, Qnil);
7592 /* Build desired tool-bar items from keymaps. */
7593 f->tool_bar_items
7594 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7596 /* Redisplay the tool-bar in case we changed it. */
7597 w->update_mode_line = Qt;
7599 unbind_to (count, Qnil);
7600 set_buffer_internal_1 (prev);
7606 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7607 F's desired tool-bar contents. F->tool_bar_items must have
7608 been set up previously by calling prepare_menu_bars. */
7610 static void
7611 build_desired_tool_bar_string (f)
7612 struct frame *f;
7614 int i, size, size_needed;
7615 struct gcpro gcpro1, gcpro2, gcpro3;
7616 Lisp_Object image, plist, props;
7618 image = plist = props = Qnil;
7619 GCPRO3 (image, plist, props);
7621 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7622 Otherwise, make a new string. */
7624 /* The size of the string we might be able to reuse. */
7625 size = (STRINGP (f->desired_tool_bar_string)
7626 ? SCHARS (f->desired_tool_bar_string)
7627 : 0);
7629 /* We need one space in the string for each image. */
7630 size_needed = f->n_tool_bar_items;
7632 /* Reuse f->desired_tool_bar_string, if possible. */
7633 if (size < size_needed || NILP (f->desired_tool_bar_string))
7634 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7635 make_number (' '));
7636 else
7638 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7639 Fremove_text_properties (make_number (0), make_number (size),
7640 props, f->desired_tool_bar_string);
7643 /* Put a `display' property on the string for the images to display,
7644 put a `menu_item' property on tool-bar items with a value that
7645 is the index of the item in F's tool-bar item vector. */
7646 for (i = 0; i < f->n_tool_bar_items; ++i)
7648 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7650 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7651 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7652 int hmargin, vmargin, relief, idx, end;
7653 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7655 /* If image is a vector, choose the image according to the
7656 button state. */
7657 image = PROP (TOOL_BAR_ITEM_IMAGES);
7658 if (VECTORP (image))
7660 if (enabled_p)
7661 idx = (selected_p
7662 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7663 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7664 else
7665 idx = (selected_p
7666 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7667 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7669 xassert (ASIZE (image) >= idx);
7670 image = AREF (image, idx);
7672 else
7673 idx = -1;
7675 /* Ignore invalid image specifications. */
7676 if (!valid_image_p (image))
7677 continue;
7679 /* Display the tool-bar button pressed, or depressed. */
7680 plist = Fcopy_sequence (XCDR (image));
7682 /* Compute margin and relief to draw. */
7683 relief = (tool_bar_button_relief >= 0
7684 ? tool_bar_button_relief
7685 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7686 hmargin = vmargin = relief;
7688 if (INTEGERP (Vtool_bar_button_margin)
7689 && XINT (Vtool_bar_button_margin) > 0)
7691 hmargin += XFASTINT (Vtool_bar_button_margin);
7692 vmargin += XFASTINT (Vtool_bar_button_margin);
7694 else if (CONSP (Vtool_bar_button_margin))
7696 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7697 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7698 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7700 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7701 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7702 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7705 if (auto_raise_tool_bar_buttons_p)
7707 /* Add a `:relief' property to the image spec if the item is
7708 selected. */
7709 if (selected_p)
7711 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7712 hmargin -= relief;
7713 vmargin -= relief;
7716 else
7718 /* If image is selected, display it pressed, i.e. with a
7719 negative relief. If it's not selected, display it with a
7720 raised relief. */
7721 plist = Fplist_put (plist, QCrelief,
7722 (selected_p
7723 ? make_number (-relief)
7724 : make_number (relief)));
7725 hmargin -= relief;
7726 vmargin -= relief;
7729 /* Put a margin around the image. */
7730 if (hmargin || vmargin)
7732 if (hmargin == vmargin)
7733 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7734 else
7735 plist = Fplist_put (plist, QCmargin,
7736 Fcons (make_number (hmargin),
7737 make_number (vmargin)));
7740 /* If button is not enabled, and we don't have special images
7741 for the disabled state, make the image appear disabled by
7742 applying an appropriate algorithm to it. */
7743 if (!enabled_p && idx < 0)
7744 plist = Fplist_put (plist, QCconversion, Qdisabled);
7746 /* Put a `display' text property on the string for the image to
7747 display. Put a `menu-item' property on the string that gives
7748 the start of this item's properties in the tool-bar items
7749 vector. */
7750 image = Fcons (Qimage, plist);
7751 props = list4 (Qdisplay, image,
7752 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7754 /* Let the last image hide all remaining spaces in the tool bar
7755 string. The string can be longer than needed when we reuse a
7756 previous string. */
7757 if (i + 1 == f->n_tool_bar_items)
7758 end = SCHARS (f->desired_tool_bar_string);
7759 else
7760 end = i + 1;
7761 Fadd_text_properties (make_number (i), make_number (end),
7762 props, f->desired_tool_bar_string);
7763 #undef PROP
7766 UNGCPRO;
7770 /* Display one line of the tool-bar of frame IT->f. */
7772 static void
7773 display_tool_bar_line (it)
7774 struct it *it;
7776 struct glyph_row *row = it->glyph_row;
7777 int max_x = it->last_visible_x;
7778 struct glyph *last;
7780 prepare_desired_row (row);
7781 row->y = it->current_y;
7783 /* Note that this isn't made use of if the face hasn't a box,
7784 so there's no need to check the face here. */
7785 it->start_of_box_run_p = 1;
7787 while (it->current_x < max_x)
7789 int x_before, x, n_glyphs_before, i, nglyphs;
7791 /* Get the next display element. */
7792 if (!get_next_display_element (it))
7793 break;
7795 /* Produce glyphs. */
7796 x_before = it->current_x;
7797 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7798 PRODUCE_GLYPHS (it);
7800 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7801 i = 0;
7802 x = x_before;
7803 while (i < nglyphs)
7805 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7807 if (x + glyph->pixel_width > max_x)
7809 /* Glyph doesn't fit on line. */
7810 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7811 it->current_x = x;
7812 goto out;
7815 ++it->hpos;
7816 x += glyph->pixel_width;
7817 ++i;
7820 /* Stop at line ends. */
7821 if (ITERATOR_AT_END_OF_LINE_P (it))
7822 break;
7824 set_iterator_to_next (it, 1);
7827 out:;
7829 row->displays_text_p = row->used[TEXT_AREA] != 0;
7830 extend_face_to_end_of_line (it);
7831 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7832 last->right_box_line_p = 1;
7833 if (last == row->glyphs[TEXT_AREA])
7834 last->left_box_line_p = 1;
7835 compute_line_metrics (it);
7837 /* If line is empty, make it occupy the rest of the tool-bar. */
7838 if (!row->displays_text_p)
7840 row->height = row->phys_height = it->last_visible_y - row->y;
7841 row->ascent = row->phys_ascent = 0;
7844 row->full_width_p = 1;
7845 row->continued_p = 0;
7846 row->truncated_on_left_p = 0;
7847 row->truncated_on_right_p = 0;
7849 it->current_x = it->hpos = 0;
7850 it->current_y += row->height;
7851 ++it->vpos;
7852 ++it->glyph_row;
7856 /* Value is the number of screen lines needed to make all tool-bar
7857 items of frame F visible. */
7859 static int
7860 tool_bar_lines_needed (f)
7861 struct frame *f;
7863 struct window *w = XWINDOW (f->tool_bar_window);
7864 struct it it;
7866 /* Initialize an iterator for iteration over
7867 F->desired_tool_bar_string in the tool-bar window of frame F. */
7868 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7869 it.first_visible_x = 0;
7870 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7871 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7873 while (!ITERATOR_AT_END_P (&it))
7875 it.glyph_row = w->desired_matrix->rows;
7876 clear_glyph_row (it.glyph_row);
7877 display_tool_bar_line (&it);
7880 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7884 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7885 0, 1, 0,
7886 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7887 (frame)
7888 Lisp_Object frame;
7890 struct frame *f;
7891 struct window *w;
7892 int nlines = 0;
7894 if (NILP (frame))
7895 frame = selected_frame;
7896 else
7897 CHECK_FRAME (frame);
7898 f = XFRAME (frame);
7900 if (WINDOWP (f->tool_bar_window)
7901 || (w = XWINDOW (f->tool_bar_window),
7902 XFASTINT (w->height) > 0))
7904 update_tool_bar (f, 1);
7905 if (f->n_tool_bar_items)
7907 build_desired_tool_bar_string (f);
7908 nlines = tool_bar_lines_needed (f);
7912 return make_number (nlines);
7916 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7917 height should be changed. */
7919 static int
7920 redisplay_tool_bar (f)
7921 struct frame *f;
7923 struct window *w;
7924 struct it it;
7925 struct glyph_row *row;
7926 int change_height_p = 0;
7928 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7929 do anything. This means you must start with tool-bar-lines
7930 non-zero to get the auto-sizing effect. Or in other words, you
7931 can turn off tool-bars by specifying tool-bar-lines zero. */
7932 if (!WINDOWP (f->tool_bar_window)
7933 || (w = XWINDOW (f->tool_bar_window),
7934 XFASTINT (w->height) == 0))
7935 return 0;
7937 /* Set up an iterator for the tool-bar window. */
7938 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7939 it.first_visible_x = 0;
7940 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7941 row = it.glyph_row;
7943 /* Build a string that represents the contents of the tool-bar. */
7944 build_desired_tool_bar_string (f);
7945 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7947 /* Display as many lines as needed to display all tool-bar items. */
7948 while (it.current_y < it.last_visible_y)
7949 display_tool_bar_line (&it);
7951 /* It doesn't make much sense to try scrolling in the tool-bar
7952 window, so don't do it. */
7953 w->desired_matrix->no_scrolling_p = 1;
7954 w->must_be_updated_p = 1;
7956 if (auto_resize_tool_bars_p)
7958 int nlines;
7960 /* If we couldn't display everything, change the tool-bar's
7961 height. */
7962 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7963 change_height_p = 1;
7965 /* If there are blank lines at the end, except for a partially
7966 visible blank line at the end that is smaller than
7967 CANON_Y_UNIT, change the tool-bar's height. */
7968 row = it.glyph_row - 1;
7969 if (!row->displays_text_p
7970 && row->height >= CANON_Y_UNIT (f))
7971 change_height_p = 1;
7973 /* If row displays tool-bar items, but is partially visible,
7974 change the tool-bar's height. */
7975 if (row->displays_text_p
7976 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7977 change_height_p = 1;
7979 /* Resize windows as needed by changing the `tool-bar-lines'
7980 frame parameter. */
7981 if (change_height_p
7982 && (nlines = tool_bar_lines_needed (f),
7983 nlines != XFASTINT (w->height)))
7985 extern Lisp_Object Qtool_bar_lines;
7986 Lisp_Object frame;
7987 int old_height = XFASTINT (w->height);
7989 XSETFRAME (frame, f);
7990 clear_glyph_matrix (w->desired_matrix);
7991 Fmodify_frame_parameters (frame,
7992 Fcons (Fcons (Qtool_bar_lines,
7993 make_number (nlines)),
7994 Qnil));
7995 if (XFASTINT (w->height) != old_height)
7996 fonts_changed_p = 1;
8000 return change_height_p;
8004 /* Get information about the tool-bar item which is displayed in GLYPH
8005 on frame F. Return in *PROP_IDX the index where tool-bar item
8006 properties start in F->tool_bar_items. Value is zero if
8007 GLYPH doesn't display a tool-bar item. */
8010 tool_bar_item_info (f, glyph, prop_idx)
8011 struct frame *f;
8012 struct glyph *glyph;
8013 int *prop_idx;
8015 Lisp_Object prop;
8016 int success_p;
8017 int charpos;
8019 /* This function can be called asynchronously, which means we must
8020 exclude any possibility that Fget_text_property signals an
8021 error. */
8022 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8023 charpos = max (0, charpos);
8025 /* Get the text property `menu-item' at pos. The value of that
8026 property is the start index of this item's properties in
8027 F->tool_bar_items. */
8028 prop = Fget_text_property (make_number (charpos),
8029 Qmenu_item, f->current_tool_bar_string);
8030 if (INTEGERP (prop))
8032 *prop_idx = XINT (prop);
8033 success_p = 1;
8035 else
8036 success_p = 0;
8038 return success_p;
8041 #endif /* HAVE_WINDOW_SYSTEM */
8045 /************************************************************************
8046 Horizontal scrolling
8047 ************************************************************************/
8049 static int hscroll_window_tree P_ ((Lisp_Object));
8050 static int hscroll_windows P_ ((Lisp_Object));
8052 /* For all leaf windows in the window tree rooted at WINDOW, set their
8053 hscroll value so that PT is (i) visible in the window, and (ii) so
8054 that it is not within a certain margin at the window's left and
8055 right border. Value is non-zero if any window's hscroll has been
8056 changed. */
8058 static int
8059 hscroll_window_tree (window)
8060 Lisp_Object window;
8062 int hscrolled_p = 0;
8063 int hscroll_relative_p = FLOATP (Vhscroll_step);
8064 int hscroll_step_abs = 0;
8065 double hscroll_step_rel = 0;
8067 if (hscroll_relative_p)
8069 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8070 if (hscroll_step_rel < 0)
8072 hscroll_relative_p = 0;
8073 hscroll_step_abs = 0;
8076 else if (INTEGERP (Vhscroll_step))
8078 hscroll_step_abs = XINT (Vhscroll_step);
8079 if (hscroll_step_abs < 0)
8080 hscroll_step_abs = 0;
8082 else
8083 hscroll_step_abs = 0;
8085 while (WINDOWP (window))
8087 struct window *w = XWINDOW (window);
8089 if (WINDOWP (w->hchild))
8090 hscrolled_p |= hscroll_window_tree (w->hchild);
8091 else if (WINDOWP (w->vchild))
8092 hscrolled_p |= hscroll_window_tree (w->vchild);
8093 else if (w->cursor.vpos >= 0)
8095 int h_margin, text_area_x, text_area_y;
8096 int text_area_width, text_area_height;
8097 struct glyph_row *current_cursor_row
8098 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8099 struct glyph_row *desired_cursor_row
8100 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8101 struct glyph_row *cursor_row
8102 = (desired_cursor_row->enabled_p
8103 ? desired_cursor_row
8104 : current_cursor_row);
8106 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8107 &text_area_width, &text_area_height);
8109 /* Scroll when cursor is inside this scroll margin. */
8110 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8112 if ((XFASTINT (w->hscroll)
8113 && w->cursor.x <= h_margin)
8114 || (cursor_row->enabled_p
8115 && cursor_row->truncated_on_right_p
8116 && (w->cursor.x >= text_area_width - h_margin)))
8118 struct it it;
8119 int hscroll;
8120 struct buffer *saved_current_buffer;
8121 int pt;
8122 int wanted_x;
8124 /* Find point in a display of infinite width. */
8125 saved_current_buffer = current_buffer;
8126 current_buffer = XBUFFER (w->buffer);
8128 if (w == XWINDOW (selected_window))
8129 pt = BUF_PT (current_buffer);
8130 else
8132 pt = marker_position (w->pointm);
8133 pt = max (BEGV, pt);
8134 pt = min (ZV, pt);
8137 /* Move iterator to pt starting at cursor_row->start in
8138 a line with infinite width. */
8139 init_to_row_start (&it, w, cursor_row);
8140 it.last_visible_x = INFINITY;
8141 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8142 current_buffer = saved_current_buffer;
8144 /* Position cursor in window. */
8145 if (!hscroll_relative_p && hscroll_step_abs == 0)
8146 hscroll = max (0, it.current_x - text_area_width / 2)
8147 / CANON_X_UNIT (it.f);
8148 else if (w->cursor.x >= text_area_width - h_margin)
8150 if (hscroll_relative_p)
8151 wanted_x = text_area_width * (1 - hscroll_step_rel)
8152 - h_margin;
8153 else
8154 wanted_x = text_area_width
8155 - hscroll_step_abs * CANON_X_UNIT (it.f)
8156 - h_margin;
8157 hscroll
8158 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8160 else
8162 if (hscroll_relative_p)
8163 wanted_x = text_area_width * hscroll_step_rel
8164 + h_margin;
8165 else
8166 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8167 + h_margin;
8168 hscroll
8169 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8171 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8173 /* Don't call Fset_window_hscroll if value hasn't
8174 changed because it will prevent redisplay
8175 optimizations. */
8176 if (XFASTINT (w->hscroll) != hscroll)
8178 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8179 w->hscroll = make_number (hscroll);
8180 hscrolled_p = 1;
8185 window = w->next;
8188 /* Value is non-zero if hscroll of any leaf window has been changed. */
8189 return hscrolled_p;
8193 /* Set hscroll so that cursor is visible and not inside horizontal
8194 scroll margins for all windows in the tree rooted at WINDOW. See
8195 also hscroll_window_tree above. Value is non-zero if any window's
8196 hscroll has been changed. If it has, desired matrices on the frame
8197 of WINDOW are cleared. */
8199 static int
8200 hscroll_windows (window)
8201 Lisp_Object window;
8203 int hscrolled_p;
8205 if (automatic_hscrolling_p)
8207 hscrolled_p = hscroll_window_tree (window);
8208 if (hscrolled_p)
8209 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8211 else
8212 hscrolled_p = 0;
8213 return hscrolled_p;
8218 /************************************************************************
8219 Redisplay
8220 ************************************************************************/
8222 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8223 to a non-zero value. This is sometimes handy to have in a debugger
8224 session. */
8226 #if GLYPH_DEBUG
8228 /* First and last unchanged row for try_window_id. */
8230 int debug_first_unchanged_at_end_vpos;
8231 int debug_last_unchanged_at_beg_vpos;
8233 /* Delta vpos and y. */
8235 int debug_dvpos, debug_dy;
8237 /* Delta in characters and bytes for try_window_id. */
8239 int debug_delta, debug_delta_bytes;
8241 /* Values of window_end_pos and window_end_vpos at the end of
8242 try_window_id. */
8244 EMACS_INT debug_end_pos, debug_end_vpos;
8246 /* Append a string to W->desired_matrix->method. FMT is a printf
8247 format string. A1...A9 are a supplement for a variable-length
8248 argument list. If trace_redisplay_p is non-zero also printf the
8249 resulting string to stderr. */
8251 static void
8252 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8253 struct window *w;
8254 char *fmt;
8255 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8257 char buffer[512];
8258 char *method = w->desired_matrix->method;
8259 int len = strlen (method);
8260 int size = sizeof w->desired_matrix->method;
8261 int remaining = size - len - 1;
8263 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8264 if (len && remaining)
8266 method[len] = '|';
8267 --remaining, ++len;
8270 strncpy (method + len, buffer, remaining);
8272 if (trace_redisplay_p)
8273 fprintf (stderr, "%p (%s): %s\n",
8275 ((BUFFERP (w->buffer)
8276 && STRINGP (XBUFFER (w->buffer)->name))
8277 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8278 : "no buffer"),
8279 buffer);
8282 #endif /* GLYPH_DEBUG */
8285 /* This counter is used to clear the face cache every once in a while
8286 in redisplay_internal. It is incremented for each redisplay.
8287 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8288 cleared. */
8290 #define CLEAR_FACE_CACHE_COUNT 500
8291 static int clear_face_cache_count;
8293 /* Record the previous terminal frame we displayed. */
8295 static struct frame *previous_terminal_frame;
8297 /* Non-zero while redisplay_internal is in progress. */
8299 int redisplaying_p;
8302 /* Value is non-zero if all changes in window W, which displays
8303 current_buffer, are in the text between START and END. START is a
8304 buffer position, END is given as a distance from Z. Used in
8305 redisplay_internal for display optimization. */
8307 static INLINE int
8308 text_outside_line_unchanged_p (w, start, end)
8309 struct window *w;
8310 int start, end;
8312 int unchanged_p = 1;
8314 /* If text or overlays have changed, see where. */
8315 if (XFASTINT (w->last_modified) < MODIFF
8316 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8318 /* Gap in the line? */
8319 if (GPT < start || Z - GPT < end)
8320 unchanged_p = 0;
8322 /* Changes start in front of the line, or end after it? */
8323 if (unchanged_p
8324 && (BEG_UNCHANGED < start - 1
8325 || END_UNCHANGED < end))
8326 unchanged_p = 0;
8328 /* If selective display, can't optimize if changes start at the
8329 beginning of the line. */
8330 if (unchanged_p
8331 && INTEGERP (current_buffer->selective_display)
8332 && XINT (current_buffer->selective_display) > 0
8333 && (BEG_UNCHANGED < start || GPT <= start))
8334 unchanged_p = 0;
8336 /* If there are overlays at the start or end of the line, these
8337 may have overlay strings with newlines in them. A change at
8338 START, for instance, may actually concern the display of such
8339 overlay strings as well, and they are displayed on different
8340 lines. So, quickly rule out this case. (For the future, it
8341 might be desirable to implement something more telling than
8342 just BEG/END_UNCHANGED.) */
8343 if (unchanged_p)
8345 if (BEG + BEG_UNCHANGED == start
8346 && overlay_touches_p (start))
8347 unchanged_p = 0;
8348 if (END_UNCHANGED == end
8349 && overlay_touches_p (Z - end))
8350 unchanged_p = 0;
8354 return unchanged_p;
8358 /* Do a frame update, taking possible shortcuts into account. This is
8359 the main external entry point for redisplay.
8361 If the last redisplay displayed an echo area message and that message
8362 is no longer requested, we clear the echo area or bring back the
8363 mini-buffer if that is in use. */
8365 void
8366 redisplay ()
8368 redisplay_internal (0);
8372 /* Return 1 if point moved out of or into a composition. Otherwise
8373 return 0. PREV_BUF and PREV_PT are the last point buffer and
8374 position. BUF and PT are the current point buffer and position. */
8377 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8378 struct buffer *prev_buf, *buf;
8379 int prev_pt, pt;
8381 int start, end;
8382 Lisp_Object prop;
8383 Lisp_Object buffer;
8385 XSETBUFFER (buffer, buf);
8386 /* Check a composition at the last point if point moved within the
8387 same buffer. */
8388 if (prev_buf == buf)
8390 if (prev_pt == pt)
8391 /* Point didn't move. */
8392 return 0;
8394 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8395 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8396 && COMPOSITION_VALID_P (start, end, prop)
8397 && start < prev_pt && end > prev_pt)
8398 /* The last point was within the composition. Return 1 iff
8399 point moved out of the composition. */
8400 return (pt <= start || pt >= end);
8403 /* Check a composition at the current point. */
8404 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8405 && find_composition (pt, -1, &start, &end, &prop, buffer)
8406 && COMPOSITION_VALID_P (start, end, prop)
8407 && start < pt && end > pt);
8411 /* Reconsider the setting of B->clip_changed which is displayed
8412 in window W. */
8414 static INLINE void
8415 reconsider_clip_changes (w, b)
8416 struct window *w;
8417 struct buffer *b;
8419 if (b->prevent_redisplay_optimizations_p)
8420 b->clip_changed = 1;
8421 else if (b->clip_changed
8422 && !NILP (w->window_end_valid)
8423 && w->current_matrix->buffer == b
8424 && w->current_matrix->zv == BUF_ZV (b)
8425 && w->current_matrix->begv == BUF_BEGV (b))
8426 b->clip_changed = 0;
8428 /* If display wasn't paused, and W is not a tool bar window, see if
8429 point has been moved into or out of a composition. In that case,
8430 we set b->clip_changed to 1 to force updating the screen. If
8431 b->clip_changed has already been set to 1, we can skip this
8432 check. */
8433 if (!b->clip_changed
8434 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8436 int pt;
8438 if (w == XWINDOW (selected_window))
8439 pt = BUF_PT (current_buffer);
8440 else
8441 pt = marker_position (w->pointm);
8443 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8444 || pt != XINT (w->last_point))
8445 && check_point_in_composition (w->current_matrix->buffer,
8446 XINT (w->last_point),
8447 XBUFFER (w->buffer), pt))
8448 b->clip_changed = 1;
8453 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8454 response to any user action; therefore, we should preserve the echo
8455 area. (Actually, our caller does that job.) Perhaps in the future
8456 avoid recentering windows if it is not necessary; currently that
8457 causes some problems. */
8459 static void
8460 redisplay_internal (preserve_echo_area)
8461 int preserve_echo_area;
8463 struct window *w = XWINDOW (selected_window);
8464 struct frame *f = XFRAME (w->frame);
8465 int pause;
8466 int must_finish = 0;
8467 struct text_pos tlbufpos, tlendpos;
8468 int number_of_visible_frames;
8469 int count;
8470 struct frame *sf = SELECTED_FRAME ();
8472 /* Non-zero means redisplay has to consider all windows on all
8473 frames. Zero means, only selected_window is considered. */
8474 int consider_all_windows_p;
8476 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8478 /* No redisplay if running in batch mode or frame is not yet fully
8479 initialized, or redisplay is explicitly turned off by setting
8480 Vinhibit_redisplay. */
8481 if (noninteractive
8482 || !NILP (Vinhibit_redisplay)
8483 || !f->glyphs_initialized_p)
8484 return;
8486 /* The flag redisplay_performed_directly_p is set by
8487 direct_output_for_insert when it already did the whole screen
8488 update necessary. */
8489 if (redisplay_performed_directly_p)
8491 redisplay_performed_directly_p = 0;
8492 if (!hscroll_windows (selected_window))
8493 return;
8496 #ifdef USE_X_TOOLKIT
8497 if (popup_activated ())
8498 return;
8499 #endif
8501 /* I don't think this happens but let's be paranoid. */
8502 if (redisplaying_p)
8503 return;
8505 /* Record a function that resets redisplaying_p to its old value
8506 when we leave this function. */
8507 count = BINDING_STACK_SIZE ();
8508 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8509 ++redisplaying_p;
8511 retry:
8512 pause = 0;
8513 reconsider_clip_changes (w, current_buffer);
8515 /* If new fonts have been loaded that make a glyph matrix adjustment
8516 necessary, do it. */
8517 if (fonts_changed_p)
8519 adjust_glyphs (NULL);
8520 ++windows_or_buffers_changed;
8521 fonts_changed_p = 0;
8524 /* If face_change_count is non-zero, init_iterator will free all
8525 realized faces, which includes the faces referenced from current
8526 matrices. So, we can't reuse current matrices in this case. */
8527 if (face_change_count)
8528 ++windows_or_buffers_changed;
8530 if (! FRAME_WINDOW_P (sf)
8531 && previous_terminal_frame != sf)
8533 /* Since frames on an ASCII terminal share the same display
8534 area, displaying a different frame means redisplay the whole
8535 thing. */
8536 windows_or_buffers_changed++;
8537 SET_FRAME_GARBAGED (sf);
8538 XSETFRAME (Vterminal_frame, sf);
8540 previous_terminal_frame = sf;
8542 /* Set the visible flags for all frames. Do this before checking
8543 for resized or garbaged frames; they want to know if their frames
8544 are visible. See the comment in frame.h for
8545 FRAME_SAMPLE_VISIBILITY. */
8547 Lisp_Object tail, frame;
8549 number_of_visible_frames = 0;
8551 FOR_EACH_FRAME (tail, frame)
8553 struct frame *f = XFRAME (frame);
8555 FRAME_SAMPLE_VISIBILITY (f);
8556 if (FRAME_VISIBLE_P (f))
8557 ++number_of_visible_frames;
8558 clear_desired_matrices (f);
8562 /* Notice any pending interrupt request to change frame size. */
8563 do_pending_window_change (1);
8565 /* Clear frames marked as garbaged. */
8566 if (frame_garbaged)
8567 clear_garbaged_frames ();
8569 /* Build menubar and tool-bar items. */
8570 prepare_menu_bars ();
8572 if (windows_or_buffers_changed)
8573 update_mode_lines++;
8575 /* Detect case that we need to write or remove a star in the mode line. */
8576 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8578 w->update_mode_line = Qt;
8579 if (buffer_shared > 1)
8580 update_mode_lines++;
8583 /* If %c is in the mode line, update it if needed. */
8584 if (!NILP (w->column_number_displayed)
8585 /* This alternative quickly identifies a common case
8586 where no change is needed. */
8587 && !(PT == XFASTINT (w->last_point)
8588 && XFASTINT (w->last_modified) >= MODIFF
8589 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8590 && XFASTINT (w->column_number_displayed) != current_column ())
8591 w->update_mode_line = Qt;
8593 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8595 /* The variable buffer_shared is set in redisplay_window and
8596 indicates that we redisplay a buffer in different windows. See
8597 there. */
8598 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8599 || cursor_type_changed);
8601 /* If specs for an arrow have changed, do thorough redisplay
8602 to ensure we remove any arrow that should no longer exist. */
8603 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8604 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8605 consider_all_windows_p = windows_or_buffers_changed = 1;
8607 /* Normally the message* functions will have already displayed and
8608 updated the echo area, but the frame may have been trashed, or
8609 the update may have been preempted, so display the echo area
8610 again here. Checking message_cleared_p captures the case that
8611 the echo area should be cleared. */
8612 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8613 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8614 || (message_cleared_p
8615 && minibuf_level == 0
8616 /* If the mini-window is currently selected, this means the
8617 echo-area doesn't show through. */
8618 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8620 int window_height_changed_p = echo_area_display (0);
8621 must_finish = 1;
8623 /* If we don't display the current message, don't clear the
8624 message_cleared_p flag, because, if we did, we wouldn't clear
8625 the echo area in the next redisplay which doesn't preserve
8626 the echo area. */
8627 if (!display_last_displayed_message_p)
8628 message_cleared_p = 0;
8630 if (fonts_changed_p)
8631 goto retry;
8632 else if (window_height_changed_p)
8634 consider_all_windows_p = 1;
8635 ++update_mode_lines;
8636 ++windows_or_buffers_changed;
8638 /* If window configuration was changed, frames may have been
8639 marked garbaged. Clear them or we will experience
8640 surprises wrt scrolling. */
8641 if (frame_garbaged)
8642 clear_garbaged_frames ();
8645 else if (EQ (selected_window, minibuf_window)
8646 && (current_buffer->clip_changed
8647 || XFASTINT (w->last_modified) < MODIFF
8648 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8649 && resize_mini_window (w, 0))
8651 /* Resized active mini-window to fit the size of what it is
8652 showing if its contents might have changed. */
8653 must_finish = 1;
8654 consider_all_windows_p = 1;
8655 ++windows_or_buffers_changed;
8656 ++update_mode_lines;
8658 /* If window configuration was changed, frames may have been
8659 marked garbaged. Clear them or we will experience
8660 surprises wrt scrolling. */
8661 if (frame_garbaged)
8662 clear_garbaged_frames ();
8666 /* If showing the region, and mark has changed, we must redisplay
8667 the whole window. The assignment to this_line_start_pos prevents
8668 the optimization directly below this if-statement. */
8669 if (((!NILP (Vtransient_mark_mode)
8670 && !NILP (XBUFFER (w->buffer)->mark_active))
8671 != !NILP (w->region_showing))
8672 || (!NILP (w->region_showing)
8673 && !EQ (w->region_showing,
8674 Fmarker_position (XBUFFER (w->buffer)->mark))))
8675 CHARPOS (this_line_start_pos) = 0;
8677 /* Optimize the case that only the line containing the cursor in the
8678 selected window has changed. Variables starting with this_ are
8679 set in display_line and record information about the line
8680 containing the cursor. */
8681 tlbufpos = this_line_start_pos;
8682 tlendpos = this_line_end_pos;
8683 if (!consider_all_windows_p
8684 && CHARPOS (tlbufpos) > 0
8685 && NILP (w->update_mode_line)
8686 && !current_buffer->clip_changed
8687 && FRAME_VISIBLE_P (XFRAME (w->frame))
8688 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8689 /* Make sure recorded data applies to current buffer, etc. */
8690 && this_line_buffer == current_buffer
8691 && current_buffer == XBUFFER (w->buffer)
8692 && NILP (w->force_start)
8693 /* Point must be on the line that we have info recorded about. */
8694 && PT >= CHARPOS (tlbufpos)
8695 && PT <= Z - CHARPOS (tlendpos)
8696 /* All text outside that line, including its final newline,
8697 must be unchanged */
8698 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8699 CHARPOS (tlendpos)))
8701 if (CHARPOS (tlbufpos) > BEGV
8702 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8703 && (CHARPOS (tlbufpos) == ZV
8704 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8705 /* Former continuation line has disappeared by becoming empty */
8706 goto cancel;
8707 else if (XFASTINT (w->last_modified) < MODIFF
8708 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8709 || MINI_WINDOW_P (w))
8711 /* We have to handle the case of continuation around a
8712 wide-column character (See the comment in indent.c around
8713 line 885).
8715 For instance, in the following case:
8717 -------- Insert --------
8718 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8719 J_I_ ==> J_I_ `^^' are cursors.
8720 ^^ ^^
8721 -------- --------
8723 As we have to redraw the line above, we should goto cancel. */
8725 struct it it;
8726 int line_height_before = this_line_pixel_height;
8728 /* Note that start_display will handle the case that the
8729 line starting at tlbufpos is a continuation lines. */
8730 start_display (&it, w, tlbufpos);
8732 /* Implementation note: It this still necessary? */
8733 if (it.current_x != this_line_start_x)
8734 goto cancel;
8736 TRACE ((stderr, "trying display optimization 1\n"));
8737 w->cursor.vpos = -1;
8738 overlay_arrow_seen = 0;
8739 it.vpos = this_line_vpos;
8740 it.current_y = this_line_y;
8741 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8742 display_line (&it);
8744 /* If line contains point, is not continued,
8745 and ends at same distance from eob as before, we win */
8746 if (w->cursor.vpos >= 0
8747 /* Line is not continued, otherwise this_line_start_pos
8748 would have been set to 0 in display_line. */
8749 && CHARPOS (this_line_start_pos)
8750 /* Line ends as before. */
8751 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8752 /* Line has same height as before. Otherwise other lines
8753 would have to be shifted up or down. */
8754 && this_line_pixel_height == line_height_before)
8756 /* If this is not the window's last line, we must adjust
8757 the charstarts of the lines below. */
8758 if (it.current_y < it.last_visible_y)
8760 struct glyph_row *row
8761 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8762 int delta, delta_bytes;
8764 if (Z - CHARPOS (tlendpos) == ZV)
8766 /* This line ends at end of (accessible part of)
8767 buffer. There is no newline to count. */
8768 delta = (Z
8769 - CHARPOS (tlendpos)
8770 - MATRIX_ROW_START_CHARPOS (row));
8771 delta_bytes = (Z_BYTE
8772 - BYTEPOS (tlendpos)
8773 - MATRIX_ROW_START_BYTEPOS (row));
8775 else
8777 /* This line ends in a newline. Must take
8778 account of the newline and the rest of the
8779 text that follows. */
8780 delta = (Z
8781 - CHARPOS (tlendpos)
8782 - MATRIX_ROW_START_CHARPOS (row));
8783 delta_bytes = (Z_BYTE
8784 - BYTEPOS (tlendpos)
8785 - MATRIX_ROW_START_BYTEPOS (row));
8788 increment_matrix_positions (w->current_matrix,
8789 this_line_vpos + 1,
8790 w->current_matrix->nrows,
8791 delta, delta_bytes);
8794 /* If this row displays text now but previously didn't,
8795 or vice versa, w->window_end_vpos may have to be
8796 adjusted. */
8797 if ((it.glyph_row - 1)->displays_text_p)
8799 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8800 XSETINT (w->window_end_vpos, this_line_vpos);
8802 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8803 && this_line_vpos > 0)
8804 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8805 w->window_end_valid = Qnil;
8807 /* Update hint: No need to try to scroll in update_window. */
8808 w->desired_matrix->no_scrolling_p = 1;
8810 #if GLYPH_DEBUG
8811 *w->desired_matrix->method = 0;
8812 debug_method_add (w, "optimization 1");
8813 #endif
8814 goto update;
8816 else
8817 goto cancel;
8819 else if (/* Cursor position hasn't changed. */
8820 PT == XFASTINT (w->last_point)
8821 /* Make sure the cursor was last displayed
8822 in this window. Otherwise we have to reposition it. */
8823 && 0 <= w->cursor.vpos
8824 && XINT (w->height) > w->cursor.vpos)
8826 if (!must_finish)
8828 do_pending_window_change (1);
8830 /* We used to always goto end_of_redisplay here, but this
8831 isn't enough if we have a blinking cursor. */
8832 if (w->cursor_off_p == w->last_cursor_off_p)
8833 goto end_of_redisplay;
8835 goto update;
8837 /* If highlighting the region, or if the cursor is in the echo area,
8838 then we can't just move the cursor. */
8839 else if (! (!NILP (Vtransient_mark_mode)
8840 && !NILP (current_buffer->mark_active))
8841 && (EQ (selected_window, current_buffer->last_selected_window)
8842 || highlight_nonselected_windows)
8843 && NILP (w->region_showing)
8844 && NILP (Vshow_trailing_whitespace)
8845 && !cursor_in_echo_area)
8847 struct it it;
8848 struct glyph_row *row;
8850 /* Skip from tlbufpos to PT and see where it is. Note that
8851 PT may be in invisible text. If so, we will end at the
8852 next visible position. */
8853 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8854 NULL, DEFAULT_FACE_ID);
8855 it.current_x = this_line_start_x;
8856 it.current_y = this_line_y;
8857 it.vpos = this_line_vpos;
8859 /* The call to move_it_to stops in front of PT, but
8860 moves over before-strings. */
8861 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8863 if (it.vpos == this_line_vpos
8864 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8865 row->enabled_p))
8867 xassert (this_line_vpos == it.vpos);
8868 xassert (this_line_y == it.current_y);
8869 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8870 #if GLYPH_DEBUG
8871 *w->desired_matrix->method = 0;
8872 debug_method_add (w, "optimization 3");
8873 #endif
8874 goto update;
8876 else
8877 goto cancel;
8880 cancel:
8881 /* Text changed drastically or point moved off of line. */
8882 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8885 CHARPOS (this_line_start_pos) = 0;
8886 consider_all_windows_p |= buffer_shared > 1;
8887 ++clear_face_cache_count;
8890 /* Build desired matrices, and update the display. If
8891 consider_all_windows_p is non-zero, do it for all windows on all
8892 frames. Otherwise do it for selected_window, only. */
8894 if (consider_all_windows_p)
8896 Lisp_Object tail, frame;
8897 int i, n = 0, size = 50;
8898 struct frame **updated
8899 = (struct frame **) alloca (size * sizeof *updated);
8901 /* Clear the face cache eventually. */
8902 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8904 clear_face_cache (0);
8905 clear_face_cache_count = 0;
8908 /* Recompute # windows showing selected buffer. This will be
8909 incremented each time such a window is displayed. */
8910 buffer_shared = 0;
8912 FOR_EACH_FRAME (tail, frame)
8914 struct frame *f = XFRAME (frame);
8916 if (FRAME_WINDOW_P (f) || f == sf)
8918 #ifdef HAVE_WINDOW_SYSTEM
8919 if (clear_face_cache_count % 50 == 0
8920 && FRAME_WINDOW_P (f))
8921 clear_image_cache (f, 0);
8922 #endif /* HAVE_WINDOW_SYSTEM */
8924 /* Mark all the scroll bars to be removed; we'll redeem
8925 the ones we want when we redisplay their windows. */
8926 if (condemn_scroll_bars_hook)
8927 condemn_scroll_bars_hook (f);
8929 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8930 redisplay_windows (FRAME_ROOT_WINDOW (f));
8932 /* Any scroll bars which redisplay_windows should have
8933 nuked should now go away. */
8934 if (judge_scroll_bars_hook)
8935 judge_scroll_bars_hook (f);
8937 /* If fonts changed, display again. */
8938 if (fonts_changed_p)
8939 goto retry;
8941 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8943 /* See if we have to hscroll. */
8944 if (hscroll_windows (f->root_window))
8945 goto retry;
8947 /* Prevent various kinds of signals during display
8948 update. stdio is not robust about handling
8949 signals, which can cause an apparent I/O
8950 error. */
8951 if (interrupt_input)
8952 unrequest_sigio ();
8953 stop_polling ();
8955 /* Update the display. */
8956 set_window_update_flags (XWINDOW (f->root_window), 1);
8957 pause |= update_frame (f, 0, 0);
8958 if (pause)
8959 break;
8961 if (n == size)
8963 int nbytes = size * sizeof *updated;
8964 struct frame **p = (struct frame **) alloca (2 * nbytes);
8965 bcopy (updated, p, nbytes);
8966 size *= 2;
8969 updated[n++] = f;
8974 /* Do the mark_window_display_accurate after all windows have
8975 been redisplayed because this call resets flags in buffers
8976 which are needed for proper redisplay. */
8977 for (i = 0; i < n; ++i)
8979 struct frame *f = updated[i];
8980 mark_window_display_accurate (f->root_window, 1);
8981 if (frame_up_to_date_hook)
8982 frame_up_to_date_hook (f);
8985 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8987 Lisp_Object mini_window;
8988 struct frame *mini_frame;
8990 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8991 /* Use list_of_error, not Qerror, so that
8992 we catch only errors and don't run the debugger. */
8993 internal_condition_case_1 (redisplay_window_1, selected_window,
8994 list_of_error,
8995 redisplay_window_error);
8997 /* Compare desired and current matrices, perform output. */
8998 update:
9000 /* If fonts changed, display again. */
9001 if (fonts_changed_p)
9002 goto retry;
9004 /* Prevent various kinds of signals during display update.
9005 stdio is not robust about handling signals,
9006 which can cause an apparent I/O error. */
9007 if (interrupt_input)
9008 unrequest_sigio ();
9009 stop_polling ();
9011 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9013 if (hscroll_windows (selected_window))
9014 goto retry;
9016 XWINDOW (selected_window)->must_be_updated_p = 1;
9017 pause = update_frame (sf, 0, 0);
9020 /* We may have called echo_area_display at the top of this
9021 function. If the echo area is on another frame, that may
9022 have put text on a frame other than the selected one, so the
9023 above call to update_frame would not have caught it. Catch
9024 it here. */
9025 mini_window = FRAME_MINIBUF_WINDOW (sf);
9026 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9028 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9030 XWINDOW (mini_window)->must_be_updated_p = 1;
9031 pause |= update_frame (mini_frame, 0, 0);
9032 if (!pause && hscroll_windows (mini_window))
9033 goto retry;
9037 /* If display was paused because of pending input, make sure we do a
9038 thorough update the next time. */
9039 if (pause)
9041 /* Prevent the optimization at the beginning of
9042 redisplay_internal that tries a single-line update of the
9043 line containing the cursor in the selected window. */
9044 CHARPOS (this_line_start_pos) = 0;
9046 /* Let the overlay arrow be updated the next time. */
9047 if (!NILP (last_arrow_position))
9049 last_arrow_position = Qt;
9050 last_arrow_string = Qt;
9053 /* If we pause after scrolling, some rows in the current
9054 matrices of some windows are not valid. */
9055 if (!WINDOW_FULL_WIDTH_P (w)
9056 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9057 update_mode_lines = 1;
9059 else
9061 if (!consider_all_windows_p)
9063 /* This has already been done above if
9064 consider_all_windows_p is set. */
9065 mark_window_display_accurate_1 (w, 1);
9067 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9068 last_arrow_string = Voverlay_arrow_string;
9070 if (frame_up_to_date_hook != 0)
9071 frame_up_to_date_hook (sf);
9074 update_mode_lines = 0;
9075 windows_or_buffers_changed = 0;
9076 cursor_type_changed = 0;
9079 /* Start SIGIO interrupts coming again. Having them off during the
9080 code above makes it less likely one will discard output, but not
9081 impossible, since there might be stuff in the system buffer here.
9082 But it is much hairier to try to do anything about that. */
9083 if (interrupt_input)
9084 request_sigio ();
9085 start_polling ();
9087 /* If a frame has become visible which was not before, redisplay
9088 again, so that we display it. Expose events for such a frame
9089 (which it gets when becoming visible) don't call the parts of
9090 redisplay constructing glyphs, so simply exposing a frame won't
9091 display anything in this case. So, we have to display these
9092 frames here explicitly. */
9093 if (!pause)
9095 Lisp_Object tail, frame;
9096 int new_count = 0;
9098 FOR_EACH_FRAME (tail, frame)
9100 int this_is_visible = 0;
9102 if (XFRAME (frame)->visible)
9103 this_is_visible = 1;
9104 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9105 if (XFRAME (frame)->visible)
9106 this_is_visible = 1;
9108 if (this_is_visible)
9109 new_count++;
9112 if (new_count != number_of_visible_frames)
9113 windows_or_buffers_changed++;
9116 /* Change frame size now if a change is pending. */
9117 do_pending_window_change (1);
9119 /* If we just did a pending size change, or have additional
9120 visible frames, redisplay again. */
9121 if (windows_or_buffers_changed && !pause)
9122 goto retry;
9124 end_of_redisplay:;
9126 unbind_to (count, Qnil);
9130 /* Redisplay, but leave alone any recent echo area message unless
9131 another message has been requested in its place.
9133 This is useful in situations where you need to redisplay but no
9134 user action has occurred, making it inappropriate for the message
9135 area to be cleared. See tracking_off and
9136 wait_reading_process_input for examples of these situations.
9138 FROM_WHERE is an integer saying from where this function was
9139 called. This is useful for debugging. */
9141 void
9142 redisplay_preserve_echo_area (from_where)
9143 int from_where;
9145 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9147 if (!NILP (echo_area_buffer[1]))
9149 /* We have a previously displayed message, but no current
9150 message. Redisplay the previous message. */
9151 display_last_displayed_message_p = 1;
9152 redisplay_internal (1);
9153 display_last_displayed_message_p = 0;
9155 else
9156 redisplay_internal (1);
9160 /* Function registered with record_unwind_protect in
9161 redisplay_internal. Clears the flag indicating that a redisplay is
9162 in progress. */
9164 static Lisp_Object
9165 unwind_redisplay (old_redisplaying_p)
9166 Lisp_Object old_redisplaying_p;
9168 redisplaying_p = XFASTINT (old_redisplaying_p);
9169 return Qnil;
9173 /* Mark the display of window W as accurate or inaccurate. If
9174 ACCURATE_P is non-zero mark display of W as accurate. If
9175 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9176 redisplay_internal is called. */
9178 static void
9179 mark_window_display_accurate_1 (w, accurate_p)
9180 struct window *w;
9181 int accurate_p;
9183 if (BUFFERP (w->buffer))
9185 struct buffer *b = XBUFFER (w->buffer);
9187 w->last_modified
9188 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9189 w->last_overlay_modified
9190 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9191 w->last_had_star
9192 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9194 if (accurate_p)
9196 b->clip_changed = 0;
9197 b->prevent_redisplay_optimizations_p = 0;
9199 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9200 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9201 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9202 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9204 w->current_matrix->buffer = b;
9205 w->current_matrix->begv = BUF_BEGV (b);
9206 w->current_matrix->zv = BUF_ZV (b);
9208 w->last_cursor = w->cursor;
9209 w->last_cursor_off_p = w->cursor_off_p;
9211 if (w == XWINDOW (selected_window))
9212 w->last_point = make_number (BUF_PT (b));
9213 else
9214 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9218 if (accurate_p)
9220 w->window_end_valid = w->buffer;
9221 #if 0 /* This is incorrect with variable-height lines. */
9222 xassert (XINT (w->window_end_vpos)
9223 < (XINT (w->height)
9224 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9225 #endif
9226 w->update_mode_line = Qnil;
9231 /* Mark the display of windows in the window tree rooted at WINDOW as
9232 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9233 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9234 be redisplayed the next time redisplay_internal is called. */
9236 void
9237 mark_window_display_accurate (window, accurate_p)
9238 Lisp_Object window;
9239 int accurate_p;
9241 struct window *w;
9243 for (; !NILP (window); window = w->next)
9245 w = XWINDOW (window);
9246 mark_window_display_accurate_1 (w, accurate_p);
9248 if (!NILP (w->vchild))
9249 mark_window_display_accurate (w->vchild, accurate_p);
9250 if (!NILP (w->hchild))
9251 mark_window_display_accurate (w->hchild, accurate_p);
9254 if (accurate_p)
9256 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9257 last_arrow_string = Voverlay_arrow_string;
9259 else
9261 /* Force a thorough redisplay the next time by setting
9262 last_arrow_position and last_arrow_string to t, which is
9263 unequal to any useful value of Voverlay_arrow_... */
9264 last_arrow_position = Qt;
9265 last_arrow_string = Qt;
9270 /* Return value in display table DP (Lisp_Char_Table *) for character
9271 C. Since a display table doesn't have any parent, we don't have to
9272 follow parent. Do not call this function directly but use the
9273 macro DISP_CHAR_VECTOR. */
9275 Lisp_Object
9276 disp_char_vector (dp, c)
9277 struct Lisp_Char_Table *dp;
9278 int c;
9280 int code[4], i;
9281 Lisp_Object val;
9283 if (SINGLE_BYTE_CHAR_P (c))
9284 return (dp->contents[c]);
9286 SPLIT_CHAR (c, code[0], code[1], code[2]);
9287 if (code[1] < 32)
9288 code[1] = -1;
9289 else if (code[2] < 32)
9290 code[2] = -1;
9292 /* Here, the possible range of code[0] (== charset ID) is
9293 128..max_charset. Since the top level char table contains data
9294 for multibyte characters after 256th element, we must increment
9295 code[0] by 128 to get a correct index. */
9296 code[0] += 128;
9297 code[3] = -1; /* anchor */
9299 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9301 val = dp->contents[code[i]];
9302 if (!SUB_CHAR_TABLE_P (val))
9303 return (NILP (val) ? dp->defalt : val);
9306 /* Here, val is a sub char table. We return the default value of
9307 it. */
9308 return (dp->defalt);
9313 /***********************************************************************
9314 Window Redisplay
9315 ***********************************************************************/
9317 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9319 static void
9320 redisplay_windows (window)
9321 Lisp_Object window;
9323 while (!NILP (window))
9325 struct window *w = XWINDOW (window);
9327 if (!NILP (w->hchild))
9328 redisplay_windows (w->hchild);
9329 else if (!NILP (w->vchild))
9330 redisplay_windows (w->vchild);
9331 else
9333 displayed_buffer = XBUFFER (w->buffer);
9334 /* Use list_of_error, not Qerror, so that
9335 we catch only errors and don't run the debugger. */
9336 internal_condition_case_1 (redisplay_window_0, window,
9337 list_of_error,
9338 redisplay_window_error);
9341 window = w->next;
9345 static Lisp_Object
9346 redisplay_window_error ()
9348 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9349 return Qnil;
9352 static Lisp_Object
9353 redisplay_window_0 (window)
9354 Lisp_Object window;
9356 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9357 redisplay_window (window, 0);
9358 return Qnil;
9361 static Lisp_Object
9362 redisplay_window_1 (window)
9363 Lisp_Object window;
9365 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9366 redisplay_window (window, 1);
9367 return Qnil;
9370 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9371 DELTA is the number of bytes by which positions recorded in ROW
9372 differ from current buffer positions. */
9374 void
9375 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9376 struct window *w;
9377 struct glyph_row *row;
9378 struct glyph_matrix *matrix;
9379 int delta, delta_bytes, dy, dvpos;
9381 struct glyph *glyph = row->glyphs[TEXT_AREA];
9382 struct glyph *end = glyph + row->used[TEXT_AREA];
9383 int x = row->x;
9384 int pt_old = PT - delta;
9386 /* Skip over glyphs not having an object at the start of the row.
9387 These are special glyphs like truncation marks on terminal
9388 frames. */
9389 if (row->displays_text_p)
9390 while (glyph < end
9391 && INTEGERP (glyph->object)
9392 && glyph->charpos < 0)
9394 x += glyph->pixel_width;
9395 ++glyph;
9398 while (glyph < end
9399 && !INTEGERP (glyph->object)
9400 && (!BUFFERP (glyph->object)
9401 || glyph->charpos < pt_old))
9403 x += glyph->pixel_width;
9404 ++glyph;
9407 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9408 w->cursor.x = x;
9409 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9410 w->cursor.y = row->y + dy;
9412 if (w == XWINDOW (selected_window))
9414 if (!row->continued_p
9415 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9416 && row->x == 0)
9418 this_line_buffer = XBUFFER (w->buffer);
9420 CHARPOS (this_line_start_pos)
9421 = MATRIX_ROW_START_CHARPOS (row) + delta;
9422 BYTEPOS (this_line_start_pos)
9423 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9425 CHARPOS (this_line_end_pos)
9426 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9427 BYTEPOS (this_line_end_pos)
9428 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9430 this_line_y = w->cursor.y;
9431 this_line_pixel_height = row->height;
9432 this_line_vpos = w->cursor.vpos;
9433 this_line_start_x = row->x;
9435 else
9436 CHARPOS (this_line_start_pos) = 0;
9441 /* Run window scroll functions, if any, for WINDOW with new window
9442 start STARTP. Sets the window start of WINDOW to that position.
9444 We assume that the window's buffer is really current. */
9446 static INLINE struct text_pos
9447 run_window_scroll_functions (window, startp)
9448 Lisp_Object window;
9449 struct text_pos startp;
9451 struct window *w = XWINDOW (window);
9452 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9454 if (current_buffer != XBUFFER (w->buffer))
9455 abort ();
9457 if (!NILP (Vwindow_scroll_functions))
9459 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9460 make_number (CHARPOS (startp)));
9461 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9462 /* In case the hook functions switch buffers. */
9463 if (current_buffer != XBUFFER (w->buffer))
9464 set_buffer_internal_1 (XBUFFER (w->buffer));
9467 return startp;
9471 /* Modify the desired matrix of window W and W->vscroll so that the
9472 line containing the cursor is fully visible. If this requires
9473 larger matrices than are allocated, set fonts_changed_p and return
9474 0. */
9476 static int
9477 make_cursor_line_fully_visible (w)
9478 struct window *w;
9480 struct glyph_matrix *matrix;
9481 struct glyph_row *row;
9482 int window_height;
9484 /* It's not always possible to find the cursor, e.g, when a window
9485 is full of overlay strings. Don't do anything in that case. */
9486 if (w->cursor.vpos < 0)
9487 return 1;
9489 matrix = w->desired_matrix;
9490 row = MATRIX_ROW (matrix, w->cursor.vpos);
9492 /* If the cursor row is not partially visible, there's nothing
9493 to do. */
9494 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9495 return 1;
9497 /* If the row the cursor is in is taller than the window's height,
9498 it's not clear what to do, so do nothing. */
9499 window_height = window_box_height (w);
9500 if (row->height >= window_height)
9501 return 1;
9503 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9505 int dy = row->height - row->visible_height;
9506 w->vscroll = 0;
9507 w->cursor.y += dy;
9508 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9510 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9512 int dy = - (row->height - row->visible_height);
9513 w->vscroll = dy;
9514 w->cursor.y += dy;
9515 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9518 /* When we change the cursor y-position of the selected window,
9519 change this_line_y as well so that the display optimization for
9520 the cursor line of the selected window in redisplay_internal uses
9521 the correct y-position. */
9522 if (w == XWINDOW (selected_window))
9523 this_line_y = w->cursor.y;
9525 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9526 redisplay with larger matrices. */
9527 if (matrix->nrows < required_matrix_height (w))
9529 fonts_changed_p = 1;
9530 return 0;
9533 return 1;
9537 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9538 non-zero means only WINDOW is redisplayed in redisplay_internal.
9539 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9540 in redisplay_window to bring a partially visible line into view in
9541 the case that only the cursor has moved.
9543 Value is
9545 1 if scrolling succeeded
9547 0 if scrolling didn't find point.
9549 -1 if new fonts have been loaded so that we must interrupt
9550 redisplay, adjust glyph matrices, and try again. */
9552 enum
9554 SCROLLING_SUCCESS,
9555 SCROLLING_FAILED,
9556 SCROLLING_NEED_LARGER_MATRICES
9559 static int
9560 try_scrolling (window, just_this_one_p, scroll_conservatively,
9561 scroll_step, temp_scroll_step)
9562 Lisp_Object window;
9563 int just_this_one_p;
9564 EMACS_INT scroll_conservatively, scroll_step;
9565 int temp_scroll_step;
9567 struct window *w = XWINDOW (window);
9568 struct frame *f = XFRAME (w->frame);
9569 struct text_pos scroll_margin_pos;
9570 struct text_pos pos;
9571 struct text_pos startp;
9572 struct it it;
9573 Lisp_Object window_end;
9574 int this_scroll_margin;
9575 int dy = 0;
9576 int scroll_max;
9577 int rc;
9578 int amount_to_scroll = 0;
9579 Lisp_Object aggressive;
9580 int height;
9582 #if GLYPH_DEBUG
9583 debug_method_add (w, "try_scrolling");
9584 #endif
9586 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9588 /* Compute scroll margin height in pixels. We scroll when point is
9589 within this distance from the top or bottom of the window. */
9590 if (scroll_margin > 0)
9592 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9593 this_scroll_margin *= CANON_Y_UNIT (f);
9595 else
9596 this_scroll_margin = 0;
9598 /* Compute how much we should try to scroll maximally to bring point
9599 into view. */
9600 if (scroll_step || scroll_conservatively || temp_scroll_step)
9601 scroll_max = max (scroll_step,
9602 max (scroll_conservatively, temp_scroll_step));
9603 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9604 || NUMBERP (current_buffer->scroll_up_aggressively))
9605 /* We're trying to scroll because of aggressive scrolling
9606 but no scroll_step is set. Choose an arbitrary one. Maybe
9607 there should be a variable for this. */
9608 scroll_max = 10;
9609 else
9610 scroll_max = 0;
9611 scroll_max *= CANON_Y_UNIT (f);
9613 /* Decide whether we have to scroll down. Start at the window end
9614 and move this_scroll_margin up to find the position of the scroll
9615 margin. */
9616 window_end = Fwindow_end (window, Qt);
9617 CHARPOS (scroll_margin_pos) = XINT (window_end);
9618 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9619 if (this_scroll_margin)
9621 start_display (&it, w, scroll_margin_pos);
9622 move_it_vertically (&it, - this_scroll_margin);
9623 scroll_margin_pos = it.current.pos;
9626 if (PT >= CHARPOS (scroll_margin_pos))
9628 int y0;
9630 /* Point is in the scroll margin at the bottom of the window, or
9631 below. Compute a new window start that makes point visible. */
9633 /* Compute the distance from the scroll margin to PT.
9634 Give up if the distance is greater than scroll_max. */
9635 start_display (&it, w, scroll_margin_pos);
9636 y0 = it.current_y;
9637 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9638 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9640 /* To make point visible, we have to move the window start
9641 down so that the line the cursor is in is visible, which
9642 means we have to add in the height of the cursor line. */
9643 dy = line_bottom_y (&it) - y0;
9645 if (dy > scroll_max)
9646 return SCROLLING_FAILED;
9648 /* Move the window start down. If scrolling conservatively,
9649 move it just enough down to make point visible. If
9650 scroll_step is set, move it down by scroll_step. */
9651 start_display (&it, w, startp);
9653 if (scroll_conservatively)
9654 amount_to_scroll
9655 = max (max (dy, CANON_Y_UNIT (f)),
9656 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9657 else if (scroll_step || temp_scroll_step)
9658 amount_to_scroll = scroll_max;
9659 else
9661 aggressive = current_buffer->scroll_up_aggressively;
9662 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9663 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9664 if (NUMBERP (aggressive))
9665 amount_to_scroll = XFLOATINT (aggressive) * height;
9668 if (amount_to_scroll <= 0)
9669 return SCROLLING_FAILED;
9671 move_it_vertically (&it, amount_to_scroll);
9672 startp = it.current.pos;
9674 else
9676 /* See if point is inside the scroll margin at the top of the
9677 window. */
9678 scroll_margin_pos = startp;
9679 if (this_scroll_margin)
9681 start_display (&it, w, startp);
9682 move_it_vertically (&it, this_scroll_margin);
9683 scroll_margin_pos = it.current.pos;
9686 if (PT < CHARPOS (scroll_margin_pos))
9688 /* Point is in the scroll margin at the top of the window or
9689 above what is displayed in the window. */
9690 int y0;
9692 /* Compute the vertical distance from PT to the scroll
9693 margin position. Give up if distance is greater than
9694 scroll_max. */
9695 SET_TEXT_POS (pos, PT, PT_BYTE);
9696 start_display (&it, w, pos);
9697 y0 = it.current_y;
9698 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9699 it.last_visible_y, -1,
9700 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9701 dy = it.current_y - y0;
9702 if (dy > scroll_max)
9703 return SCROLLING_FAILED;
9705 /* Compute new window start. */
9706 start_display (&it, w, startp);
9708 if (scroll_conservatively)
9709 amount_to_scroll =
9710 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9711 else if (scroll_step || temp_scroll_step)
9712 amount_to_scroll = scroll_max;
9713 else
9715 aggressive = current_buffer->scroll_down_aggressively;
9716 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9717 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9718 if (NUMBERP (aggressive))
9719 amount_to_scroll = XFLOATINT (aggressive) * height;
9722 if (amount_to_scroll <= 0)
9723 return SCROLLING_FAILED;
9725 move_it_vertically (&it, - amount_to_scroll);
9726 startp = it.current.pos;
9730 /* Run window scroll functions. */
9731 startp = run_window_scroll_functions (window, startp);
9733 /* Display the window. Give up if new fonts are loaded, or if point
9734 doesn't appear. */
9735 if (!try_window (window, startp))
9736 rc = SCROLLING_NEED_LARGER_MATRICES;
9737 else if (w->cursor.vpos < 0)
9739 clear_glyph_matrix (w->desired_matrix);
9740 rc = SCROLLING_FAILED;
9742 else
9744 /* Maybe forget recorded base line for line number display. */
9745 if (!just_this_one_p
9746 || current_buffer->clip_changed
9747 || BEG_UNCHANGED < CHARPOS (startp))
9748 w->base_line_number = Qnil;
9750 /* If cursor ends up on a partially visible line, shift display
9751 lines up or down. If that fails because we need larger
9752 matrices, give up. */
9753 if (!make_cursor_line_fully_visible (w))
9754 rc = SCROLLING_NEED_LARGER_MATRICES;
9755 else
9756 rc = SCROLLING_SUCCESS;
9759 return rc;
9763 /* Compute a suitable window start for window W if display of W starts
9764 on a continuation line. Value is non-zero if a new window start
9765 was computed.
9767 The new window start will be computed, based on W's width, starting
9768 from the start of the continued line. It is the start of the
9769 screen line with the minimum distance from the old start W->start. */
9771 static int
9772 compute_window_start_on_continuation_line (w)
9773 struct window *w;
9775 struct text_pos pos, start_pos;
9776 int window_start_changed_p = 0;
9778 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9780 /* If window start is on a continuation line... Window start may be
9781 < BEGV in case there's invisible text at the start of the
9782 buffer (M-x rmail, for example). */
9783 if (CHARPOS (start_pos) > BEGV
9784 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9786 struct it it;
9787 struct glyph_row *row;
9789 /* Handle the case that the window start is out of range. */
9790 if (CHARPOS (start_pos) < BEGV)
9791 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9792 else if (CHARPOS (start_pos) > ZV)
9793 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9795 /* Find the start of the continued line. This should be fast
9796 because scan_buffer is fast (newline cache). */
9797 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9798 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9799 row, DEFAULT_FACE_ID);
9800 reseat_at_previous_visible_line_start (&it);
9802 /* If the line start is "too far" away from the window start,
9803 say it takes too much time to compute a new window start. */
9804 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9805 < XFASTINT (w->height) * XFASTINT (w->width))
9807 int min_distance, distance;
9809 /* Move forward by display lines to find the new window
9810 start. If window width was enlarged, the new start can
9811 be expected to be > the old start. If window width was
9812 decreased, the new window start will be < the old start.
9813 So, we're looking for the display line start with the
9814 minimum distance from the old window start. */
9815 pos = it.current.pos;
9816 min_distance = INFINITY;
9817 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9818 distance < min_distance)
9820 min_distance = distance;
9821 pos = it.current.pos;
9822 move_it_by_lines (&it, 1, 0);
9825 /* Set the window start there. */
9826 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9827 window_start_changed_p = 1;
9831 return window_start_changed_p;
9835 /* Try cursor movement in case text has not changed in window WINDOW,
9836 with window start STARTP. Value is
9838 CURSOR_MOVEMENT_SUCCESS if successful
9840 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9842 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9843 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9844 we want to scroll as if scroll-step were set to 1. See the code.
9846 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9847 which case we have to abort this redisplay, and adjust matrices
9848 first. */
9850 enum
9852 CURSOR_MOVEMENT_SUCCESS,
9853 CURSOR_MOVEMENT_CANNOT_BE_USED,
9854 CURSOR_MOVEMENT_MUST_SCROLL,
9855 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9858 static int
9859 try_cursor_movement (window, startp, scroll_step)
9860 Lisp_Object window;
9861 struct text_pos startp;
9862 int *scroll_step;
9864 struct window *w = XWINDOW (window);
9865 struct frame *f = XFRAME (w->frame);
9866 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9868 #if GLYPH_DEBUG
9869 if (inhibit_try_cursor_movement)
9870 return rc;
9871 #endif
9873 /* Handle case where text has not changed, only point, and it has
9874 not moved off the frame. */
9875 if (/* Point may be in this window. */
9876 PT >= CHARPOS (startp)
9877 /* Selective display hasn't changed. */
9878 && !current_buffer->clip_changed
9879 /* Function force-mode-line-update is used to force a thorough
9880 redisplay. It sets either windows_or_buffers_changed or
9881 update_mode_lines. So don't take a shortcut here for these
9882 cases. */
9883 && !update_mode_lines
9884 && !windows_or_buffers_changed
9885 && !cursor_type_changed
9886 /* Can't use this case if highlighting a region. When a
9887 region exists, cursor movement has to do more than just
9888 set the cursor. */
9889 && !(!NILP (Vtransient_mark_mode)
9890 && !NILP (current_buffer->mark_active))
9891 && NILP (w->region_showing)
9892 && NILP (Vshow_trailing_whitespace)
9893 /* Right after splitting windows, last_point may be nil. */
9894 && INTEGERP (w->last_point)
9895 /* This code is not used for mini-buffer for the sake of the case
9896 of redisplaying to replace an echo area message; since in
9897 that case the mini-buffer contents per se are usually
9898 unchanged. This code is of no real use in the mini-buffer
9899 since the handling of this_line_start_pos, etc., in redisplay
9900 handles the same cases. */
9901 && !EQ (window, minibuf_window)
9902 /* When splitting windows or for new windows, it happens that
9903 redisplay is called with a nil window_end_vpos or one being
9904 larger than the window. This should really be fixed in
9905 window.c. I don't have this on my list, now, so we do
9906 approximately the same as the old redisplay code. --gerd. */
9907 && INTEGERP (w->window_end_vpos)
9908 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9909 && (FRAME_WINDOW_P (f)
9910 || !MARKERP (Voverlay_arrow_position)
9911 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9913 int this_scroll_margin;
9914 struct glyph_row *row = NULL;
9916 #if GLYPH_DEBUG
9917 debug_method_add (w, "cursor movement");
9918 #endif
9920 /* Scroll if point within this distance from the top or bottom
9921 of the window. This is a pixel value. */
9922 this_scroll_margin = max (0, scroll_margin);
9923 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9924 this_scroll_margin *= CANON_Y_UNIT (f);
9926 /* Start with the row the cursor was displayed during the last
9927 not paused redisplay. Give up if that row is not valid. */
9928 if (w->last_cursor.vpos < 0
9929 || w->last_cursor.vpos >= w->current_matrix->nrows)
9930 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9931 else
9933 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9934 if (row->mode_line_p)
9935 ++row;
9936 if (!row->enabled_p)
9937 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9940 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9942 int scroll_p = 0;
9943 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9945 if (PT > XFASTINT (w->last_point))
9947 /* Point has moved forward. */
9948 while (MATRIX_ROW_END_CHARPOS (row) < PT
9949 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9951 xassert (row->enabled_p);
9952 ++row;
9955 /* The end position of a row equals the start position
9956 of the next row. If PT is there, we would rather
9957 display it in the next line. */
9958 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9959 && MATRIX_ROW_END_CHARPOS (row) == PT
9960 && !cursor_row_p (w, row))
9961 ++row;
9963 /* If within the scroll margin, scroll. Note that
9964 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9965 the next line would be drawn, and that
9966 this_scroll_margin can be zero. */
9967 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9968 || PT > MATRIX_ROW_END_CHARPOS (row)
9969 /* Line is completely visible last line in window
9970 and PT is to be set in the next line. */
9971 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9972 && PT == MATRIX_ROW_END_CHARPOS (row)
9973 && !row->ends_at_zv_p
9974 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9975 scroll_p = 1;
9977 else if (PT < XFASTINT (w->last_point))
9979 /* Cursor has to be moved backward. Note that PT >=
9980 CHARPOS (startp) because of the outer
9981 if-statement. */
9982 while (!row->mode_line_p
9983 && (MATRIX_ROW_START_CHARPOS (row) > PT
9984 || (MATRIX_ROW_START_CHARPOS (row) == PT
9985 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9986 && (row->y > this_scroll_margin
9987 || CHARPOS (startp) == BEGV))
9989 xassert (row->enabled_p);
9990 --row;
9993 /* Consider the following case: Window starts at BEGV,
9994 there is invisible, intangible text at BEGV, so that
9995 display starts at some point START > BEGV. It can
9996 happen that we are called with PT somewhere between
9997 BEGV and START. Try to handle that case. */
9998 if (row < w->current_matrix->rows
9999 || row->mode_line_p)
10001 row = w->current_matrix->rows;
10002 if (row->mode_line_p)
10003 ++row;
10006 /* Due to newlines in overlay strings, we may have to
10007 skip forward over overlay strings. */
10008 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10009 && MATRIX_ROW_END_CHARPOS (row) == PT
10010 && !cursor_row_p (w, row))
10011 ++row;
10013 /* If within the scroll margin, scroll. */
10014 if (row->y < this_scroll_margin
10015 && CHARPOS (startp) != BEGV)
10016 scroll_p = 1;
10019 if (PT < MATRIX_ROW_START_CHARPOS (row)
10020 || PT > MATRIX_ROW_END_CHARPOS (row))
10022 /* if PT is not in the glyph row, give up. */
10023 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10025 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10027 if (PT == MATRIX_ROW_END_CHARPOS (row)
10028 && !row->ends_at_zv_p
10029 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10030 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10031 else if (row->height > window_box_height (w))
10033 /* If we end up in a partially visible line, let's
10034 make it fully visible, except when it's taller
10035 than the window, in which case we can't do much
10036 about it. */
10037 *scroll_step = 1;
10038 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10040 else
10042 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10043 try_window (window, startp);
10044 if (!make_cursor_line_fully_visible (w))
10045 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
10046 else
10047 rc = CURSOR_MOVEMENT_SUCCESS;
10050 else if (scroll_p)
10051 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10052 else
10054 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10055 rc = CURSOR_MOVEMENT_SUCCESS;
10060 return rc;
10064 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10065 selected_window is redisplayed. */
10067 static void
10068 redisplay_window (window, just_this_one_p)
10069 Lisp_Object window;
10070 int just_this_one_p;
10072 struct window *w = XWINDOW (window);
10073 struct frame *f = XFRAME (w->frame);
10074 struct buffer *buffer = XBUFFER (w->buffer);
10075 struct buffer *old = current_buffer;
10076 struct text_pos lpoint, opoint, startp;
10077 int update_mode_line;
10078 int tem;
10079 struct it it;
10080 /* Record it now because it's overwritten. */
10081 int current_matrix_up_to_date_p = 0;
10082 int temp_scroll_step = 0;
10083 int count = BINDING_STACK_SIZE ();
10084 int rc;
10086 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10087 opoint = lpoint;
10089 /* W must be a leaf window here. */
10090 xassert (!NILP (w->buffer));
10091 #if GLYPH_DEBUG
10092 *w->desired_matrix->method = 0;
10093 #endif
10095 specbind (Qinhibit_point_motion_hooks, Qt);
10097 reconsider_clip_changes (w, buffer);
10099 /* Has the mode line to be updated? */
10100 update_mode_line = (!NILP (w->update_mode_line)
10101 || update_mode_lines
10102 || buffer->clip_changed);
10104 if (MINI_WINDOW_P (w))
10106 if (w == XWINDOW (echo_area_window)
10107 && !NILP (echo_area_buffer[0]))
10109 if (update_mode_line)
10110 /* We may have to update a tty frame's menu bar or a
10111 tool-bar. Example `M-x C-h C-h C-g'. */
10112 goto finish_menu_bars;
10113 else
10114 /* We've already displayed the echo area glyphs in this window. */
10115 goto finish_scroll_bars;
10117 else if (w != XWINDOW (minibuf_window))
10119 /* W is a mini-buffer window, but it's not the currently
10120 active one, so clear it. */
10121 int yb = window_text_bottom_y (w);
10122 struct glyph_row *row;
10123 int y;
10125 for (y = 0, row = w->desired_matrix->rows;
10126 y < yb;
10127 y += row->height, ++row)
10128 blank_row (w, row, y);
10129 goto finish_scroll_bars;
10132 clear_glyph_matrix (w->desired_matrix);
10135 /* Otherwise set up data on this window; select its buffer and point
10136 value. */
10137 /* Really select the buffer, for the sake of buffer-local
10138 variables. */
10139 set_buffer_internal_1 (XBUFFER (w->buffer));
10140 SET_TEXT_POS (opoint, PT, PT_BYTE);
10142 current_matrix_up_to_date_p
10143 = (!NILP (w->window_end_valid)
10144 && !current_buffer->clip_changed
10145 && XFASTINT (w->last_modified) >= MODIFF
10146 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10148 /* When windows_or_buffers_changed is non-zero, we can't rely on
10149 the window end being valid, so set it to nil there. */
10150 if (windows_or_buffers_changed)
10152 /* If window starts on a continuation line, maybe adjust the
10153 window start in case the window's width changed. */
10154 if (XMARKER (w->start)->buffer == current_buffer)
10155 compute_window_start_on_continuation_line (w);
10157 w->window_end_valid = Qnil;
10160 /* Some sanity checks. */
10161 CHECK_WINDOW_END (w);
10162 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10163 abort ();
10164 if (BYTEPOS (opoint) < CHARPOS (opoint))
10165 abort ();
10167 /* If %c is in mode line, update it if needed. */
10168 if (!NILP (w->column_number_displayed)
10169 /* This alternative quickly identifies a common case
10170 where no change is needed. */
10171 && !(PT == XFASTINT (w->last_point)
10172 && XFASTINT (w->last_modified) >= MODIFF
10173 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10174 && XFASTINT (w->column_number_displayed) != current_column ())
10175 update_mode_line = 1;
10177 /* Count number of windows showing the selected buffer. An indirect
10178 buffer counts as its base buffer. */
10179 if (!just_this_one_p)
10181 struct buffer *current_base, *window_base;
10182 current_base = current_buffer;
10183 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10184 if (current_base->base_buffer)
10185 current_base = current_base->base_buffer;
10186 if (window_base->base_buffer)
10187 window_base = window_base->base_buffer;
10188 if (current_base == window_base)
10189 buffer_shared++;
10192 /* Point refers normally to the selected window. For any other
10193 window, set up appropriate value. */
10194 if (!EQ (window, selected_window))
10196 int new_pt = XMARKER (w->pointm)->charpos;
10197 int new_pt_byte = marker_byte_position (w->pointm);
10198 if (new_pt < BEGV)
10200 new_pt = BEGV;
10201 new_pt_byte = BEGV_BYTE;
10202 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10204 else if (new_pt > (ZV - 1))
10206 new_pt = ZV;
10207 new_pt_byte = ZV_BYTE;
10208 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10211 /* We don't use SET_PT so that the point-motion hooks don't run. */
10212 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10215 /* If any of the character widths specified in the display table
10216 have changed, invalidate the width run cache. It's true that
10217 this may be a bit late to catch such changes, but the rest of
10218 redisplay goes (non-fatally) haywire when the display table is
10219 changed, so why should we worry about doing any better? */
10220 if (current_buffer->width_run_cache)
10222 struct Lisp_Char_Table *disptab = buffer_display_table ();
10224 if (! disptab_matches_widthtab (disptab,
10225 XVECTOR (current_buffer->width_table)))
10227 invalidate_region_cache (current_buffer,
10228 current_buffer->width_run_cache,
10229 BEG, Z);
10230 recompute_width_table (current_buffer, disptab);
10234 /* If window-start is screwed up, choose a new one. */
10235 if (XMARKER (w->start)->buffer != current_buffer)
10236 goto recenter;
10238 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10240 /* If someone specified a new starting point but did not insist,
10241 check whether it can be used. */
10242 if (!NILP (w->optional_new_start)
10243 && CHARPOS (startp) >= BEGV
10244 && CHARPOS (startp) <= ZV)
10246 w->optional_new_start = Qnil;
10247 start_display (&it, w, startp);
10248 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10249 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10250 if (IT_CHARPOS (it) == PT)
10251 w->force_start = Qt;
10254 /* Handle case where place to start displaying has been specified,
10255 unless the specified location is outside the accessible range. */
10256 if (!NILP (w->force_start)
10257 || w->frozen_window_start_p)
10259 w->force_start = Qnil;
10260 w->vscroll = 0;
10261 w->window_end_valid = Qnil;
10263 /* Forget any recorded base line for line number display. */
10264 if (!current_matrix_up_to_date_p
10265 || current_buffer->clip_changed)
10266 w->base_line_number = Qnil;
10268 /* Redisplay the mode line. Select the buffer properly for that.
10269 Also, run the hook window-scroll-functions
10270 because we have scrolled. */
10271 /* Note, we do this after clearing force_start because
10272 if there's an error, it is better to forget about force_start
10273 than to get into an infinite loop calling the hook functions
10274 and having them get more errors. */
10275 if (!update_mode_line
10276 || ! NILP (Vwindow_scroll_functions))
10278 update_mode_line = 1;
10279 w->update_mode_line = Qt;
10280 startp = run_window_scroll_functions (window, startp);
10283 w->last_modified = make_number (0);
10284 w->last_overlay_modified = make_number (0);
10285 if (CHARPOS (startp) < BEGV)
10286 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10287 else if (CHARPOS (startp) > ZV)
10288 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10290 /* Redisplay, then check if cursor has been set during the
10291 redisplay. Give up if new fonts were loaded. */
10292 if (!try_window (window, startp))
10294 w->force_start = Qt;
10295 clear_glyph_matrix (w->desired_matrix);
10296 goto finish_scroll_bars;
10299 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10301 /* If point does not appear, try to move point so it does
10302 appear. The desired matrix has been built above, so we
10303 can use it here. */
10304 int window_height;
10305 struct glyph_row *row;
10307 window_height = window_box_height (w) / 2;
10308 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10309 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10310 ++row;
10312 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10313 MATRIX_ROW_START_BYTEPOS (row));
10315 if (w != XWINDOW (selected_window))
10316 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10317 else if (current_buffer == old)
10318 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10320 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10322 /* If we are highlighting the region, then we just changed
10323 the region, so redisplay to show it. */
10324 if (!NILP (Vtransient_mark_mode)
10325 && !NILP (current_buffer->mark_active))
10327 clear_glyph_matrix (w->desired_matrix);
10328 if (!try_window (window, startp))
10329 goto need_larger_matrices;
10333 if (!make_cursor_line_fully_visible (w))
10334 goto need_larger_matrices;
10335 #if GLYPH_DEBUG
10336 debug_method_add (w, "forced window start");
10337 #endif
10338 goto done;
10341 /* Handle case where text has not changed, only point, and it has
10342 not moved off the frame. */
10343 if (current_matrix_up_to_date_p
10344 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10345 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10347 switch (rc)
10349 case CURSOR_MOVEMENT_SUCCESS:
10350 goto done;
10352 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10353 goto need_larger_matrices;
10355 case CURSOR_MOVEMENT_MUST_SCROLL:
10356 goto try_to_scroll;
10358 default:
10359 abort ();
10362 /* If current starting point was originally the beginning of a line
10363 but no longer is, find a new starting point. */
10364 else if (!NILP (w->start_at_line_beg)
10365 && !(CHARPOS (startp) <= BEGV
10366 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10368 #if GLYPH_DEBUG
10369 debug_method_add (w, "recenter 1");
10370 #endif
10371 goto recenter;
10374 /* Try scrolling with try_window_id. Value is > 0 if update has
10375 been done, it is -1 if we know that the same window start will
10376 not work. It is 0 if unsuccessful for some other reason. */
10377 else if ((tem = try_window_id (w)) != 0)
10379 #if GLYPH_DEBUG
10380 debug_method_add (w, "try_window_id %d", tem);
10381 #endif
10383 if (fonts_changed_p)
10384 goto need_larger_matrices;
10385 if (tem > 0)
10386 goto done;
10388 /* Otherwise try_window_id has returned -1 which means that we
10389 don't want the alternative below this comment to execute. */
10391 else if (CHARPOS (startp) >= BEGV
10392 && CHARPOS (startp) <= ZV
10393 && PT >= CHARPOS (startp)
10394 && (CHARPOS (startp) < ZV
10395 /* Avoid starting at end of buffer. */
10396 || CHARPOS (startp) == BEGV
10397 || (XFASTINT (w->last_modified) >= MODIFF
10398 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10400 #if GLYPH_DEBUG
10401 debug_method_add (w, "same window start");
10402 #endif
10404 /* Try to redisplay starting at same place as before.
10405 If point has not moved off frame, accept the results. */
10406 if (!current_matrix_up_to_date_p
10407 /* Don't use try_window_reusing_current_matrix in this case
10408 because a window scroll function can have changed the
10409 buffer. */
10410 || !NILP (Vwindow_scroll_functions)
10411 || MINI_WINDOW_P (w)
10412 || !try_window_reusing_current_matrix (w))
10414 IF_DEBUG (debug_method_add (w, "1"));
10415 try_window (window, startp);
10418 if (fonts_changed_p)
10419 goto need_larger_matrices;
10421 if (w->cursor.vpos >= 0)
10423 if (!just_this_one_p
10424 || current_buffer->clip_changed
10425 || BEG_UNCHANGED < CHARPOS (startp))
10426 /* Forget any recorded base line for line number display. */
10427 w->base_line_number = Qnil;
10429 if (!make_cursor_line_fully_visible (w))
10430 goto need_larger_matrices;
10431 goto done;
10433 else
10434 clear_glyph_matrix (w->desired_matrix);
10437 try_to_scroll:
10439 w->last_modified = make_number (0);
10440 w->last_overlay_modified = make_number (0);
10442 /* Redisplay the mode line. Select the buffer properly for that. */
10443 if (!update_mode_line)
10445 update_mode_line = 1;
10446 w->update_mode_line = Qt;
10449 /* Try to scroll by specified few lines. */
10450 if ((scroll_conservatively
10451 || scroll_step
10452 || temp_scroll_step
10453 || NUMBERP (current_buffer->scroll_up_aggressively)
10454 || NUMBERP (current_buffer->scroll_down_aggressively))
10455 && !current_buffer->clip_changed
10456 && CHARPOS (startp) >= BEGV
10457 && CHARPOS (startp) <= ZV)
10459 /* The function returns -1 if new fonts were loaded, 1 if
10460 successful, 0 if not successful. */
10461 int rc = try_scrolling (window, just_this_one_p,
10462 scroll_conservatively,
10463 scroll_step,
10464 temp_scroll_step);
10465 switch (rc)
10467 case SCROLLING_SUCCESS:
10468 goto done;
10470 case SCROLLING_NEED_LARGER_MATRICES:
10471 goto need_larger_matrices;
10473 case SCROLLING_FAILED:
10474 break;
10476 default:
10477 abort ();
10481 /* Finally, just choose place to start which centers point */
10483 recenter:
10485 #if GLYPH_DEBUG
10486 debug_method_add (w, "recenter");
10487 #endif
10489 /* w->vscroll = 0; */
10491 /* Forget any previously recorded base line for line number display. */
10492 if (!current_matrix_up_to_date_p
10493 || current_buffer->clip_changed)
10494 w->base_line_number = Qnil;
10496 /* Move backward half the height of the window. */
10497 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10498 it.current_y = it.last_visible_y;
10499 move_it_vertically_backward (&it, window_box_height (w) / 2);
10500 xassert (IT_CHARPOS (it) >= BEGV);
10502 /* The function move_it_vertically_backward may move over more
10503 than the specified y-distance. If it->w is small, e.g. a
10504 mini-buffer window, we may end up in front of the window's
10505 display area. Start displaying at the start of the line
10506 containing PT in this case. */
10507 if (it.current_y <= 0)
10509 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10510 move_it_vertically (&it, 0);
10511 xassert (IT_CHARPOS (it) <= PT);
10512 it.current_y = 0;
10515 it.current_x = it.hpos = 0;
10517 /* Set startp here explicitly in case that helps avoid an infinite loop
10518 in case the window-scroll-functions functions get errors. */
10519 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10521 /* Run scroll hooks. */
10522 startp = run_window_scroll_functions (window, it.current.pos);
10524 /* Redisplay the window. */
10525 if (!current_matrix_up_to_date_p
10526 || windows_or_buffers_changed
10527 || cursor_type_changed
10528 /* Don't use try_window_reusing_current_matrix in this case
10529 because it can have changed the buffer. */
10530 || !NILP (Vwindow_scroll_functions)
10531 || !just_this_one_p
10532 || MINI_WINDOW_P (w)
10533 || !try_window_reusing_current_matrix (w))
10534 try_window (window, startp);
10536 /* If new fonts have been loaded (due to fontsets), give up. We
10537 have to start a new redisplay since we need to re-adjust glyph
10538 matrices. */
10539 if (fonts_changed_p)
10540 goto need_larger_matrices;
10542 /* If cursor did not appear assume that the middle of the window is
10543 in the first line of the window. Do it again with the next line.
10544 (Imagine a window of height 100, displaying two lines of height
10545 60. Moving back 50 from it->last_visible_y will end in the first
10546 line.) */
10547 if (w->cursor.vpos < 0)
10549 if (!NILP (w->window_end_valid)
10550 && PT >= Z - XFASTINT (w->window_end_pos))
10552 clear_glyph_matrix (w->desired_matrix);
10553 move_it_by_lines (&it, 1, 0);
10554 try_window (window, it.current.pos);
10556 else if (PT < IT_CHARPOS (it))
10558 clear_glyph_matrix (w->desired_matrix);
10559 move_it_by_lines (&it, -1, 0);
10560 try_window (window, it.current.pos);
10562 else
10564 /* Not much we can do about it. */
10568 /* Consider the following case: Window starts at BEGV, there is
10569 invisible, intangible text at BEGV, so that display starts at
10570 some point START > BEGV. It can happen that we are called with
10571 PT somewhere between BEGV and START. Try to handle that case. */
10572 if (w->cursor.vpos < 0)
10574 struct glyph_row *row = w->current_matrix->rows;
10575 if (row->mode_line_p)
10576 ++row;
10577 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10580 if (!make_cursor_line_fully_visible (w))
10581 goto need_larger_matrices;
10583 done:
10585 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10586 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10587 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10588 ? Qt : Qnil);
10590 /* Display the mode line, if we must. */
10591 if ((update_mode_line
10592 /* If window not full width, must redo its mode line
10593 if (a) the window to its side is being redone and
10594 (b) we do a frame-based redisplay. This is a consequence
10595 of how inverted lines are drawn in frame-based redisplay. */
10596 || (!just_this_one_p
10597 && !FRAME_WINDOW_P (f)
10598 && !WINDOW_FULL_WIDTH_P (w))
10599 /* Line number to display. */
10600 || INTEGERP (w->base_line_pos)
10601 /* Column number is displayed and different from the one displayed. */
10602 || (!NILP (w->column_number_displayed)
10603 && XFASTINT (w->column_number_displayed) != current_column ()))
10604 /* This means that the window has a mode line. */
10605 && (WINDOW_WANTS_MODELINE_P (w)
10606 || WINDOW_WANTS_HEADER_LINE_P (w)))
10608 display_mode_lines (w);
10610 /* If mode line height has changed, arrange for a thorough
10611 immediate redisplay using the correct mode line height. */
10612 if (WINDOW_WANTS_MODELINE_P (w)
10613 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10615 fonts_changed_p = 1;
10616 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10617 = DESIRED_MODE_LINE_HEIGHT (w);
10620 /* If top line height has changed, arrange for a thorough
10621 immediate redisplay using the correct mode line height. */
10622 if (WINDOW_WANTS_HEADER_LINE_P (w)
10623 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10625 fonts_changed_p = 1;
10626 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10627 = DESIRED_HEADER_LINE_HEIGHT (w);
10630 if (fonts_changed_p)
10631 goto need_larger_matrices;
10634 if (!line_number_displayed
10635 && !BUFFERP (w->base_line_pos))
10637 w->base_line_pos = Qnil;
10638 w->base_line_number = Qnil;
10641 finish_menu_bars:
10643 /* When we reach a frame's selected window, redo the frame's menu bar. */
10644 if (update_mode_line
10645 && EQ (FRAME_SELECTED_WINDOW (f), window))
10647 int redisplay_menu_p = 0;
10649 if (FRAME_WINDOW_P (f))
10651 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10652 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10653 #else
10654 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10655 #endif
10657 else
10658 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10660 if (redisplay_menu_p)
10661 display_menu_bar (w);
10663 #ifdef HAVE_WINDOW_SYSTEM
10664 if (WINDOWP (f->tool_bar_window)
10665 && (FRAME_TOOL_BAR_LINES (f) > 0
10666 || auto_resize_tool_bars_p))
10667 redisplay_tool_bar (f);
10668 #endif
10671 need_larger_matrices:
10673 finish_scroll_bars:
10675 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10677 int start, end, whole;
10679 /* Calculate the start and end positions for the current window.
10680 At some point, it would be nice to choose between scrollbars
10681 which reflect the whole buffer size, with special markers
10682 indicating narrowing, and scrollbars which reflect only the
10683 visible region.
10685 Note that mini-buffers sometimes aren't displaying any text. */
10686 if (!MINI_WINDOW_P (w)
10687 || (w == XWINDOW (minibuf_window)
10688 && NILP (echo_area_buffer[0])))
10690 whole = ZV - BEGV;
10691 start = marker_position (w->start) - BEGV;
10692 /* I don't think this is guaranteed to be right. For the
10693 moment, we'll pretend it is. */
10694 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10696 if (end < start)
10697 end = start;
10698 if (whole < (end - start))
10699 whole = end - start;
10701 else
10702 start = end = whole = 0;
10704 /* Indicate what this scroll bar ought to be displaying now. */
10705 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10707 /* Note that we actually used the scroll bar attached to this
10708 window, so it shouldn't be deleted at the end of redisplay. */
10709 redeem_scroll_bar_hook (w);
10712 /* Restore current_buffer and value of point in it. */
10713 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10714 set_buffer_internal_1 (old);
10715 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10717 unbind_to (count, Qnil);
10721 /* Build the complete desired matrix of WINDOW with a window start
10722 buffer position POS. Value is non-zero if successful. It is zero
10723 if fonts were loaded during redisplay which makes re-adjusting
10724 glyph matrices necessary. */
10727 try_window (window, pos)
10728 Lisp_Object window;
10729 struct text_pos pos;
10731 struct window *w = XWINDOW (window);
10732 struct it it;
10733 struct glyph_row *last_text_row = NULL;
10735 /* Make POS the new window start. */
10736 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10738 /* Mark cursor position as unknown. No overlay arrow seen. */
10739 w->cursor.vpos = -1;
10740 overlay_arrow_seen = 0;
10742 /* Initialize iterator and info to start at POS. */
10743 start_display (&it, w, pos);
10745 /* Display all lines of W. */
10746 while (it.current_y < it.last_visible_y)
10748 if (display_line (&it))
10749 last_text_row = it.glyph_row - 1;
10750 if (fonts_changed_p)
10751 return 0;
10754 /* If bottom moved off end of frame, change mode line percentage. */
10755 if (XFASTINT (w->window_end_pos) <= 0
10756 && Z != IT_CHARPOS (it))
10757 w->update_mode_line = Qt;
10759 /* Set window_end_pos to the offset of the last character displayed
10760 on the window from the end of current_buffer. Set
10761 window_end_vpos to its row number. */
10762 if (last_text_row)
10764 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10765 w->window_end_bytepos
10766 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10767 w->window_end_pos
10768 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10769 w->window_end_vpos
10770 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10771 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10772 ->displays_text_p);
10774 else
10776 w->window_end_bytepos = 0;
10777 w->window_end_pos = w->window_end_vpos = make_number (0);
10780 /* But that is not valid info until redisplay finishes. */
10781 w->window_end_valid = Qnil;
10782 return 1;
10787 /************************************************************************
10788 Window redisplay reusing current matrix when buffer has not changed
10789 ************************************************************************/
10791 /* Try redisplay of window W showing an unchanged buffer with a
10792 different window start than the last time it was displayed by
10793 reusing its current matrix. Value is non-zero if successful.
10794 W->start is the new window start. */
10796 static int
10797 try_window_reusing_current_matrix (w)
10798 struct window *w;
10800 struct frame *f = XFRAME (w->frame);
10801 struct glyph_row *row, *bottom_row;
10802 struct it it;
10803 struct run run;
10804 struct text_pos start, new_start;
10805 int nrows_scrolled, i;
10806 struct glyph_row *last_text_row;
10807 struct glyph_row *last_reused_text_row;
10808 struct glyph_row *start_row;
10809 int start_vpos, min_y, max_y;
10811 #if GLYPH_DEBUG
10812 if (inhibit_try_window_reusing)
10813 return 0;
10814 #endif
10816 if (/* This function doesn't handle terminal frames. */
10817 !FRAME_WINDOW_P (f)
10818 /* Don't try to reuse the display if windows have been split
10819 or such. */
10820 || windows_or_buffers_changed
10821 || cursor_type_changed)
10822 return 0;
10824 /* Can't do this if region may have changed. */
10825 if ((!NILP (Vtransient_mark_mode)
10826 && !NILP (current_buffer->mark_active))
10827 || !NILP (w->region_showing)
10828 || !NILP (Vshow_trailing_whitespace))
10829 return 0;
10831 /* If top-line visibility has changed, give up. */
10832 if (WINDOW_WANTS_HEADER_LINE_P (w)
10833 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10834 return 0;
10836 /* Give up if old or new display is scrolled vertically. We could
10837 make this function handle this, but right now it doesn't. */
10838 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10839 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10840 return 0;
10842 /* The variable new_start now holds the new window start. The old
10843 start `start' can be determined from the current matrix. */
10844 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10845 start = start_row->start.pos;
10846 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10848 /* Clear the desired matrix for the display below. */
10849 clear_glyph_matrix (w->desired_matrix);
10851 if (CHARPOS (new_start) <= CHARPOS (start))
10853 int first_row_y;
10855 /* Don't use this method if the display starts with an ellipsis
10856 displayed for invisible text. It's not easy to handle that case
10857 below, and it's certainly not worth the effort since this is
10858 not a frequent case. */
10859 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10860 return 0;
10862 IF_DEBUG (debug_method_add (w, "twu1"));
10864 /* Display up to a row that can be reused. The variable
10865 last_text_row is set to the last row displayed that displays
10866 text. Note that it.vpos == 0 if or if not there is a
10867 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10868 start_display (&it, w, new_start);
10869 first_row_y = it.current_y;
10870 w->cursor.vpos = -1;
10871 last_text_row = last_reused_text_row = NULL;
10873 while (it.current_y < it.last_visible_y
10874 && IT_CHARPOS (it) < CHARPOS (start)
10875 && !fonts_changed_p)
10876 if (display_line (&it))
10877 last_text_row = it.glyph_row - 1;
10879 /* A value of current_y < last_visible_y means that we stopped
10880 at the previous window start, which in turn means that we
10881 have at least one reusable row. */
10882 if (it.current_y < it.last_visible_y)
10884 /* IT.vpos always starts from 0; it counts text lines. */
10885 nrows_scrolled = it.vpos;
10887 /* Find PT if not already found in the lines displayed. */
10888 if (w->cursor.vpos < 0)
10890 int dy = it.current_y - first_row_y;
10892 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10893 row = row_containing_pos (w, PT, row, NULL, dy);
10894 if (row)
10895 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10896 dy, nrows_scrolled);
10897 else
10899 clear_glyph_matrix (w->desired_matrix);
10900 return 0;
10904 /* Scroll the display. Do it before the current matrix is
10905 changed. The problem here is that update has not yet
10906 run, i.e. part of the current matrix is not up to date.
10907 scroll_run_hook will clear the cursor, and use the
10908 current matrix to get the height of the row the cursor is
10909 in. */
10910 run.current_y = first_row_y;
10911 run.desired_y = it.current_y;
10912 run.height = it.last_visible_y - it.current_y;
10914 if (run.height > 0 && run.current_y != run.desired_y)
10916 update_begin (f);
10917 rif->update_window_begin_hook (w);
10918 rif->clear_mouse_face (w);
10919 rif->scroll_run_hook (w, &run);
10920 rif->update_window_end_hook (w, 0, 0);
10921 update_end (f);
10924 /* Shift current matrix down by nrows_scrolled lines. */
10925 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10926 rotate_matrix (w->current_matrix,
10927 start_vpos,
10928 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10929 nrows_scrolled);
10931 /* Disable lines that must be updated. */
10932 for (i = 0; i < it.vpos; ++i)
10933 (start_row + i)->enabled_p = 0;
10935 /* Re-compute Y positions. */
10936 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10937 max_y = it.last_visible_y;
10938 for (row = start_row + nrows_scrolled;
10939 row < bottom_row;
10940 ++row)
10942 row->y = it.current_y;
10943 row->visible_height = row->height;
10945 if (row->y < min_y)
10946 row->visible_height -= min_y - row->y;
10947 if (row->y + row->height > max_y)
10948 row->visible_height -= row->y + row->height - max_y;
10950 it.current_y += row->height;
10952 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10953 last_reused_text_row = row;
10954 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10955 break;
10958 /* Disable lines in the current matrix which are now
10959 below the window. */
10960 for (++row; row < bottom_row; ++row)
10961 row->enabled_p = 0;
10964 /* Update window_end_pos etc.; last_reused_text_row is the last
10965 reused row from the current matrix containing text, if any.
10966 The value of last_text_row is the last displayed line
10967 containing text. */
10968 if (last_reused_text_row)
10970 w->window_end_bytepos
10971 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10972 w->window_end_pos
10973 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10974 w->window_end_vpos
10975 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10976 w->current_matrix));
10978 else if (last_text_row)
10980 w->window_end_bytepos
10981 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10982 w->window_end_pos
10983 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10984 w->window_end_vpos
10985 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10987 else
10989 /* This window must be completely empty. */
10990 w->window_end_bytepos = 0;
10991 w->window_end_pos = w->window_end_vpos = make_number (0);
10993 w->window_end_valid = Qnil;
10995 /* Update hint: don't try scrolling again in update_window. */
10996 w->desired_matrix->no_scrolling_p = 1;
10998 #if GLYPH_DEBUG
10999 debug_method_add (w, "try_window_reusing_current_matrix 1");
11000 #endif
11001 return 1;
11003 else if (CHARPOS (new_start) > CHARPOS (start))
11005 struct glyph_row *pt_row, *row;
11006 struct glyph_row *first_reusable_row;
11007 struct glyph_row *first_row_to_display;
11008 int dy;
11009 int yb = window_text_bottom_y (w);
11011 /* Find the row starting at new_start, if there is one. Don't
11012 reuse a partially visible line at the end. */
11013 first_reusable_row = start_row;
11014 while (first_reusable_row->enabled_p
11015 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11016 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11017 < CHARPOS (new_start)))
11018 ++first_reusable_row;
11020 /* Give up if there is no row to reuse. */
11021 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11022 || !first_reusable_row->enabled_p
11023 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11024 != CHARPOS (new_start)))
11025 return 0;
11027 /* We can reuse fully visible rows beginning with
11028 first_reusable_row to the end of the window. Set
11029 first_row_to_display to the first row that cannot be reused.
11030 Set pt_row to the row containing point, if there is any. */
11031 pt_row = NULL;
11032 for (first_row_to_display = first_reusable_row;
11033 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11034 ++first_row_to_display)
11036 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11037 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11038 pt_row = first_row_to_display;
11041 /* Start displaying at the start of first_row_to_display. */
11042 xassert (first_row_to_display->y < yb);
11043 init_to_row_start (&it, w, first_row_to_display);
11045 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11046 - start_vpos);
11047 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11048 - nrows_scrolled);
11049 it.current_y = (first_row_to_display->y - first_reusable_row->y
11050 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11052 /* Display lines beginning with first_row_to_display in the
11053 desired matrix. Set last_text_row to the last row displayed
11054 that displays text. */
11055 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11056 if (pt_row == NULL)
11057 w->cursor.vpos = -1;
11058 last_text_row = NULL;
11059 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11060 if (display_line (&it))
11061 last_text_row = it.glyph_row - 1;
11063 /* Give up If point isn't in a row displayed or reused. */
11064 if (w->cursor.vpos < 0)
11066 clear_glyph_matrix (w->desired_matrix);
11067 return 0;
11070 /* If point is in a reused row, adjust y and vpos of the cursor
11071 position. */
11072 if (pt_row)
11074 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11075 w->current_matrix);
11076 w->cursor.y -= first_reusable_row->y;
11079 /* Scroll the display. */
11080 run.current_y = first_reusable_row->y;
11081 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11082 run.height = it.last_visible_y - run.current_y;
11083 dy = run.current_y - run.desired_y;
11085 if (run.height)
11087 struct frame *f = XFRAME (WINDOW_FRAME (w));
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 /* Adjust Y positions of reused rows. */
11097 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11098 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11099 max_y = it.last_visible_y;
11100 for (row = first_reusable_row; row < first_row_to_display; ++row)
11102 row->y -= dy;
11103 row->visible_height = row->height;
11104 if (row->y < min_y)
11105 row->visible_height -= min_y - row->y;
11106 if (row->y + row->height > max_y)
11107 row->visible_height -= row->y + row->height - max_y;
11110 /* Scroll the current matrix. */
11111 xassert (nrows_scrolled > 0);
11112 rotate_matrix (w->current_matrix,
11113 start_vpos,
11114 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11115 -nrows_scrolled);
11117 /* Disable rows not reused. */
11118 for (row -= nrows_scrolled; row < bottom_row; ++row)
11119 row->enabled_p = 0;
11121 /* Adjust window end. A null value of last_text_row means that
11122 the window end is in reused rows which in turn means that
11123 only its vpos can have changed. */
11124 if (last_text_row)
11126 w->window_end_bytepos
11127 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11128 w->window_end_pos
11129 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11130 w->window_end_vpos
11131 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11133 else
11135 w->window_end_vpos
11136 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11139 w->window_end_valid = Qnil;
11140 w->desired_matrix->no_scrolling_p = 1;
11142 #if GLYPH_DEBUG
11143 debug_method_add (w, "try_window_reusing_current_matrix 2");
11144 #endif
11145 return 1;
11148 return 0;
11153 /************************************************************************
11154 Window redisplay reusing current matrix when buffer has changed
11155 ************************************************************************/
11157 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11158 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11159 int *, int *));
11160 static struct glyph_row *
11161 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11162 struct glyph_row *));
11165 /* Return the last row in MATRIX displaying text. If row START is
11166 non-null, start searching with that row. IT gives the dimensions
11167 of the display. Value is null if matrix is empty; otherwise it is
11168 a pointer to the row found. */
11170 static struct glyph_row *
11171 find_last_row_displaying_text (matrix, it, start)
11172 struct glyph_matrix *matrix;
11173 struct it *it;
11174 struct glyph_row *start;
11176 struct glyph_row *row, *row_found;
11178 /* Set row_found to the last row in IT->w's current matrix
11179 displaying text. The loop looks funny but think of partially
11180 visible lines. */
11181 row_found = NULL;
11182 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11183 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11185 xassert (row->enabled_p);
11186 row_found = row;
11187 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11188 break;
11189 ++row;
11192 return row_found;
11196 /* Return the last row in the current matrix of W that is not affected
11197 by changes at the start of current_buffer that occurred since W's
11198 current matrix was built. Value is null if no such row exists.
11200 BEG_UNCHANGED us the number of characters unchanged at the start of
11201 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11202 first changed character in current_buffer. Characters at positions <
11203 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11204 when the current matrix was built. */
11206 static struct glyph_row *
11207 find_last_unchanged_at_beg_row (w)
11208 struct window *w;
11210 int first_changed_pos = BEG + BEG_UNCHANGED;
11211 struct glyph_row *row;
11212 struct glyph_row *row_found = NULL;
11213 int yb = window_text_bottom_y (w);
11215 /* Find the last row displaying unchanged text. */
11216 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11217 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11218 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11220 if (/* If row ends before first_changed_pos, it is unchanged,
11221 except in some case. */
11222 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11223 /* When row ends in ZV and we write at ZV it is not
11224 unchanged. */
11225 && !row->ends_at_zv_p
11226 /* When first_changed_pos is the end of a continued line,
11227 row is not unchanged because it may be no longer
11228 continued. */
11229 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11230 && row->continued_p))
11231 row_found = row;
11233 /* Stop if last visible row. */
11234 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11235 break;
11237 ++row;
11240 return row_found;
11244 /* Find the first glyph row in the current matrix of W that is not
11245 affected by changes at the end of current_buffer since the
11246 time W's current matrix was built.
11248 Return in *DELTA the number of chars by which buffer positions in
11249 unchanged text at the end of current_buffer must be adjusted.
11251 Return in *DELTA_BYTES the corresponding number of bytes.
11253 Value is null if no such row exists, i.e. all rows are affected by
11254 changes. */
11256 static struct glyph_row *
11257 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11258 struct window *w;
11259 int *delta, *delta_bytes;
11261 struct glyph_row *row;
11262 struct glyph_row *row_found = NULL;
11264 *delta = *delta_bytes = 0;
11266 /* Display must not have been paused, otherwise the current matrix
11267 is not up to date. */
11268 if (NILP (w->window_end_valid))
11269 abort ();
11271 /* A value of window_end_pos >= END_UNCHANGED means that the window
11272 end is in the range of changed text. If so, there is no
11273 unchanged row at the end of W's current matrix. */
11274 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11275 return NULL;
11277 /* Set row to the last row in W's current matrix displaying text. */
11278 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11280 /* If matrix is entirely empty, no unchanged row exists. */
11281 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11283 /* The value of row is the last glyph row in the matrix having a
11284 meaningful buffer position in it. The end position of row
11285 corresponds to window_end_pos. This allows us to translate
11286 buffer positions in the current matrix to current buffer
11287 positions for characters not in changed text. */
11288 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11289 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11290 int last_unchanged_pos, last_unchanged_pos_old;
11291 struct glyph_row *first_text_row
11292 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11294 *delta = Z - Z_old;
11295 *delta_bytes = Z_BYTE - Z_BYTE_old;
11297 /* Set last_unchanged_pos to the buffer position of the last
11298 character in the buffer that has not been changed. Z is the
11299 index + 1 of the last character in current_buffer, i.e. by
11300 subtracting END_UNCHANGED we get the index of the last
11301 unchanged character, and we have to add BEG to get its buffer
11302 position. */
11303 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11304 last_unchanged_pos_old = last_unchanged_pos - *delta;
11306 /* Search backward from ROW for a row displaying a line that
11307 starts at a minimum position >= last_unchanged_pos_old. */
11308 for (; row > first_text_row; --row)
11310 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11311 abort ();
11313 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11314 row_found = row;
11318 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11319 abort ();
11321 return row_found;
11325 /* Make sure that glyph rows in the current matrix of window W
11326 reference the same glyph memory as corresponding rows in the
11327 frame's frame matrix. This function is called after scrolling W's
11328 current matrix on a terminal frame in try_window_id and
11329 try_window_reusing_current_matrix. */
11331 static void
11332 sync_frame_with_window_matrix_rows (w)
11333 struct window *w;
11335 struct frame *f = XFRAME (w->frame);
11336 struct glyph_row *window_row, *window_row_end, *frame_row;
11338 /* Preconditions: W must be a leaf window and full-width. Its frame
11339 must have a frame matrix. */
11340 xassert (NILP (w->hchild) && NILP (w->vchild));
11341 xassert (WINDOW_FULL_WIDTH_P (w));
11342 xassert (!FRAME_WINDOW_P (f));
11344 /* If W is a full-width window, glyph pointers in W's current matrix
11345 have, by definition, to be the same as glyph pointers in the
11346 corresponding frame matrix. Note that frame matrices have no
11347 marginal areas (see build_frame_matrix). */
11348 window_row = w->current_matrix->rows;
11349 window_row_end = window_row + w->current_matrix->nrows;
11350 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11351 while (window_row < window_row_end)
11353 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11354 struct glyph *end = window_row->glyphs[LAST_AREA];
11356 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11357 frame_row->glyphs[TEXT_AREA] = start;
11358 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11359 frame_row->glyphs[LAST_AREA] = end;
11361 /* Disable frame rows whose corresponding window rows have
11362 been disabled in try_window_id. */
11363 if (!window_row->enabled_p)
11364 frame_row->enabled_p = 0;
11366 ++window_row, ++frame_row;
11371 /* Find the glyph row in window W containing CHARPOS. Consider all
11372 rows between START and END (not inclusive). END null means search
11373 all rows to the end of the display area of W. Value is the row
11374 containing CHARPOS or null. */
11376 struct glyph_row *
11377 row_containing_pos (w, charpos, start, end, dy)
11378 struct window *w;
11379 int charpos;
11380 struct glyph_row *start, *end;
11381 int dy;
11383 struct glyph_row *row = start;
11384 int last_y;
11386 /* If we happen to start on a header-line, skip that. */
11387 if (row->mode_line_p)
11388 ++row;
11390 if ((end && row >= end) || !row->enabled_p)
11391 return NULL;
11393 last_y = window_text_bottom_y (w) - dy;
11395 while ((end == NULL || row < end)
11396 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11397 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11398 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11399 /* The end position of a row equals the start
11400 position of the next row. If CHARPOS is there, we
11401 would rather display it in the next line, except
11402 when this line ends in ZV. */
11403 && !row->ends_at_zv_p
11404 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11405 ++row;
11407 /* Give up if CHARPOS not found. */
11408 if ((end && row >= end)
11409 || charpos < MATRIX_ROW_START_CHARPOS (row)
11410 || charpos > MATRIX_ROW_END_CHARPOS (row))
11411 row = NULL;
11413 return row;
11417 /* Try to redisplay window W by reusing its existing display. W's
11418 current matrix must be up to date when this function is called,
11419 i.e. window_end_valid must not be nil.
11421 Value is
11423 1 if display has been updated
11424 0 if otherwise unsuccessful
11425 -1 if redisplay with same window start is known not to succeed
11427 The following steps are performed:
11429 1. Find the last row in the current matrix of W that is not
11430 affected by changes at the start of current_buffer. If no such row
11431 is found, give up.
11433 2. Find the first row in W's current matrix that is not affected by
11434 changes at the end of current_buffer. Maybe there is no such row.
11436 3. Display lines beginning with the row + 1 found in step 1 to the
11437 row found in step 2 or, if step 2 didn't find a row, to the end of
11438 the window.
11440 4. If cursor is not known to appear on the window, give up.
11442 5. If display stopped at the row found in step 2, scroll the
11443 display and current matrix as needed.
11445 6. Maybe display some lines at the end of W, if we must. This can
11446 happen under various circumstances, like a partially visible line
11447 becoming fully visible, or because newly displayed lines are displayed
11448 in smaller font sizes.
11450 7. Update W's window end information. */
11452 static int
11453 try_window_id (w)
11454 struct window *w;
11456 struct frame *f = XFRAME (w->frame);
11457 struct glyph_matrix *current_matrix = w->current_matrix;
11458 struct glyph_matrix *desired_matrix = w->desired_matrix;
11459 struct glyph_row *last_unchanged_at_beg_row;
11460 struct glyph_row *first_unchanged_at_end_row;
11461 struct glyph_row *row;
11462 struct glyph_row *bottom_row;
11463 int bottom_vpos;
11464 struct it it;
11465 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11466 struct text_pos start_pos;
11467 struct run run;
11468 int first_unchanged_at_end_vpos = 0;
11469 struct glyph_row *last_text_row, *last_text_row_at_end;
11470 struct text_pos start;
11471 int first_changed_charpos, last_changed_charpos;
11473 #if GLYPH_DEBUG
11474 if (inhibit_try_window_id)
11475 return 0;
11476 #endif
11478 /* This is handy for debugging. */
11479 #if 0
11480 #define GIVE_UP(X) \
11481 do { \
11482 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11483 return 0; \
11484 } while (0)
11485 #else
11486 #define GIVE_UP(X) return 0
11487 #endif
11489 SET_TEXT_POS_FROM_MARKER (start, w->start);
11491 /* Don't use this for mini-windows because these can show
11492 messages and mini-buffers, and we don't handle that here. */
11493 if (MINI_WINDOW_P (w))
11494 GIVE_UP (1);
11496 /* This flag is used to prevent redisplay optimizations. */
11497 if (windows_or_buffers_changed || cursor_type_changed)
11498 GIVE_UP (2);
11500 /* Verify that narrowing has not changed. This flag is also set to prevent
11501 redisplay optimizations. It would be nice to further
11502 reduce the number of cases where this prevents try_window_id. */
11503 if (current_buffer->clip_changed)
11504 GIVE_UP (3);
11506 /* Window must either use window-based redisplay or be full width. */
11507 if (!FRAME_WINDOW_P (f)
11508 && (!line_ins_del_ok
11509 || !WINDOW_FULL_WIDTH_P (w)))
11510 GIVE_UP (4);
11512 /* Give up if point is not known NOT to appear in W. */
11513 if (PT < CHARPOS (start))
11514 GIVE_UP (5);
11516 /* Another way to prevent redisplay optimizations. */
11517 if (XFASTINT (w->last_modified) == 0)
11518 GIVE_UP (6);
11520 /* Verify that window is not hscrolled. */
11521 if (XFASTINT (w->hscroll) != 0)
11522 GIVE_UP (7);
11524 /* Verify that display wasn't paused. */
11525 if (NILP (w->window_end_valid))
11526 GIVE_UP (8);
11528 /* Can't use this if highlighting a region because a cursor movement
11529 will do more than just set the cursor. */
11530 if (!NILP (Vtransient_mark_mode)
11531 && !NILP (current_buffer->mark_active))
11532 GIVE_UP (9);
11534 /* Likewise if highlighting trailing whitespace. */
11535 if (!NILP (Vshow_trailing_whitespace))
11536 GIVE_UP (11);
11538 /* Likewise if showing a region. */
11539 if (!NILP (w->region_showing))
11540 GIVE_UP (10);
11542 /* Can use this if overlay arrow position and or string have changed. */
11543 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11544 || !EQ (last_arrow_string, Voverlay_arrow_string))
11545 GIVE_UP (12);
11548 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11549 only if buffer has really changed. The reason is that the gap is
11550 initially at Z for freshly visited files. The code below would
11551 set end_unchanged to 0 in that case. */
11552 if (MODIFF > SAVE_MODIFF
11553 /* This seems to happen sometimes after saving a buffer. */
11554 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11556 if (GPT - BEG < BEG_UNCHANGED)
11557 BEG_UNCHANGED = GPT - BEG;
11558 if (Z - GPT < END_UNCHANGED)
11559 END_UNCHANGED = Z - GPT;
11562 /* The position of the first and last character that has been changed. */
11563 first_changed_charpos = BEG + BEG_UNCHANGED;
11564 last_changed_charpos = Z - END_UNCHANGED;
11566 /* If window starts after a line end, and the last change is in
11567 front of that newline, then changes don't affect the display.
11568 This case happens with stealth-fontification. Note that although
11569 the display is unchanged, glyph positions in the matrix have to
11570 be adjusted, of course. */
11571 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11572 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11573 && ((last_changed_charpos < CHARPOS (start)
11574 && CHARPOS (start) == BEGV)
11575 || (last_changed_charpos < CHARPOS (start) - 1
11576 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11578 int Z_old, delta, Z_BYTE_old, delta_bytes;
11579 struct glyph_row *r0;
11581 /* Compute how many chars/bytes have been added to or removed
11582 from the buffer. */
11583 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11584 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11585 delta = Z - Z_old;
11586 delta_bytes = Z_BYTE - Z_BYTE_old;
11588 /* Give up if PT is not in the window. Note that it already has
11589 been checked at the start of try_window_id that PT is not in
11590 front of the window start. */
11591 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11592 GIVE_UP (13);
11594 /* If window start is unchanged, we can reuse the whole matrix
11595 as is, after adjusting glyph positions. No need to compute
11596 the window end again, since its offset from Z hasn't changed. */
11597 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11598 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11599 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11601 /* Adjust positions in the glyph matrix. */
11602 if (delta || delta_bytes)
11604 struct glyph_row *r1
11605 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11606 increment_matrix_positions (w->current_matrix,
11607 MATRIX_ROW_VPOS (r0, current_matrix),
11608 MATRIX_ROW_VPOS (r1, current_matrix),
11609 delta, delta_bytes);
11612 /* Set the cursor. */
11613 row = row_containing_pos (w, PT, r0, NULL, 0);
11614 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11615 return 1;
11619 /* Handle the case that changes are all below what is displayed in
11620 the window, and that PT is in the window. This shortcut cannot
11621 be taken if ZV is visible in the window, and text has been added
11622 there that is visible in the window. */
11623 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11624 /* ZV is not visible in the window, or there are no
11625 changes at ZV, actually. */
11626 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11627 || first_changed_charpos == last_changed_charpos))
11629 struct glyph_row *r0;
11631 /* Give up if PT is not in the window. Note that it already has
11632 been checked at the start of try_window_id that PT is not in
11633 front of the window start. */
11634 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11635 GIVE_UP (14);
11637 /* If window start is unchanged, we can reuse the whole matrix
11638 as is, without changing glyph positions since no text has
11639 been added/removed in front of the window end. */
11640 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11641 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11643 /* We have to compute the window end anew since text
11644 can have been added/removed after it. */
11645 w->window_end_pos
11646 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11647 w->window_end_bytepos
11648 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11650 /* Set the cursor. */
11651 row = row_containing_pos (w, PT, r0, NULL, 0);
11652 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11653 return 2;
11657 /* Give up if window start is in the changed area.
11659 The condition used to read
11661 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11663 but why that was tested escapes me at the moment. */
11664 if (CHARPOS (start) >= first_changed_charpos
11665 && CHARPOS (start) <= last_changed_charpos)
11666 GIVE_UP (15);
11668 /* Check that window start agrees with the start of the first glyph
11669 row in its current matrix. Check this after we know the window
11670 start is not in changed text, otherwise positions would not be
11671 comparable. */
11672 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11673 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11674 GIVE_UP (16);
11676 /* Give up if the window ends in strings. Overlay strings
11677 at the end are difficult to handle, so don't try. */
11678 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11679 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11680 GIVE_UP (20);
11682 /* Compute the position at which we have to start displaying new
11683 lines. Some of the lines at the top of the window might be
11684 reusable because they are not displaying changed text. Find the
11685 last row in W's current matrix not affected by changes at the
11686 start of current_buffer. Value is null if changes start in the
11687 first line of window. */
11688 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11689 if (last_unchanged_at_beg_row)
11691 /* Avoid starting to display in the moddle of a character, a TAB
11692 for instance. This is easier than to set up the iterator
11693 exactly, and it's not a frequent case, so the additional
11694 effort wouldn't really pay off. */
11695 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11696 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11697 && last_unchanged_at_beg_row > w->current_matrix->rows)
11698 --last_unchanged_at_beg_row;
11700 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11701 GIVE_UP (17);
11703 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11704 GIVE_UP (18);
11705 start_pos = it.current.pos;
11707 /* Start displaying new lines in the desired matrix at the same
11708 vpos we would use in the current matrix, i.e. below
11709 last_unchanged_at_beg_row. */
11710 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11711 current_matrix);
11712 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11713 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11715 xassert (it.hpos == 0 && it.current_x == 0);
11717 else
11719 /* There are no reusable lines at the start of the window.
11720 Start displaying in the first line. */
11721 start_display (&it, w, start);
11722 start_pos = it.current.pos;
11725 /* Find the first row that is not affected by changes at the end of
11726 the buffer. Value will be null if there is no unchanged row, in
11727 which case we must redisplay to the end of the window. delta
11728 will be set to the value by which buffer positions beginning with
11729 first_unchanged_at_end_row have to be adjusted due to text
11730 changes. */
11731 first_unchanged_at_end_row
11732 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11733 IF_DEBUG (debug_delta = delta);
11734 IF_DEBUG (debug_delta_bytes = delta_bytes);
11736 /* Set stop_pos to the buffer position up to which we will have to
11737 display new lines. If first_unchanged_at_end_row != NULL, this
11738 is the buffer position of the start of the line displayed in that
11739 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11740 that we don't stop at a buffer position. */
11741 stop_pos = 0;
11742 if (first_unchanged_at_end_row)
11744 xassert (last_unchanged_at_beg_row == NULL
11745 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11747 /* If this is a continuation line, move forward to the next one
11748 that isn't. Changes in lines above affect this line.
11749 Caution: this may move first_unchanged_at_end_row to a row
11750 not displaying text. */
11751 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11752 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11753 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11754 < it.last_visible_y))
11755 ++first_unchanged_at_end_row;
11757 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11758 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11759 >= it.last_visible_y))
11760 first_unchanged_at_end_row = NULL;
11761 else
11763 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11764 + delta);
11765 first_unchanged_at_end_vpos
11766 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11767 xassert (stop_pos >= Z - END_UNCHANGED);
11770 else if (last_unchanged_at_beg_row == NULL)
11771 GIVE_UP (19);
11774 #if GLYPH_DEBUG
11776 /* Either there is no unchanged row at the end, or the one we have
11777 now displays text. This is a necessary condition for the window
11778 end pos calculation at the end of this function. */
11779 xassert (first_unchanged_at_end_row == NULL
11780 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11782 debug_last_unchanged_at_beg_vpos
11783 = (last_unchanged_at_beg_row
11784 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11785 : -1);
11786 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11788 #endif /* GLYPH_DEBUG != 0 */
11791 /* Display new lines. Set last_text_row to the last new line
11792 displayed which has text on it, i.e. might end up as being the
11793 line where the window_end_vpos is. */
11794 w->cursor.vpos = -1;
11795 last_text_row = NULL;
11796 overlay_arrow_seen = 0;
11797 while (it.current_y < it.last_visible_y
11798 && !fonts_changed_p
11799 && (first_unchanged_at_end_row == NULL
11800 || IT_CHARPOS (it) < stop_pos))
11802 if (display_line (&it))
11803 last_text_row = it.glyph_row - 1;
11806 if (fonts_changed_p)
11807 return -1;
11810 /* Compute differences in buffer positions, y-positions etc. for
11811 lines reused at the bottom of the window. Compute what we can
11812 scroll. */
11813 if (first_unchanged_at_end_row
11814 /* No lines reused because we displayed everything up to the
11815 bottom of the window. */
11816 && it.current_y < it.last_visible_y)
11818 dvpos = (it.vpos
11819 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11820 current_matrix));
11821 dy = it.current_y - first_unchanged_at_end_row->y;
11822 run.current_y = first_unchanged_at_end_row->y;
11823 run.desired_y = run.current_y + dy;
11824 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11826 else
11828 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11829 first_unchanged_at_end_row = NULL;
11831 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11834 /* Find the cursor if not already found. We have to decide whether
11835 PT will appear on this window (it sometimes doesn't, but this is
11836 not a very frequent case.) This decision has to be made before
11837 the current matrix is altered. A value of cursor.vpos < 0 means
11838 that PT is either in one of the lines beginning at
11839 first_unchanged_at_end_row or below the window. Don't care for
11840 lines that might be displayed later at the window end; as
11841 mentioned, this is not a frequent case. */
11842 if (w->cursor.vpos < 0)
11844 /* Cursor in unchanged rows at the top? */
11845 if (PT < CHARPOS (start_pos)
11846 && last_unchanged_at_beg_row)
11848 row = row_containing_pos (w, PT,
11849 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11850 last_unchanged_at_beg_row + 1, 0);
11851 if (row)
11852 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11855 /* Start from first_unchanged_at_end_row looking for PT. */
11856 else if (first_unchanged_at_end_row)
11858 row = row_containing_pos (w, PT - delta,
11859 first_unchanged_at_end_row, NULL, 0);
11860 if (row)
11861 set_cursor_from_row (w, row, w->current_matrix, delta,
11862 delta_bytes, dy, dvpos);
11865 /* Give up if cursor was not found. */
11866 if (w->cursor.vpos < 0)
11868 clear_glyph_matrix (w->desired_matrix);
11869 return -1;
11873 /* Don't let the cursor end in the scroll margins. */
11875 int this_scroll_margin, cursor_height;
11877 this_scroll_margin = max (0, scroll_margin);
11878 this_scroll_margin = min (this_scroll_margin,
11879 XFASTINT (w->height) / 4);
11880 this_scroll_margin *= CANON_Y_UNIT (it.f);
11881 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11883 if ((w->cursor.y < this_scroll_margin
11884 && CHARPOS (start) > BEGV)
11885 /* Don't take scroll margin into account at the bottom because
11886 old redisplay didn't do it either. */
11887 || w->cursor.y + cursor_height > it.last_visible_y)
11889 w->cursor.vpos = -1;
11890 clear_glyph_matrix (w->desired_matrix);
11891 return -1;
11895 /* Scroll the display. Do it before changing the current matrix so
11896 that xterm.c doesn't get confused about where the cursor glyph is
11897 found. */
11898 if (dy && run.height)
11900 update_begin (f);
11902 if (FRAME_WINDOW_P (f))
11904 rif->update_window_begin_hook (w);
11905 rif->clear_mouse_face (w);
11906 rif->scroll_run_hook (w, &run);
11907 rif->update_window_end_hook (w, 0, 0);
11909 else
11911 /* Terminal frame. In this case, dvpos gives the number of
11912 lines to scroll by; dvpos < 0 means scroll up. */
11913 int first_unchanged_at_end_vpos
11914 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11915 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11916 int end = (XFASTINT (w->top)
11917 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11918 + window_internal_height (w));
11920 /* Perform the operation on the screen. */
11921 if (dvpos > 0)
11923 /* Scroll last_unchanged_at_beg_row to the end of the
11924 window down dvpos lines. */
11925 set_terminal_window (end);
11927 /* On dumb terminals delete dvpos lines at the end
11928 before inserting dvpos empty lines. */
11929 if (!scroll_region_ok)
11930 ins_del_lines (end - dvpos, -dvpos);
11932 /* Insert dvpos empty lines in front of
11933 last_unchanged_at_beg_row. */
11934 ins_del_lines (from, dvpos);
11936 else if (dvpos < 0)
11938 /* Scroll up last_unchanged_at_beg_vpos to the end of
11939 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11940 set_terminal_window (end);
11942 /* Delete dvpos lines in front of
11943 last_unchanged_at_beg_vpos. ins_del_lines will set
11944 the cursor to the given vpos and emit |dvpos| delete
11945 line sequences. */
11946 ins_del_lines (from + dvpos, dvpos);
11948 /* On a dumb terminal insert dvpos empty lines at the
11949 end. */
11950 if (!scroll_region_ok)
11951 ins_del_lines (end + dvpos, -dvpos);
11954 set_terminal_window (0);
11957 update_end (f);
11960 /* Shift reused rows of the current matrix to the right position.
11961 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11962 text. */
11963 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11964 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11965 if (dvpos < 0)
11967 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11968 bottom_vpos, dvpos);
11969 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11970 bottom_vpos, 0);
11972 else if (dvpos > 0)
11974 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11975 bottom_vpos, dvpos);
11976 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11977 first_unchanged_at_end_vpos + dvpos, 0);
11980 /* For frame-based redisplay, make sure that current frame and window
11981 matrix are in sync with respect to glyph memory. */
11982 if (!FRAME_WINDOW_P (f))
11983 sync_frame_with_window_matrix_rows (w);
11985 /* Adjust buffer positions in reused rows. */
11986 if (delta)
11987 increment_matrix_positions (current_matrix,
11988 first_unchanged_at_end_vpos + dvpos,
11989 bottom_vpos, delta, delta_bytes);
11991 /* Adjust Y positions. */
11992 if (dy)
11993 shift_glyph_matrix (w, current_matrix,
11994 first_unchanged_at_end_vpos + dvpos,
11995 bottom_vpos, dy);
11997 if (first_unchanged_at_end_row)
11998 first_unchanged_at_end_row += dvpos;
12000 /* If scrolling up, there may be some lines to display at the end of
12001 the window. */
12002 last_text_row_at_end = NULL;
12003 if (dy < 0)
12005 /* Scrolling up can leave for example a partially visible line
12006 at the end of the window to be redisplayed. */
12007 /* Set last_row to the glyph row in the current matrix where the
12008 window end line is found. It has been moved up or down in
12009 the matrix by dvpos. */
12010 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12011 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12013 /* If last_row is the window end line, it should display text. */
12014 xassert (last_row->displays_text_p);
12016 /* If window end line was partially visible before, begin
12017 displaying at that line. Otherwise begin displaying with the
12018 line following it. */
12019 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12021 init_to_row_start (&it, w, last_row);
12022 it.vpos = last_vpos;
12023 it.current_y = last_row->y;
12025 else
12027 init_to_row_end (&it, w, last_row);
12028 it.vpos = 1 + last_vpos;
12029 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12030 ++last_row;
12033 /* We may start in a continuation line. If so, we have to
12034 get the right continuation_lines_width and current_x. */
12035 it.continuation_lines_width = last_row->continuation_lines_width;
12036 it.hpos = it.current_x = 0;
12038 /* Display the rest of the lines at the window end. */
12039 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12040 while (it.current_y < it.last_visible_y
12041 && !fonts_changed_p)
12043 /* Is it always sure that the display agrees with lines in
12044 the current matrix? I don't think so, so we mark rows
12045 displayed invalid in the current matrix by setting their
12046 enabled_p flag to zero. */
12047 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12048 if (display_line (&it))
12049 last_text_row_at_end = it.glyph_row - 1;
12053 /* Update window_end_pos and window_end_vpos. */
12054 if (first_unchanged_at_end_row
12055 && first_unchanged_at_end_row->y < it.last_visible_y
12056 && !last_text_row_at_end)
12058 /* Window end line if one of the preserved rows from the current
12059 matrix. Set row to the last row displaying text in current
12060 matrix starting at first_unchanged_at_end_row, after
12061 scrolling. */
12062 xassert (first_unchanged_at_end_row->displays_text_p);
12063 row = find_last_row_displaying_text (w->current_matrix, &it,
12064 first_unchanged_at_end_row);
12065 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12067 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12068 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12069 w->window_end_vpos
12070 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12071 xassert (w->window_end_bytepos >= 0);
12072 IF_DEBUG (debug_method_add (w, "A"));
12074 else if (last_text_row_at_end)
12076 w->window_end_pos
12077 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12078 w->window_end_bytepos
12079 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12080 w->window_end_vpos
12081 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12082 xassert (w->window_end_bytepos >= 0);
12083 IF_DEBUG (debug_method_add (w, "B"));
12085 else if (last_text_row)
12087 /* We have displayed either to the end of the window or at the
12088 end of the window, i.e. the last row with text is to be found
12089 in the desired matrix. */
12090 w->window_end_pos
12091 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12092 w->window_end_bytepos
12093 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12094 w->window_end_vpos
12095 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12096 xassert (w->window_end_bytepos >= 0);
12098 else if (first_unchanged_at_end_row == NULL
12099 && last_text_row == NULL
12100 && last_text_row_at_end == NULL)
12102 /* Displayed to end of window, but no line containing text was
12103 displayed. Lines were deleted at the end of the window. */
12104 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12105 int vpos = XFASTINT (w->window_end_vpos);
12106 struct glyph_row *current_row = current_matrix->rows + vpos;
12107 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12109 for (row = NULL;
12110 row == NULL && vpos >= first_vpos;
12111 --vpos, --current_row, --desired_row)
12113 if (desired_row->enabled_p)
12115 if (desired_row->displays_text_p)
12116 row = desired_row;
12118 else if (current_row->displays_text_p)
12119 row = current_row;
12122 xassert (row != NULL);
12123 w->window_end_vpos = make_number (vpos + 1);
12124 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12125 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12126 xassert (w->window_end_bytepos >= 0);
12127 IF_DEBUG (debug_method_add (w, "C"));
12129 else
12130 abort ();
12132 #if 0 /* This leads to problems, for instance when the cursor is
12133 at ZV, and the cursor line displays no text. */
12134 /* Disable rows below what's displayed in the window. This makes
12135 debugging easier. */
12136 enable_glyph_matrix_rows (current_matrix,
12137 XFASTINT (w->window_end_vpos) + 1,
12138 bottom_vpos, 0);
12139 #endif
12141 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12142 debug_end_vpos = XFASTINT (w->window_end_vpos));
12144 /* Record that display has not been completed. */
12145 w->window_end_valid = Qnil;
12146 w->desired_matrix->no_scrolling_p = 1;
12147 return 3;
12149 #undef GIVE_UP
12154 /***********************************************************************
12155 More debugging support
12156 ***********************************************************************/
12158 #if GLYPH_DEBUG
12160 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12161 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12162 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12165 /* Dump the contents of glyph matrix MATRIX on stderr.
12167 GLYPHS 0 means don't show glyph contents.
12168 GLYPHS 1 means show glyphs in short form
12169 GLYPHS > 1 means show glyphs in long form. */
12171 void
12172 dump_glyph_matrix (matrix, glyphs)
12173 struct glyph_matrix *matrix;
12174 int glyphs;
12176 int i;
12177 for (i = 0; i < matrix->nrows; ++i)
12178 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12182 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12183 the glyph row and area where the glyph comes from. */
12185 void
12186 dump_glyph (row, glyph, area)
12187 struct glyph_row *row;
12188 struct glyph *glyph;
12189 int area;
12191 if (glyph->type == CHAR_GLYPH)
12193 fprintf (stderr,
12194 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12195 glyph - row->glyphs[TEXT_AREA],
12196 'C',
12197 glyph->charpos,
12198 (BUFFERP (glyph->object)
12199 ? 'B'
12200 : (STRINGP (glyph->object)
12201 ? 'S'
12202 : '-')),
12203 glyph->pixel_width,
12204 glyph->u.ch,
12205 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12206 ? glyph->u.ch
12207 : '.'),
12208 glyph->face_id,
12209 glyph->left_box_line_p,
12210 glyph->right_box_line_p);
12212 else if (glyph->type == STRETCH_GLYPH)
12214 fprintf (stderr,
12215 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12216 glyph - row->glyphs[TEXT_AREA],
12217 'S',
12218 glyph->charpos,
12219 (BUFFERP (glyph->object)
12220 ? 'B'
12221 : (STRINGP (glyph->object)
12222 ? 'S'
12223 : '-')),
12224 glyph->pixel_width,
12226 '.',
12227 glyph->face_id,
12228 glyph->left_box_line_p,
12229 glyph->right_box_line_p);
12231 else if (glyph->type == IMAGE_GLYPH)
12233 fprintf (stderr,
12234 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12235 glyph - row->glyphs[TEXT_AREA],
12236 'I',
12237 glyph->charpos,
12238 (BUFFERP (glyph->object)
12239 ? 'B'
12240 : (STRINGP (glyph->object)
12241 ? 'S'
12242 : '-')),
12243 glyph->pixel_width,
12244 glyph->u.img_id,
12245 '.',
12246 glyph->face_id,
12247 glyph->left_box_line_p,
12248 glyph->right_box_line_p);
12253 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12254 GLYPHS 0 means don't show glyph contents.
12255 GLYPHS 1 means show glyphs in short form
12256 GLYPHS > 1 means show glyphs in long form. */
12258 void
12259 dump_glyph_row (row, vpos, glyphs)
12260 struct glyph_row *row;
12261 int vpos, glyphs;
12263 if (glyphs != 1)
12265 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12266 fprintf (stderr, "=======================================================================\n");
12268 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12269 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12270 vpos,
12271 MATRIX_ROW_START_CHARPOS (row),
12272 MATRIX_ROW_END_CHARPOS (row),
12273 row->used[TEXT_AREA],
12274 row->contains_overlapping_glyphs_p,
12275 row->enabled_p,
12276 row->truncated_on_left_p,
12277 row->truncated_on_right_p,
12278 row->overlay_arrow_p,
12279 row->continued_p,
12280 MATRIX_ROW_CONTINUATION_LINE_P (row),
12281 row->displays_text_p,
12282 row->ends_at_zv_p,
12283 row->fill_line_p,
12284 row->ends_in_middle_of_char_p,
12285 row->starts_in_middle_of_char_p,
12286 row->mouse_face_p,
12287 row->x,
12288 row->y,
12289 row->pixel_width,
12290 row->height,
12291 row->visible_height,
12292 row->ascent,
12293 row->phys_ascent);
12294 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12295 row->end.overlay_string_index,
12296 row->continuation_lines_width);
12297 fprintf (stderr, "%9d %5d\n",
12298 CHARPOS (row->start.string_pos),
12299 CHARPOS (row->end.string_pos));
12300 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12301 row->end.dpvec_index);
12304 if (glyphs > 1)
12306 int area;
12308 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12310 struct glyph *glyph = row->glyphs[area];
12311 struct glyph *glyph_end = glyph + row->used[area];
12313 /* Glyph for a line end in text. */
12314 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12315 ++glyph_end;
12317 if (glyph < glyph_end)
12318 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12320 for (; glyph < glyph_end; ++glyph)
12321 dump_glyph (row, glyph, area);
12324 else if (glyphs == 1)
12326 int area;
12328 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12330 char *s = (char *) alloca (row->used[area] + 1);
12331 int i;
12333 for (i = 0; i < row->used[area]; ++i)
12335 struct glyph *glyph = row->glyphs[area] + i;
12336 if (glyph->type == CHAR_GLYPH
12337 && glyph->u.ch < 0x80
12338 && glyph->u.ch >= ' ')
12339 s[i] = glyph->u.ch;
12340 else
12341 s[i] = '.';
12344 s[i] = '\0';
12345 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12351 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12352 Sdump_glyph_matrix, 0, 1, "p",
12353 doc: /* Dump the current matrix of the selected window to stderr.
12354 Shows contents of glyph row structures. With non-nil
12355 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12356 glyphs in short form, otherwise show glyphs in long form. */)
12357 (glyphs)
12358 Lisp_Object glyphs;
12360 struct window *w = XWINDOW (selected_window);
12361 struct buffer *buffer = XBUFFER (w->buffer);
12363 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12364 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12365 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12366 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12367 fprintf (stderr, "=============================================\n");
12368 dump_glyph_matrix (w->current_matrix,
12369 NILP (glyphs) ? 0 : XINT (glyphs));
12370 return Qnil;
12374 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12375 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12378 struct frame *f = XFRAME (selected_frame);
12379 dump_glyph_matrix (f->current_matrix, 1);
12380 return Qnil;
12384 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12385 doc: /* Dump glyph row ROW to stderr.
12386 GLYPH 0 means don't dump glyphs.
12387 GLYPH 1 means dump glyphs in short form.
12388 GLYPH > 1 or omitted means dump glyphs in long form. */)
12389 (row, glyphs)
12390 Lisp_Object row, glyphs;
12392 struct glyph_matrix *matrix;
12393 int vpos;
12395 CHECK_NUMBER (row);
12396 matrix = XWINDOW (selected_window)->current_matrix;
12397 vpos = XINT (row);
12398 if (vpos >= 0 && vpos < matrix->nrows)
12399 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12400 vpos,
12401 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12402 return Qnil;
12406 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12407 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12408 GLYPH 0 means don't dump glyphs.
12409 GLYPH 1 means dump glyphs in short form.
12410 GLYPH > 1 or omitted means dump glyphs in long form. */)
12411 (row, glyphs)
12412 Lisp_Object row, glyphs;
12414 struct frame *sf = SELECTED_FRAME ();
12415 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12416 int vpos;
12418 CHECK_NUMBER (row);
12419 vpos = XINT (row);
12420 if (vpos >= 0 && vpos < m->nrows)
12421 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12422 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12423 return Qnil;
12427 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12428 doc: /* Toggle tracing of redisplay.
12429 With ARG, turn tracing on if and only if ARG is positive. */)
12430 (arg)
12431 Lisp_Object arg;
12433 if (NILP (arg))
12434 trace_redisplay_p = !trace_redisplay_p;
12435 else
12437 arg = Fprefix_numeric_value (arg);
12438 trace_redisplay_p = XINT (arg) > 0;
12441 return Qnil;
12445 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12446 doc: /* Like `format', but print result to stderr. */)
12447 (nargs, args)
12448 int nargs;
12449 Lisp_Object *args;
12451 Lisp_Object s = Fformat (nargs, args);
12452 fprintf (stderr, "%s", SDATA (s));
12453 return Qnil;
12456 #endif /* GLYPH_DEBUG */
12460 /***********************************************************************
12461 Building Desired Matrix Rows
12462 ***********************************************************************/
12464 /* Return a temporary glyph row holding the glyphs of an overlay
12465 arrow. Only used for non-window-redisplay windows. */
12467 static struct glyph_row *
12468 get_overlay_arrow_glyph_row (w)
12469 struct window *w;
12471 struct frame *f = XFRAME (WINDOW_FRAME (w));
12472 struct buffer *buffer = XBUFFER (w->buffer);
12473 struct buffer *old = current_buffer;
12474 unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12475 int arrow_len = SCHARS (Voverlay_arrow_string);
12476 unsigned char *arrow_end = arrow_string + arrow_len;
12477 unsigned char *p;
12478 struct it it;
12479 int multibyte_p;
12480 int n_glyphs_before;
12482 set_buffer_temp (buffer);
12483 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12484 it.glyph_row->used[TEXT_AREA] = 0;
12485 SET_TEXT_POS (it.position, 0, 0);
12487 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12488 p = arrow_string;
12489 while (p < arrow_end)
12491 Lisp_Object face, ilisp;
12493 /* Get the next character. */
12494 if (multibyte_p)
12495 it.c = string_char_and_length (p, arrow_len, &it.len);
12496 else
12497 it.c = *p, it.len = 1;
12498 p += it.len;
12500 /* Get its face. */
12501 ilisp = make_number (p - arrow_string);
12502 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12503 it.face_id = compute_char_face (f, it.c, face);
12505 /* Compute its width, get its glyphs. */
12506 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12507 SET_TEXT_POS (it.position, -1, -1);
12508 PRODUCE_GLYPHS (&it);
12510 /* If this character doesn't fit any more in the line, we have
12511 to remove some glyphs. */
12512 if (it.current_x > it.last_visible_x)
12514 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12515 break;
12519 set_buffer_temp (old);
12520 return it.glyph_row;
12524 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12525 glyphs are only inserted for terminal frames since we can't really
12526 win with truncation glyphs when partially visible glyphs are
12527 involved. Which glyphs to insert is determined by
12528 produce_special_glyphs. */
12530 static void
12531 insert_left_trunc_glyphs (it)
12532 struct it *it;
12534 struct it truncate_it;
12535 struct glyph *from, *end, *to, *toend;
12537 xassert (!FRAME_WINDOW_P (it->f));
12539 /* Get the truncation glyphs. */
12540 truncate_it = *it;
12541 truncate_it.current_x = 0;
12542 truncate_it.face_id = DEFAULT_FACE_ID;
12543 truncate_it.glyph_row = &scratch_glyph_row;
12544 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12545 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12546 truncate_it.object = make_number (0);
12547 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12549 /* Overwrite glyphs from IT with truncation glyphs. */
12550 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12551 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12552 to = it->glyph_row->glyphs[TEXT_AREA];
12553 toend = to + it->glyph_row->used[TEXT_AREA];
12555 while (from < end)
12556 *to++ = *from++;
12558 /* There may be padding glyphs left over. Overwrite them too. */
12559 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12561 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12562 while (from < end)
12563 *to++ = *from++;
12566 if (to > toend)
12567 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12571 /* Compute the pixel height and width of IT->glyph_row.
12573 Most of the time, ascent and height of a display line will be equal
12574 to the max_ascent and max_height values of the display iterator
12575 structure. This is not the case if
12577 1. We hit ZV without displaying anything. In this case, max_ascent
12578 and max_height will be zero.
12580 2. We have some glyphs that don't contribute to the line height.
12581 (The glyph row flag contributes_to_line_height_p is for future
12582 pixmap extensions).
12584 The first case is easily covered by using default values because in
12585 these cases, the line height does not really matter, except that it
12586 must not be zero. */
12588 static void
12589 compute_line_metrics (it)
12590 struct it *it;
12592 struct glyph_row *row = it->glyph_row;
12593 int area, i;
12595 if (FRAME_WINDOW_P (it->f))
12597 int i, min_y, max_y;
12599 /* The line may consist of one space only, that was added to
12600 place the cursor on it. If so, the row's height hasn't been
12601 computed yet. */
12602 if (row->height == 0)
12604 if (it->max_ascent + it->max_descent == 0)
12605 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12606 row->ascent = it->max_ascent;
12607 row->height = it->max_ascent + it->max_descent;
12608 row->phys_ascent = it->max_phys_ascent;
12609 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12612 /* Compute the width of this line. */
12613 row->pixel_width = row->x;
12614 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12615 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12617 xassert (row->pixel_width >= 0);
12618 xassert (row->ascent >= 0 && row->height > 0);
12620 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12621 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12623 /* If first line's physical ascent is larger than its logical
12624 ascent, use the physical ascent, and make the row taller.
12625 This makes accented characters fully visible. */
12626 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12627 && row->phys_ascent > row->ascent)
12629 row->height += row->phys_ascent - row->ascent;
12630 row->ascent = row->phys_ascent;
12633 /* Compute how much of the line is visible. */
12634 row->visible_height = row->height;
12636 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12637 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12639 if (row->y < min_y)
12640 row->visible_height -= min_y - row->y;
12641 if (row->y + row->height > max_y)
12642 row->visible_height -= row->y + row->height - max_y;
12644 else
12646 row->pixel_width = row->used[TEXT_AREA];
12647 if (row->continued_p)
12648 row->pixel_width -= it->continuation_pixel_width;
12649 else if (row->truncated_on_right_p)
12650 row->pixel_width -= it->truncation_pixel_width;
12651 row->ascent = row->phys_ascent = 0;
12652 row->height = row->phys_height = row->visible_height = 1;
12655 /* Compute a hash code for this row. */
12656 row->hash = 0;
12657 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12658 for (i = 0; i < row->used[area]; ++i)
12659 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12660 + row->glyphs[area][i].u.val
12661 + row->glyphs[area][i].face_id
12662 + row->glyphs[area][i].padding_p
12663 + (row->glyphs[area][i].type << 2));
12665 it->max_ascent = it->max_descent = 0;
12666 it->max_phys_ascent = it->max_phys_descent = 0;
12670 /* Append one space to the glyph row of iterator IT if doing a
12671 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12672 space have the default face, otherwise let it have the same face as
12673 IT->face_id. Value is non-zero if a space was added.
12675 This function is called to make sure that there is always one glyph
12676 at the end of a glyph row that the cursor can be set on under
12677 window-systems. (If there weren't such a glyph we would not know
12678 how wide and tall a box cursor should be displayed).
12680 At the same time this space let's a nicely handle clearing to the
12681 end of the line if the row ends in italic text. */
12683 static int
12684 append_space (it, default_face_p)
12685 struct it *it;
12686 int default_face_p;
12688 if (FRAME_WINDOW_P (it->f))
12690 int n = it->glyph_row->used[TEXT_AREA];
12692 if (it->glyph_row->glyphs[TEXT_AREA] + n
12693 < it->glyph_row->glyphs[1 + TEXT_AREA])
12695 /* Save some values that must not be changed.
12696 Must save IT->c and IT->len because otherwise
12697 ITERATOR_AT_END_P wouldn't work anymore after
12698 append_space has been called. */
12699 enum display_element_type saved_what = it->what;
12700 int saved_c = it->c, saved_len = it->len;
12701 int saved_x = it->current_x;
12702 int saved_face_id = it->face_id;
12703 struct text_pos saved_pos;
12704 Lisp_Object saved_object;
12705 struct face *face;
12707 saved_object = it->object;
12708 saved_pos = it->position;
12710 it->what = IT_CHARACTER;
12711 bzero (&it->position, sizeof it->position);
12712 it->object = make_number (0);
12713 it->c = ' ';
12714 it->len = 1;
12716 if (default_face_p)
12717 it->face_id = DEFAULT_FACE_ID;
12718 else if (it->face_before_selective_p)
12719 it->face_id = it->saved_face_id;
12720 face = FACE_FROM_ID (it->f, it->face_id);
12721 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12723 PRODUCE_GLYPHS (it);
12725 it->current_x = saved_x;
12726 it->object = saved_object;
12727 it->position = saved_pos;
12728 it->what = saved_what;
12729 it->face_id = saved_face_id;
12730 it->len = saved_len;
12731 it->c = saved_c;
12732 return 1;
12736 return 0;
12740 /* Extend the face of the last glyph in the text area of IT->glyph_row
12741 to the end of the display line. Called from display_line.
12742 If the glyph row is empty, add a space glyph to it so that we
12743 know the face to draw. Set the glyph row flag fill_line_p. */
12745 static void
12746 extend_face_to_end_of_line (it)
12747 struct it *it;
12749 struct face *face;
12750 struct frame *f = it->f;
12752 /* If line is already filled, do nothing. */
12753 if (it->current_x >= it->last_visible_x)
12754 return;
12756 /* Face extension extends the background and box of IT->face_id
12757 to the end of the line. If the background equals the background
12758 of the frame, we don't have to do anything. */
12759 if (it->face_before_selective_p)
12760 face = FACE_FROM_ID (it->f, it->saved_face_id);
12761 else
12762 face = FACE_FROM_ID (f, it->face_id);
12764 if (FRAME_WINDOW_P (f)
12765 && face->box == FACE_NO_BOX
12766 && face->background == FRAME_BACKGROUND_PIXEL (f)
12767 && !face->stipple)
12768 return;
12770 /* Set the glyph row flag indicating that the face of the last glyph
12771 in the text area has to be drawn to the end of the text area. */
12772 it->glyph_row->fill_line_p = 1;
12774 /* If current character of IT is not ASCII, make sure we have the
12775 ASCII face. This will be automatically undone the next time
12776 get_next_display_element returns a multibyte character. Note
12777 that the character will always be single byte in unibyte text. */
12778 if (!SINGLE_BYTE_CHAR_P (it->c))
12780 it->face_id = FACE_FOR_CHAR (f, face, 0);
12783 if (FRAME_WINDOW_P (f))
12785 /* If the row is empty, add a space with the current face of IT,
12786 so that we know which face to draw. */
12787 if (it->glyph_row->used[TEXT_AREA] == 0)
12789 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12790 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12791 it->glyph_row->used[TEXT_AREA] = 1;
12794 else
12796 /* Save some values that must not be changed. */
12797 int saved_x = it->current_x;
12798 struct text_pos saved_pos;
12799 Lisp_Object saved_object;
12800 enum display_element_type saved_what = it->what;
12801 int saved_face_id = it->face_id;
12803 saved_object = it->object;
12804 saved_pos = it->position;
12806 it->what = IT_CHARACTER;
12807 bzero (&it->position, sizeof it->position);
12808 it->object = make_number (0);
12809 it->c = ' ';
12810 it->len = 1;
12811 it->face_id = face->id;
12813 PRODUCE_GLYPHS (it);
12815 while (it->current_x <= it->last_visible_x)
12816 PRODUCE_GLYPHS (it);
12818 /* Don't count these blanks really. It would let us insert a left
12819 truncation glyph below and make us set the cursor on them, maybe. */
12820 it->current_x = saved_x;
12821 it->object = saved_object;
12822 it->position = saved_pos;
12823 it->what = saved_what;
12824 it->face_id = saved_face_id;
12829 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12830 trailing whitespace. */
12832 static int
12833 trailing_whitespace_p (charpos)
12834 int charpos;
12836 int bytepos = CHAR_TO_BYTE (charpos);
12837 int c = 0;
12839 while (bytepos < ZV_BYTE
12840 && (c = FETCH_CHAR (bytepos),
12841 c == ' ' || c == '\t'))
12842 ++bytepos;
12844 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12846 if (bytepos != PT_BYTE)
12847 return 1;
12849 return 0;
12853 /* Highlight trailing whitespace, if any, in ROW. */
12855 void
12856 highlight_trailing_whitespace (f, row)
12857 struct frame *f;
12858 struct glyph_row *row;
12860 int used = row->used[TEXT_AREA];
12862 if (used)
12864 struct glyph *start = row->glyphs[TEXT_AREA];
12865 struct glyph *glyph = start + used - 1;
12867 /* Skip over glyphs inserted to display the cursor at the
12868 end of a line, for extending the face of the last glyph
12869 to the end of the line on terminals, and for truncation
12870 and continuation glyphs. */
12871 while (glyph >= start
12872 && glyph->type == CHAR_GLYPH
12873 && INTEGERP (glyph->object))
12874 --glyph;
12876 /* If last glyph is a space or stretch, and it's trailing
12877 whitespace, set the face of all trailing whitespace glyphs in
12878 IT->glyph_row to `trailing-whitespace'. */
12879 if (glyph >= start
12880 && BUFFERP (glyph->object)
12881 && (glyph->type == STRETCH_GLYPH
12882 || (glyph->type == CHAR_GLYPH
12883 && glyph->u.ch == ' '))
12884 && trailing_whitespace_p (glyph->charpos))
12886 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12888 while (glyph >= start
12889 && BUFFERP (glyph->object)
12890 && (glyph->type == STRETCH_GLYPH
12891 || (glyph->type == CHAR_GLYPH
12892 && glyph->u.ch == ' ')))
12893 (glyph--)->face_id = face_id;
12899 /* Value is non-zero if glyph row ROW in window W should be
12900 used to hold the cursor. */
12902 static int
12903 cursor_row_p (w, row)
12904 struct window *w;
12905 struct glyph_row *row;
12907 int cursor_row_p = 1;
12909 if (PT == MATRIX_ROW_END_CHARPOS (row))
12911 /* If the row ends with a newline from a string, we don't want
12912 the cursor there (if the row is continued it doesn't end in a
12913 newline). */
12914 if (CHARPOS (row->end.string_pos) >= 0
12915 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12916 cursor_row_p = row->continued_p;
12918 /* If the row ends at ZV, display the cursor at the end of that
12919 row instead of at the start of the row below. */
12920 else if (row->ends_at_zv_p)
12921 cursor_row_p = 1;
12922 else
12923 cursor_row_p = 0;
12926 return cursor_row_p;
12930 /* Construct the glyph row IT->glyph_row in the desired matrix of
12931 IT->w from text at the current position of IT. See dispextern.h
12932 for an overview of struct it. Value is non-zero if
12933 IT->glyph_row displays text, as opposed to a line displaying ZV
12934 only. */
12936 static int
12937 display_line (it)
12938 struct it *it;
12940 struct glyph_row *row = it->glyph_row;
12942 /* We always start displaying at hpos zero even if hscrolled. */
12943 xassert (it->hpos == 0 && it->current_x == 0);
12945 /* We must not display in a row that's not a text row. */
12946 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12947 < it->w->desired_matrix->nrows);
12949 /* Is IT->w showing the region? */
12950 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12952 /* Clear the result glyph row and enable it. */
12953 prepare_desired_row (row);
12955 row->y = it->current_y;
12956 row->start = it->current;
12957 row->continuation_lines_width = it->continuation_lines_width;
12958 row->displays_text_p = 1;
12959 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12960 it->starts_in_middle_of_char_p = 0;
12962 /* Arrange the overlays nicely for our purposes. Usually, we call
12963 display_line on only one line at a time, in which case this
12964 can't really hurt too much, or we call it on lines which appear
12965 one after another in the buffer, in which case all calls to
12966 recenter_overlay_lists but the first will be pretty cheap. */
12967 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12969 /* Move over display elements that are not visible because we are
12970 hscrolled. This may stop at an x-position < IT->first_visible_x
12971 if the first glyph is partially visible or if we hit a line end. */
12972 if (it->current_x < it->first_visible_x)
12973 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12974 MOVE_TO_POS | MOVE_TO_X);
12976 /* Get the initial row height. This is either the height of the
12977 text hscrolled, if there is any, or zero. */
12978 row->ascent = it->max_ascent;
12979 row->height = it->max_ascent + it->max_descent;
12980 row->phys_ascent = it->max_phys_ascent;
12981 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12983 /* Loop generating characters. The loop is left with IT on the next
12984 character to display. */
12985 while (1)
12987 int n_glyphs_before, hpos_before, x_before;
12988 int x, i, nglyphs;
12989 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12991 /* Retrieve the next thing to display. Value is zero if end of
12992 buffer reached. */
12993 if (!get_next_display_element (it))
12995 /* Maybe add a space at the end of this line that is used to
12996 display the cursor there under X. Set the charpos of the
12997 first glyph of blank lines not corresponding to any text
12998 to -1. */
12999 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13000 || row->used[TEXT_AREA] == 0)
13002 row->glyphs[TEXT_AREA]->charpos = -1;
13003 row->displays_text_p = 0;
13005 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13006 && (!MINI_WINDOW_P (it->w)
13007 || (minibuf_level && EQ (it->window, minibuf_window))))
13008 row->indicate_empty_line_p = 1;
13011 it->continuation_lines_width = 0;
13012 row->ends_at_zv_p = 1;
13013 break;
13016 /* Now, get the metrics of what we want to display. This also
13017 generates glyphs in `row' (which is IT->glyph_row). */
13018 n_glyphs_before = row->used[TEXT_AREA];
13019 x = it->current_x;
13021 /* Remember the line height so far in case the next element doesn't
13022 fit on the line. */
13023 if (!it->truncate_lines_p)
13025 ascent = it->max_ascent;
13026 descent = it->max_descent;
13027 phys_ascent = it->max_phys_ascent;
13028 phys_descent = it->max_phys_descent;
13031 PRODUCE_GLYPHS (it);
13033 /* If this display element was in marginal areas, continue with
13034 the next one. */
13035 if (it->area != TEXT_AREA)
13037 row->ascent = max (row->ascent, it->max_ascent);
13038 row->height = max (row->height, it->max_ascent + it->max_descent);
13039 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13040 row->phys_height = max (row->phys_height,
13041 it->max_phys_ascent + it->max_phys_descent);
13042 set_iterator_to_next (it, 1);
13043 continue;
13046 /* Does the display element fit on the line? If we truncate
13047 lines, we should draw past the right edge of the window. If
13048 we don't truncate, we want to stop so that we can display the
13049 continuation glyph before the right margin. If lines are
13050 continued, there are two possible strategies for characters
13051 resulting in more than 1 glyph (e.g. tabs): Display as many
13052 glyphs as possible in this line and leave the rest for the
13053 continuation line, or display the whole element in the next
13054 line. Original redisplay did the former, so we do it also. */
13055 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13056 hpos_before = it->hpos;
13057 x_before = x;
13059 if (/* Not a newline. */
13060 nglyphs > 0
13061 /* Glyphs produced fit entirely in the line. */
13062 && it->current_x < it->last_visible_x)
13064 it->hpos += nglyphs;
13065 row->ascent = max (row->ascent, it->max_ascent);
13066 row->height = max (row->height, it->max_ascent + it->max_descent);
13067 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13068 row->phys_height = max (row->phys_height,
13069 it->max_phys_ascent + it->max_phys_descent);
13070 if (it->current_x - it->pixel_width < it->first_visible_x)
13071 row->x = x - it->first_visible_x;
13073 else
13075 int new_x;
13076 struct glyph *glyph;
13078 for (i = 0; i < nglyphs; ++i, x = new_x)
13080 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13081 new_x = x + glyph->pixel_width;
13083 if (/* Lines are continued. */
13084 !it->truncate_lines_p
13085 && (/* Glyph doesn't fit on the line. */
13086 new_x > it->last_visible_x
13087 /* Or it fits exactly on a window system frame. */
13088 || (new_x == it->last_visible_x
13089 && FRAME_WINDOW_P (it->f))))
13091 /* End of a continued line. */
13093 if (it->hpos == 0
13094 || (new_x == it->last_visible_x
13095 && FRAME_WINDOW_P (it->f)))
13097 /* Current glyph is the only one on the line or
13098 fits exactly on the line. We must continue
13099 the line because we can't draw the cursor
13100 after the glyph. */
13101 row->continued_p = 1;
13102 it->current_x = new_x;
13103 it->continuation_lines_width += new_x;
13104 ++it->hpos;
13105 if (i == nglyphs - 1)
13106 set_iterator_to_next (it, 1);
13108 else if (CHAR_GLYPH_PADDING_P (*glyph)
13109 && !FRAME_WINDOW_P (it->f))
13111 /* A padding glyph that doesn't fit on this line.
13112 This means the whole character doesn't fit
13113 on the line. */
13114 row->used[TEXT_AREA] = n_glyphs_before;
13116 /* Fill the rest of the row with continuation
13117 glyphs like in 20.x. */
13118 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13119 < row->glyphs[1 + TEXT_AREA])
13120 produce_special_glyphs (it, IT_CONTINUATION);
13122 row->continued_p = 1;
13123 it->current_x = x_before;
13124 it->continuation_lines_width += x_before;
13126 /* Restore the height to what it was before the
13127 element not fitting on the line. */
13128 it->max_ascent = ascent;
13129 it->max_descent = descent;
13130 it->max_phys_ascent = phys_ascent;
13131 it->max_phys_descent = phys_descent;
13133 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13135 /* A TAB that extends past the right edge of the
13136 window. This produces a single glyph on
13137 window system frames. We leave the glyph in
13138 this row and let it fill the row, but don't
13139 consume the TAB. */
13140 it->continuation_lines_width += it->last_visible_x;
13141 row->ends_in_middle_of_char_p = 1;
13142 row->continued_p = 1;
13143 glyph->pixel_width = it->last_visible_x - x;
13144 it->starts_in_middle_of_char_p = 1;
13146 else
13148 /* Something other than a TAB that draws past
13149 the right edge of the window. Restore
13150 positions to values before the element. */
13151 row->used[TEXT_AREA] = n_glyphs_before + i;
13153 /* Display continuation glyphs. */
13154 if (!FRAME_WINDOW_P (it->f))
13155 produce_special_glyphs (it, IT_CONTINUATION);
13156 row->continued_p = 1;
13158 it->continuation_lines_width += x;
13160 if (nglyphs > 1 && i > 0)
13162 row->ends_in_middle_of_char_p = 1;
13163 it->starts_in_middle_of_char_p = 1;
13166 /* Restore the height to what it was before the
13167 element not fitting on the line. */
13168 it->max_ascent = ascent;
13169 it->max_descent = descent;
13170 it->max_phys_ascent = phys_ascent;
13171 it->max_phys_descent = phys_descent;
13174 break;
13176 else if (new_x > it->first_visible_x)
13178 /* Increment number of glyphs actually displayed. */
13179 ++it->hpos;
13181 if (x < it->first_visible_x)
13182 /* Glyph is partially visible, i.e. row starts at
13183 negative X position. */
13184 row->x = x - it->first_visible_x;
13186 else
13188 /* Glyph is completely off the left margin of the
13189 window. This should not happen because of the
13190 move_it_in_display_line at the start of
13191 this function. */
13192 abort ();
13196 row->ascent = max (row->ascent, it->max_ascent);
13197 row->height = max (row->height, it->max_ascent + it->max_descent);
13198 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13199 row->phys_height = max (row->phys_height,
13200 it->max_phys_ascent + it->max_phys_descent);
13202 /* End of this display line if row is continued. */
13203 if (row->continued_p)
13204 break;
13207 /* Is this a line end? If yes, we're also done, after making
13208 sure that a non-default face is extended up to the right
13209 margin of the window. */
13210 if (ITERATOR_AT_END_OF_LINE_P (it))
13212 int used_before = row->used[TEXT_AREA];
13214 row->ends_in_newline_from_string_p = STRINGP (it->object);
13216 /* Add a space at the end of the line that is used to
13217 display the cursor there. */
13218 append_space (it, 0);
13220 /* Extend the face to the end of the line. */
13221 extend_face_to_end_of_line (it);
13223 /* Make sure we have the position. */
13224 if (used_before == 0)
13225 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13227 /* Consume the line end. This skips over invisible lines. */
13228 set_iterator_to_next (it, 1);
13229 it->continuation_lines_width = 0;
13230 break;
13233 /* Proceed with next display element. Note that this skips
13234 over lines invisible because of selective display. */
13235 set_iterator_to_next (it, 1);
13237 /* If we truncate lines, we are done when the last displayed
13238 glyphs reach past the right margin of the window. */
13239 if (it->truncate_lines_p
13240 && (FRAME_WINDOW_P (it->f)
13241 ? (it->current_x >= it->last_visible_x)
13242 : (it->current_x > it->last_visible_x)))
13244 /* Maybe add truncation glyphs. */
13245 if (!FRAME_WINDOW_P (it->f))
13247 int i, n;
13249 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13250 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13251 break;
13253 for (n = row->used[TEXT_AREA]; i < n; ++i)
13255 row->used[TEXT_AREA] = i;
13256 produce_special_glyphs (it, IT_TRUNCATION);
13260 row->truncated_on_right_p = 1;
13261 it->continuation_lines_width = 0;
13262 reseat_at_next_visible_line_start (it, 0);
13263 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13264 it->hpos = hpos_before;
13265 it->current_x = x_before;
13266 break;
13270 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13271 at the left window margin. */
13272 if (it->first_visible_x
13273 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13275 if (!FRAME_WINDOW_P (it->f))
13276 insert_left_trunc_glyphs (it);
13277 row->truncated_on_left_p = 1;
13280 /* If the start of this line is the overlay arrow-position, then
13281 mark this glyph row as the one containing the overlay arrow.
13282 This is clearly a mess with variable size fonts. It would be
13283 better to let it be displayed like cursors under X. */
13284 if (MARKERP (Voverlay_arrow_position)
13285 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13286 && (MATRIX_ROW_START_CHARPOS (row)
13287 == marker_position (Voverlay_arrow_position))
13288 && STRINGP (Voverlay_arrow_string)
13289 && ! overlay_arrow_seen)
13291 /* Overlay arrow in window redisplay is a fringe bitmap. */
13292 if (!FRAME_WINDOW_P (it->f))
13294 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13295 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13296 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13297 struct glyph *p = row->glyphs[TEXT_AREA];
13298 struct glyph *p2, *end;
13300 /* Copy the arrow glyphs. */
13301 while (glyph < arrow_end)
13302 *p++ = *glyph++;
13304 /* Throw away padding glyphs. */
13305 p2 = p;
13306 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13307 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13308 ++p2;
13309 if (p2 > p)
13311 while (p2 < end)
13312 *p++ = *p2++;
13313 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13317 overlay_arrow_seen = 1;
13318 row->overlay_arrow_p = 1;
13321 /* Compute pixel dimensions of this line. */
13322 compute_line_metrics (it);
13324 /* Remember the position at which this line ends. */
13325 row->end = it->current;
13327 /* Maybe set the cursor. */
13328 if (it->w->cursor.vpos < 0
13329 && PT >= MATRIX_ROW_START_CHARPOS (row)
13330 && PT <= MATRIX_ROW_END_CHARPOS (row)
13331 && cursor_row_p (it->w, row))
13332 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13334 /* Highlight trailing whitespace. */
13335 if (!NILP (Vshow_trailing_whitespace))
13336 highlight_trailing_whitespace (it->f, it->glyph_row);
13338 /* Prepare for the next line. This line starts horizontally at (X
13339 HPOS) = (0 0). Vertical positions are incremented. As a
13340 convenience for the caller, IT->glyph_row is set to the next
13341 row to be used. */
13342 it->current_x = it->hpos = 0;
13343 it->current_y += row->height;
13344 ++it->vpos;
13345 ++it->glyph_row;
13346 return row->displays_text_p;
13351 /***********************************************************************
13352 Menu Bar
13353 ***********************************************************************/
13355 /* Redisplay the menu bar in the frame for window W.
13357 The menu bar of X frames that don't have X toolkit support is
13358 displayed in a special window W->frame->menu_bar_window.
13360 The menu bar of terminal frames is treated specially as far as
13361 glyph matrices are concerned. Menu bar lines are not part of
13362 windows, so the update is done directly on the frame matrix rows
13363 for the menu bar. */
13365 static void
13366 display_menu_bar (w)
13367 struct window *w;
13369 struct frame *f = XFRAME (WINDOW_FRAME (w));
13370 struct it it;
13371 Lisp_Object items;
13372 int i;
13374 /* Don't do all this for graphical frames. */
13375 #ifdef HAVE_NTGUI
13376 if (!NILP (Vwindow_system))
13377 return;
13378 #endif
13379 #ifdef USE_X_TOOLKIT
13380 if (FRAME_X_P (f))
13381 return;
13382 #endif
13383 #ifdef macintosh
13384 if (FRAME_MAC_P (f))
13385 return;
13386 #endif
13388 #ifdef USE_X_TOOLKIT
13389 xassert (!FRAME_WINDOW_P (f));
13390 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13391 it.first_visible_x = 0;
13392 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13393 #else /* not USE_X_TOOLKIT */
13394 if (FRAME_WINDOW_P (f))
13396 /* Menu bar lines are displayed in the desired matrix of the
13397 dummy window menu_bar_window. */
13398 struct window *menu_w;
13399 xassert (WINDOWP (f->menu_bar_window));
13400 menu_w = XWINDOW (f->menu_bar_window);
13401 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13402 MENU_FACE_ID);
13403 it.first_visible_x = 0;
13404 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13406 else
13408 /* This is a TTY frame, i.e. character hpos/vpos are used as
13409 pixel x/y. */
13410 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13411 MENU_FACE_ID);
13412 it.first_visible_x = 0;
13413 it.last_visible_x = FRAME_WIDTH (f);
13415 #endif /* not USE_X_TOOLKIT */
13417 if (! mode_line_inverse_video)
13418 /* Force the menu-bar to be displayed in the default face. */
13419 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13421 /* Clear all rows of the menu bar. */
13422 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13424 struct glyph_row *row = it.glyph_row + i;
13425 clear_glyph_row (row);
13426 row->enabled_p = 1;
13427 row->full_width_p = 1;
13430 /* Display all items of the menu bar. */
13431 items = FRAME_MENU_BAR_ITEMS (it.f);
13432 for (i = 0; i < XVECTOR (items)->size; i += 4)
13434 Lisp_Object string;
13436 /* Stop at nil string. */
13437 string = AREF (items, i + 1);
13438 if (NILP (string))
13439 break;
13441 /* Remember where item was displayed. */
13442 AREF (items, i + 3) = make_number (it.hpos);
13444 /* Display the item, pad with one space. */
13445 if (it.current_x < it.last_visible_x)
13446 display_string (NULL, string, Qnil, 0, 0, &it,
13447 SCHARS (string) + 1, 0, 0, -1);
13450 /* Fill out the line with spaces. */
13451 if (it.current_x < it.last_visible_x)
13452 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13454 /* Compute the total height of the lines. */
13455 compute_line_metrics (&it);
13460 /***********************************************************************
13461 Mode Line
13462 ***********************************************************************/
13464 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13465 FORCE is non-zero, redisplay mode lines unconditionally.
13466 Otherwise, redisplay only mode lines that are garbaged. Value is
13467 the number of windows whose mode lines were redisplayed. */
13469 static int
13470 redisplay_mode_lines (window, force)
13471 Lisp_Object window;
13472 int force;
13474 int nwindows = 0;
13476 while (!NILP (window))
13478 struct window *w = XWINDOW (window);
13480 if (WINDOWP (w->hchild))
13481 nwindows += redisplay_mode_lines (w->hchild, force);
13482 else if (WINDOWP (w->vchild))
13483 nwindows += redisplay_mode_lines (w->vchild, force);
13484 else if (force
13485 || FRAME_GARBAGED_P (XFRAME (w->frame))
13486 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13488 struct text_pos lpoint;
13489 struct buffer *old = current_buffer;
13491 /* Set the window's buffer for the mode line display. */
13492 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13493 set_buffer_internal_1 (XBUFFER (w->buffer));
13495 /* Point refers normally to the selected window. For any
13496 other window, set up appropriate value. */
13497 if (!EQ (window, selected_window))
13499 struct text_pos pt;
13501 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13502 if (CHARPOS (pt) < BEGV)
13503 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13504 else if (CHARPOS (pt) > (ZV - 1))
13505 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13506 else
13507 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13510 /* Display mode lines. */
13511 clear_glyph_matrix (w->desired_matrix);
13512 if (display_mode_lines (w))
13514 ++nwindows;
13515 w->must_be_updated_p = 1;
13518 /* Restore old settings. */
13519 set_buffer_internal_1 (old);
13520 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13523 window = w->next;
13526 return nwindows;
13530 /* Display the mode and/or top line of window W. Value is the number
13531 of mode lines displayed. */
13533 static int
13534 display_mode_lines (w)
13535 struct window *w;
13537 Lisp_Object old_selected_window, old_selected_frame;
13538 int n = 0;
13540 old_selected_frame = selected_frame;
13541 selected_frame = w->frame;
13542 old_selected_window = selected_window;
13543 XSETWINDOW (selected_window, w);
13545 /* These will be set while the mode line specs are processed. */
13546 line_number_displayed = 0;
13547 w->column_number_displayed = Qnil;
13549 if (WINDOW_WANTS_MODELINE_P (w))
13551 struct window *sel_w = XWINDOW (old_selected_window);
13553 /* Select mode line face based on the real selected window. */
13554 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13555 current_buffer->mode_line_format);
13556 ++n;
13559 if (WINDOW_WANTS_HEADER_LINE_P (w))
13561 display_mode_line (w, HEADER_LINE_FACE_ID,
13562 current_buffer->header_line_format);
13563 ++n;
13566 selected_frame = old_selected_frame;
13567 selected_window = old_selected_window;
13568 return n;
13572 /* Display mode or top line of window W. FACE_ID specifies which line
13573 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13574 FORMAT is the mode line format to display. Value is the pixel
13575 height of the mode line displayed. */
13577 static int
13578 display_mode_line (w, face_id, format)
13579 struct window *w;
13580 enum face_id face_id;
13581 Lisp_Object format;
13583 struct it it;
13584 struct face *face;
13586 init_iterator (&it, w, -1, -1, NULL, face_id);
13587 prepare_desired_row (it.glyph_row);
13589 if (! mode_line_inverse_video)
13590 /* Force the mode-line to be displayed in the default face. */
13591 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13593 /* Temporarily make frame's keyboard the current kboard so that
13594 kboard-local variables in the mode_line_format will get the right
13595 values. */
13596 push_frame_kboard (it.f);
13597 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13598 pop_frame_kboard ();
13600 /* Fill up with spaces. */
13601 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13603 compute_line_metrics (&it);
13604 it.glyph_row->full_width_p = 1;
13605 it.glyph_row->mode_line_p = 1;
13606 it.glyph_row->continued_p = 0;
13607 it.glyph_row->truncated_on_left_p = 0;
13608 it.glyph_row->truncated_on_right_p = 0;
13610 /* Make a 3D mode-line have a shadow at its right end. */
13611 face = FACE_FROM_ID (it.f, face_id);
13612 extend_face_to_end_of_line (&it);
13613 if (face->box != FACE_NO_BOX)
13615 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13616 + it.glyph_row->used[TEXT_AREA] - 1);
13617 last->right_box_line_p = 1;
13620 return it.glyph_row->height;
13623 /* Alist that caches the results of :propertize.
13624 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13625 Lisp_Object mode_line_proptrans_alist;
13627 /* Contribute ELT to the mode line for window IT->w. How it
13628 translates into text depends on its data type.
13630 IT describes the display environment in which we display, as usual.
13632 DEPTH is the depth in recursion. It is used to prevent
13633 infinite recursion here.
13635 FIELD_WIDTH is the number of characters the display of ELT should
13636 occupy in the mode line, and PRECISION is the maximum number of
13637 characters to display from ELT's representation. See
13638 display_string for details.
13640 Returns the hpos of the end of the text generated by ELT.
13642 PROPS is a property list to add to any string we encounter.
13644 If RISKY is nonzero, remove (disregard) any properties in any string
13645 we encounter, and ignore :eval and :propertize. */
13647 static int
13648 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13649 struct it *it;
13650 int depth;
13651 int field_width, precision;
13652 Lisp_Object elt, props;
13653 int risky;
13655 int n = 0, field, prec;
13656 int literal = 0;
13658 tail_recurse:
13659 if (depth > 10)
13660 goto invalid;
13662 depth++;
13664 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13666 case Lisp_String:
13668 /* A string: output it and check for %-constructs within it. */
13669 unsigned char c;
13670 unsigned char *this, *lisp_string;
13672 if (!NILP (props) || risky)
13674 Lisp_Object oprops, aelt;
13675 oprops = Ftext_properties_at (make_number (0), elt);
13677 if (NILP (Fequal (props, oprops)) || risky)
13679 /* If the starting string has properties,
13680 merge the specified ones onto the existing ones. */
13681 if (! NILP (oprops) && !risky)
13683 Lisp_Object tem;
13685 oprops = Fcopy_sequence (oprops);
13686 tem = props;
13687 while (CONSP (tem))
13689 oprops = Fplist_put (oprops, XCAR (tem),
13690 XCAR (XCDR (tem)));
13691 tem = XCDR (XCDR (tem));
13693 props = oprops;
13696 aelt = Fassoc (elt, mode_line_proptrans_alist);
13697 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13698 elt = XCAR (aelt);
13699 else
13701 elt = Fcopy_sequence (elt);
13702 Fset_text_properties (make_number (0), Flength (elt),
13703 props, elt);
13704 mode_line_proptrans_alist
13705 = Fcons (Fcons (elt, props),
13706 mode_line_proptrans_alist);
13711 this = SDATA (elt);
13712 lisp_string = this;
13714 if (literal)
13716 prec = precision - n;
13717 if (frame_title_ptr)
13718 n += store_frame_title (SDATA (elt), -1, prec);
13719 else
13720 n += display_string (NULL, elt, Qnil, 0, 0, it,
13721 0, prec, 0, STRING_MULTIBYTE (elt));
13723 break;
13726 while ((precision <= 0 || n < precision)
13727 && *this
13728 && (frame_title_ptr
13729 || it->current_x < it->last_visible_x))
13731 unsigned char *last = this;
13733 /* Advance to end of string or next format specifier. */
13734 while ((c = *this++) != '\0' && c != '%')
13737 if (this - 1 != last)
13739 /* Output to end of string or up to '%'. Field width
13740 is length of string. Don't output more than
13741 PRECISION allows us. */
13742 --this;
13744 prec = chars_in_text (last, this - last);
13745 if (precision > 0 && prec > precision - n)
13746 prec = precision - n;
13748 if (frame_title_ptr)
13749 n += store_frame_title (last, 0, prec);
13750 else
13752 int bytepos = last - lisp_string;
13753 int charpos = string_byte_to_char (elt, bytepos);
13754 n += display_string (NULL, elt, Qnil, 0, charpos,
13755 it, 0, prec, 0,
13756 STRING_MULTIBYTE (elt));
13759 else /* c == '%' */
13761 unsigned char *percent_position = this;
13763 /* Get the specified minimum width. Zero means
13764 don't pad. */
13765 field = 0;
13766 while ((c = *this++) >= '0' && c <= '9')
13767 field = field * 10 + c - '0';
13769 /* Don't pad beyond the total padding allowed. */
13770 if (field_width - n > 0 && field > field_width - n)
13771 field = field_width - n;
13773 /* Note that either PRECISION <= 0 or N < PRECISION. */
13774 prec = precision - n;
13776 if (c == 'M')
13777 n += display_mode_element (it, depth, field, prec,
13778 Vglobal_mode_string, props,
13779 risky);
13780 else if (c != 0)
13782 int multibyte;
13783 int bytepos, charpos;
13784 unsigned char *spec;
13786 bytepos = percent_position - lisp_string;
13787 charpos = (STRING_MULTIBYTE (elt)
13788 ? string_byte_to_char (elt, bytepos)
13789 : bytepos);
13791 spec
13792 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13794 if (frame_title_ptr)
13795 n += store_frame_title (spec, field, prec);
13796 else
13798 int nglyphs_before, nwritten;
13800 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13801 nwritten = display_string (spec, Qnil, elt,
13802 charpos, 0, it,
13803 field, prec, 0,
13804 multibyte);
13806 /* Assign to the glyphs written above the
13807 string where the `%x' came from, position
13808 of the `%'. */
13809 if (nwritten > 0)
13811 struct glyph *glyph
13812 = (it->glyph_row->glyphs[TEXT_AREA]
13813 + nglyphs_before);
13814 int i;
13816 for (i = 0; i < nwritten; ++i)
13818 glyph[i].object = elt;
13819 glyph[i].charpos = charpos;
13822 n += nwritten;
13826 else /* c == 0 */
13827 break;
13831 break;
13833 case Lisp_Symbol:
13834 /* A symbol: process the value of the symbol recursively
13835 as if it appeared here directly. Avoid error if symbol void.
13836 Special case: if value of symbol is a string, output the string
13837 literally. */
13839 register Lisp_Object tem;
13841 /* If the variable is not marked as risky to set
13842 then its contents are risky to use. */
13843 if (NILP (Fget (elt, Qrisky_local_variable)))
13844 risky = 1;
13846 tem = Fboundp (elt);
13847 if (!NILP (tem))
13849 tem = Fsymbol_value (elt);
13850 /* If value is a string, output that string literally:
13851 don't check for % within it. */
13852 if (STRINGP (tem))
13853 literal = 1;
13855 if (!EQ (tem, elt))
13857 /* Give up right away for nil or t. */
13858 elt = tem;
13859 goto tail_recurse;
13863 break;
13865 case Lisp_Cons:
13867 register Lisp_Object car, tem;
13869 /* A cons cell: five distinct cases.
13870 If first element is :eval or :propertize, do something special.
13871 If first element is a string or a cons, process all the elements
13872 and effectively concatenate them.
13873 If first element is a negative number, truncate displaying cdr to
13874 at most that many characters. If positive, pad (with spaces)
13875 to at least that many characters.
13876 If first element is a symbol, process the cadr or caddr recursively
13877 according to whether the symbol's value is non-nil or nil. */
13878 car = XCAR (elt);
13879 if (EQ (car, QCeval))
13881 /* An element of the form (:eval FORM) means evaluate FORM
13882 and use the result as mode line elements. */
13884 if (risky)
13885 break;
13887 if (CONSP (XCDR (elt)))
13889 Lisp_Object spec;
13890 spec = safe_eval (XCAR (XCDR (elt)));
13891 n += display_mode_element (it, depth, field_width - n,
13892 precision - n, spec, props,
13893 risky);
13896 else if (EQ (car, QCpropertize))
13898 /* An element of the form (:propertize ELT PROPS...)
13899 means display ELT but applying properties PROPS. */
13901 if (risky)
13902 break;
13904 if (CONSP (XCDR (elt)))
13905 n += display_mode_element (it, depth, field_width - n,
13906 precision - n, XCAR (XCDR (elt)),
13907 XCDR (XCDR (elt)), risky);
13909 else if (SYMBOLP (car))
13911 tem = Fboundp (car);
13912 elt = XCDR (elt);
13913 if (!CONSP (elt))
13914 goto invalid;
13915 /* elt is now the cdr, and we know it is a cons cell.
13916 Use its car if CAR has a non-nil value. */
13917 if (!NILP (tem))
13919 tem = Fsymbol_value (car);
13920 if (!NILP (tem))
13922 elt = XCAR (elt);
13923 goto tail_recurse;
13926 /* Symbol's value is nil (or symbol is unbound)
13927 Get the cddr of the original list
13928 and if possible find the caddr and use that. */
13929 elt = XCDR (elt);
13930 if (NILP (elt))
13931 break;
13932 else if (!CONSP (elt))
13933 goto invalid;
13934 elt = XCAR (elt);
13935 goto tail_recurse;
13937 else if (INTEGERP (car))
13939 register int lim = XINT (car);
13940 elt = XCDR (elt);
13941 if (lim < 0)
13943 /* Negative int means reduce maximum width. */
13944 if (precision <= 0)
13945 precision = -lim;
13946 else
13947 precision = min (precision, -lim);
13949 else if (lim > 0)
13951 /* Padding specified. Don't let it be more than
13952 current maximum. */
13953 if (precision > 0)
13954 lim = min (precision, lim);
13956 /* If that's more padding than already wanted, queue it.
13957 But don't reduce padding already specified even if
13958 that is beyond the current truncation point. */
13959 field_width = max (lim, field_width);
13961 goto tail_recurse;
13963 else if (STRINGP (car) || CONSP (car))
13965 register int limit = 50;
13966 /* Limit is to protect against circular lists. */
13967 while (CONSP (elt)
13968 && --limit > 0
13969 && (precision <= 0 || n < precision))
13971 n += display_mode_element (it, depth, field_width - n,
13972 precision - n, XCAR (elt),
13973 props, risky);
13974 elt = XCDR (elt);
13978 break;
13980 default:
13981 invalid:
13982 if (frame_title_ptr)
13983 n += store_frame_title ("*invalid*", 0, precision - n);
13984 else
13985 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13986 precision - n, 0, 0);
13987 return n;
13990 /* Pad to FIELD_WIDTH. */
13991 if (field_width > 0 && n < field_width)
13993 if (frame_title_ptr)
13994 n += store_frame_title ("", field_width - n, 0);
13995 else
13996 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13997 0, 0, 0);
14000 return n;
14004 /* Write a null-terminated, right justified decimal representation of
14005 the positive integer D to BUF using a minimal field width WIDTH. */
14007 static void
14008 pint2str (buf, width, d)
14009 register char *buf;
14010 register int width;
14011 register int d;
14013 register char *p = buf;
14015 if (d <= 0)
14016 *p++ = '0';
14017 else
14019 while (d > 0)
14021 *p++ = d % 10 + '0';
14022 d /= 10;
14026 for (width -= (int) (p - buf); width > 0; --width)
14027 *p++ = ' ';
14028 *p-- = '\0';
14029 while (p > buf)
14031 d = *buf;
14032 *buf++ = *p;
14033 *p-- = d;
14037 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14038 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14039 type of CODING_SYSTEM. Return updated pointer into BUF. */
14041 static unsigned char invalid_eol_type[] = "(*invalid*)";
14043 static char *
14044 decode_mode_spec_coding (coding_system, buf, eol_flag)
14045 Lisp_Object coding_system;
14046 register char *buf;
14047 int eol_flag;
14049 Lisp_Object val;
14050 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14051 unsigned char *eol_str;
14052 int eol_str_len;
14053 /* The EOL conversion we are using. */
14054 Lisp_Object eoltype;
14056 val = Fget (coding_system, Qcoding_system);
14057 eoltype = Qnil;
14059 if (!VECTORP (val)) /* Not yet decided. */
14061 if (multibyte)
14062 *buf++ = '-';
14063 if (eol_flag)
14064 eoltype = eol_mnemonic_undecided;
14065 /* Don't mention EOL conversion if it isn't decided. */
14067 else
14069 Lisp_Object eolvalue;
14071 eolvalue = Fget (coding_system, Qeol_type);
14073 if (multibyte)
14074 *buf++ = XFASTINT (AREF (val, 1));
14076 if (eol_flag)
14078 /* The EOL conversion that is normal on this system. */
14080 if (NILP (eolvalue)) /* Not yet decided. */
14081 eoltype = eol_mnemonic_undecided;
14082 else if (VECTORP (eolvalue)) /* Not yet decided. */
14083 eoltype = eol_mnemonic_undecided;
14084 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14085 eoltype = (XFASTINT (eolvalue) == 0
14086 ? eol_mnemonic_unix
14087 : (XFASTINT (eolvalue) == 1
14088 ? eol_mnemonic_dos : eol_mnemonic_mac));
14092 if (eol_flag)
14094 /* Mention the EOL conversion if it is not the usual one. */
14095 if (STRINGP (eoltype))
14097 eol_str = SDATA (eoltype);
14098 eol_str_len = SBYTES (eoltype);
14100 else if (INTEGERP (eoltype)
14101 && CHAR_VALID_P (XINT (eoltype), 0))
14103 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14104 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
14106 else
14108 eol_str = invalid_eol_type;
14109 eol_str_len = sizeof (invalid_eol_type) - 1;
14111 bcopy (eol_str, buf, eol_str_len);
14112 buf += eol_str_len;
14115 return buf;
14118 /* Return a string for the output of a mode line %-spec for window W,
14119 generated by character C. PRECISION >= 0 means don't return a
14120 string longer than that value. FIELD_WIDTH > 0 means pad the
14121 string returned with spaces to that value. Return 1 in *MULTIBYTE
14122 if the result is multibyte text. */
14124 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14126 static char *
14127 decode_mode_spec (w, c, field_width, precision, multibyte)
14128 struct window *w;
14129 register int c;
14130 int field_width, precision;
14131 int *multibyte;
14133 Lisp_Object obj;
14134 struct frame *f = XFRAME (WINDOW_FRAME (w));
14135 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14136 struct buffer *b = XBUFFER (w->buffer);
14138 obj = Qnil;
14139 *multibyte = 0;
14141 switch (c)
14143 case '*':
14144 if (!NILP (b->read_only))
14145 return "%";
14146 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14147 return "*";
14148 return "-";
14150 case '+':
14151 /* This differs from %* only for a modified read-only buffer. */
14152 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14153 return "*";
14154 if (!NILP (b->read_only))
14155 return "%";
14156 return "-";
14158 case '&':
14159 /* This differs from %* in ignoring read-only-ness. */
14160 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14161 return "*";
14162 return "-";
14164 case '%':
14165 return "%";
14167 case '[':
14169 int i;
14170 char *p;
14172 if (command_loop_level > 5)
14173 return "[[[... ";
14174 p = decode_mode_spec_buf;
14175 for (i = 0; i < command_loop_level; i++)
14176 *p++ = '[';
14177 *p = 0;
14178 return decode_mode_spec_buf;
14181 case ']':
14183 int i;
14184 char *p;
14186 if (command_loop_level > 5)
14187 return " ...]]]";
14188 p = decode_mode_spec_buf;
14189 for (i = 0; i < command_loop_level; i++)
14190 *p++ = ']';
14191 *p = 0;
14192 return decode_mode_spec_buf;
14195 case '-':
14197 register int i;
14199 /* Let lots_of_dashes be a string of infinite length. */
14200 if (field_width <= 0
14201 || field_width > sizeof (lots_of_dashes))
14203 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14204 decode_mode_spec_buf[i] = '-';
14205 decode_mode_spec_buf[i] = '\0';
14206 return decode_mode_spec_buf;
14208 else
14209 return lots_of_dashes;
14212 case 'b':
14213 obj = b->name;
14214 break;
14216 case 'c':
14218 int col = current_column ();
14219 w->column_number_displayed = make_number (col);
14220 pint2str (decode_mode_spec_buf, field_width, col);
14221 return decode_mode_spec_buf;
14224 case 'F':
14225 /* %F displays the frame name. */
14226 if (!NILP (f->title))
14227 return (char *) SDATA (f->title);
14228 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14229 return (char *) SDATA (f->name);
14230 return "Emacs";
14232 case 'f':
14233 obj = b->filename;
14234 break;
14236 case 'l':
14238 int startpos = XMARKER (w->start)->charpos;
14239 int startpos_byte = marker_byte_position (w->start);
14240 int line, linepos, linepos_byte, topline;
14241 int nlines, junk;
14242 int height = XFASTINT (w->height);
14244 /* If we decided that this buffer isn't suitable for line numbers,
14245 don't forget that too fast. */
14246 if (EQ (w->base_line_pos, w->buffer))
14247 goto no_value;
14248 /* But do forget it, if the window shows a different buffer now. */
14249 else if (BUFFERP (w->base_line_pos))
14250 w->base_line_pos = Qnil;
14252 /* If the buffer is very big, don't waste time. */
14253 if (INTEGERP (Vline_number_display_limit)
14254 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14256 w->base_line_pos = Qnil;
14257 w->base_line_number = Qnil;
14258 goto no_value;
14261 if (!NILP (w->base_line_number)
14262 && !NILP (w->base_line_pos)
14263 && XFASTINT (w->base_line_pos) <= startpos)
14265 line = XFASTINT (w->base_line_number);
14266 linepos = XFASTINT (w->base_line_pos);
14267 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14269 else
14271 line = 1;
14272 linepos = BUF_BEGV (b);
14273 linepos_byte = BUF_BEGV_BYTE (b);
14276 /* Count lines from base line to window start position. */
14277 nlines = display_count_lines (linepos, linepos_byte,
14278 startpos_byte,
14279 startpos, &junk);
14281 topline = nlines + line;
14283 /* Determine a new base line, if the old one is too close
14284 or too far away, or if we did not have one.
14285 "Too close" means it's plausible a scroll-down would
14286 go back past it. */
14287 if (startpos == BUF_BEGV (b))
14289 w->base_line_number = make_number (topline);
14290 w->base_line_pos = make_number (BUF_BEGV (b));
14292 else if (nlines < height + 25 || nlines > height * 3 + 50
14293 || linepos == BUF_BEGV (b))
14295 int limit = BUF_BEGV (b);
14296 int limit_byte = BUF_BEGV_BYTE (b);
14297 int position;
14298 int distance = (height * 2 + 30) * line_number_display_limit_width;
14300 if (startpos - distance > limit)
14302 limit = startpos - distance;
14303 limit_byte = CHAR_TO_BYTE (limit);
14306 nlines = display_count_lines (startpos, startpos_byte,
14307 limit_byte,
14308 - (height * 2 + 30),
14309 &position);
14310 /* If we couldn't find the lines we wanted within
14311 line_number_display_limit_width chars per line,
14312 give up on line numbers for this window. */
14313 if (position == limit_byte && limit == startpos - distance)
14315 w->base_line_pos = w->buffer;
14316 w->base_line_number = Qnil;
14317 goto no_value;
14320 w->base_line_number = make_number (topline - nlines);
14321 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14324 /* Now count lines from the start pos to point. */
14325 nlines = display_count_lines (startpos, startpos_byte,
14326 PT_BYTE, PT, &junk);
14328 /* Record that we did display the line number. */
14329 line_number_displayed = 1;
14331 /* Make the string to show. */
14332 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14333 return decode_mode_spec_buf;
14334 no_value:
14336 char* p = decode_mode_spec_buf;
14337 int pad = field_width - 2;
14338 while (pad-- > 0)
14339 *p++ = ' ';
14340 *p++ = '?';
14341 *p++ = '?';
14342 *p = '\0';
14343 return decode_mode_spec_buf;
14346 break;
14348 case 'm':
14349 obj = b->mode_name;
14350 break;
14352 case 'n':
14353 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14354 return " Narrow";
14355 break;
14357 case 'p':
14359 int pos = marker_position (w->start);
14360 int total = BUF_ZV (b) - BUF_BEGV (b);
14362 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14364 if (pos <= BUF_BEGV (b))
14365 return "All";
14366 else
14367 return "Bottom";
14369 else if (pos <= BUF_BEGV (b))
14370 return "Top";
14371 else
14373 if (total > 1000000)
14374 /* Do it differently for a large value, to avoid overflow. */
14375 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14376 else
14377 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14378 /* We can't normally display a 3-digit number,
14379 so get us a 2-digit number that is close. */
14380 if (total == 100)
14381 total = 99;
14382 sprintf (decode_mode_spec_buf, "%2d%%", total);
14383 return decode_mode_spec_buf;
14387 /* Display percentage of size above the bottom of the screen. */
14388 case 'P':
14390 int toppos = marker_position (w->start);
14391 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14392 int total = BUF_ZV (b) - BUF_BEGV (b);
14394 if (botpos >= BUF_ZV (b))
14396 if (toppos <= BUF_BEGV (b))
14397 return "All";
14398 else
14399 return "Bottom";
14401 else
14403 if (total > 1000000)
14404 /* Do it differently for a large value, to avoid overflow. */
14405 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14406 else
14407 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14408 /* We can't normally display a 3-digit number,
14409 so get us a 2-digit number that is close. */
14410 if (total == 100)
14411 total = 99;
14412 if (toppos <= BUF_BEGV (b))
14413 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14414 else
14415 sprintf (decode_mode_spec_buf, "%2d%%", total);
14416 return decode_mode_spec_buf;
14420 case 's':
14421 /* status of process */
14422 obj = Fget_buffer_process (w->buffer);
14423 if (NILP (obj))
14424 return "no process";
14425 #ifdef subprocesses
14426 obj = Fsymbol_name (Fprocess_status (obj));
14427 #endif
14428 break;
14430 case 't': /* indicate TEXT or BINARY */
14431 #ifdef MODE_LINE_BINARY_TEXT
14432 return MODE_LINE_BINARY_TEXT (b);
14433 #else
14434 return "T";
14435 #endif
14437 case 'z':
14438 /* coding-system (not including end-of-line format) */
14439 case 'Z':
14440 /* coding-system (including end-of-line type) */
14442 int eol_flag = (c == 'Z');
14443 char *p = decode_mode_spec_buf;
14445 if (! FRAME_WINDOW_P (f))
14447 /* No need to mention EOL here--the terminal never needs
14448 to do EOL conversion. */
14449 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14450 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14452 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14453 p, eol_flag);
14455 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14456 #ifdef subprocesses
14457 obj = Fget_buffer_process (Fcurrent_buffer ());
14458 if (PROCESSP (obj))
14460 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14461 p, eol_flag);
14462 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14463 p, eol_flag);
14465 #endif /* subprocesses */
14466 #endif /* 0 */
14467 *p = 0;
14468 return decode_mode_spec_buf;
14472 if (STRINGP (obj))
14474 *multibyte = STRING_MULTIBYTE (obj);
14475 return (char *) SDATA (obj);
14477 else
14478 return "";
14482 /* Count up to COUNT lines starting from START / START_BYTE.
14483 But don't go beyond LIMIT_BYTE.
14484 Return the number of lines thus found (always nonnegative).
14486 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14488 static int
14489 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14490 int start, start_byte, limit_byte, count;
14491 int *byte_pos_ptr;
14493 register unsigned char *cursor;
14494 unsigned char *base;
14496 register int ceiling;
14497 register unsigned char *ceiling_addr;
14498 int orig_count = count;
14500 /* If we are not in selective display mode,
14501 check only for newlines. */
14502 int selective_display = (!NILP (current_buffer->selective_display)
14503 && !INTEGERP (current_buffer->selective_display));
14505 if (count > 0)
14507 while (start_byte < limit_byte)
14509 ceiling = BUFFER_CEILING_OF (start_byte);
14510 ceiling = min (limit_byte - 1, ceiling);
14511 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14512 base = (cursor = BYTE_POS_ADDR (start_byte));
14513 while (1)
14515 if (selective_display)
14516 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14518 else
14519 while (*cursor != '\n' && ++cursor != ceiling_addr)
14522 if (cursor != ceiling_addr)
14524 if (--count == 0)
14526 start_byte += cursor - base + 1;
14527 *byte_pos_ptr = start_byte;
14528 return orig_count;
14530 else
14531 if (++cursor == ceiling_addr)
14532 break;
14534 else
14535 break;
14537 start_byte += cursor - base;
14540 else
14542 while (start_byte > limit_byte)
14544 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14545 ceiling = max (limit_byte, ceiling);
14546 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14547 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14548 while (1)
14550 if (selective_display)
14551 while (--cursor != ceiling_addr
14552 && *cursor != '\n' && *cursor != 015)
14554 else
14555 while (--cursor != ceiling_addr && *cursor != '\n')
14558 if (cursor != ceiling_addr)
14560 if (++count == 0)
14562 start_byte += cursor - base + 1;
14563 *byte_pos_ptr = start_byte;
14564 /* When scanning backwards, we should
14565 not count the newline posterior to which we stop. */
14566 return - orig_count - 1;
14569 else
14570 break;
14572 /* Here we add 1 to compensate for the last decrement
14573 of CURSOR, which took it past the valid range. */
14574 start_byte += cursor - base + 1;
14578 *byte_pos_ptr = limit_byte;
14580 if (count < 0)
14581 return - orig_count + count;
14582 return orig_count - count;
14588 /***********************************************************************
14589 Displaying strings
14590 ***********************************************************************/
14592 /* Display a NUL-terminated string, starting with index START.
14594 If STRING is non-null, display that C string. Otherwise, the Lisp
14595 string LISP_STRING is displayed.
14597 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14598 FACE_STRING. Display STRING or LISP_STRING with the face at
14599 FACE_STRING_POS in FACE_STRING:
14601 Display the string in the environment given by IT, but use the
14602 standard display table, temporarily.
14604 FIELD_WIDTH is the minimum number of output glyphs to produce.
14605 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14606 with spaces. If STRING has more characters, more than FIELD_WIDTH
14607 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14609 PRECISION is the maximum number of characters to output from
14610 STRING. PRECISION < 0 means don't truncate the string.
14612 This is roughly equivalent to printf format specifiers:
14614 FIELD_WIDTH PRECISION PRINTF
14615 ----------------------------------------
14616 -1 -1 %s
14617 -1 10 %.10s
14618 10 -1 %10s
14619 20 10 %20.10s
14621 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14622 display them, and < 0 means obey the current buffer's value of
14623 enable_multibyte_characters.
14625 Value is the number of glyphs produced. */
14627 static int
14628 display_string (string, lisp_string, face_string, face_string_pos,
14629 start, it, field_width, precision, max_x, multibyte)
14630 unsigned char *string;
14631 Lisp_Object lisp_string;
14632 Lisp_Object face_string;
14633 int face_string_pos;
14634 int start;
14635 struct it *it;
14636 int field_width, precision, max_x;
14637 int multibyte;
14639 int hpos_at_start = it->hpos;
14640 int saved_face_id = it->face_id;
14641 struct glyph_row *row = it->glyph_row;
14643 /* Initialize the iterator IT for iteration over STRING beginning
14644 with index START. */
14645 reseat_to_string (it, string, lisp_string, start,
14646 precision, field_width, multibyte);
14648 /* If displaying STRING, set up the face of the iterator
14649 from LISP_STRING, if that's given. */
14650 if (STRINGP (face_string))
14652 int endptr;
14653 struct face *face;
14655 it->face_id
14656 = face_at_string_position (it->w, face_string, face_string_pos,
14657 0, it->region_beg_charpos,
14658 it->region_end_charpos,
14659 &endptr, it->base_face_id, 0);
14660 face = FACE_FROM_ID (it->f, it->face_id);
14661 it->face_box_p = face->box != FACE_NO_BOX;
14664 /* Set max_x to the maximum allowed X position. Don't let it go
14665 beyond the right edge of the window. */
14666 if (max_x <= 0)
14667 max_x = it->last_visible_x;
14668 else
14669 max_x = min (max_x, it->last_visible_x);
14671 /* Skip over display elements that are not visible. because IT->w is
14672 hscrolled. */
14673 if (it->current_x < it->first_visible_x)
14674 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14675 MOVE_TO_POS | MOVE_TO_X);
14677 row->ascent = it->max_ascent;
14678 row->height = it->max_ascent + it->max_descent;
14679 row->phys_ascent = it->max_phys_ascent;
14680 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14682 /* This condition is for the case that we are called with current_x
14683 past last_visible_x. */
14684 while (it->current_x < max_x)
14686 int x_before, x, n_glyphs_before, i, nglyphs;
14688 /* Get the next display element. */
14689 if (!get_next_display_element (it))
14690 break;
14692 /* Produce glyphs. */
14693 x_before = it->current_x;
14694 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14695 PRODUCE_GLYPHS (it);
14697 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14698 i = 0;
14699 x = x_before;
14700 while (i < nglyphs)
14702 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14704 if (!it->truncate_lines_p
14705 && x + glyph->pixel_width > max_x)
14707 /* End of continued line or max_x reached. */
14708 if (CHAR_GLYPH_PADDING_P (*glyph))
14710 /* A wide character is unbreakable. */
14711 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14712 it->current_x = x_before;
14714 else
14716 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14717 it->current_x = x;
14719 break;
14721 else if (x + glyph->pixel_width > it->first_visible_x)
14723 /* Glyph is at least partially visible. */
14724 ++it->hpos;
14725 if (x < it->first_visible_x)
14726 it->glyph_row->x = x - it->first_visible_x;
14728 else
14730 /* Glyph is off the left margin of the display area.
14731 Should not happen. */
14732 abort ();
14735 row->ascent = max (row->ascent, it->max_ascent);
14736 row->height = max (row->height, it->max_ascent + it->max_descent);
14737 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14738 row->phys_height = max (row->phys_height,
14739 it->max_phys_ascent + it->max_phys_descent);
14740 x += glyph->pixel_width;
14741 ++i;
14744 /* Stop if max_x reached. */
14745 if (i < nglyphs)
14746 break;
14748 /* Stop at line ends. */
14749 if (ITERATOR_AT_END_OF_LINE_P (it))
14751 it->continuation_lines_width = 0;
14752 break;
14755 set_iterator_to_next (it, 1);
14757 /* Stop if truncating at the right edge. */
14758 if (it->truncate_lines_p
14759 && it->current_x >= it->last_visible_x)
14761 /* Add truncation mark, but don't do it if the line is
14762 truncated at a padding space. */
14763 if (IT_CHARPOS (*it) < it->string_nchars)
14765 if (!FRAME_WINDOW_P (it->f))
14767 int i, n;
14769 if (it->current_x > it->last_visible_x)
14771 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14772 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14773 break;
14774 for (n = row->used[TEXT_AREA]; i < n; ++i)
14776 row->used[TEXT_AREA] = i;
14777 produce_special_glyphs (it, IT_TRUNCATION);
14780 produce_special_glyphs (it, IT_TRUNCATION);
14782 it->glyph_row->truncated_on_right_p = 1;
14784 break;
14788 /* Maybe insert a truncation at the left. */
14789 if (it->first_visible_x
14790 && IT_CHARPOS (*it) > 0)
14792 if (!FRAME_WINDOW_P (it->f))
14793 insert_left_trunc_glyphs (it);
14794 it->glyph_row->truncated_on_left_p = 1;
14797 it->face_id = saved_face_id;
14799 /* Value is number of columns displayed. */
14800 return it->hpos - hpos_at_start;
14805 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14806 appears as an element of LIST or as the car of an element of LIST.
14807 If PROPVAL is a list, compare each element against LIST in that
14808 way, and return 1/2 if any element of PROPVAL is found in LIST.
14809 Otherwise return 0. This function cannot quit.
14810 The return value is 2 if the text is invisible but with an ellipsis
14811 and 1 if it's invisible and without an ellipsis. */
14814 invisible_p (propval, list)
14815 register Lisp_Object propval;
14816 Lisp_Object list;
14818 register Lisp_Object tail, proptail;
14820 for (tail = list; CONSP (tail); tail = XCDR (tail))
14822 register Lisp_Object tem;
14823 tem = XCAR (tail);
14824 if (EQ (propval, tem))
14825 return 1;
14826 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14827 return NILP (XCDR (tem)) ? 1 : 2;
14830 if (CONSP (propval))
14832 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14834 Lisp_Object propelt;
14835 propelt = XCAR (proptail);
14836 for (tail = list; CONSP (tail); tail = XCDR (tail))
14838 register Lisp_Object tem;
14839 tem = XCAR (tail);
14840 if (EQ (propelt, tem))
14841 return 1;
14842 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14843 return NILP (XCDR (tem)) ? 1 : 2;
14848 return 0;
14852 /***********************************************************************
14853 Initialization
14854 ***********************************************************************/
14856 void
14857 syms_of_xdisp ()
14859 Vwith_echo_area_save_vector = Qnil;
14860 staticpro (&Vwith_echo_area_save_vector);
14862 Vmessage_stack = Qnil;
14863 staticpro (&Vmessage_stack);
14865 Qinhibit_redisplay = intern ("inhibit-redisplay");
14866 staticpro (&Qinhibit_redisplay);
14868 message_dolog_marker1 = Fmake_marker ();
14869 staticpro (&message_dolog_marker1);
14870 message_dolog_marker2 = Fmake_marker ();
14871 staticpro (&message_dolog_marker2);
14872 message_dolog_marker3 = Fmake_marker ();
14873 staticpro (&message_dolog_marker3);
14875 #if GLYPH_DEBUG
14876 defsubr (&Sdump_frame_glyph_matrix);
14877 defsubr (&Sdump_glyph_matrix);
14878 defsubr (&Sdump_glyph_row);
14879 defsubr (&Sdump_tool_bar_row);
14880 defsubr (&Strace_redisplay);
14881 defsubr (&Strace_to_stderr);
14882 #endif
14883 #ifdef HAVE_WINDOW_SYSTEM
14884 defsubr (&Stool_bar_lines_needed);
14885 #endif
14887 staticpro (&Qmenu_bar_update_hook);
14888 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14890 staticpro (&Qoverriding_terminal_local_map);
14891 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14893 staticpro (&Qoverriding_local_map);
14894 Qoverriding_local_map = intern ("overriding-local-map");
14896 staticpro (&Qwindow_scroll_functions);
14897 Qwindow_scroll_functions = intern ("window-scroll-functions");
14899 staticpro (&Qredisplay_end_trigger_functions);
14900 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14902 staticpro (&Qinhibit_point_motion_hooks);
14903 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14905 QCdata = intern (":data");
14906 staticpro (&QCdata);
14907 Qdisplay = intern ("display");
14908 staticpro (&Qdisplay);
14909 Qspace_width = intern ("space-width");
14910 staticpro (&Qspace_width);
14911 Qraise = intern ("raise");
14912 staticpro (&Qraise);
14913 Qspace = intern ("space");
14914 staticpro (&Qspace);
14915 Qmargin = intern ("margin");
14916 staticpro (&Qmargin);
14917 Qleft_margin = intern ("left-margin");
14918 staticpro (&Qleft_margin);
14919 Qright_margin = intern ("right-margin");
14920 staticpro (&Qright_margin);
14921 Qalign_to = intern ("align-to");
14922 staticpro (&Qalign_to);
14923 QCalign_to = intern (":align-to");
14924 staticpro (&QCalign_to);
14925 Qrelative_width = intern ("relative-width");
14926 staticpro (&Qrelative_width);
14927 QCrelative_width = intern (":relative-width");
14928 staticpro (&QCrelative_width);
14929 QCrelative_height = intern (":relative-height");
14930 staticpro (&QCrelative_height);
14931 QCeval = intern (":eval");
14932 staticpro (&QCeval);
14933 QCpropertize = intern (":propertize");
14934 staticpro (&QCpropertize);
14935 Qwhen = intern ("when");
14936 staticpro (&Qwhen);
14937 QCfile = intern (":file");
14938 staticpro (&QCfile);
14939 Qfontified = intern ("fontified");
14940 staticpro (&Qfontified);
14941 Qfontification_functions = intern ("fontification-functions");
14942 staticpro (&Qfontification_functions);
14943 Qtrailing_whitespace = intern ("trailing-whitespace");
14944 staticpro (&Qtrailing_whitespace);
14945 Qimage = intern ("image");
14946 staticpro (&Qimage);
14947 Qmessage_truncate_lines = intern ("message-truncate-lines");
14948 staticpro (&Qmessage_truncate_lines);
14949 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14950 staticpro (&Qcursor_in_non_selected_windows);
14951 Qgrow_only = intern ("grow-only");
14952 staticpro (&Qgrow_only);
14953 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14954 staticpro (&Qinhibit_menubar_update);
14955 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14956 staticpro (&Qinhibit_eval_during_redisplay);
14957 Qposition = intern ("position");
14958 staticpro (&Qposition);
14959 Qbuffer_position = intern ("buffer-position");
14960 staticpro (&Qbuffer_position);
14961 Qobject = intern ("object");
14962 staticpro (&Qobject);
14963 Qrisky_local_variable = intern ("risky-local-variable");
14964 staticpro (&Qrisky_local_variable);
14966 list_of_error = Fcons (intern ("error"), Qnil);
14967 staticpro (&list_of_error);
14969 last_arrow_position = Qnil;
14970 last_arrow_string = Qnil;
14971 staticpro (&last_arrow_position);
14972 staticpro (&last_arrow_string);
14974 echo_buffer[0] = echo_buffer[1] = Qnil;
14975 staticpro (&echo_buffer[0]);
14976 staticpro (&echo_buffer[1]);
14978 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14979 staticpro (&echo_area_buffer[0]);
14980 staticpro (&echo_area_buffer[1]);
14982 Vmessages_buffer_name = build_string ("*Messages*");
14983 staticpro (&Vmessages_buffer_name);
14985 mode_line_proptrans_alist = Qnil;
14986 staticpro (&mode_line_proptrans_alist);
14988 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14989 doc: /* Non-nil means highlight trailing whitespace.
14990 The face used for trailing whitespace is `trailing-whitespace'. */);
14991 Vshow_trailing_whitespace = Qnil;
14993 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14994 doc: /* Non-nil means don't actually do any redisplay.
14995 This is used for internal purposes. */);
14996 Vinhibit_redisplay = Qnil;
14998 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14999 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15000 Vglobal_mode_string = Qnil;
15002 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15003 doc: /* Marker for where to display an arrow on top of the buffer text.
15004 This must be the beginning of a line in order to work.
15005 See also `overlay-arrow-string'. */);
15006 Voverlay_arrow_position = Qnil;
15008 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15009 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15010 Voverlay_arrow_string = Qnil;
15012 DEFVAR_INT ("scroll-step", &scroll_step,
15013 doc: /* *The number of lines to try scrolling a window by when point moves out.
15014 If that fails to bring point back on frame, point is centered instead.
15015 If this is zero, point is always centered after it moves off frame.
15016 If you want scrolling to always be a line at a time, you should set
15017 `scroll-conservatively' to a large value rather than set this to 1. */);
15019 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15020 doc: /* *Scroll up to this many lines, to bring point back on screen.
15021 A value of zero means to scroll the text to center point vertically
15022 in the window. */);
15023 scroll_conservatively = 0;
15025 DEFVAR_INT ("scroll-margin", &scroll_margin,
15026 doc: /* *Number of lines of margin at the top and bottom of a window.
15027 Recenter the window whenever point gets within this many lines
15028 of the top or bottom of the window. */);
15029 scroll_margin = 0;
15031 #if GLYPH_DEBUG
15032 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15033 #endif
15035 DEFVAR_BOOL ("truncate-partial-width-windows",
15036 &truncate_partial_width_windows,
15037 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15038 truncate_partial_width_windows = 1;
15040 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15041 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15042 Any other value means to use the appropriate face, `mode-line',
15043 `header-line', or `menu' respectively.
15045 This variable is deprecated; please change the above faces instead. */);
15046 mode_line_inverse_video = 1;
15048 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15049 doc: /* *Maximum buffer size for which line number should be displayed.
15050 If the buffer is bigger than this, the line number does not appear
15051 in the mode line. A value of nil means no limit. */);
15052 Vline_number_display_limit = Qnil;
15054 DEFVAR_INT ("line-number-display-limit-width",
15055 &line_number_display_limit_width,
15056 doc: /* *Maximum line width (in characters) for line number display.
15057 If the average length of the lines near point is bigger than this, then the
15058 line number may be omitted from the mode line. */);
15059 line_number_display_limit_width = 200;
15061 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15062 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15063 highlight_nonselected_windows = 0;
15065 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15066 doc: /* Non-nil if more than one frame is visible on this display.
15067 Minibuffer-only frames don't count, but iconified frames do.
15068 This variable is not guaranteed to be accurate except while processing
15069 `frame-title-format' and `icon-title-format'. */);
15071 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15072 doc: /* Template for displaying the title bar of visible frames.
15073 \(Assuming the window manager supports this feature.)
15074 This variable has the same structure as `mode-line-format' (which see),
15075 and is used only on frames for which no explicit name has been set
15076 \(see `modify-frame-parameters'). */);
15077 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15078 doc: /* Template for displaying the title bar of an iconified frame.
15079 \(Assuming the window manager supports this feature.)
15080 This variable has the same structure as `mode-line-format' (which see),
15081 and is used only on frames for which no explicit name has been set
15082 \(see `modify-frame-parameters'). */);
15083 Vicon_title_format
15084 = Vframe_title_format
15085 = Fcons (intern ("multiple-frames"),
15086 Fcons (build_string ("%b"),
15087 Fcons (Fcons (empty_string,
15088 Fcons (intern ("invocation-name"),
15089 Fcons (build_string ("@"),
15090 Fcons (intern ("system-name"),
15091 Qnil)))),
15092 Qnil)));
15094 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15095 doc: /* Maximum number of lines to keep in the message log buffer.
15096 If nil, disable message logging. If t, log messages but don't truncate
15097 the buffer when it becomes large. */);
15098 Vmessage_log_max = make_number (50);
15100 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15101 doc: /* Functions called before redisplay, if window sizes have changed.
15102 The value should be a list of functions that take one argument.
15103 Just before redisplay, for each frame, if any of its windows have changed
15104 size since the last redisplay, or have been split or deleted,
15105 all the functions in the list are called, with the frame as argument. */);
15106 Vwindow_size_change_functions = Qnil;
15108 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15109 doc: /* List of Functions to call before redisplaying a window with scrolling.
15110 Each function is called with two arguments, the window
15111 and its new display-start position. Note that the value of `window-end'
15112 is not valid when these functions are called. */);
15113 Vwindow_scroll_functions = Qnil;
15115 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15116 doc: /* *Non-nil means automatically resize tool-bars.
15117 This increases a tool-bar's height if not all tool-bar items are visible.
15118 It decreases a tool-bar's height when it would display blank lines
15119 otherwise. */);
15120 auto_resize_tool_bars_p = 1;
15122 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15123 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15124 auto_raise_tool_bar_buttons_p = 1;
15126 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15127 doc: /* *Margin around tool-bar buttons in pixels.
15128 If an integer, use that for both horizontal and vertical margins.
15129 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15130 HORZ specifying the horizontal margin, and VERT specifying the
15131 vertical margin. */);
15132 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15134 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15135 doc: /* *Relief thickness of tool-bar buttons. */);
15136 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15138 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15139 doc: /* List of functions to call to fontify regions of text.
15140 Each function is called with one argument POS. Functions must
15141 fontify a region starting at POS in the current buffer, and give
15142 fontified regions the property `fontified'. */);
15143 Vfontification_functions = Qnil;
15144 Fmake_variable_buffer_local (Qfontification_functions);
15146 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15147 &unibyte_display_via_language_environment,
15148 doc: /* *Non-nil means display unibyte text according to language environment.
15149 Specifically this means that unibyte non-ASCII characters
15150 are displayed by converting them to the equivalent multibyte characters
15151 according to the current language environment. As a result, they are
15152 displayed according to the current fontset. */);
15153 unibyte_display_via_language_environment = 0;
15155 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15156 doc: /* *Maximum height for resizing mini-windows.
15157 If a float, it specifies a fraction of the mini-window frame's height.
15158 If an integer, it specifies a number of lines. */);
15159 Vmax_mini_window_height = make_float (0.25);
15161 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15162 doc: /* *How to resize mini-windows.
15163 A value of nil means don't automatically resize mini-windows.
15164 A value of t means resize them to fit the text displayed in them.
15165 A value of `grow-only', the default, means let mini-windows grow
15166 only, until their display becomes empty, at which point the windows
15167 go back to their normal size. */);
15168 Vresize_mini_windows = Qgrow_only;
15170 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15171 &cursor_in_non_selected_windows,
15172 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15173 nil means don't display a cursor there. */);
15174 cursor_in_non_selected_windows = 1;
15176 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15177 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15178 automatic_hscrolling_p = 1;
15180 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15181 doc: /* *How many columns away from the window edge point is allowed to get
15182 before automatic hscrolling will horizontally scroll the window. */);
15183 hscroll_margin = 5;
15185 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15186 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15187 When point is less than `automatic-hscroll-margin' columns from the window
15188 edge, automatic hscrolling will scroll the window by the amount of columns
15189 determined by this variable. If its value is a positive integer, scroll that
15190 many columns. If it's a positive floating-point number, it specifies the
15191 fraction of the window's width to scroll. If it's nil or zero, point will be
15192 centered horizontally after the scroll. Any other value, including negative
15193 numbers, are treated as if the value were zero.
15195 Automatic hscrolling always moves point outside the scroll margin, so if
15196 point was more than scroll step columns inside the margin, the window will
15197 scroll more than the value given by the scroll step.
15199 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15200 and `scroll-right' overrides this variable's effect. */);
15201 Vhscroll_step = make_number (0);
15203 DEFVAR_LISP ("image-types", &Vimage_types,
15204 doc: /* List of supported image types.
15205 Each element of the list is a symbol for a supported image type. */);
15206 Vimage_types = Qnil;
15208 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15209 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15210 Bind this around calls to `message' to let it take effect. */);
15211 message_truncate_lines = 0;
15213 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15214 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15215 Can be used to update submenus whose contents should vary. */);
15216 Vmenu_bar_update_hook = Qnil;
15218 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15219 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15220 inhibit_menubar_update = 0;
15222 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15223 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15224 inhibit_eval_during_redisplay = 0;
15226 #if GLYPH_DEBUG
15227 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15228 doc: /* Inhibit try_window_id display optimization. */);
15229 inhibit_try_window_id = 0;
15231 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15232 doc: /* Inhibit try_window_reusing display optimization. */);
15233 inhibit_try_window_reusing = 0;
15235 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15236 doc: /* Inhibit try_cursor_movement display optimization. */);
15237 inhibit_try_cursor_movement = 0;
15238 #endif /* GLYPH_DEBUG */
15242 /* Initialize this module when Emacs starts. */
15244 void
15245 init_xdisp ()
15247 Lisp_Object root_window;
15248 struct window *mini_w;
15250 current_header_line_height = current_mode_line_height = -1;
15252 CHARPOS (this_line_start_pos) = 0;
15254 mini_w = XWINDOW (minibuf_window);
15255 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15257 if (!noninteractive)
15259 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15260 int i;
15262 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15263 set_window_height (root_window,
15264 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15266 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15267 set_window_height (minibuf_window, 1, 0);
15269 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15270 mini_w->width = make_number (FRAME_WIDTH (f));
15272 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15273 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15274 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15276 /* The default ellipsis glyphs `...'. */
15277 for (i = 0; i < 3; ++i)
15278 default_invis_vector[i] = make_number ('.');
15281 #ifdef HAVE_WINDOW_SYSTEM
15283 /* Allocate the buffer for frame titles. */
15284 int size = 100;
15285 frame_title_buf = (char *) xmalloc (size);
15286 frame_title_buf_end = frame_title_buf + size;
15287 frame_title_ptr = NULL;
15289 #endif /* HAVE_WINDOW_SYSTEM */
15291 help_echo_showing_p = 0;