(try_scrolling): Set scroll_max to max of scroll_* args
[emacs.git] / src / xdisp.c
blob60f06b6e0c2d397dd15cde9e58307b5ca783781d
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
204 #define INFINITY 10000000
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
211 extern int interrupt_input;
212 extern int command_loop_level;
214 extern int minibuffer_auto_raise;
216 extern Lisp_Object Qface;
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
230 /* Functions called to fontify regions of text. */
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
238 int auto_raise_tool_bar_buttons_p;
240 /* Margin around tool bar buttons in pixels. */
242 int tool_bar_button_margin;
244 /* Thickness of shadow to draw around tool bar buttons. */
246 int tool_bar_button_relief;
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
251 int auto_resize_tool_bars_p;
253 /* Non-nil means don't actually do any redisplay. */
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257 /* Names of text properties relevant for redisplay. */
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
262 /* Symbols used in text property values. */
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
269 /* Non-nil means highlight trailing whitespace. */
271 Lisp_Object Vshow_trailing_whitespace;
273 /* Name of the face used to highlight trailing whitespace. */
275 Lisp_Object Qtrailing_whitespace;
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
280 Lisp_Object Qimage;
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
285 int noninteractive_need_newline;
287 /* Non-zero means print newline to message log before next message. */
289 static int message_log_need_newline;
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
297 static struct text_pos this_line_start_pos;
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
302 static struct text_pos this_line_end_pos;
304 /* The vertical positions and the height of this line. */
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
313 static int this_line_start_x;
315 /* Buffer that this_line_.* variables are referring to. */
317 static struct buffer *this_line_buffer;
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
322 int truncate_partial_width_windows;
324 /* A flag to control how to display unibyte 8-bit character. */
326 int unibyte_display_via_language_environment;
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
332 int multiple_frames;
334 Lisp_Object Vglobal_mode_string;
336 /* Marker for where to display an arrow on top of the buffer text. */
338 Lisp_Object Voverlay_arrow_position;
340 /* String to display for the arrow. Only used on terminal frames. */
342 Lisp_Object Voverlay_arrow_string;
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
348 static Lisp_Object last_arrow_position, last_arrow_string;
350 /* Like mode-line-format, but for the title bar on a visible frame. */
352 Lisp_Object Vframe_title_format;
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
356 Lisp_Object Vicon_title_format;
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
362 static Lisp_Object Vwindow_size_change_functions;
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
366 /* Nonzero if overlay arrow has been displayed once in this window. */
368 static int overlay_arrow_seen;
370 /* Nonzero means highlight the region even in nonselected windows. */
372 int highlight_nonselected_windows;
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
377 static int scroll_step;
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
382 static int scroll_conservatively;
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
389 int scroll_margin;
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
395 int buffer_shared;
397 /* Vector containing glyphs for an ellipsis `...'. */
399 static Lisp_Object default_invis_vector[3];
401 /* Nonzero means display mode line highlighted. */
403 int mode_line_inverse_video;
405 /* Prompt to display in front of the mini-buffer contents. */
407 Lisp_Object minibuf_prompt;
409 /* Width of current mini-buffer prompt. Only set after display_line
410 of the line that contains the prompt. */
412 int minibuf_prompt_width;
413 int minibuf_prompt_pixel_width;
415 /* This is the window where the echo area message was displayed. It
416 is always a mini-buffer window, but it may not be the same window
417 currently active as a mini-buffer. */
419 Lisp_Object echo_area_window;
421 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
422 pushes the current message and the value of
423 message_enable_multibyte on the stack, the function restore_message
424 pops the stack and displays MESSAGE again. */
426 Lisp_Object Vmessage_stack;
428 /* Nonzero means multibyte characters were enabled when the echo area
429 message was specified. */
431 int message_enable_multibyte;
433 /* True if we should redraw the mode lines on the next redisplay. */
435 int update_mode_lines;
437 /* Nonzero if window sizes or contents have changed since last
438 redisplay that finished */
440 int windows_or_buffers_changed;
442 /* Nonzero after display_mode_line if %l was used and it displayed a
443 line number. */
445 int line_number_displayed;
447 /* Maximum buffer size for which to display line numbers. */
449 Lisp_Object Vline_number_display_limit;
451 /* line width to consider when repostioning for line number display */
453 static int line_number_display_limit_width;
455 /* Number of lines to keep in the message log buffer. t means
456 infinite. nil means don't log at all. */
458 Lisp_Object Vmessage_log_max;
460 /* The name of the *Messages* buffer, a string. */
462 static Lisp_Object Vmessages_buffer_name;
464 /* Current, index 0, and last displayed echo area message. Either
465 buffers from echo_buffers, or nil to indicate no message. */
467 Lisp_Object echo_area_buffer[2];
469 /* The buffers referenced from echo_area_buffer. */
471 static Lisp_Object echo_buffer[2];
473 /* A vector saved used in with_area_buffer to reduce consing. */
475 static Lisp_Object Vwith_echo_area_save_vector;
477 /* Non-zero means display_echo_area should display the last echo area
478 message again. Set by redisplay_preserve_echo_area. */
480 static int display_last_displayed_message_p;
482 /* Nonzero if echo area is being used by print; zero if being used by
483 message. */
485 int message_buf_print;
487 /* Maximum height for resizing mini-windows. Either a float
488 specifying a fraction of the available height, or an integer
489 specifying a number of lines. */
491 Lisp_Object Vmax_mini_window_height;
493 /* Non-zero means messages should be displayed with truncated
494 lines instead of being continued. */
496 int message_truncate_lines;
497 Lisp_Object Qmessage_truncate_lines;
499 /* Non-zero means we want a hollow cursor in windows that are not
500 selected. Zero means there's no cursor in such windows. */
502 int cursor_in_non_selected_windows;
504 /* A scratch glyph row with contents used for generating truncation
505 glyphs. Also used in direct_output_for_insert. */
507 #define MAX_SCRATCH_GLYPHS 100
508 struct glyph_row scratch_glyph_row;
509 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
511 /* Ascent and height of the last line processed by move_it_to. */
513 static int last_max_ascent, last_height;
515 /* Non-zero if there's a help-echo in the echo area. */
517 int help_echo_showing_p;
519 /* If >= 0, computed, exact values of mode-line and header-line height
520 to use in the macros CURRENT_MODE_LINE_HEIGHT and
521 CURRENT_HEADER_LINE_HEIGHT. */
523 int current_mode_line_height, current_header_line_height;
525 /* The maximum distance to look ahead for text properties. Values
526 that are too small let us call compute_char_face and similar
527 functions too often which is expensive. Values that are too large
528 let us call compute_char_face and alike too often because we
529 might not be interested in text properties that far away. */
531 #define TEXT_PROP_DISTANCE_LIMIT 100
533 #if GLYPH_DEBUG
535 /* Non-zero means print traces of redisplay if compiled with
536 GLYPH_DEBUG != 0. */
538 int trace_redisplay_p;
540 #endif /* GLYPH_DEBUG */
542 #ifdef DEBUG_TRACE_MOVE
543 /* Non-zero means trace with TRACE_MOVE to stderr. */
544 int trace_move;
546 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
547 #else
548 #define TRACE_MOVE(x) (void) 0
549 #endif
551 /* Non-zero means automatically scroll windows horizontally to make
552 point visible. */
554 int automatic_hscrolling_p;
556 /* A list of symbols, one for each supported image type. */
558 Lisp_Object Vimage_types;
560 /* The variable `resize-mini-windows'. If nil, don't resize
561 mini-windows. If t, always resize them to fit the text they
562 display. If `grow-only', let mini-windows grow only until they
563 become empty. */
565 Lisp_Object Vresize_mini_windows;
567 /* Value returned from text property handlers (see below). */
569 enum prop_handled
571 HANDLED_NORMALLY,
572 HANDLED_RECOMPUTE_PROPS,
573 HANDLED_OVERLAY_STRING_CONSUMED,
574 HANDLED_RETURN
577 /* A description of text properties that redisplay is interested
578 in. */
580 struct props
582 /* The name of the property. */
583 Lisp_Object *name;
585 /* A unique index for the property. */
586 enum prop_idx idx;
588 /* A handler function called to set up iterator IT from the property
589 at IT's current position. Value is used to steer handle_stop. */
590 enum prop_handled (*handler) P_ ((struct it *it));
593 static enum prop_handled handle_face_prop P_ ((struct it *));
594 static enum prop_handled handle_invisible_prop P_ ((struct it *));
595 static enum prop_handled handle_display_prop P_ ((struct it *));
596 static enum prop_handled handle_composition_prop P_ ((struct it *));
597 static enum prop_handled handle_overlay_change P_ ((struct it *));
598 static enum prop_handled handle_fontified_prop P_ ((struct it *));
600 /* Properties handled by iterators. */
602 static struct props it_props[] =
604 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
605 /* Handle `face' before `display' because some sub-properties of
606 `display' need to know the face. */
607 {&Qface, FACE_PROP_IDX, handle_face_prop},
608 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
609 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
610 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
611 {NULL, 0, NULL}
614 /* Value is the position described by X. If X is a marker, value is
615 the marker_position of X. Otherwise, value is X. */
617 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
619 /* Enumeration returned by some move_it_.* functions internally. */
621 enum move_it_result
623 /* Not used. Undefined value. */
624 MOVE_UNDEFINED,
626 /* Move ended at the requested buffer position or ZV. */
627 MOVE_POS_MATCH_OR_ZV,
629 /* Move ended at the requested X pixel position. */
630 MOVE_X_REACHED,
632 /* Move within a line ended at the end of a line that must be
633 continued. */
634 MOVE_LINE_CONTINUED,
636 /* Move within a line ended at the end of a line that would
637 be displayed truncated. */
638 MOVE_LINE_TRUNCATED,
640 /* Move within a line ended at a line end. */
641 MOVE_NEWLINE_OR_CR
646 /* Function prototypes. */
648 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
649 static int redisplay_mode_lines P_ ((Lisp_Object, int));
650 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
651 static int invisible_text_between_p P_ ((struct it *, int, int));
652 static int next_element_from_ellipsis P_ ((struct it *));
653 static void pint2str P_ ((char *, int, int));
654 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
655 struct text_pos));
656 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
657 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
658 static void store_frame_title_char P_ ((char));
659 static int store_frame_title P_ ((unsigned char *, int, int));
660 static void x_consider_frame_title P_ ((Lisp_Object));
661 static void handle_stop P_ ((struct it *));
662 static int tool_bar_lines_needed P_ ((struct frame *));
663 static int single_display_prop_intangible_p P_ ((Lisp_Object));
664 static void ensure_echo_area_buffers P_ ((void));
665 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
666 struct glyph_row *,
667 struct glyph_row *));
668 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
669 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
670 static int with_echo_area_buffer P_ ((struct window *, int,
671 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
672 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
673 static void clear_garbaged_frames P_ ((void));
674 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
675 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
676 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
677 static int display_echo_area P_ ((struct window *));
678 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
679 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
680 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
681 static int string_char_and_length P_ ((unsigned char *, int, int *));
682 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
683 struct text_pos));
684 static int compute_window_start_on_continuation_line P_ ((struct window *));
685 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
686 static void insert_left_trunc_glyphs P_ ((struct it *));
687 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
688 static void extend_face_to_end_of_line P_ ((struct it *));
689 static int append_space P_ ((struct it *, int));
690 static void make_cursor_line_fully_visible P_ ((struct window *));
691 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
692 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
693 static int trailing_whitespace_p P_ ((int));
694 static int message_log_check_duplicate P_ ((int, int, int, int));
695 int invisible_p P_ ((Lisp_Object, Lisp_Object));
696 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
697 static void push_it P_ ((struct it *));
698 static void pop_it P_ ((struct it *));
699 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
700 static void redisplay_internal P_ ((int));
701 static int echo_area_display P_ ((int));
702 static void redisplay_windows P_ ((Lisp_Object));
703 static void redisplay_window P_ ((Lisp_Object, int));
704 static void update_menu_bar P_ ((struct frame *, int));
705 static int try_window_reusing_current_matrix P_ ((struct window *));
706 static int try_window_id P_ ((struct window *));
707 static int display_line P_ ((struct it *));
708 static int display_mode_lines P_ ((struct window *));
709 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
710 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
711 static char *decode_mode_spec P_ ((struct window *, int, int, int));
712 static void display_menu_bar P_ ((struct window *));
713 static int display_count_lines P_ ((int, int, int, int, int *));
714 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
715 int, int, struct it *, int, int, int, int));
716 static void compute_line_metrics P_ ((struct it *));
717 static void run_redisplay_end_trigger_hook P_ ((struct it *));
718 static int get_overlay_strings P_ ((struct it *));
719 static void next_overlay_string P_ ((struct it *));
720 static void reseat P_ ((struct it *, struct text_pos, int));
721 static void reseat_1 P_ ((struct it *, struct text_pos, int));
722 static void back_to_previous_visible_line_start P_ ((struct it *));
723 static void reseat_at_previous_visible_line_start P_ ((struct it *));
724 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
725 static int next_element_from_display_vector P_ ((struct it *));
726 static int next_element_from_string P_ ((struct it *));
727 static int next_element_from_c_string P_ ((struct it *));
728 static int next_element_from_buffer P_ ((struct it *));
729 static int next_element_from_composition P_ ((struct it *));
730 static int next_element_from_image P_ ((struct it *));
731 static int next_element_from_stretch P_ ((struct it *));
732 static void load_overlay_strings P_ ((struct it *));
733 static void init_from_display_pos P_ ((struct it *, struct window *,
734 struct display_pos *));
735 static void reseat_to_string P_ ((struct it *, unsigned char *,
736 Lisp_Object, int, int, int, int));
737 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
738 int, int, int));
739 void move_it_vertically_backward P_ ((struct it *, int));
740 static void init_to_row_start P_ ((struct it *, struct window *,
741 struct glyph_row *));
742 static void init_to_row_end P_ ((struct it *, struct window *,
743 struct glyph_row *));
744 static void back_to_previous_line_start P_ ((struct it *));
745 static int forward_to_next_line_start P_ ((struct it *, int *));
746 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
747 Lisp_Object, int));
748 static struct text_pos string_pos P_ ((int, Lisp_Object));
749 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
750 static int number_of_chars P_ ((unsigned char *, int));
751 static void compute_stop_pos P_ ((struct it *));
752 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
753 Lisp_Object));
754 static int face_before_or_after_it_pos P_ ((struct it *, int));
755 static int next_overlay_change P_ ((int));
756 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
757 Lisp_Object, struct text_pos *));
759 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
760 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
762 #ifdef HAVE_WINDOW_SYSTEM
764 static void update_tool_bar P_ ((struct frame *, int));
765 static void build_desired_tool_bar_string P_ ((struct frame *f));
766 static int redisplay_tool_bar P_ ((struct frame *));
767 static void display_tool_bar_line P_ ((struct it *));
769 #endif /* HAVE_WINDOW_SYSTEM */
772 /***********************************************************************
773 Window display dimensions
774 ***********************************************************************/
776 /* Return the window-relative maximum y + 1 for glyph rows displaying
777 text in window W. This is the height of W minus the height of a
778 mode line, if any. */
780 INLINE int
781 window_text_bottom_y (w)
782 struct window *w;
784 struct frame *f = XFRAME (w->frame);
785 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
787 if (WINDOW_WANTS_MODELINE_P (w))
788 height -= CURRENT_MODE_LINE_HEIGHT (w);
789 return height;
793 /* Return the pixel width of display area AREA of window W. AREA < 0
794 means return the total width of W, not including bitmap areas to
795 the left and right of the window. */
797 INLINE int
798 window_box_width (w, area)
799 struct window *w;
800 int area;
802 struct frame *f = XFRAME (w->frame);
803 int width = XFASTINT (w->width);
805 if (!w->pseudo_window_p)
807 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
809 if (area == TEXT_AREA)
811 if (INTEGERP (w->left_margin_width))
812 width -= XFASTINT (w->left_margin_width);
813 if (INTEGERP (w->right_margin_width))
814 width -= XFASTINT (w->right_margin_width);
816 else if (area == LEFT_MARGIN_AREA)
817 width = (INTEGERP (w->left_margin_width)
818 ? XFASTINT (w->left_margin_width) : 0);
819 else if (area == RIGHT_MARGIN_AREA)
820 width = (INTEGERP (w->right_margin_width)
821 ? XFASTINT (w->right_margin_width) : 0);
824 return width * CANON_X_UNIT (f);
828 /* Return the pixel height of the display area of window W, not
829 including mode lines of W, if any.. */
831 INLINE int
832 window_box_height (w)
833 struct window *w;
835 struct frame *f = XFRAME (w->frame);
836 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
838 xassert (height >= 0);
840 if (WINDOW_WANTS_MODELINE_P (w))
841 height -= CURRENT_MODE_LINE_HEIGHT (w);
843 if (WINDOW_WANTS_HEADER_LINE_P (w))
844 height -= CURRENT_HEADER_LINE_HEIGHT (w);
846 return height;
850 /* Return the frame-relative coordinate of the left edge of display
851 area AREA of window W. AREA < 0 means return the left edge of the
852 whole window, to the right of any bitmap area at the left side of
853 W. */
855 INLINE int
856 window_box_left (w, area)
857 struct window *w;
858 int area;
860 struct frame *f = XFRAME (w->frame);
861 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
863 if (!w->pseudo_window_p)
865 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
866 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
868 if (area == TEXT_AREA)
869 x += window_box_width (w, LEFT_MARGIN_AREA);
870 else if (area == RIGHT_MARGIN_AREA)
871 x += (window_box_width (w, LEFT_MARGIN_AREA)
872 + window_box_width (w, TEXT_AREA));
875 return x;
879 /* Return the frame-relative coordinate of the right edge of display
880 area AREA of window W. AREA < 0 means return the left edge of the
881 whole window, to the left of any bitmap area at the right side of
882 W. */
884 INLINE int
885 window_box_right (w, area)
886 struct window *w;
887 int area;
889 return window_box_left (w, area) + window_box_width (w, area);
893 /* Get the bounding box of the display area AREA of window W, without
894 mode lines, in frame-relative coordinates. AREA < 0 means the
895 whole window, not including bitmap areas to the left and right of
896 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
897 coordinates of the upper-left corner of the box. Return in
898 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
900 INLINE void
901 window_box (w, area, box_x, box_y, box_width, box_height)
902 struct window *w;
903 int area;
904 int *box_x, *box_y, *box_width, *box_height;
906 struct frame *f = XFRAME (w->frame);
908 *box_width = window_box_width (w, area);
909 *box_height = window_box_height (w);
910 *box_x = window_box_left (w, area);
911 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
912 + XFASTINT (w->top) * CANON_Y_UNIT (f));
913 if (WINDOW_WANTS_HEADER_LINE_P (w))
914 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
918 /* Get the bounding box of the display area AREA of window W, without
919 mode lines. AREA < 0 means the whole window, not including bitmap
920 areas to the left and right of the window. Return in *TOP_LEFT_X
921 and TOP_LEFT_Y the frame-relative pixel coordinates of the
922 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
923 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
924 box. */
926 INLINE void
927 window_box_edges (w, area, top_left_x, top_left_y,
928 bottom_right_x, bottom_right_y)
929 struct window *w;
930 int area;
931 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
933 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
934 bottom_right_y);
935 *bottom_right_x += *top_left_x;
936 *bottom_right_y += *top_left_y;
941 /***********************************************************************
942 Utilities
943 ***********************************************************************/
945 /* Return 1 if position CHARPOS is visible in window W.
946 Set *FULLY to 1 if POS is visible and the line containing
947 POS is fully visible. */
950 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
951 struct window *w;
952 int charpos, *fully, exact_mode_line_heights_p;
954 struct it it;
955 struct text_pos top;
956 int visible_p;
957 struct buffer *old_buffer = NULL;
959 if (XBUFFER (w->buffer) != current_buffer)
961 old_buffer = current_buffer;
962 set_buffer_internal_1 (XBUFFER (w->buffer));
965 *fully = visible_p = 0;
966 SET_TEXT_POS_FROM_MARKER (top, w->start);
968 /* Compute exact mode line heights, if requested. */
969 if (exact_mode_line_heights_p)
971 if (WINDOW_WANTS_MODELINE_P (w))
972 current_mode_line_height
973 = display_mode_line (w, MODE_LINE_FACE_ID,
974 current_buffer->mode_line_format);
976 if (WINDOW_WANTS_HEADER_LINE_P (w))
977 current_header_line_height
978 = display_mode_line (w, HEADER_LINE_FACE_ID,
979 current_buffer->header_line_format);
982 start_display (&it, w, top);
983 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
984 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
986 if (IT_CHARPOS (it) == charpos)
988 int line_height, line_bottom_y;
989 int line_top_y = it.current_y;
990 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
992 line_height = it.max_ascent + it.max_descent;
993 if (line_height == 0)
995 if (last_height)
996 line_height = last_height;
997 else
999 move_it_by_lines (&it, 1, 1);
1000 line_height = (it.max_ascent || it.max_descent
1001 ? it.max_ascent + it.max_descent
1002 : last_height);
1005 line_bottom_y = line_top_y + line_height;
1007 if (line_top_y < window_top_y)
1008 visible_p = line_bottom_y > window_top_y;
1009 else if (line_top_y < it.last_visible_y)
1011 visible_p = 1;
1012 *fully = line_bottom_y <= it.last_visible_y;
1015 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1017 move_it_by_lines (&it, 1, 0);
1018 if (charpos < IT_CHARPOS (it))
1020 visible_p = 1;
1021 *fully = 0;
1025 if (old_buffer)
1026 set_buffer_internal_1 (old_buffer);
1028 current_header_line_height = current_mode_line_height = -1;
1029 return visible_p;
1033 /* Return the next character from STR which is MAXLEN bytes long.
1034 Return in *LEN the length of the character. This is like
1035 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1036 we find one, we return a `?', but with the length of the invalid
1037 character. */
1039 static INLINE int
1040 string_char_and_length (str, maxlen, len)
1041 unsigned char *str;
1042 int maxlen, *len;
1044 int c;
1046 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1047 if (!CHAR_VALID_P (c, 1))
1048 /* We may not change the length here because other places in Emacs
1049 don't use this function, i.e. they silently accept invalid
1050 characters. */
1051 c = '?';
1053 return c;
1058 /* Given a position POS containing a valid character and byte position
1059 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1061 static struct text_pos
1062 string_pos_nchars_ahead (pos, string, nchars)
1063 struct text_pos pos;
1064 Lisp_Object string;
1065 int nchars;
1067 xassert (STRINGP (string) && nchars >= 0);
1069 if (STRING_MULTIBYTE (string))
1071 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1072 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1073 int len;
1075 while (nchars--)
1077 string_char_and_length (p, rest, &len);
1078 p += len, rest -= len;
1079 xassert (rest >= 0);
1080 CHARPOS (pos) += 1;
1081 BYTEPOS (pos) += len;
1084 else
1085 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1087 return pos;
1091 /* Value is the text position, i.e. character and byte position,
1092 for character position CHARPOS in STRING. */
1094 static INLINE struct text_pos
1095 string_pos (charpos, string)
1096 int charpos;
1097 Lisp_Object string;
1099 struct text_pos pos;
1100 xassert (STRINGP (string));
1101 xassert (charpos >= 0);
1102 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1103 return pos;
1107 /* Value is a text position, i.e. character and byte position, for
1108 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1109 means recognize multibyte characters. */
1111 static struct text_pos
1112 c_string_pos (charpos, s, multibyte_p)
1113 int charpos;
1114 unsigned char *s;
1115 int multibyte_p;
1117 struct text_pos pos;
1119 xassert (s != NULL);
1120 xassert (charpos >= 0);
1122 if (multibyte_p)
1124 int rest = strlen (s), len;
1126 SET_TEXT_POS (pos, 0, 0);
1127 while (charpos--)
1129 string_char_and_length (s, rest, &len);
1130 s += len, rest -= len;
1131 xassert (rest >= 0);
1132 CHARPOS (pos) += 1;
1133 BYTEPOS (pos) += len;
1136 else
1137 SET_TEXT_POS (pos, charpos, charpos);
1139 return pos;
1143 /* Value is the number of characters in C string S. MULTIBYTE_P
1144 non-zero means recognize multibyte characters. */
1146 static int
1147 number_of_chars (s, multibyte_p)
1148 unsigned char *s;
1149 int multibyte_p;
1151 int nchars;
1153 if (multibyte_p)
1155 int rest = strlen (s), len;
1156 unsigned char *p = (unsigned char *) s;
1158 for (nchars = 0; rest > 0; ++nchars)
1160 string_char_and_length (p, rest, &len);
1161 rest -= len, p += len;
1164 else
1165 nchars = strlen (s);
1167 return nchars;
1171 /* Compute byte position NEWPOS->bytepos corresponding to
1172 NEWPOS->charpos. POS is a known position in string STRING.
1173 NEWPOS->charpos must be >= POS.charpos. */
1175 static void
1176 compute_string_pos (newpos, pos, string)
1177 struct text_pos *newpos, pos;
1178 Lisp_Object string;
1180 xassert (STRINGP (string));
1181 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1183 if (STRING_MULTIBYTE (string))
1184 *newpos = string_pos_nchars_ahead (pos, string,
1185 CHARPOS (*newpos) - CHARPOS (pos));
1186 else
1187 BYTEPOS (*newpos) = CHARPOS (*newpos);
1192 /***********************************************************************
1193 Lisp form evaluation
1194 ***********************************************************************/
1196 /* Error handler for safe_eval and safe_call. */
1198 static Lisp_Object
1199 safe_eval_handler (arg)
1200 Lisp_Object arg;
1202 add_to_log ("Error during redisplay: %s", arg, Qnil);
1203 return Qnil;
1207 /* Evaluate SEXPR and return the result, or nil if something went
1208 wrong. */
1210 Lisp_Object
1211 safe_eval (sexpr)
1212 Lisp_Object sexpr;
1214 int count = specpdl_ptr - specpdl;
1215 struct gcpro gcpro1;
1216 Lisp_Object val;
1218 GCPRO1 (sexpr);
1219 specbind (Qinhibit_redisplay, Qt);
1220 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1221 UNGCPRO;
1222 return unbind_to (count, val);
1226 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1227 Return the result, or nil if something went wrong. */
1229 Lisp_Object
1230 safe_call (nargs, args)
1231 int nargs;
1232 Lisp_Object *args;
1234 int count = specpdl_ptr - specpdl;
1235 Lisp_Object val;
1236 struct gcpro gcpro1;
1238 GCPRO1 (args[0]);
1239 gcpro1.nvars = nargs;
1240 specbind (Qinhibit_redisplay, Qt);
1241 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1242 safe_eval_handler);
1243 UNGCPRO;
1244 return unbind_to (count, val);
1248 /* Call function FN with one argument ARG.
1249 Return the result, or nil if something went wrong. */
1251 Lisp_Object
1252 safe_call1 (fn, arg)
1253 Lisp_Object fn, arg;
1255 Lisp_Object args[2];
1256 args[0] = fn;
1257 args[1] = arg;
1258 return safe_call (2, args);
1263 /***********************************************************************
1264 Debugging
1265 ***********************************************************************/
1267 #if 0
1269 /* Define CHECK_IT to perform sanity checks on iterators.
1270 This is for debugging. It is too slow to do unconditionally. */
1272 static void
1273 check_it (it)
1274 struct it *it;
1276 if (it->method == next_element_from_string)
1278 xassert (STRINGP (it->string));
1279 xassert (IT_STRING_CHARPOS (*it) >= 0);
1281 else if (it->method == next_element_from_buffer)
1283 /* Check that character and byte positions agree. */
1284 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1287 if (it->dpvec)
1288 xassert (it->current.dpvec_index >= 0);
1289 else
1290 xassert (it->current.dpvec_index < 0);
1293 #define CHECK_IT(IT) check_it ((IT))
1295 #else /* not 0 */
1297 #define CHECK_IT(IT) (void) 0
1299 #endif /* not 0 */
1302 #if GLYPH_DEBUG
1304 /* Check that the window end of window W is what we expect it
1305 to be---the last row in the current matrix displaying text. */
1307 static void
1308 check_window_end (w)
1309 struct window *w;
1311 if (!MINI_WINDOW_P (w)
1312 && !NILP (w->window_end_valid))
1314 struct glyph_row *row;
1315 xassert ((row = MATRIX_ROW (w->current_matrix,
1316 XFASTINT (w->window_end_vpos)),
1317 !row->enabled_p
1318 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1319 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1323 #define CHECK_WINDOW_END(W) check_window_end ((W))
1325 #else /* not GLYPH_DEBUG */
1327 #define CHECK_WINDOW_END(W) (void) 0
1329 #endif /* not GLYPH_DEBUG */
1333 /***********************************************************************
1334 Iterator initialization
1335 ***********************************************************************/
1337 /* Initialize IT for displaying current_buffer in window W, starting
1338 at character position CHARPOS. CHARPOS < 0 means that no buffer
1339 position is specified which is useful when the iterator is assigned
1340 a position later. BYTEPOS is the byte position corresponding to
1341 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1343 If ROW is not null, calls to produce_glyphs with IT as parameter
1344 will produce glyphs in that row.
1346 BASE_FACE_ID is the id of a base face to use. It must be one of
1347 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1348 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1349 displaying the tool-bar.
1351 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1352 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1353 corresponding mode line glyph row of the desired matrix of W. */
1355 void
1356 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1357 struct it *it;
1358 struct window *w;
1359 int charpos, bytepos;
1360 struct glyph_row *row;
1361 enum face_id base_face_id;
1363 int highlight_region_p;
1365 /* Some precondition checks. */
1366 xassert (w != NULL && it != NULL);
1367 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1369 /* If face attributes have been changed since the last redisplay,
1370 free realized faces now because they depend on face definitions
1371 that might have changed. */
1372 if (face_change_count)
1374 face_change_count = 0;
1375 free_all_realized_faces (Qnil);
1378 /* Use one of the mode line rows of W's desired matrix if
1379 appropriate. */
1380 if (row == NULL)
1382 if (base_face_id == MODE_LINE_FACE_ID)
1383 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1384 else if (base_face_id == HEADER_LINE_FACE_ID)
1385 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1388 /* Clear IT. */
1389 bzero (it, sizeof *it);
1390 it->current.overlay_string_index = -1;
1391 it->current.dpvec_index = -1;
1392 it->base_face_id = base_face_id;
1394 /* The window in which we iterate over current_buffer: */
1395 XSETWINDOW (it->window, w);
1396 it->w = w;
1397 it->f = XFRAME (w->frame);
1399 /* Extra space between lines (on window systems only). */
1400 if (base_face_id == DEFAULT_FACE_ID
1401 && FRAME_WINDOW_P (it->f))
1403 if (NATNUMP (current_buffer->extra_line_spacing))
1404 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1405 else if (it->f->extra_line_spacing > 0)
1406 it->extra_line_spacing = it->f->extra_line_spacing;
1409 /* If realized faces have been removed, e.g. because of face
1410 attribute changes of named faces, recompute them. */
1411 if (FRAME_FACE_CACHE (it->f)->used == 0)
1412 recompute_basic_faces (it->f);
1414 /* Current value of the `space-width', and 'height' properties. */
1415 it->space_width = Qnil;
1416 it->font_height = Qnil;
1418 /* Are control characters displayed as `^C'? */
1419 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1421 /* -1 means everything between a CR and the following line end
1422 is invisible. >0 means lines indented more than this value are
1423 invisible. */
1424 it->selective = (INTEGERP (current_buffer->selective_display)
1425 ? XFASTINT (current_buffer->selective_display)
1426 : (!NILP (current_buffer->selective_display)
1427 ? -1 : 0));
1428 it->selective_display_ellipsis_p
1429 = !NILP (current_buffer->selective_display_ellipses);
1431 /* Display table to use. */
1432 it->dp = window_display_table (w);
1434 /* Are multibyte characters enabled in current_buffer? */
1435 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1437 /* Non-zero if we should highlight the region. */
1438 highlight_region_p
1439 = (!NILP (Vtransient_mark_mode)
1440 && !NILP (current_buffer->mark_active)
1441 && XMARKER (current_buffer->mark)->buffer != 0);
1443 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1444 start and end of a visible region in window IT->w. Set both to
1445 -1 to indicate no region. */
1446 if (highlight_region_p
1447 /* Maybe highlight only in selected window. */
1448 && (/* Either show region everywhere. */
1449 highlight_nonselected_windows
1450 /* Or show region in the selected window. */
1451 || w == XWINDOW (selected_window)
1452 /* Or show the region if we are in the mini-buffer and W is
1453 the window the mini-buffer refers to. */
1454 || (MINI_WINDOW_P (XWINDOW (selected_window))
1455 && w == XWINDOW (Vminibuf_scroll_window))))
1457 int charpos = marker_position (current_buffer->mark);
1458 it->region_beg_charpos = min (PT, charpos);
1459 it->region_end_charpos = max (PT, charpos);
1461 else
1462 it->region_beg_charpos = it->region_end_charpos = -1;
1464 /* Get the position at which the redisplay_end_trigger hook should
1465 be run, if it is to be run at all. */
1466 if (MARKERP (w->redisplay_end_trigger)
1467 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1468 it->redisplay_end_trigger_charpos
1469 = marker_position (w->redisplay_end_trigger);
1470 else if (INTEGERP (w->redisplay_end_trigger))
1471 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1473 /* Correct bogus values of tab_width. */
1474 it->tab_width = XINT (current_buffer->tab_width);
1475 if (it->tab_width <= 0 || it->tab_width > 1000)
1476 it->tab_width = 8;
1478 /* Are lines in the display truncated? */
1479 it->truncate_lines_p
1480 = (base_face_id != DEFAULT_FACE_ID
1481 || XINT (it->w->hscroll)
1482 || (truncate_partial_width_windows
1483 && !WINDOW_FULL_WIDTH_P (it->w))
1484 || !NILP (current_buffer->truncate_lines));
1486 /* Get dimensions of truncation and continuation glyphs. These are
1487 displayed as bitmaps under X, so we don't need them for such
1488 frames. */
1489 if (!FRAME_WINDOW_P (it->f))
1491 if (it->truncate_lines_p)
1493 /* We will need the truncation glyph. */
1494 xassert (it->glyph_row == NULL);
1495 produce_special_glyphs (it, IT_TRUNCATION);
1496 it->truncation_pixel_width = it->pixel_width;
1498 else
1500 /* We will need the continuation glyph. */
1501 xassert (it->glyph_row == NULL);
1502 produce_special_glyphs (it, IT_CONTINUATION);
1503 it->continuation_pixel_width = it->pixel_width;
1506 /* Reset these values to zero becaue the produce_special_glyphs
1507 above has changed them. */
1508 it->pixel_width = it->ascent = it->descent = 0;
1509 it->phys_ascent = it->phys_descent = 0;
1512 /* Set this after getting the dimensions of truncation and
1513 continuation glyphs, so that we don't produce glyphs when calling
1514 produce_special_glyphs, above. */
1515 it->glyph_row = row;
1516 it->area = TEXT_AREA;
1518 /* Get the dimensions of the display area. The display area
1519 consists of the visible window area plus a horizontally scrolled
1520 part to the left of the window. All x-values are relative to the
1521 start of this total display area. */
1522 if (base_face_id != DEFAULT_FACE_ID)
1524 /* Mode lines, menu bar in terminal frames. */
1525 it->first_visible_x = 0;
1526 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1528 else
1530 it->first_visible_x
1531 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1532 it->last_visible_x = (it->first_visible_x
1533 + window_box_width (w, TEXT_AREA));
1535 /* If we truncate lines, leave room for the truncator glyph(s) at
1536 the right margin. Otherwise, leave room for the continuation
1537 glyph(s). Truncation and continuation glyphs are not inserted
1538 for window-based redisplay. */
1539 if (!FRAME_WINDOW_P (it->f))
1541 if (it->truncate_lines_p)
1542 it->last_visible_x -= it->truncation_pixel_width;
1543 else
1544 it->last_visible_x -= it->continuation_pixel_width;
1547 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1548 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1551 /* Leave room for a border glyph. */
1552 if (!FRAME_WINDOW_P (it->f)
1553 && !WINDOW_RIGHTMOST_P (it->w))
1554 it->last_visible_x -= 1;
1556 it->last_visible_y = window_text_bottom_y (w);
1558 /* For mode lines and alike, arrange for the first glyph having a
1559 left box line if the face specifies a box. */
1560 if (base_face_id != DEFAULT_FACE_ID)
1562 struct face *face;
1564 it->face_id = base_face_id;
1566 /* If we have a boxed mode line, make the first character appear
1567 with a left box line. */
1568 face = FACE_FROM_ID (it->f, base_face_id);
1569 if (face->box != FACE_NO_BOX)
1570 it->start_of_box_run_p = 1;
1573 /* If a buffer position was specified, set the iterator there,
1574 getting overlays and face properties from that position. */
1575 if (charpos > 0)
1577 it->end_charpos = ZV;
1578 it->face_id = -1;
1579 IT_CHARPOS (*it) = charpos;
1581 /* Compute byte position if not specified. */
1582 if (bytepos <= 0)
1583 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1584 else
1585 IT_BYTEPOS (*it) = bytepos;
1587 /* Compute faces etc. */
1588 reseat (it, it->current.pos, 1);
1591 CHECK_IT (it);
1595 /* Initialize IT for the display of window W with window start POS. */
1597 void
1598 start_display (it, w, pos)
1599 struct it *it;
1600 struct window *w;
1601 struct text_pos pos;
1603 int start_at_line_beg_p;
1604 struct glyph_row *row;
1605 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1606 int first_y;
1608 row = w->desired_matrix->rows + first_vpos;
1609 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1610 first_y = it->current_y;
1612 /* If window start is not at a line start, move back to the line
1613 start. This makes sure that we take continuation lines into
1614 account. */
1615 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1616 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1617 if (!start_at_line_beg_p)
1618 reseat_at_previous_visible_line_start (it);
1620 /* If window start is not at a line start, skip forward to POS to
1621 get the correct continuation_lines_width and current_x. */
1622 if (!start_at_line_beg_p)
1624 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1626 /* If lines are continued, this line may end in the middle of a
1627 multi-glyph character (e.g. a control character displayed as
1628 \003, or in the middle of an overlay string). In this case
1629 move_it_to above will not have taken us to the start of
1630 the continuation line but to the end of the continued line. */
1631 if (!it->truncate_lines_p)
1633 if (it->current_x > 0)
1635 if (it->current.dpvec_index >= 0
1636 || it->current.overlay_string_index >= 0)
1638 set_iterator_to_next (it, 1);
1639 move_it_in_display_line_to (it, -1, -1, 0);
1642 it->continuation_lines_width += it->current_x;
1645 /* We're starting a new display line, not affected by the
1646 height of the continued line, so clear the appropriate
1647 fields in the iterator structure. */
1648 it->max_ascent = it->max_descent = 0;
1649 it->max_phys_ascent = it->max_phys_descent = 0;
1652 it->current_y = first_y;
1653 it->vpos = 0;
1654 it->current_x = it->hpos = 0;
1657 #if 0 /* Don't assert the following because start_display is sometimes
1658 called intentionally with a window start that is not at a
1659 line start. Please leave this code in as a comment. */
1661 /* Window start should be on a line start, now. */
1662 xassert (it->continuation_lines_width
1663 || IT_CHARPOS (it) == BEGV
1664 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1665 #endif /* 0 */
1669 /* Initialize IT for stepping through current_buffer in window W,
1670 starting at position POS that includes overlay string and display
1671 vector/ control character translation position information. */
1673 static void
1674 init_from_display_pos (it, w, pos)
1675 struct it *it;
1676 struct window *w;
1677 struct display_pos *pos;
1679 /* Keep in mind: the call to reseat in init_iterator skips invisible
1680 text, so we might end up at a position different from POS. This
1681 is only a problem when POS is a row start after a newline and an
1682 overlay starts there with an after-string, and the overlay has an
1683 invisible property. Since we don't skip invisible text in
1684 display_line and elsewhere immediately after consuming the
1685 newline before the row start, such a POS will not be in a string,
1686 but the call to init_iterator below will move us to the
1687 after-string. */
1688 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1689 NULL, DEFAULT_FACE_ID);
1691 /* If position is within an overlay string, set up IT to
1692 the right overlay string. */
1693 if (pos->overlay_string_index >= 0)
1695 int relative_index;
1697 /* We already have the first chunk of overlay strings in
1698 IT->overlay_strings. Load more until the one for
1699 pos->overlay_string_index is in IT->overlay_strings. */
1700 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1702 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1703 it->current.overlay_string_index = 0;
1704 while (n--)
1706 load_overlay_strings (it);
1707 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1711 it->current.overlay_string_index = pos->overlay_string_index;
1712 relative_index = (it->current.overlay_string_index
1713 % OVERLAY_STRING_CHUNK_SIZE);
1714 it->string = it->overlay_strings[relative_index];
1715 xassert (STRINGP (it->string));
1716 it->current.string_pos = pos->string_pos;
1717 it->method = next_element_from_string;
1719 else if (CHARPOS (pos->string_pos) >= 0)
1721 /* Recorded position is not in an overlay string, but in another
1722 string. This can only be a string from a `display' property.
1723 IT should already be filled with that string. */
1724 it->current.string_pos = pos->string_pos;
1725 xassert (STRINGP (it->string));
1728 /* Restore position in display vector translations or control
1729 character translations. */
1730 if (pos->dpvec_index >= 0)
1732 /* This fills IT->dpvec. */
1733 get_next_display_element (it);
1734 xassert (it->dpvec && it->current.dpvec_index == 0);
1735 it->current.dpvec_index = pos->dpvec_index;
1738 CHECK_IT (it);
1742 /* Initialize IT for stepping through current_buffer in window W
1743 starting at ROW->start. */
1745 static void
1746 init_to_row_start (it, w, row)
1747 struct it *it;
1748 struct window *w;
1749 struct glyph_row *row;
1751 init_from_display_pos (it, w, &row->start);
1752 it->continuation_lines_width = row->continuation_lines_width;
1753 CHECK_IT (it);
1757 /* Initialize IT for stepping through current_buffer in window W
1758 starting in the line following ROW, i.e. starting at ROW->end. */
1760 static void
1761 init_to_row_end (it, w, row)
1762 struct it *it;
1763 struct window *w;
1764 struct glyph_row *row;
1766 init_from_display_pos (it, w, &row->end);
1768 if (row->continued_p)
1769 it->continuation_lines_width = (row->continuation_lines_width
1770 + row->pixel_width);
1771 CHECK_IT (it);
1777 /***********************************************************************
1778 Text properties
1779 ***********************************************************************/
1781 /* Called when IT reaches IT->stop_charpos. Handle text property and
1782 overlay changes. Set IT->stop_charpos to the next position where
1783 to stop. */
1785 static void
1786 handle_stop (it)
1787 struct it *it;
1789 enum prop_handled handled;
1790 int handle_overlay_change_p = 1;
1791 struct props *p;
1793 it->dpvec = NULL;
1794 it->current.dpvec_index = -1;
1798 handled = HANDLED_NORMALLY;
1800 /* Call text property handlers. */
1801 for (p = it_props; p->handler; ++p)
1803 handled = p->handler (it);
1805 if (handled == HANDLED_RECOMPUTE_PROPS)
1806 break;
1807 else if (handled == HANDLED_RETURN)
1808 return;
1809 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1810 handle_overlay_change_p = 0;
1813 if (handled != HANDLED_RECOMPUTE_PROPS)
1815 /* Don't check for overlay strings below when set to deliver
1816 characters from a display vector. */
1817 if (it->method == next_element_from_display_vector)
1818 handle_overlay_change_p = 0;
1820 /* Handle overlay changes. */
1821 if (handle_overlay_change_p)
1822 handled = handle_overlay_change (it);
1824 /* Determine where to stop next. */
1825 if (handled == HANDLED_NORMALLY)
1826 compute_stop_pos (it);
1829 while (handled == HANDLED_RECOMPUTE_PROPS);
1833 /* Compute IT->stop_charpos from text property and overlay change
1834 information for IT's current position. */
1836 static void
1837 compute_stop_pos (it)
1838 struct it *it;
1840 register INTERVAL iv, next_iv;
1841 Lisp_Object object, limit, position;
1843 /* If nowhere else, stop at the end. */
1844 it->stop_charpos = it->end_charpos;
1846 if (STRINGP (it->string))
1848 /* Strings are usually short, so don't limit the search for
1849 properties. */
1850 object = it->string;
1851 limit = Qnil;
1852 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1854 else
1856 int charpos;
1858 /* If next overlay change is in front of the current stop pos
1859 (which is IT->end_charpos), stop there. Note: value of
1860 next_overlay_change is point-max if no overlay change
1861 follows. */
1862 charpos = next_overlay_change (IT_CHARPOS (*it));
1863 if (charpos < it->stop_charpos)
1864 it->stop_charpos = charpos;
1866 /* If showing the region, we have to stop at the region
1867 start or end because the face might change there. */
1868 if (it->region_beg_charpos > 0)
1870 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1871 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1872 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1873 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1876 /* Set up variables for computing the stop position from text
1877 property changes. */
1878 XSETBUFFER (object, current_buffer);
1879 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1880 XSETFASTINT (position, IT_CHARPOS (*it));
1884 /* Get the interval containing IT's position. Value is a null
1885 interval if there isn't such an interval. */
1886 iv = validate_interval_range (object, &position, &position, 0);
1887 if (!NULL_INTERVAL_P (iv))
1889 Lisp_Object values_here[LAST_PROP_IDX];
1890 struct props *p;
1892 /* Get properties here. */
1893 for (p = it_props; p->handler; ++p)
1894 values_here[p->idx] = textget (iv->plist, *p->name);
1896 /* Look for an interval following iv that has different
1897 properties. */
1898 for (next_iv = next_interval (iv);
1899 (!NULL_INTERVAL_P (next_iv)
1900 && (NILP (limit)
1901 || XFASTINT (limit) > next_iv->position));
1902 next_iv = next_interval (next_iv))
1904 for (p = it_props; p->handler; ++p)
1906 Lisp_Object new_value;
1908 new_value = textget (next_iv->plist, *p->name);
1909 if (!EQ (values_here[p->idx], new_value))
1910 break;
1913 if (p->handler)
1914 break;
1917 if (!NULL_INTERVAL_P (next_iv))
1919 if (INTEGERP (limit)
1920 && next_iv->position >= XFASTINT (limit))
1921 /* No text property change up to limit. */
1922 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1923 else
1924 /* Text properties change in next_iv. */
1925 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1929 xassert (STRINGP (it->string)
1930 || (it->stop_charpos >= BEGV
1931 && it->stop_charpos >= IT_CHARPOS (*it)));
1935 /* Return the position of the next overlay change after POS in
1936 current_buffer. Value is point-max if no overlay change
1937 follows. This is like `next-overlay-change' but doesn't use
1938 xmalloc. */
1940 static int
1941 next_overlay_change (pos)
1942 int pos;
1944 int noverlays;
1945 int endpos;
1946 Lisp_Object *overlays;
1947 int len;
1948 int i;
1950 /* Get all overlays at the given position. */
1951 len = 10;
1952 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1953 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1954 if (noverlays > len)
1956 len = noverlays;
1957 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1958 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1961 /* If any of these overlays ends before endpos,
1962 use its ending point instead. */
1963 for (i = 0; i < noverlays; ++i)
1965 Lisp_Object oend;
1966 int oendpos;
1968 oend = OVERLAY_END (overlays[i]);
1969 oendpos = OVERLAY_POSITION (oend);
1970 endpos = min (endpos, oendpos);
1973 return endpos;
1978 /***********************************************************************
1979 Fontification
1980 ***********************************************************************/
1982 /* Handle changes in the `fontified' property of the current buffer by
1983 calling hook functions from Qfontification_functions to fontify
1984 regions of text. */
1986 static enum prop_handled
1987 handle_fontified_prop (it)
1988 struct it *it;
1990 Lisp_Object prop, pos;
1991 enum prop_handled handled = HANDLED_NORMALLY;
1993 /* Get the value of the `fontified' property at IT's current buffer
1994 position. (The `fontified' property doesn't have a special
1995 meaning in strings.) If the value is nil, call functions from
1996 Qfontification_functions. */
1997 if (!STRINGP (it->string)
1998 && it->s == NULL
1999 && !NILP (Vfontification_functions)
2000 && !NILP (Vrun_hooks)
2001 && (pos = make_number (IT_CHARPOS (*it)),
2002 prop = Fget_char_property (pos, Qfontified, Qnil),
2003 NILP (prop)))
2005 int count = specpdl_ptr - specpdl;
2006 Lisp_Object val;
2008 val = Vfontification_functions;
2009 specbind (Qfontification_functions, Qnil);
2010 specbind (Qafter_change_functions, Qnil);
2012 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2013 safe_call1 (val, pos);
2014 else
2016 Lisp_Object globals, fn;
2017 struct gcpro gcpro1, gcpro2;
2019 globals = Qnil;
2020 GCPRO2 (val, globals);
2022 for (; CONSP (val); val = XCDR (val))
2024 fn = XCAR (val);
2026 if (EQ (fn, Qt))
2028 /* A value of t indicates this hook has a local
2029 binding; it means to run the global binding too.
2030 In a global value, t should not occur. If it
2031 does, we must ignore it to avoid an endless
2032 loop. */
2033 for (globals = Fdefault_value (Qfontification_functions);
2034 CONSP (globals);
2035 globals = XCDR (globals))
2037 fn = XCAR (globals);
2038 if (!EQ (fn, Qt))
2039 safe_call1 (fn, pos);
2042 else
2043 safe_call1 (fn, pos);
2046 UNGCPRO;
2049 unbind_to (count, Qnil);
2051 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2052 something. This avoids an endless loop if they failed to
2053 fontify the text for which reason ever. */
2054 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2055 handled = HANDLED_RECOMPUTE_PROPS;
2058 return handled;
2063 /***********************************************************************
2064 Faces
2065 ***********************************************************************/
2067 /* Set up iterator IT from face properties at its current position.
2068 Called from handle_stop. */
2070 static enum prop_handled
2071 handle_face_prop (it)
2072 struct it *it;
2074 int new_face_id, next_stop;
2076 if (!STRINGP (it->string))
2078 new_face_id
2079 = face_at_buffer_position (it->w,
2080 IT_CHARPOS (*it),
2081 it->region_beg_charpos,
2082 it->region_end_charpos,
2083 &next_stop,
2084 (IT_CHARPOS (*it)
2085 + TEXT_PROP_DISTANCE_LIMIT),
2088 /* Is this a start of a run of characters with box face?
2089 Caveat: this can be called for a freshly initialized
2090 iterator; face_id is -1 is this case. We know that the new
2091 face will not change until limit, i.e. if the new face has a
2092 box, all characters up to limit will have one. But, as
2093 usual, we don't know whether limit is really the end. */
2094 if (new_face_id != it->face_id)
2096 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2098 /* If new face has a box but old face has not, this is
2099 the start of a run of characters with box, i.e. it has
2100 a shadow on the left side. The value of face_id of the
2101 iterator will be -1 if this is the initial call that gets
2102 the face. In this case, we have to look in front of IT's
2103 position and see whether there is a face != new_face_id. */
2104 it->start_of_box_run_p
2105 = (new_face->box != FACE_NO_BOX
2106 && (it->face_id >= 0
2107 || IT_CHARPOS (*it) == BEG
2108 || new_face_id != face_before_it_pos (it)));
2109 it->face_box_p = new_face->box != FACE_NO_BOX;
2112 else
2114 new_face_id
2115 = face_at_string_position (it->w,
2116 it->string,
2117 IT_STRING_CHARPOS (*it),
2118 (it->current.overlay_string_index >= 0
2119 ? IT_CHARPOS (*it)
2120 : 0),
2121 it->region_beg_charpos,
2122 it->region_end_charpos,
2123 &next_stop,
2124 it->base_face_id);
2126 #if 0 /* This shouldn't be neccessary. Let's check it. */
2127 /* If IT is used to display a mode line we would really like to
2128 use the mode line face instead of the frame's default face. */
2129 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2130 && new_face_id == DEFAULT_FACE_ID)
2131 new_face_id = MODE_LINE_FACE_ID;
2132 #endif
2134 /* Is this a start of a run of characters with box? Caveat:
2135 this can be called for a freshly allocated iterator; face_id
2136 is -1 is this case. We know that the new face will not
2137 change until the next check pos, i.e. if the new face has a
2138 box, all characters up to that position will have a
2139 box. But, as usual, we don't know whether that position
2140 is really the end. */
2141 if (new_face_id != it->face_id)
2143 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2144 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2146 /* If new face has a box but old face hasn't, this is the
2147 start of a run of characters with box, i.e. it has a
2148 shadow on the left side. */
2149 it->start_of_box_run_p
2150 = new_face->box && (old_face == NULL || !old_face->box);
2151 it->face_box_p = new_face->box != FACE_NO_BOX;
2155 it->face_id = new_face_id;
2156 return HANDLED_NORMALLY;
2160 /* Compute the face one character before or after the current position
2161 of IT. BEFORE_P non-zero means get the face in front of IT's
2162 position. Value is the id of the face. */
2164 static int
2165 face_before_or_after_it_pos (it, before_p)
2166 struct it *it;
2167 int before_p;
2169 int face_id, limit;
2170 int next_check_charpos;
2171 struct text_pos pos;
2173 xassert (it->s == NULL);
2175 if (STRINGP (it->string))
2177 /* No face change past the end of the string (for the case
2178 we are padding with spaces). No face change before the
2179 string start. */
2180 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2181 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2182 return it->face_id;
2184 /* Set pos to the position before or after IT's current position. */
2185 if (before_p)
2186 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2187 else
2188 /* For composition, we must check the character after the
2189 composition. */
2190 pos = (it->what == IT_COMPOSITION
2191 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2192 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2194 /* Get the face for ASCII, or unibyte. */
2195 face_id
2196 = face_at_string_position (it->w,
2197 it->string,
2198 CHARPOS (pos),
2199 (it->current.overlay_string_index >= 0
2200 ? IT_CHARPOS (*it)
2201 : 0),
2202 it->region_beg_charpos,
2203 it->region_end_charpos,
2204 &next_check_charpos,
2205 it->base_face_id);
2207 /* Correct the face for charsets different from ASCII. Do it
2208 for the multibyte case only. The face returned above is
2209 suitable for unibyte text if IT->string is unibyte. */
2210 if (STRING_MULTIBYTE (it->string))
2212 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2213 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2214 int c, len;
2215 struct face *face = FACE_FROM_ID (it->f, face_id);
2217 c = string_char_and_length (p, rest, &len);
2218 face_id = FACE_FOR_CHAR (it->f, face, c);
2221 else
2223 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2224 || (IT_CHARPOS (*it) <= BEGV && before_p))
2225 return it->face_id;
2227 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2228 pos = it->current.pos;
2230 if (before_p)
2231 DEC_TEXT_POS (pos, it->multibyte_p);
2232 else
2234 if (it->what == IT_COMPOSITION)
2235 /* For composition, we must check the position after the
2236 composition. */
2237 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2238 else
2239 INC_TEXT_POS (pos, it->multibyte_p);
2241 /* Determine face for CHARSET_ASCII, or unibyte. */
2242 face_id = face_at_buffer_position (it->w,
2243 CHARPOS (pos),
2244 it->region_beg_charpos,
2245 it->region_end_charpos,
2246 &next_check_charpos,
2247 limit, 0);
2249 /* Correct the face for charsets different from ASCII. Do it
2250 for the multibyte case only. The face returned above is
2251 suitable for unibyte text if current_buffer is unibyte. */
2252 if (it->multibyte_p)
2254 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2255 struct face *face = FACE_FROM_ID (it->f, face_id);
2256 face_id = FACE_FOR_CHAR (it->f, face, c);
2260 return face_id;
2265 /***********************************************************************
2266 Invisible text
2267 ***********************************************************************/
2269 /* Set up iterator IT from invisible properties at its current
2270 position. Called from handle_stop. */
2272 static enum prop_handled
2273 handle_invisible_prop (it)
2274 struct it *it;
2276 enum prop_handled handled = HANDLED_NORMALLY;
2278 if (STRINGP (it->string))
2280 extern Lisp_Object Qinvisible;
2281 Lisp_Object prop, end_charpos, limit, charpos;
2283 /* Get the value of the invisible text property at the
2284 current position. Value will be nil if there is no such
2285 property. */
2286 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2287 prop = Fget_text_property (charpos, Qinvisible, it->string);
2289 if (!NILP (prop)
2290 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2292 handled = HANDLED_RECOMPUTE_PROPS;
2294 /* Get the position at which the next change of the
2295 invisible text property can be found in IT->string.
2296 Value will be nil if the property value is the same for
2297 all the rest of IT->string. */
2298 XSETINT (limit, XSTRING (it->string)->size);
2299 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2300 it->string, limit);
2302 /* Text at current position is invisible. The next
2303 change in the property is at position end_charpos.
2304 Move IT's current position to that position. */
2305 if (INTEGERP (end_charpos)
2306 && XFASTINT (end_charpos) < XFASTINT (limit))
2308 struct text_pos old;
2309 old = it->current.string_pos;
2310 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2311 compute_string_pos (&it->current.string_pos, old, it->string);
2313 else
2315 /* The rest of the string is invisible. If this is an
2316 overlay string, proceed with the next overlay string
2317 or whatever comes and return a character from there. */
2318 if (it->current.overlay_string_index >= 0)
2320 next_overlay_string (it);
2321 /* Don't check for overlay strings when we just
2322 finished processing them. */
2323 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2325 else
2327 struct Lisp_String *s = XSTRING (it->string);
2328 IT_STRING_CHARPOS (*it) = s->size;
2329 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2334 else
2336 int visible_p, newpos, next_stop;
2337 Lisp_Object pos, prop;
2339 /* First of all, is there invisible text at this position? */
2340 XSETFASTINT (pos, IT_CHARPOS (*it));
2341 prop = Fget_char_property (pos, Qinvisible, it->window);
2343 /* If we are on invisible text, skip over it. */
2344 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2345 && IT_CHARPOS (*it) < it->end_charpos)
2347 /* Record whether we have to display an ellipsis for the
2348 invisible text. */
2349 int display_ellipsis_p
2350 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2352 handled = HANDLED_RECOMPUTE_PROPS;
2354 /* Loop skipping over invisible text. The loop is left at
2355 ZV or with IT on the first char being visible again. */
2358 /* Try to skip some invisible text. Return value is the
2359 position reached which can be equal to IT's position
2360 if there is nothing invisible here. This skips both
2361 over invisible text properties and overlays with
2362 invisible property. */
2363 newpos = skip_invisible (IT_CHARPOS (*it),
2364 &next_stop, ZV, it->window);
2366 /* If we skipped nothing at all we weren't at invisible
2367 text in the first place. If everything to the end of
2368 the buffer was skipped, end the loop. */
2369 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2370 visible_p = 1;
2371 else
2373 /* We skipped some characters but not necessarily
2374 all there are. Check if we ended up on visible
2375 text. Fget_char_property returns the property of
2376 the char before the given position, i.e. if we
2377 get visible_p = 1, this means that the char at
2378 newpos is visible. */
2379 XSETFASTINT (pos, newpos);
2380 prop = Fget_char_property (pos, Qinvisible, it->window);
2381 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2384 /* If we ended up on invisible text, proceed to
2385 skip starting with next_stop. */
2386 if (!visible_p)
2387 IT_CHARPOS (*it) = next_stop;
2389 while (!visible_p);
2391 /* The position newpos is now either ZV or on visible text. */
2392 IT_CHARPOS (*it) = newpos;
2393 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2395 /* Maybe return `...' next for the end of the invisible text. */
2396 if (display_ellipsis_p)
2398 if (it->dp
2399 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2401 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2402 it->dpvec = v->contents;
2403 it->dpend = v->contents + v->size;
2405 else
2407 /* Default `...'. */
2408 it->dpvec = default_invis_vector;
2409 it->dpend = default_invis_vector + 3;
2412 /* The ellipsis display does not replace the display of
2413 the character at the new position. Indicate this by
2414 setting IT->dpvec_char_len to zero. */
2415 it->dpvec_char_len = 0;
2417 it->current.dpvec_index = 0;
2418 it->method = next_element_from_display_vector;
2423 return handled;
2428 /***********************************************************************
2429 'display' property
2430 ***********************************************************************/
2432 /* Set up iterator IT from `display' property at its current position.
2433 Called from handle_stop. */
2435 static enum prop_handled
2436 handle_display_prop (it)
2437 struct it *it;
2439 Lisp_Object prop, object;
2440 struct text_pos *position;
2441 int space_or_image_found_p;
2443 if (STRINGP (it->string))
2445 object = it->string;
2446 position = &it->current.string_pos;
2448 else
2450 object = Qnil;
2451 position = &it->current.pos;
2454 /* Reset those iterator values set from display property values. */
2455 it->font_height = Qnil;
2456 it->space_width = Qnil;
2457 it->voffset = 0;
2459 /* We don't support recursive `display' properties, i.e. string
2460 values that have a string `display' property, that have a string
2461 `display' property etc. */
2462 if (!it->string_from_display_prop_p)
2463 it->area = TEXT_AREA;
2465 prop = Fget_char_property (make_number (position->charpos),
2466 Qdisplay, object);
2467 if (NILP (prop))
2468 return HANDLED_NORMALLY;
2470 space_or_image_found_p = 0;
2471 if (CONSP (prop)
2472 && CONSP (XCAR (prop))
2473 && !EQ (Qmargin, XCAR (XCAR (prop))))
2475 /* A list of sub-properties. */
2476 while (CONSP (prop))
2478 if (handle_single_display_prop (it, XCAR (prop), object, position))
2479 space_or_image_found_p = 1;
2480 prop = XCDR (prop);
2483 else if (VECTORP (prop))
2485 int i;
2486 for (i = 0; i < XVECTOR (prop)->size; ++i)
2487 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2488 object, position))
2489 space_or_image_found_p = 1;
2491 else
2493 if (handle_single_display_prop (it, prop, object, position))
2494 space_or_image_found_p = 1;
2497 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2501 /* Value is the position of the end of the `display' property starting
2502 at START_POS in OBJECT. */
2504 static struct text_pos
2505 display_prop_end (it, object, start_pos)
2506 struct it *it;
2507 Lisp_Object object;
2508 struct text_pos start_pos;
2510 Lisp_Object end;
2511 struct text_pos end_pos;
2513 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2514 Qdisplay, object, Qnil);
2515 CHARPOS (end_pos) = XFASTINT (end);
2516 if (STRINGP (object))
2517 compute_string_pos (&end_pos, start_pos, it->string);
2518 else
2519 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2521 return end_pos;
2525 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2526 is the object in which the `display' property was found. *POSITION
2527 is the position at which it was found.
2529 If PROP is a `space' or `image' sub-property, set *POSITION to the
2530 end position of the `display' property.
2532 Value is non-zero if a `space' or `image' property value was found. */
2534 static int
2535 handle_single_display_prop (it, prop, object, position)
2536 struct it *it;
2537 Lisp_Object prop;
2538 Lisp_Object object;
2539 struct text_pos *position;
2541 Lisp_Object value;
2542 int space_or_image_found_p = 0;
2543 Lisp_Object form;
2545 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2546 evaluated. If the result is nil, VALUE is ignored. */
2547 form = Qt;
2548 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2550 prop = XCDR (prop);
2551 if (!CONSP (prop))
2552 return 0;
2553 form = XCAR (prop);
2554 prop = XCDR (prop);
2557 if (!NILP (form) && !EQ (form, Qt))
2559 struct gcpro gcpro1;
2560 struct text_pos end_pos, pt;
2562 GCPRO1 (form);
2563 end_pos = display_prop_end (it, object, *position);
2565 /* Temporarily set point to the end position, and then evaluate
2566 the form. This makes `(eolp)' work as FORM. */
2567 if (BUFFERP (object))
2569 CHARPOS (pt) = PT;
2570 BYTEPOS (pt) = PT_BYTE;
2571 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2574 form = safe_eval (form);
2576 if (BUFFERP (object))
2577 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2578 UNGCPRO;
2581 if (NILP (form))
2582 return 0;
2584 if (CONSP (prop)
2585 && EQ (XCAR (prop), Qheight)
2586 && CONSP (XCDR (prop)))
2588 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2589 return 0;
2591 /* `(height HEIGHT)'. */
2592 it->font_height = XCAR (XCDR (prop));
2593 if (!NILP (it->font_height))
2595 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2596 int new_height = -1;
2598 if (CONSP (it->font_height)
2599 && (EQ (XCAR (it->font_height), Qplus)
2600 || EQ (XCAR (it->font_height), Qminus))
2601 && CONSP (XCDR (it->font_height))
2602 && INTEGERP (XCAR (XCDR (it->font_height))))
2604 /* `(+ N)' or `(- N)' where N is an integer. */
2605 int steps = XINT (XCAR (XCDR (it->font_height)));
2606 if (EQ (XCAR (it->font_height), Qplus))
2607 steps = - steps;
2608 it->face_id = smaller_face (it->f, it->face_id, steps);
2610 else if (FUNCTIONP (it->font_height))
2612 /* Call function with current height as argument.
2613 Value is the new height. */
2614 Lisp_Object height;
2615 height = safe_call1 (it->font_height,
2616 face->lface[LFACE_HEIGHT_INDEX]);
2617 if (NUMBERP (height))
2618 new_height = XFLOATINT (height);
2620 else if (NUMBERP (it->font_height))
2622 /* Value is a multiple of the canonical char height. */
2623 struct face *face;
2625 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2626 new_height = (XFLOATINT (it->font_height)
2627 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2629 else
2631 /* Evaluate IT->font_height with `height' bound to the
2632 current specified height to get the new height. */
2633 Lisp_Object value;
2634 int count = specpdl_ptr - specpdl;
2636 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2637 value = safe_eval (it->font_height);
2638 unbind_to (count, Qnil);
2640 if (NUMBERP (value))
2641 new_height = XFLOATINT (value);
2644 if (new_height > 0)
2645 it->face_id = face_with_height (it->f, it->face_id, new_height);
2648 else if (CONSP (prop)
2649 && EQ (XCAR (prop), Qspace_width)
2650 && CONSP (XCDR (prop)))
2652 /* `(space_width WIDTH)'. */
2653 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2654 return 0;
2656 value = XCAR (XCDR (prop));
2657 if (NUMBERP (value) && XFLOATINT (value) > 0)
2658 it->space_width = value;
2660 else if (CONSP (prop)
2661 && EQ (XCAR (prop), Qraise)
2662 && CONSP (XCDR (prop)))
2664 /* `(raise FACTOR)'. */
2665 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2666 return 0;
2668 #ifdef HAVE_WINDOW_SYSTEM
2669 value = XCAR (XCDR (prop));
2670 if (NUMBERP (value))
2672 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2673 it->voffset = - (XFLOATINT (value)
2674 * (FONT_HEIGHT (face->font)));
2676 #endif /* HAVE_WINDOW_SYSTEM */
2678 else if (!it->string_from_display_prop_p)
2680 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2681 VALUE) or `((margin nil) VALUE)' or VALUE. */
2682 Lisp_Object location, value;
2683 struct text_pos start_pos;
2684 int valid_p;
2686 /* Characters having this form of property are not displayed, so
2687 we have to find the end of the property. */
2688 start_pos = *position;
2689 *position = display_prop_end (it, object, start_pos);
2690 value = Qnil;
2692 /* Let's stop at the new position and assume that all
2693 text properties change there. */
2694 it->stop_charpos = position->charpos;
2696 location = Qunbound;
2697 if (CONSP (prop) && CONSP (XCAR (prop)))
2699 Lisp_Object tem;
2701 value = XCDR (prop);
2702 if (CONSP (value))
2703 value = XCAR (value);
2705 tem = XCAR (prop);
2706 if (EQ (XCAR (tem), Qmargin)
2707 && (tem = XCDR (tem),
2708 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2709 (NILP (tem)
2710 || EQ (tem, Qleft_margin)
2711 || EQ (tem, Qright_margin))))
2712 location = tem;
2715 if (EQ (location, Qunbound))
2717 location = Qnil;
2718 value = prop;
2721 #ifdef HAVE_WINDOW_SYSTEM
2722 if (FRAME_TERMCAP_P (it->f))
2723 valid_p = STRINGP (value);
2724 else
2725 valid_p = (STRINGP (value)
2726 || (CONSP (value) && EQ (XCAR (value), Qspace))
2727 || valid_image_p (value));
2728 #else /* not HAVE_WINDOW_SYSTEM */
2729 valid_p = STRINGP (value);
2730 #endif /* not HAVE_WINDOW_SYSTEM */
2732 if ((EQ (location, Qleft_margin)
2733 || EQ (location, Qright_margin)
2734 || NILP (location))
2735 && valid_p)
2737 space_or_image_found_p = 1;
2739 /* Save current settings of IT so that we can restore them
2740 when we are finished with the glyph property value. */
2741 push_it (it);
2743 if (NILP (location))
2744 it->area = TEXT_AREA;
2745 else if (EQ (location, Qleft_margin))
2746 it->area = LEFT_MARGIN_AREA;
2747 else
2748 it->area = RIGHT_MARGIN_AREA;
2750 if (STRINGP (value))
2752 it->string = value;
2753 it->multibyte_p = STRING_MULTIBYTE (it->string);
2754 it->current.overlay_string_index = -1;
2755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2756 it->end_charpos = it->string_nchars
2757 = XSTRING (it->string)->size;
2758 it->method = next_element_from_string;
2759 it->stop_charpos = 0;
2760 it->string_from_display_prop_p = 1;
2762 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2764 it->method = next_element_from_stretch;
2765 it->object = value;
2766 it->current.pos = it->position = start_pos;
2768 #ifdef HAVE_WINDOW_SYSTEM
2769 else
2771 it->what = IT_IMAGE;
2772 it->image_id = lookup_image (it->f, value);
2773 it->position = start_pos;
2774 it->object = NILP (object) ? it->w->buffer : object;
2775 it->method = next_element_from_image;
2777 /* Say that we haven't consumed the characters with
2778 `display' property yet. The call to pop_it in
2779 set_iterator_to_next will clean this up. */
2780 *position = start_pos;
2782 #endif /* HAVE_WINDOW_SYSTEM */
2784 else
2785 /* Invalid property or property not supported. Restore
2786 the position to what it was before. */
2787 *position = start_pos;
2790 return space_or_image_found_p;
2794 /* Check if PROP is a display sub-property value whose text should be
2795 treated as intangible. */
2797 static int
2798 single_display_prop_intangible_p (prop)
2799 Lisp_Object prop;
2801 /* Skip over `when FORM'. */
2802 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2804 prop = XCDR (prop);
2805 if (!CONSP (prop))
2806 return 0;
2807 prop = XCDR (prop);
2810 if (!CONSP (prop))
2811 return 0;
2813 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2814 we don't need to treat text as intangible. */
2815 if (EQ (XCAR (prop), Qmargin))
2817 prop = XCDR (prop);
2818 if (!CONSP (prop))
2819 return 0;
2821 prop = XCDR (prop);
2822 if (!CONSP (prop)
2823 || EQ (XCAR (prop), Qleft_margin)
2824 || EQ (XCAR (prop), Qright_margin))
2825 return 0;
2828 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2832 /* Check if PROP is a display property value whose text should be
2833 treated as intangible. */
2836 display_prop_intangible_p (prop)
2837 Lisp_Object prop;
2839 if (CONSP (prop)
2840 && CONSP (XCAR (prop))
2841 && !EQ (Qmargin, XCAR (XCAR (prop))))
2843 /* A list of sub-properties. */
2844 while (CONSP (prop))
2846 if (single_display_prop_intangible_p (XCAR (prop)))
2847 return 1;
2848 prop = XCDR (prop);
2851 else if (VECTORP (prop))
2853 /* A vector of sub-properties. */
2854 int i;
2855 for (i = 0; i < XVECTOR (prop)->size; ++i)
2856 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2857 return 1;
2859 else
2860 return single_display_prop_intangible_p (prop);
2862 return 0;
2866 /***********************************************************************
2867 `composition' property
2868 ***********************************************************************/
2870 /* Set up iterator IT from `composition' property at its current
2871 position. Called from handle_stop. */
2873 static enum prop_handled
2874 handle_composition_prop (it)
2875 struct it *it;
2877 Lisp_Object prop, string;
2878 int pos, pos_byte, end;
2879 enum prop_handled handled = HANDLED_NORMALLY;
2881 if (STRINGP (it->string))
2883 pos = IT_STRING_CHARPOS (*it);
2884 pos_byte = IT_STRING_BYTEPOS (*it);
2885 string = it->string;
2887 else
2889 pos = IT_CHARPOS (*it);
2890 pos_byte = IT_BYTEPOS (*it);
2891 string = Qnil;
2894 /* If there's a valid composition and point is not inside of the
2895 composition (in the case that the composition is from the current
2896 buffer), draw a glyph composed from the composition components. */
2897 if (find_composition (pos, -1, &pos, &end, &prop, string)
2898 && COMPOSITION_VALID_P (pos, end, prop)
2899 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2901 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2903 if (id >= 0)
2905 it->method = next_element_from_composition;
2906 it->cmp_id = id;
2907 it->cmp_len = COMPOSITION_LENGTH (prop);
2908 /* For a terminal, draw only the first character of the
2909 components. */
2910 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2911 it->len = (STRINGP (it->string)
2912 ? string_char_to_byte (it->string, end)
2913 : CHAR_TO_BYTE (end)) - pos_byte;
2914 it->stop_charpos = end;
2915 handled = HANDLED_RETURN;
2919 return handled;
2924 /***********************************************************************
2925 Overlay strings
2926 ***********************************************************************/
2928 /* The following structure is used to record overlay strings for
2929 later sorting in load_overlay_strings. */
2931 struct overlay_entry
2933 Lisp_Object overlay;
2934 Lisp_Object string;
2935 int priority;
2936 int after_string_p;
2940 /* Set up iterator IT from overlay strings at its current position.
2941 Called from handle_stop. */
2943 static enum prop_handled
2944 handle_overlay_change (it)
2945 struct it *it;
2947 if (!STRINGP (it->string) && get_overlay_strings (it))
2948 return HANDLED_RECOMPUTE_PROPS;
2949 else
2950 return HANDLED_NORMALLY;
2954 /* Set up the next overlay string for delivery by IT, if there is an
2955 overlay string to deliver. Called by set_iterator_to_next when the
2956 end of the current overlay string is reached. If there are more
2957 overlay strings to display, IT->string and
2958 IT->current.overlay_string_index are set appropriately here.
2959 Otherwise IT->string is set to nil. */
2961 static void
2962 next_overlay_string (it)
2963 struct it *it;
2965 ++it->current.overlay_string_index;
2966 if (it->current.overlay_string_index == it->n_overlay_strings)
2968 /* No more overlay strings. Restore IT's settings to what
2969 they were before overlay strings were processed, and
2970 continue to deliver from current_buffer. */
2971 pop_it (it);
2972 xassert (it->stop_charpos >= BEGV
2973 && it->stop_charpos <= it->end_charpos);
2974 it->string = Qnil;
2975 it->current.overlay_string_index = -1;
2976 SET_TEXT_POS (it->current.string_pos, -1, -1);
2977 it->n_overlay_strings = 0;
2978 it->method = next_element_from_buffer;
2980 /* If we're at the end of the buffer, record that we have
2981 processed the overlay strings there already, so that
2982 next_element_from_buffer doesn't try it again. */
2983 if (IT_CHARPOS (*it) >= it->end_charpos)
2984 it->overlay_strings_at_end_processed_p = 1;
2986 else
2988 /* There are more overlay strings to process. If
2989 IT->current.overlay_string_index has advanced to a position
2990 where we must load IT->overlay_strings with more strings, do
2991 it. */
2992 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2994 if (it->current.overlay_string_index && i == 0)
2995 load_overlay_strings (it);
2997 /* Initialize IT to deliver display elements from the overlay
2998 string. */
2999 it->string = it->overlay_strings[i];
3000 it->multibyte_p = STRING_MULTIBYTE (it->string);
3001 SET_TEXT_POS (it->current.string_pos, 0, 0);
3002 it->method = next_element_from_string;
3003 it->stop_charpos = 0;
3006 CHECK_IT (it);
3010 /* Compare two overlay_entry structures E1 and E2. Used as a
3011 comparison function for qsort in load_overlay_strings. Overlay
3012 strings for the same position are sorted so that
3014 1. All after-strings come in front of before-strings, except
3015 when they come from the same overlay.
3017 2. Within after-strings, strings are sorted so that overlay strings
3018 from overlays with higher priorities come first.
3020 2. Within before-strings, strings are sorted so that overlay
3021 strings from overlays with higher priorities come last.
3023 Value is analogous to strcmp. */
3026 static int
3027 compare_overlay_entries (e1, e2)
3028 void *e1, *e2;
3030 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3031 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3032 int result;
3034 if (entry1->after_string_p != entry2->after_string_p)
3036 /* Let after-strings appear in front of before-strings if
3037 they come from different overlays. */
3038 if (EQ (entry1->overlay, entry2->overlay))
3039 result = entry1->after_string_p ? 1 : -1;
3040 else
3041 result = entry1->after_string_p ? -1 : 1;
3043 else if (entry1->after_string_p)
3044 /* After-strings sorted in order of decreasing priority. */
3045 result = entry2->priority - entry1->priority;
3046 else
3047 /* Before-strings sorted in order of increasing priority. */
3048 result = entry1->priority - entry2->priority;
3050 return result;
3054 /* Load the vector IT->overlay_strings with overlay strings from IT's
3055 current buffer position. Set IT->n_overlays to the total number of
3056 overlay strings found.
3058 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3059 a time. On entry into load_overlay_strings,
3060 IT->current.overlay_string_index gives the number of overlay
3061 strings that have already been loaded by previous calls to this
3062 function.
3064 IT->add_overlay_start contains an additional overlay start
3065 position to consider for taking overlay strings from, if non-zero.
3066 This position comes into play when the overlay has an `invisible'
3067 property, and both before and after-strings. When we've skipped to
3068 the end of the overlay, because of its `invisible' property, we
3069 nevertheless want its before-string to appear.
3070 IT->add_overlay_start will contain the overlay start position
3071 in this case.
3073 Overlay strings are sorted so that after-string strings come in
3074 front of before-string strings. Within before and after-strings,
3075 strings are sorted by overlay priority. See also function
3076 compare_overlay_entries. */
3078 static void
3079 load_overlay_strings (it)
3080 struct it *it;
3082 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3083 Lisp_Object ov, overlay, window, str, invisible;
3084 int start, end;
3085 int size = 20;
3086 int n = 0, i, j, invis_p;
3087 struct overlay_entry *entries
3088 = (struct overlay_entry *) alloca (size * sizeof *entries);
3089 int charpos = IT_CHARPOS (*it);
3091 /* Append the overlay string STRING of overlay OVERLAY to vector
3092 `entries' which has size `size' and currently contains `n'
3093 elements. AFTER_P non-zero means STRING is an after-string of
3094 OVERLAY. */
3095 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3096 do \
3098 Lisp_Object priority; \
3100 if (n == size) \
3102 int new_size = 2 * size; \
3103 struct overlay_entry *old = entries; \
3104 entries = \
3105 (struct overlay_entry *) alloca (new_size \
3106 * sizeof *entries); \
3107 bcopy (old, entries, size * sizeof *entries); \
3108 size = new_size; \
3111 entries[n].string = (STRING); \
3112 entries[n].overlay = (OVERLAY); \
3113 priority = Foverlay_get ((OVERLAY), Qpriority); \
3114 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3115 entries[n].after_string_p = (AFTER_P); \
3116 ++n; \
3118 while (0)
3120 /* Process overlay before the overlay center. */
3121 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3123 overlay = XCAR (ov);
3124 xassert (OVERLAYP (overlay));
3125 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3126 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3128 if (end < charpos)
3129 break;
3131 /* Skip this overlay if it doesn't start or end at IT's current
3132 position. */
3133 if (end != charpos && start != charpos)
3134 continue;
3136 /* Skip this overlay if it doesn't apply to IT->w. */
3137 window = Foverlay_get (overlay, Qwindow);
3138 if (WINDOWP (window) && XWINDOW (window) != it->w)
3139 continue;
3141 /* If the text ``under'' the overlay is invisible, both before-
3142 and after-strings from this overlay are visible; start and
3143 end position are indistinguishable. */
3144 invisible = Foverlay_get (overlay, Qinvisible);
3145 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3147 /* If overlay has a non-empty before-string, record it. */
3148 if ((start == charpos || (end == charpos && invis_p))
3149 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3150 && XSTRING (str)->size)
3151 RECORD_OVERLAY_STRING (overlay, str, 0);
3153 /* If overlay has a non-empty after-string, record it. */
3154 if ((end == charpos || (start == charpos && invis_p))
3155 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3156 && XSTRING (str)->size)
3157 RECORD_OVERLAY_STRING (overlay, str, 1);
3160 /* Process overlays after the overlay center. */
3161 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3163 overlay = XCAR (ov);
3164 xassert (OVERLAYP (overlay));
3165 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3166 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3168 if (start > charpos)
3169 break;
3171 /* Skip this overlay if it doesn't start or end at IT's current
3172 position. */
3173 if (end != charpos && start != charpos)
3174 continue;
3176 /* Skip this overlay if it doesn't apply to IT->w. */
3177 window = Foverlay_get (overlay, Qwindow);
3178 if (WINDOWP (window) && XWINDOW (window) != it->w)
3179 continue;
3181 /* If the text ``under'' the overlay is invisible, it has a zero
3182 dimension, and both before- and after-strings apply. */
3183 invisible = Foverlay_get (overlay, Qinvisible);
3184 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3186 /* If overlay has a non-empty before-string, record it. */
3187 if ((start == charpos || (end == charpos && invis_p))
3188 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3189 && XSTRING (str)->size)
3190 RECORD_OVERLAY_STRING (overlay, str, 0);
3192 /* If overlay has a non-empty after-string, record it. */
3193 if ((end == charpos || (start == charpos && invis_p))
3194 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3195 && XSTRING (str)->size)
3196 RECORD_OVERLAY_STRING (overlay, str, 1);
3199 #undef RECORD_OVERLAY_STRING
3201 /* Sort entries. */
3202 if (n > 1)
3203 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3205 /* Record the total number of strings to process. */
3206 it->n_overlay_strings = n;
3208 /* IT->current.overlay_string_index is the number of overlay strings
3209 that have already been consumed by IT. Copy some of the
3210 remaining overlay strings to IT->overlay_strings. */
3211 i = 0;
3212 j = it->current.overlay_string_index;
3213 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3214 it->overlay_strings[i++] = entries[j++].string;
3216 CHECK_IT (it);
3220 /* Get the first chunk of overlay strings at IT's current buffer
3221 position. Value is non-zero if at least one overlay string was
3222 found. */
3224 static int
3225 get_overlay_strings (it)
3226 struct it *it;
3228 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3229 process. This fills IT->overlay_strings with strings, and sets
3230 IT->n_overlay_strings to the total number of strings to process.
3231 IT->pos.overlay_string_index has to be set temporarily to zero
3232 because load_overlay_strings needs this; it must be set to -1
3233 when no overlay strings are found because a zero value would
3234 indicate a position in the first overlay string. */
3235 it->current.overlay_string_index = 0;
3236 load_overlay_strings (it);
3238 /* If we found overlay strings, set up IT to deliver display
3239 elements from the first one. Otherwise set up IT to deliver
3240 from current_buffer. */
3241 if (it->n_overlay_strings)
3243 /* Make sure we know settings in current_buffer, so that we can
3244 restore meaningful values when we're done with the overlay
3245 strings. */
3246 compute_stop_pos (it);
3247 xassert (it->face_id >= 0);
3249 /* Save IT's settings. They are restored after all overlay
3250 strings have been processed. */
3251 xassert (it->sp == 0);
3252 push_it (it);
3254 /* Set up IT to deliver display elements from the first overlay
3255 string. */
3256 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3257 it->stop_charpos = 0;
3258 it->string = it->overlay_strings[0];
3259 it->multibyte_p = STRING_MULTIBYTE (it->string);
3260 xassert (STRINGP (it->string));
3261 it->method = next_element_from_string;
3263 else
3265 it->string = Qnil;
3266 it->current.overlay_string_index = -1;
3267 it->method = next_element_from_buffer;
3270 CHECK_IT (it);
3272 /* Value is non-zero if we found at least one overlay string. */
3273 return STRINGP (it->string);
3278 /***********************************************************************
3279 Saving and restoring state
3280 ***********************************************************************/
3282 /* Save current settings of IT on IT->stack. Called, for example,
3283 before setting up IT for an overlay string, to be able to restore
3284 IT's settings to what they were after the overlay string has been
3285 processed. */
3287 static void
3288 push_it (it)
3289 struct it *it;
3291 struct iterator_stack_entry *p;
3293 xassert (it->sp < 2);
3294 p = it->stack + it->sp;
3296 p->stop_charpos = it->stop_charpos;
3297 xassert (it->face_id >= 0);
3298 p->face_id = it->face_id;
3299 p->string = it->string;
3300 p->pos = it->current;
3301 p->end_charpos = it->end_charpos;
3302 p->string_nchars = it->string_nchars;
3303 p->area = it->area;
3304 p->multibyte_p = it->multibyte_p;
3305 p->space_width = it->space_width;
3306 p->font_height = it->font_height;
3307 p->voffset = it->voffset;
3308 p->string_from_display_prop_p = it->string_from_display_prop_p;
3309 ++it->sp;
3313 /* Restore IT's settings from IT->stack. Called, for example, when no
3314 more overlay strings must be processed, and we return to delivering
3315 display elements from a buffer, or when the end of a string from a
3316 `display' property is reached and we return to delivering display
3317 elements from an overlay string, or from a buffer. */
3319 static void
3320 pop_it (it)
3321 struct it *it;
3323 struct iterator_stack_entry *p;
3325 xassert (it->sp > 0);
3326 --it->sp;
3327 p = it->stack + it->sp;
3328 it->stop_charpos = p->stop_charpos;
3329 it->face_id = p->face_id;
3330 it->string = p->string;
3331 it->current = p->pos;
3332 it->end_charpos = p->end_charpos;
3333 it->string_nchars = p->string_nchars;
3334 it->area = p->area;
3335 it->multibyte_p = p->multibyte_p;
3336 it->space_width = p->space_width;
3337 it->font_height = p->font_height;
3338 it->voffset = p->voffset;
3339 it->string_from_display_prop_p = p->string_from_display_prop_p;
3344 /***********************************************************************
3345 Moving over lines
3346 ***********************************************************************/
3348 /* Set IT's current position to the previous line start. */
3350 static void
3351 back_to_previous_line_start (it)
3352 struct it *it;
3354 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3355 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3359 /* Move IT to the next line start.
3361 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3362 we skipped over part of the text (as opposed to moving the iterator
3363 continuously over the text). Otherwise, don't change the value
3364 of *SKIPPED_P.
3366 Newlines may come from buffer text, overlay strings, or strings
3367 displayed via the `display' property. That's the reason we can't
3368 simply use find_next_newline_no_quit. */
3370 static int
3371 forward_to_next_line_start (it, skipped_p)
3372 struct it *it;
3373 int *skipped_p;
3375 int old_selective, newline_found_p, n;
3376 const int MAX_NEWLINE_DISTANCE = 500;
3378 /* Don't handle selective display in the following. It's (a)
3379 unnecessary and (b) leads to an infinite recursion because
3380 next_element_from_ellipsis indirectly calls this function. */
3381 old_selective = it->selective;
3382 it->selective = 0;
3384 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3385 from buffer text. */
3386 n = newline_found_p = 0;
3387 while (n < MAX_NEWLINE_DISTANCE
3388 && get_next_display_element (it)
3389 && !newline_found_p)
3391 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3392 set_iterator_to_next (it, 0);
3393 if (!STRINGP (it->string))
3394 ++n;
3397 /* If we didn't find a newline near enough, see if we can use a
3398 short-cut. */
3399 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3401 int start = IT_CHARPOS (*it);
3402 int limit = find_next_newline_no_quit (start, 1);
3403 Lisp_Object pos;
3405 xassert (!STRINGP (it->string));
3407 /* If there isn't any `display' property in sight, and no
3408 overlays, we can just use the position of the newline in
3409 buffer text. */
3410 if (it->stop_charpos >= limit
3411 || ((pos = Fnext_single_property_change (make_number (start),
3412 Qdisplay,
3413 Qnil, make_number (limit)),
3414 NILP (pos))
3415 && next_overlay_change (start) == ZV))
3417 IT_CHARPOS (*it) = limit;
3418 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3419 *skipped_p = newline_found_p = 1;
3421 else
3423 while (get_next_display_element (it)
3424 && !newline_found_p)
3426 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3427 set_iterator_to_next (it, 0);
3432 it->selective = old_selective;
3433 return newline_found_p;
3437 /* Set IT's current position to the previous visible line start. Skip
3438 invisible text that is so either due to text properties or due to
3439 selective display. Caution: this does not change IT->current_x and
3440 IT->hpos. */
3442 static void
3443 back_to_previous_visible_line_start (it)
3444 struct it *it;
3446 int visible_p = 0;
3448 /* Go back one newline if not on BEGV already. */
3449 if (IT_CHARPOS (*it) > BEGV)
3450 back_to_previous_line_start (it);
3452 /* Move over lines that are invisible because of selective display
3453 or text properties. */
3454 while (IT_CHARPOS (*it) > BEGV
3455 && !visible_p)
3457 visible_p = 1;
3459 /* If selective > 0, then lines indented more than that values
3460 are invisible. */
3461 if (it->selective > 0
3462 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3463 it->selective))
3464 visible_p = 0;
3465 else
3467 Lisp_Object prop;
3469 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3470 Qinvisible, it->window);
3471 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3472 visible_p = 0;
3475 /* Back one more newline if the current one is invisible. */
3476 if (!visible_p)
3477 back_to_previous_line_start (it);
3480 xassert (IT_CHARPOS (*it) >= BEGV);
3481 xassert (IT_CHARPOS (*it) == BEGV
3482 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3483 CHECK_IT (it);
3487 /* Reseat iterator IT at the previous visible line start. Skip
3488 invisible text that is so either due to text properties or due to
3489 selective display. At the end, update IT's overlay information,
3490 face information etc. */
3492 static void
3493 reseat_at_previous_visible_line_start (it)
3494 struct it *it;
3496 back_to_previous_visible_line_start (it);
3497 reseat (it, it->current.pos, 1);
3498 CHECK_IT (it);
3502 /* Reseat iterator IT on the next visible line start in the current
3503 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3504 preceding the line start. Skip over invisible text that is so
3505 because of selective display. Compute faces, overlays etc at the
3506 new position. Note that this function does not skip over text that
3507 is invisible because of text properties. */
3509 static void
3510 reseat_at_next_visible_line_start (it, on_newline_p)
3511 struct it *it;
3512 int on_newline_p;
3514 int newline_found_p, skipped_p = 0;
3516 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3518 /* Skip over lines that are invisible because they are indented
3519 more than the value of IT->selective. */
3520 if (it->selective > 0)
3521 while (IT_CHARPOS (*it) < ZV
3522 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3523 it->selective))
3524 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3526 /* Position on the newline if that's what's requested. */
3527 if (on_newline_p && newline_found_p)
3529 if (STRINGP (it->string))
3531 if (IT_STRING_CHARPOS (*it) > 0)
3533 --IT_STRING_CHARPOS (*it);
3534 --IT_STRING_BYTEPOS (*it);
3537 else if (IT_CHARPOS (*it) > BEGV)
3539 --IT_CHARPOS (*it);
3540 --IT_BYTEPOS (*it);
3541 reseat (it, it->current.pos, 0);
3544 else if (skipped_p)
3545 reseat (it, it->current.pos, 0);
3547 CHECK_IT (it);
3552 /***********************************************************************
3553 Changing an iterator's position
3554 ***********************************************************************/
3556 /* Change IT's current position to POS in current_buffer. If FORCE_P
3557 is non-zero, always check for text properties at the new position.
3558 Otherwise, text properties are only looked up if POS >=
3559 IT->check_charpos of a property. */
3561 static void
3562 reseat (it, pos, force_p)
3563 struct it *it;
3564 struct text_pos pos;
3565 int force_p;
3567 int original_pos = IT_CHARPOS (*it);
3569 reseat_1 (it, pos, 0);
3571 /* Determine where to check text properties. Avoid doing it
3572 where possible because text property lookup is very expensive. */
3573 if (force_p
3574 || CHARPOS (pos) > it->stop_charpos
3575 || CHARPOS (pos) < original_pos)
3576 handle_stop (it);
3578 CHECK_IT (it);
3582 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3583 IT->stop_pos to POS, also. */
3585 static void
3586 reseat_1 (it, pos, set_stop_p)
3587 struct it *it;
3588 struct text_pos pos;
3589 int set_stop_p;
3591 /* Don't call this function when scanning a C string. */
3592 xassert (it->s == NULL);
3594 /* POS must be a reasonable value. */
3595 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3597 it->current.pos = it->position = pos;
3598 XSETBUFFER (it->object, current_buffer);
3599 it->dpvec = NULL;
3600 it->current.dpvec_index = -1;
3601 it->current.overlay_string_index = -1;
3602 IT_STRING_CHARPOS (*it) = -1;
3603 IT_STRING_BYTEPOS (*it) = -1;
3604 it->string = Qnil;
3605 it->method = next_element_from_buffer;
3606 it->sp = 0;
3608 if (set_stop_p)
3609 it->stop_charpos = CHARPOS (pos);
3613 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3614 If S is non-null, it is a C string to iterate over. Otherwise,
3615 STRING gives a Lisp string to iterate over.
3617 If PRECISION > 0, don't return more then PRECISION number of
3618 characters from the string.
3620 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3621 characters have been returned. FIELD_WIDTH < 0 means an infinite
3622 field width.
3624 MULTIBYTE = 0 means disable processing of multibyte characters,
3625 MULTIBYTE > 0 means enable it,
3626 MULTIBYTE < 0 means use IT->multibyte_p.
3628 IT must be initialized via a prior call to init_iterator before
3629 calling this function. */
3631 static void
3632 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3633 struct it *it;
3634 unsigned char *s;
3635 Lisp_Object string;
3636 int charpos;
3637 int precision, field_width, multibyte;
3639 /* No region in strings. */
3640 it->region_beg_charpos = it->region_end_charpos = -1;
3642 /* No text property checks performed by default, but see below. */
3643 it->stop_charpos = -1;
3645 /* Set iterator position and end position. */
3646 bzero (&it->current, sizeof it->current);
3647 it->current.overlay_string_index = -1;
3648 it->current.dpvec_index = -1;
3649 xassert (charpos >= 0);
3651 /* Use the setting of MULTIBYTE if specified. */
3652 if (multibyte >= 0)
3653 it->multibyte_p = multibyte > 0;
3655 if (s == NULL)
3657 xassert (STRINGP (string));
3658 it->string = string;
3659 it->s = NULL;
3660 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3661 it->method = next_element_from_string;
3662 it->current.string_pos = string_pos (charpos, string);
3664 else
3666 it->s = s;
3667 it->string = Qnil;
3669 /* Note that we use IT->current.pos, not it->current.string_pos,
3670 for displaying C strings. */
3671 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3672 if (it->multibyte_p)
3674 it->current.pos = c_string_pos (charpos, s, 1);
3675 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3677 else
3679 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3680 it->end_charpos = it->string_nchars = strlen (s);
3683 it->method = next_element_from_c_string;
3686 /* PRECISION > 0 means don't return more than PRECISION characters
3687 from the string. */
3688 if (precision > 0 && it->end_charpos - charpos > precision)
3689 it->end_charpos = it->string_nchars = charpos + precision;
3691 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3692 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3693 FIELD_WIDTH < 0 means infinite field width. This is useful for
3694 padding with `-' at the end of a mode line. */
3695 if (field_width < 0)
3696 field_width = INFINITY;
3697 if (field_width > it->end_charpos - charpos)
3698 it->end_charpos = charpos + field_width;
3700 /* Use the standard display table for displaying strings. */
3701 if (DISP_TABLE_P (Vstandard_display_table))
3702 it->dp = XCHAR_TABLE (Vstandard_display_table);
3704 it->stop_charpos = charpos;
3705 CHECK_IT (it);
3710 /***********************************************************************
3711 Iteration
3712 ***********************************************************************/
3714 /* Load IT's display element fields with information about the next
3715 display element from the current position of IT. Value is zero if
3716 end of buffer (or C string) is reached. */
3719 get_next_display_element (it)
3720 struct it *it;
3722 /* Non-zero means that we found an display element. Zero means that
3723 we hit the end of what we iterate over. Performance note: the
3724 function pointer `method' used here turns out to be faster than
3725 using a sequence of if-statements. */
3726 int success_p = (*it->method) (it);
3728 if (it->what == IT_CHARACTER)
3730 /* Map via display table or translate control characters.
3731 IT->c, IT->len etc. have been set to the next character by
3732 the function call above. If we have a display table, and it
3733 contains an entry for IT->c, translate it. Don't do this if
3734 IT->c itself comes from a display table, otherwise we could
3735 end up in an infinite recursion. (An alternative could be to
3736 count the recursion depth of this function and signal an
3737 error when a certain maximum depth is reached.) Is it worth
3738 it? */
3739 if (success_p && it->dpvec == NULL)
3741 Lisp_Object dv;
3743 if (it->dp
3744 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3745 VECTORP (dv)))
3747 struct Lisp_Vector *v = XVECTOR (dv);
3749 /* Return the first character from the display table
3750 entry, if not empty. If empty, don't display the
3751 current character. */
3752 if (v->size)
3754 it->dpvec_char_len = it->len;
3755 it->dpvec = v->contents;
3756 it->dpend = v->contents + v->size;
3757 it->current.dpvec_index = 0;
3758 it->method = next_element_from_display_vector;
3761 success_p = get_next_display_element (it);
3764 /* Translate control characters into `\003' or `^C' form.
3765 Control characters coming from a display table entry are
3766 currently not translated because we use IT->dpvec to hold
3767 the translation. This could easily be changed but I
3768 don't believe that it is worth doing.
3770 Non-printable multibyte characters are also translated
3771 octal form. */
3772 else if ((it->c < ' '
3773 && (it->area != TEXT_AREA
3774 || (it->c != '\n' && it->c != '\t')))
3775 || (it->c >= 127
3776 && it->len == 1)
3777 || !CHAR_PRINTABLE_P (it->c))
3779 /* IT->c is a control character which must be displayed
3780 either as '\003' or as `^C' where the '\\' and '^'
3781 can be defined in the display table. Fill
3782 IT->ctl_chars with glyphs for what we have to
3783 display. Then, set IT->dpvec to these glyphs. */
3784 GLYPH g;
3786 if (it->c < 128 && it->ctl_arrow_p)
3788 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3789 if (it->dp
3790 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3791 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3792 g = XINT (DISP_CTRL_GLYPH (it->dp));
3793 else
3794 g = FAST_MAKE_GLYPH ('^', 0);
3795 XSETINT (it->ctl_chars[0], g);
3797 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3798 XSETINT (it->ctl_chars[1], g);
3800 /* Set up IT->dpvec and return first character from it. */
3801 it->dpvec_char_len = it->len;
3802 it->dpvec = it->ctl_chars;
3803 it->dpend = it->dpvec + 2;
3804 it->current.dpvec_index = 0;
3805 it->method = next_element_from_display_vector;
3806 get_next_display_element (it);
3808 else
3810 unsigned char str[MAX_MULTIBYTE_LENGTH];
3811 int len;
3812 int i;
3813 GLYPH escape_glyph;
3815 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3816 if (it->dp
3817 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3818 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3819 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3820 else
3821 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3823 if (SINGLE_BYTE_CHAR_P (it->c))
3824 str[0] = it->c, len = 1;
3825 else
3826 len = CHAR_STRING (it->c, str);
3828 for (i = 0; i < len; i++)
3830 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3831 /* Insert three more glyphs into IT->ctl_chars for
3832 the octal display of the character. */
3833 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3834 XSETINT (it->ctl_chars[i * 4 + 1], g);
3835 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3836 XSETINT (it->ctl_chars[i * 4 + 2], g);
3837 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3838 XSETINT (it->ctl_chars[i * 4 + 3], g);
3841 /* Set up IT->dpvec and return the first character
3842 from it. */
3843 it->dpvec_char_len = it->len;
3844 it->dpvec = it->ctl_chars;
3845 it->dpend = it->dpvec + len * 4;
3846 it->current.dpvec_index = 0;
3847 it->method = next_element_from_display_vector;
3848 get_next_display_element (it);
3853 /* Adjust face id for a multibyte character. There are no
3854 multibyte character in unibyte text. */
3855 if (it->multibyte_p
3856 && success_p
3857 && FRAME_WINDOW_P (it->f))
3859 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3860 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3864 /* Is this character the last one of a run of characters with
3865 box? If yes, set IT->end_of_box_run_p to 1. */
3866 if (it->face_box_p
3867 && it->s == NULL)
3869 int face_id;
3870 struct face *face;
3872 it->end_of_box_run_p
3873 = ((face_id = face_after_it_pos (it),
3874 face_id != it->face_id)
3875 && (face = FACE_FROM_ID (it->f, face_id),
3876 face->box == FACE_NO_BOX));
3879 /* Value is 0 if end of buffer or string reached. */
3880 return success_p;
3884 /* Move IT to the next display element.
3886 RESEAT_P non-zero means if called on a newline in buffer text,
3887 skip to the next visible line start.
3889 Functions get_next_display_element and set_iterator_to_next are
3890 separate because I find this arrangement easier to handle than a
3891 get_next_display_element function that also increments IT's
3892 position. The way it is we can first look at an iterator's current
3893 display element, decide whether it fits on a line, and if it does,
3894 increment the iterator position. The other way around we probably
3895 would either need a flag indicating whether the iterator has to be
3896 incremented the next time, or we would have to implement a
3897 decrement position function which would not be easy to write. */
3899 void
3900 set_iterator_to_next (it, reseat_p)
3901 struct it *it;
3902 int reseat_p;
3904 /* Reset flags indicating start and end of a sequence of characters
3905 with box. Reset them at the start of this function because
3906 moving the iterator to a new position might set them. */
3907 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3909 if (it->method == next_element_from_buffer)
3911 /* The current display element of IT is a character from
3912 current_buffer. Advance in the buffer, and maybe skip over
3913 invisible lines that are so because of selective display. */
3914 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3915 reseat_at_next_visible_line_start (it, 0);
3916 else
3918 xassert (it->len != 0);
3919 IT_BYTEPOS (*it) += it->len;
3920 IT_CHARPOS (*it) += 1;
3921 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3924 else if (it->method == next_element_from_composition)
3926 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3927 if (STRINGP (it->string))
3929 IT_STRING_BYTEPOS (*it) += it->len;
3930 IT_STRING_CHARPOS (*it) += it->cmp_len;
3931 it->method = next_element_from_string;
3932 goto consider_string_end;
3934 else
3936 IT_BYTEPOS (*it) += it->len;
3937 IT_CHARPOS (*it) += it->cmp_len;
3938 it->method = next_element_from_buffer;
3941 else if (it->method == next_element_from_c_string)
3943 /* Current display element of IT is from a C string. */
3944 IT_BYTEPOS (*it) += it->len;
3945 IT_CHARPOS (*it) += 1;
3947 else if (it->method == next_element_from_display_vector)
3949 /* Current display element of IT is from a display table entry.
3950 Advance in the display table definition. Reset it to null if
3951 end reached, and continue with characters from buffers/
3952 strings. */
3953 ++it->current.dpvec_index;
3955 /* Restore face of the iterator to what they were before the
3956 display vector entry (these entries may contain faces). */
3957 it->face_id = it->saved_face_id;
3959 if (it->dpvec + it->current.dpvec_index == it->dpend)
3961 if (it->s)
3962 it->method = next_element_from_c_string;
3963 else if (STRINGP (it->string))
3964 it->method = next_element_from_string;
3965 else
3966 it->method = next_element_from_buffer;
3968 it->dpvec = NULL;
3969 it->current.dpvec_index = -1;
3971 /* Skip over characters which were displayed via IT->dpvec. */
3972 if (it->dpvec_char_len < 0)
3973 reseat_at_next_visible_line_start (it, 1);
3974 else if (it->dpvec_char_len > 0)
3976 it->len = it->dpvec_char_len;
3977 set_iterator_to_next (it, reseat_p);
3981 else if (it->method == next_element_from_string)
3983 /* Current display element is a character from a Lisp string. */
3984 xassert (it->s == NULL && STRINGP (it->string));
3985 IT_STRING_BYTEPOS (*it) += it->len;
3986 IT_STRING_CHARPOS (*it) += 1;
3988 consider_string_end:
3990 if (it->current.overlay_string_index >= 0)
3992 /* IT->string is an overlay string. Advance to the
3993 next, if there is one. */
3994 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3995 next_overlay_string (it);
3997 else
3999 /* IT->string is not an overlay string. If we reached
4000 its end, and there is something on IT->stack, proceed
4001 with what is on the stack. This can be either another
4002 string, this time an overlay string, or a buffer. */
4003 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4004 && it->sp > 0)
4006 pop_it (it);
4007 if (!STRINGP (it->string))
4008 it->method = next_element_from_buffer;
4012 else if (it->method == next_element_from_image
4013 || it->method == next_element_from_stretch)
4015 /* The position etc with which we have to proceed are on
4016 the stack. The position may be at the end of a string,
4017 if the `display' property takes up the whole string. */
4018 pop_it (it);
4019 it->image_id = 0;
4020 if (STRINGP (it->string))
4022 it->method = next_element_from_string;
4023 goto consider_string_end;
4025 else
4026 it->method = next_element_from_buffer;
4028 else
4029 /* There are no other methods defined, so this should be a bug. */
4030 abort ();
4032 xassert (it->method != next_element_from_string
4033 || (STRINGP (it->string)
4034 && IT_STRING_CHARPOS (*it) >= 0));
4038 /* Load IT's display element fields with information about the next
4039 display element which comes from a display table entry or from the
4040 result of translating a control character to one of the forms `^C'
4041 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4043 static int
4044 next_element_from_display_vector (it)
4045 struct it *it;
4047 /* Precondition. */
4048 xassert (it->dpvec && it->current.dpvec_index >= 0);
4050 /* Remember the current face id in case glyphs specify faces.
4051 IT's face is restored in set_iterator_to_next. */
4052 it->saved_face_id = it->face_id;
4054 if (INTEGERP (*it->dpvec)
4055 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4057 int lface_id;
4058 GLYPH g;
4060 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4061 it->c = FAST_GLYPH_CHAR (g);
4062 it->len = CHAR_BYTES (it->c);
4064 /* The entry may contain a face id to use. Such a face id is
4065 the id of a Lisp face, not a realized face. A face id of
4066 zero means no face is specified. */
4067 lface_id = FAST_GLYPH_FACE (g);
4068 if (lface_id)
4070 /* The function returns -1 if lface_id is invalid. */
4071 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4072 if (face_id >= 0)
4073 it->face_id = face_id;
4076 else
4077 /* Display table entry is invalid. Return a space. */
4078 it->c = ' ', it->len = 1;
4080 /* Don't change position and object of the iterator here. They are
4081 still the values of the character that had this display table
4082 entry or was translated, and that's what we want. */
4083 it->what = IT_CHARACTER;
4084 return 1;
4088 /* Load IT with the next display element from Lisp string IT->string.
4089 IT->current.string_pos is the current position within the string.
4090 If IT->current.overlay_string_index >= 0, the Lisp string is an
4091 overlay string. */
4093 static int
4094 next_element_from_string (it)
4095 struct it *it;
4097 struct text_pos position;
4099 xassert (STRINGP (it->string));
4100 xassert (IT_STRING_CHARPOS (*it) >= 0);
4101 position = it->current.string_pos;
4103 /* Time to check for invisible text? */
4104 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4105 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4107 handle_stop (it);
4109 /* Since a handler may have changed IT->method, we must
4110 recurse here. */
4111 return get_next_display_element (it);
4114 if (it->current.overlay_string_index >= 0)
4116 /* Get the next character from an overlay string. In overlay
4117 strings, There is no field width or padding with spaces to
4118 do. */
4119 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4121 it->what = IT_EOB;
4122 return 0;
4124 else if (STRING_MULTIBYTE (it->string))
4126 int remaining = (STRING_BYTES (XSTRING (it->string))
4127 - IT_STRING_BYTEPOS (*it));
4128 unsigned char *s = (XSTRING (it->string)->data
4129 + IT_STRING_BYTEPOS (*it));
4130 it->c = string_char_and_length (s, remaining, &it->len);
4132 else
4134 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4135 it->len = 1;
4138 else
4140 /* Get the next character from a Lisp string that is not an
4141 overlay string. Such strings come from the mode line, for
4142 example. We may have to pad with spaces, or truncate the
4143 string. See also next_element_from_c_string. */
4144 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4146 it->what = IT_EOB;
4147 return 0;
4149 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4151 /* Pad with spaces. */
4152 it->c = ' ', it->len = 1;
4153 CHARPOS (position) = BYTEPOS (position) = -1;
4155 else if (STRING_MULTIBYTE (it->string))
4157 int maxlen = (STRING_BYTES (XSTRING (it->string))
4158 - IT_STRING_BYTEPOS (*it));
4159 unsigned char *s = (XSTRING (it->string)->data
4160 + IT_STRING_BYTEPOS (*it));
4161 it->c = string_char_and_length (s, maxlen, &it->len);
4163 else
4165 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4166 it->len = 1;
4170 /* Record what we have and where it came from. Note that we store a
4171 buffer position in IT->position although it could arguably be a
4172 string position. */
4173 it->what = IT_CHARACTER;
4174 it->object = it->string;
4175 it->position = position;
4176 return 1;
4180 /* Load IT with next display element from C string IT->s.
4181 IT->string_nchars is the maximum number of characters to return
4182 from the string. IT->end_charpos may be greater than
4183 IT->string_nchars when this function is called, in which case we
4184 may have to return padding spaces. Value is zero if end of string
4185 reached, including padding spaces. */
4187 static int
4188 next_element_from_c_string (it)
4189 struct it *it;
4191 int success_p = 1;
4193 xassert (it->s);
4194 it->what = IT_CHARACTER;
4195 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4196 it->object = Qnil;
4198 /* IT's position can be greater IT->string_nchars in case a field
4199 width or precision has been specified when the iterator was
4200 initialized. */
4201 if (IT_CHARPOS (*it) >= it->end_charpos)
4203 /* End of the game. */
4204 it->what = IT_EOB;
4205 success_p = 0;
4207 else if (IT_CHARPOS (*it) >= it->string_nchars)
4209 /* Pad with spaces. */
4210 it->c = ' ', it->len = 1;
4211 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4213 else if (it->multibyte_p)
4215 /* Implementation note: The calls to strlen apparently aren't a
4216 performance problem because there is no noticeable performance
4217 difference between Emacs running in unibyte or multibyte mode. */
4218 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4219 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4220 maxlen, &it->len);
4222 else
4223 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4225 return success_p;
4229 /* Set up IT to return characters from an ellipsis, if appropriate.
4230 The definition of the ellipsis glyphs may come from a display table
4231 entry. This function Fills IT with the first glyph from the
4232 ellipsis if an ellipsis is to be displayed. */
4234 static int
4235 next_element_from_ellipsis (it)
4236 struct it *it;
4238 if (it->selective_display_ellipsis_p)
4240 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4242 /* Use the display table definition for `...'. Invalid glyphs
4243 will be handled by the method returning elements from dpvec. */
4244 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4245 it->dpvec_char_len = it->len;
4246 it->dpvec = v->contents;
4247 it->dpend = v->contents + v->size;
4248 it->current.dpvec_index = 0;
4249 it->method = next_element_from_display_vector;
4251 else
4253 /* Use default `...' which is stored in default_invis_vector. */
4254 it->dpvec_char_len = it->len;
4255 it->dpvec = default_invis_vector;
4256 it->dpend = default_invis_vector + 3;
4257 it->current.dpvec_index = 0;
4258 it->method = next_element_from_display_vector;
4261 else
4263 it->method = next_element_from_buffer;
4264 reseat_at_next_visible_line_start (it, 1);
4267 return get_next_display_element (it);
4271 /* Deliver an image display element. The iterator IT is already
4272 filled with image information (done in handle_display_prop). Value
4273 is always 1. */
4276 static int
4277 next_element_from_image (it)
4278 struct it *it;
4280 it->what = IT_IMAGE;
4281 return 1;
4285 /* Fill iterator IT with next display element from a stretch glyph
4286 property. IT->object is the value of the text property. Value is
4287 always 1. */
4289 static int
4290 next_element_from_stretch (it)
4291 struct it *it;
4293 it->what = IT_STRETCH;
4294 return 1;
4298 /* Load IT with the next display element from current_buffer. Value
4299 is zero if end of buffer reached. IT->stop_charpos is the next
4300 position at which to stop and check for text properties or buffer
4301 end. */
4303 static int
4304 next_element_from_buffer (it)
4305 struct it *it;
4307 int success_p = 1;
4309 /* Check this assumption, otherwise, we would never enter the
4310 if-statement, below. */
4311 xassert (IT_CHARPOS (*it) >= BEGV
4312 && IT_CHARPOS (*it) <= it->stop_charpos);
4314 if (IT_CHARPOS (*it) >= it->stop_charpos)
4316 if (IT_CHARPOS (*it) >= it->end_charpos)
4318 int overlay_strings_follow_p;
4320 /* End of the game, except when overlay strings follow that
4321 haven't been returned yet. */
4322 if (it->overlay_strings_at_end_processed_p)
4323 overlay_strings_follow_p = 0;
4324 else
4326 it->overlay_strings_at_end_processed_p = 1;
4327 overlay_strings_follow_p = get_overlay_strings (it);
4330 if (overlay_strings_follow_p)
4331 success_p = get_next_display_element (it);
4332 else
4334 it->what = IT_EOB;
4335 it->position = it->current.pos;
4336 success_p = 0;
4339 else
4341 handle_stop (it);
4342 return get_next_display_element (it);
4345 else
4347 /* No face changes, overlays etc. in sight, so just return a
4348 character from current_buffer. */
4349 unsigned char *p;
4351 /* Maybe run the redisplay end trigger hook. Performance note:
4352 This doesn't seem to cost measurable time. */
4353 if (it->redisplay_end_trigger_charpos
4354 && it->glyph_row
4355 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4356 run_redisplay_end_trigger_hook (it);
4358 /* Get the next character, maybe multibyte. */
4359 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4360 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4362 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4363 - IT_BYTEPOS (*it));
4364 it->c = string_char_and_length (p, maxlen, &it->len);
4366 else
4367 it->c = *p, it->len = 1;
4369 /* Record what we have and where it came from. */
4370 it->what = IT_CHARACTER;;
4371 it->object = it->w->buffer;
4372 it->position = it->current.pos;
4374 /* Normally we return the character found above, except when we
4375 really want to return an ellipsis for selective display. */
4376 if (it->selective)
4378 if (it->c == '\n')
4380 /* A value of selective > 0 means hide lines indented more
4381 than that number of columns. */
4382 if (it->selective > 0
4383 && IT_CHARPOS (*it) + 1 < ZV
4384 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4385 IT_BYTEPOS (*it) + 1,
4386 it->selective))
4388 success_p = next_element_from_ellipsis (it);
4389 it->dpvec_char_len = -1;
4392 else if (it->c == '\r' && it->selective == -1)
4394 /* A value of selective == -1 means that everything from the
4395 CR to the end of the line is invisible, with maybe an
4396 ellipsis displayed for it. */
4397 success_p = next_element_from_ellipsis (it);
4398 it->dpvec_char_len = -1;
4403 /* Value is zero if end of buffer reached. */
4404 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4405 return success_p;
4409 /* Run the redisplay end trigger hook for IT. */
4411 static void
4412 run_redisplay_end_trigger_hook (it)
4413 struct it *it;
4415 Lisp_Object args[3];
4417 /* IT->glyph_row should be non-null, i.e. we should be actually
4418 displaying something, or otherwise we should not run the hook. */
4419 xassert (it->glyph_row);
4421 /* Set up hook arguments. */
4422 args[0] = Qredisplay_end_trigger_functions;
4423 args[1] = it->window;
4424 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4425 it->redisplay_end_trigger_charpos = 0;
4427 /* Since we are *trying* to run these functions, don't try to run
4428 them again, even if they get an error. */
4429 it->w->redisplay_end_trigger = Qnil;
4430 Frun_hook_with_args (3, args);
4432 /* Notice if it changed the face of the character we are on. */
4433 handle_face_prop (it);
4437 /* Deliver a composition display element. The iterator IT is already
4438 filled with composition information (done in
4439 handle_composition_prop). Value is always 1. */
4441 static int
4442 next_element_from_composition (it)
4443 struct it *it;
4445 it->what = IT_COMPOSITION;
4446 it->position = (STRINGP (it->string)
4447 ? it->current.string_pos
4448 : it->current.pos);
4449 return 1;
4454 /***********************************************************************
4455 Moving an iterator without producing glyphs
4456 ***********************************************************************/
4458 /* Move iterator IT to a specified buffer or X position within one
4459 line on the display without producing glyphs.
4461 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4462 whichever is reached first.
4464 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4466 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4467 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4468 TO_X includes the amount by which a window is horizontally
4469 scrolled.
4471 Value is
4473 MOVE_POS_MATCH_OR_ZV
4474 - when TO_POS or ZV was reached.
4476 MOVE_X_REACHED
4477 -when TO_X was reached before TO_POS or ZV were reached.
4479 MOVE_LINE_CONTINUED
4480 - when we reached the end of the display area and the line must
4481 be continued.
4483 MOVE_LINE_TRUNCATED
4484 - when we reached the end of the display area and the line is
4485 truncated.
4487 MOVE_NEWLINE_OR_CR
4488 - when we stopped at a line end, i.e. a newline or a CR and selective
4489 display is on. */
4491 static enum move_it_result
4492 move_it_in_display_line_to (it, to_charpos, to_x, op)
4493 struct it *it;
4494 int to_charpos, to_x, op;
4496 enum move_it_result result = MOVE_UNDEFINED;
4497 struct glyph_row *saved_glyph_row;
4499 /* Don't produce glyphs in produce_glyphs. */
4500 saved_glyph_row = it->glyph_row;
4501 it->glyph_row = NULL;
4503 while (1)
4505 int x, i, ascent = 0, descent = 0;
4507 /* Stop when ZV or TO_CHARPOS reached. */
4508 if (!get_next_display_element (it)
4509 || ((op & MOVE_TO_POS) != 0
4510 && BUFFERP (it->object)
4511 && IT_CHARPOS (*it) >= to_charpos))
4513 result = MOVE_POS_MATCH_OR_ZV;
4514 break;
4517 /* The call to produce_glyphs will get the metrics of the
4518 display element IT is loaded with. We record in x the
4519 x-position before this display element in case it does not
4520 fit on the line. */
4521 x = it->current_x;
4523 /* Remember the line height so far in case the next element doesn't
4524 fit on the line. */
4525 if (!it->truncate_lines_p)
4527 ascent = it->max_ascent;
4528 descent = it->max_descent;
4531 PRODUCE_GLYPHS (it);
4533 if (it->area != TEXT_AREA)
4535 set_iterator_to_next (it, 1);
4536 continue;
4539 /* The number of glyphs we get back in IT->nglyphs will normally
4540 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4541 character on a terminal frame, or (iii) a line end. For the
4542 second case, IT->nglyphs - 1 padding glyphs will be present
4543 (on X frames, there is only one glyph produced for a
4544 composite character.
4546 The behavior implemented below means, for continuation lines,
4547 that as many spaces of a TAB as fit on the current line are
4548 displayed there. For terminal frames, as many glyphs of a
4549 multi-glyph character are displayed in the current line, too.
4550 This is what the old redisplay code did, and we keep it that
4551 way. Under X, the whole shape of a complex character must
4552 fit on the line or it will be completely displayed in the
4553 next line.
4555 Note that both for tabs and padding glyphs, all glyphs have
4556 the same width. */
4557 if (it->nglyphs)
4559 /* More than one glyph or glyph doesn't fit on line. All
4560 glyphs have the same width. */
4561 int single_glyph_width = it->pixel_width / it->nglyphs;
4562 int new_x;
4564 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4566 new_x = x + single_glyph_width;
4568 /* We want to leave anything reaching TO_X to the caller. */
4569 if ((op & MOVE_TO_X) && new_x > to_x)
4571 it->current_x = x;
4572 result = MOVE_X_REACHED;
4573 break;
4575 else if (/* Lines are continued. */
4576 !it->truncate_lines_p
4577 && (/* And glyph doesn't fit on the line. */
4578 new_x > it->last_visible_x
4579 /* Or it fits exactly and we're on a window
4580 system frame. */
4581 || (new_x == it->last_visible_x
4582 && FRAME_WINDOW_P (it->f))))
4584 if (/* IT->hpos == 0 means the very first glyph
4585 doesn't fit on the line, e.g. a wide image. */
4586 it->hpos == 0
4587 || (new_x == it->last_visible_x
4588 && FRAME_WINDOW_P (it->f)))
4590 ++it->hpos;
4591 it->current_x = new_x;
4592 if (i == it->nglyphs - 1)
4593 set_iterator_to_next (it, 1);
4595 else
4597 it->current_x = x;
4598 it->max_ascent = ascent;
4599 it->max_descent = descent;
4602 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4603 IT_CHARPOS (*it)));
4604 result = MOVE_LINE_CONTINUED;
4605 break;
4607 else if (new_x > it->first_visible_x)
4609 /* Glyph is visible. Increment number of glyphs that
4610 would be displayed. */
4611 ++it->hpos;
4613 else
4615 /* Glyph is completely off the left margin of the display
4616 area. Nothing to do. */
4620 if (result != MOVE_UNDEFINED)
4621 break;
4623 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4625 /* Stop when TO_X specified and reached. This check is
4626 necessary here because of lines consisting of a line end,
4627 only. The line end will not produce any glyphs and we
4628 would never get MOVE_X_REACHED. */
4629 xassert (it->nglyphs == 0);
4630 result = MOVE_X_REACHED;
4631 break;
4634 /* Is this a line end? If yes, we're done. */
4635 if (ITERATOR_AT_END_OF_LINE_P (it))
4637 result = MOVE_NEWLINE_OR_CR;
4638 break;
4641 /* The current display element has been consumed. Advance
4642 to the next. */
4643 set_iterator_to_next (it, 1);
4645 /* Stop if lines are truncated and IT's current x-position is
4646 past the right edge of the window now. */
4647 if (it->truncate_lines_p
4648 && it->current_x >= it->last_visible_x)
4650 result = MOVE_LINE_TRUNCATED;
4651 break;
4655 /* Restore the iterator settings altered at the beginning of this
4656 function. */
4657 it->glyph_row = saved_glyph_row;
4658 return result;
4662 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4663 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4664 the description of enum move_operation_enum.
4666 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4667 screen line, this function will set IT to the next position >
4668 TO_CHARPOS. */
4670 void
4671 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4672 struct it *it;
4673 int to_charpos, to_x, to_y, to_vpos;
4674 int op;
4676 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4677 int line_height;
4678 int reached = 0;
4680 for (;;)
4682 if (op & MOVE_TO_VPOS)
4684 /* If no TO_CHARPOS and no TO_X specified, stop at the
4685 start of the line TO_VPOS. */
4686 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4688 if (it->vpos == to_vpos)
4690 reached = 1;
4691 break;
4693 else
4694 skip = move_it_in_display_line_to (it, -1, -1, 0);
4696 else
4698 /* TO_VPOS >= 0 means stop at TO_X in the line at
4699 TO_VPOS, or at TO_POS, whichever comes first. */
4700 if (it->vpos == to_vpos)
4702 reached = 2;
4703 break;
4706 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4708 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4710 reached = 3;
4711 break;
4713 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4715 /* We have reached TO_X but not in the line we want. */
4716 skip = move_it_in_display_line_to (it, to_charpos,
4717 -1, MOVE_TO_POS);
4718 if (skip == MOVE_POS_MATCH_OR_ZV)
4720 reached = 4;
4721 break;
4726 else if (op & MOVE_TO_Y)
4728 struct it it_backup;
4730 /* TO_Y specified means stop at TO_X in the line containing
4731 TO_Y---or at TO_CHARPOS if this is reached first. The
4732 problem is that we can't really tell whether the line
4733 contains TO_Y before we have completely scanned it, and
4734 this may skip past TO_X. What we do is to first scan to
4735 TO_X.
4737 If TO_X is not specified, use a TO_X of zero. The reason
4738 is to make the outcome of this function more predictable.
4739 If we didn't use TO_X == 0, we would stop at the end of
4740 the line which is probably not what a caller would expect
4741 to happen. */
4742 skip = move_it_in_display_line_to (it, to_charpos,
4743 ((op & MOVE_TO_X)
4744 ? to_x : 0),
4745 (MOVE_TO_X
4746 | (op & MOVE_TO_POS)));
4748 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4749 if (skip == MOVE_POS_MATCH_OR_ZV)
4751 reached = 5;
4752 break;
4755 /* If TO_X was reached, we would like to know whether TO_Y
4756 is in the line. This can only be said if we know the
4757 total line height which requires us to scan the rest of
4758 the line. */
4759 if (skip == MOVE_X_REACHED)
4761 it_backup = *it;
4762 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4763 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4764 op & MOVE_TO_POS);
4765 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4768 /* Now, decide whether TO_Y is in this line. */
4769 line_height = it->max_ascent + it->max_descent;
4770 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4772 if (to_y >= it->current_y
4773 && to_y < it->current_y + line_height)
4775 if (skip == MOVE_X_REACHED)
4776 /* If TO_Y is in this line and TO_X was reached above,
4777 we scanned too far. We have to restore IT's settings
4778 to the ones before skipping. */
4779 *it = it_backup;
4780 reached = 6;
4782 else if (skip == MOVE_X_REACHED)
4784 skip = skip2;
4785 if (skip == MOVE_POS_MATCH_OR_ZV)
4786 reached = 7;
4789 if (reached)
4790 break;
4792 else
4793 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4795 switch (skip)
4797 case MOVE_POS_MATCH_OR_ZV:
4798 reached = 8;
4799 goto out;
4801 case MOVE_NEWLINE_OR_CR:
4802 set_iterator_to_next (it, 1);
4803 it->continuation_lines_width = 0;
4804 break;
4806 case MOVE_LINE_TRUNCATED:
4807 it->continuation_lines_width = 0;
4808 reseat_at_next_visible_line_start (it, 0);
4809 if ((op & MOVE_TO_POS) != 0
4810 && IT_CHARPOS (*it) > to_charpos)
4812 reached = 9;
4813 goto out;
4815 break;
4817 case MOVE_LINE_CONTINUED:
4818 it->continuation_lines_width += it->current_x;
4819 break;
4821 default:
4822 abort ();
4825 /* Reset/increment for the next run. */
4826 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4827 it->current_x = it->hpos = 0;
4828 it->current_y += it->max_ascent + it->max_descent;
4829 ++it->vpos;
4830 last_height = it->max_ascent + it->max_descent;
4831 last_max_ascent = it->max_ascent;
4832 it->max_ascent = it->max_descent = 0;
4835 out:
4837 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4841 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4843 If DY > 0, move IT backward at least that many pixels. DY = 0
4844 means move IT backward to the preceding line start or BEGV. This
4845 function may move over more than DY pixels if IT->current_y - DY
4846 ends up in the middle of a line; in this case IT->current_y will be
4847 set to the top of the line moved to. */
4849 void
4850 move_it_vertically_backward (it, dy)
4851 struct it *it;
4852 int dy;
4854 int nlines, h, line_height;
4855 struct it it2;
4856 int start_pos = IT_CHARPOS (*it);
4858 xassert (dy >= 0);
4860 /* Estimate how many newlines we must move back. */
4861 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4863 /* Set the iterator's position that many lines back. */
4864 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4865 back_to_previous_visible_line_start (it);
4867 /* Reseat the iterator here. When moving backward, we don't want
4868 reseat to skip forward over invisible text, set up the iterator
4869 to deliver from overlay strings at the new position etc. So,
4870 use reseat_1 here. */
4871 reseat_1 (it, it->current.pos, 1);
4873 /* We are now surely at a line start. */
4874 it->current_x = it->hpos = 0;
4876 /* Move forward and see what y-distance we moved. First move to the
4877 start of the next line so that we get its height. We need this
4878 height to be able to tell whether we reached the specified
4879 y-distance. */
4880 it2 = *it;
4881 it2.max_ascent = it2.max_descent = 0;
4882 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4883 MOVE_TO_POS | MOVE_TO_VPOS);
4884 xassert (IT_CHARPOS (*it) >= BEGV);
4885 line_height = it2.max_ascent + it2.max_descent;
4887 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4888 xassert (IT_CHARPOS (*it) >= BEGV);
4889 h = it2.current_y - it->current_y;
4890 nlines = it2.vpos - it->vpos;
4892 /* Correct IT's y and vpos position. */
4893 it->vpos -= nlines;
4894 it->current_y -= h;
4896 if (dy == 0)
4898 /* DY == 0 means move to the start of the screen line. The
4899 value of nlines is > 0 if continuation lines were involved. */
4900 if (nlines > 0)
4901 move_it_by_lines (it, nlines, 1);
4902 xassert (IT_CHARPOS (*it) <= start_pos);
4904 else if (nlines)
4906 /* The y-position we try to reach. Note that h has been
4907 subtracted in front of the if-statement. */
4908 int target_y = it->current_y + h - dy;
4910 /* If we did not reach target_y, try to move further backward if
4911 we can. If we moved too far backward, try to move forward. */
4912 if (target_y < it->current_y
4913 && IT_CHARPOS (*it) > BEGV)
4915 move_it_vertically (it, target_y - it->current_y);
4916 xassert (IT_CHARPOS (*it) >= BEGV);
4918 else if (target_y >= it->current_y + line_height
4919 && IT_CHARPOS (*it) < ZV)
4921 move_it_vertically (it, target_y - (it->current_y + line_height));
4922 xassert (IT_CHARPOS (*it) >= BEGV);
4928 /* Move IT by a specified amount of pixel lines DY. DY negative means
4929 move backwards. DY = 0 means move to start of screen line. At the
4930 end, IT will be on the start of a screen line. */
4932 void
4933 move_it_vertically (it, dy)
4934 struct it *it;
4935 int dy;
4937 if (dy <= 0)
4938 move_it_vertically_backward (it, -dy);
4939 else if (dy > 0)
4941 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4942 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4943 MOVE_TO_POS | MOVE_TO_Y);
4944 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4946 /* If buffer ends in ZV without a newline, move to the start of
4947 the line to satisfy the post-condition. */
4948 if (IT_CHARPOS (*it) == ZV
4949 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4950 move_it_by_lines (it, 0, 0);
4955 /* Return non-zero if some text between buffer positions START_CHARPOS
4956 and END_CHARPOS is invisible. IT->window is the window for text
4957 property lookup. */
4959 static int
4960 invisible_text_between_p (it, start_charpos, end_charpos)
4961 struct it *it;
4962 int start_charpos, end_charpos;
4964 Lisp_Object prop, limit;
4965 int invisible_found_p;
4967 xassert (it != NULL && start_charpos <= end_charpos);
4969 /* Is text at START invisible? */
4970 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4971 it->window);
4972 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4973 invisible_found_p = 1;
4974 else
4976 limit = Fnext_single_char_property_change (make_number (start_charpos),
4977 Qinvisible, Qnil,
4978 make_number (end_charpos));
4979 invisible_found_p = XFASTINT (limit) < end_charpos;
4982 return invisible_found_p;
4986 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4987 negative means move up. DVPOS == 0 means move to the start of the
4988 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4989 NEED_Y_P is zero, IT->current_y will be left unchanged.
4991 Further optimization ideas: If we would know that IT->f doesn't use
4992 a face with proportional font, we could be faster for
4993 truncate-lines nil. */
4995 void
4996 move_it_by_lines (it, dvpos, need_y_p)
4997 struct it *it;
4998 int dvpos, need_y_p;
5000 struct position pos;
5002 if (!FRAME_WINDOW_P (it->f))
5004 struct text_pos textpos;
5006 /* We can use vmotion on frames without proportional fonts. */
5007 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5008 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5009 reseat (it, textpos, 1);
5010 it->vpos += pos.vpos;
5011 it->current_y += pos.vpos;
5013 else if (dvpos == 0)
5015 /* DVPOS == 0 means move to the start of the screen line. */
5016 move_it_vertically_backward (it, 0);
5017 xassert (it->current_x == 0 && it->hpos == 0);
5019 else if (dvpos > 0)
5021 /* If there are no continuation lines, and if there is no
5022 selective display, try the simple method of moving forward
5023 DVPOS newlines, then see where we are. */
5024 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5026 int shortage = 0, charpos;
5028 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
5029 charpos = IT_CHARPOS (*it) + 1;
5030 else
5031 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5032 &shortage, 0);
5034 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5036 struct text_pos pos;
5037 CHARPOS (pos) = charpos;
5038 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5039 reseat (it, pos, 1);
5040 it->vpos += dvpos - shortage;
5041 it->hpos = it->current_x = 0;
5042 return;
5046 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5048 else
5050 struct it it2;
5051 int start_charpos, i;
5053 /* If there are no continuation lines, and if there is no
5054 selective display, try the simple method of moving backward
5055 -DVPOS newlines. */
5056 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5058 int shortage;
5059 int charpos = IT_CHARPOS (*it);
5060 int bytepos = IT_BYTEPOS (*it);
5062 /* If in the middle of a line, go to its start. */
5063 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5065 charpos = find_next_newline_no_quit (charpos, -1);
5066 bytepos = CHAR_TO_BYTE (charpos);
5069 if (charpos == BEGV)
5071 struct text_pos pos;
5072 CHARPOS (pos) = charpos;
5073 BYTEPOS (pos) = bytepos;
5074 reseat (it, pos, 1);
5075 it->hpos = it->current_x = 0;
5076 return;
5078 else
5080 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5081 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5083 struct text_pos pos;
5084 CHARPOS (pos) = charpos;
5085 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5086 reseat (it, pos, 1);
5087 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5088 it->hpos = it->current_x = 0;
5089 return;
5094 /* Go back -DVPOS visible lines and reseat the iterator there. */
5095 start_charpos = IT_CHARPOS (*it);
5096 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5097 back_to_previous_visible_line_start (it);
5098 reseat (it, it->current.pos, 1);
5099 it->current_x = it->hpos = 0;
5101 /* Above call may have moved too far if continuation lines
5102 are involved. Scan forward and see if it did. */
5103 it2 = *it;
5104 it2.vpos = it2.current_y = 0;
5105 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5106 it->vpos -= it2.vpos;
5107 it->current_y -= it2.current_y;
5108 it->current_x = it->hpos = 0;
5110 /* If we moved too far, move IT some lines forward. */
5111 if (it2.vpos > -dvpos)
5113 int delta = it2.vpos + dvpos;
5114 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5121 /***********************************************************************
5122 Messages
5123 ***********************************************************************/
5126 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5127 to *Messages*. */
5129 void
5130 add_to_log (format, arg1, arg2)
5131 char *format;
5132 Lisp_Object arg1, arg2;
5134 Lisp_Object args[3];
5135 Lisp_Object msg, fmt;
5136 char *buffer;
5137 int len;
5138 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5140 fmt = msg = Qnil;
5141 GCPRO4 (fmt, msg, arg1, arg2);
5143 args[0] = fmt = build_string (format);
5144 args[1] = arg1;
5145 args[2] = arg2;
5146 msg = Fformat (3, args);
5148 len = STRING_BYTES (XSTRING (msg)) + 1;
5149 buffer = (char *) alloca (len);
5150 strcpy (buffer, XSTRING (msg)->data);
5152 message_dolog (buffer, len - 1, 1, 0);
5153 UNGCPRO;
5157 /* Output a newline in the *Messages* buffer if "needs" one. */
5159 void
5160 message_log_maybe_newline ()
5162 if (message_log_need_newline)
5163 message_dolog ("", 0, 1, 0);
5167 /* Add a string M of length LEN to the message log, optionally
5168 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5169 nonzero, means interpret the contents of M as multibyte. This
5170 function calls low-level routines in order to bypass text property
5171 hooks, etc. which might not be safe to run. */
5173 void
5174 message_dolog (m, len, nlflag, multibyte)
5175 char *m;
5176 int len, nlflag, multibyte;
5178 if (!NILP (Vmessage_log_max))
5180 struct buffer *oldbuf;
5181 Lisp_Object oldpoint, oldbegv, oldzv;
5182 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5183 int point_at_end = 0;
5184 int zv_at_end = 0;
5185 Lisp_Object old_deactivate_mark, tem;
5186 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5188 old_deactivate_mark = Vdeactivate_mark;
5189 oldbuf = current_buffer;
5190 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5191 current_buffer->undo_list = Qt;
5193 oldpoint = Fpoint_marker ();
5194 oldbegv = Fpoint_min_marker ();
5195 oldzv = Fpoint_max_marker ();
5196 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5198 if (PT == Z)
5199 point_at_end = 1;
5200 if (ZV == Z)
5201 zv_at_end = 1;
5203 BEGV = BEG;
5204 BEGV_BYTE = BEG_BYTE;
5205 ZV = Z;
5206 ZV_BYTE = Z_BYTE;
5207 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5209 /* Insert the string--maybe converting multibyte to single byte
5210 or vice versa, so that all the text fits the buffer. */
5211 if (multibyte
5212 && NILP (current_buffer->enable_multibyte_characters))
5214 int i, c, nbytes;
5215 unsigned char work[1];
5217 /* Convert a multibyte string to single-byte
5218 for the *Message* buffer. */
5219 for (i = 0; i < len; i += nbytes)
5221 c = string_char_and_length (m + i, len - i, &nbytes);
5222 work[0] = (SINGLE_BYTE_CHAR_P (c)
5224 : multibyte_char_to_unibyte (c, Qnil));
5225 insert_1_both (work, 1, 1, 1, 0, 0);
5228 else if (! multibyte
5229 && ! NILP (current_buffer->enable_multibyte_characters))
5231 int i, c, nbytes;
5232 unsigned char *msg = (unsigned char *) m;
5233 unsigned char str[MAX_MULTIBYTE_LENGTH];
5234 /* Convert a single-byte string to multibyte
5235 for the *Message* buffer. */
5236 for (i = 0; i < len; i++)
5238 c = unibyte_char_to_multibyte (msg[i]);
5239 nbytes = CHAR_STRING (c, str);
5240 insert_1_both (str, 1, nbytes, 1, 0, 0);
5243 else if (len)
5244 insert_1 (m, len, 1, 0, 0);
5246 if (nlflag)
5248 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5249 insert_1 ("\n", 1, 1, 0, 0);
5251 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5252 this_bol = PT;
5253 this_bol_byte = PT_BYTE;
5255 if (this_bol > BEG)
5257 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5258 prev_bol = PT;
5259 prev_bol_byte = PT_BYTE;
5261 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5262 this_bol, this_bol_byte);
5263 if (dup)
5265 del_range_both (prev_bol, prev_bol_byte,
5266 this_bol, this_bol_byte, 0);
5267 if (dup > 1)
5269 char dupstr[40];
5270 int duplen;
5272 /* If you change this format, don't forget to also
5273 change message_log_check_duplicate. */
5274 sprintf (dupstr, " [%d times]", dup);
5275 duplen = strlen (dupstr);
5276 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5277 insert_1 (dupstr, duplen, 1, 0, 1);
5282 if (NATNUMP (Vmessage_log_max))
5284 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5285 -XFASTINT (Vmessage_log_max) - 1, 0);
5286 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5289 BEGV = XMARKER (oldbegv)->charpos;
5290 BEGV_BYTE = marker_byte_position (oldbegv);
5292 if (zv_at_end)
5294 ZV = Z;
5295 ZV_BYTE = Z_BYTE;
5297 else
5299 ZV = XMARKER (oldzv)->charpos;
5300 ZV_BYTE = marker_byte_position (oldzv);
5303 if (point_at_end)
5304 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5305 else
5306 /* We can't do Fgoto_char (oldpoint) because it will run some
5307 Lisp code. */
5308 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5309 XMARKER (oldpoint)->bytepos);
5311 UNGCPRO;
5312 free_marker (oldpoint);
5313 free_marker (oldbegv);
5314 free_marker (oldzv);
5316 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5317 set_buffer_internal (oldbuf);
5318 if (NILP (tem))
5319 windows_or_buffers_changed = old_windows_or_buffers_changed;
5320 message_log_need_newline = !nlflag;
5321 Vdeactivate_mark = old_deactivate_mark;
5326 /* We are at the end of the buffer after just having inserted a newline.
5327 (Note: We depend on the fact we won't be crossing the gap.)
5328 Check to see if the most recent message looks a lot like the previous one.
5329 Return 0 if different, 1 if the new one should just replace it, or a
5330 value N > 1 if we should also append " [N times]". */
5332 static int
5333 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5334 int prev_bol, this_bol;
5335 int prev_bol_byte, this_bol_byte;
5337 int i;
5338 int len = Z_BYTE - 1 - this_bol_byte;
5339 int seen_dots = 0;
5340 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5341 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5343 for (i = 0; i < len; i++)
5345 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5346 && p1[i] != '\n')
5347 seen_dots = 1;
5348 if (p1[i] != p2[i])
5349 return seen_dots;
5351 p1 += len;
5352 if (*p1 == '\n')
5353 return 2;
5354 if (*p1++ == ' ' && *p1++ == '[')
5356 int n = 0;
5357 while (*p1 >= '0' && *p1 <= '9')
5358 n = n * 10 + *p1++ - '0';
5359 if (strncmp (p1, " times]\n", 8) == 0)
5360 return n+1;
5362 return 0;
5366 /* Display an echo area message M with a specified length of LEN
5367 chars. The string may include null characters. If M is 0, clear
5368 out any existing message, and let the mini-buffer text show through.
5370 The buffer M must continue to exist until after the echo area gets
5371 cleared or some other message gets displayed there. This means do
5372 not pass text that is stored in a Lisp string; do not pass text in
5373 a buffer that was alloca'd. */
5375 void
5376 message2 (m, len, multibyte)
5377 char *m;
5378 int len;
5379 int multibyte;
5381 /* First flush out any partial line written with print. */
5382 message_log_maybe_newline ();
5383 if (m)
5384 message_dolog (m, len, 1, multibyte);
5385 message2_nolog (m, len, multibyte);
5389 /* The non-logging counterpart of message2. */
5391 void
5392 message2_nolog (m, len, multibyte)
5393 char *m;
5394 int len;
5396 struct frame *sf = SELECTED_FRAME ();
5397 message_enable_multibyte = multibyte;
5399 if (noninteractive)
5401 if (noninteractive_need_newline)
5402 putc ('\n', stderr);
5403 noninteractive_need_newline = 0;
5404 if (m)
5405 fwrite (m, len, 1, stderr);
5406 if (cursor_in_echo_area == 0)
5407 fprintf (stderr, "\n");
5408 fflush (stderr);
5410 /* A null message buffer means that the frame hasn't really been
5411 initialized yet. Error messages get reported properly by
5412 cmd_error, so this must be just an informative message; toss it. */
5413 else if (INTERACTIVE
5414 && sf->glyphs_initialized_p
5415 && FRAME_MESSAGE_BUF (sf))
5417 Lisp_Object mini_window;
5418 struct frame *f;
5420 /* Get the frame containing the mini-buffer
5421 that the selected frame is using. */
5422 mini_window = FRAME_MINIBUF_WINDOW (sf);
5423 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5425 FRAME_SAMPLE_VISIBILITY (f);
5426 if (FRAME_VISIBLE_P (sf)
5427 && ! FRAME_VISIBLE_P (f))
5428 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5430 if (m)
5432 set_message (m, Qnil, len, multibyte);
5433 if (minibuffer_auto_raise)
5434 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5436 else
5437 clear_message (1, 1);
5439 do_pending_window_change (0);
5440 echo_area_display (1);
5441 do_pending_window_change (0);
5442 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5443 (*frame_up_to_date_hook) (f);
5448 /* Display an echo area message M with a specified length of NBYTES
5449 bytes. The string may include null characters. If M is not a
5450 string, clear out any existing message, and let the mini-buffer
5451 text show through. */
5453 void
5454 message3 (m, nbytes, multibyte)
5455 Lisp_Object m;
5456 int nbytes;
5457 int multibyte;
5459 struct gcpro gcpro1;
5461 GCPRO1 (m);
5463 /* First flush out any partial line written with print. */
5464 message_log_maybe_newline ();
5465 if (STRINGP (m))
5466 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5467 message3_nolog (m, nbytes, multibyte);
5469 UNGCPRO;
5473 /* The non-logging version of message3. */
5475 void
5476 message3_nolog (m, nbytes, multibyte)
5477 Lisp_Object m;
5478 int nbytes, multibyte;
5480 struct frame *sf = SELECTED_FRAME ();
5481 message_enable_multibyte = multibyte;
5483 if (noninteractive)
5485 if (noninteractive_need_newline)
5486 putc ('\n', stderr);
5487 noninteractive_need_newline = 0;
5488 if (STRINGP (m))
5489 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5490 if (cursor_in_echo_area == 0)
5491 fprintf (stderr, "\n");
5492 fflush (stderr);
5494 /* A null message buffer means that the frame hasn't really been
5495 initialized yet. Error messages get reported properly by
5496 cmd_error, so this must be just an informative message; toss it. */
5497 else if (INTERACTIVE
5498 && sf->glyphs_initialized_p
5499 && FRAME_MESSAGE_BUF (sf))
5501 Lisp_Object mini_window;
5502 Lisp_Object frame;
5503 struct frame *f;
5505 /* Get the frame containing the mini-buffer
5506 that the selected frame is using. */
5507 mini_window = FRAME_MINIBUF_WINDOW (sf);
5508 frame = XWINDOW (mini_window)->frame;
5509 f = XFRAME (frame);
5511 FRAME_SAMPLE_VISIBILITY (f);
5512 if (FRAME_VISIBLE_P (sf)
5513 && !FRAME_VISIBLE_P (f))
5514 Fmake_frame_visible (frame);
5516 if (STRINGP (m) && XSTRING (m)->size)
5518 set_message (NULL, m, nbytes, multibyte);
5519 if (minibuffer_auto_raise)
5520 Fraise_frame (frame);
5522 else
5523 clear_message (1, 1);
5525 do_pending_window_change (0);
5526 echo_area_display (1);
5527 do_pending_window_change (0);
5528 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5529 (*frame_up_to_date_hook) (f);
5534 /* Display a null-terminated echo area message M. If M is 0, clear
5535 out any existing message, and let the mini-buffer text show through.
5537 The buffer M must continue to exist until after the echo area gets
5538 cleared or some other message gets displayed there. Do not pass
5539 text that is stored in a Lisp string. Do not pass text in a buffer
5540 that was alloca'd. */
5542 void
5543 message1 (m)
5544 char *m;
5546 message2 (m, (m ? strlen (m) : 0), 0);
5550 /* The non-logging counterpart of message1. */
5552 void
5553 message1_nolog (m)
5554 char *m;
5556 message2_nolog (m, (m ? strlen (m) : 0), 0);
5559 /* Display a message M which contains a single %s
5560 which gets replaced with STRING. */
5562 void
5563 message_with_string (m, string, log)
5564 char *m;
5565 Lisp_Object string;
5566 int log;
5568 if (noninteractive)
5570 if (m)
5572 if (noninteractive_need_newline)
5573 putc ('\n', stderr);
5574 noninteractive_need_newline = 0;
5575 fprintf (stderr, m, XSTRING (string)->data);
5576 if (cursor_in_echo_area == 0)
5577 fprintf (stderr, "\n");
5578 fflush (stderr);
5581 else if (INTERACTIVE)
5583 /* The frame whose minibuffer we're going to display the message on.
5584 It may be larger than the selected frame, so we need
5585 to use its buffer, not the selected frame's buffer. */
5586 Lisp_Object mini_window;
5587 struct frame *f, *sf = SELECTED_FRAME ();
5589 /* Get the frame containing the minibuffer
5590 that the selected frame is using. */
5591 mini_window = FRAME_MINIBUF_WINDOW (sf);
5592 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5594 /* A null message buffer means that the frame hasn't really been
5595 initialized yet. Error messages get reported properly by
5596 cmd_error, so this must be just an informative message; toss it. */
5597 if (FRAME_MESSAGE_BUF (f))
5599 int len;
5600 char *a[1];
5601 a[0] = (char *) XSTRING (string)->data;
5603 len = doprnt (FRAME_MESSAGE_BUF (f),
5604 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5606 if (log)
5607 message2 (FRAME_MESSAGE_BUF (f), len,
5608 STRING_MULTIBYTE (string));
5609 else
5610 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5611 STRING_MULTIBYTE (string));
5613 /* Print should start at the beginning of the message
5614 buffer next time. */
5615 message_buf_print = 0;
5621 /* Dump an informative message to the minibuf. If M is 0, clear out
5622 any existing message, and let the mini-buffer text show through. */
5624 /* VARARGS 1 */
5625 void
5626 message (m, a1, a2, a3)
5627 char *m;
5628 EMACS_INT a1, a2, a3;
5630 if (noninteractive)
5632 if (m)
5634 if (noninteractive_need_newline)
5635 putc ('\n', stderr);
5636 noninteractive_need_newline = 0;
5637 fprintf (stderr, m, a1, a2, a3);
5638 if (cursor_in_echo_area == 0)
5639 fprintf (stderr, "\n");
5640 fflush (stderr);
5643 else if (INTERACTIVE)
5645 /* The frame whose mini-buffer we're going to display the message
5646 on. It may be larger than the selected frame, so we need to
5647 use its buffer, not the selected frame's buffer. */
5648 Lisp_Object mini_window;
5649 struct frame *f, *sf = SELECTED_FRAME ();
5651 /* Get the frame containing the mini-buffer
5652 that the selected frame is using. */
5653 mini_window = FRAME_MINIBUF_WINDOW (sf);
5654 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5656 /* A null message buffer means that the frame hasn't really been
5657 initialized yet. Error messages get reported properly by
5658 cmd_error, so this must be just an informative message; toss
5659 it. */
5660 if (FRAME_MESSAGE_BUF (f))
5662 if (m)
5664 int len;
5665 #ifdef NO_ARG_ARRAY
5666 char *a[3];
5667 a[0] = (char *) a1;
5668 a[1] = (char *) a2;
5669 a[2] = (char *) a3;
5671 len = doprnt (FRAME_MESSAGE_BUF (f),
5672 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5673 #else
5674 len = doprnt (FRAME_MESSAGE_BUF (f),
5675 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5676 (char **) &a1);
5677 #endif /* NO_ARG_ARRAY */
5679 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5681 else
5682 message1 (0);
5684 /* Print should start at the beginning of the message
5685 buffer next time. */
5686 message_buf_print = 0;
5692 /* The non-logging version of message. */
5694 void
5695 message_nolog (m, a1, a2, a3)
5696 char *m;
5697 EMACS_INT a1, a2, a3;
5699 Lisp_Object old_log_max;
5700 old_log_max = Vmessage_log_max;
5701 Vmessage_log_max = Qnil;
5702 message (m, a1, a2, a3);
5703 Vmessage_log_max = old_log_max;
5707 /* Display the current message in the current mini-buffer. This is
5708 only called from error handlers in process.c, and is not time
5709 critical. */
5711 void
5712 update_echo_area ()
5714 if (!NILP (echo_area_buffer[0]))
5716 Lisp_Object string;
5717 string = Fcurrent_message ();
5718 message3 (string, XSTRING (string)->size,
5719 !NILP (current_buffer->enable_multibyte_characters));
5724 /* Make sure echo area buffers in echo_buffers[] are life. If they
5725 aren't, make new ones. */
5727 static void
5728 ensure_echo_area_buffers ()
5730 int i;
5732 for (i = 0; i < 2; ++i)
5733 if (!BUFFERP (echo_buffer[i])
5734 || NILP (XBUFFER (echo_buffer[i])->name))
5736 char name[30];
5737 Lisp_Object old_buffer;
5738 int j;
5740 old_buffer = echo_buffer[i];
5741 sprintf (name, " *Echo Area %d*", i);
5742 echo_buffer[i] = Fget_buffer_create (build_string (name));
5743 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5745 for (j = 0; j < 2; ++j)
5746 if (EQ (old_buffer, echo_area_buffer[j]))
5747 echo_area_buffer[j] = echo_buffer[i];
5752 /* Call FN with args A1..A4 with either the current or last displayed
5753 echo_area_buffer as current buffer.
5755 WHICH zero means use the current message buffer
5756 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5757 from echo_buffer[] and clear it.
5759 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5760 suitable buffer from echo_buffer[] and clear it.
5762 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5763 that the current message becomes the last displayed one, make
5764 choose a suitable buffer for echo_area_buffer[0], and clear it.
5766 Value is what FN returns. */
5768 static int
5769 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5770 struct window *w;
5771 int which;
5772 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5773 EMACS_INT a1;
5774 Lisp_Object a2;
5775 EMACS_INT a3, a4;
5777 Lisp_Object buffer;
5778 int this_one, the_other, clear_buffer_p, rc;
5779 int count = specpdl_ptr - specpdl;
5781 /* If buffers aren't life, make new ones. */
5782 ensure_echo_area_buffers ();
5784 clear_buffer_p = 0;
5786 if (which == 0)
5787 this_one = 0, the_other = 1;
5788 else if (which > 0)
5789 this_one = 1, the_other = 0;
5790 else
5792 this_one = 0, the_other = 1;
5793 clear_buffer_p = 1;
5795 /* We need a fresh one in case the current echo buffer equals
5796 the one containing the last displayed echo area message. */
5797 if (!NILP (echo_area_buffer[this_one])
5798 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5799 echo_area_buffer[this_one] = Qnil;
5802 /* Choose a suitable buffer from echo_buffer[] is we don't
5803 have one. */
5804 if (NILP (echo_area_buffer[this_one]))
5806 echo_area_buffer[this_one]
5807 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5808 ? echo_buffer[the_other]
5809 : echo_buffer[this_one]);
5810 clear_buffer_p = 1;
5813 buffer = echo_area_buffer[this_one];
5815 record_unwind_protect (unwind_with_echo_area_buffer,
5816 with_echo_area_buffer_unwind_data (w));
5818 /* Make the echo area buffer current. Note that for display
5819 purposes, it is not necessary that the displayed window's buffer
5820 == current_buffer, except for text property lookup. So, let's
5821 only set that buffer temporarily here without doing a full
5822 Fset_window_buffer. We must also change w->pointm, though,
5823 because otherwise an assertions in unshow_buffer fails, and Emacs
5824 aborts. */
5825 set_buffer_internal_1 (XBUFFER (buffer));
5826 if (w)
5828 w->buffer = buffer;
5829 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5832 current_buffer->undo_list = Qt;
5833 current_buffer->read_only = Qnil;
5835 if (clear_buffer_p && Z > BEG)
5836 del_range (BEG, Z);
5838 xassert (BEGV >= BEG);
5839 xassert (ZV <= Z && ZV >= BEGV);
5841 rc = fn (a1, a2, a3, a4);
5843 xassert (BEGV >= BEG);
5844 xassert (ZV <= Z && ZV >= BEGV);
5846 unbind_to (count, Qnil);
5847 return rc;
5851 /* Save state that should be preserved around the call to the function
5852 FN called in with_echo_area_buffer. */
5854 static Lisp_Object
5855 with_echo_area_buffer_unwind_data (w)
5856 struct window *w;
5858 int i = 0;
5859 Lisp_Object vector;
5861 /* Reduce consing by keeping one vector in
5862 Vwith_echo_area_save_vector. */
5863 vector = Vwith_echo_area_save_vector;
5864 Vwith_echo_area_save_vector = Qnil;
5866 if (NILP (vector))
5867 vector = Fmake_vector (make_number (7), Qnil);
5869 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5870 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5871 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5873 if (w)
5875 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5876 XVECTOR (vector)->contents[i++] = w->buffer;
5877 XVECTOR (vector)->contents[i++]
5878 = make_number (XMARKER (w->pointm)->charpos);
5879 XVECTOR (vector)->contents[i++]
5880 = make_number (XMARKER (w->pointm)->bytepos);
5882 else
5884 int end = i + 4;
5885 while (i < end)
5886 XVECTOR (vector)->contents[i++] = Qnil;
5889 xassert (i == XVECTOR (vector)->size);
5890 return vector;
5894 /* Restore global state from VECTOR which was created by
5895 with_echo_area_buffer_unwind_data. */
5897 static Lisp_Object
5898 unwind_with_echo_area_buffer (vector)
5899 Lisp_Object vector;
5901 int i = 0;
5903 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5904 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5905 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5907 if (WINDOWP (XVECTOR (vector)->contents[i]))
5909 struct window *w;
5910 Lisp_Object buffer, charpos, bytepos;
5912 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5913 buffer = XVECTOR (vector)->contents[i]; ++i;
5914 charpos = XVECTOR (vector)->contents[i]; ++i;
5915 bytepos = XVECTOR (vector)->contents[i]; ++i;
5917 w->buffer = buffer;
5918 set_marker_both (w->pointm, buffer,
5919 XFASTINT (charpos), XFASTINT (bytepos));
5922 Vwith_echo_area_save_vector = vector;
5923 return Qnil;
5927 /* Set up the echo area for use by print functions. MULTIBYTE_P
5928 non-zero means we will print multibyte. */
5930 void
5931 setup_echo_area_for_printing (multibyte_p)
5932 int multibyte_p;
5934 ensure_echo_area_buffers ();
5936 if (!message_buf_print)
5938 /* A message has been output since the last time we printed.
5939 Choose a fresh echo area buffer. */
5940 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5941 echo_area_buffer[0] = echo_buffer[1];
5942 else
5943 echo_area_buffer[0] = echo_buffer[0];
5945 /* Switch to that buffer and clear it. */
5946 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5947 if (Z > BEG)
5948 del_range (BEG, Z);
5949 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5951 /* Set up the buffer for the multibyteness we need. */
5952 if (multibyte_p
5953 != !NILP (current_buffer->enable_multibyte_characters))
5954 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5956 /* Raise the frame containing the echo area. */
5957 if (minibuffer_auto_raise)
5959 struct frame *sf = SELECTED_FRAME ();
5960 Lisp_Object mini_window;
5961 mini_window = FRAME_MINIBUF_WINDOW (sf);
5962 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5965 message_log_maybe_newline ();
5966 message_buf_print = 1;
5968 else
5970 if (NILP (echo_area_buffer[0]))
5972 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5973 echo_area_buffer[0] = echo_buffer[1];
5974 else
5975 echo_area_buffer[0] = echo_buffer[0];
5978 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5979 /* Someone switched buffers between print requests. */
5980 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5985 /* Display an echo area message in window W. Value is non-zero if W's
5986 height is changed. If display_last_displayed_message_p is
5987 non-zero, display the message that was last displayed, otherwise
5988 display the current message. */
5990 static int
5991 display_echo_area (w)
5992 struct window *w;
5994 int i, no_message_p, window_height_changed_p, count;
5996 /* Temporarily disable garbage collections while displaying the echo
5997 area. This is done because a GC can print a message itself.
5998 That message would modify the echo area buffer's contents while a
5999 redisplay of the buffer is going on, and seriously confuse
6000 redisplay. */
6001 count = inhibit_garbage_collection ();
6003 /* If there is no message, we must call display_echo_area_1
6004 nevertheless because it resizes the window. But we will have to
6005 reset the echo_area_buffer in question to nil at the end because
6006 with_echo_area_buffer will sets it to an empty buffer. */
6007 i = display_last_displayed_message_p ? 1 : 0;
6008 no_message_p = NILP (echo_area_buffer[i]);
6010 window_height_changed_p
6011 = with_echo_area_buffer (w, display_last_displayed_message_p,
6012 display_echo_area_1,
6013 (EMACS_INT) w, Qnil, 0, 0);
6015 if (no_message_p)
6016 echo_area_buffer[i] = Qnil;
6018 unbind_to (count, Qnil);
6019 return window_height_changed_p;
6023 /* Helper for display_echo_area. Display the current buffer which
6024 contains the current echo area message in window W, a mini-window,
6025 a pointer to which is passed in A1. A2..A4 are currently not used.
6026 Change the height of W so that all of the message is displayed.
6027 Value is non-zero if height of W was changed. */
6029 static int
6030 display_echo_area_1 (a1, a2, a3, a4)
6031 EMACS_INT a1;
6032 Lisp_Object a2;
6033 EMACS_INT a3, a4;
6035 struct window *w = (struct window *) a1;
6036 Lisp_Object window;
6037 struct text_pos start;
6038 int window_height_changed_p = 0;
6040 /* Do this before displaying, so that we have a large enough glyph
6041 matrix for the display. */
6042 window_height_changed_p = resize_mini_window (w, 0);
6044 /* Display. */
6045 clear_glyph_matrix (w->desired_matrix);
6046 XSETWINDOW (window, w);
6047 SET_TEXT_POS (start, BEG, BEG_BYTE);
6048 try_window (window, start);
6050 return window_height_changed_p;
6054 /* Resize the echo area window to exactly the size needed for the
6055 currently displayed message, if there is one. */
6057 void
6058 resize_echo_area_axactly ()
6060 if (BUFFERP (echo_area_buffer[0])
6061 && WINDOWP (echo_area_window))
6063 struct window *w = XWINDOW (echo_area_window);
6064 int resized_p;
6066 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6067 (EMACS_INT) w, Qnil, 0, 0);
6068 if (resized_p)
6070 ++windows_or_buffers_changed;
6071 ++update_mode_lines;
6072 redisplay_internal (0);
6078 /* Callback function for with_echo_area_buffer, when used from
6079 resize_echo_area_axactly. A1 contains a pointer to the window to
6080 resize, A2 to A4 are not used. Value is what resize_mini_window
6081 returns. */
6083 static int
6084 resize_mini_window_1 (a1, a2, a3, a4)
6085 EMACS_INT a1;
6086 Lisp_Object a2;
6087 EMACS_INT a3, a4;
6089 return resize_mini_window ((struct window *) a1, 1);
6093 /* Resize mini-window W to fit the size of its contents. EXACT:P
6094 means size the window exactly to the size needed. Otherwise, it's
6095 only enlarged until W's buffer is empty. Value is non-zero if
6096 the window height has been changed. */
6099 resize_mini_window (w, exact_p)
6100 struct window *w;
6101 int exact_p;
6103 struct frame *f = XFRAME (w->frame);
6104 int window_height_changed_p = 0;
6106 xassert (MINI_WINDOW_P (w));
6108 /* Nil means don't try to resize. */
6109 if (NILP (Vresize_mini_windows)
6110 || (FRAME_X_P (f) && f->output_data.x == NULL))
6111 return 0;
6113 if (!FRAME_MINIBUF_ONLY_P (f))
6115 struct it it;
6116 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6117 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6118 int height, max_height;
6119 int unit = CANON_Y_UNIT (f);
6120 struct text_pos start;
6122 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6124 /* Compute the max. number of lines specified by the user. */
6125 if (FLOATP (Vmax_mini_window_height))
6126 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6127 else if (INTEGERP (Vmax_mini_window_height))
6128 max_height = XINT (Vmax_mini_window_height);
6129 else
6130 max_height = total_height / 4;
6132 /* Correct that max. height if it's bogus. */
6133 max_height = max (1, max_height);
6134 max_height = min (total_height, max_height);
6136 /* Find out the height of the text in the window. */
6137 if (it.truncate_lines_p)
6138 height = 1;
6139 else
6141 last_height = 0;
6142 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6143 if (it.max_ascent == 0 && it.max_descent == 0)
6144 height = it.current_y + last_height;
6145 else
6146 height = it.current_y + it.max_ascent + it.max_descent;
6147 height -= it.extra_line_spacing;
6148 height = (height + unit - 1) / unit;
6151 /* Compute a suitable window start. */
6152 if (height > max_height)
6154 height = max_height;
6155 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6156 move_it_vertically_backward (&it, (height - 1) * unit);
6157 start = it.current.pos;
6159 else
6160 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6161 SET_MARKER_FROM_TEXT_POS (w->start, start);
6163 if (EQ (Vresize_mini_windows, Qgrow_only))
6165 /* Let it grow only, until we display an empty message, in which
6166 case the window shrinks again. */
6167 if (height > XFASTINT (w->height))
6169 int old_height = XFASTINT (w->height);
6170 freeze_window_starts (f, 1);
6171 grow_mini_window (w, height - XFASTINT (w->height));
6172 window_height_changed_p = XFASTINT (w->height) != old_height;
6174 else if (height < XFASTINT (w->height)
6175 && (exact_p || BEGV == ZV))
6177 int old_height = XFASTINT (w->height);
6178 freeze_window_starts (f, 0);
6179 shrink_mini_window (w);
6180 window_height_changed_p = XFASTINT (w->height) != old_height;
6183 else
6185 /* Always resize to exact size needed. */
6186 if (height > XFASTINT (w->height))
6188 int old_height = XFASTINT (w->height);
6189 freeze_window_starts (f, 1);
6190 grow_mini_window (w, height - XFASTINT (w->height));
6191 window_height_changed_p = XFASTINT (w->height) != old_height;
6193 else if (height < XFASTINT (w->height))
6195 int old_height = XFASTINT (w->height);
6196 freeze_window_starts (f, 0);
6197 shrink_mini_window (w);
6199 if (height)
6201 freeze_window_starts (f, 1);
6202 grow_mini_window (w, height - XFASTINT (w->height));
6205 window_height_changed_p = XFASTINT (w->height) != old_height;
6210 return window_height_changed_p;
6214 /* Value is the current message, a string, or nil if there is no
6215 current message. */
6217 Lisp_Object
6218 current_message ()
6220 Lisp_Object msg;
6222 if (NILP (echo_area_buffer[0]))
6223 msg = Qnil;
6224 else
6226 with_echo_area_buffer (0, 0, current_message_1,
6227 (EMACS_INT) &msg, Qnil, 0, 0);
6228 if (NILP (msg))
6229 echo_area_buffer[0] = Qnil;
6232 return msg;
6236 static int
6237 current_message_1 (a1, a2, a3, a4)
6238 EMACS_INT a1;
6239 Lisp_Object a2;
6240 EMACS_INT a3, a4;
6242 Lisp_Object *msg = (Lisp_Object *) a1;
6244 if (Z > BEG)
6245 *msg = make_buffer_string (BEG, Z, 1);
6246 else
6247 *msg = Qnil;
6248 return 0;
6252 /* Push the current message on Vmessage_stack for later restauration
6253 by restore_message. Value is non-zero if the current message isn't
6254 empty. This is a relatively infrequent operation, so it's not
6255 worth optimizing. */
6258 push_message ()
6260 Lisp_Object msg;
6261 msg = current_message ();
6262 Vmessage_stack = Fcons (msg, Vmessage_stack);
6263 return STRINGP (msg);
6267 /* Restore message display from the top of Vmessage_stack. */
6269 void
6270 restore_message ()
6272 Lisp_Object msg;
6274 xassert (CONSP (Vmessage_stack));
6275 msg = XCAR (Vmessage_stack);
6276 if (STRINGP (msg))
6277 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6278 else
6279 message3_nolog (msg, 0, 0);
6283 /* Pop the top-most entry off Vmessage_stack. */
6285 void
6286 pop_message ()
6288 xassert (CONSP (Vmessage_stack));
6289 Vmessage_stack = XCDR (Vmessage_stack);
6293 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6294 exits. If the stack is not empty, we have a missing pop_message
6295 somewhere. */
6297 void
6298 check_message_stack ()
6300 if (!NILP (Vmessage_stack))
6301 abort ();
6305 /* Truncate to NCHARS what will be displayed in the echo area the next
6306 time we display it---but don't redisplay it now. */
6308 void
6309 truncate_echo_area (nchars)
6310 int nchars;
6312 if (nchars == 0)
6313 echo_area_buffer[0] = Qnil;
6314 /* A null message buffer means that the frame hasn't really been
6315 initialized yet. Error messages get reported properly by
6316 cmd_error, so this must be just an informative message; toss it. */
6317 else if (!noninteractive
6318 && INTERACTIVE
6319 && !NILP (echo_area_buffer[0]))
6321 struct frame *sf = SELECTED_FRAME ();
6322 if (FRAME_MESSAGE_BUF (sf))
6323 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6328 /* Helper function for truncate_echo_area. Truncate the current
6329 message to at most NCHARS characters. */
6331 static int
6332 truncate_message_1 (nchars, a2, a3, a4)
6333 EMACS_INT nchars;
6334 Lisp_Object a2;
6335 EMACS_INT a3, a4;
6337 if (BEG + nchars < Z)
6338 del_range (BEG + nchars, Z);
6339 if (Z == BEG)
6340 echo_area_buffer[0] = Qnil;
6341 return 0;
6345 /* Set the current message to a substring of S or STRING.
6347 If STRING is a Lisp string, set the message to the first NBYTES
6348 bytes from STRING. NBYTES zero means use the whole string. If
6349 STRING is multibyte, the message will be displayed multibyte.
6351 If S is not null, set the message to the first LEN bytes of S. LEN
6352 zero means use the whole string. MULTIBYTE_P non-zero means S is
6353 multibyte. Display the message multibyte in that case. */
6355 void
6356 set_message (s, string, nbytes, multibyte_p)
6357 char *s;
6358 Lisp_Object string;
6359 int nbytes;
6361 message_enable_multibyte
6362 = ((s && multibyte_p)
6363 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6365 with_echo_area_buffer (0, -1, set_message_1,
6366 (EMACS_INT) s, string, nbytes, multibyte_p);
6367 message_buf_print = 0;
6368 help_echo_showing_p = 0;
6372 /* Helper function for set_message. Arguments have the same meaning
6373 as there, with A1 corresponding to S and A2 corresponding to STRING
6374 This function is called with the echo area buffer being
6375 current. */
6377 static int
6378 set_message_1 (a1, a2, nbytes, multibyte_p)
6379 EMACS_INT a1;
6380 Lisp_Object a2;
6381 EMACS_INT nbytes, multibyte_p;
6383 char *s = (char *) a1;
6384 Lisp_Object string = a2;
6386 xassert (BEG == Z);
6388 /* Change multibyteness of the echo buffer appropriately. */
6389 if (message_enable_multibyte
6390 != !NILP (current_buffer->enable_multibyte_characters))
6391 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6393 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6395 /* Insert new message at BEG. */
6396 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6398 if (STRINGP (string))
6400 int nchars;
6402 if (nbytes == 0)
6403 nbytes = XSTRING (string)->size_byte;
6404 nchars = string_byte_to_char (string, nbytes);
6406 /* This function takes care of single/multibyte conversion. We
6407 just have to ensure that the echo area buffer has the right
6408 setting of enable_multibyte_characters. */
6409 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6411 else if (s)
6413 if (nbytes == 0)
6414 nbytes = strlen (s);
6416 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6418 /* Convert from multi-byte to single-byte. */
6419 int i, c, n;
6420 unsigned char work[1];
6422 /* Convert a multibyte string to single-byte. */
6423 for (i = 0; i < nbytes; i += n)
6425 c = string_char_and_length (s + i, nbytes - i, &n);
6426 work[0] = (SINGLE_BYTE_CHAR_P (c)
6428 : multibyte_char_to_unibyte (c, Qnil));
6429 insert_1_both (work, 1, 1, 1, 0, 0);
6432 else if (!multibyte_p
6433 && !NILP (current_buffer->enable_multibyte_characters))
6435 /* Convert from single-byte to multi-byte. */
6436 int i, c, n;
6437 unsigned char *msg = (unsigned char *) s;
6438 unsigned char str[MAX_MULTIBYTE_LENGTH];
6440 /* Convert a single-byte string to multibyte. */
6441 for (i = 0; i < nbytes; i++)
6443 c = unibyte_char_to_multibyte (msg[i]);
6444 n = CHAR_STRING (c, str);
6445 insert_1_both (str, 1, n, 1, 0, 0);
6448 else
6449 insert_1 (s, nbytes, 1, 0, 0);
6452 return 0;
6456 /* Clear messages. CURRENT_P non-zero means clear the current
6457 message. LAST_DISPLAYED_P non-zero means clear the message
6458 last displayed. */
6460 void
6461 clear_message (current_p, last_displayed_p)
6462 int current_p, last_displayed_p;
6464 if (current_p)
6465 echo_area_buffer[0] = Qnil;
6467 if (last_displayed_p)
6468 echo_area_buffer[1] = Qnil;
6470 message_buf_print = 0;
6473 /* Clear garbaged frames.
6475 This function is used where the old redisplay called
6476 redraw_garbaged_frames which in turn called redraw_frame which in
6477 turn called clear_frame. The call to clear_frame was a source of
6478 flickering. I believe a clear_frame is not necessary. It should
6479 suffice in the new redisplay to invalidate all current matrices,
6480 and ensure a complete redisplay of all windows. */
6482 static void
6483 clear_garbaged_frames ()
6485 if (frame_garbaged)
6487 Lisp_Object tail, frame;
6489 FOR_EACH_FRAME (tail, frame)
6491 struct frame *f = XFRAME (frame);
6493 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6495 clear_current_matrices (f);
6496 f->garbaged = 0;
6500 frame_garbaged = 0;
6501 ++windows_or_buffers_changed;
6506 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6507 is non-zero update selected_frame. Value is non-zero if the
6508 mini-windows height has been changed. */
6510 static int
6511 echo_area_display (update_frame_p)
6512 int update_frame_p;
6514 Lisp_Object mini_window;
6515 struct window *w;
6516 struct frame *f;
6517 int window_height_changed_p = 0;
6518 struct frame *sf = SELECTED_FRAME ();
6520 mini_window = FRAME_MINIBUF_WINDOW (sf);
6521 w = XWINDOW (mini_window);
6522 f = XFRAME (WINDOW_FRAME (w));
6524 /* Don't display if frame is invisible or not yet initialized. */
6525 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6526 return 0;
6528 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6529 #ifndef macintosh
6530 #ifdef HAVE_WINDOW_SYSTEM
6531 /* When Emacs starts, selected_frame may be a visible terminal
6532 frame, even if we run under a window system. If we let this
6533 through, a message would be displayed on the terminal. */
6534 if (EQ (selected_frame, Vterminal_frame)
6535 && !NILP (Vwindow_system))
6536 return 0;
6537 #endif /* HAVE_WINDOW_SYSTEM */
6538 #endif
6540 /* Redraw garbaged frames. */
6541 if (frame_garbaged)
6542 clear_garbaged_frames ();
6544 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6546 echo_area_window = mini_window;
6547 window_height_changed_p = display_echo_area (w);
6548 w->must_be_updated_p = 1;
6550 /* Update the display, unless called from redisplay_internal.
6551 Also don't update the screen during redisplay itself. The
6552 update will happen at the end of redisplay, and an update
6553 here could cause confusion. */
6554 if (update_frame_p && !redisplaying_p)
6556 int n = 0;
6558 /* If the display update has been interrupted by pending
6559 input, update mode lines in the frame. Due to the
6560 pending input, it might have been that redisplay hasn't
6561 been called, so that mode lines above the echo area are
6562 garbaged. This looks odd, so we prevent it here. */
6563 if (!display_completed)
6564 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6566 if (window_height_changed_p)
6568 /* Must update other windows. */
6569 windows_or_buffers_changed = 1;
6570 redisplay_internal (0);
6572 else if (FRAME_WINDOW_P (f) && n == 0)
6574 /* Window configuration is the same as before.
6575 Can do with a display update of the echo area,
6576 unless we displayed some mode lines. */
6577 update_single_window (w, 1);
6578 rif->flush_display (f);
6580 else
6581 update_frame (f, 1, 1);
6584 else if (!EQ (mini_window, selected_window))
6585 windows_or_buffers_changed++;
6587 /* Last displayed message is now the current message. */
6588 echo_area_buffer[1] = echo_area_buffer[0];
6590 /* Prevent redisplay optimization in redisplay_internal by resetting
6591 this_line_start_pos. This is done because the mini-buffer now
6592 displays the message instead of its buffer text. */
6593 if (EQ (mini_window, selected_window))
6594 CHARPOS (this_line_start_pos) = 0;
6596 return window_height_changed_p;
6601 /***********************************************************************
6602 Frame Titles
6603 ***********************************************************************/
6606 #ifdef HAVE_WINDOW_SYSTEM
6608 /* A buffer for constructing frame titles in it; allocated from the
6609 heap in init_xdisp and resized as needed in store_frame_title_char. */
6611 static char *frame_title_buf;
6613 /* The buffer's end, and a current output position in it. */
6615 static char *frame_title_buf_end;
6616 static char *frame_title_ptr;
6619 /* Store a single character C for the frame title in frame_title_buf.
6620 Re-allocate frame_title_buf if necessary. */
6622 static void
6623 store_frame_title_char (c)
6624 char c;
6626 /* If output position has reached the end of the allocated buffer,
6627 double the buffer's size. */
6628 if (frame_title_ptr == frame_title_buf_end)
6630 int len = frame_title_ptr - frame_title_buf;
6631 int new_size = 2 * len * sizeof *frame_title_buf;
6632 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6633 frame_title_buf_end = frame_title_buf + new_size;
6634 frame_title_ptr = frame_title_buf + len;
6637 *frame_title_ptr++ = c;
6641 /* Store part of a frame title in frame_title_buf, beginning at
6642 frame_title_ptr. STR is the string to store. Do not copy more
6643 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6644 the whole string. Pad with spaces until FIELD_WIDTH number of
6645 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6646 Called from display_mode_element when it is used to build a frame
6647 title. */
6649 static int
6650 store_frame_title (str, field_width, precision)
6651 unsigned char *str;
6652 int field_width, precision;
6654 int n = 0;
6656 /* Copy at most PRECISION chars from STR. */
6657 while ((precision <= 0 || n < precision)
6658 && *str)
6660 store_frame_title_char (*str++);
6661 ++n;
6664 /* Fill up with spaces until FIELD_WIDTH reached. */
6665 while (field_width > 0
6666 && n < field_width)
6668 store_frame_title_char (' ');
6669 ++n;
6672 return n;
6676 /* Set the title of FRAME, if it has changed. The title format is
6677 Vicon_title_format if FRAME is iconified, otherwise it is
6678 frame_title_format. */
6680 static void
6681 x_consider_frame_title (frame)
6682 Lisp_Object frame;
6684 struct frame *f = XFRAME (frame);
6686 if (FRAME_WINDOW_P (f)
6687 || FRAME_MINIBUF_ONLY_P (f)
6688 || f->explicit_name)
6690 /* Do we have more than one visible frame on this X display? */
6691 Lisp_Object tail;
6692 Lisp_Object fmt;
6693 struct buffer *obuf;
6694 int len;
6695 struct it it;
6697 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6699 struct frame *tf = XFRAME (XCAR (tail));
6701 if (tf != f
6702 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6703 && !FRAME_MINIBUF_ONLY_P (tf)
6704 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6705 break;
6708 /* Set global variable indicating that multiple frames exist. */
6709 multiple_frames = CONSP (tail);
6711 /* Switch to the buffer of selected window of the frame. Set up
6712 frame_title_ptr so that display_mode_element will output into it;
6713 then display the title. */
6714 obuf = current_buffer;
6715 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6716 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6717 frame_title_ptr = frame_title_buf;
6718 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6719 NULL, DEFAULT_FACE_ID);
6720 len = display_mode_element (&it, 0, -1, -1, fmt);
6721 frame_title_ptr = NULL;
6722 set_buffer_internal (obuf);
6724 /* Set the title only if it's changed. This avoids consing in
6725 the common case where it hasn't. (If it turns out that we've
6726 already wasted too much time by walking through the list with
6727 display_mode_element, then we might need to optimize at a
6728 higher level than this.) */
6729 if (! STRINGP (f->name)
6730 || STRING_BYTES (XSTRING (f->name)) != len
6731 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6732 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6736 #else /* not HAVE_WINDOW_SYSTEM */
6738 #define frame_title_ptr ((char *)0)
6739 #define store_frame_title(str, mincol, maxcol) 0
6741 #endif /* not HAVE_WINDOW_SYSTEM */
6746 /***********************************************************************
6747 Menu Bars
6748 ***********************************************************************/
6751 /* Prepare for redisplay by updating menu-bar item lists when
6752 appropriate. This can call eval. */
6754 void
6755 prepare_menu_bars ()
6757 int all_windows;
6758 struct gcpro gcpro1, gcpro2;
6759 struct frame *f;
6760 struct frame *tooltip_frame;
6762 #ifdef HAVE_X_WINDOWS
6763 tooltip_frame = tip_frame;
6764 #else
6765 tooltip_frame = NULL;
6766 #endif
6768 /* Update all frame titles based on their buffer names, etc. We do
6769 this before the menu bars so that the buffer-menu will show the
6770 up-to-date frame titles. */
6771 #ifdef HAVE_WINDOW_SYSTEM
6772 if (windows_or_buffers_changed || update_mode_lines)
6774 Lisp_Object tail, frame;
6776 FOR_EACH_FRAME (tail, frame)
6778 f = XFRAME (frame);
6779 if (f != tooltip_frame
6780 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6781 x_consider_frame_title (frame);
6784 #endif /* HAVE_WINDOW_SYSTEM */
6786 /* Update the menu bar item lists, if appropriate. This has to be
6787 done before any actual redisplay or generation of display lines. */
6788 all_windows = (update_mode_lines
6789 || buffer_shared > 1
6790 || windows_or_buffers_changed);
6791 if (all_windows)
6793 Lisp_Object tail, frame;
6794 int count = specpdl_ptr - specpdl;
6796 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6798 FOR_EACH_FRAME (tail, frame)
6800 f = XFRAME (frame);
6802 /* Ignore tooltip frame. */
6803 if (f == tooltip_frame)
6804 continue;
6806 /* If a window on this frame changed size, report that to
6807 the user and clear the size-change flag. */
6808 if (FRAME_WINDOW_SIZES_CHANGED (f))
6810 Lisp_Object functions;
6812 /* Clear flag first in case we get an error below. */
6813 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6814 functions = Vwindow_size_change_functions;
6815 GCPRO2 (tail, functions);
6817 while (CONSP (functions))
6819 call1 (XCAR (functions), frame);
6820 functions = XCDR (functions);
6822 UNGCPRO;
6825 GCPRO1 (tail);
6826 update_menu_bar (f, 0);
6827 #ifdef HAVE_WINDOW_SYSTEM
6828 update_tool_bar (f, 0);
6829 #endif
6830 UNGCPRO;
6833 unbind_to (count, Qnil);
6835 else
6837 struct frame *sf = SELECTED_FRAME ();
6838 update_menu_bar (sf, 1);
6839 #ifdef HAVE_WINDOW_SYSTEM
6840 update_tool_bar (sf, 1);
6841 #endif
6844 /* Motif needs this. See comment in xmenu.c. Turn it off when
6845 pending_menu_activation is not defined. */
6846 #ifdef USE_X_TOOLKIT
6847 pending_menu_activation = 0;
6848 #endif
6852 /* Update the menu bar item list for frame F. This has to be done
6853 before we start to fill in any display lines, because it can call
6854 eval.
6856 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6858 static void
6859 update_menu_bar (f, save_match_data)
6860 struct frame *f;
6861 int save_match_data;
6863 Lisp_Object window;
6864 register struct window *w;
6866 window = FRAME_SELECTED_WINDOW (f);
6867 w = XWINDOW (window);
6869 if (update_mode_lines)
6870 w->update_mode_line = Qt;
6872 if (FRAME_WINDOW_P (f)
6874 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6875 FRAME_EXTERNAL_MENU_BAR (f)
6876 #else
6877 FRAME_MENU_BAR_LINES (f) > 0
6878 #endif
6879 : FRAME_MENU_BAR_LINES (f) > 0)
6881 /* If the user has switched buffers or windows, we need to
6882 recompute to reflect the new bindings. But we'll
6883 recompute when update_mode_lines is set too; that means
6884 that people can use force-mode-line-update to request
6885 that the menu bar be recomputed. The adverse effect on
6886 the rest of the redisplay algorithm is about the same as
6887 windows_or_buffers_changed anyway. */
6888 if (windows_or_buffers_changed
6889 || !NILP (w->update_mode_line)
6890 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6891 < BUF_MODIFF (XBUFFER (w->buffer)))
6892 != !NILP (w->last_had_star))
6893 || ((!NILP (Vtransient_mark_mode)
6894 && !NILP (XBUFFER (w->buffer)->mark_active))
6895 != !NILP (w->region_showing)))
6897 struct buffer *prev = current_buffer;
6898 int count = specpdl_ptr - specpdl;
6900 set_buffer_internal_1 (XBUFFER (w->buffer));
6901 if (save_match_data)
6902 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6903 if (NILP (Voverriding_local_map_menu_flag))
6905 specbind (Qoverriding_terminal_local_map, Qnil);
6906 specbind (Qoverriding_local_map, Qnil);
6909 /* Run the Lucid hook. */
6910 call1 (Vrun_hooks, Qactivate_menubar_hook);
6912 /* If it has changed current-menubar from previous value,
6913 really recompute the menu-bar from the value. */
6914 if (! NILP (Vlucid_menu_bar_dirty_flag))
6915 call0 (Qrecompute_lucid_menubar);
6917 safe_run_hooks (Qmenu_bar_update_hook);
6918 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6920 /* Redisplay the menu bar in case we changed it. */
6921 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6922 if (FRAME_WINDOW_P (f)
6923 #if defined (macintosh)
6924 /* All frames on Mac OS share the same menubar. So only the
6925 selected frame should be allowed to set it. */
6926 && f == SELECTED_FRAME ()
6927 #endif
6929 set_frame_menubar (f, 0, 0);
6930 else
6931 /* On a terminal screen, the menu bar is an ordinary screen
6932 line, and this makes it get updated. */
6933 w->update_mode_line = Qt;
6934 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6935 /* In the non-toolkit version, the menu bar is an ordinary screen
6936 line, and this makes it get updated. */
6937 w->update_mode_line = Qt;
6938 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6940 unbind_to (count, Qnil);
6941 set_buffer_internal_1 (prev);
6948 /***********************************************************************
6949 Tool-bars
6950 ***********************************************************************/
6952 #ifdef HAVE_WINDOW_SYSTEM
6954 /* Update the tool-bar item list for frame F. This has to be done
6955 before we start to fill in any display lines. Called from
6956 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6957 and restore it here. */
6959 static void
6960 update_tool_bar (f, save_match_data)
6961 struct frame *f;
6962 int save_match_data;
6964 if (WINDOWP (f->tool_bar_window)
6965 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6967 Lisp_Object window;
6968 struct window *w;
6970 window = FRAME_SELECTED_WINDOW (f);
6971 w = XWINDOW (window);
6973 /* If the user has switched buffers or windows, we need to
6974 recompute to reflect the new bindings. But we'll
6975 recompute when update_mode_lines is set too; that means
6976 that people can use force-mode-line-update to request
6977 that the menu bar be recomputed. The adverse effect on
6978 the rest of the redisplay algorithm is about the same as
6979 windows_or_buffers_changed anyway. */
6980 if (windows_or_buffers_changed
6981 || !NILP (w->update_mode_line)
6982 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6983 < BUF_MODIFF (XBUFFER (w->buffer)))
6984 != !NILP (w->last_had_star))
6985 || ((!NILP (Vtransient_mark_mode)
6986 && !NILP (XBUFFER (w->buffer)->mark_active))
6987 != !NILP (w->region_showing)))
6989 struct buffer *prev = current_buffer;
6990 int count = specpdl_ptr - specpdl;
6992 /* Set current_buffer to the buffer of the selected
6993 window of the frame, so that we get the right local
6994 keymaps. */
6995 set_buffer_internal_1 (XBUFFER (w->buffer));
6997 /* Save match data, if we must. */
6998 if (save_match_data)
6999 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7001 /* Make sure that we don't accidentally use bogus keymaps. */
7002 if (NILP (Voverriding_local_map_menu_flag))
7004 specbind (Qoverriding_terminal_local_map, Qnil);
7005 specbind (Qoverriding_local_map, Qnil);
7008 /* Build desired tool-bar items from keymaps. */
7009 f->desired_tool_bar_items
7010 = tool_bar_items (f->desired_tool_bar_items,
7011 &f->n_desired_tool_bar_items);
7013 /* Redisplay the tool-bar in case we changed it. */
7014 w->update_mode_line = Qt;
7016 unbind_to (count, Qnil);
7017 set_buffer_internal_1 (prev);
7023 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7024 F's desired tool-bar contents. F->desired_tool_bar_items must have
7025 been set up previously by calling prepare_menu_bars. */
7027 static void
7028 build_desired_tool_bar_string (f)
7029 struct frame *f;
7031 int i, size, size_needed, string_idx;
7032 struct gcpro gcpro1, gcpro2, gcpro3;
7033 Lisp_Object image, plist, props;
7035 image = plist = props = Qnil;
7036 GCPRO3 (image, plist, props);
7038 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7039 Otherwise, make a new string. */
7041 /* The size of the string we might be able to reuse. */
7042 size = (STRINGP (f->desired_tool_bar_string)
7043 ? XSTRING (f->desired_tool_bar_string)->size
7044 : 0);
7046 /* Each image in the string we build is preceded by a space,
7047 and there is a space at the end. */
7048 size_needed = f->n_desired_tool_bar_items + 1;
7050 /* Reuse f->desired_tool_bar_string, if possible. */
7051 if (size < size_needed)
7052 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7053 make_number (' '));
7054 else
7056 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7057 Fremove_text_properties (make_number (0), make_number (size),
7058 props, f->desired_tool_bar_string);
7061 /* Put a `display' property on the string for the images to display,
7062 put a `menu_item' property on tool-bar items with a value that
7063 is the index of the item in F's tool-bar item vector. */
7064 for (i = 0, string_idx = 0;
7065 i < f->n_desired_tool_bar_items;
7066 ++i, string_idx += 1)
7068 #define PROP(IDX) \
7069 (XVECTOR (f->desired_tool_bar_items) \
7070 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
7072 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7073 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7074 int margin, relief, idx;
7075 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7076 extern Lisp_Object Qlaplace;
7078 /* If image is a vector, choose the image according to the
7079 button state. */
7080 image = PROP (TOOL_BAR_ITEM_IMAGES);
7081 if (VECTORP (image))
7083 if (enabled_p)
7084 idx = (selected_p
7085 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7086 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7087 else
7088 idx = (selected_p
7089 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7090 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7092 xassert (ASIZE (image) >= idx);
7093 image = AREF (image, idx);
7095 else
7096 idx = -1;
7098 /* Ignore invalid image specifications. */
7099 if (!valid_image_p (image))
7100 continue;
7102 /* Display the tool-bar button pressed, or depressed. */
7103 plist = Fcopy_sequence (XCDR (image));
7105 /* Compute margin and relief to draw. */
7106 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7107 margin = relief + max (0, tool_bar_button_margin);
7109 if (auto_raise_tool_bar_buttons_p)
7111 /* Add a `:relief' property to the image spec if the item is
7112 selected. */
7113 if (selected_p)
7115 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7116 margin -= relief;
7119 else
7121 /* If image is selected, display it pressed, i.e. with a
7122 negative relief. If it's not selected, display it with a
7123 raised relief. */
7124 plist = Fplist_put (plist, QCrelief,
7125 (selected_p
7126 ? make_number (-relief)
7127 : make_number (relief)));
7128 margin -= relief;
7131 /* Put a margin around the image. */
7132 if (margin)
7133 plist = Fplist_put (plist, QCmargin, make_number (margin));
7135 /* If button is not enabled, and we don't have special images
7136 for the disabled state, make the image appear disabled by
7137 applying an appropriate algorithm to it. */
7138 if (!enabled_p && idx < 0)
7139 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7141 /* Put a `display' text property on the string for the image to
7142 display. Put a `menu-item' property on the string that gives
7143 the start of this item's properties in the tool-bar items
7144 vector. */
7145 image = Fcons (Qimage, plist);
7146 props = list4 (Qdisplay, image,
7147 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7148 Fadd_text_properties (make_number (string_idx),
7149 make_number (string_idx + 1),
7150 props, f->desired_tool_bar_string);
7151 #undef PROP
7154 UNGCPRO;
7158 /* Display one line of the tool-bar of frame IT->f. */
7160 static void
7161 display_tool_bar_line (it)
7162 struct it *it;
7164 struct glyph_row *row = it->glyph_row;
7165 int max_x = it->last_visible_x;
7166 struct glyph *last;
7168 prepare_desired_row (row);
7169 row->y = it->current_y;
7171 while (it->current_x < max_x)
7173 int x_before, x, n_glyphs_before, i, nglyphs;
7175 /* Get the next display element. */
7176 if (!get_next_display_element (it))
7177 break;
7179 /* Produce glyphs. */
7180 x_before = it->current_x;
7181 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7182 PRODUCE_GLYPHS (it);
7184 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7185 i = 0;
7186 x = x_before;
7187 while (i < nglyphs)
7189 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7191 if (x + glyph->pixel_width > max_x)
7193 /* Glyph doesn't fit on line. */
7194 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7195 it->current_x = x;
7196 goto out;
7199 ++it->hpos;
7200 x += glyph->pixel_width;
7201 ++i;
7204 /* Stop at line ends. */
7205 if (ITERATOR_AT_END_OF_LINE_P (it))
7206 break;
7208 set_iterator_to_next (it, 1);
7211 out:;
7213 row->displays_text_p = row->used[TEXT_AREA] != 0;
7214 extend_face_to_end_of_line (it);
7215 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7216 last->right_box_line_p = 1;
7217 compute_line_metrics (it);
7219 /* If line is empty, make it occupy the rest of the tool-bar. */
7220 if (!row->displays_text_p)
7222 row->height = row->phys_height = it->last_visible_y - row->y;
7223 row->ascent = row->phys_ascent = 0;
7226 row->full_width_p = 1;
7227 row->continued_p = 0;
7228 row->truncated_on_left_p = 0;
7229 row->truncated_on_right_p = 0;
7231 it->current_x = it->hpos = 0;
7232 it->current_y += row->height;
7233 ++it->vpos;
7234 ++it->glyph_row;
7238 /* Value is the number of screen lines needed to make all tool-bar
7239 items of frame F visible. */
7241 static int
7242 tool_bar_lines_needed (f)
7243 struct frame *f;
7245 struct window *w = XWINDOW (f->tool_bar_window);
7246 struct it it;
7248 /* Initialize an iterator for iteration over
7249 F->desired_tool_bar_string in the tool-bar window of frame F. */
7250 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7251 it.first_visible_x = 0;
7252 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7253 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7255 while (!ITERATOR_AT_END_P (&it))
7257 it.glyph_row = w->desired_matrix->rows;
7258 clear_glyph_row (it.glyph_row);
7259 display_tool_bar_line (&it);
7262 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7266 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7267 height should be changed. */
7269 static int
7270 redisplay_tool_bar (f)
7271 struct frame *f;
7273 struct window *w;
7274 struct it it;
7275 struct glyph_row *row;
7276 int change_height_p = 0;
7278 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7279 do anything. This means you must start with tool-bar-lines
7280 non-zero to get the auto-sizing effect. Or in other words, you
7281 can turn off tool-bars by specifying tool-bar-lines zero. */
7282 if (!WINDOWP (f->tool_bar_window)
7283 || (w = XWINDOW (f->tool_bar_window),
7284 XFASTINT (w->height) == 0))
7285 return 0;
7287 /* Set up an iterator for the tool-bar window. */
7288 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7289 it.first_visible_x = 0;
7290 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7291 row = it.glyph_row;
7293 /* Build a string that represents the contents of the tool-bar. */
7294 build_desired_tool_bar_string (f);
7295 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7297 /* Display as many lines as needed to display all tool-bar items. */
7298 while (it.current_y < it.last_visible_y)
7299 display_tool_bar_line (&it);
7301 /* It doesn't make much sense to try scrolling in the tool-bar
7302 window, so don't do it. */
7303 w->desired_matrix->no_scrolling_p = 1;
7304 w->must_be_updated_p = 1;
7306 if (auto_resize_tool_bars_p)
7308 int nlines;
7310 /* If there are blank lines at the end, except for a partially
7311 visible blank line at the end that is smaller than
7312 CANON_Y_UNIT, change the tool-bar's height. */
7313 row = it.glyph_row - 1;
7314 if (!row->displays_text_p
7315 && row->height >= CANON_Y_UNIT (f))
7316 change_height_p = 1;
7318 /* If row displays tool-bar items, but is partially visible,
7319 change the tool-bar's height. */
7320 if (row->displays_text_p
7321 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7322 change_height_p = 1;
7324 /* Resize windows as needed by changing the `tool-bar-lines'
7325 frame parameter. */
7326 if (change_height_p
7327 && (nlines = tool_bar_lines_needed (f),
7328 nlines != XFASTINT (w->height)))
7330 extern Lisp_Object Qtool_bar_lines;
7331 Lisp_Object frame;
7332 int old_height = XFASTINT (w->height);
7334 XSETFRAME (frame, f);
7335 clear_glyph_matrix (w->desired_matrix);
7336 Fmodify_frame_parameters (frame,
7337 Fcons (Fcons (Qtool_bar_lines,
7338 make_number (nlines)),
7339 Qnil));
7340 if (XFASTINT (w->height) != old_height)
7341 fonts_changed_p = 1;
7345 return change_height_p;
7349 /* Get information about the tool-bar item which is displayed in GLYPH
7350 on frame F. Return in *PROP_IDX the index where tool-bar item
7351 properties start in F->current_tool_bar_items. Value is zero if
7352 GLYPH doesn't display a tool-bar item. */
7355 tool_bar_item_info (f, glyph, prop_idx)
7356 struct frame *f;
7357 struct glyph *glyph;
7358 int *prop_idx;
7360 Lisp_Object prop;
7361 int success_p;
7363 /* Get the text property `menu-item' at pos. The value of that
7364 property is the start index of this item's properties in
7365 F->current_tool_bar_items. */
7366 prop = Fget_text_property (make_number (glyph->charpos),
7367 Qmenu_item, f->current_tool_bar_string);
7368 if (INTEGERP (prop))
7370 *prop_idx = XINT (prop);
7371 success_p = 1;
7373 else
7374 success_p = 0;
7376 return success_p;
7379 #endif /* HAVE_WINDOW_SYSTEM */
7383 /************************************************************************
7384 Horizontal scrolling
7385 ************************************************************************/
7387 static int hscroll_window_tree P_ ((Lisp_Object));
7388 static int hscroll_windows P_ ((Lisp_Object));
7390 /* For all leaf windows in the window tree rooted at WINDOW, set their
7391 hscroll value so that PT is (i) visible in the window, and (ii) so
7392 that it is not within a certain margin at the window's left and
7393 right border. Value is non-zero if any window's hscroll has been
7394 changed. */
7396 static int
7397 hscroll_window_tree (window)
7398 Lisp_Object window;
7400 int hscrolled_p = 0;
7402 while (WINDOWP (window))
7404 struct window *w = XWINDOW (window);
7406 if (WINDOWP (w->hchild))
7407 hscrolled_p |= hscroll_window_tree (w->hchild);
7408 else if (WINDOWP (w->vchild))
7409 hscrolled_p |= hscroll_window_tree (w->vchild);
7410 else if (w->cursor.vpos >= 0)
7412 int hscroll_margin, text_area_x, text_area_y;
7413 int text_area_width, text_area_height;
7414 struct glyph_row *current_cursor_row
7415 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7416 struct glyph_row *desired_cursor_row
7417 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7418 struct glyph_row *cursor_row
7419 = (desired_cursor_row->enabled_p
7420 ? desired_cursor_row
7421 : current_cursor_row);
7423 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7424 &text_area_width, &text_area_height);
7426 /* Scroll when cursor is inside this scroll margin. */
7427 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7429 if ((XFASTINT (w->hscroll)
7430 && w->cursor.x < hscroll_margin)
7431 || (cursor_row->enabled_p
7432 && cursor_row->truncated_on_right_p
7433 && (w->cursor.x > text_area_width - hscroll_margin)))
7435 struct it it;
7436 int hscroll;
7437 struct buffer *saved_current_buffer;
7438 int pt;
7440 /* Find point in a display of infinite width. */
7441 saved_current_buffer = current_buffer;
7442 current_buffer = XBUFFER (w->buffer);
7444 if (w == XWINDOW (selected_window))
7445 pt = BUF_PT (current_buffer);
7446 else
7448 pt = marker_position (w->pointm);
7449 pt = max (BEGV, pt);
7450 pt = min (ZV, pt);
7453 /* Move iterator to pt starting at cursor_row->start in
7454 a line with infinite width. */
7455 init_to_row_start (&it, w, cursor_row);
7456 it.last_visible_x = INFINITY;
7457 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7458 current_buffer = saved_current_buffer;
7460 /* Center cursor in window. */
7461 hscroll = (max (0, it.current_x - text_area_width / 2)
7462 / CANON_X_UNIT (it.f));
7464 /* Don't call Fset_window_hscroll if value hasn't
7465 changed because it will prevent redisplay
7466 optimizations. */
7467 if (XFASTINT (w->hscroll) != hscroll)
7469 Fset_window_hscroll (window, make_number (hscroll));
7470 hscrolled_p = 1;
7475 window = w->next;
7478 /* Value is non-zero if hscroll of any leaf window has been changed. */
7479 return hscrolled_p;
7483 /* Set hscroll so that cursor is visible and not inside horizontal
7484 scroll margins for all windows in the tree rooted at WINDOW. See
7485 also hscroll_window_tree above. Value is non-zero if any window's
7486 hscroll has been changed. If it has, desired matrices on the frame
7487 of WINDOW are cleared. */
7489 static int
7490 hscroll_windows (window)
7491 Lisp_Object window;
7493 int hscrolled_p;
7495 if (automatic_hscrolling_p)
7497 hscrolled_p = hscroll_window_tree (window);
7498 if (hscrolled_p)
7499 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7501 else
7502 hscrolled_p = 0;
7503 return hscrolled_p;
7508 /************************************************************************
7509 Redisplay
7510 ************************************************************************/
7512 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7513 to a non-zero value. This is sometimes handy to have in a debugger
7514 session. */
7516 #if GLYPH_DEBUG
7518 /* First and last unchanged row for try_window_id. */
7520 int debug_first_unchanged_at_end_vpos;
7521 int debug_last_unchanged_at_beg_vpos;
7523 /* Delta vpos and y. */
7525 int debug_dvpos, debug_dy;
7527 /* Delta in characters and bytes for try_window_id. */
7529 int debug_delta, debug_delta_bytes;
7531 /* Values of window_end_pos and window_end_vpos at the end of
7532 try_window_id. */
7534 int debug_end_pos, debug_end_vpos;
7536 /* Append a string to W->desired_matrix->method. FMT is a printf
7537 format string. A1...A9 are a supplement for a variable-length
7538 argument list. If trace_redisplay_p is non-zero also printf the
7539 resulting string to stderr. */
7541 static void
7542 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7543 struct window *w;
7544 char *fmt;
7545 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7547 char buffer[512];
7548 char *method = w->desired_matrix->method;
7549 int len = strlen (method);
7550 int size = sizeof w->desired_matrix->method;
7551 int remaining = size - len - 1;
7553 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7554 if (len && remaining)
7556 method[len] = '|';
7557 --remaining, ++len;
7560 strncpy (method + len, buffer, remaining);
7562 if (trace_redisplay_p)
7563 fprintf (stderr, "%p (%s): %s\n",
7565 ((BUFFERP (w->buffer)
7566 && STRINGP (XBUFFER (w->buffer)->name))
7567 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7568 : "no buffer"),
7569 buffer);
7572 #endif /* GLYPH_DEBUG */
7575 /* This counter is used to clear the face cache every once in a while
7576 in redisplay_internal. It is incremented for each redisplay.
7577 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7578 cleared. */
7580 #define CLEAR_FACE_CACHE_COUNT 10000
7581 static int clear_face_cache_count;
7583 /* Record the previous terminal frame we displayed. */
7585 static struct frame *previous_terminal_frame;
7587 /* Non-zero while redisplay_internal is in progress. */
7589 int redisplaying_p;
7592 /* Value is non-zero if all changes in window W, which displays
7593 current_buffer, are in the text between START and END. START is a
7594 buffer position, END is given as a distance from Z. Used in
7595 redisplay_internal for display optimization. */
7597 static INLINE int
7598 text_outside_line_unchanged_p (w, start, end)
7599 struct window *w;
7600 int start, end;
7602 int unchanged_p = 1;
7604 /* If text or overlays have changed, see where. */
7605 if (XFASTINT (w->last_modified) < MODIFF
7606 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7608 /* Gap in the line? */
7609 if (GPT < start || Z - GPT < end)
7610 unchanged_p = 0;
7612 /* Changes start in front of the line, or end after it? */
7613 if (unchanged_p
7614 && (BEG_UNCHANGED < start - 1
7615 || END_UNCHANGED < end))
7616 unchanged_p = 0;
7618 /* If selective display, can't optimize if changes start at the
7619 beginning of the line. */
7620 if (unchanged_p
7621 && INTEGERP (current_buffer->selective_display)
7622 && XINT (current_buffer->selective_display) > 0
7623 && (BEG_UNCHANGED < start || GPT <= start))
7624 unchanged_p = 0;
7627 return unchanged_p;
7631 /* Do a frame update, taking possible shortcuts into account. This is
7632 the main external entry point for redisplay.
7634 If the last redisplay displayed an echo area message and that message
7635 is no longer requested, we clear the echo area or bring back the
7636 mini-buffer if that is in use. */
7638 void
7639 redisplay ()
7641 redisplay_internal (0);
7644 /* Return 1 if point moved out of or into a composition. Otherwise
7645 return 0. PREV_BUF and PREV_PT are the last point buffer and
7646 position. BUF and PT are the current point buffer and position. */
7649 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7650 struct buffer *prev_buf, *buf;
7651 int prev_pt, pt;
7653 int start, end;
7654 Lisp_Object prop;
7655 Lisp_Object buffer;
7657 XSETBUFFER (buffer, buf);
7658 /* Check a composition at the last point if point moved within the
7659 same buffer. */
7660 if (prev_buf == buf)
7662 if (prev_pt == pt)
7663 /* Point didn't move. */
7664 return 0;
7666 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7667 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7668 && COMPOSITION_VALID_P (start, end, prop)
7669 && start < prev_pt && end > prev_pt)
7670 /* The last point was within the composition. Return 1 iff
7671 point moved out of the composition. */
7672 return (pt <= start || pt >= end);
7675 /* Check a composition at the current point. */
7676 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7677 && find_composition (pt, -1, &start, &end, &prop, buffer)
7678 && COMPOSITION_VALID_P (start, end, prop)
7679 && start < pt && end > pt);
7682 /* Reconsider the setting of B->clip_changed which is displayed
7683 in window W. */
7685 static INLINE void
7686 reconsider_clip_changes (w, b)
7687 struct window *w;
7688 struct buffer *b;
7690 if (b->prevent_redisplay_optimizations_p)
7691 b->clip_changed = 1;
7692 else if (b->clip_changed
7693 && !NILP (w->window_end_valid)
7694 && w->current_matrix->buffer == b
7695 && w->current_matrix->zv == BUF_ZV (b)
7696 && w->current_matrix->begv == BUF_BEGV (b))
7697 b->clip_changed = 0;
7699 /* If display wasn't paused, and W is not a tool bar window, see if
7700 point has been moved into or out of a composition. In that case,
7701 we set b->clip_changed to 1 to force updating the screen. If
7702 b->clip_changed has already been set to 1, we can skip this
7703 check. */
7704 if (!b->clip_changed
7705 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7707 int pt;
7709 if (w == XWINDOW (selected_window))
7710 pt = BUF_PT (current_buffer);
7711 else
7712 pt = marker_position (w->pointm);
7714 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7715 || pt != XINT (w->last_point))
7716 && check_point_in_composition (w->current_matrix->buffer,
7717 XINT (w->last_point),
7718 XBUFFER (w->buffer), pt))
7719 b->clip_changed = 1;
7724 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7725 response to any user action; therefore, we should preserve the echo
7726 area. (Actually, our caller does that job.) Perhaps in the future
7727 avoid recentering windows if it is not necessary; currently that
7728 causes some problems. */
7730 static void
7731 redisplay_internal (preserve_echo_area)
7732 int preserve_echo_area;
7734 struct window *w = XWINDOW (selected_window);
7735 struct frame *f = XFRAME (w->frame);
7736 int pause;
7737 int must_finish = 0;
7738 struct text_pos tlbufpos, tlendpos;
7739 int number_of_visible_frames;
7740 int count;
7741 struct frame *sf = SELECTED_FRAME ();
7743 /* Non-zero means redisplay has to consider all windows on all
7744 frames. Zero means, only selected_window is considered. */
7745 int consider_all_windows_p;
7747 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7749 /* No redisplay if running in batch mode or frame is not yet fully
7750 initialized, or redisplay is explicitly turned off by setting
7751 Vinhibit_redisplay. */
7752 if (noninteractive
7753 || !NILP (Vinhibit_redisplay)
7754 || !f->glyphs_initialized_p)
7755 return;
7757 /* The flag redisplay_performed_directly_p is set by
7758 direct_output_for_insert when it already did the whole screen
7759 update necessary. */
7760 if (redisplay_performed_directly_p)
7762 redisplay_performed_directly_p = 0;
7763 if (!hscroll_windows (selected_window))
7764 return;
7767 #ifdef USE_X_TOOLKIT
7768 if (popup_activated ())
7769 return;
7770 #endif
7772 /* I don't think this happens but let's be paranoid. */
7773 if (redisplaying_p)
7774 return;
7776 /* Record a function that resets redisplaying_p to its old value
7777 when we leave this function. */
7778 count = specpdl_ptr - specpdl;
7779 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7780 ++redisplaying_p;
7782 retry:
7783 pause = 0;
7784 reconsider_clip_changes (w, current_buffer);
7786 /* If new fonts have been loaded that make a glyph matrix adjustment
7787 necessary, do it. */
7788 if (fonts_changed_p)
7790 adjust_glyphs (NULL);
7791 ++windows_or_buffers_changed;
7792 fonts_changed_p = 0;
7795 if (! FRAME_WINDOW_P (sf)
7796 && previous_terminal_frame != sf)
7798 /* Since frames on an ASCII terminal share the same display
7799 area, displaying a different frame means redisplay the whole
7800 thing. */
7801 windows_or_buffers_changed++;
7802 SET_FRAME_GARBAGED (sf);
7803 XSETFRAME (Vterminal_frame, sf);
7805 previous_terminal_frame = sf;
7807 /* Set the visible flags for all frames. Do this before checking
7808 for resized or garbaged frames; they want to know if their frames
7809 are visible. See the comment in frame.h for
7810 FRAME_SAMPLE_VISIBILITY. */
7812 Lisp_Object tail, frame;
7814 number_of_visible_frames = 0;
7816 FOR_EACH_FRAME (tail, frame)
7818 struct frame *f = XFRAME (frame);
7820 FRAME_SAMPLE_VISIBILITY (f);
7821 if (FRAME_VISIBLE_P (f))
7822 ++number_of_visible_frames;
7823 clear_desired_matrices (f);
7827 /* Notice any pending interrupt request to change frame size. */
7828 do_pending_window_change (1);
7830 /* Clear frames marked as garbaged. */
7831 if (frame_garbaged)
7832 clear_garbaged_frames ();
7834 /* Build menubar and tool-bar items. */
7835 prepare_menu_bars ();
7837 if (windows_or_buffers_changed)
7838 update_mode_lines++;
7840 /* Detect case that we need to write or remove a star in the mode line. */
7841 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7843 w->update_mode_line = Qt;
7844 if (buffer_shared > 1)
7845 update_mode_lines++;
7848 /* If %c is in the mode line, update it if needed. */
7849 if (!NILP (w->column_number_displayed)
7850 /* This alternative quickly identifies a common case
7851 where no change is needed. */
7852 && !(PT == XFASTINT (w->last_point)
7853 && XFASTINT (w->last_modified) >= MODIFF
7854 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7855 && XFASTINT (w->column_number_displayed) != current_column ())
7856 w->update_mode_line = Qt;
7858 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7860 /* The variable buffer_shared is set in redisplay_window and
7861 indicates that we redisplay a buffer in different windows. See
7862 there. */
7863 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7865 /* If specs for an arrow have changed, do thorough redisplay
7866 to ensure we remove any arrow that should no longer exist. */
7867 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7868 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7869 consider_all_windows_p = windows_or_buffers_changed = 1;
7871 /* Normally the message* functions will have already displayed and
7872 updated the echo area, but the frame may have been trashed, or
7873 the update may have been preempted, so display the echo area
7874 again here. Checking both message buffers captures the case that
7875 the echo area should be cleared. */
7876 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7878 int window_height_changed_p = echo_area_display (0);
7879 must_finish = 1;
7881 if (fonts_changed_p)
7882 goto retry;
7883 else if (window_height_changed_p)
7885 consider_all_windows_p = 1;
7886 ++update_mode_lines;
7887 ++windows_or_buffers_changed;
7889 /* If window configuration was changed, frames may have been
7890 marked garbaged. Clear them or we will experience
7891 surprises wrt scrolling. */
7892 if (frame_garbaged)
7893 clear_garbaged_frames ();
7896 else if (EQ (selected_window, minibuf_window)
7897 && (current_buffer->clip_changed
7898 || XFASTINT (w->last_modified) < MODIFF
7899 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7900 && resize_mini_window (w, 0))
7902 /* Resized active mini-window to fit the size of what it is
7903 showing if its contents might have changed. */
7904 must_finish = 1;
7905 consider_all_windows_p = 1;
7906 ++windows_or_buffers_changed;
7907 ++update_mode_lines;
7909 /* If window configuration was changed, frames may have been
7910 marked garbaged. Clear them or we will experience
7911 surprises wrt scrolling. */
7912 if (frame_garbaged)
7913 clear_garbaged_frames ();
7917 /* If showing the region, and mark has changed, we must redisplay
7918 the whole window. The assignment to this_line_start_pos prevents
7919 the optimization directly below this if-statement. */
7920 if (((!NILP (Vtransient_mark_mode)
7921 && !NILP (XBUFFER (w->buffer)->mark_active))
7922 != !NILP (w->region_showing))
7923 || (!NILP (w->region_showing)
7924 && !EQ (w->region_showing,
7925 Fmarker_position (XBUFFER (w->buffer)->mark))))
7926 CHARPOS (this_line_start_pos) = 0;
7928 /* Optimize the case that only the line containing the cursor in the
7929 selected window has changed. Variables starting with this_ are
7930 set in display_line and record information about the line
7931 containing the cursor. */
7932 tlbufpos = this_line_start_pos;
7933 tlendpos = this_line_end_pos;
7934 if (!consider_all_windows_p
7935 && CHARPOS (tlbufpos) > 0
7936 && NILP (w->update_mode_line)
7937 && !current_buffer->clip_changed
7938 && FRAME_VISIBLE_P (XFRAME (w->frame))
7939 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7940 /* Make sure recorded data applies to current buffer, etc. */
7941 && this_line_buffer == current_buffer
7942 && current_buffer == XBUFFER (w->buffer)
7943 && NILP (w->force_start)
7944 /* Point must be on the line that we have info recorded about. */
7945 && PT >= CHARPOS (tlbufpos)
7946 && PT <= Z - CHARPOS (tlendpos)
7947 /* All text outside that line, including its final newline,
7948 must be unchanged */
7949 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7950 CHARPOS (tlendpos)))
7952 if (CHARPOS (tlbufpos) > BEGV
7953 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7954 && (CHARPOS (tlbufpos) == ZV
7955 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7956 /* Former continuation line has disappeared by becoming empty */
7957 goto cancel;
7958 else if (XFASTINT (w->last_modified) < MODIFF
7959 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7960 || MINI_WINDOW_P (w))
7962 /* We have to handle the case of continuation around a
7963 wide-column character (See the comment in indent.c around
7964 line 885).
7966 For instance, in the following case:
7968 -------- Insert --------
7969 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7970 J_I_ ==> J_I_ `^^' are cursors.
7971 ^^ ^^
7972 -------- --------
7974 As we have to redraw the line above, we should goto cancel. */
7976 struct it it;
7977 int line_height_before = this_line_pixel_height;
7979 /* Note that start_display will handle the case that the
7980 line starting at tlbufpos is a continuation lines. */
7981 start_display (&it, w, tlbufpos);
7983 /* Implementation note: It this still necessary? */
7984 if (it.current_x != this_line_start_x)
7985 goto cancel;
7987 TRACE ((stderr, "trying display optimization 1\n"));
7988 w->cursor.vpos = -1;
7989 overlay_arrow_seen = 0;
7990 it.vpos = this_line_vpos;
7991 it.current_y = this_line_y;
7992 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7993 display_line (&it);
7995 /* If line contains point, is not continued,
7996 and ends at same distance from eob as before, we win */
7997 if (w->cursor.vpos >= 0
7998 /* Line is not continued, otherwise this_line_start_pos
7999 would have been set to 0 in display_line. */
8000 && CHARPOS (this_line_start_pos)
8001 /* Line ends as before. */
8002 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8003 /* Line has same height as before. Otherwise other lines
8004 would have to be shifted up or down. */
8005 && this_line_pixel_height == line_height_before)
8007 /* If this is not the window's last line, we must adjust
8008 the charstarts of the lines below. */
8009 if (it.current_y < it.last_visible_y)
8011 struct glyph_row *row
8012 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8013 int delta, delta_bytes;
8015 if (Z - CHARPOS (tlendpos) == ZV)
8017 /* This line ends at end of (accessible part of)
8018 buffer. There is no newline to count. */
8019 delta = (Z
8020 - CHARPOS (tlendpos)
8021 - MATRIX_ROW_START_CHARPOS (row));
8022 delta_bytes = (Z_BYTE
8023 - BYTEPOS (tlendpos)
8024 - MATRIX_ROW_START_BYTEPOS (row));
8026 else
8028 /* This line ends in a newline. Must take
8029 account of the newline and the rest of the
8030 text that follows. */
8031 delta = (Z
8032 - CHARPOS (tlendpos)
8033 - MATRIX_ROW_START_CHARPOS (row));
8034 delta_bytes = (Z_BYTE
8035 - BYTEPOS (tlendpos)
8036 - MATRIX_ROW_START_BYTEPOS (row));
8039 increment_matrix_positions (w->current_matrix,
8040 this_line_vpos + 1,
8041 w->current_matrix->nrows,
8042 delta, delta_bytes);
8045 /* If this row displays text now but previously didn't,
8046 or vice versa, w->window_end_vpos may have to be
8047 adjusted. */
8048 if ((it.glyph_row - 1)->displays_text_p)
8050 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8051 XSETINT (w->window_end_vpos, this_line_vpos);
8053 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8054 && this_line_vpos > 0)
8055 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8056 w->window_end_valid = Qnil;
8058 /* Update hint: No need to try to scroll in update_window. */
8059 w->desired_matrix->no_scrolling_p = 1;
8061 #if GLYPH_DEBUG
8062 *w->desired_matrix->method = 0;
8063 debug_method_add (w, "optimization 1");
8064 #endif
8065 goto update;
8067 else
8068 goto cancel;
8070 else if (/* Cursor position hasn't changed. */
8071 PT == XFASTINT (w->last_point)
8072 /* Make sure the cursor was last displayed
8073 in this window. Otherwise we have to reposition it. */
8074 && 0 <= w->cursor.vpos
8075 && XINT (w->height) > w->cursor.vpos)
8077 if (!must_finish)
8079 do_pending_window_change (1);
8081 /* We used to always goto end_of_redisplay here, but this
8082 isn't enough if we have a blinking cursor. */
8083 if (w->cursor_off_p == w->last_cursor_off_p)
8084 goto end_of_redisplay;
8086 goto update;
8088 /* If highlighting the region, or if the cursor is in the echo area,
8089 then we can't just move the cursor. */
8090 else if (! (!NILP (Vtransient_mark_mode)
8091 && !NILP (current_buffer->mark_active))
8092 && (EQ (selected_window, current_buffer->last_selected_window)
8093 || highlight_nonselected_windows)
8094 && NILP (w->region_showing)
8095 && NILP (Vshow_trailing_whitespace)
8096 && !cursor_in_echo_area)
8098 struct it it;
8099 struct glyph_row *row;
8101 /* Skip from tlbufpos to PT and see where it is. Note that
8102 PT may be in invisible text. If so, we will end at the
8103 next visible position. */
8104 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8105 NULL, DEFAULT_FACE_ID);
8106 it.current_x = this_line_start_x;
8107 it.current_y = this_line_y;
8108 it.vpos = this_line_vpos;
8110 /* The call to move_it_to stops in front of PT, but
8111 moves over before-strings. */
8112 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8114 if (it.vpos == this_line_vpos
8115 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8116 row->enabled_p))
8118 xassert (this_line_vpos == it.vpos);
8119 xassert (this_line_y == it.current_y);
8120 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8121 goto update;
8123 else
8124 goto cancel;
8127 cancel:
8128 /* Text changed drastically or point moved off of line. */
8129 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8132 CHARPOS (this_line_start_pos) = 0;
8133 consider_all_windows_p |= buffer_shared > 1;
8134 ++clear_face_cache_count;
8137 /* Build desired matrices, and update the display. If
8138 consider_all_windows_p is non-zero, do it for all windows on all
8139 frames. Otherwise do it for selected_window, only. */
8141 if (consider_all_windows_p)
8143 Lisp_Object tail, frame;
8145 /* Clear the face cache eventually. */
8146 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8148 clear_face_cache (0);
8149 clear_face_cache_count = 0;
8152 /* Recompute # windows showing selected buffer. This will be
8153 incremented each time such a window is displayed. */
8154 buffer_shared = 0;
8156 FOR_EACH_FRAME (tail, frame)
8158 struct frame *f = XFRAME (frame);
8160 if (FRAME_WINDOW_P (f) || f == sf)
8162 /* Mark all the scroll bars to be removed; we'll redeem
8163 the ones we want when we redisplay their windows. */
8164 if (condemn_scroll_bars_hook)
8165 (*condemn_scroll_bars_hook) (f);
8167 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8168 redisplay_windows (FRAME_ROOT_WINDOW (f));
8170 /* Any scroll bars which redisplay_windows should have
8171 nuked should now go away. */
8172 if (judge_scroll_bars_hook)
8173 (*judge_scroll_bars_hook) (f);
8175 /* If fonts changed, display again. */
8176 if (fonts_changed_p)
8177 goto retry;
8179 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8181 /* See if we have to hscroll. */
8182 if (hscroll_windows (f->root_window))
8183 goto retry;
8185 /* Prevent various kinds of signals during display
8186 update. stdio is not robust about handling
8187 signals, which can cause an apparent I/O
8188 error. */
8189 if (interrupt_input)
8190 unrequest_sigio ();
8191 stop_polling ();
8193 /* Update the display. */
8194 set_window_update_flags (XWINDOW (f->root_window), 1);
8195 pause |= update_frame (f, 0, 0);
8196 if (pause)
8197 break;
8199 mark_window_display_accurate (f->root_window, 1);
8200 if (frame_up_to_date_hook)
8201 frame_up_to_date_hook (f);
8206 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8208 Lisp_Object mini_window;
8209 struct frame *mini_frame;
8211 redisplay_window (selected_window, 1);
8213 /* Compare desired and current matrices, perform output. */
8214 update:
8216 /* If fonts changed, display again. */
8217 if (fonts_changed_p)
8218 goto retry;
8220 /* Prevent various kinds of signals during display update.
8221 stdio is not robust about handling signals,
8222 which can cause an apparent I/O error. */
8223 if (interrupt_input)
8224 unrequest_sigio ();
8225 stop_polling ();
8227 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8229 if (hscroll_windows (selected_window))
8230 goto retry;
8232 XWINDOW (selected_window)->must_be_updated_p = 1;
8233 pause = update_frame (sf, 0, 0);
8236 /* We may have called echo_area_display at the top of this
8237 function. If the echo area is on another frame, that may
8238 have put text on a frame other than the selected one, so the
8239 above call to update_frame would not have caught it. Catch
8240 it here. */
8241 mini_window = FRAME_MINIBUF_WINDOW (sf);
8242 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8244 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8246 XWINDOW (mini_window)->must_be_updated_p = 1;
8247 pause |= update_frame (mini_frame, 0, 0);
8248 if (!pause && hscroll_windows (mini_window))
8249 goto retry;
8253 /* If display was paused because of pending input, make sure we do a
8254 thorough update the next time. */
8255 if (pause)
8257 /* Prevent the optimization at the beginning of
8258 redisplay_internal that tries a single-line update of the
8259 line containing the cursor in the selected window. */
8260 CHARPOS (this_line_start_pos) = 0;
8262 /* Let the overlay arrow be updated the next time. */
8263 if (!NILP (last_arrow_position))
8265 last_arrow_position = Qt;
8266 last_arrow_string = Qt;
8269 /* If we pause after scrolling, some rows in the current
8270 matrices of some windows are not valid. */
8271 if (!WINDOW_FULL_WIDTH_P (w)
8272 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8273 update_mode_lines = 1;
8276 /* Now text on frame agrees with windows, so put info into the
8277 windows for partial redisplay to follow. */
8278 if (!pause)
8280 register struct buffer *b = XBUFFER (w->buffer);
8282 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8283 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8284 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8285 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8287 if (consider_all_windows_p)
8288 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8289 else
8291 XSETFASTINT (w->last_point, BUF_PT (b));
8292 w->last_cursor = w->cursor;
8293 w->last_cursor_off_p = w->cursor_off_p;
8295 b->clip_changed = 0;
8296 b->prevent_redisplay_optimizations_p = 0;
8297 w->update_mode_line = Qnil;
8298 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8299 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8300 w->last_had_star
8301 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8302 ? Qt : Qnil);
8304 /* Record if we are showing a region, so can make sure to
8305 update it fully at next redisplay. */
8306 w->region_showing = (!NILP (Vtransient_mark_mode)
8307 && (EQ (selected_window,
8308 current_buffer->last_selected_window)
8309 || highlight_nonselected_windows)
8310 && !NILP (XBUFFER (w->buffer)->mark_active)
8311 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8312 : Qnil);
8314 w->window_end_valid = w->buffer;
8315 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8316 last_arrow_string = Voverlay_arrow_string;
8317 if (frame_up_to_date_hook != 0)
8318 (*frame_up_to_date_hook) (sf);
8320 w->current_matrix->buffer = b;
8321 w->current_matrix->begv = BUF_BEGV (b);
8322 w->current_matrix->zv = BUF_ZV (b);
8325 update_mode_lines = 0;
8326 windows_or_buffers_changed = 0;
8329 /* Start SIGIO interrupts coming again. Having them off during the
8330 code above makes it less likely one will discard output, but not
8331 impossible, since there might be stuff in the system buffer here.
8332 But it is much hairier to try to do anything about that. */
8333 if (interrupt_input)
8334 request_sigio ();
8335 start_polling ();
8337 /* If a frame has become visible which was not before, redisplay
8338 again, so that we display it. Expose events for such a frame
8339 (which it gets when becoming visible) don't call the parts of
8340 redisplay constructing glyphs, so simply exposing a frame won't
8341 display anything in this case. So, we have to display these
8342 frames here explicitly. */
8343 if (!pause)
8345 Lisp_Object tail, frame;
8346 int new_count = 0;
8348 FOR_EACH_FRAME (tail, frame)
8350 int this_is_visible = 0;
8352 if (XFRAME (frame)->visible)
8353 this_is_visible = 1;
8354 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8355 if (XFRAME (frame)->visible)
8356 this_is_visible = 1;
8358 if (this_is_visible)
8359 new_count++;
8362 if (new_count != number_of_visible_frames)
8363 windows_or_buffers_changed++;
8366 /* Change frame size now if a change is pending. */
8367 do_pending_window_change (1);
8369 /* If we just did a pending size change, or have additional
8370 visible frames, redisplay again. */
8371 if (windows_or_buffers_changed && !pause)
8372 goto retry;
8374 end_of_redisplay:;
8376 unbind_to (count, Qnil);
8380 /* Redisplay, but leave alone any recent echo area message unless
8381 another message has been requested in its place.
8383 This is useful in situations where you need to redisplay but no
8384 user action has occurred, making it inappropriate for the message
8385 area to be cleared. See tracking_off and
8386 wait_reading_process_input for examples of these situations. */
8388 void
8389 redisplay_preserve_echo_area ()
8391 if (!NILP (echo_area_buffer[1]))
8393 /* We have a previously displayed message, but no current
8394 message. Redisplay the previous message. */
8395 display_last_displayed_message_p = 1;
8396 redisplay_internal (1);
8397 display_last_displayed_message_p = 0;
8399 else
8400 redisplay_internal (1);
8404 /* Function registered with record_unwind_protect in
8405 redisplay_internal. Clears the flag indicating that a redisplay is
8406 in progress. */
8408 static Lisp_Object
8409 unwind_redisplay (old_redisplaying_p)
8410 Lisp_Object old_redisplaying_p;
8412 redisplaying_p = XFASTINT (old_redisplaying_p);
8413 return Qnil;
8417 /* Mark the display of windows in the window tree rooted at WINDOW as
8418 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8419 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8420 the next time redisplay_internal is called. */
8422 void
8423 mark_window_display_accurate (window, accurate_p)
8424 Lisp_Object window;
8425 int accurate_p;
8427 struct window *w;
8429 for (; !NILP (window); window = w->next)
8431 w = XWINDOW (window);
8433 if (BUFFERP (w->buffer))
8435 struct buffer *b = XBUFFER (w->buffer);
8437 XSETFASTINT (w->last_modified,
8438 accurate_p ? BUF_MODIFF (b) : 0);
8439 XSETFASTINT (w->last_overlay_modified,
8440 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8441 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8442 ? Qt : Qnil);
8444 #if 0 /* I don't think this is necessary because display_line does it.
8445 Let's check it. */
8446 /* Record if we are showing a region, so can make sure to
8447 update it fully at next redisplay. */
8448 w->region_showing
8449 = (!NILP (Vtransient_mark_mode)
8450 && (w == XWINDOW (current_buffer->last_selected_window)
8451 || highlight_nonselected_windows)
8452 && (!NILP (b->mark_active)
8453 ? Fmarker_position (b->mark)
8454 : Qnil));
8455 #endif
8457 if (accurate_p)
8459 b->clip_changed = 0;
8460 b->prevent_redisplay_optimizations_p = 0;
8461 w->current_matrix->buffer = b;
8462 w->current_matrix->begv = BUF_BEGV (b);
8463 w->current_matrix->zv = BUF_ZV (b);
8464 w->last_cursor = w->cursor;
8465 w->last_cursor_off_p = w->cursor_off_p;
8466 if (w == XWINDOW (selected_window))
8467 w->last_point = make_number (BUF_PT (b));
8468 else
8469 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8473 w->window_end_valid = w->buffer;
8474 w->update_mode_line = Qnil;
8476 if (!NILP (w->vchild))
8477 mark_window_display_accurate (w->vchild, accurate_p);
8478 if (!NILP (w->hchild))
8479 mark_window_display_accurate (w->hchild, accurate_p);
8482 if (accurate_p)
8484 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8485 last_arrow_string = Voverlay_arrow_string;
8487 else
8489 /* Force a thorough redisplay the next time by setting
8490 last_arrow_position and last_arrow_string to t, which is
8491 unequal to any useful value of Voverlay_arrow_... */
8492 last_arrow_position = Qt;
8493 last_arrow_string = Qt;
8498 /* Return value in display table DP (Lisp_Char_Table *) for character
8499 C. Since a display table doesn't have any parent, we don't have to
8500 follow parent. Do not call this function directly but use the
8501 macro DISP_CHAR_VECTOR. */
8503 Lisp_Object
8504 disp_char_vector (dp, c)
8505 struct Lisp_Char_Table *dp;
8506 int c;
8508 int code[4], i;
8509 Lisp_Object val;
8511 if (SINGLE_BYTE_CHAR_P (c))
8512 return (dp->contents[c]);
8514 SPLIT_CHAR (c, code[0], code[1], code[2]);
8515 if (code[1] < 32)
8516 code[1] = -1;
8517 else if (code[2] < 32)
8518 code[2] = -1;
8520 /* Here, the possible range of code[0] (== charset ID) is
8521 128..max_charset. Since the top level char table contains data
8522 for multibyte characters after 256th element, we must increment
8523 code[0] by 128 to get a correct index. */
8524 code[0] += 128;
8525 code[3] = -1; /* anchor */
8527 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8529 val = dp->contents[code[i]];
8530 if (!SUB_CHAR_TABLE_P (val))
8531 return (NILP (val) ? dp->defalt : val);
8534 /* Here, val is a sub char table. We return the default value of
8535 it. */
8536 return (dp->defalt);
8541 /***********************************************************************
8542 Window Redisplay
8543 ***********************************************************************/
8545 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8547 static void
8548 redisplay_windows (window)
8549 Lisp_Object window;
8551 while (!NILP (window))
8553 struct window *w = XWINDOW (window);
8555 if (!NILP (w->hchild))
8556 redisplay_windows (w->hchild);
8557 else if (!NILP (w->vchild))
8558 redisplay_windows (w->vchild);
8559 else
8560 redisplay_window (window, 0);
8562 window = w->next;
8567 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8568 DELTA is the number of bytes by which positions recorded in ROW
8569 differ from current buffer positions. */
8571 void
8572 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8573 struct window *w;
8574 struct glyph_row *row;
8575 struct glyph_matrix *matrix;
8576 int delta, delta_bytes, dy, dvpos;
8578 struct glyph *glyph = row->glyphs[TEXT_AREA];
8579 struct glyph *end = glyph + row->used[TEXT_AREA];
8580 int x = row->x;
8581 int pt_old = PT - delta;
8583 /* Skip over glyphs not having an object at the start of the row.
8584 These are special glyphs like truncation marks on terminal
8585 frames. */
8586 if (row->displays_text_p)
8587 while (glyph < end
8588 && INTEGERP (glyph->object)
8589 && glyph->charpos < 0)
8591 x += glyph->pixel_width;
8592 ++glyph;
8595 while (glyph < end
8596 && !INTEGERP (glyph->object)
8597 && (!BUFFERP (glyph->object)
8598 || glyph->charpos < pt_old))
8600 x += glyph->pixel_width;
8601 ++glyph;
8604 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8605 w->cursor.x = x;
8606 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8607 w->cursor.y = row->y + dy;
8609 if (w == XWINDOW (selected_window))
8611 if (!row->continued_p
8612 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8613 && row->x == 0)
8615 this_line_buffer = XBUFFER (w->buffer);
8617 CHARPOS (this_line_start_pos)
8618 = MATRIX_ROW_START_CHARPOS (row) + delta;
8619 BYTEPOS (this_line_start_pos)
8620 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8622 CHARPOS (this_line_end_pos)
8623 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8624 BYTEPOS (this_line_end_pos)
8625 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8627 this_line_y = w->cursor.y;
8628 this_line_pixel_height = row->height;
8629 this_line_vpos = w->cursor.vpos;
8630 this_line_start_x = row->x;
8632 else
8633 CHARPOS (this_line_start_pos) = 0;
8638 /* Run window scroll functions, if any, for WINDOW with new window
8639 start STARTP. Sets the window start of WINDOW to that position.
8641 We assume that the window's buffer is really current. */
8643 static INLINE struct text_pos
8644 run_window_scroll_functions (window, startp)
8645 Lisp_Object window;
8646 struct text_pos startp;
8648 struct window *w = XWINDOW (window);
8649 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8651 if (current_buffer != XBUFFER (w->buffer))
8652 abort ();
8654 if (!NILP (Vwindow_scroll_functions))
8656 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8657 make_number (CHARPOS (startp)));
8658 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8659 /* In case the hook functions switch buffers. */
8660 if (current_buffer != XBUFFER (w->buffer))
8661 set_buffer_internal_1 (XBUFFER (w->buffer));
8664 return startp;
8668 /* Modify the desired matrix of window W and W->vscroll so that the
8669 line containing the cursor is fully visible. */
8671 static void
8672 make_cursor_line_fully_visible (w)
8673 struct window *w;
8675 struct glyph_matrix *matrix;
8676 struct glyph_row *row;
8677 int window_height, header_line_height;
8679 /* It's not always possible to find the cursor, e.g, when a window
8680 is full of overlay strings. Don't do anything in that case. */
8681 if (w->cursor.vpos < 0)
8682 return;
8684 matrix = w->desired_matrix;
8685 row = MATRIX_ROW (matrix, w->cursor.vpos);
8687 /* If the cursor row is not partially visible, there's nothing
8688 to do. */
8689 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8690 return;
8692 /* If the row the cursor is in is taller than the window's height,
8693 it's not clear what to do, so do nothing. */
8694 window_height = window_box_height (w);
8695 if (row->height >= window_height)
8696 return;
8698 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8700 int dy = row->height - row->visible_height;
8701 w->vscroll = 0;
8702 w->cursor.y += dy;
8703 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8705 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8707 int dy = - (row->height - row->visible_height);
8708 w->vscroll = dy;
8709 w->cursor.y += dy;
8710 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8713 /* When we change the cursor y-position of the selected window,
8714 change this_line_y as well so that the display optimization for
8715 the cursor line of the selected window in redisplay_internal uses
8716 the correct y-position. */
8717 if (w == XWINDOW (selected_window))
8718 this_line_y = w->cursor.y;
8722 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8723 non-zero means only WINDOW is redisplayed in redisplay_internal.
8724 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8725 in redisplay_window to bring a partially visible line into view in
8726 the case that only the cursor has moved.
8728 Value is
8730 1 if scrolling succeeded
8732 0 if scrolling didn't find point.
8734 -1 if new fonts have been loaded so that we must interrupt
8735 redisplay, adjust glyph matrices, and try again. */
8737 static int
8738 try_scrolling (window, just_this_one_p, scroll_conservatively,
8739 scroll_step, temp_scroll_step)
8740 Lisp_Object window;
8741 int just_this_one_p;
8742 int scroll_conservatively, scroll_step;
8743 int temp_scroll_step;
8745 struct window *w = XWINDOW (window);
8746 struct frame *f = XFRAME (w->frame);
8747 struct text_pos scroll_margin_pos;
8748 struct text_pos pos;
8749 struct text_pos startp;
8750 struct it it;
8751 Lisp_Object window_end;
8752 int this_scroll_margin;
8753 int dy = 0;
8754 int scroll_max;
8755 int rc;
8756 int amount_to_scroll = 0;
8757 Lisp_Object aggressive;
8758 int height;
8760 #if GLYPH_DEBUG
8761 debug_method_add (w, "try_scrolling");
8762 #endif
8764 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8766 /* Compute scroll margin height in pixels. We scroll when point is
8767 within this distance from the top or bottom of the window. */
8768 if (scroll_margin > 0)
8770 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8771 this_scroll_margin *= CANON_Y_UNIT (f);
8773 else
8774 this_scroll_margin = 0;
8776 /* Compute how much we should try to scroll maximally to bring point
8777 into view. */
8778 if (scroll_step || scroll_conservatively || temp_scroll_step)
8779 scroll_max = max (scroll_step,
8780 max (scroll_conservatively, temp_scroll_step));
8781 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8782 || NUMBERP (current_buffer->scroll_up_aggressively))
8783 /* We're trying to scroll because of aggressive scrolling
8784 but no scroll_step is set. Choose an arbitrary one. Maybe
8785 there should be a variable for this. */
8786 scroll_max = 10;
8787 else
8788 scroll_max = 0;
8789 scroll_max *= CANON_Y_UNIT (f);
8791 /* Decide whether we have to scroll down. Start at the window end
8792 and move this_scroll_margin up to find the position of the scroll
8793 margin. */
8794 window_end = Fwindow_end (window, Qt);
8795 CHARPOS (scroll_margin_pos) = XINT (window_end);
8796 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8797 if (this_scroll_margin)
8799 start_display (&it, w, scroll_margin_pos);
8800 move_it_vertically (&it, - this_scroll_margin);
8801 scroll_margin_pos = it.current.pos;
8804 if (PT >= CHARPOS (scroll_margin_pos))
8806 int y0;
8807 #if 0
8808 int line_height;
8809 #endif
8811 /* Point is in the scroll margin at the bottom of the window, or
8812 below. Compute a new window start that makes point visible. */
8814 /* Compute the distance from the scroll margin to PT.
8815 Give up if the distance is greater than scroll_max. */
8816 start_display (&it, w, scroll_margin_pos);
8817 y0 = it.current_y;
8818 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8819 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8820 #if 0 /* Taking the line's height into account here looks wrong. */
8821 line_height = (it.max_ascent + it.max_descent
8822 ? it.max_ascent + it.max_descent
8823 : last_height);
8824 dy = it.current_y + line_height - y0;
8825 #else
8826 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8827 end, which is one line below the window. The iterator's
8828 current_y will be same as y0 in that case, but we have to
8829 scroll a line to make PT visible. That's the reason why 1 is
8830 added below. */
8831 dy = 1 + it.current_y - y0;
8832 #endif
8834 if (dy > scroll_max)
8835 return 0;
8837 /* Move the window start down. If scrolling conservatively,
8838 move it just enough down to make point visible. If
8839 scroll_step is set, move it down by scroll_step. */
8840 start_display (&it, w, startp);
8842 if (scroll_conservatively)
8843 amount_to_scroll =
8844 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8845 else if (scroll_step || temp_scroll_step)
8846 amount_to_scroll = scroll_max;
8847 else
8849 aggressive = current_buffer->scroll_down_aggressively;
8850 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8851 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8852 if (NUMBERP (aggressive))
8853 amount_to_scroll = XFLOATINT (aggressive) * height;
8856 if (amount_to_scroll <= 0)
8857 return 0;
8859 move_it_vertically (&it, amount_to_scroll);
8860 startp = it.current.pos;
8862 else
8864 /* See if point is inside the scroll margin at the top of the
8865 window. */
8866 scroll_margin_pos = startp;
8867 if (this_scroll_margin)
8869 start_display (&it, w, startp);
8870 move_it_vertically (&it, this_scroll_margin);
8871 scroll_margin_pos = it.current.pos;
8874 if (PT < CHARPOS (scroll_margin_pos))
8876 /* Point is in the scroll margin at the top of the window or
8877 above what is displayed in the window. */
8878 int y0;
8880 /* Compute the vertical distance from PT to the scroll
8881 margin position. Give up if distance is greater than
8882 scroll_max. */
8883 SET_TEXT_POS (pos, PT, PT_BYTE);
8884 start_display (&it, w, pos);
8885 y0 = it.current_y;
8886 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8887 it.last_visible_y, -1,
8888 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8889 dy = it.current_y - y0;
8890 if (dy > scroll_max)
8891 return 0;
8893 /* Compute new window start. */
8894 start_display (&it, w, startp);
8896 if (scroll_conservatively)
8897 amount_to_scroll =
8898 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8899 else if (scroll_step || temp_scroll_step)
8900 amount_to_scroll = scroll_max;
8901 else
8903 aggressive = current_buffer->scroll_up_aggressively;
8904 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8905 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8906 if (NUMBERP (aggressive))
8907 amount_to_scroll = XFLOATINT (aggressive) * height;
8910 if (amount_to_scroll <= 0)
8911 return 0;
8913 move_it_vertically (&it, - amount_to_scroll);
8914 startp = it.current.pos;
8918 /* Run window scroll functions. */
8919 startp = run_window_scroll_functions (window, startp);
8921 /* Display the window. Give up if new fonts are loaded, or if point
8922 doesn't appear. */
8923 if (!try_window (window, startp))
8924 rc = -1;
8925 else if (w->cursor.vpos < 0)
8927 clear_glyph_matrix (w->desired_matrix);
8928 rc = 0;
8930 else
8932 /* Maybe forget recorded base line for line number display. */
8933 if (!just_this_one_p
8934 || current_buffer->clip_changed
8935 || BEG_UNCHANGED < CHARPOS (startp))
8936 w->base_line_number = Qnil;
8938 /* If cursor ends up on a partially visible line, shift display
8939 lines up or down. */
8940 make_cursor_line_fully_visible (w);
8941 rc = 1;
8944 return rc;
8948 /* Compute a suitable window start for window W if display of W starts
8949 on a continuation line. Value is non-zero if a new window start
8950 was computed.
8952 The new window start will be computed, based on W's width, starting
8953 from the start of the continued line. It is the start of the
8954 screen line with the minimum distance from the old start W->start. */
8956 static int
8957 compute_window_start_on_continuation_line (w)
8958 struct window *w;
8960 struct text_pos pos, start_pos;
8961 int window_start_changed_p = 0;
8963 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8965 /* If window start is on a continuation line... Window start may be
8966 < BEGV in case there's invisible text at the start of the
8967 buffer (M-x rmail, for example). */
8968 if (CHARPOS (start_pos) > BEGV
8969 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8971 struct it it;
8972 struct glyph_row *row;
8974 /* Handle the case that the window start is out of range. */
8975 if (CHARPOS (start_pos) < BEGV)
8976 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8977 else if (CHARPOS (start_pos) > ZV)
8978 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8980 /* Find the start of the continued line. This should be fast
8981 because scan_buffer is fast (newline cache). */
8982 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8983 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8984 row, DEFAULT_FACE_ID);
8985 reseat_at_previous_visible_line_start (&it);
8987 /* If the line start is "too far" away from the window start,
8988 say it takes too much time to compute a new window start. */
8989 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8990 < XFASTINT (w->height) * XFASTINT (w->width))
8992 int min_distance, distance;
8994 /* Move forward by display lines to find the new window
8995 start. If window width was enlarged, the new start can
8996 be expected to be > the old start. If window width was
8997 decreased, the new window start will be < the old start.
8998 So, we're looking for the display line start with the
8999 minimum distance from the old window start. */
9000 pos = it.current.pos;
9001 min_distance = INFINITY;
9002 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9003 distance < min_distance)
9005 min_distance = distance;
9006 pos = it.current.pos;
9007 move_it_by_lines (&it, 1, 0);
9010 /* Set the window start there. */
9011 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9012 window_start_changed_p = 1;
9016 return window_start_changed_p;
9020 /* Try cursor movement in case text has not changes in window WINDOW,
9021 with window start STARTP. Value is
9023 1 if successful
9025 0 if this method cannot be used
9027 -1 if we know we have to scroll the display. *SCROLL_STEP is
9028 set to 1, under certain circumstances, if we want to scroll as
9029 if scroll-step were set to 1. See the code. */
9031 static int
9032 try_cursor_movement (window, startp, scroll_step)
9033 Lisp_Object window;
9034 struct text_pos startp;
9035 int *scroll_step;
9037 struct window *w = XWINDOW (window);
9038 struct frame *f = XFRAME (w->frame);
9039 int rc = 0;
9041 /* Handle case where text has not changed, only point, and it has
9042 not moved off the frame. */
9043 if (/* Point may be in this window. */
9044 PT >= CHARPOS (startp)
9045 /* If we don't check this, we are called to move the cursor in a
9046 horizontally split window with a current matrix that doesn't
9047 fit the display. */
9048 && !windows_or_buffers_changed
9049 /* Selective display hasn't changed. */
9050 && !current_buffer->clip_changed
9051 /* If force-mode-line-update was called, really redisplay;
9052 that's how redisplay is forced after e.g. changing
9053 buffer-invisibility-spec. */
9054 && NILP (w->update_mode_line)
9055 /* Can't use this case if highlighting a region. When a
9056 region exists, cursor movement has to do more than just
9057 set the cursor. */
9058 && !(!NILP (Vtransient_mark_mode)
9059 && !NILP (current_buffer->mark_active))
9060 && NILP (w->region_showing)
9061 && NILP (Vshow_trailing_whitespace)
9062 /* Right after splitting windows, last_point may be nil. */
9063 && INTEGERP (w->last_point)
9064 /* This code is not used for mini-buffer for the sake of the case
9065 of redisplaying to replace an echo area message; since in
9066 that case the mini-buffer contents per se are usually
9067 unchanged. This code is of no real use in the mini-buffer
9068 since the handling of this_line_start_pos, etc., in redisplay
9069 handles the same cases. */
9070 && !EQ (window, minibuf_window)
9071 /* When splitting windows or for new windows, it happens that
9072 redisplay is called with a nil window_end_vpos or one being
9073 larger than the window. This should really be fixed in
9074 window.c. I don't have this on my list, now, so we do
9075 approximately the same as the old redisplay code. --gerd. */
9076 && INTEGERP (w->window_end_vpos)
9077 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9078 && (FRAME_WINDOW_P (f)
9079 || !MARKERP (Voverlay_arrow_position)
9080 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9082 int this_scroll_margin;
9083 struct glyph_row *row;
9085 #if GLYPH_DEBUG
9086 debug_method_add (w, "cursor movement");
9087 #endif
9089 /* Scroll if point within this distance from the top or bottom
9090 of the window. This is a pixel value. */
9091 this_scroll_margin = max (0, scroll_margin);
9092 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9093 this_scroll_margin *= CANON_Y_UNIT (f);
9095 /* Start with the row the cursor was displayed during the last
9096 not paused redisplay. Give up if that row is not valid. */
9097 if (w->last_cursor.vpos < 0
9098 || w->last_cursor.vpos >= w->current_matrix->nrows)
9099 rc = -1;
9100 else
9102 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9103 if (row->mode_line_p)
9104 ++row;
9105 if (!row->enabled_p)
9106 rc = -1;
9109 if (rc == 0)
9111 int scroll_p = 0;
9112 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9115 if (PT > XFASTINT (w->last_point))
9117 /* Point has moved forward. */
9118 while (MATRIX_ROW_END_CHARPOS (row) < PT
9119 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9121 xassert (row->enabled_p);
9122 ++row;
9125 /* The end position of a row equals the start position
9126 of the next row. If PT is there, we would rather
9127 display it in the next line. */
9128 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9129 && MATRIX_ROW_END_CHARPOS (row) == PT
9130 && !cursor_row_p (w, row))
9131 ++row;
9133 /* If within the scroll margin, scroll. Note that
9134 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9135 the next line would be drawn, and that
9136 this_scroll_margin can be zero. */
9137 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9138 || PT > MATRIX_ROW_END_CHARPOS (row)
9139 /* Line is completely visible last line in window
9140 and PT is to be set in the next line. */
9141 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9142 && PT == MATRIX_ROW_END_CHARPOS (row)
9143 && !row->ends_at_zv_p
9144 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9145 scroll_p = 1;
9147 else if (PT < XFASTINT (w->last_point))
9149 /* Cursor has to be moved backward. Note that PT >=
9150 CHARPOS (startp) because of the outer
9151 if-statement. */
9152 while (!row->mode_line_p
9153 && (MATRIX_ROW_START_CHARPOS (row) > PT
9154 || (MATRIX_ROW_START_CHARPOS (row) == PT
9155 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9156 && (row->y > this_scroll_margin
9157 || CHARPOS (startp) == BEGV))
9159 xassert (row->enabled_p);
9160 --row;
9163 /* Consider the following case: Window starts at BEGV,
9164 there is invisible, intangible text at BEGV, so that
9165 display starts at some point START > BEGV. It can
9166 happen that we are called with PT somewhere between
9167 BEGV and START. Try to handle that case. */
9168 if (row < w->current_matrix->rows
9169 || row->mode_line_p)
9171 row = w->current_matrix->rows;
9172 if (row->mode_line_p)
9173 ++row;
9176 /* Due to newlines in overlay strings, we may have to
9177 skip forward over overlay strings. */
9178 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9179 && MATRIX_ROW_END_CHARPOS (row) == PT
9180 && !cursor_row_p (w, row))
9181 ++row;
9183 /* If within the scroll margin, scroll. */
9184 if (row->y < this_scroll_margin
9185 && CHARPOS (startp) != BEGV)
9186 scroll_p = 1;
9189 if (PT < MATRIX_ROW_START_CHARPOS (row)
9190 || PT > MATRIX_ROW_END_CHARPOS (row))
9192 /* if PT is not in the glyph row, give up. */
9193 rc = -1;
9195 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9197 /* If we end up in a partially visible line, let's make it
9198 fully visible, except when it's taller than the window,
9199 in which case we can't do much about it. */
9200 if (row->height > window_box_height (w))
9202 *scroll_step = 1;
9203 rc = -1;
9205 else
9207 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9208 try_window (window, startp);
9209 make_cursor_line_fully_visible (w);
9210 rc = 1;
9213 else if (scroll_p)
9214 rc = -1;
9215 else
9217 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9218 rc = 1;
9223 return rc;
9227 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9228 selected_window is redisplayed. */
9230 static void
9231 redisplay_window (window, just_this_one_p)
9232 Lisp_Object window;
9233 int just_this_one_p;
9235 struct window *w = XWINDOW (window);
9236 struct frame *f = XFRAME (w->frame);
9237 struct buffer *buffer = XBUFFER (w->buffer);
9238 struct buffer *old = current_buffer;
9239 struct text_pos lpoint, opoint, startp;
9240 int update_mode_line;
9241 int tem;
9242 struct it it;
9243 /* Record it now because it's overwritten. */
9244 int current_matrix_up_to_date_p = 0;
9245 int temp_scroll_step = 0;
9246 int count = specpdl_ptr - specpdl;
9247 int rc;
9249 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9250 opoint = lpoint;
9252 /* W must be a leaf window here. */
9253 xassert (!NILP (w->buffer));
9254 #if GLYPH_DEBUG
9255 *w->desired_matrix->method = 0;
9256 #endif
9258 specbind (Qinhibit_point_motion_hooks, Qt);
9260 reconsider_clip_changes (w, buffer);
9262 /* Has the mode line to be updated? */
9263 update_mode_line = (!NILP (w->update_mode_line)
9264 || update_mode_lines
9265 || buffer->clip_changed);
9267 if (MINI_WINDOW_P (w))
9269 if (w == XWINDOW (echo_area_window)
9270 && !NILP (echo_area_buffer[0]))
9272 if (update_mode_line)
9273 /* We may have to update a tty frame's menu bar or a
9274 tool-bar. Example `M-x C-h C-h C-g'. */
9275 goto finish_menu_bars;
9276 else
9277 /* We've already displayed the echo area glyphs in this window. */
9278 goto finish_scroll_bars;
9280 else if (w != XWINDOW (minibuf_window))
9282 /* W is a mini-buffer window, but it's not the currently
9283 active one, so clear it. */
9284 int yb = window_text_bottom_y (w);
9285 struct glyph_row *row;
9286 int y;
9288 for (y = 0, row = w->desired_matrix->rows;
9289 y < yb;
9290 y += row->height, ++row)
9291 blank_row (w, row, y);
9292 goto finish_scroll_bars;
9296 /* Otherwise set up data on this window; select its buffer and point
9297 value. */
9298 /* Really select the buffer, for the sake of buffer-local
9299 variables. */
9300 set_buffer_internal_1 (XBUFFER (w->buffer));
9301 SET_TEXT_POS (opoint, PT, PT_BYTE);
9303 current_matrix_up_to_date_p
9304 = (!NILP (w->window_end_valid)
9305 && !current_buffer->clip_changed
9306 && XFASTINT (w->last_modified) >= MODIFF
9307 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9309 /* When windows_or_buffers_changed is non-zero, we can't rely on
9310 the window end being valid, so set it to nil there. */
9311 if (windows_or_buffers_changed)
9313 /* If window starts on a continuation line, maybe adjust the
9314 window start in case the window's width changed. */
9315 if (XMARKER (w->start)->buffer == current_buffer)
9316 compute_window_start_on_continuation_line (w);
9318 w->window_end_valid = Qnil;
9321 /* Some sanity checks. */
9322 CHECK_WINDOW_END (w);
9323 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9324 abort ();
9325 if (BYTEPOS (opoint) < CHARPOS (opoint))
9326 abort ();
9328 /* If %c is in mode line, update it if needed. */
9329 if (!NILP (w->column_number_displayed)
9330 /* This alternative quickly identifies a common case
9331 where no change is needed. */
9332 && !(PT == XFASTINT (w->last_point)
9333 && XFASTINT (w->last_modified) >= MODIFF
9334 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9335 && XFASTINT (w->column_number_displayed) != current_column ())
9336 update_mode_line = 1;
9338 /* Count number of windows showing the selected buffer. An indirect
9339 buffer counts as its base buffer. */
9340 if (!just_this_one_p)
9342 struct buffer *current_base, *window_base;
9343 current_base = current_buffer;
9344 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9345 if (current_base->base_buffer)
9346 current_base = current_base->base_buffer;
9347 if (window_base->base_buffer)
9348 window_base = window_base->base_buffer;
9349 if (current_base == window_base)
9350 buffer_shared++;
9353 /* Point refers normally to the selected window. For any other
9354 window, set up appropriate value. */
9355 if (!EQ (window, selected_window))
9357 int new_pt = XMARKER (w->pointm)->charpos;
9358 int new_pt_byte = marker_byte_position (w->pointm);
9359 if (new_pt < BEGV)
9361 new_pt = BEGV;
9362 new_pt_byte = BEGV_BYTE;
9363 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9365 else if (new_pt > (ZV - 1))
9367 new_pt = ZV;
9368 new_pt_byte = ZV_BYTE;
9369 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9372 /* We don't use SET_PT so that the point-motion hooks don't run. */
9373 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9376 /* If any of the character widths specified in the display table
9377 have changed, invalidate the width run cache. It's true that
9378 this may be a bit late to catch such changes, but the rest of
9379 redisplay goes (non-fatally) haywire when the display table is
9380 changed, so why should we worry about doing any better? */
9381 if (current_buffer->width_run_cache)
9383 struct Lisp_Char_Table *disptab = buffer_display_table ();
9385 if (! disptab_matches_widthtab (disptab,
9386 XVECTOR (current_buffer->width_table)))
9388 invalidate_region_cache (current_buffer,
9389 current_buffer->width_run_cache,
9390 BEG, Z);
9391 recompute_width_table (current_buffer, disptab);
9395 /* If window-start is screwed up, choose a new one. */
9396 if (XMARKER (w->start)->buffer != current_buffer)
9397 goto recenter;
9399 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9401 /* If someone specified a new starting point but did not insist,
9402 check whether it can be used. */
9403 if (!NILP (w->optional_new_start)
9404 && CHARPOS (startp) >= BEGV
9405 && CHARPOS (startp) <= ZV)
9407 w->optional_new_start = Qnil;
9408 start_display (&it, w, startp);
9409 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9410 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9411 if (IT_CHARPOS (it) == PT)
9412 w->force_start = Qt;
9415 /* Handle case where place to start displaying has been specified,
9416 unless the specified location is outside the accessible range. */
9417 if (!NILP (w->force_start)
9418 || w->frozen_window_start_p)
9420 w->force_start = Qnil;
9421 w->vscroll = 0;
9422 w->window_end_valid = Qnil;
9424 /* Forget any recorded base line for line number display. */
9425 if (!current_matrix_up_to_date_p
9426 || current_buffer->clip_changed)
9427 w->base_line_number = Qnil;
9429 /* Redisplay the mode line. Select the buffer properly for that.
9430 Also, run the hook window-scroll-functions
9431 because we have scrolled. */
9432 /* Note, we do this after clearing force_start because
9433 if there's an error, it is better to forget about force_start
9434 than to get into an infinite loop calling the hook functions
9435 and having them get more errors. */
9436 if (!update_mode_line
9437 || ! NILP (Vwindow_scroll_functions))
9439 update_mode_line = 1;
9440 w->update_mode_line = Qt;
9441 startp = run_window_scroll_functions (window, startp);
9444 XSETFASTINT (w->last_modified, 0);
9445 XSETFASTINT (w->last_overlay_modified, 0);
9446 if (CHARPOS (startp) < BEGV)
9447 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9448 else if (CHARPOS (startp) > ZV)
9449 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9451 /* Redisplay, then check if cursor has been set during the
9452 redisplay. Give up if new fonts were loaded. */
9453 if (!try_window (window, startp))
9455 w->force_start = Qt;
9456 clear_glyph_matrix (w->desired_matrix);
9457 goto restore_buffers;
9460 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9462 /* If point does not appear, try to move point so it does
9463 appear. The desired matrix has been built above, so we
9464 can use it here. */
9465 int window_height;
9466 struct glyph_row *row;
9468 window_height = window_box_height (w) / 2;
9469 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9470 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9471 ++row;
9473 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9474 MATRIX_ROW_START_BYTEPOS (row));
9476 if (w != XWINDOW (selected_window))
9477 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9478 else if (current_buffer == old)
9479 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9481 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9483 /* If we are highlighting the region, then we just changed
9484 the region, so redisplay to show it. */
9485 if (!NILP (Vtransient_mark_mode)
9486 && !NILP (current_buffer->mark_active))
9488 clear_glyph_matrix (w->desired_matrix);
9489 if (!try_window (window, startp))
9490 goto restore_buffers;
9494 make_cursor_line_fully_visible (w);
9495 #if GLYPH_DEBUG
9496 debug_method_add (w, "forced window start");
9497 #endif
9498 goto done;
9501 /* Handle case where text has not changed, only point, and it has
9502 not moved off the frame. */
9503 if (current_matrix_up_to_date_p
9504 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9505 rc != 0))
9507 if (rc == -1)
9508 goto try_to_scroll;
9509 else
9510 goto done;
9512 /* If current starting point was originally the beginning of a line
9513 but no longer is, find a new starting point. */
9514 else if (!NILP (w->start_at_line_beg)
9515 && !(CHARPOS (startp) <= BEGV
9516 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9518 #if GLYPH_DEBUG
9519 debug_method_add (w, "recenter 1");
9520 #endif
9521 goto recenter;
9524 /* Try scrolling with try_window_id. */
9525 else if (/* Windows and buffers haven't changed. */
9526 !windows_or_buffers_changed
9527 /* Window must be either use window-based redisplay or
9528 be full width. */
9529 && (FRAME_WINDOW_P (f)
9530 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9531 && !MINI_WINDOW_P (w)
9532 /* Point is not known NOT to appear in window. */
9533 && PT >= CHARPOS (startp)
9534 && XFASTINT (w->last_modified)
9535 /* Window is not hscrolled. */
9536 && XFASTINT (w->hscroll) == 0
9537 /* Selective display has not changed. */
9538 && !current_buffer->clip_changed
9539 /* Current matrix is up to date. */
9540 && !NILP (w->window_end_valid)
9541 /* Can't use this case if highlighting a region because
9542 a cursor movement will do more than just set the cursor. */
9543 && !(!NILP (Vtransient_mark_mode)
9544 && !NILP (current_buffer->mark_active))
9545 && NILP (w->region_showing)
9546 && NILP (Vshow_trailing_whitespace)
9547 /* Overlay arrow position and string not changed. */
9548 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9549 && EQ (last_arrow_string, Voverlay_arrow_string)
9550 /* Value is > 0 if update has been done, it is -1 if we
9551 know that the same window start will not work. It is 0
9552 if unsuccessful for some other reason. */
9553 && (tem = try_window_id (w)) != 0)
9555 #if GLYPH_DEBUG
9556 debug_method_add (w, "try_window_id %d", tem);
9557 #endif
9559 if (fonts_changed_p)
9560 goto restore_buffers;
9561 if (tem > 0)
9562 goto done;
9564 /* Otherwise try_window_id has returned -1 which means that we
9565 don't want the alternative below this comment to execute. */
9567 else if (CHARPOS (startp) >= BEGV
9568 && CHARPOS (startp) <= ZV
9569 && PT >= CHARPOS (startp)
9570 && (CHARPOS (startp) < ZV
9571 /* Avoid starting at end of buffer. */
9572 || CHARPOS (startp) == BEGV
9573 || (XFASTINT (w->last_modified) >= MODIFF
9574 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9576 #if GLYPH_DEBUG
9577 debug_method_add (w, "same window start");
9578 #endif
9580 /* Try to redisplay starting at same place as before.
9581 If point has not moved off frame, accept the results. */
9582 if (!current_matrix_up_to_date_p
9583 /* Don't use try_window_reusing_current_matrix in this case
9584 because a window scroll function can have changed the
9585 buffer. */
9586 || !NILP (Vwindow_scroll_functions)
9587 || MINI_WINDOW_P (w)
9588 || !try_window_reusing_current_matrix (w))
9590 IF_DEBUG (debug_method_add (w, "1"));
9591 try_window (window, startp);
9594 if (fonts_changed_p)
9595 goto restore_buffers;
9597 if (w->cursor.vpos >= 0)
9599 if (!just_this_one_p
9600 || current_buffer->clip_changed
9601 || BEG_UNCHANGED < CHARPOS (startp))
9602 /* Forget any recorded base line for line number display. */
9603 w->base_line_number = Qnil;
9605 make_cursor_line_fully_visible (w);
9606 goto done;
9608 else
9609 clear_glyph_matrix (w->desired_matrix);
9612 try_to_scroll:
9614 XSETFASTINT (w->last_modified, 0);
9615 XSETFASTINT (w->last_overlay_modified, 0);
9617 /* Redisplay the mode line. Select the buffer properly for that. */
9618 if (!update_mode_line)
9620 update_mode_line = 1;
9621 w->update_mode_line = Qt;
9624 /* Try to scroll by specified few lines. */
9625 if ((scroll_conservatively
9626 || scroll_step
9627 || temp_scroll_step
9628 || NUMBERP (current_buffer->scroll_up_aggressively)
9629 || NUMBERP (current_buffer->scroll_down_aggressively))
9630 && !current_buffer->clip_changed
9631 && CHARPOS (startp) >= BEGV
9632 && CHARPOS (startp) <= ZV)
9634 /* The function returns -1 if new fonts were loaded, 1 if
9635 successful, 0 if not successful. */
9636 int rc = try_scrolling (window, just_this_one_p,
9637 scroll_conservatively,
9638 scroll_step,
9639 temp_scroll_step);
9640 if (rc > 0)
9641 goto done;
9642 else if (rc < 0)
9643 goto restore_buffers;
9646 /* Finally, just choose place to start which centers point */
9648 recenter:
9650 #if GLYPH_DEBUG
9651 debug_method_add (w, "recenter");
9652 #endif
9654 /* w->vscroll = 0; */
9656 /* Forget any previously recorded base line for line number display. */
9657 if (!current_matrix_up_to_date_p
9658 || current_buffer->clip_changed)
9659 w->base_line_number = Qnil;
9661 /* Move backward half the height of the window. */
9662 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9663 it.current_y = it.last_visible_y;
9664 move_it_vertically_backward (&it, it.last_visible_y / 2);
9665 xassert (IT_CHARPOS (it) >= BEGV);
9667 /* The function move_it_vertically_backward may move over more
9668 than the specified y-distance. If it->w is small, e.g. a
9669 mini-buffer window, we may end up in front of the window's
9670 display area. Start displaying at the start of the line
9671 containing PT in this case. */
9672 if (it.current_y <= 0)
9674 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9675 move_it_vertically (&it, 0);
9676 xassert (IT_CHARPOS (it) <= PT);
9677 it.current_y = 0;
9680 it.current_x = it.hpos = 0;
9682 /* Set startp here explicitly in case that helps avoid an infinite loop
9683 in case the window-scroll-functions functions get errors. */
9684 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9686 /* Run scroll hooks. */
9687 startp = run_window_scroll_functions (window, it.current.pos);
9689 /* Redisplay the window. */
9690 if (!current_matrix_up_to_date_p
9691 || windows_or_buffers_changed
9692 /* Don't use try_window_reusing_current_matrix in this case
9693 because it can have changed the buffer. */
9694 || !NILP (Vwindow_scroll_functions)
9695 || !just_this_one_p
9696 || MINI_WINDOW_P (w)
9697 || !try_window_reusing_current_matrix (w))
9698 try_window (window, startp);
9700 /* If new fonts have been loaded (due to fontsets), give up. We
9701 have to start a new redisplay since we need to re-adjust glyph
9702 matrices. */
9703 if (fonts_changed_p)
9704 goto restore_buffers;
9706 /* If cursor did not appear assume that the middle of the window is
9707 in the first line of the window. Do it again with the next line.
9708 (Imagine a window of height 100, displaying two lines of height
9709 60. Moving back 50 from it->last_visible_y will end in the first
9710 line.) */
9711 if (w->cursor.vpos < 0)
9713 if (!NILP (w->window_end_valid)
9714 && PT >= Z - XFASTINT (w->window_end_pos))
9716 clear_glyph_matrix (w->desired_matrix);
9717 move_it_by_lines (&it, 1, 0);
9718 try_window (window, it.current.pos);
9720 else if (PT < IT_CHARPOS (it))
9722 clear_glyph_matrix (w->desired_matrix);
9723 move_it_by_lines (&it, -1, 0);
9724 try_window (window, it.current.pos);
9726 else
9728 /* Not much we can do about it. */
9732 /* Consider the following case: Window starts at BEGV, there is
9733 invisible, intangible text at BEGV, so that display starts at
9734 some point START > BEGV. It can happen that we are called with
9735 PT somewhere between BEGV and START. Try to handle that case. */
9736 if (w->cursor.vpos < 0)
9738 struct glyph_row *row = w->current_matrix->rows;
9739 if (row->mode_line_p)
9740 ++row;
9741 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9744 make_cursor_line_fully_visible (w);
9746 done:
9748 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9749 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9750 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9751 ? Qt : Qnil);
9753 /* Display the mode line, if we must. */
9754 if ((update_mode_line
9755 /* If window not full width, must redo its mode line
9756 if (a) the window to its side is being redone and
9757 (b) we do a frame-based redisplay. This is a consequence
9758 of how inverted lines are drawn in frame-based redisplay. */
9759 || (!just_this_one_p
9760 && !FRAME_WINDOW_P (f)
9761 && !WINDOW_FULL_WIDTH_P (w))
9762 /* Line number to display. */
9763 || INTEGERP (w->base_line_pos)
9764 /* Column number is displayed and different from the one displayed. */
9765 || (!NILP (w->column_number_displayed)
9766 && XFASTINT (w->column_number_displayed) != current_column ()))
9767 /* This means that the window has a mode line. */
9768 && (WINDOW_WANTS_MODELINE_P (w)
9769 || WINDOW_WANTS_HEADER_LINE_P (w)))
9771 Lisp_Object old_selected_frame;
9773 old_selected_frame = selected_frame;
9775 XSETFRAME (selected_frame, f);
9776 display_mode_lines (w);
9777 selected_frame = old_selected_frame;
9779 /* If mode line height has changed, arrange for a thorough
9780 immediate redisplay using the correct mode line height. */
9781 if (WINDOW_WANTS_MODELINE_P (w)
9782 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9784 fonts_changed_p = 1;
9785 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9786 = DESIRED_MODE_LINE_HEIGHT (w);
9789 /* If top line height has changed, arrange for a thorough
9790 immediate redisplay using the correct mode line height. */
9791 if (WINDOW_WANTS_HEADER_LINE_P (w)
9792 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9794 fonts_changed_p = 1;
9795 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9796 = DESIRED_HEADER_LINE_HEIGHT (w);
9799 if (fonts_changed_p)
9800 goto restore_buffers;
9803 if (!line_number_displayed
9804 && !BUFFERP (w->base_line_pos))
9806 w->base_line_pos = Qnil;
9807 w->base_line_number = Qnil;
9810 finish_menu_bars:
9812 /* When we reach a frame's selected window, redo the frame's menu bar. */
9813 if (update_mode_line
9814 && EQ (FRAME_SELECTED_WINDOW (f), window))
9816 int redisplay_menu_p = 0;
9818 if (FRAME_WINDOW_P (f))
9820 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9821 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9822 #else
9823 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9824 #endif
9826 else
9827 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9829 if (redisplay_menu_p)
9830 display_menu_bar (w);
9832 #ifdef HAVE_WINDOW_SYSTEM
9833 if (WINDOWP (f->tool_bar_window)
9834 && (FRAME_TOOL_BAR_LINES (f) > 0
9835 || auto_resize_tool_bars_p))
9836 redisplay_tool_bar (f);
9837 #endif
9840 finish_scroll_bars:
9842 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9844 int start, end, whole;
9846 /* Calculate the start and end positions for the current window.
9847 At some point, it would be nice to choose between scrollbars
9848 which reflect the whole buffer size, with special markers
9849 indicating narrowing, and scrollbars which reflect only the
9850 visible region.
9852 Note that mini-buffers sometimes aren't displaying any text. */
9853 if (!MINI_WINDOW_P (w)
9854 || (w == XWINDOW (minibuf_window)
9855 && NILP (echo_area_buffer[0])))
9857 whole = ZV - BEGV;
9858 start = marker_position (w->start) - BEGV;
9859 /* I don't think this is guaranteed to be right. For the
9860 moment, we'll pretend it is. */
9861 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9863 if (end < start)
9864 end = start;
9865 if (whole < (end - start))
9866 whole = end - start;
9868 else
9869 start = end = whole = 0;
9871 /* Indicate what this scroll bar ought to be displaying now. */
9872 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9874 /* Note that we actually used the scroll bar attached to this
9875 window, so it shouldn't be deleted at the end of redisplay. */
9876 (*redeem_scroll_bar_hook) (w);
9879 restore_buffers:
9881 /* Restore current_buffer and value of point in it. */
9882 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9883 set_buffer_internal_1 (old);
9884 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9886 unbind_to (count, Qnil);
9890 /* Build the complete desired matrix of WINDOW with a window start
9891 buffer position POS. Value is non-zero if successful. It is zero
9892 if fonts were loaded during redisplay which makes re-adjusting
9893 glyph matrices necessary. */
9896 try_window (window, pos)
9897 Lisp_Object window;
9898 struct text_pos pos;
9900 struct window *w = XWINDOW (window);
9901 struct it it;
9902 struct glyph_row *last_text_row = NULL;
9904 /* Make POS the new window start. */
9905 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9907 /* Mark cursor position as unknown. No overlay arrow seen. */
9908 w->cursor.vpos = -1;
9909 overlay_arrow_seen = 0;
9911 /* Initialize iterator and info to start at POS. */
9912 start_display (&it, w, pos);
9914 /* Display all lines of W. */
9915 while (it.current_y < it.last_visible_y)
9917 if (display_line (&it))
9918 last_text_row = it.glyph_row - 1;
9919 if (fonts_changed_p)
9920 return 0;
9923 /* If bottom moved off end of frame, change mode line percentage. */
9924 if (XFASTINT (w->window_end_pos) <= 0
9925 && Z != IT_CHARPOS (it))
9926 w->update_mode_line = Qt;
9928 /* Set window_end_pos to the offset of the last character displayed
9929 on the window from the end of current_buffer. Set
9930 window_end_vpos to its row number. */
9931 if (last_text_row)
9933 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9934 w->window_end_bytepos
9935 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9936 XSETFASTINT (w->window_end_pos,
9937 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9938 XSETFASTINT (w->window_end_vpos,
9939 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9940 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9941 ->displays_text_p);
9943 else
9945 w->window_end_bytepos = 0;
9946 XSETFASTINT (w->window_end_pos, 0);
9947 XSETFASTINT (w->window_end_vpos, 0);
9950 /* But that is not valid info until redisplay finishes. */
9951 w->window_end_valid = Qnil;
9952 return 1;
9957 /************************************************************************
9958 Window redisplay reusing current matrix when buffer has not changed
9959 ************************************************************************/
9961 /* Try redisplay of window W showing an unchanged buffer with a
9962 different window start than the last time it was displayed by
9963 reusing its current matrix. Value is non-zero if successful.
9964 W->start is the new window start. */
9966 static int
9967 try_window_reusing_current_matrix (w)
9968 struct window *w;
9970 struct frame *f = XFRAME (w->frame);
9971 struct glyph_row *row, *bottom_row;
9972 struct it it;
9973 struct run run;
9974 struct text_pos start, new_start;
9975 int nrows_scrolled, i;
9976 struct glyph_row *last_text_row;
9977 struct glyph_row *last_reused_text_row;
9978 struct glyph_row *start_row;
9979 int start_vpos, min_y, max_y;
9981 if (/* This function doesn't handle terminal frames. */
9982 !FRAME_WINDOW_P (f)
9983 /* Don't try to reuse the display if windows have been split
9984 or such. */
9985 || windows_or_buffers_changed)
9986 return 0;
9988 /* Can't do this if region may have changed. */
9989 if ((!NILP (Vtransient_mark_mode)
9990 && !NILP (current_buffer->mark_active))
9991 || !NILP (w->region_showing)
9992 || !NILP (Vshow_trailing_whitespace))
9993 return 0;
9995 /* If top-line visibility has changed, give up. */
9996 if (WINDOW_WANTS_HEADER_LINE_P (w)
9997 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9998 return 0;
10000 /* Give up if old or new display is scrolled vertically. We could
10001 make this function handle this, but right now it doesn't. */
10002 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10003 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10004 return 0;
10006 /* The variable new_start now holds the new window start. The old
10007 start `start' can be determined from the current matrix. */
10008 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10009 start = start_row->start.pos;
10010 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10012 /* Clear the desired matrix for the display below. */
10013 clear_glyph_matrix (w->desired_matrix);
10015 if (CHARPOS (new_start) <= CHARPOS (start))
10017 int first_row_y;
10019 IF_DEBUG (debug_method_add (w, "twu1"));
10021 /* Display up to a row that can be reused. The variable
10022 last_text_row is set to the last row displayed that displays
10023 text. Note that it.vpos == 0 if or if not there is a
10024 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10025 start_display (&it, w, new_start);
10026 first_row_y = it.current_y;
10027 w->cursor.vpos = -1;
10028 last_text_row = last_reused_text_row = NULL;
10030 while (it.current_y < it.last_visible_y
10031 && IT_CHARPOS (it) < CHARPOS (start)
10032 && !fonts_changed_p)
10033 if (display_line (&it))
10034 last_text_row = it.glyph_row - 1;
10036 /* A value of current_y < last_visible_y means that we stopped
10037 at the previous window start, which in turn means that we
10038 have at least one reusable row. */
10039 if (it.current_y < it.last_visible_y)
10041 /* IT.vpos always starts from 0; it counts text lines. */
10042 nrows_scrolled = it.vpos;
10044 /* Find PT if not already found in the lines displayed. */
10045 if (w->cursor.vpos < 0)
10047 int dy = it.current_y - first_row_y;
10049 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10050 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10052 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10053 && PT < MATRIX_ROW_END_CHARPOS (row))
10055 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10056 dy, nrows_scrolled);
10057 break;
10060 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10061 break;
10063 ++row;
10066 /* Give up if point was not found. This shouldn't
10067 happen often; not more often than with try_window
10068 itself. */
10069 if (w->cursor.vpos < 0)
10071 clear_glyph_matrix (w->desired_matrix);
10072 return 0;
10076 /* Scroll the display. Do it before the current matrix is
10077 changed. The problem here is that update has not yet
10078 run, i.e. part of the current matrix is not up to date.
10079 scroll_run_hook will clear the cursor, and use the
10080 current matrix to get the height of the row the cursor is
10081 in. */
10082 run.current_y = first_row_y;
10083 run.desired_y = it.current_y;
10084 run.height = it.last_visible_y - it.current_y;
10086 if (run.height > 0 && run.current_y != run.desired_y)
10088 update_begin (f);
10089 rif->update_window_begin_hook (w);
10090 rif->clear_mouse_face (w);
10091 rif->scroll_run_hook (w, &run);
10092 rif->update_window_end_hook (w, 0, 0);
10093 update_end (f);
10096 /* Shift current matrix down by nrows_scrolled lines. */
10097 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10098 rotate_matrix (w->current_matrix,
10099 start_vpos,
10100 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10101 nrows_scrolled);
10103 /* Disable lines not reused. */
10104 for (i = 0; i < it.vpos; ++i)
10105 (start_row + i)->enabled_p = 0;
10107 /* Re-compute Y positions. */
10108 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10109 max_y = it.last_visible_y;
10110 for (row = start_row + nrows_scrolled;
10111 row < bottom_row;
10112 ++row)
10114 row->y = it.current_y;
10116 if (row->y < min_y)
10117 row->visible_height = row->height - (min_y - row->y);
10118 else if (row->y + row->height > max_y)
10119 row->visible_height
10120 = row->height - (row->y + row->height - max_y);
10121 else
10122 row->visible_height = row->height;
10124 it.current_y += row->height;
10126 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10127 last_reused_text_row = row;
10128 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10129 break;
10133 /* Update window_end_pos etc.; last_reused_text_row is the last
10134 reused row from the current matrix containing text, if any.
10135 The value of last_text_row is the last displayed line
10136 containing text. */
10137 if (last_reused_text_row)
10139 w->window_end_bytepos
10140 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10141 XSETFASTINT (w->window_end_pos,
10142 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10143 XSETFASTINT (w->window_end_vpos,
10144 MATRIX_ROW_VPOS (last_reused_text_row,
10145 w->current_matrix));
10147 else if (last_text_row)
10149 w->window_end_bytepos
10150 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10151 XSETFASTINT (w->window_end_pos,
10152 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10153 XSETFASTINT (w->window_end_vpos,
10154 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10156 else
10158 /* This window must be completely empty. */
10159 w->window_end_bytepos = 0;
10160 XSETFASTINT (w->window_end_pos, 0);
10161 XSETFASTINT (w->window_end_vpos, 0);
10163 w->window_end_valid = Qnil;
10165 /* Update hint: don't try scrolling again in update_window. */
10166 w->desired_matrix->no_scrolling_p = 1;
10168 #if GLYPH_DEBUG
10169 debug_method_add (w, "try_window_reusing_current_matrix 1");
10170 #endif
10171 return 1;
10173 else if (CHARPOS (new_start) > CHARPOS (start))
10175 struct glyph_row *pt_row, *row;
10176 struct glyph_row *first_reusable_row;
10177 struct glyph_row *first_row_to_display;
10178 int dy;
10179 int yb = window_text_bottom_y (w);
10181 IF_DEBUG (debug_method_add (w, "twu2"));
10183 /* Find the row starting at new_start, if there is one. Don't
10184 reuse a partially visible line at the end. */
10185 first_reusable_row = start_row;
10186 while (first_reusable_row->enabled_p
10187 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10188 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10189 < CHARPOS (new_start)))
10190 ++first_reusable_row;
10192 /* Give up if there is no row to reuse. */
10193 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10194 || !first_reusable_row->enabled_p
10195 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10196 != CHARPOS (new_start)))
10197 return 0;
10199 /* We can reuse fully visible rows beginning with
10200 first_reusable_row to the end of the window. Set
10201 first_row_to_display to the first row that cannot be reused.
10202 Set pt_row to the row containing point, if there is any. */
10203 first_row_to_display = first_reusable_row;
10204 pt_row = NULL;
10205 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10207 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10208 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10209 pt_row = first_row_to_display;
10211 ++first_row_to_display;
10214 /* Start displaying at the start of first_row_to_display. */
10215 xassert (first_row_to_display->y < yb);
10216 init_to_row_start (&it, w, first_row_to_display);
10217 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10218 - start_vpos);
10219 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10220 - nrows_scrolled);
10221 it.current_y = (first_row_to_display->y - first_reusable_row->y
10222 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10224 /* Display lines beginning with first_row_to_display in the
10225 desired matrix. Set last_text_row to the last row displayed
10226 that displays text. */
10227 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10228 if (pt_row == NULL)
10229 w->cursor.vpos = -1;
10230 last_text_row = NULL;
10231 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10232 if (display_line (&it))
10233 last_text_row = it.glyph_row - 1;
10235 /* Give up If point isn't in a row displayed or reused. */
10236 if (w->cursor.vpos < 0)
10238 clear_glyph_matrix (w->desired_matrix);
10239 return 0;
10242 /* If point is in a reused row, adjust y and vpos of the cursor
10243 position. */
10244 if (pt_row)
10246 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10247 w->current_matrix);
10248 w->cursor.y -= first_reusable_row->y;
10251 /* Scroll the display. */
10252 run.current_y = first_reusable_row->y;
10253 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10254 run.height = it.last_visible_y - run.current_y;
10255 dy = run.current_y - run.desired_y;
10257 if (run.height)
10259 struct frame *f = XFRAME (WINDOW_FRAME (w));
10260 update_begin (f);
10261 rif->update_window_begin_hook (w);
10262 rif->clear_mouse_face (w);
10263 rif->scroll_run_hook (w, &run);
10264 rif->update_window_end_hook (w, 0, 0);
10265 update_end (f);
10268 /* Adjust Y positions of reused rows. */
10269 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10270 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10271 max_y = it.last_visible_y;
10272 for (row = first_reusable_row; row < first_row_to_display; ++row)
10274 row->y -= dy;
10275 if (row->y < min_y)
10276 row->visible_height = row->height - (min_y - row->y);
10277 else if (row->y + row->height > max_y)
10278 row->visible_height
10279 = row->height - (row->y + row->height - max_y);
10280 else
10281 row->visible_height = row->height;
10284 /* Disable rows not reused. */
10285 while (row < bottom_row)
10287 row->enabled_p = 0;
10288 ++row;
10291 /* Scroll the current matrix. */
10292 xassert (nrows_scrolled > 0);
10293 rotate_matrix (w->current_matrix,
10294 start_vpos,
10295 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10296 -nrows_scrolled);
10298 /* Adjust window end. A null value of last_text_row means that
10299 the window end is in reused rows which in turn means that
10300 only its vpos can have changed. */
10301 if (last_text_row)
10303 w->window_end_bytepos
10304 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10305 XSETFASTINT (w->window_end_pos,
10306 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10307 XSETFASTINT (w->window_end_vpos,
10308 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10310 else
10312 XSETFASTINT (w->window_end_vpos,
10313 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10316 w->window_end_valid = Qnil;
10317 w->desired_matrix->no_scrolling_p = 1;
10319 #if GLYPH_DEBUG
10320 debug_method_add (w, "try_window_reusing_current_matrix 2");
10321 #endif
10322 return 1;
10325 return 0;
10330 /************************************************************************
10331 Window redisplay reusing current matrix when buffer has changed
10332 ************************************************************************/
10334 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10335 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10336 int *, int *));
10337 static struct glyph_row *
10338 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10339 struct glyph_row *));
10342 /* Return the last row in MATRIX displaying text. If row START is
10343 non-null, start searching with that row. IT gives the dimensions
10344 of the display. Value is null if matrix is empty; otherwise it is
10345 a pointer to the row found. */
10347 static struct glyph_row *
10348 find_last_row_displaying_text (matrix, it, start)
10349 struct glyph_matrix *matrix;
10350 struct it *it;
10351 struct glyph_row *start;
10353 struct glyph_row *row, *row_found;
10355 /* Set row_found to the last row in IT->w's current matrix
10356 displaying text. The loop looks funny but think of partially
10357 visible lines. */
10358 row_found = NULL;
10359 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10360 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10362 xassert (row->enabled_p);
10363 row_found = row;
10364 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10365 break;
10366 ++row;
10369 return row_found;
10373 /* Return the last row in the current matrix of W that is not affected
10374 by changes at the start of current_buffer that occurred since the
10375 last time W was redisplayed. Value is null if no such row exists.
10377 The global variable beg_unchanged has to contain the number of
10378 bytes unchanged at the start of current_buffer. BEG +
10379 beg_unchanged is the buffer position of the first changed byte in
10380 current_buffer. Characters at positions < BEG + beg_unchanged are
10381 at the same buffer positions as they were when the current matrix
10382 was built. */
10384 static struct glyph_row *
10385 find_last_unchanged_at_beg_row (w)
10386 struct window *w;
10388 int first_changed_pos = BEG + BEG_UNCHANGED;
10389 struct glyph_row *row;
10390 struct glyph_row *row_found = NULL;
10391 int yb = window_text_bottom_y (w);
10393 /* Find the last row displaying unchanged text. */
10394 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10395 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10396 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10398 if (/* If row ends before first_changed_pos, it is unchanged,
10399 except in some case. */
10400 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10401 /* When row ends in ZV and we write at ZV it is not
10402 unchanged. */
10403 && !row->ends_at_zv_p
10404 /* When first_changed_pos is the end of a continued line,
10405 row is not unchanged because it may be no longer
10406 continued. */
10407 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10408 && row->continued_p))
10409 row_found = row;
10411 /* Stop if last visible row. */
10412 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10413 break;
10415 ++row;
10418 return row_found;
10422 /* Find the first glyph row in the current matrix of W that is not
10423 affected by changes at the end of current_buffer since the last
10424 time the window was redisplayed. Return in *DELTA the number of
10425 chars by which buffer positions in unchanged text at the end of
10426 current_buffer must be adjusted. Return in *DELTA_BYTES the
10427 corresponding number of bytes. Value is null if no such row
10428 exists, i.e. all rows are affected by changes. */
10430 static struct glyph_row *
10431 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10432 struct window *w;
10433 int *delta, *delta_bytes;
10435 struct glyph_row *row;
10436 struct glyph_row *row_found = NULL;
10438 *delta = *delta_bytes = 0;
10440 /* Display must not have been paused, otherwise the current matrix
10441 is not up to date. */
10442 if (NILP (w->window_end_valid))
10443 abort ();
10445 /* A value of window_end_pos >= END_UNCHANGED means that the window
10446 end is in the range of changed text. If so, there is no
10447 unchanged row at the end of W's current matrix. */
10448 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10449 return NULL;
10451 /* Set row to the last row in W's current matrix displaying text. */
10452 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10454 /* If matrix is entirely empty, no unchanged row exists. */
10455 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10457 /* The value of row is the last glyph row in the matrix having a
10458 meaningful buffer position in it. The end position of row
10459 corresponds to window_end_pos. This allows us to translate
10460 buffer positions in the current matrix to current buffer
10461 positions for characters not in changed text. */
10462 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10463 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10464 int last_unchanged_pos, last_unchanged_pos_old;
10465 struct glyph_row *first_text_row
10466 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10468 *delta = Z - Z_old;
10469 *delta_bytes = Z_BYTE - Z_BYTE_old;
10471 /* Set last_unchanged_pos to the buffer position of the last
10472 character in the buffer that has not been changed. Z is the
10473 index + 1 of the last byte in current_buffer, i.e. by
10474 subtracting end_unchanged we get the index of the last
10475 unchanged character, and we have to add BEG to get its buffer
10476 position. */
10477 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10478 last_unchanged_pos_old = last_unchanged_pos - *delta;
10480 /* Search backward from ROW for a row displaying a line that
10481 starts at a minimum position >= last_unchanged_pos_old. */
10482 for (; row > first_text_row; --row)
10484 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10485 abort ();
10487 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10488 row_found = row;
10492 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10493 abort ();
10495 return row_found;
10499 /* Make sure that glyph rows in the current matrix of window W
10500 reference the same glyph memory as corresponding rows in the
10501 frame's frame matrix. This function is called after scrolling W's
10502 current matrix on a terminal frame in try_window_id and
10503 try_window_reusing_current_matrix. */
10505 static void
10506 sync_frame_with_window_matrix_rows (w)
10507 struct window *w;
10509 struct frame *f = XFRAME (w->frame);
10510 struct glyph_row *window_row, *window_row_end, *frame_row;
10512 /* Preconditions: W must be a leaf window and full-width. Its frame
10513 must have a frame matrix. */
10514 xassert (NILP (w->hchild) && NILP (w->vchild));
10515 xassert (WINDOW_FULL_WIDTH_P (w));
10516 xassert (!FRAME_WINDOW_P (f));
10518 /* If W is a full-width window, glyph pointers in W's current matrix
10519 have, by definition, to be the same as glyph pointers in the
10520 corresponding frame matrix. */
10521 window_row = w->current_matrix->rows;
10522 window_row_end = window_row + w->current_matrix->nrows;
10523 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10524 while (window_row < window_row_end)
10526 int area;
10528 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10529 frame_row->glyphs[area] = window_row->glyphs[area];
10531 /* Disable frame rows whose corresponding window rows have
10532 been disabled in try_window_id. */
10533 if (!window_row->enabled_p)
10534 frame_row->enabled_p = 0;
10536 ++window_row, ++frame_row;
10541 /* Find the glyph row in window W containing CHARPOS. Consider all
10542 rows between START and END (not inclusive). END null means search
10543 all rows to the end of the display area of W. Value is the row
10544 containing CHARPOS or null. */
10546 static struct glyph_row *
10547 row_containing_pos (w, charpos, start, end)
10548 struct window *w;
10549 int charpos;
10550 struct glyph_row *start, *end;
10552 struct glyph_row *row = start;
10553 int last_y;
10555 /* If we happen to start on a header-line, skip that. */
10556 if (row->mode_line_p)
10557 ++row;
10559 if ((end && row >= end) || !row->enabled_p)
10560 return NULL;
10562 last_y = window_text_bottom_y (w);
10564 while ((end == NULL || row < end)
10565 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10566 /* The end position of a row equals the start
10567 position of the next row. If CHARPOS is there, we
10568 would rather display it in the next line, except
10569 when this line ends in ZV. */
10570 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10571 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10572 || !row->ends_at_zv_p)))
10573 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10574 ++row;
10576 /* Give up if CHARPOS not found. */
10577 if ((end && row >= end)
10578 || charpos < MATRIX_ROW_START_CHARPOS (row)
10579 || charpos > MATRIX_ROW_END_CHARPOS (row))
10580 row = NULL;
10582 return row;
10586 /* Try to redisplay window W by reusing its existing display. W's
10587 current matrix must be up to date when this function is called,
10588 i.e. window_end_valid must not be nil.
10590 Value is
10592 1 if display has been updated
10593 0 if otherwise unsuccessful
10594 -1 if redisplay with same window start is known not to succeed
10596 The following steps are performed:
10598 1. Find the last row in the current matrix of W that is not
10599 affected by changes at the start of current_buffer. If no such row
10600 is found, give up.
10602 2. Find the first row in W's current matrix that is not affected by
10603 changes at the end of current_buffer. Maybe there is no such row.
10605 3. Display lines beginning with the row + 1 found in step 1 to the
10606 row found in step 2 or, if step 2 didn't find a row, to the end of
10607 the window.
10609 4. If cursor is not known to appear on the window, give up.
10611 5. If display stopped at the row found in step 2, scroll the
10612 display and current matrix as needed.
10614 6. Maybe display some lines at the end of W, if we must. This can
10615 happen under various circumstances, like a partially visible line
10616 becoming fully visible, or because newly displayed lines are displayed
10617 in smaller font sizes.
10619 7. Update W's window end information. */
10621 /* Check that window end is what we expect it to be. */
10623 static int
10624 try_window_id (w)
10625 struct window *w;
10627 struct frame *f = XFRAME (w->frame);
10628 struct glyph_matrix *current_matrix = w->current_matrix;
10629 struct glyph_matrix *desired_matrix = w->desired_matrix;
10630 struct glyph_row *last_unchanged_at_beg_row;
10631 struct glyph_row *first_unchanged_at_end_row;
10632 struct glyph_row *row;
10633 struct glyph_row *bottom_row;
10634 int bottom_vpos;
10635 struct it it;
10636 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10637 struct text_pos start_pos;
10638 struct run run;
10639 int first_unchanged_at_end_vpos = 0;
10640 struct glyph_row *last_text_row, *last_text_row_at_end;
10641 struct text_pos start;
10643 SET_TEXT_POS_FROM_MARKER (start, w->start);
10645 /* Check pre-conditions. Window end must be valid, otherwise
10646 the current matrix would not be up to date. */
10647 xassert (!NILP (w->window_end_valid));
10648 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10649 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10651 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10652 only if buffer has really changed. The reason is that the gap is
10653 initially at Z for freshly visited files. The code below would
10654 set end_unchanged to 0 in that case. */
10655 if (MODIFF > SAVE_MODIFF
10656 /* This seems to happen sometimes after saving a buffer. */
10657 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10659 if (GPT - BEG < BEG_UNCHANGED)
10660 BEG_UNCHANGED = GPT - BEG;
10661 if (Z - GPT < END_UNCHANGED)
10662 END_UNCHANGED = Z - GPT;
10665 /* If window starts after a line end, and the last change is in
10666 front of that newline, then changes don't affect the display.
10667 This case happens with stealth-fontification. Note that although
10668 the display is unchanged, glyph positions in the matrix have to
10669 be adjusted, of course. */
10670 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10671 if (CHARPOS (start) > BEGV
10672 && Z - END_UNCHANGED < CHARPOS (start) - 1
10673 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10674 && PT < MATRIX_ROW_END_CHARPOS (row))
10676 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10677 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10679 if (delta)
10681 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10682 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10684 increment_matrix_positions (w->current_matrix,
10685 MATRIX_ROW_VPOS (r0, current_matrix),
10686 MATRIX_ROW_VPOS (r1, current_matrix),
10687 delta, delta_bytes);
10690 #if 0 /* If changes are all in front of the window start, the
10691 distance of the last displayed glyph from Z hasn't
10692 changed. */
10693 w->window_end_pos
10694 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10695 w->window_end_bytepos
10696 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10697 #endif
10699 return 1;
10702 /* Return quickly if changes are all below what is displayed in the
10703 window, and if PT is in the window. */
10704 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10705 && PT < MATRIX_ROW_END_CHARPOS (row))
10707 /* We have to update window end positions because the buffer's
10708 size has changed. */
10709 w->window_end_pos
10710 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10711 w->window_end_bytepos
10712 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10714 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10715 row = row_containing_pos (w, PT, row, NULL);
10716 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10717 return 2;
10720 /* Check that window start agrees with the start of the first glyph
10721 row in its current matrix. Check this after we know the window
10722 start is not in changed text, otherwise positions would not be
10723 comparable. */
10724 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10725 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10726 return 0;
10728 /* Compute the position at which we have to start displaying new
10729 lines. Some of the lines at the top of the window might be
10730 reusable because they are not displaying changed text. Find the
10731 last row in W's current matrix not affected by changes at the
10732 start of current_buffer. Value is null if changes start in the
10733 first line of window. */
10734 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10735 if (last_unchanged_at_beg_row)
10737 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10738 start_pos = it.current.pos;
10740 /* Start displaying new lines in the desired matrix at the same
10741 vpos we would use in the current matrix, i.e. below
10742 last_unchanged_at_beg_row. */
10743 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10744 current_matrix);
10745 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10746 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10748 xassert (it.hpos == 0 && it.current_x == 0);
10750 else
10752 /* There are no reusable lines at the start of the window.
10753 Start displaying in the first line. */
10754 start_display (&it, w, start);
10755 start_pos = it.current.pos;
10758 /* Find the first row that is not affected by changes at the end of
10759 the buffer. Value will be null if there is no unchanged row, in
10760 which case we must redisplay to the end of the window. delta
10761 will be set to the value by which buffer positions beginning with
10762 first_unchanged_at_end_row have to be adjusted due to text
10763 changes. */
10764 first_unchanged_at_end_row
10765 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10766 IF_DEBUG (debug_delta = delta);
10767 IF_DEBUG (debug_delta_bytes = delta_bytes);
10769 /* Set stop_pos to the buffer position up to which we will have to
10770 display new lines. If first_unchanged_at_end_row != NULL, this
10771 is the buffer position of the start of the line displayed in that
10772 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10773 that we don't stop at a buffer position. */
10774 stop_pos = 0;
10775 if (first_unchanged_at_end_row)
10777 xassert (last_unchanged_at_beg_row == NULL
10778 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10780 /* If this is a continuation line, move forward to the next one
10781 that isn't. Changes in lines above affect this line.
10782 Caution: this may move first_unchanged_at_end_row to a row
10783 not displaying text. */
10784 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10785 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10786 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10787 < it.last_visible_y))
10788 ++first_unchanged_at_end_row;
10790 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10791 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10792 >= it.last_visible_y))
10793 first_unchanged_at_end_row = NULL;
10794 else
10796 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10797 + delta);
10798 first_unchanged_at_end_vpos
10799 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10800 xassert (stop_pos >= Z - END_UNCHANGED);
10803 else if (last_unchanged_at_beg_row == NULL)
10804 return 0;
10807 #if GLYPH_DEBUG
10809 /* Either there is no unchanged row at the end, or the one we have
10810 now displays text. This is a necessary condition for the window
10811 end pos calculation at the end of this function. */
10812 xassert (first_unchanged_at_end_row == NULL
10813 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10815 debug_last_unchanged_at_beg_vpos
10816 = (last_unchanged_at_beg_row
10817 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10818 : -1);
10819 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10821 #endif /* GLYPH_DEBUG != 0 */
10824 /* Display new lines. Set last_text_row to the last new line
10825 displayed which has text on it, i.e. might end up as being the
10826 line where the window_end_vpos is. */
10827 w->cursor.vpos = -1;
10828 last_text_row = NULL;
10829 overlay_arrow_seen = 0;
10830 while (it.current_y < it.last_visible_y
10831 && !fonts_changed_p
10832 && (first_unchanged_at_end_row == NULL
10833 || IT_CHARPOS (it) < stop_pos))
10835 if (display_line (&it))
10836 last_text_row = it.glyph_row - 1;
10839 if (fonts_changed_p)
10840 return -1;
10843 /* Compute differences in buffer positions, y-positions etc. for
10844 lines reused at the bottom of the window. Compute what we can
10845 scroll. */
10846 if (first_unchanged_at_end_row
10847 /* No lines reused because we displayed everything up to the
10848 bottom of the window. */
10849 && it.current_y < it.last_visible_y)
10851 dvpos = (it.vpos
10852 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10853 current_matrix));
10854 dy = it.current_y - first_unchanged_at_end_row->y;
10855 run.current_y = first_unchanged_at_end_row->y;
10856 run.desired_y = run.current_y + dy;
10857 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10859 else
10861 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10862 first_unchanged_at_end_row = NULL;
10864 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10867 /* Find the cursor if not already found. We have to decide whether
10868 PT will appear on this window (it sometimes doesn't, but this is
10869 not a very frequent case.) This decision has to be made before
10870 the current matrix is altered. A value of cursor.vpos < 0 means
10871 that PT is either in one of the lines beginning at
10872 first_unchanged_at_end_row or below the window. Don't care for
10873 lines that might be displayed later at the window end; as
10874 mentioned, this is not a frequent case. */
10875 if (w->cursor.vpos < 0)
10877 /* Cursor in unchanged rows at the top? */
10878 if (PT < CHARPOS (start_pos)
10879 && last_unchanged_at_beg_row)
10881 row = row_containing_pos (w, PT,
10882 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10883 last_unchanged_at_beg_row + 1);
10884 if (row)
10885 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10888 /* Start from first_unchanged_at_end_row looking for PT. */
10889 else if (first_unchanged_at_end_row)
10891 row = row_containing_pos (w, PT - delta,
10892 first_unchanged_at_end_row, NULL);
10893 if (row)
10894 set_cursor_from_row (w, row, w->current_matrix, delta,
10895 delta_bytes, dy, dvpos);
10898 /* Give up if cursor was not found. */
10899 if (w->cursor.vpos < 0)
10901 clear_glyph_matrix (w->desired_matrix);
10902 return -1;
10906 /* Don't let the cursor end in the scroll margins. */
10908 int this_scroll_margin, cursor_height;
10910 this_scroll_margin = max (0, scroll_margin);
10911 this_scroll_margin = min (this_scroll_margin,
10912 XFASTINT (w->height) / 4);
10913 this_scroll_margin *= CANON_Y_UNIT (it.f);
10914 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10916 if ((w->cursor.y < this_scroll_margin
10917 && CHARPOS (start) > BEGV)
10918 /* Don't take scroll margin into account at the bottom because
10919 old redisplay didn't do it either. */
10920 || w->cursor.y + cursor_height > it.last_visible_y)
10922 w->cursor.vpos = -1;
10923 clear_glyph_matrix (w->desired_matrix);
10924 return -1;
10928 /* Scroll the display. Do it before changing the current matrix so
10929 that xterm.c doesn't get confused about where the cursor glyph is
10930 found. */
10931 if (dy && run.height)
10933 update_begin (f);
10935 if (FRAME_WINDOW_P (f))
10937 rif->update_window_begin_hook (w);
10938 rif->clear_mouse_face (w);
10939 rif->scroll_run_hook (w, &run);
10940 rif->update_window_end_hook (w, 0, 0);
10942 else
10944 /* Terminal frame. In this case, dvpos gives the number of
10945 lines to scroll by; dvpos < 0 means scroll up. */
10946 int first_unchanged_at_end_vpos
10947 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10948 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10949 int end = XFASTINT (w->top) + window_internal_height (w);
10951 /* Perform the operation on the screen. */
10952 if (dvpos > 0)
10954 /* Scroll last_unchanged_at_beg_row to the end of the
10955 window down dvpos lines. */
10956 set_terminal_window (end);
10958 /* On dumb terminals delete dvpos lines at the end
10959 before inserting dvpos empty lines. */
10960 if (!scroll_region_ok)
10961 ins_del_lines (end - dvpos, -dvpos);
10963 /* Insert dvpos empty lines in front of
10964 last_unchanged_at_beg_row. */
10965 ins_del_lines (from, dvpos);
10967 else if (dvpos < 0)
10969 /* Scroll up last_unchanged_at_beg_vpos to the end of
10970 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10971 set_terminal_window (end);
10973 /* Delete dvpos lines in front of
10974 last_unchanged_at_beg_vpos. ins_del_lines will set
10975 the cursor to the given vpos and emit |dvpos| delete
10976 line sequences. */
10977 ins_del_lines (from + dvpos, dvpos);
10979 /* On a dumb terminal insert dvpos empty lines at the
10980 end. */
10981 if (!scroll_region_ok)
10982 ins_del_lines (end + dvpos, -dvpos);
10985 set_terminal_window (0);
10988 update_end (f);
10991 /* Shift reused rows of the current matrix to the right position.
10992 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10993 text. */
10994 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10995 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10996 if (dvpos < 0)
10998 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10999 bottom_vpos, dvpos);
11000 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11001 bottom_vpos, 0);
11003 else if (dvpos > 0)
11005 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11006 bottom_vpos, dvpos);
11007 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11008 first_unchanged_at_end_vpos + dvpos, 0);
11011 /* For frame-based redisplay, make sure that current frame and window
11012 matrix are in sync with respect to glyph memory. */
11013 if (!FRAME_WINDOW_P (f))
11014 sync_frame_with_window_matrix_rows (w);
11016 /* Adjust buffer positions in reused rows. */
11017 if (delta)
11018 increment_matrix_positions (current_matrix,
11019 first_unchanged_at_end_vpos + dvpos,
11020 bottom_vpos, delta, delta_bytes);
11022 /* Adjust Y positions. */
11023 if (dy)
11024 shift_glyph_matrix (w, current_matrix,
11025 first_unchanged_at_end_vpos + dvpos,
11026 bottom_vpos, dy);
11028 if (first_unchanged_at_end_row)
11029 first_unchanged_at_end_row += dvpos;
11031 /* If scrolling up, there may be some lines to display at the end of
11032 the window. */
11033 last_text_row_at_end = NULL;
11034 if (dy < 0)
11036 /* Set last_row to the glyph row in the current matrix where the
11037 window end line is found. It has been moved up or down in
11038 the matrix by dvpos. */
11039 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11040 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11042 /* If last_row is the window end line, it should display text. */
11043 xassert (last_row->displays_text_p);
11045 /* If window end line was partially visible before, begin
11046 displaying at that line. Otherwise begin displaying with the
11047 line following it. */
11048 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11050 init_to_row_start (&it, w, last_row);
11051 it.vpos = last_vpos;
11052 it.current_y = last_row->y;
11054 else
11056 init_to_row_end (&it, w, last_row);
11057 it.vpos = 1 + last_vpos;
11058 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11059 ++last_row;
11062 /* We may start in a continuation line. If so, we have to get
11063 the right continuation_lines_width and current_x. */
11064 it.continuation_lines_width = last_row->continuation_lines_width;
11065 it.hpos = it.current_x = 0;
11067 /* Display the rest of the lines at the window end. */
11068 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11069 while (it.current_y < it.last_visible_y
11070 && !fonts_changed_p)
11072 /* Is it always sure that the display agrees with lines in
11073 the current matrix? I don't think so, so we mark rows
11074 displayed invalid in the current matrix by setting their
11075 enabled_p flag to zero. */
11076 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11077 if (display_line (&it))
11078 last_text_row_at_end = it.glyph_row - 1;
11082 /* Update window_end_pos and window_end_vpos. */
11083 if (first_unchanged_at_end_row
11084 && first_unchanged_at_end_row->y < it.last_visible_y
11085 && !last_text_row_at_end)
11087 /* Window end line if one of the preserved rows from the current
11088 matrix. Set row to the last row displaying text in current
11089 matrix starting at first_unchanged_at_end_row, after
11090 scrolling. */
11091 xassert (first_unchanged_at_end_row->displays_text_p);
11092 row = find_last_row_displaying_text (w->current_matrix, &it,
11093 first_unchanged_at_end_row);
11094 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11096 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11097 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11098 XSETFASTINT (w->window_end_vpos,
11099 MATRIX_ROW_VPOS (row, w->current_matrix));
11101 else if (last_text_row_at_end)
11103 XSETFASTINT (w->window_end_pos,
11104 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11105 w->window_end_bytepos
11106 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11107 XSETFASTINT (w->window_end_vpos,
11108 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11110 else if (last_text_row)
11112 /* We have displayed either to the end of the window or at the
11113 end of the window, i.e. the last row with text is to be found
11114 in the desired matrix. */
11115 XSETFASTINT (w->window_end_pos,
11116 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11117 w->window_end_bytepos
11118 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11119 XSETFASTINT (w->window_end_vpos,
11120 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11122 else if (first_unchanged_at_end_row == NULL
11123 && last_text_row == NULL
11124 && last_text_row_at_end == NULL)
11126 /* Displayed to end of window, but no line containing text was
11127 displayed. Lines were deleted at the end of the window. */
11128 int vpos;
11129 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11131 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11132 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11133 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11134 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11135 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11136 break;
11138 w->window_end_vpos = make_number (vpos);
11140 else
11141 abort ();
11143 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11144 debug_end_vpos = XFASTINT (w->window_end_vpos));
11146 /* Record that display has not been completed. */
11147 w->window_end_valid = Qnil;
11148 w->desired_matrix->no_scrolling_p = 1;
11149 return 3;
11154 /***********************************************************************
11155 More debugging support
11156 ***********************************************************************/
11158 #if GLYPH_DEBUG
11160 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11161 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11164 /* Dump the contents of glyph matrix MATRIX on stderr. If
11165 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11167 static void
11168 dump_glyph_matrix (matrix, with_glyphs_p)
11169 struct glyph_matrix *matrix;
11170 int with_glyphs_p;
11172 int i;
11173 for (i = 0; i < matrix->nrows; ++i)
11174 dump_glyph_row (matrix, i, with_glyphs_p);
11178 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11179 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11181 void
11182 dump_glyph_row (matrix, vpos, with_glyphs_p)
11183 struct glyph_matrix *matrix;
11184 int vpos, with_glyphs_p;
11186 struct glyph_row *row;
11188 if (vpos < 0 || vpos >= matrix->nrows)
11189 return;
11191 row = MATRIX_ROW (matrix, vpos);
11193 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11194 fprintf (stderr, "=======================================================================\n");
11196 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11197 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11198 row - matrix->rows,
11199 MATRIX_ROW_START_CHARPOS (row),
11200 MATRIX_ROW_END_CHARPOS (row),
11201 row->used[TEXT_AREA],
11202 row->contains_overlapping_glyphs_p,
11203 row->enabled_p,
11204 row->inverse_p,
11205 row->truncated_on_left_p,
11206 row->truncated_on_right_p,
11207 row->overlay_arrow_p,
11208 row->continued_p,
11209 MATRIX_ROW_CONTINUATION_LINE_P (row),
11210 row->displays_text_p,
11211 row->ends_at_zv_p,
11212 row->fill_line_p,
11213 row->ends_in_middle_of_char_p,
11214 row->starts_in_middle_of_char_p,
11215 row->x,
11216 row->y,
11217 row->pixel_width,
11218 row->height,
11219 row->visible_height,
11220 row->ascent,
11221 row->phys_ascent);
11222 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11223 row->end.overlay_string_index);
11224 fprintf (stderr, "%9d %5d\n",
11225 CHARPOS (row->start.string_pos),
11226 CHARPOS (row->end.string_pos));
11227 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11228 row->end.dpvec_index);
11230 if (with_glyphs_p)
11232 struct glyph *glyph, *glyph_end;
11233 int prev_had_glyphs_p;
11235 glyph = row->glyphs[TEXT_AREA];
11236 glyph_end = glyph + row->used[TEXT_AREA];
11238 /* Glyph for a line end in text. */
11239 if (glyph == glyph_end && glyph->charpos > 0)
11240 ++glyph_end;
11242 if (glyph < glyph_end)
11244 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11245 prev_had_glyphs_p = 1;
11247 else
11248 prev_had_glyphs_p = 0;
11250 while (glyph < glyph_end)
11252 if (glyph->type == CHAR_GLYPH)
11254 fprintf (stderr,
11255 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11256 glyph - row->glyphs[TEXT_AREA],
11257 'C',
11258 glyph->charpos,
11259 (BUFFERP (glyph->object)
11260 ? 'B'
11261 : (STRINGP (glyph->object)
11262 ? 'S'
11263 : '-')),
11264 glyph->pixel_width,
11265 glyph->u.ch,
11266 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11267 ? glyph->u.ch
11268 : '.'),
11269 glyph->face_id,
11270 glyph->left_box_line_p,
11271 glyph->right_box_line_p);
11273 else if (glyph->type == STRETCH_GLYPH)
11275 fprintf (stderr,
11276 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11277 glyph - row->glyphs[TEXT_AREA],
11278 'S',
11279 glyph->charpos,
11280 (BUFFERP (glyph->object)
11281 ? 'B'
11282 : (STRINGP (glyph->object)
11283 ? 'S'
11284 : '-')),
11285 glyph->pixel_width,
11287 '.',
11288 glyph->face_id,
11289 glyph->left_box_line_p,
11290 glyph->right_box_line_p);
11292 else if (glyph->type == IMAGE_GLYPH)
11294 fprintf (stderr,
11295 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11296 glyph - row->glyphs[TEXT_AREA],
11297 'I',
11298 glyph->charpos,
11299 (BUFFERP (glyph->object)
11300 ? 'B'
11301 : (STRINGP (glyph->object)
11302 ? 'S'
11303 : '-')),
11304 glyph->pixel_width,
11305 glyph->u.img_id,
11306 '.',
11307 glyph->face_id,
11308 glyph->left_box_line_p,
11309 glyph->right_box_line_p);
11311 ++glyph;
11317 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11318 Sdump_glyph_matrix, 0, 1, "p",
11319 "Dump the current matrix of the selected window to stderr.\n\
11320 Shows contents of glyph row structures. With non-nil optional\n\
11321 parameter WITH-GLYPHS-P, dump glyphs as well.")
11322 (with_glyphs_p)
11323 Lisp_Object with_glyphs_p;
11325 struct window *w = XWINDOW (selected_window);
11326 struct buffer *buffer = XBUFFER (w->buffer);
11328 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11329 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11330 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11331 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11332 fprintf (stderr, "=============================================\n");
11333 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11334 return Qnil;
11338 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11339 "Dump glyph row ROW to stderr.")
11340 (row)
11341 Lisp_Object row;
11343 CHECK_NUMBER (row, 0);
11344 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11345 return Qnil;
11349 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11350 0, 0, "", "")
11353 struct frame *sf = SELECTED_FRAME ();
11354 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11355 ->current_matrix);
11356 dump_glyph_row (m, 0, 1);
11357 return Qnil;
11361 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11362 Strace_redisplay_toggle, 0, 0, "",
11363 "Toggle tracing of redisplay.")
11366 trace_redisplay_p = !trace_redisplay_p;
11367 return Qnil;
11371 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11372 "Print STRING to stderr.")
11373 (string)
11374 Lisp_Object string;
11376 CHECK_STRING (string, 0);
11377 fprintf (stderr, "%s", XSTRING (string)->data);
11378 return Qnil;
11381 #endif /* GLYPH_DEBUG */
11385 /***********************************************************************
11386 Building Desired Matrix Rows
11387 ***********************************************************************/
11389 /* Return a temporary glyph row holding the glyphs of an overlay
11390 arrow. Only used for non-window-redisplay windows. */
11392 static struct glyph_row *
11393 get_overlay_arrow_glyph_row (w)
11394 struct window *w;
11396 struct frame *f = XFRAME (WINDOW_FRAME (w));
11397 struct buffer *buffer = XBUFFER (w->buffer);
11398 struct buffer *old = current_buffer;
11399 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11400 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11401 unsigned char *arrow_end = arrow_string + arrow_len;
11402 unsigned char *p;
11403 struct it it;
11404 int multibyte_p;
11405 int n_glyphs_before;
11407 set_buffer_temp (buffer);
11408 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11409 it.glyph_row->used[TEXT_AREA] = 0;
11410 SET_TEXT_POS (it.position, 0, 0);
11412 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11413 p = arrow_string;
11414 while (p < arrow_end)
11416 Lisp_Object face, ilisp;
11418 /* Get the next character. */
11419 if (multibyte_p)
11420 it.c = string_char_and_length (p, arrow_len, &it.len);
11421 else
11422 it.c = *p, it.len = 1;
11423 p += it.len;
11425 /* Get its face. */
11426 XSETFASTINT (ilisp, p - arrow_string);
11427 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11428 it.face_id = compute_char_face (f, it.c, face);
11430 /* Compute its width, get its glyphs. */
11431 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11432 SET_TEXT_POS (it.position, -1, -1);
11433 PRODUCE_GLYPHS (&it);
11435 /* If this character doesn't fit any more in the line, we have
11436 to remove some glyphs. */
11437 if (it.current_x > it.last_visible_x)
11439 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11440 break;
11444 set_buffer_temp (old);
11445 return it.glyph_row;
11449 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11450 glyphs are only inserted for terminal frames since we can't really
11451 win with truncation glyphs when partially visible glyphs are
11452 involved. Which glyphs to insert is determined by
11453 produce_special_glyphs. */
11455 static void
11456 insert_left_trunc_glyphs (it)
11457 struct it *it;
11459 struct it truncate_it;
11460 struct glyph *from, *end, *to, *toend;
11462 xassert (!FRAME_WINDOW_P (it->f));
11464 /* Get the truncation glyphs. */
11465 truncate_it = *it;
11466 truncate_it.current_x = 0;
11467 truncate_it.face_id = DEFAULT_FACE_ID;
11468 truncate_it.glyph_row = &scratch_glyph_row;
11469 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11470 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11471 truncate_it.object = make_number (0);
11472 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11474 /* Overwrite glyphs from IT with truncation glyphs. */
11475 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11476 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11477 to = it->glyph_row->glyphs[TEXT_AREA];
11478 toend = to + it->glyph_row->used[TEXT_AREA];
11480 while (from < end)
11481 *to++ = *from++;
11483 /* There may be padding glyphs left over. Remove them. */
11484 from = to;
11485 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11486 ++from;
11487 while (from < toend)
11488 *to++ = *from++;
11490 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11494 /* Compute the pixel height and width of IT->glyph_row.
11496 Most of the time, ascent and height of a display line will be equal
11497 to the max_ascent and max_height values of the display iterator
11498 structure. This is not the case if
11500 1. We hit ZV without displaying anything. In this case, max_ascent
11501 and max_height will be zero.
11503 2. We have some glyphs that don't contribute to the line height.
11504 (The glyph row flag contributes_to_line_height_p is for future
11505 pixmap extensions).
11507 The first case is easily covered by using default values because in
11508 these cases, the line height does not really matter, except that it
11509 must not be zero. */
11511 static void
11512 compute_line_metrics (it)
11513 struct it *it;
11515 struct glyph_row *row = it->glyph_row;
11516 int area, i;
11518 if (FRAME_WINDOW_P (it->f))
11520 int i, header_line_height;
11522 /* The line may consist of one space only, that was added to
11523 place the cursor on it. If so, the row's height hasn't been
11524 computed yet. */
11525 if (row->height == 0)
11527 if (it->max_ascent + it->max_descent == 0)
11528 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11529 row->ascent = it->max_ascent;
11530 row->height = it->max_ascent + it->max_descent;
11531 row->phys_ascent = it->max_phys_ascent;
11532 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11535 /* Compute the width of this line. */
11536 row->pixel_width = row->x;
11537 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11538 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11540 xassert (row->pixel_width >= 0);
11541 xassert (row->ascent >= 0 && row->height > 0);
11543 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11544 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11546 /* If first line's physical ascent is larger than its logical
11547 ascent, use the physical ascent, and make the row taller.
11548 This makes accented characters fully visible. */
11549 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11550 && row->phys_ascent > row->ascent)
11552 row->height += row->phys_ascent - row->ascent;
11553 row->ascent = row->phys_ascent;
11556 /* Compute how much of the line is visible. */
11557 row->visible_height = row->height;
11559 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11560 if (row->y < header_line_height)
11561 row->visible_height -= header_line_height - row->y;
11562 else
11564 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11565 if (row->y + row->height > max_y)
11566 row->visible_height -= row->y + row->height - max_y;
11569 else
11571 row->pixel_width = row->used[TEXT_AREA];
11572 row->ascent = row->phys_ascent = 0;
11573 row->height = row->phys_height = row->visible_height = 1;
11576 /* Compute a hash code for this row. */
11577 row->hash = 0;
11578 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11579 for (i = 0; i < row->used[area]; ++i)
11580 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11581 + row->glyphs[area][i].u.val
11582 + row->glyphs[area][i].face_id
11583 + row->glyphs[area][i].padding_p
11584 + (row->glyphs[area][i].type << 2));
11586 it->max_ascent = it->max_descent = 0;
11587 it->max_phys_ascent = it->max_phys_descent = 0;
11591 /* Append one space to the glyph row of iterator IT if doing a
11592 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11593 space have the default face, otherwise let it have the same face as
11594 IT->face_id. Value is non-zero if a space was added.
11596 This function is called to make sure that there is always one glyph
11597 at the end of a glyph row that the cursor can be set on under
11598 window-systems. (If there weren't such a glyph we would not know
11599 how wide and tall a box cursor should be displayed).
11601 At the same time this space let's a nicely handle clearing to the
11602 end of the line if the row ends in italic text. */
11604 static int
11605 append_space (it, default_face_p)
11606 struct it *it;
11607 int default_face_p;
11609 if (FRAME_WINDOW_P (it->f))
11611 int n = it->glyph_row->used[TEXT_AREA];
11613 if (it->glyph_row->glyphs[TEXT_AREA] + n
11614 < it->glyph_row->glyphs[1 + TEXT_AREA])
11616 /* Save some values that must not be changed.
11617 Must save IT->c and IT->len because otherwise
11618 ITERATOR_AT_END_P wouldn't work anymore after
11619 append_space has been called. */
11620 int saved_what = it->what;
11621 int saved_c = it->c, saved_len = it->len;
11622 int saved_x = it->current_x;
11623 int saved_face_id = it->face_id;
11624 struct text_pos saved_pos;
11625 Lisp_Object saved_object;
11626 struct face *face;
11628 saved_object = it->object;
11629 saved_pos = it->position;
11631 it->what = IT_CHARACTER;
11632 bzero (&it->position, sizeof it->position);
11633 it->object = make_number (0);
11634 it->c = ' ';
11635 it->len = 1;
11637 if (default_face_p)
11638 it->face_id = DEFAULT_FACE_ID;
11639 face = FACE_FROM_ID (it->f, it->face_id);
11640 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11642 PRODUCE_GLYPHS (it);
11644 it->current_x = saved_x;
11645 it->object = saved_object;
11646 it->position = saved_pos;
11647 it->what = saved_what;
11648 it->face_id = saved_face_id;
11649 it->len = saved_len;
11650 it->c = saved_c;
11651 return 1;
11655 return 0;
11659 /* Extend the face of the last glyph in the text area of IT->glyph_row
11660 to the end of the display line. Called from display_line.
11661 If the glyph row is empty, add a space glyph to it so that we
11662 know the face to draw. Set the glyph row flag fill_line_p. */
11664 static void
11665 extend_face_to_end_of_line (it)
11666 struct it *it;
11668 struct face *face;
11669 struct frame *f = it->f;
11671 /* If line is already filled, do nothing. */
11672 if (it->current_x >= it->last_visible_x)
11673 return;
11675 /* Face extension extends the background and box of IT->face_id
11676 to the end of the line. If the background equals the background
11677 of the frame, we haven't to do anything. */
11678 face = FACE_FROM_ID (f, it->face_id);
11679 if (FRAME_WINDOW_P (f)
11680 && face->box == FACE_NO_BOX
11681 && face->background == FRAME_BACKGROUND_PIXEL (f)
11682 && !face->stipple)
11683 return;
11685 /* Set the glyph row flag indicating that the face of the last glyph
11686 in the text area has to be drawn to the end of the text area. */
11687 it->glyph_row->fill_line_p = 1;
11689 /* If current character of IT is not ASCII, make sure we have the
11690 ASCII face. This will be automatically undone the next time
11691 get_next_display_element returns a multibyte character. Note
11692 that the character will always be single byte in unibyte text. */
11693 if (!SINGLE_BYTE_CHAR_P (it->c))
11695 it->face_id = FACE_FOR_CHAR (f, face, 0);
11698 if (FRAME_WINDOW_P (f))
11700 /* If the row is empty, add a space with the current face of IT,
11701 so that we know which face to draw. */
11702 if (it->glyph_row->used[TEXT_AREA] == 0)
11704 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11705 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11706 it->glyph_row->used[TEXT_AREA] = 1;
11709 else
11711 /* Save some values that must not be changed. */
11712 int saved_x = it->current_x;
11713 struct text_pos saved_pos;
11714 Lisp_Object saved_object;
11715 int saved_what = it->what;
11717 saved_object = it->object;
11718 saved_pos = it->position;
11720 it->what = IT_CHARACTER;
11721 bzero (&it->position, sizeof it->position);
11722 it->object = make_number (0);
11723 it->c = ' ';
11724 it->len = 1;
11726 PRODUCE_GLYPHS (it);
11728 while (it->current_x <= it->last_visible_x)
11729 PRODUCE_GLYPHS (it);
11731 /* Don't count these blanks really. It would let us insert a left
11732 truncation glyph below and make us set the cursor on them, maybe. */
11733 it->current_x = saved_x;
11734 it->object = saved_object;
11735 it->position = saved_pos;
11736 it->what = saved_what;
11741 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11742 trailing whitespace. */
11744 static int
11745 trailing_whitespace_p (charpos)
11746 int charpos;
11748 int bytepos = CHAR_TO_BYTE (charpos);
11749 int c = 0;
11751 while (bytepos < ZV_BYTE
11752 && (c = FETCH_CHAR (bytepos),
11753 c == ' ' || c == '\t'))
11754 ++bytepos;
11756 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11758 if (bytepos != PT_BYTE)
11759 return 1;
11761 return 0;
11765 /* Highlight trailing whitespace, if any, in ROW. */
11767 void
11768 highlight_trailing_whitespace (f, row)
11769 struct frame *f;
11770 struct glyph_row *row;
11772 int used = row->used[TEXT_AREA];
11774 if (used)
11776 struct glyph *start = row->glyphs[TEXT_AREA];
11777 struct glyph *glyph = start + used - 1;
11779 /* Skip over the space glyph inserted to display the
11780 cursor at the end of a line. */
11781 if (glyph->type == CHAR_GLYPH
11782 && glyph->u.ch == ' '
11783 && INTEGERP (glyph->object))
11784 --glyph;
11786 /* If last glyph is a space or stretch, and it's trailing
11787 whitespace, set the face of all trailing whitespace glyphs in
11788 IT->glyph_row to `trailing-whitespace'. */
11789 if (glyph >= start
11790 && BUFFERP (glyph->object)
11791 && (glyph->type == STRETCH_GLYPH
11792 || (glyph->type == CHAR_GLYPH
11793 && glyph->u.ch == ' '))
11794 && trailing_whitespace_p (glyph->charpos))
11796 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11798 while (glyph >= start
11799 && BUFFERP (glyph->object)
11800 && (glyph->type == STRETCH_GLYPH
11801 || (glyph->type == CHAR_GLYPH
11802 && glyph->u.ch == ' ')))
11803 (glyph--)->face_id = face_id;
11809 /* Value is non-zero if glyph row ROW in window W should be
11810 used to put the cursor on. */
11812 static int
11813 cursor_row_p (w, row)
11814 struct window *w;
11815 struct glyph_row *row;
11817 int cursor_row_p = 1;
11819 if (PT == MATRIX_ROW_END_CHARPOS (row))
11821 /* If the row ends with a newline from a string, we don't want
11822 the cursor there (if the row is continued it doesn't end in a
11823 newline). */
11824 if (CHARPOS (row->end.string_pos) >= 0
11825 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11826 cursor_row_p = row->continued_p;
11828 /* If the row ends at ZV, display the cursor at the end of that
11829 row instead of at the start of the row below. */
11830 else if (row->ends_at_zv_p)
11831 cursor_row_p = 1;
11832 else
11833 cursor_row_p = 0;
11836 return cursor_row_p;
11840 /* Construct the glyph row IT->glyph_row in the desired matrix of
11841 IT->w from text at the current position of IT. See dispextern.h
11842 for an overview of struct it. Value is non-zero if
11843 IT->glyph_row displays text, as opposed to a line displaying ZV
11844 only. */
11846 static int
11847 display_line (it)
11848 struct it *it;
11850 struct glyph_row *row = it->glyph_row;
11852 /* We always start displaying at hpos zero even if hscrolled. */
11853 xassert (it->hpos == 0 && it->current_x == 0);
11855 /* We must not display in a row that's not a text row. */
11856 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11857 < it->w->desired_matrix->nrows);
11859 /* Is IT->w showing the region? */
11860 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11862 /* Clear the result glyph row and enable it. */
11863 prepare_desired_row (row);
11865 row->y = it->current_y;
11866 row->start = it->current;
11867 row->continuation_lines_width = it->continuation_lines_width;
11868 row->displays_text_p = 1;
11869 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11870 it->starts_in_middle_of_char_p = 0;
11872 /* Arrange the overlays nicely for our purposes. Usually, we call
11873 display_line on only one line at a time, in which case this
11874 can't really hurt too much, or we call it on lines which appear
11875 one after another in the buffer, in which case all calls to
11876 recenter_overlay_lists but the first will be pretty cheap. */
11877 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11879 /* Move over display elements that are not visible because we are
11880 hscrolled. This may stop at an x-position < IT->first_visible_x
11881 if the first glyph is partially visible or if we hit a line end. */
11882 if (it->current_x < it->first_visible_x)
11883 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11884 MOVE_TO_POS | MOVE_TO_X);
11886 /* Get the initial row height. This is either the height of the
11887 text hscrolled, if there is any, or zero. */
11888 row->ascent = it->max_ascent;
11889 row->height = it->max_ascent + it->max_descent;
11890 row->phys_ascent = it->max_phys_ascent;
11891 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11893 /* Loop generating characters. The loop is left with IT on the next
11894 character to display. */
11895 while (1)
11897 int n_glyphs_before, hpos_before, x_before;
11898 int x, i, nglyphs;
11899 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11901 /* Retrieve the next thing to display. Value is zero if end of
11902 buffer reached. */
11903 if (!get_next_display_element (it))
11905 /* Maybe add a space at the end of this line that is used to
11906 display the cursor there under X. Set the charpos of the
11907 first glyph of blank lines not corresponding to any text
11908 to -1. */
11909 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11910 || row->used[TEXT_AREA] == 0)
11912 row->glyphs[TEXT_AREA]->charpos = -1;
11913 row->displays_text_p = 0;
11915 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11916 row->indicate_empty_line_p = 1;
11919 it->continuation_lines_width = 0;
11920 row->ends_at_zv_p = 1;
11921 break;
11924 /* Now, get the metrics of what we want to display. This also
11925 generates glyphs in `row' (which is IT->glyph_row). */
11926 n_glyphs_before = row->used[TEXT_AREA];
11927 x = it->current_x;
11929 /* Remember the line height so far in case the next element doesn't
11930 fit on the line. */
11931 if (!it->truncate_lines_p)
11933 ascent = it->max_ascent;
11934 descent = it->max_descent;
11935 phys_ascent = it->max_phys_ascent;
11936 phys_descent = it->max_phys_descent;
11939 PRODUCE_GLYPHS (it);
11941 /* If this display element was in marginal areas, continue with
11942 the next one. */
11943 if (it->area != TEXT_AREA)
11945 row->ascent = max (row->ascent, it->max_ascent);
11946 row->height = max (row->height, it->max_ascent + it->max_descent);
11947 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11948 row->phys_height = max (row->phys_height,
11949 it->max_phys_ascent + it->max_phys_descent);
11950 set_iterator_to_next (it, 1);
11951 continue;
11954 /* Does the display element fit on the line? If we truncate
11955 lines, we should draw past the right edge of the window. If
11956 we don't truncate, we want to stop so that we can display the
11957 continuation glyph before the right margin. If lines are
11958 continued, there are two possible strategies for characters
11959 resulting in more than 1 glyph (e.g. tabs): Display as many
11960 glyphs as possible in this line and leave the rest for the
11961 continuation line, or display the whole element in the next
11962 line. Original redisplay did the former, so we do it also. */
11963 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11964 hpos_before = it->hpos;
11965 x_before = x;
11967 if (nglyphs == 1
11968 && it->current_x < it->last_visible_x)
11970 ++it->hpos;
11971 row->ascent = max (row->ascent, it->max_ascent);
11972 row->height = max (row->height, it->max_ascent + it->max_descent);
11973 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11974 row->phys_height = max (row->phys_height,
11975 it->max_phys_ascent + it->max_phys_descent);
11976 if (it->current_x - it->pixel_width < it->first_visible_x)
11977 row->x = x - it->first_visible_x;
11979 else
11981 int new_x;
11982 struct glyph *glyph;
11984 for (i = 0; i < nglyphs; ++i, x = new_x)
11986 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11987 new_x = x + glyph->pixel_width;
11989 if (/* Lines are continued. */
11990 !it->truncate_lines_p
11991 && (/* Glyph doesn't fit on the line. */
11992 new_x > it->last_visible_x
11993 /* Or it fits exactly on a window system frame. */
11994 || (new_x == it->last_visible_x
11995 && FRAME_WINDOW_P (it->f))))
11997 /* End of a continued line. */
11999 if (it->hpos == 0
12000 || (new_x == it->last_visible_x
12001 && FRAME_WINDOW_P (it->f)))
12003 /* Current glyph is the only one on the line or
12004 fits exactly on the line. We must continue
12005 the line because we can't draw the cursor
12006 after the glyph. */
12007 row->continued_p = 1;
12008 it->current_x = new_x;
12009 it->continuation_lines_width += new_x;
12010 ++it->hpos;
12011 if (i == nglyphs - 1)
12012 set_iterator_to_next (it, 1);
12014 else if (CHAR_GLYPH_PADDING_P (*glyph)
12015 && !FRAME_WINDOW_P (it->f))
12017 /* A padding glyph that doesn't fit on this line.
12018 This means the whole character doesn't fit
12019 on the line. */
12020 row->used[TEXT_AREA] = n_glyphs_before;
12022 /* Fill the rest of the row with continuation
12023 glyphs like in 20.x. */
12024 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12025 < row->glyphs[1 + TEXT_AREA])
12026 produce_special_glyphs (it, IT_CONTINUATION);
12028 row->continued_p = 1;
12029 it->current_x = x_before;
12030 it->continuation_lines_width += x_before;
12032 /* Restore the height to what it was before the
12033 element not fitting on the line. */
12034 it->max_ascent = ascent;
12035 it->max_descent = descent;
12036 it->max_phys_ascent = phys_ascent;
12037 it->max_phys_descent = phys_descent;
12039 else
12041 /* Display element draws past the right edge of
12042 the window. Restore positions to values
12043 before the element. The next line starts
12044 with current_x before the glyph that could
12045 not be displayed, so that TAB works right. */
12046 row->used[TEXT_AREA] = n_glyphs_before + i;
12048 /* Display continuation glyphs. */
12049 if (!FRAME_WINDOW_P (it->f))
12050 produce_special_glyphs (it, IT_CONTINUATION);
12051 row->continued_p = 1;
12053 it->current_x = x;
12054 it->continuation_lines_width += x;
12055 if (nglyphs > 1 && i > 0)
12057 row->ends_in_middle_of_char_p = 1;
12058 it->starts_in_middle_of_char_p = 1;
12061 /* Restore the height to what it was before the
12062 element not fitting on the line. */
12063 it->max_ascent = ascent;
12064 it->max_descent = descent;
12065 it->max_phys_ascent = phys_ascent;
12066 it->max_phys_descent = phys_descent;
12069 break;
12071 else if (new_x > it->first_visible_x)
12073 /* Increment number of glyphs actually displayed. */
12074 ++it->hpos;
12076 if (x < it->first_visible_x)
12077 /* Glyph is partially visible, i.e. row starts at
12078 negative X position. */
12079 row->x = x - it->first_visible_x;
12081 else
12083 /* Glyph is completely off the left margin of the
12084 window. This should not happen because of the
12085 move_it_in_display_line at the start of
12086 this function. */
12087 abort ();
12091 row->ascent = max (row->ascent, it->max_ascent);
12092 row->height = max (row->height, it->max_ascent + it->max_descent);
12093 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12094 row->phys_height = max (row->phys_height,
12095 it->max_phys_ascent + it->max_phys_descent);
12097 /* End of this display line if row is continued. */
12098 if (row->continued_p)
12099 break;
12102 /* Is this a line end? If yes, we're also done, after making
12103 sure that a non-default face is extended up to the right
12104 margin of the window. */
12105 if (ITERATOR_AT_END_OF_LINE_P (it))
12107 int used_before = row->used[TEXT_AREA];
12109 /* Add a space at the end of the line that is used to
12110 display the cursor there. */
12111 append_space (it, 0);
12113 /* Extend the face to the end of the line. */
12114 extend_face_to_end_of_line (it);
12116 /* Make sure we have the position. */
12117 if (used_before == 0)
12118 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12120 /* Consume the line end. This skips over invisible lines. */
12121 set_iterator_to_next (it, 1);
12122 it->continuation_lines_width = 0;
12123 break;
12126 /* Proceed with next display element. Note that this skips
12127 over lines invisible because of selective display. */
12128 set_iterator_to_next (it, 1);
12130 /* If we truncate lines, we are done when the last displayed
12131 glyphs reach past the right margin of the window. */
12132 if (it->truncate_lines_p
12133 && (FRAME_WINDOW_P (it->f)
12134 ? (it->current_x >= it->last_visible_x)
12135 : (it->current_x > it->last_visible_x)))
12137 /* Maybe add truncation glyphs. */
12138 if (!FRAME_WINDOW_P (it->f))
12140 --it->glyph_row->used[TEXT_AREA];
12141 produce_special_glyphs (it, IT_TRUNCATION);
12144 row->truncated_on_right_p = 1;
12145 it->continuation_lines_width = 0;
12146 reseat_at_next_visible_line_start (it, 0);
12147 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12148 it->hpos = hpos_before;
12149 it->current_x = x_before;
12150 break;
12154 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12155 at the left window margin. */
12156 if (it->first_visible_x
12157 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12159 if (!FRAME_WINDOW_P (it->f))
12160 insert_left_trunc_glyphs (it);
12161 row->truncated_on_left_p = 1;
12164 /* If the start of this line is the overlay arrow-position, then
12165 mark this glyph row as the one containing the overlay arrow.
12166 This is clearly a mess with variable size fonts. It would be
12167 better to let it be displayed like cursors under X. */
12168 if (MARKERP (Voverlay_arrow_position)
12169 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12170 && (MATRIX_ROW_START_CHARPOS (row)
12171 == marker_position (Voverlay_arrow_position))
12172 && STRINGP (Voverlay_arrow_string)
12173 && ! overlay_arrow_seen)
12175 /* Overlay arrow in window redisplay is a bitmap. */
12176 if (!FRAME_WINDOW_P (it->f))
12178 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12179 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12180 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12181 struct glyph *p = row->glyphs[TEXT_AREA];
12182 struct glyph *p2, *end;
12184 /* Copy the arrow glyphs. */
12185 while (glyph < arrow_end)
12186 *p++ = *glyph++;
12188 /* Throw away padding glyphs. */
12189 p2 = p;
12190 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12191 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12192 ++p2;
12193 if (p2 > p)
12195 while (p2 < end)
12196 *p++ = *p2++;
12197 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12201 overlay_arrow_seen = 1;
12202 row->overlay_arrow_p = 1;
12205 /* Compute pixel dimensions of this line. */
12206 compute_line_metrics (it);
12208 /* Remember the position at which this line ends. */
12209 row->end = it->current;
12211 /* Maybe set the cursor. */
12212 if (it->w->cursor.vpos < 0
12213 && PT >= MATRIX_ROW_START_CHARPOS (row)
12214 && PT <= MATRIX_ROW_END_CHARPOS (row)
12215 && cursor_row_p (it->w, row))
12216 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12218 /* Highlight trailing whitespace. */
12219 if (!NILP (Vshow_trailing_whitespace))
12220 highlight_trailing_whitespace (it->f, it->glyph_row);
12222 /* Prepare for the next line. This line starts horizontally at (X
12223 HPOS) = (0 0). Vertical positions are incremented. As a
12224 convenience for the caller, IT->glyph_row is set to the next
12225 row to be used. */
12226 it->current_x = it->hpos = 0;
12227 it->current_y += row->height;
12228 ++it->vpos;
12229 ++it->glyph_row;
12230 return row->displays_text_p;
12235 /***********************************************************************
12236 Menu Bar
12237 ***********************************************************************/
12239 /* Redisplay the menu bar in the frame for window W.
12241 The menu bar of X frames that don't have X toolkit support is
12242 displayed in a special window W->frame->menu_bar_window.
12244 The menu bar of terminal frames is treated specially as far as
12245 glyph matrices are concerned. Menu bar lines are not part of
12246 windows, so the update is done directly on the frame matrix rows
12247 for the menu bar. */
12249 static void
12250 display_menu_bar (w)
12251 struct window *w;
12253 struct frame *f = XFRAME (WINDOW_FRAME (w));
12254 struct it it;
12255 Lisp_Object items;
12256 int i;
12258 /* Don't do all this for graphical frames. */
12259 #ifdef HAVE_NTGUI
12260 if (!NILP (Vwindow_system))
12261 return;
12262 #endif
12263 #ifdef USE_X_TOOLKIT
12264 if (FRAME_X_P (f))
12265 return;
12266 #endif
12267 #ifdef macintosh
12268 if (FRAME_MAC_P (f))
12269 return;
12270 #endif
12272 #ifdef USE_X_TOOLKIT
12273 xassert (!FRAME_WINDOW_P (f));
12274 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12275 it.first_visible_x = 0;
12276 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12277 #else /* not USE_X_TOOLKIT */
12278 if (FRAME_WINDOW_P (f))
12280 /* Menu bar lines are displayed in the desired matrix of the
12281 dummy window menu_bar_window. */
12282 struct window *menu_w;
12283 xassert (WINDOWP (f->menu_bar_window));
12284 menu_w = XWINDOW (f->menu_bar_window);
12285 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12286 MENU_FACE_ID);
12287 it.first_visible_x = 0;
12288 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12290 else
12292 /* This is a TTY frame, i.e. character hpos/vpos are used as
12293 pixel x/y. */
12294 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12295 MENU_FACE_ID);
12296 it.first_visible_x = 0;
12297 it.last_visible_x = FRAME_WIDTH (f);
12299 #endif /* not USE_X_TOOLKIT */
12301 /* Clear all rows of the menu bar. */
12302 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12304 struct glyph_row *row = it.glyph_row + i;
12305 clear_glyph_row (row);
12306 row->enabled_p = 1;
12307 row->full_width_p = 1;
12310 /* Make the first line of the menu bar appear in reverse video. */
12311 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12313 /* Display all items of the menu bar. */
12314 items = FRAME_MENU_BAR_ITEMS (it.f);
12315 for (i = 0; i < XVECTOR (items)->size; i += 4)
12317 Lisp_Object string;
12319 /* Stop at nil string. */
12320 string = XVECTOR (items)->contents[i + 1];
12321 if (NILP (string))
12322 break;
12324 /* Remember where item was displayed. */
12325 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12327 /* Display the item, pad with one space. */
12328 if (it.current_x < it.last_visible_x)
12329 display_string (NULL, string, Qnil, 0, 0, &it,
12330 XSTRING (string)->size + 1, 0, 0, -1);
12333 /* Fill out the line with spaces. */
12334 if (it.current_x < it.last_visible_x)
12335 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12337 /* Compute the total height of the lines. */
12338 compute_line_metrics (&it);
12343 /***********************************************************************
12344 Mode Line
12345 ***********************************************************************/
12347 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12348 FORCE is non-zero, redisplay mode lines unconditionally.
12349 Otherwise, redisplay only mode lines that are garbaged. Value is
12350 the number of windows whose mode lines were redisplayed. */
12352 static int
12353 redisplay_mode_lines (window, force)
12354 Lisp_Object window;
12355 int force;
12357 int nwindows = 0;
12359 while (!NILP (window))
12361 struct window *w = XWINDOW (window);
12363 if (WINDOWP (w->hchild))
12364 nwindows += redisplay_mode_lines (w->hchild, force);
12365 else if (WINDOWP (w->vchild))
12366 nwindows += redisplay_mode_lines (w->vchild, force);
12367 else if (force
12368 || FRAME_GARBAGED_P (XFRAME (w->frame))
12369 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12371 Lisp_Object old_selected_frame;
12372 struct text_pos lpoint;
12373 struct buffer *old = current_buffer;
12375 /* Set the window's buffer for the mode line display. */
12376 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12377 set_buffer_internal_1 (XBUFFER (w->buffer));
12379 /* Point refers normally to the selected window. For any
12380 other window, set up appropriate value. */
12381 if (!EQ (window, selected_window))
12383 struct text_pos pt;
12385 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12386 if (CHARPOS (pt) < BEGV)
12387 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12388 else if (CHARPOS (pt) > (ZV - 1))
12389 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12390 else
12391 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12394 /* Temporarily set up the selected frame. */
12395 old_selected_frame = selected_frame;
12396 selected_frame = w->frame;
12398 /* Display mode lines. */
12399 clear_glyph_matrix (w->desired_matrix);
12400 if (display_mode_lines (w))
12402 ++nwindows;
12403 w->must_be_updated_p = 1;
12406 /* Restore old settings. */
12407 selected_frame = old_selected_frame;
12408 set_buffer_internal_1 (old);
12409 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12412 window = w->next;
12415 return nwindows;
12419 /* Display the mode and/or top line of window W. Value is the number
12420 of mode lines displayed. */
12422 static int
12423 display_mode_lines (w)
12424 struct window *w;
12426 int n = 0;
12428 /* These will be set while the mode line specs are processed. */
12429 line_number_displayed = 0;
12430 w->column_number_displayed = Qnil;
12432 if (WINDOW_WANTS_MODELINE_P (w))
12434 display_mode_line (w, MODE_LINE_FACE_ID,
12435 current_buffer->mode_line_format);
12436 ++n;
12439 if (WINDOW_WANTS_HEADER_LINE_P (w))
12441 display_mode_line (w, HEADER_LINE_FACE_ID,
12442 current_buffer->header_line_format);
12443 ++n;
12446 return n;
12450 /* Display mode or top line of window W. FACE_ID specifies which line
12451 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12452 FORMAT is the mode line format to display. Value is the pixel
12453 height of the mode line displayed. */
12455 static int
12456 display_mode_line (w, face_id, format)
12457 struct window *w;
12458 enum face_id face_id;
12459 Lisp_Object format;
12461 struct it it;
12462 struct face *face;
12464 init_iterator (&it, w, -1, -1, NULL, face_id);
12465 prepare_desired_row (it.glyph_row);
12467 /* Temporarily make frame's keyboard the current kboard so that
12468 kboard-local variables in the mode_line_format will get the right
12469 values. */
12470 push_frame_kboard (it.f);
12471 display_mode_element (&it, 0, 0, 0, format);
12472 pop_frame_kboard ();
12474 /* Fill up with spaces. */
12475 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12477 compute_line_metrics (&it);
12478 it.glyph_row->full_width_p = 1;
12479 it.glyph_row->mode_line_p = 1;
12480 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12481 it.glyph_row->continued_p = 0;
12482 it.glyph_row->truncated_on_left_p = 0;
12483 it.glyph_row->truncated_on_right_p = 0;
12485 /* Make a 3D mode-line have a shadow at its right end. */
12486 face = FACE_FROM_ID (it.f, face_id);
12487 extend_face_to_end_of_line (&it);
12488 if (face->box != FACE_NO_BOX)
12490 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12491 + it.glyph_row->used[TEXT_AREA] - 1);
12492 last->right_box_line_p = 1;
12495 return it.glyph_row->height;
12499 /* Contribute ELT to the mode line for window IT->w. How it
12500 translates into text depends on its data type.
12502 IT describes the display environment in which we display, as usual.
12504 DEPTH is the depth in recursion. It is used to prevent
12505 infinite recursion here.
12507 FIELD_WIDTH is the number of characters the display of ELT should
12508 occupy in the mode line, and PRECISION is the maximum number of
12509 characters to display from ELT's representation. See
12510 display_string for details. *
12512 Returns the hpos of the end of the text generated by ELT. */
12514 static int
12515 display_mode_element (it, depth, field_width, precision, elt)
12516 struct it *it;
12517 int depth;
12518 int field_width, precision;
12519 Lisp_Object elt;
12521 int n = 0, field, prec;
12523 tail_recurse:
12524 if (depth > 10)
12525 goto invalid;
12527 depth++;
12529 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12531 case Lisp_String:
12533 /* A string: output it and check for %-constructs within it. */
12534 unsigned char c;
12535 unsigned char *this = XSTRING (elt)->data;
12536 unsigned char *lisp_string = this;
12538 while ((precision <= 0 || n < precision)
12539 && *this
12540 && (frame_title_ptr
12541 || it->current_x < it->last_visible_x))
12543 unsigned char *last = this;
12545 /* Advance to end of string or next format specifier. */
12546 while ((c = *this++) != '\0' && c != '%')
12549 if (this - 1 != last)
12551 /* Output to end of string or up to '%'. Field width
12552 is length of string. Don't output more than
12553 PRECISION allows us. */
12554 prec = --this - last;
12555 if (precision > 0 && prec > precision - n)
12556 prec = precision - n;
12558 if (frame_title_ptr)
12559 n += store_frame_title (last, prec, prec);
12560 else
12561 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12562 it, 0, prec, 0, -1);
12564 else /* c == '%' */
12566 unsigned char *percent_position = this;
12568 /* Get the specified minimum width. Zero means
12569 don't pad. */
12570 field = 0;
12571 while ((c = *this++) >= '0' && c <= '9')
12572 field = field * 10 + c - '0';
12574 /* Don't pad beyond the total padding allowed. */
12575 if (field_width - n > 0 && field > field_width - n)
12576 field = field_width - n;
12578 /* Note that either PRECISION <= 0 or N < PRECISION. */
12579 prec = precision - n;
12581 if (c == 'M')
12582 n += display_mode_element (it, depth, field, prec,
12583 Vglobal_mode_string);
12584 else if (c != 0)
12586 unsigned char *spec
12587 = decode_mode_spec (it->w, c, field, prec);
12589 if (frame_title_ptr)
12590 n += store_frame_title (spec, field, prec);
12591 else
12593 int nglyphs_before
12594 = it->glyph_row->used[TEXT_AREA];
12595 int charpos
12596 = percent_position - XSTRING (elt)->data;
12597 int nwritten
12598 = display_string (spec, Qnil, elt, charpos, 0, it,
12599 field, prec, 0, -1);
12601 /* Assign to the glyphs written above the
12602 string where the `%x' came from, position
12603 of the `%'. */
12604 if (nwritten > 0)
12606 struct glyph *glyph
12607 = (it->glyph_row->glyphs[TEXT_AREA]
12608 + nglyphs_before);
12609 int i;
12611 for (i = 0; i < nwritten; ++i)
12613 glyph[i].object = elt;
12614 glyph[i].charpos = charpos;
12617 n += nwritten;
12624 break;
12626 case Lisp_Symbol:
12627 /* A symbol: process the value of the symbol recursively
12628 as if it appeared here directly. Avoid error if symbol void.
12629 Special case: if value of symbol is a string, output the string
12630 literally. */
12632 register Lisp_Object tem;
12633 tem = Fboundp (elt);
12634 if (!NILP (tem))
12636 tem = Fsymbol_value (elt);
12637 /* If value is a string, output that string literally:
12638 don't check for % within it. */
12639 if (STRINGP (tem))
12641 prec = XSTRING (tem)->size;
12642 if (precision > 0 && prec > precision - n)
12643 prec = precision - n;
12644 if (frame_title_ptr)
12645 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12646 else
12647 n += display_string (NULL, tem, Qnil, 0, 0, it,
12648 0, prec, 0, -1);
12650 else if (!EQ (tem, elt))
12652 /* Give up right away for nil or t. */
12653 elt = tem;
12654 goto tail_recurse;
12658 break;
12660 case Lisp_Cons:
12662 register Lisp_Object car, tem;
12664 /* A cons cell: three distinct cases.
12665 If first element is a string or a cons, process all the elements
12666 and effectively concatenate them.
12667 If first element is a negative number, truncate displaying cdr to
12668 at most that many characters. If positive, pad (with spaces)
12669 to at least that many characters.
12670 If first element is a symbol, process the cadr or caddr recursively
12671 according to whether the symbol's value is non-nil or nil. */
12672 car = XCAR (elt);
12673 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12675 /* An element of the form (:eval FORM) means evaluate FORM
12676 and use the result as mode line elements. */
12677 struct gcpro gcpro1;
12678 Lisp_Object spec;
12680 spec = safe_eval (XCAR (XCDR (elt)));
12681 GCPRO1 (spec);
12682 n += display_mode_element (it, depth, field_width - n,
12683 precision - n, spec);
12684 UNGCPRO;
12686 else if (SYMBOLP (car))
12688 tem = Fboundp (car);
12689 elt = XCDR (elt);
12690 if (!CONSP (elt))
12691 goto invalid;
12692 /* elt is now the cdr, and we know it is a cons cell.
12693 Use its car if CAR has a non-nil value. */
12694 if (!NILP (tem))
12696 tem = Fsymbol_value (car);
12697 if (!NILP (tem))
12699 elt = XCAR (elt);
12700 goto tail_recurse;
12703 /* Symbol's value is nil (or symbol is unbound)
12704 Get the cddr of the original list
12705 and if possible find the caddr and use that. */
12706 elt = XCDR (elt);
12707 if (NILP (elt))
12708 break;
12709 else if (!CONSP (elt))
12710 goto invalid;
12711 elt = XCAR (elt);
12712 goto tail_recurse;
12714 else if (INTEGERP (car))
12716 register int lim = XINT (car);
12717 elt = XCDR (elt);
12718 if (lim < 0)
12720 /* Negative int means reduce maximum width. */
12721 if (precision <= 0)
12722 precision = -lim;
12723 else
12724 precision = min (precision, -lim);
12726 else if (lim > 0)
12728 /* Padding specified. Don't let it be more than
12729 current maximum. */
12730 if (precision > 0)
12731 lim = min (precision, lim);
12733 /* If that's more padding than already wanted, queue it.
12734 But don't reduce padding already specified even if
12735 that is beyond the current truncation point. */
12736 field_width = max (lim, field_width);
12738 goto tail_recurse;
12740 else if (STRINGP (car) || CONSP (car))
12742 register int limit = 50;
12743 /* Limit is to protect against circular lists. */
12744 while (CONSP (elt)
12745 && --limit > 0
12746 && (precision <= 0 || n < precision))
12748 n += display_mode_element (it, depth, field_width - n,
12749 precision - n, XCAR (elt));
12750 elt = XCDR (elt);
12754 break;
12756 default:
12757 invalid:
12758 if (frame_title_ptr)
12759 n += store_frame_title ("*invalid*", 0, precision - n);
12760 else
12761 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12762 precision - n, 0, 0);
12763 return n;
12766 /* Pad to FIELD_WIDTH. */
12767 if (field_width > 0 && n < field_width)
12769 if (frame_title_ptr)
12770 n += store_frame_title ("", field_width - n, 0);
12771 else
12772 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12773 0, 0, 0);
12776 return n;
12780 /* Write a null-terminated, right justified decimal representation of
12781 the positive integer D to BUF using a minimal field width WIDTH. */
12783 static void
12784 pint2str (buf, width, d)
12785 register char *buf;
12786 register int width;
12787 register int d;
12789 register char *p = buf;
12791 if (d <= 0)
12792 *p++ = '0';
12793 else
12795 while (d > 0)
12797 *p++ = d % 10 + '0';
12798 d /= 10;
12802 for (width -= (int) (p - buf); width > 0; --width)
12803 *p++ = ' ';
12804 *p-- = '\0';
12805 while (p > buf)
12807 d = *buf;
12808 *buf++ = *p;
12809 *p-- = d;
12813 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12814 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12815 type of CODING_SYSTEM. Return updated pointer into BUF. */
12817 static unsigned char invalid_eol_type[] = "(*invalid*)";
12819 static char *
12820 decode_mode_spec_coding (coding_system, buf, eol_flag)
12821 Lisp_Object coding_system;
12822 register char *buf;
12823 int eol_flag;
12825 Lisp_Object val;
12826 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12827 unsigned char *eol_str;
12828 int eol_str_len;
12829 /* The EOL conversion we are using. */
12830 Lisp_Object eoltype;
12832 val = Fget (coding_system, Qcoding_system);
12833 eoltype = Qnil;
12835 if (!VECTORP (val)) /* Not yet decided. */
12837 if (multibyte)
12838 *buf++ = '-';
12839 if (eol_flag)
12840 eoltype = eol_mnemonic_undecided;
12841 /* Don't mention EOL conversion if it isn't decided. */
12843 else
12845 Lisp_Object eolvalue;
12847 eolvalue = Fget (coding_system, Qeol_type);
12849 if (multibyte)
12850 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12852 if (eol_flag)
12854 /* The EOL conversion that is normal on this system. */
12856 if (NILP (eolvalue)) /* Not yet decided. */
12857 eoltype = eol_mnemonic_undecided;
12858 else if (VECTORP (eolvalue)) /* Not yet decided. */
12859 eoltype = eol_mnemonic_undecided;
12860 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12861 eoltype = (XFASTINT (eolvalue) == 0
12862 ? eol_mnemonic_unix
12863 : (XFASTINT (eolvalue) == 1
12864 ? eol_mnemonic_dos : eol_mnemonic_mac));
12868 if (eol_flag)
12870 /* Mention the EOL conversion if it is not the usual one. */
12871 if (STRINGP (eoltype))
12873 eol_str = XSTRING (eoltype)->data;
12874 eol_str_len = XSTRING (eoltype)->size;
12876 else if (INTEGERP (eoltype)
12877 && CHAR_VALID_P (XINT (eoltype), 0))
12879 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12880 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12882 else
12884 eol_str = invalid_eol_type;
12885 eol_str_len = sizeof (invalid_eol_type) - 1;
12887 bcopy (eol_str, buf, eol_str_len);
12888 buf += eol_str_len;
12891 return buf;
12894 /* Return a string for the output of a mode line %-spec for window W,
12895 generated by character C. PRECISION >= 0 means don't return a
12896 string longer than that value. FIELD_WIDTH > 0 means pad the
12897 string returned with spaces to that value. */
12899 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12901 static char *
12902 decode_mode_spec (w, c, field_width, precision)
12903 struct window *w;
12904 register int c;
12905 int field_width, precision;
12907 Lisp_Object obj;
12908 struct frame *f = XFRAME (WINDOW_FRAME (w));
12909 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12910 struct buffer *b = XBUFFER (w->buffer);
12912 obj = Qnil;
12914 switch (c)
12916 case '*':
12917 if (!NILP (b->read_only))
12918 return "%";
12919 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12920 return "*";
12921 return "-";
12923 case '+':
12924 /* This differs from %* only for a modified read-only buffer. */
12925 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12926 return "*";
12927 if (!NILP (b->read_only))
12928 return "%";
12929 return "-";
12931 case '&':
12932 /* This differs from %* in ignoring read-only-ness. */
12933 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12934 return "*";
12935 return "-";
12937 case '%':
12938 return "%";
12940 case '[':
12942 int i;
12943 char *p;
12945 if (command_loop_level > 5)
12946 return "[[[... ";
12947 p = decode_mode_spec_buf;
12948 for (i = 0; i < command_loop_level; i++)
12949 *p++ = '[';
12950 *p = 0;
12951 return decode_mode_spec_buf;
12954 case ']':
12956 int i;
12957 char *p;
12959 if (command_loop_level > 5)
12960 return " ...]]]";
12961 p = decode_mode_spec_buf;
12962 for (i = 0; i < command_loop_level; i++)
12963 *p++ = ']';
12964 *p = 0;
12965 return decode_mode_spec_buf;
12968 case '-':
12970 register int i;
12972 /* Let lots_of_dashes be a string of infinite length. */
12973 if (field_width <= 0
12974 || field_width > sizeof (lots_of_dashes))
12976 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12977 decode_mode_spec_buf[i] = '-';
12978 decode_mode_spec_buf[i] = '\0';
12979 return decode_mode_spec_buf;
12981 else
12982 return lots_of_dashes;
12985 case 'b':
12986 obj = b->name;
12987 break;
12989 case 'c':
12991 int col = current_column ();
12992 XSETFASTINT (w->column_number_displayed, col);
12993 pint2str (decode_mode_spec_buf, field_width, col);
12994 return decode_mode_spec_buf;
12997 case 'F':
12998 /* %F displays the frame name. */
12999 if (!NILP (f->title))
13000 return (char *) XSTRING (f->title)->data;
13001 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13002 return (char *) XSTRING (f->name)->data;
13003 return "Emacs";
13005 case 'f':
13006 obj = b->filename;
13007 break;
13009 case 'l':
13011 int startpos = XMARKER (w->start)->charpos;
13012 int startpos_byte = marker_byte_position (w->start);
13013 int line, linepos, linepos_byte, topline;
13014 int nlines, junk;
13015 int height = XFASTINT (w->height);
13017 /* If we decided that this buffer isn't suitable for line numbers,
13018 don't forget that too fast. */
13019 if (EQ (w->base_line_pos, w->buffer))
13020 goto no_value;
13021 /* But do forget it, if the window shows a different buffer now. */
13022 else if (BUFFERP (w->base_line_pos))
13023 w->base_line_pos = Qnil;
13025 /* If the buffer is very big, don't waste time. */
13026 if (INTEGERP (Vline_number_display_limit)
13027 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13029 w->base_line_pos = Qnil;
13030 w->base_line_number = Qnil;
13031 goto no_value;
13034 if (!NILP (w->base_line_number)
13035 && !NILP (w->base_line_pos)
13036 && XFASTINT (w->base_line_pos) <= startpos)
13038 line = XFASTINT (w->base_line_number);
13039 linepos = XFASTINT (w->base_line_pos);
13040 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13042 else
13044 line = 1;
13045 linepos = BUF_BEGV (b);
13046 linepos_byte = BUF_BEGV_BYTE (b);
13049 /* Count lines from base line to window start position. */
13050 nlines = display_count_lines (linepos, linepos_byte,
13051 startpos_byte,
13052 startpos, &junk);
13054 topline = nlines + line;
13056 /* Determine a new base line, if the old one is too close
13057 or too far away, or if we did not have one.
13058 "Too close" means it's plausible a scroll-down would
13059 go back past it. */
13060 if (startpos == BUF_BEGV (b))
13062 XSETFASTINT (w->base_line_number, topline);
13063 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13065 else if (nlines < height + 25 || nlines > height * 3 + 50
13066 || linepos == BUF_BEGV (b))
13068 int limit = BUF_BEGV (b);
13069 int limit_byte = BUF_BEGV_BYTE (b);
13070 int position;
13071 int distance = (height * 2 + 30) * line_number_display_limit_width;
13073 if (startpos - distance > limit)
13075 limit = startpos - distance;
13076 limit_byte = CHAR_TO_BYTE (limit);
13079 nlines = display_count_lines (startpos, startpos_byte,
13080 limit_byte,
13081 - (height * 2 + 30),
13082 &position);
13083 /* If we couldn't find the lines we wanted within
13084 line_number_display_limit_width chars per line,
13085 give up on line numbers for this window. */
13086 if (position == limit_byte && limit == startpos - distance)
13088 w->base_line_pos = w->buffer;
13089 w->base_line_number = Qnil;
13090 goto no_value;
13093 XSETFASTINT (w->base_line_number, topline - nlines);
13094 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13097 /* Now count lines from the start pos to point. */
13098 nlines = display_count_lines (startpos, startpos_byte,
13099 PT_BYTE, PT, &junk);
13101 /* Record that we did display the line number. */
13102 line_number_displayed = 1;
13104 /* Make the string to show. */
13105 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13106 return decode_mode_spec_buf;
13107 no_value:
13109 char* p = decode_mode_spec_buf;
13110 int pad = field_width - 2;
13111 while (pad-- > 0)
13112 *p++ = ' ';
13113 *p++ = '?';
13114 *p++ = '?';
13115 *p = '\0';
13116 return decode_mode_spec_buf;
13119 break;
13121 case 'm':
13122 obj = b->mode_name;
13123 break;
13125 case 'n':
13126 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13127 return " Narrow";
13128 break;
13130 case 'p':
13132 int pos = marker_position (w->start);
13133 int total = BUF_ZV (b) - BUF_BEGV (b);
13135 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13137 if (pos <= BUF_BEGV (b))
13138 return "All";
13139 else
13140 return "Bottom";
13142 else if (pos <= BUF_BEGV (b))
13143 return "Top";
13144 else
13146 if (total > 1000000)
13147 /* Do it differently for a large value, to avoid overflow. */
13148 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13149 else
13150 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13151 /* We can't normally display a 3-digit number,
13152 so get us a 2-digit number that is close. */
13153 if (total == 100)
13154 total = 99;
13155 sprintf (decode_mode_spec_buf, "%2d%%", total);
13156 return decode_mode_spec_buf;
13160 /* Display percentage of size above the bottom of the screen. */
13161 case 'P':
13163 int toppos = marker_position (w->start);
13164 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13165 int total = BUF_ZV (b) - BUF_BEGV (b);
13167 if (botpos >= BUF_ZV (b))
13169 if (toppos <= BUF_BEGV (b))
13170 return "All";
13171 else
13172 return "Bottom";
13174 else
13176 if (total > 1000000)
13177 /* Do it differently for a large value, to avoid overflow. */
13178 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13179 else
13180 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13181 /* We can't normally display a 3-digit number,
13182 so get us a 2-digit number that is close. */
13183 if (total == 100)
13184 total = 99;
13185 if (toppos <= BUF_BEGV (b))
13186 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13187 else
13188 sprintf (decode_mode_spec_buf, "%2d%%", total);
13189 return decode_mode_spec_buf;
13193 case 's':
13194 /* status of process */
13195 obj = Fget_buffer_process (w->buffer);
13196 if (NILP (obj))
13197 return "no process";
13198 #ifdef subprocesses
13199 obj = Fsymbol_name (Fprocess_status (obj));
13200 #endif
13201 break;
13203 case 't': /* indicate TEXT or BINARY */
13204 #ifdef MODE_LINE_BINARY_TEXT
13205 return MODE_LINE_BINARY_TEXT (b);
13206 #else
13207 return "T";
13208 #endif
13210 case 'z':
13211 /* coding-system (not including end-of-line format) */
13212 case 'Z':
13213 /* coding-system (including end-of-line type) */
13215 int eol_flag = (c == 'Z');
13216 char *p = decode_mode_spec_buf;
13218 if (! FRAME_WINDOW_P (f))
13220 /* No need to mention EOL here--the terminal never needs
13221 to do EOL conversion. */
13222 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13223 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13225 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13226 p, eol_flag);
13228 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13229 #ifdef subprocesses
13230 obj = Fget_buffer_process (Fcurrent_buffer ());
13231 if (PROCESSP (obj))
13233 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13234 p, eol_flag);
13235 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13236 p, eol_flag);
13238 #endif /* subprocesses */
13239 #endif /* 0 */
13240 *p = 0;
13241 return decode_mode_spec_buf;
13245 if (STRINGP (obj))
13246 return (char *) XSTRING (obj)->data;
13247 else
13248 return "";
13252 /* Count up to COUNT lines starting from START / START_BYTE.
13253 But don't go beyond LIMIT_BYTE.
13254 Return the number of lines thus found (always nonnegative).
13256 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13258 static int
13259 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13260 int start, start_byte, limit_byte, count;
13261 int *byte_pos_ptr;
13263 register unsigned char *cursor;
13264 unsigned char *base;
13266 register int ceiling;
13267 register unsigned char *ceiling_addr;
13268 int orig_count = count;
13270 /* If we are not in selective display mode,
13271 check only for newlines. */
13272 int selective_display = (!NILP (current_buffer->selective_display)
13273 && !INTEGERP (current_buffer->selective_display));
13275 if (count > 0)
13277 while (start_byte < limit_byte)
13279 ceiling = BUFFER_CEILING_OF (start_byte);
13280 ceiling = min (limit_byte - 1, ceiling);
13281 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13282 base = (cursor = BYTE_POS_ADDR (start_byte));
13283 while (1)
13285 if (selective_display)
13286 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13288 else
13289 while (*cursor != '\n' && ++cursor != ceiling_addr)
13292 if (cursor != ceiling_addr)
13294 if (--count == 0)
13296 start_byte += cursor - base + 1;
13297 *byte_pos_ptr = start_byte;
13298 return orig_count;
13300 else
13301 if (++cursor == ceiling_addr)
13302 break;
13304 else
13305 break;
13307 start_byte += cursor - base;
13310 else
13312 while (start_byte > limit_byte)
13314 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13315 ceiling = max (limit_byte, ceiling);
13316 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13317 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13318 while (1)
13320 if (selective_display)
13321 while (--cursor != ceiling_addr
13322 && *cursor != '\n' && *cursor != 015)
13324 else
13325 while (--cursor != ceiling_addr && *cursor != '\n')
13328 if (cursor != ceiling_addr)
13330 if (++count == 0)
13332 start_byte += cursor - base + 1;
13333 *byte_pos_ptr = start_byte;
13334 /* When scanning backwards, we should
13335 not count the newline posterior to which we stop. */
13336 return - orig_count - 1;
13339 else
13340 break;
13342 /* Here we add 1 to compensate for the last decrement
13343 of CURSOR, which took it past the valid range. */
13344 start_byte += cursor - base + 1;
13348 *byte_pos_ptr = limit_byte;
13350 if (count < 0)
13351 return - orig_count + count;
13352 return orig_count - count;
13358 /***********************************************************************
13359 Displaying strings
13360 ***********************************************************************/
13362 /* Display a NUL-terminated string, starting with index START.
13364 If STRING is non-null, display that C string. Otherwise, the Lisp
13365 string LISP_STRING is displayed.
13367 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13368 FACE_STRING. Display STRING or LISP_STRING with the face at
13369 FACE_STRING_POS in FACE_STRING:
13371 Display the string in the environment given by IT, but use the
13372 standard display table, temporarily.
13374 FIELD_WIDTH is the minimum number of output glyphs to produce.
13375 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13376 with spaces. If STRING has more characters, more than FIELD_WIDTH
13377 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13379 PRECISION is the maximum number of characters to output from
13380 STRING. PRECISION < 0 means don't truncate the string.
13382 This is roughly equivalent to printf format specifiers:
13384 FIELD_WIDTH PRECISION PRINTF
13385 ----------------------------------------
13386 -1 -1 %s
13387 -1 10 %.10s
13388 10 -1 %10s
13389 20 10 %20.10s
13391 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13392 display them, and < 0 means obey the current buffer's value of
13393 enable_multibyte_characters.
13395 Value is the number of glyphs produced. */
13397 static int
13398 display_string (string, lisp_string, face_string, face_string_pos,
13399 start, it, field_width, precision, max_x, multibyte)
13400 unsigned char *string;
13401 Lisp_Object lisp_string;
13402 Lisp_Object face_string;
13403 int face_string_pos;
13404 int start;
13405 struct it *it;
13406 int field_width, precision, max_x;
13407 int multibyte;
13409 int hpos_at_start = it->hpos;
13410 int saved_face_id = it->face_id;
13411 struct glyph_row *row = it->glyph_row;
13413 /* Initialize the iterator IT for iteration over STRING beginning
13414 with index START. We assume that IT may be modified here (which
13415 means that display_line has to do something when displaying a
13416 mini-buffer prompt, which it does). */
13417 reseat_to_string (it, string, lisp_string, start,
13418 precision, field_width, multibyte);
13420 /* If displaying STRING, set up the face of the iterator
13421 from LISP_STRING, if that's given. */
13422 if (STRINGP (face_string))
13424 int endptr;
13425 struct face *face;
13427 it->face_id
13428 = face_at_string_position (it->w, face_string, face_string_pos,
13429 0, it->region_beg_charpos,
13430 it->region_end_charpos,
13431 &endptr, it->base_face_id);
13432 face = FACE_FROM_ID (it->f, it->face_id);
13433 it->face_box_p = face->box != FACE_NO_BOX;
13436 /* Set max_x to the maximum allowed X position. Don't let it go
13437 beyond the right edge of the window. */
13438 if (max_x <= 0)
13439 max_x = it->last_visible_x;
13440 else
13441 max_x = min (max_x, it->last_visible_x);
13443 /* Skip over display elements that are not visible. because IT->w is
13444 hscrolled. */
13445 if (it->current_x < it->first_visible_x)
13446 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13447 MOVE_TO_POS | MOVE_TO_X);
13449 row->ascent = it->max_ascent;
13450 row->height = it->max_ascent + it->max_descent;
13451 row->phys_ascent = it->max_phys_ascent;
13452 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13454 /* This condition is for the case that we are called with current_x
13455 past last_visible_x. */
13456 while (it->current_x < max_x)
13458 int x_before, x, n_glyphs_before, i, nglyphs;
13460 /* Get the next display element. */
13461 if (!get_next_display_element (it))
13462 break;
13464 /* Produce glyphs. */
13465 x_before = it->current_x;
13466 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13467 PRODUCE_GLYPHS (it);
13469 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13470 i = 0;
13471 x = x_before;
13472 while (i < nglyphs)
13474 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13476 if (!it->truncate_lines_p
13477 && x + glyph->pixel_width > max_x)
13479 /* End of continued line or max_x reached. */
13480 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13481 it->current_x = x;
13482 break;
13484 else if (x + glyph->pixel_width > it->first_visible_x)
13486 /* Glyph is at least partially visible. */
13487 ++it->hpos;
13488 if (x < it->first_visible_x)
13489 it->glyph_row->x = x - it->first_visible_x;
13491 else
13493 /* Glyph is off the left margin of the display area.
13494 Should not happen. */
13495 abort ();
13498 row->ascent = max (row->ascent, it->max_ascent);
13499 row->height = max (row->height, it->max_ascent + it->max_descent);
13500 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13501 row->phys_height = max (row->phys_height,
13502 it->max_phys_ascent + it->max_phys_descent);
13503 x += glyph->pixel_width;
13504 ++i;
13507 /* Stop if max_x reached. */
13508 if (i < nglyphs)
13509 break;
13511 /* Stop at line ends. */
13512 if (ITERATOR_AT_END_OF_LINE_P (it))
13514 it->continuation_lines_width = 0;
13515 break;
13518 set_iterator_to_next (it, 1);
13520 /* Stop if truncating at the right edge. */
13521 if (it->truncate_lines_p
13522 && it->current_x >= it->last_visible_x)
13524 /* Add truncation mark, but don't do it if the line is
13525 truncated at a padding space. */
13526 if (IT_CHARPOS (*it) < it->string_nchars)
13528 if (!FRAME_WINDOW_P (it->f))
13529 produce_special_glyphs (it, IT_TRUNCATION);
13530 it->glyph_row->truncated_on_right_p = 1;
13532 break;
13536 /* Maybe insert a truncation at the left. */
13537 if (it->first_visible_x
13538 && IT_CHARPOS (*it) > 0)
13540 if (!FRAME_WINDOW_P (it->f))
13541 insert_left_trunc_glyphs (it);
13542 it->glyph_row->truncated_on_left_p = 1;
13545 it->face_id = saved_face_id;
13547 /* Value is number of columns displayed. */
13548 return it->hpos - hpos_at_start;
13553 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13554 appears as an element of LIST or as the car of an element of LIST.
13555 If PROPVAL is a list, compare each element against LIST in that
13556 way, and return 1 if any element of PROPVAL is found in LIST.
13557 Otherwise return 0. This function cannot quit. */
13560 invisible_p (propval, list)
13561 register Lisp_Object propval;
13562 Lisp_Object list;
13564 register Lisp_Object tail, proptail;
13566 for (tail = list; CONSP (tail); tail = XCDR (tail))
13568 register Lisp_Object tem;
13569 tem = XCAR (tail);
13570 if (EQ (propval, tem))
13571 return 1;
13572 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13573 return 1;
13576 if (CONSP (propval))
13578 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13580 Lisp_Object propelt;
13581 propelt = XCAR (proptail);
13582 for (tail = list; CONSP (tail); tail = XCDR (tail))
13584 register Lisp_Object tem;
13585 tem = XCAR (tail);
13586 if (EQ (propelt, tem))
13587 return 1;
13588 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13589 return 1;
13594 return 0;
13598 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13599 the cdr of that element is non-nil. If PROPVAL is a list, check
13600 each element of PROPVAL in that way, and the first time some
13601 element is found, return 1 if the cdr of that element is non-nil.
13602 Otherwise return 0. This function cannot quit. */
13605 invisible_ellipsis_p (propval, list)
13606 register Lisp_Object propval;
13607 Lisp_Object list;
13609 register Lisp_Object tail, proptail;
13611 for (tail = list; CONSP (tail); tail = XCDR (tail))
13613 register Lisp_Object tem;
13614 tem = XCAR (tail);
13615 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13616 return ! NILP (XCDR (tem));
13619 if (CONSP (propval))
13620 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13622 Lisp_Object propelt;
13623 propelt = XCAR (proptail);
13624 for (tail = list; CONSP (tail); tail = XCDR (tail))
13626 register Lisp_Object tem;
13627 tem = XCAR (tail);
13628 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13629 return ! NILP (XCDR (tem));
13633 return 0;
13638 /***********************************************************************
13639 Initialization
13640 ***********************************************************************/
13642 void
13643 syms_of_xdisp ()
13645 Vwith_echo_area_save_vector = Qnil;
13646 staticpro (&Vwith_echo_area_save_vector);
13648 Vmessage_stack = Qnil;
13649 staticpro (&Vmessage_stack);
13651 Qinhibit_redisplay = intern ("inhibit-redisplay");
13652 staticpro (&Qinhibit_redisplay);
13654 #if GLYPH_DEBUG
13655 defsubr (&Sdump_glyph_matrix);
13656 defsubr (&Sdump_glyph_row);
13657 defsubr (&Sdump_tool_bar_row);
13658 defsubr (&Strace_redisplay_toggle);
13659 defsubr (&Strace_to_stderr);
13660 #endif
13662 staticpro (&Qmenu_bar_update_hook);
13663 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13665 staticpro (&Qoverriding_terminal_local_map);
13666 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13668 staticpro (&Qoverriding_local_map);
13669 Qoverriding_local_map = intern ("overriding-local-map");
13671 staticpro (&Qwindow_scroll_functions);
13672 Qwindow_scroll_functions = intern ("window-scroll-functions");
13674 staticpro (&Qredisplay_end_trigger_functions);
13675 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13677 staticpro (&Qinhibit_point_motion_hooks);
13678 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13680 QCdata = intern (":data");
13681 staticpro (&QCdata);
13682 Qdisplay = intern ("display");
13683 staticpro (&Qdisplay);
13684 Qspace_width = intern ("space-width");
13685 staticpro (&Qspace_width);
13686 Qraise = intern ("raise");
13687 staticpro (&Qraise);
13688 Qspace = intern ("space");
13689 staticpro (&Qspace);
13690 Qmargin = intern ("margin");
13691 staticpro (&Qmargin);
13692 Qleft_margin = intern ("left-margin");
13693 staticpro (&Qleft_margin);
13694 Qright_margin = intern ("right-margin");
13695 staticpro (&Qright_margin);
13696 Qalign_to = intern ("align-to");
13697 staticpro (&Qalign_to);
13698 QCalign_to = intern (":align-to");
13699 staticpro (&QCalign_to);
13700 Qrelative_width = intern ("relative-width");
13701 staticpro (&Qrelative_width);
13702 QCrelative_width = intern (":relative-width");
13703 staticpro (&QCrelative_width);
13704 QCrelative_height = intern (":relative-height");
13705 staticpro (&QCrelative_height);
13706 QCeval = intern (":eval");
13707 staticpro (&QCeval);
13708 Qwhen = intern ("when");
13709 staticpro (&Qwhen);
13710 QCfile = intern (":file");
13711 staticpro (&QCfile);
13712 Qfontified = intern ("fontified");
13713 staticpro (&Qfontified);
13714 Qfontification_functions = intern ("fontification-functions");
13715 staticpro (&Qfontification_functions);
13716 Qtrailing_whitespace = intern ("trailing-whitespace");
13717 staticpro (&Qtrailing_whitespace);
13718 Qimage = intern ("image");
13719 staticpro (&Qimage);
13720 Qmessage_truncate_lines = intern ("message-truncate-lines");
13721 staticpro (&Qmessage_truncate_lines);
13722 Qgrow_only = intern ("grow-only");
13723 staticpro (&Qgrow_only);
13725 last_arrow_position = Qnil;
13726 last_arrow_string = Qnil;
13727 staticpro (&last_arrow_position);
13728 staticpro (&last_arrow_string);
13730 echo_buffer[0] = echo_buffer[1] = Qnil;
13731 staticpro (&echo_buffer[0]);
13732 staticpro (&echo_buffer[1]);
13734 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13735 staticpro (&echo_area_buffer[0]);
13736 staticpro (&echo_area_buffer[1]);
13738 Vmessages_buffer_name = build_string ("*Messages*");
13739 staticpro (&Vmessages_buffer_name);
13741 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13742 "Non-nil means highlight trailing whitespace.\n\
13743 The face used for trailing whitespace is `trailing-whitespace'.");
13744 Vshow_trailing_whitespace = Qnil;
13746 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13747 "Non-nil means don't actually do any redisplay.\n\
13748 This is used for internal purposes.");
13749 Vinhibit_redisplay = Qnil;
13751 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13752 "String (or mode line construct) included (normally) in `mode-line-format'.");
13753 Vglobal_mode_string = Qnil;
13755 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13756 "Marker for where to display an arrow on top of the buffer text.\n\
13757 This must be the beginning of a line in order to work.\n\
13758 See also `overlay-arrow-string'.");
13759 Voverlay_arrow_position = Qnil;
13761 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13762 "String to display as an arrow. See also `overlay-arrow-position'.");
13763 Voverlay_arrow_string = Qnil;
13765 DEFVAR_INT ("scroll-step", &scroll_step,
13766 "*The number of lines to try scrolling a window by when point moves out.\n\
13767 If that fails to bring point back on frame, point is centered instead.\n\
13768 If this is zero, point is always centered after it moves off frame.\n\
13769 If you want scrolling to always be a line at a time, you should set\n\
13770 `scroll-conservatively' to a large value rather than set this to 1.");
13772 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13773 "*Scroll up to this many lines, to bring point back on screen.\n\
13774 A value of zero means to scroll the text to center point vertically\n\
13775 in the window.");
13776 scroll_conservatively = 0;
13778 DEFVAR_INT ("scroll-margin", &scroll_margin,
13779 "*Number of lines of margin at the top and bottom of a window.\n\
13780 Recenter the window whenever point gets within this many lines\n\
13781 of the top or bottom of the window.");
13782 scroll_margin = 0;
13784 #if GLYPH_DEBUG
13785 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13786 #endif
13788 DEFVAR_BOOL ("truncate-partial-width-windows",
13789 &truncate_partial_width_windows,
13790 "*Non-nil means truncate lines in all windows less than full frame wide.");
13791 truncate_partial_width_windows = 1;
13793 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13794 "*Non-nil means use inverse video for the mode line.");
13795 mode_line_inverse_video = 1;
13797 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13798 "*Maximum buffer size for which line number should be displayed.\n\
13799 If the buffer is bigger than this, the line number does not appear\n\
13800 in the mode line. A value of nil means no limit.");
13801 Vline_number_display_limit = Qnil;
13803 DEFVAR_INT ("line-number-display-limit-width",
13804 &line_number_display_limit_width,
13805 "*Maximum line width (in characters) for line number display.\n\
13806 If the average length of the lines near point is bigger than this, then the\n\
13807 line number may be omitted from the mode line.");
13808 line_number_display_limit_width = 200;
13810 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13811 "*Non-nil means highlight region even in nonselected windows.");
13812 highlight_nonselected_windows = 0;
13814 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13815 "Non-nil if more than one frame is visible on this display.\n\
13816 Minibuffer-only frames don't count, but iconified frames do.\n\
13817 This variable is not guaranteed to be accurate except while processing\n\
13818 `frame-title-format' and `icon-title-format'.");
13820 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13821 "Template for displaying the title bar of visible frames.\n\
13822 \(Assuming the window manager supports this feature.)\n\
13823 This variable has the same structure as `mode-line-format' (which see),\n\
13824 and is used only on frames for which no explicit name has been set\n\
13825 \(see `modify-frame-parameters').");
13826 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13827 "Template for displaying the title bar of an iconified frame.\n\
13828 \(Assuming the window manager supports this feature.)\n\
13829 This variable has the same structure as `mode-line-format' (which see),\n\
13830 and is used only on frames for which no explicit name has been set\n\
13831 \(see `modify-frame-parameters').");
13832 Vicon_title_format
13833 = Vframe_title_format
13834 = Fcons (intern ("multiple-frames"),
13835 Fcons (build_string ("%b"),
13836 Fcons (Fcons (build_string (""),
13837 Fcons (intern ("invocation-name"),
13838 Fcons (build_string ("@"),
13839 Fcons (intern ("system-name"),
13840 Qnil)))),
13841 Qnil)));
13843 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13844 "Maximum number of lines to keep in the message log buffer.\n\
13845 If nil, disable message logging. If t, log messages but don't truncate\n\
13846 the buffer when it becomes large.");
13847 XSETFASTINT (Vmessage_log_max, 50);
13849 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13850 "Functions called before redisplay, if window sizes have changed.\n\
13851 The value should be a list of functions that take one argument.\n\
13852 Just before redisplay, for each frame, if any of its windows have changed\n\
13853 size since the last redisplay, or have been split or deleted,\n\
13854 all the functions in the list are called, with the frame as argument.");
13855 Vwindow_size_change_functions = Qnil;
13857 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13858 "List of Functions to call before redisplaying a window with scrolling.\n\
13859 Each function is called with two arguments, the window\n\
13860 and its new display-start position. Note that the value of `window-end'\n\
13861 is not valid when these functions are called.");
13862 Vwindow_scroll_functions = Qnil;
13864 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13865 "*Non-nil means automatically resize tool-bars.\n\
13866 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13867 It decreases a tool-bar's height when it would display blank lines\n\
13868 otherwise.");
13869 auto_resize_tool_bars_p = 1;
13871 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13872 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13873 auto_raise_tool_bar_buttons_p = 1;
13875 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13876 "*Margin around tool-bar buttons in pixels.");
13877 tool_bar_button_margin = 1;
13879 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13880 "Relief thickness of tool-bar buttons.");
13881 tool_bar_button_relief = 3;
13883 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13884 "List of functions to call to fontify regions of text.\n\
13885 Each function is called with one argument POS. Functions must\n\
13886 fontify a region starting at POS in the current buffer, and give\n\
13887 fontified regions the property `fontified'.\n\
13888 This variable automatically becomes buffer-local when set.");
13889 Vfontification_functions = Qnil;
13890 Fmake_local_variable (Qfontification_functions);
13892 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13893 &unibyte_display_via_language_environment,
13894 "*Non-nil means display unibyte text according to language environment.\n\
13895 Specifically this means that unibyte non-ASCII characters\n\
13896 are displayed by converting them to the equivalent multibyte characters\n\
13897 according to the current language environment. As a result, they are\n\
13898 displayed according to the current fontset.");
13899 unibyte_display_via_language_environment = 0;
13901 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13902 "*Maximum height for resizing mini-windows.\n\
13903 If a float, it specifies a fraction of the mini-window frame's height.\n\
13904 If an integer, it specifies a number of lines.");
13905 Vmax_mini_window_height = make_float (0.25);
13907 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
13908 "*How to resize the mini-window.\n\
13909 A value of nil means don't automatically resize mini-windows.\n\
13910 A value of t means resize it to fit the text displayed in it.\n\
13911 A value of `grow-only', the default, means let mini-windows grow\n\
13912 only, until the its display becomes empty, at which point the mini-window\n\
13913 goes back to its normal size.");
13914 Vresize_mini_windows = Qgrow_only;
13916 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13917 &cursor_in_non_selected_windows,
13918 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13919 Nil means don't display a cursor there.");
13920 cursor_in_non_selected_windows = 1;
13922 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13923 "*Non-nil means scroll the display automatically to make point visible.");
13924 automatic_hscrolling_p = 1;
13926 DEFVAR_LISP ("image-types", &Vimage_types,
13927 "List of supported image types.\n\
13928 Each element of the list is a symbol for a supported image type.");
13929 Vimage_types = Qnil;
13931 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13932 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13933 Bind this around calls to `message' to let it take effect.");
13934 message_truncate_lines = 0;
13936 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13937 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13938 Can be used to update submenus whose contents should vary.");
13939 Vmenu_bar_update_hook = Qnil;
13943 /* Initialize this module when Emacs starts. */
13945 void
13946 init_xdisp ()
13948 Lisp_Object root_window;
13949 struct window *mini_w;
13951 current_header_line_height = current_mode_line_height = -1;
13953 CHARPOS (this_line_start_pos) = 0;
13955 mini_w = XWINDOW (minibuf_window);
13956 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13958 if (!noninteractive)
13960 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13961 int i;
13963 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13964 set_window_height (root_window,
13965 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13967 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13968 set_window_height (minibuf_window, 1, 0);
13970 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13971 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13973 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13974 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13975 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13977 /* The default ellipsis glyphs `...'. */
13978 for (i = 0; i < 3; ++i)
13979 XSETFASTINT (default_invis_vector[i], '.');
13982 #ifdef HAVE_WINDOW_SYSTEM
13984 /* Allocate the buffer for frame titles. */
13985 int size = 100;
13986 frame_title_buf = (char *) xmalloc (size);
13987 frame_title_buf_end = frame_title_buf + size;
13988 frame_title_ptr = NULL;
13990 #endif /* HAVE_WINDOW_SYSTEM */
13992 help_echo_showing_p = 0;