(find-function-do-it): Quote the hook.
[emacs.git] / src / xdisp.c
blobd5b16ec571ae2f391e90d676b20c810f1dc0047d
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;
229 /* Functions called to fontify regions of text. */
231 Lisp_Object Vfontification_functions;
232 Lisp_Object Qfontification_functions;
234 /* Non-zero means draw tool bar buttons raised when the mouse moves
235 over them. */
237 int auto_raise_tool_bar_buttons_p;
239 /* Margin around tool bar buttons in pixels. */
241 int tool_bar_button_margin;
243 /* Thickness of shadow to draw around tool bar buttons. */
245 int tool_bar_button_relief;
247 /* Non-zero means automatically resize tool-bars so that all tool-bar
248 items are visible, and no blank lines remain. */
250 int auto_resize_tool_bars_p;
252 /* Non-nil means don't actually do any redisplay. */
254 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256 /* Names of text properties relevant for redisplay. */
258 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
259 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
261 /* Symbols used in text property values. */
263 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
264 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
265 Lisp_Object Qmargin;
266 extern Lisp_Object Qheight;
268 /* Non-nil means highlight trailing whitespace. */
270 Lisp_Object Vshow_trailing_whitespace;
272 /* Name of the face used to highlight trailing whitespace. */
274 Lisp_Object Qtrailing_whitespace;
276 /* The symbol `image' which is the car of the lists used to represent
277 images in Lisp. */
279 Lisp_Object Qimage;
281 /* Non-zero means print newline to stdout before next mini-buffer
282 message. */
284 int noninteractive_need_newline;
286 /* Non-zero means print newline to message log before next message. */
288 static int message_log_need_newline;
291 /* The buffer position of the first character appearing entirely or
292 partially on the line of the selected window which contains the
293 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
294 redisplay optimization in redisplay_internal. */
296 static struct text_pos this_line_start_pos;
298 /* Number of characters past the end of the line above, including the
299 terminating newline. */
301 static struct text_pos this_line_end_pos;
303 /* The vertical positions and the height of this line. */
305 static int this_line_vpos;
306 static int this_line_y;
307 static int this_line_pixel_height;
309 /* X position at which this display line starts. Usually zero;
310 negative if first character is partially visible. */
312 static int this_line_start_x;
314 /* Buffer that this_line_.* variables are referring to. */
316 static struct buffer *this_line_buffer;
318 /* Nonzero means truncate lines in all windows less wide than the
319 frame. */
321 int truncate_partial_width_windows;
323 /* A flag to control how to display unibyte 8-bit character. */
325 int unibyte_display_via_language_environment;
327 /* Nonzero means we have more than one non-mini-buffer-only frame.
328 Not guaranteed to be accurate except while parsing
329 frame-title-format. */
331 int multiple_frames;
333 Lisp_Object Vglobal_mode_string;
335 /* Marker for where to display an arrow on top of the buffer text. */
337 Lisp_Object Voverlay_arrow_position;
339 /* String to display for the arrow. Only used on terminal frames. */
341 Lisp_Object Voverlay_arrow_string;
343 /* Values of those variables at last redisplay. However, if
344 Voverlay_arrow_position is a marker, last_arrow_position is its
345 numerical position. */
347 static Lisp_Object last_arrow_position, last_arrow_string;
349 /* Like mode-line-format, but for the title bar on a visible frame. */
351 Lisp_Object Vframe_title_format;
353 /* Like mode-line-format, but for the title bar on an iconified frame. */
355 Lisp_Object Vicon_title_format;
357 /* List of functions to call when a window's size changes. These
358 functions get one arg, a frame on which one or more windows' sizes
359 have changed. */
361 static Lisp_Object Vwindow_size_change_functions;
363 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
365 /* Nonzero if overlay arrow has been displayed once in this window. */
367 static int overlay_arrow_seen;
369 /* Nonzero means highlight the region even in nonselected windows. */
371 int highlight_nonselected_windows;
373 /* If cursor motion alone moves point off frame, try scrolling this
374 many lines up or down if that will bring it back. */
376 static int scroll_step;
378 /* Non-0 means scroll just far enough to bring point back on the
379 screen, when appropriate. */
381 static int scroll_conservatively;
383 /* Recenter the window whenever point gets within this many lines of
384 the top or bottom of the window. This value is translated into a
385 pixel value by multiplying it with CANON_Y_UNIT, which means that
386 there is really a fixed pixel height scroll margin. */
388 int scroll_margin;
390 /* Number of windows showing the buffer of the selected window (or
391 another buffer with the same base buffer). keyboard.c refers to
392 this. */
394 int buffer_shared;
396 /* Vector containing glyphs for an ellipsis `...'. */
398 static Lisp_Object default_invis_vector[3];
400 /* Nonzero means display mode line highlighted. */
402 int mode_line_inverse_video;
404 /* Prompt to display in front of the mini-buffer contents. */
406 Lisp_Object minibuf_prompt;
408 /* Width of current mini-buffer prompt. Only set after display_line
409 of the line that contains the prompt. */
411 int minibuf_prompt_width;
412 int minibuf_prompt_pixel_width;
414 /* This is the window where the echo area message was displayed. It
415 is always a mini-buffer window, but it may not be the same window
416 currently active as a mini-buffer. */
418 Lisp_Object echo_area_window;
420 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
421 pushes the current message and the value of
422 message_enable_multibyte on the stack, the function restore_message
423 pops the stack and displays MESSAGE again. */
425 Lisp_Object Vmessage_stack;
427 /* Nonzero means multibyte characters were enabled when the echo area
428 message was specified. */
430 int message_enable_multibyte;
432 /* True if we should redraw the mode lines on the next redisplay. */
434 int update_mode_lines;
436 /* Nonzero if window sizes or contents have changed since last
437 redisplay that finished */
439 int windows_or_buffers_changed;
441 /* Nonzero after display_mode_line if %l was used and it displayed a
442 line number. */
444 int line_number_displayed;
446 /* Maximum buffer size for which to display line numbers. */
448 Lisp_Object Vline_number_display_limit;
450 /* line width to consider when repostioning for line number display */
452 static int line_number_display_limit_width;
454 /* Number of lines to keep in the message log buffer. t means
455 infinite. nil means don't log at all. */
457 Lisp_Object Vmessage_log_max;
459 /* The name of the *Messages* buffer, a string. */
461 static Lisp_Object Vmessages_buffer_name;
463 /* Current, index 0, and last displayed echo area message. Either
464 buffers from echo_buffers, or nil to indicate no message. */
466 Lisp_Object echo_area_buffer[2];
468 /* The buffers referenced from echo_area_buffer. */
470 static Lisp_Object echo_buffer[2];
472 /* A vector saved used in with_area_buffer to reduce consing. */
474 static Lisp_Object Vwith_echo_area_save_vector;
476 /* Non-zero means display_echo_area should display the last echo area
477 message again. Set by redisplay_preserve_echo_area. */
479 static int display_last_displayed_message_p;
481 /* Nonzero if echo area is being used by print; zero if being used by
482 message. */
484 int message_buf_print;
486 /* Maximum height for resizing mini-windows. Either a float
487 specifying a fraction of the available height, or an integer
488 specifying a number of lines. */
490 Lisp_Object Vmax_mini_window_height;
492 /* Non-zero means messages should be displayed with truncated
493 lines instead of being continued. */
495 int message_truncate_lines;
496 Lisp_Object Qmessage_truncate_lines;
498 /* Non-zero means we want a hollow cursor in windows that are not
499 selected. Zero means there's no cursor in such windows. */
501 int cursor_in_non_selected_windows;
503 /* A scratch glyph row with contents used for generating truncation
504 glyphs. Also used in direct_output_for_insert. */
506 #define MAX_SCRATCH_GLYPHS 100
507 struct glyph_row scratch_glyph_row;
508 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
510 /* Ascent and height of the last line processed by move_it_to. */
512 static int last_max_ascent, last_height;
514 /* Non-zero if there's a help-echo in the echo area. */
516 int help_echo_showing_p;
518 /* The maximum distance to look ahead for text properties. Values
519 that are too small let us call compute_char_face and similar
520 functions too often which is expensive. Values that are too large
521 let us call compute_char_face and alike too often because we
522 might not be interested in text properties that far away. */
524 #define TEXT_PROP_DISTANCE_LIMIT 100
526 #if GLYPH_DEBUG
528 /* Non-zero means print traces of redisplay if compiled with
529 GLYPH_DEBUG != 0. */
531 int trace_redisplay_p;
533 #endif /* GLYPH_DEBUG */
535 #ifdef DEBUG_TRACE_MOVE
536 /* Non-zero means trace with TRACE_MOVE to stderr. */
537 int trace_move;
539 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
540 #else
541 #define TRACE_MOVE(x) (void) 0
542 #endif
544 /* Non-zero means automatically scroll windows horizontally to make
545 point visible. */
547 int automatic_hscrolling_p;
549 /* A list of symbols, one for each supported image type. */
551 Lisp_Object Vimage_types;
553 /* Value returned from text property handlers (see below). */
555 enum prop_handled
557 HANDLED_NORMALLY,
558 HANDLED_RECOMPUTE_PROPS,
559 HANDLED_OVERLAY_STRING_CONSUMED,
560 HANDLED_RETURN
563 /* A description of text properties that redisplay is interested
564 in. */
566 struct props
568 /* The name of the property. */
569 Lisp_Object *name;
571 /* A unique index for the property. */
572 enum prop_idx idx;
574 /* A handler function called to set up iterator IT from the property
575 at IT's current position. Value is used to steer handle_stop. */
576 enum prop_handled (*handler) P_ ((struct it *it));
579 static enum prop_handled handle_face_prop P_ ((struct it *));
580 static enum prop_handled handle_invisible_prop P_ ((struct it *));
581 static enum prop_handled handle_display_prop P_ ((struct it *));
582 static enum prop_handled handle_composition_prop P_ ((struct it *));
583 static enum prop_handled handle_overlay_change P_ ((struct it *));
584 static enum prop_handled handle_fontified_prop P_ ((struct it *));
586 /* Properties handled by iterators. */
588 static struct props it_props[] =
590 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
591 /* Handle `face' before `display' because some sub-properties of
592 `display' need to know the face. */
593 {&Qface, FACE_PROP_IDX, handle_face_prop},
594 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
595 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
596 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
597 {NULL, 0, NULL}
600 /* Value is the position described by X. If X is a marker, value is
601 the marker_position of X. Otherwise, value is X. */
603 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
605 /* Enumeration returned by some move_it_.* functions internally. */
607 enum move_it_result
609 /* Not used. Undefined value. */
610 MOVE_UNDEFINED,
612 /* Move ended at the requested buffer position or ZV. */
613 MOVE_POS_MATCH_OR_ZV,
615 /* Move ended at the requested X pixel position. */
616 MOVE_X_REACHED,
618 /* Move within a line ended at the end of a line that must be
619 continued. */
620 MOVE_LINE_CONTINUED,
622 /* Move within a line ended at the end of a line that would
623 be displayed truncated. */
624 MOVE_LINE_TRUNCATED,
626 /* Move within a line ended at a line end. */
627 MOVE_NEWLINE_OR_CR
632 /* Function prototypes. */
634 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
635 static int redisplay_mode_lines P_ ((Lisp_Object, int));
636 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
637 static int invisible_text_between_p P_ ((struct it *, int, int));
638 static int next_element_from_ellipsis P_ ((struct it *));
639 static void pint2str P_ ((char *, int, int));
640 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
641 struct text_pos));
642 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
643 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
644 static void store_frame_title_char P_ ((char));
645 static int store_frame_title P_ ((unsigned char *, int, int));
646 static void x_consider_frame_title P_ ((Lisp_Object));
647 static void handle_stop P_ ((struct it *));
648 static int tool_bar_lines_needed P_ ((struct frame *));
649 static int single_display_prop_intangible_p P_ ((Lisp_Object));
650 static void ensure_echo_area_buffers P_ ((void));
651 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
652 struct glyph_row *,
653 struct glyph_row *));
654 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
655 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
656 static int with_echo_area_buffer P_ ((struct window *, int,
657 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
658 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
659 static void clear_garbaged_frames P_ ((void));
660 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
661 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
662 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
663 static int display_echo_area P_ ((struct window *));
664 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
665 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
666 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
667 static int string_char_and_length P_ ((unsigned char *, int, int *));
668 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
669 struct text_pos));
670 static int compute_window_start_on_continuation_line P_ ((struct window *));
671 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
672 static void insert_left_trunc_glyphs P_ ((struct it *));
673 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
674 static void extend_face_to_end_of_line P_ ((struct it *));
675 static int append_space P_ ((struct it *, int));
676 static void make_cursor_line_fully_visible P_ ((struct window *));
677 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
678 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
679 static int trailing_whitespace_p P_ ((int));
680 static int message_log_check_duplicate P_ ((int, int, int, int));
681 int invisible_p P_ ((Lisp_Object, Lisp_Object));
682 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
683 static void push_it P_ ((struct it *));
684 static void pop_it P_ ((struct it *));
685 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
686 static void redisplay_internal P_ ((int));
687 static int echo_area_display P_ ((int));
688 static void redisplay_windows P_ ((Lisp_Object));
689 static void redisplay_window P_ ((Lisp_Object, int));
690 static void update_menu_bar P_ ((struct frame *, int));
691 static int try_window_reusing_current_matrix P_ ((struct window *));
692 static int try_window_id P_ ((struct window *));
693 static int display_line P_ ((struct it *));
694 static int display_mode_lines P_ ((struct window *));
695 static void display_mode_line P_ ((struct window *, enum face_id,
696 Lisp_Object));
697 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
698 static char *decode_mode_spec P_ ((struct window *, int, int, int));
699 static void display_menu_bar P_ ((struct window *));
700 static int display_count_lines P_ ((int, int, int, int, int *));
701 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
702 int, int, struct it *, int, int, int, int));
703 static void compute_line_metrics P_ ((struct it *));
704 static void run_redisplay_end_trigger_hook P_ ((struct it *));
705 static int get_overlay_strings P_ ((struct it *));
706 static void next_overlay_string P_ ((struct it *));
707 static void reseat P_ ((struct it *, struct text_pos, int));
708 static void reseat_1 P_ ((struct it *, struct text_pos, int));
709 static void back_to_previous_visible_line_start P_ ((struct it *));
710 static void reseat_at_previous_visible_line_start P_ ((struct it *));
711 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
712 static int next_element_from_display_vector P_ ((struct it *));
713 static int next_element_from_string P_ ((struct it *));
714 static int next_element_from_c_string P_ ((struct it *));
715 static int next_element_from_buffer P_ ((struct it *));
716 static int next_element_from_composition P_ ((struct it *));
717 static int next_element_from_image P_ ((struct it *));
718 static int next_element_from_stretch P_ ((struct it *));
719 static void load_overlay_strings P_ ((struct it *));
720 static void init_from_display_pos P_ ((struct it *, struct window *,
721 struct display_pos *));
722 static void reseat_to_string P_ ((struct it *, unsigned char *,
723 Lisp_Object, int, int, int, int));
724 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
725 int, int, int));
726 void move_it_vertically_backward P_ ((struct it *, int));
727 static void init_to_row_start P_ ((struct it *, struct window *,
728 struct glyph_row *));
729 static void init_to_row_end P_ ((struct it *, struct window *,
730 struct glyph_row *));
731 static void back_to_previous_line_start P_ ((struct it *));
732 static int forward_to_next_line_start P_ ((struct it *, int *));
733 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
734 Lisp_Object, int));
735 static struct text_pos string_pos P_ ((int, Lisp_Object));
736 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
737 static int number_of_chars P_ ((unsigned char *, int));
738 static void compute_stop_pos P_ ((struct it *));
739 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
740 Lisp_Object));
741 static int face_before_or_after_it_pos P_ ((struct it *, int));
742 static int next_overlay_change P_ ((int));
743 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
744 Lisp_Object, struct text_pos *));
746 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
747 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
749 #ifdef HAVE_WINDOW_SYSTEM
751 static void update_tool_bar P_ ((struct frame *, int));
752 static void build_desired_tool_bar_string P_ ((struct frame *f));
753 static int redisplay_tool_bar P_ ((struct frame *));
754 static void display_tool_bar_line P_ ((struct it *));
756 #endif /* HAVE_WINDOW_SYSTEM */
759 /***********************************************************************
760 Window display dimensions
761 ***********************************************************************/
763 /* Return the window-relative maximum y + 1 for glyph rows displaying
764 text in window W. This is the height of W minus the height of a
765 mode line, if any. */
767 INLINE int
768 window_text_bottom_y (w)
769 struct window *w;
771 struct frame *f = XFRAME (w->frame);
772 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
774 if (WINDOW_WANTS_MODELINE_P (w))
775 height -= CURRENT_MODE_LINE_HEIGHT (w);
776 return height;
780 /* Return the pixel width of display area AREA of window W. AREA < 0
781 means return the total width of W, not including bitmap areas to
782 the left and right of the window. */
784 INLINE int
785 window_box_width (w, area)
786 struct window *w;
787 int area;
789 struct frame *f = XFRAME (w->frame);
790 int width = XFASTINT (w->width);
792 if (!w->pseudo_window_p)
794 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
796 if (area == TEXT_AREA)
798 if (INTEGERP (w->left_margin_width))
799 width -= XFASTINT (w->left_margin_width);
800 if (INTEGERP (w->right_margin_width))
801 width -= XFASTINT (w->right_margin_width);
803 else if (area == LEFT_MARGIN_AREA)
804 width = (INTEGERP (w->left_margin_width)
805 ? XFASTINT (w->left_margin_width) : 0);
806 else if (area == RIGHT_MARGIN_AREA)
807 width = (INTEGERP (w->right_margin_width)
808 ? XFASTINT (w->right_margin_width) : 0);
811 return width * CANON_X_UNIT (f);
815 /* Return the pixel height of the display area of window W, not
816 including mode lines of W, if any.. */
818 INLINE int
819 window_box_height (w)
820 struct window *w;
822 struct frame *f = XFRAME (w->frame);
823 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
825 xassert (height >= 0);
827 if (WINDOW_WANTS_MODELINE_P (w))
828 height -= CURRENT_MODE_LINE_HEIGHT (w);
830 if (WINDOW_WANTS_HEADER_LINE_P (w))
831 height -= CURRENT_HEADER_LINE_HEIGHT (w);
833 return height;
837 /* Return the frame-relative coordinate of the left edge of display
838 area AREA of window W. AREA < 0 means return the left edge of the
839 whole window, to the right of any bitmap area at the left side of
840 W. */
842 INLINE int
843 window_box_left (w, area)
844 struct window *w;
845 int area;
847 struct frame *f = XFRAME (w->frame);
848 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
850 if (!w->pseudo_window_p)
852 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
853 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
855 if (area == TEXT_AREA)
856 x += window_box_width (w, LEFT_MARGIN_AREA);
857 else if (area == RIGHT_MARGIN_AREA)
858 x += (window_box_width (w, LEFT_MARGIN_AREA)
859 + window_box_width (w, TEXT_AREA));
862 return x;
866 /* Return the frame-relative coordinate of the right edge of display
867 area AREA of window W. AREA < 0 means return the left edge of the
868 whole window, to the left of any bitmap area at the right side of
869 W. */
871 INLINE int
872 window_box_right (w, area)
873 struct window *w;
874 int area;
876 return window_box_left (w, area) + window_box_width (w, area);
880 /* Get the bounding box of the display area AREA of window W, without
881 mode lines, in frame-relative coordinates. AREA < 0 means the
882 whole window, not including bitmap areas to the left and right of
883 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
884 coordinates of the upper-left corner of the box. Return in
885 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
887 INLINE void
888 window_box (w, area, box_x, box_y, box_width, box_height)
889 struct window *w;
890 int area;
891 int *box_x, *box_y, *box_width, *box_height;
893 struct frame *f = XFRAME (w->frame);
895 *box_width = window_box_width (w, area);
896 *box_height = window_box_height (w);
897 *box_x = window_box_left (w, area);
898 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
899 + XFASTINT (w->top) * CANON_Y_UNIT (f));
900 if (WINDOW_WANTS_HEADER_LINE_P (w))
901 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
905 /* Get the bounding box of the display area AREA of window W, without
906 mode lines. AREA < 0 means the whole window, not including bitmap
907 areas to the left and right of the window. Return in *TOP_LEFT_X
908 and TOP_LEFT_Y the frame-relative pixel coordinates of the
909 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
910 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
911 box. */
913 INLINE void
914 window_box_edges (w, area, top_left_x, top_left_y,
915 bottom_right_x, bottom_right_y)
916 struct window *w;
917 int area;
918 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
920 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
921 bottom_right_y);
922 *bottom_right_x += *top_left_x;
923 *bottom_right_y += *top_left_y;
928 /***********************************************************************
929 Utilities
930 ***********************************************************************/
932 /* Return 1 if position CHARPOS is visible in window W.
933 Set *FULLY to 1 if POS is visible and the line containing
934 POS is fully visible. */
937 pos_visible_p (w, charpos, fully)
938 struct window *w;
939 int charpos, *fully;
941 struct it it;
942 struct text_pos top;
943 int visible_p;
944 struct buffer *old_buffer = NULL;
946 if (XBUFFER (w->buffer) != current_buffer)
948 old_buffer = current_buffer;
949 set_buffer_internal_1 (XBUFFER (w->buffer));
952 *fully = visible_p = 0;
953 SET_TEXT_POS_FROM_MARKER (top, w->start);
954 start_display (&it, w, top);
956 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
957 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
959 if (IT_CHARPOS (it) == charpos)
961 int line_height, line_bottom_y;
962 int line_top_y = it.current_y;
963 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
965 line_height = it.max_ascent + it.max_descent;
966 if (line_height == 0)
968 if (last_height)
969 line_height = last_height;
970 else
972 move_it_by_lines (&it, 1, 1);
973 line_height = (it.max_ascent || it.max_descent
974 ? it.max_ascent + it.max_descent
975 : last_height);
978 line_bottom_y = line_top_y + line_height;
980 if (line_top_y < window_top_y)
981 visible_p = line_bottom_y > window_top_y;
982 else if (line_top_y < it.last_visible_y)
984 visible_p = 1;
985 *fully = line_bottom_y <= it.last_visible_y;
988 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
990 move_it_by_lines (&it, 1, 0);
991 if (charpos < IT_CHARPOS (it))
993 visible_p = 1;
994 *fully = 0;
998 if (old_buffer)
999 set_buffer_internal_1 (old_buffer);
1001 return visible_p;
1005 /* Return the next character from STR which is MAXLEN bytes long.
1006 Return in *LEN the length of the character. This is like
1007 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1008 we find one, we return a `?', but with the length of the invalid
1009 character. */
1011 static INLINE int
1012 string_char_and_length (str, maxlen, len)
1013 unsigned char *str;
1014 int maxlen, *len;
1016 int c;
1018 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1019 if (!CHAR_VALID_P (c, 1))
1020 /* We may not change the length here because other places in Emacs
1021 don't use this function, i.e. they silently accept invalid
1022 characters. */
1023 c = '?';
1025 return c;
1030 /* Given a position POS containing a valid character and byte position
1031 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1033 static struct text_pos
1034 string_pos_nchars_ahead (pos, string, nchars)
1035 struct text_pos pos;
1036 Lisp_Object string;
1037 int nchars;
1039 xassert (STRINGP (string) && nchars >= 0);
1041 if (STRING_MULTIBYTE (string))
1043 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1044 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1045 int len;
1047 while (nchars--)
1049 string_char_and_length (p, rest, &len);
1050 p += len, rest -= len;
1051 xassert (rest >= 0);
1052 CHARPOS (pos) += 1;
1053 BYTEPOS (pos) += len;
1056 else
1057 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1059 return pos;
1063 /* Value is the text position, i.e. character and byte position,
1064 for character position CHARPOS in STRING. */
1066 static INLINE struct text_pos
1067 string_pos (charpos, string)
1068 int charpos;
1069 Lisp_Object string;
1071 struct text_pos pos;
1072 xassert (STRINGP (string));
1073 xassert (charpos >= 0);
1074 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1075 return pos;
1079 /* Value is a text position, i.e. character and byte position, for
1080 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1081 means recognize multibyte characters. */
1083 static struct text_pos
1084 c_string_pos (charpos, s, multibyte_p)
1085 int charpos;
1086 unsigned char *s;
1087 int multibyte_p;
1089 struct text_pos pos;
1091 xassert (s != NULL);
1092 xassert (charpos >= 0);
1094 if (multibyte_p)
1096 int rest = strlen (s), len;
1098 SET_TEXT_POS (pos, 0, 0);
1099 while (charpos--)
1101 string_char_and_length (s, rest, &len);
1102 s += len, rest -= len;
1103 xassert (rest >= 0);
1104 CHARPOS (pos) += 1;
1105 BYTEPOS (pos) += len;
1108 else
1109 SET_TEXT_POS (pos, charpos, charpos);
1111 return pos;
1115 /* Value is the number of characters in C string S. MULTIBYTE_P
1116 non-zero means recognize multibyte characters. */
1118 static int
1119 number_of_chars (s, multibyte_p)
1120 unsigned char *s;
1121 int multibyte_p;
1123 int nchars;
1125 if (multibyte_p)
1127 int rest = strlen (s), len;
1128 unsigned char *p = (unsigned char *) s;
1130 for (nchars = 0; rest > 0; ++nchars)
1132 string_char_and_length (p, rest, &len);
1133 rest -= len, p += len;
1136 else
1137 nchars = strlen (s);
1139 return nchars;
1143 /* Compute byte position NEWPOS->bytepos corresponding to
1144 NEWPOS->charpos. POS is a known position in string STRING.
1145 NEWPOS->charpos must be >= POS.charpos. */
1147 static void
1148 compute_string_pos (newpos, pos, string)
1149 struct text_pos *newpos, pos;
1150 Lisp_Object string;
1152 xassert (STRINGP (string));
1153 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1155 if (STRING_MULTIBYTE (string))
1156 *newpos = string_pos_nchars_ahead (pos, string,
1157 CHARPOS (*newpos) - CHARPOS (pos));
1158 else
1159 BYTEPOS (*newpos) = CHARPOS (*newpos);
1164 /***********************************************************************
1165 Lisp form evaluation
1166 ***********************************************************************/
1168 /* Error handler for safe_eval and safe_call. */
1170 static Lisp_Object
1171 safe_eval_handler (arg)
1172 Lisp_Object arg;
1174 add_to_log ("Error during redisplay: %s", arg, Qnil);
1175 return Qnil;
1179 /* Evaluate SEXPR and return the result, or nil if something went
1180 wrong. */
1182 Lisp_Object
1183 safe_eval (sexpr)
1184 Lisp_Object sexpr;
1186 int count = specpdl_ptr - specpdl;
1187 struct gcpro gcpro1;
1188 Lisp_Object val;
1190 GCPRO1 (sexpr);
1191 specbind (Qinhibit_redisplay, Qt);
1192 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1193 UNGCPRO;
1194 return unbind_to (count, val);
1198 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1199 Return the result, or nil if something went wrong. */
1201 Lisp_Object
1202 safe_call (nargs, args)
1203 int nargs;
1204 Lisp_Object *args;
1206 int count = specpdl_ptr - specpdl;
1207 Lisp_Object val;
1208 struct gcpro gcpro1;
1210 GCPRO1 (args[0]);
1211 gcpro1.nvars = nargs;
1212 specbind (Qinhibit_redisplay, Qt);
1213 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1214 safe_eval_handler);
1215 UNGCPRO;
1216 return unbind_to (count, val);
1220 /* Call function FN with one argument ARG.
1221 Return the result, or nil if something went wrong. */
1223 Lisp_Object
1224 safe_call1 (fn, arg)
1225 Lisp_Object fn, arg;
1227 Lisp_Object args[2];
1228 args[0] = fn;
1229 args[1] = arg;
1230 return safe_call (2, args);
1235 /***********************************************************************
1236 Debugging
1237 ***********************************************************************/
1239 #if 0
1241 /* Define CHECK_IT to perform sanity checks on iterators.
1242 This is for debugging. It is too slow to do unconditionally. */
1244 static void
1245 check_it (it)
1246 struct it *it;
1248 if (it->method == next_element_from_string)
1250 xassert (STRINGP (it->string));
1251 xassert (IT_STRING_CHARPOS (*it) >= 0);
1253 else if (it->method == next_element_from_buffer)
1255 /* Check that character and byte positions agree. */
1256 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1259 if (it->dpvec)
1260 xassert (it->current.dpvec_index >= 0);
1261 else
1262 xassert (it->current.dpvec_index < 0);
1265 #define CHECK_IT(IT) check_it ((IT))
1267 #else /* not 0 */
1269 #define CHECK_IT(IT) (void) 0
1271 #endif /* not 0 */
1274 #if GLYPH_DEBUG
1276 /* Check that the window end of window W is what we expect it
1277 to be---the last row in the current matrix displaying text. */
1279 static void
1280 check_window_end (w)
1281 struct window *w;
1283 if (!MINI_WINDOW_P (w)
1284 && !NILP (w->window_end_valid))
1286 struct glyph_row *row;
1287 xassert ((row = MATRIX_ROW (w->current_matrix,
1288 XFASTINT (w->window_end_vpos)),
1289 !row->enabled_p
1290 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1291 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1295 #define CHECK_WINDOW_END(W) check_window_end ((W))
1297 #else /* not GLYPH_DEBUG */
1299 #define CHECK_WINDOW_END(W) (void) 0
1301 #endif /* not GLYPH_DEBUG */
1305 /***********************************************************************
1306 Iterator initialization
1307 ***********************************************************************/
1309 /* Initialize IT for displaying current_buffer in window W, starting
1310 at character position CHARPOS. CHARPOS < 0 means that no buffer
1311 position is specified which is useful when the iterator is assigned
1312 a position later. BYTEPOS is the byte position corresponding to
1313 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1315 If ROW is not null, calls to produce_glyphs with IT as parameter
1316 will produce glyphs in that row.
1318 BASE_FACE_ID is the id of a base face to use. It must be one of
1319 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1320 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1321 displaying the tool-bar.
1323 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1324 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1325 corresponding mode line glyph row of the desired matrix of W. */
1327 void
1328 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1329 struct it *it;
1330 struct window *w;
1331 int charpos, bytepos;
1332 struct glyph_row *row;
1333 enum face_id base_face_id;
1335 int highlight_region_p;
1337 /* Some precondition checks. */
1338 xassert (w != NULL && it != NULL);
1339 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1341 /* If face attributes have been changed since the last redisplay,
1342 free realized faces now because they depend on face definitions
1343 that might have changed. */
1344 if (face_change_count)
1346 face_change_count = 0;
1347 free_all_realized_faces (Qnil);
1350 /* Use one of the mode line rows of W's desired matrix if
1351 appropriate. */
1352 if (row == NULL)
1354 if (base_face_id == MODE_LINE_FACE_ID)
1355 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1356 else if (base_face_id == HEADER_LINE_FACE_ID)
1357 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1360 /* Clear IT. */
1361 bzero (it, sizeof *it);
1362 it->current.overlay_string_index = -1;
1363 it->current.dpvec_index = -1;
1364 it->base_face_id = base_face_id;
1366 /* The window in which we iterate over current_buffer: */
1367 XSETWINDOW (it->window, w);
1368 it->w = w;
1369 it->f = XFRAME (w->frame);
1371 /* Extra space between lines (on window systems only). */
1372 if (base_face_id == DEFAULT_FACE_ID
1373 && FRAME_WINDOW_P (it->f))
1375 if (NATNUMP (current_buffer->extra_line_spacing))
1376 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1377 else if (it->f->extra_line_spacing > 0)
1378 it->extra_line_spacing = it->f->extra_line_spacing;
1381 /* If realized faces have been removed, e.g. because of face
1382 attribute changes of named faces, recompute them. */
1383 if (FRAME_FACE_CACHE (it->f)->used == 0)
1384 recompute_basic_faces (it->f);
1386 /* Current value of the `space-width', and 'height' properties. */
1387 it->space_width = Qnil;
1388 it->font_height = Qnil;
1390 /* Are control characters displayed as `^C'? */
1391 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1393 /* -1 means everything between a CR and the following line end
1394 is invisible. >0 means lines indented more than this value are
1395 invisible. */
1396 it->selective = (INTEGERP (current_buffer->selective_display)
1397 ? XFASTINT (current_buffer->selective_display)
1398 : (!NILP (current_buffer->selective_display)
1399 ? -1 : 0));
1400 it->selective_display_ellipsis_p
1401 = !NILP (current_buffer->selective_display_ellipses);
1403 /* Display table to use. */
1404 it->dp = window_display_table (w);
1406 /* Are multibyte characters enabled in current_buffer? */
1407 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1409 /* Non-zero if we should highlight the region. */
1410 highlight_region_p
1411 = (!NILP (Vtransient_mark_mode)
1412 && !NILP (current_buffer->mark_active)
1413 && XMARKER (current_buffer->mark)->buffer != 0);
1415 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1416 start and end of a visible region in window IT->w. Set both to
1417 -1 to indicate no region. */
1418 if (highlight_region_p
1419 /* Maybe highlight only in selected window. */
1420 && (/* Either show region everywhere. */
1421 highlight_nonselected_windows
1422 /* Or show region in the selected window. */
1423 || w == XWINDOW (selected_window)
1424 /* Or show the region if we are in the mini-buffer and W is
1425 the window the mini-buffer refers to. */
1426 || (MINI_WINDOW_P (XWINDOW (selected_window))
1427 && w == XWINDOW (Vminibuf_scroll_window))))
1429 int charpos = marker_position (current_buffer->mark);
1430 it->region_beg_charpos = min (PT, charpos);
1431 it->region_end_charpos = max (PT, charpos);
1433 else
1434 it->region_beg_charpos = it->region_end_charpos = -1;
1436 /* Get the position at which the redisplay_end_trigger hook should
1437 be run, if it is to be run at all. */
1438 if (MARKERP (w->redisplay_end_trigger)
1439 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1440 it->redisplay_end_trigger_charpos
1441 = marker_position (w->redisplay_end_trigger);
1442 else if (INTEGERP (w->redisplay_end_trigger))
1443 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1445 /* Correct bogus values of tab_width. */
1446 it->tab_width = XINT (current_buffer->tab_width);
1447 if (it->tab_width <= 0 || it->tab_width > 1000)
1448 it->tab_width = 8;
1450 /* Are lines in the display truncated? */
1451 it->truncate_lines_p
1452 = (base_face_id != DEFAULT_FACE_ID
1453 || XINT (it->w->hscroll)
1454 || (truncate_partial_width_windows
1455 && !WINDOW_FULL_WIDTH_P (it->w))
1456 || !NILP (current_buffer->truncate_lines));
1458 /* Get dimensions of truncation and continuation glyphs. These are
1459 displayed as bitmaps under X, so we don't need them for such
1460 frames. */
1461 if (!FRAME_WINDOW_P (it->f))
1463 if (it->truncate_lines_p)
1465 /* We will need the truncation glyph. */
1466 xassert (it->glyph_row == NULL);
1467 produce_special_glyphs (it, IT_TRUNCATION);
1468 it->truncation_pixel_width = it->pixel_width;
1470 else
1472 /* We will need the continuation glyph. */
1473 xassert (it->glyph_row == NULL);
1474 produce_special_glyphs (it, IT_CONTINUATION);
1475 it->continuation_pixel_width = it->pixel_width;
1478 /* Reset these values to zero becaue the produce_special_glyphs
1479 above has changed them. */
1480 it->pixel_width = it->ascent = it->descent = 0;
1481 it->phys_ascent = it->phys_descent = 0;
1484 /* Set this after getting the dimensions of truncation and
1485 continuation glyphs, so that we don't produce glyphs when calling
1486 produce_special_glyphs, above. */
1487 it->glyph_row = row;
1488 it->area = TEXT_AREA;
1490 /* Get the dimensions of the display area. The display area
1491 consists of the visible window area plus a horizontally scrolled
1492 part to the left of the window. All x-values are relative to the
1493 start of this total display area. */
1494 if (base_face_id != DEFAULT_FACE_ID)
1496 /* Mode lines, menu bar in terminal frames. */
1497 it->first_visible_x = 0;
1498 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1500 else
1502 it->first_visible_x
1503 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1504 it->last_visible_x = (it->first_visible_x
1505 + window_box_width (w, TEXT_AREA));
1507 /* If we truncate lines, leave room for the truncator glyph(s) at
1508 the right margin. Otherwise, leave room for the continuation
1509 glyph(s). Truncation and continuation glyphs are not inserted
1510 for window-based redisplay. */
1511 if (!FRAME_WINDOW_P (it->f))
1513 if (it->truncate_lines_p)
1514 it->last_visible_x -= it->truncation_pixel_width;
1515 else
1516 it->last_visible_x -= it->continuation_pixel_width;
1519 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1520 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1523 /* Leave room for a border glyph. */
1524 if (!FRAME_WINDOW_P (it->f)
1525 && !WINDOW_RIGHTMOST_P (it->w))
1526 it->last_visible_x -= 1;
1528 it->last_visible_y = window_text_bottom_y (w);
1530 /* For mode lines and alike, arrange for the first glyph having a
1531 left box line if the face specifies a box. */
1532 if (base_face_id != DEFAULT_FACE_ID)
1534 struct face *face;
1536 it->face_id = base_face_id;
1538 /* If we have a boxed mode line, make the first character appear
1539 with a left box line. */
1540 face = FACE_FROM_ID (it->f, base_face_id);
1541 if (face->box != FACE_NO_BOX)
1542 it->start_of_box_run_p = 1;
1545 /* If a buffer position was specified, set the iterator there,
1546 getting overlays and face properties from that position. */
1547 if (charpos > 0)
1549 it->end_charpos = ZV;
1550 it->face_id = -1;
1551 IT_CHARPOS (*it) = charpos;
1553 /* Compute byte position if not specified. */
1554 if (bytepos <= 0)
1555 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1556 else
1557 IT_BYTEPOS (*it) = bytepos;
1559 /* Compute faces etc. */
1560 reseat (it, it->current.pos, 1);
1563 CHECK_IT (it);
1567 /* Initialize IT for the display of window W with window start POS. */
1569 void
1570 start_display (it, w, pos)
1571 struct it *it;
1572 struct window *w;
1573 struct text_pos pos;
1575 int start_at_line_beg_p;
1576 struct glyph_row *row;
1577 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1578 int first_y;
1580 row = w->desired_matrix->rows + first_vpos;
1581 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1582 first_y = it->current_y;
1584 /* If window start is not at a line start, move back to the line
1585 start. This makes sure that we take continuation lines into
1586 account. */
1587 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1588 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1589 if (!start_at_line_beg_p)
1590 reseat_at_previous_visible_line_start (it);
1592 /* If window start is not at a line start, skip forward to POS to
1593 get the correct continuation_lines_width and current_x. */
1594 if (!start_at_line_beg_p)
1596 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1598 /* If lines are continued, this line may end in the middle of a
1599 multi-glyph character (e.g. a control character displayed as
1600 \003, or in the middle of an overlay string). In this case
1601 move_it_to above will not have taken us to the start of
1602 the continuation line but to the end of the continued line. */
1603 if (!it->truncate_lines_p)
1605 if (it->current_x > 0)
1607 if (it->current.dpvec_index >= 0
1608 || it->current.overlay_string_index >= 0)
1610 set_iterator_to_next (it, 1);
1611 move_it_in_display_line_to (it, -1, -1, 0);
1614 it->continuation_lines_width += it->current_x;
1617 /* We're starting a new display line, not affected by the
1618 height of the continued line, so clear the appropriate
1619 fields in the iterator structure. */
1620 it->max_ascent = it->max_descent = 0;
1621 it->max_phys_ascent = it->max_phys_descent = 0;
1624 it->current_y = first_y;
1625 it->vpos = 0;
1626 it->current_x = it->hpos = 0;
1629 #if 0 /* Don't assert the following because start_display is sometimes
1630 called intentionally with a window start that is not at a
1631 line start. Please leave this code in as a comment. */
1633 /* Window start should be on a line start, now. */
1634 xassert (it->continuation_lines_width
1635 || IT_CHARPOS (it) == BEGV
1636 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1637 #endif /* 0 */
1641 /* Initialize IT for stepping through current_buffer in window W,
1642 starting at position POS that includes overlay string and display
1643 vector/ control character translation position information. */
1645 static void
1646 init_from_display_pos (it, w, pos)
1647 struct it *it;
1648 struct window *w;
1649 struct display_pos *pos;
1651 /* Keep in mind: the call to reseat in init_iterator skips invisible
1652 text, so we might end up at a position different from POS. This
1653 is only a problem when POS is a row start after a newline and an
1654 overlay starts there with an after-string, and the overlay has an
1655 invisible property. Since we don't skip invisible text in
1656 display_line and elsewhere immediately after consuming the
1657 newline before the row start, such a POS will not be in a string,
1658 but the call to init_iterator below will move us to the
1659 after-string. */
1660 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1661 NULL, DEFAULT_FACE_ID);
1663 /* If position is within an overlay string, set up IT to
1664 the right overlay string. */
1665 if (pos->overlay_string_index >= 0)
1667 int relative_index;
1669 /* We already have the first chunk of overlay strings in
1670 IT->overlay_strings. Load more until the one for
1671 pos->overlay_string_index is in IT->overlay_strings. */
1672 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1674 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1675 it->current.overlay_string_index = 0;
1676 while (n--)
1678 load_overlay_strings (it);
1679 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1683 it->current.overlay_string_index = pos->overlay_string_index;
1684 relative_index = (it->current.overlay_string_index
1685 % OVERLAY_STRING_CHUNK_SIZE);
1686 it->string = it->overlay_strings[relative_index];
1687 xassert (STRINGP (it->string));
1688 it->current.string_pos = pos->string_pos;
1689 it->method = next_element_from_string;
1691 else if (CHARPOS (pos->string_pos) >= 0)
1693 /* Recorded position is not in an overlay string, but in another
1694 string. This can only be a string from a `display' property.
1695 IT should already be filled with that string. */
1696 it->current.string_pos = pos->string_pos;
1697 xassert (STRINGP (it->string));
1700 /* Restore position in display vector translations or control
1701 character translations. */
1702 if (pos->dpvec_index >= 0)
1704 /* This fills IT->dpvec. */
1705 get_next_display_element (it);
1706 xassert (it->dpvec && it->current.dpvec_index == 0);
1707 it->current.dpvec_index = pos->dpvec_index;
1710 CHECK_IT (it);
1714 /* Initialize IT for stepping through current_buffer in window W
1715 starting at ROW->start. */
1717 static void
1718 init_to_row_start (it, w, row)
1719 struct it *it;
1720 struct window *w;
1721 struct glyph_row *row;
1723 init_from_display_pos (it, w, &row->start);
1724 it->continuation_lines_width = row->continuation_lines_width;
1725 CHECK_IT (it);
1729 /* Initialize IT for stepping through current_buffer in window W
1730 starting in the line following ROW, i.e. starting at ROW->end. */
1732 static void
1733 init_to_row_end (it, w, row)
1734 struct it *it;
1735 struct window *w;
1736 struct glyph_row *row;
1738 init_from_display_pos (it, w, &row->end);
1740 if (row->continued_p)
1741 it->continuation_lines_width = (row->continuation_lines_width
1742 + row->pixel_width);
1743 CHECK_IT (it);
1749 /***********************************************************************
1750 Text properties
1751 ***********************************************************************/
1753 /* Called when IT reaches IT->stop_charpos. Handle text property and
1754 overlay changes. Set IT->stop_charpos to the next position where
1755 to stop. */
1757 static void
1758 handle_stop (it)
1759 struct it *it;
1761 enum prop_handled handled;
1762 int handle_overlay_change_p = 1;
1763 struct props *p;
1765 it->dpvec = NULL;
1766 it->current.dpvec_index = -1;
1770 handled = HANDLED_NORMALLY;
1772 /* Call text property handlers. */
1773 for (p = it_props; p->handler; ++p)
1775 handled = p->handler (it);
1777 if (handled == HANDLED_RECOMPUTE_PROPS)
1778 break;
1779 else if (handled == HANDLED_RETURN)
1780 return;
1781 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1782 handle_overlay_change_p = 0;
1785 if (handled != HANDLED_RECOMPUTE_PROPS)
1787 /* Don't check for overlay strings below when set to deliver
1788 characters from a display vector. */
1789 if (it->method == next_element_from_display_vector)
1790 handle_overlay_change_p = 0;
1792 /* Handle overlay changes. */
1793 if (handle_overlay_change_p)
1794 handled = handle_overlay_change (it);
1796 /* Determine where to stop next. */
1797 if (handled == HANDLED_NORMALLY)
1798 compute_stop_pos (it);
1801 while (handled == HANDLED_RECOMPUTE_PROPS);
1805 /* Compute IT->stop_charpos from text property and overlay change
1806 information for IT's current position. */
1808 static void
1809 compute_stop_pos (it)
1810 struct it *it;
1812 register INTERVAL iv, next_iv;
1813 Lisp_Object object, limit, position;
1815 /* If nowhere else, stop at the end. */
1816 it->stop_charpos = it->end_charpos;
1818 if (STRINGP (it->string))
1820 /* Strings are usually short, so don't limit the search for
1821 properties. */
1822 object = it->string;
1823 limit = Qnil;
1824 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1826 else
1828 int charpos;
1830 /* If next overlay change is in front of the current stop pos
1831 (which is IT->end_charpos), stop there. Note: value of
1832 next_overlay_change is point-max if no overlay change
1833 follows. */
1834 charpos = next_overlay_change (IT_CHARPOS (*it));
1835 if (charpos < it->stop_charpos)
1836 it->stop_charpos = charpos;
1838 /* If showing the region, we have to stop at the region
1839 start or end because the face might change there. */
1840 if (it->region_beg_charpos > 0)
1842 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1843 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1844 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1845 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1848 /* Set up variables for computing the stop position from text
1849 property changes. */
1850 XSETBUFFER (object, current_buffer);
1851 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1852 XSETFASTINT (position, IT_CHARPOS (*it));
1856 /* Get the interval containing IT's position. Value is a null
1857 interval if there isn't such an interval. */
1858 iv = validate_interval_range (object, &position, &position, 0);
1859 if (!NULL_INTERVAL_P (iv))
1861 Lisp_Object values_here[LAST_PROP_IDX];
1862 struct props *p;
1864 /* Get properties here. */
1865 for (p = it_props; p->handler; ++p)
1866 values_here[p->idx] = textget (iv->plist, *p->name);
1868 /* Look for an interval following iv that has different
1869 properties. */
1870 for (next_iv = next_interval (iv);
1871 (!NULL_INTERVAL_P (next_iv)
1872 && (NILP (limit)
1873 || XFASTINT (limit) > next_iv->position));
1874 next_iv = next_interval (next_iv))
1876 for (p = it_props; p->handler; ++p)
1878 Lisp_Object new_value;
1880 new_value = textget (next_iv->plist, *p->name);
1881 if (!EQ (values_here[p->idx], new_value))
1882 break;
1885 if (p->handler)
1886 break;
1889 if (!NULL_INTERVAL_P (next_iv))
1891 if (INTEGERP (limit)
1892 && next_iv->position >= XFASTINT (limit))
1893 /* No text property change up to limit. */
1894 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1895 else
1896 /* Text properties change in next_iv. */
1897 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1901 xassert (STRINGP (it->string)
1902 || (it->stop_charpos >= BEGV
1903 && it->stop_charpos >= IT_CHARPOS (*it)));
1907 /* Return the position of the next overlay change after POS in
1908 current_buffer. Value is point-max if no overlay change
1909 follows. This is like `next-overlay-change' but doesn't use
1910 xmalloc. */
1912 static int
1913 next_overlay_change (pos)
1914 int pos;
1916 int noverlays;
1917 int endpos;
1918 Lisp_Object *overlays;
1919 int len;
1920 int i;
1922 /* Get all overlays at the given position. */
1923 len = 10;
1924 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1925 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1926 if (noverlays > len)
1928 len = noverlays;
1929 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1930 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1933 /* If any of these overlays ends before endpos,
1934 use its ending point instead. */
1935 for (i = 0; i < noverlays; ++i)
1937 Lisp_Object oend;
1938 int oendpos;
1940 oend = OVERLAY_END (overlays[i]);
1941 oendpos = OVERLAY_POSITION (oend);
1942 endpos = min (endpos, oendpos);
1945 return endpos;
1950 /***********************************************************************
1951 Fontification
1952 ***********************************************************************/
1954 /* Handle changes in the `fontified' property of the current buffer by
1955 calling hook functions from Qfontification_functions to fontify
1956 regions of text. */
1958 static enum prop_handled
1959 handle_fontified_prop (it)
1960 struct it *it;
1962 Lisp_Object prop, pos;
1963 enum prop_handled handled = HANDLED_NORMALLY;
1965 /* Get the value of the `fontified' property at IT's current buffer
1966 position. (The `fontified' property doesn't have a special
1967 meaning in strings.) If the value is nil, call functions from
1968 Qfontification_functions. */
1969 if (!STRINGP (it->string)
1970 && it->s == NULL
1971 && !NILP (Vfontification_functions)
1972 && !NILP (Vrun_hooks)
1973 && (pos = make_number (IT_CHARPOS (*it)),
1974 prop = Fget_char_property (pos, Qfontified, Qnil),
1975 NILP (prop)))
1977 int count = specpdl_ptr - specpdl;
1978 Lisp_Object val;
1980 val = Vfontification_functions;
1981 specbind (Qfontification_functions, Qnil);
1982 specbind (Qafter_change_functions, Qnil);
1984 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
1985 safe_call1 (val, pos);
1986 else
1988 Lisp_Object globals, fn;
1989 struct gcpro gcpro1, gcpro2;
1991 globals = Qnil;
1992 GCPRO2 (val, globals);
1994 for (; CONSP (val); val = XCDR (val))
1996 fn = XCAR (val);
1998 if (EQ (fn, Qt))
2000 /* A value of t indicates this hook has a local
2001 binding; it means to run the global binding too.
2002 In a global value, t should not occur. If it
2003 does, we must ignore it to avoid an endless
2004 loop. */
2005 for (globals = Fdefault_value (Qfontification_functions);
2006 CONSP (globals);
2007 globals = XCDR (globals))
2009 fn = XCAR (globals);
2010 if (!EQ (fn, Qt))
2011 safe_call1 (fn, pos);
2014 else
2015 safe_call1 (fn, pos);
2018 UNGCPRO;
2021 unbind_to (count, Qnil);
2023 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2024 something. This avoids an endless loop if they failed to
2025 fontify the text for which reason ever. */
2026 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2027 handled = HANDLED_RECOMPUTE_PROPS;
2030 return handled;
2035 /***********************************************************************
2036 Faces
2037 ***********************************************************************/
2039 /* Set up iterator IT from face properties at its current position.
2040 Called from handle_stop. */
2042 static enum prop_handled
2043 handle_face_prop (it)
2044 struct it *it;
2046 int new_face_id, next_stop;
2048 if (!STRINGP (it->string))
2050 new_face_id
2051 = face_at_buffer_position (it->w,
2052 IT_CHARPOS (*it),
2053 it->region_beg_charpos,
2054 it->region_end_charpos,
2055 &next_stop,
2056 (IT_CHARPOS (*it)
2057 + TEXT_PROP_DISTANCE_LIMIT),
2060 /* Is this a start of a run of characters with box face?
2061 Caveat: this can be called for a freshly initialized
2062 iterator; face_id is -1 is this case. We know that the new
2063 face will not change until limit, i.e. if the new face has a
2064 box, all characters up to limit will have one. But, as
2065 usual, we don't know whether limit is really the end. */
2066 if (new_face_id != it->face_id)
2068 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2070 /* If new face has a box but old face has not, this is
2071 the start of a run of characters with box, i.e. it has
2072 a shadow on the left side. The value of face_id of the
2073 iterator will be -1 if this is the initial call that gets
2074 the face. In this case, we have to look in front of IT's
2075 position and see whether there is a face != new_face_id. */
2076 it->start_of_box_run_p
2077 = (new_face->box != FACE_NO_BOX
2078 && (it->face_id >= 0
2079 || IT_CHARPOS (*it) == BEG
2080 || new_face_id != face_before_it_pos (it)));
2081 it->face_box_p = new_face->box != FACE_NO_BOX;
2084 else
2086 new_face_id
2087 = face_at_string_position (it->w,
2088 it->string,
2089 IT_STRING_CHARPOS (*it),
2090 (it->current.overlay_string_index >= 0
2091 ? IT_CHARPOS (*it)
2092 : 0),
2093 it->region_beg_charpos,
2094 it->region_end_charpos,
2095 &next_stop,
2096 it->base_face_id);
2098 #if 0 /* This shouldn't be neccessary. Let's check it. */
2099 /* If IT is used to display a mode line we would really like to
2100 use the mode line face instead of the frame's default face. */
2101 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2102 && new_face_id == DEFAULT_FACE_ID)
2103 new_face_id = MODE_LINE_FACE_ID;
2104 #endif
2106 /* Is this a start of a run of characters with box? Caveat:
2107 this can be called for a freshly allocated iterator; face_id
2108 is -1 is this case. We know that the new face will not
2109 change until the next check pos, i.e. if the new face has a
2110 box, all characters up to that position will have a
2111 box. But, as usual, we don't know whether that position
2112 is really the end. */
2113 if (new_face_id != it->face_id)
2115 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2116 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2118 /* If new face has a box but old face hasn't, this is the
2119 start of a run of characters with box, i.e. it has a
2120 shadow on the left side. */
2121 it->start_of_box_run_p
2122 = new_face->box && (old_face == NULL || !old_face->box);
2123 it->face_box_p = new_face->box != FACE_NO_BOX;
2127 it->face_id = new_face_id;
2128 return HANDLED_NORMALLY;
2132 /* Compute the face one character before or after the current position
2133 of IT. BEFORE_P non-zero means get the face in front of IT's
2134 position. Value is the id of the face. */
2136 static int
2137 face_before_or_after_it_pos (it, before_p)
2138 struct it *it;
2139 int before_p;
2141 int face_id, limit;
2142 int next_check_charpos;
2143 struct text_pos pos;
2145 xassert (it->s == NULL);
2147 if (STRINGP (it->string))
2149 /* No face change past the end of the string (for the case
2150 we are padding with spaces). No face change before the
2151 string start. */
2152 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2153 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2154 return it->face_id;
2156 /* Set pos to the position before or after IT's current position. */
2157 if (before_p)
2158 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2159 else
2160 /* For composition, we must check the character after the
2161 composition. */
2162 pos = (it->what == IT_COMPOSITION
2163 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2164 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2166 /* Get the face for ASCII, or unibyte. */
2167 face_id
2168 = face_at_string_position (it->w,
2169 it->string,
2170 CHARPOS (pos),
2171 (it->current.overlay_string_index >= 0
2172 ? IT_CHARPOS (*it)
2173 : 0),
2174 it->region_beg_charpos,
2175 it->region_end_charpos,
2176 &next_check_charpos,
2177 it->base_face_id);
2179 /* Correct the face for charsets different from ASCII. Do it
2180 for the multibyte case only. The face returned above is
2181 suitable for unibyte text if IT->string is unibyte. */
2182 if (STRING_MULTIBYTE (it->string))
2184 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2185 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2186 int c, len;
2187 struct face *face = FACE_FROM_ID (it->f, face_id);
2189 c = string_char_and_length (p, rest, &len);
2190 face_id = FACE_FOR_CHAR (it->f, face, c);
2193 else
2195 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2196 || (IT_CHARPOS (*it) <= BEGV && before_p))
2197 return it->face_id;
2199 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2200 pos = it->current.pos;
2202 if (before_p)
2203 DEC_TEXT_POS (pos, it->multibyte_p);
2204 else
2206 if (it->what == IT_COMPOSITION)
2207 /* For composition, we must check the position after the
2208 composition. */
2209 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2210 else
2211 INC_TEXT_POS (pos, it->multibyte_p);
2213 /* Determine face for CHARSET_ASCII, or unibyte. */
2214 face_id = face_at_buffer_position (it->w,
2215 CHARPOS (pos),
2216 it->region_beg_charpos,
2217 it->region_end_charpos,
2218 &next_check_charpos,
2219 limit, 0);
2221 /* Correct the face for charsets different from ASCII. Do it
2222 for the multibyte case only. The face returned above is
2223 suitable for unibyte text if current_buffer is unibyte. */
2224 if (it->multibyte_p)
2226 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2227 struct face *face = FACE_FROM_ID (it->f, face_id);
2228 face_id = FACE_FOR_CHAR (it->f, face, c);
2232 return face_id;
2237 /***********************************************************************
2238 Invisible text
2239 ***********************************************************************/
2241 /* Set up iterator IT from invisible properties at its current
2242 position. Called from handle_stop. */
2244 static enum prop_handled
2245 handle_invisible_prop (it)
2246 struct it *it;
2248 enum prop_handled handled = HANDLED_NORMALLY;
2250 if (STRINGP (it->string))
2252 extern Lisp_Object Qinvisible;
2253 Lisp_Object prop, end_charpos, limit, charpos;
2255 /* Get the value of the invisible text property at the
2256 current position. Value will be nil if there is no such
2257 property. */
2258 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2259 prop = Fget_text_property (charpos, Qinvisible, it->string);
2261 if (!NILP (prop)
2262 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2264 handled = HANDLED_RECOMPUTE_PROPS;
2266 /* Get the position at which the next change of the
2267 invisible text property can be found in IT->string.
2268 Value will be nil if the property value is the same for
2269 all the rest of IT->string. */
2270 XSETINT (limit, XSTRING (it->string)->size);
2271 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2272 it->string, limit);
2274 /* Text at current position is invisible. The next
2275 change in the property is at position end_charpos.
2276 Move IT's current position to that position. */
2277 if (INTEGERP (end_charpos)
2278 && XFASTINT (end_charpos) < XFASTINT (limit))
2280 struct text_pos old;
2281 old = it->current.string_pos;
2282 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2283 compute_string_pos (&it->current.string_pos, old, it->string);
2285 else
2287 /* The rest of the string is invisible. If this is an
2288 overlay string, proceed with the next overlay string
2289 or whatever comes and return a character from there. */
2290 if (it->current.overlay_string_index >= 0)
2292 next_overlay_string (it);
2293 /* Don't check for overlay strings when we just
2294 finished processing them. */
2295 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2297 else
2299 struct Lisp_String *s = XSTRING (it->string);
2300 IT_STRING_CHARPOS (*it) = s->size;
2301 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2306 else
2308 int visible_p, newpos, next_stop;
2309 Lisp_Object pos, prop;
2311 /* First of all, is there invisible text at this position? */
2312 XSETFASTINT (pos, IT_CHARPOS (*it));
2313 prop = Fget_char_property (pos, Qinvisible, it->window);
2315 /* If we are on invisible text, skip over it. */
2316 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2317 && IT_CHARPOS (*it) < it->end_charpos)
2319 /* Record whether we have to display an ellipsis for the
2320 invisible text. */
2321 int display_ellipsis_p
2322 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2324 handled = HANDLED_RECOMPUTE_PROPS;
2326 /* Loop skipping over invisible text. The loop is left at
2327 ZV or with IT on the first char being visible again. */
2330 /* Try to skip some invisible text. Return value is the
2331 position reached which can be equal to IT's position
2332 if there is nothing invisible here. This skips both
2333 over invisible text properties and overlays with
2334 invisible property. */
2335 newpos = skip_invisible (IT_CHARPOS (*it),
2336 &next_stop, ZV, it->window);
2338 /* If we skipped nothing at all we weren't at invisible
2339 text in the first place. If everything to the end of
2340 the buffer was skipped, end the loop. */
2341 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2342 visible_p = 1;
2343 else
2345 /* We skipped some characters but not necessarily
2346 all there are. Check if we ended up on visible
2347 text. Fget_char_property returns the property of
2348 the char before the given position, i.e. if we
2349 get visible_p = 1, this means that the char at
2350 newpos is visible. */
2351 XSETFASTINT (pos, newpos);
2352 prop = Fget_char_property (pos, Qinvisible, it->window);
2353 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2356 /* If we ended up on invisible text, proceed to
2357 skip starting with next_stop. */
2358 if (!visible_p)
2359 IT_CHARPOS (*it) = next_stop;
2361 while (!visible_p);
2363 /* The position newpos is now either ZV or on visible text. */
2364 IT_CHARPOS (*it) = newpos;
2365 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2367 /* Maybe return `...' next for the end of the invisible text. */
2368 if (display_ellipsis_p)
2370 if (it->dp
2371 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2373 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2374 it->dpvec = v->contents;
2375 it->dpend = v->contents + v->size;
2377 else
2379 /* Default `...'. */
2380 it->dpvec = default_invis_vector;
2381 it->dpend = default_invis_vector + 3;
2384 /* The ellipsis display does not replace the display of
2385 the character at the new position. Indicate this by
2386 setting IT->dpvec_char_len to zero. */
2387 it->dpvec_char_len = 0;
2389 it->current.dpvec_index = 0;
2390 it->method = next_element_from_display_vector;
2395 return handled;
2400 /***********************************************************************
2401 'display' property
2402 ***********************************************************************/
2404 /* Set up iterator IT from `display' property at its current position.
2405 Called from handle_stop. */
2407 static enum prop_handled
2408 handle_display_prop (it)
2409 struct it *it;
2411 Lisp_Object prop, object;
2412 struct text_pos *position;
2413 int space_or_image_found_p;
2415 if (STRINGP (it->string))
2417 object = it->string;
2418 position = &it->current.string_pos;
2420 else
2422 object = Qnil;
2423 position = &it->current.pos;
2426 /* Reset those iterator values set from display property values. */
2427 it->font_height = Qnil;
2428 it->space_width = Qnil;
2429 it->voffset = 0;
2431 /* We don't support recursive `display' properties, i.e. string
2432 values that have a string `display' property, that have a string
2433 `display' property etc. */
2434 if (!it->string_from_display_prop_p)
2435 it->area = TEXT_AREA;
2437 prop = Fget_char_property (make_number (position->charpos),
2438 Qdisplay, object);
2439 if (NILP (prop))
2440 return HANDLED_NORMALLY;
2442 space_or_image_found_p = 0;
2443 if (CONSP (prop)
2444 && CONSP (XCAR (prop))
2445 && !EQ (Qmargin, XCAR (XCAR (prop))))
2447 /* A list of sub-properties. */
2448 while (CONSP (prop))
2450 if (handle_single_display_prop (it, XCAR (prop), object, position))
2451 space_or_image_found_p = 1;
2452 prop = XCDR (prop);
2455 else if (VECTORP (prop))
2457 int i;
2458 for (i = 0; i < XVECTOR (prop)->size; ++i)
2459 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2460 object, position))
2461 space_or_image_found_p = 1;
2463 else
2465 if (handle_single_display_prop (it, prop, object, position))
2466 space_or_image_found_p = 1;
2469 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2473 /* Value is the position of the end of the `display' property starting
2474 at START_POS in OBJECT. */
2476 static struct text_pos
2477 display_prop_end (it, object, start_pos)
2478 struct it *it;
2479 Lisp_Object object;
2480 struct text_pos start_pos;
2482 Lisp_Object end;
2483 struct text_pos end_pos;
2485 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2486 Qdisplay, object, Qnil);
2487 CHARPOS (end_pos) = XFASTINT (end);
2488 if (STRINGP (object))
2489 compute_string_pos (&end_pos, start_pos, it->string);
2490 else
2491 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2493 return end_pos;
2497 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2498 is the object in which the `display' property was found. *POSITION
2499 is the position at which it was found.
2501 If PROP is a `space' or `image' sub-property, set *POSITION to the
2502 end position of the `display' property.
2504 Value is non-zero if a `space' or `image' property value was found. */
2506 static int
2507 handle_single_display_prop (it, prop, object, position)
2508 struct it *it;
2509 Lisp_Object prop;
2510 Lisp_Object object;
2511 struct text_pos *position;
2513 Lisp_Object value;
2514 int space_or_image_found_p = 0;
2515 Lisp_Object form;
2517 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2518 evaluated. If the result is nil, VALUE is ignored. */
2519 form = Qt;
2520 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2522 prop = XCDR (prop);
2523 if (!CONSP (prop))
2524 return 0;
2525 form = XCAR (prop);
2526 prop = XCDR (prop);
2529 if (!NILP (form) && !EQ (form, Qt))
2531 struct gcpro gcpro1;
2532 struct text_pos end_pos, pt;
2534 GCPRO1 (form);
2535 end_pos = display_prop_end (it, object, *position);
2537 /* Temporarily set point to the end position, and then evaluate
2538 the form. This makes `(eolp)' work as FORM. */
2539 if (BUFFERP (object))
2541 CHARPOS (pt) = PT;
2542 BYTEPOS (pt) = PT_BYTE;
2543 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2546 form = safe_eval (form);
2548 if (BUFFERP (object))
2549 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2550 UNGCPRO;
2553 if (NILP (form))
2554 return 0;
2556 if (CONSP (prop)
2557 && EQ (XCAR (prop), Qheight)
2558 && CONSP (XCDR (prop)))
2560 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2561 return 0;
2563 /* `(height HEIGHT)'. */
2564 it->font_height = XCAR (XCDR (prop));
2565 if (!NILP (it->font_height))
2567 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2568 int new_height = -1;
2570 if (CONSP (it->font_height)
2571 && (EQ (XCAR (it->font_height), Qplus)
2572 || EQ (XCAR (it->font_height), Qminus))
2573 && CONSP (XCDR (it->font_height))
2574 && INTEGERP (XCAR (XCDR (it->font_height))))
2576 /* `(+ N)' or `(- N)' where N is an integer. */
2577 int steps = XINT (XCAR (XCDR (it->font_height)));
2578 if (EQ (XCAR (it->font_height), Qplus))
2579 steps = - steps;
2580 it->face_id = smaller_face (it->f, it->face_id, steps);
2582 else if (FUNCTIONP (it->font_height))
2584 /* Call function with current height as argument.
2585 Value is the new height. */
2586 Lisp_Object height;
2587 height = safe_call1 (it->font_height,
2588 face->lface[LFACE_HEIGHT_INDEX]);
2589 if (NUMBERP (height))
2590 new_height = XFLOATINT (height);
2592 else if (NUMBERP (it->font_height))
2594 /* Value is a multiple of the canonical char height. */
2595 struct face *face;
2597 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2598 new_height = (XFLOATINT (it->font_height)
2599 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2601 else
2603 /* Evaluate IT->font_height with `height' bound to the
2604 current specified height to get the new height. */
2605 Lisp_Object value;
2606 int count = specpdl_ptr - specpdl;
2608 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2609 value = safe_eval (it->font_height);
2610 unbind_to (count, Qnil);
2612 if (NUMBERP (value))
2613 new_height = XFLOATINT (value);
2616 if (new_height > 0)
2617 it->face_id = face_with_height (it->f, it->face_id, new_height);
2620 else if (CONSP (prop)
2621 && EQ (XCAR (prop), Qspace_width)
2622 && CONSP (XCDR (prop)))
2624 /* `(space_width WIDTH)'. */
2625 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2626 return 0;
2628 value = XCAR (XCDR (prop));
2629 if (NUMBERP (value) && XFLOATINT (value) > 0)
2630 it->space_width = value;
2632 else if (CONSP (prop)
2633 && EQ (XCAR (prop), Qraise)
2634 && CONSP (XCDR (prop)))
2636 /* `(raise FACTOR)'. */
2637 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2638 return 0;
2640 #ifdef HAVE_WINDOW_SYSTEM
2641 value = XCAR (XCDR (prop));
2642 if (NUMBERP (value))
2644 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2645 it->voffset = - (XFLOATINT (value)
2646 * (FONT_HEIGHT (face->font)));
2648 #endif /* HAVE_WINDOW_SYSTEM */
2650 else if (!it->string_from_display_prop_p)
2652 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2653 VALUE) or `((margin nil) VALUE)' or VALUE. */
2654 Lisp_Object location, value;
2655 struct text_pos start_pos;
2656 int valid_p;
2658 /* Characters having this form of property are not displayed, so
2659 we have to find the end of the property. */
2660 start_pos = *position;
2661 *position = display_prop_end (it, object, start_pos);
2662 value = Qnil;
2664 /* Let's stop at the new position and assume that all
2665 text properties change there. */
2666 it->stop_charpos = position->charpos;
2668 location = Qunbound;
2669 if (CONSP (prop) && CONSP (XCAR (prop)))
2671 Lisp_Object tem;
2673 value = XCDR (prop);
2674 if (CONSP (value))
2675 value = XCAR (value);
2677 tem = XCAR (prop);
2678 if (EQ (XCAR (tem), Qmargin)
2679 && (tem = XCDR (tem),
2680 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2681 (NILP (tem)
2682 || EQ (tem, Qleft_margin)
2683 || EQ (tem, Qright_margin))))
2684 location = tem;
2687 if (EQ (location, Qunbound))
2689 location = Qnil;
2690 value = prop;
2693 #ifdef HAVE_WINDOW_SYSTEM
2694 if (FRAME_TERMCAP_P (it->f))
2695 valid_p = STRINGP (value);
2696 else
2697 valid_p = (STRINGP (value)
2698 || (CONSP (value) && EQ (XCAR (value), Qspace))
2699 || valid_image_p (value));
2700 #else /* not HAVE_WINDOW_SYSTEM */
2701 valid_p = STRINGP (value);
2702 #endif /* not HAVE_WINDOW_SYSTEM */
2704 if ((EQ (location, Qleft_margin)
2705 || EQ (location, Qright_margin)
2706 || NILP (location))
2707 && valid_p)
2709 space_or_image_found_p = 1;
2711 /* Save current settings of IT so that we can restore them
2712 when we are finished with the glyph property value. */
2713 push_it (it);
2715 if (NILP (location))
2716 it->area = TEXT_AREA;
2717 else if (EQ (location, Qleft_margin))
2718 it->area = LEFT_MARGIN_AREA;
2719 else
2720 it->area = RIGHT_MARGIN_AREA;
2722 if (STRINGP (value))
2724 it->string = value;
2725 it->multibyte_p = STRING_MULTIBYTE (it->string);
2726 it->current.overlay_string_index = -1;
2727 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2728 it->end_charpos = it->string_nchars
2729 = XSTRING (it->string)->size;
2730 it->method = next_element_from_string;
2731 it->stop_charpos = 0;
2732 it->string_from_display_prop_p = 1;
2734 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2736 it->method = next_element_from_stretch;
2737 it->object = value;
2738 it->current.pos = it->position = start_pos;
2740 #ifdef HAVE_WINDOW_SYSTEM
2741 else
2743 it->what = IT_IMAGE;
2744 it->image_id = lookup_image (it->f, value);
2745 it->position = start_pos;
2746 it->object = NILP (object) ? it->w->buffer : object;
2747 it->method = next_element_from_image;
2749 /* Say that we haven't consumed the characters with
2750 `display' property yet. The call to pop_it in
2751 set_iterator_to_next will clean this up. */
2752 *position = start_pos;
2754 #endif /* HAVE_WINDOW_SYSTEM */
2756 else
2757 /* Invalid property or property not supported. Restore
2758 the position to what it was before. */
2759 *position = start_pos;
2762 return space_or_image_found_p;
2766 /* Check if PROP is a display sub-property value whose text should be
2767 treated as intangible. */
2769 static int
2770 single_display_prop_intangible_p (prop)
2771 Lisp_Object prop;
2773 /* Skip over `when FORM'. */
2774 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2776 prop = XCDR (prop);
2777 if (!CONSP (prop))
2778 return 0;
2779 prop = XCDR (prop);
2782 if (!CONSP (prop))
2783 return 0;
2785 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2786 we don't need to treat text as intangible. */
2787 if (EQ (XCAR (prop), Qmargin))
2789 prop = XCDR (prop);
2790 if (!CONSP (prop))
2791 return 0;
2793 prop = XCDR (prop);
2794 if (!CONSP (prop)
2795 || EQ (XCAR (prop), Qleft_margin)
2796 || EQ (XCAR (prop), Qright_margin))
2797 return 0;
2800 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2804 /* Check if PROP is a display property value whose text should be
2805 treated as intangible. */
2808 display_prop_intangible_p (prop)
2809 Lisp_Object prop;
2811 if (CONSP (prop)
2812 && CONSP (XCAR (prop))
2813 && !EQ (Qmargin, XCAR (XCAR (prop))))
2815 /* A list of sub-properties. */
2816 while (CONSP (prop))
2818 if (single_display_prop_intangible_p (XCAR (prop)))
2819 return 1;
2820 prop = XCDR (prop);
2823 else if (VECTORP (prop))
2825 /* A vector of sub-properties. */
2826 int i;
2827 for (i = 0; i < XVECTOR (prop)->size; ++i)
2828 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2829 return 1;
2831 else
2832 return single_display_prop_intangible_p (prop);
2834 return 0;
2838 /***********************************************************************
2839 `composition' property
2840 ***********************************************************************/
2842 /* Set up iterator IT from `composition' property at its current
2843 position. Called from handle_stop. */
2845 static enum prop_handled
2846 handle_composition_prop (it)
2847 struct it *it;
2849 Lisp_Object prop, string;
2850 int pos, pos_byte, end;
2851 enum prop_handled handled = HANDLED_NORMALLY;
2853 if (STRINGP (it->string))
2855 pos = IT_STRING_CHARPOS (*it);
2856 pos_byte = IT_STRING_BYTEPOS (*it);
2857 string = it->string;
2859 else
2861 pos = IT_CHARPOS (*it);
2862 pos_byte = IT_BYTEPOS (*it);
2863 string = Qnil;
2866 /* If there's a valid composition and point is not inside of the
2867 composition (in the case that the composition is from the current
2868 buffer), draw a glyph composed from the composition components. */
2869 if (find_composition (pos, -1, &pos, &end, &prop, string)
2870 && COMPOSITION_VALID_P (pos, end, prop)
2871 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2873 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2875 if (id >= 0)
2877 it->method = next_element_from_composition;
2878 it->cmp_id = id;
2879 it->cmp_len = COMPOSITION_LENGTH (prop);
2880 /* For a terminal, draw only the first character of the
2881 components. */
2882 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2883 it->len = (STRINGP (it->string)
2884 ? string_char_to_byte (it->string, end)
2885 : CHAR_TO_BYTE (end)) - pos_byte;
2886 it->stop_charpos = end;
2887 handled = HANDLED_RETURN;
2891 return handled;
2896 /***********************************************************************
2897 Overlay strings
2898 ***********************************************************************/
2900 /* The following structure is used to record overlay strings for
2901 later sorting in load_overlay_strings. */
2903 struct overlay_entry
2905 Lisp_Object overlay;
2906 Lisp_Object string;
2907 int priority;
2908 int after_string_p;
2912 /* Set up iterator IT from overlay strings at its current position.
2913 Called from handle_stop. */
2915 static enum prop_handled
2916 handle_overlay_change (it)
2917 struct it *it;
2919 if (!STRINGP (it->string) && get_overlay_strings (it))
2920 return HANDLED_RECOMPUTE_PROPS;
2921 else
2922 return HANDLED_NORMALLY;
2926 /* Set up the next overlay string for delivery by IT, if there is an
2927 overlay string to deliver. Called by set_iterator_to_next when the
2928 end of the current overlay string is reached. If there are more
2929 overlay strings to display, IT->string and
2930 IT->current.overlay_string_index are set appropriately here.
2931 Otherwise IT->string is set to nil. */
2933 static void
2934 next_overlay_string (it)
2935 struct it *it;
2937 ++it->current.overlay_string_index;
2938 if (it->current.overlay_string_index == it->n_overlay_strings)
2940 /* No more overlay strings. Restore IT's settings to what
2941 they were before overlay strings were processed, and
2942 continue to deliver from current_buffer. */
2943 pop_it (it);
2944 xassert (it->stop_charpos >= BEGV
2945 && it->stop_charpos <= it->end_charpos);
2946 it->string = Qnil;
2947 it->current.overlay_string_index = -1;
2948 SET_TEXT_POS (it->current.string_pos, -1, -1);
2949 it->n_overlay_strings = 0;
2950 it->method = next_element_from_buffer;
2952 /* If we're at the end of the buffer, record that we have
2953 processed the overlay strings there already, so that
2954 next_element_from_buffer doesn't try it again. */
2955 if (IT_CHARPOS (*it) >= it->end_charpos)
2956 it->overlay_strings_at_end_processed_p = 1;
2958 else
2960 /* There are more overlay strings to process. If
2961 IT->current.overlay_string_index has advanced to a position
2962 where we must load IT->overlay_strings with more strings, do
2963 it. */
2964 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2966 if (it->current.overlay_string_index && i == 0)
2967 load_overlay_strings (it);
2969 /* Initialize IT to deliver display elements from the overlay
2970 string. */
2971 it->string = it->overlay_strings[i];
2972 it->multibyte_p = STRING_MULTIBYTE (it->string);
2973 SET_TEXT_POS (it->current.string_pos, 0, 0);
2974 it->method = next_element_from_string;
2975 it->stop_charpos = 0;
2978 CHECK_IT (it);
2982 /* Compare two overlay_entry structures E1 and E2. Used as a
2983 comparison function for qsort in load_overlay_strings. Overlay
2984 strings for the same position are sorted so that
2986 1. All after-strings come in front of before-strings, except
2987 when they come from the same overlay.
2989 2. Within after-strings, strings are sorted so that overlay strings
2990 from overlays with higher priorities come first.
2992 2. Within before-strings, strings are sorted so that overlay
2993 strings from overlays with higher priorities come last.
2995 Value is analogous to strcmp. */
2998 static int
2999 compare_overlay_entries (e1, e2)
3000 void *e1, *e2;
3002 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3003 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3004 int result;
3006 if (entry1->after_string_p != entry2->after_string_p)
3008 /* Let after-strings appear in front of before-strings if
3009 they come from different overlays. */
3010 if (EQ (entry1->overlay, entry2->overlay))
3011 result = entry1->after_string_p ? 1 : -1;
3012 else
3013 result = entry1->after_string_p ? -1 : 1;
3015 else if (entry1->after_string_p)
3016 /* After-strings sorted in order of decreasing priority. */
3017 result = entry2->priority - entry1->priority;
3018 else
3019 /* Before-strings sorted in order of increasing priority. */
3020 result = entry1->priority - entry2->priority;
3022 return result;
3026 /* Load the vector IT->overlay_strings with overlay strings from IT's
3027 current buffer position. Set IT->n_overlays to the total number of
3028 overlay strings found.
3030 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3031 a time. On entry into load_overlay_strings,
3032 IT->current.overlay_string_index gives the number of overlay
3033 strings that have already been loaded by previous calls to this
3034 function.
3036 IT->add_overlay_start contains an additional overlay start
3037 position to consider for taking overlay strings from, if non-zero.
3038 This position comes into play when the overlay has an `invisible'
3039 property, and both before and after-strings. When we've skipped to
3040 the end of the overlay, because of its `invisible' property, we
3041 nevertheless want its before-string to appear.
3042 IT->add_overlay_start will contain the overlay start position
3043 in this case.
3045 Overlay strings are sorted so that after-string strings come in
3046 front of before-string strings. Within before and after-strings,
3047 strings are sorted by overlay priority. See also function
3048 compare_overlay_entries. */
3050 static void
3051 load_overlay_strings (it)
3052 struct it *it;
3054 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3055 Lisp_Object ov, overlay, window, str, invisible;
3056 int start, end;
3057 int size = 20;
3058 int n = 0, i, j, invis_p;
3059 struct overlay_entry *entries
3060 = (struct overlay_entry *) alloca (size * sizeof *entries);
3061 int charpos = IT_CHARPOS (*it);
3063 /* Append the overlay string STRING of overlay OVERLAY to vector
3064 `entries' which has size `size' and currently contains `n'
3065 elements. AFTER_P non-zero means STRING is an after-string of
3066 OVERLAY. */
3067 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3068 do \
3070 Lisp_Object priority; \
3072 if (n == size) \
3074 int new_size = 2 * size; \
3075 struct overlay_entry *old = entries; \
3076 entries = \
3077 (struct overlay_entry *) alloca (new_size \
3078 * sizeof *entries); \
3079 bcopy (old, entries, size * sizeof *entries); \
3080 size = new_size; \
3083 entries[n].string = (STRING); \
3084 entries[n].overlay = (OVERLAY); \
3085 priority = Foverlay_get ((OVERLAY), Qpriority); \
3086 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3087 entries[n].after_string_p = (AFTER_P); \
3088 ++n; \
3090 while (0)
3092 /* Process overlay before the overlay center. */
3093 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3095 overlay = XCAR (ov);
3096 xassert (OVERLAYP (overlay));
3097 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3098 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3100 if (end < charpos)
3101 break;
3103 /* Skip this overlay if it doesn't start or end at IT's current
3104 position. */
3105 if (end != charpos && start != charpos)
3106 continue;
3108 /* Skip this overlay if it doesn't apply to IT->w. */
3109 window = Foverlay_get (overlay, Qwindow);
3110 if (WINDOWP (window) && XWINDOW (window) != it->w)
3111 continue;
3113 /* If the text ``under'' the overlay is invisible, both before-
3114 and after-strings from this overlay are visible; start and
3115 end position are indistinguishable. */
3116 invisible = Foverlay_get (overlay, Qinvisible);
3117 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3119 /* If overlay has a non-empty before-string, record it. */
3120 if ((start == charpos || (end == charpos && invis_p))
3121 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3122 && XSTRING (str)->size)
3123 RECORD_OVERLAY_STRING (overlay, str, 0);
3125 /* If overlay has a non-empty after-string, record it. */
3126 if ((end == charpos || (start == charpos && invis_p))
3127 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3128 && XSTRING (str)->size)
3129 RECORD_OVERLAY_STRING (overlay, str, 1);
3132 /* Process overlays after the overlay center. */
3133 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3135 overlay = XCAR (ov);
3136 xassert (OVERLAYP (overlay));
3137 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3138 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3140 if (start > charpos)
3141 break;
3143 /* Skip this overlay if it doesn't start or end at IT's current
3144 position. */
3145 if (end != charpos && start != charpos)
3146 continue;
3148 /* Skip this overlay if it doesn't apply to IT->w. */
3149 window = Foverlay_get (overlay, Qwindow);
3150 if (WINDOWP (window) && XWINDOW (window) != it->w)
3151 continue;
3153 /* If the text ``under'' the overlay is invisible, it has a zero
3154 dimension, and both before- and after-strings apply. */
3155 invisible = Foverlay_get (overlay, Qinvisible);
3156 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3158 /* If overlay has a non-empty before-string, record it. */
3159 if ((start == charpos || (end == charpos && invis_p))
3160 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3161 && XSTRING (str)->size)
3162 RECORD_OVERLAY_STRING (overlay, str, 0);
3164 /* If overlay has a non-empty after-string, record it. */
3165 if ((end == charpos || (start == charpos && invis_p))
3166 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3167 && XSTRING (str)->size)
3168 RECORD_OVERLAY_STRING (overlay, str, 1);
3171 #undef RECORD_OVERLAY_STRING
3173 /* Sort entries. */
3174 if (n > 1)
3175 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3177 /* Record the total number of strings to process. */
3178 it->n_overlay_strings = n;
3180 /* IT->current.overlay_string_index is the number of overlay strings
3181 that have already been consumed by IT. Copy some of the
3182 remaining overlay strings to IT->overlay_strings. */
3183 i = 0;
3184 j = it->current.overlay_string_index;
3185 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3186 it->overlay_strings[i++] = entries[j++].string;
3188 CHECK_IT (it);
3192 /* Get the first chunk of overlay strings at IT's current buffer
3193 position. Value is non-zero if at least one overlay string was
3194 found. */
3196 static int
3197 get_overlay_strings (it)
3198 struct it *it;
3200 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3201 process. This fills IT->overlay_strings with strings, and sets
3202 IT->n_overlay_strings to the total number of strings to process.
3203 IT->pos.overlay_string_index has to be set temporarily to zero
3204 because load_overlay_strings needs this; it must be set to -1
3205 when no overlay strings are found because a zero value would
3206 indicate a position in the first overlay string. */
3207 it->current.overlay_string_index = 0;
3208 load_overlay_strings (it);
3210 /* If we found overlay strings, set up IT to deliver display
3211 elements from the first one. Otherwise set up IT to deliver
3212 from current_buffer. */
3213 if (it->n_overlay_strings)
3215 /* Make sure we know settings in current_buffer, so that we can
3216 restore meaningful values when we're done with the overlay
3217 strings. */
3218 compute_stop_pos (it);
3219 xassert (it->face_id >= 0);
3221 /* Save IT's settings. They are restored after all overlay
3222 strings have been processed. */
3223 xassert (it->sp == 0);
3224 push_it (it);
3226 /* Set up IT to deliver display elements from the first overlay
3227 string. */
3228 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3229 it->stop_charpos = 0;
3230 it->string = it->overlay_strings[0];
3231 it->multibyte_p = STRING_MULTIBYTE (it->string);
3232 xassert (STRINGP (it->string));
3233 it->method = next_element_from_string;
3235 else
3237 it->string = Qnil;
3238 it->current.overlay_string_index = -1;
3239 it->method = next_element_from_buffer;
3242 CHECK_IT (it);
3244 /* Value is non-zero if we found at least one overlay string. */
3245 return STRINGP (it->string);
3250 /***********************************************************************
3251 Saving and restoring state
3252 ***********************************************************************/
3254 /* Save current settings of IT on IT->stack. Called, for example,
3255 before setting up IT for an overlay string, to be able to restore
3256 IT's settings to what they were after the overlay string has been
3257 processed. */
3259 static void
3260 push_it (it)
3261 struct it *it;
3263 struct iterator_stack_entry *p;
3265 xassert (it->sp < 2);
3266 p = it->stack + it->sp;
3268 p->stop_charpos = it->stop_charpos;
3269 xassert (it->face_id >= 0);
3270 p->face_id = it->face_id;
3271 p->string = it->string;
3272 p->pos = it->current;
3273 p->end_charpos = it->end_charpos;
3274 p->string_nchars = it->string_nchars;
3275 p->area = it->area;
3276 p->multibyte_p = it->multibyte_p;
3277 p->space_width = it->space_width;
3278 p->font_height = it->font_height;
3279 p->voffset = it->voffset;
3280 p->string_from_display_prop_p = it->string_from_display_prop_p;
3281 ++it->sp;
3285 /* Restore IT's settings from IT->stack. Called, for example, when no
3286 more overlay strings must be processed, and we return to delivering
3287 display elements from a buffer, or when the end of a string from a
3288 `display' property is reached and we return to delivering display
3289 elements from an overlay string, or from a buffer. */
3291 static void
3292 pop_it (it)
3293 struct it *it;
3295 struct iterator_stack_entry *p;
3297 xassert (it->sp > 0);
3298 --it->sp;
3299 p = it->stack + it->sp;
3300 it->stop_charpos = p->stop_charpos;
3301 it->face_id = p->face_id;
3302 it->string = p->string;
3303 it->current = p->pos;
3304 it->end_charpos = p->end_charpos;
3305 it->string_nchars = p->string_nchars;
3306 it->area = p->area;
3307 it->multibyte_p = p->multibyte_p;
3308 it->space_width = p->space_width;
3309 it->font_height = p->font_height;
3310 it->voffset = p->voffset;
3311 it->string_from_display_prop_p = p->string_from_display_prop_p;
3316 /***********************************************************************
3317 Moving over lines
3318 ***********************************************************************/
3320 /* Set IT's current position to the previous line start. */
3322 static void
3323 back_to_previous_line_start (it)
3324 struct it *it;
3326 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3327 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3331 /* Move IT to the next line start.
3333 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3334 we skipped over part of the text (as opposed to moving the iterator
3335 continuously over the text). Otherwise, don't change the value
3336 of *SKIPPED_P.
3338 Newlines may come from buffer text, overlay strings, or strings
3339 displayed via the `display' property. That's the reason we can't
3340 simply use find_next_newline_no_quit. */
3342 static int
3343 forward_to_next_line_start (it, skipped_p)
3344 struct it *it;
3345 int *skipped_p;
3347 int old_selective, newline_found_p, n;
3348 const int MAX_NEWLINE_DISTANCE = 500;
3350 /* Don't handle selective display in the following. It's (a)
3351 unnecessary and (b) leads to an infinite recursion because
3352 next_element_from_ellipsis indirectly calls this function. */
3353 old_selective = it->selective;
3354 it->selective = 0;
3356 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3357 from buffer text. */
3358 n = newline_found_p = 0;
3359 while (n < MAX_NEWLINE_DISTANCE
3360 && get_next_display_element (it)
3361 && !newline_found_p)
3363 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3364 set_iterator_to_next (it, 0);
3365 if (!STRINGP (it->string))
3366 ++n;
3369 /* If we didn't find a newline near enough, see if we can use a
3370 short-cut. */
3371 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3373 int start = IT_CHARPOS (*it);
3374 int limit = find_next_newline_no_quit (start, 1);
3375 Lisp_Object pos;
3377 xassert (!STRINGP (it->string));
3379 /* If there isn't any `display' property in sight, and no
3380 overlays, we can just use the position of the newline in
3381 buffer text. */
3382 if (it->stop_charpos >= limit
3383 || ((pos = Fnext_single_property_change (make_number (start),
3384 Qdisplay,
3385 Qnil, make_number (limit)),
3386 NILP (pos))
3387 && next_overlay_change (start) == ZV))
3389 IT_CHARPOS (*it) = limit;
3390 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3391 *skipped_p = newline_found_p = 1;
3393 else
3395 while (get_next_display_element (it)
3396 && !newline_found_p)
3398 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3399 set_iterator_to_next (it, 0);
3404 it->selective = old_selective;
3405 return newline_found_p;
3409 /* Set IT's current position to the previous visible line start. Skip
3410 invisible text that is so either due to text properties or due to
3411 selective display. Caution: this does not change IT->current_x and
3412 IT->hpos. */
3414 static void
3415 back_to_previous_visible_line_start (it)
3416 struct it *it;
3418 int visible_p = 0;
3420 /* Go back one newline if not on BEGV already. */
3421 if (IT_CHARPOS (*it) > BEGV)
3422 back_to_previous_line_start (it);
3424 /* Move over lines that are invisible because of selective display
3425 or text properties. */
3426 while (IT_CHARPOS (*it) > BEGV
3427 && !visible_p)
3429 visible_p = 1;
3431 /* If selective > 0, then lines indented more than that values
3432 are invisible. */
3433 if (it->selective > 0
3434 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3435 it->selective))
3436 visible_p = 0;
3437 else
3439 Lisp_Object prop;
3441 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3442 Qinvisible, it->window);
3443 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3444 visible_p = 0;
3447 /* Back one more newline if the current one is invisible. */
3448 if (!visible_p)
3449 back_to_previous_line_start (it);
3452 xassert (IT_CHARPOS (*it) >= BEGV);
3453 xassert (IT_CHARPOS (*it) == BEGV
3454 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3455 CHECK_IT (it);
3459 /* Reseat iterator IT at the previous visible line start. Skip
3460 invisible text that is so either due to text properties or due to
3461 selective display. At the end, update IT's overlay information,
3462 face information etc. */
3464 static void
3465 reseat_at_previous_visible_line_start (it)
3466 struct it *it;
3468 back_to_previous_visible_line_start (it);
3469 reseat (it, it->current.pos, 1);
3470 CHECK_IT (it);
3474 /* Reseat iterator IT on the next visible line start in the current
3475 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3476 preceding the line start. Skip over invisible text that is so
3477 because of selective display. Compute faces, overlays etc at the
3478 new position. Note that this function does not skip over text that
3479 is invisible because of text properties. */
3481 static void
3482 reseat_at_next_visible_line_start (it, on_newline_p)
3483 struct it *it;
3484 int on_newline_p;
3486 int newline_found_p, skipped_p = 0;
3488 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3490 /* Skip over lines that are invisible because they are indented
3491 more than the value of IT->selective. */
3492 if (it->selective > 0)
3493 while (IT_CHARPOS (*it) < ZV
3494 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3495 it->selective))
3496 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3498 /* Position on the newline if that's what's requested. */
3499 if (on_newline_p && newline_found_p)
3501 if (STRINGP (it->string))
3503 if (IT_STRING_CHARPOS (*it) > 0)
3505 --IT_STRING_CHARPOS (*it);
3506 --IT_STRING_BYTEPOS (*it);
3509 else if (IT_CHARPOS (*it) > BEGV)
3511 --IT_CHARPOS (*it);
3512 --IT_BYTEPOS (*it);
3513 reseat (it, it->current.pos, 0);
3516 else if (skipped_p)
3517 reseat (it, it->current.pos, 0);
3519 CHECK_IT (it);
3524 /***********************************************************************
3525 Changing an iterator's position
3526 ***********************************************************************/
3528 /* Change IT's current position to POS in current_buffer. If FORCE_P
3529 is non-zero, always check for text properties at the new position.
3530 Otherwise, text properties are only looked up if POS >=
3531 IT->check_charpos of a property. */
3533 static void
3534 reseat (it, pos, force_p)
3535 struct it *it;
3536 struct text_pos pos;
3537 int force_p;
3539 int original_pos = IT_CHARPOS (*it);
3541 reseat_1 (it, pos, 0);
3543 /* Determine where to check text properties. Avoid doing it
3544 where possible because text property lookup is very expensive. */
3545 if (force_p
3546 || CHARPOS (pos) > it->stop_charpos
3547 || CHARPOS (pos) < original_pos)
3548 handle_stop (it);
3550 CHECK_IT (it);
3554 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3555 IT->stop_pos to POS, also. */
3557 static void
3558 reseat_1 (it, pos, set_stop_p)
3559 struct it *it;
3560 struct text_pos pos;
3561 int set_stop_p;
3563 /* Don't call this function when scanning a C string. */
3564 xassert (it->s == NULL);
3566 /* POS must be a reasonable value. */
3567 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3569 it->current.pos = it->position = pos;
3570 XSETBUFFER (it->object, current_buffer);
3571 it->dpvec = NULL;
3572 it->current.dpvec_index = -1;
3573 it->current.overlay_string_index = -1;
3574 IT_STRING_CHARPOS (*it) = -1;
3575 IT_STRING_BYTEPOS (*it) = -1;
3576 it->string = Qnil;
3577 it->method = next_element_from_buffer;
3578 it->sp = 0;
3580 if (set_stop_p)
3581 it->stop_charpos = CHARPOS (pos);
3585 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3586 If S is non-null, it is a C string to iterate over. Otherwise,
3587 STRING gives a Lisp string to iterate over.
3589 If PRECISION > 0, don't return more then PRECISION number of
3590 characters from the string.
3592 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3593 characters have been returned. FIELD_WIDTH < 0 means an infinite
3594 field width.
3596 MULTIBYTE = 0 means disable processing of multibyte characters,
3597 MULTIBYTE > 0 means enable it,
3598 MULTIBYTE < 0 means use IT->multibyte_p.
3600 IT must be initialized via a prior call to init_iterator before
3601 calling this function. */
3603 static void
3604 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3605 struct it *it;
3606 unsigned char *s;
3607 Lisp_Object string;
3608 int charpos;
3609 int precision, field_width, multibyte;
3611 /* No region in strings. */
3612 it->region_beg_charpos = it->region_end_charpos = -1;
3614 /* No text property checks performed by default, but see below. */
3615 it->stop_charpos = -1;
3617 /* Set iterator position and end position. */
3618 bzero (&it->current, sizeof it->current);
3619 it->current.overlay_string_index = -1;
3620 it->current.dpvec_index = -1;
3621 xassert (charpos >= 0);
3623 /* Use the setting of MULTIBYTE if specified. */
3624 if (multibyte >= 0)
3625 it->multibyte_p = multibyte > 0;
3627 if (s == NULL)
3629 xassert (STRINGP (string));
3630 it->string = string;
3631 it->s = NULL;
3632 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3633 it->method = next_element_from_string;
3634 it->current.string_pos = string_pos (charpos, string);
3636 else
3638 it->s = s;
3639 it->string = Qnil;
3641 /* Note that we use IT->current.pos, not it->current.string_pos,
3642 for displaying C strings. */
3643 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3644 if (it->multibyte_p)
3646 it->current.pos = c_string_pos (charpos, s, 1);
3647 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3649 else
3651 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3652 it->end_charpos = it->string_nchars = strlen (s);
3655 it->method = next_element_from_c_string;
3658 /* PRECISION > 0 means don't return more than PRECISION characters
3659 from the string. */
3660 if (precision > 0 && it->end_charpos - charpos > precision)
3661 it->end_charpos = it->string_nchars = charpos + precision;
3663 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3664 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3665 FIELD_WIDTH < 0 means infinite field width. This is useful for
3666 padding with `-' at the end of a mode line. */
3667 if (field_width < 0)
3668 field_width = INFINITY;
3669 if (field_width > it->end_charpos - charpos)
3670 it->end_charpos = charpos + field_width;
3672 /* Use the standard display table for displaying strings. */
3673 if (DISP_TABLE_P (Vstandard_display_table))
3674 it->dp = XCHAR_TABLE (Vstandard_display_table);
3676 it->stop_charpos = charpos;
3677 CHECK_IT (it);
3682 /***********************************************************************
3683 Iteration
3684 ***********************************************************************/
3686 /* Load IT's display element fields with information about the next
3687 display element from the current position of IT. Value is zero if
3688 end of buffer (or C string) is reached. */
3691 get_next_display_element (it)
3692 struct it *it;
3694 /* Non-zero means that we found an display element. Zero means that
3695 we hit the end of what we iterate over. Performance note: the
3696 function pointer `method' used here turns out to be faster than
3697 using a sequence of if-statements. */
3698 int success_p = (*it->method) (it);
3700 if (it->what == IT_CHARACTER)
3702 /* Map via display table or translate control characters.
3703 IT->c, IT->len etc. have been set to the next character by
3704 the function call above. If we have a display table, and it
3705 contains an entry for IT->c, translate it. Don't do this if
3706 IT->c itself comes from a display table, otherwise we could
3707 end up in an infinite recursion. (An alternative could be to
3708 count the recursion depth of this function and signal an
3709 error when a certain maximum depth is reached.) Is it worth
3710 it? */
3711 if (success_p && it->dpvec == NULL)
3713 Lisp_Object dv;
3715 if (it->dp
3716 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3717 VECTORP (dv)))
3719 struct Lisp_Vector *v = XVECTOR (dv);
3721 /* Return the first character from the display table
3722 entry, if not empty. If empty, don't display the
3723 current character. */
3724 if (v->size)
3726 it->dpvec_char_len = it->len;
3727 it->dpvec = v->contents;
3728 it->dpend = v->contents + v->size;
3729 it->current.dpvec_index = 0;
3730 it->method = next_element_from_display_vector;
3733 success_p = get_next_display_element (it);
3736 /* Translate control characters into `\003' or `^C' form.
3737 Control characters coming from a display table entry are
3738 currently not translated because we use IT->dpvec to hold
3739 the translation. This could easily be changed but I
3740 don't believe that it is worth doing.
3742 Non-printable multibyte characters are also translated
3743 octal form. */
3744 else if ((it->c < ' '
3745 && (it->area != TEXT_AREA
3746 || (it->c != '\n' && it->c != '\t')))
3747 || (it->c >= 127
3748 && it->len == 1)
3749 || !CHAR_PRINTABLE_P (it->c))
3751 /* IT->c is a control character which must be displayed
3752 either as '\003' or as `^C' where the '\\' and '^'
3753 can be defined in the display table. Fill
3754 IT->ctl_chars with glyphs for what we have to
3755 display. Then, set IT->dpvec to these glyphs. */
3756 GLYPH g;
3758 if (it->c < 128 && it->ctl_arrow_p)
3760 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3761 if (it->dp
3762 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3763 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3764 g = XINT (DISP_CTRL_GLYPH (it->dp));
3765 else
3766 g = FAST_MAKE_GLYPH ('^', 0);
3767 XSETINT (it->ctl_chars[0], g);
3769 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3770 XSETINT (it->ctl_chars[1], g);
3772 /* Set up IT->dpvec and return first character from it. */
3773 it->dpvec_char_len = it->len;
3774 it->dpvec = it->ctl_chars;
3775 it->dpend = it->dpvec + 2;
3776 it->current.dpvec_index = 0;
3777 it->method = next_element_from_display_vector;
3778 get_next_display_element (it);
3780 else
3782 unsigned char str[MAX_MULTIBYTE_LENGTH];
3783 int len;
3784 int i;
3785 GLYPH escape_glyph;
3787 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3788 if (it->dp
3789 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3790 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3791 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3792 else
3793 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3795 if (SINGLE_BYTE_CHAR_P (it->c))
3796 str[0] = it->c, len = 1;
3797 else
3798 len = CHAR_STRING (it->c, str);
3800 for (i = 0; i < len; i++)
3802 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3803 /* Insert three more glyphs into IT->ctl_chars for
3804 the octal display of the character. */
3805 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3806 XSETINT (it->ctl_chars[i * 4 + 1], g);
3807 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3808 XSETINT (it->ctl_chars[i * 4 + 2], g);
3809 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3810 XSETINT (it->ctl_chars[i * 4 + 3], g);
3813 /* Set up IT->dpvec and return the first character
3814 from it. */
3815 it->dpvec_char_len = it->len;
3816 it->dpvec = it->ctl_chars;
3817 it->dpend = it->dpvec + len * 4;
3818 it->current.dpvec_index = 0;
3819 it->method = next_element_from_display_vector;
3820 get_next_display_element (it);
3825 /* Adjust face id for a multibyte character. There are no
3826 multibyte character in unibyte text. */
3827 if (it->multibyte_p
3828 && success_p
3829 && FRAME_WINDOW_P (it->f))
3831 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3832 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3836 /* Is this character the last one of a run of characters with
3837 box? If yes, set IT->end_of_box_run_p to 1. */
3838 if (it->face_box_p
3839 && it->s == NULL)
3841 int face_id;
3842 struct face *face;
3844 it->end_of_box_run_p
3845 = ((face_id = face_after_it_pos (it),
3846 face_id != it->face_id)
3847 && (face = FACE_FROM_ID (it->f, face_id),
3848 face->box == FACE_NO_BOX));
3851 /* Value is 0 if end of buffer or string reached. */
3852 return success_p;
3856 /* Move IT to the next display element.
3858 RESEAT_P non-zero means if called on a newline in buffer text,
3859 skip to the next visible line start.
3861 Functions get_next_display_element and set_iterator_to_next are
3862 separate because I find this arrangement easier to handle than a
3863 get_next_display_element function that also increments IT's
3864 position. The way it is we can first look at an iterator's current
3865 display element, decide whether it fits on a line, and if it does,
3866 increment the iterator position. The other way around we probably
3867 would either need a flag indicating whether the iterator has to be
3868 incremented the next time, or we would have to implement a
3869 decrement position function which would not be easy to write. */
3871 void
3872 set_iterator_to_next (it, reseat_p)
3873 struct it *it;
3874 int reseat_p;
3876 /* Reset flags indicating start and end of a sequence of characters
3877 with box. Reset them at the start of this function because
3878 moving the iterator to a new position might set them. */
3879 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3881 if (it->method == next_element_from_buffer)
3883 /* The current display element of IT is a character from
3884 current_buffer. Advance in the buffer, and maybe skip over
3885 invisible lines that are so because of selective display. */
3886 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3887 reseat_at_next_visible_line_start (it, 0);
3888 else
3890 xassert (it->len != 0);
3891 IT_BYTEPOS (*it) += it->len;
3892 IT_CHARPOS (*it) += 1;
3893 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3896 else if (it->method == next_element_from_composition)
3898 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3899 if (STRINGP (it->string))
3901 IT_STRING_BYTEPOS (*it) += it->len;
3902 IT_STRING_CHARPOS (*it) += it->cmp_len;
3903 it->method = next_element_from_string;
3904 goto consider_string_end;
3906 else
3908 IT_BYTEPOS (*it) += it->len;
3909 IT_CHARPOS (*it) += it->cmp_len;
3910 it->method = next_element_from_buffer;
3913 else if (it->method == next_element_from_c_string)
3915 /* Current display element of IT is from a C string. */
3916 IT_BYTEPOS (*it) += it->len;
3917 IT_CHARPOS (*it) += 1;
3919 else if (it->method == next_element_from_display_vector)
3921 /* Current display element of IT is from a display table entry.
3922 Advance in the display table definition. Reset it to null if
3923 end reached, and continue with characters from buffers/
3924 strings. */
3925 ++it->current.dpvec_index;
3927 /* Restore face of the iterator to what they were before the
3928 display vector entry (these entries may contain faces). */
3929 it->face_id = it->saved_face_id;
3931 if (it->dpvec + it->current.dpvec_index == it->dpend)
3933 if (it->s)
3934 it->method = next_element_from_c_string;
3935 else if (STRINGP (it->string))
3936 it->method = next_element_from_string;
3937 else
3938 it->method = next_element_from_buffer;
3940 it->dpvec = NULL;
3941 it->current.dpvec_index = -1;
3943 /* Skip over characters which were displayed via IT->dpvec. */
3944 if (it->dpvec_char_len < 0)
3945 reseat_at_next_visible_line_start (it, 1);
3946 else if (it->dpvec_char_len > 0)
3948 it->len = it->dpvec_char_len;
3949 set_iterator_to_next (it, reseat_p);
3953 else if (it->method == next_element_from_string)
3955 /* Current display element is a character from a Lisp string. */
3956 xassert (it->s == NULL && STRINGP (it->string));
3957 IT_STRING_BYTEPOS (*it) += it->len;
3958 IT_STRING_CHARPOS (*it) += 1;
3960 consider_string_end:
3962 if (it->current.overlay_string_index >= 0)
3964 /* IT->string is an overlay string. Advance to the
3965 next, if there is one. */
3966 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3967 next_overlay_string (it);
3969 else
3971 /* IT->string is not an overlay string. If we reached
3972 its end, and there is something on IT->stack, proceed
3973 with what is on the stack. This can be either another
3974 string, this time an overlay string, or a buffer. */
3975 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3976 && it->sp > 0)
3978 pop_it (it);
3979 if (!STRINGP (it->string))
3980 it->method = next_element_from_buffer;
3984 else if (it->method == next_element_from_image
3985 || it->method == next_element_from_stretch)
3987 /* The position etc with which we have to proceed are on
3988 the stack. The position may be at the end of a string,
3989 if the `display' property takes up the whole string. */
3990 pop_it (it);
3991 it->image_id = 0;
3992 if (STRINGP (it->string))
3994 it->method = next_element_from_string;
3995 goto consider_string_end;
3997 else
3998 it->method = next_element_from_buffer;
4000 else
4001 /* There are no other methods defined, so this should be a bug. */
4002 abort ();
4004 xassert (it->method != next_element_from_string
4005 || (STRINGP (it->string)
4006 && IT_STRING_CHARPOS (*it) >= 0));
4010 /* Load IT's display element fields with information about the next
4011 display element which comes from a display table entry or from the
4012 result of translating a control character to one of the forms `^C'
4013 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4015 static int
4016 next_element_from_display_vector (it)
4017 struct it *it;
4019 /* Precondition. */
4020 xassert (it->dpvec && it->current.dpvec_index >= 0);
4022 /* Remember the current face id in case glyphs specify faces.
4023 IT's face is restored in set_iterator_to_next. */
4024 it->saved_face_id = it->face_id;
4026 if (INTEGERP (*it->dpvec)
4027 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4029 int lface_id;
4030 GLYPH g;
4032 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4033 it->c = FAST_GLYPH_CHAR (g);
4034 it->len = CHAR_BYTES (it->c);
4036 /* The entry may contain a face id to use. Such a face id is
4037 the id of a Lisp face, not a realized face. A face id of
4038 zero means no face is specified. */
4039 lface_id = FAST_GLYPH_FACE (g);
4040 if (lface_id)
4042 /* The function returns -1 if lface_id is invalid. */
4043 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4044 if (face_id >= 0)
4045 it->face_id = face_id;
4048 else
4049 /* Display table entry is invalid. Return a space. */
4050 it->c = ' ', it->len = 1;
4052 /* Don't change position and object of the iterator here. They are
4053 still the values of the character that had this display table
4054 entry or was translated, and that's what we want. */
4055 it->what = IT_CHARACTER;
4056 return 1;
4060 /* Load IT with the next display element from Lisp string IT->string.
4061 IT->current.string_pos is the current position within the string.
4062 If IT->current.overlay_string_index >= 0, the Lisp string is an
4063 overlay string. */
4065 static int
4066 next_element_from_string (it)
4067 struct it *it;
4069 struct text_pos position;
4071 xassert (STRINGP (it->string));
4072 xassert (IT_STRING_CHARPOS (*it) >= 0);
4073 position = it->current.string_pos;
4075 /* Time to check for invisible text? */
4076 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4077 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4079 handle_stop (it);
4081 /* Since a handler may have changed IT->method, we must
4082 recurse here. */
4083 return get_next_display_element (it);
4086 if (it->current.overlay_string_index >= 0)
4088 /* Get the next character from an overlay string. In overlay
4089 strings, There is no field width or padding with spaces to
4090 do. */
4091 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4093 it->what = IT_EOB;
4094 return 0;
4096 else if (STRING_MULTIBYTE (it->string))
4098 int remaining = (STRING_BYTES (XSTRING (it->string))
4099 - IT_STRING_BYTEPOS (*it));
4100 unsigned char *s = (XSTRING (it->string)->data
4101 + IT_STRING_BYTEPOS (*it));
4102 it->c = string_char_and_length (s, remaining, &it->len);
4104 else
4106 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4107 it->len = 1;
4110 else
4112 /* Get the next character from a Lisp string that is not an
4113 overlay string. Such strings come from the mode line, for
4114 example. We may have to pad with spaces, or truncate the
4115 string. See also next_element_from_c_string. */
4116 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4118 it->what = IT_EOB;
4119 return 0;
4121 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4123 /* Pad with spaces. */
4124 it->c = ' ', it->len = 1;
4125 CHARPOS (position) = BYTEPOS (position) = -1;
4127 else if (STRING_MULTIBYTE (it->string))
4129 int maxlen = (STRING_BYTES (XSTRING (it->string))
4130 - IT_STRING_BYTEPOS (*it));
4131 unsigned char *s = (XSTRING (it->string)->data
4132 + IT_STRING_BYTEPOS (*it));
4133 it->c = string_char_and_length (s, maxlen, &it->len);
4135 else
4137 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4138 it->len = 1;
4142 /* Record what we have and where it came from. Note that we store a
4143 buffer position in IT->position although it could arguably be a
4144 string position. */
4145 it->what = IT_CHARACTER;
4146 it->object = it->string;
4147 it->position = position;
4148 return 1;
4152 /* Load IT with next display element from C string IT->s.
4153 IT->string_nchars is the maximum number of characters to return
4154 from the string. IT->end_charpos may be greater than
4155 IT->string_nchars when this function is called, in which case we
4156 may have to return padding spaces. Value is zero if end of string
4157 reached, including padding spaces. */
4159 static int
4160 next_element_from_c_string (it)
4161 struct it *it;
4163 int success_p = 1;
4165 xassert (it->s);
4166 it->what = IT_CHARACTER;
4167 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4168 it->object = Qnil;
4170 /* IT's position can be greater IT->string_nchars in case a field
4171 width or precision has been specified when the iterator was
4172 initialized. */
4173 if (IT_CHARPOS (*it) >= it->end_charpos)
4175 /* End of the game. */
4176 it->what = IT_EOB;
4177 success_p = 0;
4179 else if (IT_CHARPOS (*it) >= it->string_nchars)
4181 /* Pad with spaces. */
4182 it->c = ' ', it->len = 1;
4183 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4185 else if (it->multibyte_p)
4187 /* Implementation note: The calls to strlen apparently aren't a
4188 performance problem because there is no noticeable performance
4189 difference between Emacs running in unibyte or multibyte mode. */
4190 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4191 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4192 maxlen, &it->len);
4194 else
4195 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4197 return success_p;
4201 /* Set up IT to return characters from an ellipsis, if appropriate.
4202 The definition of the ellipsis glyphs may come from a display table
4203 entry. This function Fills IT with the first glyph from the
4204 ellipsis if an ellipsis is to be displayed. */
4206 static int
4207 next_element_from_ellipsis (it)
4208 struct it *it;
4210 if (it->selective_display_ellipsis_p)
4212 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4214 /* Use the display table definition for `...'. Invalid glyphs
4215 will be handled by the method returning elements from dpvec. */
4216 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4217 it->dpvec_char_len = it->len;
4218 it->dpvec = v->contents;
4219 it->dpend = v->contents + v->size;
4220 it->current.dpvec_index = 0;
4221 it->method = next_element_from_display_vector;
4223 else
4225 /* Use default `...' which is stored in default_invis_vector. */
4226 it->dpvec_char_len = it->len;
4227 it->dpvec = default_invis_vector;
4228 it->dpend = default_invis_vector + 3;
4229 it->current.dpvec_index = 0;
4230 it->method = next_element_from_display_vector;
4233 else
4235 it->method = next_element_from_buffer;
4236 reseat_at_next_visible_line_start (it, 1);
4239 return get_next_display_element (it);
4243 /* Deliver an image display element. The iterator IT is already
4244 filled with image information (done in handle_display_prop). Value
4245 is always 1. */
4248 static int
4249 next_element_from_image (it)
4250 struct it *it;
4252 it->what = IT_IMAGE;
4253 return 1;
4257 /* Fill iterator IT with next display element from a stretch glyph
4258 property. IT->object is the value of the text property. Value is
4259 always 1. */
4261 static int
4262 next_element_from_stretch (it)
4263 struct it *it;
4265 it->what = IT_STRETCH;
4266 return 1;
4270 /* Load IT with the next display element from current_buffer. Value
4271 is zero if end of buffer reached. IT->stop_charpos is the next
4272 position at which to stop and check for text properties or buffer
4273 end. */
4275 static int
4276 next_element_from_buffer (it)
4277 struct it *it;
4279 int success_p = 1;
4281 /* Check this assumption, otherwise, we would never enter the
4282 if-statement, below. */
4283 xassert (IT_CHARPOS (*it) >= BEGV
4284 && IT_CHARPOS (*it) <= it->stop_charpos);
4286 if (IT_CHARPOS (*it) >= it->stop_charpos)
4288 if (IT_CHARPOS (*it) >= it->end_charpos)
4290 int overlay_strings_follow_p;
4292 /* End of the game, except when overlay strings follow that
4293 haven't been returned yet. */
4294 if (it->overlay_strings_at_end_processed_p)
4295 overlay_strings_follow_p = 0;
4296 else
4298 it->overlay_strings_at_end_processed_p = 1;
4299 overlay_strings_follow_p = get_overlay_strings (it);
4302 if (overlay_strings_follow_p)
4303 success_p = get_next_display_element (it);
4304 else
4306 it->what = IT_EOB;
4307 it->position = it->current.pos;
4308 success_p = 0;
4311 else
4313 handle_stop (it);
4314 return get_next_display_element (it);
4317 else
4319 /* No face changes, overlays etc. in sight, so just return a
4320 character from current_buffer. */
4321 unsigned char *p;
4323 /* Maybe run the redisplay end trigger hook. Performance note:
4324 This doesn't seem to cost measurable time. */
4325 if (it->redisplay_end_trigger_charpos
4326 && it->glyph_row
4327 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4328 run_redisplay_end_trigger_hook (it);
4330 /* Get the next character, maybe multibyte. */
4331 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4332 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4334 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4335 - IT_BYTEPOS (*it));
4336 it->c = string_char_and_length (p, maxlen, &it->len);
4338 else
4339 it->c = *p, it->len = 1;
4341 /* Record what we have and where it came from. */
4342 it->what = IT_CHARACTER;;
4343 it->object = it->w->buffer;
4344 it->position = it->current.pos;
4346 /* Normally we return the character found above, except when we
4347 really want to return an ellipsis for selective display. */
4348 if (it->selective)
4350 if (it->c == '\n')
4352 /* A value of selective > 0 means hide lines indented more
4353 than that number of columns. */
4354 if (it->selective > 0
4355 && IT_CHARPOS (*it) + 1 < ZV
4356 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4357 IT_BYTEPOS (*it) + 1,
4358 it->selective))
4360 success_p = next_element_from_ellipsis (it);
4361 it->dpvec_char_len = -1;
4364 else if (it->c == '\r' && it->selective == -1)
4366 /* A value of selective == -1 means that everything from the
4367 CR to the end of the line is invisible, with maybe an
4368 ellipsis displayed for it. */
4369 success_p = next_element_from_ellipsis (it);
4370 it->dpvec_char_len = -1;
4375 /* Value is zero if end of buffer reached. */
4376 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4377 return success_p;
4381 /* Run the redisplay end trigger hook for IT. */
4383 static void
4384 run_redisplay_end_trigger_hook (it)
4385 struct it *it;
4387 Lisp_Object args[3];
4389 /* IT->glyph_row should be non-null, i.e. we should be actually
4390 displaying something, or otherwise we should not run the hook. */
4391 xassert (it->glyph_row);
4393 /* Set up hook arguments. */
4394 args[0] = Qredisplay_end_trigger_functions;
4395 args[1] = it->window;
4396 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4397 it->redisplay_end_trigger_charpos = 0;
4399 /* Since we are *trying* to run these functions, don't try to run
4400 them again, even if they get an error. */
4401 it->w->redisplay_end_trigger = Qnil;
4402 Frun_hook_with_args (3, args);
4404 /* Notice if it changed the face of the character we are on. */
4405 handle_face_prop (it);
4409 /* Deliver a composition display element. The iterator IT is already
4410 filled with composition information (done in
4411 handle_composition_prop). Value is always 1. */
4413 static int
4414 next_element_from_composition (it)
4415 struct it *it;
4417 it->what = IT_COMPOSITION;
4418 it->position = (STRINGP (it->string)
4419 ? it->current.string_pos
4420 : it->current.pos);
4421 return 1;
4426 /***********************************************************************
4427 Moving an iterator without producing glyphs
4428 ***********************************************************************/
4430 /* Move iterator IT to a specified buffer or X position within one
4431 line on the display without producing glyphs.
4433 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4434 whichever is reached first.
4436 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4438 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4439 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4440 TO_X includes the amount by which a window is horizontally
4441 scrolled.
4443 Value is
4445 MOVE_POS_MATCH_OR_ZV
4446 - when TO_POS or ZV was reached.
4448 MOVE_X_REACHED
4449 -when TO_X was reached before TO_POS or ZV were reached.
4451 MOVE_LINE_CONTINUED
4452 - when we reached the end of the display area and the line must
4453 be continued.
4455 MOVE_LINE_TRUNCATED
4456 - when we reached the end of the display area and the line is
4457 truncated.
4459 MOVE_NEWLINE_OR_CR
4460 - when we stopped at a line end, i.e. a newline or a CR and selective
4461 display is on. */
4463 static enum move_it_result
4464 move_it_in_display_line_to (it, to_charpos, to_x, op)
4465 struct it *it;
4466 int to_charpos, to_x, op;
4468 enum move_it_result result = MOVE_UNDEFINED;
4469 struct glyph_row *saved_glyph_row;
4471 /* Don't produce glyphs in produce_glyphs. */
4472 saved_glyph_row = it->glyph_row;
4473 it->glyph_row = NULL;
4475 while (1)
4477 int x, i, ascent = 0, descent = 0;
4479 /* Stop when ZV or TO_CHARPOS reached. */
4480 if (!get_next_display_element (it)
4481 || ((op & MOVE_TO_POS) != 0
4482 && BUFFERP (it->object)
4483 && IT_CHARPOS (*it) >= to_charpos))
4485 result = MOVE_POS_MATCH_OR_ZV;
4486 break;
4489 /* The call to produce_glyphs will get the metrics of the
4490 display element IT is loaded with. We record in x the
4491 x-position before this display element in case it does not
4492 fit on the line. */
4493 x = it->current_x;
4495 /* Remember the line height so far in case the next element doesn't
4496 fit on the line. */
4497 if (!it->truncate_lines_p)
4499 ascent = it->max_ascent;
4500 descent = it->max_descent;
4503 PRODUCE_GLYPHS (it);
4505 if (it->area != TEXT_AREA)
4507 set_iterator_to_next (it, 1);
4508 continue;
4511 /* The number of glyphs we get back in IT->nglyphs will normally
4512 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4513 character on a terminal frame, or (iii) a line end. For the
4514 second case, IT->nglyphs - 1 padding glyphs will be present
4515 (on X frames, there is only one glyph produced for a
4516 composite character.
4518 The behavior implemented below means, for continuation lines,
4519 that as many spaces of a TAB as fit on the current line are
4520 displayed there. For terminal frames, as many glyphs of a
4521 multi-glyph character are displayed in the current line, too.
4522 This is what the old redisplay code did, and we keep it that
4523 way. Under X, the whole shape of a complex character must
4524 fit on the line or it will be completely displayed in the
4525 next line.
4527 Note that both for tabs and padding glyphs, all glyphs have
4528 the same width. */
4529 if (it->nglyphs)
4531 /* More than one glyph or glyph doesn't fit on line. All
4532 glyphs have the same width. */
4533 int single_glyph_width = it->pixel_width / it->nglyphs;
4534 int new_x;
4536 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4538 new_x = x + single_glyph_width;
4540 /* We want to leave anything reaching TO_X to the caller. */
4541 if ((op & MOVE_TO_X) && new_x > to_x)
4543 it->current_x = x;
4544 result = MOVE_X_REACHED;
4545 break;
4547 else if (/* Lines are continued. */
4548 !it->truncate_lines_p
4549 && (/* And glyph doesn't fit on the line. */
4550 new_x > it->last_visible_x
4551 /* Or it fits exactly and we're on a window
4552 system frame. */
4553 || (new_x == it->last_visible_x
4554 && FRAME_WINDOW_P (it->f))))
4556 if (/* IT->hpos == 0 means the very first glyph
4557 doesn't fit on the line, e.g. a wide image. */
4558 it->hpos == 0
4559 || (new_x == it->last_visible_x
4560 && FRAME_WINDOW_P (it->f)))
4562 ++it->hpos;
4563 it->current_x = new_x;
4564 if (i == it->nglyphs - 1)
4565 set_iterator_to_next (it, 1);
4567 else
4569 it->current_x = x;
4570 it->max_ascent = ascent;
4571 it->max_descent = descent;
4574 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4575 IT_CHARPOS (*it)));
4576 result = MOVE_LINE_CONTINUED;
4577 break;
4579 else if (new_x > it->first_visible_x)
4581 /* Glyph is visible. Increment number of glyphs that
4582 would be displayed. */
4583 ++it->hpos;
4585 else
4587 /* Glyph is completely off the left margin of the display
4588 area. Nothing to do. */
4592 if (result != MOVE_UNDEFINED)
4593 break;
4595 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4597 /* Stop when TO_X specified and reached. This check is
4598 necessary here because of lines consisting of a line end,
4599 only. The line end will not produce any glyphs and we
4600 would never get MOVE_X_REACHED. */
4601 xassert (it->nglyphs == 0);
4602 result = MOVE_X_REACHED;
4603 break;
4606 /* Is this a line end? If yes, we're done. */
4607 if (ITERATOR_AT_END_OF_LINE_P (it))
4609 result = MOVE_NEWLINE_OR_CR;
4610 break;
4613 /* The current display element has been consumed. Advance
4614 to the next. */
4615 set_iterator_to_next (it, 1);
4617 /* Stop if lines are truncated and IT's current x-position is
4618 past the right edge of the window now. */
4619 if (it->truncate_lines_p
4620 && it->current_x >= it->last_visible_x)
4622 result = MOVE_LINE_TRUNCATED;
4623 break;
4627 /* Restore the iterator settings altered at the beginning of this
4628 function. */
4629 it->glyph_row = saved_glyph_row;
4630 return result;
4634 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4635 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4636 the description of enum move_operation_enum.
4638 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4639 screen line, this function will set IT to the next position >
4640 TO_CHARPOS. */
4642 void
4643 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4644 struct it *it;
4645 int to_charpos, to_x, to_y, to_vpos;
4646 int op;
4648 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4649 int line_height;
4650 int reached = 0;
4652 for (;;)
4654 if (op & MOVE_TO_VPOS)
4656 /* If no TO_CHARPOS and no TO_X specified, stop at the
4657 start of the line TO_VPOS. */
4658 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4660 if (it->vpos == to_vpos)
4662 reached = 1;
4663 break;
4665 else
4666 skip = move_it_in_display_line_to (it, -1, -1, 0);
4668 else
4670 /* TO_VPOS >= 0 means stop at TO_X in the line at
4671 TO_VPOS, or at TO_POS, whichever comes first. */
4672 if (it->vpos == to_vpos)
4674 reached = 2;
4675 break;
4678 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4680 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4682 reached = 3;
4683 break;
4685 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4687 /* We have reached TO_X but not in the line we want. */
4688 skip = move_it_in_display_line_to (it, to_charpos,
4689 -1, MOVE_TO_POS);
4690 if (skip == MOVE_POS_MATCH_OR_ZV)
4692 reached = 4;
4693 break;
4698 else if (op & MOVE_TO_Y)
4700 struct it it_backup;
4702 /* TO_Y specified means stop at TO_X in the line containing
4703 TO_Y---or at TO_CHARPOS if this is reached first. The
4704 problem is that we can't really tell whether the line
4705 contains TO_Y before we have completely scanned it, and
4706 this may skip past TO_X. What we do is to first scan to
4707 TO_X.
4709 If TO_X is not specified, use a TO_X of zero. The reason
4710 is to make the outcome of this function more predictable.
4711 If we didn't use TO_X == 0, we would stop at the end of
4712 the line which is probably not what a caller would expect
4713 to happen. */
4714 skip = move_it_in_display_line_to (it, to_charpos,
4715 ((op & MOVE_TO_X)
4716 ? to_x : 0),
4717 (MOVE_TO_X
4718 | (op & MOVE_TO_POS)));
4720 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4721 if (skip == MOVE_POS_MATCH_OR_ZV)
4723 reached = 5;
4724 break;
4727 /* If TO_X was reached, we would like to know whether TO_Y
4728 is in the line. This can only be said if we know the
4729 total line height which requires us to scan the rest of
4730 the line. */
4731 if (skip == MOVE_X_REACHED)
4733 it_backup = *it;
4734 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4735 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4736 op & MOVE_TO_POS);
4737 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4740 /* Now, decide whether TO_Y is in this line. */
4741 line_height = it->max_ascent + it->max_descent;
4742 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4744 if (to_y >= it->current_y
4745 && to_y < it->current_y + line_height)
4747 if (skip == MOVE_X_REACHED)
4748 /* If TO_Y is in this line and TO_X was reached above,
4749 we scanned too far. We have to restore IT's settings
4750 to the ones before skipping. */
4751 *it = it_backup;
4752 reached = 6;
4754 else if (skip == MOVE_X_REACHED)
4756 skip = skip2;
4757 if (skip == MOVE_POS_MATCH_OR_ZV)
4758 reached = 7;
4761 if (reached)
4762 break;
4764 else
4765 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4767 switch (skip)
4769 case MOVE_POS_MATCH_OR_ZV:
4770 reached = 8;
4771 goto out;
4773 case MOVE_NEWLINE_OR_CR:
4774 set_iterator_to_next (it, 1);
4775 it->continuation_lines_width = 0;
4776 break;
4778 case MOVE_LINE_TRUNCATED:
4779 it->continuation_lines_width = 0;
4780 reseat_at_next_visible_line_start (it, 0);
4781 if ((op & MOVE_TO_POS) != 0
4782 && IT_CHARPOS (*it) > to_charpos)
4784 reached = 9;
4785 goto out;
4787 break;
4789 case MOVE_LINE_CONTINUED:
4790 it->continuation_lines_width += it->current_x;
4791 break;
4793 default:
4794 abort ();
4797 /* Reset/increment for the next run. */
4798 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4799 it->current_x = it->hpos = 0;
4800 it->current_y += it->max_ascent + it->max_descent;
4801 ++it->vpos;
4802 last_height = it->max_ascent + it->max_descent;
4803 last_max_ascent = it->max_ascent;
4804 it->max_ascent = it->max_descent = 0;
4807 out:
4809 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4813 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4815 If DY > 0, move IT backward at least that many pixels. DY = 0
4816 means move IT backward to the preceding line start or BEGV. This
4817 function may move over more than DY pixels if IT->current_y - DY
4818 ends up in the middle of a line; in this case IT->current_y will be
4819 set to the top of the line moved to. */
4821 void
4822 move_it_vertically_backward (it, dy)
4823 struct it *it;
4824 int dy;
4826 int nlines, h, line_height;
4827 struct it it2;
4828 int start_pos = IT_CHARPOS (*it);
4830 xassert (dy >= 0);
4832 /* Estimate how many newlines we must move back. */
4833 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4835 /* Set the iterator's position that many lines back. */
4836 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4837 back_to_previous_visible_line_start (it);
4839 /* Reseat the iterator here. When moving backward, we don't want
4840 reseat to skip forward over invisible text, set up the iterator
4841 to deliver from overlay strings at the new position etc. So,
4842 use reseat_1 here. */
4843 reseat_1 (it, it->current.pos, 1);
4845 /* We are now surely at a line start. */
4846 it->current_x = it->hpos = 0;
4848 /* Move forward and see what y-distance we moved. First move to the
4849 start of the next line so that we get its height. We need this
4850 height to be able to tell whether we reached the specified
4851 y-distance. */
4852 it2 = *it;
4853 it2.max_ascent = it2.max_descent = 0;
4854 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4855 MOVE_TO_POS | MOVE_TO_VPOS);
4856 xassert (IT_CHARPOS (*it) >= BEGV);
4857 line_height = it2.max_ascent + it2.max_descent;
4859 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4860 xassert (IT_CHARPOS (*it) >= BEGV);
4861 h = it2.current_y - it->current_y;
4862 nlines = it2.vpos - it->vpos;
4864 /* Correct IT's y and vpos position. */
4865 it->vpos -= nlines;
4866 it->current_y -= h;
4868 if (dy == 0)
4870 /* DY == 0 means move to the start of the screen line. The
4871 value of nlines is > 0 if continuation lines were involved. */
4872 if (nlines > 0)
4873 move_it_by_lines (it, nlines, 1);
4874 xassert (IT_CHARPOS (*it) <= start_pos);
4876 else if (nlines)
4878 /* The y-position we try to reach. Note that h has been
4879 subtracted in front of the if-statement. */
4880 int target_y = it->current_y + h - dy;
4882 /* If we did not reach target_y, try to move further backward if
4883 we can. If we moved too far backward, try to move forward. */
4884 if (target_y < it->current_y
4885 && IT_CHARPOS (*it) > BEGV)
4887 move_it_vertically (it, target_y - it->current_y);
4888 xassert (IT_CHARPOS (*it) >= BEGV);
4890 else if (target_y >= it->current_y + line_height
4891 && IT_CHARPOS (*it) < ZV)
4893 move_it_vertically (it, target_y - (it->current_y + line_height));
4894 xassert (IT_CHARPOS (*it) >= BEGV);
4900 /* Move IT by a specified amount of pixel lines DY. DY negative means
4901 move backwards. DY = 0 means move to start of screen line. At the
4902 end, IT will be on the start of a screen line. */
4904 void
4905 move_it_vertically (it, dy)
4906 struct it *it;
4907 int dy;
4909 if (dy <= 0)
4910 move_it_vertically_backward (it, -dy);
4911 else if (dy > 0)
4913 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4914 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4915 MOVE_TO_POS | MOVE_TO_Y);
4916 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4918 /* If buffer ends in ZV without a newline, move to the start of
4919 the line to satisfy the post-condition. */
4920 if (IT_CHARPOS (*it) == ZV
4921 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4922 move_it_by_lines (it, 0, 0);
4927 /* Return non-zero if some text between buffer positions START_CHARPOS
4928 and END_CHARPOS is invisible. IT->window is the window for text
4929 property lookup. */
4931 static int
4932 invisible_text_between_p (it, start_charpos, end_charpos)
4933 struct it *it;
4934 int start_charpos, end_charpos;
4936 Lisp_Object prop, limit;
4937 int invisible_found_p;
4939 xassert (it != NULL && start_charpos <= end_charpos);
4941 /* Is text at START invisible? */
4942 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4943 it->window);
4944 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4945 invisible_found_p = 1;
4946 else
4948 limit = Fnext_single_char_property_change (make_number (start_charpos),
4949 Qinvisible, Qnil,
4950 make_number (end_charpos));
4951 invisible_found_p = XFASTINT (limit) < end_charpos;
4954 return invisible_found_p;
4958 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4959 negative means move up. DVPOS == 0 means move to the start of the
4960 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4961 NEED_Y_P is zero, IT->current_y will be left unchanged.
4963 Further optimization ideas: If we would know that IT->f doesn't use
4964 a face with proportional font, we could be faster for
4965 truncate-lines nil. */
4967 void
4968 move_it_by_lines (it, dvpos, need_y_p)
4969 struct it *it;
4970 int dvpos, need_y_p;
4972 struct position pos;
4974 if (!FRAME_WINDOW_P (it->f))
4976 struct text_pos textpos;
4978 /* We can use vmotion on frames without proportional fonts. */
4979 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4980 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4981 reseat (it, textpos, 1);
4982 it->vpos += pos.vpos;
4983 it->current_y += pos.vpos;
4985 else if (dvpos == 0)
4987 /* DVPOS == 0 means move to the start of the screen line. */
4988 move_it_vertically_backward (it, 0);
4989 xassert (it->current_x == 0 && it->hpos == 0);
4991 else if (dvpos > 0)
4993 /* If there are no continuation lines, and if there is no
4994 selective display, try the simple method of moving forward
4995 DVPOS newlines, then see where we are. */
4996 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4998 int shortage = 0, charpos;
5000 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
5001 charpos = IT_CHARPOS (*it) + 1;
5002 else
5003 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5004 &shortage, 0);
5006 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5008 struct text_pos pos;
5009 CHARPOS (pos) = charpos;
5010 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5011 reseat (it, pos, 1);
5012 it->vpos += dvpos - shortage;
5013 it->hpos = it->current_x = 0;
5014 return;
5018 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5020 else
5022 struct it it2;
5023 int start_charpos, i;
5025 /* If there are no continuation lines, and if there is no
5026 selective display, try the simple method of moving backward
5027 -DVPOS newlines. */
5028 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5030 int shortage;
5031 int charpos = IT_CHARPOS (*it);
5032 int bytepos = IT_BYTEPOS (*it);
5034 /* If in the middle of a line, go to its start. */
5035 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5037 charpos = find_next_newline_no_quit (charpos, -1);
5038 bytepos = CHAR_TO_BYTE (charpos);
5041 if (charpos == BEGV)
5043 struct text_pos pos;
5044 CHARPOS (pos) = charpos;
5045 BYTEPOS (pos) = bytepos;
5046 reseat (it, pos, 1);
5047 it->hpos = it->current_x = 0;
5048 return;
5050 else
5052 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5053 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5055 struct text_pos pos;
5056 CHARPOS (pos) = charpos;
5057 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5058 reseat (it, pos, 1);
5059 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5060 it->hpos = it->current_x = 0;
5061 return;
5066 /* Go back -DVPOS visible lines and reseat the iterator there. */
5067 start_charpos = IT_CHARPOS (*it);
5068 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5069 back_to_previous_visible_line_start (it);
5070 reseat (it, it->current.pos, 1);
5071 it->current_x = it->hpos = 0;
5073 /* Above call may have moved too far if continuation lines
5074 are involved. Scan forward and see if it did. */
5075 it2 = *it;
5076 it2.vpos = it2.current_y = 0;
5077 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5078 it->vpos -= it2.vpos;
5079 it->current_y -= it2.current_y;
5080 it->current_x = it->hpos = 0;
5082 /* If we moved too far, move IT some lines forward. */
5083 if (it2.vpos > -dvpos)
5085 int delta = it2.vpos + dvpos;
5086 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5093 /***********************************************************************
5094 Messages
5095 ***********************************************************************/
5098 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5099 to *Messages*. */
5101 void
5102 add_to_log (format, arg1, arg2)
5103 char *format;
5104 Lisp_Object arg1, arg2;
5106 Lisp_Object args[3];
5107 Lisp_Object msg, fmt;
5108 char *buffer;
5109 int len;
5110 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5112 fmt = msg = Qnil;
5113 GCPRO4 (fmt, msg, arg1, arg2);
5115 args[0] = fmt = build_string (format);
5116 args[1] = arg1;
5117 args[2] = arg2;
5118 msg = Fformat (3, args);
5120 len = STRING_BYTES (XSTRING (msg)) + 1;
5121 buffer = (char *) alloca (len);
5122 strcpy (buffer, XSTRING (msg)->data);
5124 message_dolog (buffer, len - 1, 1, 0);
5125 UNGCPRO;
5129 /* Output a newline in the *Messages* buffer if "needs" one. */
5131 void
5132 message_log_maybe_newline ()
5134 if (message_log_need_newline)
5135 message_dolog ("", 0, 1, 0);
5139 /* Add a string M of length LEN to the message log, optionally
5140 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5141 nonzero, means interpret the contents of M as multibyte. This
5142 function calls low-level routines in order to bypass text property
5143 hooks, etc. which might not be safe to run. */
5145 void
5146 message_dolog (m, len, nlflag, multibyte)
5147 char *m;
5148 int len, nlflag, multibyte;
5150 if (!NILP (Vmessage_log_max))
5152 struct buffer *oldbuf;
5153 Lisp_Object oldpoint, oldbegv, oldzv;
5154 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5155 int point_at_end = 0;
5156 int zv_at_end = 0;
5157 Lisp_Object old_deactivate_mark, tem;
5158 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5160 old_deactivate_mark = Vdeactivate_mark;
5161 oldbuf = current_buffer;
5162 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5163 current_buffer->undo_list = Qt;
5165 oldpoint = Fpoint_marker ();
5166 oldbegv = Fpoint_min_marker ();
5167 oldzv = Fpoint_max_marker ();
5168 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5170 if (PT == Z)
5171 point_at_end = 1;
5172 if (ZV == Z)
5173 zv_at_end = 1;
5175 BEGV = BEG;
5176 BEGV_BYTE = BEG_BYTE;
5177 ZV = Z;
5178 ZV_BYTE = Z_BYTE;
5179 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5181 /* Insert the string--maybe converting multibyte to single byte
5182 or vice versa, so that all the text fits the buffer. */
5183 if (multibyte
5184 && NILP (current_buffer->enable_multibyte_characters))
5186 int i, c, nbytes;
5187 unsigned char work[1];
5189 /* Convert a multibyte string to single-byte
5190 for the *Message* buffer. */
5191 for (i = 0; i < len; i += nbytes)
5193 c = string_char_and_length (m + i, len - i, &nbytes);
5194 work[0] = (SINGLE_BYTE_CHAR_P (c)
5196 : multibyte_char_to_unibyte (c, Qnil));
5197 insert_1_both (work, 1, 1, 1, 0, 0);
5200 else if (! multibyte
5201 && ! NILP (current_buffer->enable_multibyte_characters))
5203 int i, c, nbytes;
5204 unsigned char *msg = (unsigned char *) m;
5205 unsigned char str[MAX_MULTIBYTE_LENGTH];
5206 /* Convert a single-byte string to multibyte
5207 for the *Message* buffer. */
5208 for (i = 0; i < len; i++)
5210 c = unibyte_char_to_multibyte (msg[i]);
5211 nbytes = CHAR_STRING (c, str);
5212 insert_1_both (str, 1, nbytes, 1, 0, 0);
5215 else if (len)
5216 insert_1 (m, len, 1, 0, 0);
5218 if (nlflag)
5220 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5221 insert_1 ("\n", 1, 1, 0, 0);
5223 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5224 this_bol = PT;
5225 this_bol_byte = PT_BYTE;
5227 if (this_bol > BEG)
5229 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5230 prev_bol = PT;
5231 prev_bol_byte = PT_BYTE;
5233 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5234 this_bol, this_bol_byte);
5235 if (dup)
5237 del_range_both (prev_bol, prev_bol_byte,
5238 this_bol, this_bol_byte, 0);
5239 if (dup > 1)
5241 char dupstr[40];
5242 int duplen;
5244 /* If you change this format, don't forget to also
5245 change message_log_check_duplicate. */
5246 sprintf (dupstr, " [%d times]", dup);
5247 duplen = strlen (dupstr);
5248 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5249 insert_1 (dupstr, duplen, 1, 0, 1);
5254 if (NATNUMP (Vmessage_log_max))
5256 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5257 -XFASTINT (Vmessage_log_max) - 1, 0);
5258 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5261 BEGV = XMARKER (oldbegv)->charpos;
5262 BEGV_BYTE = marker_byte_position (oldbegv);
5264 if (zv_at_end)
5266 ZV = Z;
5267 ZV_BYTE = Z_BYTE;
5269 else
5271 ZV = XMARKER (oldzv)->charpos;
5272 ZV_BYTE = marker_byte_position (oldzv);
5275 if (point_at_end)
5276 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5277 else
5278 /* We can't do Fgoto_char (oldpoint) because it will run some
5279 Lisp code. */
5280 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5281 XMARKER (oldpoint)->bytepos);
5283 UNGCPRO;
5284 free_marker (oldpoint);
5285 free_marker (oldbegv);
5286 free_marker (oldzv);
5288 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5289 set_buffer_internal (oldbuf);
5290 if (NILP (tem))
5291 windows_or_buffers_changed = old_windows_or_buffers_changed;
5292 message_log_need_newline = !nlflag;
5293 Vdeactivate_mark = old_deactivate_mark;
5298 /* We are at the end of the buffer after just having inserted a newline.
5299 (Note: We depend on the fact we won't be crossing the gap.)
5300 Check to see if the most recent message looks a lot like the previous one.
5301 Return 0 if different, 1 if the new one should just replace it, or a
5302 value N > 1 if we should also append " [N times]". */
5304 static int
5305 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5306 int prev_bol, this_bol;
5307 int prev_bol_byte, this_bol_byte;
5309 int i;
5310 int len = Z_BYTE - 1 - this_bol_byte;
5311 int seen_dots = 0;
5312 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5313 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5315 for (i = 0; i < len; i++)
5317 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5318 && p1[i] != '\n')
5319 seen_dots = 1;
5320 if (p1[i] != p2[i])
5321 return seen_dots;
5323 p1 += len;
5324 if (*p1 == '\n')
5325 return 2;
5326 if (*p1++ == ' ' && *p1++ == '[')
5328 int n = 0;
5329 while (*p1 >= '0' && *p1 <= '9')
5330 n = n * 10 + *p1++ - '0';
5331 if (strncmp (p1, " times]\n", 8) == 0)
5332 return n+1;
5334 return 0;
5338 /* Display an echo area message M with a specified length of LEN
5339 chars. The string may include null characters. If M is 0, clear
5340 out any existing message, and let the mini-buffer text show through.
5342 The buffer M must continue to exist until after the echo area gets
5343 cleared or some other message gets displayed there. This means do
5344 not pass text that is stored in a Lisp string; do not pass text in
5345 a buffer that was alloca'd. */
5347 void
5348 message2 (m, len, multibyte)
5349 char *m;
5350 int len;
5351 int multibyte;
5353 /* First flush out any partial line written with print. */
5354 message_log_maybe_newline ();
5355 if (m)
5356 message_dolog (m, len, 1, multibyte);
5357 message2_nolog (m, len, multibyte);
5361 /* The non-logging counterpart of message2. */
5363 void
5364 message2_nolog (m, len, multibyte)
5365 char *m;
5366 int len;
5368 struct frame *sf = SELECTED_FRAME ();
5369 message_enable_multibyte = multibyte;
5371 if (noninteractive)
5373 if (noninteractive_need_newline)
5374 putc ('\n', stderr);
5375 noninteractive_need_newline = 0;
5376 if (m)
5377 fwrite (m, len, 1, stderr);
5378 if (cursor_in_echo_area == 0)
5379 fprintf (stderr, "\n");
5380 fflush (stderr);
5382 /* A null message buffer means that the frame hasn't really been
5383 initialized yet. Error messages get reported properly by
5384 cmd_error, so this must be just an informative message; toss it. */
5385 else if (INTERACTIVE
5386 && sf->glyphs_initialized_p
5387 && FRAME_MESSAGE_BUF (sf))
5389 Lisp_Object mini_window;
5390 struct frame *f;
5392 /* Get the frame containing the mini-buffer
5393 that the selected frame is using. */
5394 mini_window = FRAME_MINIBUF_WINDOW (sf);
5395 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5397 FRAME_SAMPLE_VISIBILITY (f);
5398 if (FRAME_VISIBLE_P (sf)
5399 && ! FRAME_VISIBLE_P (f))
5400 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5402 if (m)
5404 set_message (m, Qnil, len, multibyte);
5405 if (minibuffer_auto_raise)
5406 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5408 else
5409 clear_message (1, 1);
5411 do_pending_window_change (0);
5412 echo_area_display (1);
5413 do_pending_window_change (0);
5414 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5415 (*frame_up_to_date_hook) (f);
5420 /* Display an echo area message M with a specified length of NBYTES
5421 bytes. The string may include null characters. If M is not a
5422 string, clear out any existing message, and let the mini-buffer
5423 text show through. */
5425 void
5426 message3 (m, nbytes, multibyte)
5427 Lisp_Object m;
5428 int nbytes;
5429 int multibyte;
5431 struct gcpro gcpro1;
5433 GCPRO1 (m);
5435 /* First flush out any partial line written with print. */
5436 message_log_maybe_newline ();
5437 if (STRINGP (m))
5438 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5439 message3_nolog (m, nbytes, multibyte);
5441 UNGCPRO;
5445 /* The non-logging version of message3. */
5447 void
5448 message3_nolog (m, nbytes, multibyte)
5449 Lisp_Object m;
5450 int nbytes, multibyte;
5452 struct frame *sf = SELECTED_FRAME ();
5453 message_enable_multibyte = multibyte;
5455 if (noninteractive)
5457 if (noninteractive_need_newline)
5458 putc ('\n', stderr);
5459 noninteractive_need_newline = 0;
5460 if (STRINGP (m))
5461 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5462 if (cursor_in_echo_area == 0)
5463 fprintf (stderr, "\n");
5464 fflush (stderr);
5466 /* A null message buffer means that the frame hasn't really been
5467 initialized yet. Error messages get reported properly by
5468 cmd_error, so this must be just an informative message; toss it. */
5469 else if (INTERACTIVE
5470 && sf->glyphs_initialized_p
5471 && FRAME_MESSAGE_BUF (sf))
5473 Lisp_Object mini_window;
5474 Lisp_Object frame;
5475 struct frame *f;
5477 /* Get the frame containing the mini-buffer
5478 that the selected frame is using. */
5479 mini_window = FRAME_MINIBUF_WINDOW (sf);
5480 frame = XWINDOW (mini_window)->frame;
5481 f = XFRAME (frame);
5483 FRAME_SAMPLE_VISIBILITY (f);
5484 if (FRAME_VISIBLE_P (sf)
5485 && !FRAME_VISIBLE_P (f))
5486 Fmake_frame_visible (frame);
5488 if (STRINGP (m) && XSTRING (m)->size)
5490 set_message (NULL, m, nbytes, multibyte);
5491 if (minibuffer_auto_raise)
5492 Fraise_frame (frame);
5494 else
5495 clear_message (1, 1);
5497 do_pending_window_change (0);
5498 echo_area_display (1);
5499 do_pending_window_change (0);
5500 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5501 (*frame_up_to_date_hook) (f);
5506 /* Display a null-terminated echo area message M. If M is 0, clear
5507 out any existing message, and let the mini-buffer text show through.
5509 The buffer M must continue to exist until after the echo area gets
5510 cleared or some other message gets displayed there. Do not pass
5511 text that is stored in a Lisp string. Do not pass text in a buffer
5512 that was alloca'd. */
5514 void
5515 message1 (m)
5516 char *m;
5518 message2 (m, (m ? strlen (m) : 0), 0);
5522 /* The non-logging counterpart of message1. */
5524 void
5525 message1_nolog (m)
5526 char *m;
5528 message2_nolog (m, (m ? strlen (m) : 0), 0);
5531 /* Display a message M which contains a single %s
5532 which gets replaced with STRING. */
5534 void
5535 message_with_string (m, string, log)
5536 char *m;
5537 Lisp_Object string;
5538 int log;
5540 if (noninteractive)
5542 if (m)
5544 if (noninteractive_need_newline)
5545 putc ('\n', stderr);
5546 noninteractive_need_newline = 0;
5547 fprintf (stderr, m, XSTRING (string)->data);
5548 if (cursor_in_echo_area == 0)
5549 fprintf (stderr, "\n");
5550 fflush (stderr);
5553 else if (INTERACTIVE)
5555 /* The frame whose minibuffer we're going to display the message on.
5556 It may be larger than the selected frame, so we need
5557 to use its buffer, not the selected frame's buffer. */
5558 Lisp_Object mini_window;
5559 struct frame *f, *sf = SELECTED_FRAME ();
5561 /* Get the frame containing the minibuffer
5562 that the selected frame is using. */
5563 mini_window = FRAME_MINIBUF_WINDOW (sf);
5564 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5566 /* A null message buffer means that the frame hasn't really been
5567 initialized yet. Error messages get reported properly by
5568 cmd_error, so this must be just an informative message; toss it. */
5569 if (FRAME_MESSAGE_BUF (f))
5571 int len;
5572 char *a[1];
5573 a[0] = (char *) XSTRING (string)->data;
5575 len = doprnt (FRAME_MESSAGE_BUF (f),
5576 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5578 if (log)
5579 message2 (FRAME_MESSAGE_BUF (f), len,
5580 STRING_MULTIBYTE (string));
5581 else
5582 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5583 STRING_MULTIBYTE (string));
5585 /* Print should start at the beginning of the message
5586 buffer next time. */
5587 message_buf_print = 0;
5593 /* Dump an informative message to the minibuf. If M is 0, clear out
5594 any existing message, and let the mini-buffer text show through. */
5596 /* VARARGS 1 */
5597 void
5598 message (m, a1, a2, a3)
5599 char *m;
5600 EMACS_INT a1, a2, a3;
5602 if (noninteractive)
5604 if (m)
5606 if (noninteractive_need_newline)
5607 putc ('\n', stderr);
5608 noninteractive_need_newline = 0;
5609 fprintf (stderr, m, a1, a2, a3);
5610 if (cursor_in_echo_area == 0)
5611 fprintf (stderr, "\n");
5612 fflush (stderr);
5615 else if (INTERACTIVE)
5617 /* The frame whose mini-buffer we're going to display the message
5618 on. It may be larger than the selected frame, so we need to
5619 use its buffer, not the selected frame's buffer. */
5620 Lisp_Object mini_window;
5621 struct frame *f, *sf = SELECTED_FRAME ();
5623 /* Get the frame containing the mini-buffer
5624 that the selected frame is using. */
5625 mini_window = FRAME_MINIBUF_WINDOW (sf);
5626 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5628 /* A null message buffer means that the frame hasn't really been
5629 initialized yet. Error messages get reported properly by
5630 cmd_error, so this must be just an informative message; toss
5631 it. */
5632 if (FRAME_MESSAGE_BUF (f))
5634 if (m)
5636 int len;
5637 #ifdef NO_ARG_ARRAY
5638 char *a[3];
5639 a[0] = (char *) a1;
5640 a[1] = (char *) a2;
5641 a[2] = (char *) a3;
5643 len = doprnt (FRAME_MESSAGE_BUF (f),
5644 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5645 #else
5646 len = doprnt (FRAME_MESSAGE_BUF (f),
5647 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5648 (char **) &a1);
5649 #endif /* NO_ARG_ARRAY */
5651 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5653 else
5654 message1 (0);
5656 /* Print should start at the beginning of the message
5657 buffer next time. */
5658 message_buf_print = 0;
5664 /* The non-logging version of message. */
5666 void
5667 message_nolog (m, a1, a2, a3)
5668 char *m;
5669 EMACS_INT a1, a2, a3;
5671 Lisp_Object old_log_max;
5672 old_log_max = Vmessage_log_max;
5673 Vmessage_log_max = Qnil;
5674 message (m, a1, a2, a3);
5675 Vmessage_log_max = old_log_max;
5679 /* Display the current message in the current mini-buffer. This is
5680 only called from error handlers in process.c, and is not time
5681 critical. */
5683 void
5684 update_echo_area ()
5686 if (!NILP (echo_area_buffer[0]))
5688 Lisp_Object string;
5689 string = Fcurrent_message ();
5690 message3 (string, XSTRING (string)->size,
5691 !NILP (current_buffer->enable_multibyte_characters));
5696 /* Make sure echo area buffers in echo_buffers[] are life. If they
5697 aren't, make new ones. */
5699 static void
5700 ensure_echo_area_buffers ()
5702 int i;
5704 for (i = 0; i < 2; ++i)
5705 if (!BUFFERP (echo_buffer[i])
5706 || NILP (XBUFFER (echo_buffer[i])->name))
5708 char name[30];
5709 Lisp_Object old_buffer;
5710 int j;
5712 old_buffer = echo_buffer[i];
5713 sprintf (name, " *Echo Area %d*", i);
5714 echo_buffer[i] = Fget_buffer_create (build_string (name));
5715 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5717 for (j = 0; j < 2; ++j)
5718 if (EQ (old_buffer, echo_area_buffer[j]))
5719 echo_area_buffer[j] = echo_buffer[i];
5724 /* Call FN with args A1..A4 with either the current or last displayed
5725 echo_area_buffer as current buffer.
5727 WHICH zero means use the current message buffer
5728 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5729 from echo_buffer[] and clear it.
5731 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5732 suitable buffer from echo_buffer[] and clear it.
5734 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5735 that the current message becomes the last displayed one, make
5736 choose a suitable buffer for echo_area_buffer[0], and clear it.
5738 Value is what FN returns. */
5740 static int
5741 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5742 struct window *w;
5743 int which;
5744 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5745 EMACS_INT a1;
5746 Lisp_Object a2;
5747 EMACS_INT a3, a4;
5749 Lisp_Object buffer;
5750 int this_one, the_other, clear_buffer_p, rc;
5751 int count = specpdl_ptr - specpdl;
5753 /* If buffers aren't life, make new ones. */
5754 ensure_echo_area_buffers ();
5756 clear_buffer_p = 0;
5758 if (which == 0)
5759 this_one = 0, the_other = 1;
5760 else if (which > 0)
5761 this_one = 1, the_other = 0;
5762 else
5764 this_one = 0, the_other = 1;
5765 clear_buffer_p = 1;
5767 /* We need a fresh one in case the current echo buffer equals
5768 the one containing the last displayed echo area message. */
5769 if (!NILP (echo_area_buffer[this_one])
5770 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5771 echo_area_buffer[this_one] = Qnil;
5774 /* Choose a suitable buffer from echo_buffer[] is we don't
5775 have one. */
5776 if (NILP (echo_area_buffer[this_one]))
5778 echo_area_buffer[this_one]
5779 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5780 ? echo_buffer[the_other]
5781 : echo_buffer[this_one]);
5782 clear_buffer_p = 1;
5785 buffer = echo_area_buffer[this_one];
5787 record_unwind_protect (unwind_with_echo_area_buffer,
5788 with_echo_area_buffer_unwind_data (w));
5790 /* Make the echo area buffer current. Note that for display
5791 purposes, it is not necessary that the displayed window's buffer
5792 == current_buffer, except for text property lookup. So, let's
5793 only set that buffer temporarily here without doing a full
5794 Fset_window_buffer. We must also change w->pointm, though,
5795 because otherwise an assertions in unshow_buffer fails, and Emacs
5796 aborts. */
5797 set_buffer_internal_1 (XBUFFER (buffer));
5798 if (w)
5800 w->buffer = buffer;
5801 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5804 current_buffer->undo_list = Qt;
5805 current_buffer->read_only = Qnil;
5807 if (clear_buffer_p && Z > BEG)
5808 del_range (BEG, Z);
5810 xassert (BEGV >= BEG);
5811 xassert (ZV <= Z && ZV >= BEGV);
5813 rc = fn (a1, a2, a3, a4);
5815 xassert (BEGV >= BEG);
5816 xassert (ZV <= Z && ZV >= BEGV);
5818 unbind_to (count, Qnil);
5819 return rc;
5823 /* Save state that should be preserved around the call to the function
5824 FN called in with_echo_area_buffer. */
5826 static Lisp_Object
5827 with_echo_area_buffer_unwind_data (w)
5828 struct window *w;
5830 int i = 0;
5831 Lisp_Object vector;
5833 /* Reduce consing by keeping one vector in
5834 Vwith_echo_area_save_vector. */
5835 vector = Vwith_echo_area_save_vector;
5836 Vwith_echo_area_save_vector = Qnil;
5838 if (NILP (vector))
5839 vector = Fmake_vector (make_number (7), Qnil);
5841 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5842 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5843 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5845 if (w)
5847 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5848 XVECTOR (vector)->contents[i++] = w->buffer;
5849 XVECTOR (vector)->contents[i++]
5850 = make_number (XMARKER (w->pointm)->charpos);
5851 XVECTOR (vector)->contents[i++]
5852 = make_number (XMARKER (w->pointm)->bytepos);
5854 else
5856 int end = i + 4;
5857 while (i < end)
5858 XVECTOR (vector)->contents[i++] = Qnil;
5861 xassert (i == XVECTOR (vector)->size);
5862 return vector;
5866 /* Restore global state from VECTOR which was created by
5867 with_echo_area_buffer_unwind_data. */
5869 static Lisp_Object
5870 unwind_with_echo_area_buffer (vector)
5871 Lisp_Object vector;
5873 int i = 0;
5875 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5876 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5877 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5879 if (WINDOWP (XVECTOR (vector)->contents[i]))
5881 struct window *w;
5882 Lisp_Object buffer, charpos, bytepos;
5884 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5885 buffer = XVECTOR (vector)->contents[i]; ++i;
5886 charpos = XVECTOR (vector)->contents[i]; ++i;
5887 bytepos = XVECTOR (vector)->contents[i]; ++i;
5889 w->buffer = buffer;
5890 set_marker_both (w->pointm, buffer,
5891 XFASTINT (charpos), XFASTINT (bytepos));
5894 Vwith_echo_area_save_vector = vector;
5895 return Qnil;
5899 /* Set up the echo area for use by print functions. MULTIBYTE_P
5900 non-zero means we will print multibyte. */
5902 void
5903 setup_echo_area_for_printing (multibyte_p)
5904 int multibyte_p;
5906 ensure_echo_area_buffers ();
5908 if (!message_buf_print)
5910 /* A message has been output since the last time we printed.
5911 Choose a fresh echo area buffer. */
5912 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5913 echo_area_buffer[0] = echo_buffer[1];
5914 else
5915 echo_area_buffer[0] = echo_buffer[0];
5917 /* Switch to that buffer and clear it. */
5918 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5919 if (Z > BEG)
5920 del_range (BEG, Z);
5921 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5923 /* Set up the buffer for the multibyteness we need. */
5924 if (multibyte_p
5925 != !NILP (current_buffer->enable_multibyte_characters))
5926 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5928 /* Raise the frame containing the echo area. */
5929 if (minibuffer_auto_raise)
5931 struct frame *sf = SELECTED_FRAME ();
5932 Lisp_Object mini_window;
5933 mini_window = FRAME_MINIBUF_WINDOW (sf);
5934 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5937 message_log_maybe_newline ();
5938 message_buf_print = 1;
5940 else
5942 if (NILP (echo_area_buffer[0]))
5944 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5945 echo_area_buffer[0] = echo_buffer[1];
5946 else
5947 echo_area_buffer[0] = echo_buffer[0];
5950 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5951 /* Someone switched buffers between print requests. */
5952 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5957 /* Display an echo area message in window W. Value is non-zero if W's
5958 height is changed. If display_last_displayed_message_p is
5959 non-zero, display the message that was last displayed, otherwise
5960 display the current message. */
5962 static int
5963 display_echo_area (w)
5964 struct window *w;
5966 int i, no_message_p, window_height_changed_p, count;
5968 /* Temporarily disable garbage collections while displaying the echo
5969 area. This is done because a GC can print a message itself.
5970 That message would modify the echo area buffer's contents while a
5971 redisplay of the buffer is going on, and seriously confuse
5972 redisplay. */
5973 count = inhibit_garbage_collection ();
5975 /* If there is no message, we must call display_echo_area_1
5976 nevertheless because it resizes the window. But we will have to
5977 reset the echo_area_buffer in question to nil at the end because
5978 with_echo_area_buffer will sets it to an empty buffer. */
5979 i = display_last_displayed_message_p ? 1 : 0;
5980 no_message_p = NILP (echo_area_buffer[i]);
5982 window_height_changed_p
5983 = with_echo_area_buffer (w, display_last_displayed_message_p,
5984 display_echo_area_1,
5985 (EMACS_INT) w, Qnil, 0, 0);
5987 if (no_message_p)
5988 echo_area_buffer[i] = Qnil;
5990 unbind_to (count, Qnil);
5991 return window_height_changed_p;
5995 /* Helper for display_echo_area. Display the current buffer which
5996 contains the current echo area message in window W, a mini-window,
5997 a pointer to which is passed in A1. A2..A4 are currently not used.
5998 Change the height of W so that all of the message is displayed.
5999 Value is non-zero if height of W was changed. */
6001 static int
6002 display_echo_area_1 (a1, a2, a3, a4)
6003 EMACS_INT a1;
6004 Lisp_Object a2;
6005 EMACS_INT a3, a4;
6007 struct window *w = (struct window *) a1;
6008 Lisp_Object window;
6009 struct text_pos start;
6010 int window_height_changed_p = 0;
6012 /* Do this before displaying, so that we have a large enough glyph
6013 matrix for the display. */
6014 window_height_changed_p = resize_mini_window (w, 0);
6016 /* Display. */
6017 clear_glyph_matrix (w->desired_matrix);
6018 XSETWINDOW (window, w);
6019 SET_TEXT_POS (start, BEG, BEG_BYTE);
6020 try_window (window, start);
6022 return window_height_changed_p;
6026 /* Resize the echo area window to exactly the size needed for the
6027 currently displayed message, if there is one. */
6029 void
6030 resize_echo_area_axactly ()
6032 if (BUFFERP (echo_area_buffer[0])
6033 && WINDOWP (echo_area_window))
6035 struct window *w = XWINDOW (echo_area_window);
6036 int resized_p;
6038 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6039 (EMACS_INT) w, Qnil, 0, 0);
6040 if (resized_p)
6042 ++windows_or_buffers_changed;
6043 ++update_mode_lines;
6044 redisplay_internal (0);
6050 /* Callback function for with_echo_area_buffer, when used from
6051 resize_echo_area_axactly. A1 contains a pointer to the window to
6052 resize, A2 to A4 are not used. Value is what resize_mini_window
6053 returns. */
6055 static int
6056 resize_mini_window_1 (a1, a2, a3, a4)
6057 EMACS_INT a1;
6058 Lisp_Object a2;
6059 EMACS_INT a3, a4;
6061 return resize_mini_window ((struct window *) a1, 1);
6065 /* Resize mini-window W to fit the size of its contents. EXACT:P
6066 means size the window exactly to the size needed. Otherwise, it's
6067 only enlarged until W's buffer is empty. Value is non-zero if
6068 the window height has been changed. */
6071 resize_mini_window (w, exact_p)
6072 struct window *w;
6073 int exact_p;
6075 struct frame *f = XFRAME (w->frame);
6076 int window_height_changed_p = 0;
6078 xassert (MINI_WINDOW_P (w));
6080 /* Nil means don't try to resize. */
6081 if (NILP (Vmax_mini_window_height)
6082 || (FRAME_X_P (f) && f->output_data.x == NULL))
6083 return 0;
6085 if (!FRAME_MINIBUF_ONLY_P (f))
6087 struct it it;
6088 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6089 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6090 int height, max_height;
6091 int unit = CANON_Y_UNIT (f);
6092 struct text_pos start;
6094 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6096 /* Compute the max. number of lines specified by the user. */
6097 if (FLOATP (Vmax_mini_window_height))
6098 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6099 else if (INTEGERP (Vmax_mini_window_height))
6100 max_height = XINT (Vmax_mini_window_height);
6101 else
6102 max_height = total_height / 4;
6104 /* Correct that max. height if it's bogus. */
6105 max_height = max (1, max_height);
6106 max_height = min (total_height, max_height);
6108 /* Find out the height of the text in the window. */
6109 if (it.truncate_lines_p)
6110 height = 1;
6111 else
6113 last_height = 0;
6114 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6115 if (it.max_ascent == 0 && it.max_descent == 0)
6116 height = it.current_y + last_height;
6117 else
6118 height = it.current_y + it.max_ascent + it.max_descent;
6119 height -= it.extra_line_spacing;
6120 height = (height + unit - 1) / unit;
6123 /* Compute a suitable window start. */
6124 if (height > max_height)
6126 height = max_height;
6127 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6128 move_it_vertically_backward (&it, (height - 1) * unit);
6129 start = it.current.pos;
6131 else
6132 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6133 SET_MARKER_FROM_TEXT_POS (w->start, start);
6135 /* Let it grow only, until we display an empty message, in which
6136 case the window shrinks again. */
6137 if (height > XFASTINT (w->height))
6139 int old_height = XFASTINT (w->height);
6140 freeze_window_starts (f, 1);
6141 grow_mini_window (w, height - XFASTINT (w->height));
6142 window_height_changed_p = XFASTINT (w->height) != old_height;
6144 else if (height < XFASTINT (w->height)
6145 && (exact_p || BEGV == ZV))
6147 int old_height = XFASTINT (w->height);
6148 freeze_window_starts (f, 0);
6149 shrink_mini_window (w);
6150 window_height_changed_p = XFASTINT (w->height) != old_height;
6154 return window_height_changed_p;
6158 /* Value is the current message, a string, or nil if there is no
6159 current message. */
6161 Lisp_Object
6162 current_message ()
6164 Lisp_Object msg;
6166 if (NILP (echo_area_buffer[0]))
6167 msg = Qnil;
6168 else
6170 with_echo_area_buffer (0, 0, current_message_1,
6171 (EMACS_INT) &msg, Qnil, 0, 0);
6172 if (NILP (msg))
6173 echo_area_buffer[0] = Qnil;
6176 return msg;
6180 static int
6181 current_message_1 (a1, a2, a3, a4)
6182 EMACS_INT a1;
6183 Lisp_Object a2;
6184 EMACS_INT a3, a4;
6186 Lisp_Object *msg = (Lisp_Object *) a1;
6188 if (Z > BEG)
6189 *msg = make_buffer_string (BEG, Z, 1);
6190 else
6191 *msg = Qnil;
6192 return 0;
6196 /* Push the current message on Vmessage_stack for later restauration
6197 by restore_message. Value is non-zero if the current message isn't
6198 empty. This is a relatively infrequent operation, so it's not
6199 worth optimizing. */
6202 push_message ()
6204 Lisp_Object msg;
6205 msg = current_message ();
6206 Vmessage_stack = Fcons (msg, Vmessage_stack);
6207 return STRINGP (msg);
6211 /* Restore message display from the top of Vmessage_stack. */
6213 void
6214 restore_message ()
6216 Lisp_Object msg;
6218 xassert (CONSP (Vmessage_stack));
6219 msg = XCAR (Vmessage_stack);
6220 if (STRINGP (msg))
6221 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6222 else
6223 message3_nolog (msg, 0, 0);
6227 /* Pop the top-most entry off Vmessage_stack. */
6229 void
6230 pop_message ()
6232 xassert (CONSP (Vmessage_stack));
6233 Vmessage_stack = XCDR (Vmessage_stack);
6237 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6238 exits. If the stack is not empty, we have a missing pop_message
6239 somewhere. */
6241 void
6242 check_message_stack ()
6244 if (!NILP (Vmessage_stack))
6245 abort ();
6249 /* Truncate to NCHARS what will be displayed in the echo area the next
6250 time we display it---but don't redisplay it now. */
6252 void
6253 truncate_echo_area (nchars)
6254 int nchars;
6256 if (nchars == 0)
6257 echo_area_buffer[0] = Qnil;
6258 /* A null message buffer means that the frame hasn't really been
6259 initialized yet. Error messages get reported properly by
6260 cmd_error, so this must be just an informative message; toss it. */
6261 else if (!noninteractive
6262 && INTERACTIVE
6263 && !NILP (echo_area_buffer[0]))
6265 struct frame *sf = SELECTED_FRAME ();
6266 if (FRAME_MESSAGE_BUF (sf))
6267 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6272 /* Helper function for truncate_echo_area. Truncate the current
6273 message to at most NCHARS characters. */
6275 static int
6276 truncate_message_1 (nchars, a2, a3, a4)
6277 EMACS_INT nchars;
6278 Lisp_Object a2;
6279 EMACS_INT a3, a4;
6281 if (BEG + nchars < Z)
6282 del_range (BEG + nchars, Z);
6283 if (Z == BEG)
6284 echo_area_buffer[0] = Qnil;
6285 return 0;
6289 /* Set the current message to a substring of S or STRING.
6291 If STRING is a Lisp string, set the message to the first NBYTES
6292 bytes from STRING. NBYTES zero means use the whole string. If
6293 STRING is multibyte, the message will be displayed multibyte.
6295 If S is not null, set the message to the first LEN bytes of S. LEN
6296 zero means use the whole string. MULTIBYTE_P non-zero means S is
6297 multibyte. Display the message multibyte in that case. */
6299 void
6300 set_message (s, string, nbytes, multibyte_p)
6301 char *s;
6302 Lisp_Object string;
6303 int nbytes;
6305 message_enable_multibyte
6306 = ((s && multibyte_p)
6307 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6309 with_echo_area_buffer (0, -1, set_message_1,
6310 (EMACS_INT) s, string, nbytes, multibyte_p);
6311 message_buf_print = 0;
6312 help_echo_showing_p = 0;
6316 /* Helper function for set_message. Arguments have the same meaning
6317 as there, with A1 corresponding to S and A2 corresponding to STRING
6318 This function is called with the echo area buffer being
6319 current. */
6321 static int
6322 set_message_1 (a1, a2, nbytes, multibyte_p)
6323 EMACS_INT a1;
6324 Lisp_Object a2;
6325 EMACS_INT nbytes, multibyte_p;
6327 char *s = (char *) a1;
6328 Lisp_Object string = a2;
6330 xassert (BEG == Z);
6332 /* Change multibyteness of the echo buffer appropriately. */
6333 if (message_enable_multibyte
6334 != !NILP (current_buffer->enable_multibyte_characters))
6335 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6337 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6339 /* Insert new message at BEG. */
6340 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6342 if (STRINGP (string))
6344 int nchars;
6346 if (nbytes == 0)
6347 nbytes = XSTRING (string)->size_byte;
6348 nchars = string_byte_to_char (string, nbytes);
6350 /* This function takes care of single/multibyte conversion. We
6351 just have to ensure that the echo area buffer has the right
6352 setting of enable_multibyte_characters. */
6353 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6355 else if (s)
6357 if (nbytes == 0)
6358 nbytes = strlen (s);
6360 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6362 /* Convert from multi-byte to single-byte. */
6363 int i, c, n;
6364 unsigned char work[1];
6366 /* Convert a multibyte string to single-byte. */
6367 for (i = 0; i < nbytes; i += n)
6369 c = string_char_and_length (s + i, nbytes - i, &n);
6370 work[0] = (SINGLE_BYTE_CHAR_P (c)
6372 : multibyte_char_to_unibyte (c, Qnil));
6373 insert_1_both (work, 1, 1, 1, 0, 0);
6376 else if (!multibyte_p
6377 && !NILP (current_buffer->enable_multibyte_characters))
6379 /* Convert from single-byte to multi-byte. */
6380 int i, c, n;
6381 unsigned char *msg = (unsigned char *) s;
6382 unsigned char str[MAX_MULTIBYTE_LENGTH];
6384 /* Convert a single-byte string to multibyte. */
6385 for (i = 0; i < nbytes; i++)
6387 c = unibyte_char_to_multibyte (msg[i]);
6388 n = CHAR_STRING (c, str);
6389 insert_1_both (str, 1, n, 1, 0, 0);
6392 else
6393 insert_1 (s, nbytes, 1, 0, 0);
6396 return 0;
6400 /* Clear messages. CURRENT_P non-zero means clear the current
6401 message. LAST_DISPLAYED_P non-zero means clear the message
6402 last displayed. */
6404 void
6405 clear_message (current_p, last_displayed_p)
6406 int current_p, last_displayed_p;
6408 if (current_p)
6409 echo_area_buffer[0] = Qnil;
6411 if (last_displayed_p)
6412 echo_area_buffer[1] = Qnil;
6414 message_buf_print = 0;
6417 /* Clear garbaged frames.
6419 This function is used where the old redisplay called
6420 redraw_garbaged_frames which in turn called redraw_frame which in
6421 turn called clear_frame. The call to clear_frame was a source of
6422 flickering. I believe a clear_frame is not necessary. It should
6423 suffice in the new redisplay to invalidate all current matrices,
6424 and ensure a complete redisplay of all windows. */
6426 static void
6427 clear_garbaged_frames ()
6429 if (frame_garbaged)
6431 Lisp_Object tail, frame;
6433 FOR_EACH_FRAME (tail, frame)
6435 struct frame *f = XFRAME (frame);
6437 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6439 clear_current_matrices (f);
6440 f->garbaged = 0;
6444 frame_garbaged = 0;
6445 ++windows_or_buffers_changed;
6450 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6451 is non-zero update selected_frame. Value is non-zero if the
6452 mini-windows height has been changed. */
6454 static int
6455 echo_area_display (update_frame_p)
6456 int update_frame_p;
6458 Lisp_Object mini_window;
6459 struct window *w;
6460 struct frame *f;
6461 int window_height_changed_p = 0;
6462 struct frame *sf = SELECTED_FRAME ();
6464 mini_window = FRAME_MINIBUF_WINDOW (sf);
6465 w = XWINDOW (mini_window);
6466 f = XFRAME (WINDOW_FRAME (w));
6468 /* Don't display if frame is invisible or not yet initialized. */
6469 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6470 return 0;
6472 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6473 #ifndef macintosh
6474 #ifdef HAVE_WINDOW_SYSTEM
6475 /* When Emacs starts, selected_frame may be a visible terminal
6476 frame, even if we run under a window system. If we let this
6477 through, a message would be displayed on the terminal. */
6478 if (EQ (selected_frame, Vterminal_frame)
6479 && !NILP (Vwindow_system))
6480 return 0;
6481 #endif /* HAVE_WINDOW_SYSTEM */
6482 #endif
6484 /* Redraw garbaged frames. */
6485 if (frame_garbaged)
6486 clear_garbaged_frames ();
6488 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6490 echo_area_window = mini_window;
6491 window_height_changed_p = display_echo_area (w);
6492 w->must_be_updated_p = 1;
6494 /* Update the display, unless called from redisplay_internal.
6495 Also don't update the screen during redisplay itself. The
6496 update will happen at the end of redisplay, and an update
6497 here could cause confusion. */
6498 if (update_frame_p && !redisplaying_p)
6500 int n = 0;
6502 /* If the display update has been interrupted by pending
6503 input, update mode lines in the frame. Due to the
6504 pending input, it might have been that redisplay hasn't
6505 been called, so that mode lines above the echo area are
6506 garbaged. This looks odd, so we prevent it here. */
6507 if (!display_completed)
6508 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6510 if (window_height_changed_p)
6512 /* Must update other windows. */
6513 windows_or_buffers_changed = 1;
6514 redisplay_internal (0);
6516 else if (FRAME_WINDOW_P (f) && n == 0)
6518 /* Window configuration is the same as before.
6519 Can do with a display update of the echo area,
6520 unless we displayed some mode lines. */
6521 update_single_window (w, 1);
6522 rif->flush_display (f);
6524 else
6525 update_frame (f, 1, 1);
6528 else if (!EQ (mini_window, selected_window))
6529 windows_or_buffers_changed++;
6531 /* Last displayed message is now the current message. */
6532 echo_area_buffer[1] = echo_area_buffer[0];
6534 /* Prevent redisplay optimization in redisplay_internal by resetting
6535 this_line_start_pos. This is done because the mini-buffer now
6536 displays the message instead of its buffer text. */
6537 if (EQ (mini_window, selected_window))
6538 CHARPOS (this_line_start_pos) = 0;
6540 return window_height_changed_p;
6545 /***********************************************************************
6546 Frame Titles
6547 ***********************************************************************/
6550 #ifdef HAVE_WINDOW_SYSTEM
6552 /* A buffer for constructing frame titles in it; allocated from the
6553 heap in init_xdisp and resized as needed in store_frame_title_char. */
6555 static char *frame_title_buf;
6557 /* The buffer's end, and a current output position in it. */
6559 static char *frame_title_buf_end;
6560 static char *frame_title_ptr;
6563 /* Store a single character C for the frame title in frame_title_buf.
6564 Re-allocate frame_title_buf if necessary. */
6566 static void
6567 store_frame_title_char (c)
6568 char c;
6570 /* If output position has reached the end of the allocated buffer,
6571 double the buffer's size. */
6572 if (frame_title_ptr == frame_title_buf_end)
6574 int len = frame_title_ptr - frame_title_buf;
6575 int new_size = 2 * len * sizeof *frame_title_buf;
6576 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6577 frame_title_buf_end = frame_title_buf + new_size;
6578 frame_title_ptr = frame_title_buf + len;
6581 *frame_title_ptr++ = c;
6585 /* Store part of a frame title in frame_title_buf, beginning at
6586 frame_title_ptr. STR is the string to store. Do not copy more
6587 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6588 the whole string. Pad with spaces until FIELD_WIDTH number of
6589 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6590 Called from display_mode_element when it is used to build a frame
6591 title. */
6593 static int
6594 store_frame_title (str, field_width, precision)
6595 unsigned char *str;
6596 int field_width, precision;
6598 int n = 0;
6600 /* Copy at most PRECISION chars from STR. */
6601 while ((precision <= 0 || n < precision)
6602 && *str)
6604 store_frame_title_char (*str++);
6605 ++n;
6608 /* Fill up with spaces until FIELD_WIDTH reached. */
6609 while (field_width > 0
6610 && n < field_width)
6612 store_frame_title_char (' ');
6613 ++n;
6616 return n;
6620 /* Set the title of FRAME, if it has changed. The title format is
6621 Vicon_title_format if FRAME is iconified, otherwise it is
6622 frame_title_format. */
6624 static void
6625 x_consider_frame_title (frame)
6626 Lisp_Object frame;
6628 struct frame *f = XFRAME (frame);
6630 if (FRAME_WINDOW_P (f)
6631 || FRAME_MINIBUF_ONLY_P (f)
6632 || f->explicit_name)
6634 /* Do we have more than one visible frame on this X display? */
6635 Lisp_Object tail;
6636 Lisp_Object fmt;
6637 struct buffer *obuf;
6638 int len;
6639 struct it it;
6641 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6643 struct frame *tf = XFRAME (XCAR (tail));
6645 if (tf != f
6646 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6647 && !FRAME_MINIBUF_ONLY_P (tf)
6648 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6649 break;
6652 /* Set global variable indicating that multiple frames exist. */
6653 multiple_frames = CONSP (tail);
6655 /* Switch to the buffer of selected window of the frame. Set up
6656 frame_title_ptr so that display_mode_element will output into it;
6657 then display the title. */
6658 obuf = current_buffer;
6659 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6660 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6661 frame_title_ptr = frame_title_buf;
6662 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6663 NULL, DEFAULT_FACE_ID);
6664 len = display_mode_element (&it, 0, -1, -1, fmt);
6665 frame_title_ptr = NULL;
6666 set_buffer_internal (obuf);
6668 /* Set the title only if it's changed. This avoids consing in
6669 the common case where it hasn't. (If it turns out that we've
6670 already wasted too much time by walking through the list with
6671 display_mode_element, then we might need to optimize at a
6672 higher level than this.) */
6673 if (! STRINGP (f->name)
6674 || STRING_BYTES (XSTRING (f->name)) != len
6675 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6676 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6680 #else /* not HAVE_WINDOW_SYSTEM */
6682 #define frame_title_ptr ((char *)0)
6683 #define store_frame_title(str, mincol, maxcol) 0
6685 #endif /* not HAVE_WINDOW_SYSTEM */
6690 /***********************************************************************
6691 Menu Bars
6692 ***********************************************************************/
6695 /* Prepare for redisplay by updating menu-bar item lists when
6696 appropriate. This can call eval. */
6698 void
6699 prepare_menu_bars ()
6701 int all_windows;
6702 struct gcpro gcpro1, gcpro2;
6703 struct frame *f;
6704 struct frame *tooltip_frame;
6706 #ifdef HAVE_X_WINDOWS
6707 tooltip_frame = tip_frame;
6708 #else
6709 tooltip_frame = NULL;
6710 #endif
6712 /* Update all frame titles based on their buffer names, etc. We do
6713 this before the menu bars so that the buffer-menu will show the
6714 up-to-date frame titles. */
6715 #ifdef HAVE_WINDOW_SYSTEM
6716 if (windows_or_buffers_changed || update_mode_lines)
6718 Lisp_Object tail, frame;
6720 FOR_EACH_FRAME (tail, frame)
6722 f = XFRAME (frame);
6723 if (f != tooltip_frame
6724 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6725 x_consider_frame_title (frame);
6728 #endif /* HAVE_WINDOW_SYSTEM */
6730 /* Update the menu bar item lists, if appropriate. This has to be
6731 done before any actual redisplay or generation of display lines. */
6732 all_windows = (update_mode_lines
6733 || buffer_shared > 1
6734 || windows_or_buffers_changed);
6735 if (all_windows)
6737 Lisp_Object tail, frame;
6738 int count = specpdl_ptr - specpdl;
6740 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6742 FOR_EACH_FRAME (tail, frame)
6744 f = XFRAME (frame);
6746 /* Ignore tooltip frame. */
6747 if (f == tooltip_frame)
6748 continue;
6750 /* If a window on this frame changed size, report that to
6751 the user and clear the size-change flag. */
6752 if (FRAME_WINDOW_SIZES_CHANGED (f))
6754 Lisp_Object functions;
6756 /* Clear flag first in case we get an error below. */
6757 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6758 functions = Vwindow_size_change_functions;
6759 GCPRO2 (tail, functions);
6761 while (CONSP (functions))
6763 call1 (XCAR (functions), frame);
6764 functions = XCDR (functions);
6766 UNGCPRO;
6769 GCPRO1 (tail);
6770 update_menu_bar (f, 0);
6771 #ifdef HAVE_WINDOW_SYSTEM
6772 update_tool_bar (f, 0);
6773 #endif
6774 UNGCPRO;
6777 unbind_to (count, Qnil);
6779 else
6781 struct frame *sf = SELECTED_FRAME ();
6782 update_menu_bar (sf, 1);
6783 #ifdef HAVE_WINDOW_SYSTEM
6784 update_tool_bar (sf, 1);
6785 #endif
6788 /* Motif needs this. See comment in xmenu.c. Turn it off when
6789 pending_menu_activation is not defined. */
6790 #ifdef USE_X_TOOLKIT
6791 pending_menu_activation = 0;
6792 #endif
6796 /* Update the menu bar item list for frame F. This has to be done
6797 before we start to fill in any display lines, because it can call
6798 eval.
6800 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6802 static void
6803 update_menu_bar (f, save_match_data)
6804 struct frame *f;
6805 int save_match_data;
6807 Lisp_Object window;
6808 register struct window *w;
6810 window = FRAME_SELECTED_WINDOW (f);
6811 w = XWINDOW (window);
6813 if (update_mode_lines)
6814 w->update_mode_line = Qt;
6816 if (FRAME_WINDOW_P (f)
6818 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6819 FRAME_EXTERNAL_MENU_BAR (f)
6820 #else
6821 FRAME_MENU_BAR_LINES (f) > 0
6822 #endif
6823 : FRAME_MENU_BAR_LINES (f) > 0)
6825 /* If the user has switched buffers or windows, we need to
6826 recompute to reflect the new bindings. But we'll
6827 recompute when update_mode_lines is set too; that means
6828 that people can use force-mode-line-update to request
6829 that the menu bar be recomputed. The adverse effect on
6830 the rest of the redisplay algorithm is about the same as
6831 windows_or_buffers_changed anyway. */
6832 if (windows_or_buffers_changed
6833 || !NILP (w->update_mode_line)
6834 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6835 < BUF_MODIFF (XBUFFER (w->buffer)))
6836 != !NILP (w->last_had_star))
6837 || ((!NILP (Vtransient_mark_mode)
6838 && !NILP (XBUFFER (w->buffer)->mark_active))
6839 != !NILP (w->region_showing)))
6841 struct buffer *prev = current_buffer;
6842 int count = specpdl_ptr - specpdl;
6844 set_buffer_internal_1 (XBUFFER (w->buffer));
6845 if (save_match_data)
6846 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6847 if (NILP (Voverriding_local_map_menu_flag))
6849 specbind (Qoverriding_terminal_local_map, Qnil);
6850 specbind (Qoverriding_local_map, Qnil);
6853 /* Run the Lucid hook. */
6854 call1 (Vrun_hooks, Qactivate_menubar_hook);
6856 /* If it has changed current-menubar from previous value,
6857 really recompute the menu-bar from the value. */
6858 if (! NILP (Vlucid_menu_bar_dirty_flag))
6859 call0 (Qrecompute_lucid_menubar);
6861 safe_run_hooks (Qmenu_bar_update_hook);
6862 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6864 /* Redisplay the menu bar in case we changed it. */
6865 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6866 if (FRAME_WINDOW_P (f)
6867 #if defined (macintosh)
6868 /* All frames on Mac OS share the same menubar. So only the
6869 selected frame should be allowed to set it. */
6870 && f == SELECTED_FRAME ()
6871 #endif
6873 set_frame_menubar (f, 0, 0);
6874 else
6875 /* On a terminal screen, the menu bar is an ordinary screen
6876 line, and this makes it get updated. */
6877 w->update_mode_line = Qt;
6878 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6879 /* In the non-toolkit version, the menu bar is an ordinary screen
6880 line, and this makes it get updated. */
6881 w->update_mode_line = Qt;
6882 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6884 unbind_to (count, Qnil);
6885 set_buffer_internal_1 (prev);
6892 /***********************************************************************
6893 Tool-bars
6894 ***********************************************************************/
6896 #ifdef HAVE_WINDOW_SYSTEM
6898 /* Update the tool-bar item list for frame F. This has to be done
6899 before we start to fill in any display lines. Called from
6900 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6901 and restore it here. */
6903 static void
6904 update_tool_bar (f, save_match_data)
6905 struct frame *f;
6906 int save_match_data;
6908 if (WINDOWP (f->tool_bar_window)
6909 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6911 Lisp_Object window;
6912 struct window *w;
6914 window = FRAME_SELECTED_WINDOW (f);
6915 w = XWINDOW (window);
6917 /* If the user has switched buffers or windows, we need to
6918 recompute to reflect the new bindings. But we'll
6919 recompute when update_mode_lines is set too; that means
6920 that people can use force-mode-line-update to request
6921 that the menu bar be recomputed. The adverse effect on
6922 the rest of the redisplay algorithm is about the same as
6923 windows_or_buffers_changed anyway. */
6924 if (windows_or_buffers_changed
6925 || !NILP (w->update_mode_line)
6926 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6927 < BUF_MODIFF (XBUFFER (w->buffer)))
6928 != !NILP (w->last_had_star))
6929 || ((!NILP (Vtransient_mark_mode)
6930 && !NILP (XBUFFER (w->buffer)->mark_active))
6931 != !NILP (w->region_showing)))
6933 struct buffer *prev = current_buffer;
6934 int count = specpdl_ptr - specpdl;
6936 /* Set current_buffer to the buffer of the selected
6937 window of the frame, so that we get the right local
6938 keymaps. */
6939 set_buffer_internal_1 (XBUFFER (w->buffer));
6941 /* Save match data, if we must. */
6942 if (save_match_data)
6943 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6945 /* Make sure that we don't accidentally use bogus keymaps. */
6946 if (NILP (Voverriding_local_map_menu_flag))
6948 specbind (Qoverriding_terminal_local_map, Qnil);
6949 specbind (Qoverriding_local_map, Qnil);
6952 /* Build desired tool-bar items from keymaps. */
6953 f->desired_tool_bar_items
6954 = tool_bar_items (f->desired_tool_bar_items,
6955 &f->n_desired_tool_bar_items);
6957 /* Redisplay the tool-bar in case we changed it. */
6958 w->update_mode_line = Qt;
6960 unbind_to (count, Qnil);
6961 set_buffer_internal_1 (prev);
6967 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6968 F's desired tool-bar contents. F->desired_tool_bar_items must have
6969 been set up previously by calling prepare_menu_bars. */
6971 static void
6972 build_desired_tool_bar_string (f)
6973 struct frame *f;
6975 int i, size, size_needed, string_idx;
6976 struct gcpro gcpro1, gcpro2, gcpro3;
6977 Lisp_Object image, plist, props;
6979 image = plist = props = Qnil;
6980 GCPRO3 (image, plist, props);
6982 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6983 Otherwise, make a new string. */
6985 /* The size of the string we might be able to reuse. */
6986 size = (STRINGP (f->desired_tool_bar_string)
6987 ? XSTRING (f->desired_tool_bar_string)->size
6988 : 0);
6990 /* Each image in the string we build is preceded by a space,
6991 and there is a space at the end. */
6992 size_needed = f->n_desired_tool_bar_items + 1;
6994 /* Reuse f->desired_tool_bar_string, if possible. */
6995 if (size < size_needed)
6996 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6997 make_number (' '));
6998 else
7000 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7001 Fremove_text_properties (make_number (0), make_number (size),
7002 props, f->desired_tool_bar_string);
7005 /* Put a `display' property on the string for the images to display,
7006 put a `menu_item' property on tool-bar items with a value that
7007 is the index of the item in F's tool-bar item vector. */
7008 for (i = 0, string_idx = 0;
7009 i < f->n_desired_tool_bar_items;
7010 ++i, string_idx += 1)
7012 #define PROP(IDX) \
7013 (XVECTOR (f->desired_tool_bar_items) \
7014 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
7016 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7017 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7018 int margin, relief, idx;
7019 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7020 extern Lisp_Object Qlaplace;
7022 /* If image is a vector, choose the image according to the
7023 button state. */
7024 image = PROP (TOOL_BAR_ITEM_IMAGES);
7025 if (VECTORP (image))
7027 if (enabled_p)
7028 idx = (selected_p
7029 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7030 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7031 else
7032 idx = (selected_p
7033 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7034 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7036 xassert (ASIZE (image) >= idx);
7037 image = AREF (image, idx);
7039 else
7040 idx = -1;
7042 /* Ignore invalid image specifications. */
7043 if (!valid_image_p (image))
7044 continue;
7046 /* Display the tool-bar button pressed, or depressed. */
7047 plist = Fcopy_sequence (XCDR (image));
7049 /* Compute margin and relief to draw. */
7050 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7051 margin = relief + max (0, tool_bar_button_margin);
7053 if (auto_raise_tool_bar_buttons_p)
7055 /* Add a `:relief' property to the image spec if the item is
7056 selected. */
7057 if (selected_p)
7059 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7060 margin -= relief;
7063 else
7065 /* If image is selected, display it pressed, i.e. with a
7066 negative relief. If it's not selected, display it with a
7067 raised relief. */
7068 plist = Fplist_put (plist, QCrelief,
7069 (selected_p
7070 ? make_number (-relief)
7071 : make_number (relief)));
7072 margin -= relief;
7075 /* Put a margin around the image. */
7076 if (margin)
7077 plist = Fplist_put (plist, QCmargin, make_number (margin));
7079 /* If button is not enabled, and we don't have special images
7080 for the disabled state, make the image appear disabled by
7081 applying an appropriate algorithm to it. */
7082 if (!enabled_p && idx < 0)
7083 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7085 /* Put a `display' text property on the string for the image to
7086 display. Put a `menu-item' property on the string that gives
7087 the start of this item's properties in the tool-bar items
7088 vector. */
7089 image = Fcons (Qimage, plist);
7090 props = list4 (Qdisplay, image,
7091 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7092 Fadd_text_properties (make_number (string_idx),
7093 make_number (string_idx + 1),
7094 props, f->desired_tool_bar_string);
7095 #undef PROP
7098 UNGCPRO;
7102 /* Display one line of the tool-bar of frame IT->f. */
7104 static void
7105 display_tool_bar_line (it)
7106 struct it *it;
7108 struct glyph_row *row = it->glyph_row;
7109 int max_x = it->last_visible_x;
7110 struct glyph *last;
7112 prepare_desired_row (row);
7113 row->y = it->current_y;
7115 while (it->current_x < max_x)
7117 int x_before, x, n_glyphs_before, i, nglyphs;
7119 /* Get the next display element. */
7120 if (!get_next_display_element (it))
7121 break;
7123 /* Produce glyphs. */
7124 x_before = it->current_x;
7125 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7126 PRODUCE_GLYPHS (it);
7128 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7129 i = 0;
7130 x = x_before;
7131 while (i < nglyphs)
7133 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7135 if (x + glyph->pixel_width > max_x)
7137 /* Glyph doesn't fit on line. */
7138 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7139 it->current_x = x;
7140 goto out;
7143 ++it->hpos;
7144 x += glyph->pixel_width;
7145 ++i;
7148 /* Stop at line ends. */
7149 if (ITERATOR_AT_END_OF_LINE_P (it))
7150 break;
7152 set_iterator_to_next (it, 1);
7155 out:;
7157 row->displays_text_p = row->used[TEXT_AREA] != 0;
7158 extend_face_to_end_of_line (it);
7159 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7160 last->right_box_line_p = 1;
7161 compute_line_metrics (it);
7163 /* If line is empty, make it occupy the rest of the tool-bar. */
7164 if (!row->displays_text_p)
7166 row->height = row->phys_height = it->last_visible_y - row->y;
7167 row->ascent = row->phys_ascent = 0;
7170 row->full_width_p = 1;
7171 row->continued_p = 0;
7172 row->truncated_on_left_p = 0;
7173 row->truncated_on_right_p = 0;
7175 it->current_x = it->hpos = 0;
7176 it->current_y += row->height;
7177 ++it->vpos;
7178 ++it->glyph_row;
7182 /* Value is the number of screen lines needed to make all tool-bar
7183 items of frame F visible. */
7185 static int
7186 tool_bar_lines_needed (f)
7187 struct frame *f;
7189 struct window *w = XWINDOW (f->tool_bar_window);
7190 struct it it;
7192 /* Initialize an iterator for iteration over
7193 F->desired_tool_bar_string in the tool-bar window of frame F. */
7194 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7195 it.first_visible_x = 0;
7196 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7197 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7199 while (!ITERATOR_AT_END_P (&it))
7201 it.glyph_row = w->desired_matrix->rows;
7202 clear_glyph_row (it.glyph_row);
7203 display_tool_bar_line (&it);
7206 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7210 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7211 height should be changed. */
7213 static int
7214 redisplay_tool_bar (f)
7215 struct frame *f;
7217 struct window *w;
7218 struct it it;
7219 struct glyph_row *row;
7220 int change_height_p = 0;
7222 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7223 do anything. This means you must start with tool-bar-lines
7224 non-zero to get the auto-sizing effect. Or in other words, you
7225 can turn off tool-bars by specifying tool-bar-lines zero. */
7226 if (!WINDOWP (f->tool_bar_window)
7227 || (w = XWINDOW (f->tool_bar_window),
7228 XFASTINT (w->height) == 0))
7229 return 0;
7231 /* Set up an iterator for the tool-bar window. */
7232 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7233 it.first_visible_x = 0;
7234 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7235 row = it.glyph_row;
7237 /* Build a string that represents the contents of the tool-bar. */
7238 build_desired_tool_bar_string (f);
7239 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7241 /* Display as many lines as needed to display all tool-bar items. */
7242 while (it.current_y < it.last_visible_y)
7243 display_tool_bar_line (&it);
7245 /* It doesn't make much sense to try scrolling in the tool-bar
7246 window, so don't do it. */
7247 w->desired_matrix->no_scrolling_p = 1;
7248 w->must_be_updated_p = 1;
7250 if (auto_resize_tool_bars_p)
7252 int nlines;
7254 /* If there are blank lines at the end, except for a partially
7255 visible blank line at the end that is smaller than
7256 CANON_Y_UNIT, change the tool-bar's height. */
7257 row = it.glyph_row - 1;
7258 if (!row->displays_text_p
7259 && row->height >= CANON_Y_UNIT (f))
7260 change_height_p = 1;
7262 /* If row displays tool-bar items, but is partially visible,
7263 change the tool-bar's height. */
7264 if (row->displays_text_p
7265 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7266 change_height_p = 1;
7268 /* Resize windows as needed by changing the `tool-bar-lines'
7269 frame parameter. */
7270 if (change_height_p
7271 && (nlines = tool_bar_lines_needed (f),
7272 nlines != XFASTINT (w->height)))
7274 extern Lisp_Object Qtool_bar_lines;
7275 Lisp_Object frame;
7276 int old_height = XFASTINT (w->height);
7278 XSETFRAME (frame, f);
7279 clear_glyph_matrix (w->desired_matrix);
7280 Fmodify_frame_parameters (frame,
7281 Fcons (Fcons (Qtool_bar_lines,
7282 make_number (nlines)),
7283 Qnil));
7284 if (XFASTINT (w->height) != old_height)
7285 fonts_changed_p = 1;
7289 return change_height_p;
7293 /* Get information about the tool-bar item which is displayed in GLYPH
7294 on frame F. Return in *PROP_IDX the index where tool-bar item
7295 properties start in F->current_tool_bar_items. Value is zero if
7296 GLYPH doesn't display a tool-bar item. */
7299 tool_bar_item_info (f, glyph, prop_idx)
7300 struct frame *f;
7301 struct glyph *glyph;
7302 int *prop_idx;
7304 Lisp_Object prop;
7305 int success_p;
7307 /* Get the text property `menu-item' at pos. The value of that
7308 property is the start index of this item's properties in
7309 F->current_tool_bar_items. */
7310 prop = Fget_text_property (make_number (glyph->charpos),
7311 Qmenu_item, f->current_tool_bar_string);
7312 if (INTEGERP (prop))
7314 *prop_idx = XINT (prop);
7315 success_p = 1;
7317 else
7318 success_p = 0;
7320 return success_p;
7323 #endif /* HAVE_WINDOW_SYSTEM */
7327 /************************************************************************
7328 Horizontal scrolling
7329 ************************************************************************/
7331 static int hscroll_window_tree P_ ((Lisp_Object));
7332 static int hscroll_windows P_ ((Lisp_Object));
7334 /* For all leaf windows in the window tree rooted at WINDOW, set their
7335 hscroll value so that PT is (i) visible in the window, and (ii) so
7336 that it is not within a certain margin at the window's left and
7337 right border. Value is non-zero if any window's hscroll has been
7338 changed. */
7340 static int
7341 hscroll_window_tree (window)
7342 Lisp_Object window;
7344 int hscrolled_p = 0;
7346 while (WINDOWP (window))
7348 struct window *w = XWINDOW (window);
7350 if (WINDOWP (w->hchild))
7351 hscrolled_p |= hscroll_window_tree (w->hchild);
7352 else if (WINDOWP (w->vchild))
7353 hscrolled_p |= hscroll_window_tree (w->vchild);
7354 else if (w->cursor.vpos >= 0)
7356 int hscroll_margin, text_area_x, text_area_y;
7357 int text_area_width, text_area_height;
7358 struct glyph_row *current_cursor_row
7359 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7360 struct glyph_row *desired_cursor_row
7361 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7362 struct glyph_row *cursor_row
7363 = (desired_cursor_row->enabled_p
7364 ? desired_cursor_row
7365 : current_cursor_row);
7367 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7368 &text_area_width, &text_area_height);
7370 /* Scroll when cursor is inside this scroll margin. */
7371 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7373 if ((XFASTINT (w->hscroll)
7374 && w->cursor.x < hscroll_margin)
7375 || (cursor_row->enabled_p
7376 && cursor_row->truncated_on_right_p
7377 && (w->cursor.x > text_area_width - hscroll_margin)))
7379 struct it it;
7380 int hscroll;
7381 struct buffer *saved_current_buffer;
7382 int pt;
7384 /* Find point in a display of infinite width. */
7385 saved_current_buffer = current_buffer;
7386 current_buffer = XBUFFER (w->buffer);
7388 if (w == XWINDOW (selected_window))
7389 pt = BUF_PT (current_buffer);
7390 else
7392 pt = marker_position (w->pointm);
7393 pt = max (BEGV, pt);
7394 pt = min (ZV, pt);
7397 /* Move iterator to pt starting at cursor_row->start in
7398 a line with infinite width. */
7399 init_to_row_start (&it, w, cursor_row);
7400 it.last_visible_x = INFINITY;
7401 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7402 current_buffer = saved_current_buffer;
7404 /* Center cursor in window. */
7405 hscroll = (max (0, it.current_x - text_area_width / 2)
7406 / CANON_X_UNIT (it.f));
7408 /* Don't call Fset_window_hscroll if value hasn't
7409 changed because it will prevent redisplay
7410 optimizations. */
7411 if (XFASTINT (w->hscroll) != hscroll)
7413 Fset_window_hscroll (window, make_number (hscroll));
7414 hscrolled_p = 1;
7419 window = w->next;
7422 /* Value is non-zero if hscroll of any leaf window has been changed. */
7423 return hscrolled_p;
7427 /* Set hscroll so that cursor is visible and not inside horizontal
7428 scroll margins for all windows in the tree rooted at WINDOW. See
7429 also hscroll_window_tree above. Value is non-zero if any window's
7430 hscroll has been changed. If it has, desired matrices on the frame
7431 of WINDOW are cleared. */
7433 static int
7434 hscroll_windows (window)
7435 Lisp_Object window;
7437 int hscrolled_p;
7439 if (automatic_hscrolling_p)
7441 hscrolled_p = hscroll_window_tree (window);
7442 if (hscrolled_p)
7443 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7445 else
7446 hscrolled_p = 0;
7447 return hscrolled_p;
7452 /************************************************************************
7453 Redisplay
7454 ************************************************************************/
7456 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7457 to a non-zero value. This is sometimes handy to have in a debugger
7458 session. */
7460 #if GLYPH_DEBUG
7462 /* First and last unchanged row for try_window_id. */
7464 int debug_first_unchanged_at_end_vpos;
7465 int debug_last_unchanged_at_beg_vpos;
7467 /* Delta vpos and y. */
7469 int debug_dvpos, debug_dy;
7471 /* Delta in characters and bytes for try_window_id. */
7473 int debug_delta, debug_delta_bytes;
7475 /* Values of window_end_pos and window_end_vpos at the end of
7476 try_window_id. */
7478 int debug_end_pos, debug_end_vpos;
7480 /* Append a string to W->desired_matrix->method. FMT is a printf
7481 format string. A1...A9 are a supplement for a variable-length
7482 argument list. If trace_redisplay_p is non-zero also printf the
7483 resulting string to stderr. */
7485 static void
7486 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7487 struct window *w;
7488 char *fmt;
7489 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7491 char buffer[512];
7492 char *method = w->desired_matrix->method;
7493 int len = strlen (method);
7494 int size = sizeof w->desired_matrix->method;
7495 int remaining = size - len - 1;
7497 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7498 if (len && remaining)
7500 method[len] = '|';
7501 --remaining, ++len;
7504 strncpy (method + len, buffer, remaining);
7506 if (trace_redisplay_p)
7507 fprintf (stderr, "%p (%s): %s\n",
7509 ((BUFFERP (w->buffer)
7510 && STRINGP (XBUFFER (w->buffer)->name))
7511 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7512 : "no buffer"),
7513 buffer);
7516 #endif /* GLYPH_DEBUG */
7519 /* This counter is used to clear the face cache every once in a while
7520 in redisplay_internal. It is incremented for each redisplay.
7521 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7522 cleared. */
7524 #define CLEAR_FACE_CACHE_COUNT 10000
7525 static int clear_face_cache_count;
7527 /* Record the previous terminal frame we displayed. */
7529 static struct frame *previous_terminal_frame;
7531 /* Non-zero while redisplay_internal is in progress. */
7533 int redisplaying_p;
7536 /* Value is non-zero if all changes in window W, which displays
7537 current_buffer, are in the text between START and END. START is a
7538 buffer position, END is given as a distance from Z. Used in
7539 redisplay_internal for display optimization. */
7541 static INLINE int
7542 text_outside_line_unchanged_p (w, start, end)
7543 struct window *w;
7544 int start, end;
7546 int unchanged_p = 1;
7548 /* If text or overlays have changed, see where. */
7549 if (XFASTINT (w->last_modified) < MODIFF
7550 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7552 /* Gap in the line? */
7553 if (GPT < start || Z - GPT < end)
7554 unchanged_p = 0;
7556 /* Changes start in front of the line, or end after it? */
7557 if (unchanged_p
7558 && (BEG_UNCHANGED < start - 1
7559 || END_UNCHANGED < end))
7560 unchanged_p = 0;
7562 /* If selective display, can't optimize if changes start at the
7563 beginning of the line. */
7564 if (unchanged_p
7565 && INTEGERP (current_buffer->selective_display)
7566 && XINT (current_buffer->selective_display) > 0
7567 && (BEG_UNCHANGED < start || GPT <= start))
7568 unchanged_p = 0;
7571 return unchanged_p;
7575 /* Do a frame update, taking possible shortcuts into account. This is
7576 the main external entry point for redisplay.
7578 If the last redisplay displayed an echo area message and that message
7579 is no longer requested, we clear the echo area or bring back the
7580 mini-buffer if that is in use. */
7582 void
7583 redisplay ()
7585 redisplay_internal (0);
7588 /* Return 1 if point moved out of or into a composition. Otherwise
7589 return 0. PREV_BUF and PREV_PT are the last point buffer and
7590 position. BUF and PT are the current point buffer and position. */
7593 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7594 struct buffer *prev_buf, *buf;
7595 int prev_pt, pt;
7597 int start, end;
7598 Lisp_Object prop;
7599 Lisp_Object buffer;
7601 XSETBUFFER (buffer, buf);
7602 /* Check a composition at the last point if point moved within the
7603 same buffer. */
7604 if (prev_buf == buf)
7606 if (prev_pt == pt)
7607 /* Point didn't move. */
7608 return 0;
7610 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7611 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7612 && COMPOSITION_VALID_P (start, end, prop)
7613 && start < prev_pt && end > prev_pt)
7614 /* The last point was within the composition. Return 1 iff
7615 point moved out of the composition. */
7616 return (pt <= start || pt >= end);
7619 /* Check a composition at the current point. */
7620 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7621 && find_composition (pt, -1, &start, &end, &prop, buffer)
7622 && COMPOSITION_VALID_P (start, end, prop)
7623 && start < pt && end > pt);
7626 /* Reconsider the setting of B->clip_changed which is displayed
7627 in window W. */
7629 static INLINE void
7630 reconsider_clip_changes (w, b)
7631 struct window *w;
7632 struct buffer *b;
7634 if (b->prevent_redisplay_optimizations_p)
7635 b->clip_changed = 1;
7636 else if (b->clip_changed
7637 && !NILP (w->window_end_valid)
7638 && w->current_matrix->buffer == b
7639 && w->current_matrix->zv == BUF_ZV (b)
7640 && w->current_matrix->begv == BUF_BEGV (b))
7641 b->clip_changed = 0;
7643 /* If display wasn't paused, and W is not a tool bar window, see if
7644 point has been moved into or out of a composition. In that case,
7645 we set b->clip_changed to 1 to force updating the screen. If
7646 b->clip_changed has already been set to 1, we can skip this
7647 check. */
7648 if (!b->clip_changed
7649 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7651 int pt;
7653 if (w == XWINDOW (selected_window))
7654 pt = BUF_PT (current_buffer);
7655 else
7656 pt = marker_position (w->pointm);
7658 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7659 || pt != XINT (w->last_point))
7660 && check_point_in_composition (w->current_matrix->buffer,
7661 XINT (w->last_point),
7662 XBUFFER (w->buffer), pt))
7663 b->clip_changed = 1;
7668 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7669 response to any user action; therefore, we should preserve the echo
7670 area. (Actually, our caller does that job.) Perhaps in the future
7671 avoid recentering windows if it is not necessary; currently that
7672 causes some problems. */
7674 static void
7675 redisplay_internal (preserve_echo_area)
7676 int preserve_echo_area;
7678 struct window *w = XWINDOW (selected_window);
7679 struct frame *f = XFRAME (w->frame);
7680 int pause;
7681 int must_finish = 0;
7682 struct text_pos tlbufpos, tlendpos;
7683 int number_of_visible_frames;
7684 int count;
7685 struct frame *sf = SELECTED_FRAME ();
7687 /* Non-zero means redisplay has to consider all windows on all
7688 frames. Zero means, only selected_window is considered. */
7689 int consider_all_windows_p;
7691 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7693 /* No redisplay if running in batch mode or frame is not yet fully
7694 initialized, or redisplay is explicitly turned off by setting
7695 Vinhibit_redisplay. */
7696 if (noninteractive
7697 || !NILP (Vinhibit_redisplay)
7698 || !f->glyphs_initialized_p)
7699 return;
7701 /* The flag redisplay_performed_directly_p is set by
7702 direct_output_for_insert when it already did the whole screen
7703 update necessary. */
7704 if (redisplay_performed_directly_p)
7706 redisplay_performed_directly_p = 0;
7707 if (!hscroll_windows (selected_window))
7708 return;
7711 #ifdef USE_X_TOOLKIT
7712 if (popup_activated ())
7713 return;
7714 #endif
7716 /* I don't think this happens but let's be paranoid. */
7717 if (redisplaying_p)
7718 return;
7720 /* Record a function that resets redisplaying_p to its old value
7721 when we leave this function. */
7722 count = specpdl_ptr - specpdl;
7723 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7724 ++redisplaying_p;
7726 retry:
7727 pause = 0;
7728 reconsider_clip_changes (w, current_buffer);
7730 /* If new fonts have been loaded that make a glyph matrix adjustment
7731 necessary, do it. */
7732 if (fonts_changed_p)
7734 adjust_glyphs (NULL);
7735 ++windows_or_buffers_changed;
7736 fonts_changed_p = 0;
7739 if (! FRAME_WINDOW_P (sf)
7740 && previous_terminal_frame != sf)
7742 /* Since frames on an ASCII terminal share the same display
7743 area, displaying a different frame means redisplay the whole
7744 thing. */
7745 windows_or_buffers_changed++;
7746 SET_FRAME_GARBAGED (sf);
7747 XSETFRAME (Vterminal_frame, sf);
7749 previous_terminal_frame = sf;
7751 /* Set the visible flags for all frames. Do this before checking
7752 for resized or garbaged frames; they want to know if their frames
7753 are visible. See the comment in frame.h for
7754 FRAME_SAMPLE_VISIBILITY. */
7756 Lisp_Object tail, frame;
7758 number_of_visible_frames = 0;
7760 FOR_EACH_FRAME (tail, frame)
7762 struct frame *f = XFRAME (frame);
7764 FRAME_SAMPLE_VISIBILITY (f);
7765 if (FRAME_VISIBLE_P (f))
7766 ++number_of_visible_frames;
7767 clear_desired_matrices (f);
7771 /* Notice any pending interrupt request to change frame size. */
7772 do_pending_window_change (1);
7774 /* Clear frames marked as garbaged. */
7775 if (frame_garbaged)
7776 clear_garbaged_frames ();
7778 /* Build menubar and tool-bar items. */
7779 prepare_menu_bars ();
7781 if (windows_or_buffers_changed)
7782 update_mode_lines++;
7784 /* Detect case that we need to write or remove a star in the mode line. */
7785 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7787 w->update_mode_line = Qt;
7788 if (buffer_shared > 1)
7789 update_mode_lines++;
7792 /* If %c is in the mode line, update it if needed. */
7793 if (!NILP (w->column_number_displayed)
7794 /* This alternative quickly identifies a common case
7795 where no change is needed. */
7796 && !(PT == XFASTINT (w->last_point)
7797 && XFASTINT (w->last_modified) >= MODIFF
7798 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7799 && XFASTINT (w->column_number_displayed) != current_column ())
7800 w->update_mode_line = Qt;
7802 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7804 /* The variable buffer_shared is set in redisplay_window and
7805 indicates that we redisplay a buffer in different windows. See
7806 there. */
7807 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7809 /* If specs for an arrow have changed, do thorough redisplay
7810 to ensure we remove any arrow that should no longer exist. */
7811 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7812 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7813 consider_all_windows_p = windows_or_buffers_changed = 1;
7815 /* Normally the message* functions will have already displayed and
7816 updated the echo area, but the frame may have been trashed, or
7817 the update may have been preempted, so display the echo area
7818 again here. Checking both message buffers captures the case that
7819 the echo area should be cleared. */
7820 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7822 int window_height_changed_p = echo_area_display (0);
7823 must_finish = 1;
7825 if (fonts_changed_p)
7826 goto retry;
7827 else if (window_height_changed_p)
7829 consider_all_windows_p = 1;
7830 ++update_mode_lines;
7831 ++windows_or_buffers_changed;
7833 /* If window configuration was changed, frames may have been
7834 marked garbaged. Clear them or we will experience
7835 surprises wrt scrolling. */
7836 if (frame_garbaged)
7837 clear_garbaged_frames ();
7840 else if (EQ (selected_window, minibuf_window)
7841 && (current_buffer->clip_changed
7842 || XFASTINT (w->last_modified) < MODIFF
7843 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7844 && resize_mini_window (w, 0))
7846 /* Resized active mini-window to fit the size of what it is
7847 showing if its contents might have changed. */
7848 must_finish = 1;
7849 consider_all_windows_p = 1;
7850 ++windows_or_buffers_changed;
7851 ++update_mode_lines;
7853 /* If window configuration was changed, frames may have been
7854 marked garbaged. Clear them or we will experience
7855 surprises wrt scrolling. */
7856 if (frame_garbaged)
7857 clear_garbaged_frames ();
7861 /* If showing the region, and mark has changed, we must redisplay
7862 the whole window. The assignment to this_line_start_pos prevents
7863 the optimization directly below this if-statement. */
7864 if (((!NILP (Vtransient_mark_mode)
7865 && !NILP (XBUFFER (w->buffer)->mark_active))
7866 != !NILP (w->region_showing))
7867 || (!NILP (w->region_showing)
7868 && !EQ (w->region_showing,
7869 Fmarker_position (XBUFFER (w->buffer)->mark))))
7870 CHARPOS (this_line_start_pos) = 0;
7872 /* Optimize the case that only the line containing the cursor in the
7873 selected window has changed. Variables starting with this_ are
7874 set in display_line and record information about the line
7875 containing the cursor. */
7876 tlbufpos = this_line_start_pos;
7877 tlendpos = this_line_end_pos;
7878 if (!consider_all_windows_p
7879 && CHARPOS (tlbufpos) > 0
7880 && NILP (w->update_mode_line)
7881 && !current_buffer->clip_changed
7882 && FRAME_VISIBLE_P (XFRAME (w->frame))
7883 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7884 /* Make sure recorded data applies to current buffer, etc. */
7885 && this_line_buffer == current_buffer
7886 && current_buffer == XBUFFER (w->buffer)
7887 && NILP (w->force_start)
7888 /* Point must be on the line that we have info recorded about. */
7889 && PT >= CHARPOS (tlbufpos)
7890 && PT <= Z - CHARPOS (tlendpos)
7891 /* All text outside that line, including its final newline,
7892 must be unchanged */
7893 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7894 CHARPOS (tlendpos)))
7896 if (CHARPOS (tlbufpos) > BEGV
7897 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7898 && (CHARPOS (tlbufpos) == ZV
7899 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7900 /* Former continuation line has disappeared by becoming empty */
7901 goto cancel;
7902 else if (XFASTINT (w->last_modified) < MODIFF
7903 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7904 || MINI_WINDOW_P (w))
7906 /* We have to handle the case of continuation around a
7907 wide-column character (See the comment in indent.c around
7908 line 885).
7910 For instance, in the following case:
7912 -------- Insert --------
7913 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7914 J_I_ ==> J_I_ `^^' are cursors.
7915 ^^ ^^
7916 -------- --------
7918 As we have to redraw the line above, we should goto cancel. */
7920 struct it it;
7921 int line_height_before = this_line_pixel_height;
7923 /* Note that start_display will handle the case that the
7924 line starting at tlbufpos is a continuation lines. */
7925 start_display (&it, w, tlbufpos);
7927 /* Implementation note: It this still necessary? */
7928 if (it.current_x != this_line_start_x)
7929 goto cancel;
7931 TRACE ((stderr, "trying display optimization 1\n"));
7932 w->cursor.vpos = -1;
7933 overlay_arrow_seen = 0;
7934 it.vpos = this_line_vpos;
7935 it.current_y = this_line_y;
7936 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7937 display_line (&it);
7939 /* If line contains point, is not continued,
7940 and ends at same distance from eob as before, we win */
7941 if (w->cursor.vpos >= 0
7942 /* Line is not continued, otherwise this_line_start_pos
7943 would have been set to 0 in display_line. */
7944 && CHARPOS (this_line_start_pos)
7945 /* Line ends as before. */
7946 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7947 /* Line has same height as before. Otherwise other lines
7948 would have to be shifted up or down. */
7949 && this_line_pixel_height == line_height_before)
7951 /* If this is not the window's last line, we must adjust
7952 the charstarts of the lines below. */
7953 if (it.current_y < it.last_visible_y)
7955 struct glyph_row *row
7956 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7957 int delta, delta_bytes;
7959 if (Z - CHARPOS (tlendpos) == ZV)
7961 /* This line ends at end of (accessible part of)
7962 buffer. There is no newline to count. */
7963 delta = (Z
7964 - CHARPOS (tlendpos)
7965 - MATRIX_ROW_START_CHARPOS (row));
7966 delta_bytes = (Z_BYTE
7967 - BYTEPOS (tlendpos)
7968 - MATRIX_ROW_START_BYTEPOS (row));
7970 else
7972 /* This line ends in a newline. Must take
7973 account of the newline and the rest of the
7974 text that follows. */
7975 delta = (Z
7976 - CHARPOS (tlendpos)
7977 - MATRIX_ROW_START_CHARPOS (row));
7978 delta_bytes = (Z_BYTE
7979 - BYTEPOS (tlendpos)
7980 - MATRIX_ROW_START_BYTEPOS (row));
7983 increment_matrix_positions (w->current_matrix,
7984 this_line_vpos + 1,
7985 w->current_matrix->nrows,
7986 delta, delta_bytes);
7989 /* If this row displays text now but previously didn't,
7990 or vice versa, w->window_end_vpos may have to be
7991 adjusted. */
7992 if ((it.glyph_row - 1)->displays_text_p)
7994 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7995 XSETINT (w->window_end_vpos, this_line_vpos);
7997 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7998 && this_line_vpos > 0)
7999 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8000 w->window_end_valid = Qnil;
8002 /* Update hint: No need to try to scroll in update_window. */
8003 w->desired_matrix->no_scrolling_p = 1;
8005 #if GLYPH_DEBUG
8006 *w->desired_matrix->method = 0;
8007 debug_method_add (w, "optimization 1");
8008 #endif
8009 goto update;
8011 else
8012 goto cancel;
8014 else if (/* Cursor position hasn't changed. */
8015 PT == XFASTINT (w->last_point)
8016 /* Make sure the cursor was last displayed
8017 in this window. Otherwise we have to reposition it. */
8018 && 0 <= w->cursor.vpos
8019 && XINT (w->height) > w->cursor.vpos)
8021 if (!must_finish)
8023 do_pending_window_change (1);
8025 /* We used to always goto end_of_redisplay here, but this
8026 isn't enough if we have a blinking cursor. */
8027 if (w->cursor_off_p == w->last_cursor_off_p)
8028 goto end_of_redisplay;
8030 goto update;
8032 /* If highlighting the region, or if the cursor is in the echo area,
8033 then we can't just move the cursor. */
8034 else if (! (!NILP (Vtransient_mark_mode)
8035 && !NILP (current_buffer->mark_active))
8036 && (EQ (selected_window, current_buffer->last_selected_window)
8037 || highlight_nonselected_windows)
8038 && NILP (w->region_showing)
8039 && NILP (Vshow_trailing_whitespace)
8040 && !cursor_in_echo_area)
8042 struct it it;
8043 struct glyph_row *row;
8045 /* Skip from tlbufpos to PT and see where it is. Note that
8046 PT may be in invisible text. If so, we will end at the
8047 next visible position. */
8048 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8049 NULL, DEFAULT_FACE_ID);
8050 it.current_x = this_line_start_x;
8051 it.current_y = this_line_y;
8052 it.vpos = this_line_vpos;
8054 /* The call to move_it_to stops in front of PT, but
8055 moves over before-strings. */
8056 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8058 if (it.vpos == this_line_vpos
8059 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8060 row->enabled_p))
8062 xassert (this_line_vpos == it.vpos);
8063 xassert (this_line_y == it.current_y);
8064 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8065 goto update;
8067 else
8068 goto cancel;
8071 cancel:
8072 /* Text changed drastically or point moved off of line. */
8073 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8076 CHARPOS (this_line_start_pos) = 0;
8077 consider_all_windows_p |= buffer_shared > 1;
8078 ++clear_face_cache_count;
8081 /* Build desired matrices, and update the display. If
8082 consider_all_windows_p is non-zero, do it for all windows on all
8083 frames. Otherwise do it for selected_window, only. */
8085 if (consider_all_windows_p)
8087 Lisp_Object tail, frame;
8089 /* Clear the face cache eventually. */
8090 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8092 clear_face_cache (0);
8093 clear_face_cache_count = 0;
8096 /* Recompute # windows showing selected buffer. This will be
8097 incremented each time such a window is displayed. */
8098 buffer_shared = 0;
8100 FOR_EACH_FRAME (tail, frame)
8102 struct frame *f = XFRAME (frame);
8104 if (FRAME_WINDOW_P (f) || f == sf)
8106 /* Mark all the scroll bars to be removed; we'll redeem
8107 the ones we want when we redisplay their windows. */
8108 if (condemn_scroll_bars_hook)
8109 (*condemn_scroll_bars_hook) (f);
8111 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8112 redisplay_windows (FRAME_ROOT_WINDOW (f));
8114 /* Any scroll bars which redisplay_windows should have
8115 nuked should now go away. */
8116 if (judge_scroll_bars_hook)
8117 (*judge_scroll_bars_hook) (f);
8119 /* If fonts changed, display again. */
8120 if (fonts_changed_p)
8121 goto retry;
8123 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8125 /* See if we have to hscroll. */
8126 if (hscroll_windows (f->root_window))
8127 goto retry;
8129 /* Prevent various kinds of signals during display
8130 update. stdio is not robust about handling
8131 signals, which can cause an apparent I/O
8132 error. */
8133 if (interrupt_input)
8134 unrequest_sigio ();
8135 stop_polling ();
8137 /* Update the display. */
8138 set_window_update_flags (XWINDOW (f->root_window), 1);
8139 pause |= update_frame (f, 0, 0);
8140 if (pause)
8141 break;
8143 mark_window_display_accurate (f->root_window, 1);
8144 if (frame_up_to_date_hook)
8145 frame_up_to_date_hook (f);
8150 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8152 Lisp_Object mini_window;
8153 struct frame *mini_frame;
8155 redisplay_window (selected_window, 1);
8157 /* Compare desired and current matrices, perform output. */
8158 update:
8160 /* If fonts changed, display again. */
8161 if (fonts_changed_p)
8162 goto retry;
8164 /* Prevent various kinds of signals during display update.
8165 stdio is not robust about handling signals,
8166 which can cause an apparent I/O error. */
8167 if (interrupt_input)
8168 unrequest_sigio ();
8169 stop_polling ();
8171 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8173 if (hscroll_windows (selected_window))
8174 goto retry;
8176 XWINDOW (selected_window)->must_be_updated_p = 1;
8177 pause = update_frame (sf, 0, 0);
8180 /* We may have called echo_area_display at the top of this
8181 function. If the echo area is on another frame, that may
8182 have put text on a frame other than the selected one, so the
8183 above call to update_frame would not have caught it. Catch
8184 it here. */
8185 mini_window = FRAME_MINIBUF_WINDOW (sf);
8186 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8188 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8190 XWINDOW (mini_window)->must_be_updated_p = 1;
8191 pause |= update_frame (mini_frame, 0, 0);
8192 if (!pause && hscroll_windows (mini_window))
8193 goto retry;
8197 /* If display was paused because of pending input, make sure we do a
8198 thorough update the next time. */
8199 if (pause)
8201 /* Prevent the optimization at the beginning of
8202 redisplay_internal that tries a single-line update of the
8203 line containing the cursor in the selected window. */
8204 CHARPOS (this_line_start_pos) = 0;
8206 /* Let the overlay arrow be updated the next time. */
8207 if (!NILP (last_arrow_position))
8209 last_arrow_position = Qt;
8210 last_arrow_string = Qt;
8213 /* If we pause after scrolling, some rows in the current
8214 matrices of some windows are not valid. */
8215 if (!WINDOW_FULL_WIDTH_P (w)
8216 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8217 update_mode_lines = 1;
8220 /* Now text on frame agrees with windows, so put info into the
8221 windows for partial redisplay to follow. */
8222 if (!pause)
8224 register struct buffer *b = XBUFFER (w->buffer);
8226 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8227 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8228 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8229 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8231 if (consider_all_windows_p)
8232 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8233 else
8235 XSETFASTINT (w->last_point, BUF_PT (b));
8236 w->last_cursor = w->cursor;
8237 w->last_cursor_off_p = w->cursor_off_p;
8239 b->clip_changed = 0;
8240 b->prevent_redisplay_optimizations_p = 0;
8241 w->update_mode_line = Qnil;
8242 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8243 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8244 w->last_had_star
8245 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8246 ? Qt : Qnil);
8248 /* Record if we are showing a region, so can make sure to
8249 update it fully at next redisplay. */
8250 w->region_showing = (!NILP (Vtransient_mark_mode)
8251 && (EQ (selected_window,
8252 current_buffer->last_selected_window)
8253 || highlight_nonselected_windows)
8254 && !NILP (XBUFFER (w->buffer)->mark_active)
8255 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8256 : Qnil);
8258 w->window_end_valid = w->buffer;
8259 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8260 last_arrow_string = Voverlay_arrow_string;
8261 if (frame_up_to_date_hook != 0)
8262 (*frame_up_to_date_hook) (sf);
8264 w->current_matrix->buffer = b;
8265 w->current_matrix->begv = BUF_BEGV (b);
8266 w->current_matrix->zv = BUF_ZV (b);
8269 update_mode_lines = 0;
8270 windows_or_buffers_changed = 0;
8273 /* Start SIGIO interrupts coming again. Having them off during the
8274 code above makes it less likely one will discard output, but not
8275 impossible, since there might be stuff in the system buffer here.
8276 But it is much hairier to try to do anything about that. */
8277 if (interrupt_input)
8278 request_sigio ();
8279 start_polling ();
8281 /* If a frame has become visible which was not before, redisplay
8282 again, so that we display it. Expose events for such a frame
8283 (which it gets when becoming visible) don't call the parts of
8284 redisplay constructing glyphs, so simply exposing a frame won't
8285 display anything in this case. So, we have to display these
8286 frames here explicitly. */
8287 if (!pause)
8289 Lisp_Object tail, frame;
8290 int new_count = 0;
8292 FOR_EACH_FRAME (tail, frame)
8294 int this_is_visible = 0;
8296 if (XFRAME (frame)->visible)
8297 this_is_visible = 1;
8298 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8299 if (XFRAME (frame)->visible)
8300 this_is_visible = 1;
8302 if (this_is_visible)
8303 new_count++;
8306 if (new_count != number_of_visible_frames)
8307 windows_or_buffers_changed++;
8310 /* Change frame size now if a change is pending. */
8311 do_pending_window_change (1);
8313 /* If we just did a pending size change, or have additional
8314 visible frames, redisplay again. */
8315 if (windows_or_buffers_changed && !pause)
8316 goto retry;
8318 end_of_redisplay:;
8320 unbind_to (count, Qnil);
8324 /* Redisplay, but leave alone any recent echo area message unless
8325 another message has been requested in its place.
8327 This is useful in situations where you need to redisplay but no
8328 user action has occurred, making it inappropriate for the message
8329 area to be cleared. See tracking_off and
8330 wait_reading_process_input for examples of these situations. */
8332 void
8333 redisplay_preserve_echo_area ()
8335 if (!NILP (echo_area_buffer[1]))
8337 /* We have a previously displayed message, but no current
8338 message. Redisplay the previous message. */
8339 display_last_displayed_message_p = 1;
8340 redisplay_internal (1);
8341 display_last_displayed_message_p = 0;
8343 else
8344 redisplay_internal (1);
8348 /* Function registered with record_unwind_protect in
8349 redisplay_internal. Clears the flag indicating that a redisplay is
8350 in progress. */
8352 static Lisp_Object
8353 unwind_redisplay (old_redisplaying_p)
8354 Lisp_Object old_redisplaying_p;
8356 redisplaying_p = XFASTINT (old_redisplaying_p);
8357 return Qnil;
8361 /* Mark the display of windows in the window tree rooted at WINDOW as
8362 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8363 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8364 the next time redisplay_internal is called. */
8366 void
8367 mark_window_display_accurate (window, accurate_p)
8368 Lisp_Object window;
8369 int accurate_p;
8371 struct window *w;
8373 for (; !NILP (window); window = w->next)
8375 w = XWINDOW (window);
8377 if (BUFFERP (w->buffer))
8379 struct buffer *b = XBUFFER (w->buffer);
8381 XSETFASTINT (w->last_modified,
8382 accurate_p ? BUF_MODIFF (b) : 0);
8383 XSETFASTINT (w->last_overlay_modified,
8384 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8385 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8386 ? Qt : Qnil);
8388 #if 0 /* I don't think this is necessary because display_line does it.
8389 Let's check it. */
8390 /* Record if we are showing a region, so can make sure to
8391 update it fully at next redisplay. */
8392 w->region_showing
8393 = (!NILP (Vtransient_mark_mode)
8394 && (w == XWINDOW (current_buffer->last_selected_window)
8395 || highlight_nonselected_windows)
8396 && (!NILP (b->mark_active)
8397 ? Fmarker_position (b->mark)
8398 : Qnil));
8399 #endif
8401 if (accurate_p)
8403 b->clip_changed = 0;
8404 b->prevent_redisplay_optimizations_p = 0;
8405 w->current_matrix->buffer = b;
8406 w->current_matrix->begv = BUF_BEGV (b);
8407 w->current_matrix->zv = BUF_ZV (b);
8408 w->last_cursor = w->cursor;
8409 w->last_cursor_off_p = w->cursor_off_p;
8410 if (w == XWINDOW (selected_window))
8411 w->last_point = make_number (BUF_PT (b));
8412 else
8413 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8417 w->window_end_valid = w->buffer;
8418 w->update_mode_line = Qnil;
8420 if (!NILP (w->vchild))
8421 mark_window_display_accurate (w->vchild, accurate_p);
8422 if (!NILP (w->hchild))
8423 mark_window_display_accurate (w->hchild, accurate_p);
8426 if (accurate_p)
8428 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8429 last_arrow_string = Voverlay_arrow_string;
8431 else
8433 /* Force a thorough redisplay the next time by setting
8434 last_arrow_position and last_arrow_string to t, which is
8435 unequal to any useful value of Voverlay_arrow_... */
8436 last_arrow_position = Qt;
8437 last_arrow_string = Qt;
8442 /* Return value in display table DP (Lisp_Char_Table *) for character
8443 C. Since a display table doesn't have any parent, we don't have to
8444 follow parent. Do not call this function directly but use the
8445 macro DISP_CHAR_VECTOR. */
8447 Lisp_Object
8448 disp_char_vector (dp, c)
8449 struct Lisp_Char_Table *dp;
8450 int c;
8452 int code[4], i;
8453 Lisp_Object val;
8455 if (SINGLE_BYTE_CHAR_P (c))
8456 return (dp->contents[c]);
8458 SPLIT_CHAR (c, code[0], code[1], code[2]);
8459 if (code[1] < 32)
8460 code[1] = -1;
8461 else if (code[2] < 32)
8462 code[2] = -1;
8464 /* Here, the possible range of code[0] (== charset ID) is
8465 128..max_charset. Since the top level char table contains data
8466 for multibyte characters after 256th element, we must increment
8467 code[0] by 128 to get a correct index. */
8468 code[0] += 128;
8469 code[3] = -1; /* anchor */
8471 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8473 val = dp->contents[code[i]];
8474 if (!SUB_CHAR_TABLE_P (val))
8475 return (NILP (val) ? dp->defalt : val);
8478 /* Here, val is a sub char table. We return the default value of
8479 it. */
8480 return (dp->defalt);
8485 /***********************************************************************
8486 Window Redisplay
8487 ***********************************************************************/
8489 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8491 static void
8492 redisplay_windows (window)
8493 Lisp_Object window;
8495 while (!NILP (window))
8497 struct window *w = XWINDOW (window);
8499 if (!NILP (w->hchild))
8500 redisplay_windows (w->hchild);
8501 else if (!NILP (w->vchild))
8502 redisplay_windows (w->vchild);
8503 else
8504 redisplay_window (window, 0);
8506 window = w->next;
8511 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8512 DELTA is the number of bytes by which positions recorded in ROW
8513 differ from current buffer positions. */
8515 void
8516 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8517 struct window *w;
8518 struct glyph_row *row;
8519 struct glyph_matrix *matrix;
8520 int delta, delta_bytes, dy, dvpos;
8522 struct glyph *glyph = row->glyphs[TEXT_AREA];
8523 struct glyph *end = glyph + row->used[TEXT_AREA];
8524 int x = row->x;
8525 int pt_old = PT - delta;
8527 /* Skip over glyphs not having an object at the start of the row.
8528 These are special glyphs like truncation marks on terminal
8529 frames. */
8530 if (row->displays_text_p)
8531 while (glyph < end
8532 && INTEGERP (glyph->object)
8533 && glyph->charpos < 0)
8535 x += glyph->pixel_width;
8536 ++glyph;
8539 while (glyph < end
8540 && !INTEGERP (glyph->object)
8541 && (!BUFFERP (glyph->object)
8542 || glyph->charpos < pt_old))
8544 x += glyph->pixel_width;
8545 ++glyph;
8548 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8549 w->cursor.x = x;
8550 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8551 w->cursor.y = row->y + dy;
8553 if (w == XWINDOW (selected_window))
8555 if (!row->continued_p
8556 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8557 && row->x == 0)
8559 this_line_buffer = XBUFFER (w->buffer);
8561 CHARPOS (this_line_start_pos)
8562 = MATRIX_ROW_START_CHARPOS (row) + delta;
8563 BYTEPOS (this_line_start_pos)
8564 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8566 CHARPOS (this_line_end_pos)
8567 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8568 BYTEPOS (this_line_end_pos)
8569 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8571 this_line_y = w->cursor.y;
8572 this_line_pixel_height = row->height;
8573 this_line_vpos = w->cursor.vpos;
8574 this_line_start_x = row->x;
8576 else
8577 CHARPOS (this_line_start_pos) = 0;
8582 /* Run window scroll functions, if any, for WINDOW with new window
8583 start STARTP. Sets the window start of WINDOW to that position.
8585 We assume that the window's buffer is really current. */
8587 static INLINE struct text_pos
8588 run_window_scroll_functions (window, startp)
8589 Lisp_Object window;
8590 struct text_pos startp;
8592 struct window *w = XWINDOW (window);
8593 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8595 if (current_buffer != XBUFFER (w->buffer))
8596 abort ();
8598 if (!NILP (Vwindow_scroll_functions))
8600 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8601 make_number (CHARPOS (startp)));
8602 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8603 /* In case the hook functions switch buffers. */
8604 if (current_buffer != XBUFFER (w->buffer))
8605 set_buffer_internal_1 (XBUFFER (w->buffer));
8608 return startp;
8612 /* Modify the desired matrix of window W and W->vscroll so that the
8613 line containing the cursor is fully visible. */
8615 static void
8616 make_cursor_line_fully_visible (w)
8617 struct window *w;
8619 struct glyph_matrix *matrix;
8620 struct glyph_row *row;
8621 int window_height, header_line_height;
8623 /* It's not always possible to find the cursor, e.g, when a window
8624 is full of overlay strings. Don't do anything in that case. */
8625 if (w->cursor.vpos < 0)
8626 return;
8628 matrix = w->desired_matrix;
8629 row = MATRIX_ROW (matrix, w->cursor.vpos);
8631 /* If the cursor row is not partially visible, there's nothing
8632 to do. */
8633 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8634 return;
8636 /* If the row the cursor is in is taller than the window's height,
8637 it's not clear what to do, so do nothing. */
8638 window_height = window_box_height (w);
8639 if (row->height >= window_height)
8640 return;
8642 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8644 int dy = row->height - row->visible_height;
8645 w->vscroll = 0;
8646 w->cursor.y += dy;
8647 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8649 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8651 int dy = - (row->height - row->visible_height);
8652 w->vscroll = dy;
8653 w->cursor.y += dy;
8654 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8657 /* When we change the cursor y-position of the selected window,
8658 change this_line_y as well so that the display optimization for
8659 the cursor line of the selected window in redisplay_internal uses
8660 the correct y-position. */
8661 if (w == XWINDOW (selected_window))
8662 this_line_y = w->cursor.y;
8666 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8667 non-zero means only WINDOW is redisplayed in redisplay_internal.
8668 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8669 in redisplay_window to bring a partially visible line into view in
8670 the case that only the cursor has moved.
8672 Value is
8674 1 if scrolling succeeded
8676 0 if scrolling didn't find point.
8678 -1 if new fonts have been loaded so that we must interrupt
8679 redisplay, adjust glyph matrices, and try again. */
8681 static int
8682 try_scrolling (window, just_this_one_p, scroll_conservatively,
8683 scroll_step, temp_scroll_step)
8684 Lisp_Object window;
8685 int just_this_one_p;
8686 int scroll_conservatively, scroll_step;
8687 int temp_scroll_step;
8689 struct window *w = XWINDOW (window);
8690 struct frame *f = XFRAME (w->frame);
8691 struct text_pos scroll_margin_pos;
8692 struct text_pos pos;
8693 struct text_pos startp;
8694 struct it it;
8695 Lisp_Object window_end;
8696 int this_scroll_margin;
8697 int dy = 0;
8698 int scroll_max;
8699 int rc;
8700 int amount_to_scroll = 0;
8701 Lisp_Object aggressive;
8702 int height;
8704 #if GLYPH_DEBUG
8705 debug_method_add (w, "try_scrolling");
8706 #endif
8708 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8710 /* Compute scroll margin height in pixels. We scroll when point is
8711 within this distance from the top or bottom of the window. */
8712 if (scroll_margin > 0)
8714 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8715 this_scroll_margin *= CANON_Y_UNIT (f);
8717 else
8718 this_scroll_margin = 0;
8720 /* Compute how much we should try to scroll maximally to bring point
8721 into view. */
8722 if (scroll_step)
8723 scroll_max = scroll_step;
8724 else if (scroll_conservatively)
8725 scroll_max = scroll_conservatively;
8726 else if (temp_scroll_step)
8727 scroll_max = temp_scroll_step;
8728 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8729 || NUMBERP (current_buffer->scroll_up_aggressively))
8730 /* We're trying to scroll because of aggressive scrolling
8731 but no scroll_step is set. Choose an arbitrary one. Maybe
8732 there should be a variable for this. */
8733 scroll_max = 10;
8734 else
8735 scroll_max = 0;
8736 scroll_max *= CANON_Y_UNIT (f);
8738 /* Decide whether we have to scroll down. Start at the window end
8739 and move this_scroll_margin up to find the position of the scroll
8740 margin. */
8741 window_end = Fwindow_end (window, Qt);
8742 CHARPOS (scroll_margin_pos) = XINT (window_end);
8743 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8744 if (this_scroll_margin)
8746 start_display (&it, w, scroll_margin_pos);
8747 move_it_vertically (&it, - this_scroll_margin);
8748 scroll_margin_pos = it.current.pos;
8751 if (PT >= CHARPOS (scroll_margin_pos))
8753 int y0;
8754 #if 0
8755 int line_height;
8756 #endif
8758 /* Point is in the scroll margin at the bottom of the window, or
8759 below. Compute a new window start that makes point visible. */
8761 /* Compute the distance from the scroll margin to PT.
8762 Give up if the distance is greater than scroll_max. */
8763 start_display (&it, w, scroll_margin_pos);
8764 y0 = it.current_y;
8765 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8766 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8767 #if 0 /* Taking the line's height into account here looks wrong. */
8768 line_height = (it.max_ascent + it.max_descent
8769 ? it.max_ascent + it.max_descent
8770 : last_height);
8771 dy = it.current_y + line_height - y0;
8772 #else
8773 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8774 end, which is one line below the window. The iterator's
8775 current_y will be same as y0 in that case, but we have to
8776 scroll a line to make PT visible. That's the reason why 1 is
8777 added below. */
8778 dy = 1 + it.current_y - y0;
8779 #endif
8781 if (dy > scroll_max)
8782 return 0;
8784 /* Move the window start down. If scrolling conservatively,
8785 move it just enough down to make point visible. If
8786 scroll_step is set, move it down by scroll_step. */
8787 start_display (&it, w, startp);
8789 if (scroll_conservatively)
8790 amount_to_scroll = dy;
8791 else if (scroll_step || temp_scroll_step)
8792 amount_to_scroll = scroll_max;
8793 else
8795 aggressive = current_buffer->scroll_down_aggressively;
8796 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8797 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8798 if (NUMBERP (aggressive))
8799 amount_to_scroll = XFLOATINT (aggressive) * height;
8802 if (amount_to_scroll <= 0)
8803 return 0;
8805 move_it_vertically (&it, amount_to_scroll);
8806 startp = it.current.pos;
8808 else
8810 /* See if point is inside the scroll margin at the top of the
8811 window. */
8812 scroll_margin_pos = startp;
8813 if (this_scroll_margin)
8815 start_display (&it, w, startp);
8816 move_it_vertically (&it, this_scroll_margin);
8817 scroll_margin_pos = it.current.pos;
8820 if (PT < CHARPOS (scroll_margin_pos))
8822 /* Point is in the scroll margin at the top of the window or
8823 above what is displayed in the window. */
8824 int y0;
8826 /* Compute the vertical distance from PT to the scroll
8827 margin position. Give up if distance is greater than
8828 scroll_max. */
8829 SET_TEXT_POS (pos, PT, PT_BYTE);
8830 start_display (&it, w, pos);
8831 y0 = it.current_y;
8832 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8833 it.last_visible_y, -1,
8834 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8835 dy = it.current_y - y0;
8836 if (dy > scroll_max)
8837 return 0;
8839 /* Compute new window start. */
8840 start_display (&it, w, startp);
8842 if (scroll_conservatively)
8843 amount_to_scroll = dy;
8844 else if (scroll_step || temp_scroll_step)
8845 amount_to_scroll = scroll_max;
8846 else
8848 aggressive = current_buffer->scroll_up_aggressively;
8849 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8850 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8851 if (NUMBERP (aggressive))
8852 amount_to_scroll = XFLOATINT (aggressive) * height;
8855 if (amount_to_scroll <= 0)
8856 return 0;
8858 move_it_vertically (&it, - amount_to_scroll);
8859 startp = it.current.pos;
8863 /* Run window scroll functions. */
8864 startp = run_window_scroll_functions (window, startp);
8866 /* Display the window. Give up if new fonts are loaded, or if point
8867 doesn't appear. */
8868 if (!try_window (window, startp))
8869 rc = -1;
8870 else if (w->cursor.vpos < 0)
8872 clear_glyph_matrix (w->desired_matrix);
8873 rc = 0;
8875 else
8877 /* Maybe forget recorded base line for line number display. */
8878 if (!just_this_one_p
8879 || current_buffer->clip_changed
8880 || BEG_UNCHANGED < CHARPOS (startp))
8881 w->base_line_number = Qnil;
8883 /* If cursor ends up on a partially visible line, shift display
8884 lines up or down. */
8885 make_cursor_line_fully_visible (w);
8886 rc = 1;
8889 return rc;
8893 /* Compute a suitable window start for window W if display of W starts
8894 on a continuation line. Value is non-zero if a new window start
8895 was computed.
8897 The new window start will be computed, based on W's width, starting
8898 from the start of the continued line. It is the start of the
8899 screen line with the minimum distance from the old start W->start. */
8901 static int
8902 compute_window_start_on_continuation_line (w)
8903 struct window *w;
8905 struct text_pos pos, start_pos;
8906 int window_start_changed_p = 0;
8908 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8910 /* If window start is on a continuation line... Window start may be
8911 < BEGV in case there's invisible text at the start of the
8912 buffer (M-x rmail, for example). */
8913 if (CHARPOS (start_pos) > BEGV
8914 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8916 struct it it;
8917 struct glyph_row *row;
8919 /* Handle the case that the window start is out of range. */
8920 if (CHARPOS (start_pos) < BEGV)
8921 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8922 else if (CHARPOS (start_pos) > ZV)
8923 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8925 /* Find the start of the continued line. This should be fast
8926 because scan_buffer is fast (newline cache). */
8927 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8928 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8929 row, DEFAULT_FACE_ID);
8930 reseat_at_previous_visible_line_start (&it);
8932 /* If the line start is "too far" away from the window start,
8933 say it takes too much time to compute a new window start. */
8934 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8935 < XFASTINT (w->height) * XFASTINT (w->width))
8937 int min_distance, distance;
8939 /* Move forward by display lines to find the new window
8940 start. If window width was enlarged, the new start can
8941 be expected to be > the old start. If window width was
8942 decreased, the new window start will be < the old start.
8943 So, we're looking for the display line start with the
8944 minimum distance from the old window start. */
8945 pos = it.current.pos;
8946 min_distance = INFINITY;
8947 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8948 distance < min_distance)
8950 min_distance = distance;
8951 pos = it.current.pos;
8952 move_it_by_lines (&it, 1, 0);
8955 /* Set the window start there. */
8956 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8957 window_start_changed_p = 1;
8961 return window_start_changed_p;
8965 /* Try cursor movement in case text has not changes in window WINDOW,
8966 with window start STARTP. Value is
8968 1 if successful
8970 0 if this method cannot be used
8972 -1 if we know we have to scroll the display. *SCROLL_STEP is
8973 set to 1, under certain circumstances, if we want to scroll as
8974 if scroll-step were set to 1. See the code. */
8976 static int
8977 try_cursor_movement (window, startp, scroll_step)
8978 Lisp_Object window;
8979 struct text_pos startp;
8980 int *scroll_step;
8982 struct window *w = XWINDOW (window);
8983 struct frame *f = XFRAME (w->frame);
8984 int rc = 0;
8986 /* Handle case where text has not changed, only point, and it has
8987 not moved off the frame. */
8988 if (/* Point may be in this window. */
8989 PT >= CHARPOS (startp)
8990 /* If we don't check this, we are called to move the cursor in a
8991 horizontally split window with a current matrix that doesn't
8992 fit the display. */
8993 && !windows_or_buffers_changed
8994 /* Selective display hasn't changed. */
8995 && !current_buffer->clip_changed
8996 /* If force-mode-line-update was called, really redisplay;
8997 that's how redisplay is forced after e.g. changing
8998 buffer-invisibility-spec. */
8999 && NILP (w->update_mode_line)
9000 /* Can't use this case if highlighting a region. When a
9001 region exists, cursor movement has to do more than just
9002 set the cursor. */
9003 && !(!NILP (Vtransient_mark_mode)
9004 && !NILP (current_buffer->mark_active))
9005 && NILP (w->region_showing)
9006 && NILP (Vshow_trailing_whitespace)
9007 /* Right after splitting windows, last_point may be nil. */
9008 && INTEGERP (w->last_point)
9009 /* This code is not used for mini-buffer for the sake of the case
9010 of redisplaying to replace an echo area message; since in
9011 that case the mini-buffer contents per se are usually
9012 unchanged. This code is of no real use in the mini-buffer
9013 since the handling of this_line_start_pos, etc., in redisplay
9014 handles the same cases. */
9015 && !EQ (window, minibuf_window)
9016 /* When splitting windows or for new windows, it happens that
9017 redisplay is called with a nil window_end_vpos or one being
9018 larger than the window. This should really be fixed in
9019 window.c. I don't have this on my list, now, so we do
9020 approximately the same as the old redisplay code. --gerd. */
9021 && INTEGERP (w->window_end_vpos)
9022 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9023 && (FRAME_WINDOW_P (f)
9024 || !MARKERP (Voverlay_arrow_position)
9025 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9027 int this_scroll_margin;
9028 struct glyph_row *row;
9030 #if GLYPH_DEBUG
9031 debug_method_add (w, "cursor movement");
9032 #endif
9034 /* Scroll if point within this distance from the top or bottom
9035 of the window. This is a pixel value. */
9036 this_scroll_margin = max (0, scroll_margin);
9037 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9038 this_scroll_margin *= CANON_Y_UNIT (f);
9040 /* Start with the row the cursor was displayed during the last
9041 not paused redisplay. Give up if that row is not valid. */
9042 if (w->last_cursor.vpos < 0
9043 || w->last_cursor.vpos >= w->current_matrix->nrows)
9044 rc = -1;
9045 else
9047 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9048 if (row->mode_line_p)
9049 ++row;
9050 if (!row->enabled_p)
9051 rc = -1;
9054 if (rc == 0)
9056 int scroll_p = 0;
9057 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9060 if (PT > XFASTINT (w->last_point))
9062 /* Point has moved forward. */
9063 while (MATRIX_ROW_END_CHARPOS (row) < PT
9064 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9066 xassert (row->enabled_p);
9067 ++row;
9070 /* The end position of a row equals the start position
9071 of the next row. If PT is there, we would rather
9072 display it in the next line. */
9073 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9074 && MATRIX_ROW_END_CHARPOS (row) == PT
9075 && !cursor_row_p (w, row))
9076 ++row;
9078 /* If within the scroll margin, scroll. Note that
9079 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9080 the next line would be drawn, and that
9081 this_scroll_margin can be zero. */
9082 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9083 || PT > MATRIX_ROW_END_CHARPOS (row)
9084 /* Line is completely visible last line in window
9085 and PT is to be set in the next line. */
9086 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9087 && PT == MATRIX_ROW_END_CHARPOS (row)
9088 && !row->ends_at_zv_p
9089 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9090 scroll_p = 1;
9092 else if (PT < XFASTINT (w->last_point))
9094 /* Cursor has to be moved backward. Note that PT >=
9095 CHARPOS (startp) because of the outer
9096 if-statement. */
9097 while (!row->mode_line_p
9098 && (MATRIX_ROW_START_CHARPOS (row) > PT
9099 || (MATRIX_ROW_START_CHARPOS (row) == PT
9100 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9101 && (row->y > this_scroll_margin
9102 || CHARPOS (startp) == BEGV))
9104 xassert (row->enabled_p);
9105 --row;
9108 /* Consider the following case: Window starts at BEGV,
9109 there is invisible, intangible text at BEGV, so that
9110 display starts at some point START > BEGV. It can
9111 happen that we are called with PT somewhere between
9112 BEGV and START. Try to handle that case. */
9113 if (row < w->current_matrix->rows
9114 || row->mode_line_p)
9116 row = w->current_matrix->rows;
9117 if (row->mode_line_p)
9118 ++row;
9121 /* Due to newlines in overlay strings, we may have to
9122 skip forward over overlay strings. */
9123 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9124 && MATRIX_ROW_END_CHARPOS (row) == PT
9125 && !cursor_row_p (w, row))
9126 ++row;
9128 /* If within the scroll margin, scroll. */
9129 if (row->y < this_scroll_margin
9130 && CHARPOS (startp) != BEGV)
9131 scroll_p = 1;
9134 if (PT < MATRIX_ROW_START_CHARPOS (row)
9135 || PT > MATRIX_ROW_END_CHARPOS (row))
9137 /* if PT is not in the glyph row, give up. */
9138 rc = -1;
9140 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9142 /* If we end up in a partially visible line, let's make it
9143 fully visible, except when it's taller than the window,
9144 in which case we can't do much about it. */
9145 if (row->height > window_box_height (w))
9147 *scroll_step = 1;
9148 rc = -1;
9150 else
9152 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9153 try_window (window, startp);
9154 make_cursor_line_fully_visible (w);
9155 rc = 1;
9158 else if (scroll_p)
9159 rc = -1;
9160 else
9162 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9163 rc = 1;
9168 return rc;
9172 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9173 selected_window is redisplayed. */
9175 static void
9176 redisplay_window (window, just_this_one_p)
9177 Lisp_Object window;
9178 int just_this_one_p;
9180 struct window *w = XWINDOW (window);
9181 struct frame *f = XFRAME (w->frame);
9182 struct buffer *buffer = XBUFFER (w->buffer);
9183 struct buffer *old = current_buffer;
9184 struct text_pos lpoint, opoint, startp;
9185 int update_mode_line;
9186 int tem;
9187 struct it it;
9188 /* Record it now because it's overwritten. */
9189 int current_matrix_up_to_date_p = 0;
9190 int temp_scroll_step = 0;
9191 int count = specpdl_ptr - specpdl;
9192 int rc;
9194 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9195 opoint = lpoint;
9197 /* W must be a leaf window here. */
9198 xassert (!NILP (w->buffer));
9199 #if GLYPH_DEBUG
9200 *w->desired_matrix->method = 0;
9201 #endif
9203 specbind (Qinhibit_point_motion_hooks, Qt);
9205 reconsider_clip_changes (w, buffer);
9207 /* Has the mode line to be updated? */
9208 update_mode_line = (!NILP (w->update_mode_line)
9209 || update_mode_lines
9210 || buffer->clip_changed);
9212 if (MINI_WINDOW_P (w))
9214 if (w == XWINDOW (echo_area_window)
9215 && !NILP (echo_area_buffer[0]))
9217 if (update_mode_line)
9218 /* We may have to update a tty frame's menu bar or a
9219 tool-bar. Example `M-x C-h C-h C-g'. */
9220 goto finish_menu_bars;
9221 else
9222 /* We've already displayed the echo area glyphs in this window. */
9223 goto finish_scroll_bars;
9225 else if (w != XWINDOW (minibuf_window))
9227 /* W is a mini-buffer window, but it's not the currently
9228 active one, so clear it. */
9229 int yb = window_text_bottom_y (w);
9230 struct glyph_row *row;
9231 int y;
9233 for (y = 0, row = w->desired_matrix->rows;
9234 y < yb;
9235 y += row->height, ++row)
9236 blank_row (w, row, y);
9237 goto finish_scroll_bars;
9241 /* Otherwise set up data on this window; select its buffer and point
9242 value. */
9243 /* Really select the buffer, for the sake of buffer-local
9244 variables. */
9245 set_buffer_internal_1 (XBUFFER (w->buffer));
9246 SET_TEXT_POS (opoint, PT, PT_BYTE);
9248 current_matrix_up_to_date_p
9249 = (!NILP (w->window_end_valid)
9250 && !current_buffer->clip_changed
9251 && XFASTINT (w->last_modified) >= MODIFF
9252 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9254 /* When windows_or_buffers_changed is non-zero, we can't rely on
9255 the window end being valid, so set it to nil there. */
9256 if (windows_or_buffers_changed)
9258 /* If window starts on a continuation line, maybe adjust the
9259 window start in case the window's width changed. */
9260 if (XMARKER (w->start)->buffer == current_buffer)
9261 compute_window_start_on_continuation_line (w);
9263 w->window_end_valid = Qnil;
9266 /* Some sanity checks. */
9267 CHECK_WINDOW_END (w);
9268 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9269 abort ();
9270 if (BYTEPOS (opoint) < CHARPOS (opoint))
9271 abort ();
9273 /* If %c is in mode line, update it if needed. */
9274 if (!NILP (w->column_number_displayed)
9275 /* This alternative quickly identifies a common case
9276 where no change is needed. */
9277 && !(PT == XFASTINT (w->last_point)
9278 && XFASTINT (w->last_modified) >= MODIFF
9279 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9280 && XFASTINT (w->column_number_displayed) != current_column ())
9281 update_mode_line = 1;
9283 /* Count number of windows showing the selected buffer. An indirect
9284 buffer counts as its base buffer. */
9285 if (!just_this_one_p)
9287 struct buffer *current_base, *window_base;
9288 current_base = current_buffer;
9289 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9290 if (current_base->base_buffer)
9291 current_base = current_base->base_buffer;
9292 if (window_base->base_buffer)
9293 window_base = window_base->base_buffer;
9294 if (current_base == window_base)
9295 buffer_shared++;
9298 /* Point refers normally to the selected window. For any other
9299 window, set up appropriate value. */
9300 if (!EQ (window, selected_window))
9302 int new_pt = XMARKER (w->pointm)->charpos;
9303 int new_pt_byte = marker_byte_position (w->pointm);
9304 if (new_pt < BEGV)
9306 new_pt = BEGV;
9307 new_pt_byte = BEGV_BYTE;
9308 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9310 else if (new_pt > (ZV - 1))
9312 new_pt = ZV;
9313 new_pt_byte = ZV_BYTE;
9314 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9317 /* We don't use SET_PT so that the point-motion hooks don't run. */
9318 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9321 /* If any of the character widths specified in the display table
9322 have changed, invalidate the width run cache. It's true that
9323 this may be a bit late to catch such changes, but the rest of
9324 redisplay goes (non-fatally) haywire when the display table is
9325 changed, so why should we worry about doing any better? */
9326 if (current_buffer->width_run_cache)
9328 struct Lisp_Char_Table *disptab = buffer_display_table ();
9330 if (! disptab_matches_widthtab (disptab,
9331 XVECTOR (current_buffer->width_table)))
9333 invalidate_region_cache (current_buffer,
9334 current_buffer->width_run_cache,
9335 BEG, Z);
9336 recompute_width_table (current_buffer, disptab);
9340 /* If window-start is screwed up, choose a new one. */
9341 if (XMARKER (w->start)->buffer != current_buffer)
9342 goto recenter;
9344 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9346 /* If someone specified a new starting point but did not insist,
9347 check whether it can be used. */
9348 if (!NILP (w->optional_new_start)
9349 && CHARPOS (startp) >= BEGV
9350 && CHARPOS (startp) <= ZV)
9352 w->optional_new_start = Qnil;
9353 start_display (&it, w, startp);
9354 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9355 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9356 if (IT_CHARPOS (it) == PT)
9357 w->force_start = Qt;
9360 /* Handle case where place to start displaying has been specified,
9361 unless the specified location is outside the accessible range. */
9362 if (!NILP (w->force_start)
9363 || w->frozen_window_start_p)
9365 w->force_start = Qnil;
9366 w->vscroll = 0;
9367 w->window_end_valid = Qnil;
9369 /* Forget any recorded base line for line number display. */
9370 if (!current_matrix_up_to_date_p
9371 || current_buffer->clip_changed)
9372 w->base_line_number = Qnil;
9374 /* Redisplay the mode line. Select the buffer properly for that.
9375 Also, run the hook window-scroll-functions
9376 because we have scrolled. */
9377 /* Note, we do this after clearing force_start because
9378 if there's an error, it is better to forget about force_start
9379 than to get into an infinite loop calling the hook functions
9380 and having them get more errors. */
9381 if (!update_mode_line
9382 || ! NILP (Vwindow_scroll_functions))
9384 update_mode_line = 1;
9385 w->update_mode_line = Qt;
9386 startp = run_window_scroll_functions (window, startp);
9389 XSETFASTINT (w->last_modified, 0);
9390 XSETFASTINT (w->last_overlay_modified, 0);
9391 if (CHARPOS (startp) < BEGV)
9392 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9393 else if (CHARPOS (startp) > ZV)
9394 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9396 /* Redisplay, then check if cursor has been set during the
9397 redisplay. Give up if new fonts were loaded. */
9398 if (!try_window (window, startp))
9400 w->force_start = Qt;
9401 clear_glyph_matrix (w->desired_matrix);
9402 goto restore_buffers;
9405 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9407 /* If point does not appear, try to move point so it does
9408 appear. The desired matrix has been built above, so we
9409 can use it here. */
9410 int window_height;
9411 struct glyph_row *row;
9413 window_height = window_box_height (w) / 2;
9414 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9415 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9416 ++row;
9418 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9419 MATRIX_ROW_START_BYTEPOS (row));
9421 if (w != XWINDOW (selected_window))
9422 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9423 else if (current_buffer == old)
9424 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9426 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9428 /* If we are highlighting the region, then we just changed
9429 the region, so redisplay to show it. */
9430 if (!NILP (Vtransient_mark_mode)
9431 && !NILP (current_buffer->mark_active))
9433 clear_glyph_matrix (w->desired_matrix);
9434 if (!try_window (window, startp))
9435 goto restore_buffers;
9439 make_cursor_line_fully_visible (w);
9440 #if GLYPH_DEBUG
9441 debug_method_add (w, "forced window start");
9442 #endif
9443 goto done;
9446 /* Handle case where text has not changed, only point, and it has
9447 not moved off the frame. */
9448 if (current_matrix_up_to_date_p
9449 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9450 rc != 0))
9452 if (rc == -1)
9453 goto try_to_scroll;
9454 else
9455 goto done;
9457 /* If current starting point was originally the beginning of a line
9458 but no longer is, find a new starting point. */
9459 else if (!NILP (w->start_at_line_beg)
9460 && !(CHARPOS (startp) <= BEGV
9461 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9463 #if GLYPH_DEBUG
9464 debug_method_add (w, "recenter 1");
9465 #endif
9466 goto recenter;
9469 /* Try scrolling with try_window_id. */
9470 else if (/* Windows and buffers haven't changed. */
9471 !windows_or_buffers_changed
9472 /* Window must be either use window-based redisplay or
9473 be full width. */
9474 && (FRAME_WINDOW_P (f)
9475 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9476 && !MINI_WINDOW_P (w)
9477 /* Point is not known NOT to appear in window. */
9478 && PT >= CHARPOS (startp)
9479 && XFASTINT (w->last_modified)
9480 /* Window is not hscrolled. */
9481 && XFASTINT (w->hscroll) == 0
9482 /* Selective display has not changed. */
9483 && !current_buffer->clip_changed
9484 /* Current matrix is up to date. */
9485 && !NILP (w->window_end_valid)
9486 /* Can't use this case if highlighting a region because
9487 a cursor movement will do more than just set the cursor. */
9488 && !(!NILP (Vtransient_mark_mode)
9489 && !NILP (current_buffer->mark_active))
9490 && NILP (w->region_showing)
9491 && NILP (Vshow_trailing_whitespace)
9492 /* Overlay arrow position and string not changed. */
9493 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9494 && EQ (last_arrow_string, Voverlay_arrow_string)
9495 /* Value is > 0 if update has been done, it is -1 if we
9496 know that the same window start will not work. It is 0
9497 if unsuccessful for some other reason. */
9498 && (tem = try_window_id (w)) != 0)
9500 #if GLYPH_DEBUG
9501 debug_method_add (w, "try_window_id %d", tem);
9502 #endif
9504 if (fonts_changed_p)
9505 goto restore_buffers;
9506 if (tem > 0)
9507 goto done;
9509 /* Otherwise try_window_id has returned -1 which means that we
9510 don't want the alternative below this comment to execute. */
9512 else if (CHARPOS (startp) >= BEGV
9513 && CHARPOS (startp) <= ZV
9514 && PT >= CHARPOS (startp)
9515 && (CHARPOS (startp) < ZV
9516 /* Avoid starting at end of buffer. */
9517 || CHARPOS (startp) == BEGV
9518 || (XFASTINT (w->last_modified) >= MODIFF
9519 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9521 #if GLYPH_DEBUG
9522 debug_method_add (w, "same window start");
9523 #endif
9525 /* Try to redisplay starting at same place as before.
9526 If point has not moved off frame, accept the results. */
9527 if (!current_matrix_up_to_date_p
9528 /* Don't use try_window_reusing_current_matrix in this case
9529 because a window scroll function can have changed the
9530 buffer. */
9531 || !NILP (Vwindow_scroll_functions)
9532 || MINI_WINDOW_P (w)
9533 || !try_window_reusing_current_matrix (w))
9535 IF_DEBUG (debug_method_add (w, "1"));
9536 try_window (window, startp);
9539 if (fonts_changed_p)
9540 goto restore_buffers;
9542 if (w->cursor.vpos >= 0)
9544 if (!just_this_one_p
9545 || current_buffer->clip_changed
9546 || BEG_UNCHANGED < CHARPOS (startp))
9547 /* Forget any recorded base line for line number display. */
9548 w->base_line_number = Qnil;
9550 make_cursor_line_fully_visible (w);
9551 goto done;
9553 else
9554 clear_glyph_matrix (w->desired_matrix);
9557 try_to_scroll:
9559 XSETFASTINT (w->last_modified, 0);
9560 XSETFASTINT (w->last_overlay_modified, 0);
9562 /* Redisplay the mode line. Select the buffer properly for that. */
9563 if (!update_mode_line)
9565 update_mode_line = 1;
9566 w->update_mode_line = Qt;
9569 /* Try to scroll by specified few lines. */
9570 if ((scroll_conservatively
9571 || scroll_step
9572 || temp_scroll_step
9573 || NUMBERP (current_buffer->scroll_up_aggressively)
9574 || NUMBERP (current_buffer->scroll_down_aggressively))
9575 && !current_buffer->clip_changed
9576 && CHARPOS (startp) >= BEGV
9577 && CHARPOS (startp) <= ZV)
9579 /* The function returns -1 if new fonts were loaded, 1 if
9580 successful, 0 if not successful. */
9581 int rc = try_scrolling (window, just_this_one_p,
9582 scroll_conservatively,
9583 scroll_step,
9584 temp_scroll_step);
9585 if (rc > 0)
9586 goto done;
9587 else if (rc < 0)
9588 goto restore_buffers;
9591 /* Finally, just choose place to start which centers point */
9593 recenter:
9595 #if GLYPH_DEBUG
9596 debug_method_add (w, "recenter");
9597 #endif
9599 /* w->vscroll = 0; */
9601 /* Forget any previously recorded base line for line number display. */
9602 if (!current_matrix_up_to_date_p
9603 || current_buffer->clip_changed)
9604 w->base_line_number = Qnil;
9606 /* Move backward half the height of the window. */
9607 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9608 it.current_y = it.last_visible_y;
9609 move_it_vertically_backward (&it, it.last_visible_y / 2);
9610 xassert (IT_CHARPOS (it) >= BEGV);
9612 /* The function move_it_vertically_backward may move over more
9613 than the specified y-distance. If it->w is small, e.g. a
9614 mini-buffer window, we may end up in front of the window's
9615 display area. Start displaying at the start of the line
9616 containing PT in this case. */
9617 if (it.current_y <= 0)
9619 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9620 move_it_vertically (&it, 0);
9621 xassert (IT_CHARPOS (it) <= PT);
9622 it.current_y = 0;
9625 it.current_x = it.hpos = 0;
9627 /* Set startp here explicitly in case that helps avoid an infinite loop
9628 in case the window-scroll-functions functions get errors. */
9629 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9631 /* Run scroll hooks. */
9632 startp = run_window_scroll_functions (window, it.current.pos);
9634 /* Redisplay the window. */
9635 if (!current_matrix_up_to_date_p
9636 || windows_or_buffers_changed
9637 /* Don't use try_window_reusing_current_matrix in this case
9638 because it can have changed the buffer. */
9639 || !NILP (Vwindow_scroll_functions)
9640 || !just_this_one_p
9641 || MINI_WINDOW_P (w)
9642 || !try_window_reusing_current_matrix (w))
9643 try_window (window, startp);
9645 /* If new fonts have been loaded (due to fontsets), give up. We
9646 have to start a new redisplay since we need to re-adjust glyph
9647 matrices. */
9648 if (fonts_changed_p)
9649 goto restore_buffers;
9651 /* If cursor did not appear assume that the middle of the window is
9652 in the first line of the window. Do it again with the next line.
9653 (Imagine a window of height 100, displaying two lines of height
9654 60. Moving back 50 from it->last_visible_y will end in the first
9655 line.) */
9656 if (w->cursor.vpos < 0)
9658 if (!NILP (w->window_end_valid)
9659 && PT >= Z - XFASTINT (w->window_end_pos))
9661 clear_glyph_matrix (w->desired_matrix);
9662 move_it_by_lines (&it, 1, 0);
9663 try_window (window, it.current.pos);
9665 else if (PT < IT_CHARPOS (it))
9667 clear_glyph_matrix (w->desired_matrix);
9668 move_it_by_lines (&it, -1, 0);
9669 try_window (window, it.current.pos);
9671 else
9673 /* Not much we can do about it. */
9677 /* Consider the following case: Window starts at BEGV, there is
9678 invisible, intangible text at BEGV, so that display starts at
9679 some point START > BEGV. It can happen that we are called with
9680 PT somewhere between BEGV and START. Try to handle that case. */
9681 if (w->cursor.vpos < 0)
9683 struct glyph_row *row = w->current_matrix->rows;
9684 if (row->mode_line_p)
9685 ++row;
9686 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9689 make_cursor_line_fully_visible (w);
9691 done:
9693 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9694 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9695 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9696 ? Qt : Qnil);
9698 /* Display the mode line, if we must. */
9699 if ((update_mode_line
9700 /* If window not full width, must redo its mode line
9701 if (a) the window to its side is being redone and
9702 (b) we do a frame-based redisplay. This is a consequence
9703 of how inverted lines are drawn in frame-based redisplay. */
9704 || (!just_this_one_p
9705 && !FRAME_WINDOW_P (f)
9706 && !WINDOW_FULL_WIDTH_P (w))
9707 /* Line number to display. */
9708 || INTEGERP (w->base_line_pos)
9709 /* Column number is displayed and different from the one displayed. */
9710 || (!NILP (w->column_number_displayed)
9711 && XFASTINT (w->column_number_displayed) != current_column ()))
9712 /* This means that the window has a mode line. */
9713 && (WINDOW_WANTS_MODELINE_P (w)
9714 || WINDOW_WANTS_HEADER_LINE_P (w)))
9716 Lisp_Object old_selected_frame;
9718 old_selected_frame = selected_frame;
9720 XSETFRAME (selected_frame, f);
9721 display_mode_lines (w);
9722 selected_frame = old_selected_frame;
9724 /* If mode line height has changed, arrange for a thorough
9725 immediate redisplay using the correct mode line height. */
9726 if (WINDOW_WANTS_MODELINE_P (w)
9727 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9729 fonts_changed_p = 1;
9730 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9731 = DESIRED_MODE_LINE_HEIGHT (w);
9734 /* If top line height has changed, arrange for a thorough
9735 immediate redisplay using the correct mode line height. */
9736 if (WINDOW_WANTS_HEADER_LINE_P (w)
9737 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9739 fonts_changed_p = 1;
9740 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9741 = DESIRED_HEADER_LINE_HEIGHT (w);
9744 if (fonts_changed_p)
9745 goto restore_buffers;
9748 if (!line_number_displayed
9749 && !BUFFERP (w->base_line_pos))
9751 w->base_line_pos = Qnil;
9752 w->base_line_number = Qnil;
9755 finish_menu_bars:
9757 /* When we reach a frame's selected window, redo the frame's menu bar. */
9758 if (update_mode_line
9759 && EQ (FRAME_SELECTED_WINDOW (f), window))
9761 int redisplay_menu_p = 0;
9763 if (FRAME_WINDOW_P (f))
9765 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9766 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9767 #else
9768 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9769 #endif
9771 else
9772 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9774 if (redisplay_menu_p)
9775 display_menu_bar (w);
9777 #ifdef HAVE_WINDOW_SYSTEM
9778 if (WINDOWP (f->tool_bar_window)
9779 && (FRAME_TOOL_BAR_LINES (f) > 0
9780 || auto_resize_tool_bars_p))
9781 redisplay_tool_bar (f);
9782 #endif
9785 finish_scroll_bars:
9787 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9789 int start, end, whole;
9791 /* Calculate the start and end positions for the current window.
9792 At some point, it would be nice to choose between scrollbars
9793 which reflect the whole buffer size, with special markers
9794 indicating narrowing, and scrollbars which reflect only the
9795 visible region.
9797 Note that mini-buffers sometimes aren't displaying any text. */
9798 if (!MINI_WINDOW_P (w)
9799 || (w == XWINDOW (minibuf_window)
9800 && NILP (echo_area_buffer[0])))
9802 whole = ZV - BEGV;
9803 start = marker_position (w->start) - BEGV;
9804 /* I don't think this is guaranteed to be right. For the
9805 moment, we'll pretend it is. */
9806 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9808 if (end < start)
9809 end = start;
9810 if (whole < (end - start))
9811 whole = end - start;
9813 else
9814 start = end = whole = 0;
9816 /* Indicate what this scroll bar ought to be displaying now. */
9817 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9819 /* Note that we actually used the scroll bar attached to this
9820 window, so it shouldn't be deleted at the end of redisplay. */
9821 (*redeem_scroll_bar_hook) (w);
9824 restore_buffers:
9826 /* Restore current_buffer and value of point in it. */
9827 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9828 set_buffer_internal_1 (old);
9829 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9831 unbind_to (count, Qnil);
9835 /* Build the complete desired matrix of WINDOW with a window start
9836 buffer position POS. Value is non-zero if successful. It is zero
9837 if fonts were loaded during redisplay which makes re-adjusting
9838 glyph matrices necessary. */
9841 try_window (window, pos)
9842 Lisp_Object window;
9843 struct text_pos pos;
9845 struct window *w = XWINDOW (window);
9846 struct it it;
9847 struct glyph_row *last_text_row = NULL;
9849 /* Make POS the new window start. */
9850 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9852 /* Mark cursor position as unknown. No overlay arrow seen. */
9853 w->cursor.vpos = -1;
9854 overlay_arrow_seen = 0;
9856 /* Initialize iterator and info to start at POS. */
9857 start_display (&it, w, pos);
9859 /* Display all lines of W. */
9860 while (it.current_y < it.last_visible_y)
9862 if (display_line (&it))
9863 last_text_row = it.glyph_row - 1;
9864 if (fonts_changed_p)
9865 return 0;
9868 /* If bottom moved off end of frame, change mode line percentage. */
9869 if (XFASTINT (w->window_end_pos) <= 0
9870 && Z != IT_CHARPOS (it))
9871 w->update_mode_line = Qt;
9873 /* Set window_end_pos to the offset of the last character displayed
9874 on the window from the end of current_buffer. Set
9875 window_end_vpos to its row number. */
9876 if (last_text_row)
9878 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9879 w->window_end_bytepos
9880 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9881 XSETFASTINT (w->window_end_pos,
9882 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9883 XSETFASTINT (w->window_end_vpos,
9884 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9885 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9886 ->displays_text_p);
9888 else
9890 w->window_end_bytepos = 0;
9891 XSETFASTINT (w->window_end_pos, 0);
9892 XSETFASTINT (w->window_end_vpos, 0);
9895 /* But that is not valid info until redisplay finishes. */
9896 w->window_end_valid = Qnil;
9897 return 1;
9902 /************************************************************************
9903 Window redisplay reusing current matrix when buffer has not changed
9904 ************************************************************************/
9906 /* Try redisplay of window W showing an unchanged buffer with a
9907 different window start than the last time it was displayed by
9908 reusing its current matrix. Value is non-zero if successful.
9909 W->start is the new window start. */
9911 static int
9912 try_window_reusing_current_matrix (w)
9913 struct window *w;
9915 struct frame *f = XFRAME (w->frame);
9916 struct glyph_row *row, *bottom_row;
9917 struct it it;
9918 struct run run;
9919 struct text_pos start, new_start;
9920 int nrows_scrolled, i;
9921 struct glyph_row *last_text_row;
9922 struct glyph_row *last_reused_text_row;
9923 struct glyph_row *start_row;
9924 int start_vpos, min_y, max_y;
9926 if (/* This function doesn't handle terminal frames. */
9927 !FRAME_WINDOW_P (f)
9928 /* Don't try to reuse the display if windows have been split
9929 or such. */
9930 || windows_or_buffers_changed)
9931 return 0;
9933 /* Can't do this if region may have changed. */
9934 if ((!NILP (Vtransient_mark_mode)
9935 && !NILP (current_buffer->mark_active))
9936 || !NILP (w->region_showing)
9937 || !NILP (Vshow_trailing_whitespace))
9938 return 0;
9940 /* If top-line visibility has changed, give up. */
9941 if (WINDOW_WANTS_HEADER_LINE_P (w)
9942 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9943 return 0;
9945 /* Give up if old or new display is scrolled vertically. We could
9946 make this function handle this, but right now it doesn't. */
9947 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9948 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9949 return 0;
9951 /* The variable new_start now holds the new window start. The old
9952 start `start' can be determined from the current matrix. */
9953 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9954 start = start_row->start.pos;
9955 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9957 /* Clear the desired matrix for the display below. */
9958 clear_glyph_matrix (w->desired_matrix);
9960 if (CHARPOS (new_start) <= CHARPOS (start))
9962 int first_row_y;
9964 IF_DEBUG (debug_method_add (w, "twu1"));
9966 /* Display up to a row that can be reused. The variable
9967 last_text_row is set to the last row displayed that displays
9968 text. Note that it.vpos == 0 if or if not there is a
9969 header-line; it's not the same as the MATRIX_ROW_VPOS! */
9970 start_display (&it, w, new_start);
9971 first_row_y = it.current_y;
9972 w->cursor.vpos = -1;
9973 last_text_row = last_reused_text_row = NULL;
9975 while (it.current_y < it.last_visible_y
9976 && IT_CHARPOS (it) < CHARPOS (start)
9977 && !fonts_changed_p)
9978 if (display_line (&it))
9979 last_text_row = it.glyph_row - 1;
9981 /* A value of current_y < last_visible_y means that we stopped
9982 at the previous window start, which in turn means that we
9983 have at least one reusable row. */
9984 if (it.current_y < it.last_visible_y)
9986 /* IT.vpos always starts from 0; it counts text lines. */
9987 nrows_scrolled = it.vpos;
9989 /* Find PT if not already found in the lines displayed. */
9990 if (w->cursor.vpos < 0)
9992 int dy = it.current_y - first_row_y;
9994 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9995 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9997 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9998 && PT < MATRIX_ROW_END_CHARPOS (row))
10000 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10001 dy, nrows_scrolled);
10002 break;
10005 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10006 break;
10008 ++row;
10011 /* Give up if point was not found. This shouldn't
10012 happen often; not more often than with try_window
10013 itself. */
10014 if (w->cursor.vpos < 0)
10016 clear_glyph_matrix (w->desired_matrix);
10017 return 0;
10021 /* Scroll the display. Do it before the current matrix is
10022 changed. The problem here is that update has not yet
10023 run, i.e. part of the current matrix is not up to date.
10024 scroll_run_hook will clear the cursor, and use the
10025 current matrix to get the height of the row the cursor is
10026 in. */
10027 run.current_y = first_row_y;
10028 run.desired_y = it.current_y;
10029 run.height = it.last_visible_y - it.current_y;
10031 if (run.height > 0 && run.current_y != run.desired_y)
10033 update_begin (f);
10034 rif->update_window_begin_hook (w);
10035 rif->clear_mouse_face (w);
10036 rif->scroll_run_hook (w, &run);
10037 rif->update_window_end_hook (w, 0, 0);
10038 update_end (f);
10041 /* Shift current matrix down by nrows_scrolled lines. */
10042 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10043 rotate_matrix (w->current_matrix,
10044 start_vpos,
10045 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10046 nrows_scrolled);
10048 /* Disable lines not reused. */
10049 for (i = 0; i < it.vpos; ++i)
10050 (start_row + i)->enabled_p = 0;
10052 /* Re-compute Y positions. */
10053 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10054 max_y = it.last_visible_y;
10055 for (row = start_row + nrows_scrolled;
10056 row < bottom_row;
10057 ++row)
10059 row->y = it.current_y;
10061 if (row->y < min_y)
10062 row->visible_height = row->height - (min_y - row->y);
10063 else if (row->y + row->height > max_y)
10064 row->visible_height
10065 = row->height - (row->y + row->height - max_y);
10066 else
10067 row->visible_height = row->height;
10069 it.current_y += row->height;
10071 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10072 last_reused_text_row = row;
10073 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10074 break;
10078 /* Update window_end_pos etc.; last_reused_text_row is the last
10079 reused row from the current matrix containing text, if any.
10080 The value of last_text_row is the last displayed line
10081 containing text. */
10082 if (last_reused_text_row)
10084 w->window_end_bytepos
10085 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10086 XSETFASTINT (w->window_end_pos,
10087 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10088 XSETFASTINT (w->window_end_vpos,
10089 MATRIX_ROW_VPOS (last_reused_text_row,
10090 w->current_matrix));
10092 else if (last_text_row)
10094 w->window_end_bytepos
10095 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10096 XSETFASTINT (w->window_end_pos,
10097 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10098 XSETFASTINT (w->window_end_vpos,
10099 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10101 else
10103 /* This window must be completely empty. */
10104 w->window_end_bytepos = 0;
10105 XSETFASTINT (w->window_end_pos, 0);
10106 XSETFASTINT (w->window_end_vpos, 0);
10108 w->window_end_valid = Qnil;
10110 /* Update hint: don't try scrolling again in update_window. */
10111 w->desired_matrix->no_scrolling_p = 1;
10113 #if GLYPH_DEBUG
10114 debug_method_add (w, "try_window_reusing_current_matrix 1");
10115 #endif
10116 return 1;
10118 else if (CHARPOS (new_start) > CHARPOS (start))
10120 struct glyph_row *pt_row, *row;
10121 struct glyph_row *first_reusable_row;
10122 struct glyph_row *first_row_to_display;
10123 int dy;
10124 int yb = window_text_bottom_y (w);
10126 IF_DEBUG (debug_method_add (w, "twu2"));
10128 /* Find the row starting at new_start, if there is one. Don't
10129 reuse a partially visible line at the end. */
10130 first_reusable_row = start_row;
10131 while (first_reusable_row->enabled_p
10132 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10133 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10134 < CHARPOS (new_start)))
10135 ++first_reusable_row;
10137 /* Give up if there is no row to reuse. */
10138 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10139 || !first_reusable_row->enabled_p
10140 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10141 != CHARPOS (new_start)))
10142 return 0;
10144 /* We can reuse fully visible rows beginning with
10145 first_reusable_row to the end of the window. Set
10146 first_row_to_display to the first row that cannot be reused.
10147 Set pt_row to the row containing point, if there is any. */
10148 first_row_to_display = first_reusable_row;
10149 pt_row = NULL;
10150 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10152 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10153 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10154 pt_row = first_row_to_display;
10156 ++first_row_to_display;
10159 /* Start displaying at the start of first_row_to_display. */
10160 xassert (first_row_to_display->y < yb);
10161 init_to_row_start (&it, w, first_row_to_display);
10162 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10163 - start_vpos);
10164 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10165 - nrows_scrolled);
10166 it.current_y = (first_row_to_display->y - first_reusable_row->y
10167 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10169 /* Display lines beginning with first_row_to_display in the
10170 desired matrix. Set last_text_row to the last row displayed
10171 that displays text. */
10172 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10173 if (pt_row == NULL)
10174 w->cursor.vpos = -1;
10175 last_text_row = NULL;
10176 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10177 if (display_line (&it))
10178 last_text_row = it.glyph_row - 1;
10180 /* Give up If point isn't in a row displayed or reused. */
10181 if (w->cursor.vpos < 0)
10183 clear_glyph_matrix (w->desired_matrix);
10184 return 0;
10187 /* If point is in a reused row, adjust y and vpos of the cursor
10188 position. */
10189 if (pt_row)
10191 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10192 w->current_matrix);
10193 w->cursor.y -= first_reusable_row->y;
10196 /* Scroll the display. */
10197 run.current_y = first_reusable_row->y;
10198 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10199 run.height = it.last_visible_y - run.current_y;
10200 dy = run.current_y - run.desired_y;
10202 if (run.height)
10204 struct frame *f = XFRAME (WINDOW_FRAME (w));
10205 update_begin (f);
10206 rif->update_window_begin_hook (w);
10207 rif->clear_mouse_face (w);
10208 rif->scroll_run_hook (w, &run);
10209 rif->update_window_end_hook (w, 0, 0);
10210 update_end (f);
10213 /* Adjust Y positions of reused rows. */
10214 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10215 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10216 max_y = it.last_visible_y;
10217 for (row = first_reusable_row; row < first_row_to_display; ++row)
10219 row->y -= dy;
10220 if (row->y < min_y)
10221 row->visible_height = row->height - (min_y - row->y);
10222 else if (row->y + row->height > max_y)
10223 row->visible_height
10224 = row->height - (row->y + row->height - max_y);
10225 else
10226 row->visible_height = row->height;
10229 /* Disable rows not reused. */
10230 while (row < bottom_row)
10232 row->enabled_p = 0;
10233 ++row;
10236 /* Scroll the current matrix. */
10237 xassert (nrows_scrolled > 0);
10238 rotate_matrix (w->current_matrix,
10239 start_vpos,
10240 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10241 -nrows_scrolled);
10243 /* Adjust window end. A null value of last_text_row means that
10244 the window end is in reused rows which in turn means that
10245 only its vpos can have changed. */
10246 if (last_text_row)
10248 w->window_end_bytepos
10249 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10250 XSETFASTINT (w->window_end_pos,
10251 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10252 XSETFASTINT (w->window_end_vpos,
10253 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10255 else
10257 XSETFASTINT (w->window_end_vpos,
10258 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10261 w->window_end_valid = Qnil;
10262 w->desired_matrix->no_scrolling_p = 1;
10264 #if GLYPH_DEBUG
10265 debug_method_add (w, "try_window_reusing_current_matrix 2");
10266 #endif
10267 return 1;
10270 return 0;
10275 /************************************************************************
10276 Window redisplay reusing current matrix when buffer has changed
10277 ************************************************************************/
10279 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10280 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10281 int *, int *));
10282 static struct glyph_row *
10283 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10284 struct glyph_row *));
10287 /* Return the last row in MATRIX displaying text. If row START is
10288 non-null, start searching with that row. IT gives the dimensions
10289 of the display. Value is null if matrix is empty; otherwise it is
10290 a pointer to the row found. */
10292 static struct glyph_row *
10293 find_last_row_displaying_text (matrix, it, start)
10294 struct glyph_matrix *matrix;
10295 struct it *it;
10296 struct glyph_row *start;
10298 struct glyph_row *row, *row_found;
10300 /* Set row_found to the last row in IT->w's current matrix
10301 displaying text. The loop looks funny but think of partially
10302 visible lines. */
10303 row_found = NULL;
10304 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10305 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10307 xassert (row->enabled_p);
10308 row_found = row;
10309 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10310 break;
10311 ++row;
10314 return row_found;
10318 /* Return the last row in the current matrix of W that is not affected
10319 by changes at the start of current_buffer that occurred since the
10320 last time W was redisplayed. Value is null if no such row exists.
10322 The global variable beg_unchanged has to contain the number of
10323 bytes unchanged at the start of current_buffer. BEG +
10324 beg_unchanged is the buffer position of the first changed byte in
10325 current_buffer. Characters at positions < BEG + beg_unchanged are
10326 at the same buffer positions as they were when the current matrix
10327 was built. */
10329 static struct glyph_row *
10330 find_last_unchanged_at_beg_row (w)
10331 struct window *w;
10333 int first_changed_pos = BEG + BEG_UNCHANGED;
10334 struct glyph_row *row;
10335 struct glyph_row *row_found = NULL;
10336 int yb = window_text_bottom_y (w);
10338 /* Find the last row displaying unchanged text. */
10339 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10340 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10341 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10343 if (/* If row ends before first_changed_pos, it is unchanged,
10344 except in some case. */
10345 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10346 /* When row ends in ZV and we write at ZV it is not
10347 unchanged. */
10348 && !row->ends_at_zv_p
10349 /* When first_changed_pos is the end of a continued line,
10350 row is not unchanged because it may be no longer
10351 continued. */
10352 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10353 && row->continued_p))
10354 row_found = row;
10356 /* Stop if last visible row. */
10357 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10358 break;
10360 ++row;
10363 return row_found;
10367 /* Find the first glyph row in the current matrix of W that is not
10368 affected by changes at the end of current_buffer since the last
10369 time the window was redisplayed. Return in *DELTA the number of
10370 chars by which buffer positions in unchanged text at the end of
10371 current_buffer must be adjusted. Return in *DELTA_BYTES the
10372 corresponding number of bytes. Value is null if no such row
10373 exists, i.e. all rows are affected by changes. */
10375 static struct glyph_row *
10376 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10377 struct window *w;
10378 int *delta, *delta_bytes;
10380 struct glyph_row *row;
10381 struct glyph_row *row_found = NULL;
10383 *delta = *delta_bytes = 0;
10385 /* Display must not have been paused, otherwise the current matrix
10386 is not up to date. */
10387 if (NILP (w->window_end_valid))
10388 abort ();
10390 /* A value of window_end_pos >= END_UNCHANGED means that the window
10391 end is in the range of changed text. If so, there is no
10392 unchanged row at the end of W's current matrix. */
10393 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10394 return NULL;
10396 /* Set row to the last row in W's current matrix displaying text. */
10397 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10399 /* If matrix is entirely empty, no unchanged row exists. */
10400 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10402 /* The value of row is the last glyph row in the matrix having a
10403 meaningful buffer position in it. The end position of row
10404 corresponds to window_end_pos. This allows us to translate
10405 buffer positions in the current matrix to current buffer
10406 positions for characters not in changed text. */
10407 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10408 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10409 int last_unchanged_pos, last_unchanged_pos_old;
10410 struct glyph_row *first_text_row
10411 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10413 *delta = Z - Z_old;
10414 *delta_bytes = Z_BYTE - Z_BYTE_old;
10416 /* Set last_unchanged_pos to the buffer position of the last
10417 character in the buffer that has not been changed. Z is the
10418 index + 1 of the last byte in current_buffer, i.e. by
10419 subtracting end_unchanged we get the index of the last
10420 unchanged character, and we have to add BEG to get its buffer
10421 position. */
10422 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10423 last_unchanged_pos_old = last_unchanged_pos - *delta;
10425 /* Search backward from ROW for a row displaying a line that
10426 starts at a minimum position >= last_unchanged_pos_old. */
10427 for (; row > first_text_row; --row)
10429 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10430 abort ();
10432 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10433 row_found = row;
10437 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10438 abort ();
10440 return row_found;
10444 /* Make sure that glyph rows in the current matrix of window W
10445 reference the same glyph memory as corresponding rows in the
10446 frame's frame matrix. This function is called after scrolling W's
10447 current matrix on a terminal frame in try_window_id and
10448 try_window_reusing_current_matrix. */
10450 static void
10451 sync_frame_with_window_matrix_rows (w)
10452 struct window *w;
10454 struct frame *f = XFRAME (w->frame);
10455 struct glyph_row *window_row, *window_row_end, *frame_row;
10457 /* Preconditions: W must be a leaf window and full-width. Its frame
10458 must have a frame matrix. */
10459 xassert (NILP (w->hchild) && NILP (w->vchild));
10460 xassert (WINDOW_FULL_WIDTH_P (w));
10461 xassert (!FRAME_WINDOW_P (f));
10463 /* If W is a full-width window, glyph pointers in W's current matrix
10464 have, by definition, to be the same as glyph pointers in the
10465 corresponding frame matrix. */
10466 window_row = w->current_matrix->rows;
10467 window_row_end = window_row + w->current_matrix->nrows;
10468 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10469 while (window_row < window_row_end)
10471 int area;
10473 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10474 frame_row->glyphs[area] = window_row->glyphs[area];
10476 /* Disable frame rows whose corresponding window rows have
10477 been disabled in try_window_id. */
10478 if (!window_row->enabled_p)
10479 frame_row->enabled_p = 0;
10481 ++window_row, ++frame_row;
10486 /* Find the glyph row in window W containing CHARPOS. Consider all
10487 rows between START and END (not inclusive). END null means search
10488 all rows to the end of the display area of W. Value is the row
10489 containing CHARPOS or null. */
10491 static struct glyph_row *
10492 row_containing_pos (w, charpos, start, end)
10493 struct window *w;
10494 int charpos;
10495 struct glyph_row *start, *end;
10497 struct glyph_row *row = start;
10498 int last_y;
10500 /* If we happen to start on a header-line, skip that. */
10501 if (row->mode_line_p)
10502 ++row;
10504 if ((end && row >= end) || !row->enabled_p)
10505 return NULL;
10507 last_y = window_text_bottom_y (w);
10509 while ((end == NULL || row < end)
10510 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10511 /* The end position of a row equals the start
10512 position of the next row. If CHARPOS is there, we
10513 would rather display it in the next line, except
10514 when this line ends in ZV. */
10515 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10516 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10517 || !row->ends_at_zv_p)))
10518 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10519 ++row;
10521 /* Give up if CHARPOS not found. */
10522 if ((end && row >= end)
10523 || charpos < MATRIX_ROW_START_CHARPOS (row)
10524 || charpos > MATRIX_ROW_END_CHARPOS (row))
10525 row = NULL;
10527 return row;
10531 /* Try to redisplay window W by reusing its existing display. W's
10532 current matrix must be up to date when this function is called,
10533 i.e. window_end_valid must not be nil.
10535 Value is
10537 1 if display has been updated
10538 0 if otherwise unsuccessful
10539 -1 if redisplay with same window start is known not to succeed
10541 The following steps are performed:
10543 1. Find the last row in the current matrix of W that is not
10544 affected by changes at the start of current_buffer. If no such row
10545 is found, give up.
10547 2. Find the first row in W's current matrix that is not affected by
10548 changes at the end of current_buffer. Maybe there is no such row.
10550 3. Display lines beginning with the row + 1 found in step 1 to the
10551 row found in step 2 or, if step 2 didn't find a row, to the end of
10552 the window.
10554 4. If cursor is not known to appear on the window, give up.
10556 5. If display stopped at the row found in step 2, scroll the
10557 display and current matrix as needed.
10559 6. Maybe display some lines at the end of W, if we must. This can
10560 happen under various circumstances, like a partially visible line
10561 becoming fully visible, or because newly displayed lines are displayed
10562 in smaller font sizes.
10564 7. Update W's window end information. */
10566 /* Check that window end is what we expect it to be. */
10568 static int
10569 try_window_id (w)
10570 struct window *w;
10572 struct frame *f = XFRAME (w->frame);
10573 struct glyph_matrix *current_matrix = w->current_matrix;
10574 struct glyph_matrix *desired_matrix = w->desired_matrix;
10575 struct glyph_row *last_unchanged_at_beg_row;
10576 struct glyph_row *first_unchanged_at_end_row;
10577 struct glyph_row *row;
10578 struct glyph_row *bottom_row;
10579 int bottom_vpos;
10580 struct it it;
10581 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10582 struct text_pos start_pos;
10583 struct run run;
10584 int first_unchanged_at_end_vpos = 0;
10585 struct glyph_row *last_text_row, *last_text_row_at_end;
10586 struct text_pos start;
10588 SET_TEXT_POS_FROM_MARKER (start, w->start);
10590 /* Check pre-conditions. Window end must be valid, otherwise
10591 the current matrix would not be up to date. */
10592 xassert (!NILP (w->window_end_valid));
10593 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10594 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10596 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10597 only if buffer has really changed. The reason is that the gap is
10598 initially at Z for freshly visited files. The code below would
10599 set end_unchanged to 0 in that case. */
10600 if (MODIFF > SAVE_MODIFF
10601 /* This seems to happen sometimes after saving a buffer. */
10602 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10604 if (GPT - BEG < BEG_UNCHANGED)
10605 BEG_UNCHANGED = GPT - BEG;
10606 if (Z - GPT < END_UNCHANGED)
10607 END_UNCHANGED = Z - GPT;
10610 /* If window starts after a line end, and the last change is in
10611 front of that newline, then changes don't affect the display.
10612 This case happens with stealth-fontification. Note that although
10613 the display is unchanged, glyph positions in the matrix have to
10614 be adjusted, of course. */
10615 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10616 if (CHARPOS (start) > BEGV
10617 && Z - END_UNCHANGED < CHARPOS (start) - 1
10618 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10619 && PT < MATRIX_ROW_END_CHARPOS (row))
10621 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10622 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10624 if (delta)
10626 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10627 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10629 increment_matrix_positions (w->current_matrix,
10630 MATRIX_ROW_VPOS (r0, current_matrix),
10631 MATRIX_ROW_VPOS (r1, current_matrix),
10632 delta, delta_bytes);
10635 #if 0 /* If changes are all in front of the window start, the
10636 distance of the last displayed glyph from Z hasn't
10637 changed. */
10638 w->window_end_pos
10639 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10640 w->window_end_bytepos
10641 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10642 #endif
10644 return 1;
10647 /* Return quickly if changes are all below what is displayed in the
10648 window, and if PT is in the window. */
10649 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10650 && PT < MATRIX_ROW_END_CHARPOS (row))
10652 /* We have to update window end positions because the buffer's
10653 size has changed. */
10654 w->window_end_pos
10655 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10656 w->window_end_bytepos
10657 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10659 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10660 row = row_containing_pos (w, PT, row, NULL);
10661 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10662 return 2;
10665 /* Check that window start agrees with the start of the first glyph
10666 row in its current matrix. Check this after we know the window
10667 start is not in changed text, otherwise positions would not be
10668 comparable. */
10669 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10670 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10671 return 0;
10673 /* Compute the position at which we have to start displaying new
10674 lines. Some of the lines at the top of the window might be
10675 reusable because they are not displaying changed text. Find the
10676 last row in W's current matrix not affected by changes at the
10677 start of current_buffer. Value is null if changes start in the
10678 first line of window. */
10679 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10680 if (last_unchanged_at_beg_row)
10682 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10683 start_pos = it.current.pos;
10685 /* Start displaying new lines in the desired matrix at the same
10686 vpos we would use in the current matrix, i.e. below
10687 last_unchanged_at_beg_row. */
10688 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10689 current_matrix);
10690 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10691 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10693 xassert (it.hpos == 0 && it.current_x == 0);
10695 else
10697 /* There are no reusable lines at the start of the window.
10698 Start displaying in the first line. */
10699 start_display (&it, w, start);
10700 start_pos = it.current.pos;
10703 /* Find the first row that is not affected by changes at the end of
10704 the buffer. Value will be null if there is no unchanged row, in
10705 which case we must redisplay to the end of the window. delta
10706 will be set to the value by which buffer positions beginning with
10707 first_unchanged_at_end_row have to be adjusted due to text
10708 changes. */
10709 first_unchanged_at_end_row
10710 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10711 IF_DEBUG (debug_delta = delta);
10712 IF_DEBUG (debug_delta_bytes = delta_bytes);
10714 /* Set stop_pos to the buffer position up to which we will have to
10715 display new lines. If first_unchanged_at_end_row != NULL, this
10716 is the buffer position of the start of the line displayed in that
10717 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10718 that we don't stop at a buffer position. */
10719 stop_pos = 0;
10720 if (first_unchanged_at_end_row)
10722 xassert (last_unchanged_at_beg_row == NULL
10723 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10725 /* If this is a continuation line, move forward to the next one
10726 that isn't. Changes in lines above affect this line.
10727 Caution: this may move first_unchanged_at_end_row to a row
10728 not displaying text. */
10729 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10730 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10731 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10732 < it.last_visible_y))
10733 ++first_unchanged_at_end_row;
10735 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10736 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10737 >= it.last_visible_y))
10738 first_unchanged_at_end_row = NULL;
10739 else
10741 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10742 + delta);
10743 first_unchanged_at_end_vpos
10744 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10745 xassert (stop_pos >= Z - END_UNCHANGED);
10748 else if (last_unchanged_at_beg_row == NULL)
10749 return 0;
10752 #if GLYPH_DEBUG
10754 /* Either there is no unchanged row at the end, or the one we have
10755 now displays text. This is a necessary condition for the window
10756 end pos calculation at the end of this function. */
10757 xassert (first_unchanged_at_end_row == NULL
10758 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10760 debug_last_unchanged_at_beg_vpos
10761 = (last_unchanged_at_beg_row
10762 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10763 : -1);
10764 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10766 #endif /* GLYPH_DEBUG != 0 */
10769 /* Display new lines. Set last_text_row to the last new line
10770 displayed which has text on it, i.e. might end up as being the
10771 line where the window_end_vpos is. */
10772 w->cursor.vpos = -1;
10773 last_text_row = NULL;
10774 overlay_arrow_seen = 0;
10775 while (it.current_y < it.last_visible_y
10776 && !fonts_changed_p
10777 && (first_unchanged_at_end_row == NULL
10778 || IT_CHARPOS (it) < stop_pos))
10780 if (display_line (&it))
10781 last_text_row = it.glyph_row - 1;
10784 if (fonts_changed_p)
10785 return -1;
10788 /* Compute differences in buffer positions, y-positions etc. for
10789 lines reused at the bottom of the window. Compute what we can
10790 scroll. */
10791 if (first_unchanged_at_end_row
10792 /* No lines reused because we displayed everything up to the
10793 bottom of the window. */
10794 && it.current_y < it.last_visible_y)
10796 dvpos = (it.vpos
10797 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10798 current_matrix));
10799 dy = it.current_y - first_unchanged_at_end_row->y;
10800 run.current_y = first_unchanged_at_end_row->y;
10801 run.desired_y = run.current_y + dy;
10802 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10804 else
10806 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10807 first_unchanged_at_end_row = NULL;
10809 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10812 /* Find the cursor if not already found. We have to decide whether
10813 PT will appear on this window (it sometimes doesn't, but this is
10814 not a very frequent case.) This decision has to be made before
10815 the current matrix is altered. A value of cursor.vpos < 0 means
10816 that PT is either in one of the lines beginning at
10817 first_unchanged_at_end_row or below the window. Don't care for
10818 lines that might be displayed later at the window end; as
10819 mentioned, this is not a frequent case. */
10820 if (w->cursor.vpos < 0)
10822 /* Cursor in unchanged rows at the top? */
10823 if (PT < CHARPOS (start_pos)
10824 && last_unchanged_at_beg_row)
10826 row = row_containing_pos (w, PT,
10827 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10828 last_unchanged_at_beg_row + 1);
10829 if (row)
10830 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10833 /* Start from first_unchanged_at_end_row looking for PT. */
10834 else if (first_unchanged_at_end_row)
10836 row = row_containing_pos (w, PT - delta,
10837 first_unchanged_at_end_row, NULL);
10838 if (row)
10839 set_cursor_from_row (w, row, w->current_matrix, delta,
10840 delta_bytes, dy, dvpos);
10843 /* Give up if cursor was not found. */
10844 if (w->cursor.vpos < 0)
10846 clear_glyph_matrix (w->desired_matrix);
10847 return -1;
10851 /* Don't let the cursor end in the scroll margins. */
10853 int this_scroll_margin, cursor_height;
10855 this_scroll_margin = max (0, scroll_margin);
10856 this_scroll_margin = min (this_scroll_margin,
10857 XFASTINT (w->height) / 4);
10858 this_scroll_margin *= CANON_Y_UNIT (it.f);
10859 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10861 if ((w->cursor.y < this_scroll_margin
10862 && CHARPOS (start) > BEGV)
10863 /* Don't take scroll margin into account at the bottom because
10864 old redisplay didn't do it either. */
10865 || w->cursor.y + cursor_height > it.last_visible_y)
10867 w->cursor.vpos = -1;
10868 clear_glyph_matrix (w->desired_matrix);
10869 return -1;
10873 /* Scroll the display. Do it before changing the current matrix so
10874 that xterm.c doesn't get confused about where the cursor glyph is
10875 found. */
10876 if (dy && run.height)
10878 update_begin (f);
10880 if (FRAME_WINDOW_P (f))
10882 rif->update_window_begin_hook (w);
10883 rif->clear_mouse_face (w);
10884 rif->scroll_run_hook (w, &run);
10885 rif->update_window_end_hook (w, 0, 0);
10887 else
10889 /* Terminal frame. In this case, dvpos gives the number of
10890 lines to scroll by; dvpos < 0 means scroll up. */
10891 int first_unchanged_at_end_vpos
10892 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10893 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10894 int end = XFASTINT (w->top) + window_internal_height (w);
10896 /* Perform the operation on the screen. */
10897 if (dvpos > 0)
10899 /* Scroll last_unchanged_at_beg_row to the end of the
10900 window down dvpos lines. */
10901 set_terminal_window (end);
10903 /* On dumb terminals delete dvpos lines at the end
10904 before inserting dvpos empty lines. */
10905 if (!scroll_region_ok)
10906 ins_del_lines (end - dvpos, -dvpos);
10908 /* Insert dvpos empty lines in front of
10909 last_unchanged_at_beg_row. */
10910 ins_del_lines (from, dvpos);
10912 else if (dvpos < 0)
10914 /* Scroll up last_unchanged_at_beg_vpos to the end of
10915 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10916 set_terminal_window (end);
10918 /* Delete dvpos lines in front of
10919 last_unchanged_at_beg_vpos. ins_del_lines will set
10920 the cursor to the given vpos and emit |dvpos| delete
10921 line sequences. */
10922 ins_del_lines (from + dvpos, dvpos);
10924 /* On a dumb terminal insert dvpos empty lines at the
10925 end. */
10926 if (!scroll_region_ok)
10927 ins_del_lines (end + dvpos, -dvpos);
10930 set_terminal_window (0);
10933 update_end (f);
10936 /* Shift reused rows of the current matrix to the right position.
10937 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10938 text. */
10939 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10940 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10941 if (dvpos < 0)
10943 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10944 bottom_vpos, dvpos);
10945 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10946 bottom_vpos, 0);
10948 else if (dvpos > 0)
10950 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10951 bottom_vpos, dvpos);
10952 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10953 first_unchanged_at_end_vpos + dvpos, 0);
10956 /* For frame-based redisplay, make sure that current frame and window
10957 matrix are in sync with respect to glyph memory. */
10958 if (!FRAME_WINDOW_P (f))
10959 sync_frame_with_window_matrix_rows (w);
10961 /* Adjust buffer positions in reused rows. */
10962 if (delta)
10963 increment_matrix_positions (current_matrix,
10964 first_unchanged_at_end_vpos + dvpos,
10965 bottom_vpos, delta, delta_bytes);
10967 /* Adjust Y positions. */
10968 if (dy)
10969 shift_glyph_matrix (w, current_matrix,
10970 first_unchanged_at_end_vpos + dvpos,
10971 bottom_vpos, dy);
10973 if (first_unchanged_at_end_row)
10974 first_unchanged_at_end_row += dvpos;
10976 /* If scrolling up, there may be some lines to display at the end of
10977 the window. */
10978 last_text_row_at_end = NULL;
10979 if (dy < 0)
10981 /* Set last_row to the glyph row in the current matrix where the
10982 window end line is found. It has been moved up or down in
10983 the matrix by dvpos. */
10984 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10985 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10987 /* If last_row is the window end line, it should display text. */
10988 xassert (last_row->displays_text_p);
10990 /* If window end line was partially visible before, begin
10991 displaying at that line. Otherwise begin displaying with the
10992 line following it. */
10993 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10995 init_to_row_start (&it, w, last_row);
10996 it.vpos = last_vpos;
10997 it.current_y = last_row->y;
10999 else
11001 init_to_row_end (&it, w, last_row);
11002 it.vpos = 1 + last_vpos;
11003 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11004 ++last_row;
11007 /* We may start in a continuation line. If so, we have to get
11008 the right continuation_lines_width and current_x. */
11009 it.continuation_lines_width = last_row->continuation_lines_width;
11010 it.hpos = it.current_x = 0;
11012 /* Display the rest of the lines at the window end. */
11013 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11014 while (it.current_y < it.last_visible_y
11015 && !fonts_changed_p)
11017 /* Is it always sure that the display agrees with lines in
11018 the current matrix? I don't think so, so we mark rows
11019 displayed invalid in the current matrix by setting their
11020 enabled_p flag to zero. */
11021 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11022 if (display_line (&it))
11023 last_text_row_at_end = it.glyph_row - 1;
11027 /* Update window_end_pos and window_end_vpos. */
11028 if (first_unchanged_at_end_row
11029 && first_unchanged_at_end_row->y < it.last_visible_y
11030 && !last_text_row_at_end)
11032 /* Window end line if one of the preserved rows from the current
11033 matrix. Set row to the last row displaying text in current
11034 matrix starting at first_unchanged_at_end_row, after
11035 scrolling. */
11036 xassert (first_unchanged_at_end_row->displays_text_p);
11037 row = find_last_row_displaying_text (w->current_matrix, &it,
11038 first_unchanged_at_end_row);
11039 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11041 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11042 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11043 XSETFASTINT (w->window_end_vpos,
11044 MATRIX_ROW_VPOS (row, w->current_matrix));
11046 else if (last_text_row_at_end)
11048 XSETFASTINT (w->window_end_pos,
11049 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11050 w->window_end_bytepos
11051 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11052 XSETFASTINT (w->window_end_vpos,
11053 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11055 else if (last_text_row)
11057 /* We have displayed either to the end of the window or at the
11058 end of the window, i.e. the last row with text is to be found
11059 in the desired matrix. */
11060 XSETFASTINT (w->window_end_pos,
11061 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11062 w->window_end_bytepos
11063 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11064 XSETFASTINT (w->window_end_vpos,
11065 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11067 else if (first_unchanged_at_end_row == NULL
11068 && last_text_row == NULL
11069 && last_text_row_at_end == NULL)
11071 /* Displayed to end of window, but no line containing text was
11072 displayed. Lines were deleted at the end of the window. */
11073 int vpos;
11074 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11076 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11077 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11078 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11079 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11080 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11081 break;
11083 w->window_end_vpos = make_number (vpos);
11085 else
11086 abort ();
11088 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11089 debug_end_vpos = XFASTINT (w->window_end_vpos));
11091 /* Record that display has not been completed. */
11092 w->window_end_valid = Qnil;
11093 w->desired_matrix->no_scrolling_p = 1;
11094 return 3;
11099 /***********************************************************************
11100 More debugging support
11101 ***********************************************************************/
11103 #if GLYPH_DEBUG
11105 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11106 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11109 /* Dump the contents of glyph matrix MATRIX on stderr. If
11110 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11112 static void
11113 dump_glyph_matrix (matrix, with_glyphs_p)
11114 struct glyph_matrix *matrix;
11115 int with_glyphs_p;
11117 int i;
11118 for (i = 0; i < matrix->nrows; ++i)
11119 dump_glyph_row (matrix, i, with_glyphs_p);
11123 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11124 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11126 void
11127 dump_glyph_row (matrix, vpos, with_glyphs_p)
11128 struct glyph_matrix *matrix;
11129 int vpos, with_glyphs_p;
11131 struct glyph_row *row;
11133 if (vpos < 0 || vpos >= matrix->nrows)
11134 return;
11136 row = MATRIX_ROW (matrix, vpos);
11138 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11139 fprintf (stderr, "=======================================================================\n");
11141 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11142 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11143 row - matrix->rows,
11144 MATRIX_ROW_START_CHARPOS (row),
11145 MATRIX_ROW_END_CHARPOS (row),
11146 row->used[TEXT_AREA],
11147 row->contains_overlapping_glyphs_p,
11148 row->enabled_p,
11149 row->inverse_p,
11150 row->truncated_on_left_p,
11151 row->truncated_on_right_p,
11152 row->overlay_arrow_p,
11153 row->continued_p,
11154 MATRIX_ROW_CONTINUATION_LINE_P (row),
11155 row->displays_text_p,
11156 row->ends_at_zv_p,
11157 row->fill_line_p,
11158 row->ends_in_middle_of_char_p,
11159 row->starts_in_middle_of_char_p,
11160 row->x,
11161 row->y,
11162 row->pixel_width,
11163 row->height,
11164 row->visible_height,
11165 row->ascent,
11166 row->phys_ascent);
11167 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11168 row->end.overlay_string_index);
11169 fprintf (stderr, "%9d %5d\n",
11170 CHARPOS (row->start.string_pos),
11171 CHARPOS (row->end.string_pos));
11172 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11173 row->end.dpvec_index);
11175 if (with_glyphs_p)
11177 struct glyph *glyph, *glyph_end;
11178 int prev_had_glyphs_p;
11180 glyph = row->glyphs[TEXT_AREA];
11181 glyph_end = glyph + row->used[TEXT_AREA];
11183 /* Glyph for a line end in text. */
11184 if (glyph == glyph_end && glyph->charpos > 0)
11185 ++glyph_end;
11187 if (glyph < glyph_end)
11189 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11190 prev_had_glyphs_p = 1;
11192 else
11193 prev_had_glyphs_p = 0;
11195 while (glyph < glyph_end)
11197 if (glyph->type == CHAR_GLYPH)
11199 fprintf (stderr,
11200 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11201 glyph - row->glyphs[TEXT_AREA],
11202 'C',
11203 glyph->charpos,
11204 (BUFFERP (glyph->object)
11205 ? 'B'
11206 : (STRINGP (glyph->object)
11207 ? 'S'
11208 : '-')),
11209 glyph->pixel_width,
11210 glyph->u.ch,
11211 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11212 ? glyph->u.ch
11213 : '.'),
11214 glyph->face_id,
11215 glyph->left_box_line_p,
11216 glyph->right_box_line_p);
11218 else if (glyph->type == STRETCH_GLYPH)
11220 fprintf (stderr,
11221 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11222 glyph - row->glyphs[TEXT_AREA],
11223 'S',
11224 glyph->charpos,
11225 (BUFFERP (glyph->object)
11226 ? 'B'
11227 : (STRINGP (glyph->object)
11228 ? 'S'
11229 : '-')),
11230 glyph->pixel_width,
11232 '.',
11233 glyph->face_id,
11234 glyph->left_box_line_p,
11235 glyph->right_box_line_p);
11237 else if (glyph->type == IMAGE_GLYPH)
11239 fprintf (stderr,
11240 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11241 glyph - row->glyphs[TEXT_AREA],
11242 'I',
11243 glyph->charpos,
11244 (BUFFERP (glyph->object)
11245 ? 'B'
11246 : (STRINGP (glyph->object)
11247 ? 'S'
11248 : '-')),
11249 glyph->pixel_width,
11250 glyph->u.img_id,
11251 '.',
11252 glyph->face_id,
11253 glyph->left_box_line_p,
11254 glyph->right_box_line_p);
11256 ++glyph;
11262 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11263 Sdump_glyph_matrix, 0, 1, "p",
11264 "Dump the current matrix of the selected window to stderr.\n\
11265 Shows contents of glyph row structures. With non-nil optional\n\
11266 parameter WITH-GLYPHS-P, dump glyphs as well.")
11267 (with_glyphs_p)
11268 Lisp_Object with_glyphs_p;
11270 struct window *w = XWINDOW (selected_window);
11271 struct buffer *buffer = XBUFFER (w->buffer);
11273 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11274 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11275 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11276 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11277 fprintf (stderr, "=============================================\n");
11278 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11279 return Qnil;
11283 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11284 "Dump glyph row ROW to stderr.")
11285 (row)
11286 Lisp_Object row;
11288 CHECK_NUMBER (row, 0);
11289 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11290 return Qnil;
11294 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11295 0, 0, "", "")
11298 struct frame *sf = SELECTED_FRAME ();
11299 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11300 ->current_matrix);
11301 dump_glyph_row (m, 0, 1);
11302 return Qnil;
11306 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11307 Strace_redisplay_toggle, 0, 0, "",
11308 "Toggle tracing of redisplay.")
11311 trace_redisplay_p = !trace_redisplay_p;
11312 return Qnil;
11316 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11317 "Print STRING to stderr.")
11318 (string)
11319 Lisp_Object string;
11321 CHECK_STRING (string, 0);
11322 fprintf (stderr, "%s", XSTRING (string)->data);
11323 return Qnil;
11326 #endif /* GLYPH_DEBUG */
11330 /***********************************************************************
11331 Building Desired Matrix Rows
11332 ***********************************************************************/
11334 /* Return a temporary glyph row holding the glyphs of an overlay
11335 arrow. Only used for non-window-redisplay windows. */
11337 static struct glyph_row *
11338 get_overlay_arrow_glyph_row (w)
11339 struct window *w;
11341 struct frame *f = XFRAME (WINDOW_FRAME (w));
11342 struct buffer *buffer = XBUFFER (w->buffer);
11343 struct buffer *old = current_buffer;
11344 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11345 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11346 unsigned char *arrow_end = arrow_string + arrow_len;
11347 unsigned char *p;
11348 struct it it;
11349 int multibyte_p;
11350 int n_glyphs_before;
11352 set_buffer_temp (buffer);
11353 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11354 it.glyph_row->used[TEXT_AREA] = 0;
11355 SET_TEXT_POS (it.position, 0, 0);
11357 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11358 p = arrow_string;
11359 while (p < arrow_end)
11361 Lisp_Object face, ilisp;
11363 /* Get the next character. */
11364 if (multibyte_p)
11365 it.c = string_char_and_length (p, arrow_len, &it.len);
11366 else
11367 it.c = *p, it.len = 1;
11368 p += it.len;
11370 /* Get its face. */
11371 XSETFASTINT (ilisp, p - arrow_string);
11372 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11373 it.face_id = compute_char_face (f, it.c, face);
11375 /* Compute its width, get its glyphs. */
11376 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11377 SET_TEXT_POS (it.position, -1, -1);
11378 PRODUCE_GLYPHS (&it);
11380 /* If this character doesn't fit any more in the line, we have
11381 to remove some glyphs. */
11382 if (it.current_x > it.last_visible_x)
11384 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11385 break;
11389 set_buffer_temp (old);
11390 return it.glyph_row;
11394 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11395 glyphs are only inserted for terminal frames since we can't really
11396 win with truncation glyphs when partially visible glyphs are
11397 involved. Which glyphs to insert is determined by
11398 produce_special_glyphs. */
11400 static void
11401 insert_left_trunc_glyphs (it)
11402 struct it *it;
11404 struct it truncate_it;
11405 struct glyph *from, *end, *to, *toend;
11407 xassert (!FRAME_WINDOW_P (it->f));
11409 /* Get the truncation glyphs. */
11410 truncate_it = *it;
11411 truncate_it.current_x = 0;
11412 truncate_it.face_id = DEFAULT_FACE_ID;
11413 truncate_it.glyph_row = &scratch_glyph_row;
11414 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11415 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11416 truncate_it.object = make_number (0);
11417 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11419 /* Overwrite glyphs from IT with truncation glyphs. */
11420 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11421 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11422 to = it->glyph_row->glyphs[TEXT_AREA];
11423 toend = to + it->glyph_row->used[TEXT_AREA];
11425 while (from < end)
11426 *to++ = *from++;
11428 /* There may be padding glyphs left over. Remove them. */
11429 from = to;
11430 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11431 ++from;
11432 while (from < toend)
11433 *to++ = *from++;
11435 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11439 /* Compute the pixel height and width of IT->glyph_row.
11441 Most of the time, ascent and height of a display line will be equal
11442 to the max_ascent and max_height values of the display iterator
11443 structure. This is not the case if
11445 1. We hit ZV without displaying anything. In this case, max_ascent
11446 and max_height will be zero.
11448 2. We have some glyphs that don't contribute to the line height.
11449 (The glyph row flag contributes_to_line_height_p is for future
11450 pixmap extensions).
11452 The first case is easily covered by using default values because in
11453 these cases, the line height does not really matter, except that it
11454 must not be zero. */
11456 static void
11457 compute_line_metrics (it)
11458 struct it *it;
11460 struct glyph_row *row = it->glyph_row;
11461 int area, i;
11463 if (FRAME_WINDOW_P (it->f))
11465 int i, header_line_height;
11467 /* The line may consist of one space only, that was added to
11468 place the cursor on it. If so, the row's height hasn't been
11469 computed yet. */
11470 if (row->height == 0)
11472 if (it->max_ascent + it->max_descent == 0)
11473 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11474 row->ascent = it->max_ascent;
11475 row->height = it->max_ascent + it->max_descent;
11476 row->phys_ascent = it->max_phys_ascent;
11477 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11480 /* Compute the width of this line. */
11481 row->pixel_width = row->x;
11482 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11483 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11485 xassert (row->pixel_width >= 0);
11486 xassert (row->ascent >= 0 && row->height > 0);
11488 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11489 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11491 /* If first line's physical ascent is larger than its logical
11492 ascent, use the physical ascent, and make the row taller.
11493 This makes accented characters fully visible. */
11494 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11495 && row->phys_ascent > row->ascent)
11497 row->height += row->phys_ascent - row->ascent;
11498 row->ascent = row->phys_ascent;
11501 /* Compute how much of the line is visible. */
11502 row->visible_height = row->height;
11504 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11505 if (row->y < header_line_height)
11506 row->visible_height -= header_line_height - row->y;
11507 else
11509 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11510 if (row->y + row->height > max_y)
11511 row->visible_height -= row->y + row->height - max_y;
11514 else
11516 row->pixel_width = row->used[TEXT_AREA];
11517 row->ascent = row->phys_ascent = 0;
11518 row->height = row->phys_height = row->visible_height = 1;
11521 /* Compute a hash code for this row. */
11522 row->hash = 0;
11523 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11524 for (i = 0; i < row->used[area]; ++i)
11525 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11526 + row->glyphs[area][i].u.val
11527 + row->glyphs[area][i].face_id
11528 + row->glyphs[area][i].padding_p
11529 + (row->glyphs[area][i].type << 2));
11531 it->max_ascent = it->max_descent = 0;
11532 it->max_phys_ascent = it->max_phys_descent = 0;
11536 /* Append one space to the glyph row of iterator IT if doing a
11537 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11538 space have the default face, otherwise let it have the same face as
11539 IT->face_id. Value is non-zero if a space was added.
11541 This function is called to make sure that there is always one glyph
11542 at the end of a glyph row that the cursor can be set on under
11543 window-systems. (If there weren't such a glyph we would not know
11544 how wide and tall a box cursor should be displayed).
11546 At the same time this space let's a nicely handle clearing to the
11547 end of the line if the row ends in italic text. */
11549 static int
11550 append_space (it, default_face_p)
11551 struct it *it;
11552 int default_face_p;
11554 if (FRAME_WINDOW_P (it->f))
11556 int n = it->glyph_row->used[TEXT_AREA];
11558 if (it->glyph_row->glyphs[TEXT_AREA] + n
11559 < it->glyph_row->glyphs[1 + TEXT_AREA])
11561 /* Save some values that must not be changed.
11562 Must save IT->c and IT->len because otherwise
11563 ITERATOR_AT_END_P wouldn't work anymore after
11564 append_space has been called. */
11565 int saved_what = it->what;
11566 int saved_c = it->c, saved_len = it->len;
11567 int saved_x = it->current_x;
11568 int saved_face_id = it->face_id;
11569 struct text_pos saved_pos;
11570 Lisp_Object saved_object;
11571 struct face *face;
11573 saved_object = it->object;
11574 saved_pos = it->position;
11576 it->what = IT_CHARACTER;
11577 bzero (&it->position, sizeof it->position);
11578 it->object = make_number (0);
11579 it->c = ' ';
11580 it->len = 1;
11582 if (default_face_p)
11583 it->face_id = DEFAULT_FACE_ID;
11584 face = FACE_FROM_ID (it->f, it->face_id);
11585 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11587 PRODUCE_GLYPHS (it);
11589 it->current_x = saved_x;
11590 it->object = saved_object;
11591 it->position = saved_pos;
11592 it->what = saved_what;
11593 it->face_id = saved_face_id;
11594 it->len = saved_len;
11595 it->c = saved_c;
11596 return 1;
11600 return 0;
11604 /* Extend the face of the last glyph in the text area of IT->glyph_row
11605 to the end of the display line. Called from display_line.
11606 If the glyph row is empty, add a space glyph to it so that we
11607 know the face to draw. Set the glyph row flag fill_line_p. */
11609 static void
11610 extend_face_to_end_of_line (it)
11611 struct it *it;
11613 struct face *face;
11614 struct frame *f = it->f;
11616 /* If line is already filled, do nothing. */
11617 if (it->current_x >= it->last_visible_x)
11618 return;
11620 /* Face extension extends the background and box of IT->face_id
11621 to the end of the line. If the background equals the background
11622 of the frame, we haven't to do anything. */
11623 face = FACE_FROM_ID (f, it->face_id);
11624 if (FRAME_WINDOW_P (f)
11625 && face->box == FACE_NO_BOX
11626 && face->background == FRAME_BACKGROUND_PIXEL (f)
11627 && !face->stipple)
11628 return;
11630 /* Set the glyph row flag indicating that the face of the last glyph
11631 in the text area has to be drawn to the end of the text area. */
11632 it->glyph_row->fill_line_p = 1;
11634 /* If current character of IT is not ASCII, make sure we have the
11635 ASCII face. This will be automatically undone the next time
11636 get_next_display_element returns a multibyte character. Note
11637 that the character will always be single byte in unibyte text. */
11638 if (!SINGLE_BYTE_CHAR_P (it->c))
11640 it->face_id = FACE_FOR_CHAR (f, face, 0);
11643 if (FRAME_WINDOW_P (f))
11645 /* If the row is empty, add a space with the current face of IT,
11646 so that we know which face to draw. */
11647 if (it->glyph_row->used[TEXT_AREA] == 0)
11649 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11650 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11651 it->glyph_row->used[TEXT_AREA] = 1;
11654 else
11656 /* Save some values that must not be changed. */
11657 int saved_x = it->current_x;
11658 struct text_pos saved_pos;
11659 Lisp_Object saved_object;
11660 int saved_what = it->what;
11662 saved_object = it->object;
11663 saved_pos = it->position;
11665 it->what = IT_CHARACTER;
11666 bzero (&it->position, sizeof it->position);
11667 it->object = make_number (0);
11668 it->c = ' ';
11669 it->len = 1;
11671 PRODUCE_GLYPHS (it);
11673 while (it->current_x <= it->last_visible_x)
11674 PRODUCE_GLYPHS (it);
11676 /* Don't count these blanks really. It would let us insert a left
11677 truncation glyph below and make us set the cursor on them, maybe. */
11678 it->current_x = saved_x;
11679 it->object = saved_object;
11680 it->position = saved_pos;
11681 it->what = saved_what;
11686 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11687 trailing whitespace. */
11689 static int
11690 trailing_whitespace_p (charpos)
11691 int charpos;
11693 int bytepos = CHAR_TO_BYTE (charpos);
11694 int c = 0;
11696 while (bytepos < ZV_BYTE
11697 && (c = FETCH_CHAR (bytepos),
11698 c == ' ' || c == '\t'))
11699 ++bytepos;
11701 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11703 if (bytepos != PT_BYTE)
11704 return 1;
11706 return 0;
11710 /* Highlight trailing whitespace, if any, in ROW. */
11712 void
11713 highlight_trailing_whitespace (f, row)
11714 struct frame *f;
11715 struct glyph_row *row;
11717 int used = row->used[TEXT_AREA];
11719 if (used)
11721 struct glyph *start = row->glyphs[TEXT_AREA];
11722 struct glyph *glyph = start + used - 1;
11724 /* Skip over the space glyph inserted to display the
11725 cursor at the end of a line. */
11726 if (glyph->type == CHAR_GLYPH
11727 && glyph->u.ch == ' '
11728 && INTEGERP (glyph->object))
11729 --glyph;
11731 /* If last glyph is a space or stretch, and it's trailing
11732 whitespace, set the face of all trailing whitespace glyphs in
11733 IT->glyph_row to `trailing-whitespace'. */
11734 if (glyph >= start
11735 && BUFFERP (glyph->object)
11736 && (glyph->type == STRETCH_GLYPH
11737 || (glyph->type == CHAR_GLYPH
11738 && glyph->u.ch == ' '))
11739 && trailing_whitespace_p (glyph->charpos))
11741 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11743 while (glyph >= start
11744 && BUFFERP (glyph->object)
11745 && (glyph->type == STRETCH_GLYPH
11746 || (glyph->type == CHAR_GLYPH
11747 && glyph->u.ch == ' ')))
11748 (glyph--)->face_id = face_id;
11754 /* Value is non-zero if glyph row ROW in window W should be
11755 used to put the cursor on. */
11757 static int
11758 cursor_row_p (w, row)
11759 struct window *w;
11760 struct glyph_row *row;
11762 int cursor_row_p = 1;
11764 if (PT == MATRIX_ROW_END_CHARPOS (row))
11766 /* If the row ends with a newline from a string, we don't want
11767 the cursor there (if the row is continued it doesn't end in a
11768 newline). */
11769 if (CHARPOS (row->end.string_pos) >= 0
11770 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11771 cursor_row_p = row->continued_p;
11773 /* If the row ends at ZV, display the cursor at the end of that
11774 row instead of at the start of the row below. */
11775 else if (row->ends_at_zv_p)
11776 cursor_row_p = 1;
11777 else
11778 cursor_row_p = 0;
11781 return cursor_row_p;
11785 /* Construct the glyph row IT->glyph_row in the desired matrix of
11786 IT->w from text at the current position of IT. See dispextern.h
11787 for an overview of struct it. Value is non-zero if
11788 IT->glyph_row displays text, as opposed to a line displaying ZV
11789 only. */
11791 static int
11792 display_line (it)
11793 struct it *it;
11795 struct glyph_row *row = it->glyph_row;
11797 /* We always start displaying at hpos zero even if hscrolled. */
11798 xassert (it->hpos == 0 && it->current_x == 0);
11800 /* We must not display in a row that's not a text row. */
11801 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11802 < it->w->desired_matrix->nrows);
11804 /* Is IT->w showing the region? */
11805 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11807 /* Clear the result glyph row and enable it. */
11808 prepare_desired_row (row);
11810 row->y = it->current_y;
11811 row->start = it->current;
11812 row->continuation_lines_width = it->continuation_lines_width;
11813 row->displays_text_p = 1;
11814 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11815 it->starts_in_middle_of_char_p = 0;
11817 /* Arrange the overlays nicely for our purposes. Usually, we call
11818 display_line on only one line at a time, in which case this
11819 can't really hurt too much, or we call it on lines which appear
11820 one after another in the buffer, in which case all calls to
11821 recenter_overlay_lists but the first will be pretty cheap. */
11822 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11824 /* Move over display elements that are not visible because we are
11825 hscrolled. This may stop at an x-position < IT->first_visible_x
11826 if the first glyph is partially visible or if we hit a line end. */
11827 if (it->current_x < it->first_visible_x)
11828 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11829 MOVE_TO_POS | MOVE_TO_X);
11831 /* Get the initial row height. This is either the height of the
11832 text hscrolled, if there is any, or zero. */
11833 row->ascent = it->max_ascent;
11834 row->height = it->max_ascent + it->max_descent;
11835 row->phys_ascent = it->max_phys_ascent;
11836 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11838 /* Loop generating characters. The loop is left with IT on the next
11839 character to display. */
11840 while (1)
11842 int n_glyphs_before, hpos_before, x_before;
11843 int x, i, nglyphs;
11844 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11846 /* Retrieve the next thing to display. Value is zero if end of
11847 buffer reached. */
11848 if (!get_next_display_element (it))
11850 /* Maybe add a space at the end of this line that is used to
11851 display the cursor there under X. Set the charpos of the
11852 first glyph of blank lines not corresponding to any text
11853 to -1. */
11854 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11855 || row->used[TEXT_AREA] == 0)
11857 row->glyphs[TEXT_AREA]->charpos = -1;
11858 row->displays_text_p = 0;
11860 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11861 row->indicate_empty_line_p = 1;
11864 it->continuation_lines_width = 0;
11865 row->ends_at_zv_p = 1;
11866 break;
11869 /* Now, get the metrics of what we want to display. This also
11870 generates glyphs in `row' (which is IT->glyph_row). */
11871 n_glyphs_before = row->used[TEXT_AREA];
11872 x = it->current_x;
11874 /* Remember the line height so far in case the next element doesn't
11875 fit on the line. */
11876 if (!it->truncate_lines_p)
11878 ascent = it->max_ascent;
11879 descent = it->max_descent;
11880 phys_ascent = it->max_phys_ascent;
11881 phys_descent = it->max_phys_descent;
11884 PRODUCE_GLYPHS (it);
11886 /* If this display element was in marginal areas, continue with
11887 the next one. */
11888 if (it->area != TEXT_AREA)
11890 row->ascent = max (row->ascent, it->max_ascent);
11891 row->height = max (row->height, it->max_ascent + it->max_descent);
11892 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11893 row->phys_height = max (row->phys_height,
11894 it->max_phys_ascent + it->max_phys_descent);
11895 set_iterator_to_next (it, 1);
11896 continue;
11899 /* Does the display element fit on the line? If we truncate
11900 lines, we should draw past the right edge of the window. If
11901 we don't truncate, we want to stop so that we can display the
11902 continuation glyph before the right margin. If lines are
11903 continued, there are two possible strategies for characters
11904 resulting in more than 1 glyph (e.g. tabs): Display as many
11905 glyphs as possible in this line and leave the rest for the
11906 continuation line, or display the whole element in the next
11907 line. Original redisplay did the former, so we do it also. */
11908 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11909 hpos_before = it->hpos;
11910 x_before = x;
11912 if (nglyphs == 1
11913 && it->current_x < it->last_visible_x)
11915 ++it->hpos;
11916 row->ascent = max (row->ascent, it->max_ascent);
11917 row->height = max (row->height, it->max_ascent + it->max_descent);
11918 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11919 row->phys_height = max (row->phys_height,
11920 it->max_phys_ascent + it->max_phys_descent);
11921 if (it->current_x - it->pixel_width < it->first_visible_x)
11922 row->x = x - it->first_visible_x;
11924 else
11926 int new_x;
11927 struct glyph *glyph;
11929 for (i = 0; i < nglyphs; ++i, x = new_x)
11931 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11932 new_x = x + glyph->pixel_width;
11934 if (/* Lines are continued. */
11935 !it->truncate_lines_p
11936 && (/* Glyph doesn't fit on the line. */
11937 new_x > it->last_visible_x
11938 /* Or it fits exactly on a window system frame. */
11939 || (new_x == it->last_visible_x
11940 && FRAME_WINDOW_P (it->f))))
11942 /* End of a continued line. */
11944 if (it->hpos == 0
11945 || (new_x == it->last_visible_x
11946 && FRAME_WINDOW_P (it->f)))
11948 /* Current glyph is the only one on the line or
11949 fits exactly on the line. We must continue
11950 the line because we can't draw the cursor
11951 after the glyph. */
11952 row->continued_p = 1;
11953 it->current_x = new_x;
11954 it->continuation_lines_width += new_x;
11955 ++it->hpos;
11956 if (i == nglyphs - 1)
11957 set_iterator_to_next (it, 1);
11959 else if (CHAR_GLYPH_PADDING_P (*glyph)
11960 && !FRAME_WINDOW_P (it->f))
11962 /* A padding glyph that doesn't fit on this line.
11963 This means the whole character doesn't fit
11964 on the line. */
11965 row->used[TEXT_AREA] = n_glyphs_before;
11967 /* Fill the rest of the row with continuation
11968 glyphs like in 20.x. */
11969 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11970 < row->glyphs[1 + TEXT_AREA])
11971 produce_special_glyphs (it, IT_CONTINUATION);
11973 row->continued_p = 1;
11974 it->current_x = x_before;
11975 it->continuation_lines_width += x_before;
11977 /* Restore the height to what it was before the
11978 element not fitting on the line. */
11979 it->max_ascent = ascent;
11980 it->max_descent = descent;
11981 it->max_phys_ascent = phys_ascent;
11982 it->max_phys_descent = phys_descent;
11984 else
11986 /* Display element draws past the right edge of
11987 the window. Restore positions to values
11988 before the element. The next line starts
11989 with current_x before the glyph that could
11990 not be displayed, so that TAB works right. */
11991 row->used[TEXT_AREA] = n_glyphs_before + i;
11993 /* Display continuation glyphs. */
11994 if (!FRAME_WINDOW_P (it->f))
11995 produce_special_glyphs (it, IT_CONTINUATION);
11996 row->continued_p = 1;
11998 it->current_x = x;
11999 it->continuation_lines_width += x;
12000 if (nglyphs > 1 && i > 0)
12002 row->ends_in_middle_of_char_p = 1;
12003 it->starts_in_middle_of_char_p = 1;
12006 /* Restore the height to what it was before the
12007 element not fitting on the line. */
12008 it->max_ascent = ascent;
12009 it->max_descent = descent;
12010 it->max_phys_ascent = phys_ascent;
12011 it->max_phys_descent = phys_descent;
12014 break;
12016 else if (new_x > it->first_visible_x)
12018 /* Increment number of glyphs actually displayed. */
12019 ++it->hpos;
12021 if (x < it->first_visible_x)
12022 /* Glyph is partially visible, i.e. row starts at
12023 negative X position. */
12024 row->x = x - it->first_visible_x;
12026 else
12028 /* Glyph is completely off the left margin of the
12029 window. This should not happen because of the
12030 move_it_in_display_line at the start of
12031 this function. */
12032 abort ();
12036 row->ascent = max (row->ascent, it->max_ascent);
12037 row->height = max (row->height, it->max_ascent + it->max_descent);
12038 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12039 row->phys_height = max (row->phys_height,
12040 it->max_phys_ascent + it->max_phys_descent);
12042 /* End of this display line if row is continued. */
12043 if (row->continued_p)
12044 break;
12047 /* Is this a line end? If yes, we're also done, after making
12048 sure that a non-default face is extended up to the right
12049 margin of the window. */
12050 if (ITERATOR_AT_END_OF_LINE_P (it))
12052 int used_before = row->used[TEXT_AREA];
12054 /* Add a space at the end of the line that is used to
12055 display the cursor there. */
12056 append_space (it, 0);
12058 /* Extend the face to the end of the line. */
12059 extend_face_to_end_of_line (it);
12061 /* Make sure we have the position. */
12062 if (used_before == 0)
12063 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12065 /* Consume the line end. This skips over invisible lines. */
12066 set_iterator_to_next (it, 1);
12067 it->continuation_lines_width = 0;
12068 break;
12071 /* Proceed with next display element. Note that this skips
12072 over lines invisible because of selective display. */
12073 set_iterator_to_next (it, 1);
12075 /* If we truncate lines, we are done when the last displayed
12076 glyphs reach past the right margin of the window. */
12077 if (it->truncate_lines_p
12078 && (FRAME_WINDOW_P (it->f)
12079 ? (it->current_x >= it->last_visible_x)
12080 : (it->current_x > it->last_visible_x)))
12082 /* Maybe add truncation glyphs. */
12083 if (!FRAME_WINDOW_P (it->f))
12085 --it->glyph_row->used[TEXT_AREA];
12086 produce_special_glyphs (it, IT_TRUNCATION);
12089 row->truncated_on_right_p = 1;
12090 it->continuation_lines_width = 0;
12091 reseat_at_next_visible_line_start (it, 0);
12092 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12093 it->hpos = hpos_before;
12094 it->current_x = x_before;
12095 break;
12099 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12100 at the left window margin. */
12101 if (it->first_visible_x
12102 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12104 if (!FRAME_WINDOW_P (it->f))
12105 insert_left_trunc_glyphs (it);
12106 row->truncated_on_left_p = 1;
12109 /* If the start of this line is the overlay arrow-position, then
12110 mark this glyph row as the one containing the overlay arrow.
12111 This is clearly a mess with variable size fonts. It would be
12112 better to let it be displayed like cursors under X. */
12113 if (MARKERP (Voverlay_arrow_position)
12114 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12115 && (MATRIX_ROW_START_CHARPOS (row)
12116 == marker_position (Voverlay_arrow_position))
12117 && STRINGP (Voverlay_arrow_string)
12118 && ! overlay_arrow_seen)
12120 /* Overlay arrow in window redisplay is a bitmap. */
12121 if (!FRAME_WINDOW_P (it->f))
12123 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12124 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12125 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12126 struct glyph *p = row->glyphs[TEXT_AREA];
12127 struct glyph *p2, *end;
12129 /* Copy the arrow glyphs. */
12130 while (glyph < arrow_end)
12131 *p++ = *glyph++;
12133 /* Throw away padding glyphs. */
12134 p2 = p;
12135 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12136 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12137 ++p2;
12138 if (p2 > p)
12140 while (p2 < end)
12141 *p++ = *p2++;
12142 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12146 overlay_arrow_seen = 1;
12147 row->overlay_arrow_p = 1;
12150 /* Compute pixel dimensions of this line. */
12151 compute_line_metrics (it);
12153 /* Remember the position at which this line ends. */
12154 row->end = it->current;
12156 /* Maybe set the cursor. */
12157 if (it->w->cursor.vpos < 0
12158 && PT >= MATRIX_ROW_START_CHARPOS (row)
12159 && PT <= MATRIX_ROW_END_CHARPOS (row)
12160 && cursor_row_p (it->w, row))
12161 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12163 /* Highlight trailing whitespace. */
12164 if (!NILP (Vshow_trailing_whitespace))
12165 highlight_trailing_whitespace (it->f, it->glyph_row);
12167 /* Prepare for the next line. This line starts horizontally at (X
12168 HPOS) = (0 0). Vertical positions are incremented. As a
12169 convenience for the caller, IT->glyph_row is set to the next
12170 row to be used. */
12171 it->current_x = it->hpos = 0;
12172 it->current_y += row->height;
12173 ++it->vpos;
12174 ++it->glyph_row;
12175 return row->displays_text_p;
12180 /***********************************************************************
12181 Menu Bar
12182 ***********************************************************************/
12184 /* Redisplay the menu bar in the frame for window W.
12186 The menu bar of X frames that don't have X toolkit support is
12187 displayed in a special window W->frame->menu_bar_window.
12189 The menu bar of terminal frames is treated specially as far as
12190 glyph matrices are concerned. Menu bar lines are not part of
12191 windows, so the update is done directly on the frame matrix rows
12192 for the menu bar. */
12194 static void
12195 display_menu_bar (w)
12196 struct window *w;
12198 struct frame *f = XFRAME (WINDOW_FRAME (w));
12199 struct it it;
12200 Lisp_Object items;
12201 int i;
12203 /* Don't do all this for graphical frames. */
12204 #ifdef HAVE_NTGUI
12205 if (!NILP (Vwindow_system))
12206 return;
12207 #endif
12208 #ifdef USE_X_TOOLKIT
12209 if (FRAME_X_P (f))
12210 return;
12211 #endif
12212 #ifdef macintosh
12213 if (FRAME_MAC_P (f))
12214 return;
12215 #endif
12217 #ifdef USE_X_TOOLKIT
12218 xassert (!FRAME_WINDOW_P (f));
12219 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12220 it.first_visible_x = 0;
12221 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12222 #else /* not USE_X_TOOLKIT */
12223 if (FRAME_WINDOW_P (f))
12225 /* Menu bar lines are displayed in the desired matrix of the
12226 dummy window menu_bar_window. */
12227 struct window *menu_w;
12228 xassert (WINDOWP (f->menu_bar_window));
12229 menu_w = XWINDOW (f->menu_bar_window);
12230 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12231 MENU_FACE_ID);
12232 it.first_visible_x = 0;
12233 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12235 else
12237 /* This is a TTY frame, i.e. character hpos/vpos are used as
12238 pixel x/y. */
12239 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12240 MENU_FACE_ID);
12241 it.first_visible_x = 0;
12242 it.last_visible_x = FRAME_WIDTH (f);
12244 #endif /* not USE_X_TOOLKIT */
12246 /* Clear all rows of the menu bar. */
12247 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12249 struct glyph_row *row = it.glyph_row + i;
12250 clear_glyph_row (row);
12251 row->enabled_p = 1;
12252 row->full_width_p = 1;
12255 /* Make the first line of the menu bar appear in reverse video. */
12256 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12258 /* Display all items of the menu bar. */
12259 items = FRAME_MENU_BAR_ITEMS (it.f);
12260 for (i = 0; i < XVECTOR (items)->size; i += 4)
12262 Lisp_Object string;
12264 /* Stop at nil string. */
12265 string = XVECTOR (items)->contents[i + 1];
12266 if (NILP (string))
12267 break;
12269 /* Remember where item was displayed. */
12270 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12272 /* Display the item, pad with one space. */
12273 if (it.current_x < it.last_visible_x)
12274 display_string (NULL, string, Qnil, 0, 0, &it,
12275 XSTRING (string)->size + 1, 0, 0, -1);
12278 /* Fill out the line with spaces. */
12279 if (it.current_x < it.last_visible_x)
12280 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12282 /* Compute the total height of the lines. */
12283 compute_line_metrics (&it);
12288 /***********************************************************************
12289 Mode Line
12290 ***********************************************************************/
12292 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12293 FORCE is non-zero, redisplay mode lines unconditionally.
12294 Otherwise, redisplay only mode lines that are garbaged. Value is
12295 the number of windows whose mode lines were redisplayed. */
12297 static int
12298 redisplay_mode_lines (window, force)
12299 Lisp_Object window;
12300 int force;
12302 int nwindows = 0;
12304 while (!NILP (window))
12306 struct window *w = XWINDOW (window);
12308 if (WINDOWP (w->hchild))
12309 nwindows += redisplay_mode_lines (w->hchild, force);
12310 else if (WINDOWP (w->vchild))
12311 nwindows += redisplay_mode_lines (w->vchild, force);
12312 else if (force
12313 || FRAME_GARBAGED_P (XFRAME (w->frame))
12314 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12316 Lisp_Object old_selected_frame;
12317 struct text_pos lpoint;
12318 struct buffer *old = current_buffer;
12320 /* Set the window's buffer for the mode line display. */
12321 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12322 set_buffer_internal_1 (XBUFFER (w->buffer));
12324 /* Point refers normally to the selected window. For any
12325 other window, set up appropriate value. */
12326 if (!EQ (window, selected_window))
12328 struct text_pos pt;
12330 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12331 if (CHARPOS (pt) < BEGV)
12332 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12333 else if (CHARPOS (pt) > (ZV - 1))
12334 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12335 else
12336 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12339 /* Temporarily set up the selected frame. */
12340 old_selected_frame = selected_frame;
12341 selected_frame = w->frame;
12343 /* Display mode lines. */
12344 clear_glyph_matrix (w->desired_matrix);
12345 if (display_mode_lines (w))
12347 ++nwindows;
12348 w->must_be_updated_p = 1;
12351 /* Restore old settings. */
12352 selected_frame = old_selected_frame;
12353 set_buffer_internal_1 (old);
12354 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12357 window = w->next;
12360 return nwindows;
12364 /* Display the mode and/or top line of window W. Value is the number
12365 of mode lines displayed. */
12367 static int
12368 display_mode_lines (w)
12369 struct window *w;
12371 int n = 0;
12373 /* These will be set while the mode line specs are processed. */
12374 line_number_displayed = 0;
12375 w->column_number_displayed = Qnil;
12377 if (WINDOW_WANTS_MODELINE_P (w))
12379 display_mode_line (w, MODE_LINE_FACE_ID,
12380 current_buffer->mode_line_format);
12381 ++n;
12384 if (WINDOW_WANTS_HEADER_LINE_P (w))
12386 display_mode_line (w, HEADER_LINE_FACE_ID,
12387 current_buffer->header_line_format);
12388 ++n;
12391 return n;
12395 /* Display mode or top line of window W. FACE_ID specifies which line
12396 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12397 FORMAT is the mode line format to display. */
12399 static void
12400 display_mode_line (w, face_id, format)
12401 struct window *w;
12402 enum face_id face_id;
12403 Lisp_Object format;
12405 struct it it;
12406 struct face *face;
12408 init_iterator (&it, w, -1, -1, NULL, face_id);
12409 prepare_desired_row (it.glyph_row);
12411 /* Temporarily make frame's keyboard the current kboard so that
12412 kboard-local variables in the mode_line_format will get the right
12413 values. */
12414 push_frame_kboard (it.f);
12415 display_mode_element (&it, 0, 0, 0, format);
12416 pop_frame_kboard ();
12418 /* Fill up with spaces. */
12419 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12421 compute_line_metrics (&it);
12422 it.glyph_row->full_width_p = 1;
12423 it.glyph_row->mode_line_p = 1;
12424 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12425 it.glyph_row->continued_p = 0;
12426 it.glyph_row->truncated_on_left_p = 0;
12427 it.glyph_row->truncated_on_right_p = 0;
12429 /* Make a 3D mode-line have a shadow at its right end. */
12430 face = FACE_FROM_ID (it.f, face_id);
12431 extend_face_to_end_of_line (&it);
12432 if (face->box != FACE_NO_BOX)
12434 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12435 + it.glyph_row->used[TEXT_AREA] - 1);
12436 last->right_box_line_p = 1;
12441 /* Contribute ELT to the mode line for window IT->w. How it
12442 translates into text depends on its data type.
12444 IT describes the display environment in which we display, as usual.
12446 DEPTH is the depth in recursion. It is used to prevent
12447 infinite recursion here.
12449 FIELD_WIDTH is the number of characters the display of ELT should
12450 occupy in the mode line, and PRECISION is the maximum number of
12451 characters to display from ELT's representation. See
12452 display_string for details. *
12454 Returns the hpos of the end of the text generated by ELT. */
12456 static int
12457 display_mode_element (it, depth, field_width, precision, elt)
12458 struct it *it;
12459 int depth;
12460 int field_width, precision;
12461 Lisp_Object elt;
12463 int n = 0, field, prec;
12465 tail_recurse:
12466 if (depth > 10)
12467 goto invalid;
12469 depth++;
12471 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12473 case Lisp_String:
12475 /* A string: output it and check for %-constructs within it. */
12476 unsigned char c;
12477 unsigned char *this = XSTRING (elt)->data;
12478 unsigned char *lisp_string = this;
12480 while ((precision <= 0 || n < precision)
12481 && *this
12482 && (frame_title_ptr
12483 || it->current_x < it->last_visible_x))
12485 unsigned char *last = this;
12487 /* Advance to end of string or next format specifier. */
12488 while ((c = *this++) != '\0' && c != '%')
12491 if (this - 1 != last)
12493 /* Output to end of string or up to '%'. Field width
12494 is length of string. Don't output more than
12495 PRECISION allows us. */
12496 prec = --this - last;
12497 if (precision > 0 && prec > precision - n)
12498 prec = precision - n;
12500 if (frame_title_ptr)
12501 n += store_frame_title (last, prec, prec);
12502 else
12503 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12504 it, 0, prec, 0, -1);
12506 else /* c == '%' */
12508 unsigned char *percent_position = this;
12510 /* Get the specified minimum width. Zero means
12511 don't pad. */
12512 field = 0;
12513 while ((c = *this++) >= '0' && c <= '9')
12514 field = field * 10 + c - '0';
12516 /* Don't pad beyond the total padding allowed. */
12517 if (field_width - n > 0 && field > field_width - n)
12518 field = field_width - n;
12520 /* Note that either PRECISION <= 0 or N < PRECISION. */
12521 prec = precision - n;
12523 if (c == 'M')
12524 n += display_mode_element (it, depth, field, prec,
12525 Vglobal_mode_string);
12526 else if (c != 0)
12528 unsigned char *spec
12529 = decode_mode_spec (it->w, c, field, prec);
12531 if (frame_title_ptr)
12532 n += store_frame_title (spec, field, prec);
12533 else
12535 int nglyphs_before
12536 = it->glyph_row->used[TEXT_AREA];
12537 int charpos
12538 = percent_position - XSTRING (elt)->data;
12539 int nwritten
12540 = display_string (spec, Qnil, elt, charpos, 0, it,
12541 field, prec, 0, -1);
12543 /* Assign to the glyphs written above the
12544 string where the `%x' came from, position
12545 of the `%'. */
12546 if (nwritten > 0)
12548 struct glyph *glyph
12549 = (it->glyph_row->glyphs[TEXT_AREA]
12550 + nglyphs_before);
12551 int i;
12553 for (i = 0; i < nwritten; ++i)
12555 glyph[i].object = elt;
12556 glyph[i].charpos = charpos;
12559 n += nwritten;
12566 break;
12568 case Lisp_Symbol:
12569 /* A symbol: process the value of the symbol recursively
12570 as if it appeared here directly. Avoid error if symbol void.
12571 Special case: if value of symbol is a string, output the string
12572 literally. */
12574 register Lisp_Object tem;
12575 tem = Fboundp (elt);
12576 if (!NILP (tem))
12578 tem = Fsymbol_value (elt);
12579 /* If value is a string, output that string literally:
12580 don't check for % within it. */
12581 if (STRINGP (tem))
12583 prec = XSTRING (tem)->size;
12584 if (precision > 0 && prec > precision - n)
12585 prec = precision - n;
12586 if (frame_title_ptr)
12587 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12588 else
12589 n += display_string (NULL, tem, Qnil, 0, 0, it,
12590 0, prec, 0, -1);
12592 else if (!EQ (tem, elt))
12594 /* Give up right away for nil or t. */
12595 elt = tem;
12596 goto tail_recurse;
12600 break;
12602 case Lisp_Cons:
12604 register Lisp_Object car, tem;
12606 /* A cons cell: three distinct cases.
12607 If first element is a string or a cons, process all the elements
12608 and effectively concatenate them.
12609 If first element is a negative number, truncate displaying cdr to
12610 at most that many characters. If positive, pad (with spaces)
12611 to at least that many characters.
12612 If first element is a symbol, process the cadr or caddr recursively
12613 according to whether the symbol's value is non-nil or nil. */
12614 car = XCAR (elt);
12615 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12617 /* An element of the form (:eval FORM) means evaluate FORM
12618 and use the result as mode line elements. */
12619 struct gcpro gcpro1;
12620 Lisp_Object spec;
12622 spec = safe_eval (XCAR (XCDR (elt)));
12623 GCPRO1 (spec);
12624 n += display_mode_element (it, depth, field_width - n,
12625 precision - n, spec);
12626 UNGCPRO;
12628 else if (SYMBOLP (car))
12630 tem = Fboundp (car);
12631 elt = XCDR (elt);
12632 if (!CONSP (elt))
12633 goto invalid;
12634 /* elt is now the cdr, and we know it is a cons cell.
12635 Use its car if CAR has a non-nil value. */
12636 if (!NILP (tem))
12638 tem = Fsymbol_value (car);
12639 if (!NILP (tem))
12641 elt = XCAR (elt);
12642 goto tail_recurse;
12645 /* Symbol's value is nil (or symbol is unbound)
12646 Get the cddr of the original list
12647 and if possible find the caddr and use that. */
12648 elt = XCDR (elt);
12649 if (NILP (elt))
12650 break;
12651 else if (!CONSP (elt))
12652 goto invalid;
12653 elt = XCAR (elt);
12654 goto tail_recurse;
12656 else if (INTEGERP (car))
12658 register int lim = XINT (car);
12659 elt = XCDR (elt);
12660 if (lim < 0)
12662 /* Negative int means reduce maximum width. */
12663 if (precision <= 0)
12664 precision = -lim;
12665 else
12666 precision = min (precision, -lim);
12668 else if (lim > 0)
12670 /* Padding specified. Don't let it be more than
12671 current maximum. */
12672 if (precision > 0)
12673 lim = min (precision, lim);
12675 /* If that's more padding than already wanted, queue it.
12676 But don't reduce padding already specified even if
12677 that is beyond the current truncation point. */
12678 field_width = max (lim, field_width);
12680 goto tail_recurse;
12682 else if (STRINGP (car) || CONSP (car))
12684 register int limit = 50;
12685 /* Limit is to protect against circular lists. */
12686 while (CONSP (elt)
12687 && --limit > 0
12688 && (precision <= 0 || n < precision))
12690 n += display_mode_element (it, depth, field_width - n,
12691 precision - n, XCAR (elt));
12692 elt = XCDR (elt);
12696 break;
12698 default:
12699 invalid:
12700 if (frame_title_ptr)
12701 n += store_frame_title ("*invalid*", 0, precision - n);
12702 else
12703 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12704 precision - n, 0, 0);
12705 return n;
12708 /* Pad to FIELD_WIDTH. */
12709 if (field_width > 0 && n < field_width)
12711 if (frame_title_ptr)
12712 n += store_frame_title ("", field_width - n, 0);
12713 else
12714 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12715 0, 0, 0);
12718 return n;
12722 /* Write a null-terminated, right justified decimal representation of
12723 the positive integer D to BUF using a minimal field width WIDTH. */
12725 static void
12726 pint2str (buf, width, d)
12727 register char *buf;
12728 register int width;
12729 register int d;
12731 register char *p = buf;
12733 if (d <= 0)
12734 *p++ = '0';
12735 else
12737 while (d > 0)
12739 *p++ = d % 10 + '0';
12740 d /= 10;
12744 for (width -= (int) (p - buf); width > 0; --width)
12745 *p++ = ' ';
12746 *p-- = '\0';
12747 while (p > buf)
12749 d = *buf;
12750 *buf++ = *p;
12751 *p-- = d;
12755 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12756 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12757 type of CODING_SYSTEM. Return updated pointer into BUF. */
12759 static unsigned char invalid_eol_type[] = "(*invalid*)";
12761 static char *
12762 decode_mode_spec_coding (coding_system, buf, eol_flag)
12763 Lisp_Object coding_system;
12764 register char *buf;
12765 int eol_flag;
12767 Lisp_Object val;
12768 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12769 unsigned char *eol_str;
12770 int eol_str_len;
12771 /* The EOL conversion we are using. */
12772 Lisp_Object eoltype;
12774 val = Fget (coding_system, Qcoding_system);
12775 eoltype = Qnil;
12777 if (!VECTORP (val)) /* Not yet decided. */
12779 if (multibyte)
12780 *buf++ = '-';
12781 if (eol_flag)
12782 eoltype = eol_mnemonic_undecided;
12783 /* Don't mention EOL conversion if it isn't decided. */
12785 else
12787 Lisp_Object eolvalue;
12789 eolvalue = Fget (coding_system, Qeol_type);
12791 if (multibyte)
12792 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12794 if (eol_flag)
12796 /* The EOL conversion that is normal on this system. */
12798 if (NILP (eolvalue)) /* Not yet decided. */
12799 eoltype = eol_mnemonic_undecided;
12800 else if (VECTORP (eolvalue)) /* Not yet decided. */
12801 eoltype = eol_mnemonic_undecided;
12802 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12803 eoltype = (XFASTINT (eolvalue) == 0
12804 ? eol_mnemonic_unix
12805 : (XFASTINT (eolvalue) == 1
12806 ? eol_mnemonic_dos : eol_mnemonic_mac));
12810 if (eol_flag)
12812 /* Mention the EOL conversion if it is not the usual one. */
12813 if (STRINGP (eoltype))
12815 eol_str = XSTRING (eoltype)->data;
12816 eol_str_len = XSTRING (eoltype)->size;
12818 else if (INTEGERP (eoltype)
12819 && CHAR_VALID_P (XINT (eoltype), 0))
12821 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12822 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12824 else
12826 eol_str = invalid_eol_type;
12827 eol_str_len = sizeof (invalid_eol_type) - 1;
12829 bcopy (eol_str, buf, eol_str_len);
12830 buf += eol_str_len;
12833 return buf;
12836 /* Return a string for the output of a mode line %-spec for window W,
12837 generated by character C. PRECISION >= 0 means don't return a
12838 string longer than that value. FIELD_WIDTH > 0 means pad the
12839 string returned with spaces to that value. */
12841 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12843 static char *
12844 decode_mode_spec (w, c, field_width, precision)
12845 struct window *w;
12846 register int c;
12847 int field_width, precision;
12849 Lisp_Object obj;
12850 struct frame *f = XFRAME (WINDOW_FRAME (w));
12851 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12852 struct buffer *b = XBUFFER (w->buffer);
12854 obj = Qnil;
12856 switch (c)
12858 case '*':
12859 if (!NILP (b->read_only))
12860 return "%";
12861 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12862 return "*";
12863 return "-";
12865 case '+':
12866 /* This differs from %* only for a modified read-only buffer. */
12867 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12868 return "*";
12869 if (!NILP (b->read_only))
12870 return "%";
12871 return "-";
12873 case '&':
12874 /* This differs from %* in ignoring read-only-ness. */
12875 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12876 return "*";
12877 return "-";
12879 case '%':
12880 return "%";
12882 case '[':
12884 int i;
12885 char *p;
12887 if (command_loop_level > 5)
12888 return "[[[... ";
12889 p = decode_mode_spec_buf;
12890 for (i = 0; i < command_loop_level; i++)
12891 *p++ = '[';
12892 *p = 0;
12893 return decode_mode_spec_buf;
12896 case ']':
12898 int i;
12899 char *p;
12901 if (command_loop_level > 5)
12902 return " ...]]]";
12903 p = decode_mode_spec_buf;
12904 for (i = 0; i < command_loop_level; i++)
12905 *p++ = ']';
12906 *p = 0;
12907 return decode_mode_spec_buf;
12910 case '-':
12912 register int i;
12914 /* Let lots_of_dashes be a string of infinite length. */
12915 if (field_width <= 0
12916 || field_width > sizeof (lots_of_dashes))
12918 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12919 decode_mode_spec_buf[i] = '-';
12920 decode_mode_spec_buf[i] = '\0';
12921 return decode_mode_spec_buf;
12923 else
12924 return lots_of_dashes;
12927 case 'b':
12928 obj = b->name;
12929 break;
12931 case 'c':
12933 int col = current_column ();
12934 XSETFASTINT (w->column_number_displayed, col);
12935 pint2str (decode_mode_spec_buf, field_width, col);
12936 return decode_mode_spec_buf;
12939 case 'F':
12940 /* %F displays the frame name. */
12941 if (!NILP (f->title))
12942 return (char *) XSTRING (f->title)->data;
12943 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12944 return (char *) XSTRING (f->name)->data;
12945 return "Emacs";
12947 case 'f':
12948 obj = b->filename;
12949 break;
12951 case 'l':
12953 int startpos = XMARKER (w->start)->charpos;
12954 int startpos_byte = marker_byte_position (w->start);
12955 int line, linepos, linepos_byte, topline;
12956 int nlines, junk;
12957 int height = XFASTINT (w->height);
12959 /* If we decided that this buffer isn't suitable for line numbers,
12960 don't forget that too fast. */
12961 if (EQ (w->base_line_pos, w->buffer))
12962 goto no_value;
12963 /* But do forget it, if the window shows a different buffer now. */
12964 else if (BUFFERP (w->base_line_pos))
12965 w->base_line_pos = Qnil;
12967 /* If the buffer is very big, don't waste time. */
12968 if (INTEGERP (Vline_number_display_limit)
12969 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
12971 w->base_line_pos = Qnil;
12972 w->base_line_number = Qnil;
12973 goto no_value;
12976 if (!NILP (w->base_line_number)
12977 && !NILP (w->base_line_pos)
12978 && XFASTINT (w->base_line_pos) <= startpos)
12980 line = XFASTINT (w->base_line_number);
12981 linepos = XFASTINT (w->base_line_pos);
12982 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12984 else
12986 line = 1;
12987 linepos = BUF_BEGV (b);
12988 linepos_byte = BUF_BEGV_BYTE (b);
12991 /* Count lines from base line to window start position. */
12992 nlines = display_count_lines (linepos, linepos_byte,
12993 startpos_byte,
12994 startpos, &junk);
12996 topline = nlines + line;
12998 /* Determine a new base line, if the old one is too close
12999 or too far away, or if we did not have one.
13000 "Too close" means it's plausible a scroll-down would
13001 go back past it. */
13002 if (startpos == BUF_BEGV (b))
13004 XSETFASTINT (w->base_line_number, topline);
13005 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13007 else if (nlines < height + 25 || nlines > height * 3 + 50
13008 || linepos == BUF_BEGV (b))
13010 int limit = BUF_BEGV (b);
13011 int limit_byte = BUF_BEGV_BYTE (b);
13012 int position;
13013 int distance = (height * 2 + 30) * line_number_display_limit_width;
13015 if (startpos - distance > limit)
13017 limit = startpos - distance;
13018 limit_byte = CHAR_TO_BYTE (limit);
13021 nlines = display_count_lines (startpos, startpos_byte,
13022 limit_byte,
13023 - (height * 2 + 30),
13024 &position);
13025 /* If we couldn't find the lines we wanted within
13026 line_number_display_limit_width chars per line,
13027 give up on line numbers for this window. */
13028 if (position == limit_byte && limit == startpos - distance)
13030 w->base_line_pos = w->buffer;
13031 w->base_line_number = Qnil;
13032 goto no_value;
13035 XSETFASTINT (w->base_line_number, topline - nlines);
13036 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13039 /* Now count lines from the start pos to point. */
13040 nlines = display_count_lines (startpos, startpos_byte,
13041 PT_BYTE, PT, &junk);
13043 /* Record that we did display the line number. */
13044 line_number_displayed = 1;
13046 /* Make the string to show. */
13047 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13048 return decode_mode_spec_buf;
13049 no_value:
13051 char* p = decode_mode_spec_buf;
13052 int pad = field_width - 2;
13053 while (pad-- > 0)
13054 *p++ = ' ';
13055 *p++ = '?';
13056 *p++ = '?';
13057 *p = '\0';
13058 return decode_mode_spec_buf;
13061 break;
13063 case 'm':
13064 obj = b->mode_name;
13065 break;
13067 case 'n':
13068 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13069 return " Narrow";
13070 break;
13072 case 'p':
13074 int pos = marker_position (w->start);
13075 int total = BUF_ZV (b) - BUF_BEGV (b);
13077 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13079 if (pos <= BUF_BEGV (b))
13080 return "All";
13081 else
13082 return "Bottom";
13084 else if (pos <= BUF_BEGV (b))
13085 return "Top";
13086 else
13088 if (total > 1000000)
13089 /* Do it differently for a large value, to avoid overflow. */
13090 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13091 else
13092 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13093 /* We can't normally display a 3-digit number,
13094 so get us a 2-digit number that is close. */
13095 if (total == 100)
13096 total = 99;
13097 sprintf (decode_mode_spec_buf, "%2d%%", total);
13098 return decode_mode_spec_buf;
13102 /* Display percentage of size above the bottom of the screen. */
13103 case 'P':
13105 int toppos = marker_position (w->start);
13106 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13107 int total = BUF_ZV (b) - BUF_BEGV (b);
13109 if (botpos >= BUF_ZV (b))
13111 if (toppos <= BUF_BEGV (b))
13112 return "All";
13113 else
13114 return "Bottom";
13116 else
13118 if (total > 1000000)
13119 /* Do it differently for a large value, to avoid overflow. */
13120 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13121 else
13122 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13123 /* We can't normally display a 3-digit number,
13124 so get us a 2-digit number that is close. */
13125 if (total == 100)
13126 total = 99;
13127 if (toppos <= BUF_BEGV (b))
13128 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13129 else
13130 sprintf (decode_mode_spec_buf, "%2d%%", total);
13131 return decode_mode_spec_buf;
13135 case 's':
13136 /* status of process */
13137 obj = Fget_buffer_process (w->buffer);
13138 if (NILP (obj))
13139 return "no process";
13140 #ifdef subprocesses
13141 obj = Fsymbol_name (Fprocess_status (obj));
13142 #endif
13143 break;
13145 case 't': /* indicate TEXT or BINARY */
13146 #ifdef MODE_LINE_BINARY_TEXT
13147 return MODE_LINE_BINARY_TEXT (b);
13148 #else
13149 return "T";
13150 #endif
13152 case 'z':
13153 /* coding-system (not including end-of-line format) */
13154 case 'Z':
13155 /* coding-system (including end-of-line type) */
13157 int eol_flag = (c == 'Z');
13158 char *p = decode_mode_spec_buf;
13160 if (! FRAME_WINDOW_P (f))
13162 /* No need to mention EOL here--the terminal never needs
13163 to do EOL conversion. */
13164 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13165 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13167 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13168 p, eol_flag);
13170 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13171 #ifdef subprocesses
13172 obj = Fget_buffer_process (Fcurrent_buffer ());
13173 if (PROCESSP (obj))
13175 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13176 p, eol_flag);
13177 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13178 p, eol_flag);
13180 #endif /* subprocesses */
13181 #endif /* 0 */
13182 *p = 0;
13183 return decode_mode_spec_buf;
13187 if (STRINGP (obj))
13188 return (char *) XSTRING (obj)->data;
13189 else
13190 return "";
13194 /* Count up to COUNT lines starting from START / START_BYTE.
13195 But don't go beyond LIMIT_BYTE.
13196 Return the number of lines thus found (always nonnegative).
13198 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13200 static int
13201 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13202 int start, start_byte, limit_byte, count;
13203 int *byte_pos_ptr;
13205 register unsigned char *cursor;
13206 unsigned char *base;
13208 register int ceiling;
13209 register unsigned char *ceiling_addr;
13210 int orig_count = count;
13212 /* If we are not in selective display mode,
13213 check only for newlines. */
13214 int selective_display = (!NILP (current_buffer->selective_display)
13215 && !INTEGERP (current_buffer->selective_display));
13217 if (count > 0)
13219 while (start_byte < limit_byte)
13221 ceiling = BUFFER_CEILING_OF (start_byte);
13222 ceiling = min (limit_byte - 1, ceiling);
13223 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13224 base = (cursor = BYTE_POS_ADDR (start_byte));
13225 while (1)
13227 if (selective_display)
13228 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13230 else
13231 while (*cursor != '\n' && ++cursor != ceiling_addr)
13234 if (cursor != ceiling_addr)
13236 if (--count == 0)
13238 start_byte += cursor - base + 1;
13239 *byte_pos_ptr = start_byte;
13240 return orig_count;
13242 else
13243 if (++cursor == ceiling_addr)
13244 break;
13246 else
13247 break;
13249 start_byte += cursor - base;
13252 else
13254 while (start_byte > limit_byte)
13256 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13257 ceiling = max (limit_byte, ceiling);
13258 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13259 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13260 while (1)
13262 if (selective_display)
13263 while (--cursor != ceiling_addr
13264 && *cursor != '\n' && *cursor != 015)
13266 else
13267 while (--cursor != ceiling_addr && *cursor != '\n')
13270 if (cursor != ceiling_addr)
13272 if (++count == 0)
13274 start_byte += cursor - base + 1;
13275 *byte_pos_ptr = start_byte;
13276 /* When scanning backwards, we should
13277 not count the newline posterior to which we stop. */
13278 return - orig_count - 1;
13281 else
13282 break;
13284 /* Here we add 1 to compensate for the last decrement
13285 of CURSOR, which took it past the valid range. */
13286 start_byte += cursor - base + 1;
13290 *byte_pos_ptr = limit_byte;
13292 if (count < 0)
13293 return - orig_count + count;
13294 return orig_count - count;
13300 /***********************************************************************
13301 Displaying strings
13302 ***********************************************************************/
13304 /* Display a NUL-terminated string, starting with index START.
13306 If STRING is non-null, display that C string. Otherwise, the Lisp
13307 string LISP_STRING is displayed.
13309 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13310 FACE_STRING. Display STRING or LISP_STRING with the face at
13311 FACE_STRING_POS in FACE_STRING:
13313 Display the string in the environment given by IT, but use the
13314 standard display table, temporarily.
13316 FIELD_WIDTH is the minimum number of output glyphs to produce.
13317 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13318 with spaces. If STRING has more characters, more than FIELD_WIDTH
13319 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13321 PRECISION is the maximum number of characters to output from
13322 STRING. PRECISION < 0 means don't truncate the string.
13324 This is roughly equivalent to printf format specifiers:
13326 FIELD_WIDTH PRECISION PRINTF
13327 ----------------------------------------
13328 -1 -1 %s
13329 -1 10 %.10s
13330 10 -1 %10s
13331 20 10 %20.10s
13333 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13334 display them, and < 0 means obey the current buffer's value of
13335 enable_multibyte_characters.
13337 Value is the number of glyphs produced. */
13339 static int
13340 display_string (string, lisp_string, face_string, face_string_pos,
13341 start, it, field_width, precision, max_x, multibyte)
13342 unsigned char *string;
13343 Lisp_Object lisp_string;
13344 Lisp_Object face_string;
13345 int face_string_pos;
13346 int start;
13347 struct it *it;
13348 int field_width, precision, max_x;
13349 int multibyte;
13351 int hpos_at_start = it->hpos;
13352 int saved_face_id = it->face_id;
13353 struct glyph_row *row = it->glyph_row;
13355 /* Initialize the iterator IT for iteration over STRING beginning
13356 with index START. We assume that IT may be modified here (which
13357 means that display_line has to do something when displaying a
13358 mini-buffer prompt, which it does). */
13359 reseat_to_string (it, string, lisp_string, start,
13360 precision, field_width, multibyte);
13362 /* If displaying STRING, set up the face of the iterator
13363 from LISP_STRING, if that's given. */
13364 if (STRINGP (face_string))
13366 int endptr;
13367 struct face *face;
13369 it->face_id
13370 = face_at_string_position (it->w, face_string, face_string_pos,
13371 0, it->region_beg_charpos,
13372 it->region_end_charpos,
13373 &endptr, it->base_face_id);
13374 face = FACE_FROM_ID (it->f, it->face_id);
13375 it->face_box_p = face->box != FACE_NO_BOX;
13378 /* Set max_x to the maximum allowed X position. Don't let it go
13379 beyond the right edge of the window. */
13380 if (max_x <= 0)
13381 max_x = it->last_visible_x;
13382 else
13383 max_x = min (max_x, it->last_visible_x);
13385 /* Skip over display elements that are not visible. because IT->w is
13386 hscrolled. */
13387 if (it->current_x < it->first_visible_x)
13388 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13389 MOVE_TO_POS | MOVE_TO_X);
13391 row->ascent = it->max_ascent;
13392 row->height = it->max_ascent + it->max_descent;
13393 row->phys_ascent = it->max_phys_ascent;
13394 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13396 /* This condition is for the case that we are called with current_x
13397 past last_visible_x. */
13398 while (it->current_x < max_x)
13400 int x_before, x, n_glyphs_before, i, nglyphs;
13402 /* Get the next display element. */
13403 if (!get_next_display_element (it))
13404 break;
13406 /* Produce glyphs. */
13407 x_before = it->current_x;
13408 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13409 PRODUCE_GLYPHS (it);
13411 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13412 i = 0;
13413 x = x_before;
13414 while (i < nglyphs)
13416 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13418 if (!it->truncate_lines_p
13419 && x + glyph->pixel_width > max_x)
13421 /* End of continued line or max_x reached. */
13422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13423 it->current_x = x;
13424 break;
13426 else if (x + glyph->pixel_width > it->first_visible_x)
13428 /* Glyph is at least partially visible. */
13429 ++it->hpos;
13430 if (x < it->first_visible_x)
13431 it->glyph_row->x = x - it->first_visible_x;
13433 else
13435 /* Glyph is off the left margin of the display area.
13436 Should not happen. */
13437 abort ();
13440 row->ascent = max (row->ascent, it->max_ascent);
13441 row->height = max (row->height, it->max_ascent + it->max_descent);
13442 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13443 row->phys_height = max (row->phys_height,
13444 it->max_phys_ascent + it->max_phys_descent);
13445 x += glyph->pixel_width;
13446 ++i;
13449 /* Stop if max_x reached. */
13450 if (i < nglyphs)
13451 break;
13453 /* Stop at line ends. */
13454 if (ITERATOR_AT_END_OF_LINE_P (it))
13456 it->continuation_lines_width = 0;
13457 break;
13460 set_iterator_to_next (it, 1);
13462 /* Stop if truncating at the right edge. */
13463 if (it->truncate_lines_p
13464 && it->current_x >= it->last_visible_x)
13466 /* Add truncation mark, but don't do it if the line is
13467 truncated at a padding space. */
13468 if (IT_CHARPOS (*it) < it->string_nchars)
13470 if (!FRAME_WINDOW_P (it->f))
13471 produce_special_glyphs (it, IT_TRUNCATION);
13472 it->glyph_row->truncated_on_right_p = 1;
13474 break;
13478 /* Maybe insert a truncation at the left. */
13479 if (it->first_visible_x
13480 && IT_CHARPOS (*it) > 0)
13482 if (!FRAME_WINDOW_P (it->f))
13483 insert_left_trunc_glyphs (it);
13484 it->glyph_row->truncated_on_left_p = 1;
13487 it->face_id = saved_face_id;
13489 /* Value is number of columns displayed. */
13490 return it->hpos - hpos_at_start;
13495 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13496 appears as an element of LIST or as the car of an element of LIST.
13497 If PROPVAL is a list, compare each element against LIST in that
13498 way, and return 1 if any element of PROPVAL is found in LIST.
13499 Otherwise return 0. This function cannot quit. */
13502 invisible_p (propval, list)
13503 register Lisp_Object propval;
13504 Lisp_Object list;
13506 register Lisp_Object tail, proptail;
13508 for (tail = list; CONSP (tail); tail = XCDR (tail))
13510 register Lisp_Object tem;
13511 tem = XCAR (tail);
13512 if (EQ (propval, tem))
13513 return 1;
13514 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13515 return 1;
13518 if (CONSP (propval))
13520 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13522 Lisp_Object propelt;
13523 propelt = XCAR (proptail);
13524 for (tail = list; CONSP (tail); tail = XCDR (tail))
13526 register Lisp_Object tem;
13527 tem = XCAR (tail);
13528 if (EQ (propelt, tem))
13529 return 1;
13530 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13531 return 1;
13536 return 0;
13540 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13541 the cdr of that element is non-nil. If PROPVAL is a list, check
13542 each element of PROPVAL in that way, and the first time some
13543 element is found, return 1 if the cdr of that element is non-nil.
13544 Otherwise return 0. This function cannot quit. */
13547 invisible_ellipsis_p (propval, list)
13548 register Lisp_Object propval;
13549 Lisp_Object list;
13551 register Lisp_Object tail, proptail;
13553 for (tail = list; CONSP (tail); tail = XCDR (tail))
13555 register Lisp_Object tem;
13556 tem = XCAR (tail);
13557 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13558 return ! NILP (XCDR (tem));
13561 if (CONSP (propval))
13562 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13564 Lisp_Object propelt;
13565 propelt = XCAR (proptail);
13566 for (tail = list; CONSP (tail); tail = XCDR (tail))
13568 register Lisp_Object tem;
13569 tem = XCAR (tail);
13570 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13571 return ! NILP (XCDR (tem));
13575 return 0;
13580 /***********************************************************************
13581 Initialization
13582 ***********************************************************************/
13584 void
13585 syms_of_xdisp ()
13587 Vwith_echo_area_save_vector = Qnil;
13588 staticpro (&Vwith_echo_area_save_vector);
13590 Vmessage_stack = Qnil;
13591 staticpro (&Vmessage_stack);
13593 Qinhibit_redisplay = intern ("inhibit-redisplay");
13594 staticpro (&Qinhibit_redisplay);
13596 #if GLYPH_DEBUG
13597 defsubr (&Sdump_glyph_matrix);
13598 defsubr (&Sdump_glyph_row);
13599 defsubr (&Sdump_tool_bar_row);
13600 defsubr (&Strace_redisplay_toggle);
13601 defsubr (&Strace_to_stderr);
13602 #endif
13604 staticpro (&Qmenu_bar_update_hook);
13605 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13607 staticpro (&Qoverriding_terminal_local_map);
13608 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13610 staticpro (&Qoverriding_local_map);
13611 Qoverriding_local_map = intern ("overriding-local-map");
13613 staticpro (&Qwindow_scroll_functions);
13614 Qwindow_scroll_functions = intern ("window-scroll-functions");
13616 staticpro (&Qredisplay_end_trigger_functions);
13617 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13619 staticpro (&Qinhibit_point_motion_hooks);
13620 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13622 QCdata = intern (":data");
13623 staticpro (&QCdata);
13624 Qdisplay = intern ("display");
13625 staticpro (&Qdisplay);
13626 Qspace_width = intern ("space-width");
13627 staticpro (&Qspace_width);
13628 Qraise = intern ("raise");
13629 staticpro (&Qraise);
13630 Qspace = intern ("space");
13631 staticpro (&Qspace);
13632 Qmargin = intern ("margin");
13633 staticpro (&Qmargin);
13634 Qleft_margin = intern ("left-margin");
13635 staticpro (&Qleft_margin);
13636 Qright_margin = intern ("right-margin");
13637 staticpro (&Qright_margin);
13638 Qalign_to = intern ("align-to");
13639 staticpro (&Qalign_to);
13640 QCalign_to = intern (":align-to");
13641 staticpro (&QCalign_to);
13642 Qrelative_width = intern ("relative-width");
13643 staticpro (&Qrelative_width);
13644 QCrelative_width = intern (":relative-width");
13645 staticpro (&QCrelative_width);
13646 QCrelative_height = intern (":relative-height");
13647 staticpro (&QCrelative_height);
13648 QCeval = intern (":eval");
13649 staticpro (&QCeval);
13650 Qwhen = intern ("when");
13651 staticpro (&Qwhen);
13652 QCfile = intern (":file");
13653 staticpro (&QCfile);
13654 Qfontified = intern ("fontified");
13655 staticpro (&Qfontified);
13656 Qfontification_functions = intern ("fontification-functions");
13657 staticpro (&Qfontification_functions);
13658 Qtrailing_whitespace = intern ("trailing-whitespace");
13659 staticpro (&Qtrailing_whitespace);
13660 Qimage = intern ("image");
13661 staticpro (&Qimage);
13662 Qmessage_truncate_lines = intern ("message-truncate-lines");
13663 staticpro (&Qmessage_truncate_lines);
13665 last_arrow_position = Qnil;
13666 last_arrow_string = Qnil;
13667 staticpro (&last_arrow_position);
13668 staticpro (&last_arrow_string);
13670 echo_buffer[0] = echo_buffer[1] = Qnil;
13671 staticpro (&echo_buffer[0]);
13672 staticpro (&echo_buffer[1]);
13674 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13675 staticpro (&echo_area_buffer[0]);
13676 staticpro (&echo_area_buffer[1]);
13678 Vmessages_buffer_name = build_string ("*Messages*");
13679 staticpro (&Vmessages_buffer_name);
13681 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13682 "Non-nil means highlight trailing whitespace.\n\
13683 The face used for trailing whitespace is `trailing-whitespace'.");
13684 Vshow_trailing_whitespace = Qnil;
13686 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13687 "Non-nil means don't actually do any redisplay.\n\
13688 This is used for internal purposes.");
13689 Vinhibit_redisplay = Qnil;
13691 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13692 "String (or mode line construct) included (normally) in `mode-line-format'.");
13693 Vglobal_mode_string = Qnil;
13695 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13696 "Marker for where to display an arrow on top of the buffer text.\n\
13697 This must be the beginning of a line in order to work.\n\
13698 See also `overlay-arrow-string'.");
13699 Voverlay_arrow_position = Qnil;
13701 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13702 "String to display as an arrow. See also `overlay-arrow-position'.");
13703 Voverlay_arrow_string = Qnil;
13705 DEFVAR_INT ("scroll-step", &scroll_step,
13706 "*The number of lines to try scrolling a window by when point moves out.\n\
13707 If that fails to bring point back on frame, point is centered instead.\n\
13708 If this is zero, point is always centered after it moves off frame.");
13710 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13711 "*Scroll up to this many lines, to bring point back on screen.\n\
13712 A value of zero means to scroll the text to center point vertically\n\
13713 in the window.");
13714 scroll_conservatively = 0;
13716 DEFVAR_INT ("scroll-margin", &scroll_margin,
13717 "*Number of lines of margin at the top and bottom of a window.\n\
13718 Recenter the window whenever point gets within this many lines\n\
13719 of the top or bottom of the window.");
13720 scroll_margin = 0;
13722 #if GLYPH_DEBUG
13723 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13724 #endif
13726 DEFVAR_BOOL ("truncate-partial-width-windows",
13727 &truncate_partial_width_windows,
13728 "*Non-nil means truncate lines in all windows less than full frame wide.");
13729 truncate_partial_width_windows = 1;
13731 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13732 "*Non-nil means use inverse video for the mode line.");
13733 mode_line_inverse_video = 1;
13735 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13736 "*Maximum buffer size for which line number should be displayed.\n\
13737 If the buffer is bigger than this, the line number does not appear\n\
13738 in the mode line. A value of nil means no limit.");
13739 Vline_number_display_limit = Qnil;
13741 DEFVAR_INT ("line-number-display-limit-width",
13742 &line_number_display_limit_width,
13743 "*Maximum line width (in characters) for line number display.\n\
13744 If the average length of the lines near point is bigger than this, then the\n\
13745 line number may be omitted from the mode line.");
13746 line_number_display_limit_width = 200;
13748 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13749 "*Non-nil means highlight region even in nonselected windows.");
13750 highlight_nonselected_windows = 0;
13752 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13753 "Non-nil if more than one frame is visible on this display.\n\
13754 Minibuffer-only frames don't count, but iconified frames do.\n\
13755 This variable is not guaranteed to be accurate except while processing\n\
13756 `frame-title-format' and `icon-title-format'.");
13758 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13759 "Template for displaying the title bar of visible frames.\n\
13760 \(Assuming the window manager supports this feature.)\n\
13761 This variable has the same structure as `mode-line-format' (which see),\n\
13762 and is used only on frames for which no explicit name has been set\n\
13763 \(see `modify-frame-parameters').");
13764 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13765 "Template for displaying the title bar of an iconified frame.\n\
13766 \(Assuming the window manager supports this feature.)\n\
13767 This variable has the same structure as `mode-line-format' (which see),\n\
13768 and is used only on frames for which no explicit name has been set\n\
13769 \(see `modify-frame-parameters').");
13770 Vicon_title_format
13771 = Vframe_title_format
13772 = Fcons (intern ("multiple-frames"),
13773 Fcons (build_string ("%b"),
13774 Fcons (Fcons (build_string (""),
13775 Fcons (intern ("invocation-name"),
13776 Fcons (build_string ("@"),
13777 Fcons (intern ("system-name"),
13778 Qnil)))),
13779 Qnil)));
13781 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13782 "Maximum number of lines to keep in the message log buffer.\n\
13783 If nil, disable message logging. If t, log messages but don't truncate\n\
13784 the buffer when it becomes large.");
13785 XSETFASTINT (Vmessage_log_max, 50);
13787 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13788 "Functions called before redisplay, if window sizes have changed.\n\
13789 The value should be a list of functions that take one argument.\n\
13790 Just before redisplay, for each frame, if any of its windows have changed\n\
13791 size since the last redisplay, or have been split or deleted,\n\
13792 all the functions in the list are called, with the frame as argument.");
13793 Vwindow_size_change_functions = Qnil;
13795 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13796 "List of Functions to call before redisplaying a window with scrolling.\n\
13797 Each function is called with two arguments, the window\n\
13798 and its new display-start position. Note that the value of `window-end'\n\
13799 is not valid when these functions are called.");
13800 Vwindow_scroll_functions = Qnil;
13802 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13803 "*Non-nil means automatically resize tool-bars.\n\
13804 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13805 It decreases a tool-bar's height when it would display blank lines\n\
13806 otherwise.");
13807 auto_resize_tool_bars_p = 1;
13809 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13810 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13811 auto_raise_tool_bar_buttons_p = 1;
13813 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13814 "*Margin around tool-bar buttons in pixels.");
13815 tool_bar_button_margin = 1;
13817 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13818 "Relief thickness of tool-bar buttons.");
13819 tool_bar_button_relief = 3;
13821 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13822 "List of functions to call to fontify regions of text.\n\
13823 Each function is called with one argument POS. Functions must\n\
13824 fontify a region starting at POS in the current buffer, and give\n\
13825 fontified regions the property `fontified'.\n\
13826 This variable automatically becomes buffer-local when set.");
13827 Vfontification_functions = Qnil;
13828 Fmake_local_variable (Qfontification_functions);
13830 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13831 &unibyte_display_via_language_environment,
13832 "*Non-nil means display unibyte text according to language environment.\n\
13833 Specifically this means that unibyte non-ASCII characters\n\
13834 are displayed by converting them to the equivalent multibyte characters\n\
13835 according to the current language environment. As a result, they are\n\
13836 displayed according to the current fontset.");
13837 unibyte_display_via_language_environment = 0;
13839 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13840 "*Maximum height for resizing mini-windows.\n\
13841 If a float, it specifies a fraction of the mini-window frame's height.\n\
13842 If an integer, it specifies a number of lines.\n\
13843 If nil, don't resize.");
13844 Vmax_mini_window_height = make_float (0.25);
13846 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13847 &cursor_in_non_selected_windows,
13848 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13849 Nil means don't display a cursor there.");
13850 cursor_in_non_selected_windows = 1;
13852 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13853 "*Non-nil means scroll the display automatically to make point visible.");
13854 automatic_hscrolling_p = 1;
13856 DEFVAR_LISP ("image-types", &Vimage_types,
13857 "List of supported image types.\n\
13858 Each element of the list is a symbol for a supported image type.");
13859 Vimage_types = Qnil;
13861 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13862 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13863 Bind this around calls to `message' to let it take effect.");
13864 message_truncate_lines = 0;
13866 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13867 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13868 Can be used to update submenus whose contents should vary.");
13873 /* Initialize this module when Emacs starts. */
13875 void
13876 init_xdisp ()
13878 Lisp_Object root_window;
13879 struct window *mini_w;
13881 CHARPOS (this_line_start_pos) = 0;
13883 mini_w = XWINDOW (minibuf_window);
13884 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13886 if (!noninteractive)
13888 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13889 int i;
13891 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13892 set_window_height (root_window,
13893 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13895 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13896 set_window_height (minibuf_window, 1, 0);
13898 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13899 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13901 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13902 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13903 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13905 /* The default ellipsis glyphs `...'. */
13906 for (i = 0; i < 3; ++i)
13907 XSETFASTINT (default_invis_vector[i], '.');
13910 #ifdef HAVE_WINDOW_SYSTEM
13912 /* Allocate the buffer for frame titles. */
13913 int size = 100;
13914 frame_title_buf = (char *) xmalloc (size);
13915 frame_title_buf_end = frame_title_buf + size;
13916 frame_title_ptr = NULL;
13918 #endif /* HAVE_WINDOW_SYSTEM */
13920 help_echo_showing_p = 0;