(describe-function-1): Skip arglist note if
[emacs.git] / src / xdisp.c
blob783a1b9423f616dcce053b4e6de3343be7c38939
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 MAC_OS
198 #include "macterm.h"
199 #endif
201 #define INFINITY 10000000
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
204 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;
214 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
230 Lisp_Object Qrisky_local_variable;
232 /* Holds the list (error). */
233 Lisp_Object list_of_error;
235 /* Functions called to fontify regions of text. */
237 Lisp_Object Vfontification_functions;
238 Lisp_Object Qfontification_functions;
240 /* Non-zero means draw tool bar buttons raised when the mouse moves
241 over them. */
243 int auto_raise_tool_bar_buttons_p;
245 /* Margin around tool bar buttons in pixels. */
247 Lisp_Object Vtool_bar_button_margin;
249 /* Thickness of shadow to draw around tool bar buttons. */
251 EMACS_INT tool_bar_button_relief;
253 /* Non-zero means automatically resize tool-bars so that all tool-bar
254 items are visible, and no blank lines remain. */
256 int auto_resize_tool_bars_p;
258 /* Non-nil means don't actually do any redisplay. */
260 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
262 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
264 int inhibit_eval_during_redisplay;
266 /* Names of text properties relevant for redisplay. */
268 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
269 extern Lisp_Object Qface, Qinvisible, Qwidth;
271 /* Symbols used in text property values. */
273 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
274 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
275 Lisp_Object Qmargin;
276 extern Lisp_Object Qheight;
278 /* Non-nil means highlight trailing whitespace. */
280 Lisp_Object Vshow_trailing_whitespace;
282 /* Name of the face used to highlight trailing whitespace. */
284 Lisp_Object Qtrailing_whitespace;
286 /* The symbol `image' which is the car of the lists used to represent
287 images in Lisp. */
289 Lisp_Object Qimage;
291 /* Non-zero means print newline to stdout before next mini-buffer
292 message. */
294 int noninteractive_need_newline;
296 /* Non-zero means print newline to message log before next message. */
298 static int message_log_need_newline;
300 /* Three markers that message_dolog uses.
301 It could allocate them itself, but that causes trouble
302 in handling memory-full errors. */
303 static Lisp_Object message_dolog_marker1;
304 static Lisp_Object message_dolog_marker2;
305 static Lisp_Object message_dolog_marker3;
307 /* The buffer position of the first character appearing entirely or
308 partially on the line of the selected window which contains the
309 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
310 redisplay optimization in redisplay_internal. */
312 static struct text_pos this_line_start_pos;
314 /* Number of characters past the end of the line above, including the
315 terminating newline. */
317 static struct text_pos this_line_end_pos;
319 /* The vertical positions and the height of this line. */
321 static int this_line_vpos;
322 static int this_line_y;
323 static int this_line_pixel_height;
325 /* X position at which this display line starts. Usually zero;
326 negative if first character is partially visible. */
328 static int this_line_start_x;
330 /* Buffer that this_line_.* variables are referring to. */
332 static struct buffer *this_line_buffer;
334 /* Nonzero means truncate lines in all windows less wide than the
335 frame. */
337 int truncate_partial_width_windows;
339 /* A flag to control how to display unibyte 8-bit character. */
341 int unibyte_display_via_language_environment;
343 /* Nonzero means we have more than one non-mini-buffer-only frame.
344 Not guaranteed to be accurate except while parsing
345 frame-title-format. */
347 int multiple_frames;
349 Lisp_Object Vglobal_mode_string;
351 /* Marker for where to display an arrow on top of the buffer text. */
353 Lisp_Object Voverlay_arrow_position;
355 /* String to display for the arrow. Only used on terminal frames. */
357 Lisp_Object Voverlay_arrow_string;
359 /* Values of those variables at last redisplay. However, if
360 Voverlay_arrow_position is a marker, last_arrow_position is its
361 numerical position. */
363 static Lisp_Object last_arrow_position, last_arrow_string;
365 /* Like mode-line-format, but for the title bar on a visible frame. */
367 Lisp_Object Vframe_title_format;
369 /* Like mode-line-format, but for the title bar on an iconified frame. */
371 Lisp_Object Vicon_title_format;
373 /* List of functions to call when a window's size changes. These
374 functions get one arg, a frame on which one or more windows' sizes
375 have changed. */
377 static Lisp_Object Vwindow_size_change_functions;
379 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
381 /* Nonzero if overlay arrow has been displayed once in this window. */
383 static int overlay_arrow_seen;
385 /* Nonzero means highlight the region even in nonselected windows. */
387 int highlight_nonselected_windows;
389 /* If cursor motion alone moves point off frame, try scrolling this
390 many lines up or down if that will bring it back. */
392 static EMACS_INT scroll_step;
394 /* Nonzero means scroll just far enough to bring point back on the
395 screen, when appropriate. */
397 static EMACS_INT scroll_conservatively;
399 /* Recenter the window whenever point gets within this many lines of
400 the top or bottom of the window. This value is translated into a
401 pixel value by multiplying it with CANON_Y_UNIT, which means that
402 there is really a fixed pixel height scroll margin. */
404 EMACS_INT scroll_margin;
406 /* Number of windows showing the buffer of the selected window (or
407 another buffer with the same base buffer). keyboard.c refers to
408 this. */
410 int buffer_shared;
412 /* Vector containing glyphs for an ellipsis `...'. */
414 static Lisp_Object default_invis_vector[3];
416 /* Zero means display the mode-line/header-line/menu-bar in the default face
417 (this slightly odd definition is for compatibility with previous versions
418 of emacs), non-zero means display them using their respective faces.
420 This variable is deprecated. */
422 int mode_line_inverse_video;
424 /* Prompt to display in front of the mini-buffer contents. */
426 Lisp_Object minibuf_prompt;
428 /* Width of current mini-buffer prompt. Only set after display_line
429 of the line that contains the prompt. */
431 int minibuf_prompt_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
693 /* This counter is used to clear the face cache every once in a while
694 in redisplay_internal. It is incremented for each redisplay.
695 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
696 cleared. */
698 #define CLEAR_FACE_CACHE_COUNT 500
699 static int clear_face_cache_count;
701 /* Record the previous terminal frame we displayed. */
703 static struct frame *previous_terminal_frame;
705 /* Non-zero while redisplay_internal is in progress. */
707 int redisplaying_p;
709 /* Non-zero while redisplay is updating the display. */
711 int redisplay_updating_p;
714 /* Function prototypes. */
716 static void setup_for_ellipsis P_ ((struct it *));
717 static void mark_window_display_accurate_1 P_ ((struct window *, int));
718 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
719 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
720 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
721 static int redisplay_mode_lines P_ ((Lisp_Object, int));
722 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
724 #if 0
725 static int invisible_text_between_p P_ ((struct it *, int, int));
726 #endif
728 static int next_element_from_ellipsis P_ ((struct it *));
729 static void pint2str P_ ((char *, int, int));
730 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
731 struct text_pos));
732 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
733 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
734 static void store_frame_title_char P_ ((char));
735 static int store_frame_title P_ ((const unsigned char *, int, int));
736 static void x_consider_frame_title P_ ((Lisp_Object));
737 static void handle_stop P_ ((struct it *));
738 static int tool_bar_lines_needed P_ ((struct frame *));
739 static int single_display_prop_intangible_p P_ ((Lisp_Object));
740 static void ensure_echo_area_buffers P_ ((void));
741 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
742 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
743 static int with_echo_area_buffer P_ ((struct window *, int,
744 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
745 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
746 static void clear_garbaged_frames P_ ((void));
747 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
748 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
749 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
750 static int display_echo_area P_ ((struct window *));
751 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
752 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
753 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
754 static int string_char_and_length P_ ((const unsigned char *, int, int *));
755 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
756 struct text_pos));
757 static int compute_window_start_on_continuation_line P_ ((struct window *));
758 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
759 static void insert_left_trunc_glyphs P_ ((struct it *));
760 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
761 static void extend_face_to_end_of_line P_ ((struct it *));
762 static int append_space P_ ((struct it *, int));
763 static int make_cursor_line_fully_visible P_ ((struct window *));
764 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
765 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
766 static int trailing_whitespace_p P_ ((int));
767 static int message_log_check_duplicate P_ ((int, int, int, int));
768 static void push_it P_ ((struct it *));
769 static void pop_it P_ ((struct it *));
770 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
771 static void redisplay_internal P_ ((int));
772 static int echo_area_display P_ ((int));
773 static void redisplay_windows P_ ((Lisp_Object));
774 static void redisplay_window P_ ((Lisp_Object, int));
775 static Lisp_Object redisplay_window_error ();
776 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
777 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
778 static void update_menu_bar P_ ((struct frame *, int));
779 static int try_window_reusing_current_matrix P_ ((struct window *));
780 static int try_window_id P_ ((struct window *));
781 static int display_line P_ ((struct it *));
782 static int display_mode_lines P_ ((struct window *));
783 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
784 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
785 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
786 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
787 static void display_menu_bar P_ ((struct window *));
788 static int display_count_lines P_ ((int, int, int, int, int *));
789 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
790 int, int, struct it *, int, int, int, int));
791 static void compute_line_metrics P_ ((struct it *));
792 static void run_redisplay_end_trigger_hook P_ ((struct it *));
793 static int get_overlay_strings P_ ((struct it *, int));
794 static void next_overlay_string P_ ((struct it *));
795 static void reseat P_ ((struct it *, struct text_pos, int));
796 static void reseat_1 P_ ((struct it *, struct text_pos, int));
797 static void back_to_previous_visible_line_start P_ ((struct it *));
798 static void reseat_at_previous_visible_line_start P_ ((struct it *));
799 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
800 static int next_element_from_display_vector P_ ((struct it *));
801 static int next_element_from_string P_ ((struct it *));
802 static int next_element_from_c_string P_ ((struct it *));
803 static int next_element_from_buffer P_ ((struct it *));
804 static int next_element_from_composition P_ ((struct it *));
805 static int next_element_from_image P_ ((struct it *));
806 static int next_element_from_stretch P_ ((struct it *));
807 static void load_overlay_strings P_ ((struct it *, int));
808 static int init_from_display_pos P_ ((struct it *, struct window *,
809 struct display_pos *));
810 static void reseat_to_string P_ ((struct it *, unsigned char *,
811 Lisp_Object, int, int, int, int));
812 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
813 int, int, int));
814 void move_it_vertically_backward P_ ((struct it *, int));
815 static void init_to_row_start P_ ((struct it *, struct window *,
816 struct glyph_row *));
817 static int init_to_row_end P_ ((struct it *, struct window *,
818 struct glyph_row *));
819 static void back_to_previous_line_start P_ ((struct it *));
820 static int forward_to_next_line_start P_ ((struct it *, int *));
821 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
822 Lisp_Object, int));
823 static struct text_pos string_pos P_ ((int, Lisp_Object));
824 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
825 static int number_of_chars P_ ((unsigned char *, int));
826 static void compute_stop_pos P_ ((struct it *));
827 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
828 Lisp_Object));
829 static int face_before_or_after_it_pos P_ ((struct it *, int));
830 static int next_overlay_change P_ ((int));
831 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
832 Lisp_Object, struct text_pos *,
833 int));
834 static int underlying_face_id P_ ((struct it *));
835 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
836 struct window *));
838 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
839 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
841 #ifdef HAVE_WINDOW_SYSTEM
843 static void update_tool_bar P_ ((struct frame *, int));
844 static void build_desired_tool_bar_string P_ ((struct frame *f));
845 static int redisplay_tool_bar P_ ((struct frame *));
846 static void display_tool_bar_line P_ ((struct it *));
848 #endif /* HAVE_WINDOW_SYSTEM */
851 /***********************************************************************
852 Window display dimensions
853 ***********************************************************************/
855 /* Return the window-relative maximum y + 1 for glyph rows displaying
856 text in window W. This is the height of W minus the height of a
857 mode line, if any. */
859 INLINE int
860 window_text_bottom_y (w)
861 struct window *w;
863 struct frame *f = XFRAME (w->frame);
864 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
866 if (WINDOW_WANTS_MODELINE_P (w))
867 height -= CURRENT_MODE_LINE_HEIGHT (w);
868 return height;
872 /* Return the pixel width of display area AREA of window W. AREA < 0
873 means return the total width of W, not including fringes to
874 the left and right of the window. */
876 INLINE int
877 window_box_width (w, area)
878 struct window *w;
879 int area;
881 struct frame *f = XFRAME (w->frame);
882 int width = XFASTINT (w->width);
884 if (!w->pseudo_window_p)
886 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
888 if (area == TEXT_AREA)
890 if (INTEGERP (w->left_margin_width))
891 width -= XFASTINT (w->left_margin_width);
892 if (INTEGERP (w->right_margin_width))
893 width -= XFASTINT (w->right_margin_width);
895 else if (area == LEFT_MARGIN_AREA)
896 width = (INTEGERP (w->left_margin_width)
897 ? XFASTINT (w->left_margin_width) : 0);
898 else if (area == RIGHT_MARGIN_AREA)
899 width = (INTEGERP (w->right_margin_width)
900 ? XFASTINT (w->right_margin_width) : 0);
903 return width * CANON_X_UNIT (f);
907 /* Return the pixel height of the display area of window W, not
908 including mode lines of W, if any. */
910 INLINE int
911 window_box_height (w)
912 struct window *w;
914 struct frame *f = XFRAME (w->frame);
915 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
917 xassert (height >= 0);
919 /* Note: the code below that determines the mode-line/header-line
920 height is essentially the same as that contained in the macro
921 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
922 the appropriate glyph row has its `mode_line_p' flag set,
923 and if it doesn't, uses estimate_mode_line_height instead. */
925 if (WINDOW_WANTS_MODELINE_P (w))
927 struct glyph_row *ml_row
928 = (w->current_matrix && w->current_matrix->rows
929 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
930 : 0);
931 if (ml_row && ml_row->mode_line_p)
932 height -= ml_row->height;
933 else
934 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
937 if (WINDOW_WANTS_HEADER_LINE_P (w))
939 struct glyph_row *hl_row
940 = (w->current_matrix && w->current_matrix->rows
941 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
942 : 0);
943 if (hl_row && hl_row->mode_line_p)
944 height -= hl_row->height;
945 else
946 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
949 /* With a very small font and a mode-line that's taller than
950 default, we might end up with a negative height. */
951 return max (0, height);
955 /* Return the frame-relative coordinate of the left edge of display
956 area AREA of window W. AREA < 0 means return the left edge of the
957 whole window, to the right of the left fringe of W. */
959 INLINE int
960 window_box_left (w, area)
961 struct window *w;
962 int area;
964 struct frame *f = XFRAME (w->frame);
965 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
967 if (!w->pseudo_window_p)
969 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
970 + FRAME_LEFT_FRINGE_WIDTH (f));
972 if (area == TEXT_AREA)
973 x += window_box_width (w, LEFT_MARGIN_AREA);
974 else if (area == RIGHT_MARGIN_AREA)
975 x += (window_box_width (w, LEFT_MARGIN_AREA)
976 + window_box_width (w, TEXT_AREA));
979 return x;
983 /* Return the frame-relative coordinate of the right edge of display
984 area AREA of window W. AREA < 0 means return the left edge of the
985 whole window, to the left of the right fringe of W. */
987 INLINE int
988 window_box_right (w, area)
989 struct window *w;
990 int area;
992 return window_box_left (w, area) + window_box_width (w, area);
996 /* Get the bounding box of the display area AREA of window W, without
997 mode lines, in frame-relative coordinates. AREA < 0 means the
998 whole window, not including the left and right fringes of
999 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1000 coordinates of the upper-left corner of the box. Return in
1001 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1003 INLINE void
1004 window_box (w, area, box_x, box_y, box_width, box_height)
1005 struct window *w;
1006 int area;
1007 int *box_x, *box_y, *box_width, *box_height;
1009 struct frame *f = XFRAME (w->frame);
1011 *box_width = window_box_width (w, area);
1012 *box_height = window_box_height (w);
1013 *box_x = window_box_left (w, area);
1014 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1015 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1016 if (WINDOW_WANTS_HEADER_LINE_P (w))
1017 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1021 /* Get the bounding box of the display area AREA of window W, without
1022 mode lines. AREA < 0 means the whole window, not including the
1023 left and right fringe of the window. Return in *TOP_LEFT_X
1024 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1025 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1026 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1027 box. */
1029 INLINE void
1030 window_box_edges (w, area, top_left_x, top_left_y,
1031 bottom_right_x, bottom_right_y)
1032 struct window *w;
1033 int area;
1034 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1036 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1037 bottom_right_y);
1038 *bottom_right_x += *top_left_x;
1039 *bottom_right_y += *top_left_y;
1044 /***********************************************************************
1045 Utilities
1046 ***********************************************************************/
1048 /* Return the bottom y-position of the line the iterator IT is in.
1049 This can modify IT's settings. */
1052 line_bottom_y (it)
1053 struct it *it;
1055 int line_height = it->max_ascent + it->max_descent;
1056 int line_top_y = it->current_y;
1058 if (line_height == 0)
1060 if (last_height)
1061 line_height = last_height;
1062 else if (IT_CHARPOS (*it) < ZV)
1064 move_it_by_lines (it, 1, 1);
1065 line_height = (it->max_ascent || it->max_descent
1066 ? it->max_ascent + it->max_descent
1067 : last_height);
1069 else
1071 struct glyph_row *row = it->glyph_row;
1073 /* Use the default character height. */
1074 it->glyph_row = NULL;
1075 it->what = IT_CHARACTER;
1076 it->c = ' ';
1077 it->len = 1;
1078 PRODUCE_GLYPHS (it);
1079 line_height = it->ascent + it->descent;
1080 it->glyph_row = row;
1084 return line_top_y + line_height;
1088 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1089 1 if POS is visible and the line containing POS is fully visible.
1090 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1091 and header-lines heights. */
1094 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1095 struct window *w;
1096 int charpos, *fully, exact_mode_line_heights_p;
1098 struct it it;
1099 struct text_pos top;
1100 int visible_p;
1101 struct buffer *old_buffer = NULL;
1103 if (XBUFFER (w->buffer) != current_buffer)
1105 old_buffer = current_buffer;
1106 set_buffer_internal_1 (XBUFFER (w->buffer));
1109 *fully = visible_p = 0;
1110 SET_TEXT_POS_FROM_MARKER (top, w->start);
1112 /* Compute exact mode line heights, if requested. */
1113 if (exact_mode_line_heights_p)
1115 if (WINDOW_WANTS_MODELINE_P (w))
1116 current_mode_line_height
1117 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1118 current_buffer->mode_line_format);
1120 if (WINDOW_WANTS_HEADER_LINE_P (w))
1121 current_header_line_height
1122 = display_mode_line (w, HEADER_LINE_FACE_ID,
1123 current_buffer->header_line_format);
1126 start_display (&it, w, top);
1127 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1128 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1130 /* Note that we may overshoot because of invisible text. */
1131 if (IT_CHARPOS (it) >= charpos)
1133 int top_y = it.current_y;
1134 int bottom_y = line_bottom_y (&it);
1135 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1137 if (top_y < window_top_y)
1138 visible_p = bottom_y > window_top_y;
1139 else if (top_y < it.last_visible_y)
1141 visible_p = 1;
1142 *fully = bottom_y <= it.last_visible_y;
1145 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1147 move_it_by_lines (&it, 1, 0);
1148 if (charpos < IT_CHARPOS (it))
1150 visible_p = 1;
1151 *fully = 0;
1155 if (old_buffer)
1156 set_buffer_internal_1 (old_buffer);
1158 current_header_line_height = current_mode_line_height = -1;
1159 return visible_p;
1163 /* Return the next character from STR which is MAXLEN bytes long.
1164 Return in *LEN the length of the character. This is like
1165 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1166 we find one, we return a `?', but with the length of the invalid
1167 character. */
1169 static INLINE int
1170 string_char_and_length (str, maxlen, len)
1171 const unsigned char *str;
1172 int maxlen, *len;
1174 int c;
1176 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1177 if (!CHAR_VALID_P (c, 1))
1178 /* We may not change the length here because other places in Emacs
1179 don't use this function, i.e. they silently accept invalid
1180 characters. */
1181 c = '?';
1183 return c;
1188 /* Given a position POS containing a valid character and byte position
1189 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1191 static struct text_pos
1192 string_pos_nchars_ahead (pos, string, nchars)
1193 struct text_pos pos;
1194 Lisp_Object string;
1195 int nchars;
1197 xassert (STRINGP (string) && nchars >= 0);
1199 if (STRING_MULTIBYTE (string))
1201 int rest = SBYTES (string) - BYTEPOS (pos);
1202 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1203 int len;
1205 while (nchars--)
1207 string_char_and_length (p, rest, &len);
1208 p += len, rest -= len;
1209 xassert (rest >= 0);
1210 CHARPOS (pos) += 1;
1211 BYTEPOS (pos) += len;
1214 else
1215 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1217 return pos;
1221 /* Value is the text position, i.e. character and byte position,
1222 for character position CHARPOS in STRING. */
1224 static INLINE struct text_pos
1225 string_pos (charpos, string)
1226 int charpos;
1227 Lisp_Object string;
1229 struct text_pos pos;
1230 xassert (STRINGP (string));
1231 xassert (charpos >= 0);
1232 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1233 return pos;
1237 /* Value is a text position, i.e. character and byte position, for
1238 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1239 means recognize multibyte characters. */
1241 static struct text_pos
1242 c_string_pos (charpos, s, multibyte_p)
1243 int charpos;
1244 unsigned char *s;
1245 int multibyte_p;
1247 struct text_pos pos;
1249 xassert (s != NULL);
1250 xassert (charpos >= 0);
1252 if (multibyte_p)
1254 int rest = strlen (s), len;
1256 SET_TEXT_POS (pos, 0, 0);
1257 while (charpos--)
1259 string_char_and_length (s, rest, &len);
1260 s += len, rest -= len;
1261 xassert (rest >= 0);
1262 CHARPOS (pos) += 1;
1263 BYTEPOS (pos) += len;
1266 else
1267 SET_TEXT_POS (pos, charpos, charpos);
1269 return pos;
1273 /* Value is the number of characters in C string S. MULTIBYTE_P
1274 non-zero means recognize multibyte characters. */
1276 static int
1277 number_of_chars (s, multibyte_p)
1278 unsigned char *s;
1279 int multibyte_p;
1281 int nchars;
1283 if (multibyte_p)
1285 int rest = strlen (s), len;
1286 unsigned char *p = (unsigned char *) s;
1288 for (nchars = 0; rest > 0; ++nchars)
1290 string_char_and_length (p, rest, &len);
1291 rest -= len, p += len;
1294 else
1295 nchars = strlen (s);
1297 return nchars;
1301 /* Compute byte position NEWPOS->bytepos corresponding to
1302 NEWPOS->charpos. POS is a known position in string STRING.
1303 NEWPOS->charpos must be >= POS.charpos. */
1305 static void
1306 compute_string_pos (newpos, pos, string)
1307 struct text_pos *newpos, pos;
1308 Lisp_Object string;
1310 xassert (STRINGP (string));
1311 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1313 if (STRING_MULTIBYTE (string))
1314 *newpos = string_pos_nchars_ahead (pos, string,
1315 CHARPOS (*newpos) - CHARPOS (pos));
1316 else
1317 BYTEPOS (*newpos) = CHARPOS (*newpos);
1322 /***********************************************************************
1323 Lisp form evaluation
1324 ***********************************************************************/
1326 /* Error handler for safe_eval and safe_call. */
1328 static Lisp_Object
1329 safe_eval_handler (arg)
1330 Lisp_Object arg;
1332 add_to_log ("Error during redisplay: %s", arg, Qnil);
1333 return Qnil;
1337 /* Evaluate SEXPR and return the result, or nil if something went
1338 wrong. Prevent redisplay during the evaluation. */
1340 Lisp_Object
1341 safe_eval (sexpr)
1342 Lisp_Object sexpr;
1344 Lisp_Object val;
1346 if (inhibit_eval_during_redisplay)
1347 val = Qnil;
1348 else
1350 int count = SPECPDL_INDEX ();
1351 struct gcpro gcpro1;
1353 GCPRO1 (sexpr);
1354 specbind (Qinhibit_redisplay, Qt);
1355 /* Use Qt to ensure debugger does not run,
1356 so there is no possibility of wanting to redisplay. */
1357 val = internal_condition_case_1 (Feval, sexpr, Qt,
1358 safe_eval_handler);
1359 UNGCPRO;
1360 val = unbind_to (count, val);
1363 return val;
1367 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1368 Return the result, or nil if something went wrong. Prevent
1369 redisplay during the evaluation. */
1371 Lisp_Object
1372 safe_call (nargs, args)
1373 int nargs;
1374 Lisp_Object *args;
1376 Lisp_Object val;
1378 if (inhibit_eval_during_redisplay)
1379 val = Qnil;
1380 else
1382 int count = SPECPDL_INDEX ();
1383 struct gcpro gcpro1;
1385 GCPRO1 (args[0]);
1386 gcpro1.nvars = nargs;
1387 specbind (Qinhibit_redisplay, Qt);
1388 /* Use Qt to ensure debugger does not run,
1389 so there is no possibility of wanting to redisplay. */
1390 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1391 safe_eval_handler);
1392 UNGCPRO;
1393 val = unbind_to (count, val);
1396 return val;
1400 /* Call function FN with one argument ARG.
1401 Return the result, or nil if something went wrong. */
1403 Lisp_Object
1404 safe_call1 (fn, arg)
1405 Lisp_Object fn, arg;
1407 Lisp_Object args[2];
1408 args[0] = fn;
1409 args[1] = arg;
1410 return safe_call (2, args);
1415 /***********************************************************************
1416 Debugging
1417 ***********************************************************************/
1419 #if 0
1421 /* Define CHECK_IT to perform sanity checks on iterators.
1422 This is for debugging. It is too slow to do unconditionally. */
1424 static void
1425 check_it (it)
1426 struct it *it;
1428 if (it->method == next_element_from_string)
1430 xassert (STRINGP (it->string));
1431 xassert (IT_STRING_CHARPOS (*it) >= 0);
1433 else if (it->method == next_element_from_buffer)
1435 /* Check that character and byte positions agree. */
1436 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1439 if (it->dpvec)
1440 xassert (it->current.dpvec_index >= 0);
1441 else
1442 xassert (it->current.dpvec_index < 0);
1445 #define CHECK_IT(IT) check_it ((IT))
1447 #else /* not 0 */
1449 #define CHECK_IT(IT) (void) 0
1451 #endif /* not 0 */
1454 #if GLYPH_DEBUG
1456 /* Check that the window end of window W is what we expect it
1457 to be---the last row in the current matrix displaying text. */
1459 static void
1460 check_window_end (w)
1461 struct window *w;
1463 if (!MINI_WINDOW_P (w)
1464 && !NILP (w->window_end_valid))
1466 struct glyph_row *row;
1467 xassert ((row = MATRIX_ROW (w->current_matrix,
1468 XFASTINT (w->window_end_vpos)),
1469 !row->enabled_p
1470 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1471 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1475 #define CHECK_WINDOW_END(W) check_window_end ((W))
1477 #else /* not GLYPH_DEBUG */
1479 #define CHECK_WINDOW_END(W) (void) 0
1481 #endif /* not GLYPH_DEBUG */
1485 /***********************************************************************
1486 Iterator initialization
1487 ***********************************************************************/
1489 /* Initialize IT for displaying current_buffer in window W, starting
1490 at character position CHARPOS. CHARPOS < 0 means that no buffer
1491 position is specified which is useful when the iterator is assigned
1492 a position later. BYTEPOS is the byte position corresponding to
1493 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1495 If ROW is not null, calls to produce_glyphs with IT as parameter
1496 will produce glyphs in that row.
1498 BASE_FACE_ID is the id of a base face to use. It must be one of
1499 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1500 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1501 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1503 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1504 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1505 will be initialized to use the corresponding mode line glyph row of
1506 the desired matrix of W. */
1508 void
1509 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1510 struct it *it;
1511 struct window *w;
1512 int charpos, bytepos;
1513 struct glyph_row *row;
1514 enum face_id base_face_id;
1516 int highlight_region_p;
1518 /* Some precondition checks. */
1519 xassert (w != NULL && it != NULL);
1520 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1521 && charpos <= ZV));
1523 /* If face attributes have been changed since the last redisplay,
1524 free realized faces now because they depend on face definitions
1525 that might have changed. Don't free faces while there might be
1526 desired matrices pending which reference these faces. */
1527 if (face_change_count && !redisplay_updating_p)
1529 face_change_count = 0;
1530 free_all_realized_faces (Qnil);
1533 /* Use one of the mode line rows of W's desired matrix if
1534 appropriate. */
1535 if (row == NULL)
1537 if (base_face_id == MODE_LINE_FACE_ID
1538 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1539 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1540 else if (base_face_id == HEADER_LINE_FACE_ID)
1541 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1544 /* Clear IT. */
1545 bzero (it, sizeof *it);
1546 it->current.overlay_string_index = -1;
1547 it->current.dpvec_index = -1;
1548 it->base_face_id = base_face_id;
1550 /* The window in which we iterate over current_buffer: */
1551 XSETWINDOW (it->window, w);
1552 it->w = w;
1553 it->f = XFRAME (w->frame);
1555 /* Extra space between lines (on window systems only). */
1556 if (base_face_id == DEFAULT_FACE_ID
1557 && FRAME_WINDOW_P (it->f))
1559 if (NATNUMP (current_buffer->extra_line_spacing))
1560 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1561 else if (it->f->extra_line_spacing > 0)
1562 it->extra_line_spacing = it->f->extra_line_spacing;
1565 /* If realized faces have been removed, e.g. because of face
1566 attribute changes of named faces, recompute them. When running
1567 in batch mode, the face cache of Vterminal_frame is null. If
1568 we happen to get called, make a dummy face cache. */
1569 if (
1570 #ifndef WINDOWSNT
1571 noninteractive &&
1572 #endif
1573 FRAME_FACE_CACHE (it->f) == NULL)
1574 init_frame_faces (it->f);
1575 if (FRAME_FACE_CACHE (it->f)->used == 0)
1576 recompute_basic_faces (it->f);
1578 /* Current value of the `space-width', and 'height' properties. */
1579 it->space_width = Qnil;
1580 it->font_height = Qnil;
1582 /* Are control characters displayed as `^C'? */
1583 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1585 /* -1 means everything between a CR and the following line end
1586 is invisible. >0 means lines indented more than this value are
1587 invisible. */
1588 it->selective = (INTEGERP (current_buffer->selective_display)
1589 ? XFASTINT (current_buffer->selective_display)
1590 : (!NILP (current_buffer->selective_display)
1591 ? -1 : 0));
1592 it->selective_display_ellipsis_p
1593 = !NILP (current_buffer->selective_display_ellipses);
1595 /* Display table to use. */
1596 it->dp = window_display_table (w);
1598 /* Are multibyte characters enabled in current_buffer? */
1599 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1601 /* Non-zero if we should highlight the region. */
1602 highlight_region_p
1603 = (!NILP (Vtransient_mark_mode)
1604 && !NILP (current_buffer->mark_active)
1605 && XMARKER (current_buffer->mark)->buffer != 0);
1607 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1608 start and end of a visible region in window IT->w. Set both to
1609 -1 to indicate no region. */
1610 if (highlight_region_p
1611 /* Maybe highlight only in selected window. */
1612 && (/* Either show region everywhere. */
1613 highlight_nonselected_windows
1614 /* Or show region in the selected window. */
1615 || w == XWINDOW (selected_window)
1616 /* Or show the region if we are in the mini-buffer and W is
1617 the window the mini-buffer refers to. */
1618 || (MINI_WINDOW_P (XWINDOW (selected_window))
1619 && WINDOWP (minibuf_selected_window)
1620 && w == XWINDOW (minibuf_selected_window))))
1622 int charpos = marker_position (current_buffer->mark);
1623 it->region_beg_charpos = min (PT, charpos);
1624 it->region_end_charpos = max (PT, charpos);
1626 else
1627 it->region_beg_charpos = it->region_end_charpos = -1;
1629 /* Get the position at which the redisplay_end_trigger hook should
1630 be run, if it is to be run at all. */
1631 if (MARKERP (w->redisplay_end_trigger)
1632 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1633 it->redisplay_end_trigger_charpos
1634 = marker_position (w->redisplay_end_trigger);
1635 else if (INTEGERP (w->redisplay_end_trigger))
1636 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1638 /* Correct bogus values of tab_width. */
1639 it->tab_width = XINT (current_buffer->tab_width);
1640 if (it->tab_width <= 0 || it->tab_width > 1000)
1641 it->tab_width = 8;
1643 /* Are lines in the display truncated? */
1644 it->truncate_lines_p
1645 = (base_face_id != DEFAULT_FACE_ID
1646 || XINT (it->w->hscroll)
1647 || (truncate_partial_width_windows
1648 && !WINDOW_FULL_WIDTH_P (it->w))
1649 || !NILP (current_buffer->truncate_lines));
1651 /* Get dimensions of truncation and continuation glyphs. These are
1652 displayed as fringe bitmaps under X, so we don't need them for such
1653 frames. */
1654 if (!FRAME_WINDOW_P (it->f))
1656 if (it->truncate_lines_p)
1658 /* We will need the truncation glyph. */
1659 xassert (it->glyph_row == NULL);
1660 produce_special_glyphs (it, IT_TRUNCATION);
1661 it->truncation_pixel_width = it->pixel_width;
1663 else
1665 /* We will need the continuation glyph. */
1666 xassert (it->glyph_row == NULL);
1667 produce_special_glyphs (it, IT_CONTINUATION);
1668 it->continuation_pixel_width = it->pixel_width;
1671 /* Reset these values to zero because the produce_special_glyphs
1672 above has changed them. */
1673 it->pixel_width = it->ascent = it->descent = 0;
1674 it->phys_ascent = it->phys_descent = 0;
1677 /* Set this after getting the dimensions of truncation and
1678 continuation glyphs, so that we don't produce glyphs when calling
1679 produce_special_glyphs, above. */
1680 it->glyph_row = row;
1681 it->area = TEXT_AREA;
1683 /* Get the dimensions of the display area. The display area
1684 consists of the visible window area plus a horizontally scrolled
1685 part to the left of the window. All x-values are relative to the
1686 start of this total display area. */
1687 if (base_face_id != DEFAULT_FACE_ID)
1689 /* Mode lines, menu bar in terminal frames. */
1690 it->first_visible_x = 0;
1691 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1693 else
1695 it->first_visible_x
1696 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1697 it->last_visible_x = (it->first_visible_x
1698 + window_box_width (w, TEXT_AREA));
1700 /* If we truncate lines, leave room for the truncator glyph(s) at
1701 the right margin. Otherwise, leave room for the continuation
1702 glyph(s). Truncation and continuation glyphs are not inserted
1703 for window-based redisplay. */
1704 if (!FRAME_WINDOW_P (it->f))
1706 if (it->truncate_lines_p)
1707 it->last_visible_x -= it->truncation_pixel_width;
1708 else
1709 it->last_visible_x -= it->continuation_pixel_width;
1712 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1713 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1716 /* Leave room for a border glyph. */
1717 if (!FRAME_WINDOW_P (it->f)
1718 && !WINDOW_RIGHTMOST_P (it->w))
1719 it->last_visible_x -= 1;
1721 it->last_visible_y = window_text_bottom_y (w);
1723 /* For mode lines and alike, arrange for the first glyph having a
1724 left box line if the face specifies a box. */
1725 if (base_face_id != DEFAULT_FACE_ID)
1727 struct face *face;
1729 it->face_id = base_face_id;
1731 /* If we have a boxed mode line, make the first character appear
1732 with a left box line. */
1733 face = FACE_FROM_ID (it->f, base_face_id);
1734 if (face->box != FACE_NO_BOX)
1735 it->start_of_box_run_p = 1;
1738 /* If a buffer position was specified, set the iterator there,
1739 getting overlays and face properties from that position. */
1740 if (charpos >= BUF_BEG (current_buffer))
1742 it->end_charpos = ZV;
1743 it->face_id = -1;
1744 IT_CHARPOS (*it) = charpos;
1746 /* Compute byte position if not specified. */
1747 if (bytepos < charpos)
1748 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1749 else
1750 IT_BYTEPOS (*it) = bytepos;
1752 /* Compute faces etc. */
1753 reseat (it, it->current.pos, 1);
1756 CHECK_IT (it);
1760 /* Initialize IT for the display of window W with window start POS. */
1762 void
1763 start_display (it, w, pos)
1764 struct it *it;
1765 struct window *w;
1766 struct text_pos pos;
1768 struct glyph_row *row;
1769 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1771 row = w->desired_matrix->rows + first_vpos;
1772 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1774 if (!it->truncate_lines_p)
1776 int start_at_line_beg_p;
1777 int first_y = it->current_y;
1779 /* If window start is not at a line start, skip forward to POS to
1780 get the correct continuation lines width. */
1781 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1782 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1783 if (!start_at_line_beg_p)
1785 reseat_at_previous_visible_line_start (it);
1786 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1788 /* If lines are continued, this line may end in the middle
1789 of a multi-glyph character (e.g. a control character
1790 displayed as \003, or in the middle of an overlay
1791 string). In this case move_it_to above will not have
1792 taken us to the start of the continuation line but to the
1793 end of the continued line. */
1794 if (it->current_x > 0)
1796 if (it->current.dpvec_index >= 0
1797 || it->current.overlay_string_index >= 0)
1799 set_iterator_to_next (it, 1);
1800 move_it_in_display_line_to (it, -1, -1, 0);
1803 it->continuation_lines_width += it->current_x;
1806 /* We're starting a new display line, not affected by the
1807 height of the continued line, so clear the appropriate
1808 fields in the iterator structure. */
1809 it->max_ascent = it->max_descent = 0;
1810 it->max_phys_ascent = it->max_phys_descent = 0;
1812 it->current_y = first_y;
1813 it->vpos = 0;
1814 it->current_x = it->hpos = 0;
1818 #if 0 /* Don't assert the following because start_display is sometimes
1819 called intentionally with a window start that is not at a
1820 line start. Please leave this code in as a comment. */
1822 /* Window start should be on a line start, now. */
1823 xassert (it->continuation_lines_width
1824 || IT_CHARPOS (it) == BEGV
1825 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1826 #endif /* 0 */
1830 /* Return 1 if POS is a position in ellipses displayed for invisible
1831 text. W is the window we display, for text property lookup. */
1833 static int
1834 in_ellipses_for_invisible_text_p (pos, w)
1835 struct display_pos *pos;
1836 struct window *w;
1838 Lisp_Object prop, window;
1839 int ellipses_p = 0;
1840 int charpos = CHARPOS (pos->pos);
1842 /* If POS specifies a position in a display vector, this might
1843 be for an ellipsis displayed for invisible text. We won't
1844 get the iterator set up for delivering that ellipsis unless
1845 we make sure that it gets aware of the invisible text. */
1846 if (pos->dpvec_index >= 0
1847 && pos->overlay_string_index < 0
1848 && CHARPOS (pos->string_pos) < 0
1849 && charpos > BEGV
1850 && (XSETWINDOW (window, w),
1851 prop = Fget_char_property (make_number (charpos),
1852 Qinvisible, window),
1853 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1855 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1856 window);
1857 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1860 return ellipses_p;
1864 /* Initialize IT for stepping through current_buffer in window W,
1865 starting at position POS that includes overlay string and display
1866 vector/ control character translation position information. Value
1867 is zero if there are overlay strings with newlines at POS. */
1869 static int
1870 init_from_display_pos (it, w, pos)
1871 struct it *it;
1872 struct window *w;
1873 struct display_pos *pos;
1875 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1876 int i, overlay_strings_with_newlines = 0;
1878 /* If POS specifies a position in a display vector, this might
1879 be for an ellipsis displayed for invisible text. We won't
1880 get the iterator set up for delivering that ellipsis unless
1881 we make sure that it gets aware of the invisible text. */
1882 if (in_ellipses_for_invisible_text_p (pos, w))
1884 --charpos;
1885 bytepos = 0;
1888 /* Keep in mind: the call to reseat in init_iterator skips invisible
1889 text, so we might end up at a position different from POS. This
1890 is only a problem when POS is a row start after a newline and an
1891 overlay starts there with an after-string, and the overlay has an
1892 invisible property. Since we don't skip invisible text in
1893 display_line and elsewhere immediately after consuming the
1894 newline before the row start, such a POS will not be in a string,
1895 but the call to init_iterator below will move us to the
1896 after-string. */
1897 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1899 for (i = 0; i < it->n_overlay_strings; ++i)
1901 const char *s = SDATA (it->overlay_strings[i]);
1902 const char *e = s + SBYTES (it->overlay_strings[i]);
1904 while (s < e && *s != '\n')
1905 ++s;
1907 if (s < e)
1909 overlay_strings_with_newlines = 1;
1910 break;
1914 /* If position is within an overlay string, set up IT to the right
1915 overlay string. */
1916 if (pos->overlay_string_index >= 0)
1918 int relative_index;
1920 /* If the first overlay string happens to have a `display'
1921 property for an image, the iterator will be set up for that
1922 image, and we have to undo that setup first before we can
1923 correct the overlay string index. */
1924 if (it->method == next_element_from_image)
1925 pop_it (it);
1927 /* We already have the first chunk of overlay strings in
1928 IT->overlay_strings. Load more until the one for
1929 pos->overlay_string_index is in IT->overlay_strings. */
1930 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1932 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1933 it->current.overlay_string_index = 0;
1934 while (n--)
1936 load_overlay_strings (it, 0);
1937 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1941 it->current.overlay_string_index = pos->overlay_string_index;
1942 relative_index = (it->current.overlay_string_index
1943 % OVERLAY_STRING_CHUNK_SIZE);
1944 it->string = it->overlay_strings[relative_index];
1945 xassert (STRINGP (it->string));
1946 it->current.string_pos = pos->string_pos;
1947 it->method = next_element_from_string;
1950 #if 0 /* This is bogus because POS not having an overlay string
1951 position does not mean it's after the string. Example: A
1952 line starting with a before-string and initialization of IT
1953 to the previous row's end position. */
1954 else if (it->current.overlay_string_index >= 0)
1956 /* If POS says we're already after an overlay string ending at
1957 POS, make sure to pop the iterator because it will be in
1958 front of that overlay string. When POS is ZV, we've thereby
1959 also ``processed'' overlay strings at ZV. */
1960 while (it->sp)
1961 pop_it (it);
1962 it->current.overlay_string_index = -1;
1963 it->method = next_element_from_buffer;
1964 if (CHARPOS (pos->pos) == ZV)
1965 it->overlay_strings_at_end_processed_p = 1;
1967 #endif /* 0 */
1969 if (CHARPOS (pos->string_pos) >= 0)
1971 /* Recorded position is not in an overlay string, but in another
1972 string. This can only be a string from a `display' property.
1973 IT should already be filled with that string. */
1974 it->current.string_pos = pos->string_pos;
1975 xassert (STRINGP (it->string));
1978 /* Restore position in display vector translations, control
1979 character translations or ellipses. */
1980 if (pos->dpvec_index >= 0)
1982 if (it->dpvec == NULL)
1983 get_next_display_element (it);
1984 xassert (it->dpvec && it->current.dpvec_index == 0);
1985 it->current.dpvec_index = pos->dpvec_index;
1988 CHECK_IT (it);
1989 return !overlay_strings_with_newlines;
1993 /* Initialize IT for stepping through current_buffer in window W
1994 starting at ROW->start. */
1996 static void
1997 init_to_row_start (it, w, row)
1998 struct it *it;
1999 struct window *w;
2000 struct glyph_row *row;
2002 init_from_display_pos (it, w, &row->start);
2003 it->continuation_lines_width = row->continuation_lines_width;
2004 CHECK_IT (it);
2008 /* Initialize IT for stepping through current_buffer in window W
2009 starting in the line following ROW, i.e. starting at ROW->end.
2010 Value is zero if there are overlay strings with newlines at ROW's
2011 end position. */
2013 static int
2014 init_to_row_end (it, w, row)
2015 struct it *it;
2016 struct window *w;
2017 struct glyph_row *row;
2019 int success = 0;
2021 if (init_from_display_pos (it, w, &row->end))
2023 if (row->continued_p)
2024 it->continuation_lines_width
2025 = row->continuation_lines_width + row->pixel_width;
2026 CHECK_IT (it);
2027 success = 1;
2030 return success;
2036 /***********************************************************************
2037 Text properties
2038 ***********************************************************************/
2040 /* Called when IT reaches IT->stop_charpos. Handle text property and
2041 overlay changes. Set IT->stop_charpos to the next position where
2042 to stop. */
2044 static void
2045 handle_stop (it)
2046 struct it *it;
2048 enum prop_handled handled;
2049 int handle_overlay_change_p = 1;
2050 struct props *p;
2052 it->dpvec = NULL;
2053 it->current.dpvec_index = -1;
2057 handled = HANDLED_NORMALLY;
2059 /* Call text property handlers. */
2060 for (p = it_props; p->handler; ++p)
2062 handled = p->handler (it);
2064 if (handled == HANDLED_RECOMPUTE_PROPS)
2065 break;
2066 else if (handled == HANDLED_RETURN)
2067 return;
2068 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2069 handle_overlay_change_p = 0;
2072 if (handled != HANDLED_RECOMPUTE_PROPS)
2074 /* Don't check for overlay strings below when set to deliver
2075 characters from a display vector. */
2076 if (it->method == next_element_from_display_vector)
2077 handle_overlay_change_p = 0;
2079 /* Handle overlay changes. */
2080 if (handle_overlay_change_p)
2081 handled = handle_overlay_change (it);
2083 /* Determine where to stop next. */
2084 if (handled == HANDLED_NORMALLY)
2085 compute_stop_pos (it);
2088 while (handled == HANDLED_RECOMPUTE_PROPS);
2092 /* Compute IT->stop_charpos from text property and overlay change
2093 information for IT's current position. */
2095 static void
2096 compute_stop_pos (it)
2097 struct it *it;
2099 register INTERVAL iv, next_iv;
2100 Lisp_Object object, limit, position;
2102 /* If nowhere else, stop at the end. */
2103 it->stop_charpos = it->end_charpos;
2105 if (STRINGP (it->string))
2107 /* Strings are usually short, so don't limit the search for
2108 properties. */
2109 object = it->string;
2110 limit = Qnil;
2111 position = make_number (IT_STRING_CHARPOS (*it));
2113 else
2115 int charpos;
2117 /* If next overlay change is in front of the current stop pos
2118 (which is IT->end_charpos), stop there. Note: value of
2119 next_overlay_change is point-max if no overlay change
2120 follows. */
2121 charpos = next_overlay_change (IT_CHARPOS (*it));
2122 if (charpos < it->stop_charpos)
2123 it->stop_charpos = charpos;
2125 /* If showing the region, we have to stop at the region
2126 start or end because the face might change there. */
2127 if (it->region_beg_charpos > 0)
2129 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2130 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2131 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2132 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2135 /* Set up variables for computing the stop position from text
2136 property changes. */
2137 XSETBUFFER (object, current_buffer);
2138 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2139 position = make_number (IT_CHARPOS (*it));
2143 /* Get the interval containing IT's position. Value is a null
2144 interval if there isn't such an interval. */
2145 iv = validate_interval_range (object, &position, &position, 0);
2146 if (!NULL_INTERVAL_P (iv))
2148 Lisp_Object values_here[LAST_PROP_IDX];
2149 struct props *p;
2151 /* Get properties here. */
2152 for (p = it_props; p->handler; ++p)
2153 values_here[p->idx] = textget (iv->plist, *p->name);
2155 /* Look for an interval following iv that has different
2156 properties. */
2157 for (next_iv = next_interval (iv);
2158 (!NULL_INTERVAL_P (next_iv)
2159 && (NILP (limit)
2160 || XFASTINT (limit) > next_iv->position));
2161 next_iv = next_interval (next_iv))
2163 for (p = it_props; p->handler; ++p)
2165 Lisp_Object new_value;
2167 new_value = textget (next_iv->plist, *p->name);
2168 if (!EQ (values_here[p->idx], new_value))
2169 break;
2172 if (p->handler)
2173 break;
2176 if (!NULL_INTERVAL_P (next_iv))
2178 if (INTEGERP (limit)
2179 && next_iv->position >= XFASTINT (limit))
2180 /* No text property change up to limit. */
2181 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2182 else
2183 /* Text properties change in next_iv. */
2184 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2188 xassert (STRINGP (it->string)
2189 || (it->stop_charpos >= BEGV
2190 && it->stop_charpos >= IT_CHARPOS (*it)));
2194 /* Return the position of the next overlay change after POS in
2195 current_buffer. Value is point-max if no overlay change
2196 follows. This is like `next-overlay-change' but doesn't use
2197 xmalloc. */
2199 static int
2200 next_overlay_change (pos)
2201 int pos;
2203 int noverlays;
2204 int endpos;
2205 Lisp_Object *overlays;
2206 int len;
2207 int i;
2209 /* Get all overlays at the given position. */
2210 len = 10;
2211 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2212 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2213 if (noverlays > len)
2215 len = noverlays;
2216 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2217 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2220 /* If any of these overlays ends before endpos,
2221 use its ending point instead. */
2222 for (i = 0; i < noverlays; ++i)
2224 Lisp_Object oend;
2225 int oendpos;
2227 oend = OVERLAY_END (overlays[i]);
2228 oendpos = OVERLAY_POSITION (oend);
2229 endpos = min (endpos, oendpos);
2232 return endpos;
2237 /***********************************************************************
2238 Fontification
2239 ***********************************************************************/
2241 /* Handle changes in the `fontified' property of the current buffer by
2242 calling hook functions from Qfontification_functions to fontify
2243 regions of text. */
2245 static enum prop_handled
2246 handle_fontified_prop (it)
2247 struct it *it;
2249 Lisp_Object prop, pos;
2250 enum prop_handled handled = HANDLED_NORMALLY;
2252 /* Get the value of the `fontified' property at IT's current buffer
2253 position. (The `fontified' property doesn't have a special
2254 meaning in strings.) If the value is nil, call functions from
2255 Qfontification_functions. */
2256 if (!STRINGP (it->string)
2257 && it->s == NULL
2258 && !NILP (Vfontification_functions)
2259 && !NILP (Vrun_hooks)
2260 && (pos = make_number (IT_CHARPOS (*it)),
2261 prop = Fget_char_property (pos, Qfontified, Qnil),
2262 NILP (prop)))
2264 int count = SPECPDL_INDEX ();
2265 Lisp_Object val;
2267 val = Vfontification_functions;
2268 specbind (Qfontification_functions, Qnil);
2270 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2271 safe_call1 (val, pos);
2272 else
2274 Lisp_Object globals, fn;
2275 struct gcpro gcpro1, gcpro2;
2277 globals = Qnil;
2278 GCPRO2 (val, globals);
2280 for (; CONSP (val); val = XCDR (val))
2282 fn = XCAR (val);
2284 if (EQ (fn, Qt))
2286 /* A value of t indicates this hook has a local
2287 binding; it means to run the global binding too.
2288 In a global value, t should not occur. If it
2289 does, we must ignore it to avoid an endless
2290 loop. */
2291 for (globals = Fdefault_value (Qfontification_functions);
2292 CONSP (globals);
2293 globals = XCDR (globals))
2295 fn = XCAR (globals);
2296 if (!EQ (fn, Qt))
2297 safe_call1 (fn, pos);
2300 else
2301 safe_call1 (fn, pos);
2304 UNGCPRO;
2307 unbind_to (count, Qnil);
2309 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2310 something. This avoids an endless loop if they failed to
2311 fontify the text for which reason ever. */
2312 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2313 handled = HANDLED_RECOMPUTE_PROPS;
2316 return handled;
2321 /***********************************************************************
2322 Faces
2323 ***********************************************************************/
2325 /* Set up iterator IT from face properties at its current position.
2326 Called from handle_stop. */
2328 static enum prop_handled
2329 handle_face_prop (it)
2330 struct it *it;
2332 int new_face_id, next_stop;
2334 if (!STRINGP (it->string))
2336 new_face_id
2337 = face_at_buffer_position (it->w,
2338 IT_CHARPOS (*it),
2339 it->region_beg_charpos,
2340 it->region_end_charpos,
2341 &next_stop,
2342 (IT_CHARPOS (*it)
2343 + TEXT_PROP_DISTANCE_LIMIT),
2346 /* Is this a start of a run of characters with box face?
2347 Caveat: this can be called for a freshly initialized
2348 iterator; face_id is -1 in this case. We know that the new
2349 face will not change until limit, i.e. if the new face has a
2350 box, all characters up to limit will have one. But, as
2351 usual, we don't know whether limit is really the end. */
2352 if (new_face_id != it->face_id)
2354 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2356 /* If new face has a box but old face has not, this is
2357 the start of a run of characters with box, i.e. it has
2358 a shadow on the left side. The value of face_id of the
2359 iterator will be -1 if this is the initial call that gets
2360 the face. In this case, we have to look in front of IT's
2361 position and see whether there is a face != new_face_id. */
2362 it->start_of_box_run_p
2363 = (new_face->box != FACE_NO_BOX
2364 && (it->face_id >= 0
2365 || IT_CHARPOS (*it) == BEG
2366 || new_face_id != face_before_it_pos (it)));
2367 it->face_box_p = new_face->box != FACE_NO_BOX;
2370 else
2372 int base_face_id, bufpos;
2374 if (it->current.overlay_string_index >= 0)
2375 bufpos = IT_CHARPOS (*it);
2376 else
2377 bufpos = 0;
2379 /* For strings from a buffer, i.e. overlay strings or strings
2380 from a `display' property, use the face at IT's current
2381 buffer position as the base face to merge with, so that
2382 overlay strings appear in the same face as surrounding
2383 text, unless they specify their own faces. */
2384 base_face_id = underlying_face_id (it);
2386 new_face_id = face_at_string_position (it->w,
2387 it->string,
2388 IT_STRING_CHARPOS (*it),
2389 bufpos,
2390 it->region_beg_charpos,
2391 it->region_end_charpos,
2392 &next_stop,
2393 base_face_id, 0);
2395 #if 0 /* This shouldn't be neccessary. Let's check it. */
2396 /* If IT is used to display a mode line we would really like to
2397 use the mode line face instead of the frame's default face. */
2398 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2399 && new_face_id == DEFAULT_FACE_ID)
2400 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2401 #endif
2403 /* Is this a start of a run of characters with box? Caveat:
2404 this can be called for a freshly allocated iterator; face_id
2405 is -1 is this case. We know that the new face will not
2406 change until the next check pos, i.e. if the new face has a
2407 box, all characters up to that position will have a
2408 box. But, as usual, we don't know whether that position
2409 is really the end. */
2410 if (new_face_id != it->face_id)
2412 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2413 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2415 /* If new face has a box but old face hasn't, this is the
2416 start of a run of characters with box, i.e. it has a
2417 shadow on the left side. */
2418 it->start_of_box_run_p
2419 = new_face->box && (old_face == NULL || !old_face->box);
2420 it->face_box_p = new_face->box != FACE_NO_BOX;
2424 it->face_id = new_face_id;
2425 return HANDLED_NORMALLY;
2429 /* Return the ID of the face ``underlying'' IT's current position,
2430 which is in a string. If the iterator is associated with a
2431 buffer, return the face at IT's current buffer position.
2432 Otherwise, use the iterator's base_face_id. */
2434 static int
2435 underlying_face_id (it)
2436 struct it *it;
2438 int face_id = it->base_face_id, i;
2440 xassert (STRINGP (it->string));
2442 for (i = it->sp - 1; i >= 0; --i)
2443 if (NILP (it->stack[i].string))
2444 face_id = it->stack[i].face_id;
2446 return face_id;
2450 /* Compute the face one character before or after the current position
2451 of IT. BEFORE_P non-zero means get the face in front of IT's
2452 position. Value is the id of the face. */
2454 static int
2455 face_before_or_after_it_pos (it, before_p)
2456 struct it *it;
2457 int before_p;
2459 int face_id, limit;
2460 int next_check_charpos;
2461 struct text_pos pos;
2463 xassert (it->s == NULL);
2465 if (STRINGP (it->string))
2467 int bufpos, base_face_id;
2469 /* No face change past the end of the string (for the case
2470 we are padding with spaces). No face change before the
2471 string start. */
2472 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2473 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2474 return it->face_id;
2476 /* Set pos to the position before or after IT's current position. */
2477 if (before_p)
2478 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2479 else
2480 /* For composition, we must check the character after the
2481 composition. */
2482 pos = (it->what == IT_COMPOSITION
2483 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2484 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2486 if (it->current.overlay_string_index >= 0)
2487 bufpos = IT_CHARPOS (*it);
2488 else
2489 bufpos = 0;
2491 base_face_id = underlying_face_id (it);
2493 /* Get the face for ASCII, or unibyte. */
2494 face_id = face_at_string_position (it->w,
2495 it->string,
2496 CHARPOS (pos),
2497 bufpos,
2498 it->region_beg_charpos,
2499 it->region_end_charpos,
2500 &next_check_charpos,
2501 base_face_id, 0);
2503 /* Correct the face for charsets different from ASCII. Do it
2504 for the multibyte case only. The face returned above is
2505 suitable for unibyte text if IT->string is unibyte. */
2506 if (STRING_MULTIBYTE (it->string))
2508 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2509 int rest = SBYTES (it->string) - BYTEPOS (pos);
2510 int c, len;
2511 struct face *face = FACE_FROM_ID (it->f, face_id);
2513 c = string_char_and_length (p, rest, &len);
2514 face_id = FACE_FOR_CHAR (it->f, face, c);
2517 else
2519 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2520 || (IT_CHARPOS (*it) <= BEGV && before_p))
2521 return it->face_id;
2523 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2524 pos = it->current.pos;
2526 if (before_p)
2527 DEC_TEXT_POS (pos, it->multibyte_p);
2528 else
2530 if (it->what == IT_COMPOSITION)
2531 /* For composition, we must check the position after the
2532 composition. */
2533 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2534 else
2535 INC_TEXT_POS (pos, it->multibyte_p);
2538 /* Determine face for CHARSET_ASCII, or unibyte. */
2539 face_id = face_at_buffer_position (it->w,
2540 CHARPOS (pos),
2541 it->region_beg_charpos,
2542 it->region_end_charpos,
2543 &next_check_charpos,
2544 limit, 0);
2546 /* Correct the face for charsets different from ASCII. Do it
2547 for the multibyte case only. The face returned above is
2548 suitable for unibyte text if current_buffer is unibyte. */
2549 if (it->multibyte_p)
2551 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2552 struct face *face = FACE_FROM_ID (it->f, face_id);
2553 face_id = FACE_FOR_CHAR (it->f, face, c);
2557 return face_id;
2562 /***********************************************************************
2563 Invisible text
2564 ***********************************************************************/
2566 /* Set up iterator IT from invisible properties at its current
2567 position. Called from handle_stop. */
2569 static enum prop_handled
2570 handle_invisible_prop (it)
2571 struct it *it;
2573 enum prop_handled handled = HANDLED_NORMALLY;
2575 if (STRINGP (it->string))
2577 extern Lisp_Object Qinvisible;
2578 Lisp_Object prop, end_charpos, limit, charpos;
2580 /* Get the value of the invisible text property at the
2581 current position. Value will be nil if there is no such
2582 property. */
2583 charpos = make_number (IT_STRING_CHARPOS (*it));
2584 prop = Fget_text_property (charpos, Qinvisible, it->string);
2586 if (!NILP (prop)
2587 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2589 handled = HANDLED_RECOMPUTE_PROPS;
2591 /* Get the position at which the next change of the
2592 invisible text property can be found in IT->string.
2593 Value will be nil if the property value is the same for
2594 all the rest of IT->string. */
2595 XSETINT (limit, SCHARS (it->string));
2596 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2597 it->string, limit);
2599 /* Text at current position is invisible. The next
2600 change in the property is at position end_charpos.
2601 Move IT's current position to that position. */
2602 if (INTEGERP (end_charpos)
2603 && XFASTINT (end_charpos) < XFASTINT (limit))
2605 struct text_pos old;
2606 old = it->current.string_pos;
2607 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2608 compute_string_pos (&it->current.string_pos, old, it->string);
2610 else
2612 /* The rest of the string is invisible. If this is an
2613 overlay string, proceed with the next overlay string
2614 or whatever comes and return a character from there. */
2615 if (it->current.overlay_string_index >= 0)
2617 next_overlay_string (it);
2618 /* Don't check for overlay strings when we just
2619 finished processing them. */
2620 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2622 else
2624 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2625 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2630 else
2632 int invis_p, newpos, next_stop, start_charpos;
2633 Lisp_Object pos, prop, overlay;
2635 /* First of all, is there invisible text at this position? */
2636 start_charpos = IT_CHARPOS (*it);
2637 pos = make_number (IT_CHARPOS (*it));
2638 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2639 &overlay);
2640 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2642 /* If we are on invisible text, skip over it. */
2643 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2645 /* Record whether we have to display an ellipsis for the
2646 invisible text. */
2647 int display_ellipsis_p = invis_p == 2;
2649 handled = HANDLED_RECOMPUTE_PROPS;
2651 /* Loop skipping over invisible text. The loop is left at
2652 ZV or with IT on the first char being visible again. */
2655 /* Try to skip some invisible text. Return value is the
2656 position reached which can be equal to IT's position
2657 if there is nothing invisible here. This skips both
2658 over invisible text properties and overlays with
2659 invisible property. */
2660 newpos = skip_invisible (IT_CHARPOS (*it),
2661 &next_stop, ZV, it->window);
2663 /* If we skipped nothing at all we weren't at invisible
2664 text in the first place. If everything to the end of
2665 the buffer was skipped, end the loop. */
2666 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2667 invis_p = 0;
2668 else
2670 /* We skipped some characters but not necessarily
2671 all there are. Check if we ended up on visible
2672 text. Fget_char_property returns the property of
2673 the char before the given position, i.e. if we
2674 get invis_p = 0, this means that the char at
2675 newpos is visible. */
2676 pos = make_number (newpos);
2677 prop = Fget_char_property (pos, Qinvisible, it->window);
2678 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2681 /* If we ended up on invisible text, proceed to
2682 skip starting with next_stop. */
2683 if (invis_p)
2684 IT_CHARPOS (*it) = next_stop;
2686 while (invis_p);
2688 /* The position newpos is now either ZV or on visible text. */
2689 IT_CHARPOS (*it) = newpos;
2690 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2692 /* If there are before-strings at the start of invisible
2693 text, and the text is invisible because of a text
2694 property, arrange to show before-strings because 20.x did
2695 it that way. (If the text is invisible because of an
2696 overlay property instead of a text property, this is
2697 already handled in the overlay code.) */
2698 if (NILP (overlay)
2699 && get_overlay_strings (it, start_charpos))
2701 handled = HANDLED_RECOMPUTE_PROPS;
2702 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2704 else if (display_ellipsis_p)
2705 setup_for_ellipsis (it);
2709 return handled;
2713 /* Make iterator IT return `...' next. */
2715 static void
2716 setup_for_ellipsis (it)
2717 struct it *it;
2719 if (it->dp
2720 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2722 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2723 it->dpvec = v->contents;
2724 it->dpend = v->contents + v->size;
2726 else
2728 /* Default `...'. */
2729 it->dpvec = default_invis_vector;
2730 it->dpend = default_invis_vector + 3;
2733 /* The ellipsis display does not replace the display of the
2734 character at the new position. Indicate this by setting
2735 IT->dpvec_char_len to zero. */
2736 it->dpvec_char_len = 0;
2738 it->current.dpvec_index = 0;
2739 it->method = next_element_from_display_vector;
2744 /***********************************************************************
2745 'display' property
2746 ***********************************************************************/
2748 /* Set up iterator IT from `display' property at its current position.
2749 Called from handle_stop. */
2751 static enum prop_handled
2752 handle_display_prop (it)
2753 struct it *it;
2755 Lisp_Object prop, object;
2756 struct text_pos *position;
2757 int display_replaced_p = 0;
2759 if (STRINGP (it->string))
2761 object = it->string;
2762 position = &it->current.string_pos;
2764 else
2766 object = it->w->buffer;
2767 position = &it->current.pos;
2770 /* Reset those iterator values set from display property values. */
2771 it->font_height = Qnil;
2772 it->space_width = Qnil;
2773 it->voffset = 0;
2775 /* We don't support recursive `display' properties, i.e. string
2776 values that have a string `display' property, that have a string
2777 `display' property etc. */
2778 if (!it->string_from_display_prop_p)
2779 it->area = TEXT_AREA;
2781 prop = Fget_char_property (make_number (position->charpos),
2782 Qdisplay, object);
2783 if (NILP (prop))
2784 return HANDLED_NORMALLY;
2786 if (CONSP (prop)
2787 /* Simple properties. */
2788 && !EQ (XCAR (prop), Qimage)
2789 && !EQ (XCAR (prop), Qspace)
2790 && !EQ (XCAR (prop), Qwhen)
2791 && !EQ (XCAR (prop), Qspace_width)
2792 && !EQ (XCAR (prop), Qheight)
2793 && !EQ (XCAR (prop), Qraise)
2794 /* Marginal area specifications. */
2795 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2796 && !NILP (XCAR (prop)))
2798 for (; CONSP (prop); prop = XCDR (prop))
2800 if (handle_single_display_prop (it, XCAR (prop), object,
2801 position, display_replaced_p))
2802 display_replaced_p = 1;
2805 else if (VECTORP (prop))
2807 int i;
2808 for (i = 0; i < ASIZE (prop); ++i)
2809 if (handle_single_display_prop (it, AREF (prop, i), object,
2810 position, display_replaced_p))
2811 display_replaced_p = 1;
2813 else
2815 if (handle_single_display_prop (it, prop, object, position, 0))
2816 display_replaced_p = 1;
2819 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2823 /* Value is the position of the end of the `display' property starting
2824 at START_POS in OBJECT. */
2826 static struct text_pos
2827 display_prop_end (it, object, start_pos)
2828 struct it *it;
2829 Lisp_Object object;
2830 struct text_pos start_pos;
2832 Lisp_Object end;
2833 struct text_pos end_pos;
2835 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2836 Qdisplay, object, Qnil);
2837 CHARPOS (end_pos) = XFASTINT (end);
2838 if (STRINGP (object))
2839 compute_string_pos (&end_pos, start_pos, it->string);
2840 else
2841 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2843 return end_pos;
2847 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2848 is the object in which the `display' property was found. *POSITION
2849 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2850 means that we previously saw a display sub-property which already
2851 replaced text display with something else, for example an image;
2852 ignore such properties after the first one has been processed.
2854 If PROP is a `space' or `image' sub-property, set *POSITION to the
2855 end position of the `display' property.
2857 Value is non-zero if something was found which replaces the display
2858 of buffer or string text. */
2860 static int
2861 handle_single_display_prop (it, prop, object, position,
2862 display_replaced_before_p)
2863 struct it *it;
2864 Lisp_Object prop;
2865 Lisp_Object object;
2866 struct text_pos *position;
2867 int display_replaced_before_p;
2869 Lisp_Object value;
2870 int replaces_text_display_p = 0;
2871 Lisp_Object form;
2873 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2874 evaluated. If the result is nil, VALUE is ignored. */
2875 form = Qt;
2876 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2878 prop = XCDR (prop);
2879 if (!CONSP (prop))
2880 return 0;
2881 form = XCAR (prop);
2882 prop = XCDR (prop);
2885 if (!NILP (form) && !EQ (form, Qt))
2887 int count = SPECPDL_INDEX ();
2888 struct gcpro gcpro1;
2890 /* Bind `object' to the object having the `display' property, a
2891 buffer or string. Bind `position' to the position in the
2892 object where the property was found, and `buffer-position'
2893 to the current position in the buffer. */
2894 specbind (Qobject, object);
2895 specbind (Qposition, make_number (CHARPOS (*position)));
2896 specbind (Qbuffer_position,
2897 make_number (STRINGP (object)
2898 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2899 GCPRO1 (form);
2900 form = safe_eval (form);
2901 UNGCPRO;
2902 unbind_to (count, Qnil);
2905 if (NILP (form))
2906 return 0;
2908 if (CONSP (prop)
2909 && EQ (XCAR (prop), Qheight)
2910 && CONSP (XCDR (prop)))
2912 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2913 return 0;
2915 /* `(height HEIGHT)'. */
2916 it->font_height = XCAR (XCDR (prop));
2917 if (!NILP (it->font_height))
2919 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2920 int new_height = -1;
2922 if (CONSP (it->font_height)
2923 && (EQ (XCAR (it->font_height), Qplus)
2924 || EQ (XCAR (it->font_height), Qminus))
2925 && CONSP (XCDR (it->font_height))
2926 && INTEGERP (XCAR (XCDR (it->font_height))))
2928 /* `(+ N)' or `(- N)' where N is an integer. */
2929 int steps = XINT (XCAR (XCDR (it->font_height)));
2930 if (EQ (XCAR (it->font_height), Qplus))
2931 steps = - steps;
2932 it->face_id = smaller_face (it->f, it->face_id, steps);
2934 else if (FUNCTIONP (it->font_height))
2936 /* Call function with current height as argument.
2937 Value is the new height. */
2938 Lisp_Object height;
2939 height = safe_call1 (it->font_height,
2940 face->lface[LFACE_HEIGHT_INDEX]);
2941 if (NUMBERP (height))
2942 new_height = XFLOATINT (height);
2944 else if (NUMBERP (it->font_height))
2946 /* Value is a multiple of the canonical char height. */
2947 struct face *face;
2949 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2950 new_height = (XFLOATINT (it->font_height)
2951 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2953 else
2955 /* Evaluate IT->font_height with `height' bound to the
2956 current specified height to get the new height. */
2957 Lisp_Object value;
2958 int count = SPECPDL_INDEX ();
2960 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2961 value = safe_eval (it->font_height);
2962 unbind_to (count, Qnil);
2964 if (NUMBERP (value))
2965 new_height = XFLOATINT (value);
2968 if (new_height > 0)
2969 it->face_id = face_with_height (it->f, it->face_id, new_height);
2972 else if (CONSP (prop)
2973 && EQ (XCAR (prop), Qspace_width)
2974 && CONSP (XCDR (prop)))
2976 /* `(space_width WIDTH)'. */
2977 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2978 return 0;
2980 value = XCAR (XCDR (prop));
2981 if (NUMBERP (value) && XFLOATINT (value) > 0)
2982 it->space_width = value;
2984 else if (CONSP (prop)
2985 && EQ (XCAR (prop), Qraise)
2986 && CONSP (XCDR (prop)))
2988 /* `(raise FACTOR)'. */
2989 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2990 return 0;
2992 #ifdef HAVE_WINDOW_SYSTEM
2993 value = XCAR (XCDR (prop));
2994 if (NUMBERP (value))
2996 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2997 it->voffset = - (XFLOATINT (value)
2998 * (FONT_HEIGHT (face->font)));
3000 #endif /* HAVE_WINDOW_SYSTEM */
3002 else if (!it->string_from_display_prop_p)
3004 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3005 VALUE) or `((margin nil) VALUE)' or VALUE. */
3006 Lisp_Object location, value;
3007 struct text_pos start_pos;
3008 int valid_p;
3010 /* Characters having this form of property are not displayed, so
3011 we have to find the end of the property. */
3012 start_pos = *position;
3013 *position = display_prop_end (it, object, start_pos);
3014 value = Qnil;
3016 /* Let's stop at the new position and assume that all
3017 text properties change there. */
3018 it->stop_charpos = position->charpos;
3020 location = Qunbound;
3021 if (CONSP (prop) && CONSP (XCAR (prop)))
3023 Lisp_Object tem;
3025 value = XCDR (prop);
3026 if (CONSP (value))
3027 value = XCAR (value);
3029 tem = XCAR (prop);
3030 if (EQ (XCAR (tem), Qmargin)
3031 && (tem = XCDR (tem),
3032 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3033 (NILP (tem)
3034 || EQ (tem, Qleft_margin)
3035 || EQ (tem, Qright_margin))))
3036 location = tem;
3039 if (EQ (location, Qunbound))
3041 location = Qnil;
3042 value = prop;
3045 #ifdef HAVE_WINDOW_SYSTEM
3046 if (FRAME_TERMCAP_P (it->f))
3047 valid_p = STRINGP (value);
3048 else
3049 valid_p = (STRINGP (value)
3050 || (CONSP (value) && EQ (XCAR (value), Qspace))
3051 || valid_image_p (value));
3052 #else /* not HAVE_WINDOW_SYSTEM */
3053 valid_p = STRINGP (value);
3054 #endif /* not HAVE_WINDOW_SYSTEM */
3056 if ((EQ (location, Qleft_margin)
3057 || EQ (location, Qright_margin)
3058 || NILP (location))
3059 && valid_p
3060 && !display_replaced_before_p)
3062 replaces_text_display_p = 1;
3064 /* Save current settings of IT so that we can restore them
3065 when we are finished with the glyph property value. */
3066 push_it (it);
3068 if (NILP (location))
3069 it->area = TEXT_AREA;
3070 else if (EQ (location, Qleft_margin))
3071 it->area = LEFT_MARGIN_AREA;
3072 else
3073 it->area = RIGHT_MARGIN_AREA;
3075 if (STRINGP (value))
3077 it->string = value;
3078 it->multibyte_p = STRING_MULTIBYTE (it->string);
3079 it->current.overlay_string_index = -1;
3080 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3081 it->end_charpos = it->string_nchars = SCHARS (it->string);
3082 it->method = next_element_from_string;
3083 it->stop_charpos = 0;
3084 it->string_from_display_prop_p = 1;
3085 /* Say that we haven't consumed the characters with
3086 `display' property yet. The call to pop_it in
3087 set_iterator_to_next will clean this up. */
3088 *position = start_pos;
3090 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3092 it->method = next_element_from_stretch;
3093 it->object = value;
3094 it->current.pos = it->position = start_pos;
3096 #ifdef HAVE_WINDOW_SYSTEM
3097 else
3099 it->what = IT_IMAGE;
3100 it->image_id = lookup_image (it->f, value);
3101 it->position = start_pos;
3102 it->object = NILP (object) ? it->w->buffer : object;
3103 it->method = next_element_from_image;
3105 /* Say that we haven't consumed the characters with
3106 `display' property yet. The call to pop_it in
3107 set_iterator_to_next will clean this up. */
3108 *position = start_pos;
3110 #endif /* HAVE_WINDOW_SYSTEM */
3112 else
3113 /* Invalid property or property not supported. Restore
3114 the position to what it was before. */
3115 *position = start_pos;
3118 return replaces_text_display_p;
3122 /* Check if PROP is a display sub-property value whose text should be
3123 treated as intangible. */
3125 static int
3126 single_display_prop_intangible_p (prop)
3127 Lisp_Object prop;
3129 /* Skip over `when FORM'. */
3130 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3132 prop = XCDR (prop);
3133 if (!CONSP (prop))
3134 return 0;
3135 prop = XCDR (prop);
3138 if (!CONSP (prop))
3139 return 0;
3141 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3142 we don't need to treat text as intangible. */
3143 if (EQ (XCAR (prop), Qmargin))
3145 prop = XCDR (prop);
3146 if (!CONSP (prop))
3147 return 0;
3149 prop = XCDR (prop);
3150 if (!CONSP (prop)
3151 || EQ (XCAR (prop), Qleft_margin)
3152 || EQ (XCAR (prop), Qright_margin))
3153 return 0;
3156 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3160 /* Check if PROP is a display property value whose text should be
3161 treated as intangible. */
3164 display_prop_intangible_p (prop)
3165 Lisp_Object prop;
3167 if (CONSP (prop)
3168 && CONSP (XCAR (prop))
3169 && !EQ (Qmargin, XCAR (XCAR (prop))))
3171 /* A list of sub-properties. */
3172 while (CONSP (prop))
3174 if (single_display_prop_intangible_p (XCAR (prop)))
3175 return 1;
3176 prop = XCDR (prop);
3179 else if (VECTORP (prop))
3181 /* A vector of sub-properties. */
3182 int i;
3183 for (i = 0; i < ASIZE (prop); ++i)
3184 if (single_display_prop_intangible_p (AREF (prop, i)))
3185 return 1;
3187 else
3188 return single_display_prop_intangible_p (prop);
3190 return 0;
3194 /* Return 1 if PROP is a display sub-property value containing STRING. */
3196 static int
3197 single_display_prop_string_p (prop, string)
3198 Lisp_Object prop, string;
3200 if (EQ (string, prop))
3201 return 1;
3203 /* Skip over `when FORM'. */
3204 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3206 prop = XCDR (prop);
3207 if (!CONSP (prop))
3208 return 0;
3209 prop = XCDR (prop);
3212 if (CONSP (prop))
3213 /* Skip over `margin LOCATION'. */
3214 if (EQ (XCAR (prop), Qmargin))
3216 prop = XCDR (prop);
3217 if (!CONSP (prop))
3218 return 0;
3220 prop = XCDR (prop);
3221 if (!CONSP (prop))
3222 return 0;
3225 return CONSP (prop) && EQ (XCAR (prop), string);
3229 /* Return 1 if STRING appears in the `display' property PROP. */
3231 static int
3232 display_prop_string_p (prop, string)
3233 Lisp_Object prop, string;
3235 if (CONSP (prop)
3236 && CONSP (XCAR (prop))
3237 && !EQ (Qmargin, XCAR (XCAR (prop))))
3239 /* A list of sub-properties. */
3240 while (CONSP (prop))
3242 if (single_display_prop_string_p (XCAR (prop), string))
3243 return 1;
3244 prop = XCDR (prop);
3247 else if (VECTORP (prop))
3249 /* A vector of sub-properties. */
3250 int i;
3251 for (i = 0; i < ASIZE (prop); ++i)
3252 if (single_display_prop_string_p (AREF (prop, i), string))
3253 return 1;
3255 else
3256 return single_display_prop_string_p (prop, string);
3258 return 0;
3262 /* Determine from which buffer position in W's buffer STRING comes
3263 from. AROUND_CHARPOS is an approximate position where it could
3264 be from. Value is the buffer position or 0 if it couldn't be
3265 determined.
3267 W's buffer must be current.
3269 This function is necessary because we don't record buffer positions
3270 in glyphs generated from strings (to keep struct glyph small).
3271 This function may only use code that doesn't eval because it is
3272 called asynchronously from note_mouse_highlight. */
3275 string_buffer_position (w, string, around_charpos)
3276 struct window *w;
3277 Lisp_Object string;
3278 int around_charpos;
3280 Lisp_Object limit, prop, pos;
3281 const int MAX_DISTANCE = 1000;
3282 int found = 0;
3284 pos = make_number (around_charpos);
3285 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3286 while (!found && !EQ (pos, limit))
3288 prop = Fget_char_property (pos, Qdisplay, Qnil);
3289 if (!NILP (prop) && display_prop_string_p (prop, string))
3290 found = 1;
3291 else
3292 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3295 if (!found)
3297 pos = make_number (around_charpos);
3298 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3299 while (!found && !EQ (pos, limit))
3301 prop = Fget_char_property (pos, Qdisplay, Qnil);
3302 if (!NILP (prop) && display_prop_string_p (prop, string))
3303 found = 1;
3304 else
3305 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3306 limit);
3310 return found ? XINT (pos) : 0;
3315 /***********************************************************************
3316 `composition' property
3317 ***********************************************************************/
3319 /* Set up iterator IT from `composition' property at its current
3320 position. Called from handle_stop. */
3322 static enum prop_handled
3323 handle_composition_prop (it)
3324 struct it *it;
3326 Lisp_Object prop, string;
3327 int pos, pos_byte, end;
3328 enum prop_handled handled = HANDLED_NORMALLY;
3330 if (STRINGP (it->string))
3332 pos = IT_STRING_CHARPOS (*it);
3333 pos_byte = IT_STRING_BYTEPOS (*it);
3334 string = it->string;
3336 else
3338 pos = IT_CHARPOS (*it);
3339 pos_byte = IT_BYTEPOS (*it);
3340 string = Qnil;
3343 /* If there's a valid composition and point is not inside of the
3344 composition (in the case that the composition is from the current
3345 buffer), draw a glyph composed from the composition components. */
3346 if (find_composition (pos, -1, &pos, &end, &prop, string)
3347 && COMPOSITION_VALID_P (pos, end, prop)
3348 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3350 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3352 if (id >= 0)
3354 it->method = next_element_from_composition;
3355 it->cmp_id = id;
3356 it->cmp_len = COMPOSITION_LENGTH (prop);
3357 /* For a terminal, draw only the first character of the
3358 components. */
3359 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3360 it->len = (STRINGP (it->string)
3361 ? string_char_to_byte (it->string, end)
3362 : CHAR_TO_BYTE (end)) - pos_byte;
3363 it->stop_charpos = end;
3364 handled = HANDLED_RETURN;
3368 return handled;
3373 /***********************************************************************
3374 Overlay strings
3375 ***********************************************************************/
3377 /* The following structure is used to record overlay strings for
3378 later sorting in load_overlay_strings. */
3380 struct overlay_entry
3382 Lisp_Object overlay;
3383 Lisp_Object string;
3384 int priority;
3385 int after_string_p;
3389 /* Set up iterator IT from overlay strings at its current position.
3390 Called from handle_stop. */
3392 static enum prop_handled
3393 handle_overlay_change (it)
3394 struct it *it;
3396 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3397 return HANDLED_RECOMPUTE_PROPS;
3398 else
3399 return HANDLED_NORMALLY;
3403 /* Set up the next overlay string for delivery by IT, if there is an
3404 overlay string to deliver. Called by set_iterator_to_next when the
3405 end of the current overlay string is reached. If there are more
3406 overlay strings to display, IT->string and
3407 IT->current.overlay_string_index are set appropriately here.
3408 Otherwise IT->string is set to nil. */
3410 static void
3411 next_overlay_string (it)
3412 struct it *it;
3414 ++it->current.overlay_string_index;
3415 if (it->current.overlay_string_index == it->n_overlay_strings)
3417 /* No more overlay strings. Restore IT's settings to what
3418 they were before overlay strings were processed, and
3419 continue to deliver from current_buffer. */
3420 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3422 pop_it (it);
3423 xassert (it->stop_charpos >= BEGV
3424 && it->stop_charpos <= it->end_charpos);
3425 it->string = Qnil;
3426 it->current.overlay_string_index = -1;
3427 SET_TEXT_POS (it->current.string_pos, -1, -1);
3428 it->n_overlay_strings = 0;
3429 it->method = next_element_from_buffer;
3431 /* If we're at the end of the buffer, record that we have
3432 processed the overlay strings there already, so that
3433 next_element_from_buffer doesn't try it again. */
3434 if (IT_CHARPOS (*it) >= it->end_charpos)
3435 it->overlay_strings_at_end_processed_p = 1;
3437 /* If we have to display `...' for invisible text, set
3438 the iterator up for that. */
3439 if (display_ellipsis_p)
3440 setup_for_ellipsis (it);
3442 else
3444 /* There are more overlay strings to process. If
3445 IT->current.overlay_string_index has advanced to a position
3446 where we must load IT->overlay_strings with more strings, do
3447 it. */
3448 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3450 if (it->current.overlay_string_index && i == 0)
3451 load_overlay_strings (it, 0);
3453 /* Initialize IT to deliver display elements from the overlay
3454 string. */
3455 it->string = it->overlay_strings[i];
3456 it->multibyte_p = STRING_MULTIBYTE (it->string);
3457 SET_TEXT_POS (it->current.string_pos, 0, 0);
3458 it->method = next_element_from_string;
3459 it->stop_charpos = 0;
3462 CHECK_IT (it);
3466 /* Compare two overlay_entry structures E1 and E2. Used as a
3467 comparison function for qsort in load_overlay_strings. Overlay
3468 strings for the same position are sorted so that
3470 1. All after-strings come in front of before-strings, except
3471 when they come from the same overlay.
3473 2. Within after-strings, strings are sorted so that overlay strings
3474 from overlays with higher priorities come first.
3476 2. Within before-strings, strings are sorted so that overlay
3477 strings from overlays with higher priorities come last.
3479 Value is analogous to strcmp. */
3482 static int
3483 compare_overlay_entries (e1, e2)
3484 void *e1, *e2;
3486 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3487 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3488 int result;
3490 if (entry1->after_string_p != entry2->after_string_p)
3492 /* Let after-strings appear in front of before-strings if
3493 they come from different overlays. */
3494 if (EQ (entry1->overlay, entry2->overlay))
3495 result = entry1->after_string_p ? 1 : -1;
3496 else
3497 result = entry1->after_string_p ? -1 : 1;
3499 else if (entry1->after_string_p)
3500 /* After-strings sorted in order of decreasing priority. */
3501 result = entry2->priority - entry1->priority;
3502 else
3503 /* Before-strings sorted in order of increasing priority. */
3504 result = entry1->priority - entry2->priority;
3506 return result;
3510 /* Load the vector IT->overlay_strings with overlay strings from IT's
3511 current buffer position, or from CHARPOS if that is > 0. Set
3512 IT->n_overlays to the total number of overlay strings found.
3514 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3515 a time. On entry into load_overlay_strings,
3516 IT->current.overlay_string_index gives the number of overlay
3517 strings that have already been loaded by previous calls to this
3518 function.
3520 IT->add_overlay_start contains an additional overlay start
3521 position to consider for taking overlay strings from, if non-zero.
3522 This position comes into play when the overlay has an `invisible'
3523 property, and both before and after-strings. When we've skipped to
3524 the end of the overlay, because of its `invisible' property, we
3525 nevertheless want its before-string to appear.
3526 IT->add_overlay_start will contain the overlay start position
3527 in this case.
3529 Overlay strings are sorted so that after-string strings come in
3530 front of before-string strings. Within before and after-strings,
3531 strings are sorted by overlay priority. See also function
3532 compare_overlay_entries. */
3534 static void
3535 load_overlay_strings (it, charpos)
3536 struct it *it;
3537 int charpos;
3539 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3540 Lisp_Object ov, overlay, window, str, invisible;
3541 int start, end;
3542 int size = 20;
3543 int n = 0, i, j, invis_p;
3544 struct overlay_entry *entries
3545 = (struct overlay_entry *) alloca (size * sizeof *entries);
3547 if (charpos <= 0)
3548 charpos = IT_CHARPOS (*it);
3550 /* Append the overlay string STRING of overlay OVERLAY to vector
3551 `entries' which has size `size' and currently contains `n'
3552 elements. AFTER_P non-zero means STRING is an after-string of
3553 OVERLAY. */
3554 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3555 do \
3557 Lisp_Object priority; \
3559 if (n == size) \
3561 int new_size = 2 * size; \
3562 struct overlay_entry *old = entries; \
3563 entries = \
3564 (struct overlay_entry *) alloca (new_size \
3565 * sizeof *entries); \
3566 bcopy (old, entries, size * sizeof *entries); \
3567 size = new_size; \
3570 entries[n].string = (STRING); \
3571 entries[n].overlay = (OVERLAY); \
3572 priority = Foverlay_get ((OVERLAY), Qpriority); \
3573 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3574 entries[n].after_string_p = (AFTER_P); \
3575 ++n; \
3577 while (0)
3579 /* Process overlay before the overlay center. */
3580 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3582 overlay = XCAR (ov);
3583 xassert (OVERLAYP (overlay));
3584 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3585 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3587 if (end < charpos)
3588 break;
3590 /* Skip this overlay if it doesn't start or end at IT's current
3591 position. */
3592 if (end != charpos && start != charpos)
3593 continue;
3595 /* Skip this overlay if it doesn't apply to IT->w. */
3596 window = Foverlay_get (overlay, Qwindow);
3597 if (WINDOWP (window) && XWINDOW (window) != it->w)
3598 continue;
3600 /* If the text ``under'' the overlay is invisible, both before-
3601 and after-strings from this overlay are visible; start and
3602 end position are indistinguishable. */
3603 invisible = Foverlay_get (overlay, Qinvisible);
3604 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3606 /* If overlay has a non-empty before-string, record it. */
3607 if ((start == charpos || (end == charpos && invis_p))
3608 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3609 && SCHARS (str))
3610 RECORD_OVERLAY_STRING (overlay, str, 0);
3612 /* If overlay has a non-empty after-string, record it. */
3613 if ((end == charpos || (start == charpos && invis_p))
3614 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3615 && SCHARS (str))
3616 RECORD_OVERLAY_STRING (overlay, str, 1);
3619 /* Process overlays after the overlay center. */
3620 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3622 overlay = XCAR (ov);
3623 xassert (OVERLAYP (overlay));
3624 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3625 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3627 if (start > charpos)
3628 break;
3630 /* Skip this overlay if it doesn't start or end at IT's current
3631 position. */
3632 if (end != charpos && start != charpos)
3633 continue;
3635 /* Skip this overlay if it doesn't apply to IT->w. */
3636 window = Foverlay_get (overlay, Qwindow);
3637 if (WINDOWP (window) && XWINDOW (window) != it->w)
3638 continue;
3640 /* If the text ``under'' the overlay is invisible, it has a zero
3641 dimension, and both before- and after-strings apply. */
3642 invisible = Foverlay_get (overlay, Qinvisible);
3643 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3645 /* If overlay has a non-empty before-string, record it. */
3646 if ((start == charpos || (end == charpos && invis_p))
3647 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3648 && SCHARS (str))
3649 RECORD_OVERLAY_STRING (overlay, str, 0);
3651 /* If overlay has a non-empty after-string, record it. */
3652 if ((end == charpos || (start == charpos && invis_p))
3653 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3654 && SCHARS (str))
3655 RECORD_OVERLAY_STRING (overlay, str, 1);
3658 #undef RECORD_OVERLAY_STRING
3660 /* Sort entries. */
3661 if (n > 1)
3662 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3664 /* Record the total number of strings to process. */
3665 it->n_overlay_strings = n;
3667 /* IT->current.overlay_string_index is the number of overlay strings
3668 that have already been consumed by IT. Copy some of the
3669 remaining overlay strings to IT->overlay_strings. */
3670 i = 0;
3671 j = it->current.overlay_string_index;
3672 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3673 it->overlay_strings[i++] = entries[j++].string;
3675 CHECK_IT (it);
3679 /* Get the first chunk of overlay strings at IT's current buffer
3680 position, or at CHARPOS if that is > 0. Value is non-zero if at
3681 least one overlay string was found. */
3683 static int
3684 get_overlay_strings (it, charpos)
3685 struct it *it;
3686 int charpos;
3688 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3689 process. This fills IT->overlay_strings with strings, and sets
3690 IT->n_overlay_strings to the total number of strings to process.
3691 IT->pos.overlay_string_index has to be set temporarily to zero
3692 because load_overlay_strings needs this; it must be set to -1
3693 when no overlay strings are found because a zero value would
3694 indicate a position in the first overlay string. */
3695 it->current.overlay_string_index = 0;
3696 load_overlay_strings (it, charpos);
3698 /* If we found overlay strings, set up IT to deliver display
3699 elements from the first one. Otherwise set up IT to deliver
3700 from current_buffer. */
3701 if (it->n_overlay_strings)
3703 /* Make sure we know settings in current_buffer, so that we can
3704 restore meaningful values when we're done with the overlay
3705 strings. */
3706 compute_stop_pos (it);
3707 xassert (it->face_id >= 0);
3709 /* Save IT's settings. They are restored after all overlay
3710 strings have been processed. */
3711 xassert (it->sp == 0);
3712 push_it (it);
3714 /* Set up IT to deliver display elements from the first overlay
3715 string. */
3716 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3717 it->string = it->overlay_strings[0];
3718 it->stop_charpos = 0;
3719 xassert (STRINGP (it->string));
3720 it->end_charpos = SCHARS (it->string);
3721 it->multibyte_p = STRING_MULTIBYTE (it->string);
3722 it->method = next_element_from_string;
3724 else
3726 it->string = Qnil;
3727 it->current.overlay_string_index = -1;
3728 it->method = next_element_from_buffer;
3731 CHECK_IT (it);
3733 /* Value is non-zero if we found at least one overlay string. */
3734 return STRINGP (it->string);
3739 /***********************************************************************
3740 Saving and restoring state
3741 ***********************************************************************/
3743 /* Save current settings of IT on IT->stack. Called, for example,
3744 before setting up IT for an overlay string, to be able to restore
3745 IT's settings to what they were after the overlay string has been
3746 processed. */
3748 static void
3749 push_it (it)
3750 struct it *it;
3752 struct iterator_stack_entry *p;
3754 xassert (it->sp < 2);
3755 p = it->stack + it->sp;
3757 p->stop_charpos = it->stop_charpos;
3758 xassert (it->face_id >= 0);
3759 p->face_id = it->face_id;
3760 p->string = it->string;
3761 p->pos = it->current;
3762 p->end_charpos = it->end_charpos;
3763 p->string_nchars = it->string_nchars;
3764 p->area = it->area;
3765 p->multibyte_p = it->multibyte_p;
3766 p->space_width = it->space_width;
3767 p->font_height = it->font_height;
3768 p->voffset = it->voffset;
3769 p->string_from_display_prop_p = it->string_from_display_prop_p;
3770 p->display_ellipsis_p = 0;
3771 ++it->sp;
3775 /* Restore IT's settings from IT->stack. Called, for example, when no
3776 more overlay strings must be processed, and we return to delivering
3777 display elements from a buffer, or when the end of a string from a
3778 `display' property is reached and we return to delivering display
3779 elements from an overlay string, or from a buffer. */
3781 static void
3782 pop_it (it)
3783 struct it *it;
3785 struct iterator_stack_entry *p;
3787 xassert (it->sp > 0);
3788 --it->sp;
3789 p = it->stack + it->sp;
3790 it->stop_charpos = p->stop_charpos;
3791 it->face_id = p->face_id;
3792 it->string = p->string;
3793 it->current = p->pos;
3794 it->end_charpos = p->end_charpos;
3795 it->string_nchars = p->string_nchars;
3796 it->area = p->area;
3797 it->multibyte_p = p->multibyte_p;
3798 it->space_width = p->space_width;
3799 it->font_height = p->font_height;
3800 it->voffset = p->voffset;
3801 it->string_from_display_prop_p = p->string_from_display_prop_p;
3806 /***********************************************************************
3807 Moving over lines
3808 ***********************************************************************/
3810 /* Set IT's current position to the previous line start. */
3812 static void
3813 back_to_previous_line_start (it)
3814 struct it *it;
3816 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3817 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3821 /* Move IT to the next line start.
3823 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3824 we skipped over part of the text (as opposed to moving the iterator
3825 continuously over the text). Otherwise, don't change the value
3826 of *SKIPPED_P.
3828 Newlines may come from buffer text, overlay strings, or strings
3829 displayed via the `display' property. That's the reason we can't
3830 simply use find_next_newline_no_quit.
3832 Note that this function may not skip over invisible text that is so
3833 because of text properties and immediately follows a newline. If
3834 it would, function reseat_at_next_visible_line_start, when called
3835 from set_iterator_to_next, would effectively make invisible
3836 characters following a newline part of the wrong glyph row, which
3837 leads to wrong cursor motion. */
3839 static int
3840 forward_to_next_line_start (it, skipped_p)
3841 struct it *it;
3842 int *skipped_p;
3844 int old_selective, newline_found_p, n;
3845 const int MAX_NEWLINE_DISTANCE = 500;
3847 /* If already on a newline, just consume it to avoid unintended
3848 skipping over invisible text below. */
3849 if (it->what == IT_CHARACTER
3850 && it->c == '\n'
3851 && CHARPOS (it->position) == IT_CHARPOS (*it))
3853 set_iterator_to_next (it, 0);
3854 it->c = 0;
3855 return 1;
3858 /* Don't handle selective display in the following. It's (a)
3859 unnecessary because it's done by the caller, and (b) leads to an
3860 infinite recursion because next_element_from_ellipsis indirectly
3861 calls this function. */
3862 old_selective = it->selective;
3863 it->selective = 0;
3865 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3866 from buffer text. */
3867 for (n = newline_found_p = 0;
3868 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3869 n += STRINGP (it->string) ? 0 : 1)
3871 if (!get_next_display_element (it))
3872 break;
3873 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3874 set_iterator_to_next (it, 0);
3877 /* If we didn't find a newline near enough, see if we can use a
3878 short-cut. */
3879 if (!newline_found_p)
3881 int start = IT_CHARPOS (*it);
3882 int limit = find_next_newline_no_quit (start, 1);
3883 Lisp_Object pos;
3885 xassert (!STRINGP (it->string));
3887 /* If there isn't any `display' property in sight, and no
3888 overlays, we can just use the position of the newline in
3889 buffer text. */
3890 if (it->stop_charpos >= limit
3891 || ((pos = Fnext_single_property_change (make_number (start),
3892 Qdisplay,
3893 Qnil, make_number (limit)),
3894 NILP (pos))
3895 && next_overlay_change (start) == ZV))
3897 IT_CHARPOS (*it) = limit;
3898 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3899 *skipped_p = newline_found_p = 1;
3901 else
3903 while (get_next_display_element (it)
3904 && !newline_found_p)
3906 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3907 set_iterator_to_next (it, 0);
3912 it->selective = old_selective;
3913 return newline_found_p;
3917 /* Set IT's current position to the previous visible line start. Skip
3918 invisible text that is so either due to text properties or due to
3919 selective display. Caution: this does not change IT->current_x and
3920 IT->hpos. */
3922 static void
3923 back_to_previous_visible_line_start (it)
3924 struct it *it;
3926 int visible_p = 0;
3928 /* Go back one newline if not on BEGV already. */
3929 if (IT_CHARPOS (*it) > BEGV)
3930 back_to_previous_line_start (it);
3932 /* Move over lines that are invisible because of selective display
3933 or text properties. */
3934 while (IT_CHARPOS (*it) > BEGV
3935 && !visible_p)
3937 visible_p = 1;
3939 /* If selective > 0, then lines indented more than that values
3940 are invisible. */
3941 if (it->selective > 0
3942 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3943 (double) it->selective)) /* iftc */
3944 visible_p = 0;
3945 else
3947 Lisp_Object prop;
3949 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3950 Qinvisible, it->window);
3951 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3952 visible_p = 0;
3955 /* Back one more newline if the current one is invisible. */
3956 if (!visible_p)
3957 back_to_previous_line_start (it);
3960 xassert (IT_CHARPOS (*it) >= BEGV);
3961 xassert (IT_CHARPOS (*it) == BEGV
3962 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3963 CHECK_IT (it);
3967 /* Reseat iterator IT at the previous visible line start. Skip
3968 invisible text that is so either due to text properties or due to
3969 selective display. At the end, update IT's overlay information,
3970 face information etc. */
3972 static void
3973 reseat_at_previous_visible_line_start (it)
3974 struct it *it;
3976 back_to_previous_visible_line_start (it);
3977 reseat (it, it->current.pos, 1);
3978 CHECK_IT (it);
3982 /* Reseat iterator IT on the next visible line start in the current
3983 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3984 preceding the line start. Skip over invisible text that is so
3985 because of selective display. Compute faces, overlays etc at the
3986 new position. Note that this function does not skip over text that
3987 is invisible because of text properties. */
3989 static void
3990 reseat_at_next_visible_line_start (it, on_newline_p)
3991 struct it *it;
3992 int on_newline_p;
3994 int newline_found_p, skipped_p = 0;
3996 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3998 /* Skip over lines that are invisible because they are indented
3999 more than the value of IT->selective. */
4000 if (it->selective > 0)
4001 while (IT_CHARPOS (*it) < ZV
4002 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4003 (double) it->selective)) /* iftc */
4005 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4006 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4009 /* Position on the newline if that's what's requested. */
4010 if (on_newline_p && newline_found_p)
4012 if (STRINGP (it->string))
4014 if (IT_STRING_CHARPOS (*it) > 0)
4016 --IT_STRING_CHARPOS (*it);
4017 --IT_STRING_BYTEPOS (*it);
4020 else if (IT_CHARPOS (*it) > BEGV)
4022 --IT_CHARPOS (*it);
4023 --IT_BYTEPOS (*it);
4024 reseat (it, it->current.pos, 0);
4027 else if (skipped_p)
4028 reseat (it, it->current.pos, 0);
4030 CHECK_IT (it);
4035 /***********************************************************************
4036 Changing an iterator's position
4037 ***********************************************************************/
4039 /* Change IT's current position to POS in current_buffer. If FORCE_P
4040 is non-zero, always check for text properties at the new position.
4041 Otherwise, text properties are only looked up if POS >=
4042 IT->check_charpos of a property. */
4044 static void
4045 reseat (it, pos, force_p)
4046 struct it *it;
4047 struct text_pos pos;
4048 int force_p;
4050 int original_pos = IT_CHARPOS (*it);
4052 reseat_1 (it, pos, 0);
4054 /* Determine where to check text properties. Avoid doing it
4055 where possible because text property lookup is very expensive. */
4056 if (force_p
4057 || CHARPOS (pos) > it->stop_charpos
4058 || CHARPOS (pos) < original_pos)
4059 handle_stop (it);
4061 CHECK_IT (it);
4065 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4066 IT->stop_pos to POS, also. */
4068 static void
4069 reseat_1 (it, pos, set_stop_p)
4070 struct it *it;
4071 struct text_pos pos;
4072 int set_stop_p;
4074 /* Don't call this function when scanning a C string. */
4075 xassert (it->s == NULL);
4077 /* POS must be a reasonable value. */
4078 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4080 it->current.pos = it->position = pos;
4081 XSETBUFFER (it->object, current_buffer);
4082 it->end_charpos = ZV;
4083 it->dpvec = NULL;
4084 it->current.dpvec_index = -1;
4085 it->current.overlay_string_index = -1;
4086 IT_STRING_CHARPOS (*it) = -1;
4087 IT_STRING_BYTEPOS (*it) = -1;
4088 it->string = Qnil;
4089 it->method = next_element_from_buffer;
4090 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4091 it->sp = 0;
4092 it->face_before_selective_p = 0;
4094 if (set_stop_p)
4095 it->stop_charpos = CHARPOS (pos);
4099 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4100 If S is non-null, it is a C string to iterate over. Otherwise,
4101 STRING gives a Lisp string to iterate over.
4103 If PRECISION > 0, don't return more then PRECISION number of
4104 characters from the string.
4106 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4107 characters have been returned. FIELD_WIDTH < 0 means an infinite
4108 field width.
4110 MULTIBYTE = 0 means disable processing of multibyte characters,
4111 MULTIBYTE > 0 means enable it,
4112 MULTIBYTE < 0 means use IT->multibyte_p.
4114 IT must be initialized via a prior call to init_iterator before
4115 calling this function. */
4117 static void
4118 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4119 struct it *it;
4120 unsigned char *s;
4121 Lisp_Object string;
4122 int charpos;
4123 int precision, field_width, multibyte;
4125 /* No region in strings. */
4126 it->region_beg_charpos = it->region_end_charpos = -1;
4128 /* No text property checks performed by default, but see below. */
4129 it->stop_charpos = -1;
4131 /* Set iterator position and end position. */
4132 bzero (&it->current, sizeof it->current);
4133 it->current.overlay_string_index = -1;
4134 it->current.dpvec_index = -1;
4135 xassert (charpos >= 0);
4137 /* If STRING is specified, use its multibyteness, otherwise use the
4138 setting of MULTIBYTE, if specified. */
4139 if (multibyte >= 0)
4140 it->multibyte_p = multibyte > 0;
4142 if (s == NULL)
4144 xassert (STRINGP (string));
4145 it->string = string;
4146 it->s = NULL;
4147 it->end_charpos = it->string_nchars = SCHARS (string);
4148 it->method = next_element_from_string;
4149 it->current.string_pos = string_pos (charpos, string);
4151 else
4153 it->s = s;
4154 it->string = Qnil;
4156 /* Note that we use IT->current.pos, not it->current.string_pos,
4157 for displaying C strings. */
4158 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4159 if (it->multibyte_p)
4161 it->current.pos = c_string_pos (charpos, s, 1);
4162 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4164 else
4166 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4167 it->end_charpos = it->string_nchars = strlen (s);
4170 it->method = next_element_from_c_string;
4173 /* PRECISION > 0 means don't return more than PRECISION characters
4174 from the string. */
4175 if (precision > 0 && it->end_charpos - charpos > precision)
4176 it->end_charpos = it->string_nchars = charpos + precision;
4178 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4179 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4180 FIELD_WIDTH < 0 means infinite field width. This is useful for
4181 padding with `-' at the end of a mode line. */
4182 if (field_width < 0)
4183 field_width = INFINITY;
4184 if (field_width > it->end_charpos - charpos)
4185 it->end_charpos = charpos + field_width;
4187 /* Use the standard display table for displaying strings. */
4188 if (DISP_TABLE_P (Vstandard_display_table))
4189 it->dp = XCHAR_TABLE (Vstandard_display_table);
4191 it->stop_charpos = charpos;
4192 CHECK_IT (it);
4197 /***********************************************************************
4198 Iteration
4199 ***********************************************************************/
4201 /* Load IT's display element fields with information about the next
4202 display element from the current position of IT. Value is zero if
4203 end of buffer (or C string) is reached. */
4206 get_next_display_element (it)
4207 struct it *it;
4209 /* Non-zero means that we found an display element. Zero means that
4210 we hit the end of what we iterate over. Performance note: the
4211 function pointer `method' used here turns out to be faster than
4212 using a sequence of if-statements. */
4213 int success_p = (*it->method) (it);
4215 if (it->what == IT_CHARACTER)
4217 /* Map via display table or translate control characters.
4218 IT->c, IT->len etc. have been set to the next character by
4219 the function call above. If we have a display table, and it
4220 contains an entry for IT->c, translate it. Don't do this if
4221 IT->c itself comes from a display table, otherwise we could
4222 end up in an infinite recursion. (An alternative could be to
4223 count the recursion depth of this function and signal an
4224 error when a certain maximum depth is reached.) Is it worth
4225 it? */
4226 if (success_p && it->dpvec == NULL)
4228 Lisp_Object dv;
4230 if (it->dp
4231 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4232 VECTORP (dv)))
4234 struct Lisp_Vector *v = XVECTOR (dv);
4236 /* Return the first character from the display table
4237 entry, if not empty. If empty, don't display the
4238 current character. */
4239 if (v->size)
4241 it->dpvec_char_len = it->len;
4242 it->dpvec = v->contents;
4243 it->dpend = v->contents + v->size;
4244 it->current.dpvec_index = 0;
4245 it->method = next_element_from_display_vector;
4246 success_p = get_next_display_element (it);
4248 else
4250 set_iterator_to_next (it, 0);
4251 success_p = get_next_display_element (it);
4255 /* Translate control characters into `\003' or `^C' form.
4256 Control characters coming from a display table entry are
4257 currently not translated because we use IT->dpvec to hold
4258 the translation. This could easily be changed but I
4259 don't believe that it is worth doing.
4261 Non-printable multibyte characters are also translated
4262 octal form. */
4263 else if ((it->c < ' '
4264 && (it->area != TEXT_AREA
4265 || (it->c != '\n' && it->c != '\t')))
4266 || (it->c >= 127
4267 && it->len == 1)
4268 || !CHAR_PRINTABLE_P (it->c))
4270 /* IT->c is a control character which must be displayed
4271 either as '\003' or as `^C' where the '\\' and '^'
4272 can be defined in the display table. Fill
4273 IT->ctl_chars with glyphs for what we have to
4274 display. Then, set IT->dpvec to these glyphs. */
4275 GLYPH g;
4277 if (it->c < 128 && it->ctl_arrow_p)
4279 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4280 if (it->dp
4281 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4282 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4283 g = XINT (DISP_CTRL_GLYPH (it->dp));
4284 else
4285 g = FAST_MAKE_GLYPH ('^', 0);
4286 XSETINT (it->ctl_chars[0], g);
4288 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4289 XSETINT (it->ctl_chars[1], g);
4291 /* Set up IT->dpvec and return first character from it. */
4292 it->dpvec_char_len = it->len;
4293 it->dpvec = it->ctl_chars;
4294 it->dpend = it->dpvec + 2;
4295 it->current.dpvec_index = 0;
4296 it->method = next_element_from_display_vector;
4297 get_next_display_element (it);
4299 else
4301 unsigned char str[MAX_MULTIBYTE_LENGTH];
4302 int len;
4303 int i;
4304 GLYPH escape_glyph;
4306 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4307 if (it->dp
4308 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4309 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4310 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4311 else
4312 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4314 if (SINGLE_BYTE_CHAR_P (it->c))
4315 str[0] = it->c, len = 1;
4316 else
4318 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4319 if (len < 0)
4321 /* It's an invalid character, which
4322 shouldn't happen actually, but due to
4323 bugs it may happen. Let's print the char
4324 as is, there's not much meaningful we can
4325 do with it. */
4326 str[0] = it->c;
4327 str[1] = it->c >> 8;
4328 str[2] = it->c >> 16;
4329 str[3] = it->c >> 24;
4330 len = 4;
4334 for (i = 0; i < len; i++)
4336 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4337 /* Insert three more glyphs into IT->ctl_chars for
4338 the octal display of the character. */
4339 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4340 XSETINT (it->ctl_chars[i * 4 + 1], g);
4341 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4342 XSETINT (it->ctl_chars[i * 4 + 2], g);
4343 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4344 XSETINT (it->ctl_chars[i * 4 + 3], g);
4347 /* Set up IT->dpvec and return the first character
4348 from it. */
4349 it->dpvec_char_len = it->len;
4350 it->dpvec = it->ctl_chars;
4351 it->dpend = it->dpvec + len * 4;
4352 it->current.dpvec_index = 0;
4353 it->method = next_element_from_display_vector;
4354 get_next_display_element (it);
4359 /* Adjust face id for a multibyte character. There are no
4360 multibyte character in unibyte text. */
4361 if (it->multibyte_p
4362 && success_p
4363 && FRAME_WINDOW_P (it->f))
4365 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4366 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4370 /* Is this character the last one of a run of characters with
4371 box? If yes, set IT->end_of_box_run_p to 1. */
4372 if (it->face_box_p
4373 && it->s == NULL)
4375 int face_id;
4376 struct face *face;
4378 it->end_of_box_run_p
4379 = ((face_id = face_after_it_pos (it),
4380 face_id != it->face_id)
4381 && (face = FACE_FROM_ID (it->f, face_id),
4382 face->box == FACE_NO_BOX));
4385 /* Value is 0 if end of buffer or string reached. */
4386 return success_p;
4390 /* Move IT to the next display element.
4392 RESEAT_P non-zero means if called on a newline in buffer text,
4393 skip to the next visible line start.
4395 Functions get_next_display_element and set_iterator_to_next are
4396 separate because I find this arrangement easier to handle than a
4397 get_next_display_element function that also increments IT's
4398 position. The way it is we can first look at an iterator's current
4399 display element, decide whether it fits on a line, and if it does,
4400 increment the iterator position. The other way around we probably
4401 would either need a flag indicating whether the iterator has to be
4402 incremented the next time, or we would have to implement a
4403 decrement position function which would not be easy to write. */
4405 void
4406 set_iterator_to_next (it, reseat_p)
4407 struct it *it;
4408 int reseat_p;
4410 /* Reset flags indicating start and end of a sequence of characters
4411 with box. Reset them at the start of this function because
4412 moving the iterator to a new position might set them. */
4413 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4415 if (it->method == next_element_from_buffer)
4417 /* The current display element of IT is a character from
4418 current_buffer. Advance in the buffer, and maybe skip over
4419 invisible lines that are so because of selective display. */
4420 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4421 reseat_at_next_visible_line_start (it, 0);
4422 else
4424 xassert (it->len != 0);
4425 IT_BYTEPOS (*it) += it->len;
4426 IT_CHARPOS (*it) += 1;
4427 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4430 else if (it->method == next_element_from_composition)
4432 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4433 if (STRINGP (it->string))
4435 IT_STRING_BYTEPOS (*it) += it->len;
4436 IT_STRING_CHARPOS (*it) += it->cmp_len;
4437 it->method = next_element_from_string;
4438 goto consider_string_end;
4440 else
4442 IT_BYTEPOS (*it) += it->len;
4443 IT_CHARPOS (*it) += it->cmp_len;
4444 it->method = next_element_from_buffer;
4447 else if (it->method == next_element_from_c_string)
4449 /* Current display element of IT is from a C string. */
4450 IT_BYTEPOS (*it) += it->len;
4451 IT_CHARPOS (*it) += 1;
4453 else if (it->method == next_element_from_display_vector)
4455 /* Current display element of IT is from a display table entry.
4456 Advance in the display table definition. Reset it to null if
4457 end reached, and continue with characters from buffers/
4458 strings. */
4459 ++it->current.dpvec_index;
4461 /* Restore face of the iterator to what they were before the
4462 display vector entry (these entries may contain faces). */
4463 it->face_id = it->saved_face_id;
4465 if (it->dpvec + it->current.dpvec_index == it->dpend)
4467 if (it->s)
4468 it->method = next_element_from_c_string;
4469 else if (STRINGP (it->string))
4470 it->method = next_element_from_string;
4471 else
4472 it->method = next_element_from_buffer;
4474 it->dpvec = NULL;
4475 it->current.dpvec_index = -1;
4477 /* Skip over characters which were displayed via IT->dpvec. */
4478 if (it->dpvec_char_len < 0)
4479 reseat_at_next_visible_line_start (it, 1);
4480 else if (it->dpvec_char_len > 0)
4482 it->len = it->dpvec_char_len;
4483 set_iterator_to_next (it, reseat_p);
4487 else if (it->method == next_element_from_string)
4489 /* Current display element is a character from a Lisp string. */
4490 xassert (it->s == NULL && STRINGP (it->string));
4491 IT_STRING_BYTEPOS (*it) += it->len;
4492 IT_STRING_CHARPOS (*it) += 1;
4494 consider_string_end:
4496 if (it->current.overlay_string_index >= 0)
4498 /* IT->string is an overlay string. Advance to the
4499 next, if there is one. */
4500 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4501 next_overlay_string (it);
4503 else
4505 /* IT->string is not an overlay string. If we reached
4506 its end, and there is something on IT->stack, proceed
4507 with what is on the stack. This can be either another
4508 string, this time an overlay string, or a buffer. */
4509 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4510 && it->sp > 0)
4512 pop_it (it);
4513 if (!STRINGP (it->string))
4514 it->method = next_element_from_buffer;
4515 else
4516 goto consider_string_end;
4520 else if (it->method == next_element_from_image
4521 || it->method == next_element_from_stretch)
4523 /* The position etc with which we have to proceed are on
4524 the stack. The position may be at the end of a string,
4525 if the `display' property takes up the whole string. */
4526 pop_it (it);
4527 it->image_id = 0;
4528 if (STRINGP (it->string))
4530 it->method = next_element_from_string;
4531 goto consider_string_end;
4533 else
4534 it->method = next_element_from_buffer;
4536 else
4537 /* There are no other methods defined, so this should be a bug. */
4538 abort ();
4540 xassert (it->method != next_element_from_string
4541 || (STRINGP (it->string)
4542 && IT_STRING_CHARPOS (*it) >= 0));
4546 /* Load IT's display element fields with information about the next
4547 display element which comes from a display table entry or from the
4548 result of translating a control character to one of the forms `^C'
4549 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4551 static int
4552 next_element_from_display_vector (it)
4553 struct it *it;
4555 /* Precondition. */
4556 xassert (it->dpvec && it->current.dpvec_index >= 0);
4558 /* Remember the current face id in case glyphs specify faces.
4559 IT's face is restored in set_iterator_to_next. */
4560 it->saved_face_id = it->face_id;
4562 if (INTEGERP (*it->dpvec)
4563 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4565 int lface_id;
4566 GLYPH g;
4568 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4569 it->c = FAST_GLYPH_CHAR (g);
4570 it->len = CHAR_BYTES (it->c);
4572 /* The entry may contain a face id to use. Such a face id is
4573 the id of a Lisp face, not a realized face. A face id of
4574 zero means no face is specified. */
4575 lface_id = FAST_GLYPH_FACE (g);
4576 if (lface_id)
4578 /* The function returns -1 if lface_id is invalid. */
4579 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4580 if (face_id >= 0)
4581 it->face_id = face_id;
4584 else
4585 /* Display table entry is invalid. Return a space. */
4586 it->c = ' ', it->len = 1;
4588 /* Don't change position and object of the iterator here. They are
4589 still the values of the character that had this display table
4590 entry or was translated, and that's what we want. */
4591 it->what = IT_CHARACTER;
4592 return 1;
4596 /* Load IT with the next display element from Lisp string IT->string.
4597 IT->current.string_pos is the current position within the string.
4598 If IT->current.overlay_string_index >= 0, the Lisp string is an
4599 overlay string. */
4601 static int
4602 next_element_from_string (it)
4603 struct it *it;
4605 struct text_pos position;
4607 xassert (STRINGP (it->string));
4608 xassert (IT_STRING_CHARPOS (*it) >= 0);
4609 position = it->current.string_pos;
4611 /* Time to check for invisible text? */
4612 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4613 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4615 handle_stop (it);
4617 /* Since a handler may have changed IT->method, we must
4618 recurse here. */
4619 return get_next_display_element (it);
4622 if (it->current.overlay_string_index >= 0)
4624 /* Get the next character from an overlay string. In overlay
4625 strings, There is no field width or padding with spaces to
4626 do. */
4627 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4629 it->what = IT_EOB;
4630 return 0;
4632 else if (STRING_MULTIBYTE (it->string))
4634 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4635 const unsigned char *s = (SDATA (it->string)
4636 + IT_STRING_BYTEPOS (*it));
4637 it->c = string_char_and_length (s, remaining, &it->len);
4639 else
4641 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4642 it->len = 1;
4645 else
4647 /* Get the next character from a Lisp string that is not an
4648 overlay string. Such strings come from the mode line, for
4649 example. We may have to pad with spaces, or truncate the
4650 string. See also next_element_from_c_string. */
4651 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4653 it->what = IT_EOB;
4654 return 0;
4656 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4658 /* Pad with spaces. */
4659 it->c = ' ', it->len = 1;
4660 CHARPOS (position) = BYTEPOS (position) = -1;
4662 else if (STRING_MULTIBYTE (it->string))
4664 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4665 const unsigned char *s = (SDATA (it->string)
4666 + IT_STRING_BYTEPOS (*it));
4667 it->c = string_char_and_length (s, maxlen, &it->len);
4669 else
4671 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4672 it->len = 1;
4676 /* Record what we have and where it came from. Note that we store a
4677 buffer position in IT->position although it could arguably be a
4678 string position. */
4679 it->what = IT_CHARACTER;
4680 it->object = it->string;
4681 it->position = position;
4682 return 1;
4686 /* Load IT with next display element from C string IT->s.
4687 IT->string_nchars is the maximum number of characters to return
4688 from the string. IT->end_charpos may be greater than
4689 IT->string_nchars when this function is called, in which case we
4690 may have to return padding spaces. Value is zero if end of string
4691 reached, including padding spaces. */
4693 static int
4694 next_element_from_c_string (it)
4695 struct it *it;
4697 int success_p = 1;
4699 xassert (it->s);
4700 it->what = IT_CHARACTER;
4701 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4702 it->object = Qnil;
4704 /* IT's position can be greater IT->string_nchars in case a field
4705 width or precision has been specified when the iterator was
4706 initialized. */
4707 if (IT_CHARPOS (*it) >= it->end_charpos)
4709 /* End of the game. */
4710 it->what = IT_EOB;
4711 success_p = 0;
4713 else if (IT_CHARPOS (*it) >= it->string_nchars)
4715 /* Pad with spaces. */
4716 it->c = ' ', it->len = 1;
4717 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4719 else if (it->multibyte_p)
4721 /* Implementation note: The calls to strlen apparently aren't a
4722 performance problem because there is no noticeable performance
4723 difference between Emacs running in unibyte or multibyte mode. */
4724 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4725 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4726 maxlen, &it->len);
4728 else
4729 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4731 return success_p;
4735 /* Set up IT to return characters from an ellipsis, if appropriate.
4736 The definition of the ellipsis glyphs may come from a display table
4737 entry. This function Fills IT with the first glyph from the
4738 ellipsis if an ellipsis is to be displayed. */
4740 static int
4741 next_element_from_ellipsis (it)
4742 struct it *it;
4744 if (it->selective_display_ellipsis_p)
4746 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4748 /* Use the display table definition for `...'. Invalid glyphs
4749 will be handled by the method returning elements from dpvec. */
4750 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4751 it->dpvec_char_len = it->len;
4752 it->dpvec = v->contents;
4753 it->dpend = v->contents + v->size;
4754 it->current.dpvec_index = 0;
4755 it->method = next_element_from_display_vector;
4757 else
4759 /* Use default `...' which is stored in default_invis_vector. */
4760 it->dpvec_char_len = it->len;
4761 it->dpvec = default_invis_vector;
4762 it->dpend = default_invis_vector + 3;
4763 it->current.dpvec_index = 0;
4764 it->method = next_element_from_display_vector;
4767 else
4769 /* The face at the current position may be different from the
4770 face we find after the invisible text. Remember what it
4771 was in IT->saved_face_id, and signal that it's there by
4772 setting face_before_selective_p. */
4773 it->saved_face_id = it->face_id;
4774 it->method = next_element_from_buffer;
4775 reseat_at_next_visible_line_start (it, 1);
4776 it->face_before_selective_p = 1;
4779 return get_next_display_element (it);
4783 /* Deliver an image display element. The iterator IT is already
4784 filled with image information (done in handle_display_prop). Value
4785 is always 1. */
4788 static int
4789 next_element_from_image (it)
4790 struct it *it;
4792 it->what = IT_IMAGE;
4793 return 1;
4797 /* Fill iterator IT with next display element from a stretch glyph
4798 property. IT->object is the value of the text property. Value is
4799 always 1. */
4801 static int
4802 next_element_from_stretch (it)
4803 struct it *it;
4805 it->what = IT_STRETCH;
4806 return 1;
4810 /* Load IT with the next display element from current_buffer. Value
4811 is zero if end of buffer reached. IT->stop_charpos is the next
4812 position at which to stop and check for text properties or buffer
4813 end. */
4815 static int
4816 next_element_from_buffer (it)
4817 struct it *it;
4819 int success_p = 1;
4821 /* Check this assumption, otherwise, we would never enter the
4822 if-statement, below. */
4823 xassert (IT_CHARPOS (*it) >= BEGV
4824 && IT_CHARPOS (*it) <= it->stop_charpos);
4826 if (IT_CHARPOS (*it) >= it->stop_charpos)
4828 if (IT_CHARPOS (*it) >= it->end_charpos)
4830 int overlay_strings_follow_p;
4832 /* End of the game, except when overlay strings follow that
4833 haven't been returned yet. */
4834 if (it->overlay_strings_at_end_processed_p)
4835 overlay_strings_follow_p = 0;
4836 else
4838 it->overlay_strings_at_end_processed_p = 1;
4839 overlay_strings_follow_p = get_overlay_strings (it, 0);
4842 if (overlay_strings_follow_p)
4843 success_p = get_next_display_element (it);
4844 else
4846 it->what = IT_EOB;
4847 it->position = it->current.pos;
4848 success_p = 0;
4851 else
4853 handle_stop (it);
4854 return get_next_display_element (it);
4857 else
4859 /* No face changes, overlays etc. in sight, so just return a
4860 character from current_buffer. */
4861 unsigned char *p;
4863 /* Maybe run the redisplay end trigger hook. Performance note:
4864 This doesn't seem to cost measurable time. */
4865 if (it->redisplay_end_trigger_charpos
4866 && it->glyph_row
4867 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4868 run_redisplay_end_trigger_hook (it);
4870 /* Get the next character, maybe multibyte. */
4871 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4872 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4874 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4875 - IT_BYTEPOS (*it));
4876 it->c = string_char_and_length (p, maxlen, &it->len);
4878 else
4879 it->c = *p, it->len = 1;
4881 /* Record what we have and where it came from. */
4882 it->what = IT_CHARACTER;;
4883 it->object = it->w->buffer;
4884 it->position = it->current.pos;
4886 /* Normally we return the character found above, except when we
4887 really want to return an ellipsis for selective display. */
4888 if (it->selective)
4890 if (it->c == '\n')
4892 /* A value of selective > 0 means hide lines indented more
4893 than that number of columns. */
4894 if (it->selective > 0
4895 && IT_CHARPOS (*it) + 1 < ZV
4896 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4897 IT_BYTEPOS (*it) + 1,
4898 (double) it->selective)) /* iftc */
4900 success_p = next_element_from_ellipsis (it);
4901 it->dpvec_char_len = -1;
4904 else if (it->c == '\r' && it->selective == -1)
4906 /* A value of selective == -1 means that everything from the
4907 CR to the end of the line is invisible, with maybe an
4908 ellipsis displayed for it. */
4909 success_p = next_element_from_ellipsis (it);
4910 it->dpvec_char_len = -1;
4915 /* Value is zero if end of buffer reached. */
4916 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4917 return success_p;
4921 /* Run the redisplay end trigger hook for IT. */
4923 static void
4924 run_redisplay_end_trigger_hook (it)
4925 struct it *it;
4927 Lisp_Object args[3];
4929 /* IT->glyph_row should be non-null, i.e. we should be actually
4930 displaying something, or otherwise we should not run the hook. */
4931 xassert (it->glyph_row);
4933 /* Set up hook arguments. */
4934 args[0] = Qredisplay_end_trigger_functions;
4935 args[1] = it->window;
4936 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4937 it->redisplay_end_trigger_charpos = 0;
4939 /* Since we are *trying* to run these functions, don't try to run
4940 them again, even if they get an error. */
4941 it->w->redisplay_end_trigger = Qnil;
4942 Frun_hook_with_args (3, args);
4944 /* Notice if it changed the face of the character we are on. */
4945 handle_face_prop (it);
4949 /* Deliver a composition display element. The iterator IT is already
4950 filled with composition information (done in
4951 handle_composition_prop). Value is always 1. */
4953 static int
4954 next_element_from_composition (it)
4955 struct it *it;
4957 it->what = IT_COMPOSITION;
4958 it->position = (STRINGP (it->string)
4959 ? it->current.string_pos
4960 : it->current.pos);
4961 return 1;
4966 /***********************************************************************
4967 Moving an iterator without producing glyphs
4968 ***********************************************************************/
4970 /* Move iterator IT to a specified buffer or X position within one
4971 line on the display without producing glyphs.
4973 OP should be a bit mask including some or all of these bits:
4974 MOVE_TO_X: Stop on reaching x-position TO_X.
4975 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4976 Regardless of OP's value, stop in reaching the end of the display line.
4978 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4979 This means, in particular, that TO_X includes window's horizontal
4980 scroll amount.
4982 The return value has several possible values that
4983 say what condition caused the scan to stop:
4985 MOVE_POS_MATCH_OR_ZV
4986 - when TO_POS or ZV was reached.
4988 MOVE_X_REACHED
4989 -when TO_X was reached before TO_POS or ZV were reached.
4991 MOVE_LINE_CONTINUED
4992 - when we reached the end of the display area and the line must
4993 be continued.
4995 MOVE_LINE_TRUNCATED
4996 - when we reached the end of the display area and the line is
4997 truncated.
4999 MOVE_NEWLINE_OR_CR
5000 - when we stopped at a line end, i.e. a newline or a CR and selective
5001 display is on. */
5003 static enum move_it_result
5004 move_it_in_display_line_to (it, to_charpos, to_x, op)
5005 struct it *it;
5006 int to_charpos, to_x, op;
5008 enum move_it_result result = MOVE_UNDEFINED;
5009 struct glyph_row *saved_glyph_row;
5011 /* Don't produce glyphs in produce_glyphs. */
5012 saved_glyph_row = it->glyph_row;
5013 it->glyph_row = NULL;
5015 while (1)
5017 int x, i, ascent = 0, descent = 0;
5019 /* Stop when ZV or TO_CHARPOS reached. */
5020 if (!get_next_display_element (it)
5021 || ((op & MOVE_TO_POS) != 0
5022 && BUFFERP (it->object)
5023 && IT_CHARPOS (*it) >= to_charpos))
5025 result = MOVE_POS_MATCH_OR_ZV;
5026 break;
5029 /* The call to produce_glyphs will get the metrics of the
5030 display element IT is loaded with. We record in x the
5031 x-position before this display element in case it does not
5032 fit on the line. */
5033 x = it->current_x;
5035 /* Remember the line height so far in case the next element doesn't
5036 fit on the line. */
5037 if (!it->truncate_lines_p)
5039 ascent = it->max_ascent;
5040 descent = it->max_descent;
5043 PRODUCE_GLYPHS (it);
5045 if (it->area != TEXT_AREA)
5047 set_iterator_to_next (it, 1);
5048 continue;
5051 /* The number of glyphs we get back in IT->nglyphs will normally
5052 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5053 character on a terminal frame, or (iii) a line end. For the
5054 second case, IT->nglyphs - 1 padding glyphs will be present
5055 (on X frames, there is only one glyph produced for a
5056 composite character.
5058 The behavior implemented below means, for continuation lines,
5059 that as many spaces of a TAB as fit on the current line are
5060 displayed there. For terminal frames, as many glyphs of a
5061 multi-glyph character are displayed in the current line, too.
5062 This is what the old redisplay code did, and we keep it that
5063 way. Under X, the whole shape of a complex character must
5064 fit on the line or it will be completely displayed in the
5065 next line.
5067 Note that both for tabs and padding glyphs, all glyphs have
5068 the same width. */
5069 if (it->nglyphs)
5071 /* More than one glyph or glyph doesn't fit on line. All
5072 glyphs have the same width. */
5073 int single_glyph_width = it->pixel_width / it->nglyphs;
5074 int new_x;
5076 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5078 new_x = x + single_glyph_width;
5080 /* We want to leave anything reaching TO_X to the caller. */
5081 if ((op & MOVE_TO_X) && new_x > to_x)
5083 it->current_x = x;
5084 result = MOVE_X_REACHED;
5085 break;
5087 else if (/* Lines are continued. */
5088 !it->truncate_lines_p
5089 && (/* And glyph doesn't fit on the line. */
5090 new_x > it->last_visible_x
5091 /* Or it fits exactly and we're on a window
5092 system frame. */
5093 || (new_x == it->last_visible_x
5094 && FRAME_WINDOW_P (it->f))))
5096 if (/* IT->hpos == 0 means the very first glyph
5097 doesn't fit on the line, e.g. a wide image. */
5098 it->hpos == 0
5099 || (new_x == it->last_visible_x
5100 && FRAME_WINDOW_P (it->f)))
5102 ++it->hpos;
5103 it->current_x = new_x;
5104 if (i == it->nglyphs - 1)
5105 set_iterator_to_next (it, 1);
5107 else
5109 it->current_x = x;
5110 it->max_ascent = ascent;
5111 it->max_descent = descent;
5114 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5115 IT_CHARPOS (*it)));
5116 result = MOVE_LINE_CONTINUED;
5117 break;
5119 else if (new_x > it->first_visible_x)
5121 /* Glyph is visible. Increment number of glyphs that
5122 would be displayed. */
5123 ++it->hpos;
5125 else
5127 /* Glyph is completely off the left margin of the display
5128 area. Nothing to do. */
5132 if (result != MOVE_UNDEFINED)
5133 break;
5135 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5137 /* Stop when TO_X specified and reached. This check is
5138 necessary here because of lines consisting of a line end,
5139 only. The line end will not produce any glyphs and we
5140 would never get MOVE_X_REACHED. */
5141 xassert (it->nglyphs == 0);
5142 result = MOVE_X_REACHED;
5143 break;
5146 /* Is this a line end? If yes, we're done. */
5147 if (ITERATOR_AT_END_OF_LINE_P (it))
5149 result = MOVE_NEWLINE_OR_CR;
5150 break;
5153 /* The current display element has been consumed. Advance
5154 to the next. */
5155 set_iterator_to_next (it, 1);
5157 /* Stop if lines are truncated and IT's current x-position is
5158 past the right edge of the window now. */
5159 if (it->truncate_lines_p
5160 && it->current_x >= it->last_visible_x)
5162 result = MOVE_LINE_TRUNCATED;
5163 break;
5167 /* Restore the iterator settings altered at the beginning of this
5168 function. */
5169 it->glyph_row = saved_glyph_row;
5170 return result;
5174 /* Move IT forward until it satisfies one or more of the criteria in
5175 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5177 OP is a bit-mask that specifies where to stop, and in particular,
5178 which of those four position arguments makes a difference. See the
5179 description of enum move_operation_enum.
5181 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5182 screen line, this function will set IT to the next position >
5183 TO_CHARPOS. */
5185 void
5186 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5187 struct it *it;
5188 int to_charpos, to_x, to_y, to_vpos;
5189 int op;
5191 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5192 int line_height;
5193 int reached = 0;
5195 for (;;)
5197 if (op & MOVE_TO_VPOS)
5199 /* If no TO_CHARPOS and no TO_X specified, stop at the
5200 start of the line TO_VPOS. */
5201 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5203 if (it->vpos == to_vpos)
5205 reached = 1;
5206 break;
5208 else
5209 skip = move_it_in_display_line_to (it, -1, -1, 0);
5211 else
5213 /* TO_VPOS >= 0 means stop at TO_X in the line at
5214 TO_VPOS, or at TO_POS, whichever comes first. */
5215 if (it->vpos == to_vpos)
5217 reached = 2;
5218 break;
5221 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5223 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5225 reached = 3;
5226 break;
5228 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5230 /* We have reached TO_X but not in the line we want. */
5231 skip = move_it_in_display_line_to (it, to_charpos,
5232 -1, MOVE_TO_POS);
5233 if (skip == MOVE_POS_MATCH_OR_ZV)
5235 reached = 4;
5236 break;
5241 else if (op & MOVE_TO_Y)
5243 struct it it_backup;
5245 /* TO_Y specified means stop at TO_X in the line containing
5246 TO_Y---or at TO_CHARPOS if this is reached first. The
5247 problem is that we can't really tell whether the line
5248 contains TO_Y before we have completely scanned it, and
5249 this may skip past TO_X. What we do is to first scan to
5250 TO_X.
5252 If TO_X is not specified, use a TO_X of zero. The reason
5253 is to make the outcome of this function more predictable.
5254 If we didn't use TO_X == 0, we would stop at the end of
5255 the line which is probably not what a caller would expect
5256 to happen. */
5257 skip = move_it_in_display_line_to (it, to_charpos,
5258 ((op & MOVE_TO_X)
5259 ? to_x : 0),
5260 (MOVE_TO_X
5261 | (op & MOVE_TO_POS)));
5263 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5264 if (skip == MOVE_POS_MATCH_OR_ZV)
5266 reached = 5;
5267 break;
5270 /* If TO_X was reached, we would like to know whether TO_Y
5271 is in the line. This can only be said if we know the
5272 total line height which requires us to scan the rest of
5273 the line. */
5274 if (skip == MOVE_X_REACHED)
5276 it_backup = *it;
5277 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5278 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5279 op & MOVE_TO_POS);
5280 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5283 /* Now, decide whether TO_Y is in this line. */
5284 line_height = it->max_ascent + it->max_descent;
5285 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5287 if (to_y >= it->current_y
5288 && to_y < it->current_y + line_height)
5290 if (skip == MOVE_X_REACHED)
5291 /* If TO_Y is in this line and TO_X was reached above,
5292 we scanned too far. We have to restore IT's settings
5293 to the ones before skipping. */
5294 *it = it_backup;
5295 reached = 6;
5297 else if (skip == MOVE_X_REACHED)
5299 skip = skip2;
5300 if (skip == MOVE_POS_MATCH_OR_ZV)
5301 reached = 7;
5304 if (reached)
5305 break;
5307 else
5308 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5310 switch (skip)
5312 case MOVE_POS_MATCH_OR_ZV:
5313 reached = 8;
5314 goto out;
5316 case MOVE_NEWLINE_OR_CR:
5317 set_iterator_to_next (it, 1);
5318 it->continuation_lines_width = 0;
5319 break;
5321 case MOVE_LINE_TRUNCATED:
5322 it->continuation_lines_width = 0;
5323 reseat_at_next_visible_line_start (it, 0);
5324 if ((op & MOVE_TO_POS) != 0
5325 && IT_CHARPOS (*it) > to_charpos)
5327 reached = 9;
5328 goto out;
5330 break;
5332 case MOVE_LINE_CONTINUED:
5333 it->continuation_lines_width += it->current_x;
5334 break;
5336 default:
5337 abort ();
5340 /* Reset/increment for the next run. */
5341 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5342 it->current_x = it->hpos = 0;
5343 it->current_y += it->max_ascent + it->max_descent;
5344 ++it->vpos;
5345 last_height = it->max_ascent + it->max_descent;
5346 last_max_ascent = it->max_ascent;
5347 it->max_ascent = it->max_descent = 0;
5350 out:
5352 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5356 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5358 If DY > 0, move IT backward at least that many pixels. DY = 0
5359 means move IT backward to the preceding line start or BEGV. This
5360 function may move over more than DY pixels if IT->current_y - DY
5361 ends up in the middle of a line; in this case IT->current_y will be
5362 set to the top of the line moved to. */
5364 void
5365 move_it_vertically_backward (it, dy)
5366 struct it *it;
5367 int dy;
5369 int nlines, h;
5370 struct it it2, it3;
5371 int start_pos = IT_CHARPOS (*it);
5373 xassert (dy >= 0);
5375 /* Estimate how many newlines we must move back. */
5376 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5378 /* Set the iterator's position that many lines back. */
5379 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5380 back_to_previous_visible_line_start (it);
5382 /* Reseat the iterator here. When moving backward, we don't want
5383 reseat to skip forward over invisible text, set up the iterator
5384 to deliver from overlay strings at the new position etc. So,
5385 use reseat_1 here. */
5386 reseat_1 (it, it->current.pos, 1);
5388 /* We are now surely at a line start. */
5389 it->current_x = it->hpos = 0;
5391 /* Move forward and see what y-distance we moved. First move to the
5392 start of the next line so that we get its height. We need this
5393 height to be able to tell whether we reached the specified
5394 y-distance. */
5395 it2 = *it;
5396 it2.max_ascent = it2.max_descent = 0;
5397 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5398 MOVE_TO_POS | MOVE_TO_VPOS);
5399 xassert (IT_CHARPOS (*it) >= BEGV);
5400 it3 = it2;
5402 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5403 xassert (IT_CHARPOS (*it) >= BEGV);
5404 h = it2.current_y - it->current_y;
5405 nlines = it2.vpos - it->vpos;
5407 /* Correct IT's y and vpos position. */
5408 it->vpos -= nlines;
5409 it->current_y -= h;
5411 if (dy == 0)
5413 /* DY == 0 means move to the start of the screen line. The
5414 value of nlines is > 0 if continuation lines were involved. */
5415 if (nlines > 0)
5416 move_it_by_lines (it, nlines, 1);
5417 xassert (IT_CHARPOS (*it) <= start_pos);
5419 else if (nlines)
5421 /* The y-position we try to reach. Note that h has been
5422 subtracted in front of the if-statement. */
5423 int target_y = it->current_y + h - dy;
5424 int y0 = it3.current_y;
5425 int y1 = line_bottom_y (&it3);
5426 int line_height = y1 - y0;
5428 /* If we did not reach target_y, try to move further backward if
5429 we can. If we moved too far backward, try to move forward. */
5430 if (target_y < it->current_y
5431 /* This is heuristic. In a window that's 3 lines high, with
5432 a line height of 13 pixels each, recentering with point
5433 on the bottom line will try to move -39/2 = 19 pixels
5434 backward. Try to avoid moving into the first line. */
5435 && it->current_y - target_y > line_height / 3 * 2
5436 && IT_CHARPOS (*it) > BEGV)
5438 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5439 target_y - it->current_y));
5440 move_it_vertically (it, target_y - it->current_y);
5441 xassert (IT_CHARPOS (*it) >= BEGV);
5443 else if (target_y >= it->current_y + line_height
5444 && IT_CHARPOS (*it) < ZV)
5446 /* Should move forward by at least one line, maybe more.
5448 Note: Calling move_it_by_lines can be expensive on
5449 terminal frames, where compute_motion is used (via
5450 vmotion) to do the job, when there are very long lines
5451 and truncate-lines is nil. That's the reason for
5452 treating terminal frames specially here. */
5454 if (!FRAME_WINDOW_P (it->f))
5455 move_it_vertically (it, target_y - (it->current_y + line_height));
5456 else
5460 move_it_by_lines (it, 1, 1);
5462 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5465 xassert (IT_CHARPOS (*it) >= BEGV);
5471 /* Move IT by a specified amount of pixel lines DY. DY negative means
5472 move backwards. DY = 0 means move to start of screen line. At the
5473 end, IT will be on the start of a screen line. */
5475 void
5476 move_it_vertically (it, dy)
5477 struct it *it;
5478 int dy;
5480 if (dy <= 0)
5481 move_it_vertically_backward (it, -dy);
5482 else if (dy > 0)
5484 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5485 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5486 MOVE_TO_POS | MOVE_TO_Y);
5487 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5489 /* If buffer ends in ZV without a newline, move to the start of
5490 the line to satisfy the post-condition. */
5491 if (IT_CHARPOS (*it) == ZV
5492 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5493 move_it_by_lines (it, 0, 0);
5498 /* Move iterator IT past the end of the text line it is in. */
5500 void
5501 move_it_past_eol (it)
5502 struct it *it;
5504 enum move_it_result rc;
5506 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5507 if (rc == MOVE_NEWLINE_OR_CR)
5508 set_iterator_to_next (it, 0);
5512 #if 0 /* Currently not used. */
5514 /* Return non-zero if some text between buffer positions START_CHARPOS
5515 and END_CHARPOS is invisible. IT->window is the window for text
5516 property lookup. */
5518 static int
5519 invisible_text_between_p (it, start_charpos, end_charpos)
5520 struct it *it;
5521 int start_charpos, end_charpos;
5523 Lisp_Object prop, limit;
5524 int invisible_found_p;
5526 xassert (it != NULL && start_charpos <= end_charpos);
5528 /* Is text at START invisible? */
5529 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5530 it->window);
5531 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5532 invisible_found_p = 1;
5533 else
5535 limit = Fnext_single_char_property_change (make_number (start_charpos),
5536 Qinvisible, Qnil,
5537 make_number (end_charpos));
5538 invisible_found_p = XFASTINT (limit) < end_charpos;
5541 return invisible_found_p;
5544 #endif /* 0 */
5547 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5548 negative means move up. DVPOS == 0 means move to the start of the
5549 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5550 NEED_Y_P is zero, IT->current_y will be left unchanged.
5552 Further optimization ideas: If we would know that IT->f doesn't use
5553 a face with proportional font, we could be faster for
5554 truncate-lines nil. */
5556 void
5557 move_it_by_lines (it, dvpos, need_y_p)
5558 struct it *it;
5559 int dvpos, need_y_p;
5561 struct position pos;
5563 if (!FRAME_WINDOW_P (it->f))
5565 struct text_pos textpos;
5567 /* We can use vmotion on frames without proportional fonts. */
5568 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5569 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5570 reseat (it, textpos, 1);
5571 it->vpos += pos.vpos;
5572 it->current_y += pos.vpos;
5574 else if (dvpos == 0)
5576 /* DVPOS == 0 means move to the start of the screen line. */
5577 move_it_vertically_backward (it, 0);
5578 xassert (it->current_x == 0 && it->hpos == 0);
5580 else if (dvpos > 0)
5581 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5582 else
5584 struct it it2;
5585 int start_charpos, i;
5587 /* Start at the beginning of the screen line containing IT's
5588 position. */
5589 move_it_vertically_backward (it, 0);
5591 /* Go back -DVPOS visible lines and reseat the iterator there. */
5592 start_charpos = IT_CHARPOS (*it);
5593 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5594 back_to_previous_visible_line_start (it);
5595 reseat (it, it->current.pos, 1);
5596 it->current_x = it->hpos = 0;
5598 /* Above call may have moved too far if continuation lines
5599 are involved. Scan forward and see if it did. */
5600 it2 = *it;
5601 it2.vpos = it2.current_y = 0;
5602 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5603 it->vpos -= it2.vpos;
5604 it->current_y -= it2.current_y;
5605 it->current_x = it->hpos = 0;
5607 /* If we moved too far, move IT some lines forward. */
5608 if (it2.vpos > -dvpos)
5610 int delta = it2.vpos + dvpos;
5611 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5618 /***********************************************************************
5619 Messages
5620 ***********************************************************************/
5623 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5624 to *Messages*. */
5626 void
5627 add_to_log (format, arg1, arg2)
5628 char *format;
5629 Lisp_Object arg1, arg2;
5631 Lisp_Object args[3];
5632 Lisp_Object msg, fmt;
5633 char *buffer;
5634 int len;
5635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5637 /* Do nothing if called asynchronously. Inserting text into
5638 a buffer may call after-change-functions and alike and
5639 that would means running Lisp asynchronously. */
5640 if (handling_signal)
5641 return;
5643 fmt = msg = Qnil;
5644 GCPRO4 (fmt, msg, arg1, arg2);
5646 args[0] = fmt = build_string (format);
5647 args[1] = arg1;
5648 args[2] = arg2;
5649 msg = Fformat (3, args);
5651 len = SBYTES (msg) + 1;
5652 buffer = (char *) alloca (len);
5653 bcopy (SDATA (msg), buffer, len);
5655 message_dolog (buffer, len - 1, 1, 0);
5656 UNGCPRO;
5660 /* Output a newline in the *Messages* buffer if "needs" one. */
5662 void
5663 message_log_maybe_newline ()
5665 if (message_log_need_newline)
5666 message_dolog ("", 0, 1, 0);
5670 /* Add a string M of length NBYTES to the message log, optionally
5671 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5672 nonzero, means interpret the contents of M as multibyte. This
5673 function calls low-level routines in order to bypass text property
5674 hooks, etc. which might not be safe to run. */
5676 void
5677 message_dolog (m, nbytes, nlflag, multibyte)
5678 const char *m;
5679 int nbytes, nlflag, multibyte;
5681 if (!NILP (Vmemory_full))
5682 return;
5684 if (!NILP (Vmessage_log_max))
5686 struct buffer *oldbuf;
5687 Lisp_Object oldpoint, oldbegv, oldzv;
5688 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5689 int point_at_end = 0;
5690 int zv_at_end = 0;
5691 Lisp_Object old_deactivate_mark, tem;
5692 struct gcpro gcpro1;
5694 old_deactivate_mark = Vdeactivate_mark;
5695 oldbuf = current_buffer;
5696 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5697 current_buffer->undo_list = Qt;
5699 oldpoint = message_dolog_marker1;
5700 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5701 oldbegv = message_dolog_marker2;
5702 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5703 oldzv = message_dolog_marker3;
5704 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5705 GCPRO1 (old_deactivate_mark);
5707 if (PT == Z)
5708 point_at_end = 1;
5709 if (ZV == Z)
5710 zv_at_end = 1;
5712 BEGV = BEG;
5713 BEGV_BYTE = BEG_BYTE;
5714 ZV = Z;
5715 ZV_BYTE = Z_BYTE;
5716 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5718 /* Insert the string--maybe converting multibyte to single byte
5719 or vice versa, so that all the text fits the buffer. */
5720 if (multibyte
5721 && NILP (current_buffer->enable_multibyte_characters))
5723 int i, c, char_bytes;
5724 unsigned char work[1];
5726 /* Convert a multibyte string to single-byte
5727 for the *Message* buffer. */
5728 for (i = 0; i < nbytes; i += nbytes)
5730 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5731 work[0] = (SINGLE_BYTE_CHAR_P (c)
5733 : multibyte_char_to_unibyte (c, Qnil));
5734 insert_1_both (work, 1, 1, 1, 0, 0);
5737 else if (! multibyte
5738 && ! NILP (current_buffer->enable_multibyte_characters))
5740 int i, c, char_bytes;
5741 unsigned char *msg = (unsigned char *) m;
5742 unsigned char str[MAX_MULTIBYTE_LENGTH];
5743 /* Convert a single-byte string to multibyte
5744 for the *Message* buffer. */
5745 for (i = 0; i < nbytes; i++)
5747 c = unibyte_char_to_multibyte (msg[i]);
5748 char_bytes = CHAR_STRING (c, str);
5749 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5752 else if (nbytes)
5753 insert_1 (m, nbytes, 1, 0, 0);
5755 if (nlflag)
5757 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5758 insert_1 ("\n", 1, 1, 0, 0);
5760 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5761 this_bol = PT;
5762 this_bol_byte = PT_BYTE;
5764 /* See if this line duplicates the previous one.
5765 If so, combine duplicates. */
5766 if (this_bol > BEG)
5768 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5769 prev_bol = PT;
5770 prev_bol_byte = PT_BYTE;
5772 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5773 this_bol, this_bol_byte);
5774 if (dup)
5776 del_range_both (prev_bol, prev_bol_byte,
5777 this_bol, this_bol_byte, 0);
5778 if (dup > 1)
5780 char dupstr[40];
5781 int duplen;
5783 /* If you change this format, don't forget to also
5784 change message_log_check_duplicate. */
5785 sprintf (dupstr, " [%d times]", dup);
5786 duplen = strlen (dupstr);
5787 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5788 insert_1 (dupstr, duplen, 1, 0, 1);
5793 /* If we have more than the desired maximum number of lines
5794 in the *Messages* buffer now, delete the oldest ones.
5795 This is safe because we don't have undo in this buffer. */
5797 if (NATNUMP (Vmessage_log_max))
5799 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5800 -XFASTINT (Vmessage_log_max) - 1, 0);
5801 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5804 BEGV = XMARKER (oldbegv)->charpos;
5805 BEGV_BYTE = marker_byte_position (oldbegv);
5807 if (zv_at_end)
5809 ZV = Z;
5810 ZV_BYTE = Z_BYTE;
5812 else
5814 ZV = XMARKER (oldzv)->charpos;
5815 ZV_BYTE = marker_byte_position (oldzv);
5818 if (point_at_end)
5819 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5820 else
5821 /* We can't do Fgoto_char (oldpoint) because it will run some
5822 Lisp code. */
5823 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5824 XMARKER (oldpoint)->bytepos);
5826 UNGCPRO;
5827 unchain_marker (oldpoint);
5828 unchain_marker (oldbegv);
5829 unchain_marker (oldzv);
5831 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5832 set_buffer_internal (oldbuf);
5833 if (NILP (tem))
5834 windows_or_buffers_changed = old_windows_or_buffers_changed;
5835 message_log_need_newline = !nlflag;
5836 Vdeactivate_mark = old_deactivate_mark;
5841 /* We are at the end of the buffer after just having inserted a newline.
5842 (Note: We depend on the fact we won't be crossing the gap.)
5843 Check to see if the most recent message looks a lot like the previous one.
5844 Return 0 if different, 1 if the new one should just replace it, or a
5845 value N > 1 if we should also append " [N times]". */
5847 static int
5848 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5849 int prev_bol, this_bol;
5850 int prev_bol_byte, this_bol_byte;
5852 int i;
5853 int len = Z_BYTE - 1 - this_bol_byte;
5854 int seen_dots = 0;
5855 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5856 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5858 for (i = 0; i < len; i++)
5860 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5861 seen_dots = 1;
5862 if (p1[i] != p2[i])
5863 return seen_dots;
5865 p1 += len;
5866 if (*p1 == '\n')
5867 return 2;
5868 if (*p1++ == ' ' && *p1++ == '[')
5870 int n = 0;
5871 while (*p1 >= '0' && *p1 <= '9')
5872 n = n * 10 + *p1++ - '0';
5873 if (strncmp (p1, " times]\n", 8) == 0)
5874 return n+1;
5876 return 0;
5880 /* Display an echo area message M with a specified length of NBYTES
5881 bytes. The string may include null characters. If M is 0, clear
5882 out any existing message, and let the mini-buffer text show
5883 through.
5885 The buffer M must continue to exist until after the echo area gets
5886 cleared or some other message gets displayed there. This means do
5887 not pass text that is stored in a Lisp string; do not pass text in
5888 a buffer that was alloca'd. */
5890 void
5891 message2 (m, nbytes, multibyte)
5892 const char *m;
5893 int nbytes;
5894 int multibyte;
5896 /* First flush out any partial line written with print. */
5897 message_log_maybe_newline ();
5898 if (m)
5899 message_dolog (m, nbytes, 1, multibyte);
5900 message2_nolog (m, nbytes, multibyte);
5904 /* The non-logging counterpart of message2. */
5906 void
5907 message2_nolog (m, nbytes, multibyte)
5908 const char *m;
5909 int nbytes;
5911 struct frame *sf = SELECTED_FRAME ();
5912 message_enable_multibyte = multibyte;
5914 if (noninteractive)
5916 if (noninteractive_need_newline)
5917 putc ('\n', stderr);
5918 noninteractive_need_newline = 0;
5919 if (m)
5920 fwrite (m, nbytes, 1, stderr);
5921 if (cursor_in_echo_area == 0)
5922 fprintf (stderr, "\n");
5923 fflush (stderr);
5925 /* A null message buffer means that the frame hasn't really been
5926 initialized yet. Error messages get reported properly by
5927 cmd_error, so this must be just an informative message; toss it. */
5928 else if (INTERACTIVE
5929 && sf->glyphs_initialized_p
5930 && FRAME_MESSAGE_BUF (sf))
5932 Lisp_Object mini_window;
5933 struct frame *f;
5935 /* Get the frame containing the mini-buffer
5936 that the selected frame is using. */
5937 mini_window = FRAME_MINIBUF_WINDOW (sf);
5938 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5940 FRAME_SAMPLE_VISIBILITY (f);
5941 if (FRAME_VISIBLE_P (sf)
5942 && ! FRAME_VISIBLE_P (f))
5943 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5945 if (m)
5947 set_message (m, Qnil, nbytes, multibyte);
5948 if (minibuffer_auto_raise)
5949 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5951 else
5952 clear_message (1, 1);
5954 do_pending_window_change (0);
5955 echo_area_display (1);
5956 do_pending_window_change (0);
5957 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5958 (*frame_up_to_date_hook) (f);
5963 /* Display an echo area message M with a specified length of NBYTES
5964 bytes. The string may include null characters. If M is not a
5965 string, clear out any existing message, and let the mini-buffer
5966 text show through. */
5968 void
5969 message3 (m, nbytes, multibyte)
5970 Lisp_Object m;
5971 int nbytes;
5972 int multibyte;
5974 struct gcpro gcpro1;
5976 GCPRO1 (m);
5978 /* First flush out any partial line written with print. */
5979 message_log_maybe_newline ();
5980 if (STRINGP (m))
5981 message_dolog (SDATA (m), nbytes, 1, multibyte);
5982 message3_nolog (m, nbytes, multibyte);
5984 UNGCPRO;
5988 /* The non-logging version of message3. */
5990 void
5991 message3_nolog (m, nbytes, multibyte)
5992 Lisp_Object m;
5993 int nbytes, multibyte;
5995 struct frame *sf = SELECTED_FRAME ();
5996 message_enable_multibyte = multibyte;
5998 if (noninteractive)
6000 if (noninteractive_need_newline)
6001 putc ('\n', stderr);
6002 noninteractive_need_newline = 0;
6003 if (STRINGP (m))
6004 fwrite (SDATA (m), nbytes, 1, stderr);
6005 if (cursor_in_echo_area == 0)
6006 fprintf (stderr, "\n");
6007 fflush (stderr);
6009 /* A null message buffer means that the frame hasn't really been
6010 initialized yet. Error messages get reported properly by
6011 cmd_error, so this must be just an informative message; toss it. */
6012 else if (INTERACTIVE
6013 && sf->glyphs_initialized_p
6014 && FRAME_MESSAGE_BUF (sf))
6016 Lisp_Object mini_window;
6017 Lisp_Object frame;
6018 struct frame *f;
6020 /* Get the frame containing the mini-buffer
6021 that the selected frame is using. */
6022 mini_window = FRAME_MINIBUF_WINDOW (sf);
6023 frame = XWINDOW (mini_window)->frame;
6024 f = XFRAME (frame);
6026 FRAME_SAMPLE_VISIBILITY (f);
6027 if (FRAME_VISIBLE_P (sf)
6028 && !FRAME_VISIBLE_P (f))
6029 Fmake_frame_visible (frame);
6031 if (STRINGP (m) && SCHARS (m) > 0)
6033 set_message (NULL, m, nbytes, multibyte);
6034 if (minibuffer_auto_raise)
6035 Fraise_frame (frame);
6037 else
6038 clear_message (1, 1);
6040 do_pending_window_change (0);
6041 echo_area_display (1);
6042 do_pending_window_change (0);
6043 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6044 (*frame_up_to_date_hook) (f);
6049 /* Display a null-terminated echo area message M. If M is 0, clear
6050 out any existing message, and let the mini-buffer text show through.
6052 The buffer M must continue to exist until after the echo area gets
6053 cleared or some other message gets displayed there. Do not pass
6054 text that is stored in a Lisp string. Do not pass text in a buffer
6055 that was alloca'd. */
6057 void
6058 message1 (m)
6059 char *m;
6061 message2 (m, (m ? strlen (m) : 0), 0);
6065 /* The non-logging counterpart of message1. */
6067 void
6068 message1_nolog (m)
6069 char *m;
6071 message2_nolog (m, (m ? strlen (m) : 0), 0);
6074 /* Display a message M which contains a single %s
6075 which gets replaced with STRING. */
6077 void
6078 message_with_string (m, string, log)
6079 char *m;
6080 Lisp_Object string;
6081 int log;
6083 CHECK_STRING (string);
6085 if (noninteractive)
6087 if (m)
6089 if (noninteractive_need_newline)
6090 putc ('\n', stderr);
6091 noninteractive_need_newline = 0;
6092 fprintf (stderr, m, SDATA (string));
6093 if (cursor_in_echo_area == 0)
6094 fprintf (stderr, "\n");
6095 fflush (stderr);
6098 else if (INTERACTIVE)
6100 /* The frame whose minibuffer we're going to display the message on.
6101 It may be larger than the selected frame, so we need
6102 to use its buffer, not the selected frame's buffer. */
6103 Lisp_Object mini_window;
6104 struct frame *f, *sf = SELECTED_FRAME ();
6106 /* Get the frame containing the minibuffer
6107 that the selected frame is using. */
6108 mini_window = FRAME_MINIBUF_WINDOW (sf);
6109 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6111 /* A null message buffer means that the frame hasn't really been
6112 initialized yet. Error messages get reported properly by
6113 cmd_error, so this must be just an informative message; toss it. */
6114 if (FRAME_MESSAGE_BUF (f))
6116 Lisp_Object args[2], message;
6117 struct gcpro gcpro1, gcpro2;
6119 args[0] = build_string (m);
6120 args[1] = message = string;
6121 GCPRO2 (args[0], message);
6122 gcpro1.nvars = 2;
6124 message = Fformat (2, args);
6126 if (log)
6127 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6128 else
6129 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6131 UNGCPRO;
6133 /* Print should start at the beginning of the message
6134 buffer next time. */
6135 message_buf_print = 0;
6141 /* Dump an informative message to the minibuf. If M is 0, clear out
6142 any existing message, and let the mini-buffer text show through. */
6144 /* VARARGS 1 */
6145 void
6146 message (m, a1, a2, a3)
6147 char *m;
6148 EMACS_INT a1, a2, a3;
6150 if (noninteractive)
6152 if (m)
6154 if (noninteractive_need_newline)
6155 putc ('\n', stderr);
6156 noninteractive_need_newline = 0;
6157 fprintf (stderr, m, a1, a2, a3);
6158 if (cursor_in_echo_area == 0)
6159 fprintf (stderr, "\n");
6160 fflush (stderr);
6163 else if (INTERACTIVE)
6165 /* The frame whose mini-buffer we're going to display the message
6166 on. It may be larger than the selected frame, so we need to
6167 use its buffer, not the selected frame's buffer. */
6168 Lisp_Object mini_window;
6169 struct frame *f, *sf = SELECTED_FRAME ();
6171 /* Get the frame containing the mini-buffer
6172 that the selected frame is using. */
6173 mini_window = FRAME_MINIBUF_WINDOW (sf);
6174 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6176 /* A null message buffer means that the frame hasn't really been
6177 initialized yet. Error messages get reported properly by
6178 cmd_error, so this must be just an informative message; toss
6179 it. */
6180 if (FRAME_MESSAGE_BUF (f))
6182 if (m)
6184 int len;
6185 #ifdef NO_ARG_ARRAY
6186 char *a[3];
6187 a[0] = (char *) a1;
6188 a[1] = (char *) a2;
6189 a[2] = (char *) a3;
6191 len = doprnt (FRAME_MESSAGE_BUF (f),
6192 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6193 #else
6194 len = doprnt (FRAME_MESSAGE_BUF (f),
6195 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6196 (char **) &a1);
6197 #endif /* NO_ARG_ARRAY */
6199 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6201 else
6202 message1 (0);
6204 /* Print should start at the beginning of the message
6205 buffer next time. */
6206 message_buf_print = 0;
6212 /* The non-logging version of message. */
6214 void
6215 message_nolog (m, a1, a2, a3)
6216 char *m;
6217 EMACS_INT a1, a2, a3;
6219 Lisp_Object old_log_max;
6220 old_log_max = Vmessage_log_max;
6221 Vmessage_log_max = Qnil;
6222 message (m, a1, a2, a3);
6223 Vmessage_log_max = old_log_max;
6227 /* Display the current message in the current mini-buffer. This is
6228 only called from error handlers in process.c, and is not time
6229 critical. */
6231 void
6232 update_echo_area ()
6234 if (!NILP (echo_area_buffer[0]))
6236 Lisp_Object string;
6237 string = Fcurrent_message ();
6238 message3 (string, SBYTES (string),
6239 !NILP (current_buffer->enable_multibyte_characters));
6244 /* Make sure echo area buffers in `echo_buffers' are live.
6245 If they aren't, make new ones. */
6247 static void
6248 ensure_echo_area_buffers ()
6250 int i;
6252 for (i = 0; i < 2; ++i)
6253 if (!BUFFERP (echo_buffer[i])
6254 || NILP (XBUFFER (echo_buffer[i])->name))
6256 char name[30];
6257 Lisp_Object old_buffer;
6258 int j;
6260 old_buffer = echo_buffer[i];
6261 sprintf (name, " *Echo Area %d*", i);
6262 echo_buffer[i] = Fget_buffer_create (build_string (name));
6263 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6265 for (j = 0; j < 2; ++j)
6266 if (EQ (old_buffer, echo_area_buffer[j]))
6267 echo_area_buffer[j] = echo_buffer[i];
6272 /* Call FN with args A1..A4 with either the current or last displayed
6273 echo_area_buffer as current buffer.
6275 WHICH zero means use the current message buffer
6276 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6277 from echo_buffer[] and clear it.
6279 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6280 suitable buffer from echo_buffer[] and clear it.
6282 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6283 that the current message becomes the last displayed one, make
6284 choose a suitable buffer for echo_area_buffer[0], and clear it.
6286 Value is what FN returns. */
6288 static int
6289 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6290 struct window *w;
6291 int which;
6292 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6293 EMACS_INT a1;
6294 Lisp_Object a2;
6295 EMACS_INT a3, a4;
6297 Lisp_Object buffer;
6298 int this_one, the_other, clear_buffer_p, rc;
6299 int count = SPECPDL_INDEX ();
6301 /* If buffers aren't live, make new ones. */
6302 ensure_echo_area_buffers ();
6304 clear_buffer_p = 0;
6306 if (which == 0)
6307 this_one = 0, the_other = 1;
6308 else if (which > 0)
6309 this_one = 1, the_other = 0;
6310 else
6312 this_one = 0, the_other = 1;
6313 clear_buffer_p = 1;
6315 /* We need a fresh one in case the current echo buffer equals
6316 the one containing the last displayed echo area message. */
6317 if (!NILP (echo_area_buffer[this_one])
6318 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6319 echo_area_buffer[this_one] = Qnil;
6322 /* Choose a suitable buffer from echo_buffer[] is we don't
6323 have one. */
6324 if (NILP (echo_area_buffer[this_one]))
6326 echo_area_buffer[this_one]
6327 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6328 ? echo_buffer[the_other]
6329 : echo_buffer[this_one]);
6330 clear_buffer_p = 1;
6333 buffer = echo_area_buffer[this_one];
6335 /* Don't get confused by reusing the buffer used for echoing
6336 for a different purpose. */
6337 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6338 cancel_echoing ();
6340 record_unwind_protect (unwind_with_echo_area_buffer,
6341 with_echo_area_buffer_unwind_data (w));
6343 /* Make the echo area buffer current. Note that for display
6344 purposes, it is not necessary that the displayed window's buffer
6345 == current_buffer, except for text property lookup. So, let's
6346 only set that buffer temporarily here without doing a full
6347 Fset_window_buffer. We must also change w->pointm, though,
6348 because otherwise an assertions in unshow_buffer fails, and Emacs
6349 aborts. */
6350 set_buffer_internal_1 (XBUFFER (buffer));
6351 if (w)
6353 w->buffer = buffer;
6354 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6357 current_buffer->undo_list = Qt;
6358 current_buffer->read_only = Qnil;
6359 specbind (Qinhibit_read_only, Qt);
6360 specbind (Qinhibit_modification_hooks, Qt);
6362 if (clear_buffer_p && Z > BEG)
6363 del_range (BEG, Z);
6365 xassert (BEGV >= BEG);
6366 xassert (ZV <= Z && ZV >= BEGV);
6368 rc = fn (a1, a2, a3, a4);
6370 xassert (BEGV >= BEG);
6371 xassert (ZV <= Z && ZV >= BEGV);
6373 unbind_to (count, Qnil);
6374 return rc;
6378 /* Save state that should be preserved around the call to the function
6379 FN called in with_echo_area_buffer. */
6381 static Lisp_Object
6382 with_echo_area_buffer_unwind_data (w)
6383 struct window *w;
6385 int i = 0;
6386 Lisp_Object vector;
6388 /* Reduce consing by keeping one vector in
6389 Vwith_echo_area_save_vector. */
6390 vector = Vwith_echo_area_save_vector;
6391 Vwith_echo_area_save_vector = Qnil;
6393 if (NILP (vector))
6394 vector = Fmake_vector (make_number (7), Qnil);
6396 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6397 AREF (vector, i) = Vdeactivate_mark, ++i;
6398 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6400 if (w)
6402 XSETWINDOW (AREF (vector, i), w); ++i;
6403 AREF (vector, i) = w->buffer; ++i;
6404 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6405 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6407 else
6409 int end = i + 4;
6410 for (; i < end; ++i)
6411 AREF (vector, i) = Qnil;
6414 xassert (i == ASIZE (vector));
6415 return vector;
6419 /* Restore global state from VECTOR which was created by
6420 with_echo_area_buffer_unwind_data. */
6422 static Lisp_Object
6423 unwind_with_echo_area_buffer (vector)
6424 Lisp_Object vector;
6426 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6427 Vdeactivate_mark = AREF (vector, 1);
6428 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6430 if (WINDOWP (AREF (vector, 3)))
6432 struct window *w;
6433 Lisp_Object buffer, charpos, bytepos;
6435 w = XWINDOW (AREF (vector, 3));
6436 buffer = AREF (vector, 4);
6437 charpos = AREF (vector, 5);
6438 bytepos = AREF (vector, 6);
6440 w->buffer = buffer;
6441 set_marker_both (w->pointm, buffer,
6442 XFASTINT (charpos), XFASTINT (bytepos));
6445 Vwith_echo_area_save_vector = vector;
6446 return Qnil;
6450 /* Set up the echo area for use by print functions. MULTIBYTE_P
6451 non-zero means we will print multibyte. */
6453 void
6454 setup_echo_area_for_printing (multibyte_p)
6455 int multibyte_p;
6457 ensure_echo_area_buffers ();
6459 if (!message_buf_print)
6461 /* A message has been output since the last time we printed.
6462 Choose a fresh echo area buffer. */
6463 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6464 echo_area_buffer[0] = echo_buffer[1];
6465 else
6466 echo_area_buffer[0] = echo_buffer[0];
6468 /* Switch to that buffer and clear it. */
6469 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6470 current_buffer->truncate_lines = Qnil;
6472 if (Z > BEG)
6474 int count = SPECPDL_INDEX ();
6475 specbind (Qinhibit_read_only, Qt);
6476 /* Note that undo recording is always disabled. */
6477 del_range (BEG, Z);
6478 unbind_to (count, Qnil);
6480 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6482 /* Set up the buffer for the multibyteness we need. */
6483 if (multibyte_p
6484 != !NILP (current_buffer->enable_multibyte_characters))
6485 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6487 /* Raise the frame containing the echo area. */
6488 if (minibuffer_auto_raise)
6490 struct frame *sf = SELECTED_FRAME ();
6491 Lisp_Object mini_window;
6492 mini_window = FRAME_MINIBUF_WINDOW (sf);
6493 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6496 message_log_maybe_newline ();
6497 message_buf_print = 1;
6499 else
6501 if (NILP (echo_area_buffer[0]))
6503 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6504 echo_area_buffer[0] = echo_buffer[1];
6505 else
6506 echo_area_buffer[0] = echo_buffer[0];
6509 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6511 /* Someone switched buffers between print requests. */
6512 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6513 current_buffer->truncate_lines = Qnil;
6519 /* Display an echo area message in window W. Value is non-zero if W's
6520 height is changed. If display_last_displayed_message_p is
6521 non-zero, display the message that was last displayed, otherwise
6522 display the current message. */
6524 static int
6525 display_echo_area (w)
6526 struct window *w;
6528 int i, no_message_p, window_height_changed_p, count;
6530 /* Temporarily disable garbage collections while displaying the echo
6531 area. This is done because a GC can print a message itself.
6532 That message would modify the echo area buffer's contents while a
6533 redisplay of the buffer is going on, and seriously confuse
6534 redisplay. */
6535 count = inhibit_garbage_collection ();
6537 /* If there is no message, we must call display_echo_area_1
6538 nevertheless because it resizes the window. But we will have to
6539 reset the echo_area_buffer in question to nil at the end because
6540 with_echo_area_buffer will sets it to an empty buffer. */
6541 i = display_last_displayed_message_p ? 1 : 0;
6542 no_message_p = NILP (echo_area_buffer[i]);
6544 window_height_changed_p
6545 = with_echo_area_buffer (w, display_last_displayed_message_p,
6546 display_echo_area_1,
6547 (EMACS_INT) w, Qnil, 0, 0);
6549 if (no_message_p)
6550 echo_area_buffer[i] = Qnil;
6552 unbind_to (count, Qnil);
6553 return window_height_changed_p;
6557 /* Helper for display_echo_area. Display the current buffer which
6558 contains the current echo area message in window W, a mini-window,
6559 a pointer to which is passed in A1. A2..A4 are currently not used.
6560 Change the height of W so that all of the message is displayed.
6561 Value is non-zero if height of W was changed. */
6563 static int
6564 display_echo_area_1 (a1, a2, a3, a4)
6565 EMACS_INT a1;
6566 Lisp_Object a2;
6567 EMACS_INT a3, a4;
6569 struct window *w = (struct window *) a1;
6570 Lisp_Object window;
6571 struct text_pos start;
6572 int window_height_changed_p = 0;
6574 /* Do this before displaying, so that we have a large enough glyph
6575 matrix for the display. */
6576 window_height_changed_p = resize_mini_window (w, 0);
6578 /* Display. */
6579 clear_glyph_matrix (w->desired_matrix);
6580 XSETWINDOW (window, w);
6581 SET_TEXT_POS (start, BEG, BEG_BYTE);
6582 try_window (window, start);
6584 return window_height_changed_p;
6588 /* Resize the echo area window to exactly the size needed for the
6589 currently displayed message, if there is one. If a mini-buffer
6590 is active, don't shrink it. */
6592 void
6593 resize_echo_area_exactly ()
6595 if (BUFFERP (echo_area_buffer[0])
6596 && WINDOWP (echo_area_window))
6598 struct window *w = XWINDOW (echo_area_window);
6599 int resized_p;
6600 Lisp_Object resize_exactly;
6602 if (minibuf_level == 0)
6603 resize_exactly = Qt;
6604 else
6605 resize_exactly = Qnil;
6607 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6608 (EMACS_INT) w, resize_exactly, 0, 0);
6609 if (resized_p)
6611 ++windows_or_buffers_changed;
6612 ++update_mode_lines;
6613 redisplay_internal (0);
6619 /* Callback function for with_echo_area_buffer, when used from
6620 resize_echo_area_exactly. A1 contains a pointer to the window to
6621 resize, EXACTLY non-nil means resize the mini-window exactly to the
6622 size of the text displayed. A3 and A4 are not used. Value is what
6623 resize_mini_window returns. */
6625 static int
6626 resize_mini_window_1 (a1, exactly, a3, a4)
6627 EMACS_INT a1;
6628 Lisp_Object exactly;
6629 EMACS_INT a3, a4;
6631 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6635 /* Resize mini-window W to fit the size of its contents. EXACT:P
6636 means size the window exactly to the size needed. Otherwise, it's
6637 only enlarged until W's buffer is empty. Value is non-zero if
6638 the window height has been changed. */
6641 resize_mini_window (w, exact_p)
6642 struct window *w;
6643 int exact_p;
6645 struct frame *f = XFRAME (w->frame);
6646 int window_height_changed_p = 0;
6648 xassert (MINI_WINDOW_P (w));
6650 /* Don't resize windows while redisplaying a window; it would
6651 confuse redisplay functions when the size of the window they are
6652 displaying changes from under them. Such a resizing can happen,
6653 for instance, when which-func prints a long message while
6654 we are running fontification-functions. We're running these
6655 functions with safe_call which binds inhibit-redisplay to t. */
6656 if (!NILP (Vinhibit_redisplay))
6657 return 0;
6659 /* Nil means don't try to resize. */
6660 if (NILP (Vresize_mini_windows)
6661 || (FRAME_X_P (f) && f->output_data.x == NULL))
6662 return 0;
6664 if (!FRAME_MINIBUF_ONLY_P (f))
6666 struct it it;
6667 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6668 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6669 int height, max_height;
6670 int unit = CANON_Y_UNIT (f);
6671 struct text_pos start;
6672 struct buffer *old_current_buffer = NULL;
6674 if (current_buffer != XBUFFER (w->buffer))
6676 old_current_buffer = current_buffer;
6677 set_buffer_internal (XBUFFER (w->buffer));
6680 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6682 /* Compute the max. number of lines specified by the user. */
6683 if (FLOATP (Vmax_mini_window_height))
6684 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6685 else if (INTEGERP (Vmax_mini_window_height))
6686 max_height = XINT (Vmax_mini_window_height);
6687 else
6688 max_height = total_height / 4;
6690 /* Correct that max. height if it's bogus. */
6691 max_height = max (1, max_height);
6692 max_height = min (total_height, max_height);
6694 /* Find out the height of the text in the window. */
6695 if (it.truncate_lines_p)
6696 height = 1;
6697 else
6699 last_height = 0;
6700 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6701 if (it.max_ascent == 0 && it.max_descent == 0)
6702 height = it.current_y + last_height;
6703 else
6704 height = it.current_y + it.max_ascent + it.max_descent;
6705 height -= it.extra_line_spacing;
6706 height = (height + unit - 1) / unit;
6709 /* Compute a suitable window start. */
6710 if (height > max_height)
6712 height = max_height;
6713 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6714 move_it_vertically_backward (&it, (height - 1) * unit);
6715 start = it.current.pos;
6717 else
6718 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6719 SET_MARKER_FROM_TEXT_POS (w->start, start);
6721 if (EQ (Vresize_mini_windows, Qgrow_only))
6723 /* Let it grow only, until we display an empty message, in which
6724 case the window shrinks again. */
6725 if (height > XFASTINT (w->height))
6727 int old_height = XFASTINT (w->height);
6728 freeze_window_starts (f, 1);
6729 grow_mini_window (w, height - XFASTINT (w->height));
6730 window_height_changed_p = XFASTINT (w->height) != old_height;
6732 else if (height < XFASTINT (w->height)
6733 && (exact_p || BEGV == ZV))
6735 int old_height = XFASTINT (w->height);
6736 freeze_window_starts (f, 0);
6737 shrink_mini_window (w);
6738 window_height_changed_p = XFASTINT (w->height) != old_height;
6741 else
6743 /* Always resize to exact size needed. */
6744 if (height > XFASTINT (w->height))
6746 int old_height = XFASTINT (w->height);
6747 freeze_window_starts (f, 1);
6748 grow_mini_window (w, height - XFASTINT (w->height));
6749 window_height_changed_p = XFASTINT (w->height) != old_height;
6751 else if (height < XFASTINT (w->height))
6753 int old_height = XFASTINT (w->height);
6754 freeze_window_starts (f, 0);
6755 shrink_mini_window (w);
6757 if (height)
6759 freeze_window_starts (f, 1);
6760 grow_mini_window (w, height - XFASTINT (w->height));
6763 window_height_changed_p = XFASTINT (w->height) != old_height;
6767 if (old_current_buffer)
6768 set_buffer_internal (old_current_buffer);
6771 return window_height_changed_p;
6775 /* Value is the current message, a string, or nil if there is no
6776 current message. */
6778 Lisp_Object
6779 current_message ()
6781 Lisp_Object msg;
6783 if (NILP (echo_area_buffer[0]))
6784 msg = Qnil;
6785 else
6787 with_echo_area_buffer (0, 0, current_message_1,
6788 (EMACS_INT) &msg, Qnil, 0, 0);
6789 if (NILP (msg))
6790 echo_area_buffer[0] = Qnil;
6793 return msg;
6797 static int
6798 current_message_1 (a1, a2, a3, a4)
6799 EMACS_INT a1;
6800 Lisp_Object a2;
6801 EMACS_INT a3, a4;
6803 Lisp_Object *msg = (Lisp_Object *) a1;
6805 if (Z > BEG)
6806 *msg = make_buffer_string (BEG, Z, 1);
6807 else
6808 *msg = Qnil;
6809 return 0;
6813 /* Push the current message on Vmessage_stack for later restauration
6814 by restore_message. Value is non-zero if the current message isn't
6815 empty. This is a relatively infrequent operation, so it's not
6816 worth optimizing. */
6819 push_message ()
6821 Lisp_Object msg;
6822 msg = current_message ();
6823 Vmessage_stack = Fcons (msg, Vmessage_stack);
6824 return STRINGP (msg);
6828 /* Handler for record_unwind_protect calling pop_message. */
6830 Lisp_Object
6831 push_message_unwind (dummy)
6832 Lisp_Object dummy;
6834 pop_message ();
6835 return Qnil;
6839 /* Restore message display from the top of Vmessage_stack. */
6841 void
6842 restore_message ()
6844 Lisp_Object msg;
6846 xassert (CONSP (Vmessage_stack));
6847 msg = XCAR (Vmessage_stack);
6848 if (STRINGP (msg))
6849 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6850 else
6851 message3_nolog (msg, 0, 0);
6855 /* Pop the top-most entry off Vmessage_stack. */
6857 void
6858 pop_message ()
6860 xassert (CONSP (Vmessage_stack));
6861 Vmessage_stack = XCDR (Vmessage_stack);
6865 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6866 exits. If the stack is not empty, we have a missing pop_message
6867 somewhere. */
6869 void
6870 check_message_stack ()
6872 if (!NILP (Vmessage_stack))
6873 abort ();
6877 /* Truncate to NCHARS what will be displayed in the echo area the next
6878 time we display it---but don't redisplay it now. */
6880 void
6881 truncate_echo_area (nchars)
6882 int nchars;
6884 if (nchars == 0)
6885 echo_area_buffer[0] = Qnil;
6886 /* A null message buffer means that the frame hasn't really been
6887 initialized yet. Error messages get reported properly by
6888 cmd_error, so this must be just an informative message; toss it. */
6889 else if (!noninteractive
6890 && INTERACTIVE
6891 && !NILP (echo_area_buffer[0]))
6893 struct frame *sf = SELECTED_FRAME ();
6894 if (FRAME_MESSAGE_BUF (sf))
6895 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6900 /* Helper function for truncate_echo_area. Truncate the current
6901 message to at most NCHARS characters. */
6903 static int
6904 truncate_message_1 (nchars, a2, a3, a4)
6905 EMACS_INT nchars;
6906 Lisp_Object a2;
6907 EMACS_INT a3, a4;
6909 if (BEG + nchars < Z)
6910 del_range (BEG + nchars, Z);
6911 if (Z == BEG)
6912 echo_area_buffer[0] = Qnil;
6913 return 0;
6917 /* Set the current message to a substring of S or STRING.
6919 If STRING is a Lisp string, set the message to the first NBYTES
6920 bytes from STRING. NBYTES zero means use the whole string. If
6921 STRING is multibyte, the message will be displayed multibyte.
6923 If S is not null, set the message to the first LEN bytes of S. LEN
6924 zero means use the whole string. MULTIBYTE_P non-zero means S is
6925 multibyte. Display the message multibyte in that case. */
6927 void
6928 set_message (s, string, nbytes, multibyte_p)
6929 const char *s;
6930 Lisp_Object string;
6931 int nbytes;
6933 message_enable_multibyte
6934 = ((s && multibyte_p)
6935 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6937 with_echo_area_buffer (0, -1, set_message_1,
6938 (EMACS_INT) s, string, nbytes, multibyte_p);
6939 message_buf_print = 0;
6940 help_echo_showing_p = 0;
6944 /* Helper function for set_message. Arguments have the same meaning
6945 as there, with A1 corresponding to S and A2 corresponding to STRING
6946 This function is called with the echo area buffer being
6947 current. */
6949 static int
6950 set_message_1 (a1, a2, nbytes, multibyte_p)
6951 EMACS_INT a1;
6952 Lisp_Object a2;
6953 EMACS_INT nbytes, multibyte_p;
6955 const char *s = (const char *) a1;
6956 Lisp_Object string = a2;
6958 xassert (BEG == Z);
6960 /* Change multibyteness of the echo buffer appropriately. */
6961 if (message_enable_multibyte
6962 != !NILP (current_buffer->enable_multibyte_characters))
6963 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6965 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6967 /* Insert new message at BEG. */
6968 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6970 if (STRINGP (string))
6972 int nchars;
6974 if (nbytes == 0)
6975 nbytes = SBYTES (string);
6976 nchars = string_byte_to_char (string, nbytes);
6978 /* This function takes care of single/multibyte conversion. We
6979 just have to ensure that the echo area buffer has the right
6980 setting of enable_multibyte_characters. */
6981 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6983 else if (s)
6985 if (nbytes == 0)
6986 nbytes = strlen (s);
6988 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6990 /* Convert from multi-byte to single-byte. */
6991 int i, c, n;
6992 unsigned char work[1];
6994 /* Convert a multibyte string to single-byte. */
6995 for (i = 0; i < nbytes; i += n)
6997 c = string_char_and_length (s + i, nbytes - i, &n);
6998 work[0] = (SINGLE_BYTE_CHAR_P (c)
7000 : multibyte_char_to_unibyte (c, Qnil));
7001 insert_1_both (work, 1, 1, 1, 0, 0);
7004 else if (!multibyte_p
7005 && !NILP (current_buffer->enable_multibyte_characters))
7007 /* Convert from single-byte to multi-byte. */
7008 int i, c, n;
7009 const unsigned char *msg = (const unsigned char *) s;
7010 unsigned char str[MAX_MULTIBYTE_LENGTH];
7012 /* Convert a single-byte string to multibyte. */
7013 for (i = 0; i < nbytes; i++)
7015 c = unibyte_char_to_multibyte (msg[i]);
7016 n = CHAR_STRING (c, str);
7017 insert_1_both (str, 1, n, 1, 0, 0);
7020 else
7021 insert_1 (s, nbytes, 1, 0, 0);
7024 return 0;
7028 /* Clear messages. CURRENT_P non-zero means clear the current
7029 message. LAST_DISPLAYED_P non-zero means clear the message
7030 last displayed. */
7032 void
7033 clear_message (current_p, last_displayed_p)
7034 int current_p, last_displayed_p;
7036 if (current_p)
7038 echo_area_buffer[0] = Qnil;
7039 message_cleared_p = 1;
7042 if (last_displayed_p)
7043 echo_area_buffer[1] = Qnil;
7045 message_buf_print = 0;
7048 /* Clear garbaged frames.
7050 This function is used where the old redisplay called
7051 redraw_garbaged_frames which in turn called redraw_frame which in
7052 turn called clear_frame. The call to clear_frame was a source of
7053 flickering. I believe a clear_frame is not necessary. It should
7054 suffice in the new redisplay to invalidate all current matrices,
7055 and ensure a complete redisplay of all windows. */
7057 static void
7058 clear_garbaged_frames ()
7060 if (frame_garbaged)
7062 Lisp_Object tail, frame;
7063 int changed_count = 0;
7065 FOR_EACH_FRAME (tail, frame)
7067 struct frame *f = XFRAME (frame);
7069 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7071 if (f->resized_p)
7072 Fredraw_frame (frame);
7073 clear_current_matrices (f);
7074 changed_count++;
7075 f->garbaged = 0;
7076 f->resized_p = 0;
7080 frame_garbaged = 0;
7081 if (changed_count)
7082 ++windows_or_buffers_changed;
7087 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7088 is non-zero update selected_frame. Value is non-zero if the
7089 mini-windows height has been changed. */
7091 static int
7092 echo_area_display (update_frame_p)
7093 int update_frame_p;
7095 Lisp_Object mini_window;
7096 struct window *w;
7097 struct frame *f;
7098 int window_height_changed_p = 0;
7099 struct frame *sf = SELECTED_FRAME ();
7101 mini_window = FRAME_MINIBUF_WINDOW (sf);
7102 w = XWINDOW (mini_window);
7103 f = XFRAME (WINDOW_FRAME (w));
7105 /* Don't display if frame is invisible or not yet initialized. */
7106 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7107 return 0;
7109 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7110 #ifndef MAC_OS8
7111 #ifdef HAVE_WINDOW_SYSTEM
7112 /* When Emacs starts, selected_frame may be a visible terminal
7113 frame, even if we run under a window system. If we let this
7114 through, a message would be displayed on the terminal. */
7115 if (EQ (selected_frame, Vterminal_frame)
7116 && !NILP (Vwindow_system))
7117 return 0;
7118 #endif /* HAVE_WINDOW_SYSTEM */
7119 #endif
7121 /* Redraw garbaged frames. */
7122 if (frame_garbaged)
7123 clear_garbaged_frames ();
7125 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7127 echo_area_window = mini_window;
7128 window_height_changed_p = display_echo_area (w);
7129 w->must_be_updated_p = 1;
7131 /* Update the display, unless called from redisplay_internal.
7132 Also don't update the screen during redisplay itself. The
7133 update will happen at the end of redisplay, and an update
7134 here could cause confusion. */
7135 if (update_frame_p && !redisplaying_p)
7137 int n = 0;
7139 /* If the display update has been interrupted by pending
7140 input, update mode lines in the frame. Due to the
7141 pending input, it might have been that redisplay hasn't
7142 been called, so that mode lines above the echo area are
7143 garbaged. This looks odd, so we prevent it here. */
7144 if (!display_completed)
7145 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7147 if (window_height_changed_p
7148 /* Don't do this if Emacs is shutting down. Redisplay
7149 needs to run hooks. */
7150 && !NILP (Vrun_hooks))
7152 /* Must update other windows. Likewise as in other
7153 cases, don't let this update be interrupted by
7154 pending input. */
7155 int count = SPECPDL_INDEX ();
7156 specbind (Qredisplay_dont_pause, Qt);
7157 windows_or_buffers_changed = 1;
7158 redisplay_internal (0);
7159 unbind_to (count, Qnil);
7161 else if (FRAME_WINDOW_P (f) && n == 0)
7163 /* Window configuration is the same as before.
7164 Can do with a display update of the echo area,
7165 unless we displayed some mode lines. */
7166 update_single_window (w, 1);
7167 rif->flush_display (f);
7169 else
7170 update_frame (f, 1, 1);
7172 /* If cursor is in the echo area, make sure that the next
7173 redisplay displays the minibuffer, so that the cursor will
7174 be replaced with what the minibuffer wants. */
7175 if (cursor_in_echo_area)
7176 ++windows_or_buffers_changed;
7179 else if (!EQ (mini_window, selected_window))
7180 windows_or_buffers_changed++;
7182 /* Last displayed message is now the current message. */
7183 echo_area_buffer[1] = echo_area_buffer[0];
7185 /* Prevent redisplay optimization in redisplay_internal by resetting
7186 this_line_start_pos. This is done because the mini-buffer now
7187 displays the message instead of its buffer text. */
7188 if (EQ (mini_window, selected_window))
7189 CHARPOS (this_line_start_pos) = 0;
7191 return window_height_changed_p;
7196 /***********************************************************************
7197 Frame Titles
7198 ***********************************************************************/
7201 /* The frame title buffering code is also used by Fformat_mode_line.
7202 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7204 /* A buffer for constructing frame titles in it; allocated from the
7205 heap in init_xdisp and resized as needed in store_frame_title_char. */
7207 static char *frame_title_buf;
7209 /* The buffer's end, and a current output position in it. */
7211 static char *frame_title_buf_end;
7212 static char *frame_title_ptr;
7215 /* Store a single character C for the frame title in frame_title_buf.
7216 Re-allocate frame_title_buf if necessary. */
7218 static void
7219 store_frame_title_char (c)
7220 char c;
7222 /* If output position has reached the end of the allocated buffer,
7223 double the buffer's size. */
7224 if (frame_title_ptr == frame_title_buf_end)
7226 int len = frame_title_ptr - frame_title_buf;
7227 int new_size = 2 * len * sizeof *frame_title_buf;
7228 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7229 frame_title_buf_end = frame_title_buf + new_size;
7230 frame_title_ptr = frame_title_buf + len;
7233 *frame_title_ptr++ = c;
7237 /* Store part of a frame title in frame_title_buf, beginning at
7238 frame_title_ptr. STR is the string to store. Do not copy
7239 characters that yield more columns than PRECISION; PRECISION <= 0
7240 means copy the whole string. Pad with spaces until FIELD_WIDTH
7241 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7242 pad. Called from display_mode_element when it is used to build a
7243 frame title. */
7245 static int
7246 store_frame_title (str, field_width, precision)
7247 const unsigned char *str;
7248 int field_width, precision;
7250 int n = 0;
7251 int dummy, nbytes;
7253 /* Copy at most PRECISION chars from STR. */
7254 nbytes = strlen (str);
7255 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7256 while (nbytes--)
7257 store_frame_title_char (*str++);
7259 /* Fill up with spaces until FIELD_WIDTH reached. */
7260 while (field_width > 0
7261 && n < field_width)
7263 store_frame_title_char (' ');
7264 ++n;
7267 return n;
7270 #ifdef HAVE_WINDOW_SYSTEM
7272 /* Set the title of FRAME, if it has changed. The title format is
7273 Vicon_title_format if FRAME is iconified, otherwise it is
7274 frame_title_format. */
7276 static void
7277 x_consider_frame_title (frame)
7278 Lisp_Object frame;
7280 struct frame *f = XFRAME (frame);
7282 if (FRAME_WINDOW_P (f)
7283 || FRAME_MINIBUF_ONLY_P (f)
7284 || f->explicit_name)
7286 /* Do we have more than one visible frame on this X display? */
7287 Lisp_Object tail;
7288 Lisp_Object fmt;
7289 struct buffer *obuf;
7290 int len;
7291 struct it it;
7293 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7295 Lisp_Object other_frame = XCAR (tail);
7296 struct frame *tf = XFRAME (other_frame);
7298 if (tf != f
7299 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7300 && !FRAME_MINIBUF_ONLY_P (tf)
7301 && !EQ (other_frame, tip_frame)
7302 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7303 break;
7306 /* Set global variable indicating that multiple frames exist. */
7307 multiple_frames = CONSP (tail);
7309 /* Switch to the buffer of selected window of the frame. Set up
7310 frame_title_ptr so that display_mode_element will output into it;
7311 then display the title. */
7312 obuf = current_buffer;
7313 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7314 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7315 frame_title_ptr = frame_title_buf;
7316 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7317 NULL, DEFAULT_FACE_ID);
7318 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7319 len = frame_title_ptr - frame_title_buf;
7320 frame_title_ptr = NULL;
7321 set_buffer_internal_1 (obuf);
7323 /* Set the title only if it's changed. This avoids consing in
7324 the common case where it hasn't. (If it turns out that we've
7325 already wasted too much time by walking through the list with
7326 display_mode_element, then we might need to optimize at a
7327 higher level than this.) */
7328 if (! STRINGP (f->name)
7329 || SBYTES (f->name) != len
7330 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7331 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7335 #endif /* not HAVE_WINDOW_SYSTEM */
7340 /***********************************************************************
7341 Menu Bars
7342 ***********************************************************************/
7345 /* Prepare for redisplay by updating menu-bar item lists when
7346 appropriate. This can call eval. */
7348 void
7349 prepare_menu_bars ()
7351 int all_windows;
7352 struct gcpro gcpro1, gcpro2;
7353 struct frame *f;
7354 Lisp_Object tooltip_frame;
7356 #ifdef HAVE_WINDOW_SYSTEM
7357 tooltip_frame = tip_frame;
7358 #else
7359 tooltip_frame = Qnil;
7360 #endif
7362 /* Update all frame titles based on their buffer names, etc. We do
7363 this before the menu bars so that the buffer-menu will show the
7364 up-to-date frame titles. */
7365 #ifdef HAVE_WINDOW_SYSTEM
7366 if (windows_or_buffers_changed || update_mode_lines)
7368 Lisp_Object tail, frame;
7370 FOR_EACH_FRAME (tail, frame)
7372 f = XFRAME (frame);
7373 if (!EQ (frame, tooltip_frame)
7374 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7375 x_consider_frame_title (frame);
7378 #endif /* HAVE_WINDOW_SYSTEM */
7380 /* Update the menu bar item lists, if appropriate. This has to be
7381 done before any actual redisplay or generation of display lines. */
7382 all_windows = (update_mode_lines
7383 || buffer_shared > 1
7384 || windows_or_buffers_changed);
7385 if (all_windows)
7387 Lisp_Object tail, frame;
7388 int count = SPECPDL_INDEX ();
7390 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7392 FOR_EACH_FRAME (tail, frame)
7394 f = XFRAME (frame);
7396 /* Ignore tooltip frame. */
7397 if (EQ (frame, tooltip_frame))
7398 continue;
7400 /* If a window on this frame changed size, report that to
7401 the user and clear the size-change flag. */
7402 if (FRAME_WINDOW_SIZES_CHANGED (f))
7404 Lisp_Object functions;
7406 /* Clear flag first in case we get an error below. */
7407 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7408 functions = Vwindow_size_change_functions;
7409 GCPRO2 (tail, functions);
7411 while (CONSP (functions))
7413 call1 (XCAR (functions), frame);
7414 functions = XCDR (functions);
7416 UNGCPRO;
7419 GCPRO1 (tail);
7420 update_menu_bar (f, 0);
7421 #ifdef HAVE_WINDOW_SYSTEM
7422 update_tool_bar (f, 0);
7423 #endif
7424 UNGCPRO;
7427 unbind_to (count, Qnil);
7429 else
7431 struct frame *sf = SELECTED_FRAME ();
7432 update_menu_bar (sf, 1);
7433 #ifdef HAVE_WINDOW_SYSTEM
7434 update_tool_bar (sf, 1);
7435 #endif
7438 /* Motif needs this. See comment in xmenu.c. Turn it off when
7439 pending_menu_activation is not defined. */
7440 #ifdef USE_X_TOOLKIT
7441 pending_menu_activation = 0;
7442 #endif
7446 /* Update the menu bar item list for frame F. This has to be done
7447 before we start to fill in any display lines, because it can call
7448 eval.
7450 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7452 static void
7453 update_menu_bar (f, save_match_data)
7454 struct frame *f;
7455 int save_match_data;
7457 Lisp_Object window;
7458 register struct window *w;
7460 /* If called recursively during a menu update, do nothing. This can
7461 happen when, for instance, an activate-menubar-hook causes a
7462 redisplay. */
7463 if (inhibit_menubar_update)
7464 return;
7466 window = FRAME_SELECTED_WINDOW (f);
7467 w = XWINDOW (window);
7469 #if 0 /* The if statement below this if statement used to include the
7470 condition !NILP (w->update_mode_line), rather than using
7471 update_mode_lines directly, and this if statement may have
7472 been added to make that condition work. Now the if
7473 statement below matches its comment, this isn't needed. */
7474 if (update_mode_lines)
7475 w->update_mode_line = Qt;
7476 #endif
7478 if (FRAME_WINDOW_P (f)
7480 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7481 FRAME_EXTERNAL_MENU_BAR (f)
7482 #else
7483 FRAME_MENU_BAR_LINES (f) > 0
7484 #endif
7485 : FRAME_MENU_BAR_LINES (f) > 0)
7487 /* If the user has switched buffers or windows, we need to
7488 recompute to reflect the new bindings. But we'll
7489 recompute when update_mode_lines is set too; that means
7490 that people can use force-mode-line-update to request
7491 that the menu bar be recomputed. The adverse effect on
7492 the rest of the redisplay algorithm is about the same as
7493 windows_or_buffers_changed anyway. */
7494 if (windows_or_buffers_changed
7495 /* This used to test w->update_mode_line, but we believe
7496 there is no need to recompute the menu in that case. */
7497 || update_mode_lines
7498 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7499 < BUF_MODIFF (XBUFFER (w->buffer)))
7500 != !NILP (w->last_had_star))
7501 || ((!NILP (Vtransient_mark_mode)
7502 && !NILP (XBUFFER (w->buffer)->mark_active))
7503 != !NILP (w->region_showing)))
7505 struct buffer *prev = current_buffer;
7506 int count = SPECPDL_INDEX ();
7508 specbind (Qinhibit_menubar_update, Qt);
7510 set_buffer_internal_1 (XBUFFER (w->buffer));
7511 if (save_match_data)
7512 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7513 if (NILP (Voverriding_local_map_menu_flag))
7515 specbind (Qoverriding_terminal_local_map, Qnil);
7516 specbind (Qoverriding_local_map, Qnil);
7519 /* Run the Lucid hook. */
7520 safe_run_hooks (Qactivate_menubar_hook);
7522 /* If it has changed current-menubar from previous value,
7523 really recompute the menu-bar from the value. */
7524 if (! NILP (Vlucid_menu_bar_dirty_flag))
7525 call0 (Qrecompute_lucid_menubar);
7527 safe_run_hooks (Qmenu_bar_update_hook);
7528 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7530 /* Redisplay the menu bar in case we changed it. */
7531 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7532 if (FRAME_WINDOW_P (f)
7533 #if defined (MAC_OS)
7534 /* All frames on Mac OS share the same menubar. So only the
7535 selected frame should be allowed to set it. */
7536 && f == SELECTED_FRAME ()
7537 #endif
7539 set_frame_menubar (f, 0, 0);
7540 else
7541 /* On a terminal screen, the menu bar is an ordinary screen
7542 line, and this makes it get updated. */
7543 w->update_mode_line = Qt;
7544 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7545 /* In the non-toolkit version, the menu bar is an ordinary screen
7546 line, and this makes it get updated. */
7547 w->update_mode_line = Qt;
7548 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7550 unbind_to (count, Qnil);
7551 set_buffer_internal_1 (prev);
7558 /***********************************************************************
7559 Tool-bars
7560 ***********************************************************************/
7562 #ifdef HAVE_WINDOW_SYSTEM
7564 /* Update the tool-bar item list for frame F. This has to be done
7565 before we start to fill in any display lines. Called from
7566 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7567 and restore it here. */
7569 static void
7570 update_tool_bar (f, save_match_data)
7571 struct frame *f;
7572 int save_match_data;
7574 if (WINDOWP (f->tool_bar_window)
7575 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7577 Lisp_Object window;
7578 struct window *w;
7580 window = FRAME_SELECTED_WINDOW (f);
7581 w = XWINDOW (window);
7583 /* If the user has switched buffers or windows, we need to
7584 recompute to reflect the new bindings. But we'll
7585 recompute when update_mode_lines is set too; that means
7586 that people can use force-mode-line-update to request
7587 that the menu bar be recomputed. The adverse effect on
7588 the rest of the redisplay algorithm is about the same as
7589 windows_or_buffers_changed anyway. */
7590 if (windows_or_buffers_changed
7591 || !NILP (w->update_mode_line)
7592 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7593 < BUF_MODIFF (XBUFFER (w->buffer)))
7594 != !NILP (w->last_had_star))
7595 || ((!NILP (Vtransient_mark_mode)
7596 && !NILP (XBUFFER (w->buffer)->mark_active))
7597 != !NILP (w->region_showing)))
7599 struct buffer *prev = current_buffer;
7600 int count = SPECPDL_INDEX ();
7602 /* Set current_buffer to the buffer of the selected
7603 window of the frame, so that we get the right local
7604 keymaps. */
7605 set_buffer_internal_1 (XBUFFER (w->buffer));
7607 /* Save match data, if we must. */
7608 if (save_match_data)
7609 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7611 /* Make sure that we don't accidentally use bogus keymaps. */
7612 if (NILP (Voverriding_local_map_menu_flag))
7614 specbind (Qoverriding_terminal_local_map, Qnil);
7615 specbind (Qoverriding_local_map, Qnil);
7618 /* Build desired tool-bar items from keymaps. */
7619 f->tool_bar_items
7620 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7622 /* Redisplay the tool-bar in case we changed it. */
7623 w->update_mode_line = Qt;
7625 unbind_to (count, Qnil);
7626 set_buffer_internal_1 (prev);
7632 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7633 F's desired tool-bar contents. F->tool_bar_items must have
7634 been set up previously by calling prepare_menu_bars. */
7636 static void
7637 build_desired_tool_bar_string (f)
7638 struct frame *f;
7640 int i, size, size_needed;
7641 struct gcpro gcpro1, gcpro2, gcpro3;
7642 Lisp_Object image, plist, props;
7644 image = plist = props = Qnil;
7645 GCPRO3 (image, plist, props);
7647 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7648 Otherwise, make a new string. */
7650 /* The size of the string we might be able to reuse. */
7651 size = (STRINGP (f->desired_tool_bar_string)
7652 ? SCHARS (f->desired_tool_bar_string)
7653 : 0);
7655 /* We need one space in the string for each image. */
7656 size_needed = f->n_tool_bar_items;
7658 /* Reuse f->desired_tool_bar_string, if possible. */
7659 if (size < size_needed || NILP (f->desired_tool_bar_string))
7660 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7661 make_number (' '));
7662 else
7664 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7665 Fremove_text_properties (make_number (0), make_number (size),
7666 props, f->desired_tool_bar_string);
7669 /* Put a `display' property on the string for the images to display,
7670 put a `menu_item' property on tool-bar items with a value that
7671 is the index of the item in F's tool-bar item vector. */
7672 for (i = 0; i < f->n_tool_bar_items; ++i)
7674 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7676 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7677 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7678 int hmargin, vmargin, relief, idx, end;
7679 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7681 /* If image is a vector, choose the image according to the
7682 button state. */
7683 image = PROP (TOOL_BAR_ITEM_IMAGES);
7684 if (VECTORP (image))
7686 if (enabled_p)
7687 idx = (selected_p
7688 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7689 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7690 else
7691 idx = (selected_p
7692 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7693 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7695 xassert (ASIZE (image) >= idx);
7696 image = AREF (image, idx);
7698 else
7699 idx = -1;
7701 /* Ignore invalid image specifications. */
7702 if (!valid_image_p (image))
7703 continue;
7705 /* Display the tool-bar button pressed, or depressed. */
7706 plist = Fcopy_sequence (XCDR (image));
7708 /* Compute margin and relief to draw. */
7709 relief = (tool_bar_button_relief >= 0
7710 ? tool_bar_button_relief
7711 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7712 hmargin = vmargin = relief;
7714 if (INTEGERP (Vtool_bar_button_margin)
7715 && XINT (Vtool_bar_button_margin) > 0)
7717 hmargin += XFASTINT (Vtool_bar_button_margin);
7718 vmargin += XFASTINT (Vtool_bar_button_margin);
7720 else if (CONSP (Vtool_bar_button_margin))
7722 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7723 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7724 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7726 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7727 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7728 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7731 if (auto_raise_tool_bar_buttons_p)
7733 /* Add a `:relief' property to the image spec if the item is
7734 selected. */
7735 if (selected_p)
7737 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7738 hmargin -= relief;
7739 vmargin -= relief;
7742 else
7744 /* If image is selected, display it pressed, i.e. with a
7745 negative relief. If it's not selected, display it with a
7746 raised relief. */
7747 plist = Fplist_put (plist, QCrelief,
7748 (selected_p
7749 ? make_number (-relief)
7750 : make_number (relief)));
7751 hmargin -= relief;
7752 vmargin -= relief;
7755 /* Put a margin around the image. */
7756 if (hmargin || vmargin)
7758 if (hmargin == vmargin)
7759 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7760 else
7761 plist = Fplist_put (plist, QCmargin,
7762 Fcons (make_number (hmargin),
7763 make_number (vmargin)));
7766 /* If button is not enabled, and we don't have special images
7767 for the disabled state, make the image appear disabled by
7768 applying an appropriate algorithm to it. */
7769 if (!enabled_p && idx < 0)
7770 plist = Fplist_put (plist, QCconversion, Qdisabled);
7772 /* Put a `display' text property on the string for the image to
7773 display. Put a `menu-item' property on the string that gives
7774 the start of this item's properties in the tool-bar items
7775 vector. */
7776 image = Fcons (Qimage, plist);
7777 props = list4 (Qdisplay, image,
7778 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7780 /* Let the last image hide all remaining spaces in the tool bar
7781 string. The string can be longer than needed when we reuse a
7782 previous string. */
7783 if (i + 1 == f->n_tool_bar_items)
7784 end = SCHARS (f->desired_tool_bar_string);
7785 else
7786 end = i + 1;
7787 Fadd_text_properties (make_number (i), make_number (end),
7788 props, f->desired_tool_bar_string);
7789 #undef PROP
7792 UNGCPRO;
7796 /* Display one line of the tool-bar of frame IT->f. */
7798 static void
7799 display_tool_bar_line (it)
7800 struct it *it;
7802 struct glyph_row *row = it->glyph_row;
7803 int max_x = it->last_visible_x;
7804 struct glyph *last;
7806 prepare_desired_row (row);
7807 row->y = it->current_y;
7809 /* Note that this isn't made use of if the face hasn't a box,
7810 so there's no need to check the face here. */
7811 it->start_of_box_run_p = 1;
7813 while (it->current_x < max_x)
7815 int x_before, x, n_glyphs_before, i, nglyphs;
7817 /* Get the next display element. */
7818 if (!get_next_display_element (it))
7819 break;
7821 /* Produce glyphs. */
7822 x_before = it->current_x;
7823 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7824 PRODUCE_GLYPHS (it);
7826 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7827 i = 0;
7828 x = x_before;
7829 while (i < nglyphs)
7831 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7833 if (x + glyph->pixel_width > max_x)
7835 /* Glyph doesn't fit on line. */
7836 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7837 it->current_x = x;
7838 goto out;
7841 ++it->hpos;
7842 x += glyph->pixel_width;
7843 ++i;
7846 /* Stop at line ends. */
7847 if (ITERATOR_AT_END_OF_LINE_P (it))
7848 break;
7850 set_iterator_to_next (it, 1);
7853 out:;
7855 row->displays_text_p = row->used[TEXT_AREA] != 0;
7856 extend_face_to_end_of_line (it);
7857 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7858 last->right_box_line_p = 1;
7859 if (last == row->glyphs[TEXT_AREA])
7860 last->left_box_line_p = 1;
7861 compute_line_metrics (it);
7863 /* If line is empty, make it occupy the rest of the tool-bar. */
7864 if (!row->displays_text_p)
7866 row->height = row->phys_height = it->last_visible_y - row->y;
7867 row->ascent = row->phys_ascent = 0;
7870 row->full_width_p = 1;
7871 row->continued_p = 0;
7872 row->truncated_on_left_p = 0;
7873 row->truncated_on_right_p = 0;
7875 it->current_x = it->hpos = 0;
7876 it->current_y += row->height;
7877 ++it->vpos;
7878 ++it->glyph_row;
7882 /* Value is the number of screen lines needed to make all tool-bar
7883 items of frame F visible. */
7885 static int
7886 tool_bar_lines_needed (f)
7887 struct frame *f;
7889 struct window *w = XWINDOW (f->tool_bar_window);
7890 struct it it;
7892 /* Initialize an iterator for iteration over
7893 F->desired_tool_bar_string in the tool-bar window of frame F. */
7894 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7895 it.first_visible_x = 0;
7896 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7897 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7899 while (!ITERATOR_AT_END_P (&it))
7901 it.glyph_row = w->desired_matrix->rows;
7902 clear_glyph_row (it.glyph_row);
7903 display_tool_bar_line (&it);
7906 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7910 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7911 0, 1, 0,
7912 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7913 (frame)
7914 Lisp_Object frame;
7916 struct frame *f;
7917 struct window *w;
7918 int nlines = 0;
7920 if (NILP (frame))
7921 frame = selected_frame;
7922 else
7923 CHECK_FRAME (frame);
7924 f = XFRAME (frame);
7926 if (WINDOWP (f->tool_bar_window)
7927 || (w = XWINDOW (f->tool_bar_window),
7928 XFASTINT (w->height) > 0))
7930 update_tool_bar (f, 1);
7931 if (f->n_tool_bar_items)
7933 build_desired_tool_bar_string (f);
7934 nlines = tool_bar_lines_needed (f);
7938 return make_number (nlines);
7942 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7943 height should be changed. */
7945 static int
7946 redisplay_tool_bar (f)
7947 struct frame *f;
7949 struct window *w;
7950 struct it it;
7951 struct glyph_row *row;
7952 int change_height_p = 0;
7954 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7955 do anything. This means you must start with tool-bar-lines
7956 non-zero to get the auto-sizing effect. Or in other words, you
7957 can turn off tool-bars by specifying tool-bar-lines zero. */
7958 if (!WINDOWP (f->tool_bar_window)
7959 || (w = XWINDOW (f->tool_bar_window),
7960 XFASTINT (w->height) == 0))
7961 return 0;
7963 /* Set up an iterator for the tool-bar window. */
7964 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7965 it.first_visible_x = 0;
7966 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7967 row = it.glyph_row;
7969 /* Build a string that represents the contents of the tool-bar. */
7970 build_desired_tool_bar_string (f);
7971 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7973 /* Display as many lines as needed to display all tool-bar items. */
7974 while (it.current_y < it.last_visible_y)
7975 display_tool_bar_line (&it);
7977 /* It doesn't make much sense to try scrolling in the tool-bar
7978 window, so don't do it. */
7979 w->desired_matrix->no_scrolling_p = 1;
7980 w->must_be_updated_p = 1;
7982 if (auto_resize_tool_bars_p)
7984 int nlines;
7986 /* If we couldn't display everything, change the tool-bar's
7987 height. */
7988 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7989 change_height_p = 1;
7991 /* If there are blank lines at the end, except for a partially
7992 visible blank line at the end that is smaller than
7993 CANON_Y_UNIT, change the tool-bar's height. */
7994 row = it.glyph_row - 1;
7995 if (!row->displays_text_p
7996 && row->height >= CANON_Y_UNIT (f))
7997 change_height_p = 1;
7999 /* If row displays tool-bar items, but is partially visible,
8000 change the tool-bar's height. */
8001 if (row->displays_text_p
8002 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8003 change_height_p = 1;
8005 /* Resize windows as needed by changing the `tool-bar-lines'
8006 frame parameter. */
8007 if (change_height_p
8008 && (nlines = tool_bar_lines_needed (f),
8009 nlines != XFASTINT (w->height)))
8011 extern Lisp_Object Qtool_bar_lines;
8012 Lisp_Object frame;
8013 int old_height = XFASTINT (w->height);
8015 XSETFRAME (frame, f);
8016 clear_glyph_matrix (w->desired_matrix);
8017 Fmodify_frame_parameters (frame,
8018 Fcons (Fcons (Qtool_bar_lines,
8019 make_number (nlines)),
8020 Qnil));
8021 if (XFASTINT (w->height) != old_height)
8022 fonts_changed_p = 1;
8026 return change_height_p;
8030 /* Get information about the tool-bar item which is displayed in GLYPH
8031 on frame F. Return in *PROP_IDX the index where tool-bar item
8032 properties start in F->tool_bar_items. Value is zero if
8033 GLYPH doesn't display a tool-bar item. */
8036 tool_bar_item_info (f, glyph, prop_idx)
8037 struct frame *f;
8038 struct glyph *glyph;
8039 int *prop_idx;
8041 Lisp_Object prop;
8042 int success_p;
8043 int charpos;
8045 /* This function can be called asynchronously, which means we must
8046 exclude any possibility that Fget_text_property signals an
8047 error. */
8048 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8049 charpos = max (0, charpos);
8051 /* Get the text property `menu-item' at pos. The value of that
8052 property is the start index of this item's properties in
8053 F->tool_bar_items. */
8054 prop = Fget_text_property (make_number (charpos),
8055 Qmenu_item, f->current_tool_bar_string);
8056 if (INTEGERP (prop))
8058 *prop_idx = XINT (prop);
8059 success_p = 1;
8061 else
8062 success_p = 0;
8064 return success_p;
8067 #endif /* HAVE_WINDOW_SYSTEM */
8071 /************************************************************************
8072 Horizontal scrolling
8073 ************************************************************************/
8075 static int hscroll_window_tree P_ ((Lisp_Object));
8076 static int hscroll_windows P_ ((Lisp_Object));
8078 /* For all leaf windows in the window tree rooted at WINDOW, set their
8079 hscroll value so that PT is (i) visible in the window, and (ii) so
8080 that it is not within a certain margin at the window's left and
8081 right border. Value is non-zero if any window's hscroll has been
8082 changed. */
8084 static int
8085 hscroll_window_tree (window)
8086 Lisp_Object window;
8088 int hscrolled_p = 0;
8089 int hscroll_relative_p = FLOATP (Vhscroll_step);
8090 int hscroll_step_abs = 0;
8091 double hscroll_step_rel = 0;
8093 if (hscroll_relative_p)
8095 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8096 if (hscroll_step_rel < 0)
8098 hscroll_relative_p = 0;
8099 hscroll_step_abs = 0;
8102 else if (INTEGERP (Vhscroll_step))
8104 hscroll_step_abs = XINT (Vhscroll_step);
8105 if (hscroll_step_abs < 0)
8106 hscroll_step_abs = 0;
8108 else
8109 hscroll_step_abs = 0;
8111 while (WINDOWP (window))
8113 struct window *w = XWINDOW (window);
8115 if (WINDOWP (w->hchild))
8116 hscrolled_p |= hscroll_window_tree (w->hchild);
8117 else if (WINDOWP (w->vchild))
8118 hscrolled_p |= hscroll_window_tree (w->vchild);
8119 else if (w->cursor.vpos >= 0)
8121 int h_margin, text_area_x, text_area_y;
8122 int text_area_width, text_area_height;
8123 struct glyph_row *current_cursor_row
8124 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8125 struct glyph_row *desired_cursor_row
8126 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8127 struct glyph_row *cursor_row
8128 = (desired_cursor_row->enabled_p
8129 ? desired_cursor_row
8130 : current_cursor_row);
8132 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8133 &text_area_width, &text_area_height);
8135 /* Scroll when cursor is inside this scroll margin. */
8136 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8138 if ((XFASTINT (w->hscroll)
8139 && w->cursor.x <= h_margin)
8140 || (cursor_row->enabled_p
8141 && cursor_row->truncated_on_right_p
8142 && (w->cursor.x >= text_area_width - h_margin)))
8144 struct it it;
8145 int hscroll;
8146 struct buffer *saved_current_buffer;
8147 int pt;
8148 int wanted_x;
8150 /* Find point in a display of infinite width. */
8151 saved_current_buffer = current_buffer;
8152 current_buffer = XBUFFER (w->buffer);
8154 if (w == XWINDOW (selected_window))
8155 pt = BUF_PT (current_buffer);
8156 else
8158 pt = marker_position (w->pointm);
8159 pt = max (BEGV, pt);
8160 pt = min (ZV, pt);
8163 /* Move iterator to pt starting at cursor_row->start in
8164 a line with infinite width. */
8165 init_to_row_start (&it, w, cursor_row);
8166 it.last_visible_x = INFINITY;
8167 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8168 current_buffer = saved_current_buffer;
8170 /* Position cursor in window. */
8171 if (!hscroll_relative_p && hscroll_step_abs == 0)
8172 hscroll = max (0, it.current_x - text_area_width / 2)
8173 / CANON_X_UNIT (it.f);
8174 else if (w->cursor.x >= text_area_width - h_margin)
8176 if (hscroll_relative_p)
8177 wanted_x = text_area_width * (1 - hscroll_step_rel)
8178 - h_margin;
8179 else
8180 wanted_x = text_area_width
8181 - hscroll_step_abs * CANON_X_UNIT (it.f)
8182 - h_margin;
8183 hscroll
8184 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8186 else
8188 if (hscroll_relative_p)
8189 wanted_x = text_area_width * hscroll_step_rel
8190 + h_margin;
8191 else
8192 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8193 + h_margin;
8194 hscroll
8195 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8197 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8199 /* Don't call Fset_window_hscroll if value hasn't
8200 changed because it will prevent redisplay
8201 optimizations. */
8202 if (XFASTINT (w->hscroll) != hscroll)
8204 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8205 w->hscroll = make_number (hscroll);
8206 hscrolled_p = 1;
8211 window = w->next;
8214 /* Value is non-zero if hscroll of any leaf window has been changed. */
8215 return hscrolled_p;
8219 /* Set hscroll so that cursor is visible and not inside horizontal
8220 scroll margins for all windows in the tree rooted at WINDOW. See
8221 also hscroll_window_tree above. Value is non-zero if any window's
8222 hscroll has been changed. If it has, desired matrices on the frame
8223 of WINDOW are cleared. */
8225 static int
8226 hscroll_windows (window)
8227 Lisp_Object window;
8229 int hscrolled_p;
8231 if (automatic_hscrolling_p)
8233 hscrolled_p = hscroll_window_tree (window);
8234 if (hscrolled_p)
8235 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8237 else
8238 hscrolled_p = 0;
8239 return hscrolled_p;
8244 /************************************************************************
8245 Redisplay
8246 ************************************************************************/
8248 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8249 to a non-zero value. This is sometimes handy to have in a debugger
8250 session. */
8252 #if GLYPH_DEBUG
8254 /* First and last unchanged row for try_window_id. */
8256 int debug_first_unchanged_at_end_vpos;
8257 int debug_last_unchanged_at_beg_vpos;
8259 /* Delta vpos and y. */
8261 int debug_dvpos, debug_dy;
8263 /* Delta in characters and bytes for try_window_id. */
8265 int debug_delta, debug_delta_bytes;
8267 /* Values of window_end_pos and window_end_vpos at the end of
8268 try_window_id. */
8270 EMACS_INT debug_end_pos, debug_end_vpos;
8272 /* Append a string to W->desired_matrix->method. FMT is a printf
8273 format string. A1...A9 are a supplement for a variable-length
8274 argument list. If trace_redisplay_p is non-zero also printf the
8275 resulting string to stderr. */
8277 static void
8278 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8279 struct window *w;
8280 char *fmt;
8281 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8283 char buffer[512];
8284 char *method = w->desired_matrix->method;
8285 int len = strlen (method);
8286 int size = sizeof w->desired_matrix->method;
8287 int remaining = size - len - 1;
8289 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8290 if (len && remaining)
8292 method[len] = '|';
8293 --remaining, ++len;
8296 strncpy (method + len, buffer, remaining);
8298 if (trace_redisplay_p)
8299 fprintf (stderr, "%p (%s): %s\n",
8301 ((BUFFERP (w->buffer)
8302 && STRINGP (XBUFFER (w->buffer)->name))
8303 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8304 : "no buffer"),
8305 buffer);
8308 #endif /* GLYPH_DEBUG */
8311 /* Value is non-zero if all changes in window W, which displays
8312 current_buffer, are in the text between START and END. START is a
8313 buffer position, END is given as a distance from Z. Used in
8314 redisplay_internal for display optimization. */
8316 static INLINE int
8317 text_outside_line_unchanged_p (w, start, end)
8318 struct window *w;
8319 int start, end;
8321 int unchanged_p = 1;
8323 /* If text or overlays have changed, see where. */
8324 if (XFASTINT (w->last_modified) < MODIFF
8325 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8327 /* Gap in the line? */
8328 if (GPT < start || Z - GPT < end)
8329 unchanged_p = 0;
8331 /* Changes start in front of the line, or end after it? */
8332 if (unchanged_p
8333 && (BEG_UNCHANGED < start - 1
8334 || END_UNCHANGED < end))
8335 unchanged_p = 0;
8337 /* If selective display, can't optimize if changes start at the
8338 beginning of the line. */
8339 if (unchanged_p
8340 && INTEGERP (current_buffer->selective_display)
8341 && XINT (current_buffer->selective_display) > 0
8342 && (BEG_UNCHANGED < start || GPT <= start))
8343 unchanged_p = 0;
8345 /* If there are overlays at the start or end of the line, these
8346 may have overlay strings with newlines in them. A change at
8347 START, for instance, may actually concern the display of such
8348 overlay strings as well, and they are displayed on different
8349 lines. So, quickly rule out this case. (For the future, it
8350 might be desirable to implement something more telling than
8351 just BEG/END_UNCHANGED.) */
8352 if (unchanged_p)
8354 if (BEG + BEG_UNCHANGED == start
8355 && overlay_touches_p (start))
8356 unchanged_p = 0;
8357 if (END_UNCHANGED == end
8358 && overlay_touches_p (Z - end))
8359 unchanged_p = 0;
8363 return unchanged_p;
8367 /* Do a frame update, taking possible shortcuts into account. This is
8368 the main external entry point for redisplay.
8370 If the last redisplay displayed an echo area message and that message
8371 is no longer requested, we clear the echo area or bring back the
8372 mini-buffer if that is in use. */
8374 void
8375 redisplay ()
8377 redisplay_internal (0);
8381 /* Return 1 if point moved out of or into a composition. Otherwise
8382 return 0. PREV_BUF and PREV_PT are the last point buffer and
8383 position. BUF and PT are the current point buffer and position. */
8386 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8387 struct buffer *prev_buf, *buf;
8388 int prev_pt, pt;
8390 int start, end;
8391 Lisp_Object prop;
8392 Lisp_Object buffer;
8394 XSETBUFFER (buffer, buf);
8395 /* Check a composition at the last point if point moved within the
8396 same buffer. */
8397 if (prev_buf == buf)
8399 if (prev_pt == pt)
8400 /* Point didn't move. */
8401 return 0;
8403 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8404 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8405 && COMPOSITION_VALID_P (start, end, prop)
8406 && start < prev_pt && end > prev_pt)
8407 /* The last point was within the composition. Return 1 iff
8408 point moved out of the composition. */
8409 return (pt <= start || pt >= end);
8412 /* Check a composition at the current point. */
8413 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8414 && find_composition (pt, -1, &start, &end, &prop, buffer)
8415 && COMPOSITION_VALID_P (start, end, prop)
8416 && start < pt && end > pt);
8420 /* Reconsider the setting of B->clip_changed which is displayed
8421 in window W. */
8423 static INLINE void
8424 reconsider_clip_changes (w, b)
8425 struct window *w;
8426 struct buffer *b;
8428 if (b->clip_changed
8429 && !NILP (w->window_end_valid)
8430 && w->current_matrix->buffer == b
8431 && w->current_matrix->zv == BUF_ZV (b)
8432 && w->current_matrix->begv == BUF_BEGV (b))
8433 b->clip_changed = 0;
8435 /* If display wasn't paused, and W is not a tool bar window, see if
8436 point has been moved into or out of a composition. In that case,
8437 we set b->clip_changed to 1 to force updating the screen. If
8438 b->clip_changed has already been set to 1, we can skip this
8439 check. */
8440 if (!b->clip_changed
8441 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8443 int pt;
8445 if (w == XWINDOW (selected_window))
8446 pt = BUF_PT (current_buffer);
8447 else
8448 pt = marker_position (w->pointm);
8450 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8451 || pt != XINT (w->last_point))
8452 && check_point_in_composition (w->current_matrix->buffer,
8453 XINT (w->last_point),
8454 XBUFFER (w->buffer), pt))
8455 b->clip_changed = 1;
8460 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8461 response to any user action; therefore, we should preserve the echo
8462 area. (Actually, our caller does that job.) Perhaps in the future
8463 avoid recentering windows if it is not necessary; currently that
8464 causes some problems. */
8466 static void
8467 redisplay_internal (preserve_echo_area)
8468 int preserve_echo_area;
8470 struct window *w = XWINDOW (selected_window);
8471 struct frame *f = XFRAME (w->frame);
8472 int pause;
8473 int must_finish = 0;
8474 struct text_pos tlbufpos, tlendpos;
8475 int number_of_visible_frames;
8476 int count;
8477 struct frame *sf = SELECTED_FRAME ();
8479 /* Non-zero means redisplay has to consider all windows on all
8480 frames. Zero means, only selected_window is considered. */
8481 int consider_all_windows_p;
8483 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8485 /* No redisplay if running in batch mode or frame is not yet fully
8486 initialized, or redisplay is explicitly turned off by setting
8487 Vinhibit_redisplay. */
8488 if (noninteractive
8489 || !NILP (Vinhibit_redisplay)
8490 || !f->glyphs_initialized_p)
8491 return;
8493 /* The flag redisplay_performed_directly_p is set by
8494 direct_output_for_insert when it already did the whole screen
8495 update necessary. */
8496 if (redisplay_performed_directly_p)
8498 redisplay_performed_directly_p = 0;
8499 if (!hscroll_windows (selected_window))
8500 return;
8503 #ifdef USE_X_TOOLKIT
8504 if (popup_activated ())
8505 return;
8506 #endif
8508 /* I don't think this happens but let's be paranoid. */
8509 if (redisplaying_p)
8510 return;
8512 /* Record a function that resets redisplaying_p to its old value
8513 when we leave this function. */
8514 count = SPECPDL_INDEX ();
8515 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8516 ++redisplaying_p;
8518 retry:
8519 pause = 0;
8520 reconsider_clip_changes (w, current_buffer);
8521 redisplay_updating_p = 0;
8523 /* If new fonts have been loaded that make a glyph matrix adjustment
8524 necessary, do it. */
8525 if (fonts_changed_p)
8527 adjust_glyphs (NULL);
8528 ++windows_or_buffers_changed;
8529 fonts_changed_p = 0;
8532 /* If face_change_count is non-zero, init_iterator will free all
8533 realized faces, which includes the faces referenced from current
8534 matrices. So, we can't reuse current matrices in this case. */
8535 if (face_change_count)
8536 ++windows_or_buffers_changed;
8538 if (! FRAME_WINDOW_P (sf)
8539 && previous_terminal_frame != sf)
8541 /* Since frames on an ASCII terminal share the same display
8542 area, displaying a different frame means redisplay the whole
8543 thing. */
8544 windows_or_buffers_changed++;
8545 SET_FRAME_GARBAGED (sf);
8546 XSETFRAME (Vterminal_frame, sf);
8548 previous_terminal_frame = sf;
8550 /* Set the visible flags for all frames. Do this before checking
8551 for resized or garbaged frames; they want to know if their frames
8552 are visible. See the comment in frame.h for
8553 FRAME_SAMPLE_VISIBILITY. */
8555 Lisp_Object tail, frame;
8557 number_of_visible_frames = 0;
8559 FOR_EACH_FRAME (tail, frame)
8561 struct frame *f = XFRAME (frame);
8563 FRAME_SAMPLE_VISIBILITY (f);
8564 if (FRAME_VISIBLE_P (f))
8565 ++number_of_visible_frames;
8566 clear_desired_matrices (f);
8570 /* Notice any pending interrupt request to change frame size. */
8571 do_pending_window_change (1);
8573 /* Clear frames marked as garbaged. */
8574 if (frame_garbaged)
8575 clear_garbaged_frames ();
8577 /* Build menubar and tool-bar items. */
8578 prepare_menu_bars ();
8580 if (windows_or_buffers_changed)
8581 update_mode_lines++;
8583 /* Detect case that we need to write or remove a star in the mode line. */
8584 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8586 w->update_mode_line = Qt;
8587 if (buffer_shared > 1)
8588 update_mode_lines++;
8591 /* If %c is in the mode line, update it if needed. */
8592 if (!NILP (w->column_number_displayed)
8593 /* This alternative quickly identifies a common case
8594 where no change is needed. */
8595 && !(PT == XFASTINT (w->last_point)
8596 && XFASTINT (w->last_modified) >= MODIFF
8597 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8598 && (XFASTINT (w->column_number_displayed)
8599 != (int) current_column ())) /* iftc */
8600 w->update_mode_line = Qt;
8602 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8604 /* The variable buffer_shared is set in redisplay_window and
8605 indicates that we redisplay a buffer in different windows. See
8606 there. */
8607 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8608 || cursor_type_changed);
8610 /* If specs for an arrow have changed, do thorough redisplay
8611 to ensure we remove any arrow that should no longer exist. */
8612 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8613 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8614 consider_all_windows_p = windows_or_buffers_changed = 1;
8616 /* Normally the message* functions will have already displayed and
8617 updated the echo area, but the frame may have been trashed, or
8618 the update may have been preempted, so display the echo area
8619 again here. Checking message_cleared_p captures the case that
8620 the echo area should be cleared. */
8621 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8622 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8623 || (message_cleared_p
8624 && minibuf_level == 0
8625 /* If the mini-window is currently selected, this means the
8626 echo-area doesn't show through. */
8627 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8629 int window_height_changed_p = echo_area_display (0);
8630 must_finish = 1;
8632 /* If we don't display the current message, don't clear the
8633 message_cleared_p flag, because, if we did, we wouldn't clear
8634 the echo area in the next redisplay which doesn't preserve
8635 the echo area. */
8636 if (!display_last_displayed_message_p)
8637 message_cleared_p = 0;
8639 if (fonts_changed_p)
8640 goto retry;
8641 else if (window_height_changed_p)
8643 consider_all_windows_p = 1;
8644 ++update_mode_lines;
8645 ++windows_or_buffers_changed;
8647 /* If window configuration was changed, frames may have been
8648 marked garbaged. Clear them or we will experience
8649 surprises wrt scrolling. */
8650 if (frame_garbaged)
8651 clear_garbaged_frames ();
8654 else if (EQ (selected_window, minibuf_window)
8655 && (current_buffer->clip_changed
8656 || XFASTINT (w->last_modified) < MODIFF
8657 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8658 && resize_mini_window (w, 0))
8660 /* Resized active mini-window to fit the size of what it is
8661 showing if its contents might have changed. */
8662 must_finish = 1;
8663 consider_all_windows_p = 1;
8664 ++windows_or_buffers_changed;
8665 ++update_mode_lines;
8667 /* If window configuration was changed, frames may have been
8668 marked garbaged. Clear them or we will experience
8669 surprises wrt scrolling. */
8670 if (frame_garbaged)
8671 clear_garbaged_frames ();
8675 /* If showing the region, and mark has changed, we must redisplay
8676 the whole window. The assignment to this_line_start_pos prevents
8677 the optimization directly below this if-statement. */
8678 if (((!NILP (Vtransient_mark_mode)
8679 && !NILP (XBUFFER (w->buffer)->mark_active))
8680 != !NILP (w->region_showing))
8681 || (!NILP (w->region_showing)
8682 && !EQ (w->region_showing,
8683 Fmarker_position (XBUFFER (w->buffer)->mark))))
8684 CHARPOS (this_line_start_pos) = 0;
8686 /* Optimize the case that only the line containing the cursor in the
8687 selected window has changed. Variables starting with this_ are
8688 set in display_line and record information about the line
8689 containing the cursor. */
8690 tlbufpos = this_line_start_pos;
8691 tlendpos = this_line_end_pos;
8692 if (!consider_all_windows_p
8693 && CHARPOS (tlbufpos) > 0
8694 && NILP (w->update_mode_line)
8695 && !current_buffer->clip_changed
8696 && !current_buffer->prevent_redisplay_optimizations_p
8697 && FRAME_VISIBLE_P (XFRAME (w->frame))
8698 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8699 /* Make sure recorded data applies to current buffer, etc. */
8700 && this_line_buffer == current_buffer
8701 && current_buffer == XBUFFER (w->buffer)
8702 && NILP (w->force_start)
8703 && NILP (w->optional_new_start)
8704 /* Point must be on the line that we have info recorded about. */
8705 && PT >= CHARPOS (tlbufpos)
8706 && PT <= Z - CHARPOS (tlendpos)
8707 /* All text outside that line, including its final newline,
8708 must be unchanged */
8709 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8710 CHARPOS (tlendpos)))
8712 if (CHARPOS (tlbufpos) > BEGV
8713 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8714 && (CHARPOS (tlbufpos) == ZV
8715 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8716 /* Former continuation line has disappeared by becoming empty */
8717 goto cancel;
8718 else if (XFASTINT (w->last_modified) < MODIFF
8719 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8720 || MINI_WINDOW_P (w))
8722 /* We have to handle the case of continuation around a
8723 wide-column character (See the comment in indent.c around
8724 line 885).
8726 For instance, in the following case:
8728 -------- Insert --------
8729 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8730 J_I_ ==> J_I_ `^^' are cursors.
8731 ^^ ^^
8732 -------- --------
8734 As we have to redraw the line above, we should goto cancel. */
8736 struct it it;
8737 int line_height_before = this_line_pixel_height;
8739 /* Note that start_display will handle the case that the
8740 line starting at tlbufpos is a continuation lines. */
8741 start_display (&it, w, tlbufpos);
8743 /* Implementation note: It this still necessary? */
8744 if (it.current_x != this_line_start_x)
8745 goto cancel;
8747 TRACE ((stderr, "trying display optimization 1\n"));
8748 w->cursor.vpos = -1;
8749 overlay_arrow_seen = 0;
8750 it.vpos = this_line_vpos;
8751 it.current_y = this_line_y;
8752 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8753 display_line (&it);
8755 /* If line contains point, is not continued,
8756 and ends at same distance from eob as before, we win */
8757 if (w->cursor.vpos >= 0
8758 /* Line is not continued, otherwise this_line_start_pos
8759 would have been set to 0 in display_line. */
8760 && CHARPOS (this_line_start_pos)
8761 /* Line ends as before. */
8762 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8763 /* Line has same height as before. Otherwise other lines
8764 would have to be shifted up or down. */
8765 && this_line_pixel_height == line_height_before)
8767 /* If this is not the window's last line, we must adjust
8768 the charstarts of the lines below. */
8769 if (it.current_y < it.last_visible_y)
8771 struct glyph_row *row
8772 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8773 int delta, delta_bytes;
8775 if (Z - CHARPOS (tlendpos) == ZV)
8777 /* This line ends at end of (accessible part of)
8778 buffer. There is no newline to count. */
8779 delta = (Z
8780 - CHARPOS (tlendpos)
8781 - MATRIX_ROW_START_CHARPOS (row));
8782 delta_bytes = (Z_BYTE
8783 - BYTEPOS (tlendpos)
8784 - MATRIX_ROW_START_BYTEPOS (row));
8786 else
8788 /* This line ends in a newline. Must take
8789 account of the newline and the rest of the
8790 text that follows. */
8791 delta = (Z
8792 - CHARPOS (tlendpos)
8793 - MATRIX_ROW_START_CHARPOS (row));
8794 delta_bytes = (Z_BYTE
8795 - BYTEPOS (tlendpos)
8796 - MATRIX_ROW_START_BYTEPOS (row));
8799 increment_matrix_positions (w->current_matrix,
8800 this_line_vpos + 1,
8801 w->current_matrix->nrows,
8802 delta, delta_bytes);
8805 /* If this row displays text now but previously didn't,
8806 or vice versa, w->window_end_vpos may have to be
8807 adjusted. */
8808 if ((it.glyph_row - 1)->displays_text_p)
8810 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8811 XSETINT (w->window_end_vpos, this_line_vpos);
8813 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8814 && this_line_vpos > 0)
8815 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8816 w->window_end_valid = Qnil;
8818 /* Update hint: No need to try to scroll in update_window. */
8819 w->desired_matrix->no_scrolling_p = 1;
8821 #if GLYPH_DEBUG
8822 *w->desired_matrix->method = 0;
8823 debug_method_add (w, "optimization 1");
8824 #endif
8825 goto update;
8827 else
8828 goto cancel;
8830 else if (/* Cursor position hasn't changed. */
8831 PT == XFASTINT (w->last_point)
8832 /* Make sure the cursor was last displayed
8833 in this window. Otherwise we have to reposition it. */
8834 && 0 <= w->cursor.vpos
8835 && XINT (w->height) > w->cursor.vpos)
8837 if (!must_finish)
8839 do_pending_window_change (1);
8841 /* We used to always goto end_of_redisplay here, but this
8842 isn't enough if we have a blinking cursor. */
8843 if (w->cursor_off_p == w->last_cursor_off_p)
8844 goto end_of_redisplay;
8846 goto update;
8848 /* If highlighting the region, or if the cursor is in the echo area,
8849 then we can't just move the cursor. */
8850 else if (! (!NILP (Vtransient_mark_mode)
8851 && !NILP (current_buffer->mark_active))
8852 && (EQ (selected_window, current_buffer->last_selected_window)
8853 || highlight_nonselected_windows)
8854 && NILP (w->region_showing)
8855 && NILP (Vshow_trailing_whitespace)
8856 && !cursor_in_echo_area)
8858 struct it it;
8859 struct glyph_row *row;
8861 /* Skip from tlbufpos to PT and see where it is. Note that
8862 PT may be in invisible text. If so, we will end at the
8863 next visible position. */
8864 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8865 NULL, DEFAULT_FACE_ID);
8866 it.current_x = this_line_start_x;
8867 it.current_y = this_line_y;
8868 it.vpos = this_line_vpos;
8870 /* The call to move_it_to stops in front of PT, but
8871 moves over before-strings. */
8872 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8874 if (it.vpos == this_line_vpos
8875 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8876 row->enabled_p))
8878 xassert (this_line_vpos == it.vpos);
8879 xassert (this_line_y == it.current_y);
8880 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8881 #if GLYPH_DEBUG
8882 *w->desired_matrix->method = 0;
8883 debug_method_add (w, "optimization 3");
8884 #endif
8885 goto update;
8887 else
8888 goto cancel;
8891 cancel:
8892 /* Text changed drastically or point moved off of line. */
8893 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8896 CHARPOS (this_line_start_pos) = 0;
8897 consider_all_windows_p |= buffer_shared > 1;
8898 ++clear_face_cache_count;
8901 /* Build desired matrices, and update the display. If
8902 consider_all_windows_p is non-zero, do it for all windows on all
8903 frames. Otherwise do it for selected_window, only. */
8905 if (consider_all_windows_p)
8907 Lisp_Object tail, frame;
8908 int i, n = 0, size = 50;
8909 struct frame **updated
8910 = (struct frame **) alloca (size * sizeof *updated);
8912 /* Clear the face cache eventually. */
8913 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8915 clear_face_cache (0);
8916 clear_face_cache_count = 0;
8919 /* Recompute # windows showing selected buffer. This will be
8920 incremented each time such a window is displayed. */
8921 buffer_shared = 0;
8923 FOR_EACH_FRAME (tail, frame)
8925 struct frame *f = XFRAME (frame);
8927 if (FRAME_WINDOW_P (f) || f == sf)
8929 #ifdef HAVE_WINDOW_SYSTEM
8930 if (clear_face_cache_count % 50 == 0
8931 && FRAME_WINDOW_P (f))
8932 clear_image_cache (f, 0);
8933 #endif /* HAVE_WINDOW_SYSTEM */
8935 /* Mark all the scroll bars to be removed; we'll redeem
8936 the ones we want when we redisplay their windows. */
8937 if (condemn_scroll_bars_hook)
8938 condemn_scroll_bars_hook (f);
8940 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8941 redisplay_windows (FRAME_ROOT_WINDOW (f));
8943 /* Any scroll bars which redisplay_windows should have
8944 nuked should now go away. */
8945 if (judge_scroll_bars_hook)
8946 judge_scroll_bars_hook (f);
8948 /* If fonts changed, display again. */
8949 if (fonts_changed_p)
8950 goto retry;
8952 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8954 /* See if we have to hscroll. */
8955 if (hscroll_windows (f->root_window))
8956 goto retry;
8958 /* Prevent various kinds of signals during display
8959 update. stdio is not robust about handling
8960 signals, which can cause an apparent I/O
8961 error. */
8962 if (interrupt_input)
8963 unrequest_sigio ();
8964 stop_polling ();
8966 /* Update the display. */
8967 set_window_update_flags (XWINDOW (f->root_window), 1);
8968 pause |= update_frame (f, 0, 0);
8969 if (pause)
8970 break;
8972 if (n == size)
8974 int nbytes = size * sizeof *updated;
8975 struct frame **p = (struct frame **) alloca (2 * nbytes);
8976 bcopy (updated, p, nbytes);
8977 size *= 2;
8980 updated[n++] = f;
8985 /* Do the mark_window_display_accurate after all windows have
8986 been redisplayed because this call resets flags in buffers
8987 which are needed for proper redisplay. */
8988 for (i = 0; i < n; ++i)
8990 struct frame *f = updated[i];
8991 mark_window_display_accurate (f->root_window, 1);
8992 if (frame_up_to_date_hook)
8993 frame_up_to_date_hook (f);
8996 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8998 Lisp_Object mini_window;
8999 struct frame *mini_frame;
9001 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9002 /* Use list_of_error, not Qerror, so that
9003 we catch only errors and don't run the debugger. */
9004 internal_condition_case_1 (redisplay_window_1, selected_window,
9005 list_of_error,
9006 redisplay_window_error);
9008 /* Compare desired and current matrices, perform output. */
9009 update:
9010 redisplay_updating_p = 1;
9012 /* If fonts changed, display again. */
9013 if (fonts_changed_p)
9014 goto retry;
9016 /* Prevent various kinds of signals during display update.
9017 stdio is not robust about handling signals,
9018 which can cause an apparent I/O error. */
9019 if (interrupt_input)
9020 unrequest_sigio ();
9021 stop_polling ();
9023 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9025 if (hscroll_windows (selected_window))
9026 goto retry;
9028 XWINDOW (selected_window)->must_be_updated_p = 1;
9029 pause = update_frame (sf, 0, 0);
9032 /* We may have called echo_area_display at the top of this
9033 function. If the echo area is on another frame, that may
9034 have put text on a frame other than the selected one, so the
9035 above call to update_frame would not have caught it. Catch
9036 it here. */
9037 mini_window = FRAME_MINIBUF_WINDOW (sf);
9038 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9040 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9042 XWINDOW (mini_window)->must_be_updated_p = 1;
9043 pause |= update_frame (mini_frame, 0, 0);
9044 if (!pause && hscroll_windows (mini_window))
9045 goto retry;
9049 /* If display was paused because of pending input, make sure we do a
9050 thorough update the next time. */
9051 if (pause)
9053 /* Prevent the optimization at the beginning of
9054 redisplay_internal that tries a single-line update of the
9055 line containing the cursor in the selected window. */
9056 CHARPOS (this_line_start_pos) = 0;
9058 /* Let the overlay arrow be updated the next time. */
9059 if (!NILP (last_arrow_position))
9061 last_arrow_position = Qt;
9062 last_arrow_string = Qt;
9065 /* If we pause after scrolling, some rows in the current
9066 matrices of some windows are not valid. */
9067 if (!WINDOW_FULL_WIDTH_P (w)
9068 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9069 update_mode_lines = 1;
9071 else
9073 if (!consider_all_windows_p)
9075 /* This has already been done above if
9076 consider_all_windows_p is set. */
9077 mark_window_display_accurate_1 (w, 1);
9079 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9080 last_arrow_string = Voverlay_arrow_string;
9082 if (frame_up_to_date_hook != 0)
9083 frame_up_to_date_hook (sf);
9086 update_mode_lines = 0;
9087 windows_or_buffers_changed = 0;
9088 cursor_type_changed = 0;
9091 /* Start SIGIO interrupts coming again. Having them off during the
9092 code above makes it less likely one will discard output, but not
9093 impossible, since there might be stuff in the system buffer here.
9094 But it is much hairier to try to do anything about that. */
9095 if (interrupt_input)
9096 request_sigio ();
9097 start_polling ();
9099 /* If a frame has become visible which was not before, redisplay
9100 again, so that we display it. Expose events for such a frame
9101 (which it gets when becoming visible) don't call the parts of
9102 redisplay constructing glyphs, so simply exposing a frame won't
9103 display anything in this case. So, we have to display these
9104 frames here explicitly. */
9105 if (!pause)
9107 Lisp_Object tail, frame;
9108 int new_count = 0;
9110 FOR_EACH_FRAME (tail, frame)
9112 int this_is_visible = 0;
9114 if (XFRAME (frame)->visible)
9115 this_is_visible = 1;
9116 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9117 if (XFRAME (frame)->visible)
9118 this_is_visible = 1;
9120 if (this_is_visible)
9121 new_count++;
9124 if (new_count != number_of_visible_frames)
9125 windows_or_buffers_changed++;
9128 /* Change frame size now if a change is pending. */
9129 do_pending_window_change (1);
9131 /* If we just did a pending size change, or have additional
9132 visible frames, redisplay again. */
9133 if (windows_or_buffers_changed && !pause)
9134 goto retry;
9136 end_of_redisplay:
9137 redisplay_updating_p = 0;
9138 unbind_to (count, Qnil);
9142 /* Redisplay, but leave alone any recent echo area message unless
9143 another message has been requested in its place.
9145 This is useful in situations where you need to redisplay but no
9146 user action has occurred, making it inappropriate for the message
9147 area to be cleared. See tracking_off and
9148 wait_reading_process_input for examples of these situations.
9150 FROM_WHERE is an integer saying from where this function was
9151 called. This is useful for debugging. */
9153 void
9154 redisplay_preserve_echo_area (from_where)
9155 int from_where;
9157 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9159 if (!NILP (echo_area_buffer[1]))
9161 /* We have a previously displayed message, but no current
9162 message. Redisplay the previous message. */
9163 display_last_displayed_message_p = 1;
9164 redisplay_internal (1);
9165 display_last_displayed_message_p = 0;
9167 else
9168 redisplay_internal (1);
9172 /* Function registered with record_unwind_protect in
9173 redisplay_internal. Reset redisplaying_p to the value it had
9174 before redisplay_internal was called, and clear
9175 redisplay_updating_p. */
9177 static Lisp_Object
9178 unwind_redisplay (old_redisplaying_p)
9179 Lisp_Object old_redisplaying_p;
9181 redisplaying_p = XFASTINT (old_redisplaying_p);
9182 redisplay_updating_p = 0;
9183 return Qnil;
9187 /* Mark the display of window W as accurate or inaccurate. If
9188 ACCURATE_P is non-zero mark display of W as accurate. If
9189 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9190 redisplay_internal is called. */
9192 static void
9193 mark_window_display_accurate_1 (w, accurate_p)
9194 struct window *w;
9195 int accurate_p;
9197 if (BUFFERP (w->buffer))
9199 struct buffer *b = XBUFFER (w->buffer);
9201 w->last_modified
9202 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9203 w->last_overlay_modified
9204 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9205 w->last_had_star
9206 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9208 if (accurate_p)
9210 b->clip_changed = 0;
9211 b->prevent_redisplay_optimizations_p = 0;
9213 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9214 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9215 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9216 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9218 w->current_matrix->buffer = b;
9219 w->current_matrix->begv = BUF_BEGV (b);
9220 w->current_matrix->zv = BUF_ZV (b);
9222 w->last_cursor = w->cursor;
9223 w->last_cursor_off_p = w->cursor_off_p;
9225 if (w == XWINDOW (selected_window))
9226 w->last_point = make_number (BUF_PT (b));
9227 else
9228 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9232 if (accurate_p)
9234 w->window_end_valid = w->buffer;
9235 #if 0 /* This is incorrect with variable-height lines. */
9236 xassert (XINT (w->window_end_vpos)
9237 < (XINT (w->height)
9238 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9239 #endif
9240 w->update_mode_line = Qnil;
9245 /* Mark the display of windows in the window tree rooted at WINDOW as
9246 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9247 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9248 be redisplayed the next time redisplay_internal is called. */
9250 void
9251 mark_window_display_accurate (window, accurate_p)
9252 Lisp_Object window;
9253 int accurate_p;
9255 struct window *w;
9257 for (; !NILP (window); window = w->next)
9259 w = XWINDOW (window);
9260 mark_window_display_accurate_1 (w, accurate_p);
9262 if (!NILP (w->vchild))
9263 mark_window_display_accurate (w->vchild, accurate_p);
9264 if (!NILP (w->hchild))
9265 mark_window_display_accurate (w->hchild, accurate_p);
9268 if (accurate_p)
9270 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9271 last_arrow_string = Voverlay_arrow_string;
9273 else
9275 /* Force a thorough redisplay the next time by setting
9276 last_arrow_position and last_arrow_string to t, which is
9277 unequal to any useful value of Voverlay_arrow_... */
9278 last_arrow_position = Qt;
9279 last_arrow_string = Qt;
9284 /* Return value in display table DP (Lisp_Char_Table *) for character
9285 C. Since a display table doesn't have any parent, we don't have to
9286 follow parent. Do not call this function directly but use the
9287 macro DISP_CHAR_VECTOR. */
9289 Lisp_Object
9290 disp_char_vector (dp, c)
9291 struct Lisp_Char_Table *dp;
9292 int c;
9294 int code[4], i;
9295 Lisp_Object val;
9297 if (SINGLE_BYTE_CHAR_P (c))
9298 return (dp->contents[c]);
9300 SPLIT_CHAR (c, code[0], code[1], code[2]);
9301 if (code[1] < 32)
9302 code[1] = -1;
9303 else if (code[2] < 32)
9304 code[2] = -1;
9306 /* Here, the possible range of code[0] (== charset ID) is
9307 128..max_charset. Since the top level char table contains data
9308 for multibyte characters after 256th element, we must increment
9309 code[0] by 128 to get a correct index. */
9310 code[0] += 128;
9311 code[3] = -1; /* anchor */
9313 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9315 val = dp->contents[code[i]];
9316 if (!SUB_CHAR_TABLE_P (val))
9317 return (NILP (val) ? dp->defalt : val);
9320 /* Here, val is a sub char table. We return the default value of
9321 it. */
9322 return (dp->defalt);
9327 /***********************************************************************
9328 Window Redisplay
9329 ***********************************************************************/
9331 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9333 static void
9334 redisplay_windows (window)
9335 Lisp_Object window;
9337 while (!NILP (window))
9339 struct window *w = XWINDOW (window);
9341 if (!NILP (w->hchild))
9342 redisplay_windows (w->hchild);
9343 else if (!NILP (w->vchild))
9344 redisplay_windows (w->vchild);
9345 else
9347 displayed_buffer = XBUFFER (w->buffer);
9348 /* Use list_of_error, not Qerror, so that
9349 we catch only errors and don't run the debugger. */
9350 internal_condition_case_1 (redisplay_window_0, window,
9351 list_of_error,
9352 redisplay_window_error);
9355 window = w->next;
9359 static Lisp_Object
9360 redisplay_window_error ()
9362 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9363 return Qnil;
9366 static Lisp_Object
9367 redisplay_window_0 (window)
9368 Lisp_Object window;
9370 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9371 redisplay_window (window, 0);
9372 return Qnil;
9375 static Lisp_Object
9376 redisplay_window_1 (window)
9377 Lisp_Object window;
9379 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9380 redisplay_window (window, 1);
9381 return Qnil;
9384 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9385 DELTA is the number of bytes by which positions recorded in ROW
9386 differ from current buffer positions. */
9388 void
9389 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9390 struct window *w;
9391 struct glyph_row *row;
9392 struct glyph_matrix *matrix;
9393 int delta, delta_bytes, dy, dvpos;
9395 struct glyph *glyph = row->glyphs[TEXT_AREA];
9396 struct glyph *end = glyph + row->used[TEXT_AREA];
9397 int x = row->x;
9398 int pt_old = PT - delta;
9400 /* Skip over glyphs not having an object at the start of the row.
9401 These are special glyphs like truncation marks on terminal
9402 frames. */
9403 if (row->displays_text_p)
9404 while (glyph < end
9405 && INTEGERP (glyph->object)
9406 && glyph->charpos < 0)
9408 x += glyph->pixel_width;
9409 ++glyph;
9412 while (glyph < end
9413 && !INTEGERP (glyph->object)
9414 && (!BUFFERP (glyph->object)
9415 || glyph->charpos < pt_old))
9417 x += glyph->pixel_width;
9418 ++glyph;
9421 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9422 w->cursor.x = x;
9423 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9424 w->cursor.y = row->y + dy;
9426 if (w == XWINDOW (selected_window))
9428 if (!row->continued_p
9429 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9430 && row->x == 0)
9432 this_line_buffer = XBUFFER (w->buffer);
9434 CHARPOS (this_line_start_pos)
9435 = MATRIX_ROW_START_CHARPOS (row) + delta;
9436 BYTEPOS (this_line_start_pos)
9437 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9439 CHARPOS (this_line_end_pos)
9440 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9441 BYTEPOS (this_line_end_pos)
9442 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9444 this_line_y = w->cursor.y;
9445 this_line_pixel_height = row->height;
9446 this_line_vpos = w->cursor.vpos;
9447 this_line_start_x = row->x;
9449 else
9450 CHARPOS (this_line_start_pos) = 0;
9455 /* Run window scroll functions, if any, for WINDOW with new window
9456 start STARTP. Sets the window start of WINDOW to that position.
9458 We assume that the window's buffer is really current. */
9460 static INLINE struct text_pos
9461 run_window_scroll_functions (window, startp)
9462 Lisp_Object window;
9463 struct text_pos startp;
9465 struct window *w = XWINDOW (window);
9466 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9468 if (current_buffer != XBUFFER (w->buffer))
9469 abort ();
9471 if (!NILP (Vwindow_scroll_functions))
9473 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9474 make_number (CHARPOS (startp)));
9475 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9476 /* In case the hook functions switch buffers. */
9477 if (current_buffer != XBUFFER (w->buffer))
9478 set_buffer_internal_1 (XBUFFER (w->buffer));
9481 return startp;
9485 /* Make sure the line containing the cursor is fully visible.
9486 A value of 1 means there is nothing to be done.
9487 (Either the line is fully visible, or it cannot be made so,
9488 or we cannot tell.)
9489 A value of 0 means the caller should do scrolling
9490 as if point had gone off the screen. */
9492 static int
9493 make_cursor_line_fully_visible (w)
9494 struct window *w;
9496 struct glyph_matrix *matrix;
9497 struct glyph_row *row;
9498 int window_height;
9500 /* It's not always possible to find the cursor, e.g, when a window
9501 is full of overlay strings. Don't do anything in that case. */
9502 if (w->cursor.vpos < 0)
9503 return 1;
9505 matrix = w->desired_matrix;
9506 row = MATRIX_ROW (matrix, w->cursor.vpos);
9508 /* If the cursor row is not partially visible, there's nothing to do. */
9509 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9510 return 1;
9512 /* If the row the cursor is in is taller than the window's height,
9513 it's not clear what to do, so do nothing. */
9514 window_height = window_box_height (w);
9515 if (row->height >= window_height)
9516 return 1;
9518 return 0;
9520 #if 0
9521 /* This code used to try to scroll the window just enough to make
9522 the line visible. It returned 0 to say that the caller should
9523 allocate larger glyph matrices. */
9525 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9527 int dy = row->height - row->visible_height;
9528 w->vscroll = 0;
9529 w->cursor.y += dy;
9530 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9532 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9534 int dy = - (row->height - row->visible_height);
9535 w->vscroll = dy;
9536 w->cursor.y += dy;
9537 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9540 /* When we change the cursor y-position of the selected window,
9541 change this_line_y as well so that the display optimization for
9542 the cursor line of the selected window in redisplay_internal uses
9543 the correct y-position. */
9544 if (w == XWINDOW (selected_window))
9545 this_line_y = w->cursor.y;
9547 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9548 redisplay with larger matrices. */
9549 if (matrix->nrows < required_matrix_height (w))
9551 fonts_changed_p = 1;
9552 return 0;
9555 return 1;
9556 #endif /* 0 */
9560 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9561 non-zero means only WINDOW is redisplayed in redisplay_internal.
9562 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9563 in redisplay_window to bring a partially visible line into view in
9564 the case that only the cursor has moved.
9566 Value is
9568 1 if scrolling succeeded
9570 0 if scrolling didn't find point.
9572 -1 if new fonts have been loaded so that we must interrupt
9573 redisplay, adjust glyph matrices, and try again. */
9575 enum
9577 SCROLLING_SUCCESS,
9578 SCROLLING_FAILED,
9579 SCROLLING_NEED_LARGER_MATRICES
9582 static int
9583 try_scrolling (window, just_this_one_p, scroll_conservatively,
9584 scroll_step, temp_scroll_step)
9585 Lisp_Object window;
9586 int just_this_one_p;
9587 EMACS_INT scroll_conservatively, scroll_step;
9588 int temp_scroll_step;
9590 struct window *w = XWINDOW (window);
9591 struct frame *f = XFRAME (w->frame);
9592 struct text_pos scroll_margin_pos;
9593 struct text_pos pos;
9594 struct text_pos startp;
9595 struct it it;
9596 Lisp_Object window_end;
9597 int this_scroll_margin;
9598 int dy = 0;
9599 int scroll_max;
9600 int rc;
9601 int amount_to_scroll = 0;
9602 Lisp_Object aggressive;
9603 int height;
9605 #if GLYPH_DEBUG
9606 debug_method_add (w, "try_scrolling");
9607 #endif
9609 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9611 /* Compute scroll margin height in pixels. We scroll when point is
9612 within this distance from the top or bottom of the window. */
9613 if (scroll_margin > 0)
9615 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9616 this_scroll_margin *= CANON_Y_UNIT (f);
9618 else
9619 this_scroll_margin = 0;
9621 /* Compute how much we should try to scroll maximally to bring point
9622 into view. */
9623 if (scroll_step || scroll_conservatively || temp_scroll_step)
9624 scroll_max = max (scroll_step,
9625 max (scroll_conservatively, temp_scroll_step));
9626 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9627 || NUMBERP (current_buffer->scroll_up_aggressively))
9628 /* We're trying to scroll because of aggressive scrolling
9629 but no scroll_step is set. Choose an arbitrary one. Maybe
9630 there should be a variable for this. */
9631 scroll_max = 10;
9632 else
9633 scroll_max = 0;
9634 scroll_max *= CANON_Y_UNIT (f);
9636 /* Decide whether we have to scroll down. Start at the window end
9637 and move this_scroll_margin up to find the position of the scroll
9638 margin. */
9639 window_end = Fwindow_end (window, Qt);
9640 CHARPOS (scroll_margin_pos) = XINT (window_end);
9641 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9642 if (this_scroll_margin)
9644 start_display (&it, w, scroll_margin_pos);
9645 move_it_vertically (&it, - this_scroll_margin);
9646 scroll_margin_pos = it.current.pos;
9649 if (PT >= CHARPOS (scroll_margin_pos))
9651 int y0;
9653 too_near_end:
9654 /* Point is in the scroll margin at the bottom of the window, or
9655 below. Compute a new window start that makes point visible. */
9657 /* Compute the distance from the scroll margin to PT.
9658 Give up if the distance is greater than scroll_max. */
9659 start_display (&it, w, scroll_margin_pos);
9660 y0 = it.current_y;
9661 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9662 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9664 /* To make point visible, we have to move the window start
9665 down so that the line the cursor is in is visible, which
9666 means we have to add in the height of the cursor line. */
9667 dy = line_bottom_y (&it) - y0;
9669 if (dy > scroll_max)
9670 return SCROLLING_FAILED;
9672 /* Move the window start down. If scrolling conservatively,
9673 move it just enough down to make point visible. If
9674 scroll_step is set, move it down by scroll_step. */
9675 start_display (&it, w, startp);
9677 if (scroll_conservatively)
9678 amount_to_scroll
9679 = max (max (dy, CANON_Y_UNIT (f)),
9680 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9681 else if (scroll_step || temp_scroll_step)
9682 amount_to_scroll = scroll_max;
9683 else
9685 aggressive = current_buffer->scroll_up_aggressively;
9686 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9687 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9688 if (NUMBERP (aggressive))
9689 amount_to_scroll = XFLOATINT (aggressive) * height;
9692 if (amount_to_scroll <= 0)
9693 return SCROLLING_FAILED;
9695 move_it_vertically (&it, amount_to_scroll);
9696 startp = it.current.pos;
9698 else
9700 /* See if point is inside the scroll margin at the top of the
9701 window. */
9702 scroll_margin_pos = startp;
9703 if (this_scroll_margin)
9705 start_display (&it, w, startp);
9706 move_it_vertically (&it, this_scroll_margin);
9707 scroll_margin_pos = it.current.pos;
9710 if (PT < CHARPOS (scroll_margin_pos))
9712 /* Point is in the scroll margin at the top of the window or
9713 above what is displayed in the window. */
9714 int y0;
9716 /* Compute the vertical distance from PT to the scroll
9717 margin position. Give up if distance is greater than
9718 scroll_max. */
9719 SET_TEXT_POS (pos, PT, PT_BYTE);
9720 start_display (&it, w, pos);
9721 y0 = it.current_y;
9722 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9723 it.last_visible_y, -1,
9724 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9725 dy = it.current_y - y0;
9726 if (dy > scroll_max)
9727 return SCROLLING_FAILED;
9729 /* Compute new window start. */
9730 start_display (&it, w, startp);
9732 if (scroll_conservatively)
9733 amount_to_scroll =
9734 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9735 else if (scroll_step || temp_scroll_step)
9736 amount_to_scroll = scroll_max;
9737 else
9739 aggressive = current_buffer->scroll_down_aggressively;
9740 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9741 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9742 if (NUMBERP (aggressive))
9743 amount_to_scroll = XFLOATINT (aggressive) * height;
9746 if (amount_to_scroll <= 0)
9747 return SCROLLING_FAILED;
9749 move_it_vertically (&it, - amount_to_scroll);
9750 startp = it.current.pos;
9754 /* Run window scroll functions. */
9755 startp = run_window_scroll_functions (window, startp);
9757 /* Display the window. Give up if new fonts are loaded, or if point
9758 doesn't appear. */
9759 if (!try_window (window, startp))
9760 rc = SCROLLING_NEED_LARGER_MATRICES;
9761 else if (w->cursor.vpos < 0)
9763 clear_glyph_matrix (w->desired_matrix);
9764 rc = SCROLLING_FAILED;
9766 else
9768 /* Maybe forget recorded base line for line number display. */
9769 if (!just_this_one_p
9770 || current_buffer->clip_changed
9771 || BEG_UNCHANGED < CHARPOS (startp))
9772 w->base_line_number = Qnil;
9774 /* If cursor ends up on a partially visible line,
9775 treat that as being off the bottom of the screen. */
9776 if (! make_cursor_line_fully_visible (w))
9777 goto too_near_end;
9778 rc = SCROLLING_SUCCESS;
9781 return rc;
9785 /* Compute a suitable window start for window W if display of W starts
9786 on a continuation line. Value is non-zero if a new window start
9787 was computed.
9789 The new window start will be computed, based on W's width, starting
9790 from the start of the continued line. It is the start of the
9791 screen line with the minimum distance from the old start W->start. */
9793 static int
9794 compute_window_start_on_continuation_line (w)
9795 struct window *w;
9797 struct text_pos pos, start_pos;
9798 int window_start_changed_p = 0;
9800 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9802 /* If window start is on a continuation line... Window start may be
9803 < BEGV in case there's invisible text at the start of the
9804 buffer (M-x rmail, for example). */
9805 if (CHARPOS (start_pos) > BEGV
9806 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9808 struct it it;
9809 struct glyph_row *row;
9811 /* Handle the case that the window start is out of range. */
9812 if (CHARPOS (start_pos) < BEGV)
9813 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9814 else if (CHARPOS (start_pos) > ZV)
9815 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9817 /* Find the start of the continued line. This should be fast
9818 because scan_buffer is fast (newline cache). */
9819 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9820 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9821 row, DEFAULT_FACE_ID);
9822 reseat_at_previous_visible_line_start (&it);
9824 /* If the line start is "too far" away from the window start,
9825 say it takes too much time to compute a new window start. */
9826 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9827 < XFASTINT (w->height) * XFASTINT (w->width))
9829 int min_distance, distance;
9831 /* Move forward by display lines to find the new window
9832 start. If window width was enlarged, the new start can
9833 be expected to be > the old start. If window width was
9834 decreased, the new window start will be < the old start.
9835 So, we're looking for the display line start with the
9836 minimum distance from the old window start. */
9837 pos = it.current.pos;
9838 min_distance = INFINITY;
9839 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9840 distance < min_distance)
9842 min_distance = distance;
9843 pos = it.current.pos;
9844 move_it_by_lines (&it, 1, 0);
9847 /* Set the window start there. */
9848 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9849 window_start_changed_p = 1;
9853 return window_start_changed_p;
9857 /* Try cursor movement in case text has not changed in window WINDOW,
9858 with window start STARTP. Value is
9860 CURSOR_MOVEMENT_SUCCESS if successful
9862 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9864 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9865 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9866 we want to scroll as if scroll-step were set to 1. See the code.
9868 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9869 which case we have to abort this redisplay, and adjust matrices
9870 first. */
9872 enum
9874 CURSOR_MOVEMENT_SUCCESS,
9875 CURSOR_MOVEMENT_CANNOT_BE_USED,
9876 CURSOR_MOVEMENT_MUST_SCROLL,
9877 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9880 static int
9881 try_cursor_movement (window, startp, scroll_step)
9882 Lisp_Object window;
9883 struct text_pos startp;
9884 int *scroll_step;
9886 struct window *w = XWINDOW (window);
9887 struct frame *f = XFRAME (w->frame);
9888 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9890 #if GLYPH_DEBUG
9891 if (inhibit_try_cursor_movement)
9892 return rc;
9893 #endif
9895 /* Handle case where text has not changed, only point, and it has
9896 not moved off the frame. */
9897 if (/* Point may be in this window. */
9898 PT >= CHARPOS (startp)
9899 /* Selective display hasn't changed. */
9900 && !current_buffer->clip_changed
9901 /* Function force-mode-line-update is used to force a thorough
9902 redisplay. It sets either windows_or_buffers_changed or
9903 update_mode_lines. So don't take a shortcut here for these
9904 cases. */
9905 && !update_mode_lines
9906 && !windows_or_buffers_changed
9907 && !cursor_type_changed
9908 /* Can't use this case if highlighting a region. When a
9909 region exists, cursor movement has to do more than just
9910 set the cursor. */
9911 && !(!NILP (Vtransient_mark_mode)
9912 && !NILP (current_buffer->mark_active))
9913 && NILP (w->region_showing)
9914 && NILP (Vshow_trailing_whitespace)
9915 /* Right after splitting windows, last_point may be nil. */
9916 && INTEGERP (w->last_point)
9917 /* This code is not used for mini-buffer for the sake of the case
9918 of redisplaying to replace an echo area message; since in
9919 that case the mini-buffer contents per se are usually
9920 unchanged. This code is of no real use in the mini-buffer
9921 since the handling of this_line_start_pos, etc., in redisplay
9922 handles the same cases. */
9923 && !EQ (window, minibuf_window)
9924 /* When splitting windows or for new windows, it happens that
9925 redisplay is called with a nil window_end_vpos or one being
9926 larger than the window. This should really be fixed in
9927 window.c. I don't have this on my list, now, so we do
9928 approximately the same as the old redisplay code. --gerd. */
9929 && INTEGERP (w->window_end_vpos)
9930 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9931 && (FRAME_WINDOW_P (f)
9932 || !MARKERP (Voverlay_arrow_position)
9933 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9935 int this_scroll_margin;
9936 struct glyph_row *row = NULL;
9938 #if GLYPH_DEBUG
9939 debug_method_add (w, "cursor movement");
9940 #endif
9942 /* Scroll if point within this distance from the top or bottom
9943 of the window. This is a pixel value. */
9944 this_scroll_margin = max (0, scroll_margin);
9945 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9946 this_scroll_margin *= CANON_Y_UNIT (f);
9948 /* Start with the row the cursor was displayed during the last
9949 not paused redisplay. Give up if that row is not valid. */
9950 if (w->last_cursor.vpos < 0
9951 || w->last_cursor.vpos >= w->current_matrix->nrows)
9952 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9953 else
9955 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9956 if (row->mode_line_p)
9957 ++row;
9958 if (!row->enabled_p)
9959 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9962 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9964 int scroll_p = 0;
9965 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9967 if (PT > XFASTINT (w->last_point))
9969 /* Point has moved forward. */
9970 while (MATRIX_ROW_END_CHARPOS (row) < PT
9971 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9973 xassert (row->enabled_p);
9974 ++row;
9977 /* The end position of a row equals the start position
9978 of the next row. If PT is there, we would rather
9979 display it in the next line. */
9980 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9981 && MATRIX_ROW_END_CHARPOS (row) == PT
9982 && !cursor_row_p (w, row))
9983 ++row;
9985 /* If within the scroll margin, scroll. Note that
9986 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9987 the next line would be drawn, and that
9988 this_scroll_margin can be zero. */
9989 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9990 || PT > MATRIX_ROW_END_CHARPOS (row)
9991 /* Line is completely visible last line in window
9992 and PT is to be set in the next line. */
9993 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9994 && PT == MATRIX_ROW_END_CHARPOS (row)
9995 && !row->ends_at_zv_p
9996 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9997 scroll_p = 1;
9999 else if (PT < XFASTINT (w->last_point))
10001 /* Cursor has to be moved backward. Note that PT >=
10002 CHARPOS (startp) because of the outer
10003 if-statement. */
10004 while (!row->mode_line_p
10005 && (MATRIX_ROW_START_CHARPOS (row) > PT
10006 || (MATRIX_ROW_START_CHARPOS (row) == PT
10007 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10008 && (row->y > this_scroll_margin
10009 || CHARPOS (startp) == BEGV))
10011 xassert (row->enabled_p);
10012 --row;
10015 /* Consider the following case: Window starts at BEGV,
10016 there is invisible, intangible text at BEGV, so that
10017 display starts at some point START > BEGV. It can
10018 happen that we are called with PT somewhere between
10019 BEGV and START. Try to handle that case. */
10020 if (row < w->current_matrix->rows
10021 || row->mode_line_p)
10023 row = w->current_matrix->rows;
10024 if (row->mode_line_p)
10025 ++row;
10028 /* Due to newlines in overlay strings, we may have to
10029 skip forward over overlay strings. */
10030 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10031 && MATRIX_ROW_END_CHARPOS (row) == PT
10032 && !cursor_row_p (w, row))
10033 ++row;
10035 /* If within the scroll margin, scroll. */
10036 if (row->y < this_scroll_margin
10037 && CHARPOS (startp) != BEGV)
10038 scroll_p = 1;
10041 if (PT < MATRIX_ROW_START_CHARPOS (row)
10042 || PT > MATRIX_ROW_END_CHARPOS (row))
10044 /* if PT is not in the glyph row, give up. */
10045 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10047 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10049 if (PT == MATRIX_ROW_END_CHARPOS (row)
10050 && !row->ends_at_zv_p
10051 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10052 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10053 else if (row->height > window_box_height (w))
10055 /* If we end up in a partially visible line, let's
10056 make it fully visible, except when it's taller
10057 than the window, in which case we can't do much
10058 about it. */
10059 *scroll_step = 1;
10060 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10062 else
10064 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10065 try_window (window, startp);
10066 if (!make_cursor_line_fully_visible (w))
10067 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10068 else
10069 rc = CURSOR_MOVEMENT_SUCCESS;
10072 else if (scroll_p)
10073 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10074 else
10076 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10077 rc = CURSOR_MOVEMENT_SUCCESS;
10082 return rc;
10086 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10087 selected_window is redisplayed. */
10089 static void
10090 redisplay_window (window, just_this_one_p)
10091 Lisp_Object window;
10092 int just_this_one_p;
10094 struct window *w = XWINDOW (window);
10095 struct frame *f = XFRAME (w->frame);
10096 struct buffer *buffer = XBUFFER (w->buffer);
10097 struct buffer *old = current_buffer;
10098 struct text_pos lpoint, opoint, startp;
10099 int update_mode_line;
10100 int tem;
10101 struct it it;
10102 /* Record it now because it's overwritten. */
10103 int current_matrix_up_to_date_p = 0;
10104 /* This is less strict than current_matrix_up_to_date_p.
10105 It indictes that the buffer contents and narrowing are unchanged. */
10106 int buffer_unchanged_p = 0;
10107 int temp_scroll_step = 0;
10108 int count = SPECPDL_INDEX ();
10109 int rc;
10110 int centering_position;
10112 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10113 opoint = lpoint;
10115 /* W must be a leaf window here. */
10116 xassert (!NILP (w->buffer));
10117 #if GLYPH_DEBUG
10118 *w->desired_matrix->method = 0;
10119 #endif
10121 specbind (Qinhibit_point_motion_hooks, Qt);
10123 reconsider_clip_changes (w, buffer);
10125 /* Has the mode line to be updated? */
10126 update_mode_line = (!NILP (w->update_mode_line)
10127 || update_mode_lines
10128 || buffer->clip_changed
10129 || buffer->prevent_redisplay_optimizations_p);
10131 if (MINI_WINDOW_P (w))
10133 if (w == XWINDOW (echo_area_window)
10134 && !NILP (echo_area_buffer[0]))
10136 if (update_mode_line)
10137 /* We may have to update a tty frame's menu bar or a
10138 tool-bar. Example `M-x C-h C-h C-g'. */
10139 goto finish_menu_bars;
10140 else
10141 /* We've already displayed the echo area glyphs in this window. */
10142 goto finish_scroll_bars;
10144 else if (w != XWINDOW (minibuf_window))
10146 /* W is a mini-buffer window, but it's not the currently
10147 active one, so clear it. */
10148 int yb = window_text_bottom_y (w);
10149 struct glyph_row *row;
10150 int y;
10152 for (y = 0, row = w->desired_matrix->rows;
10153 y < yb;
10154 y += row->height, ++row)
10155 blank_row (w, row, y);
10156 goto finish_scroll_bars;
10159 clear_glyph_matrix (w->desired_matrix);
10162 /* Otherwise set up data on this window; select its buffer and point
10163 value. */
10164 /* Really select the buffer, for the sake of buffer-local
10165 variables. */
10166 set_buffer_internal_1 (XBUFFER (w->buffer));
10167 SET_TEXT_POS (opoint, PT, PT_BYTE);
10169 current_matrix_up_to_date_p
10170 = (!NILP (w->window_end_valid)
10171 && !current_buffer->clip_changed
10172 && !current_buffer->prevent_redisplay_optimizations_p
10173 && XFASTINT (w->last_modified) >= MODIFF
10174 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10176 buffer_unchanged_p
10177 = (!NILP (w->window_end_valid)
10178 && !current_buffer->clip_changed
10179 && XFASTINT (w->last_modified) >= MODIFF
10180 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10182 /* When windows_or_buffers_changed is non-zero, we can't rely on
10183 the window end being valid, so set it to nil there. */
10184 if (windows_or_buffers_changed)
10186 /* If window starts on a continuation line, maybe adjust the
10187 window start in case the window's width changed. */
10188 if (XMARKER (w->start)->buffer == current_buffer)
10189 compute_window_start_on_continuation_line (w);
10191 w->window_end_valid = Qnil;
10194 /* Some sanity checks. */
10195 CHECK_WINDOW_END (w);
10196 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10197 abort ();
10198 if (BYTEPOS (opoint) < CHARPOS (opoint))
10199 abort ();
10201 /* If %c is in mode line, update it if needed. */
10202 if (!NILP (w->column_number_displayed)
10203 /* This alternative quickly identifies a common case
10204 where no change is needed. */
10205 && !(PT == XFASTINT (w->last_point)
10206 && XFASTINT (w->last_modified) >= MODIFF
10207 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10208 && (XFASTINT (w->column_number_displayed)
10209 != (int) current_column ())) /* iftc */
10210 update_mode_line = 1;
10212 /* Count number of windows showing the selected buffer. An indirect
10213 buffer counts as its base buffer. */
10214 if (!just_this_one_p)
10216 struct buffer *current_base, *window_base;
10217 current_base = current_buffer;
10218 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10219 if (current_base->base_buffer)
10220 current_base = current_base->base_buffer;
10221 if (window_base->base_buffer)
10222 window_base = window_base->base_buffer;
10223 if (current_base == window_base)
10224 buffer_shared++;
10227 /* Point refers normally to the selected window. For any other
10228 window, set up appropriate value. */
10229 if (!EQ (window, selected_window))
10231 int new_pt = XMARKER (w->pointm)->charpos;
10232 int new_pt_byte = marker_byte_position (w->pointm);
10233 if (new_pt < BEGV)
10235 new_pt = BEGV;
10236 new_pt_byte = BEGV_BYTE;
10237 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10239 else if (new_pt > (ZV - 1))
10241 new_pt = ZV;
10242 new_pt_byte = ZV_BYTE;
10243 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10246 /* We don't use SET_PT so that the point-motion hooks don't run. */
10247 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10250 /* If any of the character widths specified in the display table
10251 have changed, invalidate the width run cache. It's true that
10252 this may be a bit late to catch such changes, but the rest of
10253 redisplay goes (non-fatally) haywire when the display table is
10254 changed, so why should we worry about doing any better? */
10255 if (current_buffer->width_run_cache)
10257 struct Lisp_Char_Table *disptab = buffer_display_table ();
10259 if (! disptab_matches_widthtab (disptab,
10260 XVECTOR (current_buffer->width_table)))
10262 invalidate_region_cache (current_buffer,
10263 current_buffer->width_run_cache,
10264 BEG, Z);
10265 recompute_width_table (current_buffer, disptab);
10269 /* If window-start is screwed up, choose a new one. */
10270 if (XMARKER (w->start)->buffer != current_buffer)
10271 goto recenter;
10273 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10275 /* If someone specified a new starting point but did not insist,
10276 check whether it can be used. */
10277 if (!NILP (w->optional_new_start)
10278 && CHARPOS (startp) >= BEGV
10279 && CHARPOS (startp) <= ZV)
10281 w->optional_new_start = Qnil;
10282 start_display (&it, w, startp);
10283 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10284 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10285 if (IT_CHARPOS (it) == PT)
10286 w->force_start = Qt;
10289 /* Handle case where place to start displaying has been specified,
10290 unless the specified location is outside the accessible range. */
10291 if (!NILP (w->force_start)
10292 || w->frozen_window_start_p)
10294 w->force_start = Qnil;
10295 w->vscroll = 0;
10296 w->window_end_valid = Qnil;
10298 /* Forget any recorded base line for line number display. */
10299 if (!buffer_unchanged_p)
10300 w->base_line_number = Qnil;
10302 /* Redisplay the mode line. Select the buffer properly for that.
10303 Also, run the hook window-scroll-functions
10304 because we have scrolled. */
10305 /* Note, we do this after clearing force_start because
10306 if there's an error, it is better to forget about force_start
10307 than to get into an infinite loop calling the hook functions
10308 and having them get more errors. */
10309 if (!update_mode_line
10310 || ! NILP (Vwindow_scroll_functions))
10312 update_mode_line = 1;
10313 w->update_mode_line = Qt;
10314 startp = run_window_scroll_functions (window, startp);
10317 w->last_modified = make_number (0);
10318 w->last_overlay_modified = make_number (0);
10319 if (CHARPOS (startp) < BEGV)
10320 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10321 else if (CHARPOS (startp) > ZV)
10322 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10324 /* Redisplay, then check if cursor has been set during the
10325 redisplay. Give up if new fonts were loaded. */
10326 if (!try_window (window, startp))
10328 w->force_start = Qt;
10329 clear_glyph_matrix (w->desired_matrix);
10330 goto finish_scroll_bars;
10333 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10335 /* If point does not appear, try to move point so it does
10336 appear. The desired matrix has been built above, so we
10337 can use it here. */
10338 int window_height;
10339 struct glyph_row *row;
10341 window_height = window_box_height (w) / 2;
10342 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10343 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10344 ++row;
10346 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10347 MATRIX_ROW_START_BYTEPOS (row));
10349 if (w != XWINDOW (selected_window))
10350 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10351 else if (current_buffer == old)
10352 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10354 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10356 /* If we are highlighting the region, then we just changed
10357 the region, so redisplay to show it. */
10358 if (!NILP (Vtransient_mark_mode)
10359 && !NILP (current_buffer->mark_active))
10361 clear_glyph_matrix (w->desired_matrix);
10362 if (!try_window (window, startp))
10363 goto need_larger_matrices;
10367 if (!make_cursor_line_fully_visible (w))
10368 goto try_to_scroll;
10369 #if GLYPH_DEBUG
10370 debug_method_add (w, "forced window start");
10371 #endif
10372 goto done;
10375 /* Handle case where text has not changed, only point, and it has
10376 not moved off the frame, and we are not retrying after hscroll.
10377 (current_matrix_up_to_date_p is nonzero when retrying.) */
10378 if (current_matrix_up_to_date_p
10379 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10380 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10382 switch (rc)
10384 case CURSOR_MOVEMENT_SUCCESS:
10385 goto done;
10387 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10388 goto need_larger_matrices;
10390 case CURSOR_MOVEMENT_MUST_SCROLL:
10391 goto try_to_scroll;
10393 default:
10394 abort ();
10397 /* If current starting point was originally the beginning of a line
10398 but no longer is, find a new starting point. */
10399 else if (!NILP (w->start_at_line_beg)
10400 && !(CHARPOS (startp) <= BEGV
10401 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10403 #if GLYPH_DEBUG
10404 debug_method_add (w, "recenter 1");
10405 #endif
10406 goto recenter;
10409 /* Try scrolling with try_window_id. Value is > 0 if update has
10410 been done, it is -1 if we know that the same window start will
10411 not work. It is 0 if unsuccessful for some other reason. */
10412 else if ((tem = try_window_id (w)) != 0)
10414 #if GLYPH_DEBUG
10415 debug_method_add (w, "try_window_id %d", tem);
10416 #endif
10418 if (fonts_changed_p)
10419 goto need_larger_matrices;
10420 if (tem > 0)
10421 goto done;
10423 /* Otherwise try_window_id has returned -1 which means that we
10424 don't want the alternative below this comment to execute. */
10426 else if (CHARPOS (startp) >= BEGV
10427 && CHARPOS (startp) <= ZV
10428 && PT >= CHARPOS (startp)
10429 && (CHARPOS (startp) < ZV
10430 /* Avoid starting at end of buffer. */
10431 || CHARPOS (startp) == BEGV
10432 || (XFASTINT (w->last_modified) >= MODIFF
10433 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10435 #if GLYPH_DEBUG
10436 debug_method_add (w, "same window start");
10437 #endif
10439 /* Try to redisplay starting at same place as before.
10440 If point has not moved off frame, accept the results. */
10441 if (!current_matrix_up_to_date_p
10442 /* Don't use try_window_reusing_current_matrix in this case
10443 because a window scroll function can have changed the
10444 buffer. */
10445 || !NILP (Vwindow_scroll_functions)
10446 || MINI_WINDOW_P (w)
10447 || !try_window_reusing_current_matrix (w))
10449 IF_DEBUG (debug_method_add (w, "1"));
10450 try_window (window, startp);
10453 if (fonts_changed_p)
10454 goto need_larger_matrices;
10456 if (w->cursor.vpos >= 0)
10458 if (!just_this_one_p
10459 || current_buffer->clip_changed
10460 || BEG_UNCHANGED < CHARPOS (startp))
10461 /* Forget any recorded base line for line number display. */
10462 w->base_line_number = Qnil;
10464 if (!make_cursor_line_fully_visible (w))
10465 /* Drop through and scroll. */
10467 goto done;
10469 else
10470 clear_glyph_matrix (w->desired_matrix);
10473 try_to_scroll:
10475 w->last_modified = make_number (0);
10476 w->last_overlay_modified = make_number (0);
10478 /* Redisplay the mode line. Select the buffer properly for that. */
10479 if (!update_mode_line)
10481 update_mode_line = 1;
10482 w->update_mode_line = Qt;
10485 /* Try to scroll by specified few lines. */
10486 if ((scroll_conservatively
10487 || scroll_step
10488 || temp_scroll_step
10489 || NUMBERP (current_buffer->scroll_up_aggressively)
10490 || NUMBERP (current_buffer->scroll_down_aggressively))
10491 && !current_buffer->clip_changed
10492 && CHARPOS (startp) >= BEGV
10493 && CHARPOS (startp) <= ZV)
10495 /* The function returns -1 if new fonts were loaded, 1 if
10496 successful, 0 if not successful. */
10497 int rc = try_scrolling (window, just_this_one_p,
10498 scroll_conservatively,
10499 scroll_step,
10500 temp_scroll_step);
10501 switch (rc)
10503 case SCROLLING_SUCCESS:
10504 goto done;
10506 case SCROLLING_NEED_LARGER_MATRICES:
10507 goto need_larger_matrices;
10509 case SCROLLING_FAILED:
10510 break;
10512 default:
10513 abort ();
10517 /* Finally, just choose place to start which centers point */
10519 recenter:
10520 centering_position = window_box_height (w) / 2;
10522 point_at_top:
10523 /* Jump here with centering_position already set to 0. */
10525 #if GLYPH_DEBUG
10526 debug_method_add (w, "recenter");
10527 #endif
10529 /* w->vscroll = 0; */
10531 /* Forget any previously recorded base line for line number display. */
10532 if (!buffer_unchanged_p)
10533 w->base_line_number = Qnil;
10535 /* Move backward half the height of the window. */
10536 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10537 it.current_y = it.last_visible_y;
10538 move_it_vertically_backward (&it, centering_position);
10539 xassert (IT_CHARPOS (it) >= BEGV);
10541 /* The function move_it_vertically_backward may move over more
10542 than the specified y-distance. If it->w is small, e.g. a
10543 mini-buffer window, we may end up in front of the window's
10544 display area. Start displaying at the start of the line
10545 containing PT in this case. */
10546 if (it.current_y <= 0)
10548 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10549 move_it_vertically (&it, 0);
10550 xassert (IT_CHARPOS (it) <= PT);
10551 it.current_y = 0;
10554 it.current_x = it.hpos = 0;
10556 /* Set startp here explicitly in case that helps avoid an infinite loop
10557 in case the window-scroll-functions functions get errors. */
10558 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10560 /* Run scroll hooks. */
10561 startp = run_window_scroll_functions (window, it.current.pos);
10563 /* Redisplay the window. */
10564 if (!current_matrix_up_to_date_p
10565 || windows_or_buffers_changed
10566 || cursor_type_changed
10567 /* Don't use try_window_reusing_current_matrix in this case
10568 because it can have changed the buffer. */
10569 || !NILP (Vwindow_scroll_functions)
10570 || !just_this_one_p
10571 || MINI_WINDOW_P (w)
10572 || !try_window_reusing_current_matrix (w))
10573 try_window (window, startp);
10575 /* If new fonts have been loaded (due to fontsets), give up. We
10576 have to start a new redisplay since we need to re-adjust glyph
10577 matrices. */
10578 if (fonts_changed_p)
10579 goto need_larger_matrices;
10581 /* If cursor did not appear assume that the middle of the window is
10582 in the first line of the window. Do it again with the next line.
10583 (Imagine a window of height 100, displaying two lines of height
10584 60. Moving back 50 from it->last_visible_y will end in the first
10585 line.) */
10586 if (w->cursor.vpos < 0)
10588 if (!NILP (w->window_end_valid)
10589 && PT >= Z - XFASTINT (w->window_end_pos))
10591 clear_glyph_matrix (w->desired_matrix);
10592 move_it_by_lines (&it, 1, 0);
10593 try_window (window, it.current.pos);
10595 else if (PT < IT_CHARPOS (it))
10597 clear_glyph_matrix (w->desired_matrix);
10598 move_it_by_lines (&it, -1, 0);
10599 try_window (window, it.current.pos);
10601 else
10603 /* Not much we can do about it. */
10607 /* Consider the following case: Window starts at BEGV, there is
10608 invisible, intangible text at BEGV, so that display starts at
10609 some point START > BEGV. It can happen that we are called with
10610 PT somewhere between BEGV and START. Try to handle that case. */
10611 if (w->cursor.vpos < 0)
10613 struct glyph_row *row = w->current_matrix->rows;
10614 if (row->mode_line_p)
10615 ++row;
10616 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10619 if (!make_cursor_line_fully_visible (w))
10621 /* If centering point failed to make the whole line visible,
10622 put point at the top instead. That has to make the whole line
10623 visible, if it can be done. */
10624 centering_position = 0;
10625 goto point_at_top;
10628 done:
10630 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10631 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10632 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10633 ? Qt : Qnil);
10635 /* Display the mode line, if we must. */
10636 if ((update_mode_line
10637 /* If window not full width, must redo its mode line
10638 if (a) the window to its side is being redone and
10639 (b) we do a frame-based redisplay. This is a consequence
10640 of how inverted lines are drawn in frame-based redisplay. */
10641 || (!just_this_one_p
10642 && !FRAME_WINDOW_P (f)
10643 && !WINDOW_FULL_WIDTH_P (w))
10644 /* Line number to display. */
10645 || INTEGERP (w->base_line_pos)
10646 /* Column number is displayed and different from the one displayed. */
10647 || (!NILP (w->column_number_displayed)
10648 && (XFASTINT (w->column_number_displayed)
10649 != (int) current_column ()))) /* iftc */
10650 /* This means that the window has a mode line. */
10651 && (WINDOW_WANTS_MODELINE_P (w)
10652 || WINDOW_WANTS_HEADER_LINE_P (w)))
10654 display_mode_lines (w);
10656 /* If mode line height has changed, arrange for a thorough
10657 immediate redisplay using the correct mode line height. */
10658 if (WINDOW_WANTS_MODELINE_P (w)
10659 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10661 fonts_changed_p = 1;
10662 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10663 = DESIRED_MODE_LINE_HEIGHT (w);
10666 /* If top line height has changed, arrange for a thorough
10667 immediate redisplay using the correct mode line height. */
10668 if (WINDOW_WANTS_HEADER_LINE_P (w)
10669 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10671 fonts_changed_p = 1;
10672 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10673 = DESIRED_HEADER_LINE_HEIGHT (w);
10676 if (fonts_changed_p)
10677 goto need_larger_matrices;
10680 if (!line_number_displayed
10681 && !BUFFERP (w->base_line_pos))
10683 w->base_line_pos = Qnil;
10684 w->base_line_number = Qnil;
10687 finish_menu_bars:
10689 /* When we reach a frame's selected window, redo the frame's menu bar. */
10690 if (update_mode_line
10691 && EQ (FRAME_SELECTED_WINDOW (f), window))
10693 int redisplay_menu_p = 0;
10695 if (FRAME_WINDOW_P (f))
10697 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10698 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10699 #else
10700 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10701 #endif
10703 else
10704 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10706 if (redisplay_menu_p)
10707 display_menu_bar (w);
10709 #ifdef HAVE_WINDOW_SYSTEM
10710 if (WINDOWP (f->tool_bar_window)
10711 && (FRAME_TOOL_BAR_LINES (f) > 0
10712 || auto_resize_tool_bars_p))
10713 redisplay_tool_bar (f);
10714 #endif
10717 need_larger_matrices:
10719 finish_scroll_bars:
10721 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10723 int start, end, whole;
10725 /* Calculate the start and end positions for the current window.
10726 At some point, it would be nice to choose between scrollbars
10727 which reflect the whole buffer size, with special markers
10728 indicating narrowing, and scrollbars which reflect only the
10729 visible region.
10731 Note that mini-buffers sometimes aren't displaying any text. */
10732 if (!MINI_WINDOW_P (w)
10733 || (w == XWINDOW (minibuf_window)
10734 && NILP (echo_area_buffer[0])))
10736 whole = ZV - BEGV;
10737 start = marker_position (w->start) - BEGV;
10738 /* I don't think this is guaranteed to be right. For the
10739 moment, we'll pretend it is. */
10740 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10742 if (end < start)
10743 end = start;
10744 if (whole < (end - start))
10745 whole = end - start;
10747 else
10748 start = end = whole = 0;
10750 /* Indicate what this scroll bar ought to be displaying now. */
10751 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10753 /* Note that we actually used the scroll bar attached to this
10754 window, so it shouldn't be deleted at the end of redisplay. */
10755 redeem_scroll_bar_hook (w);
10758 /* Restore current_buffer and value of point in it. */
10759 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10760 set_buffer_internal_1 (old);
10761 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10763 unbind_to (count, Qnil);
10767 /* Build the complete desired matrix of WINDOW with a window start
10768 buffer position POS. Value is non-zero if successful. It is zero
10769 if fonts were loaded during redisplay which makes re-adjusting
10770 glyph matrices necessary. */
10773 try_window (window, pos)
10774 Lisp_Object window;
10775 struct text_pos pos;
10777 struct window *w = XWINDOW (window);
10778 struct it it;
10779 struct glyph_row *last_text_row = NULL;
10781 /* Make POS the new window start. */
10782 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10784 /* Mark cursor position as unknown. No overlay arrow seen. */
10785 w->cursor.vpos = -1;
10786 overlay_arrow_seen = 0;
10788 /* Initialize iterator and info to start at POS. */
10789 start_display (&it, w, pos);
10791 /* Display all lines of W. */
10792 while (it.current_y < it.last_visible_y)
10794 if (display_line (&it))
10795 last_text_row = it.glyph_row - 1;
10796 if (fonts_changed_p)
10797 return 0;
10800 /* If bottom moved off end of frame, change mode line percentage. */
10801 if (XFASTINT (w->window_end_pos) <= 0
10802 && Z != IT_CHARPOS (it))
10803 w->update_mode_line = Qt;
10805 /* Set window_end_pos to the offset of the last character displayed
10806 on the window from the end of current_buffer. Set
10807 window_end_vpos to its row number. */
10808 if (last_text_row)
10810 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10811 w->window_end_bytepos
10812 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10813 w->window_end_pos
10814 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10815 w->window_end_vpos
10816 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10817 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10818 ->displays_text_p);
10820 else
10822 w->window_end_bytepos = 0;
10823 w->window_end_pos = w->window_end_vpos = make_number (0);
10826 /* But that is not valid info until redisplay finishes. */
10827 w->window_end_valid = Qnil;
10828 return 1;
10833 /************************************************************************
10834 Window redisplay reusing current matrix when buffer has not changed
10835 ************************************************************************/
10837 /* Try redisplay of window W showing an unchanged buffer with a
10838 different window start than the last time it was displayed by
10839 reusing its current matrix. Value is non-zero if successful.
10840 W->start is the new window start. */
10842 static int
10843 try_window_reusing_current_matrix (w)
10844 struct window *w;
10846 struct frame *f = XFRAME (w->frame);
10847 struct glyph_row *row, *bottom_row;
10848 struct it it;
10849 struct run run;
10850 struct text_pos start, new_start;
10851 int nrows_scrolled, i;
10852 struct glyph_row *last_text_row;
10853 struct glyph_row *last_reused_text_row;
10854 struct glyph_row *start_row;
10855 int start_vpos, min_y, max_y;
10857 #if GLYPH_DEBUG
10858 if (inhibit_try_window_reusing)
10859 return 0;
10860 #endif
10862 if (/* This function doesn't handle terminal frames. */
10863 !FRAME_WINDOW_P (f)
10864 /* Don't try to reuse the display if windows have been split
10865 or such. */
10866 || windows_or_buffers_changed
10867 || cursor_type_changed)
10868 return 0;
10870 /* Can't do this if region may have changed. */
10871 if ((!NILP (Vtransient_mark_mode)
10872 && !NILP (current_buffer->mark_active))
10873 || !NILP (w->region_showing)
10874 || !NILP (Vshow_trailing_whitespace))
10875 return 0;
10877 /* If top-line visibility has changed, give up. */
10878 if (WINDOW_WANTS_HEADER_LINE_P (w)
10879 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10880 return 0;
10882 /* Give up if old or new display is scrolled vertically. We could
10883 make this function handle this, but right now it doesn't. */
10884 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10885 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10886 return 0;
10888 /* The variable new_start now holds the new window start. The old
10889 start `start' can be determined from the current matrix. */
10890 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10891 start = start_row->start.pos;
10892 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10894 /* Clear the desired matrix for the display below. */
10895 clear_glyph_matrix (w->desired_matrix);
10897 if (CHARPOS (new_start) <= CHARPOS (start))
10899 int first_row_y;
10901 /* Don't use this method if the display starts with an ellipsis
10902 displayed for invisible text. It's not easy to handle that case
10903 below, and it's certainly not worth the effort since this is
10904 not a frequent case. */
10905 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10906 return 0;
10908 IF_DEBUG (debug_method_add (w, "twu1"));
10910 /* Display up to a row that can be reused. The variable
10911 last_text_row is set to the last row displayed that displays
10912 text. Note that it.vpos == 0 if or if not there is a
10913 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10914 start_display (&it, w, new_start);
10915 first_row_y = it.current_y;
10916 w->cursor.vpos = -1;
10917 last_text_row = last_reused_text_row = NULL;
10919 while (it.current_y < it.last_visible_y
10920 && IT_CHARPOS (it) < CHARPOS (start)
10921 && !fonts_changed_p)
10922 if (display_line (&it))
10923 last_text_row = it.glyph_row - 1;
10925 /* A value of current_y < last_visible_y means that we stopped
10926 at the previous window start, which in turn means that we
10927 have at least one reusable row. */
10928 if (it.current_y < it.last_visible_y)
10930 /* IT.vpos always starts from 0; it counts text lines. */
10931 nrows_scrolled = it.vpos;
10933 /* Find PT if not already found in the lines displayed. */
10934 if (w->cursor.vpos < 0)
10936 int dy = it.current_y - first_row_y;
10938 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10939 row = row_containing_pos (w, PT, row, NULL, dy);
10940 if (row)
10941 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10942 dy, nrows_scrolled);
10943 else
10945 clear_glyph_matrix (w->desired_matrix);
10946 return 0;
10950 /* Scroll the display. Do it before the current matrix is
10951 changed. The problem here is that update has not yet
10952 run, i.e. part of the current matrix is not up to date.
10953 scroll_run_hook will clear the cursor, and use the
10954 current matrix to get the height of the row the cursor is
10955 in. */
10956 run.current_y = first_row_y;
10957 run.desired_y = it.current_y;
10958 run.height = it.last_visible_y - it.current_y;
10960 if (run.height > 0 && run.current_y != run.desired_y)
10962 update_begin (f);
10963 rif->update_window_begin_hook (w);
10964 rif->clear_mouse_face (w);
10965 rif->scroll_run_hook (w, &run);
10966 rif->update_window_end_hook (w, 0, 0);
10967 update_end (f);
10970 /* Shift current matrix down by nrows_scrolled lines. */
10971 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10972 rotate_matrix (w->current_matrix,
10973 start_vpos,
10974 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10975 nrows_scrolled);
10977 /* Disable lines that must be updated. */
10978 for (i = 0; i < it.vpos; ++i)
10979 (start_row + i)->enabled_p = 0;
10981 /* Re-compute Y positions. */
10982 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10983 max_y = it.last_visible_y;
10984 for (row = start_row + nrows_scrolled;
10985 row < bottom_row;
10986 ++row)
10988 row->y = it.current_y;
10989 row->visible_height = row->height;
10991 if (row->y < min_y)
10992 row->visible_height -= min_y - row->y;
10993 if (row->y + row->height > max_y)
10994 row->visible_height -= row->y + row->height - max_y;
10996 it.current_y += row->height;
10998 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10999 last_reused_text_row = row;
11000 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11001 break;
11004 /* Disable lines in the current matrix which are now
11005 below the window. */
11006 for (++row; row < bottom_row; ++row)
11007 row->enabled_p = 0;
11010 /* Update window_end_pos etc.; last_reused_text_row is the last
11011 reused row from the current matrix containing text, if any.
11012 The value of last_text_row is the last displayed line
11013 containing text. */
11014 if (last_reused_text_row)
11016 w->window_end_bytepos
11017 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11018 w->window_end_pos
11019 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11020 w->window_end_vpos
11021 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11022 w->current_matrix));
11024 else if (last_text_row)
11026 w->window_end_bytepos
11027 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11028 w->window_end_pos
11029 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11030 w->window_end_vpos
11031 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11033 else
11035 /* This window must be completely empty. */
11036 w->window_end_bytepos = 0;
11037 w->window_end_pos = w->window_end_vpos = make_number (0);
11039 w->window_end_valid = Qnil;
11041 /* Update hint: don't try scrolling again in update_window. */
11042 w->desired_matrix->no_scrolling_p = 1;
11044 #if GLYPH_DEBUG
11045 debug_method_add (w, "try_window_reusing_current_matrix 1");
11046 #endif
11047 return 1;
11049 else if (CHARPOS (new_start) > CHARPOS (start))
11051 struct glyph_row *pt_row, *row;
11052 struct glyph_row *first_reusable_row;
11053 struct glyph_row *first_row_to_display;
11054 int dy;
11055 int yb = window_text_bottom_y (w);
11057 /* Find the row starting at new_start, if there is one. Don't
11058 reuse a partially visible line at the end. */
11059 first_reusable_row = start_row;
11060 while (first_reusable_row->enabled_p
11061 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11062 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11063 < CHARPOS (new_start)))
11064 ++first_reusable_row;
11066 /* Give up if there is no row to reuse. */
11067 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11068 || !first_reusable_row->enabled_p
11069 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11070 != CHARPOS (new_start)))
11071 return 0;
11073 /* We can reuse fully visible rows beginning with
11074 first_reusable_row to the end of the window. Set
11075 first_row_to_display to the first row that cannot be reused.
11076 Set pt_row to the row containing point, if there is any. */
11077 pt_row = NULL;
11078 for (first_row_to_display = first_reusable_row;
11079 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11080 ++first_row_to_display)
11082 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11083 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11084 pt_row = first_row_to_display;
11087 /* Start displaying at the start of first_row_to_display. */
11088 xassert (first_row_to_display->y < yb);
11089 init_to_row_start (&it, w, first_row_to_display);
11091 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11092 - start_vpos);
11093 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11094 - nrows_scrolled);
11095 it.current_y = (first_row_to_display->y - first_reusable_row->y
11096 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11098 /* Display lines beginning with first_row_to_display in the
11099 desired matrix. Set last_text_row to the last row displayed
11100 that displays text. */
11101 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11102 if (pt_row == NULL)
11103 w->cursor.vpos = -1;
11104 last_text_row = NULL;
11105 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11106 if (display_line (&it))
11107 last_text_row = it.glyph_row - 1;
11109 /* Give up If point isn't in a row displayed or reused. */
11110 if (w->cursor.vpos < 0)
11112 clear_glyph_matrix (w->desired_matrix);
11113 return 0;
11116 /* If point is in a reused row, adjust y and vpos of the cursor
11117 position. */
11118 if (pt_row)
11120 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11121 w->current_matrix);
11122 w->cursor.y -= first_reusable_row->y;
11125 /* Scroll the display. */
11126 run.current_y = first_reusable_row->y;
11127 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11128 run.height = it.last_visible_y - run.current_y;
11129 dy = run.current_y - run.desired_y;
11131 if (run.height)
11133 struct frame *f = XFRAME (WINDOW_FRAME (w));
11134 update_begin (f);
11135 rif->update_window_begin_hook (w);
11136 rif->clear_mouse_face (w);
11137 rif->scroll_run_hook (w, &run);
11138 rif->update_window_end_hook (w, 0, 0);
11139 update_end (f);
11142 /* Adjust Y positions of reused rows. */
11143 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11144 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11145 max_y = it.last_visible_y;
11146 for (row = first_reusable_row; row < first_row_to_display; ++row)
11148 row->y -= dy;
11149 row->visible_height = row->height;
11150 if (row->y < min_y)
11151 row->visible_height -= min_y - row->y;
11152 if (row->y + row->height > max_y)
11153 row->visible_height -= row->y + row->height - max_y;
11156 /* Scroll the current matrix. */
11157 xassert (nrows_scrolled > 0);
11158 rotate_matrix (w->current_matrix,
11159 start_vpos,
11160 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11161 -nrows_scrolled);
11163 /* Disable rows not reused. */
11164 for (row -= nrows_scrolled; row < bottom_row; ++row)
11165 row->enabled_p = 0;
11167 /* Adjust window end. A null value of last_text_row means that
11168 the window end is in reused rows which in turn means that
11169 only its vpos can have changed. */
11170 if (last_text_row)
11172 w->window_end_bytepos
11173 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11174 w->window_end_pos
11175 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11176 w->window_end_vpos
11177 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11179 else
11181 w->window_end_vpos
11182 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11185 w->window_end_valid = Qnil;
11186 w->desired_matrix->no_scrolling_p = 1;
11188 #if GLYPH_DEBUG
11189 debug_method_add (w, "try_window_reusing_current_matrix 2");
11190 #endif
11191 return 1;
11194 return 0;
11199 /************************************************************************
11200 Window redisplay reusing current matrix when buffer has changed
11201 ************************************************************************/
11203 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11204 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11205 int *, int *));
11206 static struct glyph_row *
11207 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11208 struct glyph_row *));
11211 /* Return the last row in MATRIX displaying text. If row START is
11212 non-null, start searching with that row. IT gives the dimensions
11213 of the display. Value is null if matrix is empty; otherwise it is
11214 a pointer to the row found. */
11216 static struct glyph_row *
11217 find_last_row_displaying_text (matrix, it, start)
11218 struct glyph_matrix *matrix;
11219 struct it *it;
11220 struct glyph_row *start;
11222 struct glyph_row *row, *row_found;
11224 /* Set row_found to the last row in IT->w's current matrix
11225 displaying text. The loop looks funny but think of partially
11226 visible lines. */
11227 row_found = NULL;
11228 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11229 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11231 xassert (row->enabled_p);
11232 row_found = row;
11233 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11234 break;
11235 ++row;
11238 return row_found;
11242 /* Return the last row in the current matrix of W that is not affected
11243 by changes at the start of current_buffer that occurred since W's
11244 current matrix was built. Value is null if no such row exists.
11246 BEG_UNCHANGED us the number of characters unchanged at the start of
11247 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11248 first changed character in current_buffer. Characters at positions <
11249 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11250 when the current matrix was built. */
11252 static struct glyph_row *
11253 find_last_unchanged_at_beg_row (w)
11254 struct window *w;
11256 int first_changed_pos = BEG + BEG_UNCHANGED;
11257 struct glyph_row *row;
11258 struct glyph_row *row_found = NULL;
11259 int yb = window_text_bottom_y (w);
11261 /* Find the last row displaying unchanged text. */
11262 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11263 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11264 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11266 if (/* If row ends before first_changed_pos, it is unchanged,
11267 except in some case. */
11268 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11269 /* When row ends in ZV and we write at ZV it is not
11270 unchanged. */
11271 && !row->ends_at_zv_p
11272 /* When first_changed_pos is the end of a continued line,
11273 row is not unchanged because it may be no longer
11274 continued. */
11275 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11276 && row->continued_p))
11277 row_found = row;
11279 /* Stop if last visible row. */
11280 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11281 break;
11283 ++row;
11286 return row_found;
11290 /* Find the first glyph row in the current matrix of W that is not
11291 affected by changes at the end of current_buffer since the
11292 time W's current matrix was built.
11294 Return in *DELTA the number of chars by which buffer positions in
11295 unchanged text at the end of current_buffer must be adjusted.
11297 Return in *DELTA_BYTES the corresponding number of bytes.
11299 Value is null if no such row exists, i.e. all rows are affected by
11300 changes. */
11302 static struct glyph_row *
11303 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11304 struct window *w;
11305 int *delta, *delta_bytes;
11307 struct glyph_row *row;
11308 struct glyph_row *row_found = NULL;
11310 *delta = *delta_bytes = 0;
11312 /* Display must not have been paused, otherwise the current matrix
11313 is not up to date. */
11314 if (NILP (w->window_end_valid))
11315 abort ();
11317 /* A value of window_end_pos >= END_UNCHANGED means that the window
11318 end is in the range of changed text. If so, there is no
11319 unchanged row at the end of W's current matrix. */
11320 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11321 return NULL;
11323 /* Set row to the last row in W's current matrix displaying text. */
11324 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11326 /* If matrix is entirely empty, no unchanged row exists. */
11327 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11329 /* The value of row is the last glyph row in the matrix having a
11330 meaningful buffer position in it. The end position of row
11331 corresponds to window_end_pos. This allows us to translate
11332 buffer positions in the current matrix to current buffer
11333 positions for characters not in changed text. */
11334 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11335 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11336 int last_unchanged_pos, last_unchanged_pos_old;
11337 struct glyph_row *first_text_row
11338 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11340 *delta = Z - Z_old;
11341 *delta_bytes = Z_BYTE - Z_BYTE_old;
11343 /* Set last_unchanged_pos to the buffer position of the last
11344 character in the buffer that has not been changed. Z is the
11345 index + 1 of the last character in current_buffer, i.e. by
11346 subtracting END_UNCHANGED we get the index of the last
11347 unchanged character, and we have to add BEG to get its buffer
11348 position. */
11349 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11350 last_unchanged_pos_old = last_unchanged_pos - *delta;
11352 /* Search backward from ROW for a row displaying a line that
11353 starts at a minimum position >= last_unchanged_pos_old. */
11354 for (; row > first_text_row; --row)
11356 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11357 abort ();
11359 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11360 row_found = row;
11364 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11365 abort ();
11367 return row_found;
11371 /* Make sure that glyph rows in the current matrix of window W
11372 reference the same glyph memory as corresponding rows in the
11373 frame's frame matrix. This function is called after scrolling W's
11374 current matrix on a terminal frame in try_window_id and
11375 try_window_reusing_current_matrix. */
11377 static void
11378 sync_frame_with_window_matrix_rows (w)
11379 struct window *w;
11381 struct frame *f = XFRAME (w->frame);
11382 struct glyph_row *window_row, *window_row_end, *frame_row;
11384 /* Preconditions: W must be a leaf window and full-width. Its frame
11385 must have a frame matrix. */
11386 xassert (NILP (w->hchild) && NILP (w->vchild));
11387 xassert (WINDOW_FULL_WIDTH_P (w));
11388 xassert (!FRAME_WINDOW_P (f));
11390 /* If W is a full-width window, glyph pointers in W's current matrix
11391 have, by definition, to be the same as glyph pointers in the
11392 corresponding frame matrix. Note that frame matrices have no
11393 marginal areas (see build_frame_matrix). */
11394 window_row = w->current_matrix->rows;
11395 window_row_end = window_row + w->current_matrix->nrows;
11396 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11397 while (window_row < window_row_end)
11399 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11400 struct glyph *end = window_row->glyphs[LAST_AREA];
11402 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11403 frame_row->glyphs[TEXT_AREA] = start;
11404 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11405 frame_row->glyphs[LAST_AREA] = end;
11407 /* Disable frame rows whose corresponding window rows have
11408 been disabled in try_window_id. */
11409 if (!window_row->enabled_p)
11410 frame_row->enabled_p = 0;
11412 ++window_row, ++frame_row;
11417 /* Find the glyph row in window W containing CHARPOS. Consider all
11418 rows between START and END (not inclusive). END null means search
11419 all rows to the end of the display area of W. Value is the row
11420 containing CHARPOS or null. */
11422 struct glyph_row *
11423 row_containing_pos (w, charpos, start, end, dy)
11424 struct window *w;
11425 int charpos;
11426 struct glyph_row *start, *end;
11427 int dy;
11429 struct glyph_row *row = start;
11430 int last_y;
11432 /* If we happen to start on a header-line, skip that. */
11433 if (row->mode_line_p)
11434 ++row;
11436 if ((end && row >= end) || !row->enabled_p)
11437 return NULL;
11439 last_y = window_text_bottom_y (w) - dy;
11441 while ((end == NULL || row < end)
11442 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11443 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11444 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11445 /* The end position of a row equals the start
11446 position of the next row. If CHARPOS is there, we
11447 would rather display it in the next line, except
11448 when this line ends in ZV. */
11449 && !row->ends_at_zv_p
11450 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11451 ++row;
11453 /* Give up if CHARPOS not found. */
11454 if ((end && row >= end)
11455 || charpos < MATRIX_ROW_START_CHARPOS (row)
11456 || charpos > MATRIX_ROW_END_CHARPOS (row))
11457 row = NULL;
11459 return row;
11463 /* Try to redisplay window W by reusing its existing display. W's
11464 current matrix must be up to date when this function is called,
11465 i.e. window_end_valid must not be nil.
11467 Value is
11469 1 if display has been updated
11470 0 if otherwise unsuccessful
11471 -1 if redisplay with same window start is known not to succeed
11473 The following steps are performed:
11475 1. Find the last row in the current matrix of W that is not
11476 affected by changes at the start of current_buffer. If no such row
11477 is found, give up.
11479 2. Find the first row in W's current matrix that is not affected by
11480 changes at the end of current_buffer. Maybe there is no such row.
11482 3. Display lines beginning with the row + 1 found in step 1 to the
11483 row found in step 2 or, if step 2 didn't find a row, to the end of
11484 the window.
11486 4. If cursor is not known to appear on the window, give up.
11488 5. If display stopped at the row found in step 2, scroll the
11489 display and current matrix as needed.
11491 6. Maybe display some lines at the end of W, if we must. This can
11492 happen under various circumstances, like a partially visible line
11493 becoming fully visible, or because newly displayed lines are displayed
11494 in smaller font sizes.
11496 7. Update W's window end information. */
11498 static int
11499 try_window_id (w)
11500 struct window *w;
11502 struct frame *f = XFRAME (w->frame);
11503 struct glyph_matrix *current_matrix = w->current_matrix;
11504 struct glyph_matrix *desired_matrix = w->desired_matrix;
11505 struct glyph_row *last_unchanged_at_beg_row;
11506 struct glyph_row *first_unchanged_at_end_row;
11507 struct glyph_row *row;
11508 struct glyph_row *bottom_row;
11509 int bottom_vpos;
11510 struct it it;
11511 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11512 struct text_pos start_pos;
11513 struct run run;
11514 int first_unchanged_at_end_vpos = 0;
11515 struct glyph_row *last_text_row, *last_text_row_at_end;
11516 struct text_pos start;
11517 int first_changed_charpos, last_changed_charpos;
11519 #if GLYPH_DEBUG
11520 if (inhibit_try_window_id)
11521 return 0;
11522 #endif
11524 /* This is handy for debugging. */
11525 #if 0
11526 #define GIVE_UP(X) \
11527 do { \
11528 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11529 return 0; \
11530 } while (0)
11531 #else
11532 #define GIVE_UP(X) return 0
11533 #endif
11535 SET_TEXT_POS_FROM_MARKER (start, w->start);
11537 /* Don't use this for mini-windows because these can show
11538 messages and mini-buffers, and we don't handle that here. */
11539 if (MINI_WINDOW_P (w))
11540 GIVE_UP (1);
11542 /* This flag is used to prevent redisplay optimizations. */
11543 if (windows_or_buffers_changed || cursor_type_changed)
11544 GIVE_UP (2);
11546 /* Verify that narrowing has not changed.
11547 Also verify that we were not told to prevent redisplay optimizations.
11548 It would be nice to further
11549 reduce the number of cases where this prevents try_window_id. */
11550 if (current_buffer->clip_changed
11551 || current_buffer->prevent_redisplay_optimizations_p)
11552 GIVE_UP (3);
11554 /* Window must either use window-based redisplay or be full width. */
11555 if (!FRAME_WINDOW_P (f)
11556 && (!line_ins_del_ok
11557 || !WINDOW_FULL_WIDTH_P (w)))
11558 GIVE_UP (4);
11560 /* Give up if point is not known NOT to appear in W. */
11561 if (PT < CHARPOS (start))
11562 GIVE_UP (5);
11564 /* Another way to prevent redisplay optimizations. */
11565 if (XFASTINT (w->last_modified) == 0)
11566 GIVE_UP (6);
11568 /* Verify that window is not hscrolled. */
11569 if (XFASTINT (w->hscroll) != 0)
11570 GIVE_UP (7);
11572 /* Verify that display wasn't paused. */
11573 if (NILP (w->window_end_valid))
11574 GIVE_UP (8);
11576 /* Can't use this if highlighting a region because a cursor movement
11577 will do more than just set the cursor. */
11578 if (!NILP (Vtransient_mark_mode)
11579 && !NILP (current_buffer->mark_active))
11580 GIVE_UP (9);
11582 /* Likewise if highlighting trailing whitespace. */
11583 if (!NILP (Vshow_trailing_whitespace))
11584 GIVE_UP (11);
11586 /* Likewise if showing a region. */
11587 if (!NILP (w->region_showing))
11588 GIVE_UP (10);
11590 /* Can use this if overlay arrow position and or string have changed. */
11591 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11592 || !EQ (last_arrow_string, Voverlay_arrow_string))
11593 GIVE_UP (12);
11596 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11597 only if buffer has really changed. The reason is that the gap is
11598 initially at Z for freshly visited files. The code below would
11599 set end_unchanged to 0 in that case. */
11600 if (MODIFF > SAVE_MODIFF
11601 /* This seems to happen sometimes after saving a buffer. */
11602 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11604 if (GPT - BEG < BEG_UNCHANGED)
11605 BEG_UNCHANGED = GPT - BEG;
11606 if (Z - GPT < END_UNCHANGED)
11607 END_UNCHANGED = Z - GPT;
11610 /* The position of the first and last character that has been changed. */
11611 first_changed_charpos = BEG + BEG_UNCHANGED;
11612 last_changed_charpos = Z - END_UNCHANGED;
11614 /* If window starts after a line end, and the last change is in
11615 front of that newline, then changes don't affect the display.
11616 This case happens with stealth-fontification. Note that although
11617 the display is unchanged, glyph positions in the matrix have to
11618 be adjusted, of course. */
11619 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11620 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11621 && ((last_changed_charpos < CHARPOS (start)
11622 && CHARPOS (start) == BEGV)
11623 || (last_changed_charpos < CHARPOS (start) - 1
11624 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11626 int Z_old, delta, Z_BYTE_old, delta_bytes;
11627 struct glyph_row *r0;
11629 /* Compute how many chars/bytes have been added to or removed
11630 from the buffer. */
11631 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11632 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11633 delta = Z - Z_old;
11634 delta_bytes = Z_BYTE - Z_BYTE_old;
11636 /* Give up if PT is not in the window. Note that it already has
11637 been checked at the start of try_window_id that PT is not in
11638 front of the window start. */
11639 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11640 GIVE_UP (13);
11642 /* If window start is unchanged, we can reuse the whole matrix
11643 as is, after adjusting glyph positions. No need to compute
11644 the window end again, since its offset from Z hasn't changed. */
11645 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11646 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11647 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11649 /* Adjust positions in the glyph matrix. */
11650 if (delta || delta_bytes)
11652 struct glyph_row *r1
11653 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11654 increment_matrix_positions (w->current_matrix,
11655 MATRIX_ROW_VPOS (r0, current_matrix),
11656 MATRIX_ROW_VPOS (r1, current_matrix),
11657 delta, delta_bytes);
11660 /* Set the cursor. */
11661 row = row_containing_pos (w, PT, r0, NULL, 0);
11662 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11663 return 1;
11667 /* Handle the case that changes are all below what is displayed in
11668 the window, and that PT is in the window. This shortcut cannot
11669 be taken if ZV is visible in the window, and text has been added
11670 there that is visible in the window. */
11671 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11672 /* ZV is not visible in the window, or there are no
11673 changes at ZV, actually. */
11674 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11675 || first_changed_charpos == last_changed_charpos))
11677 struct glyph_row *r0;
11679 /* Give up if PT is not in the window. Note that it already has
11680 been checked at the start of try_window_id that PT is not in
11681 front of the window start. */
11682 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11683 GIVE_UP (14);
11685 /* If window start is unchanged, we can reuse the whole matrix
11686 as is, without changing glyph positions since no text has
11687 been added/removed in front of the window end. */
11688 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11689 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11691 /* We have to compute the window end anew since text
11692 can have been added/removed after it. */
11693 w->window_end_pos
11694 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11695 w->window_end_bytepos
11696 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11698 /* Set the cursor. */
11699 row = row_containing_pos (w, PT, r0, NULL, 0);
11700 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11701 return 2;
11705 /* Give up if window start is in the changed area.
11707 The condition used to read
11709 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11711 but why that was tested escapes me at the moment. */
11712 if (CHARPOS (start) >= first_changed_charpos
11713 && CHARPOS (start) <= last_changed_charpos)
11714 GIVE_UP (15);
11716 /* Check that window start agrees with the start of the first glyph
11717 row in its current matrix. Check this after we know the window
11718 start is not in changed text, otherwise positions would not be
11719 comparable. */
11720 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11721 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11722 GIVE_UP (16);
11724 /* Give up if the window ends in strings. Overlay strings
11725 at the end are difficult to handle, so don't try. */
11726 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11727 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11728 GIVE_UP (20);
11730 /* Compute the position at which we have to start displaying new
11731 lines. Some of the lines at the top of the window might be
11732 reusable because they are not displaying changed text. Find the
11733 last row in W's current matrix not affected by changes at the
11734 start of current_buffer. Value is null if changes start in the
11735 first line of window. */
11736 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11737 if (last_unchanged_at_beg_row)
11739 /* Avoid starting to display in the moddle of a character, a TAB
11740 for instance. This is easier than to set up the iterator
11741 exactly, and it's not a frequent case, so the additional
11742 effort wouldn't really pay off. */
11743 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11744 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11745 && last_unchanged_at_beg_row > w->current_matrix->rows)
11746 --last_unchanged_at_beg_row;
11748 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11749 GIVE_UP (17);
11751 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11752 GIVE_UP (18);
11753 start_pos = it.current.pos;
11755 /* Start displaying new lines in the desired matrix at the same
11756 vpos we would use in the current matrix, i.e. below
11757 last_unchanged_at_beg_row. */
11758 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11759 current_matrix);
11760 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11761 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11763 xassert (it.hpos == 0 && it.current_x == 0);
11765 else
11767 /* There are no reusable lines at the start of the window.
11768 Start displaying in the first line. */
11769 start_display (&it, w, start);
11770 start_pos = it.current.pos;
11773 /* Find the first row that is not affected by changes at the end of
11774 the buffer. Value will be null if there is no unchanged row, in
11775 which case we must redisplay to the end of the window. delta
11776 will be set to the value by which buffer positions beginning with
11777 first_unchanged_at_end_row have to be adjusted due to text
11778 changes. */
11779 first_unchanged_at_end_row
11780 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11781 IF_DEBUG (debug_delta = delta);
11782 IF_DEBUG (debug_delta_bytes = delta_bytes);
11784 /* Set stop_pos to the buffer position up to which we will have to
11785 display new lines. If first_unchanged_at_end_row != NULL, this
11786 is the buffer position of the start of the line displayed in that
11787 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11788 that we don't stop at a buffer position. */
11789 stop_pos = 0;
11790 if (first_unchanged_at_end_row)
11792 xassert (last_unchanged_at_beg_row == NULL
11793 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11795 /* If this is a continuation line, move forward to the next one
11796 that isn't. Changes in lines above affect this line.
11797 Caution: this may move first_unchanged_at_end_row to a row
11798 not displaying text. */
11799 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11800 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11801 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11802 < it.last_visible_y))
11803 ++first_unchanged_at_end_row;
11805 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11806 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11807 >= it.last_visible_y))
11808 first_unchanged_at_end_row = NULL;
11809 else
11811 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11812 + delta);
11813 first_unchanged_at_end_vpos
11814 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11815 xassert (stop_pos >= Z - END_UNCHANGED);
11818 else if (last_unchanged_at_beg_row == NULL)
11819 GIVE_UP (19);
11822 #if GLYPH_DEBUG
11824 /* Either there is no unchanged row at the end, or the one we have
11825 now displays text. This is a necessary condition for the window
11826 end pos calculation at the end of this function. */
11827 xassert (first_unchanged_at_end_row == NULL
11828 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11830 debug_last_unchanged_at_beg_vpos
11831 = (last_unchanged_at_beg_row
11832 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11833 : -1);
11834 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11836 #endif /* GLYPH_DEBUG != 0 */
11839 /* Display new lines. Set last_text_row to the last new line
11840 displayed which has text on it, i.e. might end up as being the
11841 line where the window_end_vpos is. */
11842 w->cursor.vpos = -1;
11843 last_text_row = NULL;
11844 overlay_arrow_seen = 0;
11845 while (it.current_y < it.last_visible_y
11846 && !fonts_changed_p
11847 && (first_unchanged_at_end_row == NULL
11848 || IT_CHARPOS (it) < stop_pos))
11850 if (display_line (&it))
11851 last_text_row = it.glyph_row - 1;
11854 if (fonts_changed_p)
11855 return -1;
11858 /* Compute differences in buffer positions, y-positions etc. for
11859 lines reused at the bottom of the window. Compute what we can
11860 scroll. */
11861 if (first_unchanged_at_end_row
11862 /* No lines reused because we displayed everything up to the
11863 bottom of the window. */
11864 && it.current_y < it.last_visible_y)
11866 dvpos = (it.vpos
11867 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11868 current_matrix));
11869 dy = it.current_y - first_unchanged_at_end_row->y;
11870 run.current_y = first_unchanged_at_end_row->y;
11871 run.desired_y = run.current_y + dy;
11872 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11874 else
11876 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11877 first_unchanged_at_end_row = NULL;
11879 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11882 /* Find the cursor if not already found. We have to decide whether
11883 PT will appear on this window (it sometimes doesn't, but this is
11884 not a very frequent case.) This decision has to be made before
11885 the current matrix is altered. A value of cursor.vpos < 0 means
11886 that PT is either in one of the lines beginning at
11887 first_unchanged_at_end_row or below the window. Don't care for
11888 lines that might be displayed later at the window end; as
11889 mentioned, this is not a frequent case. */
11890 if (w->cursor.vpos < 0)
11892 /* Cursor in unchanged rows at the top? */
11893 if (PT < CHARPOS (start_pos)
11894 && last_unchanged_at_beg_row)
11896 row = row_containing_pos (w, PT,
11897 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11898 last_unchanged_at_beg_row + 1, 0);
11899 if (row)
11900 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11903 /* Start from first_unchanged_at_end_row looking for PT. */
11904 else if (first_unchanged_at_end_row)
11906 row = row_containing_pos (w, PT - delta,
11907 first_unchanged_at_end_row, NULL, 0);
11908 if (row)
11909 set_cursor_from_row (w, row, w->current_matrix, delta,
11910 delta_bytes, dy, dvpos);
11913 /* Give up if cursor was not found. */
11914 if (w->cursor.vpos < 0)
11916 clear_glyph_matrix (w->desired_matrix);
11917 return -1;
11921 /* Don't let the cursor end in the scroll margins. */
11923 int this_scroll_margin, cursor_height;
11925 this_scroll_margin = max (0, scroll_margin);
11926 this_scroll_margin = min (this_scroll_margin,
11927 XFASTINT (w->height) / 4);
11928 this_scroll_margin *= CANON_Y_UNIT (it.f);
11929 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11931 if ((w->cursor.y < this_scroll_margin
11932 && CHARPOS (start) > BEGV)
11933 /* Don't take scroll margin into account at the bottom because
11934 old redisplay didn't do it either. */
11935 || w->cursor.y + cursor_height > it.last_visible_y)
11937 w->cursor.vpos = -1;
11938 clear_glyph_matrix (w->desired_matrix);
11939 return -1;
11943 /* Scroll the display. Do it before changing the current matrix so
11944 that xterm.c doesn't get confused about where the cursor glyph is
11945 found. */
11946 if (dy && run.height)
11948 update_begin (f);
11950 if (FRAME_WINDOW_P (f))
11952 rif->update_window_begin_hook (w);
11953 rif->clear_mouse_face (w);
11954 rif->scroll_run_hook (w, &run);
11955 rif->update_window_end_hook (w, 0, 0);
11957 else
11959 /* Terminal frame. In this case, dvpos gives the number of
11960 lines to scroll by; dvpos < 0 means scroll up. */
11961 int first_unchanged_at_end_vpos
11962 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11963 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11964 int end = (XFASTINT (w->top)
11965 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11966 + window_internal_height (w));
11968 /* Perform the operation on the screen. */
11969 if (dvpos > 0)
11971 /* Scroll last_unchanged_at_beg_row to the end of the
11972 window down dvpos lines. */
11973 set_terminal_window (end);
11975 /* On dumb terminals delete dvpos lines at the end
11976 before inserting dvpos empty lines. */
11977 if (!scroll_region_ok)
11978 ins_del_lines (end - dvpos, -dvpos);
11980 /* Insert dvpos empty lines in front of
11981 last_unchanged_at_beg_row. */
11982 ins_del_lines (from, dvpos);
11984 else if (dvpos < 0)
11986 /* Scroll up last_unchanged_at_beg_vpos to the end of
11987 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11988 set_terminal_window (end);
11990 /* Delete dvpos lines in front of
11991 last_unchanged_at_beg_vpos. ins_del_lines will set
11992 the cursor to the given vpos and emit |dvpos| delete
11993 line sequences. */
11994 ins_del_lines (from + dvpos, dvpos);
11996 /* On a dumb terminal insert dvpos empty lines at the
11997 end. */
11998 if (!scroll_region_ok)
11999 ins_del_lines (end + dvpos, -dvpos);
12002 set_terminal_window (0);
12005 update_end (f);
12008 /* Shift reused rows of the current matrix to the right position.
12009 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12010 text. */
12011 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12012 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12013 if (dvpos < 0)
12015 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12016 bottom_vpos, dvpos);
12017 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12018 bottom_vpos, 0);
12020 else if (dvpos > 0)
12022 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12023 bottom_vpos, dvpos);
12024 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12025 first_unchanged_at_end_vpos + dvpos, 0);
12028 /* For frame-based redisplay, make sure that current frame and window
12029 matrix are in sync with respect to glyph memory. */
12030 if (!FRAME_WINDOW_P (f))
12031 sync_frame_with_window_matrix_rows (w);
12033 /* Adjust buffer positions in reused rows. */
12034 if (delta)
12035 increment_matrix_positions (current_matrix,
12036 first_unchanged_at_end_vpos + dvpos,
12037 bottom_vpos, delta, delta_bytes);
12039 /* Adjust Y positions. */
12040 if (dy)
12041 shift_glyph_matrix (w, current_matrix,
12042 first_unchanged_at_end_vpos + dvpos,
12043 bottom_vpos, dy);
12045 if (first_unchanged_at_end_row)
12046 first_unchanged_at_end_row += dvpos;
12048 /* If scrolling up, there may be some lines to display at the end of
12049 the window. */
12050 last_text_row_at_end = NULL;
12051 if (dy < 0)
12053 /* Scrolling up can leave for example a partially visible line
12054 at the end of the window to be redisplayed. */
12055 /* Set last_row to the glyph row in the current matrix where the
12056 window end line is found. It has been moved up or down in
12057 the matrix by dvpos. */
12058 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12059 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12061 /* If last_row is the window end line, it should display text. */
12062 xassert (last_row->displays_text_p);
12064 /* If window end line was partially visible before, begin
12065 displaying at that line. Otherwise begin displaying with the
12066 line following it. */
12067 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12069 init_to_row_start (&it, w, last_row);
12070 it.vpos = last_vpos;
12071 it.current_y = last_row->y;
12073 else
12075 init_to_row_end (&it, w, last_row);
12076 it.vpos = 1 + last_vpos;
12077 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12078 ++last_row;
12081 /* We may start in a continuation line. If so, we have to
12082 get the right continuation_lines_width and current_x. */
12083 it.continuation_lines_width = last_row->continuation_lines_width;
12084 it.hpos = it.current_x = 0;
12086 /* Display the rest of the lines at the window end. */
12087 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12088 while (it.current_y < it.last_visible_y
12089 && !fonts_changed_p)
12091 /* Is it always sure that the display agrees with lines in
12092 the current matrix? I don't think so, so we mark rows
12093 displayed invalid in the current matrix by setting their
12094 enabled_p flag to zero. */
12095 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12096 if (display_line (&it))
12097 last_text_row_at_end = it.glyph_row - 1;
12101 /* Update window_end_pos and window_end_vpos. */
12102 if (first_unchanged_at_end_row
12103 && first_unchanged_at_end_row->y < it.last_visible_y
12104 && !last_text_row_at_end)
12106 /* Window end line if one of the preserved rows from the current
12107 matrix. Set row to the last row displaying text in current
12108 matrix starting at first_unchanged_at_end_row, after
12109 scrolling. */
12110 xassert (first_unchanged_at_end_row->displays_text_p);
12111 row = find_last_row_displaying_text (w->current_matrix, &it,
12112 first_unchanged_at_end_row);
12113 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12115 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12116 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12117 w->window_end_vpos
12118 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12119 xassert (w->window_end_bytepos >= 0);
12120 IF_DEBUG (debug_method_add (w, "A"));
12122 else if (last_text_row_at_end)
12124 w->window_end_pos
12125 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12126 w->window_end_bytepos
12127 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12128 w->window_end_vpos
12129 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12130 xassert (w->window_end_bytepos >= 0);
12131 IF_DEBUG (debug_method_add (w, "B"));
12133 else if (last_text_row)
12135 /* We have displayed either to the end of the window or at the
12136 end of the window, i.e. the last row with text is to be found
12137 in the desired matrix. */
12138 w->window_end_pos
12139 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12140 w->window_end_bytepos
12141 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12142 w->window_end_vpos
12143 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12144 xassert (w->window_end_bytepos >= 0);
12146 else if (first_unchanged_at_end_row == NULL
12147 && last_text_row == NULL
12148 && last_text_row_at_end == NULL)
12150 /* Displayed to end of window, but no line containing text was
12151 displayed. Lines were deleted at the end of the window. */
12152 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12153 int vpos = XFASTINT (w->window_end_vpos);
12154 struct glyph_row *current_row = current_matrix->rows + vpos;
12155 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12157 for (row = NULL;
12158 row == NULL && vpos >= first_vpos;
12159 --vpos, --current_row, --desired_row)
12161 if (desired_row->enabled_p)
12163 if (desired_row->displays_text_p)
12164 row = desired_row;
12166 else if (current_row->displays_text_p)
12167 row = current_row;
12170 xassert (row != NULL);
12171 w->window_end_vpos = make_number (vpos + 1);
12172 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12173 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12174 xassert (w->window_end_bytepos >= 0);
12175 IF_DEBUG (debug_method_add (w, "C"));
12177 else
12178 abort ();
12180 #if 0 /* This leads to problems, for instance when the cursor is
12181 at ZV, and the cursor line displays no text. */
12182 /* Disable rows below what's displayed in the window. This makes
12183 debugging easier. */
12184 enable_glyph_matrix_rows (current_matrix,
12185 XFASTINT (w->window_end_vpos) + 1,
12186 bottom_vpos, 0);
12187 #endif
12189 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12190 debug_end_vpos = XFASTINT (w->window_end_vpos));
12192 /* Record that display has not been completed. */
12193 w->window_end_valid = Qnil;
12194 w->desired_matrix->no_scrolling_p = 1;
12195 return 3;
12197 #undef GIVE_UP
12202 /***********************************************************************
12203 More debugging support
12204 ***********************************************************************/
12206 #if GLYPH_DEBUG
12208 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12209 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12210 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12213 /* Dump the contents of glyph matrix MATRIX on stderr.
12215 GLYPHS 0 means don't show glyph contents.
12216 GLYPHS 1 means show glyphs in short form
12217 GLYPHS > 1 means show glyphs in long form. */
12219 void
12220 dump_glyph_matrix (matrix, glyphs)
12221 struct glyph_matrix *matrix;
12222 int glyphs;
12224 int i;
12225 for (i = 0; i < matrix->nrows; ++i)
12226 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12230 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12231 the glyph row and area where the glyph comes from. */
12233 void
12234 dump_glyph (row, glyph, area)
12235 struct glyph_row *row;
12236 struct glyph *glyph;
12237 int area;
12239 if (glyph->type == CHAR_GLYPH)
12241 fprintf (stderr,
12242 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12243 glyph - row->glyphs[TEXT_AREA],
12244 'C',
12245 glyph->charpos,
12246 (BUFFERP (glyph->object)
12247 ? 'B'
12248 : (STRINGP (glyph->object)
12249 ? 'S'
12250 : '-')),
12251 glyph->pixel_width,
12252 glyph->u.ch,
12253 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12254 ? glyph->u.ch
12255 : '.'),
12256 glyph->face_id,
12257 glyph->left_box_line_p,
12258 glyph->right_box_line_p);
12260 else if (glyph->type == STRETCH_GLYPH)
12262 fprintf (stderr,
12263 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12264 glyph - row->glyphs[TEXT_AREA],
12265 'S',
12266 glyph->charpos,
12267 (BUFFERP (glyph->object)
12268 ? 'B'
12269 : (STRINGP (glyph->object)
12270 ? 'S'
12271 : '-')),
12272 glyph->pixel_width,
12274 '.',
12275 glyph->face_id,
12276 glyph->left_box_line_p,
12277 glyph->right_box_line_p);
12279 else if (glyph->type == IMAGE_GLYPH)
12281 fprintf (stderr,
12282 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12283 glyph - row->glyphs[TEXT_AREA],
12284 'I',
12285 glyph->charpos,
12286 (BUFFERP (glyph->object)
12287 ? 'B'
12288 : (STRINGP (glyph->object)
12289 ? 'S'
12290 : '-')),
12291 glyph->pixel_width,
12292 glyph->u.img_id,
12293 '.',
12294 glyph->face_id,
12295 glyph->left_box_line_p,
12296 glyph->right_box_line_p);
12301 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12302 GLYPHS 0 means don't show glyph contents.
12303 GLYPHS 1 means show glyphs in short form
12304 GLYPHS > 1 means show glyphs in long form. */
12306 void
12307 dump_glyph_row (row, vpos, glyphs)
12308 struct glyph_row *row;
12309 int vpos, glyphs;
12311 if (glyphs != 1)
12313 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12314 fprintf (stderr, "=======================================================================\n");
12316 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12317 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12318 vpos,
12319 MATRIX_ROW_START_CHARPOS (row),
12320 MATRIX_ROW_END_CHARPOS (row),
12321 row->used[TEXT_AREA],
12322 row->contains_overlapping_glyphs_p,
12323 row->enabled_p,
12324 row->truncated_on_left_p,
12325 row->truncated_on_right_p,
12326 row->overlay_arrow_p,
12327 row->continued_p,
12328 MATRIX_ROW_CONTINUATION_LINE_P (row),
12329 row->displays_text_p,
12330 row->ends_at_zv_p,
12331 row->fill_line_p,
12332 row->ends_in_middle_of_char_p,
12333 row->starts_in_middle_of_char_p,
12334 row->mouse_face_p,
12335 row->x,
12336 row->y,
12337 row->pixel_width,
12338 row->height,
12339 row->visible_height,
12340 row->ascent,
12341 row->phys_ascent);
12342 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12343 row->end.overlay_string_index,
12344 row->continuation_lines_width);
12345 fprintf (stderr, "%9d %5d\n",
12346 CHARPOS (row->start.string_pos),
12347 CHARPOS (row->end.string_pos));
12348 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12349 row->end.dpvec_index);
12352 if (glyphs > 1)
12354 int area;
12356 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12358 struct glyph *glyph = row->glyphs[area];
12359 struct glyph *glyph_end = glyph + row->used[area];
12361 /* Glyph for a line end in text. */
12362 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12363 ++glyph_end;
12365 if (glyph < glyph_end)
12366 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12368 for (; glyph < glyph_end; ++glyph)
12369 dump_glyph (row, glyph, area);
12372 else if (glyphs == 1)
12374 int area;
12376 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12378 char *s = (char *) alloca (row->used[area] + 1);
12379 int i;
12381 for (i = 0; i < row->used[area]; ++i)
12383 struct glyph *glyph = row->glyphs[area] + i;
12384 if (glyph->type == CHAR_GLYPH
12385 && glyph->u.ch < 0x80
12386 && glyph->u.ch >= ' ')
12387 s[i] = glyph->u.ch;
12388 else
12389 s[i] = '.';
12392 s[i] = '\0';
12393 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12399 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12400 Sdump_glyph_matrix, 0, 1, "p",
12401 doc: /* Dump the current matrix of the selected window to stderr.
12402 Shows contents of glyph row structures. With non-nil
12403 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12404 glyphs in short form, otherwise show glyphs in long form. */)
12405 (glyphs)
12406 Lisp_Object glyphs;
12408 struct window *w = XWINDOW (selected_window);
12409 struct buffer *buffer = XBUFFER (w->buffer);
12411 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12412 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12413 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12414 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12415 fprintf (stderr, "=============================================\n");
12416 dump_glyph_matrix (w->current_matrix,
12417 NILP (glyphs) ? 0 : XINT (glyphs));
12418 return Qnil;
12422 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12423 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12426 struct frame *f = XFRAME (selected_frame);
12427 dump_glyph_matrix (f->current_matrix, 1);
12428 return Qnil;
12432 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12433 doc: /* Dump glyph row ROW to stderr.
12434 GLYPH 0 means don't dump glyphs.
12435 GLYPH 1 means dump glyphs in short form.
12436 GLYPH > 1 or omitted means dump glyphs in long form. */)
12437 (row, glyphs)
12438 Lisp_Object row, glyphs;
12440 struct glyph_matrix *matrix;
12441 int vpos;
12443 CHECK_NUMBER (row);
12444 matrix = XWINDOW (selected_window)->current_matrix;
12445 vpos = XINT (row);
12446 if (vpos >= 0 && vpos < matrix->nrows)
12447 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12448 vpos,
12449 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12450 return Qnil;
12454 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12455 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12456 GLYPH 0 means don't dump glyphs.
12457 GLYPH 1 means dump glyphs in short form.
12458 GLYPH > 1 or omitted means dump glyphs in long form. */)
12459 (row, glyphs)
12460 Lisp_Object row, glyphs;
12462 struct frame *sf = SELECTED_FRAME ();
12463 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12464 int vpos;
12466 CHECK_NUMBER (row);
12467 vpos = XINT (row);
12468 if (vpos >= 0 && vpos < m->nrows)
12469 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12470 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12471 return Qnil;
12475 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12476 doc: /* Toggle tracing of redisplay.
12477 With ARG, turn tracing on if and only if ARG is positive. */)
12478 (arg)
12479 Lisp_Object arg;
12481 if (NILP (arg))
12482 trace_redisplay_p = !trace_redisplay_p;
12483 else
12485 arg = Fprefix_numeric_value (arg);
12486 trace_redisplay_p = XINT (arg) > 0;
12489 return Qnil;
12493 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12494 doc: /* Like `format', but print result to stderr.
12495 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12496 (nargs, args)
12497 int nargs;
12498 Lisp_Object *args;
12500 Lisp_Object s = Fformat (nargs, args);
12501 fprintf (stderr, "%s", SDATA (s));
12502 return Qnil;
12505 #endif /* GLYPH_DEBUG */
12509 /***********************************************************************
12510 Building Desired Matrix Rows
12511 ***********************************************************************/
12513 /* Return a temporary glyph row holding the glyphs of an overlay
12514 arrow. Only used for non-window-redisplay windows. */
12516 static struct glyph_row *
12517 get_overlay_arrow_glyph_row (w)
12518 struct window *w;
12520 struct frame *f = XFRAME (WINDOW_FRAME (w));
12521 struct buffer *buffer = XBUFFER (w->buffer);
12522 struct buffer *old = current_buffer;
12523 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12524 int arrow_len = SCHARS (Voverlay_arrow_string);
12525 const unsigned char *arrow_end = arrow_string + arrow_len;
12526 const unsigned char *p;
12527 struct it it;
12528 int multibyte_p;
12529 int n_glyphs_before;
12531 set_buffer_temp (buffer);
12532 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12533 it.glyph_row->used[TEXT_AREA] = 0;
12534 SET_TEXT_POS (it.position, 0, 0);
12536 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12537 p = arrow_string;
12538 while (p < arrow_end)
12540 Lisp_Object face, ilisp;
12542 /* Get the next character. */
12543 if (multibyte_p)
12544 it.c = string_char_and_length (p, arrow_len, &it.len);
12545 else
12546 it.c = *p, it.len = 1;
12547 p += it.len;
12549 /* Get its face. */
12550 ilisp = make_number (p - arrow_string);
12551 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12552 it.face_id = compute_char_face (f, it.c, face);
12554 /* Compute its width, get its glyphs. */
12555 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12556 SET_TEXT_POS (it.position, -1, -1);
12557 PRODUCE_GLYPHS (&it);
12559 /* If this character doesn't fit any more in the line, we have
12560 to remove some glyphs. */
12561 if (it.current_x > it.last_visible_x)
12563 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12564 break;
12568 set_buffer_temp (old);
12569 return it.glyph_row;
12573 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12574 glyphs are only inserted for terminal frames since we can't really
12575 win with truncation glyphs when partially visible glyphs are
12576 involved. Which glyphs to insert is determined by
12577 produce_special_glyphs. */
12579 static void
12580 insert_left_trunc_glyphs (it)
12581 struct it *it;
12583 struct it truncate_it;
12584 struct glyph *from, *end, *to, *toend;
12586 xassert (!FRAME_WINDOW_P (it->f));
12588 /* Get the truncation glyphs. */
12589 truncate_it = *it;
12590 truncate_it.current_x = 0;
12591 truncate_it.face_id = DEFAULT_FACE_ID;
12592 truncate_it.glyph_row = &scratch_glyph_row;
12593 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12594 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12595 truncate_it.object = make_number (0);
12596 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12598 /* Overwrite glyphs from IT with truncation glyphs. */
12599 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12600 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12601 to = it->glyph_row->glyphs[TEXT_AREA];
12602 toend = to + it->glyph_row->used[TEXT_AREA];
12604 while (from < end)
12605 *to++ = *from++;
12607 /* There may be padding glyphs left over. Overwrite them too. */
12608 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12610 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12611 while (from < end)
12612 *to++ = *from++;
12615 if (to > toend)
12616 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12620 /* Compute the pixel height and width of IT->glyph_row.
12622 Most of the time, ascent and height of a display line will be equal
12623 to the max_ascent and max_height values of the display iterator
12624 structure. This is not the case if
12626 1. We hit ZV without displaying anything. In this case, max_ascent
12627 and max_height will be zero.
12629 2. We have some glyphs that don't contribute to the line height.
12630 (The glyph row flag contributes_to_line_height_p is for future
12631 pixmap extensions).
12633 The first case is easily covered by using default values because in
12634 these cases, the line height does not really matter, except that it
12635 must not be zero. */
12637 static void
12638 compute_line_metrics (it)
12639 struct it *it;
12641 struct glyph_row *row = it->glyph_row;
12642 int area, i;
12644 if (FRAME_WINDOW_P (it->f))
12646 int i, min_y, max_y;
12648 /* The line may consist of one space only, that was added to
12649 place the cursor on it. If so, the row's height hasn't been
12650 computed yet. */
12651 if (row->height == 0)
12653 if (it->max_ascent + it->max_descent == 0)
12654 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12655 row->ascent = it->max_ascent;
12656 row->height = it->max_ascent + it->max_descent;
12657 row->phys_ascent = it->max_phys_ascent;
12658 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12661 /* Compute the width of this line. */
12662 row->pixel_width = row->x;
12663 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12664 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12666 xassert (row->pixel_width >= 0);
12667 xassert (row->ascent >= 0 && row->height > 0);
12669 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12670 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12672 /* If first line's physical ascent is larger than its logical
12673 ascent, use the physical ascent, and make the row taller.
12674 This makes accented characters fully visible. */
12675 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12676 && row->phys_ascent > row->ascent)
12678 row->height += row->phys_ascent - row->ascent;
12679 row->ascent = row->phys_ascent;
12682 /* Compute how much of the line is visible. */
12683 row->visible_height = row->height;
12685 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12686 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12688 if (row->y < min_y)
12689 row->visible_height -= min_y - row->y;
12690 if (row->y + row->height > max_y)
12691 row->visible_height -= row->y + row->height - max_y;
12693 else
12695 row->pixel_width = row->used[TEXT_AREA];
12696 if (row->continued_p)
12697 row->pixel_width -= it->continuation_pixel_width;
12698 else if (row->truncated_on_right_p)
12699 row->pixel_width -= it->truncation_pixel_width;
12700 row->ascent = row->phys_ascent = 0;
12701 row->height = row->phys_height = row->visible_height = 1;
12704 /* Compute a hash code for this row. */
12705 row->hash = 0;
12706 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12707 for (i = 0; i < row->used[area]; ++i)
12708 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12709 + row->glyphs[area][i].u.val
12710 + row->glyphs[area][i].face_id
12711 + row->glyphs[area][i].padding_p
12712 + (row->glyphs[area][i].type << 2));
12714 it->max_ascent = it->max_descent = 0;
12715 it->max_phys_ascent = it->max_phys_descent = 0;
12719 /* Append one space to the glyph row of iterator IT if doing a
12720 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12721 space have the default face, otherwise let it have the same face as
12722 IT->face_id. Value is non-zero if a space was added.
12724 This function is called to make sure that there is always one glyph
12725 at the end of a glyph row that the cursor can be set on under
12726 window-systems. (If there weren't such a glyph we would not know
12727 how wide and tall a box cursor should be displayed).
12729 At the same time this space let's a nicely handle clearing to the
12730 end of the line if the row ends in italic text. */
12732 static int
12733 append_space (it, default_face_p)
12734 struct it *it;
12735 int default_face_p;
12737 if (FRAME_WINDOW_P (it->f))
12739 int n = it->glyph_row->used[TEXT_AREA];
12741 if (it->glyph_row->glyphs[TEXT_AREA] + n
12742 < it->glyph_row->glyphs[1 + TEXT_AREA])
12744 /* Save some values that must not be changed.
12745 Must save IT->c and IT->len because otherwise
12746 ITERATOR_AT_END_P wouldn't work anymore after
12747 append_space has been called. */
12748 enum display_element_type saved_what = it->what;
12749 int saved_c = it->c, saved_len = it->len;
12750 int saved_x = it->current_x;
12751 int saved_face_id = it->face_id;
12752 struct text_pos saved_pos;
12753 Lisp_Object saved_object;
12754 struct face *face;
12756 saved_object = it->object;
12757 saved_pos = it->position;
12759 it->what = IT_CHARACTER;
12760 bzero (&it->position, sizeof it->position);
12761 it->object = make_number (0);
12762 it->c = ' ';
12763 it->len = 1;
12765 if (default_face_p)
12766 it->face_id = DEFAULT_FACE_ID;
12767 else if (it->face_before_selective_p)
12768 it->face_id = it->saved_face_id;
12769 face = FACE_FROM_ID (it->f, it->face_id);
12770 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12772 PRODUCE_GLYPHS (it);
12774 it->current_x = saved_x;
12775 it->object = saved_object;
12776 it->position = saved_pos;
12777 it->what = saved_what;
12778 it->face_id = saved_face_id;
12779 it->len = saved_len;
12780 it->c = saved_c;
12781 return 1;
12785 return 0;
12789 /* Extend the face of the last glyph in the text area of IT->glyph_row
12790 to the end of the display line. Called from display_line.
12791 If the glyph row is empty, add a space glyph to it so that we
12792 know the face to draw. Set the glyph row flag fill_line_p. */
12794 static void
12795 extend_face_to_end_of_line (it)
12796 struct it *it;
12798 struct face *face;
12799 struct frame *f = it->f;
12801 /* If line is already filled, do nothing. */
12802 if (it->current_x >= it->last_visible_x)
12803 return;
12805 /* Face extension extends the background and box of IT->face_id
12806 to the end of the line. If the background equals the background
12807 of the frame, we don't have to do anything. */
12808 if (it->face_before_selective_p)
12809 face = FACE_FROM_ID (it->f, it->saved_face_id);
12810 else
12811 face = FACE_FROM_ID (f, it->face_id);
12813 if (FRAME_WINDOW_P (f)
12814 && face->box == FACE_NO_BOX
12815 && face->background == FRAME_BACKGROUND_PIXEL (f)
12816 && !face->stipple)
12817 return;
12819 /* Set the glyph row flag indicating that the face of the last glyph
12820 in the text area has to be drawn to the end of the text area. */
12821 it->glyph_row->fill_line_p = 1;
12823 /* If current character of IT is not ASCII, make sure we have the
12824 ASCII face. This will be automatically undone the next time
12825 get_next_display_element returns a multibyte character. Note
12826 that the character will always be single byte in unibyte text. */
12827 if (!SINGLE_BYTE_CHAR_P (it->c))
12829 it->face_id = FACE_FOR_CHAR (f, face, 0);
12832 if (FRAME_WINDOW_P (f))
12834 /* If the row is empty, add a space with the current face of IT,
12835 so that we know which face to draw. */
12836 if (it->glyph_row->used[TEXT_AREA] == 0)
12838 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12839 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12840 it->glyph_row->used[TEXT_AREA] = 1;
12843 else
12845 /* Save some values that must not be changed. */
12846 int saved_x = it->current_x;
12847 struct text_pos saved_pos;
12848 Lisp_Object saved_object;
12849 enum display_element_type saved_what = it->what;
12850 int saved_face_id = it->face_id;
12852 saved_object = it->object;
12853 saved_pos = it->position;
12855 it->what = IT_CHARACTER;
12856 bzero (&it->position, sizeof it->position);
12857 it->object = make_number (0);
12858 it->c = ' ';
12859 it->len = 1;
12860 it->face_id = face->id;
12862 PRODUCE_GLYPHS (it);
12864 while (it->current_x <= it->last_visible_x)
12865 PRODUCE_GLYPHS (it);
12867 /* Don't count these blanks really. It would let us insert a left
12868 truncation glyph below and make us set the cursor on them, maybe. */
12869 it->current_x = saved_x;
12870 it->object = saved_object;
12871 it->position = saved_pos;
12872 it->what = saved_what;
12873 it->face_id = saved_face_id;
12878 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12879 trailing whitespace. */
12881 static int
12882 trailing_whitespace_p (charpos)
12883 int charpos;
12885 int bytepos = CHAR_TO_BYTE (charpos);
12886 int c = 0;
12888 while (bytepos < ZV_BYTE
12889 && (c = FETCH_CHAR (bytepos),
12890 c == ' ' || c == '\t'))
12891 ++bytepos;
12893 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12895 if (bytepos != PT_BYTE)
12896 return 1;
12898 return 0;
12902 /* Highlight trailing whitespace, if any, in ROW. */
12904 void
12905 highlight_trailing_whitespace (f, row)
12906 struct frame *f;
12907 struct glyph_row *row;
12909 int used = row->used[TEXT_AREA];
12911 if (used)
12913 struct glyph *start = row->glyphs[TEXT_AREA];
12914 struct glyph *glyph = start + used - 1;
12916 /* Skip over glyphs inserted to display the cursor at the
12917 end of a line, for extending the face of the last glyph
12918 to the end of the line on terminals, and for truncation
12919 and continuation glyphs. */
12920 while (glyph >= start
12921 && glyph->type == CHAR_GLYPH
12922 && INTEGERP (glyph->object))
12923 --glyph;
12925 /* If last glyph is a space or stretch, and it's trailing
12926 whitespace, set the face of all trailing whitespace glyphs in
12927 IT->glyph_row to `trailing-whitespace'. */
12928 if (glyph >= start
12929 && BUFFERP (glyph->object)
12930 && (glyph->type == STRETCH_GLYPH
12931 || (glyph->type == CHAR_GLYPH
12932 && glyph->u.ch == ' '))
12933 && trailing_whitespace_p (glyph->charpos))
12935 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12937 while (glyph >= start
12938 && BUFFERP (glyph->object)
12939 && (glyph->type == STRETCH_GLYPH
12940 || (glyph->type == CHAR_GLYPH
12941 && glyph->u.ch == ' ')))
12942 (glyph--)->face_id = face_id;
12948 /* Value is non-zero if glyph row ROW in window W should be
12949 used to hold the cursor. */
12951 static int
12952 cursor_row_p (w, row)
12953 struct window *w;
12954 struct glyph_row *row;
12956 int cursor_row_p = 1;
12958 if (PT == MATRIX_ROW_END_CHARPOS (row))
12960 /* If the row ends with a newline from a string, we don't want
12961 the cursor there (if the row is continued it doesn't end in a
12962 newline). */
12963 if (CHARPOS (row->end.string_pos) >= 0
12964 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12965 cursor_row_p = row->continued_p;
12967 /* If the row ends at ZV, display the cursor at the end of that
12968 row instead of at the start of the row below. */
12969 else if (row->ends_at_zv_p)
12970 cursor_row_p = 1;
12971 else
12972 cursor_row_p = 0;
12975 return cursor_row_p;
12979 /* Construct the glyph row IT->glyph_row in the desired matrix of
12980 IT->w from text at the current position of IT. See dispextern.h
12981 for an overview of struct it. Value is non-zero if
12982 IT->glyph_row displays text, as opposed to a line displaying ZV
12983 only. */
12985 static int
12986 display_line (it)
12987 struct it *it;
12989 struct glyph_row *row = it->glyph_row;
12991 /* We always start displaying at hpos zero even if hscrolled. */
12992 xassert (it->hpos == 0 && it->current_x == 0);
12994 /* We must not display in a row that's not a text row. */
12995 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12996 < it->w->desired_matrix->nrows);
12998 /* Is IT->w showing the region? */
12999 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13001 /* Clear the result glyph row and enable it. */
13002 prepare_desired_row (row);
13004 row->y = it->current_y;
13005 row->start = it->current;
13006 row->continuation_lines_width = it->continuation_lines_width;
13007 row->displays_text_p = 1;
13008 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13009 it->starts_in_middle_of_char_p = 0;
13011 /* Arrange the overlays nicely for our purposes. Usually, we call
13012 display_line on only one line at a time, in which case this
13013 can't really hurt too much, or we call it on lines which appear
13014 one after another in the buffer, in which case all calls to
13015 recenter_overlay_lists but the first will be pretty cheap. */
13016 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13018 /* Move over display elements that are not visible because we are
13019 hscrolled. This may stop at an x-position < IT->first_visible_x
13020 if the first glyph is partially visible or if we hit a line end. */
13021 if (it->current_x < it->first_visible_x)
13022 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13023 MOVE_TO_POS | MOVE_TO_X);
13025 /* Get the initial row height. This is either the height of the
13026 text hscrolled, if there is any, or zero. */
13027 row->ascent = it->max_ascent;
13028 row->height = it->max_ascent + it->max_descent;
13029 row->phys_ascent = it->max_phys_ascent;
13030 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13032 /* Loop generating characters. The loop is left with IT on the next
13033 character to display. */
13034 while (1)
13036 int n_glyphs_before, hpos_before, x_before;
13037 int x, i, nglyphs;
13038 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13040 /* Retrieve the next thing to display. Value is zero if end of
13041 buffer reached. */
13042 if (!get_next_display_element (it))
13044 /* Maybe add a space at the end of this line that is used to
13045 display the cursor there under X. Set the charpos of the
13046 first glyph of blank lines not corresponding to any text
13047 to -1. */
13048 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13049 || row->used[TEXT_AREA] == 0)
13051 row->glyphs[TEXT_AREA]->charpos = -1;
13052 row->displays_text_p = 0;
13054 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13055 && (!MINI_WINDOW_P (it->w)
13056 || (minibuf_level && EQ (it->window, minibuf_window))))
13057 row->indicate_empty_line_p = 1;
13060 it->continuation_lines_width = 0;
13061 row->ends_at_zv_p = 1;
13062 break;
13065 /* Now, get the metrics of what we want to display. This also
13066 generates glyphs in `row' (which is IT->glyph_row). */
13067 n_glyphs_before = row->used[TEXT_AREA];
13068 x = it->current_x;
13070 /* Remember the line height so far in case the next element doesn't
13071 fit on the line. */
13072 if (!it->truncate_lines_p)
13074 ascent = it->max_ascent;
13075 descent = it->max_descent;
13076 phys_ascent = it->max_phys_ascent;
13077 phys_descent = it->max_phys_descent;
13080 PRODUCE_GLYPHS (it);
13082 /* If this display element was in marginal areas, continue with
13083 the next one. */
13084 if (it->area != TEXT_AREA)
13086 row->ascent = max (row->ascent, it->max_ascent);
13087 row->height = max (row->height, it->max_ascent + it->max_descent);
13088 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13089 row->phys_height = max (row->phys_height,
13090 it->max_phys_ascent + it->max_phys_descent);
13091 set_iterator_to_next (it, 1);
13092 continue;
13095 /* Does the display element fit on the line? If we truncate
13096 lines, we should draw past the right edge of the window. If
13097 we don't truncate, we want to stop so that we can display the
13098 continuation glyph before the right margin. If lines are
13099 continued, there are two possible strategies for characters
13100 resulting in more than 1 glyph (e.g. tabs): Display as many
13101 glyphs as possible in this line and leave the rest for the
13102 continuation line, or display the whole element in the next
13103 line. Original redisplay did the former, so we do it also. */
13104 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13105 hpos_before = it->hpos;
13106 x_before = x;
13108 if (/* Not a newline. */
13109 nglyphs > 0
13110 /* Glyphs produced fit entirely in the line. */
13111 && it->current_x < it->last_visible_x)
13113 it->hpos += nglyphs;
13114 row->ascent = max (row->ascent, it->max_ascent);
13115 row->height = max (row->height, it->max_ascent + it->max_descent);
13116 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13117 row->phys_height = max (row->phys_height,
13118 it->max_phys_ascent + it->max_phys_descent);
13119 if (it->current_x - it->pixel_width < it->first_visible_x)
13120 row->x = x - it->first_visible_x;
13122 else
13124 int new_x;
13125 struct glyph *glyph;
13127 for (i = 0; i < nglyphs; ++i, x = new_x)
13129 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13130 new_x = x + glyph->pixel_width;
13132 if (/* Lines are continued. */
13133 !it->truncate_lines_p
13134 && (/* Glyph doesn't fit on the line. */
13135 new_x > it->last_visible_x
13136 /* Or it fits exactly on a window system frame. */
13137 || (new_x == it->last_visible_x
13138 && FRAME_WINDOW_P (it->f))))
13140 /* End of a continued line. */
13142 if (it->hpos == 0
13143 || (new_x == it->last_visible_x
13144 && FRAME_WINDOW_P (it->f)))
13146 /* Current glyph is the only one on the line or
13147 fits exactly on the line. We must continue
13148 the line because we can't draw the cursor
13149 after the glyph. */
13150 row->continued_p = 1;
13151 it->current_x = new_x;
13152 it->continuation_lines_width += new_x;
13153 ++it->hpos;
13154 if (i == nglyphs - 1)
13155 set_iterator_to_next (it, 1);
13157 else if (CHAR_GLYPH_PADDING_P (*glyph)
13158 && !FRAME_WINDOW_P (it->f))
13160 /* A padding glyph that doesn't fit on this line.
13161 This means the whole character doesn't fit
13162 on the line. */
13163 row->used[TEXT_AREA] = n_glyphs_before;
13165 /* Fill the rest of the row with continuation
13166 glyphs like in 20.x. */
13167 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13168 < row->glyphs[1 + TEXT_AREA])
13169 produce_special_glyphs (it, IT_CONTINUATION);
13171 row->continued_p = 1;
13172 it->current_x = x_before;
13173 it->continuation_lines_width += x_before;
13175 /* Restore the height to what it was before the
13176 element not fitting on the line. */
13177 it->max_ascent = ascent;
13178 it->max_descent = descent;
13179 it->max_phys_ascent = phys_ascent;
13180 it->max_phys_descent = phys_descent;
13182 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13184 /* A TAB that extends past the right edge of the
13185 window. This produces a single glyph on
13186 window system frames. We leave the glyph in
13187 this row and let it fill the row, but don't
13188 consume the TAB. */
13189 it->continuation_lines_width += it->last_visible_x;
13190 row->ends_in_middle_of_char_p = 1;
13191 row->continued_p = 1;
13192 glyph->pixel_width = it->last_visible_x - x;
13193 it->starts_in_middle_of_char_p = 1;
13195 else
13197 /* Something other than a TAB that draws past
13198 the right edge of the window. Restore
13199 positions to values before the element. */
13200 row->used[TEXT_AREA] = n_glyphs_before + i;
13202 /* Display continuation glyphs. */
13203 if (!FRAME_WINDOW_P (it->f))
13204 produce_special_glyphs (it, IT_CONTINUATION);
13205 row->continued_p = 1;
13207 it->continuation_lines_width += x;
13209 if (nglyphs > 1 && i > 0)
13211 row->ends_in_middle_of_char_p = 1;
13212 it->starts_in_middle_of_char_p = 1;
13215 /* Restore the height to what it was before the
13216 element not fitting on the line. */
13217 it->max_ascent = ascent;
13218 it->max_descent = descent;
13219 it->max_phys_ascent = phys_ascent;
13220 it->max_phys_descent = phys_descent;
13223 break;
13225 else if (new_x > it->first_visible_x)
13227 /* Increment number of glyphs actually displayed. */
13228 ++it->hpos;
13230 if (x < it->first_visible_x)
13231 /* Glyph is partially visible, i.e. row starts at
13232 negative X position. */
13233 row->x = x - it->first_visible_x;
13235 else
13237 /* Glyph is completely off the left margin of the
13238 window. This should not happen because of the
13239 move_it_in_display_line at the start of this
13240 function, unless the text display area of the
13241 window is empty. */
13242 xassert (it->first_visible_x <= it->last_visible_x);
13246 row->ascent = max (row->ascent, it->max_ascent);
13247 row->height = max (row->height, it->max_ascent + it->max_descent);
13248 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13249 row->phys_height = max (row->phys_height,
13250 it->max_phys_ascent + it->max_phys_descent);
13252 /* End of this display line if row is continued. */
13253 if (row->continued_p)
13254 break;
13257 /* Is this a line end? If yes, we're also done, after making
13258 sure that a non-default face is extended up to the right
13259 margin of the window. */
13260 if (ITERATOR_AT_END_OF_LINE_P (it))
13262 int used_before = row->used[TEXT_AREA];
13264 row->ends_in_newline_from_string_p = STRINGP (it->object);
13266 /* Add a space at the end of the line that is used to
13267 display the cursor there. */
13268 append_space (it, 0);
13270 /* Extend the face to the end of the line. */
13271 extend_face_to_end_of_line (it);
13273 /* Make sure we have the position. */
13274 if (used_before == 0)
13275 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13277 /* Consume the line end. This skips over invisible lines. */
13278 set_iterator_to_next (it, 1);
13279 it->continuation_lines_width = 0;
13280 break;
13283 /* Proceed with next display element. Note that this skips
13284 over lines invisible because of selective display. */
13285 set_iterator_to_next (it, 1);
13287 /* If we truncate lines, we are done when the last displayed
13288 glyphs reach past the right margin of the window. */
13289 if (it->truncate_lines_p
13290 && (FRAME_WINDOW_P (it->f)
13291 ? (it->current_x >= it->last_visible_x)
13292 : (it->current_x > it->last_visible_x)))
13294 /* Maybe add truncation glyphs. */
13295 if (!FRAME_WINDOW_P (it->f))
13297 int i, n;
13299 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13300 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13301 break;
13303 for (n = row->used[TEXT_AREA]; i < n; ++i)
13305 row->used[TEXT_AREA] = i;
13306 produce_special_glyphs (it, IT_TRUNCATION);
13310 row->truncated_on_right_p = 1;
13311 it->continuation_lines_width = 0;
13312 reseat_at_next_visible_line_start (it, 0);
13313 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13314 it->hpos = hpos_before;
13315 it->current_x = x_before;
13316 break;
13320 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13321 at the left window margin. */
13322 if (it->first_visible_x
13323 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13325 if (!FRAME_WINDOW_P (it->f))
13326 insert_left_trunc_glyphs (it);
13327 row->truncated_on_left_p = 1;
13330 /* If the start of this line is the overlay arrow-position, then
13331 mark this glyph row as the one containing the overlay arrow.
13332 This is clearly a mess with variable size fonts. It would be
13333 better to let it be displayed like cursors under X. */
13334 if (MARKERP (Voverlay_arrow_position)
13335 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13336 && (MATRIX_ROW_START_CHARPOS (row)
13337 == marker_position (Voverlay_arrow_position))
13338 && STRINGP (Voverlay_arrow_string)
13339 && ! overlay_arrow_seen)
13341 /* Overlay arrow in window redisplay is a fringe bitmap. */
13342 if (!FRAME_WINDOW_P (it->f))
13344 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13345 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13346 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13347 struct glyph *p = row->glyphs[TEXT_AREA];
13348 struct glyph *p2, *end;
13350 /* Copy the arrow glyphs. */
13351 while (glyph < arrow_end)
13352 *p++ = *glyph++;
13354 /* Throw away padding glyphs. */
13355 p2 = p;
13356 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13357 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13358 ++p2;
13359 if (p2 > p)
13361 while (p2 < end)
13362 *p++ = *p2++;
13363 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13367 overlay_arrow_seen = 1;
13368 row->overlay_arrow_p = 1;
13371 /* Compute pixel dimensions of this line. */
13372 compute_line_metrics (it);
13374 /* Remember the position at which this line ends. */
13375 row->end = it->current;
13377 /* Maybe set the cursor. */
13378 if (it->w->cursor.vpos < 0
13379 && PT >= MATRIX_ROW_START_CHARPOS (row)
13380 && PT <= MATRIX_ROW_END_CHARPOS (row)
13381 && cursor_row_p (it->w, row))
13382 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13384 /* Highlight trailing whitespace. */
13385 if (!NILP (Vshow_trailing_whitespace))
13386 highlight_trailing_whitespace (it->f, it->glyph_row);
13388 /* Prepare for the next line. This line starts horizontally at (X
13389 HPOS) = (0 0). Vertical positions are incremented. As a
13390 convenience for the caller, IT->glyph_row is set to the next
13391 row to be used. */
13392 it->current_x = it->hpos = 0;
13393 it->current_y += row->height;
13394 ++it->vpos;
13395 ++it->glyph_row;
13396 return row->displays_text_p;
13401 /***********************************************************************
13402 Menu Bar
13403 ***********************************************************************/
13405 /* Redisplay the menu bar in the frame for window W.
13407 The menu bar of X frames that don't have X toolkit support is
13408 displayed in a special window W->frame->menu_bar_window.
13410 The menu bar of terminal frames is treated specially as far as
13411 glyph matrices are concerned. Menu bar lines are not part of
13412 windows, so the update is done directly on the frame matrix rows
13413 for the menu bar. */
13415 static void
13416 display_menu_bar (w)
13417 struct window *w;
13419 struct frame *f = XFRAME (WINDOW_FRAME (w));
13420 struct it it;
13421 Lisp_Object items;
13422 int i;
13424 /* Don't do all this for graphical frames. */
13425 #ifdef HAVE_NTGUI
13426 if (!NILP (Vwindow_system))
13427 return;
13428 #endif
13429 #ifdef USE_X_TOOLKIT
13430 if (FRAME_X_P (f))
13431 return;
13432 #endif
13433 #ifdef MAC_OS
13434 if (FRAME_MAC_P (f))
13435 return;
13436 #endif
13438 #ifdef USE_X_TOOLKIT
13439 xassert (!FRAME_WINDOW_P (f));
13440 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13441 it.first_visible_x = 0;
13442 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13443 #else /* not USE_X_TOOLKIT */
13444 if (FRAME_WINDOW_P (f))
13446 /* Menu bar lines are displayed in the desired matrix of the
13447 dummy window menu_bar_window. */
13448 struct window *menu_w;
13449 xassert (WINDOWP (f->menu_bar_window));
13450 menu_w = XWINDOW (f->menu_bar_window);
13451 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13452 MENU_FACE_ID);
13453 it.first_visible_x = 0;
13454 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13456 else
13458 /* This is a TTY frame, i.e. character hpos/vpos are used as
13459 pixel x/y. */
13460 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13461 MENU_FACE_ID);
13462 it.first_visible_x = 0;
13463 it.last_visible_x = FRAME_WIDTH (f);
13465 #endif /* not USE_X_TOOLKIT */
13467 if (! mode_line_inverse_video)
13468 /* Force the menu-bar to be displayed in the default face. */
13469 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13471 /* Clear all rows of the menu bar. */
13472 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13474 struct glyph_row *row = it.glyph_row + i;
13475 clear_glyph_row (row);
13476 row->enabled_p = 1;
13477 row->full_width_p = 1;
13480 /* Display all items of the menu bar. */
13481 items = FRAME_MENU_BAR_ITEMS (it.f);
13482 for (i = 0; i < XVECTOR (items)->size; i += 4)
13484 Lisp_Object string;
13486 /* Stop at nil string. */
13487 string = AREF (items, i + 1);
13488 if (NILP (string))
13489 break;
13491 /* Remember where item was displayed. */
13492 AREF (items, i + 3) = make_number (it.hpos);
13494 /* Display the item, pad with one space. */
13495 if (it.current_x < it.last_visible_x)
13496 display_string (NULL, string, Qnil, 0, 0, &it,
13497 SCHARS (string) + 1, 0, 0, -1);
13500 /* Fill out the line with spaces. */
13501 if (it.current_x < it.last_visible_x)
13502 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13504 /* Compute the total height of the lines. */
13505 compute_line_metrics (&it);
13510 /***********************************************************************
13511 Mode Line
13512 ***********************************************************************/
13514 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13515 FORCE is non-zero, redisplay mode lines unconditionally.
13516 Otherwise, redisplay only mode lines that are garbaged. Value is
13517 the number of windows whose mode lines were redisplayed. */
13519 static int
13520 redisplay_mode_lines (window, force)
13521 Lisp_Object window;
13522 int force;
13524 int nwindows = 0;
13526 while (!NILP (window))
13528 struct window *w = XWINDOW (window);
13530 if (WINDOWP (w->hchild))
13531 nwindows += redisplay_mode_lines (w->hchild, force);
13532 else if (WINDOWP (w->vchild))
13533 nwindows += redisplay_mode_lines (w->vchild, force);
13534 else if (force
13535 || FRAME_GARBAGED_P (XFRAME (w->frame))
13536 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13538 struct text_pos lpoint;
13539 struct buffer *old = current_buffer;
13541 /* Set the window's buffer for the mode line display. */
13542 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13543 set_buffer_internal_1 (XBUFFER (w->buffer));
13545 /* Point refers normally to the selected window. For any
13546 other window, set up appropriate value. */
13547 if (!EQ (window, selected_window))
13549 struct text_pos pt;
13551 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13552 if (CHARPOS (pt) < BEGV)
13553 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13554 else if (CHARPOS (pt) > (ZV - 1))
13555 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13556 else
13557 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13560 /* Display mode lines. */
13561 clear_glyph_matrix (w->desired_matrix);
13562 if (display_mode_lines (w))
13564 ++nwindows;
13565 w->must_be_updated_p = 1;
13568 /* Restore old settings. */
13569 set_buffer_internal_1 (old);
13570 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13573 window = w->next;
13576 return nwindows;
13580 /* Display the mode and/or top line of window W. Value is the number
13581 of mode lines displayed. */
13583 static int
13584 display_mode_lines (w)
13585 struct window *w;
13587 Lisp_Object old_selected_window, old_selected_frame;
13588 int n = 0;
13590 old_selected_frame = selected_frame;
13591 selected_frame = w->frame;
13592 old_selected_window = selected_window;
13593 XSETWINDOW (selected_window, w);
13595 /* These will be set while the mode line specs are processed. */
13596 line_number_displayed = 0;
13597 w->column_number_displayed = Qnil;
13599 if (WINDOW_WANTS_MODELINE_P (w))
13601 struct window *sel_w = XWINDOW (old_selected_window);
13603 /* Select mode line face based on the real selected window. */
13604 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13605 current_buffer->mode_line_format);
13606 ++n;
13609 if (WINDOW_WANTS_HEADER_LINE_P (w))
13611 display_mode_line (w, HEADER_LINE_FACE_ID,
13612 current_buffer->header_line_format);
13613 ++n;
13616 selected_frame = old_selected_frame;
13617 selected_window = old_selected_window;
13618 return n;
13622 /* Display mode or top line of window W. FACE_ID specifies which line
13623 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13624 FORMAT is the mode line format to display. Value is the pixel
13625 height of the mode line displayed. */
13627 static int
13628 display_mode_line (w, face_id, format)
13629 struct window *w;
13630 enum face_id face_id;
13631 Lisp_Object format;
13633 struct it it;
13634 struct face *face;
13636 init_iterator (&it, w, -1, -1, NULL, face_id);
13637 prepare_desired_row (it.glyph_row);
13639 if (! mode_line_inverse_video)
13640 /* Force the mode-line to be displayed in the default face. */
13641 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13643 /* Temporarily make frame's keyboard the current kboard so that
13644 kboard-local variables in the mode_line_format will get the right
13645 values. */
13646 push_frame_kboard (it.f);
13647 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13648 pop_frame_kboard ();
13650 /* Fill up with spaces. */
13651 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13653 compute_line_metrics (&it);
13654 it.glyph_row->full_width_p = 1;
13655 it.glyph_row->mode_line_p = 1;
13656 it.glyph_row->continued_p = 0;
13657 it.glyph_row->truncated_on_left_p = 0;
13658 it.glyph_row->truncated_on_right_p = 0;
13660 /* Make a 3D mode-line have a shadow at its right end. */
13661 face = FACE_FROM_ID (it.f, face_id);
13662 extend_face_to_end_of_line (&it);
13663 if (face->box != FACE_NO_BOX)
13665 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13666 + it.glyph_row->used[TEXT_AREA] - 1);
13667 last->right_box_line_p = 1;
13670 return it.glyph_row->height;
13673 /* Alist that caches the results of :propertize.
13674 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13675 Lisp_Object mode_line_proptrans_alist;
13677 /* List of strings making up the mode-line. */
13678 Lisp_Object mode_line_string_list;
13680 /* Base face property when building propertized mode line string. */
13681 static Lisp_Object mode_line_string_face;
13682 static Lisp_Object mode_line_string_face_prop;
13685 /* Contribute ELT to the mode line for window IT->w. How it
13686 translates into text depends on its data type.
13688 IT describes the display environment in which we display, as usual.
13690 DEPTH is the depth in recursion. It is used to prevent
13691 infinite recursion here.
13693 FIELD_WIDTH is the number of characters the display of ELT should
13694 occupy in the mode line, and PRECISION is the maximum number of
13695 characters to display from ELT's representation. See
13696 display_string for details.
13698 Returns the hpos of the end of the text generated by ELT.
13700 PROPS is a property list to add to any string we encounter.
13702 If RISKY is nonzero, remove (disregard) any properties in any string
13703 we encounter, and ignore :eval and :propertize.
13705 If the global variable `frame_title_ptr' is non-NULL, then the output
13706 is passed to `store_frame_title' instead of `display_string'. */
13708 static int
13709 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13710 struct it *it;
13711 int depth;
13712 int field_width, precision;
13713 Lisp_Object elt, props;
13714 int risky;
13716 int n = 0, field, prec;
13717 int literal = 0;
13719 tail_recurse:
13720 if (depth > 10)
13721 goto invalid;
13723 depth++;
13725 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13727 case Lisp_String:
13729 /* A string: output it and check for %-constructs within it. */
13730 unsigned char c;
13731 const unsigned char *this, *lisp_string;
13733 if (!NILP (props) || risky)
13735 Lisp_Object oprops, aelt;
13736 oprops = Ftext_properties_at (make_number (0), elt);
13738 if (NILP (Fequal (props, oprops)) || risky)
13740 /* If the starting string has properties,
13741 merge the specified ones onto the existing ones. */
13742 if (! NILP (oprops) && !risky)
13744 Lisp_Object tem;
13746 oprops = Fcopy_sequence (oprops);
13747 tem = props;
13748 while (CONSP (tem))
13750 oprops = Fplist_put (oprops, XCAR (tem),
13751 XCAR (XCDR (tem)));
13752 tem = XCDR (XCDR (tem));
13754 props = oprops;
13757 aelt = Fassoc (elt, mode_line_proptrans_alist);
13758 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13760 mode_line_proptrans_alist
13761 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13762 elt = XCAR (aelt);
13764 else
13766 Lisp_Object tem;
13768 elt = Fcopy_sequence (elt);
13769 Fset_text_properties (make_number (0), Flength (elt),
13770 props, elt);
13771 /* Add this item to mode_line_proptrans_alist. */
13772 mode_line_proptrans_alist
13773 = Fcons (Fcons (elt, props),
13774 mode_line_proptrans_alist);
13775 /* Truncate mode_line_proptrans_alist
13776 to at most 50 elements. */
13777 tem = Fnthcdr (make_number (50),
13778 mode_line_proptrans_alist);
13779 if (! NILP (tem))
13780 XSETCDR (tem, Qnil);
13785 this = SDATA (elt);
13786 lisp_string = this;
13788 if (literal)
13790 prec = precision - n;
13791 if (frame_title_ptr)
13792 n += store_frame_title (SDATA (elt), -1, prec);
13793 else if (!NILP (mode_line_string_list))
13794 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13795 else
13796 n += display_string (NULL, elt, Qnil, 0, 0, it,
13797 0, prec, 0, STRING_MULTIBYTE (elt));
13799 break;
13802 while ((precision <= 0 || n < precision)
13803 && *this
13804 && (frame_title_ptr
13805 || !NILP (mode_line_string_list)
13806 || it->current_x < it->last_visible_x))
13808 const unsigned char *last = this;
13810 /* Advance to end of string or next format specifier. */
13811 while ((c = *this++) != '\0' && c != '%')
13814 if (this - 1 != last)
13816 /* Output to end of string or up to '%'. Field width
13817 is length of string. Don't output more than
13818 PRECISION allows us. */
13819 --this;
13821 prec = chars_in_text (last, this - last);
13822 if (precision > 0 && prec > precision - n)
13823 prec = precision - n;
13825 if (frame_title_ptr)
13826 n += store_frame_title (last, 0, prec);
13827 else if (!NILP (mode_line_string_list))
13829 int bytepos = last - lisp_string;
13830 int charpos = string_byte_to_char (elt, bytepos);
13831 n += store_mode_line_string (NULL,
13832 Fsubstring (elt, make_number (charpos),
13833 make_number (charpos + prec)),
13834 0, 0, 0, Qnil);
13836 else
13838 int bytepos = last - lisp_string;
13839 int charpos = string_byte_to_char (elt, bytepos);
13840 n += display_string (NULL, elt, Qnil, 0, charpos,
13841 it, 0, prec, 0,
13842 STRING_MULTIBYTE (elt));
13845 else /* c == '%' */
13847 const unsigned char *percent_position = this;
13849 /* Get the specified minimum width. Zero means
13850 don't pad. */
13851 field = 0;
13852 while ((c = *this++) >= '0' && c <= '9')
13853 field = field * 10 + c - '0';
13855 /* Don't pad beyond the total padding allowed. */
13856 if (field_width - n > 0 && field > field_width - n)
13857 field = field_width - n;
13859 /* Note that either PRECISION <= 0 or N < PRECISION. */
13860 prec = precision - n;
13862 if (c == 'M')
13863 n += display_mode_element (it, depth, field, prec,
13864 Vglobal_mode_string, props,
13865 risky);
13866 else if (c != 0)
13868 int multibyte;
13869 int bytepos, charpos;
13870 unsigned char *spec;
13872 bytepos = percent_position - lisp_string;
13873 charpos = (STRING_MULTIBYTE (elt)
13874 ? string_byte_to_char (elt, bytepos)
13875 : bytepos);
13877 spec
13878 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13880 if (frame_title_ptr)
13881 n += store_frame_title (spec, field, prec);
13882 else if (!NILP (mode_line_string_list))
13884 int len = strlen (spec);
13885 Lisp_Object tem = make_string (spec, len);
13886 props = Ftext_properties_at (make_number (charpos), elt);
13887 /* Should only keep face property in props */
13888 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13890 else
13892 int nglyphs_before, nwritten;
13894 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13895 nwritten = display_string (spec, Qnil, elt,
13896 charpos, 0, it,
13897 field, prec, 0,
13898 multibyte);
13900 /* Assign to the glyphs written above the
13901 string where the `%x' came from, position
13902 of the `%'. */
13903 if (nwritten > 0)
13905 struct glyph *glyph
13906 = (it->glyph_row->glyphs[TEXT_AREA]
13907 + nglyphs_before);
13908 int i;
13910 for (i = 0; i < nwritten; ++i)
13912 glyph[i].object = elt;
13913 glyph[i].charpos = charpos;
13916 n += nwritten;
13920 else /* c == 0 */
13921 break;
13925 break;
13927 case Lisp_Symbol:
13928 /* A symbol: process the value of the symbol recursively
13929 as if it appeared here directly. Avoid error if symbol void.
13930 Special case: if value of symbol is a string, output the string
13931 literally. */
13933 register Lisp_Object tem;
13935 /* If the variable is not marked as risky to set
13936 then its contents are risky to use. */
13937 if (NILP (Fget (elt, Qrisky_local_variable)))
13938 risky = 1;
13940 tem = Fboundp (elt);
13941 if (!NILP (tem))
13943 tem = Fsymbol_value (elt);
13944 /* If value is a string, output that string literally:
13945 don't check for % within it. */
13946 if (STRINGP (tem))
13947 literal = 1;
13949 if (!EQ (tem, elt))
13951 /* Give up right away for nil or t. */
13952 elt = tem;
13953 goto tail_recurse;
13957 break;
13959 case Lisp_Cons:
13961 register Lisp_Object car, tem;
13963 /* A cons cell: five distinct cases.
13964 If first element is :eval or :propertize, do something special.
13965 If first element is a string or a cons, process all the elements
13966 and effectively concatenate them.
13967 If first element is a negative number, truncate displaying cdr to
13968 at most that many characters. If positive, pad (with spaces)
13969 to at least that many characters.
13970 If first element is a symbol, process the cadr or caddr recursively
13971 according to whether the symbol's value is non-nil or nil. */
13972 car = XCAR (elt);
13973 if (EQ (car, QCeval))
13975 /* An element of the form (:eval FORM) means evaluate FORM
13976 and use the result as mode line elements. */
13978 if (risky)
13979 break;
13981 if (CONSP (XCDR (elt)))
13983 Lisp_Object spec;
13984 spec = safe_eval (XCAR (XCDR (elt)));
13985 n += display_mode_element (it, depth, field_width - n,
13986 precision - n, spec, props,
13987 risky);
13990 else if (EQ (car, QCpropertize))
13992 /* An element of the form (:propertize ELT PROPS...)
13993 means display ELT but applying properties PROPS. */
13995 if (risky)
13996 break;
13998 if (CONSP (XCDR (elt)))
13999 n += display_mode_element (it, depth, field_width - n,
14000 precision - n, XCAR (XCDR (elt)),
14001 XCDR (XCDR (elt)), risky);
14003 else if (SYMBOLP (car))
14005 tem = Fboundp (car);
14006 elt = XCDR (elt);
14007 if (!CONSP (elt))
14008 goto invalid;
14009 /* elt is now the cdr, and we know it is a cons cell.
14010 Use its car if CAR has a non-nil value. */
14011 if (!NILP (tem))
14013 tem = Fsymbol_value (car);
14014 if (!NILP (tem))
14016 elt = XCAR (elt);
14017 goto tail_recurse;
14020 /* Symbol's value is nil (or symbol is unbound)
14021 Get the cddr of the original list
14022 and if possible find the caddr and use that. */
14023 elt = XCDR (elt);
14024 if (NILP (elt))
14025 break;
14026 else if (!CONSP (elt))
14027 goto invalid;
14028 elt = XCAR (elt);
14029 goto tail_recurse;
14031 else if (INTEGERP (car))
14033 register int lim = XINT (car);
14034 elt = XCDR (elt);
14035 if (lim < 0)
14037 /* Negative int means reduce maximum width. */
14038 if (precision <= 0)
14039 precision = -lim;
14040 else
14041 precision = min (precision, -lim);
14043 else if (lim > 0)
14045 /* Padding specified. Don't let it be more than
14046 current maximum. */
14047 if (precision > 0)
14048 lim = min (precision, lim);
14050 /* If that's more padding than already wanted, queue it.
14051 But don't reduce padding already specified even if
14052 that is beyond the current truncation point. */
14053 field_width = max (lim, field_width);
14055 goto tail_recurse;
14057 else if (STRINGP (car) || CONSP (car))
14059 register int limit = 50;
14060 /* Limit is to protect against circular lists. */
14061 while (CONSP (elt)
14062 && --limit > 0
14063 && (precision <= 0 || n < precision))
14065 n += display_mode_element (it, depth, field_width - n,
14066 precision - n, XCAR (elt),
14067 props, risky);
14068 elt = XCDR (elt);
14072 break;
14074 default:
14075 invalid:
14076 if (frame_title_ptr)
14077 n += store_frame_title ("*invalid*", 0, precision - n);
14078 else if (!NILP (mode_line_string_list))
14079 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14080 else
14081 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14082 precision - n, 0, 0);
14083 return n;
14086 /* Pad to FIELD_WIDTH. */
14087 if (field_width > 0 && n < field_width)
14089 if (frame_title_ptr)
14090 n += store_frame_title ("", field_width - n, 0);
14091 else if (!NILP (mode_line_string_list))
14092 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14093 else
14094 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14095 0, 0, 0);
14098 return n;
14101 /* Store a mode-line string element in mode_line_string_list.
14103 If STRING is non-null, display that C string. Otherwise, the Lisp
14104 string LISP_STRING is displayed.
14106 FIELD_WIDTH is the minimum number of output glyphs to produce.
14107 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14108 with spaces. FIELD_WIDTH <= 0 means don't pad.
14110 PRECISION is the maximum number of characters to output from
14111 STRING. PRECISION <= 0 means don't truncate the string.
14113 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14114 properties to the string.
14116 PROPS are the properties to add to the string.
14117 The mode_line_string_face face property is always added to the string.
14120 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14121 char *string;
14122 Lisp_Object lisp_string;
14123 int copy_string;
14124 int field_width;
14125 int precision;
14126 Lisp_Object props;
14128 int len;
14129 int n = 0;
14131 if (string != NULL)
14133 len = strlen (string);
14134 if (precision > 0 && len > precision)
14135 len = precision;
14136 lisp_string = make_string (string, len);
14137 if (NILP (props))
14138 props = mode_line_string_face_prop;
14139 else if (!NILP (mode_line_string_face))
14141 Lisp_Object face = Fplist_get (props, Qface);
14142 props = Fcopy_sequence (props);
14143 if (NILP (face))
14144 face = mode_line_string_face;
14145 else
14146 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14147 props = Fplist_put (props, Qface, face);
14149 Fadd_text_properties (make_number (0), make_number (len),
14150 props, lisp_string);
14152 else
14154 len = XFASTINT (Flength (lisp_string));
14155 if (precision > 0 && len > precision)
14157 len = precision;
14158 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14159 precision = -1;
14161 if (!NILP (mode_line_string_face))
14163 Lisp_Object face;
14164 if (NILP (props))
14165 props = Ftext_properties_at (make_number (0), lisp_string);
14166 face = Fplist_get (props, Qface);
14167 if (NILP (face))
14168 face = mode_line_string_face;
14169 else
14170 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14171 props = Fcons (Qface, Fcons (face, Qnil));
14172 if (copy_string)
14173 lisp_string = Fcopy_sequence (lisp_string);
14175 if (!NILP (props))
14176 Fadd_text_properties (make_number (0), make_number (len),
14177 props, lisp_string);
14180 if (len > 0)
14182 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14183 n += len;
14186 if (field_width > len)
14188 field_width -= len;
14189 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14190 if (!NILP (props))
14191 Fadd_text_properties (make_number (0), make_number (field_width),
14192 props, lisp_string);
14193 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14194 n += field_width;
14197 return n;
14201 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14202 0, 3, 0,
14203 doc: /* Return the mode-line of selected window as a string.
14204 First optional arg FORMAT specifies a different format string (see
14205 `mode-line-format' for details) to use. If FORMAT is t, return
14206 the buffer's header-line. Second optional arg WINDOW specifies a
14207 different window to use as the context for the formatting.
14208 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14209 (format, window, no_props)
14210 Lisp_Object format, window, no_props;
14212 struct it it;
14213 int len;
14214 struct window *w;
14215 struct buffer *old_buffer = NULL;
14216 enum face_id face_id = DEFAULT_FACE_ID;
14218 if (NILP (window))
14219 window = selected_window;
14220 CHECK_WINDOW (window);
14221 w = XWINDOW (window);
14222 CHECK_BUFFER (w->buffer);
14224 if (XBUFFER (w->buffer) != current_buffer)
14226 old_buffer = current_buffer;
14227 set_buffer_internal_1 (XBUFFER (w->buffer));
14230 if (NILP (format) || EQ (format, Qt))
14232 face_id = NILP (format)
14233 ? CURRENT_MODE_LINE_FACE_ID (w) :
14234 HEADER_LINE_FACE_ID;
14235 format = NILP (format)
14236 ? current_buffer->mode_line_format
14237 : current_buffer->header_line_format;
14240 init_iterator (&it, w, -1, -1, NULL, face_id);
14242 if (NILP (no_props))
14244 mode_line_string_face =
14245 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14246 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14247 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14249 mode_line_string_face_prop =
14250 NILP (mode_line_string_face) ? Qnil :
14251 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14253 /* We need a dummy last element in mode_line_string_list to
14254 indicate we are building the propertized mode-line string.
14255 Using mode_line_string_face_prop here GC protects it. */
14256 mode_line_string_list =
14257 Fcons (mode_line_string_face_prop, Qnil);
14258 frame_title_ptr = NULL;
14260 else
14262 mode_line_string_face_prop = Qnil;
14263 mode_line_string_list = Qnil;
14264 frame_title_ptr = frame_title_buf;
14267 push_frame_kboard (it.f);
14268 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14269 pop_frame_kboard ();
14271 if (old_buffer)
14272 set_buffer_internal_1 (old_buffer);
14274 if (NILP (no_props))
14276 Lisp_Object str;
14277 mode_line_string_list = Fnreverse (mode_line_string_list);
14278 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14279 make_string ("", 0));
14280 mode_line_string_face_prop = Qnil;
14281 mode_line_string_list = Qnil;
14282 return str;
14285 len = frame_title_ptr - frame_title_buf;
14286 if (len > 0 && frame_title_ptr[-1] == '-')
14288 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14289 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14291 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14292 if (len > frame_title_ptr - frame_title_buf)
14293 len = frame_title_ptr - frame_title_buf;
14296 frame_title_ptr = NULL;
14297 return make_string (frame_title_buf, len);
14300 /* Write a null-terminated, right justified decimal representation of
14301 the positive integer D to BUF using a minimal field width WIDTH. */
14303 static void
14304 pint2str (buf, width, d)
14305 register char *buf;
14306 register int width;
14307 register int d;
14309 register char *p = buf;
14311 if (d <= 0)
14312 *p++ = '0';
14313 else
14315 while (d > 0)
14317 *p++ = d % 10 + '0';
14318 d /= 10;
14322 for (width -= (int) (p - buf); width > 0; --width)
14323 *p++ = ' ';
14324 *p-- = '\0';
14325 while (p > buf)
14327 d = *buf;
14328 *buf++ = *p;
14329 *p-- = d;
14333 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14334 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14335 type of CODING_SYSTEM. Return updated pointer into BUF. */
14337 static unsigned char invalid_eol_type[] = "(*invalid*)";
14339 static char *
14340 decode_mode_spec_coding (coding_system, buf, eol_flag)
14341 Lisp_Object coding_system;
14342 register char *buf;
14343 int eol_flag;
14345 Lisp_Object val;
14346 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14347 const unsigned char *eol_str;
14348 int eol_str_len;
14349 /* The EOL conversion we are using. */
14350 Lisp_Object eoltype;
14352 val = Fget (coding_system, Qcoding_system);
14353 eoltype = Qnil;
14355 if (!VECTORP (val)) /* Not yet decided. */
14357 if (multibyte)
14358 *buf++ = '-';
14359 if (eol_flag)
14360 eoltype = eol_mnemonic_undecided;
14361 /* Don't mention EOL conversion if it isn't decided. */
14363 else
14365 Lisp_Object eolvalue;
14367 eolvalue = Fget (coding_system, Qeol_type);
14369 if (multibyte)
14370 *buf++ = XFASTINT (AREF (val, 1));
14372 if (eol_flag)
14374 /* The EOL conversion that is normal on this system. */
14376 if (NILP (eolvalue)) /* Not yet decided. */
14377 eoltype = eol_mnemonic_undecided;
14378 else if (VECTORP (eolvalue)) /* Not yet decided. */
14379 eoltype = eol_mnemonic_undecided;
14380 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14381 eoltype = (XFASTINT (eolvalue) == 0
14382 ? eol_mnemonic_unix
14383 : (XFASTINT (eolvalue) == 1
14384 ? eol_mnemonic_dos : eol_mnemonic_mac));
14388 if (eol_flag)
14390 /* Mention the EOL conversion if it is not the usual one. */
14391 if (STRINGP (eoltype))
14393 eol_str = SDATA (eoltype);
14394 eol_str_len = SBYTES (eoltype);
14396 else if (INTEGERP (eoltype)
14397 && CHAR_VALID_P (XINT (eoltype), 0))
14399 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14400 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14401 eol_str = tmp;
14403 else
14405 eol_str = invalid_eol_type;
14406 eol_str_len = sizeof (invalid_eol_type) - 1;
14408 bcopy (eol_str, buf, eol_str_len);
14409 buf += eol_str_len;
14412 return buf;
14415 /* Return a string for the output of a mode line %-spec for window W,
14416 generated by character C. PRECISION >= 0 means don't return a
14417 string longer than that value. FIELD_WIDTH > 0 means pad the
14418 string returned with spaces to that value. Return 1 in *MULTIBYTE
14419 if the result is multibyte text. */
14421 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14423 static char *
14424 decode_mode_spec (w, c, field_width, precision, multibyte)
14425 struct window *w;
14426 register int c;
14427 int field_width, precision;
14428 int *multibyte;
14430 Lisp_Object obj;
14431 struct frame *f = XFRAME (WINDOW_FRAME (w));
14432 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14433 struct buffer *b = XBUFFER (w->buffer);
14435 obj = Qnil;
14436 *multibyte = 0;
14438 switch (c)
14440 case '*':
14441 if (!NILP (b->read_only))
14442 return "%";
14443 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14444 return "*";
14445 return "-";
14447 case '+':
14448 /* This differs from %* only for a modified read-only buffer. */
14449 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14450 return "*";
14451 if (!NILP (b->read_only))
14452 return "%";
14453 return "-";
14455 case '&':
14456 /* This differs from %* in ignoring read-only-ness. */
14457 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14458 return "*";
14459 return "-";
14461 case '%':
14462 return "%";
14464 case '[':
14466 int i;
14467 char *p;
14469 if (command_loop_level > 5)
14470 return "[[[... ";
14471 p = decode_mode_spec_buf;
14472 for (i = 0; i < command_loop_level; i++)
14473 *p++ = '[';
14474 *p = 0;
14475 return decode_mode_spec_buf;
14478 case ']':
14480 int i;
14481 char *p;
14483 if (command_loop_level > 5)
14484 return " ...]]]";
14485 p = decode_mode_spec_buf;
14486 for (i = 0; i < command_loop_level; i++)
14487 *p++ = ']';
14488 *p = 0;
14489 return decode_mode_spec_buf;
14492 case '-':
14494 register int i;
14496 /* Let lots_of_dashes be a string of infinite length. */
14497 if (!NILP (mode_line_string_list))
14498 return "--";
14499 if (field_width <= 0
14500 || field_width > sizeof (lots_of_dashes))
14502 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14503 decode_mode_spec_buf[i] = '-';
14504 decode_mode_spec_buf[i] = '\0';
14505 return decode_mode_spec_buf;
14507 else
14508 return lots_of_dashes;
14511 case 'b':
14512 obj = b->name;
14513 break;
14515 case 'c':
14517 int col = (int) current_column (); /* iftc */
14518 w->column_number_displayed = make_number (col);
14519 pint2str (decode_mode_spec_buf, field_width, col);
14520 return decode_mode_spec_buf;
14523 case 'F':
14524 /* %F displays the frame name. */
14525 if (!NILP (f->title))
14526 return (char *) SDATA (f->title);
14527 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14528 return (char *) SDATA (f->name);
14529 return "Emacs";
14531 case 'f':
14532 obj = b->filename;
14533 break;
14535 case 'l':
14537 int startpos = XMARKER (w->start)->charpos;
14538 int startpos_byte = marker_byte_position (w->start);
14539 int line, linepos, linepos_byte, topline;
14540 int nlines, junk;
14541 int height = XFASTINT (w->height);
14543 /* If we decided that this buffer isn't suitable for line numbers,
14544 don't forget that too fast. */
14545 if (EQ (w->base_line_pos, w->buffer))
14546 goto no_value;
14547 /* But do forget it, if the window shows a different buffer now. */
14548 else if (BUFFERP (w->base_line_pos))
14549 w->base_line_pos = Qnil;
14551 /* If the buffer is very big, don't waste time. */
14552 if (INTEGERP (Vline_number_display_limit)
14553 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14555 w->base_line_pos = Qnil;
14556 w->base_line_number = Qnil;
14557 goto no_value;
14560 if (!NILP (w->base_line_number)
14561 && !NILP (w->base_line_pos)
14562 && XFASTINT (w->base_line_pos) <= startpos)
14564 line = XFASTINT (w->base_line_number);
14565 linepos = XFASTINT (w->base_line_pos);
14566 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14568 else
14570 line = 1;
14571 linepos = BUF_BEGV (b);
14572 linepos_byte = BUF_BEGV_BYTE (b);
14575 /* Count lines from base line to window start position. */
14576 nlines = display_count_lines (linepos, linepos_byte,
14577 startpos_byte,
14578 startpos, &junk);
14580 topline = nlines + line;
14582 /* Determine a new base line, if the old one is too close
14583 or too far away, or if we did not have one.
14584 "Too close" means it's plausible a scroll-down would
14585 go back past it. */
14586 if (startpos == BUF_BEGV (b))
14588 w->base_line_number = make_number (topline);
14589 w->base_line_pos = make_number (BUF_BEGV (b));
14591 else if (nlines < height + 25 || nlines > height * 3 + 50
14592 || linepos == BUF_BEGV (b))
14594 int limit = BUF_BEGV (b);
14595 int limit_byte = BUF_BEGV_BYTE (b);
14596 int position;
14597 int distance = (height * 2 + 30) * line_number_display_limit_width;
14599 if (startpos - distance > limit)
14601 limit = startpos - distance;
14602 limit_byte = CHAR_TO_BYTE (limit);
14605 nlines = display_count_lines (startpos, startpos_byte,
14606 limit_byte,
14607 - (height * 2 + 30),
14608 &position);
14609 /* If we couldn't find the lines we wanted within
14610 line_number_display_limit_width chars per line,
14611 give up on line numbers for this window. */
14612 if (position == limit_byte && limit == startpos - distance)
14614 w->base_line_pos = w->buffer;
14615 w->base_line_number = Qnil;
14616 goto no_value;
14619 w->base_line_number = make_number (topline - nlines);
14620 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14623 /* Now count lines from the start pos to point. */
14624 nlines = display_count_lines (startpos, startpos_byte,
14625 PT_BYTE, PT, &junk);
14627 /* Record that we did display the line number. */
14628 line_number_displayed = 1;
14630 /* Make the string to show. */
14631 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14632 return decode_mode_spec_buf;
14633 no_value:
14635 char* p = decode_mode_spec_buf;
14636 int pad = field_width - 2;
14637 while (pad-- > 0)
14638 *p++ = ' ';
14639 *p++ = '?';
14640 *p++ = '?';
14641 *p = '\0';
14642 return decode_mode_spec_buf;
14645 break;
14647 case 'm':
14648 obj = b->mode_name;
14649 break;
14651 case 'n':
14652 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14653 return " Narrow";
14654 break;
14656 case 'p':
14658 int pos = marker_position (w->start);
14659 int total = BUF_ZV (b) - BUF_BEGV (b);
14661 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14663 if (pos <= BUF_BEGV (b))
14664 return "All";
14665 else
14666 return "Bottom";
14668 else if (pos <= BUF_BEGV (b))
14669 return "Top";
14670 else
14672 if (total > 1000000)
14673 /* Do it differently for a large value, to avoid overflow. */
14674 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14675 else
14676 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14677 /* We can't normally display a 3-digit number,
14678 so get us a 2-digit number that is close. */
14679 if (total == 100)
14680 total = 99;
14681 sprintf (decode_mode_spec_buf, "%2d%%", total);
14682 return decode_mode_spec_buf;
14686 /* Display percentage of size above the bottom of the screen. */
14687 case 'P':
14689 int toppos = marker_position (w->start);
14690 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14691 int total = BUF_ZV (b) - BUF_BEGV (b);
14693 if (botpos >= BUF_ZV (b))
14695 if (toppos <= BUF_BEGV (b))
14696 return "All";
14697 else
14698 return "Bottom";
14700 else
14702 if (total > 1000000)
14703 /* Do it differently for a large value, to avoid overflow. */
14704 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14705 else
14706 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14707 /* We can't normally display a 3-digit number,
14708 so get us a 2-digit number that is close. */
14709 if (total == 100)
14710 total = 99;
14711 if (toppos <= BUF_BEGV (b))
14712 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14713 else
14714 sprintf (decode_mode_spec_buf, "%2d%%", total);
14715 return decode_mode_spec_buf;
14719 case 's':
14720 /* status of process */
14721 obj = Fget_buffer_process (w->buffer);
14722 if (NILP (obj))
14723 return "no process";
14724 #ifdef subprocesses
14725 obj = Fsymbol_name (Fprocess_status (obj));
14726 #endif
14727 break;
14729 case 't': /* indicate TEXT or BINARY */
14730 #ifdef MODE_LINE_BINARY_TEXT
14731 return MODE_LINE_BINARY_TEXT (b);
14732 #else
14733 return "T";
14734 #endif
14736 case 'z':
14737 /* coding-system (not including end-of-line format) */
14738 case 'Z':
14739 /* coding-system (including end-of-line type) */
14741 int eol_flag = (c == 'Z');
14742 char *p = decode_mode_spec_buf;
14744 if (! FRAME_WINDOW_P (f))
14746 /* No need to mention EOL here--the terminal never needs
14747 to do EOL conversion. */
14748 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14749 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14751 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14752 p, eol_flag);
14754 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14755 #ifdef subprocesses
14756 obj = Fget_buffer_process (Fcurrent_buffer ());
14757 if (PROCESSP (obj))
14759 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14760 p, eol_flag);
14761 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14762 p, eol_flag);
14764 #endif /* subprocesses */
14765 #endif /* 0 */
14766 *p = 0;
14767 return decode_mode_spec_buf;
14771 if (STRINGP (obj))
14773 *multibyte = STRING_MULTIBYTE (obj);
14774 return (char *) SDATA (obj);
14776 else
14777 return "";
14781 /* Count up to COUNT lines starting from START / START_BYTE.
14782 But don't go beyond LIMIT_BYTE.
14783 Return the number of lines thus found (always nonnegative).
14785 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14787 static int
14788 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14789 int start, start_byte, limit_byte, count;
14790 int *byte_pos_ptr;
14792 register unsigned char *cursor;
14793 unsigned char *base;
14795 register int ceiling;
14796 register unsigned char *ceiling_addr;
14797 int orig_count = count;
14799 /* If we are not in selective display mode,
14800 check only for newlines. */
14801 int selective_display = (!NILP (current_buffer->selective_display)
14802 && !INTEGERP (current_buffer->selective_display));
14804 if (count > 0)
14806 while (start_byte < limit_byte)
14808 ceiling = BUFFER_CEILING_OF (start_byte);
14809 ceiling = min (limit_byte - 1, ceiling);
14810 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14811 base = (cursor = BYTE_POS_ADDR (start_byte));
14812 while (1)
14814 if (selective_display)
14815 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14817 else
14818 while (*cursor != '\n' && ++cursor != ceiling_addr)
14821 if (cursor != ceiling_addr)
14823 if (--count == 0)
14825 start_byte += cursor - base + 1;
14826 *byte_pos_ptr = start_byte;
14827 return orig_count;
14829 else
14830 if (++cursor == ceiling_addr)
14831 break;
14833 else
14834 break;
14836 start_byte += cursor - base;
14839 else
14841 while (start_byte > limit_byte)
14843 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14844 ceiling = max (limit_byte, ceiling);
14845 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14846 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14847 while (1)
14849 if (selective_display)
14850 while (--cursor != ceiling_addr
14851 && *cursor != '\n' && *cursor != 015)
14853 else
14854 while (--cursor != ceiling_addr && *cursor != '\n')
14857 if (cursor != ceiling_addr)
14859 if (++count == 0)
14861 start_byte += cursor - base + 1;
14862 *byte_pos_ptr = start_byte;
14863 /* When scanning backwards, we should
14864 not count the newline posterior to which we stop. */
14865 return - orig_count - 1;
14868 else
14869 break;
14871 /* Here we add 1 to compensate for the last decrement
14872 of CURSOR, which took it past the valid range. */
14873 start_byte += cursor - base + 1;
14877 *byte_pos_ptr = limit_byte;
14879 if (count < 0)
14880 return - orig_count + count;
14881 return orig_count - count;
14887 /***********************************************************************
14888 Displaying strings
14889 ***********************************************************************/
14891 /* Display a NUL-terminated string, starting with index START.
14893 If STRING is non-null, display that C string. Otherwise, the Lisp
14894 string LISP_STRING is displayed.
14896 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14897 FACE_STRING. Display STRING or LISP_STRING with the face at
14898 FACE_STRING_POS in FACE_STRING:
14900 Display the string in the environment given by IT, but use the
14901 standard display table, temporarily.
14903 FIELD_WIDTH is the minimum number of output glyphs to produce.
14904 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14905 with spaces. If STRING has more characters, more than FIELD_WIDTH
14906 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14908 PRECISION is the maximum number of characters to output from
14909 STRING. PRECISION < 0 means don't truncate the string.
14911 This is roughly equivalent to printf format specifiers:
14913 FIELD_WIDTH PRECISION PRINTF
14914 ----------------------------------------
14915 -1 -1 %s
14916 -1 10 %.10s
14917 10 -1 %10s
14918 20 10 %20.10s
14920 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14921 display them, and < 0 means obey the current buffer's value of
14922 enable_multibyte_characters.
14924 Value is the number of glyphs produced. */
14926 static int
14927 display_string (string, lisp_string, face_string, face_string_pos,
14928 start, it, field_width, precision, max_x, multibyte)
14929 unsigned char *string;
14930 Lisp_Object lisp_string;
14931 Lisp_Object face_string;
14932 int face_string_pos;
14933 int start;
14934 struct it *it;
14935 int field_width, precision, max_x;
14936 int multibyte;
14938 int hpos_at_start = it->hpos;
14939 int saved_face_id = it->face_id;
14940 struct glyph_row *row = it->glyph_row;
14942 /* Initialize the iterator IT for iteration over STRING beginning
14943 with index START. */
14944 reseat_to_string (it, string, lisp_string, start,
14945 precision, field_width, multibyte);
14947 /* If displaying STRING, set up the face of the iterator
14948 from LISP_STRING, if that's given. */
14949 if (STRINGP (face_string))
14951 int endptr;
14952 struct face *face;
14954 it->face_id
14955 = face_at_string_position (it->w, face_string, face_string_pos,
14956 0, it->region_beg_charpos,
14957 it->region_end_charpos,
14958 &endptr, it->base_face_id, 0);
14959 face = FACE_FROM_ID (it->f, it->face_id);
14960 it->face_box_p = face->box != FACE_NO_BOX;
14963 /* Set max_x to the maximum allowed X position. Don't let it go
14964 beyond the right edge of the window. */
14965 if (max_x <= 0)
14966 max_x = it->last_visible_x;
14967 else
14968 max_x = min (max_x, it->last_visible_x);
14970 /* Skip over display elements that are not visible. because IT->w is
14971 hscrolled. */
14972 if (it->current_x < it->first_visible_x)
14973 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14974 MOVE_TO_POS | MOVE_TO_X);
14976 row->ascent = it->max_ascent;
14977 row->height = it->max_ascent + it->max_descent;
14978 row->phys_ascent = it->max_phys_ascent;
14979 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14981 /* This condition is for the case that we are called with current_x
14982 past last_visible_x. */
14983 while (it->current_x < max_x)
14985 int x_before, x, n_glyphs_before, i, nglyphs;
14987 /* Get the next display element. */
14988 if (!get_next_display_element (it))
14989 break;
14991 /* Produce glyphs. */
14992 x_before = it->current_x;
14993 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14994 PRODUCE_GLYPHS (it);
14996 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14997 i = 0;
14998 x = x_before;
14999 while (i < nglyphs)
15001 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15003 if (!it->truncate_lines_p
15004 && x + glyph->pixel_width > max_x)
15006 /* End of continued line or max_x reached. */
15007 if (CHAR_GLYPH_PADDING_P (*glyph))
15009 /* A wide character is unbreakable. */
15010 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15011 it->current_x = x_before;
15013 else
15015 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15016 it->current_x = x;
15018 break;
15020 else if (x + glyph->pixel_width > it->first_visible_x)
15022 /* Glyph is at least partially visible. */
15023 ++it->hpos;
15024 if (x < it->first_visible_x)
15025 it->glyph_row->x = x - it->first_visible_x;
15027 else
15029 /* Glyph is off the left margin of the display area.
15030 Should not happen. */
15031 abort ();
15034 row->ascent = max (row->ascent, it->max_ascent);
15035 row->height = max (row->height, it->max_ascent + it->max_descent);
15036 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15037 row->phys_height = max (row->phys_height,
15038 it->max_phys_ascent + it->max_phys_descent);
15039 x += glyph->pixel_width;
15040 ++i;
15043 /* Stop if max_x reached. */
15044 if (i < nglyphs)
15045 break;
15047 /* Stop at line ends. */
15048 if (ITERATOR_AT_END_OF_LINE_P (it))
15050 it->continuation_lines_width = 0;
15051 break;
15054 set_iterator_to_next (it, 1);
15056 /* Stop if truncating at the right edge. */
15057 if (it->truncate_lines_p
15058 && it->current_x >= it->last_visible_x)
15060 /* Add truncation mark, but don't do it if the line is
15061 truncated at a padding space. */
15062 if (IT_CHARPOS (*it) < it->string_nchars)
15064 if (!FRAME_WINDOW_P (it->f))
15066 int i, n;
15068 if (it->current_x > it->last_visible_x)
15070 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15071 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15072 break;
15073 for (n = row->used[TEXT_AREA]; i < n; ++i)
15075 row->used[TEXT_AREA] = i;
15076 produce_special_glyphs (it, IT_TRUNCATION);
15079 produce_special_glyphs (it, IT_TRUNCATION);
15081 it->glyph_row->truncated_on_right_p = 1;
15083 break;
15087 /* Maybe insert a truncation at the left. */
15088 if (it->first_visible_x
15089 && IT_CHARPOS (*it) > 0)
15091 if (!FRAME_WINDOW_P (it->f))
15092 insert_left_trunc_glyphs (it);
15093 it->glyph_row->truncated_on_left_p = 1;
15096 it->face_id = saved_face_id;
15098 /* Value is number of columns displayed. */
15099 return it->hpos - hpos_at_start;
15104 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15105 appears as an element of LIST or as the car of an element of LIST.
15106 If PROPVAL is a list, compare each element against LIST in that
15107 way, and return 1/2 if any element of PROPVAL is found in LIST.
15108 Otherwise return 0. This function cannot quit.
15109 The return value is 2 if the text is invisible but with an ellipsis
15110 and 1 if it's invisible and without an ellipsis. */
15113 invisible_p (propval, list)
15114 register Lisp_Object propval;
15115 Lisp_Object list;
15117 register Lisp_Object tail, proptail;
15119 for (tail = list; CONSP (tail); tail = XCDR (tail))
15121 register Lisp_Object tem;
15122 tem = XCAR (tail);
15123 if (EQ (propval, tem))
15124 return 1;
15125 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15126 return NILP (XCDR (tem)) ? 1 : 2;
15129 if (CONSP (propval))
15131 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15133 Lisp_Object propelt;
15134 propelt = XCAR (proptail);
15135 for (tail = list; CONSP (tail); tail = XCDR (tail))
15137 register Lisp_Object tem;
15138 tem = XCAR (tail);
15139 if (EQ (propelt, tem))
15140 return 1;
15141 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15142 return NILP (XCDR (tem)) ? 1 : 2;
15147 return 0;
15151 /***********************************************************************
15152 Initialization
15153 ***********************************************************************/
15155 void
15156 syms_of_xdisp ()
15158 Vwith_echo_area_save_vector = Qnil;
15159 staticpro (&Vwith_echo_area_save_vector);
15161 Vmessage_stack = Qnil;
15162 staticpro (&Vmessage_stack);
15164 Qinhibit_redisplay = intern ("inhibit-redisplay");
15165 staticpro (&Qinhibit_redisplay);
15167 message_dolog_marker1 = Fmake_marker ();
15168 staticpro (&message_dolog_marker1);
15169 message_dolog_marker2 = Fmake_marker ();
15170 staticpro (&message_dolog_marker2);
15171 message_dolog_marker3 = Fmake_marker ();
15172 staticpro (&message_dolog_marker3);
15174 #if GLYPH_DEBUG
15175 defsubr (&Sdump_frame_glyph_matrix);
15176 defsubr (&Sdump_glyph_matrix);
15177 defsubr (&Sdump_glyph_row);
15178 defsubr (&Sdump_tool_bar_row);
15179 defsubr (&Strace_redisplay);
15180 defsubr (&Strace_to_stderr);
15181 #endif
15182 #ifdef HAVE_WINDOW_SYSTEM
15183 defsubr (&Stool_bar_lines_needed);
15184 #endif
15185 defsubr (&Sformat_mode_line);
15187 staticpro (&Qmenu_bar_update_hook);
15188 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15190 staticpro (&Qoverriding_terminal_local_map);
15191 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15193 staticpro (&Qoverriding_local_map);
15194 Qoverriding_local_map = intern ("overriding-local-map");
15196 staticpro (&Qwindow_scroll_functions);
15197 Qwindow_scroll_functions = intern ("window-scroll-functions");
15199 staticpro (&Qredisplay_end_trigger_functions);
15200 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15202 staticpro (&Qinhibit_point_motion_hooks);
15203 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15205 QCdata = intern (":data");
15206 staticpro (&QCdata);
15207 Qdisplay = intern ("display");
15208 staticpro (&Qdisplay);
15209 Qspace_width = intern ("space-width");
15210 staticpro (&Qspace_width);
15211 Qraise = intern ("raise");
15212 staticpro (&Qraise);
15213 Qspace = intern ("space");
15214 staticpro (&Qspace);
15215 Qmargin = intern ("margin");
15216 staticpro (&Qmargin);
15217 Qleft_margin = intern ("left-margin");
15218 staticpro (&Qleft_margin);
15219 Qright_margin = intern ("right-margin");
15220 staticpro (&Qright_margin);
15221 Qalign_to = intern ("align-to");
15222 staticpro (&Qalign_to);
15223 QCalign_to = intern (":align-to");
15224 staticpro (&QCalign_to);
15225 Qrelative_width = intern ("relative-width");
15226 staticpro (&Qrelative_width);
15227 QCrelative_width = intern (":relative-width");
15228 staticpro (&QCrelative_width);
15229 QCrelative_height = intern (":relative-height");
15230 staticpro (&QCrelative_height);
15231 QCeval = intern (":eval");
15232 staticpro (&QCeval);
15233 QCpropertize = intern (":propertize");
15234 staticpro (&QCpropertize);
15235 Qwhen = intern ("when");
15236 staticpro (&Qwhen);
15237 QCfile = intern (":file");
15238 staticpro (&QCfile);
15239 Qfontified = intern ("fontified");
15240 staticpro (&Qfontified);
15241 Qfontification_functions = intern ("fontification-functions");
15242 staticpro (&Qfontification_functions);
15243 Qtrailing_whitespace = intern ("trailing-whitespace");
15244 staticpro (&Qtrailing_whitespace);
15245 Qimage = intern ("image");
15246 staticpro (&Qimage);
15247 Qmessage_truncate_lines = intern ("message-truncate-lines");
15248 staticpro (&Qmessage_truncate_lines);
15249 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15250 staticpro (&Qcursor_in_non_selected_windows);
15251 Qgrow_only = intern ("grow-only");
15252 staticpro (&Qgrow_only);
15253 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15254 staticpro (&Qinhibit_menubar_update);
15255 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15256 staticpro (&Qinhibit_eval_during_redisplay);
15257 Qposition = intern ("position");
15258 staticpro (&Qposition);
15259 Qbuffer_position = intern ("buffer-position");
15260 staticpro (&Qbuffer_position);
15261 Qobject = intern ("object");
15262 staticpro (&Qobject);
15263 Qrisky_local_variable = intern ("risky-local-variable");
15264 staticpro (&Qrisky_local_variable);
15266 list_of_error = Fcons (intern ("error"), Qnil);
15267 staticpro (&list_of_error);
15269 last_arrow_position = Qnil;
15270 last_arrow_string = Qnil;
15271 staticpro (&last_arrow_position);
15272 staticpro (&last_arrow_string);
15274 echo_buffer[0] = echo_buffer[1] = Qnil;
15275 staticpro (&echo_buffer[0]);
15276 staticpro (&echo_buffer[1]);
15278 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15279 staticpro (&echo_area_buffer[0]);
15280 staticpro (&echo_area_buffer[1]);
15282 Vmessages_buffer_name = build_string ("*Messages*");
15283 staticpro (&Vmessages_buffer_name);
15285 mode_line_proptrans_alist = Qnil;
15286 staticpro (&mode_line_proptrans_alist);
15288 mode_line_string_list = Qnil;
15289 staticpro (&mode_line_string_list);
15291 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15292 doc: /* Non-nil means highlight trailing whitespace.
15293 The face used for trailing whitespace is `trailing-whitespace'. */);
15294 Vshow_trailing_whitespace = Qnil;
15296 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15297 doc: /* Non-nil means don't actually do any redisplay.
15298 This is used for internal purposes. */);
15299 Vinhibit_redisplay = Qnil;
15301 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15302 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15303 Vglobal_mode_string = Qnil;
15305 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15306 doc: /* Marker for where to display an arrow on top of the buffer text.
15307 This must be the beginning of a line in order to work.
15308 See also `overlay-arrow-string'. */);
15309 Voverlay_arrow_position = Qnil;
15311 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15312 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15313 Voverlay_arrow_string = Qnil;
15315 DEFVAR_INT ("scroll-step", &scroll_step,
15316 doc: /* *The number of lines to try scrolling a window by when point moves out.
15317 If that fails to bring point back on frame, point is centered instead.
15318 If this is zero, point is always centered after it moves off frame.
15319 If you want scrolling to always be a line at a time, you should set
15320 `scroll-conservatively' to a large value rather than set this to 1. */);
15322 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15323 doc: /* *Scroll up to this many lines, to bring point back on screen.
15324 A value of zero means to scroll the text to center point vertically
15325 in the window. */);
15326 scroll_conservatively = 0;
15328 DEFVAR_INT ("scroll-margin", &scroll_margin,
15329 doc: /* *Number of lines of margin at the top and bottom of a window.
15330 Recenter the window whenever point gets within this many lines
15331 of the top or bottom of the window. */);
15332 scroll_margin = 0;
15334 #if GLYPH_DEBUG
15335 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15336 #endif
15338 DEFVAR_BOOL ("truncate-partial-width-windows",
15339 &truncate_partial_width_windows,
15340 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15341 truncate_partial_width_windows = 1;
15343 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15344 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15345 Any other value means to use the appropriate face, `mode-line',
15346 `header-line', or `menu' respectively. */);
15347 mode_line_inverse_video = 1;
15349 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15350 doc: /* *Maximum buffer size for which line number should be displayed.
15351 If the buffer is bigger than this, the line number does not appear
15352 in the mode line. A value of nil means no limit. */);
15353 Vline_number_display_limit = Qnil;
15355 DEFVAR_INT ("line-number-display-limit-width",
15356 &line_number_display_limit_width,
15357 doc: /* *Maximum line width (in characters) for line number display.
15358 If the average length of the lines near point is bigger than this, then the
15359 line number may be omitted from the mode line. */);
15360 line_number_display_limit_width = 200;
15362 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15363 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15364 highlight_nonselected_windows = 0;
15366 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15367 doc: /* Non-nil if more than one frame is visible on this display.
15368 Minibuffer-only frames don't count, but iconified frames do.
15369 This variable is not guaranteed to be accurate except while processing
15370 `frame-title-format' and `icon-title-format'. */);
15372 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15373 doc: /* Template for displaying the title bar of visible frames.
15374 \(Assuming the window manager supports this feature.)
15375 This variable has the same structure as `mode-line-format' (which see),
15376 and is used only on frames for which no explicit name has been set
15377 \(see `modify-frame-parameters'). */);
15378 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15379 doc: /* Template for displaying the title bar of an iconified frame.
15380 \(Assuming the window manager supports this feature.)
15381 This variable has the same structure as `mode-line-format' (which see),
15382 and is used only on frames for which no explicit name has been set
15383 \(see `modify-frame-parameters'). */);
15384 Vicon_title_format
15385 = Vframe_title_format
15386 = Fcons (intern ("multiple-frames"),
15387 Fcons (build_string ("%b"),
15388 Fcons (Fcons (empty_string,
15389 Fcons (intern ("invocation-name"),
15390 Fcons (build_string ("@"),
15391 Fcons (intern ("system-name"),
15392 Qnil)))),
15393 Qnil)));
15395 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15396 doc: /* Maximum number of lines to keep in the message log buffer.
15397 If nil, disable message logging. If t, log messages but don't truncate
15398 the buffer when it becomes large. */);
15399 Vmessage_log_max = make_number (50);
15401 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15402 doc: /* Functions called before redisplay, if window sizes have changed.
15403 The value should be a list of functions that take one argument.
15404 Just before redisplay, for each frame, if any of its windows have changed
15405 size since the last redisplay, or have been split or deleted,
15406 all the functions in the list are called, with the frame as argument. */);
15407 Vwindow_size_change_functions = Qnil;
15409 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15410 doc: /* List of Functions to call before redisplaying a window with scrolling.
15411 Each function is called with two arguments, the window
15412 and its new display-start position. Note that the value of `window-end'
15413 is not valid when these functions are called. */);
15414 Vwindow_scroll_functions = Qnil;
15416 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15417 doc: /* *Non-nil means automatically resize tool-bars.
15418 This increases a tool-bar's height if not all tool-bar items are visible.
15419 It decreases a tool-bar's height when it would display blank lines
15420 otherwise. */);
15421 auto_resize_tool_bars_p = 1;
15423 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15424 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15425 auto_raise_tool_bar_buttons_p = 1;
15427 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15428 doc: /* *Margin around tool-bar buttons in pixels.
15429 If an integer, use that for both horizontal and vertical margins.
15430 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15431 HORZ specifying the horizontal margin, and VERT specifying the
15432 vertical margin. */);
15433 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15435 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15436 doc: /* *Relief thickness of tool-bar buttons. */);
15437 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15439 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15440 doc: /* List of functions to call to fontify regions of text.
15441 Each function is called with one argument POS. Functions must
15442 fontify a region starting at POS in the current buffer, and give
15443 fontified regions the property `fontified'. */);
15444 Vfontification_functions = Qnil;
15445 Fmake_variable_buffer_local (Qfontification_functions);
15447 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15448 &unibyte_display_via_language_environment,
15449 doc: /* *Non-nil means display unibyte text according to language environment.
15450 Specifically this means that unibyte non-ASCII characters
15451 are displayed by converting them to the equivalent multibyte characters
15452 according to the current language environment. As a result, they are
15453 displayed according to the current fontset. */);
15454 unibyte_display_via_language_environment = 0;
15456 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15457 doc: /* *Maximum height for resizing mini-windows.
15458 If a float, it specifies a fraction of the mini-window frame's height.
15459 If an integer, it specifies a number of lines. */);
15460 Vmax_mini_window_height = make_float (0.25);
15462 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15463 doc: /* *How to resize mini-windows.
15464 A value of nil means don't automatically resize mini-windows.
15465 A value of t means resize them to fit the text displayed in them.
15466 A value of `grow-only', the default, means let mini-windows grow
15467 only, until their display becomes empty, at which point the windows
15468 go back to their normal size. */);
15469 Vresize_mini_windows = Qgrow_only;
15471 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15472 &cursor_in_non_selected_windows,
15473 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15474 nil means don't display a cursor there. */);
15475 cursor_in_non_selected_windows = 1;
15477 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15478 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15479 automatic_hscrolling_p = 1;
15481 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15482 doc: /* *How many columns away from the window edge point is allowed to get
15483 before automatic hscrolling will horizontally scroll the window. */);
15484 hscroll_margin = 5;
15486 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15487 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15488 When point is less than `automatic-hscroll-margin' columns from the window
15489 edge, automatic hscrolling will scroll the window by the amount of columns
15490 determined by this variable. If its value is a positive integer, scroll that
15491 many columns. If it's a positive floating-point number, it specifies the
15492 fraction of the window's width to scroll. If it's nil or zero, point will be
15493 centered horizontally after the scroll. Any other value, including negative
15494 numbers, are treated as if the value were zero.
15496 Automatic hscrolling always moves point outside the scroll margin, so if
15497 point was more than scroll step columns inside the margin, the window will
15498 scroll more than the value given by the scroll step.
15500 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15501 and `scroll-right' overrides this variable's effect. */);
15502 Vhscroll_step = make_number (0);
15504 DEFVAR_LISP ("image-types", &Vimage_types,
15505 doc: /* List of supported image types.
15506 Each element of the list is a symbol for a supported image type. */);
15507 Vimage_types = Qnil;
15509 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15510 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15511 Bind this around calls to `message' to let it take effect. */);
15512 message_truncate_lines = 0;
15514 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15515 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15516 Can be used to update submenus whose contents should vary. */);
15517 Vmenu_bar_update_hook = Qnil;
15519 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15520 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15521 inhibit_menubar_update = 0;
15523 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15524 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15525 inhibit_eval_during_redisplay = 0;
15527 #if GLYPH_DEBUG
15528 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15529 doc: /* Inhibit try_window_id display optimization. */);
15530 inhibit_try_window_id = 0;
15532 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15533 doc: /* Inhibit try_window_reusing display optimization. */);
15534 inhibit_try_window_reusing = 0;
15536 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15537 doc: /* Inhibit try_cursor_movement display optimization. */);
15538 inhibit_try_cursor_movement = 0;
15539 #endif /* GLYPH_DEBUG */
15543 /* Initialize this module when Emacs starts. */
15545 void
15546 init_xdisp ()
15548 Lisp_Object root_window;
15549 struct window *mini_w;
15551 current_header_line_height = current_mode_line_height = -1;
15553 CHARPOS (this_line_start_pos) = 0;
15555 mini_w = XWINDOW (minibuf_window);
15556 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15558 if (!noninteractive)
15560 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15561 int i;
15563 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15564 set_window_height (root_window,
15565 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15567 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15568 set_window_height (minibuf_window, 1, 0);
15570 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15571 mini_w->width = make_number (FRAME_WIDTH (f));
15573 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15574 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15575 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15577 /* The default ellipsis glyphs `...'. */
15578 for (i = 0; i < 3; ++i)
15579 default_invis_vector[i] = make_number ('.');
15583 /* Allocate the buffer for frame titles.
15584 Also used for `format-mode-line'. */
15585 int size = 100;
15586 frame_title_buf = (char *) xmalloc (size);
15587 frame_title_buf_end = frame_title_buf + size;
15588 frame_title_ptr = NULL;
15591 help_echo_showing_p = 0;