(standard-latex-block-names): Add "math".
[emacs.git] / src / xdisp.c
blob2b61c26e59cc3bf95cbbc17feb6fdcf3aec5ba2a
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 /* The maximum distance to look ahead for text properties. Values
520 that are too small let us call compute_char_face and similar
521 functions too often which is expensive. Values that are too large
522 let us call compute_char_face and alike too often because we
523 might not be interested in text properties that far away. */
525 #define TEXT_PROP_DISTANCE_LIMIT 100
527 #if GLYPH_DEBUG
529 /* Non-zero means print traces of redisplay if compiled with
530 GLYPH_DEBUG != 0. */
532 int trace_redisplay_p;
534 #endif /* GLYPH_DEBUG */
536 #ifdef DEBUG_TRACE_MOVE
537 /* Non-zero means trace with TRACE_MOVE to stderr. */
538 int trace_move;
540 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
541 #else
542 #define TRACE_MOVE(x) (void) 0
543 #endif
545 /* Non-zero means automatically scroll windows horizontally to make
546 point visible. */
548 int automatic_hscrolling_p;
550 /* A list of symbols, one for each supported image type. */
552 Lisp_Object Vimage_types;
554 /* The variable `resize-mini-windows'. If nil, don't resize
555 mini-winodws. If t, always resize them to fit the text they
556 display. If `grow-only', let mini-windows grow only until they
557 become empty. */
559 Lisp_Object Vresize_mini_windows;
561 /* Value returned from text property handlers (see below). */
563 enum prop_handled
565 HANDLED_NORMALLY,
566 HANDLED_RECOMPUTE_PROPS,
567 HANDLED_OVERLAY_STRING_CONSUMED,
568 HANDLED_RETURN
571 /* A description of text properties that redisplay is interested
572 in. */
574 struct props
576 /* The name of the property. */
577 Lisp_Object *name;
579 /* A unique index for the property. */
580 enum prop_idx idx;
582 /* A handler function called to set up iterator IT from the property
583 at IT's current position. Value is used to steer handle_stop. */
584 enum prop_handled (*handler) P_ ((struct it *it));
587 static enum prop_handled handle_face_prop P_ ((struct it *));
588 static enum prop_handled handle_invisible_prop P_ ((struct it *));
589 static enum prop_handled handle_display_prop P_ ((struct it *));
590 static enum prop_handled handle_composition_prop P_ ((struct it *));
591 static enum prop_handled handle_overlay_change P_ ((struct it *));
592 static enum prop_handled handle_fontified_prop P_ ((struct it *));
594 /* Properties handled by iterators. */
596 static struct props it_props[] =
598 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
599 /* Handle `face' before `display' because some sub-properties of
600 `display' need to know the face. */
601 {&Qface, FACE_PROP_IDX, handle_face_prop},
602 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
603 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
604 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
605 {NULL, 0, NULL}
608 /* Value is the position described by X. If X is a marker, value is
609 the marker_position of X. Otherwise, value is X. */
611 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
613 /* Enumeration returned by some move_it_.* functions internally. */
615 enum move_it_result
617 /* Not used. Undefined value. */
618 MOVE_UNDEFINED,
620 /* Move ended at the requested buffer position or ZV. */
621 MOVE_POS_MATCH_OR_ZV,
623 /* Move ended at the requested X pixel position. */
624 MOVE_X_REACHED,
626 /* Move within a line ended at the end of a line that must be
627 continued. */
628 MOVE_LINE_CONTINUED,
630 /* Move within a line ended at the end of a line that would
631 be displayed truncated. */
632 MOVE_LINE_TRUNCATED,
634 /* Move within a line ended at a line end. */
635 MOVE_NEWLINE_OR_CR
640 /* Function prototypes. */
642 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
643 static int redisplay_mode_lines P_ ((Lisp_Object, int));
644 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
645 static int invisible_text_between_p P_ ((struct it *, int, int));
646 static int next_element_from_ellipsis P_ ((struct it *));
647 static void pint2str P_ ((char *, int, int));
648 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
649 struct text_pos));
650 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
651 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
652 static void store_frame_title_char P_ ((char));
653 static int store_frame_title P_ ((unsigned char *, int, int));
654 static void x_consider_frame_title P_ ((Lisp_Object));
655 static void handle_stop P_ ((struct it *));
656 static int tool_bar_lines_needed P_ ((struct frame *));
657 static int single_display_prop_intangible_p P_ ((Lisp_Object));
658 static void ensure_echo_area_buffers P_ ((void));
659 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
660 struct glyph_row *,
661 struct glyph_row *));
662 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
663 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
664 static int with_echo_area_buffer P_ ((struct window *, int,
665 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
666 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
667 static void clear_garbaged_frames P_ ((void));
668 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
669 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
670 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
671 static int display_echo_area P_ ((struct window *));
672 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
673 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
674 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
675 static int string_char_and_length P_ ((unsigned char *, int, int *));
676 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
677 struct text_pos));
678 static int compute_window_start_on_continuation_line P_ ((struct window *));
679 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
680 static void insert_left_trunc_glyphs P_ ((struct it *));
681 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
682 static void extend_face_to_end_of_line P_ ((struct it *));
683 static int append_space P_ ((struct it *, int));
684 static void make_cursor_line_fully_visible P_ ((struct window *));
685 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
686 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
687 static int trailing_whitespace_p P_ ((int));
688 static int message_log_check_duplicate P_ ((int, int, int, int));
689 int invisible_p P_ ((Lisp_Object, Lisp_Object));
690 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
691 static void push_it P_ ((struct it *));
692 static void pop_it P_ ((struct it *));
693 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
694 static void redisplay_internal P_ ((int));
695 static int echo_area_display P_ ((int));
696 static void redisplay_windows P_ ((Lisp_Object));
697 static void redisplay_window P_ ((Lisp_Object, int));
698 static void update_menu_bar P_ ((struct frame *, int));
699 static int try_window_reusing_current_matrix P_ ((struct window *));
700 static int try_window_id P_ ((struct window *));
701 static int display_line P_ ((struct it *));
702 static int display_mode_lines P_ ((struct window *));
703 static void display_mode_line P_ ((struct window *, enum face_id,
704 Lisp_Object));
705 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
706 static char *decode_mode_spec P_ ((struct window *, int, int, int));
707 static void display_menu_bar P_ ((struct window *));
708 static int display_count_lines P_ ((int, int, int, int, int *));
709 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
710 int, int, struct it *, int, int, int, int));
711 static void compute_line_metrics P_ ((struct it *));
712 static void run_redisplay_end_trigger_hook P_ ((struct it *));
713 static int get_overlay_strings P_ ((struct it *));
714 static void next_overlay_string P_ ((struct it *));
715 static void reseat P_ ((struct it *, struct text_pos, int));
716 static void reseat_1 P_ ((struct it *, struct text_pos, int));
717 static void back_to_previous_visible_line_start P_ ((struct it *));
718 static void reseat_at_previous_visible_line_start P_ ((struct it *));
719 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
720 static int next_element_from_display_vector P_ ((struct it *));
721 static int next_element_from_string P_ ((struct it *));
722 static int next_element_from_c_string P_ ((struct it *));
723 static int next_element_from_buffer P_ ((struct it *));
724 static int next_element_from_composition P_ ((struct it *));
725 static int next_element_from_image P_ ((struct it *));
726 static int next_element_from_stretch P_ ((struct it *));
727 static void load_overlay_strings P_ ((struct it *));
728 static void init_from_display_pos P_ ((struct it *, struct window *,
729 struct display_pos *));
730 static void reseat_to_string P_ ((struct it *, unsigned char *,
731 Lisp_Object, int, int, int, int));
732 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
733 int, int, int));
734 void move_it_vertically_backward P_ ((struct it *, int));
735 static void init_to_row_start P_ ((struct it *, struct window *,
736 struct glyph_row *));
737 static void init_to_row_end P_ ((struct it *, struct window *,
738 struct glyph_row *));
739 static void back_to_previous_line_start P_ ((struct it *));
740 static int forward_to_next_line_start P_ ((struct it *, int *));
741 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
742 Lisp_Object, int));
743 static struct text_pos string_pos P_ ((int, Lisp_Object));
744 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
745 static int number_of_chars P_ ((unsigned char *, int));
746 static void compute_stop_pos P_ ((struct it *));
747 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
748 Lisp_Object));
749 static int face_before_or_after_it_pos P_ ((struct it *, int));
750 static int next_overlay_change P_ ((int));
751 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
752 Lisp_Object, struct text_pos *));
754 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
755 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
757 #ifdef HAVE_WINDOW_SYSTEM
759 static void update_tool_bar P_ ((struct frame *, int));
760 static void build_desired_tool_bar_string P_ ((struct frame *f));
761 static int redisplay_tool_bar P_ ((struct frame *));
762 static void display_tool_bar_line P_ ((struct it *));
764 #endif /* HAVE_WINDOW_SYSTEM */
767 /***********************************************************************
768 Window display dimensions
769 ***********************************************************************/
771 /* Return the window-relative maximum y + 1 for glyph rows displaying
772 text in window W. This is the height of W minus the height of a
773 mode line, if any. */
775 INLINE int
776 window_text_bottom_y (w)
777 struct window *w;
779 struct frame *f = XFRAME (w->frame);
780 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
782 if (WINDOW_WANTS_MODELINE_P (w))
783 height -= CURRENT_MODE_LINE_HEIGHT (w);
784 return height;
788 /* Return the pixel width of display area AREA of window W. AREA < 0
789 means return the total width of W, not including bitmap areas to
790 the left and right of the window. */
792 INLINE int
793 window_box_width (w, area)
794 struct window *w;
795 int area;
797 struct frame *f = XFRAME (w->frame);
798 int width = XFASTINT (w->width);
800 if (!w->pseudo_window_p)
802 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
804 if (area == TEXT_AREA)
806 if (INTEGERP (w->left_margin_width))
807 width -= XFASTINT (w->left_margin_width);
808 if (INTEGERP (w->right_margin_width))
809 width -= XFASTINT (w->right_margin_width);
811 else if (area == LEFT_MARGIN_AREA)
812 width = (INTEGERP (w->left_margin_width)
813 ? XFASTINT (w->left_margin_width) : 0);
814 else if (area == RIGHT_MARGIN_AREA)
815 width = (INTEGERP (w->right_margin_width)
816 ? XFASTINT (w->right_margin_width) : 0);
819 return width * CANON_X_UNIT (f);
823 /* Return the pixel height of the display area of window W, not
824 including mode lines of W, if any.. */
826 INLINE int
827 window_box_height (w)
828 struct window *w;
830 struct frame *f = XFRAME (w->frame);
831 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
833 xassert (height >= 0);
835 if (WINDOW_WANTS_MODELINE_P (w))
836 height -= CURRENT_MODE_LINE_HEIGHT (w);
838 if (WINDOW_WANTS_HEADER_LINE_P (w))
839 height -= CURRENT_HEADER_LINE_HEIGHT (w);
841 return height;
845 /* Return the frame-relative coordinate of the left edge of display
846 area AREA of window W. AREA < 0 means return the left edge of the
847 whole window, to the right of any bitmap area at the left side of
848 W. */
850 INLINE int
851 window_box_left (w, area)
852 struct window *w;
853 int area;
855 struct frame *f = XFRAME (w->frame);
856 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
858 if (!w->pseudo_window_p)
860 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
861 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
863 if (area == TEXT_AREA)
864 x += window_box_width (w, LEFT_MARGIN_AREA);
865 else if (area == RIGHT_MARGIN_AREA)
866 x += (window_box_width (w, LEFT_MARGIN_AREA)
867 + window_box_width (w, TEXT_AREA));
870 return x;
874 /* Return the frame-relative coordinate of the right edge of display
875 area AREA of window W. AREA < 0 means return the left edge of the
876 whole window, to the left of any bitmap area at the right side of
877 W. */
879 INLINE int
880 window_box_right (w, area)
881 struct window *w;
882 int area;
884 return window_box_left (w, area) + window_box_width (w, area);
888 /* Get the bounding box of the display area AREA of window W, without
889 mode lines, in frame-relative coordinates. AREA < 0 means the
890 whole window, not including bitmap areas to the left and right of
891 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
892 coordinates of the upper-left corner of the box. Return in
893 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
895 INLINE void
896 window_box (w, area, box_x, box_y, box_width, box_height)
897 struct window *w;
898 int area;
899 int *box_x, *box_y, *box_width, *box_height;
901 struct frame *f = XFRAME (w->frame);
903 *box_width = window_box_width (w, area);
904 *box_height = window_box_height (w);
905 *box_x = window_box_left (w, area);
906 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
907 + XFASTINT (w->top) * CANON_Y_UNIT (f));
908 if (WINDOW_WANTS_HEADER_LINE_P (w))
909 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
913 /* Get the bounding box of the display area AREA of window W, without
914 mode lines. AREA < 0 means the whole window, not including bitmap
915 areas to the left and right of the window. Return in *TOP_LEFT_X
916 and TOP_LEFT_Y the frame-relative pixel coordinates of the
917 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
918 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
919 box. */
921 INLINE void
922 window_box_edges (w, area, top_left_x, top_left_y,
923 bottom_right_x, bottom_right_y)
924 struct window *w;
925 int area;
926 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
928 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
929 bottom_right_y);
930 *bottom_right_x += *top_left_x;
931 *bottom_right_y += *top_left_y;
936 /***********************************************************************
937 Utilities
938 ***********************************************************************/
940 /* Return 1 if position CHARPOS is visible in window W.
941 Set *FULLY to 1 if POS is visible and the line containing
942 POS is fully visible. */
945 pos_visible_p (w, charpos, fully)
946 struct window *w;
947 int charpos, *fully;
949 struct it it;
950 struct text_pos top;
951 int visible_p;
952 struct buffer *old_buffer = NULL;
954 if (XBUFFER (w->buffer) != current_buffer)
956 old_buffer = current_buffer;
957 set_buffer_internal_1 (XBUFFER (w->buffer));
960 *fully = visible_p = 0;
961 SET_TEXT_POS_FROM_MARKER (top, w->start);
962 start_display (&it, w, top);
964 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
965 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
967 if (IT_CHARPOS (it) == charpos)
969 int line_height, line_bottom_y;
970 int line_top_y = it.current_y;
971 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
973 line_height = it.max_ascent + it.max_descent;
974 if (line_height == 0)
976 if (last_height)
977 line_height = last_height;
978 else
980 move_it_by_lines (&it, 1, 1);
981 line_height = (it.max_ascent || it.max_descent
982 ? it.max_ascent + it.max_descent
983 : last_height);
986 line_bottom_y = line_top_y + line_height;
988 if (line_top_y < window_top_y)
989 visible_p = line_bottom_y > window_top_y;
990 else if (line_top_y < it.last_visible_y)
992 visible_p = 1;
993 *fully = line_bottom_y <= it.last_visible_y;
996 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
998 move_it_by_lines (&it, 1, 0);
999 if (charpos < IT_CHARPOS (it))
1001 visible_p = 1;
1002 *fully = 0;
1006 if (old_buffer)
1007 set_buffer_internal_1 (old_buffer);
1009 return visible_p;
1013 /* Return the next character from STR which is MAXLEN bytes long.
1014 Return in *LEN the length of the character. This is like
1015 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1016 we find one, we return a `?', but with the length of the invalid
1017 character. */
1019 static INLINE int
1020 string_char_and_length (str, maxlen, len)
1021 unsigned char *str;
1022 int maxlen, *len;
1024 int c;
1026 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1027 if (!CHAR_VALID_P (c, 1))
1028 /* We may not change the length here because other places in Emacs
1029 don't use this function, i.e. they silently accept invalid
1030 characters. */
1031 c = '?';
1033 return c;
1038 /* Given a position POS containing a valid character and byte position
1039 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1041 static struct text_pos
1042 string_pos_nchars_ahead (pos, string, nchars)
1043 struct text_pos pos;
1044 Lisp_Object string;
1045 int nchars;
1047 xassert (STRINGP (string) && nchars >= 0);
1049 if (STRING_MULTIBYTE (string))
1051 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1052 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1053 int len;
1055 while (nchars--)
1057 string_char_and_length (p, rest, &len);
1058 p += len, rest -= len;
1059 xassert (rest >= 0);
1060 CHARPOS (pos) += 1;
1061 BYTEPOS (pos) += len;
1064 else
1065 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1067 return pos;
1071 /* Value is the text position, i.e. character and byte position,
1072 for character position CHARPOS in STRING. */
1074 static INLINE struct text_pos
1075 string_pos (charpos, string)
1076 int charpos;
1077 Lisp_Object string;
1079 struct text_pos pos;
1080 xassert (STRINGP (string));
1081 xassert (charpos >= 0);
1082 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1083 return pos;
1087 /* Value is a text position, i.e. character and byte position, for
1088 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1089 means recognize multibyte characters. */
1091 static struct text_pos
1092 c_string_pos (charpos, s, multibyte_p)
1093 int charpos;
1094 unsigned char *s;
1095 int multibyte_p;
1097 struct text_pos pos;
1099 xassert (s != NULL);
1100 xassert (charpos >= 0);
1102 if (multibyte_p)
1104 int rest = strlen (s), len;
1106 SET_TEXT_POS (pos, 0, 0);
1107 while (charpos--)
1109 string_char_and_length (s, rest, &len);
1110 s += len, rest -= len;
1111 xassert (rest >= 0);
1112 CHARPOS (pos) += 1;
1113 BYTEPOS (pos) += len;
1116 else
1117 SET_TEXT_POS (pos, charpos, charpos);
1119 return pos;
1123 /* Value is the number of characters in C string S. MULTIBYTE_P
1124 non-zero means recognize multibyte characters. */
1126 static int
1127 number_of_chars (s, multibyte_p)
1128 unsigned char *s;
1129 int multibyte_p;
1131 int nchars;
1133 if (multibyte_p)
1135 int rest = strlen (s), len;
1136 unsigned char *p = (unsigned char *) s;
1138 for (nchars = 0; rest > 0; ++nchars)
1140 string_char_and_length (p, rest, &len);
1141 rest -= len, p += len;
1144 else
1145 nchars = strlen (s);
1147 return nchars;
1151 /* Compute byte position NEWPOS->bytepos corresponding to
1152 NEWPOS->charpos. POS is a known position in string STRING.
1153 NEWPOS->charpos must be >= POS.charpos. */
1155 static void
1156 compute_string_pos (newpos, pos, string)
1157 struct text_pos *newpos, pos;
1158 Lisp_Object string;
1160 xassert (STRINGP (string));
1161 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1163 if (STRING_MULTIBYTE (string))
1164 *newpos = string_pos_nchars_ahead (pos, string,
1165 CHARPOS (*newpos) - CHARPOS (pos));
1166 else
1167 BYTEPOS (*newpos) = CHARPOS (*newpos);
1172 /***********************************************************************
1173 Lisp form evaluation
1174 ***********************************************************************/
1176 /* Error handler for safe_eval and safe_call. */
1178 static Lisp_Object
1179 safe_eval_handler (arg)
1180 Lisp_Object arg;
1182 add_to_log ("Error during redisplay: %s", arg, Qnil);
1183 return Qnil;
1187 /* Evaluate SEXPR and return the result, or nil if something went
1188 wrong. */
1190 Lisp_Object
1191 safe_eval (sexpr)
1192 Lisp_Object sexpr;
1194 int count = specpdl_ptr - specpdl;
1195 struct gcpro gcpro1;
1196 Lisp_Object val;
1198 GCPRO1 (sexpr);
1199 specbind (Qinhibit_redisplay, Qt);
1200 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1201 UNGCPRO;
1202 return unbind_to (count, val);
1206 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1207 Return the result, or nil if something went wrong. */
1209 Lisp_Object
1210 safe_call (nargs, args)
1211 int nargs;
1212 Lisp_Object *args;
1214 int count = specpdl_ptr - specpdl;
1215 Lisp_Object val;
1216 struct gcpro gcpro1;
1218 GCPRO1 (args[0]);
1219 gcpro1.nvars = nargs;
1220 specbind (Qinhibit_redisplay, Qt);
1221 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1222 safe_eval_handler);
1223 UNGCPRO;
1224 return unbind_to (count, val);
1228 /* Call function FN with one argument ARG.
1229 Return the result, or nil if something went wrong. */
1231 Lisp_Object
1232 safe_call1 (fn, arg)
1233 Lisp_Object fn, arg;
1235 Lisp_Object args[2];
1236 args[0] = fn;
1237 args[1] = arg;
1238 return safe_call (2, args);
1243 /***********************************************************************
1244 Debugging
1245 ***********************************************************************/
1247 #if 0
1249 /* Define CHECK_IT to perform sanity checks on iterators.
1250 This is for debugging. It is too slow to do unconditionally. */
1252 static void
1253 check_it (it)
1254 struct it *it;
1256 if (it->method == next_element_from_string)
1258 xassert (STRINGP (it->string));
1259 xassert (IT_STRING_CHARPOS (*it) >= 0);
1261 else if (it->method == next_element_from_buffer)
1263 /* Check that character and byte positions agree. */
1264 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1267 if (it->dpvec)
1268 xassert (it->current.dpvec_index >= 0);
1269 else
1270 xassert (it->current.dpvec_index < 0);
1273 #define CHECK_IT(IT) check_it ((IT))
1275 #else /* not 0 */
1277 #define CHECK_IT(IT) (void) 0
1279 #endif /* not 0 */
1282 #if GLYPH_DEBUG
1284 /* Check that the window end of window W is what we expect it
1285 to be---the last row in the current matrix displaying text. */
1287 static void
1288 check_window_end (w)
1289 struct window *w;
1291 if (!MINI_WINDOW_P (w)
1292 && !NILP (w->window_end_valid))
1294 struct glyph_row *row;
1295 xassert ((row = MATRIX_ROW (w->current_matrix,
1296 XFASTINT (w->window_end_vpos)),
1297 !row->enabled_p
1298 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1299 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1303 #define CHECK_WINDOW_END(W) check_window_end ((W))
1305 #else /* not GLYPH_DEBUG */
1307 #define CHECK_WINDOW_END(W) (void) 0
1309 #endif /* not GLYPH_DEBUG */
1313 /***********************************************************************
1314 Iterator initialization
1315 ***********************************************************************/
1317 /* Initialize IT for displaying current_buffer in window W, starting
1318 at character position CHARPOS. CHARPOS < 0 means that no buffer
1319 position is specified which is useful when the iterator is assigned
1320 a position later. BYTEPOS is the byte position corresponding to
1321 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1323 If ROW is not null, calls to produce_glyphs with IT as parameter
1324 will produce glyphs in that row.
1326 BASE_FACE_ID is the id of a base face to use. It must be one of
1327 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1328 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1329 displaying the tool-bar.
1331 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1332 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1333 corresponding mode line glyph row of the desired matrix of W. */
1335 void
1336 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1337 struct it *it;
1338 struct window *w;
1339 int charpos, bytepos;
1340 struct glyph_row *row;
1341 enum face_id base_face_id;
1343 int highlight_region_p;
1345 /* Some precondition checks. */
1346 xassert (w != NULL && it != NULL);
1347 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1349 /* If face attributes have been changed since the last redisplay,
1350 free realized faces now because they depend on face definitions
1351 that might have changed. */
1352 if (face_change_count)
1354 face_change_count = 0;
1355 free_all_realized_faces (Qnil);
1358 /* Use one of the mode line rows of W's desired matrix if
1359 appropriate. */
1360 if (row == NULL)
1362 if (base_face_id == MODE_LINE_FACE_ID)
1363 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1364 else if (base_face_id == HEADER_LINE_FACE_ID)
1365 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1368 /* Clear IT. */
1369 bzero (it, sizeof *it);
1370 it->current.overlay_string_index = -1;
1371 it->current.dpvec_index = -1;
1372 it->base_face_id = base_face_id;
1374 /* The window in which we iterate over current_buffer: */
1375 XSETWINDOW (it->window, w);
1376 it->w = w;
1377 it->f = XFRAME (w->frame);
1379 /* Extra space between lines (on window systems only). */
1380 if (base_face_id == DEFAULT_FACE_ID
1381 && FRAME_WINDOW_P (it->f))
1383 if (NATNUMP (current_buffer->extra_line_spacing))
1384 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1385 else if (it->f->extra_line_spacing > 0)
1386 it->extra_line_spacing = it->f->extra_line_spacing;
1389 /* If realized faces have been removed, e.g. because of face
1390 attribute changes of named faces, recompute them. */
1391 if (FRAME_FACE_CACHE (it->f)->used == 0)
1392 recompute_basic_faces (it->f);
1394 /* Current value of the `space-width', and 'height' properties. */
1395 it->space_width = Qnil;
1396 it->font_height = Qnil;
1398 /* Are control characters displayed as `^C'? */
1399 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1401 /* -1 means everything between a CR and the following line end
1402 is invisible. >0 means lines indented more than this value are
1403 invisible. */
1404 it->selective = (INTEGERP (current_buffer->selective_display)
1405 ? XFASTINT (current_buffer->selective_display)
1406 : (!NILP (current_buffer->selective_display)
1407 ? -1 : 0));
1408 it->selective_display_ellipsis_p
1409 = !NILP (current_buffer->selective_display_ellipses);
1411 /* Display table to use. */
1412 it->dp = window_display_table (w);
1414 /* Are multibyte characters enabled in current_buffer? */
1415 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1417 /* Non-zero if we should highlight the region. */
1418 highlight_region_p
1419 = (!NILP (Vtransient_mark_mode)
1420 && !NILP (current_buffer->mark_active)
1421 && XMARKER (current_buffer->mark)->buffer != 0);
1423 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1424 start and end of a visible region in window IT->w. Set both to
1425 -1 to indicate no region. */
1426 if (highlight_region_p
1427 /* Maybe highlight only in selected window. */
1428 && (/* Either show region everywhere. */
1429 highlight_nonselected_windows
1430 /* Or show region in the selected window. */
1431 || w == XWINDOW (selected_window)
1432 /* Or show the region if we are in the mini-buffer and W is
1433 the window the mini-buffer refers to. */
1434 || (MINI_WINDOW_P (XWINDOW (selected_window))
1435 && w == XWINDOW (Vminibuf_scroll_window))))
1437 int charpos = marker_position (current_buffer->mark);
1438 it->region_beg_charpos = min (PT, charpos);
1439 it->region_end_charpos = max (PT, charpos);
1441 else
1442 it->region_beg_charpos = it->region_end_charpos = -1;
1444 /* Get the position at which the redisplay_end_trigger hook should
1445 be run, if it is to be run at all. */
1446 if (MARKERP (w->redisplay_end_trigger)
1447 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1448 it->redisplay_end_trigger_charpos
1449 = marker_position (w->redisplay_end_trigger);
1450 else if (INTEGERP (w->redisplay_end_trigger))
1451 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1453 /* Correct bogus values of tab_width. */
1454 it->tab_width = XINT (current_buffer->tab_width);
1455 if (it->tab_width <= 0 || it->tab_width > 1000)
1456 it->tab_width = 8;
1458 /* Are lines in the display truncated? */
1459 it->truncate_lines_p
1460 = (base_face_id != DEFAULT_FACE_ID
1461 || XINT (it->w->hscroll)
1462 || (truncate_partial_width_windows
1463 && !WINDOW_FULL_WIDTH_P (it->w))
1464 || !NILP (current_buffer->truncate_lines));
1466 /* Get dimensions of truncation and continuation glyphs. These are
1467 displayed as bitmaps under X, so we don't need them for such
1468 frames. */
1469 if (!FRAME_WINDOW_P (it->f))
1471 if (it->truncate_lines_p)
1473 /* We will need the truncation glyph. */
1474 xassert (it->glyph_row == NULL);
1475 produce_special_glyphs (it, IT_TRUNCATION);
1476 it->truncation_pixel_width = it->pixel_width;
1478 else
1480 /* We will need the continuation glyph. */
1481 xassert (it->glyph_row == NULL);
1482 produce_special_glyphs (it, IT_CONTINUATION);
1483 it->continuation_pixel_width = it->pixel_width;
1486 /* Reset these values to zero becaue the produce_special_glyphs
1487 above has changed them. */
1488 it->pixel_width = it->ascent = it->descent = 0;
1489 it->phys_ascent = it->phys_descent = 0;
1492 /* Set this after getting the dimensions of truncation and
1493 continuation glyphs, so that we don't produce glyphs when calling
1494 produce_special_glyphs, above. */
1495 it->glyph_row = row;
1496 it->area = TEXT_AREA;
1498 /* Get the dimensions of the display area. The display area
1499 consists of the visible window area plus a horizontally scrolled
1500 part to the left of the window. All x-values are relative to the
1501 start of this total display area. */
1502 if (base_face_id != DEFAULT_FACE_ID)
1504 /* Mode lines, menu bar in terminal frames. */
1505 it->first_visible_x = 0;
1506 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1508 else
1510 it->first_visible_x
1511 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1512 it->last_visible_x = (it->first_visible_x
1513 + window_box_width (w, TEXT_AREA));
1515 /* If we truncate lines, leave room for the truncator glyph(s) at
1516 the right margin. Otherwise, leave room for the continuation
1517 glyph(s). Truncation and continuation glyphs are not inserted
1518 for window-based redisplay. */
1519 if (!FRAME_WINDOW_P (it->f))
1521 if (it->truncate_lines_p)
1522 it->last_visible_x -= it->truncation_pixel_width;
1523 else
1524 it->last_visible_x -= it->continuation_pixel_width;
1527 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1528 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1531 /* Leave room for a border glyph. */
1532 if (!FRAME_WINDOW_P (it->f)
1533 && !WINDOW_RIGHTMOST_P (it->w))
1534 it->last_visible_x -= 1;
1536 it->last_visible_y = window_text_bottom_y (w);
1538 /* For mode lines and alike, arrange for the first glyph having a
1539 left box line if the face specifies a box. */
1540 if (base_face_id != DEFAULT_FACE_ID)
1542 struct face *face;
1544 it->face_id = base_face_id;
1546 /* If we have a boxed mode line, make the first character appear
1547 with a left box line. */
1548 face = FACE_FROM_ID (it->f, base_face_id);
1549 if (face->box != FACE_NO_BOX)
1550 it->start_of_box_run_p = 1;
1553 /* If a buffer position was specified, set the iterator there,
1554 getting overlays and face properties from that position. */
1555 if (charpos > 0)
1557 it->end_charpos = ZV;
1558 it->face_id = -1;
1559 IT_CHARPOS (*it) = charpos;
1561 /* Compute byte position if not specified. */
1562 if (bytepos <= 0)
1563 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1564 else
1565 IT_BYTEPOS (*it) = bytepos;
1567 /* Compute faces etc. */
1568 reseat (it, it->current.pos, 1);
1571 CHECK_IT (it);
1575 /* Initialize IT for the display of window W with window start POS. */
1577 void
1578 start_display (it, w, pos)
1579 struct it *it;
1580 struct window *w;
1581 struct text_pos pos;
1583 int start_at_line_beg_p;
1584 struct glyph_row *row;
1585 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1586 int first_y;
1588 row = w->desired_matrix->rows + first_vpos;
1589 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1590 first_y = it->current_y;
1592 /* If window start is not at a line start, move back to the line
1593 start. This makes sure that we take continuation lines into
1594 account. */
1595 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1596 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1597 if (!start_at_line_beg_p)
1598 reseat_at_previous_visible_line_start (it);
1600 /* If window start is not at a line start, skip forward to POS to
1601 get the correct continuation_lines_width and current_x. */
1602 if (!start_at_line_beg_p)
1604 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1606 /* If lines are continued, this line may end in the middle of a
1607 multi-glyph character (e.g. a control character displayed as
1608 \003, or in the middle of an overlay string). In this case
1609 move_it_to above will not have taken us to the start of
1610 the continuation line but to the end of the continued line. */
1611 if (!it->truncate_lines_p)
1613 if (it->current_x > 0)
1615 if (it->current.dpvec_index >= 0
1616 || it->current.overlay_string_index >= 0)
1618 set_iterator_to_next (it, 1);
1619 move_it_in_display_line_to (it, -1, -1, 0);
1622 it->continuation_lines_width += it->current_x;
1625 /* We're starting a new display line, not affected by the
1626 height of the continued line, so clear the appropriate
1627 fields in the iterator structure. */
1628 it->max_ascent = it->max_descent = 0;
1629 it->max_phys_ascent = it->max_phys_descent = 0;
1632 it->current_y = first_y;
1633 it->vpos = 0;
1634 it->current_x = it->hpos = 0;
1637 #if 0 /* Don't assert the following because start_display is sometimes
1638 called intentionally with a window start that is not at a
1639 line start. Please leave this code in as a comment. */
1641 /* Window start should be on a line start, now. */
1642 xassert (it->continuation_lines_width
1643 || IT_CHARPOS (it) == BEGV
1644 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1645 #endif /* 0 */
1649 /* Initialize IT for stepping through current_buffer in window W,
1650 starting at position POS that includes overlay string and display
1651 vector/ control character translation position information. */
1653 static void
1654 init_from_display_pos (it, w, pos)
1655 struct it *it;
1656 struct window *w;
1657 struct display_pos *pos;
1659 /* Keep in mind: the call to reseat in init_iterator skips invisible
1660 text, so we might end up at a position different from POS. This
1661 is only a problem when POS is a row start after a newline and an
1662 overlay starts there with an after-string, and the overlay has an
1663 invisible property. Since we don't skip invisible text in
1664 display_line and elsewhere immediately after consuming the
1665 newline before the row start, such a POS will not be in a string,
1666 but the call to init_iterator below will move us to the
1667 after-string. */
1668 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1669 NULL, DEFAULT_FACE_ID);
1671 /* If position is within an overlay string, set up IT to
1672 the right overlay string. */
1673 if (pos->overlay_string_index >= 0)
1675 int relative_index;
1677 /* We already have the first chunk of overlay strings in
1678 IT->overlay_strings. Load more until the one for
1679 pos->overlay_string_index is in IT->overlay_strings. */
1680 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1682 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1683 it->current.overlay_string_index = 0;
1684 while (n--)
1686 load_overlay_strings (it);
1687 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1691 it->current.overlay_string_index = pos->overlay_string_index;
1692 relative_index = (it->current.overlay_string_index
1693 % OVERLAY_STRING_CHUNK_SIZE);
1694 it->string = it->overlay_strings[relative_index];
1695 xassert (STRINGP (it->string));
1696 it->current.string_pos = pos->string_pos;
1697 it->method = next_element_from_string;
1699 else if (CHARPOS (pos->string_pos) >= 0)
1701 /* Recorded position is not in an overlay string, but in another
1702 string. This can only be a string from a `display' property.
1703 IT should already be filled with that string. */
1704 it->current.string_pos = pos->string_pos;
1705 xassert (STRINGP (it->string));
1708 /* Restore position in display vector translations or control
1709 character translations. */
1710 if (pos->dpvec_index >= 0)
1712 /* This fills IT->dpvec. */
1713 get_next_display_element (it);
1714 xassert (it->dpvec && it->current.dpvec_index == 0);
1715 it->current.dpvec_index = pos->dpvec_index;
1718 CHECK_IT (it);
1722 /* Initialize IT for stepping through current_buffer in window W
1723 starting at ROW->start. */
1725 static void
1726 init_to_row_start (it, w, row)
1727 struct it *it;
1728 struct window *w;
1729 struct glyph_row *row;
1731 init_from_display_pos (it, w, &row->start);
1732 it->continuation_lines_width = row->continuation_lines_width;
1733 CHECK_IT (it);
1737 /* Initialize IT for stepping through current_buffer in window W
1738 starting in the line following ROW, i.e. starting at ROW->end. */
1740 static void
1741 init_to_row_end (it, w, row)
1742 struct it *it;
1743 struct window *w;
1744 struct glyph_row *row;
1746 init_from_display_pos (it, w, &row->end);
1748 if (row->continued_p)
1749 it->continuation_lines_width = (row->continuation_lines_width
1750 + row->pixel_width);
1751 CHECK_IT (it);
1757 /***********************************************************************
1758 Text properties
1759 ***********************************************************************/
1761 /* Called when IT reaches IT->stop_charpos. Handle text property and
1762 overlay changes. Set IT->stop_charpos to the next position where
1763 to stop. */
1765 static void
1766 handle_stop (it)
1767 struct it *it;
1769 enum prop_handled handled;
1770 int handle_overlay_change_p = 1;
1771 struct props *p;
1773 it->dpvec = NULL;
1774 it->current.dpvec_index = -1;
1778 handled = HANDLED_NORMALLY;
1780 /* Call text property handlers. */
1781 for (p = it_props; p->handler; ++p)
1783 handled = p->handler (it);
1785 if (handled == HANDLED_RECOMPUTE_PROPS)
1786 break;
1787 else if (handled == HANDLED_RETURN)
1788 return;
1789 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1790 handle_overlay_change_p = 0;
1793 if (handled != HANDLED_RECOMPUTE_PROPS)
1795 /* Don't check for overlay strings below when set to deliver
1796 characters from a display vector. */
1797 if (it->method == next_element_from_display_vector)
1798 handle_overlay_change_p = 0;
1800 /* Handle overlay changes. */
1801 if (handle_overlay_change_p)
1802 handled = handle_overlay_change (it);
1804 /* Determine where to stop next. */
1805 if (handled == HANDLED_NORMALLY)
1806 compute_stop_pos (it);
1809 while (handled == HANDLED_RECOMPUTE_PROPS);
1813 /* Compute IT->stop_charpos from text property and overlay change
1814 information for IT's current position. */
1816 static void
1817 compute_stop_pos (it)
1818 struct it *it;
1820 register INTERVAL iv, next_iv;
1821 Lisp_Object object, limit, position;
1823 /* If nowhere else, stop at the end. */
1824 it->stop_charpos = it->end_charpos;
1826 if (STRINGP (it->string))
1828 /* Strings are usually short, so don't limit the search for
1829 properties. */
1830 object = it->string;
1831 limit = Qnil;
1832 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1834 else
1836 int charpos;
1838 /* If next overlay change is in front of the current stop pos
1839 (which is IT->end_charpos), stop there. Note: value of
1840 next_overlay_change is point-max if no overlay change
1841 follows. */
1842 charpos = next_overlay_change (IT_CHARPOS (*it));
1843 if (charpos < it->stop_charpos)
1844 it->stop_charpos = charpos;
1846 /* If showing the region, we have to stop at the region
1847 start or end because the face might change there. */
1848 if (it->region_beg_charpos > 0)
1850 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1851 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1852 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1853 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1856 /* Set up variables for computing the stop position from text
1857 property changes. */
1858 XSETBUFFER (object, current_buffer);
1859 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1860 XSETFASTINT (position, IT_CHARPOS (*it));
1864 /* Get the interval containing IT's position. Value is a null
1865 interval if there isn't such an interval. */
1866 iv = validate_interval_range (object, &position, &position, 0);
1867 if (!NULL_INTERVAL_P (iv))
1869 Lisp_Object values_here[LAST_PROP_IDX];
1870 struct props *p;
1872 /* Get properties here. */
1873 for (p = it_props; p->handler; ++p)
1874 values_here[p->idx] = textget (iv->plist, *p->name);
1876 /* Look for an interval following iv that has different
1877 properties. */
1878 for (next_iv = next_interval (iv);
1879 (!NULL_INTERVAL_P (next_iv)
1880 && (NILP (limit)
1881 || XFASTINT (limit) > next_iv->position));
1882 next_iv = next_interval (next_iv))
1884 for (p = it_props; p->handler; ++p)
1886 Lisp_Object new_value;
1888 new_value = textget (next_iv->plist, *p->name);
1889 if (!EQ (values_here[p->idx], new_value))
1890 break;
1893 if (p->handler)
1894 break;
1897 if (!NULL_INTERVAL_P (next_iv))
1899 if (INTEGERP (limit)
1900 && next_iv->position >= XFASTINT (limit))
1901 /* No text property change up to limit. */
1902 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1903 else
1904 /* Text properties change in next_iv. */
1905 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1909 xassert (STRINGP (it->string)
1910 || (it->stop_charpos >= BEGV
1911 && it->stop_charpos >= IT_CHARPOS (*it)));
1915 /* Return the position of the next overlay change after POS in
1916 current_buffer. Value is point-max if no overlay change
1917 follows. This is like `next-overlay-change' but doesn't use
1918 xmalloc. */
1920 static int
1921 next_overlay_change (pos)
1922 int pos;
1924 int noverlays;
1925 int endpos;
1926 Lisp_Object *overlays;
1927 int len;
1928 int i;
1930 /* Get all overlays at the given position. */
1931 len = 10;
1932 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1933 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1934 if (noverlays > len)
1936 len = noverlays;
1937 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1938 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1941 /* If any of these overlays ends before endpos,
1942 use its ending point instead. */
1943 for (i = 0; i < noverlays; ++i)
1945 Lisp_Object oend;
1946 int oendpos;
1948 oend = OVERLAY_END (overlays[i]);
1949 oendpos = OVERLAY_POSITION (oend);
1950 endpos = min (endpos, oendpos);
1953 return endpos;
1958 /***********************************************************************
1959 Fontification
1960 ***********************************************************************/
1962 /* Handle changes in the `fontified' property of the current buffer by
1963 calling hook functions from Qfontification_functions to fontify
1964 regions of text. */
1966 static enum prop_handled
1967 handle_fontified_prop (it)
1968 struct it *it;
1970 Lisp_Object prop, pos;
1971 enum prop_handled handled = HANDLED_NORMALLY;
1973 /* Get the value of the `fontified' property at IT's current buffer
1974 position. (The `fontified' property doesn't have a special
1975 meaning in strings.) If the value is nil, call functions from
1976 Qfontification_functions. */
1977 if (!STRINGP (it->string)
1978 && it->s == NULL
1979 && !NILP (Vfontification_functions)
1980 && !NILP (Vrun_hooks)
1981 && (pos = make_number (IT_CHARPOS (*it)),
1982 prop = Fget_char_property (pos, Qfontified, Qnil),
1983 NILP (prop)))
1985 int count = specpdl_ptr - specpdl;
1986 Lisp_Object val;
1988 val = Vfontification_functions;
1989 specbind (Qfontification_functions, Qnil);
1990 specbind (Qafter_change_functions, Qnil);
1992 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
1993 safe_call1 (val, pos);
1994 else
1996 Lisp_Object globals, fn;
1997 struct gcpro gcpro1, gcpro2;
1999 globals = Qnil;
2000 GCPRO2 (val, globals);
2002 for (; CONSP (val); val = XCDR (val))
2004 fn = XCAR (val);
2006 if (EQ (fn, Qt))
2008 /* A value of t indicates this hook has a local
2009 binding; it means to run the global binding too.
2010 In a global value, t should not occur. If it
2011 does, we must ignore it to avoid an endless
2012 loop. */
2013 for (globals = Fdefault_value (Qfontification_functions);
2014 CONSP (globals);
2015 globals = XCDR (globals))
2017 fn = XCAR (globals);
2018 if (!EQ (fn, Qt))
2019 safe_call1 (fn, pos);
2022 else
2023 safe_call1 (fn, pos);
2026 UNGCPRO;
2029 unbind_to (count, Qnil);
2031 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2032 something. This avoids an endless loop if they failed to
2033 fontify the text for which reason ever. */
2034 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2035 handled = HANDLED_RECOMPUTE_PROPS;
2038 return handled;
2043 /***********************************************************************
2044 Faces
2045 ***********************************************************************/
2047 /* Set up iterator IT from face properties at its current position.
2048 Called from handle_stop. */
2050 static enum prop_handled
2051 handle_face_prop (it)
2052 struct it *it;
2054 int new_face_id, next_stop;
2056 if (!STRINGP (it->string))
2058 new_face_id
2059 = face_at_buffer_position (it->w,
2060 IT_CHARPOS (*it),
2061 it->region_beg_charpos,
2062 it->region_end_charpos,
2063 &next_stop,
2064 (IT_CHARPOS (*it)
2065 + TEXT_PROP_DISTANCE_LIMIT),
2068 /* Is this a start of a run of characters with box face?
2069 Caveat: this can be called for a freshly initialized
2070 iterator; face_id is -1 is this case. We know that the new
2071 face will not change until limit, i.e. if the new face has a
2072 box, all characters up to limit will have one. But, as
2073 usual, we don't know whether limit is really the end. */
2074 if (new_face_id != it->face_id)
2076 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2078 /* If new face has a box but old face has not, this is
2079 the start of a run of characters with box, i.e. it has
2080 a shadow on the left side. The value of face_id of the
2081 iterator will be -1 if this is the initial call that gets
2082 the face. In this case, we have to look in front of IT's
2083 position and see whether there is a face != new_face_id. */
2084 it->start_of_box_run_p
2085 = (new_face->box != FACE_NO_BOX
2086 && (it->face_id >= 0
2087 || IT_CHARPOS (*it) == BEG
2088 || new_face_id != face_before_it_pos (it)));
2089 it->face_box_p = new_face->box != FACE_NO_BOX;
2092 else
2094 new_face_id
2095 = face_at_string_position (it->w,
2096 it->string,
2097 IT_STRING_CHARPOS (*it),
2098 (it->current.overlay_string_index >= 0
2099 ? IT_CHARPOS (*it)
2100 : 0),
2101 it->region_beg_charpos,
2102 it->region_end_charpos,
2103 &next_stop,
2104 it->base_face_id);
2106 #if 0 /* This shouldn't be neccessary. Let's check it. */
2107 /* If IT is used to display a mode line we would really like to
2108 use the mode line face instead of the frame's default face. */
2109 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2110 && new_face_id == DEFAULT_FACE_ID)
2111 new_face_id = MODE_LINE_FACE_ID;
2112 #endif
2114 /* Is this a start of a run of characters with box? Caveat:
2115 this can be called for a freshly allocated iterator; face_id
2116 is -1 is this case. We know that the new face will not
2117 change until the next check pos, i.e. if the new face has a
2118 box, all characters up to that position will have a
2119 box. But, as usual, we don't know whether that position
2120 is really the end. */
2121 if (new_face_id != it->face_id)
2123 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2124 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2126 /* If new face has a box but old face hasn't, this is the
2127 start of a run of characters with box, i.e. it has a
2128 shadow on the left side. */
2129 it->start_of_box_run_p
2130 = new_face->box && (old_face == NULL || !old_face->box);
2131 it->face_box_p = new_face->box != FACE_NO_BOX;
2135 it->face_id = new_face_id;
2136 return HANDLED_NORMALLY;
2140 /* Compute the face one character before or after the current position
2141 of IT. BEFORE_P non-zero means get the face in front of IT's
2142 position. Value is the id of the face. */
2144 static int
2145 face_before_or_after_it_pos (it, before_p)
2146 struct it *it;
2147 int before_p;
2149 int face_id, limit;
2150 int next_check_charpos;
2151 struct text_pos pos;
2153 xassert (it->s == NULL);
2155 if (STRINGP (it->string))
2157 /* No face change past the end of the string (for the case
2158 we are padding with spaces). No face change before the
2159 string start. */
2160 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2161 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2162 return it->face_id;
2164 /* Set pos to the position before or after IT's current position. */
2165 if (before_p)
2166 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2167 else
2168 /* For composition, we must check the character after the
2169 composition. */
2170 pos = (it->what == IT_COMPOSITION
2171 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2172 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2174 /* Get the face for ASCII, or unibyte. */
2175 face_id
2176 = face_at_string_position (it->w,
2177 it->string,
2178 CHARPOS (pos),
2179 (it->current.overlay_string_index >= 0
2180 ? IT_CHARPOS (*it)
2181 : 0),
2182 it->region_beg_charpos,
2183 it->region_end_charpos,
2184 &next_check_charpos,
2185 it->base_face_id);
2187 /* Correct the face for charsets different from ASCII. Do it
2188 for the multibyte case only. The face returned above is
2189 suitable for unibyte text if IT->string is unibyte. */
2190 if (STRING_MULTIBYTE (it->string))
2192 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2193 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2194 int c, len;
2195 struct face *face = FACE_FROM_ID (it->f, face_id);
2197 c = string_char_and_length (p, rest, &len);
2198 face_id = FACE_FOR_CHAR (it->f, face, c);
2201 else
2203 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2204 || (IT_CHARPOS (*it) <= BEGV && before_p))
2205 return it->face_id;
2207 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2208 pos = it->current.pos;
2210 if (before_p)
2211 DEC_TEXT_POS (pos, it->multibyte_p);
2212 else
2214 if (it->what == IT_COMPOSITION)
2215 /* For composition, we must check the position after the
2216 composition. */
2217 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2218 else
2219 INC_TEXT_POS (pos, it->multibyte_p);
2221 /* Determine face for CHARSET_ASCII, or unibyte. */
2222 face_id = face_at_buffer_position (it->w,
2223 CHARPOS (pos),
2224 it->region_beg_charpos,
2225 it->region_end_charpos,
2226 &next_check_charpos,
2227 limit, 0);
2229 /* Correct the face for charsets different from ASCII. Do it
2230 for the multibyte case only. The face returned above is
2231 suitable for unibyte text if current_buffer is unibyte. */
2232 if (it->multibyte_p)
2234 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2235 struct face *face = FACE_FROM_ID (it->f, face_id);
2236 face_id = FACE_FOR_CHAR (it->f, face, c);
2240 return face_id;
2245 /***********************************************************************
2246 Invisible text
2247 ***********************************************************************/
2249 /* Set up iterator IT from invisible properties at its current
2250 position. Called from handle_stop. */
2252 static enum prop_handled
2253 handle_invisible_prop (it)
2254 struct it *it;
2256 enum prop_handled handled = HANDLED_NORMALLY;
2258 if (STRINGP (it->string))
2260 extern Lisp_Object Qinvisible;
2261 Lisp_Object prop, end_charpos, limit, charpos;
2263 /* Get the value of the invisible text property at the
2264 current position. Value will be nil if there is no such
2265 property. */
2266 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2267 prop = Fget_text_property (charpos, Qinvisible, it->string);
2269 if (!NILP (prop)
2270 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2272 handled = HANDLED_RECOMPUTE_PROPS;
2274 /* Get the position at which the next change of the
2275 invisible text property can be found in IT->string.
2276 Value will be nil if the property value is the same for
2277 all the rest of IT->string. */
2278 XSETINT (limit, XSTRING (it->string)->size);
2279 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2280 it->string, limit);
2282 /* Text at current position is invisible. The next
2283 change in the property is at position end_charpos.
2284 Move IT's current position to that position. */
2285 if (INTEGERP (end_charpos)
2286 && XFASTINT (end_charpos) < XFASTINT (limit))
2288 struct text_pos old;
2289 old = it->current.string_pos;
2290 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2291 compute_string_pos (&it->current.string_pos, old, it->string);
2293 else
2295 /* The rest of the string is invisible. If this is an
2296 overlay string, proceed with the next overlay string
2297 or whatever comes and return a character from there. */
2298 if (it->current.overlay_string_index >= 0)
2300 next_overlay_string (it);
2301 /* Don't check for overlay strings when we just
2302 finished processing them. */
2303 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2305 else
2307 struct Lisp_String *s = XSTRING (it->string);
2308 IT_STRING_CHARPOS (*it) = s->size;
2309 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2314 else
2316 int visible_p, newpos, next_stop;
2317 Lisp_Object pos, prop;
2319 /* First of all, is there invisible text at this position? */
2320 XSETFASTINT (pos, IT_CHARPOS (*it));
2321 prop = Fget_char_property (pos, Qinvisible, it->window);
2323 /* If we are on invisible text, skip over it. */
2324 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2325 && IT_CHARPOS (*it) < it->end_charpos)
2327 /* Record whether we have to display an ellipsis for the
2328 invisible text. */
2329 int display_ellipsis_p
2330 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2332 handled = HANDLED_RECOMPUTE_PROPS;
2334 /* Loop skipping over invisible text. The loop is left at
2335 ZV or with IT on the first char being visible again. */
2338 /* Try to skip some invisible text. Return value is the
2339 position reached which can be equal to IT's position
2340 if there is nothing invisible here. This skips both
2341 over invisible text properties and overlays with
2342 invisible property. */
2343 newpos = skip_invisible (IT_CHARPOS (*it),
2344 &next_stop, ZV, it->window);
2346 /* If we skipped nothing at all we weren't at invisible
2347 text in the first place. If everything to the end of
2348 the buffer was skipped, end the loop. */
2349 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2350 visible_p = 1;
2351 else
2353 /* We skipped some characters but not necessarily
2354 all there are. Check if we ended up on visible
2355 text. Fget_char_property returns the property of
2356 the char before the given position, i.e. if we
2357 get visible_p = 1, this means that the char at
2358 newpos is visible. */
2359 XSETFASTINT (pos, newpos);
2360 prop = Fget_char_property (pos, Qinvisible, it->window);
2361 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2364 /* If we ended up on invisible text, proceed to
2365 skip starting with next_stop. */
2366 if (!visible_p)
2367 IT_CHARPOS (*it) = next_stop;
2369 while (!visible_p);
2371 /* The position newpos is now either ZV or on visible text. */
2372 IT_CHARPOS (*it) = newpos;
2373 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2375 /* Maybe return `...' next for the end of the invisible text. */
2376 if (display_ellipsis_p)
2378 if (it->dp
2379 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2381 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2382 it->dpvec = v->contents;
2383 it->dpend = v->contents + v->size;
2385 else
2387 /* Default `...'. */
2388 it->dpvec = default_invis_vector;
2389 it->dpend = default_invis_vector + 3;
2392 /* The ellipsis display does not replace the display of
2393 the character at the new position. Indicate this by
2394 setting IT->dpvec_char_len to zero. */
2395 it->dpvec_char_len = 0;
2397 it->current.dpvec_index = 0;
2398 it->method = next_element_from_display_vector;
2403 return handled;
2408 /***********************************************************************
2409 'display' property
2410 ***********************************************************************/
2412 /* Set up iterator IT from `display' property at its current position.
2413 Called from handle_stop. */
2415 static enum prop_handled
2416 handle_display_prop (it)
2417 struct it *it;
2419 Lisp_Object prop, object;
2420 struct text_pos *position;
2421 int space_or_image_found_p;
2423 if (STRINGP (it->string))
2425 object = it->string;
2426 position = &it->current.string_pos;
2428 else
2430 object = Qnil;
2431 position = &it->current.pos;
2434 /* Reset those iterator values set from display property values. */
2435 it->font_height = Qnil;
2436 it->space_width = Qnil;
2437 it->voffset = 0;
2439 /* We don't support recursive `display' properties, i.e. string
2440 values that have a string `display' property, that have a string
2441 `display' property etc. */
2442 if (!it->string_from_display_prop_p)
2443 it->area = TEXT_AREA;
2445 prop = Fget_char_property (make_number (position->charpos),
2446 Qdisplay, object);
2447 if (NILP (prop))
2448 return HANDLED_NORMALLY;
2450 space_or_image_found_p = 0;
2451 if (CONSP (prop)
2452 && CONSP (XCAR (prop))
2453 && !EQ (Qmargin, XCAR (XCAR (prop))))
2455 /* A list of sub-properties. */
2456 while (CONSP (prop))
2458 if (handle_single_display_prop (it, XCAR (prop), object, position))
2459 space_or_image_found_p = 1;
2460 prop = XCDR (prop);
2463 else if (VECTORP (prop))
2465 int i;
2466 for (i = 0; i < XVECTOR (prop)->size; ++i)
2467 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2468 object, position))
2469 space_or_image_found_p = 1;
2471 else
2473 if (handle_single_display_prop (it, prop, object, position))
2474 space_or_image_found_p = 1;
2477 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2481 /* Value is the position of the end of the `display' property starting
2482 at START_POS in OBJECT. */
2484 static struct text_pos
2485 display_prop_end (it, object, start_pos)
2486 struct it *it;
2487 Lisp_Object object;
2488 struct text_pos start_pos;
2490 Lisp_Object end;
2491 struct text_pos end_pos;
2493 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2494 Qdisplay, object, Qnil);
2495 CHARPOS (end_pos) = XFASTINT (end);
2496 if (STRINGP (object))
2497 compute_string_pos (&end_pos, start_pos, it->string);
2498 else
2499 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2501 return end_pos;
2505 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2506 is the object in which the `display' property was found. *POSITION
2507 is the position at which it was found.
2509 If PROP is a `space' or `image' sub-property, set *POSITION to the
2510 end position of the `display' property.
2512 Value is non-zero if a `space' or `image' property value was found. */
2514 static int
2515 handle_single_display_prop (it, prop, object, position)
2516 struct it *it;
2517 Lisp_Object prop;
2518 Lisp_Object object;
2519 struct text_pos *position;
2521 Lisp_Object value;
2522 int space_or_image_found_p = 0;
2523 Lisp_Object form;
2525 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2526 evaluated. If the result is nil, VALUE is ignored. */
2527 form = Qt;
2528 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2530 prop = XCDR (prop);
2531 if (!CONSP (prop))
2532 return 0;
2533 form = XCAR (prop);
2534 prop = XCDR (prop);
2537 if (!NILP (form) && !EQ (form, Qt))
2539 struct gcpro gcpro1;
2540 struct text_pos end_pos, pt;
2542 GCPRO1 (form);
2543 end_pos = display_prop_end (it, object, *position);
2545 /* Temporarily set point to the end position, and then evaluate
2546 the form. This makes `(eolp)' work as FORM. */
2547 if (BUFFERP (object))
2549 CHARPOS (pt) = PT;
2550 BYTEPOS (pt) = PT_BYTE;
2551 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2554 form = safe_eval (form);
2556 if (BUFFERP (object))
2557 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2558 UNGCPRO;
2561 if (NILP (form))
2562 return 0;
2564 if (CONSP (prop)
2565 && EQ (XCAR (prop), Qheight)
2566 && CONSP (XCDR (prop)))
2568 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2569 return 0;
2571 /* `(height HEIGHT)'. */
2572 it->font_height = XCAR (XCDR (prop));
2573 if (!NILP (it->font_height))
2575 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2576 int new_height = -1;
2578 if (CONSP (it->font_height)
2579 && (EQ (XCAR (it->font_height), Qplus)
2580 || EQ (XCAR (it->font_height), Qminus))
2581 && CONSP (XCDR (it->font_height))
2582 && INTEGERP (XCAR (XCDR (it->font_height))))
2584 /* `(+ N)' or `(- N)' where N is an integer. */
2585 int steps = XINT (XCAR (XCDR (it->font_height)));
2586 if (EQ (XCAR (it->font_height), Qplus))
2587 steps = - steps;
2588 it->face_id = smaller_face (it->f, it->face_id, steps);
2590 else if (FUNCTIONP (it->font_height))
2592 /* Call function with current height as argument.
2593 Value is the new height. */
2594 Lisp_Object height;
2595 height = safe_call1 (it->font_height,
2596 face->lface[LFACE_HEIGHT_INDEX]);
2597 if (NUMBERP (height))
2598 new_height = XFLOATINT (height);
2600 else if (NUMBERP (it->font_height))
2602 /* Value is a multiple of the canonical char height. */
2603 struct face *face;
2605 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2606 new_height = (XFLOATINT (it->font_height)
2607 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2609 else
2611 /* Evaluate IT->font_height with `height' bound to the
2612 current specified height to get the new height. */
2613 Lisp_Object value;
2614 int count = specpdl_ptr - specpdl;
2616 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2617 value = safe_eval (it->font_height);
2618 unbind_to (count, Qnil);
2620 if (NUMBERP (value))
2621 new_height = XFLOATINT (value);
2624 if (new_height > 0)
2625 it->face_id = face_with_height (it->f, it->face_id, new_height);
2628 else if (CONSP (prop)
2629 && EQ (XCAR (prop), Qspace_width)
2630 && CONSP (XCDR (prop)))
2632 /* `(space_width WIDTH)'. */
2633 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2634 return 0;
2636 value = XCAR (XCDR (prop));
2637 if (NUMBERP (value) && XFLOATINT (value) > 0)
2638 it->space_width = value;
2640 else if (CONSP (prop)
2641 && EQ (XCAR (prop), Qraise)
2642 && CONSP (XCDR (prop)))
2644 /* `(raise FACTOR)'. */
2645 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2646 return 0;
2648 #ifdef HAVE_WINDOW_SYSTEM
2649 value = XCAR (XCDR (prop));
2650 if (NUMBERP (value))
2652 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2653 it->voffset = - (XFLOATINT (value)
2654 * (FONT_HEIGHT (face->font)));
2656 #endif /* HAVE_WINDOW_SYSTEM */
2658 else if (!it->string_from_display_prop_p)
2660 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2661 VALUE) or `((margin nil) VALUE)' or VALUE. */
2662 Lisp_Object location, value;
2663 struct text_pos start_pos;
2664 int valid_p;
2666 /* Characters having this form of property are not displayed, so
2667 we have to find the end of the property. */
2668 start_pos = *position;
2669 *position = display_prop_end (it, object, start_pos);
2670 value = Qnil;
2672 /* Let's stop at the new position and assume that all
2673 text properties change there. */
2674 it->stop_charpos = position->charpos;
2676 location = Qunbound;
2677 if (CONSP (prop) && CONSP (XCAR (prop)))
2679 Lisp_Object tem;
2681 value = XCDR (prop);
2682 if (CONSP (value))
2683 value = XCAR (value);
2685 tem = XCAR (prop);
2686 if (EQ (XCAR (tem), Qmargin)
2687 && (tem = XCDR (tem),
2688 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2689 (NILP (tem)
2690 || EQ (tem, Qleft_margin)
2691 || EQ (tem, Qright_margin))))
2692 location = tem;
2695 if (EQ (location, Qunbound))
2697 location = Qnil;
2698 value = prop;
2701 #ifdef HAVE_WINDOW_SYSTEM
2702 if (FRAME_TERMCAP_P (it->f))
2703 valid_p = STRINGP (value);
2704 else
2705 valid_p = (STRINGP (value)
2706 || (CONSP (value) && EQ (XCAR (value), Qspace))
2707 || valid_image_p (value));
2708 #else /* not HAVE_WINDOW_SYSTEM */
2709 valid_p = STRINGP (value);
2710 #endif /* not HAVE_WINDOW_SYSTEM */
2712 if ((EQ (location, Qleft_margin)
2713 || EQ (location, Qright_margin)
2714 || NILP (location))
2715 && valid_p)
2717 space_or_image_found_p = 1;
2719 /* Save current settings of IT so that we can restore them
2720 when we are finished with the glyph property value. */
2721 push_it (it);
2723 if (NILP (location))
2724 it->area = TEXT_AREA;
2725 else if (EQ (location, Qleft_margin))
2726 it->area = LEFT_MARGIN_AREA;
2727 else
2728 it->area = RIGHT_MARGIN_AREA;
2730 if (STRINGP (value))
2732 it->string = value;
2733 it->multibyte_p = STRING_MULTIBYTE (it->string);
2734 it->current.overlay_string_index = -1;
2735 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2736 it->end_charpos = it->string_nchars
2737 = XSTRING (it->string)->size;
2738 it->method = next_element_from_string;
2739 it->stop_charpos = 0;
2740 it->string_from_display_prop_p = 1;
2742 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2744 it->method = next_element_from_stretch;
2745 it->object = value;
2746 it->current.pos = it->position = start_pos;
2748 #ifdef HAVE_WINDOW_SYSTEM
2749 else
2751 it->what = IT_IMAGE;
2752 it->image_id = lookup_image (it->f, value);
2753 it->position = start_pos;
2754 it->object = NILP (object) ? it->w->buffer : object;
2755 it->method = next_element_from_image;
2757 /* Say that we haven't consumed the characters with
2758 `display' property yet. The call to pop_it in
2759 set_iterator_to_next will clean this up. */
2760 *position = start_pos;
2762 #endif /* HAVE_WINDOW_SYSTEM */
2764 else
2765 /* Invalid property or property not supported. Restore
2766 the position to what it was before. */
2767 *position = start_pos;
2770 return space_or_image_found_p;
2774 /* Check if PROP is a display sub-property value whose text should be
2775 treated as intangible. */
2777 static int
2778 single_display_prop_intangible_p (prop)
2779 Lisp_Object prop;
2781 /* Skip over `when FORM'. */
2782 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2784 prop = XCDR (prop);
2785 if (!CONSP (prop))
2786 return 0;
2787 prop = XCDR (prop);
2790 if (!CONSP (prop))
2791 return 0;
2793 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2794 we don't need to treat text as intangible. */
2795 if (EQ (XCAR (prop), Qmargin))
2797 prop = XCDR (prop);
2798 if (!CONSP (prop))
2799 return 0;
2801 prop = XCDR (prop);
2802 if (!CONSP (prop)
2803 || EQ (XCAR (prop), Qleft_margin)
2804 || EQ (XCAR (prop), Qright_margin))
2805 return 0;
2808 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2812 /* Check if PROP is a display property value whose text should be
2813 treated as intangible. */
2816 display_prop_intangible_p (prop)
2817 Lisp_Object prop;
2819 if (CONSP (prop)
2820 && CONSP (XCAR (prop))
2821 && !EQ (Qmargin, XCAR (XCAR (prop))))
2823 /* A list of sub-properties. */
2824 while (CONSP (prop))
2826 if (single_display_prop_intangible_p (XCAR (prop)))
2827 return 1;
2828 prop = XCDR (prop);
2831 else if (VECTORP (prop))
2833 /* A vector of sub-properties. */
2834 int i;
2835 for (i = 0; i < XVECTOR (prop)->size; ++i)
2836 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2837 return 1;
2839 else
2840 return single_display_prop_intangible_p (prop);
2842 return 0;
2846 /***********************************************************************
2847 `composition' property
2848 ***********************************************************************/
2850 /* Set up iterator IT from `composition' property at its current
2851 position. Called from handle_stop. */
2853 static enum prop_handled
2854 handle_composition_prop (it)
2855 struct it *it;
2857 Lisp_Object prop, string;
2858 int pos, pos_byte, end;
2859 enum prop_handled handled = HANDLED_NORMALLY;
2861 if (STRINGP (it->string))
2863 pos = IT_STRING_CHARPOS (*it);
2864 pos_byte = IT_STRING_BYTEPOS (*it);
2865 string = it->string;
2867 else
2869 pos = IT_CHARPOS (*it);
2870 pos_byte = IT_BYTEPOS (*it);
2871 string = Qnil;
2874 /* If there's a valid composition and point is not inside of the
2875 composition (in the case that the composition is from the current
2876 buffer), draw a glyph composed from the composition components. */
2877 if (find_composition (pos, -1, &pos, &end, &prop, string)
2878 && COMPOSITION_VALID_P (pos, end, prop)
2879 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2881 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2883 if (id >= 0)
2885 it->method = next_element_from_composition;
2886 it->cmp_id = id;
2887 it->cmp_len = COMPOSITION_LENGTH (prop);
2888 /* For a terminal, draw only the first character of the
2889 components. */
2890 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2891 it->len = (STRINGP (it->string)
2892 ? string_char_to_byte (it->string, end)
2893 : CHAR_TO_BYTE (end)) - pos_byte;
2894 it->stop_charpos = end;
2895 handled = HANDLED_RETURN;
2899 return handled;
2904 /***********************************************************************
2905 Overlay strings
2906 ***********************************************************************/
2908 /* The following structure is used to record overlay strings for
2909 later sorting in load_overlay_strings. */
2911 struct overlay_entry
2913 Lisp_Object overlay;
2914 Lisp_Object string;
2915 int priority;
2916 int after_string_p;
2920 /* Set up iterator IT from overlay strings at its current position.
2921 Called from handle_stop. */
2923 static enum prop_handled
2924 handle_overlay_change (it)
2925 struct it *it;
2927 if (!STRINGP (it->string) && get_overlay_strings (it))
2928 return HANDLED_RECOMPUTE_PROPS;
2929 else
2930 return HANDLED_NORMALLY;
2934 /* Set up the next overlay string for delivery by IT, if there is an
2935 overlay string to deliver. Called by set_iterator_to_next when the
2936 end of the current overlay string is reached. If there are more
2937 overlay strings to display, IT->string and
2938 IT->current.overlay_string_index are set appropriately here.
2939 Otherwise IT->string is set to nil. */
2941 static void
2942 next_overlay_string (it)
2943 struct it *it;
2945 ++it->current.overlay_string_index;
2946 if (it->current.overlay_string_index == it->n_overlay_strings)
2948 /* No more overlay strings. Restore IT's settings to what
2949 they were before overlay strings were processed, and
2950 continue to deliver from current_buffer. */
2951 pop_it (it);
2952 xassert (it->stop_charpos >= BEGV
2953 && it->stop_charpos <= it->end_charpos);
2954 it->string = Qnil;
2955 it->current.overlay_string_index = -1;
2956 SET_TEXT_POS (it->current.string_pos, -1, -1);
2957 it->n_overlay_strings = 0;
2958 it->method = next_element_from_buffer;
2960 /* If we're at the end of the buffer, record that we have
2961 processed the overlay strings there already, so that
2962 next_element_from_buffer doesn't try it again. */
2963 if (IT_CHARPOS (*it) >= it->end_charpos)
2964 it->overlay_strings_at_end_processed_p = 1;
2966 else
2968 /* There are more overlay strings to process. If
2969 IT->current.overlay_string_index has advanced to a position
2970 where we must load IT->overlay_strings with more strings, do
2971 it. */
2972 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2974 if (it->current.overlay_string_index && i == 0)
2975 load_overlay_strings (it);
2977 /* Initialize IT to deliver display elements from the overlay
2978 string. */
2979 it->string = it->overlay_strings[i];
2980 it->multibyte_p = STRING_MULTIBYTE (it->string);
2981 SET_TEXT_POS (it->current.string_pos, 0, 0);
2982 it->method = next_element_from_string;
2983 it->stop_charpos = 0;
2986 CHECK_IT (it);
2990 /* Compare two overlay_entry structures E1 and E2. Used as a
2991 comparison function for qsort in load_overlay_strings. Overlay
2992 strings for the same position are sorted so that
2994 1. All after-strings come in front of before-strings, except
2995 when they come from the same overlay.
2997 2. Within after-strings, strings are sorted so that overlay strings
2998 from overlays with higher priorities come first.
3000 2. Within before-strings, strings are sorted so that overlay
3001 strings from overlays with higher priorities come last.
3003 Value is analogous to strcmp. */
3006 static int
3007 compare_overlay_entries (e1, e2)
3008 void *e1, *e2;
3010 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3011 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3012 int result;
3014 if (entry1->after_string_p != entry2->after_string_p)
3016 /* Let after-strings appear in front of before-strings if
3017 they come from different overlays. */
3018 if (EQ (entry1->overlay, entry2->overlay))
3019 result = entry1->after_string_p ? 1 : -1;
3020 else
3021 result = entry1->after_string_p ? -1 : 1;
3023 else if (entry1->after_string_p)
3024 /* After-strings sorted in order of decreasing priority. */
3025 result = entry2->priority - entry1->priority;
3026 else
3027 /* Before-strings sorted in order of increasing priority. */
3028 result = entry1->priority - entry2->priority;
3030 return result;
3034 /* Load the vector IT->overlay_strings with overlay strings from IT's
3035 current buffer position. Set IT->n_overlays to the total number of
3036 overlay strings found.
3038 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3039 a time. On entry into load_overlay_strings,
3040 IT->current.overlay_string_index gives the number of overlay
3041 strings that have already been loaded by previous calls to this
3042 function.
3044 IT->add_overlay_start contains an additional overlay start
3045 position to consider for taking overlay strings from, if non-zero.
3046 This position comes into play when the overlay has an `invisible'
3047 property, and both before and after-strings. When we've skipped to
3048 the end of the overlay, because of its `invisible' property, we
3049 nevertheless want its before-string to appear.
3050 IT->add_overlay_start will contain the overlay start position
3051 in this case.
3053 Overlay strings are sorted so that after-string strings come in
3054 front of before-string strings. Within before and after-strings,
3055 strings are sorted by overlay priority. See also function
3056 compare_overlay_entries. */
3058 static void
3059 load_overlay_strings (it)
3060 struct it *it;
3062 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3063 Lisp_Object ov, overlay, window, str, invisible;
3064 int start, end;
3065 int size = 20;
3066 int n = 0, i, j, invis_p;
3067 struct overlay_entry *entries
3068 = (struct overlay_entry *) alloca (size * sizeof *entries);
3069 int charpos = IT_CHARPOS (*it);
3071 /* Append the overlay string STRING of overlay OVERLAY to vector
3072 `entries' which has size `size' and currently contains `n'
3073 elements. AFTER_P non-zero means STRING is an after-string of
3074 OVERLAY. */
3075 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3076 do \
3078 Lisp_Object priority; \
3080 if (n == size) \
3082 int new_size = 2 * size; \
3083 struct overlay_entry *old = entries; \
3084 entries = \
3085 (struct overlay_entry *) alloca (new_size \
3086 * sizeof *entries); \
3087 bcopy (old, entries, size * sizeof *entries); \
3088 size = new_size; \
3091 entries[n].string = (STRING); \
3092 entries[n].overlay = (OVERLAY); \
3093 priority = Foverlay_get ((OVERLAY), Qpriority); \
3094 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3095 entries[n].after_string_p = (AFTER_P); \
3096 ++n; \
3098 while (0)
3100 /* Process overlay before the overlay center. */
3101 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3103 overlay = XCAR (ov);
3104 xassert (OVERLAYP (overlay));
3105 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3106 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3108 if (end < charpos)
3109 break;
3111 /* Skip this overlay if it doesn't start or end at IT's current
3112 position. */
3113 if (end != charpos && start != charpos)
3114 continue;
3116 /* Skip this overlay if it doesn't apply to IT->w. */
3117 window = Foverlay_get (overlay, Qwindow);
3118 if (WINDOWP (window) && XWINDOW (window) != it->w)
3119 continue;
3121 /* If the text ``under'' the overlay is invisible, both before-
3122 and after-strings from this overlay are visible; start and
3123 end position are indistinguishable. */
3124 invisible = Foverlay_get (overlay, Qinvisible);
3125 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3127 /* If overlay has a non-empty before-string, record it. */
3128 if ((start == charpos || (end == charpos && invis_p))
3129 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3130 && XSTRING (str)->size)
3131 RECORD_OVERLAY_STRING (overlay, str, 0);
3133 /* If overlay has a non-empty after-string, record it. */
3134 if ((end == charpos || (start == charpos && invis_p))
3135 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3136 && XSTRING (str)->size)
3137 RECORD_OVERLAY_STRING (overlay, str, 1);
3140 /* Process overlays after the overlay center. */
3141 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3143 overlay = XCAR (ov);
3144 xassert (OVERLAYP (overlay));
3145 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3146 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3148 if (start > charpos)
3149 break;
3151 /* Skip this overlay if it doesn't start or end at IT's current
3152 position. */
3153 if (end != charpos && start != charpos)
3154 continue;
3156 /* Skip this overlay if it doesn't apply to IT->w. */
3157 window = Foverlay_get (overlay, Qwindow);
3158 if (WINDOWP (window) && XWINDOW (window) != it->w)
3159 continue;
3161 /* If the text ``under'' the overlay is invisible, it has a zero
3162 dimension, and both before- and after-strings apply. */
3163 invisible = Foverlay_get (overlay, Qinvisible);
3164 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3166 /* If overlay has a non-empty before-string, record it. */
3167 if ((start == charpos || (end == charpos && invis_p))
3168 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3169 && XSTRING (str)->size)
3170 RECORD_OVERLAY_STRING (overlay, str, 0);
3172 /* If overlay has a non-empty after-string, record it. */
3173 if ((end == charpos || (start == charpos && invis_p))
3174 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3175 && XSTRING (str)->size)
3176 RECORD_OVERLAY_STRING (overlay, str, 1);
3179 #undef RECORD_OVERLAY_STRING
3181 /* Sort entries. */
3182 if (n > 1)
3183 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3185 /* Record the total number of strings to process. */
3186 it->n_overlay_strings = n;
3188 /* IT->current.overlay_string_index is the number of overlay strings
3189 that have already been consumed by IT. Copy some of the
3190 remaining overlay strings to IT->overlay_strings. */
3191 i = 0;
3192 j = it->current.overlay_string_index;
3193 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3194 it->overlay_strings[i++] = entries[j++].string;
3196 CHECK_IT (it);
3200 /* Get the first chunk of overlay strings at IT's current buffer
3201 position. Value is non-zero if at least one overlay string was
3202 found. */
3204 static int
3205 get_overlay_strings (it)
3206 struct it *it;
3208 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3209 process. This fills IT->overlay_strings with strings, and sets
3210 IT->n_overlay_strings to the total number of strings to process.
3211 IT->pos.overlay_string_index has to be set temporarily to zero
3212 because load_overlay_strings needs this; it must be set to -1
3213 when no overlay strings are found because a zero value would
3214 indicate a position in the first overlay string. */
3215 it->current.overlay_string_index = 0;
3216 load_overlay_strings (it);
3218 /* If we found overlay strings, set up IT to deliver display
3219 elements from the first one. Otherwise set up IT to deliver
3220 from current_buffer. */
3221 if (it->n_overlay_strings)
3223 /* Make sure we know settings in current_buffer, so that we can
3224 restore meaningful values when we're done with the overlay
3225 strings. */
3226 compute_stop_pos (it);
3227 xassert (it->face_id >= 0);
3229 /* Save IT's settings. They are restored after all overlay
3230 strings have been processed. */
3231 xassert (it->sp == 0);
3232 push_it (it);
3234 /* Set up IT to deliver display elements from the first overlay
3235 string. */
3236 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3237 it->stop_charpos = 0;
3238 it->string = it->overlay_strings[0];
3239 it->multibyte_p = STRING_MULTIBYTE (it->string);
3240 xassert (STRINGP (it->string));
3241 it->method = next_element_from_string;
3243 else
3245 it->string = Qnil;
3246 it->current.overlay_string_index = -1;
3247 it->method = next_element_from_buffer;
3250 CHECK_IT (it);
3252 /* Value is non-zero if we found at least one overlay string. */
3253 return STRINGP (it->string);
3258 /***********************************************************************
3259 Saving and restoring state
3260 ***********************************************************************/
3262 /* Save current settings of IT on IT->stack. Called, for example,
3263 before setting up IT for an overlay string, to be able to restore
3264 IT's settings to what they were after the overlay string has been
3265 processed. */
3267 static void
3268 push_it (it)
3269 struct it *it;
3271 struct iterator_stack_entry *p;
3273 xassert (it->sp < 2);
3274 p = it->stack + it->sp;
3276 p->stop_charpos = it->stop_charpos;
3277 xassert (it->face_id >= 0);
3278 p->face_id = it->face_id;
3279 p->string = it->string;
3280 p->pos = it->current;
3281 p->end_charpos = it->end_charpos;
3282 p->string_nchars = it->string_nchars;
3283 p->area = it->area;
3284 p->multibyte_p = it->multibyte_p;
3285 p->space_width = it->space_width;
3286 p->font_height = it->font_height;
3287 p->voffset = it->voffset;
3288 p->string_from_display_prop_p = it->string_from_display_prop_p;
3289 ++it->sp;
3293 /* Restore IT's settings from IT->stack. Called, for example, when no
3294 more overlay strings must be processed, and we return to delivering
3295 display elements from a buffer, or when the end of a string from a
3296 `display' property is reached and we return to delivering display
3297 elements from an overlay string, or from a buffer. */
3299 static void
3300 pop_it (it)
3301 struct it *it;
3303 struct iterator_stack_entry *p;
3305 xassert (it->sp > 0);
3306 --it->sp;
3307 p = it->stack + it->sp;
3308 it->stop_charpos = p->stop_charpos;
3309 it->face_id = p->face_id;
3310 it->string = p->string;
3311 it->current = p->pos;
3312 it->end_charpos = p->end_charpos;
3313 it->string_nchars = p->string_nchars;
3314 it->area = p->area;
3315 it->multibyte_p = p->multibyte_p;
3316 it->space_width = p->space_width;
3317 it->font_height = p->font_height;
3318 it->voffset = p->voffset;
3319 it->string_from_display_prop_p = p->string_from_display_prop_p;
3324 /***********************************************************************
3325 Moving over lines
3326 ***********************************************************************/
3328 /* Set IT's current position to the previous line start. */
3330 static void
3331 back_to_previous_line_start (it)
3332 struct it *it;
3334 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3335 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3339 /* Move IT to the next line start.
3341 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3342 we skipped over part of the text (as opposed to moving the iterator
3343 continuously over the text). Otherwise, don't change the value
3344 of *SKIPPED_P.
3346 Newlines may come from buffer text, overlay strings, or strings
3347 displayed via the `display' property. That's the reason we can't
3348 simply use find_next_newline_no_quit. */
3350 static int
3351 forward_to_next_line_start (it, skipped_p)
3352 struct it *it;
3353 int *skipped_p;
3355 int old_selective, newline_found_p, n;
3356 const int MAX_NEWLINE_DISTANCE = 500;
3358 /* Don't handle selective display in the following. It's (a)
3359 unnecessary and (b) leads to an infinite recursion because
3360 next_element_from_ellipsis indirectly calls this function. */
3361 old_selective = it->selective;
3362 it->selective = 0;
3364 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3365 from buffer text. */
3366 n = newline_found_p = 0;
3367 while (n < MAX_NEWLINE_DISTANCE
3368 && get_next_display_element (it)
3369 && !newline_found_p)
3371 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3372 set_iterator_to_next (it, 0);
3373 if (!STRINGP (it->string))
3374 ++n;
3377 /* If we didn't find a newline near enough, see if we can use a
3378 short-cut. */
3379 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3381 int start = IT_CHARPOS (*it);
3382 int limit = find_next_newline_no_quit (start, 1);
3383 Lisp_Object pos;
3385 xassert (!STRINGP (it->string));
3387 /* If there isn't any `display' property in sight, and no
3388 overlays, we can just use the position of the newline in
3389 buffer text. */
3390 if (it->stop_charpos >= limit
3391 || ((pos = Fnext_single_property_change (make_number (start),
3392 Qdisplay,
3393 Qnil, make_number (limit)),
3394 NILP (pos))
3395 && next_overlay_change (start) == ZV))
3397 IT_CHARPOS (*it) = limit;
3398 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3399 *skipped_p = newline_found_p = 1;
3401 else
3403 while (get_next_display_element (it)
3404 && !newline_found_p)
3406 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3407 set_iterator_to_next (it, 0);
3412 it->selective = old_selective;
3413 return newline_found_p;
3417 /* Set IT's current position to the previous visible line start. Skip
3418 invisible text that is so either due to text properties or due to
3419 selective display. Caution: this does not change IT->current_x and
3420 IT->hpos. */
3422 static void
3423 back_to_previous_visible_line_start (it)
3424 struct it *it;
3426 int visible_p = 0;
3428 /* Go back one newline if not on BEGV already. */
3429 if (IT_CHARPOS (*it) > BEGV)
3430 back_to_previous_line_start (it);
3432 /* Move over lines that are invisible because of selective display
3433 or text properties. */
3434 while (IT_CHARPOS (*it) > BEGV
3435 && !visible_p)
3437 visible_p = 1;
3439 /* If selective > 0, then lines indented more than that values
3440 are invisible. */
3441 if (it->selective > 0
3442 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3443 it->selective))
3444 visible_p = 0;
3445 else
3447 Lisp_Object prop;
3449 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3450 Qinvisible, it->window);
3451 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3452 visible_p = 0;
3455 /* Back one more newline if the current one is invisible. */
3456 if (!visible_p)
3457 back_to_previous_line_start (it);
3460 xassert (IT_CHARPOS (*it) >= BEGV);
3461 xassert (IT_CHARPOS (*it) == BEGV
3462 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3463 CHECK_IT (it);
3467 /* Reseat iterator IT at the previous visible line start. Skip
3468 invisible text that is so either due to text properties or due to
3469 selective display. At the end, update IT's overlay information,
3470 face information etc. */
3472 static void
3473 reseat_at_previous_visible_line_start (it)
3474 struct it *it;
3476 back_to_previous_visible_line_start (it);
3477 reseat (it, it->current.pos, 1);
3478 CHECK_IT (it);
3482 /* Reseat iterator IT on the next visible line start in the current
3483 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3484 preceding the line start. Skip over invisible text that is so
3485 because of selective display. Compute faces, overlays etc at the
3486 new position. Note that this function does not skip over text that
3487 is invisible because of text properties. */
3489 static void
3490 reseat_at_next_visible_line_start (it, on_newline_p)
3491 struct it *it;
3492 int on_newline_p;
3494 int newline_found_p, skipped_p = 0;
3496 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3498 /* Skip over lines that are invisible because they are indented
3499 more than the value of IT->selective. */
3500 if (it->selective > 0)
3501 while (IT_CHARPOS (*it) < ZV
3502 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3503 it->selective))
3504 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3506 /* Position on the newline if that's what's requested. */
3507 if (on_newline_p && newline_found_p)
3509 if (STRINGP (it->string))
3511 if (IT_STRING_CHARPOS (*it) > 0)
3513 --IT_STRING_CHARPOS (*it);
3514 --IT_STRING_BYTEPOS (*it);
3517 else if (IT_CHARPOS (*it) > BEGV)
3519 --IT_CHARPOS (*it);
3520 --IT_BYTEPOS (*it);
3521 reseat (it, it->current.pos, 0);
3524 else if (skipped_p)
3525 reseat (it, it->current.pos, 0);
3527 CHECK_IT (it);
3532 /***********************************************************************
3533 Changing an iterator's position
3534 ***********************************************************************/
3536 /* Change IT's current position to POS in current_buffer. If FORCE_P
3537 is non-zero, always check for text properties at the new position.
3538 Otherwise, text properties are only looked up if POS >=
3539 IT->check_charpos of a property. */
3541 static void
3542 reseat (it, pos, force_p)
3543 struct it *it;
3544 struct text_pos pos;
3545 int force_p;
3547 int original_pos = IT_CHARPOS (*it);
3549 reseat_1 (it, pos, 0);
3551 /* Determine where to check text properties. Avoid doing it
3552 where possible because text property lookup is very expensive. */
3553 if (force_p
3554 || CHARPOS (pos) > it->stop_charpos
3555 || CHARPOS (pos) < original_pos)
3556 handle_stop (it);
3558 CHECK_IT (it);
3562 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3563 IT->stop_pos to POS, also. */
3565 static void
3566 reseat_1 (it, pos, set_stop_p)
3567 struct it *it;
3568 struct text_pos pos;
3569 int set_stop_p;
3571 /* Don't call this function when scanning a C string. */
3572 xassert (it->s == NULL);
3574 /* POS must be a reasonable value. */
3575 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3577 it->current.pos = it->position = pos;
3578 XSETBUFFER (it->object, current_buffer);
3579 it->dpvec = NULL;
3580 it->current.dpvec_index = -1;
3581 it->current.overlay_string_index = -1;
3582 IT_STRING_CHARPOS (*it) = -1;
3583 IT_STRING_BYTEPOS (*it) = -1;
3584 it->string = Qnil;
3585 it->method = next_element_from_buffer;
3586 it->sp = 0;
3588 if (set_stop_p)
3589 it->stop_charpos = CHARPOS (pos);
3593 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3594 If S is non-null, it is a C string to iterate over. Otherwise,
3595 STRING gives a Lisp string to iterate over.
3597 If PRECISION > 0, don't return more then PRECISION number of
3598 characters from the string.
3600 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3601 characters have been returned. FIELD_WIDTH < 0 means an infinite
3602 field width.
3604 MULTIBYTE = 0 means disable processing of multibyte characters,
3605 MULTIBYTE > 0 means enable it,
3606 MULTIBYTE < 0 means use IT->multibyte_p.
3608 IT must be initialized via a prior call to init_iterator before
3609 calling this function. */
3611 static void
3612 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3613 struct it *it;
3614 unsigned char *s;
3615 Lisp_Object string;
3616 int charpos;
3617 int precision, field_width, multibyte;
3619 /* No region in strings. */
3620 it->region_beg_charpos = it->region_end_charpos = -1;
3622 /* No text property checks performed by default, but see below. */
3623 it->stop_charpos = -1;
3625 /* Set iterator position and end position. */
3626 bzero (&it->current, sizeof it->current);
3627 it->current.overlay_string_index = -1;
3628 it->current.dpvec_index = -1;
3629 xassert (charpos >= 0);
3631 /* Use the setting of MULTIBYTE if specified. */
3632 if (multibyte >= 0)
3633 it->multibyte_p = multibyte > 0;
3635 if (s == NULL)
3637 xassert (STRINGP (string));
3638 it->string = string;
3639 it->s = NULL;
3640 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3641 it->method = next_element_from_string;
3642 it->current.string_pos = string_pos (charpos, string);
3644 else
3646 it->s = s;
3647 it->string = Qnil;
3649 /* Note that we use IT->current.pos, not it->current.string_pos,
3650 for displaying C strings. */
3651 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3652 if (it->multibyte_p)
3654 it->current.pos = c_string_pos (charpos, s, 1);
3655 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3657 else
3659 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3660 it->end_charpos = it->string_nchars = strlen (s);
3663 it->method = next_element_from_c_string;
3666 /* PRECISION > 0 means don't return more than PRECISION characters
3667 from the string. */
3668 if (precision > 0 && it->end_charpos - charpos > precision)
3669 it->end_charpos = it->string_nchars = charpos + precision;
3671 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3672 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3673 FIELD_WIDTH < 0 means infinite field width. This is useful for
3674 padding with `-' at the end of a mode line. */
3675 if (field_width < 0)
3676 field_width = INFINITY;
3677 if (field_width > it->end_charpos - charpos)
3678 it->end_charpos = charpos + field_width;
3680 /* Use the standard display table for displaying strings. */
3681 if (DISP_TABLE_P (Vstandard_display_table))
3682 it->dp = XCHAR_TABLE (Vstandard_display_table);
3684 it->stop_charpos = charpos;
3685 CHECK_IT (it);
3690 /***********************************************************************
3691 Iteration
3692 ***********************************************************************/
3694 /* Load IT's display element fields with information about the next
3695 display element from the current position of IT. Value is zero if
3696 end of buffer (or C string) is reached. */
3699 get_next_display_element (it)
3700 struct it *it;
3702 /* Non-zero means that we found an display element. Zero means that
3703 we hit the end of what we iterate over. Performance note: the
3704 function pointer `method' used here turns out to be faster than
3705 using a sequence of if-statements. */
3706 int success_p = (*it->method) (it);
3708 if (it->what == IT_CHARACTER)
3710 /* Map via display table or translate control characters.
3711 IT->c, IT->len etc. have been set to the next character by
3712 the function call above. If we have a display table, and it
3713 contains an entry for IT->c, translate it. Don't do this if
3714 IT->c itself comes from a display table, otherwise we could
3715 end up in an infinite recursion. (An alternative could be to
3716 count the recursion depth of this function and signal an
3717 error when a certain maximum depth is reached.) Is it worth
3718 it? */
3719 if (success_p && it->dpvec == NULL)
3721 Lisp_Object dv;
3723 if (it->dp
3724 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3725 VECTORP (dv)))
3727 struct Lisp_Vector *v = XVECTOR (dv);
3729 /* Return the first character from the display table
3730 entry, if not empty. If empty, don't display the
3731 current character. */
3732 if (v->size)
3734 it->dpvec_char_len = it->len;
3735 it->dpvec = v->contents;
3736 it->dpend = v->contents + v->size;
3737 it->current.dpvec_index = 0;
3738 it->method = next_element_from_display_vector;
3741 success_p = get_next_display_element (it);
3744 /* Translate control characters into `\003' or `^C' form.
3745 Control characters coming from a display table entry are
3746 currently not translated because we use IT->dpvec to hold
3747 the translation. This could easily be changed but I
3748 don't believe that it is worth doing.
3750 Non-printable multibyte characters are also translated
3751 octal form. */
3752 else if ((it->c < ' '
3753 && (it->area != TEXT_AREA
3754 || (it->c != '\n' && it->c != '\t')))
3755 || (it->c >= 127
3756 && it->len == 1)
3757 || !CHAR_PRINTABLE_P (it->c))
3759 /* IT->c is a control character which must be displayed
3760 either as '\003' or as `^C' where the '\\' and '^'
3761 can be defined in the display table. Fill
3762 IT->ctl_chars with glyphs for what we have to
3763 display. Then, set IT->dpvec to these glyphs. */
3764 GLYPH g;
3766 if (it->c < 128 && it->ctl_arrow_p)
3768 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3769 if (it->dp
3770 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3771 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3772 g = XINT (DISP_CTRL_GLYPH (it->dp));
3773 else
3774 g = FAST_MAKE_GLYPH ('^', 0);
3775 XSETINT (it->ctl_chars[0], g);
3777 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3778 XSETINT (it->ctl_chars[1], g);
3780 /* Set up IT->dpvec and return first character from it. */
3781 it->dpvec_char_len = it->len;
3782 it->dpvec = it->ctl_chars;
3783 it->dpend = it->dpvec + 2;
3784 it->current.dpvec_index = 0;
3785 it->method = next_element_from_display_vector;
3786 get_next_display_element (it);
3788 else
3790 unsigned char str[MAX_MULTIBYTE_LENGTH];
3791 int len;
3792 int i;
3793 GLYPH escape_glyph;
3795 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3796 if (it->dp
3797 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3798 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3799 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3800 else
3801 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3803 if (SINGLE_BYTE_CHAR_P (it->c))
3804 str[0] = it->c, len = 1;
3805 else
3806 len = CHAR_STRING (it->c, str);
3808 for (i = 0; i < len; i++)
3810 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3811 /* Insert three more glyphs into IT->ctl_chars for
3812 the octal display of the character. */
3813 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3814 XSETINT (it->ctl_chars[i * 4 + 1], g);
3815 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3816 XSETINT (it->ctl_chars[i * 4 + 2], g);
3817 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3818 XSETINT (it->ctl_chars[i * 4 + 3], g);
3821 /* Set up IT->dpvec and return the first character
3822 from it. */
3823 it->dpvec_char_len = it->len;
3824 it->dpvec = it->ctl_chars;
3825 it->dpend = it->dpvec + len * 4;
3826 it->current.dpvec_index = 0;
3827 it->method = next_element_from_display_vector;
3828 get_next_display_element (it);
3833 /* Adjust face id for a multibyte character. There are no
3834 multibyte character in unibyte text. */
3835 if (it->multibyte_p
3836 && success_p
3837 && FRAME_WINDOW_P (it->f))
3839 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3840 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3844 /* Is this character the last one of a run of characters with
3845 box? If yes, set IT->end_of_box_run_p to 1. */
3846 if (it->face_box_p
3847 && it->s == NULL)
3849 int face_id;
3850 struct face *face;
3852 it->end_of_box_run_p
3853 = ((face_id = face_after_it_pos (it),
3854 face_id != it->face_id)
3855 && (face = FACE_FROM_ID (it->f, face_id),
3856 face->box == FACE_NO_BOX));
3859 /* Value is 0 if end of buffer or string reached. */
3860 return success_p;
3864 /* Move IT to the next display element.
3866 RESEAT_P non-zero means if called on a newline in buffer text,
3867 skip to the next visible line start.
3869 Functions get_next_display_element and set_iterator_to_next are
3870 separate because I find this arrangement easier to handle than a
3871 get_next_display_element function that also increments IT's
3872 position. The way it is we can first look at an iterator's current
3873 display element, decide whether it fits on a line, and if it does,
3874 increment the iterator position. The other way around we probably
3875 would either need a flag indicating whether the iterator has to be
3876 incremented the next time, or we would have to implement a
3877 decrement position function which would not be easy to write. */
3879 void
3880 set_iterator_to_next (it, reseat_p)
3881 struct it *it;
3882 int reseat_p;
3884 /* Reset flags indicating start and end of a sequence of characters
3885 with box. Reset them at the start of this function because
3886 moving the iterator to a new position might set them. */
3887 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3889 if (it->method == next_element_from_buffer)
3891 /* The current display element of IT is a character from
3892 current_buffer. Advance in the buffer, and maybe skip over
3893 invisible lines that are so because of selective display. */
3894 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3895 reseat_at_next_visible_line_start (it, 0);
3896 else
3898 xassert (it->len != 0);
3899 IT_BYTEPOS (*it) += it->len;
3900 IT_CHARPOS (*it) += 1;
3901 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3904 else if (it->method == next_element_from_composition)
3906 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3907 if (STRINGP (it->string))
3909 IT_STRING_BYTEPOS (*it) += it->len;
3910 IT_STRING_CHARPOS (*it) += it->cmp_len;
3911 it->method = next_element_from_string;
3912 goto consider_string_end;
3914 else
3916 IT_BYTEPOS (*it) += it->len;
3917 IT_CHARPOS (*it) += it->cmp_len;
3918 it->method = next_element_from_buffer;
3921 else if (it->method == next_element_from_c_string)
3923 /* Current display element of IT is from a C string. */
3924 IT_BYTEPOS (*it) += it->len;
3925 IT_CHARPOS (*it) += 1;
3927 else if (it->method == next_element_from_display_vector)
3929 /* Current display element of IT is from a display table entry.
3930 Advance in the display table definition. Reset it to null if
3931 end reached, and continue with characters from buffers/
3932 strings. */
3933 ++it->current.dpvec_index;
3935 /* Restore face of the iterator to what they were before the
3936 display vector entry (these entries may contain faces). */
3937 it->face_id = it->saved_face_id;
3939 if (it->dpvec + it->current.dpvec_index == it->dpend)
3941 if (it->s)
3942 it->method = next_element_from_c_string;
3943 else if (STRINGP (it->string))
3944 it->method = next_element_from_string;
3945 else
3946 it->method = next_element_from_buffer;
3948 it->dpvec = NULL;
3949 it->current.dpvec_index = -1;
3951 /* Skip over characters which were displayed via IT->dpvec. */
3952 if (it->dpvec_char_len < 0)
3953 reseat_at_next_visible_line_start (it, 1);
3954 else if (it->dpvec_char_len > 0)
3956 it->len = it->dpvec_char_len;
3957 set_iterator_to_next (it, reseat_p);
3961 else if (it->method == next_element_from_string)
3963 /* Current display element is a character from a Lisp string. */
3964 xassert (it->s == NULL && STRINGP (it->string));
3965 IT_STRING_BYTEPOS (*it) += it->len;
3966 IT_STRING_CHARPOS (*it) += 1;
3968 consider_string_end:
3970 if (it->current.overlay_string_index >= 0)
3972 /* IT->string is an overlay string. Advance to the
3973 next, if there is one. */
3974 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3975 next_overlay_string (it);
3977 else
3979 /* IT->string is not an overlay string. If we reached
3980 its end, and there is something on IT->stack, proceed
3981 with what is on the stack. This can be either another
3982 string, this time an overlay string, or a buffer. */
3983 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3984 && it->sp > 0)
3986 pop_it (it);
3987 if (!STRINGP (it->string))
3988 it->method = next_element_from_buffer;
3992 else if (it->method == next_element_from_image
3993 || it->method == next_element_from_stretch)
3995 /* The position etc with which we have to proceed are on
3996 the stack. The position may be at the end of a string,
3997 if the `display' property takes up the whole string. */
3998 pop_it (it);
3999 it->image_id = 0;
4000 if (STRINGP (it->string))
4002 it->method = next_element_from_string;
4003 goto consider_string_end;
4005 else
4006 it->method = next_element_from_buffer;
4008 else
4009 /* There are no other methods defined, so this should be a bug. */
4010 abort ();
4012 xassert (it->method != next_element_from_string
4013 || (STRINGP (it->string)
4014 && IT_STRING_CHARPOS (*it) >= 0));
4018 /* Load IT's display element fields with information about the next
4019 display element which comes from a display table entry or from the
4020 result of translating a control character to one of the forms `^C'
4021 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4023 static int
4024 next_element_from_display_vector (it)
4025 struct it *it;
4027 /* Precondition. */
4028 xassert (it->dpvec && it->current.dpvec_index >= 0);
4030 /* Remember the current face id in case glyphs specify faces.
4031 IT's face is restored in set_iterator_to_next. */
4032 it->saved_face_id = it->face_id;
4034 if (INTEGERP (*it->dpvec)
4035 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4037 int lface_id;
4038 GLYPH g;
4040 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4041 it->c = FAST_GLYPH_CHAR (g);
4042 it->len = CHAR_BYTES (it->c);
4044 /* The entry may contain a face id to use. Such a face id is
4045 the id of a Lisp face, not a realized face. A face id of
4046 zero means no face is specified. */
4047 lface_id = FAST_GLYPH_FACE (g);
4048 if (lface_id)
4050 /* The function returns -1 if lface_id is invalid. */
4051 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4052 if (face_id >= 0)
4053 it->face_id = face_id;
4056 else
4057 /* Display table entry is invalid. Return a space. */
4058 it->c = ' ', it->len = 1;
4060 /* Don't change position and object of the iterator here. They are
4061 still the values of the character that had this display table
4062 entry or was translated, and that's what we want. */
4063 it->what = IT_CHARACTER;
4064 return 1;
4068 /* Load IT with the next display element from Lisp string IT->string.
4069 IT->current.string_pos is the current position within the string.
4070 If IT->current.overlay_string_index >= 0, the Lisp string is an
4071 overlay string. */
4073 static int
4074 next_element_from_string (it)
4075 struct it *it;
4077 struct text_pos position;
4079 xassert (STRINGP (it->string));
4080 xassert (IT_STRING_CHARPOS (*it) >= 0);
4081 position = it->current.string_pos;
4083 /* Time to check for invisible text? */
4084 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4085 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4087 handle_stop (it);
4089 /* Since a handler may have changed IT->method, we must
4090 recurse here. */
4091 return get_next_display_element (it);
4094 if (it->current.overlay_string_index >= 0)
4096 /* Get the next character from an overlay string. In overlay
4097 strings, There is no field width or padding with spaces to
4098 do. */
4099 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4101 it->what = IT_EOB;
4102 return 0;
4104 else if (STRING_MULTIBYTE (it->string))
4106 int remaining = (STRING_BYTES (XSTRING (it->string))
4107 - IT_STRING_BYTEPOS (*it));
4108 unsigned char *s = (XSTRING (it->string)->data
4109 + IT_STRING_BYTEPOS (*it));
4110 it->c = string_char_and_length (s, remaining, &it->len);
4112 else
4114 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4115 it->len = 1;
4118 else
4120 /* Get the next character from a Lisp string that is not an
4121 overlay string. Such strings come from the mode line, for
4122 example. We may have to pad with spaces, or truncate the
4123 string. See also next_element_from_c_string. */
4124 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4126 it->what = IT_EOB;
4127 return 0;
4129 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4131 /* Pad with spaces. */
4132 it->c = ' ', it->len = 1;
4133 CHARPOS (position) = BYTEPOS (position) = -1;
4135 else if (STRING_MULTIBYTE (it->string))
4137 int maxlen = (STRING_BYTES (XSTRING (it->string))
4138 - IT_STRING_BYTEPOS (*it));
4139 unsigned char *s = (XSTRING (it->string)->data
4140 + IT_STRING_BYTEPOS (*it));
4141 it->c = string_char_and_length (s, maxlen, &it->len);
4143 else
4145 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4146 it->len = 1;
4150 /* Record what we have and where it came from. Note that we store a
4151 buffer position in IT->position although it could arguably be a
4152 string position. */
4153 it->what = IT_CHARACTER;
4154 it->object = it->string;
4155 it->position = position;
4156 return 1;
4160 /* Load IT with next display element from C string IT->s.
4161 IT->string_nchars is the maximum number of characters to return
4162 from the string. IT->end_charpos may be greater than
4163 IT->string_nchars when this function is called, in which case we
4164 may have to return padding spaces. Value is zero if end of string
4165 reached, including padding spaces. */
4167 static int
4168 next_element_from_c_string (it)
4169 struct it *it;
4171 int success_p = 1;
4173 xassert (it->s);
4174 it->what = IT_CHARACTER;
4175 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4176 it->object = Qnil;
4178 /* IT's position can be greater IT->string_nchars in case a field
4179 width or precision has been specified when the iterator was
4180 initialized. */
4181 if (IT_CHARPOS (*it) >= it->end_charpos)
4183 /* End of the game. */
4184 it->what = IT_EOB;
4185 success_p = 0;
4187 else if (IT_CHARPOS (*it) >= it->string_nchars)
4189 /* Pad with spaces. */
4190 it->c = ' ', it->len = 1;
4191 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4193 else if (it->multibyte_p)
4195 /* Implementation note: The calls to strlen apparently aren't a
4196 performance problem because there is no noticeable performance
4197 difference between Emacs running in unibyte or multibyte mode. */
4198 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4199 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4200 maxlen, &it->len);
4202 else
4203 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4205 return success_p;
4209 /* Set up IT to return characters from an ellipsis, if appropriate.
4210 The definition of the ellipsis glyphs may come from a display table
4211 entry. This function Fills IT with the first glyph from the
4212 ellipsis if an ellipsis is to be displayed. */
4214 static int
4215 next_element_from_ellipsis (it)
4216 struct it *it;
4218 if (it->selective_display_ellipsis_p)
4220 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4222 /* Use the display table definition for `...'. Invalid glyphs
4223 will be handled by the method returning elements from dpvec. */
4224 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4225 it->dpvec_char_len = it->len;
4226 it->dpvec = v->contents;
4227 it->dpend = v->contents + v->size;
4228 it->current.dpvec_index = 0;
4229 it->method = next_element_from_display_vector;
4231 else
4233 /* Use default `...' which is stored in default_invis_vector. */
4234 it->dpvec_char_len = it->len;
4235 it->dpvec = default_invis_vector;
4236 it->dpend = default_invis_vector + 3;
4237 it->current.dpvec_index = 0;
4238 it->method = next_element_from_display_vector;
4241 else
4243 it->method = next_element_from_buffer;
4244 reseat_at_next_visible_line_start (it, 1);
4247 return get_next_display_element (it);
4251 /* Deliver an image display element. The iterator IT is already
4252 filled with image information (done in handle_display_prop). Value
4253 is always 1. */
4256 static int
4257 next_element_from_image (it)
4258 struct it *it;
4260 it->what = IT_IMAGE;
4261 return 1;
4265 /* Fill iterator IT with next display element from a stretch glyph
4266 property. IT->object is the value of the text property. Value is
4267 always 1. */
4269 static int
4270 next_element_from_stretch (it)
4271 struct it *it;
4273 it->what = IT_STRETCH;
4274 return 1;
4278 /* Load IT with the next display element from current_buffer. Value
4279 is zero if end of buffer reached. IT->stop_charpos is the next
4280 position at which to stop and check for text properties or buffer
4281 end. */
4283 static int
4284 next_element_from_buffer (it)
4285 struct it *it;
4287 int success_p = 1;
4289 /* Check this assumption, otherwise, we would never enter the
4290 if-statement, below. */
4291 xassert (IT_CHARPOS (*it) >= BEGV
4292 && IT_CHARPOS (*it) <= it->stop_charpos);
4294 if (IT_CHARPOS (*it) >= it->stop_charpos)
4296 if (IT_CHARPOS (*it) >= it->end_charpos)
4298 int overlay_strings_follow_p;
4300 /* End of the game, except when overlay strings follow that
4301 haven't been returned yet. */
4302 if (it->overlay_strings_at_end_processed_p)
4303 overlay_strings_follow_p = 0;
4304 else
4306 it->overlay_strings_at_end_processed_p = 1;
4307 overlay_strings_follow_p = get_overlay_strings (it);
4310 if (overlay_strings_follow_p)
4311 success_p = get_next_display_element (it);
4312 else
4314 it->what = IT_EOB;
4315 it->position = it->current.pos;
4316 success_p = 0;
4319 else
4321 handle_stop (it);
4322 return get_next_display_element (it);
4325 else
4327 /* No face changes, overlays etc. in sight, so just return a
4328 character from current_buffer. */
4329 unsigned char *p;
4331 /* Maybe run the redisplay end trigger hook. Performance note:
4332 This doesn't seem to cost measurable time. */
4333 if (it->redisplay_end_trigger_charpos
4334 && it->glyph_row
4335 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4336 run_redisplay_end_trigger_hook (it);
4338 /* Get the next character, maybe multibyte. */
4339 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4340 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4342 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4343 - IT_BYTEPOS (*it));
4344 it->c = string_char_and_length (p, maxlen, &it->len);
4346 else
4347 it->c = *p, it->len = 1;
4349 /* Record what we have and where it came from. */
4350 it->what = IT_CHARACTER;;
4351 it->object = it->w->buffer;
4352 it->position = it->current.pos;
4354 /* Normally we return the character found above, except when we
4355 really want to return an ellipsis for selective display. */
4356 if (it->selective)
4358 if (it->c == '\n')
4360 /* A value of selective > 0 means hide lines indented more
4361 than that number of columns. */
4362 if (it->selective > 0
4363 && IT_CHARPOS (*it) + 1 < ZV
4364 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4365 IT_BYTEPOS (*it) + 1,
4366 it->selective))
4368 success_p = next_element_from_ellipsis (it);
4369 it->dpvec_char_len = -1;
4372 else if (it->c == '\r' && it->selective == -1)
4374 /* A value of selective == -1 means that everything from the
4375 CR to the end of the line is invisible, with maybe an
4376 ellipsis displayed for it. */
4377 success_p = next_element_from_ellipsis (it);
4378 it->dpvec_char_len = -1;
4383 /* Value is zero if end of buffer reached. */
4384 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4385 return success_p;
4389 /* Run the redisplay end trigger hook for IT. */
4391 static void
4392 run_redisplay_end_trigger_hook (it)
4393 struct it *it;
4395 Lisp_Object args[3];
4397 /* IT->glyph_row should be non-null, i.e. we should be actually
4398 displaying something, or otherwise we should not run the hook. */
4399 xassert (it->glyph_row);
4401 /* Set up hook arguments. */
4402 args[0] = Qredisplay_end_trigger_functions;
4403 args[1] = it->window;
4404 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4405 it->redisplay_end_trigger_charpos = 0;
4407 /* Since we are *trying* to run these functions, don't try to run
4408 them again, even if they get an error. */
4409 it->w->redisplay_end_trigger = Qnil;
4410 Frun_hook_with_args (3, args);
4412 /* Notice if it changed the face of the character we are on. */
4413 handle_face_prop (it);
4417 /* Deliver a composition display element. The iterator IT is already
4418 filled with composition information (done in
4419 handle_composition_prop). Value is always 1. */
4421 static int
4422 next_element_from_composition (it)
4423 struct it *it;
4425 it->what = IT_COMPOSITION;
4426 it->position = (STRINGP (it->string)
4427 ? it->current.string_pos
4428 : it->current.pos);
4429 return 1;
4434 /***********************************************************************
4435 Moving an iterator without producing glyphs
4436 ***********************************************************************/
4438 /* Move iterator IT to a specified buffer or X position within one
4439 line on the display without producing glyphs.
4441 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4442 whichever is reached first.
4444 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4446 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4447 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4448 TO_X includes the amount by which a window is horizontally
4449 scrolled.
4451 Value is
4453 MOVE_POS_MATCH_OR_ZV
4454 - when TO_POS or ZV was reached.
4456 MOVE_X_REACHED
4457 -when TO_X was reached before TO_POS or ZV were reached.
4459 MOVE_LINE_CONTINUED
4460 - when we reached the end of the display area and the line must
4461 be continued.
4463 MOVE_LINE_TRUNCATED
4464 - when we reached the end of the display area and the line is
4465 truncated.
4467 MOVE_NEWLINE_OR_CR
4468 - when we stopped at a line end, i.e. a newline or a CR and selective
4469 display is on. */
4471 static enum move_it_result
4472 move_it_in_display_line_to (it, to_charpos, to_x, op)
4473 struct it *it;
4474 int to_charpos, to_x, op;
4476 enum move_it_result result = MOVE_UNDEFINED;
4477 struct glyph_row *saved_glyph_row;
4479 /* Don't produce glyphs in produce_glyphs. */
4480 saved_glyph_row = it->glyph_row;
4481 it->glyph_row = NULL;
4483 while (1)
4485 int x, i, ascent = 0, descent = 0;
4487 /* Stop when ZV or TO_CHARPOS reached. */
4488 if (!get_next_display_element (it)
4489 || ((op & MOVE_TO_POS) != 0
4490 && BUFFERP (it->object)
4491 && IT_CHARPOS (*it) >= to_charpos))
4493 result = MOVE_POS_MATCH_OR_ZV;
4494 break;
4497 /* The call to produce_glyphs will get the metrics of the
4498 display element IT is loaded with. We record in x the
4499 x-position before this display element in case it does not
4500 fit on the line. */
4501 x = it->current_x;
4503 /* Remember the line height so far in case the next element doesn't
4504 fit on the line. */
4505 if (!it->truncate_lines_p)
4507 ascent = it->max_ascent;
4508 descent = it->max_descent;
4511 PRODUCE_GLYPHS (it);
4513 if (it->area != TEXT_AREA)
4515 set_iterator_to_next (it, 1);
4516 continue;
4519 /* The number of glyphs we get back in IT->nglyphs will normally
4520 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4521 character on a terminal frame, or (iii) a line end. For the
4522 second case, IT->nglyphs - 1 padding glyphs will be present
4523 (on X frames, there is only one glyph produced for a
4524 composite character.
4526 The behavior implemented below means, for continuation lines,
4527 that as many spaces of a TAB as fit on the current line are
4528 displayed there. For terminal frames, as many glyphs of a
4529 multi-glyph character are displayed in the current line, too.
4530 This is what the old redisplay code did, and we keep it that
4531 way. Under X, the whole shape of a complex character must
4532 fit on the line or it will be completely displayed in the
4533 next line.
4535 Note that both for tabs and padding glyphs, all glyphs have
4536 the same width. */
4537 if (it->nglyphs)
4539 /* More than one glyph or glyph doesn't fit on line. All
4540 glyphs have the same width. */
4541 int single_glyph_width = it->pixel_width / it->nglyphs;
4542 int new_x;
4544 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4546 new_x = x + single_glyph_width;
4548 /* We want to leave anything reaching TO_X to the caller. */
4549 if ((op & MOVE_TO_X) && new_x > to_x)
4551 it->current_x = x;
4552 result = MOVE_X_REACHED;
4553 break;
4555 else if (/* Lines are continued. */
4556 !it->truncate_lines_p
4557 && (/* And glyph doesn't fit on the line. */
4558 new_x > it->last_visible_x
4559 /* Or it fits exactly and we're on a window
4560 system frame. */
4561 || (new_x == it->last_visible_x
4562 && FRAME_WINDOW_P (it->f))))
4564 if (/* IT->hpos == 0 means the very first glyph
4565 doesn't fit on the line, e.g. a wide image. */
4566 it->hpos == 0
4567 || (new_x == it->last_visible_x
4568 && FRAME_WINDOW_P (it->f)))
4570 ++it->hpos;
4571 it->current_x = new_x;
4572 if (i == it->nglyphs - 1)
4573 set_iterator_to_next (it, 1);
4575 else
4577 it->current_x = x;
4578 it->max_ascent = ascent;
4579 it->max_descent = descent;
4582 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4583 IT_CHARPOS (*it)));
4584 result = MOVE_LINE_CONTINUED;
4585 break;
4587 else if (new_x > it->first_visible_x)
4589 /* Glyph is visible. Increment number of glyphs that
4590 would be displayed. */
4591 ++it->hpos;
4593 else
4595 /* Glyph is completely off the left margin of the display
4596 area. Nothing to do. */
4600 if (result != MOVE_UNDEFINED)
4601 break;
4603 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4605 /* Stop when TO_X specified and reached. This check is
4606 necessary here because of lines consisting of a line end,
4607 only. The line end will not produce any glyphs and we
4608 would never get MOVE_X_REACHED. */
4609 xassert (it->nglyphs == 0);
4610 result = MOVE_X_REACHED;
4611 break;
4614 /* Is this a line end? If yes, we're done. */
4615 if (ITERATOR_AT_END_OF_LINE_P (it))
4617 result = MOVE_NEWLINE_OR_CR;
4618 break;
4621 /* The current display element has been consumed. Advance
4622 to the next. */
4623 set_iterator_to_next (it, 1);
4625 /* Stop if lines are truncated and IT's current x-position is
4626 past the right edge of the window now. */
4627 if (it->truncate_lines_p
4628 && it->current_x >= it->last_visible_x)
4630 result = MOVE_LINE_TRUNCATED;
4631 break;
4635 /* Restore the iterator settings altered at the beginning of this
4636 function. */
4637 it->glyph_row = saved_glyph_row;
4638 return result;
4642 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4643 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4644 the description of enum move_operation_enum.
4646 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4647 screen line, this function will set IT to the next position >
4648 TO_CHARPOS. */
4650 void
4651 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4652 struct it *it;
4653 int to_charpos, to_x, to_y, to_vpos;
4654 int op;
4656 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4657 int line_height;
4658 int reached = 0;
4660 for (;;)
4662 if (op & MOVE_TO_VPOS)
4664 /* If no TO_CHARPOS and no TO_X specified, stop at the
4665 start of the line TO_VPOS. */
4666 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4668 if (it->vpos == to_vpos)
4670 reached = 1;
4671 break;
4673 else
4674 skip = move_it_in_display_line_to (it, -1, -1, 0);
4676 else
4678 /* TO_VPOS >= 0 means stop at TO_X in the line at
4679 TO_VPOS, or at TO_POS, whichever comes first. */
4680 if (it->vpos == to_vpos)
4682 reached = 2;
4683 break;
4686 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4688 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4690 reached = 3;
4691 break;
4693 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4695 /* We have reached TO_X but not in the line we want. */
4696 skip = move_it_in_display_line_to (it, to_charpos,
4697 -1, MOVE_TO_POS);
4698 if (skip == MOVE_POS_MATCH_OR_ZV)
4700 reached = 4;
4701 break;
4706 else if (op & MOVE_TO_Y)
4708 struct it it_backup;
4710 /* TO_Y specified means stop at TO_X in the line containing
4711 TO_Y---or at TO_CHARPOS if this is reached first. The
4712 problem is that we can't really tell whether the line
4713 contains TO_Y before we have completely scanned it, and
4714 this may skip past TO_X. What we do is to first scan to
4715 TO_X.
4717 If TO_X is not specified, use a TO_X of zero. The reason
4718 is to make the outcome of this function more predictable.
4719 If we didn't use TO_X == 0, we would stop at the end of
4720 the line which is probably not what a caller would expect
4721 to happen. */
4722 skip = move_it_in_display_line_to (it, to_charpos,
4723 ((op & MOVE_TO_X)
4724 ? to_x : 0),
4725 (MOVE_TO_X
4726 | (op & MOVE_TO_POS)));
4728 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4729 if (skip == MOVE_POS_MATCH_OR_ZV)
4731 reached = 5;
4732 break;
4735 /* If TO_X was reached, we would like to know whether TO_Y
4736 is in the line. This can only be said if we know the
4737 total line height which requires us to scan the rest of
4738 the line. */
4739 if (skip == MOVE_X_REACHED)
4741 it_backup = *it;
4742 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4743 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4744 op & MOVE_TO_POS);
4745 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4748 /* Now, decide whether TO_Y is in this line. */
4749 line_height = it->max_ascent + it->max_descent;
4750 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4752 if (to_y >= it->current_y
4753 && to_y < it->current_y + line_height)
4755 if (skip == MOVE_X_REACHED)
4756 /* If TO_Y is in this line and TO_X was reached above,
4757 we scanned too far. We have to restore IT's settings
4758 to the ones before skipping. */
4759 *it = it_backup;
4760 reached = 6;
4762 else if (skip == MOVE_X_REACHED)
4764 skip = skip2;
4765 if (skip == MOVE_POS_MATCH_OR_ZV)
4766 reached = 7;
4769 if (reached)
4770 break;
4772 else
4773 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4775 switch (skip)
4777 case MOVE_POS_MATCH_OR_ZV:
4778 reached = 8;
4779 goto out;
4781 case MOVE_NEWLINE_OR_CR:
4782 set_iterator_to_next (it, 1);
4783 it->continuation_lines_width = 0;
4784 break;
4786 case MOVE_LINE_TRUNCATED:
4787 it->continuation_lines_width = 0;
4788 reseat_at_next_visible_line_start (it, 0);
4789 if ((op & MOVE_TO_POS) != 0
4790 && IT_CHARPOS (*it) > to_charpos)
4792 reached = 9;
4793 goto out;
4795 break;
4797 case MOVE_LINE_CONTINUED:
4798 it->continuation_lines_width += it->current_x;
4799 break;
4801 default:
4802 abort ();
4805 /* Reset/increment for the next run. */
4806 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4807 it->current_x = it->hpos = 0;
4808 it->current_y += it->max_ascent + it->max_descent;
4809 ++it->vpos;
4810 last_height = it->max_ascent + it->max_descent;
4811 last_max_ascent = it->max_ascent;
4812 it->max_ascent = it->max_descent = 0;
4815 out:
4817 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4821 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4823 If DY > 0, move IT backward at least that many pixels. DY = 0
4824 means move IT backward to the preceding line start or BEGV. This
4825 function may move over more than DY pixels if IT->current_y - DY
4826 ends up in the middle of a line; in this case IT->current_y will be
4827 set to the top of the line moved to. */
4829 void
4830 move_it_vertically_backward (it, dy)
4831 struct it *it;
4832 int dy;
4834 int nlines, h, line_height;
4835 struct it it2;
4836 int start_pos = IT_CHARPOS (*it);
4838 xassert (dy >= 0);
4840 /* Estimate how many newlines we must move back. */
4841 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4843 /* Set the iterator's position that many lines back. */
4844 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4845 back_to_previous_visible_line_start (it);
4847 /* Reseat the iterator here. When moving backward, we don't want
4848 reseat to skip forward over invisible text, set up the iterator
4849 to deliver from overlay strings at the new position etc. So,
4850 use reseat_1 here. */
4851 reseat_1 (it, it->current.pos, 1);
4853 /* We are now surely at a line start. */
4854 it->current_x = it->hpos = 0;
4856 /* Move forward and see what y-distance we moved. First move to the
4857 start of the next line so that we get its height. We need this
4858 height to be able to tell whether we reached the specified
4859 y-distance. */
4860 it2 = *it;
4861 it2.max_ascent = it2.max_descent = 0;
4862 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4863 MOVE_TO_POS | MOVE_TO_VPOS);
4864 xassert (IT_CHARPOS (*it) >= BEGV);
4865 line_height = it2.max_ascent + it2.max_descent;
4867 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4868 xassert (IT_CHARPOS (*it) >= BEGV);
4869 h = it2.current_y - it->current_y;
4870 nlines = it2.vpos - it->vpos;
4872 /* Correct IT's y and vpos position. */
4873 it->vpos -= nlines;
4874 it->current_y -= h;
4876 if (dy == 0)
4878 /* DY == 0 means move to the start of the screen line. The
4879 value of nlines is > 0 if continuation lines were involved. */
4880 if (nlines > 0)
4881 move_it_by_lines (it, nlines, 1);
4882 xassert (IT_CHARPOS (*it) <= start_pos);
4884 else if (nlines)
4886 /* The y-position we try to reach. Note that h has been
4887 subtracted in front of the if-statement. */
4888 int target_y = it->current_y + h - dy;
4890 /* If we did not reach target_y, try to move further backward if
4891 we can. If we moved too far backward, try to move forward. */
4892 if (target_y < it->current_y
4893 && IT_CHARPOS (*it) > BEGV)
4895 move_it_vertically (it, target_y - it->current_y);
4896 xassert (IT_CHARPOS (*it) >= BEGV);
4898 else if (target_y >= it->current_y + line_height
4899 && IT_CHARPOS (*it) < ZV)
4901 move_it_vertically (it, target_y - (it->current_y + line_height));
4902 xassert (IT_CHARPOS (*it) >= BEGV);
4908 /* Move IT by a specified amount of pixel lines DY. DY negative means
4909 move backwards. DY = 0 means move to start of screen line. At the
4910 end, IT will be on the start of a screen line. */
4912 void
4913 move_it_vertically (it, dy)
4914 struct it *it;
4915 int dy;
4917 if (dy <= 0)
4918 move_it_vertically_backward (it, -dy);
4919 else if (dy > 0)
4921 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4922 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4923 MOVE_TO_POS | MOVE_TO_Y);
4924 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4926 /* If buffer ends in ZV without a newline, move to the start of
4927 the line to satisfy the post-condition. */
4928 if (IT_CHARPOS (*it) == ZV
4929 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4930 move_it_by_lines (it, 0, 0);
4935 /* Return non-zero if some text between buffer positions START_CHARPOS
4936 and END_CHARPOS is invisible. IT->window is the window for text
4937 property lookup. */
4939 static int
4940 invisible_text_between_p (it, start_charpos, end_charpos)
4941 struct it *it;
4942 int start_charpos, end_charpos;
4944 Lisp_Object prop, limit;
4945 int invisible_found_p;
4947 xassert (it != NULL && start_charpos <= end_charpos);
4949 /* Is text at START invisible? */
4950 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4951 it->window);
4952 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4953 invisible_found_p = 1;
4954 else
4956 limit = Fnext_single_char_property_change (make_number (start_charpos),
4957 Qinvisible, Qnil,
4958 make_number (end_charpos));
4959 invisible_found_p = XFASTINT (limit) < end_charpos;
4962 return invisible_found_p;
4966 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4967 negative means move up. DVPOS == 0 means move to the start of the
4968 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4969 NEED_Y_P is zero, IT->current_y will be left unchanged.
4971 Further optimization ideas: If we would know that IT->f doesn't use
4972 a face with proportional font, we could be faster for
4973 truncate-lines nil. */
4975 void
4976 move_it_by_lines (it, dvpos, need_y_p)
4977 struct it *it;
4978 int dvpos, need_y_p;
4980 struct position pos;
4982 if (!FRAME_WINDOW_P (it->f))
4984 struct text_pos textpos;
4986 /* We can use vmotion on frames without proportional fonts. */
4987 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4988 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4989 reseat (it, textpos, 1);
4990 it->vpos += pos.vpos;
4991 it->current_y += pos.vpos;
4993 else if (dvpos == 0)
4995 /* DVPOS == 0 means move to the start of the screen line. */
4996 move_it_vertically_backward (it, 0);
4997 xassert (it->current_x == 0 && it->hpos == 0);
4999 else if (dvpos > 0)
5001 /* If there are no continuation lines, and if there is no
5002 selective display, try the simple method of moving forward
5003 DVPOS newlines, then see where we are. */
5004 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5006 int shortage = 0, charpos;
5008 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
5009 charpos = IT_CHARPOS (*it) + 1;
5010 else
5011 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5012 &shortage, 0);
5014 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5016 struct text_pos pos;
5017 CHARPOS (pos) = charpos;
5018 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5019 reseat (it, pos, 1);
5020 it->vpos += dvpos - shortage;
5021 it->hpos = it->current_x = 0;
5022 return;
5026 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5028 else
5030 struct it it2;
5031 int start_charpos, i;
5033 /* If there are no continuation lines, and if there is no
5034 selective display, try the simple method of moving backward
5035 -DVPOS newlines. */
5036 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5038 int shortage;
5039 int charpos = IT_CHARPOS (*it);
5040 int bytepos = IT_BYTEPOS (*it);
5042 /* If in the middle of a line, go to its start. */
5043 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5045 charpos = find_next_newline_no_quit (charpos, -1);
5046 bytepos = CHAR_TO_BYTE (charpos);
5049 if (charpos == BEGV)
5051 struct text_pos pos;
5052 CHARPOS (pos) = charpos;
5053 BYTEPOS (pos) = bytepos;
5054 reseat (it, pos, 1);
5055 it->hpos = it->current_x = 0;
5056 return;
5058 else
5060 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5061 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5063 struct text_pos pos;
5064 CHARPOS (pos) = charpos;
5065 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5066 reseat (it, pos, 1);
5067 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5068 it->hpos = it->current_x = 0;
5069 return;
5074 /* Go back -DVPOS visible lines and reseat the iterator there. */
5075 start_charpos = IT_CHARPOS (*it);
5076 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5077 back_to_previous_visible_line_start (it);
5078 reseat (it, it->current.pos, 1);
5079 it->current_x = it->hpos = 0;
5081 /* Above call may have moved too far if continuation lines
5082 are involved. Scan forward and see if it did. */
5083 it2 = *it;
5084 it2.vpos = it2.current_y = 0;
5085 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5086 it->vpos -= it2.vpos;
5087 it->current_y -= it2.current_y;
5088 it->current_x = it->hpos = 0;
5090 /* If we moved too far, move IT some lines forward. */
5091 if (it2.vpos > -dvpos)
5093 int delta = it2.vpos + dvpos;
5094 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5101 /***********************************************************************
5102 Messages
5103 ***********************************************************************/
5106 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5107 to *Messages*. */
5109 void
5110 add_to_log (format, arg1, arg2)
5111 char *format;
5112 Lisp_Object arg1, arg2;
5114 Lisp_Object args[3];
5115 Lisp_Object msg, fmt;
5116 char *buffer;
5117 int len;
5118 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5120 fmt = msg = Qnil;
5121 GCPRO4 (fmt, msg, arg1, arg2);
5123 args[0] = fmt = build_string (format);
5124 args[1] = arg1;
5125 args[2] = arg2;
5126 msg = Fformat (3, args);
5128 len = STRING_BYTES (XSTRING (msg)) + 1;
5129 buffer = (char *) alloca (len);
5130 strcpy (buffer, XSTRING (msg)->data);
5132 message_dolog (buffer, len - 1, 1, 0);
5133 UNGCPRO;
5137 /* Output a newline in the *Messages* buffer if "needs" one. */
5139 void
5140 message_log_maybe_newline ()
5142 if (message_log_need_newline)
5143 message_dolog ("", 0, 1, 0);
5147 /* Add a string M of length LEN to the message log, optionally
5148 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5149 nonzero, means interpret the contents of M as multibyte. This
5150 function calls low-level routines in order to bypass text property
5151 hooks, etc. which might not be safe to run. */
5153 void
5154 message_dolog (m, len, nlflag, multibyte)
5155 char *m;
5156 int len, nlflag, multibyte;
5158 if (!NILP (Vmessage_log_max))
5160 struct buffer *oldbuf;
5161 Lisp_Object oldpoint, oldbegv, oldzv;
5162 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5163 int point_at_end = 0;
5164 int zv_at_end = 0;
5165 Lisp_Object old_deactivate_mark, tem;
5166 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5168 old_deactivate_mark = Vdeactivate_mark;
5169 oldbuf = current_buffer;
5170 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5171 current_buffer->undo_list = Qt;
5173 oldpoint = Fpoint_marker ();
5174 oldbegv = Fpoint_min_marker ();
5175 oldzv = Fpoint_max_marker ();
5176 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5178 if (PT == Z)
5179 point_at_end = 1;
5180 if (ZV == Z)
5181 zv_at_end = 1;
5183 BEGV = BEG;
5184 BEGV_BYTE = BEG_BYTE;
5185 ZV = Z;
5186 ZV_BYTE = Z_BYTE;
5187 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5189 /* Insert the string--maybe converting multibyte to single byte
5190 or vice versa, so that all the text fits the buffer. */
5191 if (multibyte
5192 && NILP (current_buffer->enable_multibyte_characters))
5194 int i, c, nbytes;
5195 unsigned char work[1];
5197 /* Convert a multibyte string to single-byte
5198 for the *Message* buffer. */
5199 for (i = 0; i < len; i += nbytes)
5201 c = string_char_and_length (m + i, len - i, &nbytes);
5202 work[0] = (SINGLE_BYTE_CHAR_P (c)
5204 : multibyte_char_to_unibyte (c, Qnil));
5205 insert_1_both (work, 1, 1, 1, 0, 0);
5208 else if (! multibyte
5209 && ! NILP (current_buffer->enable_multibyte_characters))
5211 int i, c, nbytes;
5212 unsigned char *msg = (unsigned char *) m;
5213 unsigned char str[MAX_MULTIBYTE_LENGTH];
5214 /* Convert a single-byte string to multibyte
5215 for the *Message* buffer. */
5216 for (i = 0; i < len; i++)
5218 c = unibyte_char_to_multibyte (msg[i]);
5219 nbytes = CHAR_STRING (c, str);
5220 insert_1_both (str, 1, nbytes, 1, 0, 0);
5223 else if (len)
5224 insert_1 (m, len, 1, 0, 0);
5226 if (nlflag)
5228 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5229 insert_1 ("\n", 1, 1, 0, 0);
5231 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5232 this_bol = PT;
5233 this_bol_byte = PT_BYTE;
5235 if (this_bol > BEG)
5237 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5238 prev_bol = PT;
5239 prev_bol_byte = PT_BYTE;
5241 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5242 this_bol, this_bol_byte);
5243 if (dup)
5245 del_range_both (prev_bol, prev_bol_byte,
5246 this_bol, this_bol_byte, 0);
5247 if (dup > 1)
5249 char dupstr[40];
5250 int duplen;
5252 /* If you change this format, don't forget to also
5253 change message_log_check_duplicate. */
5254 sprintf (dupstr, " [%d times]", dup);
5255 duplen = strlen (dupstr);
5256 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5257 insert_1 (dupstr, duplen, 1, 0, 1);
5262 if (NATNUMP (Vmessage_log_max))
5264 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5265 -XFASTINT (Vmessage_log_max) - 1, 0);
5266 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5269 BEGV = XMARKER (oldbegv)->charpos;
5270 BEGV_BYTE = marker_byte_position (oldbegv);
5272 if (zv_at_end)
5274 ZV = Z;
5275 ZV_BYTE = Z_BYTE;
5277 else
5279 ZV = XMARKER (oldzv)->charpos;
5280 ZV_BYTE = marker_byte_position (oldzv);
5283 if (point_at_end)
5284 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5285 else
5286 /* We can't do Fgoto_char (oldpoint) because it will run some
5287 Lisp code. */
5288 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5289 XMARKER (oldpoint)->bytepos);
5291 UNGCPRO;
5292 free_marker (oldpoint);
5293 free_marker (oldbegv);
5294 free_marker (oldzv);
5296 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5297 set_buffer_internal (oldbuf);
5298 if (NILP (tem))
5299 windows_or_buffers_changed = old_windows_or_buffers_changed;
5300 message_log_need_newline = !nlflag;
5301 Vdeactivate_mark = old_deactivate_mark;
5306 /* We are at the end of the buffer after just having inserted a newline.
5307 (Note: We depend on the fact we won't be crossing the gap.)
5308 Check to see if the most recent message looks a lot like the previous one.
5309 Return 0 if different, 1 if the new one should just replace it, or a
5310 value N > 1 if we should also append " [N times]". */
5312 static int
5313 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5314 int prev_bol, this_bol;
5315 int prev_bol_byte, this_bol_byte;
5317 int i;
5318 int len = Z_BYTE - 1 - this_bol_byte;
5319 int seen_dots = 0;
5320 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5321 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5323 for (i = 0; i < len; i++)
5325 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5326 && p1[i] != '\n')
5327 seen_dots = 1;
5328 if (p1[i] != p2[i])
5329 return seen_dots;
5331 p1 += len;
5332 if (*p1 == '\n')
5333 return 2;
5334 if (*p1++ == ' ' && *p1++ == '[')
5336 int n = 0;
5337 while (*p1 >= '0' && *p1 <= '9')
5338 n = n * 10 + *p1++ - '0';
5339 if (strncmp (p1, " times]\n", 8) == 0)
5340 return n+1;
5342 return 0;
5346 /* Display an echo area message M with a specified length of LEN
5347 chars. The string may include null characters. If M is 0, clear
5348 out any existing message, and let the mini-buffer text show through.
5350 The buffer M must continue to exist until after the echo area gets
5351 cleared or some other message gets displayed there. This means do
5352 not pass text that is stored in a Lisp string; do not pass text in
5353 a buffer that was alloca'd. */
5355 void
5356 message2 (m, len, multibyte)
5357 char *m;
5358 int len;
5359 int multibyte;
5361 /* First flush out any partial line written with print. */
5362 message_log_maybe_newline ();
5363 if (m)
5364 message_dolog (m, len, 1, multibyte);
5365 message2_nolog (m, len, multibyte);
5369 /* The non-logging counterpart of message2. */
5371 void
5372 message2_nolog (m, len, multibyte)
5373 char *m;
5374 int len;
5376 struct frame *sf = SELECTED_FRAME ();
5377 message_enable_multibyte = multibyte;
5379 if (noninteractive)
5381 if (noninteractive_need_newline)
5382 putc ('\n', stderr);
5383 noninteractive_need_newline = 0;
5384 if (m)
5385 fwrite (m, len, 1, stderr);
5386 if (cursor_in_echo_area == 0)
5387 fprintf (stderr, "\n");
5388 fflush (stderr);
5390 /* A null message buffer means that the frame hasn't really been
5391 initialized yet. Error messages get reported properly by
5392 cmd_error, so this must be just an informative message; toss it. */
5393 else if (INTERACTIVE
5394 && sf->glyphs_initialized_p
5395 && FRAME_MESSAGE_BUF (sf))
5397 Lisp_Object mini_window;
5398 struct frame *f;
5400 /* Get the frame containing the mini-buffer
5401 that the selected frame is using. */
5402 mini_window = FRAME_MINIBUF_WINDOW (sf);
5403 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5405 FRAME_SAMPLE_VISIBILITY (f);
5406 if (FRAME_VISIBLE_P (sf)
5407 && ! FRAME_VISIBLE_P (f))
5408 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5410 if (m)
5412 set_message (m, Qnil, len, multibyte);
5413 if (minibuffer_auto_raise)
5414 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5416 else
5417 clear_message (1, 1);
5419 do_pending_window_change (0);
5420 echo_area_display (1);
5421 do_pending_window_change (0);
5422 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5423 (*frame_up_to_date_hook) (f);
5428 /* Display an echo area message M with a specified length of NBYTES
5429 bytes. The string may include null characters. If M is not a
5430 string, clear out any existing message, and let the mini-buffer
5431 text show through. */
5433 void
5434 message3 (m, nbytes, multibyte)
5435 Lisp_Object m;
5436 int nbytes;
5437 int multibyte;
5439 struct gcpro gcpro1;
5441 GCPRO1 (m);
5443 /* First flush out any partial line written with print. */
5444 message_log_maybe_newline ();
5445 if (STRINGP (m))
5446 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5447 message3_nolog (m, nbytes, multibyte);
5449 UNGCPRO;
5453 /* The non-logging version of message3. */
5455 void
5456 message3_nolog (m, nbytes, multibyte)
5457 Lisp_Object m;
5458 int nbytes, multibyte;
5460 struct frame *sf = SELECTED_FRAME ();
5461 message_enable_multibyte = multibyte;
5463 if (noninteractive)
5465 if (noninteractive_need_newline)
5466 putc ('\n', stderr);
5467 noninteractive_need_newline = 0;
5468 if (STRINGP (m))
5469 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5470 if (cursor_in_echo_area == 0)
5471 fprintf (stderr, "\n");
5472 fflush (stderr);
5474 /* A null message buffer means that the frame hasn't really been
5475 initialized yet. Error messages get reported properly by
5476 cmd_error, so this must be just an informative message; toss it. */
5477 else if (INTERACTIVE
5478 && sf->glyphs_initialized_p
5479 && FRAME_MESSAGE_BUF (sf))
5481 Lisp_Object mini_window;
5482 Lisp_Object frame;
5483 struct frame *f;
5485 /* Get the frame containing the mini-buffer
5486 that the selected frame is using. */
5487 mini_window = FRAME_MINIBUF_WINDOW (sf);
5488 frame = XWINDOW (mini_window)->frame;
5489 f = XFRAME (frame);
5491 FRAME_SAMPLE_VISIBILITY (f);
5492 if (FRAME_VISIBLE_P (sf)
5493 && !FRAME_VISIBLE_P (f))
5494 Fmake_frame_visible (frame);
5496 if (STRINGP (m) && XSTRING (m)->size)
5498 set_message (NULL, m, nbytes, multibyte);
5499 if (minibuffer_auto_raise)
5500 Fraise_frame (frame);
5502 else
5503 clear_message (1, 1);
5505 do_pending_window_change (0);
5506 echo_area_display (1);
5507 do_pending_window_change (0);
5508 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5509 (*frame_up_to_date_hook) (f);
5514 /* Display a null-terminated echo area message M. If M is 0, clear
5515 out any existing message, and let the mini-buffer text show through.
5517 The buffer M must continue to exist until after the echo area gets
5518 cleared or some other message gets displayed there. Do not pass
5519 text that is stored in a Lisp string. Do not pass text in a buffer
5520 that was alloca'd. */
5522 void
5523 message1 (m)
5524 char *m;
5526 message2 (m, (m ? strlen (m) : 0), 0);
5530 /* The non-logging counterpart of message1. */
5532 void
5533 message1_nolog (m)
5534 char *m;
5536 message2_nolog (m, (m ? strlen (m) : 0), 0);
5539 /* Display a message M which contains a single %s
5540 which gets replaced with STRING. */
5542 void
5543 message_with_string (m, string, log)
5544 char *m;
5545 Lisp_Object string;
5546 int log;
5548 if (noninteractive)
5550 if (m)
5552 if (noninteractive_need_newline)
5553 putc ('\n', stderr);
5554 noninteractive_need_newline = 0;
5555 fprintf (stderr, m, XSTRING (string)->data);
5556 if (cursor_in_echo_area == 0)
5557 fprintf (stderr, "\n");
5558 fflush (stderr);
5561 else if (INTERACTIVE)
5563 /* The frame whose minibuffer we're going to display the message on.
5564 It may be larger than the selected frame, so we need
5565 to use its buffer, not the selected frame's buffer. */
5566 Lisp_Object mini_window;
5567 struct frame *f, *sf = SELECTED_FRAME ();
5569 /* Get the frame containing the minibuffer
5570 that the selected frame is using. */
5571 mini_window = FRAME_MINIBUF_WINDOW (sf);
5572 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5574 /* A null message buffer means that the frame hasn't really been
5575 initialized yet. Error messages get reported properly by
5576 cmd_error, so this must be just an informative message; toss it. */
5577 if (FRAME_MESSAGE_BUF (f))
5579 int len;
5580 char *a[1];
5581 a[0] = (char *) XSTRING (string)->data;
5583 len = doprnt (FRAME_MESSAGE_BUF (f),
5584 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5586 if (log)
5587 message2 (FRAME_MESSAGE_BUF (f), len,
5588 STRING_MULTIBYTE (string));
5589 else
5590 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5591 STRING_MULTIBYTE (string));
5593 /* Print should start at the beginning of the message
5594 buffer next time. */
5595 message_buf_print = 0;
5601 /* Dump an informative message to the minibuf. If M is 0, clear out
5602 any existing message, and let the mini-buffer text show through. */
5604 /* VARARGS 1 */
5605 void
5606 message (m, a1, a2, a3)
5607 char *m;
5608 EMACS_INT a1, a2, a3;
5610 if (noninteractive)
5612 if (m)
5614 if (noninteractive_need_newline)
5615 putc ('\n', stderr);
5616 noninteractive_need_newline = 0;
5617 fprintf (stderr, m, a1, a2, a3);
5618 if (cursor_in_echo_area == 0)
5619 fprintf (stderr, "\n");
5620 fflush (stderr);
5623 else if (INTERACTIVE)
5625 /* The frame whose mini-buffer we're going to display the message
5626 on. It may be larger than the selected frame, so we need to
5627 use its buffer, not the selected frame's buffer. */
5628 Lisp_Object mini_window;
5629 struct frame *f, *sf = SELECTED_FRAME ();
5631 /* Get the frame containing the mini-buffer
5632 that the selected frame is using. */
5633 mini_window = FRAME_MINIBUF_WINDOW (sf);
5634 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5636 /* A null message buffer means that the frame hasn't really been
5637 initialized yet. Error messages get reported properly by
5638 cmd_error, so this must be just an informative message; toss
5639 it. */
5640 if (FRAME_MESSAGE_BUF (f))
5642 if (m)
5644 int len;
5645 #ifdef NO_ARG_ARRAY
5646 char *a[3];
5647 a[0] = (char *) a1;
5648 a[1] = (char *) a2;
5649 a[2] = (char *) a3;
5651 len = doprnt (FRAME_MESSAGE_BUF (f),
5652 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5653 #else
5654 len = doprnt (FRAME_MESSAGE_BUF (f),
5655 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5656 (char **) &a1);
5657 #endif /* NO_ARG_ARRAY */
5659 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5661 else
5662 message1 (0);
5664 /* Print should start at the beginning of the message
5665 buffer next time. */
5666 message_buf_print = 0;
5672 /* The non-logging version of message. */
5674 void
5675 message_nolog (m, a1, a2, a3)
5676 char *m;
5677 EMACS_INT a1, a2, a3;
5679 Lisp_Object old_log_max;
5680 old_log_max = Vmessage_log_max;
5681 Vmessage_log_max = Qnil;
5682 message (m, a1, a2, a3);
5683 Vmessage_log_max = old_log_max;
5687 /* Display the current message in the current mini-buffer. This is
5688 only called from error handlers in process.c, and is not time
5689 critical. */
5691 void
5692 update_echo_area ()
5694 if (!NILP (echo_area_buffer[0]))
5696 Lisp_Object string;
5697 string = Fcurrent_message ();
5698 message3 (string, XSTRING (string)->size,
5699 !NILP (current_buffer->enable_multibyte_characters));
5704 /* Make sure echo area buffers in echo_buffers[] are life. If they
5705 aren't, make new ones. */
5707 static void
5708 ensure_echo_area_buffers ()
5710 int i;
5712 for (i = 0; i < 2; ++i)
5713 if (!BUFFERP (echo_buffer[i])
5714 || NILP (XBUFFER (echo_buffer[i])->name))
5716 char name[30];
5717 Lisp_Object old_buffer;
5718 int j;
5720 old_buffer = echo_buffer[i];
5721 sprintf (name, " *Echo Area %d*", i);
5722 echo_buffer[i] = Fget_buffer_create (build_string (name));
5723 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5725 for (j = 0; j < 2; ++j)
5726 if (EQ (old_buffer, echo_area_buffer[j]))
5727 echo_area_buffer[j] = echo_buffer[i];
5732 /* Call FN with args A1..A4 with either the current or last displayed
5733 echo_area_buffer as current buffer.
5735 WHICH zero means use the current message buffer
5736 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5737 from echo_buffer[] and clear it.
5739 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5740 suitable buffer from echo_buffer[] and clear it.
5742 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5743 that the current message becomes the last displayed one, make
5744 choose a suitable buffer for echo_area_buffer[0], and clear it.
5746 Value is what FN returns. */
5748 static int
5749 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5750 struct window *w;
5751 int which;
5752 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5753 EMACS_INT a1;
5754 Lisp_Object a2;
5755 EMACS_INT a3, a4;
5757 Lisp_Object buffer;
5758 int this_one, the_other, clear_buffer_p, rc;
5759 int count = specpdl_ptr - specpdl;
5761 /* If buffers aren't life, make new ones. */
5762 ensure_echo_area_buffers ();
5764 clear_buffer_p = 0;
5766 if (which == 0)
5767 this_one = 0, the_other = 1;
5768 else if (which > 0)
5769 this_one = 1, the_other = 0;
5770 else
5772 this_one = 0, the_other = 1;
5773 clear_buffer_p = 1;
5775 /* We need a fresh one in case the current echo buffer equals
5776 the one containing the last displayed echo area message. */
5777 if (!NILP (echo_area_buffer[this_one])
5778 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5779 echo_area_buffer[this_one] = Qnil;
5782 /* Choose a suitable buffer from echo_buffer[] is we don't
5783 have one. */
5784 if (NILP (echo_area_buffer[this_one]))
5786 echo_area_buffer[this_one]
5787 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5788 ? echo_buffer[the_other]
5789 : echo_buffer[this_one]);
5790 clear_buffer_p = 1;
5793 buffer = echo_area_buffer[this_one];
5795 record_unwind_protect (unwind_with_echo_area_buffer,
5796 with_echo_area_buffer_unwind_data (w));
5798 /* Make the echo area buffer current. Note that for display
5799 purposes, it is not necessary that the displayed window's buffer
5800 == current_buffer, except for text property lookup. So, let's
5801 only set that buffer temporarily here without doing a full
5802 Fset_window_buffer. We must also change w->pointm, though,
5803 because otherwise an assertions in unshow_buffer fails, and Emacs
5804 aborts. */
5805 set_buffer_internal_1 (XBUFFER (buffer));
5806 if (w)
5808 w->buffer = buffer;
5809 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5812 current_buffer->undo_list = Qt;
5813 current_buffer->read_only = Qnil;
5815 if (clear_buffer_p && Z > BEG)
5816 del_range (BEG, Z);
5818 xassert (BEGV >= BEG);
5819 xassert (ZV <= Z && ZV >= BEGV);
5821 rc = fn (a1, a2, a3, a4);
5823 xassert (BEGV >= BEG);
5824 xassert (ZV <= Z && ZV >= BEGV);
5826 unbind_to (count, Qnil);
5827 return rc;
5831 /* Save state that should be preserved around the call to the function
5832 FN called in with_echo_area_buffer. */
5834 static Lisp_Object
5835 with_echo_area_buffer_unwind_data (w)
5836 struct window *w;
5838 int i = 0;
5839 Lisp_Object vector;
5841 /* Reduce consing by keeping one vector in
5842 Vwith_echo_area_save_vector. */
5843 vector = Vwith_echo_area_save_vector;
5844 Vwith_echo_area_save_vector = Qnil;
5846 if (NILP (vector))
5847 vector = Fmake_vector (make_number (7), Qnil);
5849 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5850 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5851 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5853 if (w)
5855 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5856 XVECTOR (vector)->contents[i++] = w->buffer;
5857 XVECTOR (vector)->contents[i++]
5858 = make_number (XMARKER (w->pointm)->charpos);
5859 XVECTOR (vector)->contents[i++]
5860 = make_number (XMARKER (w->pointm)->bytepos);
5862 else
5864 int end = i + 4;
5865 while (i < end)
5866 XVECTOR (vector)->contents[i++] = Qnil;
5869 xassert (i == XVECTOR (vector)->size);
5870 return vector;
5874 /* Restore global state from VECTOR which was created by
5875 with_echo_area_buffer_unwind_data. */
5877 static Lisp_Object
5878 unwind_with_echo_area_buffer (vector)
5879 Lisp_Object vector;
5881 int i = 0;
5883 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5884 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5885 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5887 if (WINDOWP (XVECTOR (vector)->contents[i]))
5889 struct window *w;
5890 Lisp_Object buffer, charpos, bytepos;
5892 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5893 buffer = XVECTOR (vector)->contents[i]; ++i;
5894 charpos = XVECTOR (vector)->contents[i]; ++i;
5895 bytepos = XVECTOR (vector)->contents[i]; ++i;
5897 w->buffer = buffer;
5898 set_marker_both (w->pointm, buffer,
5899 XFASTINT (charpos), XFASTINT (bytepos));
5902 Vwith_echo_area_save_vector = vector;
5903 return Qnil;
5907 /* Set up the echo area for use by print functions. MULTIBYTE_P
5908 non-zero means we will print multibyte. */
5910 void
5911 setup_echo_area_for_printing (multibyte_p)
5912 int multibyte_p;
5914 ensure_echo_area_buffers ();
5916 if (!message_buf_print)
5918 /* A message has been output since the last time we printed.
5919 Choose a fresh echo area buffer. */
5920 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5921 echo_area_buffer[0] = echo_buffer[1];
5922 else
5923 echo_area_buffer[0] = echo_buffer[0];
5925 /* Switch to that buffer and clear it. */
5926 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5927 if (Z > BEG)
5928 del_range (BEG, Z);
5929 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5931 /* Set up the buffer for the multibyteness we need. */
5932 if (multibyte_p
5933 != !NILP (current_buffer->enable_multibyte_characters))
5934 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5936 /* Raise the frame containing the echo area. */
5937 if (minibuffer_auto_raise)
5939 struct frame *sf = SELECTED_FRAME ();
5940 Lisp_Object mini_window;
5941 mini_window = FRAME_MINIBUF_WINDOW (sf);
5942 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5945 message_log_maybe_newline ();
5946 message_buf_print = 1;
5948 else
5950 if (NILP (echo_area_buffer[0]))
5952 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5953 echo_area_buffer[0] = echo_buffer[1];
5954 else
5955 echo_area_buffer[0] = echo_buffer[0];
5958 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5959 /* Someone switched buffers between print requests. */
5960 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5965 /* Display an echo area message in window W. Value is non-zero if W's
5966 height is changed. If display_last_displayed_message_p is
5967 non-zero, display the message that was last displayed, otherwise
5968 display the current message. */
5970 static int
5971 display_echo_area (w)
5972 struct window *w;
5974 int i, no_message_p, window_height_changed_p, count;
5976 /* Temporarily disable garbage collections while displaying the echo
5977 area. This is done because a GC can print a message itself.
5978 That message would modify the echo area buffer's contents while a
5979 redisplay of the buffer is going on, and seriously confuse
5980 redisplay. */
5981 count = inhibit_garbage_collection ();
5983 /* If there is no message, we must call display_echo_area_1
5984 nevertheless because it resizes the window. But we will have to
5985 reset the echo_area_buffer in question to nil at the end because
5986 with_echo_area_buffer will sets it to an empty buffer. */
5987 i = display_last_displayed_message_p ? 1 : 0;
5988 no_message_p = NILP (echo_area_buffer[i]);
5990 window_height_changed_p
5991 = with_echo_area_buffer (w, display_last_displayed_message_p,
5992 display_echo_area_1,
5993 (EMACS_INT) w, Qnil, 0, 0);
5995 if (no_message_p)
5996 echo_area_buffer[i] = Qnil;
5998 unbind_to (count, Qnil);
5999 return window_height_changed_p;
6003 /* Helper for display_echo_area. Display the current buffer which
6004 contains the current echo area message in window W, a mini-window,
6005 a pointer to which is passed in A1. A2..A4 are currently not used.
6006 Change the height of W so that all of the message is displayed.
6007 Value is non-zero if height of W was changed. */
6009 static int
6010 display_echo_area_1 (a1, a2, a3, a4)
6011 EMACS_INT a1;
6012 Lisp_Object a2;
6013 EMACS_INT a3, a4;
6015 struct window *w = (struct window *) a1;
6016 Lisp_Object window;
6017 struct text_pos start;
6018 int window_height_changed_p = 0;
6020 /* Do this before displaying, so that we have a large enough glyph
6021 matrix for the display. */
6022 window_height_changed_p = resize_mini_window (w, 0);
6024 /* Display. */
6025 clear_glyph_matrix (w->desired_matrix);
6026 XSETWINDOW (window, w);
6027 SET_TEXT_POS (start, BEG, BEG_BYTE);
6028 try_window (window, start);
6030 return window_height_changed_p;
6034 /* Resize the echo area window to exactly the size needed for the
6035 currently displayed message, if there is one. */
6037 void
6038 resize_echo_area_axactly ()
6040 if (BUFFERP (echo_area_buffer[0])
6041 && WINDOWP (echo_area_window))
6043 struct window *w = XWINDOW (echo_area_window);
6044 int resized_p;
6046 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6047 (EMACS_INT) w, Qnil, 0, 0);
6048 if (resized_p)
6050 ++windows_or_buffers_changed;
6051 ++update_mode_lines;
6052 redisplay_internal (0);
6058 /* Callback function for with_echo_area_buffer, when used from
6059 resize_echo_area_axactly. A1 contains a pointer to the window to
6060 resize, A2 to A4 are not used. Value is what resize_mini_window
6061 returns. */
6063 static int
6064 resize_mini_window_1 (a1, a2, a3, a4)
6065 EMACS_INT a1;
6066 Lisp_Object a2;
6067 EMACS_INT a3, a4;
6069 return resize_mini_window ((struct window *) a1, 1);
6073 /* Resize mini-window W to fit the size of its contents. EXACT:P
6074 means size the window exactly to the size needed. Otherwise, it's
6075 only enlarged until W's buffer is empty. Value is non-zero if
6076 the window height has been changed. */
6079 resize_mini_window (w, exact_p)
6080 struct window *w;
6081 int exact_p;
6083 struct frame *f = XFRAME (w->frame);
6084 int window_height_changed_p = 0;
6086 xassert (MINI_WINDOW_P (w));
6088 /* Nil means don't try to resize. */
6089 if (NILP (Vresize_mini_windows)
6090 || (FRAME_X_P (f) && f->output_data.x == NULL))
6091 return 0;
6093 if (!FRAME_MINIBUF_ONLY_P (f))
6095 struct it it;
6096 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6097 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6098 int height, max_height;
6099 int unit = CANON_Y_UNIT (f);
6100 struct text_pos start;
6102 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6104 /* Compute the max. number of lines specified by the user. */
6105 if (FLOATP (Vmax_mini_window_height))
6106 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6107 else if (INTEGERP (Vmax_mini_window_height))
6108 max_height = XINT (Vmax_mini_window_height);
6109 else
6110 max_height = total_height / 4;
6112 /* Correct that max. height if it's bogus. */
6113 max_height = max (1, max_height);
6114 max_height = min (total_height, max_height);
6116 /* Find out the height of the text in the window. */
6117 if (it.truncate_lines_p)
6118 height = 1;
6119 else
6121 last_height = 0;
6122 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6123 if (it.max_ascent == 0 && it.max_descent == 0)
6124 height = it.current_y + last_height;
6125 else
6126 height = it.current_y + it.max_ascent + it.max_descent;
6127 height -= it.extra_line_spacing;
6128 height = (height + unit - 1) / unit;
6131 /* Compute a suitable window start. */
6132 if (height > max_height)
6134 height = max_height;
6135 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6136 move_it_vertically_backward (&it, (height - 1) * unit);
6137 start = it.current.pos;
6139 else
6140 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6141 SET_MARKER_FROM_TEXT_POS (w->start, start);
6143 if (EQ (Vresize_mini_windows, Qgrow_only))
6145 /* Let it grow only, until we display an empty message, in which
6146 case the window shrinks again. */
6147 if (height > XFASTINT (w->height))
6149 int old_height = XFASTINT (w->height);
6150 freeze_window_starts (f, 1);
6151 grow_mini_window (w, height - XFASTINT (w->height));
6152 window_height_changed_p = XFASTINT (w->height) != old_height;
6154 else if (height < XFASTINT (w->height)
6155 && (exact_p || BEGV == ZV))
6157 int old_height = XFASTINT (w->height);
6158 freeze_window_starts (f, 0);
6159 shrink_mini_window (w);
6160 window_height_changed_p = XFASTINT (w->height) != old_height;
6163 else
6165 /* Always resize to exact size needed. */
6166 if (height > XFASTINT (w->height))
6168 int old_height = XFASTINT (w->height);
6169 freeze_window_starts (f, 1);
6170 grow_mini_window (w, height - XFASTINT (w->height));
6171 window_height_changed_p = XFASTINT (w->height) != old_height;
6173 else if (height < XFASTINT (w->height))
6175 int old_height = XFASTINT (w->height);
6176 freeze_window_starts (f, 0);
6177 shrink_mini_window (w);
6179 if (height)
6181 freeze_window_starts (f, 1);
6182 grow_mini_window (w, height - XFASTINT (w->height));
6185 window_height_changed_p = XFASTINT (w->height) != old_height;
6190 return window_height_changed_p;
6194 /* Value is the current message, a string, or nil if there is no
6195 current message. */
6197 Lisp_Object
6198 current_message ()
6200 Lisp_Object msg;
6202 if (NILP (echo_area_buffer[0]))
6203 msg = Qnil;
6204 else
6206 with_echo_area_buffer (0, 0, current_message_1,
6207 (EMACS_INT) &msg, Qnil, 0, 0);
6208 if (NILP (msg))
6209 echo_area_buffer[0] = Qnil;
6212 return msg;
6216 static int
6217 current_message_1 (a1, a2, a3, a4)
6218 EMACS_INT a1;
6219 Lisp_Object a2;
6220 EMACS_INT a3, a4;
6222 Lisp_Object *msg = (Lisp_Object *) a1;
6224 if (Z > BEG)
6225 *msg = make_buffer_string (BEG, Z, 1);
6226 else
6227 *msg = Qnil;
6228 return 0;
6232 /* Push the current message on Vmessage_stack for later restauration
6233 by restore_message. Value is non-zero if the current message isn't
6234 empty. This is a relatively infrequent operation, so it's not
6235 worth optimizing. */
6238 push_message ()
6240 Lisp_Object msg;
6241 msg = current_message ();
6242 Vmessage_stack = Fcons (msg, Vmessage_stack);
6243 return STRINGP (msg);
6247 /* Restore message display from the top of Vmessage_stack. */
6249 void
6250 restore_message ()
6252 Lisp_Object msg;
6254 xassert (CONSP (Vmessage_stack));
6255 msg = XCAR (Vmessage_stack);
6256 if (STRINGP (msg))
6257 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6258 else
6259 message3_nolog (msg, 0, 0);
6263 /* Pop the top-most entry off Vmessage_stack. */
6265 void
6266 pop_message ()
6268 xassert (CONSP (Vmessage_stack));
6269 Vmessage_stack = XCDR (Vmessage_stack);
6273 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6274 exits. If the stack is not empty, we have a missing pop_message
6275 somewhere. */
6277 void
6278 check_message_stack ()
6280 if (!NILP (Vmessage_stack))
6281 abort ();
6285 /* Truncate to NCHARS what will be displayed in the echo area the next
6286 time we display it---but don't redisplay it now. */
6288 void
6289 truncate_echo_area (nchars)
6290 int nchars;
6292 if (nchars == 0)
6293 echo_area_buffer[0] = Qnil;
6294 /* A null message buffer means that the frame hasn't really been
6295 initialized yet. Error messages get reported properly by
6296 cmd_error, so this must be just an informative message; toss it. */
6297 else if (!noninteractive
6298 && INTERACTIVE
6299 && !NILP (echo_area_buffer[0]))
6301 struct frame *sf = SELECTED_FRAME ();
6302 if (FRAME_MESSAGE_BUF (sf))
6303 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6308 /* Helper function for truncate_echo_area. Truncate the current
6309 message to at most NCHARS characters. */
6311 static int
6312 truncate_message_1 (nchars, a2, a3, a4)
6313 EMACS_INT nchars;
6314 Lisp_Object a2;
6315 EMACS_INT a3, a4;
6317 if (BEG + nchars < Z)
6318 del_range (BEG + nchars, Z);
6319 if (Z == BEG)
6320 echo_area_buffer[0] = Qnil;
6321 return 0;
6325 /* Set the current message to a substring of S or STRING.
6327 If STRING is a Lisp string, set the message to the first NBYTES
6328 bytes from STRING. NBYTES zero means use the whole string. If
6329 STRING is multibyte, the message will be displayed multibyte.
6331 If S is not null, set the message to the first LEN bytes of S. LEN
6332 zero means use the whole string. MULTIBYTE_P non-zero means S is
6333 multibyte. Display the message multibyte in that case. */
6335 void
6336 set_message (s, string, nbytes, multibyte_p)
6337 char *s;
6338 Lisp_Object string;
6339 int nbytes;
6341 message_enable_multibyte
6342 = ((s && multibyte_p)
6343 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6345 with_echo_area_buffer (0, -1, set_message_1,
6346 (EMACS_INT) s, string, nbytes, multibyte_p);
6347 message_buf_print = 0;
6348 help_echo_showing_p = 0;
6352 /* Helper function for set_message. Arguments have the same meaning
6353 as there, with A1 corresponding to S and A2 corresponding to STRING
6354 This function is called with the echo area buffer being
6355 current. */
6357 static int
6358 set_message_1 (a1, a2, nbytes, multibyte_p)
6359 EMACS_INT a1;
6360 Lisp_Object a2;
6361 EMACS_INT nbytes, multibyte_p;
6363 char *s = (char *) a1;
6364 Lisp_Object string = a2;
6366 xassert (BEG == Z);
6368 /* Change multibyteness of the echo buffer appropriately. */
6369 if (message_enable_multibyte
6370 != !NILP (current_buffer->enable_multibyte_characters))
6371 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6373 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6375 /* Insert new message at BEG. */
6376 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6378 if (STRINGP (string))
6380 int nchars;
6382 if (nbytes == 0)
6383 nbytes = XSTRING (string)->size_byte;
6384 nchars = string_byte_to_char (string, nbytes);
6386 /* This function takes care of single/multibyte conversion. We
6387 just have to ensure that the echo area buffer has the right
6388 setting of enable_multibyte_characters. */
6389 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6391 else if (s)
6393 if (nbytes == 0)
6394 nbytes = strlen (s);
6396 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6398 /* Convert from multi-byte to single-byte. */
6399 int i, c, n;
6400 unsigned char work[1];
6402 /* Convert a multibyte string to single-byte. */
6403 for (i = 0; i < nbytes; i += n)
6405 c = string_char_and_length (s + i, nbytes - i, &n);
6406 work[0] = (SINGLE_BYTE_CHAR_P (c)
6408 : multibyte_char_to_unibyte (c, Qnil));
6409 insert_1_both (work, 1, 1, 1, 0, 0);
6412 else if (!multibyte_p
6413 && !NILP (current_buffer->enable_multibyte_characters))
6415 /* Convert from single-byte to multi-byte. */
6416 int i, c, n;
6417 unsigned char *msg = (unsigned char *) s;
6418 unsigned char str[MAX_MULTIBYTE_LENGTH];
6420 /* Convert a single-byte string to multibyte. */
6421 for (i = 0; i < nbytes; i++)
6423 c = unibyte_char_to_multibyte (msg[i]);
6424 n = CHAR_STRING (c, str);
6425 insert_1_both (str, 1, n, 1, 0, 0);
6428 else
6429 insert_1 (s, nbytes, 1, 0, 0);
6432 return 0;
6436 /* Clear messages. CURRENT_P non-zero means clear the current
6437 message. LAST_DISPLAYED_P non-zero means clear the message
6438 last displayed. */
6440 void
6441 clear_message (current_p, last_displayed_p)
6442 int current_p, last_displayed_p;
6444 if (current_p)
6445 echo_area_buffer[0] = Qnil;
6447 if (last_displayed_p)
6448 echo_area_buffer[1] = Qnil;
6450 message_buf_print = 0;
6453 /* Clear garbaged frames.
6455 This function is used where the old redisplay called
6456 redraw_garbaged_frames which in turn called redraw_frame which in
6457 turn called clear_frame. The call to clear_frame was a source of
6458 flickering. I believe a clear_frame is not necessary. It should
6459 suffice in the new redisplay to invalidate all current matrices,
6460 and ensure a complete redisplay of all windows. */
6462 static void
6463 clear_garbaged_frames ()
6465 if (frame_garbaged)
6467 Lisp_Object tail, frame;
6469 FOR_EACH_FRAME (tail, frame)
6471 struct frame *f = XFRAME (frame);
6473 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6475 clear_current_matrices (f);
6476 f->garbaged = 0;
6480 frame_garbaged = 0;
6481 ++windows_or_buffers_changed;
6486 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6487 is non-zero update selected_frame. Value is non-zero if the
6488 mini-windows height has been changed. */
6490 static int
6491 echo_area_display (update_frame_p)
6492 int update_frame_p;
6494 Lisp_Object mini_window;
6495 struct window *w;
6496 struct frame *f;
6497 int window_height_changed_p = 0;
6498 struct frame *sf = SELECTED_FRAME ();
6500 mini_window = FRAME_MINIBUF_WINDOW (sf);
6501 w = XWINDOW (mini_window);
6502 f = XFRAME (WINDOW_FRAME (w));
6504 /* Don't display if frame is invisible or not yet initialized. */
6505 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6506 return 0;
6508 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6509 #ifndef macintosh
6510 #ifdef HAVE_WINDOW_SYSTEM
6511 /* When Emacs starts, selected_frame may be a visible terminal
6512 frame, even if we run under a window system. If we let this
6513 through, a message would be displayed on the terminal. */
6514 if (EQ (selected_frame, Vterminal_frame)
6515 && !NILP (Vwindow_system))
6516 return 0;
6517 #endif /* HAVE_WINDOW_SYSTEM */
6518 #endif
6520 /* Redraw garbaged frames. */
6521 if (frame_garbaged)
6522 clear_garbaged_frames ();
6524 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6526 echo_area_window = mini_window;
6527 window_height_changed_p = display_echo_area (w);
6528 w->must_be_updated_p = 1;
6530 /* Update the display, unless called from redisplay_internal.
6531 Also don't update the screen during redisplay itself. The
6532 update will happen at the end of redisplay, and an update
6533 here could cause confusion. */
6534 if (update_frame_p && !redisplaying_p)
6536 int n = 0;
6538 /* If the display update has been interrupted by pending
6539 input, update mode lines in the frame. Due to the
6540 pending input, it might have been that redisplay hasn't
6541 been called, so that mode lines above the echo area are
6542 garbaged. This looks odd, so we prevent it here. */
6543 if (!display_completed)
6544 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6546 if (window_height_changed_p)
6548 /* Must update other windows. */
6549 windows_or_buffers_changed = 1;
6550 redisplay_internal (0);
6552 else if (FRAME_WINDOW_P (f) && n == 0)
6554 /* Window configuration is the same as before.
6555 Can do with a display update of the echo area,
6556 unless we displayed some mode lines. */
6557 update_single_window (w, 1);
6558 rif->flush_display (f);
6560 else
6561 update_frame (f, 1, 1);
6564 else if (!EQ (mini_window, selected_window))
6565 windows_or_buffers_changed++;
6567 /* Last displayed message is now the current message. */
6568 echo_area_buffer[1] = echo_area_buffer[0];
6570 /* Prevent redisplay optimization in redisplay_internal by resetting
6571 this_line_start_pos. This is done because the mini-buffer now
6572 displays the message instead of its buffer text. */
6573 if (EQ (mini_window, selected_window))
6574 CHARPOS (this_line_start_pos) = 0;
6576 return window_height_changed_p;
6581 /***********************************************************************
6582 Frame Titles
6583 ***********************************************************************/
6586 #ifdef HAVE_WINDOW_SYSTEM
6588 /* A buffer for constructing frame titles in it; allocated from the
6589 heap in init_xdisp and resized as needed in store_frame_title_char. */
6591 static char *frame_title_buf;
6593 /* The buffer's end, and a current output position in it. */
6595 static char *frame_title_buf_end;
6596 static char *frame_title_ptr;
6599 /* Store a single character C for the frame title in frame_title_buf.
6600 Re-allocate frame_title_buf if necessary. */
6602 static void
6603 store_frame_title_char (c)
6604 char c;
6606 /* If output position has reached the end of the allocated buffer,
6607 double the buffer's size. */
6608 if (frame_title_ptr == frame_title_buf_end)
6610 int len = frame_title_ptr - frame_title_buf;
6611 int new_size = 2 * len * sizeof *frame_title_buf;
6612 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6613 frame_title_buf_end = frame_title_buf + new_size;
6614 frame_title_ptr = frame_title_buf + len;
6617 *frame_title_ptr++ = c;
6621 /* Store part of a frame title in frame_title_buf, beginning at
6622 frame_title_ptr. STR is the string to store. Do not copy more
6623 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6624 the whole string. Pad with spaces until FIELD_WIDTH number of
6625 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6626 Called from display_mode_element when it is used to build a frame
6627 title. */
6629 static int
6630 store_frame_title (str, field_width, precision)
6631 unsigned char *str;
6632 int field_width, precision;
6634 int n = 0;
6636 /* Copy at most PRECISION chars from STR. */
6637 while ((precision <= 0 || n < precision)
6638 && *str)
6640 store_frame_title_char (*str++);
6641 ++n;
6644 /* Fill up with spaces until FIELD_WIDTH reached. */
6645 while (field_width > 0
6646 && n < field_width)
6648 store_frame_title_char (' ');
6649 ++n;
6652 return n;
6656 /* Set the title of FRAME, if it has changed. The title format is
6657 Vicon_title_format if FRAME is iconified, otherwise it is
6658 frame_title_format. */
6660 static void
6661 x_consider_frame_title (frame)
6662 Lisp_Object frame;
6664 struct frame *f = XFRAME (frame);
6666 if (FRAME_WINDOW_P (f)
6667 || FRAME_MINIBUF_ONLY_P (f)
6668 || f->explicit_name)
6670 /* Do we have more than one visible frame on this X display? */
6671 Lisp_Object tail;
6672 Lisp_Object fmt;
6673 struct buffer *obuf;
6674 int len;
6675 struct it it;
6677 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6679 struct frame *tf = XFRAME (XCAR (tail));
6681 if (tf != f
6682 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6683 && !FRAME_MINIBUF_ONLY_P (tf)
6684 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6685 break;
6688 /* Set global variable indicating that multiple frames exist. */
6689 multiple_frames = CONSP (tail);
6691 /* Switch to the buffer of selected window of the frame. Set up
6692 frame_title_ptr so that display_mode_element will output into it;
6693 then display the title. */
6694 obuf = current_buffer;
6695 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6696 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6697 frame_title_ptr = frame_title_buf;
6698 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6699 NULL, DEFAULT_FACE_ID);
6700 len = display_mode_element (&it, 0, -1, -1, fmt);
6701 frame_title_ptr = NULL;
6702 set_buffer_internal (obuf);
6704 /* Set the title only if it's changed. This avoids consing in
6705 the common case where it hasn't. (If it turns out that we've
6706 already wasted too much time by walking through the list with
6707 display_mode_element, then we might need to optimize at a
6708 higher level than this.) */
6709 if (! STRINGP (f->name)
6710 || STRING_BYTES (XSTRING (f->name)) != len
6711 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6712 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6716 #else /* not HAVE_WINDOW_SYSTEM */
6718 #define frame_title_ptr ((char *)0)
6719 #define store_frame_title(str, mincol, maxcol) 0
6721 #endif /* not HAVE_WINDOW_SYSTEM */
6726 /***********************************************************************
6727 Menu Bars
6728 ***********************************************************************/
6731 /* Prepare for redisplay by updating menu-bar item lists when
6732 appropriate. This can call eval. */
6734 void
6735 prepare_menu_bars ()
6737 int all_windows;
6738 struct gcpro gcpro1, gcpro2;
6739 struct frame *f;
6740 struct frame *tooltip_frame;
6742 #ifdef HAVE_X_WINDOWS
6743 tooltip_frame = tip_frame;
6744 #else
6745 tooltip_frame = NULL;
6746 #endif
6748 /* Update all frame titles based on their buffer names, etc. We do
6749 this before the menu bars so that the buffer-menu will show the
6750 up-to-date frame titles. */
6751 #ifdef HAVE_WINDOW_SYSTEM
6752 if (windows_or_buffers_changed || update_mode_lines)
6754 Lisp_Object tail, frame;
6756 FOR_EACH_FRAME (tail, frame)
6758 f = XFRAME (frame);
6759 if (f != tooltip_frame
6760 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6761 x_consider_frame_title (frame);
6764 #endif /* HAVE_WINDOW_SYSTEM */
6766 /* Update the menu bar item lists, if appropriate. This has to be
6767 done before any actual redisplay or generation of display lines. */
6768 all_windows = (update_mode_lines
6769 || buffer_shared > 1
6770 || windows_or_buffers_changed);
6771 if (all_windows)
6773 Lisp_Object tail, frame;
6774 int count = specpdl_ptr - specpdl;
6776 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6778 FOR_EACH_FRAME (tail, frame)
6780 f = XFRAME (frame);
6782 /* Ignore tooltip frame. */
6783 if (f == tooltip_frame)
6784 continue;
6786 /* If a window on this frame changed size, report that to
6787 the user and clear the size-change flag. */
6788 if (FRAME_WINDOW_SIZES_CHANGED (f))
6790 Lisp_Object functions;
6792 /* Clear flag first in case we get an error below. */
6793 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6794 functions = Vwindow_size_change_functions;
6795 GCPRO2 (tail, functions);
6797 while (CONSP (functions))
6799 call1 (XCAR (functions), frame);
6800 functions = XCDR (functions);
6802 UNGCPRO;
6805 GCPRO1 (tail);
6806 update_menu_bar (f, 0);
6807 #ifdef HAVE_WINDOW_SYSTEM
6808 update_tool_bar (f, 0);
6809 #endif
6810 UNGCPRO;
6813 unbind_to (count, Qnil);
6815 else
6817 struct frame *sf = SELECTED_FRAME ();
6818 update_menu_bar (sf, 1);
6819 #ifdef HAVE_WINDOW_SYSTEM
6820 update_tool_bar (sf, 1);
6821 #endif
6824 /* Motif needs this. See comment in xmenu.c. Turn it off when
6825 pending_menu_activation is not defined. */
6826 #ifdef USE_X_TOOLKIT
6827 pending_menu_activation = 0;
6828 #endif
6832 /* Update the menu bar item list for frame F. This has to be done
6833 before we start to fill in any display lines, because it can call
6834 eval.
6836 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6838 static void
6839 update_menu_bar (f, save_match_data)
6840 struct frame *f;
6841 int save_match_data;
6843 Lisp_Object window;
6844 register struct window *w;
6846 window = FRAME_SELECTED_WINDOW (f);
6847 w = XWINDOW (window);
6849 if (update_mode_lines)
6850 w->update_mode_line = Qt;
6852 if (FRAME_WINDOW_P (f)
6854 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6855 FRAME_EXTERNAL_MENU_BAR (f)
6856 #else
6857 FRAME_MENU_BAR_LINES (f) > 0
6858 #endif
6859 : FRAME_MENU_BAR_LINES (f) > 0)
6861 /* If the user has switched buffers or windows, we need to
6862 recompute to reflect the new bindings. But we'll
6863 recompute when update_mode_lines is set too; that means
6864 that people can use force-mode-line-update to request
6865 that the menu bar be recomputed. The adverse effect on
6866 the rest of the redisplay algorithm is about the same as
6867 windows_or_buffers_changed anyway. */
6868 if (windows_or_buffers_changed
6869 || !NILP (w->update_mode_line)
6870 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6871 < BUF_MODIFF (XBUFFER (w->buffer)))
6872 != !NILP (w->last_had_star))
6873 || ((!NILP (Vtransient_mark_mode)
6874 && !NILP (XBUFFER (w->buffer)->mark_active))
6875 != !NILP (w->region_showing)))
6877 struct buffer *prev = current_buffer;
6878 int count = specpdl_ptr - specpdl;
6880 set_buffer_internal_1 (XBUFFER (w->buffer));
6881 if (save_match_data)
6882 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6883 if (NILP (Voverriding_local_map_menu_flag))
6885 specbind (Qoverriding_terminal_local_map, Qnil);
6886 specbind (Qoverriding_local_map, Qnil);
6889 /* Run the Lucid hook. */
6890 call1 (Vrun_hooks, Qactivate_menubar_hook);
6892 /* If it has changed current-menubar from previous value,
6893 really recompute the menu-bar from the value. */
6894 if (! NILP (Vlucid_menu_bar_dirty_flag))
6895 call0 (Qrecompute_lucid_menubar);
6897 safe_run_hooks (Qmenu_bar_update_hook);
6898 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6900 /* Redisplay the menu bar in case we changed it. */
6901 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6902 if (FRAME_WINDOW_P (f)
6903 #if defined (macintosh)
6904 /* All frames on Mac OS share the same menubar. So only the
6905 selected frame should be allowed to set it. */
6906 && f == SELECTED_FRAME ()
6907 #endif
6909 set_frame_menubar (f, 0, 0);
6910 else
6911 /* On a terminal screen, the menu bar is an ordinary screen
6912 line, and this makes it get updated. */
6913 w->update_mode_line = Qt;
6914 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6915 /* In the non-toolkit version, the menu bar is an ordinary screen
6916 line, and this makes it get updated. */
6917 w->update_mode_line = Qt;
6918 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6920 unbind_to (count, Qnil);
6921 set_buffer_internal_1 (prev);
6928 /***********************************************************************
6929 Tool-bars
6930 ***********************************************************************/
6932 #ifdef HAVE_WINDOW_SYSTEM
6934 /* Update the tool-bar item list for frame F. This has to be done
6935 before we start to fill in any display lines. Called from
6936 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6937 and restore it here. */
6939 static void
6940 update_tool_bar (f, save_match_data)
6941 struct frame *f;
6942 int save_match_data;
6944 if (WINDOWP (f->tool_bar_window)
6945 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6947 Lisp_Object window;
6948 struct window *w;
6950 window = FRAME_SELECTED_WINDOW (f);
6951 w = XWINDOW (window);
6953 /* If the user has switched buffers or windows, we need to
6954 recompute to reflect the new bindings. But we'll
6955 recompute when update_mode_lines is set too; that means
6956 that people can use force-mode-line-update to request
6957 that the menu bar be recomputed. The adverse effect on
6958 the rest of the redisplay algorithm is about the same as
6959 windows_or_buffers_changed anyway. */
6960 if (windows_or_buffers_changed
6961 || !NILP (w->update_mode_line)
6962 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6963 < BUF_MODIFF (XBUFFER (w->buffer)))
6964 != !NILP (w->last_had_star))
6965 || ((!NILP (Vtransient_mark_mode)
6966 && !NILP (XBUFFER (w->buffer)->mark_active))
6967 != !NILP (w->region_showing)))
6969 struct buffer *prev = current_buffer;
6970 int count = specpdl_ptr - specpdl;
6972 /* Set current_buffer to the buffer of the selected
6973 window of the frame, so that we get the right local
6974 keymaps. */
6975 set_buffer_internal_1 (XBUFFER (w->buffer));
6977 /* Save match data, if we must. */
6978 if (save_match_data)
6979 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6981 /* Make sure that we don't accidentally use bogus keymaps. */
6982 if (NILP (Voverriding_local_map_menu_flag))
6984 specbind (Qoverriding_terminal_local_map, Qnil);
6985 specbind (Qoverriding_local_map, Qnil);
6988 /* Build desired tool-bar items from keymaps. */
6989 f->desired_tool_bar_items
6990 = tool_bar_items (f->desired_tool_bar_items,
6991 &f->n_desired_tool_bar_items);
6993 /* Redisplay the tool-bar in case we changed it. */
6994 w->update_mode_line = Qt;
6996 unbind_to (count, Qnil);
6997 set_buffer_internal_1 (prev);
7003 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7004 F's desired tool-bar contents. F->desired_tool_bar_items must have
7005 been set up previously by calling prepare_menu_bars. */
7007 static void
7008 build_desired_tool_bar_string (f)
7009 struct frame *f;
7011 int i, size, size_needed, string_idx;
7012 struct gcpro gcpro1, gcpro2, gcpro3;
7013 Lisp_Object image, plist, props;
7015 image = plist = props = Qnil;
7016 GCPRO3 (image, plist, props);
7018 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7019 Otherwise, make a new string. */
7021 /* The size of the string we might be able to reuse. */
7022 size = (STRINGP (f->desired_tool_bar_string)
7023 ? XSTRING (f->desired_tool_bar_string)->size
7024 : 0);
7026 /* Each image in the string we build is preceded by a space,
7027 and there is a space at the end. */
7028 size_needed = f->n_desired_tool_bar_items + 1;
7030 /* Reuse f->desired_tool_bar_string, if possible. */
7031 if (size < size_needed)
7032 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7033 make_number (' '));
7034 else
7036 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7037 Fremove_text_properties (make_number (0), make_number (size),
7038 props, f->desired_tool_bar_string);
7041 /* Put a `display' property on the string for the images to display,
7042 put a `menu_item' property on tool-bar items with a value that
7043 is the index of the item in F's tool-bar item vector. */
7044 for (i = 0, string_idx = 0;
7045 i < f->n_desired_tool_bar_items;
7046 ++i, string_idx += 1)
7048 #define PROP(IDX) \
7049 (XVECTOR (f->desired_tool_bar_items) \
7050 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
7052 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7053 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7054 int margin, relief, idx;
7055 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7056 extern Lisp_Object Qlaplace;
7058 /* If image is a vector, choose the image according to the
7059 button state. */
7060 image = PROP (TOOL_BAR_ITEM_IMAGES);
7061 if (VECTORP (image))
7063 if (enabled_p)
7064 idx = (selected_p
7065 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7066 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7067 else
7068 idx = (selected_p
7069 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7070 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7072 xassert (ASIZE (image) >= idx);
7073 image = AREF (image, idx);
7075 else
7076 idx = -1;
7078 /* Ignore invalid image specifications. */
7079 if (!valid_image_p (image))
7080 continue;
7082 /* Display the tool-bar button pressed, or depressed. */
7083 plist = Fcopy_sequence (XCDR (image));
7085 /* Compute margin and relief to draw. */
7086 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7087 margin = relief + max (0, tool_bar_button_margin);
7089 if (auto_raise_tool_bar_buttons_p)
7091 /* Add a `:relief' property to the image spec if the item is
7092 selected. */
7093 if (selected_p)
7095 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7096 margin -= relief;
7099 else
7101 /* If image is selected, display it pressed, i.e. with a
7102 negative relief. If it's not selected, display it with a
7103 raised relief. */
7104 plist = Fplist_put (plist, QCrelief,
7105 (selected_p
7106 ? make_number (-relief)
7107 : make_number (relief)));
7108 margin -= relief;
7111 /* Put a margin around the image. */
7112 if (margin)
7113 plist = Fplist_put (plist, QCmargin, make_number (margin));
7115 /* If button is not enabled, and we don't have special images
7116 for the disabled state, make the image appear disabled by
7117 applying an appropriate algorithm to it. */
7118 if (!enabled_p && idx < 0)
7119 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7121 /* Put a `display' text property on the string for the image to
7122 display. Put a `menu-item' property on the string that gives
7123 the start of this item's properties in the tool-bar items
7124 vector. */
7125 image = Fcons (Qimage, plist);
7126 props = list4 (Qdisplay, image,
7127 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7128 Fadd_text_properties (make_number (string_idx),
7129 make_number (string_idx + 1),
7130 props, f->desired_tool_bar_string);
7131 #undef PROP
7134 UNGCPRO;
7138 /* Display one line of the tool-bar of frame IT->f. */
7140 static void
7141 display_tool_bar_line (it)
7142 struct it *it;
7144 struct glyph_row *row = it->glyph_row;
7145 int max_x = it->last_visible_x;
7146 struct glyph *last;
7148 prepare_desired_row (row);
7149 row->y = it->current_y;
7151 while (it->current_x < max_x)
7153 int x_before, x, n_glyphs_before, i, nglyphs;
7155 /* Get the next display element. */
7156 if (!get_next_display_element (it))
7157 break;
7159 /* Produce glyphs. */
7160 x_before = it->current_x;
7161 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7162 PRODUCE_GLYPHS (it);
7164 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7165 i = 0;
7166 x = x_before;
7167 while (i < nglyphs)
7169 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7171 if (x + glyph->pixel_width > max_x)
7173 /* Glyph doesn't fit on line. */
7174 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7175 it->current_x = x;
7176 goto out;
7179 ++it->hpos;
7180 x += glyph->pixel_width;
7181 ++i;
7184 /* Stop at line ends. */
7185 if (ITERATOR_AT_END_OF_LINE_P (it))
7186 break;
7188 set_iterator_to_next (it, 1);
7191 out:;
7193 row->displays_text_p = row->used[TEXT_AREA] != 0;
7194 extend_face_to_end_of_line (it);
7195 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7196 last->right_box_line_p = 1;
7197 compute_line_metrics (it);
7199 /* If line is empty, make it occupy the rest of the tool-bar. */
7200 if (!row->displays_text_p)
7202 row->height = row->phys_height = it->last_visible_y - row->y;
7203 row->ascent = row->phys_ascent = 0;
7206 row->full_width_p = 1;
7207 row->continued_p = 0;
7208 row->truncated_on_left_p = 0;
7209 row->truncated_on_right_p = 0;
7211 it->current_x = it->hpos = 0;
7212 it->current_y += row->height;
7213 ++it->vpos;
7214 ++it->glyph_row;
7218 /* Value is the number of screen lines needed to make all tool-bar
7219 items of frame F visible. */
7221 static int
7222 tool_bar_lines_needed (f)
7223 struct frame *f;
7225 struct window *w = XWINDOW (f->tool_bar_window);
7226 struct it it;
7228 /* Initialize an iterator for iteration over
7229 F->desired_tool_bar_string in the tool-bar window of frame F. */
7230 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7231 it.first_visible_x = 0;
7232 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7233 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7235 while (!ITERATOR_AT_END_P (&it))
7237 it.glyph_row = w->desired_matrix->rows;
7238 clear_glyph_row (it.glyph_row);
7239 display_tool_bar_line (&it);
7242 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7246 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7247 height should be changed. */
7249 static int
7250 redisplay_tool_bar (f)
7251 struct frame *f;
7253 struct window *w;
7254 struct it it;
7255 struct glyph_row *row;
7256 int change_height_p = 0;
7258 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7259 do anything. This means you must start with tool-bar-lines
7260 non-zero to get the auto-sizing effect. Or in other words, you
7261 can turn off tool-bars by specifying tool-bar-lines zero. */
7262 if (!WINDOWP (f->tool_bar_window)
7263 || (w = XWINDOW (f->tool_bar_window),
7264 XFASTINT (w->height) == 0))
7265 return 0;
7267 /* Set up an iterator for the tool-bar window. */
7268 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7269 it.first_visible_x = 0;
7270 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7271 row = it.glyph_row;
7273 /* Build a string that represents the contents of the tool-bar. */
7274 build_desired_tool_bar_string (f);
7275 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7277 /* Display as many lines as needed to display all tool-bar items. */
7278 while (it.current_y < it.last_visible_y)
7279 display_tool_bar_line (&it);
7281 /* It doesn't make much sense to try scrolling in the tool-bar
7282 window, so don't do it. */
7283 w->desired_matrix->no_scrolling_p = 1;
7284 w->must_be_updated_p = 1;
7286 if (auto_resize_tool_bars_p)
7288 int nlines;
7290 /* If there are blank lines at the end, except for a partially
7291 visible blank line at the end that is smaller than
7292 CANON_Y_UNIT, change the tool-bar's height. */
7293 row = it.glyph_row - 1;
7294 if (!row->displays_text_p
7295 && row->height >= CANON_Y_UNIT (f))
7296 change_height_p = 1;
7298 /* If row displays tool-bar items, but is partially visible,
7299 change the tool-bar's height. */
7300 if (row->displays_text_p
7301 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7302 change_height_p = 1;
7304 /* Resize windows as needed by changing the `tool-bar-lines'
7305 frame parameter. */
7306 if (change_height_p
7307 && (nlines = tool_bar_lines_needed (f),
7308 nlines != XFASTINT (w->height)))
7310 extern Lisp_Object Qtool_bar_lines;
7311 Lisp_Object frame;
7312 int old_height = XFASTINT (w->height);
7314 XSETFRAME (frame, f);
7315 clear_glyph_matrix (w->desired_matrix);
7316 Fmodify_frame_parameters (frame,
7317 Fcons (Fcons (Qtool_bar_lines,
7318 make_number (nlines)),
7319 Qnil));
7320 if (XFASTINT (w->height) != old_height)
7321 fonts_changed_p = 1;
7325 return change_height_p;
7329 /* Get information about the tool-bar item which is displayed in GLYPH
7330 on frame F. Return in *PROP_IDX the index where tool-bar item
7331 properties start in F->current_tool_bar_items. Value is zero if
7332 GLYPH doesn't display a tool-bar item. */
7335 tool_bar_item_info (f, glyph, prop_idx)
7336 struct frame *f;
7337 struct glyph *glyph;
7338 int *prop_idx;
7340 Lisp_Object prop;
7341 int success_p;
7343 /* Get the text property `menu-item' at pos. The value of that
7344 property is the start index of this item's properties in
7345 F->current_tool_bar_items. */
7346 prop = Fget_text_property (make_number (glyph->charpos),
7347 Qmenu_item, f->current_tool_bar_string);
7348 if (INTEGERP (prop))
7350 *prop_idx = XINT (prop);
7351 success_p = 1;
7353 else
7354 success_p = 0;
7356 return success_p;
7359 #endif /* HAVE_WINDOW_SYSTEM */
7363 /************************************************************************
7364 Horizontal scrolling
7365 ************************************************************************/
7367 static int hscroll_window_tree P_ ((Lisp_Object));
7368 static int hscroll_windows P_ ((Lisp_Object));
7370 /* For all leaf windows in the window tree rooted at WINDOW, set their
7371 hscroll value so that PT is (i) visible in the window, and (ii) so
7372 that it is not within a certain margin at the window's left and
7373 right border. Value is non-zero if any window's hscroll has been
7374 changed. */
7376 static int
7377 hscroll_window_tree (window)
7378 Lisp_Object window;
7380 int hscrolled_p = 0;
7382 while (WINDOWP (window))
7384 struct window *w = XWINDOW (window);
7386 if (WINDOWP (w->hchild))
7387 hscrolled_p |= hscroll_window_tree (w->hchild);
7388 else if (WINDOWP (w->vchild))
7389 hscrolled_p |= hscroll_window_tree (w->vchild);
7390 else if (w->cursor.vpos >= 0)
7392 int hscroll_margin, text_area_x, text_area_y;
7393 int text_area_width, text_area_height;
7394 struct glyph_row *current_cursor_row
7395 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7396 struct glyph_row *desired_cursor_row
7397 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7398 struct glyph_row *cursor_row
7399 = (desired_cursor_row->enabled_p
7400 ? desired_cursor_row
7401 : current_cursor_row);
7403 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7404 &text_area_width, &text_area_height);
7406 /* Scroll when cursor is inside this scroll margin. */
7407 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7409 if ((XFASTINT (w->hscroll)
7410 && w->cursor.x < hscroll_margin)
7411 || (cursor_row->enabled_p
7412 && cursor_row->truncated_on_right_p
7413 && (w->cursor.x > text_area_width - hscroll_margin)))
7415 struct it it;
7416 int hscroll;
7417 struct buffer *saved_current_buffer;
7418 int pt;
7420 /* Find point in a display of infinite width. */
7421 saved_current_buffer = current_buffer;
7422 current_buffer = XBUFFER (w->buffer);
7424 if (w == XWINDOW (selected_window))
7425 pt = BUF_PT (current_buffer);
7426 else
7428 pt = marker_position (w->pointm);
7429 pt = max (BEGV, pt);
7430 pt = min (ZV, pt);
7433 /* Move iterator to pt starting at cursor_row->start in
7434 a line with infinite width. */
7435 init_to_row_start (&it, w, cursor_row);
7436 it.last_visible_x = INFINITY;
7437 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7438 current_buffer = saved_current_buffer;
7440 /* Center cursor in window. */
7441 hscroll = (max (0, it.current_x - text_area_width / 2)
7442 / CANON_X_UNIT (it.f));
7444 /* Don't call Fset_window_hscroll if value hasn't
7445 changed because it will prevent redisplay
7446 optimizations. */
7447 if (XFASTINT (w->hscroll) != hscroll)
7449 Fset_window_hscroll (window, make_number (hscroll));
7450 hscrolled_p = 1;
7455 window = w->next;
7458 /* Value is non-zero if hscroll of any leaf window has been changed. */
7459 return hscrolled_p;
7463 /* Set hscroll so that cursor is visible and not inside horizontal
7464 scroll margins for all windows in the tree rooted at WINDOW. See
7465 also hscroll_window_tree above. Value is non-zero if any window's
7466 hscroll has been changed. If it has, desired matrices on the frame
7467 of WINDOW are cleared. */
7469 static int
7470 hscroll_windows (window)
7471 Lisp_Object window;
7473 int hscrolled_p;
7475 if (automatic_hscrolling_p)
7477 hscrolled_p = hscroll_window_tree (window);
7478 if (hscrolled_p)
7479 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7481 else
7482 hscrolled_p = 0;
7483 return hscrolled_p;
7488 /************************************************************************
7489 Redisplay
7490 ************************************************************************/
7492 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7493 to a non-zero value. This is sometimes handy to have in a debugger
7494 session. */
7496 #if GLYPH_DEBUG
7498 /* First and last unchanged row for try_window_id. */
7500 int debug_first_unchanged_at_end_vpos;
7501 int debug_last_unchanged_at_beg_vpos;
7503 /* Delta vpos and y. */
7505 int debug_dvpos, debug_dy;
7507 /* Delta in characters and bytes for try_window_id. */
7509 int debug_delta, debug_delta_bytes;
7511 /* Values of window_end_pos and window_end_vpos at the end of
7512 try_window_id. */
7514 int debug_end_pos, debug_end_vpos;
7516 /* Append a string to W->desired_matrix->method. FMT is a printf
7517 format string. A1...A9 are a supplement for a variable-length
7518 argument list. If trace_redisplay_p is non-zero also printf the
7519 resulting string to stderr. */
7521 static void
7522 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7523 struct window *w;
7524 char *fmt;
7525 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7527 char buffer[512];
7528 char *method = w->desired_matrix->method;
7529 int len = strlen (method);
7530 int size = sizeof w->desired_matrix->method;
7531 int remaining = size - len - 1;
7533 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7534 if (len && remaining)
7536 method[len] = '|';
7537 --remaining, ++len;
7540 strncpy (method + len, buffer, remaining);
7542 if (trace_redisplay_p)
7543 fprintf (stderr, "%p (%s): %s\n",
7545 ((BUFFERP (w->buffer)
7546 && STRINGP (XBUFFER (w->buffer)->name))
7547 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7548 : "no buffer"),
7549 buffer);
7552 #endif /* GLYPH_DEBUG */
7555 /* This counter is used to clear the face cache every once in a while
7556 in redisplay_internal. It is incremented for each redisplay.
7557 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7558 cleared. */
7560 #define CLEAR_FACE_CACHE_COUNT 10000
7561 static int clear_face_cache_count;
7563 /* Record the previous terminal frame we displayed. */
7565 static struct frame *previous_terminal_frame;
7567 /* Non-zero while redisplay_internal is in progress. */
7569 int redisplaying_p;
7572 /* Value is non-zero if all changes in window W, which displays
7573 current_buffer, are in the text between START and END. START is a
7574 buffer position, END is given as a distance from Z. Used in
7575 redisplay_internal for display optimization. */
7577 static INLINE int
7578 text_outside_line_unchanged_p (w, start, end)
7579 struct window *w;
7580 int start, end;
7582 int unchanged_p = 1;
7584 /* If text or overlays have changed, see where. */
7585 if (XFASTINT (w->last_modified) < MODIFF
7586 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7588 /* Gap in the line? */
7589 if (GPT < start || Z - GPT < end)
7590 unchanged_p = 0;
7592 /* Changes start in front of the line, or end after it? */
7593 if (unchanged_p
7594 && (BEG_UNCHANGED < start - 1
7595 || END_UNCHANGED < end))
7596 unchanged_p = 0;
7598 /* If selective display, can't optimize if changes start at the
7599 beginning of the line. */
7600 if (unchanged_p
7601 && INTEGERP (current_buffer->selective_display)
7602 && XINT (current_buffer->selective_display) > 0
7603 && (BEG_UNCHANGED < start || GPT <= start))
7604 unchanged_p = 0;
7607 return unchanged_p;
7611 /* Do a frame update, taking possible shortcuts into account. This is
7612 the main external entry point for redisplay.
7614 If the last redisplay displayed an echo area message and that message
7615 is no longer requested, we clear the echo area or bring back the
7616 mini-buffer if that is in use. */
7618 void
7619 redisplay ()
7621 redisplay_internal (0);
7624 /* Return 1 if point moved out of or into a composition. Otherwise
7625 return 0. PREV_BUF and PREV_PT are the last point buffer and
7626 position. BUF and PT are the current point buffer and position. */
7629 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7630 struct buffer *prev_buf, *buf;
7631 int prev_pt, pt;
7633 int start, end;
7634 Lisp_Object prop;
7635 Lisp_Object buffer;
7637 XSETBUFFER (buffer, buf);
7638 /* Check a composition at the last point if point moved within the
7639 same buffer. */
7640 if (prev_buf == buf)
7642 if (prev_pt == pt)
7643 /* Point didn't move. */
7644 return 0;
7646 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7647 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7648 && COMPOSITION_VALID_P (start, end, prop)
7649 && start < prev_pt && end > prev_pt)
7650 /* The last point was within the composition. Return 1 iff
7651 point moved out of the composition. */
7652 return (pt <= start || pt >= end);
7655 /* Check a composition at the current point. */
7656 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7657 && find_composition (pt, -1, &start, &end, &prop, buffer)
7658 && COMPOSITION_VALID_P (start, end, prop)
7659 && start < pt && end > pt);
7662 /* Reconsider the setting of B->clip_changed which is displayed
7663 in window W. */
7665 static INLINE void
7666 reconsider_clip_changes (w, b)
7667 struct window *w;
7668 struct buffer *b;
7670 if (b->prevent_redisplay_optimizations_p)
7671 b->clip_changed = 1;
7672 else if (b->clip_changed
7673 && !NILP (w->window_end_valid)
7674 && w->current_matrix->buffer == b
7675 && w->current_matrix->zv == BUF_ZV (b)
7676 && w->current_matrix->begv == BUF_BEGV (b))
7677 b->clip_changed = 0;
7679 /* If display wasn't paused, and W is not a tool bar window, see if
7680 point has been moved into or out of a composition. In that case,
7681 we set b->clip_changed to 1 to force updating the screen. If
7682 b->clip_changed has already been set to 1, we can skip this
7683 check. */
7684 if (!b->clip_changed
7685 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7687 int pt;
7689 if (w == XWINDOW (selected_window))
7690 pt = BUF_PT (current_buffer);
7691 else
7692 pt = marker_position (w->pointm);
7694 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7695 || pt != XINT (w->last_point))
7696 && check_point_in_composition (w->current_matrix->buffer,
7697 XINT (w->last_point),
7698 XBUFFER (w->buffer), pt))
7699 b->clip_changed = 1;
7704 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7705 response to any user action; therefore, we should preserve the echo
7706 area. (Actually, our caller does that job.) Perhaps in the future
7707 avoid recentering windows if it is not necessary; currently that
7708 causes some problems. */
7710 static void
7711 redisplay_internal (preserve_echo_area)
7712 int preserve_echo_area;
7714 struct window *w = XWINDOW (selected_window);
7715 struct frame *f = XFRAME (w->frame);
7716 int pause;
7717 int must_finish = 0;
7718 struct text_pos tlbufpos, tlendpos;
7719 int number_of_visible_frames;
7720 int count;
7721 struct frame *sf = SELECTED_FRAME ();
7723 /* Non-zero means redisplay has to consider all windows on all
7724 frames. Zero means, only selected_window is considered. */
7725 int consider_all_windows_p;
7727 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7729 /* No redisplay if running in batch mode or frame is not yet fully
7730 initialized, or redisplay is explicitly turned off by setting
7731 Vinhibit_redisplay. */
7732 if (noninteractive
7733 || !NILP (Vinhibit_redisplay)
7734 || !f->glyphs_initialized_p)
7735 return;
7737 /* The flag redisplay_performed_directly_p is set by
7738 direct_output_for_insert when it already did the whole screen
7739 update necessary. */
7740 if (redisplay_performed_directly_p)
7742 redisplay_performed_directly_p = 0;
7743 if (!hscroll_windows (selected_window))
7744 return;
7747 #ifdef USE_X_TOOLKIT
7748 if (popup_activated ())
7749 return;
7750 #endif
7752 /* I don't think this happens but let's be paranoid. */
7753 if (redisplaying_p)
7754 return;
7756 /* Record a function that resets redisplaying_p to its old value
7757 when we leave this function. */
7758 count = specpdl_ptr - specpdl;
7759 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7760 ++redisplaying_p;
7762 retry:
7763 pause = 0;
7764 reconsider_clip_changes (w, current_buffer);
7766 /* If new fonts have been loaded that make a glyph matrix adjustment
7767 necessary, do it. */
7768 if (fonts_changed_p)
7770 adjust_glyphs (NULL);
7771 ++windows_or_buffers_changed;
7772 fonts_changed_p = 0;
7775 if (! FRAME_WINDOW_P (sf)
7776 && previous_terminal_frame != sf)
7778 /* Since frames on an ASCII terminal share the same display
7779 area, displaying a different frame means redisplay the whole
7780 thing. */
7781 windows_or_buffers_changed++;
7782 SET_FRAME_GARBAGED (sf);
7783 XSETFRAME (Vterminal_frame, sf);
7785 previous_terminal_frame = sf;
7787 /* Set the visible flags for all frames. Do this before checking
7788 for resized or garbaged frames; they want to know if their frames
7789 are visible. See the comment in frame.h for
7790 FRAME_SAMPLE_VISIBILITY. */
7792 Lisp_Object tail, frame;
7794 number_of_visible_frames = 0;
7796 FOR_EACH_FRAME (tail, frame)
7798 struct frame *f = XFRAME (frame);
7800 FRAME_SAMPLE_VISIBILITY (f);
7801 if (FRAME_VISIBLE_P (f))
7802 ++number_of_visible_frames;
7803 clear_desired_matrices (f);
7807 /* Notice any pending interrupt request to change frame size. */
7808 do_pending_window_change (1);
7810 /* Clear frames marked as garbaged. */
7811 if (frame_garbaged)
7812 clear_garbaged_frames ();
7814 /* Build menubar and tool-bar items. */
7815 prepare_menu_bars ();
7817 if (windows_or_buffers_changed)
7818 update_mode_lines++;
7820 /* Detect case that we need to write or remove a star in the mode line. */
7821 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7823 w->update_mode_line = Qt;
7824 if (buffer_shared > 1)
7825 update_mode_lines++;
7828 /* If %c is in the mode line, update it if needed. */
7829 if (!NILP (w->column_number_displayed)
7830 /* This alternative quickly identifies a common case
7831 where no change is needed. */
7832 && !(PT == XFASTINT (w->last_point)
7833 && XFASTINT (w->last_modified) >= MODIFF
7834 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7835 && XFASTINT (w->column_number_displayed) != current_column ())
7836 w->update_mode_line = Qt;
7838 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7840 /* The variable buffer_shared is set in redisplay_window and
7841 indicates that we redisplay a buffer in different windows. See
7842 there. */
7843 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7845 /* If specs for an arrow have changed, do thorough redisplay
7846 to ensure we remove any arrow that should no longer exist. */
7847 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7848 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7849 consider_all_windows_p = windows_or_buffers_changed = 1;
7851 /* Normally the message* functions will have already displayed and
7852 updated the echo area, but the frame may have been trashed, or
7853 the update may have been preempted, so display the echo area
7854 again here. Checking both message buffers captures the case that
7855 the echo area should be cleared. */
7856 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7858 int window_height_changed_p = echo_area_display (0);
7859 must_finish = 1;
7861 if (fonts_changed_p)
7862 goto retry;
7863 else if (window_height_changed_p)
7865 consider_all_windows_p = 1;
7866 ++update_mode_lines;
7867 ++windows_or_buffers_changed;
7869 /* If window configuration was changed, frames may have been
7870 marked garbaged. Clear them or we will experience
7871 surprises wrt scrolling. */
7872 if (frame_garbaged)
7873 clear_garbaged_frames ();
7876 else if (EQ (selected_window, minibuf_window)
7877 && (current_buffer->clip_changed
7878 || XFASTINT (w->last_modified) < MODIFF
7879 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7880 && resize_mini_window (w, 0))
7882 /* Resized active mini-window to fit the size of what it is
7883 showing if its contents might have changed. */
7884 must_finish = 1;
7885 consider_all_windows_p = 1;
7886 ++windows_or_buffers_changed;
7887 ++update_mode_lines;
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 ();
7897 /* If showing the region, and mark has changed, we must redisplay
7898 the whole window. The assignment to this_line_start_pos prevents
7899 the optimization directly below this if-statement. */
7900 if (((!NILP (Vtransient_mark_mode)
7901 && !NILP (XBUFFER (w->buffer)->mark_active))
7902 != !NILP (w->region_showing))
7903 || (!NILP (w->region_showing)
7904 && !EQ (w->region_showing,
7905 Fmarker_position (XBUFFER (w->buffer)->mark))))
7906 CHARPOS (this_line_start_pos) = 0;
7908 /* Optimize the case that only the line containing the cursor in the
7909 selected window has changed. Variables starting with this_ are
7910 set in display_line and record information about the line
7911 containing the cursor. */
7912 tlbufpos = this_line_start_pos;
7913 tlendpos = this_line_end_pos;
7914 if (!consider_all_windows_p
7915 && CHARPOS (tlbufpos) > 0
7916 && NILP (w->update_mode_line)
7917 && !current_buffer->clip_changed
7918 && FRAME_VISIBLE_P (XFRAME (w->frame))
7919 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7920 /* Make sure recorded data applies to current buffer, etc. */
7921 && this_line_buffer == current_buffer
7922 && current_buffer == XBUFFER (w->buffer)
7923 && NILP (w->force_start)
7924 /* Point must be on the line that we have info recorded about. */
7925 && PT >= CHARPOS (tlbufpos)
7926 && PT <= Z - CHARPOS (tlendpos)
7927 /* All text outside that line, including its final newline,
7928 must be unchanged */
7929 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7930 CHARPOS (tlendpos)))
7932 if (CHARPOS (tlbufpos) > BEGV
7933 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7934 && (CHARPOS (tlbufpos) == ZV
7935 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7936 /* Former continuation line has disappeared by becoming empty */
7937 goto cancel;
7938 else if (XFASTINT (w->last_modified) < MODIFF
7939 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7940 || MINI_WINDOW_P (w))
7942 /* We have to handle the case of continuation around a
7943 wide-column character (See the comment in indent.c around
7944 line 885).
7946 For instance, in the following case:
7948 -------- Insert --------
7949 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7950 J_I_ ==> J_I_ `^^' are cursors.
7951 ^^ ^^
7952 -------- --------
7954 As we have to redraw the line above, we should goto cancel. */
7956 struct it it;
7957 int line_height_before = this_line_pixel_height;
7959 /* Note that start_display will handle the case that the
7960 line starting at tlbufpos is a continuation lines. */
7961 start_display (&it, w, tlbufpos);
7963 /* Implementation note: It this still necessary? */
7964 if (it.current_x != this_line_start_x)
7965 goto cancel;
7967 TRACE ((stderr, "trying display optimization 1\n"));
7968 w->cursor.vpos = -1;
7969 overlay_arrow_seen = 0;
7970 it.vpos = this_line_vpos;
7971 it.current_y = this_line_y;
7972 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7973 display_line (&it);
7975 /* If line contains point, is not continued,
7976 and ends at same distance from eob as before, we win */
7977 if (w->cursor.vpos >= 0
7978 /* Line is not continued, otherwise this_line_start_pos
7979 would have been set to 0 in display_line. */
7980 && CHARPOS (this_line_start_pos)
7981 /* Line ends as before. */
7982 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7983 /* Line has same height as before. Otherwise other lines
7984 would have to be shifted up or down. */
7985 && this_line_pixel_height == line_height_before)
7987 /* If this is not the window's last line, we must adjust
7988 the charstarts of the lines below. */
7989 if (it.current_y < it.last_visible_y)
7991 struct glyph_row *row
7992 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7993 int delta, delta_bytes;
7995 if (Z - CHARPOS (tlendpos) == ZV)
7997 /* This line ends at end of (accessible part of)
7998 buffer. There is no newline to count. */
7999 delta = (Z
8000 - CHARPOS (tlendpos)
8001 - MATRIX_ROW_START_CHARPOS (row));
8002 delta_bytes = (Z_BYTE
8003 - BYTEPOS (tlendpos)
8004 - MATRIX_ROW_START_BYTEPOS (row));
8006 else
8008 /* This line ends in a newline. Must take
8009 account of the newline and the rest of the
8010 text that follows. */
8011 delta = (Z
8012 - CHARPOS (tlendpos)
8013 - MATRIX_ROW_START_CHARPOS (row));
8014 delta_bytes = (Z_BYTE
8015 - BYTEPOS (tlendpos)
8016 - MATRIX_ROW_START_BYTEPOS (row));
8019 increment_matrix_positions (w->current_matrix,
8020 this_line_vpos + 1,
8021 w->current_matrix->nrows,
8022 delta, delta_bytes);
8025 /* If this row displays text now but previously didn't,
8026 or vice versa, w->window_end_vpos may have to be
8027 adjusted. */
8028 if ((it.glyph_row - 1)->displays_text_p)
8030 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8031 XSETINT (w->window_end_vpos, this_line_vpos);
8033 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8034 && this_line_vpos > 0)
8035 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8036 w->window_end_valid = Qnil;
8038 /* Update hint: No need to try to scroll in update_window. */
8039 w->desired_matrix->no_scrolling_p = 1;
8041 #if GLYPH_DEBUG
8042 *w->desired_matrix->method = 0;
8043 debug_method_add (w, "optimization 1");
8044 #endif
8045 goto update;
8047 else
8048 goto cancel;
8050 else if (/* Cursor position hasn't changed. */
8051 PT == XFASTINT (w->last_point)
8052 /* Make sure the cursor was last displayed
8053 in this window. Otherwise we have to reposition it. */
8054 && 0 <= w->cursor.vpos
8055 && XINT (w->height) > w->cursor.vpos)
8057 if (!must_finish)
8059 do_pending_window_change (1);
8061 /* We used to always goto end_of_redisplay here, but this
8062 isn't enough if we have a blinking cursor. */
8063 if (w->cursor_off_p == w->last_cursor_off_p)
8064 goto end_of_redisplay;
8066 goto update;
8068 /* If highlighting the region, or if the cursor is in the echo area,
8069 then we can't just move the cursor. */
8070 else if (! (!NILP (Vtransient_mark_mode)
8071 && !NILP (current_buffer->mark_active))
8072 && (EQ (selected_window, current_buffer->last_selected_window)
8073 || highlight_nonselected_windows)
8074 && NILP (w->region_showing)
8075 && NILP (Vshow_trailing_whitespace)
8076 && !cursor_in_echo_area)
8078 struct it it;
8079 struct glyph_row *row;
8081 /* Skip from tlbufpos to PT and see where it is. Note that
8082 PT may be in invisible text. If so, we will end at the
8083 next visible position. */
8084 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8085 NULL, DEFAULT_FACE_ID);
8086 it.current_x = this_line_start_x;
8087 it.current_y = this_line_y;
8088 it.vpos = this_line_vpos;
8090 /* The call to move_it_to stops in front of PT, but
8091 moves over before-strings. */
8092 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8094 if (it.vpos == this_line_vpos
8095 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8096 row->enabled_p))
8098 xassert (this_line_vpos == it.vpos);
8099 xassert (this_line_y == it.current_y);
8100 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8101 goto update;
8103 else
8104 goto cancel;
8107 cancel:
8108 /* Text changed drastically or point moved off of line. */
8109 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8112 CHARPOS (this_line_start_pos) = 0;
8113 consider_all_windows_p |= buffer_shared > 1;
8114 ++clear_face_cache_count;
8117 /* Build desired matrices, and update the display. If
8118 consider_all_windows_p is non-zero, do it for all windows on all
8119 frames. Otherwise do it for selected_window, only. */
8121 if (consider_all_windows_p)
8123 Lisp_Object tail, frame;
8125 /* Clear the face cache eventually. */
8126 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8128 clear_face_cache (0);
8129 clear_face_cache_count = 0;
8132 /* Recompute # windows showing selected buffer. This will be
8133 incremented each time such a window is displayed. */
8134 buffer_shared = 0;
8136 FOR_EACH_FRAME (tail, frame)
8138 struct frame *f = XFRAME (frame);
8140 if (FRAME_WINDOW_P (f) || f == sf)
8142 /* Mark all the scroll bars to be removed; we'll redeem
8143 the ones we want when we redisplay their windows. */
8144 if (condemn_scroll_bars_hook)
8145 (*condemn_scroll_bars_hook) (f);
8147 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8148 redisplay_windows (FRAME_ROOT_WINDOW (f));
8150 /* Any scroll bars which redisplay_windows should have
8151 nuked should now go away. */
8152 if (judge_scroll_bars_hook)
8153 (*judge_scroll_bars_hook) (f);
8155 /* If fonts changed, display again. */
8156 if (fonts_changed_p)
8157 goto retry;
8159 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8161 /* See if we have to hscroll. */
8162 if (hscroll_windows (f->root_window))
8163 goto retry;
8165 /* Prevent various kinds of signals during display
8166 update. stdio is not robust about handling
8167 signals, which can cause an apparent I/O
8168 error. */
8169 if (interrupt_input)
8170 unrequest_sigio ();
8171 stop_polling ();
8173 /* Update the display. */
8174 set_window_update_flags (XWINDOW (f->root_window), 1);
8175 pause |= update_frame (f, 0, 0);
8176 if (pause)
8177 break;
8179 mark_window_display_accurate (f->root_window, 1);
8180 if (frame_up_to_date_hook)
8181 frame_up_to_date_hook (f);
8186 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8188 Lisp_Object mini_window;
8189 struct frame *mini_frame;
8191 redisplay_window (selected_window, 1);
8193 /* Compare desired and current matrices, perform output. */
8194 update:
8196 /* If fonts changed, display again. */
8197 if (fonts_changed_p)
8198 goto retry;
8200 /* Prevent various kinds of signals during display update.
8201 stdio is not robust about handling signals,
8202 which can cause an apparent I/O error. */
8203 if (interrupt_input)
8204 unrequest_sigio ();
8205 stop_polling ();
8207 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8209 if (hscroll_windows (selected_window))
8210 goto retry;
8212 XWINDOW (selected_window)->must_be_updated_p = 1;
8213 pause = update_frame (sf, 0, 0);
8216 /* We may have called echo_area_display at the top of this
8217 function. If the echo area is on another frame, that may
8218 have put text on a frame other than the selected one, so the
8219 above call to update_frame would not have caught it. Catch
8220 it here. */
8221 mini_window = FRAME_MINIBUF_WINDOW (sf);
8222 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8224 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8226 XWINDOW (mini_window)->must_be_updated_p = 1;
8227 pause |= update_frame (mini_frame, 0, 0);
8228 if (!pause && hscroll_windows (mini_window))
8229 goto retry;
8233 /* If display was paused because of pending input, make sure we do a
8234 thorough update the next time. */
8235 if (pause)
8237 /* Prevent the optimization at the beginning of
8238 redisplay_internal that tries a single-line update of the
8239 line containing the cursor in the selected window. */
8240 CHARPOS (this_line_start_pos) = 0;
8242 /* Let the overlay arrow be updated the next time. */
8243 if (!NILP (last_arrow_position))
8245 last_arrow_position = Qt;
8246 last_arrow_string = Qt;
8249 /* If we pause after scrolling, some rows in the current
8250 matrices of some windows are not valid. */
8251 if (!WINDOW_FULL_WIDTH_P (w)
8252 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8253 update_mode_lines = 1;
8256 /* Now text on frame agrees with windows, so put info into the
8257 windows for partial redisplay to follow. */
8258 if (!pause)
8260 register struct buffer *b = XBUFFER (w->buffer);
8262 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8263 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8264 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8265 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8267 if (consider_all_windows_p)
8268 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8269 else
8271 XSETFASTINT (w->last_point, BUF_PT (b));
8272 w->last_cursor = w->cursor;
8273 w->last_cursor_off_p = w->cursor_off_p;
8275 b->clip_changed = 0;
8276 b->prevent_redisplay_optimizations_p = 0;
8277 w->update_mode_line = Qnil;
8278 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8279 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8280 w->last_had_star
8281 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8282 ? Qt : Qnil);
8284 /* Record if we are showing a region, so can make sure to
8285 update it fully at next redisplay. */
8286 w->region_showing = (!NILP (Vtransient_mark_mode)
8287 && (EQ (selected_window,
8288 current_buffer->last_selected_window)
8289 || highlight_nonselected_windows)
8290 && !NILP (XBUFFER (w->buffer)->mark_active)
8291 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8292 : Qnil);
8294 w->window_end_valid = w->buffer;
8295 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8296 last_arrow_string = Voverlay_arrow_string;
8297 if (frame_up_to_date_hook != 0)
8298 (*frame_up_to_date_hook) (sf);
8300 w->current_matrix->buffer = b;
8301 w->current_matrix->begv = BUF_BEGV (b);
8302 w->current_matrix->zv = BUF_ZV (b);
8305 update_mode_lines = 0;
8306 windows_or_buffers_changed = 0;
8309 /* Start SIGIO interrupts coming again. Having them off during the
8310 code above makes it less likely one will discard output, but not
8311 impossible, since there might be stuff in the system buffer here.
8312 But it is much hairier to try to do anything about that. */
8313 if (interrupt_input)
8314 request_sigio ();
8315 start_polling ();
8317 /* If a frame has become visible which was not before, redisplay
8318 again, so that we display it. Expose events for such a frame
8319 (which it gets when becoming visible) don't call the parts of
8320 redisplay constructing glyphs, so simply exposing a frame won't
8321 display anything in this case. So, we have to display these
8322 frames here explicitly. */
8323 if (!pause)
8325 Lisp_Object tail, frame;
8326 int new_count = 0;
8328 FOR_EACH_FRAME (tail, frame)
8330 int this_is_visible = 0;
8332 if (XFRAME (frame)->visible)
8333 this_is_visible = 1;
8334 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8335 if (XFRAME (frame)->visible)
8336 this_is_visible = 1;
8338 if (this_is_visible)
8339 new_count++;
8342 if (new_count != number_of_visible_frames)
8343 windows_or_buffers_changed++;
8346 /* Change frame size now if a change is pending. */
8347 do_pending_window_change (1);
8349 /* If we just did a pending size change, or have additional
8350 visible frames, redisplay again. */
8351 if (windows_or_buffers_changed && !pause)
8352 goto retry;
8354 end_of_redisplay:;
8356 unbind_to (count, Qnil);
8360 /* Redisplay, but leave alone any recent echo area message unless
8361 another message has been requested in its place.
8363 This is useful in situations where you need to redisplay but no
8364 user action has occurred, making it inappropriate for the message
8365 area to be cleared. See tracking_off and
8366 wait_reading_process_input for examples of these situations. */
8368 void
8369 redisplay_preserve_echo_area ()
8371 if (!NILP (echo_area_buffer[1]))
8373 /* We have a previously displayed message, but no current
8374 message. Redisplay the previous message. */
8375 display_last_displayed_message_p = 1;
8376 redisplay_internal (1);
8377 display_last_displayed_message_p = 0;
8379 else
8380 redisplay_internal (1);
8384 /* Function registered with record_unwind_protect in
8385 redisplay_internal. Clears the flag indicating that a redisplay is
8386 in progress. */
8388 static Lisp_Object
8389 unwind_redisplay (old_redisplaying_p)
8390 Lisp_Object old_redisplaying_p;
8392 redisplaying_p = XFASTINT (old_redisplaying_p);
8393 return Qnil;
8397 /* Mark the display of windows in the window tree rooted at WINDOW as
8398 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8399 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8400 the next time redisplay_internal is called. */
8402 void
8403 mark_window_display_accurate (window, accurate_p)
8404 Lisp_Object window;
8405 int accurate_p;
8407 struct window *w;
8409 for (; !NILP (window); window = w->next)
8411 w = XWINDOW (window);
8413 if (BUFFERP (w->buffer))
8415 struct buffer *b = XBUFFER (w->buffer);
8417 XSETFASTINT (w->last_modified,
8418 accurate_p ? BUF_MODIFF (b) : 0);
8419 XSETFASTINT (w->last_overlay_modified,
8420 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8421 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8422 ? Qt : Qnil);
8424 #if 0 /* I don't think this is necessary because display_line does it.
8425 Let's check it. */
8426 /* Record if we are showing a region, so can make sure to
8427 update it fully at next redisplay. */
8428 w->region_showing
8429 = (!NILP (Vtransient_mark_mode)
8430 && (w == XWINDOW (current_buffer->last_selected_window)
8431 || highlight_nonselected_windows)
8432 && (!NILP (b->mark_active)
8433 ? Fmarker_position (b->mark)
8434 : Qnil));
8435 #endif
8437 if (accurate_p)
8439 b->clip_changed = 0;
8440 b->prevent_redisplay_optimizations_p = 0;
8441 w->current_matrix->buffer = b;
8442 w->current_matrix->begv = BUF_BEGV (b);
8443 w->current_matrix->zv = BUF_ZV (b);
8444 w->last_cursor = w->cursor;
8445 w->last_cursor_off_p = w->cursor_off_p;
8446 if (w == XWINDOW (selected_window))
8447 w->last_point = make_number (BUF_PT (b));
8448 else
8449 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8453 w->window_end_valid = w->buffer;
8454 w->update_mode_line = Qnil;
8456 if (!NILP (w->vchild))
8457 mark_window_display_accurate (w->vchild, accurate_p);
8458 if (!NILP (w->hchild))
8459 mark_window_display_accurate (w->hchild, accurate_p);
8462 if (accurate_p)
8464 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8465 last_arrow_string = Voverlay_arrow_string;
8467 else
8469 /* Force a thorough redisplay the next time by setting
8470 last_arrow_position and last_arrow_string to t, which is
8471 unequal to any useful value of Voverlay_arrow_... */
8472 last_arrow_position = Qt;
8473 last_arrow_string = Qt;
8478 /* Return value in display table DP (Lisp_Char_Table *) for character
8479 C. Since a display table doesn't have any parent, we don't have to
8480 follow parent. Do not call this function directly but use the
8481 macro DISP_CHAR_VECTOR. */
8483 Lisp_Object
8484 disp_char_vector (dp, c)
8485 struct Lisp_Char_Table *dp;
8486 int c;
8488 int code[4], i;
8489 Lisp_Object val;
8491 if (SINGLE_BYTE_CHAR_P (c))
8492 return (dp->contents[c]);
8494 SPLIT_CHAR (c, code[0], code[1], code[2]);
8495 if (code[1] < 32)
8496 code[1] = -1;
8497 else if (code[2] < 32)
8498 code[2] = -1;
8500 /* Here, the possible range of code[0] (== charset ID) is
8501 128..max_charset. Since the top level char table contains data
8502 for multibyte characters after 256th element, we must increment
8503 code[0] by 128 to get a correct index. */
8504 code[0] += 128;
8505 code[3] = -1; /* anchor */
8507 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8509 val = dp->contents[code[i]];
8510 if (!SUB_CHAR_TABLE_P (val))
8511 return (NILP (val) ? dp->defalt : val);
8514 /* Here, val is a sub char table. We return the default value of
8515 it. */
8516 return (dp->defalt);
8521 /***********************************************************************
8522 Window Redisplay
8523 ***********************************************************************/
8525 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8527 static void
8528 redisplay_windows (window)
8529 Lisp_Object window;
8531 while (!NILP (window))
8533 struct window *w = XWINDOW (window);
8535 if (!NILP (w->hchild))
8536 redisplay_windows (w->hchild);
8537 else if (!NILP (w->vchild))
8538 redisplay_windows (w->vchild);
8539 else
8540 redisplay_window (window, 0);
8542 window = w->next;
8547 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8548 DELTA is the number of bytes by which positions recorded in ROW
8549 differ from current buffer positions. */
8551 void
8552 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8553 struct window *w;
8554 struct glyph_row *row;
8555 struct glyph_matrix *matrix;
8556 int delta, delta_bytes, dy, dvpos;
8558 struct glyph *glyph = row->glyphs[TEXT_AREA];
8559 struct glyph *end = glyph + row->used[TEXT_AREA];
8560 int x = row->x;
8561 int pt_old = PT - delta;
8563 /* Skip over glyphs not having an object at the start of the row.
8564 These are special glyphs like truncation marks on terminal
8565 frames. */
8566 if (row->displays_text_p)
8567 while (glyph < end
8568 && INTEGERP (glyph->object)
8569 && glyph->charpos < 0)
8571 x += glyph->pixel_width;
8572 ++glyph;
8575 while (glyph < end
8576 && !INTEGERP (glyph->object)
8577 && (!BUFFERP (glyph->object)
8578 || glyph->charpos < pt_old))
8580 x += glyph->pixel_width;
8581 ++glyph;
8584 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8585 w->cursor.x = x;
8586 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8587 w->cursor.y = row->y + dy;
8589 if (w == XWINDOW (selected_window))
8591 if (!row->continued_p
8592 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8593 && row->x == 0)
8595 this_line_buffer = XBUFFER (w->buffer);
8597 CHARPOS (this_line_start_pos)
8598 = MATRIX_ROW_START_CHARPOS (row) + delta;
8599 BYTEPOS (this_line_start_pos)
8600 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8602 CHARPOS (this_line_end_pos)
8603 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8604 BYTEPOS (this_line_end_pos)
8605 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8607 this_line_y = w->cursor.y;
8608 this_line_pixel_height = row->height;
8609 this_line_vpos = w->cursor.vpos;
8610 this_line_start_x = row->x;
8612 else
8613 CHARPOS (this_line_start_pos) = 0;
8618 /* Run window scroll functions, if any, for WINDOW with new window
8619 start STARTP. Sets the window start of WINDOW to that position.
8621 We assume that the window's buffer is really current. */
8623 static INLINE struct text_pos
8624 run_window_scroll_functions (window, startp)
8625 Lisp_Object window;
8626 struct text_pos startp;
8628 struct window *w = XWINDOW (window);
8629 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8631 if (current_buffer != XBUFFER (w->buffer))
8632 abort ();
8634 if (!NILP (Vwindow_scroll_functions))
8636 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8637 make_number (CHARPOS (startp)));
8638 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8639 /* In case the hook functions switch buffers. */
8640 if (current_buffer != XBUFFER (w->buffer))
8641 set_buffer_internal_1 (XBUFFER (w->buffer));
8644 return startp;
8648 /* Modify the desired matrix of window W and W->vscroll so that the
8649 line containing the cursor is fully visible. */
8651 static void
8652 make_cursor_line_fully_visible (w)
8653 struct window *w;
8655 struct glyph_matrix *matrix;
8656 struct glyph_row *row;
8657 int window_height, header_line_height;
8659 /* It's not always possible to find the cursor, e.g, when a window
8660 is full of overlay strings. Don't do anything in that case. */
8661 if (w->cursor.vpos < 0)
8662 return;
8664 matrix = w->desired_matrix;
8665 row = MATRIX_ROW (matrix, w->cursor.vpos);
8667 /* If the cursor row is not partially visible, there's nothing
8668 to do. */
8669 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8670 return;
8672 /* If the row the cursor is in is taller than the window's height,
8673 it's not clear what to do, so do nothing. */
8674 window_height = window_box_height (w);
8675 if (row->height >= window_height)
8676 return;
8678 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8680 int dy = row->height - row->visible_height;
8681 w->vscroll = 0;
8682 w->cursor.y += dy;
8683 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8685 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8687 int dy = - (row->height - row->visible_height);
8688 w->vscroll = dy;
8689 w->cursor.y += dy;
8690 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8693 /* When we change the cursor y-position of the selected window,
8694 change this_line_y as well so that the display optimization for
8695 the cursor line of the selected window in redisplay_internal uses
8696 the correct y-position. */
8697 if (w == XWINDOW (selected_window))
8698 this_line_y = w->cursor.y;
8702 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8703 non-zero means only WINDOW is redisplayed in redisplay_internal.
8704 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8705 in redisplay_window to bring a partially visible line into view in
8706 the case that only the cursor has moved.
8708 Value is
8710 1 if scrolling succeeded
8712 0 if scrolling didn't find point.
8714 -1 if new fonts have been loaded so that we must interrupt
8715 redisplay, adjust glyph matrices, and try again. */
8717 static int
8718 try_scrolling (window, just_this_one_p, scroll_conservatively,
8719 scroll_step, temp_scroll_step)
8720 Lisp_Object window;
8721 int just_this_one_p;
8722 int scroll_conservatively, scroll_step;
8723 int temp_scroll_step;
8725 struct window *w = XWINDOW (window);
8726 struct frame *f = XFRAME (w->frame);
8727 struct text_pos scroll_margin_pos;
8728 struct text_pos pos;
8729 struct text_pos startp;
8730 struct it it;
8731 Lisp_Object window_end;
8732 int this_scroll_margin;
8733 int dy = 0;
8734 int scroll_max;
8735 int rc;
8736 int amount_to_scroll = 0;
8737 Lisp_Object aggressive;
8738 int height;
8740 #if GLYPH_DEBUG
8741 debug_method_add (w, "try_scrolling");
8742 #endif
8744 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8746 /* Compute scroll margin height in pixels. We scroll when point is
8747 within this distance from the top or bottom of the window. */
8748 if (scroll_margin > 0)
8750 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8751 this_scroll_margin *= CANON_Y_UNIT (f);
8753 else
8754 this_scroll_margin = 0;
8756 /* Compute how much we should try to scroll maximally to bring point
8757 into view. */
8758 if (scroll_step)
8759 scroll_max = scroll_step;
8760 else if (scroll_conservatively)
8761 scroll_max = scroll_conservatively;
8762 else if (temp_scroll_step)
8763 scroll_max = temp_scroll_step;
8764 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8765 || NUMBERP (current_buffer->scroll_up_aggressively))
8766 /* We're trying to scroll because of aggressive scrolling
8767 but no scroll_step is set. Choose an arbitrary one. Maybe
8768 there should be a variable for this. */
8769 scroll_max = 10;
8770 else
8771 scroll_max = 0;
8772 scroll_max *= CANON_Y_UNIT (f);
8774 /* Decide whether we have to scroll down. Start at the window end
8775 and move this_scroll_margin up to find the position of the scroll
8776 margin. */
8777 window_end = Fwindow_end (window, Qt);
8778 CHARPOS (scroll_margin_pos) = XINT (window_end);
8779 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8780 if (this_scroll_margin)
8782 start_display (&it, w, scroll_margin_pos);
8783 move_it_vertically (&it, - this_scroll_margin);
8784 scroll_margin_pos = it.current.pos;
8787 if (PT >= CHARPOS (scroll_margin_pos))
8789 int y0;
8790 #if 0
8791 int line_height;
8792 #endif
8794 /* Point is in the scroll margin at the bottom of the window, or
8795 below. Compute a new window start that makes point visible. */
8797 /* Compute the distance from the scroll margin to PT.
8798 Give up if the distance is greater than scroll_max. */
8799 start_display (&it, w, scroll_margin_pos);
8800 y0 = it.current_y;
8801 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8802 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8803 #if 0 /* Taking the line's height into account here looks wrong. */
8804 line_height = (it.max_ascent + it.max_descent
8805 ? it.max_ascent + it.max_descent
8806 : last_height);
8807 dy = it.current_y + line_height - y0;
8808 #else
8809 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8810 end, which is one line below the window. The iterator's
8811 current_y will be same as y0 in that case, but we have to
8812 scroll a line to make PT visible. That's the reason why 1 is
8813 added below. */
8814 dy = 1 + it.current_y - y0;
8815 #endif
8817 if (dy > scroll_max)
8818 return 0;
8820 /* Move the window start down. If scrolling conservatively,
8821 move it just enough down to make point visible. If
8822 scroll_step is set, move it down by scroll_step. */
8823 start_display (&it, w, startp);
8825 if (scroll_conservatively)
8826 amount_to_scroll = dy;
8827 else if (scroll_step || temp_scroll_step)
8828 amount_to_scroll = scroll_max;
8829 else
8831 aggressive = current_buffer->scroll_down_aggressively;
8832 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8833 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8834 if (NUMBERP (aggressive))
8835 amount_to_scroll = XFLOATINT (aggressive) * height;
8838 if (amount_to_scroll <= 0)
8839 return 0;
8841 move_it_vertically (&it, amount_to_scroll);
8842 startp = it.current.pos;
8844 else
8846 /* See if point is inside the scroll margin at the top of the
8847 window. */
8848 scroll_margin_pos = startp;
8849 if (this_scroll_margin)
8851 start_display (&it, w, startp);
8852 move_it_vertically (&it, this_scroll_margin);
8853 scroll_margin_pos = it.current.pos;
8856 if (PT < CHARPOS (scroll_margin_pos))
8858 /* Point is in the scroll margin at the top of the window or
8859 above what is displayed in the window. */
8860 int y0;
8862 /* Compute the vertical distance from PT to the scroll
8863 margin position. Give up if distance is greater than
8864 scroll_max. */
8865 SET_TEXT_POS (pos, PT, PT_BYTE);
8866 start_display (&it, w, pos);
8867 y0 = it.current_y;
8868 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8869 it.last_visible_y, -1,
8870 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8871 dy = it.current_y - y0;
8872 if (dy > scroll_max)
8873 return 0;
8875 /* Compute new window start. */
8876 start_display (&it, w, startp);
8878 if (scroll_conservatively)
8879 amount_to_scroll = dy;
8880 else if (scroll_step || temp_scroll_step)
8881 amount_to_scroll = scroll_max;
8882 else
8884 aggressive = current_buffer->scroll_up_aggressively;
8885 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8886 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8887 if (NUMBERP (aggressive))
8888 amount_to_scroll = XFLOATINT (aggressive) * height;
8891 if (amount_to_scroll <= 0)
8892 return 0;
8894 move_it_vertically (&it, - amount_to_scroll);
8895 startp = it.current.pos;
8899 /* Run window scroll functions. */
8900 startp = run_window_scroll_functions (window, startp);
8902 /* Display the window. Give up if new fonts are loaded, or if point
8903 doesn't appear. */
8904 if (!try_window (window, startp))
8905 rc = -1;
8906 else if (w->cursor.vpos < 0)
8908 clear_glyph_matrix (w->desired_matrix);
8909 rc = 0;
8911 else
8913 /* Maybe forget recorded base line for line number display. */
8914 if (!just_this_one_p
8915 || current_buffer->clip_changed
8916 || BEG_UNCHANGED < CHARPOS (startp))
8917 w->base_line_number = Qnil;
8919 /* If cursor ends up on a partially visible line, shift display
8920 lines up or down. */
8921 make_cursor_line_fully_visible (w);
8922 rc = 1;
8925 return rc;
8929 /* Compute a suitable window start for window W if display of W starts
8930 on a continuation line. Value is non-zero if a new window start
8931 was computed.
8933 The new window start will be computed, based on W's width, starting
8934 from the start of the continued line. It is the start of the
8935 screen line with the minimum distance from the old start W->start. */
8937 static int
8938 compute_window_start_on_continuation_line (w)
8939 struct window *w;
8941 struct text_pos pos, start_pos;
8942 int window_start_changed_p = 0;
8944 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8946 /* If window start is on a continuation line... Window start may be
8947 < BEGV in case there's invisible text at the start of the
8948 buffer (M-x rmail, for example). */
8949 if (CHARPOS (start_pos) > BEGV
8950 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8952 struct it it;
8953 struct glyph_row *row;
8955 /* Handle the case that the window start is out of range. */
8956 if (CHARPOS (start_pos) < BEGV)
8957 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8958 else if (CHARPOS (start_pos) > ZV)
8959 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8961 /* Find the start of the continued line. This should be fast
8962 because scan_buffer is fast (newline cache). */
8963 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8964 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8965 row, DEFAULT_FACE_ID);
8966 reseat_at_previous_visible_line_start (&it);
8968 /* If the line start is "too far" away from the window start,
8969 say it takes too much time to compute a new window start. */
8970 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8971 < XFASTINT (w->height) * XFASTINT (w->width))
8973 int min_distance, distance;
8975 /* Move forward by display lines to find the new window
8976 start. If window width was enlarged, the new start can
8977 be expected to be > the old start. If window width was
8978 decreased, the new window start will be < the old start.
8979 So, we're looking for the display line start with the
8980 minimum distance from the old window start. */
8981 pos = it.current.pos;
8982 min_distance = INFINITY;
8983 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8984 distance < min_distance)
8986 min_distance = distance;
8987 pos = it.current.pos;
8988 move_it_by_lines (&it, 1, 0);
8991 /* Set the window start there. */
8992 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8993 window_start_changed_p = 1;
8997 return window_start_changed_p;
9001 /* Try cursor movement in case text has not changes in window WINDOW,
9002 with window start STARTP. Value is
9004 1 if successful
9006 0 if this method cannot be used
9008 -1 if we know we have to scroll the display. *SCROLL_STEP is
9009 set to 1, under certain circumstances, if we want to scroll as
9010 if scroll-step were set to 1. See the code. */
9012 static int
9013 try_cursor_movement (window, startp, scroll_step)
9014 Lisp_Object window;
9015 struct text_pos startp;
9016 int *scroll_step;
9018 struct window *w = XWINDOW (window);
9019 struct frame *f = XFRAME (w->frame);
9020 int rc = 0;
9022 /* Handle case where text has not changed, only point, and it has
9023 not moved off the frame. */
9024 if (/* Point may be in this window. */
9025 PT >= CHARPOS (startp)
9026 /* If we don't check this, we are called to move the cursor in a
9027 horizontally split window with a current matrix that doesn't
9028 fit the display. */
9029 && !windows_or_buffers_changed
9030 /* Selective display hasn't changed. */
9031 && !current_buffer->clip_changed
9032 /* If force-mode-line-update was called, really redisplay;
9033 that's how redisplay is forced after e.g. changing
9034 buffer-invisibility-spec. */
9035 && NILP (w->update_mode_line)
9036 /* Can't use this case if highlighting a region. When a
9037 region exists, cursor movement has to do more than just
9038 set the cursor. */
9039 && !(!NILP (Vtransient_mark_mode)
9040 && !NILP (current_buffer->mark_active))
9041 && NILP (w->region_showing)
9042 && NILP (Vshow_trailing_whitespace)
9043 /* Right after splitting windows, last_point may be nil. */
9044 && INTEGERP (w->last_point)
9045 /* This code is not used for mini-buffer for the sake of the case
9046 of redisplaying to replace an echo area message; since in
9047 that case the mini-buffer contents per se are usually
9048 unchanged. This code is of no real use in the mini-buffer
9049 since the handling of this_line_start_pos, etc., in redisplay
9050 handles the same cases. */
9051 && !EQ (window, minibuf_window)
9052 /* When splitting windows or for new windows, it happens that
9053 redisplay is called with a nil window_end_vpos or one being
9054 larger than the window. This should really be fixed in
9055 window.c. I don't have this on my list, now, so we do
9056 approximately the same as the old redisplay code. --gerd. */
9057 && INTEGERP (w->window_end_vpos)
9058 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9059 && (FRAME_WINDOW_P (f)
9060 || !MARKERP (Voverlay_arrow_position)
9061 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9063 int this_scroll_margin;
9064 struct glyph_row *row;
9066 #if GLYPH_DEBUG
9067 debug_method_add (w, "cursor movement");
9068 #endif
9070 /* Scroll if point within this distance from the top or bottom
9071 of the window. This is a pixel value. */
9072 this_scroll_margin = max (0, scroll_margin);
9073 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9074 this_scroll_margin *= CANON_Y_UNIT (f);
9076 /* Start with the row the cursor was displayed during the last
9077 not paused redisplay. Give up if that row is not valid. */
9078 if (w->last_cursor.vpos < 0
9079 || w->last_cursor.vpos >= w->current_matrix->nrows)
9080 rc = -1;
9081 else
9083 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9084 if (row->mode_line_p)
9085 ++row;
9086 if (!row->enabled_p)
9087 rc = -1;
9090 if (rc == 0)
9092 int scroll_p = 0;
9093 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9096 if (PT > XFASTINT (w->last_point))
9098 /* Point has moved forward. */
9099 while (MATRIX_ROW_END_CHARPOS (row) < PT
9100 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9102 xassert (row->enabled_p);
9103 ++row;
9106 /* The end position of a row equals the start position
9107 of the next row. If PT is there, we would rather
9108 display it in the next line. */
9109 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9110 && MATRIX_ROW_END_CHARPOS (row) == PT
9111 && !cursor_row_p (w, row))
9112 ++row;
9114 /* If within the scroll margin, scroll. Note that
9115 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9116 the next line would be drawn, and that
9117 this_scroll_margin can be zero. */
9118 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9119 || PT > MATRIX_ROW_END_CHARPOS (row)
9120 /* Line is completely visible last line in window
9121 and PT is to be set in the next line. */
9122 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9123 && PT == MATRIX_ROW_END_CHARPOS (row)
9124 && !row->ends_at_zv_p
9125 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9126 scroll_p = 1;
9128 else if (PT < XFASTINT (w->last_point))
9130 /* Cursor has to be moved backward. Note that PT >=
9131 CHARPOS (startp) because of the outer
9132 if-statement. */
9133 while (!row->mode_line_p
9134 && (MATRIX_ROW_START_CHARPOS (row) > PT
9135 || (MATRIX_ROW_START_CHARPOS (row) == PT
9136 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9137 && (row->y > this_scroll_margin
9138 || CHARPOS (startp) == BEGV))
9140 xassert (row->enabled_p);
9141 --row;
9144 /* Consider the following case: Window starts at BEGV,
9145 there is invisible, intangible text at BEGV, so that
9146 display starts at some point START > BEGV. It can
9147 happen that we are called with PT somewhere between
9148 BEGV and START. Try to handle that case. */
9149 if (row < w->current_matrix->rows
9150 || row->mode_line_p)
9152 row = w->current_matrix->rows;
9153 if (row->mode_line_p)
9154 ++row;
9157 /* Due to newlines in overlay strings, we may have to
9158 skip forward over overlay strings. */
9159 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9160 && MATRIX_ROW_END_CHARPOS (row) == PT
9161 && !cursor_row_p (w, row))
9162 ++row;
9164 /* If within the scroll margin, scroll. */
9165 if (row->y < this_scroll_margin
9166 && CHARPOS (startp) != BEGV)
9167 scroll_p = 1;
9170 if (PT < MATRIX_ROW_START_CHARPOS (row)
9171 || PT > MATRIX_ROW_END_CHARPOS (row))
9173 /* if PT is not in the glyph row, give up. */
9174 rc = -1;
9176 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9178 /* If we end up in a partially visible line, let's make it
9179 fully visible, except when it's taller than the window,
9180 in which case we can't do much about it. */
9181 if (row->height > window_box_height (w))
9183 *scroll_step = 1;
9184 rc = -1;
9186 else
9188 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9189 try_window (window, startp);
9190 make_cursor_line_fully_visible (w);
9191 rc = 1;
9194 else if (scroll_p)
9195 rc = -1;
9196 else
9198 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9199 rc = 1;
9204 return rc;
9208 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9209 selected_window is redisplayed. */
9211 static void
9212 redisplay_window (window, just_this_one_p)
9213 Lisp_Object window;
9214 int just_this_one_p;
9216 struct window *w = XWINDOW (window);
9217 struct frame *f = XFRAME (w->frame);
9218 struct buffer *buffer = XBUFFER (w->buffer);
9219 struct buffer *old = current_buffer;
9220 struct text_pos lpoint, opoint, startp;
9221 int update_mode_line;
9222 int tem;
9223 struct it it;
9224 /* Record it now because it's overwritten. */
9225 int current_matrix_up_to_date_p = 0;
9226 int temp_scroll_step = 0;
9227 int count = specpdl_ptr - specpdl;
9228 int rc;
9230 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9231 opoint = lpoint;
9233 /* W must be a leaf window here. */
9234 xassert (!NILP (w->buffer));
9235 #if GLYPH_DEBUG
9236 *w->desired_matrix->method = 0;
9237 #endif
9239 specbind (Qinhibit_point_motion_hooks, Qt);
9241 reconsider_clip_changes (w, buffer);
9243 /* Has the mode line to be updated? */
9244 update_mode_line = (!NILP (w->update_mode_line)
9245 || update_mode_lines
9246 || buffer->clip_changed);
9248 if (MINI_WINDOW_P (w))
9250 if (w == XWINDOW (echo_area_window)
9251 && !NILP (echo_area_buffer[0]))
9253 if (update_mode_line)
9254 /* We may have to update a tty frame's menu bar or a
9255 tool-bar. Example `M-x C-h C-h C-g'. */
9256 goto finish_menu_bars;
9257 else
9258 /* We've already displayed the echo area glyphs in this window. */
9259 goto finish_scroll_bars;
9261 else if (w != XWINDOW (minibuf_window))
9263 /* W is a mini-buffer window, but it's not the currently
9264 active one, so clear it. */
9265 int yb = window_text_bottom_y (w);
9266 struct glyph_row *row;
9267 int y;
9269 for (y = 0, row = w->desired_matrix->rows;
9270 y < yb;
9271 y += row->height, ++row)
9272 blank_row (w, row, y);
9273 goto finish_scroll_bars;
9277 /* Otherwise set up data on this window; select its buffer and point
9278 value. */
9279 /* Really select the buffer, for the sake of buffer-local
9280 variables. */
9281 set_buffer_internal_1 (XBUFFER (w->buffer));
9282 SET_TEXT_POS (opoint, PT, PT_BYTE);
9284 current_matrix_up_to_date_p
9285 = (!NILP (w->window_end_valid)
9286 && !current_buffer->clip_changed
9287 && XFASTINT (w->last_modified) >= MODIFF
9288 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9290 /* When windows_or_buffers_changed is non-zero, we can't rely on
9291 the window end being valid, so set it to nil there. */
9292 if (windows_or_buffers_changed)
9294 /* If window starts on a continuation line, maybe adjust the
9295 window start in case the window's width changed. */
9296 if (XMARKER (w->start)->buffer == current_buffer)
9297 compute_window_start_on_continuation_line (w);
9299 w->window_end_valid = Qnil;
9302 /* Some sanity checks. */
9303 CHECK_WINDOW_END (w);
9304 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9305 abort ();
9306 if (BYTEPOS (opoint) < CHARPOS (opoint))
9307 abort ();
9309 /* If %c is in mode line, update it if needed. */
9310 if (!NILP (w->column_number_displayed)
9311 /* This alternative quickly identifies a common case
9312 where no change is needed. */
9313 && !(PT == XFASTINT (w->last_point)
9314 && XFASTINT (w->last_modified) >= MODIFF
9315 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9316 && XFASTINT (w->column_number_displayed) != current_column ())
9317 update_mode_line = 1;
9319 /* Count number of windows showing the selected buffer. An indirect
9320 buffer counts as its base buffer. */
9321 if (!just_this_one_p)
9323 struct buffer *current_base, *window_base;
9324 current_base = current_buffer;
9325 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9326 if (current_base->base_buffer)
9327 current_base = current_base->base_buffer;
9328 if (window_base->base_buffer)
9329 window_base = window_base->base_buffer;
9330 if (current_base == window_base)
9331 buffer_shared++;
9334 /* Point refers normally to the selected window. For any other
9335 window, set up appropriate value. */
9336 if (!EQ (window, selected_window))
9338 int new_pt = XMARKER (w->pointm)->charpos;
9339 int new_pt_byte = marker_byte_position (w->pointm);
9340 if (new_pt < BEGV)
9342 new_pt = BEGV;
9343 new_pt_byte = BEGV_BYTE;
9344 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9346 else if (new_pt > (ZV - 1))
9348 new_pt = ZV;
9349 new_pt_byte = ZV_BYTE;
9350 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9353 /* We don't use SET_PT so that the point-motion hooks don't run. */
9354 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9357 /* If any of the character widths specified in the display table
9358 have changed, invalidate the width run cache. It's true that
9359 this may be a bit late to catch such changes, but the rest of
9360 redisplay goes (non-fatally) haywire when the display table is
9361 changed, so why should we worry about doing any better? */
9362 if (current_buffer->width_run_cache)
9364 struct Lisp_Char_Table *disptab = buffer_display_table ();
9366 if (! disptab_matches_widthtab (disptab,
9367 XVECTOR (current_buffer->width_table)))
9369 invalidate_region_cache (current_buffer,
9370 current_buffer->width_run_cache,
9371 BEG, Z);
9372 recompute_width_table (current_buffer, disptab);
9376 /* If window-start is screwed up, choose a new one. */
9377 if (XMARKER (w->start)->buffer != current_buffer)
9378 goto recenter;
9380 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9382 /* If someone specified a new starting point but did not insist,
9383 check whether it can be used. */
9384 if (!NILP (w->optional_new_start)
9385 && CHARPOS (startp) >= BEGV
9386 && CHARPOS (startp) <= ZV)
9388 w->optional_new_start = Qnil;
9389 start_display (&it, w, startp);
9390 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9391 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9392 if (IT_CHARPOS (it) == PT)
9393 w->force_start = Qt;
9396 /* Handle case where place to start displaying has been specified,
9397 unless the specified location is outside the accessible range. */
9398 if (!NILP (w->force_start)
9399 || w->frozen_window_start_p)
9401 w->force_start = Qnil;
9402 w->vscroll = 0;
9403 w->window_end_valid = Qnil;
9405 /* Forget any recorded base line for line number display. */
9406 if (!current_matrix_up_to_date_p
9407 || current_buffer->clip_changed)
9408 w->base_line_number = Qnil;
9410 /* Redisplay the mode line. Select the buffer properly for that.
9411 Also, run the hook window-scroll-functions
9412 because we have scrolled. */
9413 /* Note, we do this after clearing force_start because
9414 if there's an error, it is better to forget about force_start
9415 than to get into an infinite loop calling the hook functions
9416 and having them get more errors. */
9417 if (!update_mode_line
9418 || ! NILP (Vwindow_scroll_functions))
9420 update_mode_line = 1;
9421 w->update_mode_line = Qt;
9422 startp = run_window_scroll_functions (window, startp);
9425 XSETFASTINT (w->last_modified, 0);
9426 XSETFASTINT (w->last_overlay_modified, 0);
9427 if (CHARPOS (startp) < BEGV)
9428 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9429 else if (CHARPOS (startp) > ZV)
9430 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9432 /* Redisplay, then check if cursor has been set during the
9433 redisplay. Give up if new fonts were loaded. */
9434 if (!try_window (window, startp))
9436 w->force_start = Qt;
9437 clear_glyph_matrix (w->desired_matrix);
9438 goto restore_buffers;
9441 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9443 /* If point does not appear, try to move point so it does
9444 appear. The desired matrix has been built above, so we
9445 can use it here. */
9446 int window_height;
9447 struct glyph_row *row;
9449 window_height = window_box_height (w) / 2;
9450 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9451 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9452 ++row;
9454 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9455 MATRIX_ROW_START_BYTEPOS (row));
9457 if (w != XWINDOW (selected_window))
9458 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9459 else if (current_buffer == old)
9460 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9462 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9464 /* If we are highlighting the region, then we just changed
9465 the region, so redisplay to show it. */
9466 if (!NILP (Vtransient_mark_mode)
9467 && !NILP (current_buffer->mark_active))
9469 clear_glyph_matrix (w->desired_matrix);
9470 if (!try_window (window, startp))
9471 goto restore_buffers;
9475 make_cursor_line_fully_visible (w);
9476 #if GLYPH_DEBUG
9477 debug_method_add (w, "forced window start");
9478 #endif
9479 goto done;
9482 /* Handle case where text has not changed, only point, and it has
9483 not moved off the frame. */
9484 if (current_matrix_up_to_date_p
9485 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9486 rc != 0))
9488 if (rc == -1)
9489 goto try_to_scroll;
9490 else
9491 goto done;
9493 /* If current starting point was originally the beginning of a line
9494 but no longer is, find a new starting point. */
9495 else if (!NILP (w->start_at_line_beg)
9496 && !(CHARPOS (startp) <= BEGV
9497 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9499 #if GLYPH_DEBUG
9500 debug_method_add (w, "recenter 1");
9501 #endif
9502 goto recenter;
9505 /* Try scrolling with try_window_id. */
9506 else if (/* Windows and buffers haven't changed. */
9507 !windows_or_buffers_changed
9508 /* Window must be either use window-based redisplay or
9509 be full width. */
9510 && (FRAME_WINDOW_P (f)
9511 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9512 && !MINI_WINDOW_P (w)
9513 /* Point is not known NOT to appear in window. */
9514 && PT >= CHARPOS (startp)
9515 && XFASTINT (w->last_modified)
9516 /* Window is not hscrolled. */
9517 && XFASTINT (w->hscroll) == 0
9518 /* Selective display has not changed. */
9519 && !current_buffer->clip_changed
9520 /* Current matrix is up to date. */
9521 && !NILP (w->window_end_valid)
9522 /* Can't use this case if highlighting a region because
9523 a cursor movement will do more than just set the cursor. */
9524 && !(!NILP (Vtransient_mark_mode)
9525 && !NILP (current_buffer->mark_active))
9526 && NILP (w->region_showing)
9527 && NILP (Vshow_trailing_whitespace)
9528 /* Overlay arrow position and string not changed. */
9529 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9530 && EQ (last_arrow_string, Voverlay_arrow_string)
9531 /* Value is > 0 if update has been done, it is -1 if we
9532 know that the same window start will not work. It is 0
9533 if unsuccessful for some other reason. */
9534 && (tem = try_window_id (w)) != 0)
9536 #if GLYPH_DEBUG
9537 debug_method_add (w, "try_window_id %d", tem);
9538 #endif
9540 if (fonts_changed_p)
9541 goto restore_buffers;
9542 if (tem > 0)
9543 goto done;
9545 /* Otherwise try_window_id has returned -1 which means that we
9546 don't want the alternative below this comment to execute. */
9548 else if (CHARPOS (startp) >= BEGV
9549 && CHARPOS (startp) <= ZV
9550 && PT >= CHARPOS (startp)
9551 && (CHARPOS (startp) < ZV
9552 /* Avoid starting at end of buffer. */
9553 || CHARPOS (startp) == BEGV
9554 || (XFASTINT (w->last_modified) >= MODIFF
9555 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9557 #if GLYPH_DEBUG
9558 debug_method_add (w, "same window start");
9559 #endif
9561 /* Try to redisplay starting at same place as before.
9562 If point has not moved off frame, accept the results. */
9563 if (!current_matrix_up_to_date_p
9564 /* Don't use try_window_reusing_current_matrix in this case
9565 because a window scroll function can have changed the
9566 buffer. */
9567 || !NILP (Vwindow_scroll_functions)
9568 || MINI_WINDOW_P (w)
9569 || !try_window_reusing_current_matrix (w))
9571 IF_DEBUG (debug_method_add (w, "1"));
9572 try_window (window, startp);
9575 if (fonts_changed_p)
9576 goto restore_buffers;
9578 if (w->cursor.vpos >= 0)
9580 if (!just_this_one_p
9581 || current_buffer->clip_changed
9582 || BEG_UNCHANGED < CHARPOS (startp))
9583 /* Forget any recorded base line for line number display. */
9584 w->base_line_number = Qnil;
9586 make_cursor_line_fully_visible (w);
9587 goto done;
9589 else
9590 clear_glyph_matrix (w->desired_matrix);
9593 try_to_scroll:
9595 XSETFASTINT (w->last_modified, 0);
9596 XSETFASTINT (w->last_overlay_modified, 0);
9598 /* Redisplay the mode line. Select the buffer properly for that. */
9599 if (!update_mode_line)
9601 update_mode_line = 1;
9602 w->update_mode_line = Qt;
9605 /* Try to scroll by specified few lines. */
9606 if ((scroll_conservatively
9607 || scroll_step
9608 || temp_scroll_step
9609 || NUMBERP (current_buffer->scroll_up_aggressively)
9610 || NUMBERP (current_buffer->scroll_down_aggressively))
9611 && !current_buffer->clip_changed
9612 && CHARPOS (startp) >= BEGV
9613 && CHARPOS (startp) <= ZV)
9615 /* The function returns -1 if new fonts were loaded, 1 if
9616 successful, 0 if not successful. */
9617 int rc = try_scrolling (window, just_this_one_p,
9618 scroll_conservatively,
9619 scroll_step,
9620 temp_scroll_step);
9621 if (rc > 0)
9622 goto done;
9623 else if (rc < 0)
9624 goto restore_buffers;
9627 /* Finally, just choose place to start which centers point */
9629 recenter:
9631 #if GLYPH_DEBUG
9632 debug_method_add (w, "recenter");
9633 #endif
9635 /* w->vscroll = 0; */
9637 /* Forget any previously recorded base line for line number display. */
9638 if (!current_matrix_up_to_date_p
9639 || current_buffer->clip_changed)
9640 w->base_line_number = Qnil;
9642 /* Move backward half the height of the window. */
9643 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9644 it.current_y = it.last_visible_y;
9645 move_it_vertically_backward (&it, it.last_visible_y / 2);
9646 xassert (IT_CHARPOS (it) >= BEGV);
9648 /* The function move_it_vertically_backward may move over more
9649 than the specified y-distance. If it->w is small, e.g. a
9650 mini-buffer window, we may end up in front of the window's
9651 display area. Start displaying at the start of the line
9652 containing PT in this case. */
9653 if (it.current_y <= 0)
9655 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9656 move_it_vertically (&it, 0);
9657 xassert (IT_CHARPOS (it) <= PT);
9658 it.current_y = 0;
9661 it.current_x = it.hpos = 0;
9663 /* Set startp here explicitly in case that helps avoid an infinite loop
9664 in case the window-scroll-functions functions get errors. */
9665 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9667 /* Run scroll hooks. */
9668 startp = run_window_scroll_functions (window, it.current.pos);
9670 /* Redisplay the window. */
9671 if (!current_matrix_up_to_date_p
9672 || windows_or_buffers_changed
9673 /* Don't use try_window_reusing_current_matrix in this case
9674 because it can have changed the buffer. */
9675 || !NILP (Vwindow_scroll_functions)
9676 || !just_this_one_p
9677 || MINI_WINDOW_P (w)
9678 || !try_window_reusing_current_matrix (w))
9679 try_window (window, startp);
9681 /* If new fonts have been loaded (due to fontsets), give up. We
9682 have to start a new redisplay since we need to re-adjust glyph
9683 matrices. */
9684 if (fonts_changed_p)
9685 goto restore_buffers;
9687 /* If cursor did not appear assume that the middle of the window is
9688 in the first line of the window. Do it again with the next line.
9689 (Imagine a window of height 100, displaying two lines of height
9690 60. Moving back 50 from it->last_visible_y will end in the first
9691 line.) */
9692 if (w->cursor.vpos < 0)
9694 if (!NILP (w->window_end_valid)
9695 && PT >= Z - XFASTINT (w->window_end_pos))
9697 clear_glyph_matrix (w->desired_matrix);
9698 move_it_by_lines (&it, 1, 0);
9699 try_window (window, it.current.pos);
9701 else if (PT < IT_CHARPOS (it))
9703 clear_glyph_matrix (w->desired_matrix);
9704 move_it_by_lines (&it, -1, 0);
9705 try_window (window, it.current.pos);
9707 else
9709 /* Not much we can do about it. */
9713 /* Consider the following case: Window starts at BEGV, there is
9714 invisible, intangible text at BEGV, so that display starts at
9715 some point START > BEGV. It can happen that we are called with
9716 PT somewhere between BEGV and START. Try to handle that case. */
9717 if (w->cursor.vpos < 0)
9719 struct glyph_row *row = w->current_matrix->rows;
9720 if (row->mode_line_p)
9721 ++row;
9722 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9725 make_cursor_line_fully_visible (w);
9727 done:
9729 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9730 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9731 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9732 ? Qt : Qnil);
9734 /* Display the mode line, if we must. */
9735 if ((update_mode_line
9736 /* If window not full width, must redo its mode line
9737 if (a) the window to its side is being redone and
9738 (b) we do a frame-based redisplay. This is a consequence
9739 of how inverted lines are drawn in frame-based redisplay. */
9740 || (!just_this_one_p
9741 && !FRAME_WINDOW_P (f)
9742 && !WINDOW_FULL_WIDTH_P (w))
9743 /* Line number to display. */
9744 || INTEGERP (w->base_line_pos)
9745 /* Column number is displayed and different from the one displayed. */
9746 || (!NILP (w->column_number_displayed)
9747 && XFASTINT (w->column_number_displayed) != current_column ()))
9748 /* This means that the window has a mode line. */
9749 && (WINDOW_WANTS_MODELINE_P (w)
9750 || WINDOW_WANTS_HEADER_LINE_P (w)))
9752 Lisp_Object old_selected_frame;
9754 old_selected_frame = selected_frame;
9756 XSETFRAME (selected_frame, f);
9757 display_mode_lines (w);
9758 selected_frame = old_selected_frame;
9760 /* If mode line height has changed, arrange for a thorough
9761 immediate redisplay using the correct mode line height. */
9762 if (WINDOW_WANTS_MODELINE_P (w)
9763 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9765 fonts_changed_p = 1;
9766 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9767 = DESIRED_MODE_LINE_HEIGHT (w);
9770 /* If top line height has changed, arrange for a thorough
9771 immediate redisplay using the correct mode line height. */
9772 if (WINDOW_WANTS_HEADER_LINE_P (w)
9773 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9775 fonts_changed_p = 1;
9776 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9777 = DESIRED_HEADER_LINE_HEIGHT (w);
9780 if (fonts_changed_p)
9781 goto restore_buffers;
9784 if (!line_number_displayed
9785 && !BUFFERP (w->base_line_pos))
9787 w->base_line_pos = Qnil;
9788 w->base_line_number = Qnil;
9791 finish_menu_bars:
9793 /* When we reach a frame's selected window, redo the frame's menu bar. */
9794 if (update_mode_line
9795 && EQ (FRAME_SELECTED_WINDOW (f), window))
9797 int redisplay_menu_p = 0;
9799 if (FRAME_WINDOW_P (f))
9801 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9802 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9803 #else
9804 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9805 #endif
9807 else
9808 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9810 if (redisplay_menu_p)
9811 display_menu_bar (w);
9813 #ifdef HAVE_WINDOW_SYSTEM
9814 if (WINDOWP (f->tool_bar_window)
9815 && (FRAME_TOOL_BAR_LINES (f) > 0
9816 || auto_resize_tool_bars_p))
9817 redisplay_tool_bar (f);
9818 #endif
9821 finish_scroll_bars:
9823 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9825 int start, end, whole;
9827 /* Calculate the start and end positions for the current window.
9828 At some point, it would be nice to choose between scrollbars
9829 which reflect the whole buffer size, with special markers
9830 indicating narrowing, and scrollbars which reflect only the
9831 visible region.
9833 Note that mini-buffers sometimes aren't displaying any text. */
9834 if (!MINI_WINDOW_P (w)
9835 || (w == XWINDOW (minibuf_window)
9836 && NILP (echo_area_buffer[0])))
9838 whole = ZV - BEGV;
9839 start = marker_position (w->start) - BEGV;
9840 /* I don't think this is guaranteed to be right. For the
9841 moment, we'll pretend it is. */
9842 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9844 if (end < start)
9845 end = start;
9846 if (whole < (end - start))
9847 whole = end - start;
9849 else
9850 start = end = whole = 0;
9852 /* Indicate what this scroll bar ought to be displaying now. */
9853 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9855 /* Note that we actually used the scroll bar attached to this
9856 window, so it shouldn't be deleted at the end of redisplay. */
9857 (*redeem_scroll_bar_hook) (w);
9860 restore_buffers:
9862 /* Restore current_buffer and value of point in it. */
9863 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9864 set_buffer_internal_1 (old);
9865 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9867 unbind_to (count, Qnil);
9871 /* Build the complete desired matrix of WINDOW with a window start
9872 buffer position POS. Value is non-zero if successful. It is zero
9873 if fonts were loaded during redisplay which makes re-adjusting
9874 glyph matrices necessary. */
9877 try_window (window, pos)
9878 Lisp_Object window;
9879 struct text_pos pos;
9881 struct window *w = XWINDOW (window);
9882 struct it it;
9883 struct glyph_row *last_text_row = NULL;
9885 /* Make POS the new window start. */
9886 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9888 /* Mark cursor position as unknown. No overlay arrow seen. */
9889 w->cursor.vpos = -1;
9890 overlay_arrow_seen = 0;
9892 /* Initialize iterator and info to start at POS. */
9893 start_display (&it, w, pos);
9895 /* Display all lines of W. */
9896 while (it.current_y < it.last_visible_y)
9898 if (display_line (&it))
9899 last_text_row = it.glyph_row - 1;
9900 if (fonts_changed_p)
9901 return 0;
9904 /* If bottom moved off end of frame, change mode line percentage. */
9905 if (XFASTINT (w->window_end_pos) <= 0
9906 && Z != IT_CHARPOS (it))
9907 w->update_mode_line = Qt;
9909 /* Set window_end_pos to the offset of the last character displayed
9910 on the window from the end of current_buffer. Set
9911 window_end_vpos to its row number. */
9912 if (last_text_row)
9914 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9915 w->window_end_bytepos
9916 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9917 XSETFASTINT (w->window_end_pos,
9918 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9919 XSETFASTINT (w->window_end_vpos,
9920 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9921 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9922 ->displays_text_p);
9924 else
9926 w->window_end_bytepos = 0;
9927 XSETFASTINT (w->window_end_pos, 0);
9928 XSETFASTINT (w->window_end_vpos, 0);
9931 /* But that is not valid info until redisplay finishes. */
9932 w->window_end_valid = Qnil;
9933 return 1;
9938 /************************************************************************
9939 Window redisplay reusing current matrix when buffer has not changed
9940 ************************************************************************/
9942 /* Try redisplay of window W showing an unchanged buffer with a
9943 different window start than the last time it was displayed by
9944 reusing its current matrix. Value is non-zero if successful.
9945 W->start is the new window start. */
9947 static int
9948 try_window_reusing_current_matrix (w)
9949 struct window *w;
9951 struct frame *f = XFRAME (w->frame);
9952 struct glyph_row *row, *bottom_row;
9953 struct it it;
9954 struct run run;
9955 struct text_pos start, new_start;
9956 int nrows_scrolled, i;
9957 struct glyph_row *last_text_row;
9958 struct glyph_row *last_reused_text_row;
9959 struct glyph_row *start_row;
9960 int start_vpos, min_y, max_y;
9962 if (/* This function doesn't handle terminal frames. */
9963 !FRAME_WINDOW_P (f)
9964 /* Don't try to reuse the display if windows have been split
9965 or such. */
9966 || windows_or_buffers_changed)
9967 return 0;
9969 /* Can't do this if region may have changed. */
9970 if ((!NILP (Vtransient_mark_mode)
9971 && !NILP (current_buffer->mark_active))
9972 || !NILP (w->region_showing)
9973 || !NILP (Vshow_trailing_whitespace))
9974 return 0;
9976 /* If top-line visibility has changed, give up. */
9977 if (WINDOW_WANTS_HEADER_LINE_P (w)
9978 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9979 return 0;
9981 /* Give up if old or new display is scrolled vertically. We could
9982 make this function handle this, but right now it doesn't. */
9983 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9984 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9985 return 0;
9987 /* The variable new_start now holds the new window start. The old
9988 start `start' can be determined from the current matrix. */
9989 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9990 start = start_row->start.pos;
9991 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9993 /* Clear the desired matrix for the display below. */
9994 clear_glyph_matrix (w->desired_matrix);
9996 if (CHARPOS (new_start) <= CHARPOS (start))
9998 int first_row_y;
10000 IF_DEBUG (debug_method_add (w, "twu1"));
10002 /* Display up to a row that can be reused. The variable
10003 last_text_row is set to the last row displayed that displays
10004 text. Note that it.vpos == 0 if or if not there is a
10005 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10006 start_display (&it, w, new_start);
10007 first_row_y = it.current_y;
10008 w->cursor.vpos = -1;
10009 last_text_row = last_reused_text_row = NULL;
10011 while (it.current_y < it.last_visible_y
10012 && IT_CHARPOS (it) < CHARPOS (start)
10013 && !fonts_changed_p)
10014 if (display_line (&it))
10015 last_text_row = it.glyph_row - 1;
10017 /* A value of current_y < last_visible_y means that we stopped
10018 at the previous window start, which in turn means that we
10019 have at least one reusable row. */
10020 if (it.current_y < it.last_visible_y)
10022 /* IT.vpos always starts from 0; it counts text lines. */
10023 nrows_scrolled = it.vpos;
10025 /* Find PT if not already found in the lines displayed. */
10026 if (w->cursor.vpos < 0)
10028 int dy = it.current_y - first_row_y;
10030 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10031 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10033 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10034 && PT < MATRIX_ROW_END_CHARPOS (row))
10036 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10037 dy, nrows_scrolled);
10038 break;
10041 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10042 break;
10044 ++row;
10047 /* Give up if point was not found. This shouldn't
10048 happen often; not more often than with try_window
10049 itself. */
10050 if (w->cursor.vpos < 0)
10052 clear_glyph_matrix (w->desired_matrix);
10053 return 0;
10057 /* Scroll the display. Do it before the current matrix is
10058 changed. The problem here is that update has not yet
10059 run, i.e. part of the current matrix is not up to date.
10060 scroll_run_hook will clear the cursor, and use the
10061 current matrix to get the height of the row the cursor is
10062 in. */
10063 run.current_y = first_row_y;
10064 run.desired_y = it.current_y;
10065 run.height = it.last_visible_y - it.current_y;
10067 if (run.height > 0 && run.current_y != run.desired_y)
10069 update_begin (f);
10070 rif->update_window_begin_hook (w);
10071 rif->clear_mouse_face (w);
10072 rif->scroll_run_hook (w, &run);
10073 rif->update_window_end_hook (w, 0, 0);
10074 update_end (f);
10077 /* Shift current matrix down by nrows_scrolled lines. */
10078 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10079 rotate_matrix (w->current_matrix,
10080 start_vpos,
10081 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10082 nrows_scrolled);
10084 /* Disable lines not reused. */
10085 for (i = 0; i < it.vpos; ++i)
10086 (start_row + i)->enabled_p = 0;
10088 /* Re-compute Y positions. */
10089 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10090 max_y = it.last_visible_y;
10091 for (row = start_row + nrows_scrolled;
10092 row < bottom_row;
10093 ++row)
10095 row->y = it.current_y;
10097 if (row->y < min_y)
10098 row->visible_height = row->height - (min_y - row->y);
10099 else if (row->y + row->height > max_y)
10100 row->visible_height
10101 = row->height - (row->y + row->height - max_y);
10102 else
10103 row->visible_height = row->height;
10105 it.current_y += row->height;
10107 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10108 last_reused_text_row = row;
10109 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10110 break;
10114 /* Update window_end_pos etc.; last_reused_text_row is the last
10115 reused row from the current matrix containing text, if any.
10116 The value of last_text_row is the last displayed line
10117 containing text. */
10118 if (last_reused_text_row)
10120 w->window_end_bytepos
10121 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10122 XSETFASTINT (w->window_end_pos,
10123 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10124 XSETFASTINT (w->window_end_vpos,
10125 MATRIX_ROW_VPOS (last_reused_text_row,
10126 w->current_matrix));
10128 else if (last_text_row)
10130 w->window_end_bytepos
10131 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10132 XSETFASTINT (w->window_end_pos,
10133 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10134 XSETFASTINT (w->window_end_vpos,
10135 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10137 else
10139 /* This window must be completely empty. */
10140 w->window_end_bytepos = 0;
10141 XSETFASTINT (w->window_end_pos, 0);
10142 XSETFASTINT (w->window_end_vpos, 0);
10144 w->window_end_valid = Qnil;
10146 /* Update hint: don't try scrolling again in update_window. */
10147 w->desired_matrix->no_scrolling_p = 1;
10149 #if GLYPH_DEBUG
10150 debug_method_add (w, "try_window_reusing_current_matrix 1");
10151 #endif
10152 return 1;
10154 else if (CHARPOS (new_start) > CHARPOS (start))
10156 struct glyph_row *pt_row, *row;
10157 struct glyph_row *first_reusable_row;
10158 struct glyph_row *first_row_to_display;
10159 int dy;
10160 int yb = window_text_bottom_y (w);
10162 IF_DEBUG (debug_method_add (w, "twu2"));
10164 /* Find the row starting at new_start, if there is one. Don't
10165 reuse a partially visible line at the end. */
10166 first_reusable_row = start_row;
10167 while (first_reusable_row->enabled_p
10168 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10169 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10170 < CHARPOS (new_start)))
10171 ++first_reusable_row;
10173 /* Give up if there is no row to reuse. */
10174 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10175 || !first_reusable_row->enabled_p
10176 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10177 != CHARPOS (new_start)))
10178 return 0;
10180 /* We can reuse fully visible rows beginning with
10181 first_reusable_row to the end of the window. Set
10182 first_row_to_display to the first row that cannot be reused.
10183 Set pt_row to the row containing point, if there is any. */
10184 first_row_to_display = first_reusable_row;
10185 pt_row = NULL;
10186 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10188 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10189 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10190 pt_row = first_row_to_display;
10192 ++first_row_to_display;
10195 /* Start displaying at the start of first_row_to_display. */
10196 xassert (first_row_to_display->y < yb);
10197 init_to_row_start (&it, w, first_row_to_display);
10198 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10199 - start_vpos);
10200 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10201 - nrows_scrolled);
10202 it.current_y = (first_row_to_display->y - first_reusable_row->y
10203 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10205 /* Display lines beginning with first_row_to_display in the
10206 desired matrix. Set last_text_row to the last row displayed
10207 that displays text. */
10208 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10209 if (pt_row == NULL)
10210 w->cursor.vpos = -1;
10211 last_text_row = NULL;
10212 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10213 if (display_line (&it))
10214 last_text_row = it.glyph_row - 1;
10216 /* Give up If point isn't in a row displayed or reused. */
10217 if (w->cursor.vpos < 0)
10219 clear_glyph_matrix (w->desired_matrix);
10220 return 0;
10223 /* If point is in a reused row, adjust y and vpos of the cursor
10224 position. */
10225 if (pt_row)
10227 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10228 w->current_matrix);
10229 w->cursor.y -= first_reusable_row->y;
10232 /* Scroll the display. */
10233 run.current_y = first_reusable_row->y;
10234 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10235 run.height = it.last_visible_y - run.current_y;
10236 dy = run.current_y - run.desired_y;
10238 if (run.height)
10240 struct frame *f = XFRAME (WINDOW_FRAME (w));
10241 update_begin (f);
10242 rif->update_window_begin_hook (w);
10243 rif->clear_mouse_face (w);
10244 rif->scroll_run_hook (w, &run);
10245 rif->update_window_end_hook (w, 0, 0);
10246 update_end (f);
10249 /* Adjust Y positions of reused rows. */
10250 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10251 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10252 max_y = it.last_visible_y;
10253 for (row = first_reusable_row; row < first_row_to_display; ++row)
10255 row->y -= dy;
10256 if (row->y < min_y)
10257 row->visible_height = row->height - (min_y - row->y);
10258 else if (row->y + row->height > max_y)
10259 row->visible_height
10260 = row->height - (row->y + row->height - max_y);
10261 else
10262 row->visible_height = row->height;
10265 /* Disable rows not reused. */
10266 while (row < bottom_row)
10268 row->enabled_p = 0;
10269 ++row;
10272 /* Scroll the current matrix. */
10273 xassert (nrows_scrolled > 0);
10274 rotate_matrix (w->current_matrix,
10275 start_vpos,
10276 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10277 -nrows_scrolled);
10279 /* Adjust window end. A null value of last_text_row means that
10280 the window end is in reused rows which in turn means that
10281 only its vpos can have changed. */
10282 if (last_text_row)
10284 w->window_end_bytepos
10285 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10286 XSETFASTINT (w->window_end_pos,
10287 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10288 XSETFASTINT (w->window_end_vpos,
10289 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10291 else
10293 XSETFASTINT (w->window_end_vpos,
10294 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10297 w->window_end_valid = Qnil;
10298 w->desired_matrix->no_scrolling_p = 1;
10300 #if GLYPH_DEBUG
10301 debug_method_add (w, "try_window_reusing_current_matrix 2");
10302 #endif
10303 return 1;
10306 return 0;
10311 /************************************************************************
10312 Window redisplay reusing current matrix when buffer has changed
10313 ************************************************************************/
10315 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10316 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10317 int *, int *));
10318 static struct glyph_row *
10319 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10320 struct glyph_row *));
10323 /* Return the last row in MATRIX displaying text. If row START is
10324 non-null, start searching with that row. IT gives the dimensions
10325 of the display. Value is null if matrix is empty; otherwise it is
10326 a pointer to the row found. */
10328 static struct glyph_row *
10329 find_last_row_displaying_text (matrix, it, start)
10330 struct glyph_matrix *matrix;
10331 struct it *it;
10332 struct glyph_row *start;
10334 struct glyph_row *row, *row_found;
10336 /* Set row_found to the last row in IT->w's current matrix
10337 displaying text. The loop looks funny but think of partially
10338 visible lines. */
10339 row_found = NULL;
10340 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10341 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10343 xassert (row->enabled_p);
10344 row_found = row;
10345 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10346 break;
10347 ++row;
10350 return row_found;
10354 /* Return the last row in the current matrix of W that is not affected
10355 by changes at the start of current_buffer that occurred since the
10356 last time W was redisplayed. Value is null if no such row exists.
10358 The global variable beg_unchanged has to contain the number of
10359 bytes unchanged at the start of current_buffer. BEG +
10360 beg_unchanged is the buffer position of the first changed byte in
10361 current_buffer. Characters at positions < BEG + beg_unchanged are
10362 at the same buffer positions as they were when the current matrix
10363 was built. */
10365 static struct glyph_row *
10366 find_last_unchanged_at_beg_row (w)
10367 struct window *w;
10369 int first_changed_pos = BEG + BEG_UNCHANGED;
10370 struct glyph_row *row;
10371 struct glyph_row *row_found = NULL;
10372 int yb = window_text_bottom_y (w);
10374 /* Find the last row displaying unchanged text. */
10375 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10376 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10377 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10379 if (/* If row ends before first_changed_pos, it is unchanged,
10380 except in some case. */
10381 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10382 /* When row ends in ZV and we write at ZV it is not
10383 unchanged. */
10384 && !row->ends_at_zv_p
10385 /* When first_changed_pos is the end of a continued line,
10386 row is not unchanged because it may be no longer
10387 continued. */
10388 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10389 && row->continued_p))
10390 row_found = row;
10392 /* Stop if last visible row. */
10393 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10394 break;
10396 ++row;
10399 return row_found;
10403 /* Find the first glyph row in the current matrix of W that is not
10404 affected by changes at the end of current_buffer since the last
10405 time the window was redisplayed. Return in *DELTA the number of
10406 chars by which buffer positions in unchanged text at the end of
10407 current_buffer must be adjusted. Return in *DELTA_BYTES the
10408 corresponding number of bytes. Value is null if no such row
10409 exists, i.e. all rows are affected by changes. */
10411 static struct glyph_row *
10412 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10413 struct window *w;
10414 int *delta, *delta_bytes;
10416 struct glyph_row *row;
10417 struct glyph_row *row_found = NULL;
10419 *delta = *delta_bytes = 0;
10421 /* Display must not have been paused, otherwise the current matrix
10422 is not up to date. */
10423 if (NILP (w->window_end_valid))
10424 abort ();
10426 /* A value of window_end_pos >= END_UNCHANGED means that the window
10427 end is in the range of changed text. If so, there is no
10428 unchanged row at the end of W's current matrix. */
10429 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10430 return NULL;
10432 /* Set row to the last row in W's current matrix displaying text. */
10433 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10435 /* If matrix is entirely empty, no unchanged row exists. */
10436 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10438 /* The value of row is the last glyph row in the matrix having a
10439 meaningful buffer position in it. The end position of row
10440 corresponds to window_end_pos. This allows us to translate
10441 buffer positions in the current matrix to current buffer
10442 positions for characters not in changed text. */
10443 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10444 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10445 int last_unchanged_pos, last_unchanged_pos_old;
10446 struct glyph_row *first_text_row
10447 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10449 *delta = Z - Z_old;
10450 *delta_bytes = Z_BYTE - Z_BYTE_old;
10452 /* Set last_unchanged_pos to the buffer position of the last
10453 character in the buffer that has not been changed. Z is the
10454 index + 1 of the last byte in current_buffer, i.e. by
10455 subtracting end_unchanged we get the index of the last
10456 unchanged character, and we have to add BEG to get its buffer
10457 position. */
10458 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10459 last_unchanged_pos_old = last_unchanged_pos - *delta;
10461 /* Search backward from ROW for a row displaying a line that
10462 starts at a minimum position >= last_unchanged_pos_old. */
10463 for (; row > first_text_row; --row)
10465 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10466 abort ();
10468 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10469 row_found = row;
10473 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10474 abort ();
10476 return row_found;
10480 /* Make sure that glyph rows in the current matrix of window W
10481 reference the same glyph memory as corresponding rows in the
10482 frame's frame matrix. This function is called after scrolling W's
10483 current matrix on a terminal frame in try_window_id and
10484 try_window_reusing_current_matrix. */
10486 static void
10487 sync_frame_with_window_matrix_rows (w)
10488 struct window *w;
10490 struct frame *f = XFRAME (w->frame);
10491 struct glyph_row *window_row, *window_row_end, *frame_row;
10493 /* Preconditions: W must be a leaf window and full-width. Its frame
10494 must have a frame matrix. */
10495 xassert (NILP (w->hchild) && NILP (w->vchild));
10496 xassert (WINDOW_FULL_WIDTH_P (w));
10497 xassert (!FRAME_WINDOW_P (f));
10499 /* If W is a full-width window, glyph pointers in W's current matrix
10500 have, by definition, to be the same as glyph pointers in the
10501 corresponding frame matrix. */
10502 window_row = w->current_matrix->rows;
10503 window_row_end = window_row + w->current_matrix->nrows;
10504 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10505 while (window_row < window_row_end)
10507 int area;
10509 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10510 frame_row->glyphs[area] = window_row->glyphs[area];
10512 /* Disable frame rows whose corresponding window rows have
10513 been disabled in try_window_id. */
10514 if (!window_row->enabled_p)
10515 frame_row->enabled_p = 0;
10517 ++window_row, ++frame_row;
10522 /* Find the glyph row in window W containing CHARPOS. Consider all
10523 rows between START and END (not inclusive). END null means search
10524 all rows to the end of the display area of W. Value is the row
10525 containing CHARPOS or null. */
10527 static struct glyph_row *
10528 row_containing_pos (w, charpos, start, end)
10529 struct window *w;
10530 int charpos;
10531 struct glyph_row *start, *end;
10533 struct glyph_row *row = start;
10534 int last_y;
10536 /* If we happen to start on a header-line, skip that. */
10537 if (row->mode_line_p)
10538 ++row;
10540 if ((end && row >= end) || !row->enabled_p)
10541 return NULL;
10543 last_y = window_text_bottom_y (w);
10545 while ((end == NULL || row < end)
10546 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10547 /* The end position of a row equals the start
10548 position of the next row. If CHARPOS is there, we
10549 would rather display it in the next line, except
10550 when this line ends in ZV. */
10551 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10552 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10553 || !row->ends_at_zv_p)))
10554 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10555 ++row;
10557 /* Give up if CHARPOS not found. */
10558 if ((end && row >= end)
10559 || charpos < MATRIX_ROW_START_CHARPOS (row)
10560 || charpos > MATRIX_ROW_END_CHARPOS (row))
10561 row = NULL;
10563 return row;
10567 /* Try to redisplay window W by reusing its existing display. W's
10568 current matrix must be up to date when this function is called,
10569 i.e. window_end_valid must not be nil.
10571 Value is
10573 1 if display has been updated
10574 0 if otherwise unsuccessful
10575 -1 if redisplay with same window start is known not to succeed
10577 The following steps are performed:
10579 1. Find the last row in the current matrix of W that is not
10580 affected by changes at the start of current_buffer. If no such row
10581 is found, give up.
10583 2. Find the first row in W's current matrix that is not affected by
10584 changes at the end of current_buffer. Maybe there is no such row.
10586 3. Display lines beginning with the row + 1 found in step 1 to the
10587 row found in step 2 or, if step 2 didn't find a row, to the end of
10588 the window.
10590 4. If cursor is not known to appear on the window, give up.
10592 5. If display stopped at the row found in step 2, scroll the
10593 display and current matrix as needed.
10595 6. Maybe display some lines at the end of W, if we must. This can
10596 happen under various circumstances, like a partially visible line
10597 becoming fully visible, or because newly displayed lines are displayed
10598 in smaller font sizes.
10600 7. Update W's window end information. */
10602 /* Check that window end is what we expect it to be. */
10604 static int
10605 try_window_id (w)
10606 struct window *w;
10608 struct frame *f = XFRAME (w->frame);
10609 struct glyph_matrix *current_matrix = w->current_matrix;
10610 struct glyph_matrix *desired_matrix = w->desired_matrix;
10611 struct glyph_row *last_unchanged_at_beg_row;
10612 struct glyph_row *first_unchanged_at_end_row;
10613 struct glyph_row *row;
10614 struct glyph_row *bottom_row;
10615 int bottom_vpos;
10616 struct it it;
10617 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10618 struct text_pos start_pos;
10619 struct run run;
10620 int first_unchanged_at_end_vpos = 0;
10621 struct glyph_row *last_text_row, *last_text_row_at_end;
10622 struct text_pos start;
10624 SET_TEXT_POS_FROM_MARKER (start, w->start);
10626 /* Check pre-conditions. Window end must be valid, otherwise
10627 the current matrix would not be up to date. */
10628 xassert (!NILP (w->window_end_valid));
10629 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10630 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10632 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10633 only if buffer has really changed. The reason is that the gap is
10634 initially at Z for freshly visited files. The code below would
10635 set end_unchanged to 0 in that case. */
10636 if (MODIFF > SAVE_MODIFF
10637 /* This seems to happen sometimes after saving a buffer. */
10638 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10640 if (GPT - BEG < BEG_UNCHANGED)
10641 BEG_UNCHANGED = GPT - BEG;
10642 if (Z - GPT < END_UNCHANGED)
10643 END_UNCHANGED = Z - GPT;
10646 /* If window starts after a line end, and the last change is in
10647 front of that newline, then changes don't affect the display.
10648 This case happens with stealth-fontification. Note that although
10649 the display is unchanged, glyph positions in the matrix have to
10650 be adjusted, of course. */
10651 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10652 if (CHARPOS (start) > BEGV
10653 && Z - END_UNCHANGED < CHARPOS (start) - 1
10654 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10655 && PT < MATRIX_ROW_END_CHARPOS (row))
10657 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10658 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10660 if (delta)
10662 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10663 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10665 increment_matrix_positions (w->current_matrix,
10666 MATRIX_ROW_VPOS (r0, current_matrix),
10667 MATRIX_ROW_VPOS (r1, current_matrix),
10668 delta, delta_bytes);
10671 #if 0 /* If changes are all in front of the window start, the
10672 distance of the last displayed glyph from Z hasn't
10673 changed. */
10674 w->window_end_pos
10675 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10676 w->window_end_bytepos
10677 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10678 #endif
10680 return 1;
10683 /* Return quickly if changes are all below what is displayed in the
10684 window, and if PT is in the window. */
10685 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10686 && PT < MATRIX_ROW_END_CHARPOS (row))
10688 /* We have to update window end positions because the buffer's
10689 size has changed. */
10690 w->window_end_pos
10691 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10692 w->window_end_bytepos
10693 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10695 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10696 row = row_containing_pos (w, PT, row, NULL);
10697 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10698 return 2;
10701 /* Check that window start agrees with the start of the first glyph
10702 row in its current matrix. Check this after we know the window
10703 start is not in changed text, otherwise positions would not be
10704 comparable. */
10705 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10706 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10707 return 0;
10709 /* Compute the position at which we have to start displaying new
10710 lines. Some of the lines at the top of the window might be
10711 reusable because they are not displaying changed text. Find the
10712 last row in W's current matrix not affected by changes at the
10713 start of current_buffer. Value is null if changes start in the
10714 first line of window. */
10715 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10716 if (last_unchanged_at_beg_row)
10718 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10719 start_pos = it.current.pos;
10721 /* Start displaying new lines in the desired matrix at the same
10722 vpos we would use in the current matrix, i.e. below
10723 last_unchanged_at_beg_row. */
10724 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10725 current_matrix);
10726 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10727 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10729 xassert (it.hpos == 0 && it.current_x == 0);
10731 else
10733 /* There are no reusable lines at the start of the window.
10734 Start displaying in the first line. */
10735 start_display (&it, w, start);
10736 start_pos = it.current.pos;
10739 /* Find the first row that is not affected by changes at the end of
10740 the buffer. Value will be null if there is no unchanged row, in
10741 which case we must redisplay to the end of the window. delta
10742 will be set to the value by which buffer positions beginning with
10743 first_unchanged_at_end_row have to be adjusted due to text
10744 changes. */
10745 first_unchanged_at_end_row
10746 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10747 IF_DEBUG (debug_delta = delta);
10748 IF_DEBUG (debug_delta_bytes = delta_bytes);
10750 /* Set stop_pos to the buffer position up to which we will have to
10751 display new lines. If first_unchanged_at_end_row != NULL, this
10752 is the buffer position of the start of the line displayed in that
10753 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10754 that we don't stop at a buffer position. */
10755 stop_pos = 0;
10756 if (first_unchanged_at_end_row)
10758 xassert (last_unchanged_at_beg_row == NULL
10759 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10761 /* If this is a continuation line, move forward to the next one
10762 that isn't. Changes in lines above affect this line.
10763 Caution: this may move first_unchanged_at_end_row to a row
10764 not displaying text. */
10765 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10766 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10767 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10768 < it.last_visible_y))
10769 ++first_unchanged_at_end_row;
10771 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10772 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10773 >= it.last_visible_y))
10774 first_unchanged_at_end_row = NULL;
10775 else
10777 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10778 + delta);
10779 first_unchanged_at_end_vpos
10780 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10781 xassert (stop_pos >= Z - END_UNCHANGED);
10784 else if (last_unchanged_at_beg_row == NULL)
10785 return 0;
10788 #if GLYPH_DEBUG
10790 /* Either there is no unchanged row at the end, or the one we have
10791 now displays text. This is a necessary condition for the window
10792 end pos calculation at the end of this function. */
10793 xassert (first_unchanged_at_end_row == NULL
10794 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10796 debug_last_unchanged_at_beg_vpos
10797 = (last_unchanged_at_beg_row
10798 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10799 : -1);
10800 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10802 #endif /* GLYPH_DEBUG != 0 */
10805 /* Display new lines. Set last_text_row to the last new line
10806 displayed which has text on it, i.e. might end up as being the
10807 line where the window_end_vpos is. */
10808 w->cursor.vpos = -1;
10809 last_text_row = NULL;
10810 overlay_arrow_seen = 0;
10811 while (it.current_y < it.last_visible_y
10812 && !fonts_changed_p
10813 && (first_unchanged_at_end_row == NULL
10814 || IT_CHARPOS (it) < stop_pos))
10816 if (display_line (&it))
10817 last_text_row = it.glyph_row - 1;
10820 if (fonts_changed_p)
10821 return -1;
10824 /* Compute differences in buffer positions, y-positions etc. for
10825 lines reused at the bottom of the window. Compute what we can
10826 scroll. */
10827 if (first_unchanged_at_end_row
10828 /* No lines reused because we displayed everything up to the
10829 bottom of the window. */
10830 && it.current_y < it.last_visible_y)
10832 dvpos = (it.vpos
10833 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10834 current_matrix));
10835 dy = it.current_y - first_unchanged_at_end_row->y;
10836 run.current_y = first_unchanged_at_end_row->y;
10837 run.desired_y = run.current_y + dy;
10838 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10840 else
10842 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10843 first_unchanged_at_end_row = NULL;
10845 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10848 /* Find the cursor if not already found. We have to decide whether
10849 PT will appear on this window (it sometimes doesn't, but this is
10850 not a very frequent case.) This decision has to be made before
10851 the current matrix is altered. A value of cursor.vpos < 0 means
10852 that PT is either in one of the lines beginning at
10853 first_unchanged_at_end_row or below the window. Don't care for
10854 lines that might be displayed later at the window end; as
10855 mentioned, this is not a frequent case. */
10856 if (w->cursor.vpos < 0)
10858 /* Cursor in unchanged rows at the top? */
10859 if (PT < CHARPOS (start_pos)
10860 && last_unchanged_at_beg_row)
10862 row = row_containing_pos (w, PT,
10863 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10864 last_unchanged_at_beg_row + 1);
10865 if (row)
10866 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10869 /* Start from first_unchanged_at_end_row looking for PT. */
10870 else if (first_unchanged_at_end_row)
10872 row = row_containing_pos (w, PT - delta,
10873 first_unchanged_at_end_row, NULL);
10874 if (row)
10875 set_cursor_from_row (w, row, w->current_matrix, delta,
10876 delta_bytes, dy, dvpos);
10879 /* Give up if cursor was not found. */
10880 if (w->cursor.vpos < 0)
10882 clear_glyph_matrix (w->desired_matrix);
10883 return -1;
10887 /* Don't let the cursor end in the scroll margins. */
10889 int this_scroll_margin, cursor_height;
10891 this_scroll_margin = max (0, scroll_margin);
10892 this_scroll_margin = min (this_scroll_margin,
10893 XFASTINT (w->height) / 4);
10894 this_scroll_margin *= CANON_Y_UNIT (it.f);
10895 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10897 if ((w->cursor.y < this_scroll_margin
10898 && CHARPOS (start) > BEGV)
10899 /* Don't take scroll margin into account at the bottom because
10900 old redisplay didn't do it either. */
10901 || w->cursor.y + cursor_height > it.last_visible_y)
10903 w->cursor.vpos = -1;
10904 clear_glyph_matrix (w->desired_matrix);
10905 return -1;
10909 /* Scroll the display. Do it before changing the current matrix so
10910 that xterm.c doesn't get confused about where the cursor glyph is
10911 found. */
10912 if (dy && run.height)
10914 update_begin (f);
10916 if (FRAME_WINDOW_P (f))
10918 rif->update_window_begin_hook (w);
10919 rif->clear_mouse_face (w);
10920 rif->scroll_run_hook (w, &run);
10921 rif->update_window_end_hook (w, 0, 0);
10923 else
10925 /* Terminal frame. In this case, dvpos gives the number of
10926 lines to scroll by; dvpos < 0 means scroll up. */
10927 int first_unchanged_at_end_vpos
10928 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10929 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10930 int end = XFASTINT (w->top) + window_internal_height (w);
10932 /* Perform the operation on the screen. */
10933 if (dvpos > 0)
10935 /* Scroll last_unchanged_at_beg_row to the end of the
10936 window down dvpos lines. */
10937 set_terminal_window (end);
10939 /* On dumb terminals delete dvpos lines at the end
10940 before inserting dvpos empty lines. */
10941 if (!scroll_region_ok)
10942 ins_del_lines (end - dvpos, -dvpos);
10944 /* Insert dvpos empty lines in front of
10945 last_unchanged_at_beg_row. */
10946 ins_del_lines (from, dvpos);
10948 else if (dvpos < 0)
10950 /* Scroll up last_unchanged_at_beg_vpos to the end of
10951 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10952 set_terminal_window (end);
10954 /* Delete dvpos lines in front of
10955 last_unchanged_at_beg_vpos. ins_del_lines will set
10956 the cursor to the given vpos and emit |dvpos| delete
10957 line sequences. */
10958 ins_del_lines (from + dvpos, dvpos);
10960 /* On a dumb terminal insert dvpos empty lines at the
10961 end. */
10962 if (!scroll_region_ok)
10963 ins_del_lines (end + dvpos, -dvpos);
10966 set_terminal_window (0);
10969 update_end (f);
10972 /* Shift reused rows of the current matrix to the right position.
10973 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10974 text. */
10975 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10976 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10977 if (dvpos < 0)
10979 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10980 bottom_vpos, dvpos);
10981 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10982 bottom_vpos, 0);
10984 else if (dvpos > 0)
10986 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10987 bottom_vpos, dvpos);
10988 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10989 first_unchanged_at_end_vpos + dvpos, 0);
10992 /* For frame-based redisplay, make sure that current frame and window
10993 matrix are in sync with respect to glyph memory. */
10994 if (!FRAME_WINDOW_P (f))
10995 sync_frame_with_window_matrix_rows (w);
10997 /* Adjust buffer positions in reused rows. */
10998 if (delta)
10999 increment_matrix_positions (current_matrix,
11000 first_unchanged_at_end_vpos + dvpos,
11001 bottom_vpos, delta, delta_bytes);
11003 /* Adjust Y positions. */
11004 if (dy)
11005 shift_glyph_matrix (w, current_matrix,
11006 first_unchanged_at_end_vpos + dvpos,
11007 bottom_vpos, dy);
11009 if (first_unchanged_at_end_row)
11010 first_unchanged_at_end_row += dvpos;
11012 /* If scrolling up, there may be some lines to display at the end of
11013 the window. */
11014 last_text_row_at_end = NULL;
11015 if (dy < 0)
11017 /* Set last_row to the glyph row in the current matrix where the
11018 window end line is found. It has been moved up or down in
11019 the matrix by dvpos. */
11020 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11021 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11023 /* If last_row is the window end line, it should display text. */
11024 xassert (last_row->displays_text_p);
11026 /* If window end line was partially visible before, begin
11027 displaying at that line. Otherwise begin displaying with the
11028 line following it. */
11029 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11031 init_to_row_start (&it, w, last_row);
11032 it.vpos = last_vpos;
11033 it.current_y = last_row->y;
11035 else
11037 init_to_row_end (&it, w, last_row);
11038 it.vpos = 1 + last_vpos;
11039 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11040 ++last_row;
11043 /* We may start in a continuation line. If so, we have to get
11044 the right continuation_lines_width and current_x. */
11045 it.continuation_lines_width = last_row->continuation_lines_width;
11046 it.hpos = it.current_x = 0;
11048 /* Display the rest of the lines at the window end. */
11049 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11050 while (it.current_y < it.last_visible_y
11051 && !fonts_changed_p)
11053 /* Is it always sure that the display agrees with lines in
11054 the current matrix? I don't think so, so we mark rows
11055 displayed invalid in the current matrix by setting their
11056 enabled_p flag to zero. */
11057 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11058 if (display_line (&it))
11059 last_text_row_at_end = it.glyph_row - 1;
11063 /* Update window_end_pos and window_end_vpos. */
11064 if (first_unchanged_at_end_row
11065 && first_unchanged_at_end_row->y < it.last_visible_y
11066 && !last_text_row_at_end)
11068 /* Window end line if one of the preserved rows from the current
11069 matrix. Set row to the last row displaying text in current
11070 matrix starting at first_unchanged_at_end_row, after
11071 scrolling. */
11072 xassert (first_unchanged_at_end_row->displays_text_p);
11073 row = find_last_row_displaying_text (w->current_matrix, &it,
11074 first_unchanged_at_end_row);
11075 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11077 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11078 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11079 XSETFASTINT (w->window_end_vpos,
11080 MATRIX_ROW_VPOS (row, w->current_matrix));
11082 else if (last_text_row_at_end)
11084 XSETFASTINT (w->window_end_pos,
11085 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11086 w->window_end_bytepos
11087 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11088 XSETFASTINT (w->window_end_vpos,
11089 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11091 else if (last_text_row)
11093 /* We have displayed either to the end of the window or at the
11094 end of the window, i.e. the last row with text is to be found
11095 in the desired matrix. */
11096 XSETFASTINT (w->window_end_pos,
11097 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11098 w->window_end_bytepos
11099 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11100 XSETFASTINT (w->window_end_vpos,
11101 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11103 else if (first_unchanged_at_end_row == NULL
11104 && last_text_row == NULL
11105 && last_text_row_at_end == NULL)
11107 /* Displayed to end of window, but no line containing text was
11108 displayed. Lines were deleted at the end of the window. */
11109 int vpos;
11110 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11112 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11113 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11114 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11115 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11116 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11117 break;
11119 w->window_end_vpos = make_number (vpos);
11121 else
11122 abort ();
11124 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11125 debug_end_vpos = XFASTINT (w->window_end_vpos));
11127 /* Record that display has not been completed. */
11128 w->window_end_valid = Qnil;
11129 w->desired_matrix->no_scrolling_p = 1;
11130 return 3;
11135 /***********************************************************************
11136 More debugging support
11137 ***********************************************************************/
11139 #if GLYPH_DEBUG
11141 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11142 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11145 /* Dump the contents of glyph matrix MATRIX on stderr. If
11146 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11148 static void
11149 dump_glyph_matrix (matrix, with_glyphs_p)
11150 struct glyph_matrix *matrix;
11151 int with_glyphs_p;
11153 int i;
11154 for (i = 0; i < matrix->nrows; ++i)
11155 dump_glyph_row (matrix, i, with_glyphs_p);
11159 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11160 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11162 void
11163 dump_glyph_row (matrix, vpos, with_glyphs_p)
11164 struct glyph_matrix *matrix;
11165 int vpos, with_glyphs_p;
11167 struct glyph_row *row;
11169 if (vpos < 0 || vpos >= matrix->nrows)
11170 return;
11172 row = MATRIX_ROW (matrix, vpos);
11174 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11175 fprintf (stderr, "=======================================================================\n");
11177 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11178 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11179 row - matrix->rows,
11180 MATRIX_ROW_START_CHARPOS (row),
11181 MATRIX_ROW_END_CHARPOS (row),
11182 row->used[TEXT_AREA],
11183 row->contains_overlapping_glyphs_p,
11184 row->enabled_p,
11185 row->inverse_p,
11186 row->truncated_on_left_p,
11187 row->truncated_on_right_p,
11188 row->overlay_arrow_p,
11189 row->continued_p,
11190 MATRIX_ROW_CONTINUATION_LINE_P (row),
11191 row->displays_text_p,
11192 row->ends_at_zv_p,
11193 row->fill_line_p,
11194 row->ends_in_middle_of_char_p,
11195 row->starts_in_middle_of_char_p,
11196 row->x,
11197 row->y,
11198 row->pixel_width,
11199 row->height,
11200 row->visible_height,
11201 row->ascent,
11202 row->phys_ascent);
11203 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11204 row->end.overlay_string_index);
11205 fprintf (stderr, "%9d %5d\n",
11206 CHARPOS (row->start.string_pos),
11207 CHARPOS (row->end.string_pos));
11208 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11209 row->end.dpvec_index);
11211 if (with_glyphs_p)
11213 struct glyph *glyph, *glyph_end;
11214 int prev_had_glyphs_p;
11216 glyph = row->glyphs[TEXT_AREA];
11217 glyph_end = glyph + row->used[TEXT_AREA];
11219 /* Glyph for a line end in text. */
11220 if (glyph == glyph_end && glyph->charpos > 0)
11221 ++glyph_end;
11223 if (glyph < glyph_end)
11225 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11226 prev_had_glyphs_p = 1;
11228 else
11229 prev_had_glyphs_p = 0;
11231 while (glyph < glyph_end)
11233 if (glyph->type == CHAR_GLYPH)
11235 fprintf (stderr,
11236 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11237 glyph - row->glyphs[TEXT_AREA],
11238 'C',
11239 glyph->charpos,
11240 (BUFFERP (glyph->object)
11241 ? 'B'
11242 : (STRINGP (glyph->object)
11243 ? 'S'
11244 : '-')),
11245 glyph->pixel_width,
11246 glyph->u.ch,
11247 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11248 ? glyph->u.ch
11249 : '.'),
11250 glyph->face_id,
11251 glyph->left_box_line_p,
11252 glyph->right_box_line_p);
11254 else if (glyph->type == STRETCH_GLYPH)
11256 fprintf (stderr,
11257 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11258 glyph - row->glyphs[TEXT_AREA],
11259 'S',
11260 glyph->charpos,
11261 (BUFFERP (glyph->object)
11262 ? 'B'
11263 : (STRINGP (glyph->object)
11264 ? 'S'
11265 : '-')),
11266 glyph->pixel_width,
11268 '.',
11269 glyph->face_id,
11270 glyph->left_box_line_p,
11271 glyph->right_box_line_p);
11273 else if (glyph->type == IMAGE_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 'I',
11279 glyph->charpos,
11280 (BUFFERP (glyph->object)
11281 ? 'B'
11282 : (STRINGP (glyph->object)
11283 ? 'S'
11284 : '-')),
11285 glyph->pixel_width,
11286 glyph->u.img_id,
11287 '.',
11288 glyph->face_id,
11289 glyph->left_box_line_p,
11290 glyph->right_box_line_p);
11292 ++glyph;
11298 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11299 Sdump_glyph_matrix, 0, 1, "p",
11300 "Dump the current matrix of the selected window to stderr.\n\
11301 Shows contents of glyph row structures. With non-nil optional\n\
11302 parameter WITH-GLYPHS-P, dump glyphs as well.")
11303 (with_glyphs_p)
11304 Lisp_Object with_glyphs_p;
11306 struct window *w = XWINDOW (selected_window);
11307 struct buffer *buffer = XBUFFER (w->buffer);
11309 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11310 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11311 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11312 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11313 fprintf (stderr, "=============================================\n");
11314 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11315 return Qnil;
11319 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11320 "Dump glyph row ROW to stderr.")
11321 (row)
11322 Lisp_Object row;
11324 CHECK_NUMBER (row, 0);
11325 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11326 return Qnil;
11330 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11331 0, 0, "", "")
11334 struct frame *sf = SELECTED_FRAME ();
11335 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11336 ->current_matrix);
11337 dump_glyph_row (m, 0, 1);
11338 return Qnil;
11342 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11343 Strace_redisplay_toggle, 0, 0, "",
11344 "Toggle tracing of redisplay.")
11347 trace_redisplay_p = !trace_redisplay_p;
11348 return Qnil;
11352 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11353 "Print STRING to stderr.")
11354 (string)
11355 Lisp_Object string;
11357 CHECK_STRING (string, 0);
11358 fprintf (stderr, "%s", XSTRING (string)->data);
11359 return Qnil;
11362 #endif /* GLYPH_DEBUG */
11366 /***********************************************************************
11367 Building Desired Matrix Rows
11368 ***********************************************************************/
11370 /* Return a temporary glyph row holding the glyphs of an overlay
11371 arrow. Only used for non-window-redisplay windows. */
11373 static struct glyph_row *
11374 get_overlay_arrow_glyph_row (w)
11375 struct window *w;
11377 struct frame *f = XFRAME (WINDOW_FRAME (w));
11378 struct buffer *buffer = XBUFFER (w->buffer);
11379 struct buffer *old = current_buffer;
11380 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11381 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11382 unsigned char *arrow_end = arrow_string + arrow_len;
11383 unsigned char *p;
11384 struct it it;
11385 int multibyte_p;
11386 int n_glyphs_before;
11388 set_buffer_temp (buffer);
11389 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11390 it.glyph_row->used[TEXT_AREA] = 0;
11391 SET_TEXT_POS (it.position, 0, 0);
11393 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11394 p = arrow_string;
11395 while (p < arrow_end)
11397 Lisp_Object face, ilisp;
11399 /* Get the next character. */
11400 if (multibyte_p)
11401 it.c = string_char_and_length (p, arrow_len, &it.len);
11402 else
11403 it.c = *p, it.len = 1;
11404 p += it.len;
11406 /* Get its face. */
11407 XSETFASTINT (ilisp, p - arrow_string);
11408 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11409 it.face_id = compute_char_face (f, it.c, face);
11411 /* Compute its width, get its glyphs. */
11412 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11413 SET_TEXT_POS (it.position, -1, -1);
11414 PRODUCE_GLYPHS (&it);
11416 /* If this character doesn't fit any more in the line, we have
11417 to remove some glyphs. */
11418 if (it.current_x > it.last_visible_x)
11420 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11421 break;
11425 set_buffer_temp (old);
11426 return it.glyph_row;
11430 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11431 glyphs are only inserted for terminal frames since we can't really
11432 win with truncation glyphs when partially visible glyphs are
11433 involved. Which glyphs to insert is determined by
11434 produce_special_glyphs. */
11436 static void
11437 insert_left_trunc_glyphs (it)
11438 struct it *it;
11440 struct it truncate_it;
11441 struct glyph *from, *end, *to, *toend;
11443 xassert (!FRAME_WINDOW_P (it->f));
11445 /* Get the truncation glyphs. */
11446 truncate_it = *it;
11447 truncate_it.current_x = 0;
11448 truncate_it.face_id = DEFAULT_FACE_ID;
11449 truncate_it.glyph_row = &scratch_glyph_row;
11450 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11451 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11452 truncate_it.object = make_number (0);
11453 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11455 /* Overwrite glyphs from IT with truncation glyphs. */
11456 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11457 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11458 to = it->glyph_row->glyphs[TEXT_AREA];
11459 toend = to + it->glyph_row->used[TEXT_AREA];
11461 while (from < end)
11462 *to++ = *from++;
11464 /* There may be padding glyphs left over. Remove them. */
11465 from = to;
11466 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11467 ++from;
11468 while (from < toend)
11469 *to++ = *from++;
11471 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11475 /* Compute the pixel height and width of IT->glyph_row.
11477 Most of the time, ascent and height of a display line will be equal
11478 to the max_ascent and max_height values of the display iterator
11479 structure. This is not the case if
11481 1. We hit ZV without displaying anything. In this case, max_ascent
11482 and max_height will be zero.
11484 2. We have some glyphs that don't contribute to the line height.
11485 (The glyph row flag contributes_to_line_height_p is for future
11486 pixmap extensions).
11488 The first case is easily covered by using default values because in
11489 these cases, the line height does not really matter, except that it
11490 must not be zero. */
11492 static void
11493 compute_line_metrics (it)
11494 struct it *it;
11496 struct glyph_row *row = it->glyph_row;
11497 int area, i;
11499 if (FRAME_WINDOW_P (it->f))
11501 int i, header_line_height;
11503 /* The line may consist of one space only, that was added to
11504 place the cursor on it. If so, the row's height hasn't been
11505 computed yet. */
11506 if (row->height == 0)
11508 if (it->max_ascent + it->max_descent == 0)
11509 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11510 row->ascent = it->max_ascent;
11511 row->height = it->max_ascent + it->max_descent;
11512 row->phys_ascent = it->max_phys_ascent;
11513 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11516 /* Compute the width of this line. */
11517 row->pixel_width = row->x;
11518 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11519 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11521 xassert (row->pixel_width >= 0);
11522 xassert (row->ascent >= 0 && row->height > 0);
11524 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11525 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11527 /* If first line's physical ascent is larger than its logical
11528 ascent, use the physical ascent, and make the row taller.
11529 This makes accented characters fully visible. */
11530 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11531 && row->phys_ascent > row->ascent)
11533 row->height += row->phys_ascent - row->ascent;
11534 row->ascent = row->phys_ascent;
11537 /* Compute how much of the line is visible. */
11538 row->visible_height = row->height;
11540 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11541 if (row->y < header_line_height)
11542 row->visible_height -= header_line_height - row->y;
11543 else
11545 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11546 if (row->y + row->height > max_y)
11547 row->visible_height -= row->y + row->height - max_y;
11550 else
11552 row->pixel_width = row->used[TEXT_AREA];
11553 row->ascent = row->phys_ascent = 0;
11554 row->height = row->phys_height = row->visible_height = 1;
11557 /* Compute a hash code for this row. */
11558 row->hash = 0;
11559 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11560 for (i = 0; i < row->used[area]; ++i)
11561 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11562 + row->glyphs[area][i].u.val
11563 + row->glyphs[area][i].face_id
11564 + row->glyphs[area][i].padding_p
11565 + (row->glyphs[area][i].type << 2));
11567 it->max_ascent = it->max_descent = 0;
11568 it->max_phys_ascent = it->max_phys_descent = 0;
11572 /* Append one space to the glyph row of iterator IT if doing a
11573 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11574 space have the default face, otherwise let it have the same face as
11575 IT->face_id. Value is non-zero if a space was added.
11577 This function is called to make sure that there is always one glyph
11578 at the end of a glyph row that the cursor can be set on under
11579 window-systems. (If there weren't such a glyph we would not know
11580 how wide and tall a box cursor should be displayed).
11582 At the same time this space let's a nicely handle clearing to the
11583 end of the line if the row ends in italic text. */
11585 static int
11586 append_space (it, default_face_p)
11587 struct it *it;
11588 int default_face_p;
11590 if (FRAME_WINDOW_P (it->f))
11592 int n = it->glyph_row->used[TEXT_AREA];
11594 if (it->glyph_row->glyphs[TEXT_AREA] + n
11595 < it->glyph_row->glyphs[1 + TEXT_AREA])
11597 /* Save some values that must not be changed.
11598 Must save IT->c and IT->len because otherwise
11599 ITERATOR_AT_END_P wouldn't work anymore after
11600 append_space has been called. */
11601 int saved_what = it->what;
11602 int saved_c = it->c, saved_len = it->len;
11603 int saved_x = it->current_x;
11604 int saved_face_id = it->face_id;
11605 struct text_pos saved_pos;
11606 Lisp_Object saved_object;
11607 struct face *face;
11609 saved_object = it->object;
11610 saved_pos = it->position;
11612 it->what = IT_CHARACTER;
11613 bzero (&it->position, sizeof it->position);
11614 it->object = make_number (0);
11615 it->c = ' ';
11616 it->len = 1;
11618 if (default_face_p)
11619 it->face_id = DEFAULT_FACE_ID;
11620 face = FACE_FROM_ID (it->f, it->face_id);
11621 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11623 PRODUCE_GLYPHS (it);
11625 it->current_x = saved_x;
11626 it->object = saved_object;
11627 it->position = saved_pos;
11628 it->what = saved_what;
11629 it->face_id = saved_face_id;
11630 it->len = saved_len;
11631 it->c = saved_c;
11632 return 1;
11636 return 0;
11640 /* Extend the face of the last glyph in the text area of IT->glyph_row
11641 to the end of the display line. Called from display_line.
11642 If the glyph row is empty, add a space glyph to it so that we
11643 know the face to draw. Set the glyph row flag fill_line_p. */
11645 static void
11646 extend_face_to_end_of_line (it)
11647 struct it *it;
11649 struct face *face;
11650 struct frame *f = it->f;
11652 /* If line is already filled, do nothing. */
11653 if (it->current_x >= it->last_visible_x)
11654 return;
11656 /* Face extension extends the background and box of IT->face_id
11657 to the end of the line. If the background equals the background
11658 of the frame, we haven't to do anything. */
11659 face = FACE_FROM_ID (f, it->face_id);
11660 if (FRAME_WINDOW_P (f)
11661 && face->box == FACE_NO_BOX
11662 && face->background == FRAME_BACKGROUND_PIXEL (f)
11663 && !face->stipple)
11664 return;
11666 /* Set the glyph row flag indicating that the face of the last glyph
11667 in the text area has to be drawn to the end of the text area. */
11668 it->glyph_row->fill_line_p = 1;
11670 /* If current character of IT is not ASCII, make sure we have the
11671 ASCII face. This will be automatically undone the next time
11672 get_next_display_element returns a multibyte character. Note
11673 that the character will always be single byte in unibyte text. */
11674 if (!SINGLE_BYTE_CHAR_P (it->c))
11676 it->face_id = FACE_FOR_CHAR (f, face, 0);
11679 if (FRAME_WINDOW_P (f))
11681 /* If the row is empty, add a space with the current face of IT,
11682 so that we know which face to draw. */
11683 if (it->glyph_row->used[TEXT_AREA] == 0)
11685 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11686 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11687 it->glyph_row->used[TEXT_AREA] = 1;
11690 else
11692 /* Save some values that must not be changed. */
11693 int saved_x = it->current_x;
11694 struct text_pos saved_pos;
11695 Lisp_Object saved_object;
11696 int saved_what = it->what;
11698 saved_object = it->object;
11699 saved_pos = it->position;
11701 it->what = IT_CHARACTER;
11702 bzero (&it->position, sizeof it->position);
11703 it->object = make_number (0);
11704 it->c = ' ';
11705 it->len = 1;
11707 PRODUCE_GLYPHS (it);
11709 while (it->current_x <= it->last_visible_x)
11710 PRODUCE_GLYPHS (it);
11712 /* Don't count these blanks really. It would let us insert a left
11713 truncation glyph below and make us set the cursor on them, maybe. */
11714 it->current_x = saved_x;
11715 it->object = saved_object;
11716 it->position = saved_pos;
11717 it->what = saved_what;
11722 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11723 trailing whitespace. */
11725 static int
11726 trailing_whitespace_p (charpos)
11727 int charpos;
11729 int bytepos = CHAR_TO_BYTE (charpos);
11730 int c = 0;
11732 while (bytepos < ZV_BYTE
11733 && (c = FETCH_CHAR (bytepos),
11734 c == ' ' || c == '\t'))
11735 ++bytepos;
11737 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11739 if (bytepos != PT_BYTE)
11740 return 1;
11742 return 0;
11746 /* Highlight trailing whitespace, if any, in ROW. */
11748 void
11749 highlight_trailing_whitespace (f, row)
11750 struct frame *f;
11751 struct glyph_row *row;
11753 int used = row->used[TEXT_AREA];
11755 if (used)
11757 struct glyph *start = row->glyphs[TEXT_AREA];
11758 struct glyph *glyph = start + used - 1;
11760 /* Skip over the space glyph inserted to display the
11761 cursor at the end of a line. */
11762 if (glyph->type == CHAR_GLYPH
11763 && glyph->u.ch == ' '
11764 && INTEGERP (glyph->object))
11765 --glyph;
11767 /* If last glyph is a space or stretch, and it's trailing
11768 whitespace, set the face of all trailing whitespace glyphs in
11769 IT->glyph_row to `trailing-whitespace'. */
11770 if (glyph >= start
11771 && BUFFERP (glyph->object)
11772 && (glyph->type == STRETCH_GLYPH
11773 || (glyph->type == CHAR_GLYPH
11774 && glyph->u.ch == ' '))
11775 && trailing_whitespace_p (glyph->charpos))
11777 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11779 while (glyph >= start
11780 && BUFFERP (glyph->object)
11781 && (glyph->type == STRETCH_GLYPH
11782 || (glyph->type == CHAR_GLYPH
11783 && glyph->u.ch == ' ')))
11784 (glyph--)->face_id = face_id;
11790 /* Value is non-zero if glyph row ROW in window W should be
11791 used to put the cursor on. */
11793 static int
11794 cursor_row_p (w, row)
11795 struct window *w;
11796 struct glyph_row *row;
11798 int cursor_row_p = 1;
11800 if (PT == MATRIX_ROW_END_CHARPOS (row))
11802 /* If the row ends with a newline from a string, we don't want
11803 the cursor there (if the row is continued it doesn't end in a
11804 newline). */
11805 if (CHARPOS (row->end.string_pos) >= 0
11806 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11807 cursor_row_p = row->continued_p;
11809 /* If the row ends at ZV, display the cursor at the end of that
11810 row instead of at the start of the row below. */
11811 else if (row->ends_at_zv_p)
11812 cursor_row_p = 1;
11813 else
11814 cursor_row_p = 0;
11817 return cursor_row_p;
11821 /* Construct the glyph row IT->glyph_row in the desired matrix of
11822 IT->w from text at the current position of IT. See dispextern.h
11823 for an overview of struct it. Value is non-zero if
11824 IT->glyph_row displays text, as opposed to a line displaying ZV
11825 only. */
11827 static int
11828 display_line (it)
11829 struct it *it;
11831 struct glyph_row *row = it->glyph_row;
11833 /* We always start displaying at hpos zero even if hscrolled. */
11834 xassert (it->hpos == 0 && it->current_x == 0);
11836 /* We must not display in a row that's not a text row. */
11837 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11838 < it->w->desired_matrix->nrows);
11840 /* Is IT->w showing the region? */
11841 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11843 /* Clear the result glyph row and enable it. */
11844 prepare_desired_row (row);
11846 row->y = it->current_y;
11847 row->start = it->current;
11848 row->continuation_lines_width = it->continuation_lines_width;
11849 row->displays_text_p = 1;
11850 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11851 it->starts_in_middle_of_char_p = 0;
11853 /* Arrange the overlays nicely for our purposes. Usually, we call
11854 display_line on only one line at a time, in which case this
11855 can't really hurt too much, or we call it on lines which appear
11856 one after another in the buffer, in which case all calls to
11857 recenter_overlay_lists but the first will be pretty cheap. */
11858 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11860 /* Move over display elements that are not visible because we are
11861 hscrolled. This may stop at an x-position < IT->first_visible_x
11862 if the first glyph is partially visible or if we hit a line end. */
11863 if (it->current_x < it->first_visible_x)
11864 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11865 MOVE_TO_POS | MOVE_TO_X);
11867 /* Get the initial row height. This is either the height of the
11868 text hscrolled, if there is any, or zero. */
11869 row->ascent = it->max_ascent;
11870 row->height = it->max_ascent + it->max_descent;
11871 row->phys_ascent = it->max_phys_ascent;
11872 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11874 /* Loop generating characters. The loop is left with IT on the next
11875 character to display. */
11876 while (1)
11878 int n_glyphs_before, hpos_before, x_before;
11879 int x, i, nglyphs;
11880 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11882 /* Retrieve the next thing to display. Value is zero if end of
11883 buffer reached. */
11884 if (!get_next_display_element (it))
11886 /* Maybe add a space at the end of this line that is used to
11887 display the cursor there under X. Set the charpos of the
11888 first glyph of blank lines not corresponding to any text
11889 to -1. */
11890 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11891 || row->used[TEXT_AREA] == 0)
11893 row->glyphs[TEXT_AREA]->charpos = -1;
11894 row->displays_text_p = 0;
11896 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11897 row->indicate_empty_line_p = 1;
11900 it->continuation_lines_width = 0;
11901 row->ends_at_zv_p = 1;
11902 break;
11905 /* Now, get the metrics of what we want to display. This also
11906 generates glyphs in `row' (which is IT->glyph_row). */
11907 n_glyphs_before = row->used[TEXT_AREA];
11908 x = it->current_x;
11910 /* Remember the line height so far in case the next element doesn't
11911 fit on the line. */
11912 if (!it->truncate_lines_p)
11914 ascent = it->max_ascent;
11915 descent = it->max_descent;
11916 phys_ascent = it->max_phys_ascent;
11917 phys_descent = it->max_phys_descent;
11920 PRODUCE_GLYPHS (it);
11922 /* If this display element was in marginal areas, continue with
11923 the next one. */
11924 if (it->area != TEXT_AREA)
11926 row->ascent = max (row->ascent, it->max_ascent);
11927 row->height = max (row->height, it->max_ascent + it->max_descent);
11928 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11929 row->phys_height = max (row->phys_height,
11930 it->max_phys_ascent + it->max_phys_descent);
11931 set_iterator_to_next (it, 1);
11932 continue;
11935 /* Does the display element fit on the line? If we truncate
11936 lines, we should draw past the right edge of the window. If
11937 we don't truncate, we want to stop so that we can display the
11938 continuation glyph before the right margin. If lines are
11939 continued, there are two possible strategies for characters
11940 resulting in more than 1 glyph (e.g. tabs): Display as many
11941 glyphs as possible in this line and leave the rest for the
11942 continuation line, or display the whole element in the next
11943 line. Original redisplay did the former, so we do it also. */
11944 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11945 hpos_before = it->hpos;
11946 x_before = x;
11948 if (nglyphs == 1
11949 && it->current_x < it->last_visible_x)
11951 ++it->hpos;
11952 row->ascent = max (row->ascent, it->max_ascent);
11953 row->height = max (row->height, it->max_ascent + it->max_descent);
11954 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11955 row->phys_height = max (row->phys_height,
11956 it->max_phys_ascent + it->max_phys_descent);
11957 if (it->current_x - it->pixel_width < it->first_visible_x)
11958 row->x = x - it->first_visible_x;
11960 else
11962 int new_x;
11963 struct glyph *glyph;
11965 for (i = 0; i < nglyphs; ++i, x = new_x)
11967 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11968 new_x = x + glyph->pixel_width;
11970 if (/* Lines are continued. */
11971 !it->truncate_lines_p
11972 && (/* Glyph doesn't fit on the line. */
11973 new_x > it->last_visible_x
11974 /* Or it fits exactly on a window system frame. */
11975 || (new_x == it->last_visible_x
11976 && FRAME_WINDOW_P (it->f))))
11978 /* End of a continued line. */
11980 if (it->hpos == 0
11981 || (new_x == it->last_visible_x
11982 && FRAME_WINDOW_P (it->f)))
11984 /* Current glyph is the only one on the line or
11985 fits exactly on the line. We must continue
11986 the line because we can't draw the cursor
11987 after the glyph. */
11988 row->continued_p = 1;
11989 it->current_x = new_x;
11990 it->continuation_lines_width += new_x;
11991 ++it->hpos;
11992 if (i == nglyphs - 1)
11993 set_iterator_to_next (it, 1);
11995 else if (CHAR_GLYPH_PADDING_P (*glyph)
11996 && !FRAME_WINDOW_P (it->f))
11998 /* A padding glyph that doesn't fit on this line.
11999 This means the whole character doesn't fit
12000 on the line. */
12001 row->used[TEXT_AREA] = n_glyphs_before;
12003 /* Fill the rest of the row with continuation
12004 glyphs like in 20.x. */
12005 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12006 < row->glyphs[1 + TEXT_AREA])
12007 produce_special_glyphs (it, IT_CONTINUATION);
12009 row->continued_p = 1;
12010 it->current_x = x_before;
12011 it->continuation_lines_width += x_before;
12013 /* Restore the height to what it was before the
12014 element not fitting on the line. */
12015 it->max_ascent = ascent;
12016 it->max_descent = descent;
12017 it->max_phys_ascent = phys_ascent;
12018 it->max_phys_descent = phys_descent;
12020 else
12022 /* Display element draws past the right edge of
12023 the window. Restore positions to values
12024 before the element. The next line starts
12025 with current_x before the glyph that could
12026 not be displayed, so that TAB works right. */
12027 row->used[TEXT_AREA] = n_glyphs_before + i;
12029 /* Display continuation glyphs. */
12030 if (!FRAME_WINDOW_P (it->f))
12031 produce_special_glyphs (it, IT_CONTINUATION);
12032 row->continued_p = 1;
12034 it->current_x = x;
12035 it->continuation_lines_width += x;
12036 if (nglyphs > 1 && i > 0)
12038 row->ends_in_middle_of_char_p = 1;
12039 it->starts_in_middle_of_char_p = 1;
12042 /* Restore the height to what it was before the
12043 element not fitting on the line. */
12044 it->max_ascent = ascent;
12045 it->max_descent = descent;
12046 it->max_phys_ascent = phys_ascent;
12047 it->max_phys_descent = phys_descent;
12050 break;
12052 else if (new_x > it->first_visible_x)
12054 /* Increment number of glyphs actually displayed. */
12055 ++it->hpos;
12057 if (x < it->first_visible_x)
12058 /* Glyph is partially visible, i.e. row starts at
12059 negative X position. */
12060 row->x = x - it->first_visible_x;
12062 else
12064 /* Glyph is completely off the left margin of the
12065 window. This should not happen because of the
12066 move_it_in_display_line at the start of
12067 this function. */
12068 abort ();
12072 row->ascent = max (row->ascent, it->max_ascent);
12073 row->height = max (row->height, it->max_ascent + it->max_descent);
12074 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12075 row->phys_height = max (row->phys_height,
12076 it->max_phys_ascent + it->max_phys_descent);
12078 /* End of this display line if row is continued. */
12079 if (row->continued_p)
12080 break;
12083 /* Is this a line end? If yes, we're also done, after making
12084 sure that a non-default face is extended up to the right
12085 margin of the window. */
12086 if (ITERATOR_AT_END_OF_LINE_P (it))
12088 int used_before = row->used[TEXT_AREA];
12090 /* Add a space at the end of the line that is used to
12091 display the cursor there. */
12092 append_space (it, 0);
12094 /* Extend the face to the end of the line. */
12095 extend_face_to_end_of_line (it);
12097 /* Make sure we have the position. */
12098 if (used_before == 0)
12099 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12101 /* Consume the line end. This skips over invisible lines. */
12102 set_iterator_to_next (it, 1);
12103 it->continuation_lines_width = 0;
12104 break;
12107 /* Proceed with next display element. Note that this skips
12108 over lines invisible because of selective display. */
12109 set_iterator_to_next (it, 1);
12111 /* If we truncate lines, we are done when the last displayed
12112 glyphs reach past the right margin of the window. */
12113 if (it->truncate_lines_p
12114 && (FRAME_WINDOW_P (it->f)
12115 ? (it->current_x >= it->last_visible_x)
12116 : (it->current_x > it->last_visible_x)))
12118 /* Maybe add truncation glyphs. */
12119 if (!FRAME_WINDOW_P (it->f))
12121 --it->glyph_row->used[TEXT_AREA];
12122 produce_special_glyphs (it, IT_TRUNCATION);
12125 row->truncated_on_right_p = 1;
12126 it->continuation_lines_width = 0;
12127 reseat_at_next_visible_line_start (it, 0);
12128 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12129 it->hpos = hpos_before;
12130 it->current_x = x_before;
12131 break;
12135 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12136 at the left window margin. */
12137 if (it->first_visible_x
12138 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12140 if (!FRAME_WINDOW_P (it->f))
12141 insert_left_trunc_glyphs (it);
12142 row->truncated_on_left_p = 1;
12145 /* If the start of this line is the overlay arrow-position, then
12146 mark this glyph row as the one containing the overlay arrow.
12147 This is clearly a mess with variable size fonts. It would be
12148 better to let it be displayed like cursors under X. */
12149 if (MARKERP (Voverlay_arrow_position)
12150 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12151 && (MATRIX_ROW_START_CHARPOS (row)
12152 == marker_position (Voverlay_arrow_position))
12153 && STRINGP (Voverlay_arrow_string)
12154 && ! overlay_arrow_seen)
12156 /* Overlay arrow in window redisplay is a bitmap. */
12157 if (!FRAME_WINDOW_P (it->f))
12159 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12160 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12161 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12162 struct glyph *p = row->glyphs[TEXT_AREA];
12163 struct glyph *p2, *end;
12165 /* Copy the arrow glyphs. */
12166 while (glyph < arrow_end)
12167 *p++ = *glyph++;
12169 /* Throw away padding glyphs. */
12170 p2 = p;
12171 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12172 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12173 ++p2;
12174 if (p2 > p)
12176 while (p2 < end)
12177 *p++ = *p2++;
12178 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12182 overlay_arrow_seen = 1;
12183 row->overlay_arrow_p = 1;
12186 /* Compute pixel dimensions of this line. */
12187 compute_line_metrics (it);
12189 /* Remember the position at which this line ends. */
12190 row->end = it->current;
12192 /* Maybe set the cursor. */
12193 if (it->w->cursor.vpos < 0
12194 && PT >= MATRIX_ROW_START_CHARPOS (row)
12195 && PT <= MATRIX_ROW_END_CHARPOS (row)
12196 && cursor_row_p (it->w, row))
12197 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12199 /* Highlight trailing whitespace. */
12200 if (!NILP (Vshow_trailing_whitespace))
12201 highlight_trailing_whitespace (it->f, it->glyph_row);
12203 /* Prepare for the next line. This line starts horizontally at (X
12204 HPOS) = (0 0). Vertical positions are incremented. As a
12205 convenience for the caller, IT->glyph_row is set to the next
12206 row to be used. */
12207 it->current_x = it->hpos = 0;
12208 it->current_y += row->height;
12209 ++it->vpos;
12210 ++it->glyph_row;
12211 return row->displays_text_p;
12216 /***********************************************************************
12217 Menu Bar
12218 ***********************************************************************/
12220 /* Redisplay the menu bar in the frame for window W.
12222 The menu bar of X frames that don't have X toolkit support is
12223 displayed in a special window W->frame->menu_bar_window.
12225 The menu bar of terminal frames is treated specially as far as
12226 glyph matrices are concerned. Menu bar lines are not part of
12227 windows, so the update is done directly on the frame matrix rows
12228 for the menu bar. */
12230 static void
12231 display_menu_bar (w)
12232 struct window *w;
12234 struct frame *f = XFRAME (WINDOW_FRAME (w));
12235 struct it it;
12236 Lisp_Object items;
12237 int i;
12239 /* Don't do all this for graphical frames. */
12240 #ifdef HAVE_NTGUI
12241 if (!NILP (Vwindow_system))
12242 return;
12243 #endif
12244 #ifdef USE_X_TOOLKIT
12245 if (FRAME_X_P (f))
12246 return;
12247 #endif
12248 #ifdef macintosh
12249 if (FRAME_MAC_P (f))
12250 return;
12251 #endif
12253 #ifdef USE_X_TOOLKIT
12254 xassert (!FRAME_WINDOW_P (f));
12255 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12256 it.first_visible_x = 0;
12257 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12258 #else /* not USE_X_TOOLKIT */
12259 if (FRAME_WINDOW_P (f))
12261 /* Menu bar lines are displayed in the desired matrix of the
12262 dummy window menu_bar_window. */
12263 struct window *menu_w;
12264 xassert (WINDOWP (f->menu_bar_window));
12265 menu_w = XWINDOW (f->menu_bar_window);
12266 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12267 MENU_FACE_ID);
12268 it.first_visible_x = 0;
12269 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12271 else
12273 /* This is a TTY frame, i.e. character hpos/vpos are used as
12274 pixel x/y. */
12275 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12276 MENU_FACE_ID);
12277 it.first_visible_x = 0;
12278 it.last_visible_x = FRAME_WIDTH (f);
12280 #endif /* not USE_X_TOOLKIT */
12282 /* Clear all rows of the menu bar. */
12283 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12285 struct glyph_row *row = it.glyph_row + i;
12286 clear_glyph_row (row);
12287 row->enabled_p = 1;
12288 row->full_width_p = 1;
12291 /* Make the first line of the menu bar appear in reverse video. */
12292 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12294 /* Display all items of the menu bar. */
12295 items = FRAME_MENU_BAR_ITEMS (it.f);
12296 for (i = 0; i < XVECTOR (items)->size; i += 4)
12298 Lisp_Object string;
12300 /* Stop at nil string. */
12301 string = XVECTOR (items)->contents[i + 1];
12302 if (NILP (string))
12303 break;
12305 /* Remember where item was displayed. */
12306 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12308 /* Display the item, pad with one space. */
12309 if (it.current_x < it.last_visible_x)
12310 display_string (NULL, string, Qnil, 0, 0, &it,
12311 XSTRING (string)->size + 1, 0, 0, -1);
12314 /* Fill out the line with spaces. */
12315 if (it.current_x < it.last_visible_x)
12316 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12318 /* Compute the total height of the lines. */
12319 compute_line_metrics (&it);
12324 /***********************************************************************
12325 Mode Line
12326 ***********************************************************************/
12328 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12329 FORCE is non-zero, redisplay mode lines unconditionally.
12330 Otherwise, redisplay only mode lines that are garbaged. Value is
12331 the number of windows whose mode lines were redisplayed. */
12333 static int
12334 redisplay_mode_lines (window, force)
12335 Lisp_Object window;
12336 int force;
12338 int nwindows = 0;
12340 while (!NILP (window))
12342 struct window *w = XWINDOW (window);
12344 if (WINDOWP (w->hchild))
12345 nwindows += redisplay_mode_lines (w->hchild, force);
12346 else if (WINDOWP (w->vchild))
12347 nwindows += redisplay_mode_lines (w->vchild, force);
12348 else if (force
12349 || FRAME_GARBAGED_P (XFRAME (w->frame))
12350 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12352 Lisp_Object old_selected_frame;
12353 struct text_pos lpoint;
12354 struct buffer *old = current_buffer;
12356 /* Set the window's buffer for the mode line display. */
12357 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12358 set_buffer_internal_1 (XBUFFER (w->buffer));
12360 /* Point refers normally to the selected window. For any
12361 other window, set up appropriate value. */
12362 if (!EQ (window, selected_window))
12364 struct text_pos pt;
12366 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12367 if (CHARPOS (pt) < BEGV)
12368 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12369 else if (CHARPOS (pt) > (ZV - 1))
12370 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12371 else
12372 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12375 /* Temporarily set up the selected frame. */
12376 old_selected_frame = selected_frame;
12377 selected_frame = w->frame;
12379 /* Display mode lines. */
12380 clear_glyph_matrix (w->desired_matrix);
12381 if (display_mode_lines (w))
12383 ++nwindows;
12384 w->must_be_updated_p = 1;
12387 /* Restore old settings. */
12388 selected_frame = old_selected_frame;
12389 set_buffer_internal_1 (old);
12390 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12393 window = w->next;
12396 return nwindows;
12400 /* Display the mode and/or top line of window W. Value is the number
12401 of mode lines displayed. */
12403 static int
12404 display_mode_lines (w)
12405 struct window *w;
12407 int n = 0;
12409 /* These will be set while the mode line specs are processed. */
12410 line_number_displayed = 0;
12411 w->column_number_displayed = Qnil;
12413 if (WINDOW_WANTS_MODELINE_P (w))
12415 display_mode_line (w, MODE_LINE_FACE_ID,
12416 current_buffer->mode_line_format);
12417 ++n;
12420 if (WINDOW_WANTS_HEADER_LINE_P (w))
12422 display_mode_line (w, HEADER_LINE_FACE_ID,
12423 current_buffer->header_line_format);
12424 ++n;
12427 return n;
12431 /* Display mode or top line of window W. FACE_ID specifies which line
12432 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12433 FORMAT is the mode line format to display. */
12435 static void
12436 display_mode_line (w, face_id, format)
12437 struct window *w;
12438 enum face_id face_id;
12439 Lisp_Object format;
12441 struct it it;
12442 struct face *face;
12444 init_iterator (&it, w, -1, -1, NULL, face_id);
12445 prepare_desired_row (it.glyph_row);
12447 /* Temporarily make frame's keyboard the current kboard so that
12448 kboard-local variables in the mode_line_format will get the right
12449 values. */
12450 push_frame_kboard (it.f);
12451 display_mode_element (&it, 0, 0, 0, format);
12452 pop_frame_kboard ();
12454 /* Fill up with spaces. */
12455 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12457 compute_line_metrics (&it);
12458 it.glyph_row->full_width_p = 1;
12459 it.glyph_row->mode_line_p = 1;
12460 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12461 it.glyph_row->continued_p = 0;
12462 it.glyph_row->truncated_on_left_p = 0;
12463 it.glyph_row->truncated_on_right_p = 0;
12465 /* Make a 3D mode-line have a shadow at its right end. */
12466 face = FACE_FROM_ID (it.f, face_id);
12467 extend_face_to_end_of_line (&it);
12468 if (face->box != FACE_NO_BOX)
12470 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12471 + it.glyph_row->used[TEXT_AREA] - 1);
12472 last->right_box_line_p = 1;
12477 /* Contribute ELT to the mode line for window IT->w. How it
12478 translates into text depends on its data type.
12480 IT describes the display environment in which we display, as usual.
12482 DEPTH is the depth in recursion. It is used to prevent
12483 infinite recursion here.
12485 FIELD_WIDTH is the number of characters the display of ELT should
12486 occupy in the mode line, and PRECISION is the maximum number of
12487 characters to display from ELT's representation. See
12488 display_string for details. *
12490 Returns the hpos of the end of the text generated by ELT. */
12492 static int
12493 display_mode_element (it, depth, field_width, precision, elt)
12494 struct it *it;
12495 int depth;
12496 int field_width, precision;
12497 Lisp_Object elt;
12499 int n = 0, field, prec;
12501 tail_recurse:
12502 if (depth > 10)
12503 goto invalid;
12505 depth++;
12507 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12509 case Lisp_String:
12511 /* A string: output it and check for %-constructs within it. */
12512 unsigned char c;
12513 unsigned char *this = XSTRING (elt)->data;
12514 unsigned char *lisp_string = this;
12516 while ((precision <= 0 || n < precision)
12517 && *this
12518 && (frame_title_ptr
12519 || it->current_x < it->last_visible_x))
12521 unsigned char *last = this;
12523 /* Advance to end of string or next format specifier. */
12524 while ((c = *this++) != '\0' && c != '%')
12527 if (this - 1 != last)
12529 /* Output to end of string or up to '%'. Field width
12530 is length of string. Don't output more than
12531 PRECISION allows us. */
12532 prec = --this - last;
12533 if (precision > 0 && prec > precision - n)
12534 prec = precision - n;
12536 if (frame_title_ptr)
12537 n += store_frame_title (last, prec, prec);
12538 else
12539 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12540 it, 0, prec, 0, -1);
12542 else /* c == '%' */
12544 unsigned char *percent_position = this;
12546 /* Get the specified minimum width. Zero means
12547 don't pad. */
12548 field = 0;
12549 while ((c = *this++) >= '0' && c <= '9')
12550 field = field * 10 + c - '0';
12552 /* Don't pad beyond the total padding allowed. */
12553 if (field_width - n > 0 && field > field_width - n)
12554 field = field_width - n;
12556 /* Note that either PRECISION <= 0 or N < PRECISION. */
12557 prec = precision - n;
12559 if (c == 'M')
12560 n += display_mode_element (it, depth, field, prec,
12561 Vglobal_mode_string);
12562 else if (c != 0)
12564 unsigned char *spec
12565 = decode_mode_spec (it->w, c, field, prec);
12567 if (frame_title_ptr)
12568 n += store_frame_title (spec, field, prec);
12569 else
12571 int nglyphs_before
12572 = it->glyph_row->used[TEXT_AREA];
12573 int charpos
12574 = percent_position - XSTRING (elt)->data;
12575 int nwritten
12576 = display_string (spec, Qnil, elt, charpos, 0, it,
12577 field, prec, 0, -1);
12579 /* Assign to the glyphs written above the
12580 string where the `%x' came from, position
12581 of the `%'. */
12582 if (nwritten > 0)
12584 struct glyph *glyph
12585 = (it->glyph_row->glyphs[TEXT_AREA]
12586 + nglyphs_before);
12587 int i;
12589 for (i = 0; i < nwritten; ++i)
12591 glyph[i].object = elt;
12592 glyph[i].charpos = charpos;
12595 n += nwritten;
12602 break;
12604 case Lisp_Symbol:
12605 /* A symbol: process the value of the symbol recursively
12606 as if it appeared here directly. Avoid error if symbol void.
12607 Special case: if value of symbol is a string, output the string
12608 literally. */
12610 register Lisp_Object tem;
12611 tem = Fboundp (elt);
12612 if (!NILP (tem))
12614 tem = Fsymbol_value (elt);
12615 /* If value is a string, output that string literally:
12616 don't check for % within it. */
12617 if (STRINGP (tem))
12619 prec = XSTRING (tem)->size;
12620 if (precision > 0 && prec > precision - n)
12621 prec = precision - n;
12622 if (frame_title_ptr)
12623 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12624 else
12625 n += display_string (NULL, tem, Qnil, 0, 0, it,
12626 0, prec, 0, -1);
12628 else if (!EQ (tem, elt))
12630 /* Give up right away for nil or t. */
12631 elt = tem;
12632 goto tail_recurse;
12636 break;
12638 case Lisp_Cons:
12640 register Lisp_Object car, tem;
12642 /* A cons cell: three distinct cases.
12643 If first element is a string or a cons, process all the elements
12644 and effectively concatenate them.
12645 If first element is a negative number, truncate displaying cdr to
12646 at most that many characters. If positive, pad (with spaces)
12647 to at least that many characters.
12648 If first element is a symbol, process the cadr or caddr recursively
12649 according to whether the symbol's value is non-nil or nil. */
12650 car = XCAR (elt);
12651 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12653 /* An element of the form (:eval FORM) means evaluate FORM
12654 and use the result as mode line elements. */
12655 struct gcpro gcpro1;
12656 Lisp_Object spec;
12658 spec = safe_eval (XCAR (XCDR (elt)));
12659 GCPRO1 (spec);
12660 n += display_mode_element (it, depth, field_width - n,
12661 precision - n, spec);
12662 UNGCPRO;
12664 else if (SYMBOLP (car))
12666 tem = Fboundp (car);
12667 elt = XCDR (elt);
12668 if (!CONSP (elt))
12669 goto invalid;
12670 /* elt is now the cdr, and we know it is a cons cell.
12671 Use its car if CAR has a non-nil value. */
12672 if (!NILP (tem))
12674 tem = Fsymbol_value (car);
12675 if (!NILP (tem))
12677 elt = XCAR (elt);
12678 goto tail_recurse;
12681 /* Symbol's value is nil (or symbol is unbound)
12682 Get the cddr of the original list
12683 and if possible find the caddr and use that. */
12684 elt = XCDR (elt);
12685 if (NILP (elt))
12686 break;
12687 else if (!CONSP (elt))
12688 goto invalid;
12689 elt = XCAR (elt);
12690 goto tail_recurse;
12692 else if (INTEGERP (car))
12694 register int lim = XINT (car);
12695 elt = XCDR (elt);
12696 if (lim < 0)
12698 /* Negative int means reduce maximum width. */
12699 if (precision <= 0)
12700 precision = -lim;
12701 else
12702 precision = min (precision, -lim);
12704 else if (lim > 0)
12706 /* Padding specified. Don't let it be more than
12707 current maximum. */
12708 if (precision > 0)
12709 lim = min (precision, lim);
12711 /* If that's more padding than already wanted, queue it.
12712 But don't reduce padding already specified even if
12713 that is beyond the current truncation point. */
12714 field_width = max (lim, field_width);
12716 goto tail_recurse;
12718 else if (STRINGP (car) || CONSP (car))
12720 register int limit = 50;
12721 /* Limit is to protect against circular lists. */
12722 while (CONSP (elt)
12723 && --limit > 0
12724 && (precision <= 0 || n < precision))
12726 n += display_mode_element (it, depth, field_width - n,
12727 precision - n, XCAR (elt));
12728 elt = XCDR (elt);
12732 break;
12734 default:
12735 invalid:
12736 if (frame_title_ptr)
12737 n += store_frame_title ("*invalid*", 0, precision - n);
12738 else
12739 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12740 precision - n, 0, 0);
12741 return n;
12744 /* Pad to FIELD_WIDTH. */
12745 if (field_width > 0 && n < field_width)
12747 if (frame_title_ptr)
12748 n += store_frame_title ("", field_width - n, 0);
12749 else
12750 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12751 0, 0, 0);
12754 return n;
12758 /* Write a null-terminated, right justified decimal representation of
12759 the positive integer D to BUF using a minimal field width WIDTH. */
12761 static void
12762 pint2str (buf, width, d)
12763 register char *buf;
12764 register int width;
12765 register int d;
12767 register char *p = buf;
12769 if (d <= 0)
12770 *p++ = '0';
12771 else
12773 while (d > 0)
12775 *p++ = d % 10 + '0';
12776 d /= 10;
12780 for (width -= (int) (p - buf); width > 0; --width)
12781 *p++ = ' ';
12782 *p-- = '\0';
12783 while (p > buf)
12785 d = *buf;
12786 *buf++ = *p;
12787 *p-- = d;
12791 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12792 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12793 type of CODING_SYSTEM. Return updated pointer into BUF. */
12795 static unsigned char invalid_eol_type[] = "(*invalid*)";
12797 static char *
12798 decode_mode_spec_coding (coding_system, buf, eol_flag)
12799 Lisp_Object coding_system;
12800 register char *buf;
12801 int eol_flag;
12803 Lisp_Object val;
12804 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12805 unsigned char *eol_str;
12806 int eol_str_len;
12807 /* The EOL conversion we are using. */
12808 Lisp_Object eoltype;
12810 val = Fget (coding_system, Qcoding_system);
12811 eoltype = Qnil;
12813 if (!VECTORP (val)) /* Not yet decided. */
12815 if (multibyte)
12816 *buf++ = '-';
12817 if (eol_flag)
12818 eoltype = eol_mnemonic_undecided;
12819 /* Don't mention EOL conversion if it isn't decided. */
12821 else
12823 Lisp_Object eolvalue;
12825 eolvalue = Fget (coding_system, Qeol_type);
12827 if (multibyte)
12828 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12830 if (eol_flag)
12832 /* The EOL conversion that is normal on this system. */
12834 if (NILP (eolvalue)) /* Not yet decided. */
12835 eoltype = eol_mnemonic_undecided;
12836 else if (VECTORP (eolvalue)) /* Not yet decided. */
12837 eoltype = eol_mnemonic_undecided;
12838 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12839 eoltype = (XFASTINT (eolvalue) == 0
12840 ? eol_mnemonic_unix
12841 : (XFASTINT (eolvalue) == 1
12842 ? eol_mnemonic_dos : eol_mnemonic_mac));
12846 if (eol_flag)
12848 /* Mention the EOL conversion if it is not the usual one. */
12849 if (STRINGP (eoltype))
12851 eol_str = XSTRING (eoltype)->data;
12852 eol_str_len = XSTRING (eoltype)->size;
12854 else if (INTEGERP (eoltype)
12855 && CHAR_VALID_P (XINT (eoltype), 0))
12857 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12858 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12860 else
12862 eol_str = invalid_eol_type;
12863 eol_str_len = sizeof (invalid_eol_type) - 1;
12865 bcopy (eol_str, buf, eol_str_len);
12866 buf += eol_str_len;
12869 return buf;
12872 /* Return a string for the output of a mode line %-spec for window W,
12873 generated by character C. PRECISION >= 0 means don't return a
12874 string longer than that value. FIELD_WIDTH > 0 means pad the
12875 string returned with spaces to that value. */
12877 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12879 static char *
12880 decode_mode_spec (w, c, field_width, precision)
12881 struct window *w;
12882 register int c;
12883 int field_width, precision;
12885 Lisp_Object obj;
12886 struct frame *f = XFRAME (WINDOW_FRAME (w));
12887 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12888 struct buffer *b = XBUFFER (w->buffer);
12890 obj = Qnil;
12892 switch (c)
12894 case '*':
12895 if (!NILP (b->read_only))
12896 return "%";
12897 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12898 return "*";
12899 return "-";
12901 case '+':
12902 /* This differs from %* only for a modified read-only buffer. */
12903 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12904 return "*";
12905 if (!NILP (b->read_only))
12906 return "%";
12907 return "-";
12909 case '&':
12910 /* This differs from %* in ignoring read-only-ness. */
12911 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12912 return "*";
12913 return "-";
12915 case '%':
12916 return "%";
12918 case '[':
12920 int i;
12921 char *p;
12923 if (command_loop_level > 5)
12924 return "[[[... ";
12925 p = decode_mode_spec_buf;
12926 for (i = 0; i < command_loop_level; i++)
12927 *p++ = '[';
12928 *p = 0;
12929 return decode_mode_spec_buf;
12932 case ']':
12934 int i;
12935 char *p;
12937 if (command_loop_level > 5)
12938 return " ...]]]";
12939 p = decode_mode_spec_buf;
12940 for (i = 0; i < command_loop_level; i++)
12941 *p++ = ']';
12942 *p = 0;
12943 return decode_mode_spec_buf;
12946 case '-':
12948 register int i;
12950 /* Let lots_of_dashes be a string of infinite length. */
12951 if (field_width <= 0
12952 || field_width > sizeof (lots_of_dashes))
12954 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12955 decode_mode_spec_buf[i] = '-';
12956 decode_mode_spec_buf[i] = '\0';
12957 return decode_mode_spec_buf;
12959 else
12960 return lots_of_dashes;
12963 case 'b':
12964 obj = b->name;
12965 break;
12967 case 'c':
12969 int col = current_column ();
12970 XSETFASTINT (w->column_number_displayed, col);
12971 pint2str (decode_mode_spec_buf, field_width, col);
12972 return decode_mode_spec_buf;
12975 case 'F':
12976 /* %F displays the frame name. */
12977 if (!NILP (f->title))
12978 return (char *) XSTRING (f->title)->data;
12979 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12980 return (char *) XSTRING (f->name)->data;
12981 return "Emacs";
12983 case 'f':
12984 obj = b->filename;
12985 break;
12987 case 'l':
12989 int startpos = XMARKER (w->start)->charpos;
12990 int startpos_byte = marker_byte_position (w->start);
12991 int line, linepos, linepos_byte, topline;
12992 int nlines, junk;
12993 int height = XFASTINT (w->height);
12995 /* If we decided that this buffer isn't suitable for line numbers,
12996 don't forget that too fast. */
12997 if (EQ (w->base_line_pos, w->buffer))
12998 goto no_value;
12999 /* But do forget it, if the window shows a different buffer now. */
13000 else if (BUFFERP (w->base_line_pos))
13001 w->base_line_pos = Qnil;
13003 /* If the buffer is very big, don't waste time. */
13004 if (INTEGERP (Vline_number_display_limit)
13005 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13007 w->base_line_pos = Qnil;
13008 w->base_line_number = Qnil;
13009 goto no_value;
13012 if (!NILP (w->base_line_number)
13013 && !NILP (w->base_line_pos)
13014 && XFASTINT (w->base_line_pos) <= startpos)
13016 line = XFASTINT (w->base_line_number);
13017 linepos = XFASTINT (w->base_line_pos);
13018 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13020 else
13022 line = 1;
13023 linepos = BUF_BEGV (b);
13024 linepos_byte = BUF_BEGV_BYTE (b);
13027 /* Count lines from base line to window start position. */
13028 nlines = display_count_lines (linepos, linepos_byte,
13029 startpos_byte,
13030 startpos, &junk);
13032 topline = nlines + line;
13034 /* Determine a new base line, if the old one is too close
13035 or too far away, or if we did not have one.
13036 "Too close" means it's plausible a scroll-down would
13037 go back past it. */
13038 if (startpos == BUF_BEGV (b))
13040 XSETFASTINT (w->base_line_number, topline);
13041 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13043 else if (nlines < height + 25 || nlines > height * 3 + 50
13044 || linepos == BUF_BEGV (b))
13046 int limit = BUF_BEGV (b);
13047 int limit_byte = BUF_BEGV_BYTE (b);
13048 int position;
13049 int distance = (height * 2 + 30) * line_number_display_limit_width;
13051 if (startpos - distance > limit)
13053 limit = startpos - distance;
13054 limit_byte = CHAR_TO_BYTE (limit);
13057 nlines = display_count_lines (startpos, startpos_byte,
13058 limit_byte,
13059 - (height * 2 + 30),
13060 &position);
13061 /* If we couldn't find the lines we wanted within
13062 line_number_display_limit_width chars per line,
13063 give up on line numbers for this window. */
13064 if (position == limit_byte && limit == startpos - distance)
13066 w->base_line_pos = w->buffer;
13067 w->base_line_number = Qnil;
13068 goto no_value;
13071 XSETFASTINT (w->base_line_number, topline - nlines);
13072 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13075 /* Now count lines from the start pos to point. */
13076 nlines = display_count_lines (startpos, startpos_byte,
13077 PT_BYTE, PT, &junk);
13079 /* Record that we did display the line number. */
13080 line_number_displayed = 1;
13082 /* Make the string to show. */
13083 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13084 return decode_mode_spec_buf;
13085 no_value:
13087 char* p = decode_mode_spec_buf;
13088 int pad = field_width - 2;
13089 while (pad-- > 0)
13090 *p++ = ' ';
13091 *p++ = '?';
13092 *p++ = '?';
13093 *p = '\0';
13094 return decode_mode_spec_buf;
13097 break;
13099 case 'm':
13100 obj = b->mode_name;
13101 break;
13103 case 'n':
13104 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13105 return " Narrow";
13106 break;
13108 case 'p':
13110 int pos = marker_position (w->start);
13111 int total = BUF_ZV (b) - BUF_BEGV (b);
13113 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13115 if (pos <= BUF_BEGV (b))
13116 return "All";
13117 else
13118 return "Bottom";
13120 else if (pos <= BUF_BEGV (b))
13121 return "Top";
13122 else
13124 if (total > 1000000)
13125 /* Do it differently for a large value, to avoid overflow. */
13126 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13127 else
13128 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13129 /* We can't normally display a 3-digit number,
13130 so get us a 2-digit number that is close. */
13131 if (total == 100)
13132 total = 99;
13133 sprintf (decode_mode_spec_buf, "%2d%%", total);
13134 return decode_mode_spec_buf;
13138 /* Display percentage of size above the bottom of the screen. */
13139 case 'P':
13141 int toppos = marker_position (w->start);
13142 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13143 int total = BUF_ZV (b) - BUF_BEGV (b);
13145 if (botpos >= BUF_ZV (b))
13147 if (toppos <= BUF_BEGV (b))
13148 return "All";
13149 else
13150 return "Bottom";
13152 else
13154 if (total > 1000000)
13155 /* Do it differently for a large value, to avoid overflow. */
13156 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13157 else
13158 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13159 /* We can't normally display a 3-digit number,
13160 so get us a 2-digit number that is close. */
13161 if (total == 100)
13162 total = 99;
13163 if (toppos <= BUF_BEGV (b))
13164 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13165 else
13166 sprintf (decode_mode_spec_buf, "%2d%%", total);
13167 return decode_mode_spec_buf;
13171 case 's':
13172 /* status of process */
13173 obj = Fget_buffer_process (w->buffer);
13174 if (NILP (obj))
13175 return "no process";
13176 #ifdef subprocesses
13177 obj = Fsymbol_name (Fprocess_status (obj));
13178 #endif
13179 break;
13181 case 't': /* indicate TEXT or BINARY */
13182 #ifdef MODE_LINE_BINARY_TEXT
13183 return MODE_LINE_BINARY_TEXT (b);
13184 #else
13185 return "T";
13186 #endif
13188 case 'z':
13189 /* coding-system (not including end-of-line format) */
13190 case 'Z':
13191 /* coding-system (including end-of-line type) */
13193 int eol_flag = (c == 'Z');
13194 char *p = decode_mode_spec_buf;
13196 if (! FRAME_WINDOW_P (f))
13198 /* No need to mention EOL here--the terminal never needs
13199 to do EOL conversion. */
13200 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13201 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13203 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13204 p, eol_flag);
13206 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13207 #ifdef subprocesses
13208 obj = Fget_buffer_process (Fcurrent_buffer ());
13209 if (PROCESSP (obj))
13211 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13212 p, eol_flag);
13213 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13214 p, eol_flag);
13216 #endif /* subprocesses */
13217 #endif /* 0 */
13218 *p = 0;
13219 return decode_mode_spec_buf;
13223 if (STRINGP (obj))
13224 return (char *) XSTRING (obj)->data;
13225 else
13226 return "";
13230 /* Count up to COUNT lines starting from START / START_BYTE.
13231 But don't go beyond LIMIT_BYTE.
13232 Return the number of lines thus found (always nonnegative).
13234 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13236 static int
13237 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13238 int start, start_byte, limit_byte, count;
13239 int *byte_pos_ptr;
13241 register unsigned char *cursor;
13242 unsigned char *base;
13244 register int ceiling;
13245 register unsigned char *ceiling_addr;
13246 int orig_count = count;
13248 /* If we are not in selective display mode,
13249 check only for newlines. */
13250 int selective_display = (!NILP (current_buffer->selective_display)
13251 && !INTEGERP (current_buffer->selective_display));
13253 if (count > 0)
13255 while (start_byte < limit_byte)
13257 ceiling = BUFFER_CEILING_OF (start_byte);
13258 ceiling = min (limit_byte - 1, ceiling);
13259 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13260 base = (cursor = BYTE_POS_ADDR (start_byte));
13261 while (1)
13263 if (selective_display)
13264 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13266 else
13267 while (*cursor != '\n' && ++cursor != ceiling_addr)
13270 if (cursor != ceiling_addr)
13272 if (--count == 0)
13274 start_byte += cursor - base + 1;
13275 *byte_pos_ptr = start_byte;
13276 return orig_count;
13278 else
13279 if (++cursor == ceiling_addr)
13280 break;
13282 else
13283 break;
13285 start_byte += cursor - base;
13288 else
13290 while (start_byte > limit_byte)
13292 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13293 ceiling = max (limit_byte, ceiling);
13294 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13295 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13296 while (1)
13298 if (selective_display)
13299 while (--cursor != ceiling_addr
13300 && *cursor != '\n' && *cursor != 015)
13302 else
13303 while (--cursor != ceiling_addr && *cursor != '\n')
13306 if (cursor != ceiling_addr)
13308 if (++count == 0)
13310 start_byte += cursor - base + 1;
13311 *byte_pos_ptr = start_byte;
13312 /* When scanning backwards, we should
13313 not count the newline posterior to which we stop. */
13314 return - orig_count - 1;
13317 else
13318 break;
13320 /* Here we add 1 to compensate for the last decrement
13321 of CURSOR, which took it past the valid range. */
13322 start_byte += cursor - base + 1;
13326 *byte_pos_ptr = limit_byte;
13328 if (count < 0)
13329 return - orig_count + count;
13330 return orig_count - count;
13336 /***********************************************************************
13337 Displaying strings
13338 ***********************************************************************/
13340 /* Display a NUL-terminated string, starting with index START.
13342 If STRING is non-null, display that C string. Otherwise, the Lisp
13343 string LISP_STRING is displayed.
13345 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13346 FACE_STRING. Display STRING or LISP_STRING with the face at
13347 FACE_STRING_POS in FACE_STRING:
13349 Display the string in the environment given by IT, but use the
13350 standard display table, temporarily.
13352 FIELD_WIDTH is the minimum number of output glyphs to produce.
13353 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13354 with spaces. If STRING has more characters, more than FIELD_WIDTH
13355 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13357 PRECISION is the maximum number of characters to output from
13358 STRING. PRECISION < 0 means don't truncate the string.
13360 This is roughly equivalent to printf format specifiers:
13362 FIELD_WIDTH PRECISION PRINTF
13363 ----------------------------------------
13364 -1 -1 %s
13365 -1 10 %.10s
13366 10 -1 %10s
13367 20 10 %20.10s
13369 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13370 display them, and < 0 means obey the current buffer's value of
13371 enable_multibyte_characters.
13373 Value is the number of glyphs produced. */
13375 static int
13376 display_string (string, lisp_string, face_string, face_string_pos,
13377 start, it, field_width, precision, max_x, multibyte)
13378 unsigned char *string;
13379 Lisp_Object lisp_string;
13380 Lisp_Object face_string;
13381 int face_string_pos;
13382 int start;
13383 struct it *it;
13384 int field_width, precision, max_x;
13385 int multibyte;
13387 int hpos_at_start = it->hpos;
13388 int saved_face_id = it->face_id;
13389 struct glyph_row *row = it->glyph_row;
13391 /* Initialize the iterator IT for iteration over STRING beginning
13392 with index START. We assume that IT may be modified here (which
13393 means that display_line has to do something when displaying a
13394 mini-buffer prompt, which it does). */
13395 reseat_to_string (it, string, lisp_string, start,
13396 precision, field_width, multibyte);
13398 /* If displaying STRING, set up the face of the iterator
13399 from LISP_STRING, if that's given. */
13400 if (STRINGP (face_string))
13402 int endptr;
13403 struct face *face;
13405 it->face_id
13406 = face_at_string_position (it->w, face_string, face_string_pos,
13407 0, it->region_beg_charpos,
13408 it->region_end_charpos,
13409 &endptr, it->base_face_id);
13410 face = FACE_FROM_ID (it->f, it->face_id);
13411 it->face_box_p = face->box != FACE_NO_BOX;
13414 /* Set max_x to the maximum allowed X position. Don't let it go
13415 beyond the right edge of the window. */
13416 if (max_x <= 0)
13417 max_x = it->last_visible_x;
13418 else
13419 max_x = min (max_x, it->last_visible_x);
13421 /* Skip over display elements that are not visible. because IT->w is
13422 hscrolled. */
13423 if (it->current_x < it->first_visible_x)
13424 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13425 MOVE_TO_POS | MOVE_TO_X);
13427 row->ascent = it->max_ascent;
13428 row->height = it->max_ascent + it->max_descent;
13429 row->phys_ascent = it->max_phys_ascent;
13430 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13432 /* This condition is for the case that we are called with current_x
13433 past last_visible_x. */
13434 while (it->current_x < max_x)
13436 int x_before, x, n_glyphs_before, i, nglyphs;
13438 /* Get the next display element. */
13439 if (!get_next_display_element (it))
13440 break;
13442 /* Produce glyphs. */
13443 x_before = it->current_x;
13444 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13445 PRODUCE_GLYPHS (it);
13447 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13448 i = 0;
13449 x = x_before;
13450 while (i < nglyphs)
13452 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13454 if (!it->truncate_lines_p
13455 && x + glyph->pixel_width > max_x)
13457 /* End of continued line or max_x reached. */
13458 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13459 it->current_x = x;
13460 break;
13462 else if (x + glyph->pixel_width > it->first_visible_x)
13464 /* Glyph is at least partially visible. */
13465 ++it->hpos;
13466 if (x < it->first_visible_x)
13467 it->glyph_row->x = x - it->first_visible_x;
13469 else
13471 /* Glyph is off the left margin of the display area.
13472 Should not happen. */
13473 abort ();
13476 row->ascent = max (row->ascent, it->max_ascent);
13477 row->height = max (row->height, it->max_ascent + it->max_descent);
13478 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13479 row->phys_height = max (row->phys_height,
13480 it->max_phys_ascent + it->max_phys_descent);
13481 x += glyph->pixel_width;
13482 ++i;
13485 /* Stop if max_x reached. */
13486 if (i < nglyphs)
13487 break;
13489 /* Stop at line ends. */
13490 if (ITERATOR_AT_END_OF_LINE_P (it))
13492 it->continuation_lines_width = 0;
13493 break;
13496 set_iterator_to_next (it, 1);
13498 /* Stop if truncating at the right edge. */
13499 if (it->truncate_lines_p
13500 && it->current_x >= it->last_visible_x)
13502 /* Add truncation mark, but don't do it if the line is
13503 truncated at a padding space. */
13504 if (IT_CHARPOS (*it) < it->string_nchars)
13506 if (!FRAME_WINDOW_P (it->f))
13507 produce_special_glyphs (it, IT_TRUNCATION);
13508 it->glyph_row->truncated_on_right_p = 1;
13510 break;
13514 /* Maybe insert a truncation at the left. */
13515 if (it->first_visible_x
13516 && IT_CHARPOS (*it) > 0)
13518 if (!FRAME_WINDOW_P (it->f))
13519 insert_left_trunc_glyphs (it);
13520 it->glyph_row->truncated_on_left_p = 1;
13523 it->face_id = saved_face_id;
13525 /* Value is number of columns displayed. */
13526 return it->hpos - hpos_at_start;
13531 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13532 appears as an element of LIST or as the car of an element of LIST.
13533 If PROPVAL is a list, compare each element against LIST in that
13534 way, and return 1 if any element of PROPVAL is found in LIST.
13535 Otherwise return 0. This function cannot quit. */
13538 invisible_p (propval, list)
13539 register Lisp_Object propval;
13540 Lisp_Object list;
13542 register Lisp_Object tail, proptail;
13544 for (tail = list; CONSP (tail); tail = XCDR (tail))
13546 register Lisp_Object tem;
13547 tem = XCAR (tail);
13548 if (EQ (propval, tem))
13549 return 1;
13550 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13551 return 1;
13554 if (CONSP (propval))
13556 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13558 Lisp_Object propelt;
13559 propelt = XCAR (proptail);
13560 for (tail = list; CONSP (tail); tail = XCDR (tail))
13562 register Lisp_Object tem;
13563 tem = XCAR (tail);
13564 if (EQ (propelt, tem))
13565 return 1;
13566 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13567 return 1;
13572 return 0;
13576 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13577 the cdr of that element is non-nil. If PROPVAL is a list, check
13578 each element of PROPVAL in that way, and the first time some
13579 element is found, return 1 if the cdr of that element is non-nil.
13580 Otherwise return 0. This function cannot quit. */
13583 invisible_ellipsis_p (propval, list)
13584 register Lisp_Object propval;
13585 Lisp_Object list;
13587 register Lisp_Object tail, proptail;
13589 for (tail = list; CONSP (tail); tail = XCDR (tail))
13591 register Lisp_Object tem;
13592 tem = XCAR (tail);
13593 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13594 return ! NILP (XCDR (tem));
13597 if (CONSP (propval))
13598 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13600 Lisp_Object propelt;
13601 propelt = XCAR (proptail);
13602 for (tail = list; CONSP (tail); tail = XCDR (tail))
13604 register Lisp_Object tem;
13605 tem = XCAR (tail);
13606 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13607 return ! NILP (XCDR (tem));
13611 return 0;
13616 /***********************************************************************
13617 Initialization
13618 ***********************************************************************/
13620 void
13621 syms_of_xdisp ()
13623 Vwith_echo_area_save_vector = Qnil;
13624 staticpro (&Vwith_echo_area_save_vector);
13626 Vmessage_stack = Qnil;
13627 staticpro (&Vmessage_stack);
13629 Qinhibit_redisplay = intern ("inhibit-redisplay");
13630 staticpro (&Qinhibit_redisplay);
13632 #if GLYPH_DEBUG
13633 defsubr (&Sdump_glyph_matrix);
13634 defsubr (&Sdump_glyph_row);
13635 defsubr (&Sdump_tool_bar_row);
13636 defsubr (&Strace_redisplay_toggle);
13637 defsubr (&Strace_to_stderr);
13638 #endif
13640 staticpro (&Qmenu_bar_update_hook);
13641 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13643 staticpro (&Qoverriding_terminal_local_map);
13644 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13646 staticpro (&Qoverriding_local_map);
13647 Qoverriding_local_map = intern ("overriding-local-map");
13649 staticpro (&Qwindow_scroll_functions);
13650 Qwindow_scroll_functions = intern ("window-scroll-functions");
13652 staticpro (&Qredisplay_end_trigger_functions);
13653 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13655 staticpro (&Qinhibit_point_motion_hooks);
13656 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13658 QCdata = intern (":data");
13659 staticpro (&QCdata);
13660 Qdisplay = intern ("display");
13661 staticpro (&Qdisplay);
13662 Qspace_width = intern ("space-width");
13663 staticpro (&Qspace_width);
13664 Qraise = intern ("raise");
13665 staticpro (&Qraise);
13666 Qspace = intern ("space");
13667 staticpro (&Qspace);
13668 Qmargin = intern ("margin");
13669 staticpro (&Qmargin);
13670 Qleft_margin = intern ("left-margin");
13671 staticpro (&Qleft_margin);
13672 Qright_margin = intern ("right-margin");
13673 staticpro (&Qright_margin);
13674 Qalign_to = intern ("align-to");
13675 staticpro (&Qalign_to);
13676 QCalign_to = intern (":align-to");
13677 staticpro (&QCalign_to);
13678 Qrelative_width = intern ("relative-width");
13679 staticpro (&Qrelative_width);
13680 QCrelative_width = intern (":relative-width");
13681 staticpro (&QCrelative_width);
13682 QCrelative_height = intern (":relative-height");
13683 staticpro (&QCrelative_height);
13684 QCeval = intern (":eval");
13685 staticpro (&QCeval);
13686 Qwhen = intern ("when");
13687 staticpro (&Qwhen);
13688 QCfile = intern (":file");
13689 staticpro (&QCfile);
13690 Qfontified = intern ("fontified");
13691 staticpro (&Qfontified);
13692 Qfontification_functions = intern ("fontification-functions");
13693 staticpro (&Qfontification_functions);
13694 Qtrailing_whitespace = intern ("trailing-whitespace");
13695 staticpro (&Qtrailing_whitespace);
13696 Qimage = intern ("image");
13697 staticpro (&Qimage);
13698 Qmessage_truncate_lines = intern ("message-truncate-lines");
13699 staticpro (&Qmessage_truncate_lines);
13700 Qgrow_only = intern ("grow-only");
13701 staticpro (&Qgrow_only);
13703 last_arrow_position = Qnil;
13704 last_arrow_string = Qnil;
13705 staticpro (&last_arrow_position);
13706 staticpro (&last_arrow_string);
13708 echo_buffer[0] = echo_buffer[1] = Qnil;
13709 staticpro (&echo_buffer[0]);
13710 staticpro (&echo_buffer[1]);
13712 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13713 staticpro (&echo_area_buffer[0]);
13714 staticpro (&echo_area_buffer[1]);
13716 Vmessages_buffer_name = build_string ("*Messages*");
13717 staticpro (&Vmessages_buffer_name);
13719 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13720 "Non-nil means highlight trailing whitespace.\n\
13721 The face used for trailing whitespace is `trailing-whitespace'.");
13722 Vshow_trailing_whitespace = Qnil;
13724 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13725 "Non-nil means don't actually do any redisplay.\n\
13726 This is used for internal purposes.");
13727 Vinhibit_redisplay = Qnil;
13729 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13730 "String (or mode line construct) included (normally) in `mode-line-format'.");
13731 Vglobal_mode_string = Qnil;
13733 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13734 "Marker for where to display an arrow on top of the buffer text.\n\
13735 This must be the beginning of a line in order to work.\n\
13736 See also `overlay-arrow-string'.");
13737 Voverlay_arrow_position = Qnil;
13739 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13740 "String to display as an arrow. See also `overlay-arrow-position'.");
13741 Voverlay_arrow_string = Qnil;
13743 DEFVAR_INT ("scroll-step", &scroll_step,
13744 "*The number of lines to try scrolling a window by when point moves out.\n\
13745 If that fails to bring point back on frame, point is centered instead.\n\
13746 If this is zero, point is always centered after it moves off frame.");
13748 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13749 "*Scroll up to this many lines, to bring point back on screen.\n\
13750 A value of zero means to scroll the text to center point vertically\n\
13751 in the window.");
13752 scroll_conservatively = 0;
13754 DEFVAR_INT ("scroll-margin", &scroll_margin,
13755 "*Number of lines of margin at the top and bottom of a window.\n\
13756 Recenter the window whenever point gets within this many lines\n\
13757 of the top or bottom of the window.");
13758 scroll_margin = 0;
13760 #if GLYPH_DEBUG
13761 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13762 #endif
13764 DEFVAR_BOOL ("truncate-partial-width-windows",
13765 &truncate_partial_width_windows,
13766 "*Non-nil means truncate lines in all windows less than full frame wide.");
13767 truncate_partial_width_windows = 1;
13769 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13770 "*Non-nil means use inverse video for the mode line.");
13771 mode_line_inverse_video = 1;
13773 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13774 "*Maximum buffer size for which line number should be displayed.\n\
13775 If the buffer is bigger than this, the line number does not appear\n\
13776 in the mode line. A value of nil means no limit.");
13777 Vline_number_display_limit = Qnil;
13779 DEFVAR_INT ("line-number-display-limit-width",
13780 &line_number_display_limit_width,
13781 "*Maximum line width (in characters) for line number display.\n\
13782 If the average length of the lines near point is bigger than this, then the\n\
13783 line number may be omitted from the mode line.");
13784 line_number_display_limit_width = 200;
13786 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13787 "*Non-nil means highlight region even in nonselected windows.");
13788 highlight_nonselected_windows = 0;
13790 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13791 "Non-nil if more than one frame is visible on this display.\n\
13792 Minibuffer-only frames don't count, but iconified frames do.\n\
13793 This variable is not guaranteed to be accurate except while processing\n\
13794 `frame-title-format' and `icon-title-format'.");
13796 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13797 "Template for displaying the title bar of visible frames.\n\
13798 \(Assuming the window manager supports this feature.)\n\
13799 This variable has the same structure as `mode-line-format' (which see),\n\
13800 and is used only on frames for which no explicit name has been set\n\
13801 \(see `modify-frame-parameters').");
13802 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13803 "Template for displaying the title bar of an iconified frame.\n\
13804 \(Assuming the window manager supports this feature.)\n\
13805 This variable has the same structure as `mode-line-format' (which see),\n\
13806 and is used only on frames for which no explicit name has been set\n\
13807 \(see `modify-frame-parameters').");
13808 Vicon_title_format
13809 = Vframe_title_format
13810 = Fcons (intern ("multiple-frames"),
13811 Fcons (build_string ("%b"),
13812 Fcons (Fcons (build_string (""),
13813 Fcons (intern ("invocation-name"),
13814 Fcons (build_string ("@"),
13815 Fcons (intern ("system-name"),
13816 Qnil)))),
13817 Qnil)));
13819 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13820 "Maximum number of lines to keep in the message log buffer.\n\
13821 If nil, disable message logging. If t, log messages but don't truncate\n\
13822 the buffer when it becomes large.");
13823 XSETFASTINT (Vmessage_log_max, 50);
13825 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13826 "Functions called before redisplay, if window sizes have changed.\n\
13827 The value should be a list of functions that take one argument.\n\
13828 Just before redisplay, for each frame, if any of its windows have changed\n\
13829 size since the last redisplay, or have been split or deleted,\n\
13830 all the functions in the list are called, with the frame as argument.");
13831 Vwindow_size_change_functions = Qnil;
13833 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13834 "List of Functions to call before redisplaying a window with scrolling.\n\
13835 Each function is called with two arguments, the window\n\
13836 and its new display-start position. Note that the value of `window-end'\n\
13837 is not valid when these functions are called.");
13838 Vwindow_scroll_functions = Qnil;
13840 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13841 "*Non-nil means automatically resize tool-bars.\n\
13842 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13843 It decreases a tool-bar's height when it would display blank lines\n\
13844 otherwise.");
13845 auto_resize_tool_bars_p = 1;
13847 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13848 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13849 auto_raise_tool_bar_buttons_p = 1;
13851 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13852 "*Margin around tool-bar buttons in pixels.");
13853 tool_bar_button_margin = 1;
13855 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13856 "Relief thickness of tool-bar buttons.");
13857 tool_bar_button_relief = 3;
13859 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13860 "List of functions to call to fontify regions of text.\n\
13861 Each function is called with one argument POS. Functions must\n\
13862 fontify a region starting at POS in the current buffer, and give\n\
13863 fontified regions the property `fontified'.\n\
13864 This variable automatically becomes buffer-local when set.");
13865 Vfontification_functions = Qnil;
13866 Fmake_local_variable (Qfontification_functions);
13868 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13869 &unibyte_display_via_language_environment,
13870 "*Non-nil means display unibyte text according to language environment.\n\
13871 Specifically this means that unibyte non-ASCII characters\n\
13872 are displayed by converting them to the equivalent multibyte characters\n\
13873 according to the current language environment. As a result, they are\n\
13874 displayed according to the current fontset.");
13875 unibyte_display_via_language_environment = 0;
13877 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13878 "*Maximum height for resizing mini-windows.\n\
13879 If a float, it specifies a fraction of the mini-window frame's height.\n\
13880 If an integer, it specifies a number of lines.");
13881 Vmax_mini_window_height = make_float (0.25);
13883 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
13884 "*How to resize the mini-window.\n\
13885 A value of nil means don't automatically resize mini-windows.\n\
13886 A value of t means resize it to fit the text displayed in it.\n\
13887 A value of `grow-only', the default, means let mini-windows grow\n\
13888 only, until the its display becomes empty, at which point the mini-window\n\
13889 goes back to its normal size.");
13890 Vresize_mini_windows = Qgrow_only;
13892 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13893 &cursor_in_non_selected_windows,
13894 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13895 Nil means don't display a cursor there.");
13896 cursor_in_non_selected_windows = 1;
13898 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13899 "*Non-nil means scroll the display automatically to make point visible.");
13900 automatic_hscrolling_p = 1;
13902 DEFVAR_LISP ("image-types", &Vimage_types,
13903 "List of supported image types.\n\
13904 Each element of the list is a symbol for a supported image type.");
13905 Vimage_types = Qnil;
13907 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13908 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13909 Bind this around calls to `message' to let it take effect.");
13910 message_truncate_lines = 0;
13912 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13913 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13914 Can be used to update submenus whose contents should vary.");
13915 Vmenu_bar_update_hook = Qnil;
13919 /* Initialize this module when Emacs starts. */
13921 void
13922 init_xdisp ()
13924 Lisp_Object root_window;
13925 struct window *mini_w;
13927 CHARPOS (this_line_start_pos) = 0;
13929 mini_w = XWINDOW (minibuf_window);
13930 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13932 if (!noninteractive)
13934 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13935 int i;
13937 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13938 set_window_height (root_window,
13939 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13941 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13942 set_window_height (minibuf_window, 1, 0);
13944 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13945 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13947 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13948 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13949 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13951 /* The default ellipsis glyphs `...'. */
13952 for (i = 0; i < 3; ++i)
13953 XSETFASTINT (default_invis_vector[i], '.');
13956 #ifdef HAVE_WINDOW_SYSTEM
13958 /* Allocate the buffer for frame titles. */
13959 int size = 100;
13960 frame_title_buf = (char *) xmalloc (size);
13961 frame_title_buf_end = frame_title_buf + size;
13962 frame_title_ptr = NULL;
13964 #endif /* HAVE_WINDOW_SYSTEM */
13966 help_echo_showing_p = 0;