(font-lock-syntactically-fontified): New var.
[emacs.git] / src / xdisp.c
blob3c78296aa706b2ecec008071187fc0c00eea37f3
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
198 #define min(a, b) ((a) < (b) ? (a) : (b))
199 #define max(a, b) ((a) > (b) ? (a) : (b))
201 #define INFINITY 10000000
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
208 extern int interrupt_input;
209 extern int command_loop_level;
211 extern int minibuffer_auto_raise;
213 extern Lisp_Object Qface;
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
224 Lisp_Object Qfontified;
226 /* Functions called to fontify regions of text. */
228 Lisp_Object Vfontification_functions;
229 Lisp_Object Qfontification_functions;
231 /* Non-zero means draw tool bar buttons raised when the mouse moves
232 over them. */
234 int auto_raise_tool_bar_buttons_p;
236 /* Margin around tool bar buttons in pixels. */
238 int tool_bar_button_margin;
240 /* Thickness of shadow to draw around tool bar buttons. */
242 int tool_bar_button_relief;
244 /* Non-zero means automatically resize tool-bars so that all tool-bar
245 items are visible, and no blank lines remain. */
247 int auto_resize_tool_bars_p;
249 /* Non-nil means don't actually do any redisplay. */
251 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
253 /* Names of text properties relevant for redisplay. */
255 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
256 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
258 /* Symbols used in text property values. */
260 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
261 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
262 Lisp_Object Qmargin;
263 extern Lisp_Object Qheight;
265 /* Non-nil means highlight trailing whitespace. */
267 Lisp_Object Vshow_trailing_whitespace;
269 /* Name of the face used to highlight trailing whitespace. */
271 Lisp_Object Qtrailing_whitespace;
273 /* The symbol `image' which is the car of the lists used to represent
274 images in Lisp. */
276 Lisp_Object Qimage;
278 /* Non-zero means print newline to stdout before next mini-buffer
279 message. */
281 int noninteractive_need_newline;
283 /* Non-zero means print newline to message log before next message. */
285 static int message_log_need_newline;
288 /* The buffer position of the first character appearing entirely or
289 partially on the line of the selected window which contains the
290 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
291 redisplay optimization in redisplay_internal. */
293 static struct text_pos this_line_start_pos;
295 /* Number of characters past the end of the line above, including the
296 terminating newline. */
298 static struct text_pos this_line_end_pos;
300 /* The vertical positions and the height of this line. */
302 static int this_line_vpos;
303 static int this_line_y;
304 static int this_line_pixel_height;
306 /* X position at which this display line starts. Usually zero;
307 negative if first character is partially visible. */
309 static int this_line_start_x;
311 /* Buffer that this_line_.* variables are referring to. */
313 static struct buffer *this_line_buffer;
315 /* Nonzero means truncate lines in all windows less wide than the
316 frame. */
318 int truncate_partial_width_windows;
320 /* A flag to control how to display unibyte 8-bit character. */
322 int unibyte_display_via_language_environment;
324 /* Nonzero means we have more than one non-mini-buffer-only frame.
325 Not guaranteed to be accurate except while parsing
326 frame-title-format. */
328 int multiple_frames;
330 Lisp_Object Vglobal_mode_string;
332 /* Marker for where to display an arrow on top of the buffer text. */
334 Lisp_Object Voverlay_arrow_position;
336 /* String to display for the arrow. Only used on terminal frames. */
338 Lisp_Object Voverlay_arrow_string;
340 /* Values of those variables at last redisplay. However, if
341 Voverlay_arrow_position is a marker, last_arrow_position is its
342 numerical position. */
344 static Lisp_Object last_arrow_position, last_arrow_string;
346 /* Like mode-line-format, but for the title bar on a visible frame. */
348 Lisp_Object Vframe_title_format;
350 /* Like mode-line-format, but for the title bar on an iconified frame. */
352 Lisp_Object Vicon_title_format;
354 /* List of functions to call when a window's size changes. These
355 functions get one arg, a frame on which one or more windows' sizes
356 have changed. */
358 static Lisp_Object Vwindow_size_change_functions;
360 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
362 /* Nonzero if overlay arrow has been displayed once in this window. */
364 static int overlay_arrow_seen;
366 /* Nonzero means highlight the region even in nonselected windows. */
368 int highlight_nonselected_windows;
370 /* If cursor motion alone moves point off frame, try scrolling this
371 many lines up or down if that will bring it back. */
373 static int scroll_step;
375 /* Non-0 means scroll just far enough to bring point back on the
376 screen, when appropriate. */
378 static int scroll_conservatively;
380 /* Recenter the window whenever point gets within this many lines of
381 the top or bottom of the window. This value is translated into a
382 pixel value by multiplying it with CANON_Y_UNIT, which means that
383 there is really a fixed pixel height scroll margin. */
385 int scroll_margin;
387 /* Number of windows showing the buffer of the selected window (or
388 another buffer with the same base buffer). keyboard.c refers to
389 this. */
391 int buffer_shared;
393 /* Vector containing glyphs for an ellipsis `...'. */
395 static Lisp_Object default_invis_vector[3];
397 /* Nonzero means display mode line highlighted. */
399 int mode_line_inverse_video;
401 /* Prompt to display in front of the mini-buffer contents. */
403 Lisp_Object minibuf_prompt;
405 /* Width of current mini-buffer prompt. Only set after display_line
406 of the line that contains the prompt. */
408 int minibuf_prompt_width;
409 int minibuf_prompt_pixel_width;
411 /* This is the window where the echo area message was displayed. It
412 is always a mini-buffer window, but it may not be the same window
413 currently active as a mini-buffer. */
415 Lisp_Object echo_area_window;
417 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
418 pushes the current message and the value of
419 message_enable_multibyte on the stack, the function restore_message
420 pops the stack and displays MESSAGE again. */
422 Lisp_Object Vmessage_stack;
424 /* Nonzero means multibyte characters were enabled when the echo area
425 message was specified. */
427 int message_enable_multibyte;
429 /* True if we should redraw the mode lines on the next redisplay. */
431 int update_mode_lines;
433 /* Nonzero if window sizes or contents have changed since last
434 redisplay that finished */
436 int windows_or_buffers_changed;
438 /* Nonzero after display_mode_line if %l was used and it displayed a
439 line number. */
441 int line_number_displayed;
443 /* Maximum buffer size for which to display line numbers. */
445 Lisp_Object Vline_number_display_limit;
447 /* line width to consider when repostioning for line number display */
449 static int line_number_display_limit_width;
451 /* Number of lines to keep in the message log buffer. t means
452 infinite. nil means don't log at all. */
454 Lisp_Object Vmessage_log_max;
456 /* The name of the *Messages* buffer, a string. */
458 static Lisp_Object Vmessages_buffer_name;
460 /* Current, index 0, and last displayed echo area message. Either
461 buffers from echo_buffers, or nil to indicate no message. */
463 Lisp_Object echo_area_buffer[2];
465 /* The buffers referenced from echo_area_buffer. */
467 static Lisp_Object echo_buffer[2];
469 /* A vector saved used in with_area_buffer to reduce consing. */
471 static Lisp_Object Vwith_echo_area_save_vector;
473 /* Non-zero means display_echo_area should display the last echo area
474 message again. Set by redisplay_preserve_echo_area. */
476 static int display_last_displayed_message_p;
478 /* Nonzero if echo area is being used by print; zero if being used by
479 message. */
481 int message_buf_print;
483 /* Maximum height for resizing mini-windows. Either a float
484 specifying a fraction of the available height, or an integer
485 specifying a number of lines. */
487 Lisp_Object Vmax_mini_window_height;
489 /* Non-zero means messages should be displayed with truncated
490 lines instead of being continued. */
492 int message_truncate_lines;
493 Lisp_Object Qmessage_truncate_lines;
495 /* Non-zero means we want a hollow cursor in windows that are not
496 selected. Zero means there's no cursor in such windows. */
498 int cursor_in_non_selected_windows;
500 /* A scratch glyph row with contents used for generating truncation
501 glyphs. Also used in direct_output_for_insert. */
503 #define MAX_SCRATCH_GLYPHS 100
504 struct glyph_row scratch_glyph_row;
505 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
507 /* Ascent and height of the last line processed by move_it_to. */
509 static int last_max_ascent, last_height;
511 /* Non-zero if there's a help-echo in the echo area. */
513 int help_echo_showing_p;
515 /* The maximum distance to look ahead for text properties. Values
516 that are too small let us call compute_char_face and similar
517 functions too often which is expensive. Values that are too large
518 let us call compute_char_face and alike too often because we
519 might not be interested in text properties that far away. */
521 #define TEXT_PROP_DISTANCE_LIMIT 100
523 #if GLYPH_DEBUG
525 /* Non-zero means print traces of redisplay if compiled with
526 GLYPH_DEBUG != 0. */
528 int trace_redisplay_p;
530 #endif /* GLYPH_DEBUG */
532 #ifdef DEBUG_TRACE_MOVE
533 /* Non-zero means trace with TRACE_MOVE to stderr. */
534 int trace_move;
536 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
537 #else
538 #define TRACE_MOVE(x) (void) 0
539 #endif
541 /* Non-zero means automatically scroll windows horizontally to make
542 point visible. */
544 int automatic_hscrolling_p;
546 /* A list of symbols, one for each supported image type. */
548 Lisp_Object Vimage_types;
550 /* Value returned from text property handlers (see below). */
552 enum prop_handled
554 HANDLED_NORMALLY,
555 HANDLED_RECOMPUTE_PROPS,
556 HANDLED_OVERLAY_STRING_CONSUMED,
557 HANDLED_RETURN
560 /* A description of text properties that redisplay is interested
561 in. */
563 struct props
565 /* The name of the property. */
566 Lisp_Object *name;
568 /* A unique index for the property. */
569 enum prop_idx idx;
571 /* A handler function called to set up iterator IT from the property
572 at IT's current position. Value is used to steer handle_stop. */
573 enum prop_handled (*handler) P_ ((struct it *it));
576 static enum prop_handled handle_face_prop P_ ((struct it *));
577 static enum prop_handled handle_invisible_prop P_ ((struct it *));
578 static enum prop_handled handle_display_prop P_ ((struct it *));
579 static enum prop_handled handle_composition_prop P_ ((struct it *));
580 static enum prop_handled handle_overlay_change P_ ((struct it *));
581 static enum prop_handled handle_fontified_prop P_ ((struct it *));
583 /* Properties handled by iterators. */
585 static struct props it_props[] =
587 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
588 /* Handle `face' before `display' because some sub-properties of
589 `display' need to know the face. */
590 {&Qface, FACE_PROP_IDX, handle_face_prop},
591 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
592 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
593 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
594 {NULL, 0, NULL}
597 /* Value is the position described by X. If X is a marker, value is
598 the marker_position of X. Otherwise, value is X. */
600 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
602 /* Enumeration returned by some move_it_.* functions internally. */
604 enum move_it_result
606 /* Not used. Undefined value. */
607 MOVE_UNDEFINED,
609 /* Move ended at the requested buffer position or ZV. */
610 MOVE_POS_MATCH_OR_ZV,
612 /* Move ended at the requested X pixel position. */
613 MOVE_X_REACHED,
615 /* Move within a line ended at the end of a line that must be
616 continued. */
617 MOVE_LINE_CONTINUED,
619 /* Move within a line ended at the end of a line that would
620 be displayed truncated. */
621 MOVE_LINE_TRUNCATED,
623 /* Move within a line ended at a line end. */
624 MOVE_NEWLINE_OR_CR
629 /* Function prototypes. */
631 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
632 static int redisplay_mode_lines P_ ((Lisp_Object, int));
633 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
634 static int invisible_text_between_p P_ ((struct it *, int, int));
635 static int next_element_from_ellipsis P_ ((struct it *));
636 static void pint2str P_ ((char *, int, int));
637 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
638 struct text_pos));
639 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
640 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
641 static void store_frame_title_char P_ ((char));
642 static int store_frame_title P_ ((unsigned char *, int, int));
643 static void x_consider_frame_title P_ ((Lisp_Object));
644 static void handle_stop P_ ((struct it *));
645 static int tool_bar_lines_needed P_ ((struct frame *));
646 static int single_display_prop_intangible_p P_ ((Lisp_Object));
647 static void ensure_echo_area_buffers P_ ((void));
648 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
649 struct glyph_row *,
650 struct glyph_row *));
651 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
652 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
653 static int with_echo_area_buffer P_ ((struct window *, int,
654 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
655 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
656 static void clear_garbaged_frames P_ ((void));
657 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
658 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
659 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
660 static int display_echo_area P_ ((struct window *));
661 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
662 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
663 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
664 static int string_char_and_length P_ ((unsigned char *, int, int *));
665 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
666 struct text_pos));
667 static int compute_window_start_on_continuation_line P_ ((struct window *));
668 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
669 static void insert_left_trunc_glyphs P_ ((struct it *));
670 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
671 static void extend_face_to_end_of_line P_ ((struct it *));
672 static int append_space P_ ((struct it *, int));
673 static void make_cursor_line_fully_visible P_ ((struct window *));
674 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
675 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
676 static int trailing_whitespace_p P_ ((int));
677 static int message_log_check_duplicate P_ ((int, int, int, int));
678 int invisible_p P_ ((Lisp_Object, Lisp_Object));
679 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
680 static void push_it P_ ((struct it *));
681 static void pop_it P_ ((struct it *));
682 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
683 static void redisplay_internal P_ ((int));
684 static int echo_area_display P_ ((int));
685 static void redisplay_windows P_ ((Lisp_Object));
686 static void redisplay_window P_ ((Lisp_Object, int));
687 static void update_menu_bar P_ ((struct frame *, int));
688 static int try_window_reusing_current_matrix P_ ((struct window *));
689 static int try_window_id P_ ((struct window *));
690 static int display_line P_ ((struct it *));
691 static int display_mode_lines P_ ((struct window *));
692 static void display_mode_line P_ ((struct window *, enum face_id,
693 Lisp_Object));
694 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
695 static char *decode_mode_spec P_ ((struct window *, int, int, int));
696 static void display_menu_bar P_ ((struct window *));
697 static int display_count_lines P_ ((int, int, int, int, int *));
698 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
699 int, int, struct it *, int, int, int, int));
700 static void compute_line_metrics P_ ((struct it *));
701 static void run_redisplay_end_trigger_hook P_ ((struct it *));
702 static int get_overlay_strings P_ ((struct it *));
703 static void next_overlay_string P_ ((struct it *));
704 static void reseat P_ ((struct it *, struct text_pos, int));
705 static void reseat_1 P_ ((struct it *, struct text_pos, int));
706 static void back_to_previous_visible_line_start P_ ((struct it *));
707 static void reseat_at_previous_visible_line_start P_ ((struct it *));
708 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
709 static int next_element_from_display_vector P_ ((struct it *));
710 static int next_element_from_string P_ ((struct it *));
711 static int next_element_from_c_string P_ ((struct it *));
712 static int next_element_from_buffer P_ ((struct it *));
713 static int next_element_from_composition P_ ((struct it *));
714 static int next_element_from_image P_ ((struct it *));
715 static int next_element_from_stretch P_ ((struct it *));
716 static void load_overlay_strings P_ ((struct it *));
717 static void init_from_display_pos P_ ((struct it *, struct window *,
718 struct display_pos *));
719 static void reseat_to_string P_ ((struct it *, unsigned char *,
720 Lisp_Object, int, int, int, int));
721 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
722 int, int, int));
723 void move_it_vertically_backward P_ ((struct it *, int));
724 static void init_to_row_start P_ ((struct it *, struct window *,
725 struct glyph_row *));
726 static void init_to_row_end P_ ((struct it *, struct window *,
727 struct glyph_row *));
728 static void back_to_previous_line_start P_ ((struct it *));
729 static int forward_to_next_line_start P_ ((struct it *, int *));
730 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
731 Lisp_Object, int));
732 static struct text_pos string_pos P_ ((int, Lisp_Object));
733 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
734 static int number_of_chars P_ ((unsigned char *, int));
735 static void compute_stop_pos P_ ((struct it *));
736 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
737 Lisp_Object));
738 static int face_before_or_after_it_pos P_ ((struct it *, int));
739 static int next_overlay_change P_ ((int));
740 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
741 Lisp_Object, struct text_pos *));
743 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
744 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
746 #ifdef HAVE_WINDOW_SYSTEM
748 static void update_tool_bar P_ ((struct frame *, int));
749 static void build_desired_tool_bar_string P_ ((struct frame *f));
750 static int redisplay_tool_bar P_ ((struct frame *));
751 static void display_tool_bar_line P_ ((struct it *));
753 #endif /* HAVE_WINDOW_SYSTEM */
756 /***********************************************************************
757 Window display dimensions
758 ***********************************************************************/
760 /* Return the window-relative maximum y + 1 for glyph rows displaying
761 text in window W. This is the height of W minus the height of a
762 mode line, if any. */
764 INLINE int
765 window_text_bottom_y (w)
766 struct window *w;
768 struct frame *f = XFRAME (w->frame);
769 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
771 if (WINDOW_WANTS_MODELINE_P (w))
772 height -= CURRENT_MODE_LINE_HEIGHT (w);
773 return height;
777 /* Return the pixel width of display area AREA of window W. AREA < 0
778 means return the total width of W, not including bitmap areas to
779 the left and right of the window. */
781 INLINE int
782 window_box_width (w, area)
783 struct window *w;
784 int area;
786 struct frame *f = XFRAME (w->frame);
787 int width = XFASTINT (w->width);
789 if (!w->pseudo_window_p)
791 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
793 if (area == TEXT_AREA)
795 if (INTEGERP (w->left_margin_width))
796 width -= XFASTINT (w->left_margin_width);
797 if (INTEGERP (w->right_margin_width))
798 width -= XFASTINT (w->right_margin_width);
800 else if (area == LEFT_MARGIN_AREA)
801 width = (INTEGERP (w->left_margin_width)
802 ? XFASTINT (w->left_margin_width) : 0);
803 else if (area == RIGHT_MARGIN_AREA)
804 width = (INTEGERP (w->right_margin_width)
805 ? XFASTINT (w->right_margin_width) : 0);
808 return width * CANON_X_UNIT (f);
812 /* Return the pixel height of the display area of window W, not
813 including mode lines of W, if any.. */
815 INLINE int
816 window_box_height (w)
817 struct window *w;
819 struct frame *f = XFRAME (w->frame);
820 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
822 xassert (height >= 0);
824 if (WINDOW_WANTS_MODELINE_P (w))
825 height -= CURRENT_MODE_LINE_HEIGHT (w);
827 if (WINDOW_WANTS_HEADER_LINE_P (w))
828 height -= CURRENT_HEADER_LINE_HEIGHT (w);
830 return height;
834 /* Return the frame-relative coordinate of the left edge of display
835 area AREA of window W. AREA < 0 means return the left edge of the
836 whole window, to the right of any bitmap area at the left side of
837 W. */
839 INLINE int
840 window_box_left (w, area)
841 struct window *w;
842 int area;
844 struct frame *f = XFRAME (w->frame);
845 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
847 if (!w->pseudo_window_p)
849 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
850 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
852 if (area == TEXT_AREA)
853 x += window_box_width (w, LEFT_MARGIN_AREA);
854 else if (area == RIGHT_MARGIN_AREA)
855 x += (window_box_width (w, LEFT_MARGIN_AREA)
856 + window_box_width (w, TEXT_AREA));
859 return x;
863 /* Return the frame-relative coordinate of the right edge of display
864 area AREA of window W. AREA < 0 means return the left edge of the
865 whole window, to the left of any bitmap area at the right side of
866 W. */
868 INLINE int
869 window_box_right (w, area)
870 struct window *w;
871 int area;
873 return window_box_left (w, area) + window_box_width (w, area);
877 /* Get the bounding box of the display area AREA of window W, without
878 mode lines, in frame-relative coordinates. AREA < 0 means the
879 whole window, not including bitmap areas to the left and right of
880 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
881 coordinates of the upper-left corner of the box. Return in
882 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
884 INLINE void
885 window_box (w, area, box_x, box_y, box_width, box_height)
886 struct window *w;
887 int area;
888 int *box_x, *box_y, *box_width, *box_height;
890 struct frame *f = XFRAME (w->frame);
892 *box_width = window_box_width (w, area);
893 *box_height = window_box_height (w);
894 *box_x = window_box_left (w, area);
895 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
896 + XFASTINT (w->top) * CANON_Y_UNIT (f));
897 if (WINDOW_WANTS_HEADER_LINE_P (w))
898 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
902 /* Get the bounding box of the display area AREA of window W, without
903 mode lines. AREA < 0 means the whole window, not including bitmap
904 areas to the left and right of the window. Return in *TOP_LEFT_X
905 and TOP_LEFT_Y the frame-relative pixel coordinates of the
906 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
907 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
908 box. */
910 INLINE void
911 window_box_edges (w, area, top_left_x, top_left_y,
912 bottom_right_x, bottom_right_y)
913 struct window *w;
914 int area;
915 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
917 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
918 bottom_right_y);
919 *bottom_right_x += *top_left_x;
920 *bottom_right_y += *top_left_y;
925 /***********************************************************************
926 Utilities
927 ***********************************************************************/
929 /* Return the next character from STR which is MAXLEN bytes long.
930 Return in *LEN the length of the character. This is like
931 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
932 we find one, we return a `?', but with the length of the invalid
933 character. */
935 static INLINE int
936 string_char_and_length (str, maxlen, len)
937 unsigned char *str;
938 int maxlen, *len;
940 int c;
942 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
943 if (!CHAR_VALID_P (c, 1))
944 /* We may not change the length here because other places in Emacs
945 don't use this function, i.e. they silently accept invalid
946 characters. */
947 c = '?';
949 return c;
954 /* Given a position POS containing a valid character and byte position
955 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
957 static struct text_pos
958 string_pos_nchars_ahead (pos, string, nchars)
959 struct text_pos pos;
960 Lisp_Object string;
961 int nchars;
963 xassert (STRINGP (string) && nchars >= 0);
965 if (STRING_MULTIBYTE (string))
967 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
968 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
969 int len;
971 while (nchars--)
973 string_char_and_length (p, rest, &len);
974 p += len, rest -= len;
975 xassert (rest >= 0);
976 CHARPOS (pos) += 1;
977 BYTEPOS (pos) += len;
980 else
981 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
983 return pos;
987 /* Value is the text position, i.e. character and byte position,
988 for character position CHARPOS in STRING. */
990 static INLINE struct text_pos
991 string_pos (charpos, string)
992 int charpos;
993 Lisp_Object string;
995 struct text_pos pos;
996 xassert (STRINGP (string));
997 xassert (charpos >= 0);
998 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
999 return pos;
1003 /* Value is a text position, i.e. character and byte position, for
1004 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1005 means recognize multibyte characters. */
1007 static struct text_pos
1008 c_string_pos (charpos, s, multibyte_p)
1009 int charpos;
1010 unsigned char *s;
1011 int multibyte_p;
1013 struct text_pos pos;
1015 xassert (s != NULL);
1016 xassert (charpos >= 0);
1018 if (multibyte_p)
1020 int rest = strlen (s), len;
1022 SET_TEXT_POS (pos, 0, 0);
1023 while (charpos--)
1025 string_char_and_length (s, rest, &len);
1026 s += len, rest -= len;
1027 xassert (rest >= 0);
1028 CHARPOS (pos) += 1;
1029 BYTEPOS (pos) += len;
1032 else
1033 SET_TEXT_POS (pos, charpos, charpos);
1035 return pos;
1039 /* Value is the number of characters in C string S. MULTIBYTE_P
1040 non-zero means recognize multibyte characters. */
1042 static int
1043 number_of_chars (s, multibyte_p)
1044 unsigned char *s;
1045 int multibyte_p;
1047 int nchars;
1049 if (multibyte_p)
1051 int rest = strlen (s), len;
1052 unsigned char *p = (unsigned char *) s;
1054 for (nchars = 0; rest > 0; ++nchars)
1056 string_char_and_length (p, rest, &len);
1057 rest -= len, p += len;
1060 else
1061 nchars = strlen (s);
1063 return nchars;
1067 /* Compute byte position NEWPOS->bytepos corresponding to
1068 NEWPOS->charpos. POS is a known position in string STRING.
1069 NEWPOS->charpos must be >= POS.charpos. */
1071 static void
1072 compute_string_pos (newpos, pos, string)
1073 struct text_pos *newpos, pos;
1074 Lisp_Object string;
1076 xassert (STRINGP (string));
1077 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1079 if (STRING_MULTIBYTE (string))
1080 *newpos = string_pos_nchars_ahead (pos, string,
1081 CHARPOS (*newpos) - CHARPOS (pos));
1082 else
1083 BYTEPOS (*newpos) = CHARPOS (*newpos);
1088 /***********************************************************************
1089 Lisp form evaluation
1090 ***********************************************************************/
1092 /* Error handler for safe_eval and safe_call. */
1094 static Lisp_Object
1095 safe_eval_handler (arg)
1096 Lisp_Object arg;
1098 return Qnil;
1102 /* Evaluate SEXPR and return the result, or nil if something went
1103 wrong. */
1105 Lisp_Object
1106 safe_eval (sexpr)
1107 Lisp_Object sexpr;
1109 int count = specpdl_ptr - specpdl;
1110 struct gcpro gcpro1;
1111 Lisp_Object val;
1113 GCPRO1 (sexpr);
1114 specbind (Qinhibit_redisplay, Qt);
1115 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1116 UNGCPRO;
1117 return unbind_to (count, val);
1121 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1122 Return the result, or nil if something went wrong. */
1124 Lisp_Object
1125 safe_call (nargs, args)
1126 int nargs;
1127 Lisp_Object *args;
1129 int count = specpdl_ptr - specpdl;
1130 Lisp_Object val;
1131 struct gcpro gcpro1;
1133 GCPRO1 (args[0]);
1134 gcpro1.nvars = nargs;
1135 specbind (Qinhibit_redisplay, Qt);
1136 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1137 safe_eval_handler);
1138 UNGCPRO;
1139 return unbind_to (count, val);
1143 /* Call function FN with one argument ARG.
1144 Return the result, or nil if something went wrong. */
1146 Lisp_Object
1147 safe_call1 (fn, arg)
1148 Lisp_Object fn, arg;
1150 Lisp_Object args[2];
1151 args[0] = fn;
1152 args[1] = arg;
1153 return safe_call (2, args);
1158 /***********************************************************************
1159 Debugging
1160 ***********************************************************************/
1162 #if 0
1164 /* Define CHECK_IT to perform sanity checks on iterators.
1165 This is for debugging. It is too slow to do unconditionally. */
1167 static void
1168 check_it (it)
1169 struct it *it;
1171 if (it->method == next_element_from_string)
1173 xassert (STRINGP (it->string));
1174 xassert (IT_STRING_CHARPOS (*it) >= 0);
1176 else if (it->method == next_element_from_buffer)
1178 /* Check that character and byte positions agree. */
1179 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1182 if (it->dpvec)
1183 xassert (it->current.dpvec_index >= 0);
1184 else
1185 xassert (it->current.dpvec_index < 0);
1188 #define CHECK_IT(IT) check_it ((IT))
1190 #else /* not 0 */
1192 #define CHECK_IT(IT) (void) 0
1194 #endif /* not 0 */
1197 #if GLYPH_DEBUG
1199 /* Check that the window end of window W is what we expect it
1200 to be---the last row in the current matrix displaying text. */
1202 static void
1203 check_window_end (w)
1204 struct window *w;
1206 if (!MINI_WINDOW_P (w)
1207 && !NILP (w->window_end_valid))
1209 struct glyph_row *row;
1210 xassert ((row = MATRIX_ROW (w->current_matrix,
1211 XFASTINT (w->window_end_vpos)),
1212 !row->enabled_p
1213 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1214 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1218 #define CHECK_WINDOW_END(W) check_window_end ((W))
1220 #else /* not GLYPH_DEBUG */
1222 #define CHECK_WINDOW_END(W) (void) 0
1224 #endif /* not GLYPH_DEBUG */
1228 /***********************************************************************
1229 Iterator initialization
1230 ***********************************************************************/
1232 /* Initialize IT for displaying current_buffer in window W, starting
1233 at character position CHARPOS. CHARPOS < 0 means that no buffer
1234 position is specified which is useful when the iterator is assigned
1235 a position later. BYTEPOS is the byte position corresponding to
1236 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1238 If ROW is not null, calls to produce_glyphs with IT as parameter
1239 will produce glyphs in that row.
1241 BASE_FACE_ID is the id of a base face to use. It must be one of
1242 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1243 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1244 displaying the tool-bar.
1246 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1247 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1248 corresponding mode line glyph row of the desired matrix of W. */
1250 void
1251 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1252 struct it *it;
1253 struct window *w;
1254 int charpos, bytepos;
1255 struct glyph_row *row;
1256 enum face_id base_face_id;
1258 int highlight_region_p;
1260 /* Some precondition checks. */
1261 xassert (w != NULL && it != NULL);
1262 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1264 /* If face attributes have been changed since the last redisplay,
1265 free realized faces now because they depend on face definitions
1266 that might have changed. */
1267 if (face_change_count)
1269 face_change_count = 0;
1270 free_all_realized_faces (Qnil);
1273 /* Use one of the mode line rows of W's desired matrix if
1274 appropriate. */
1275 if (row == NULL)
1277 if (base_face_id == MODE_LINE_FACE_ID)
1278 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1279 else if (base_face_id == HEADER_LINE_FACE_ID)
1280 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1283 /* Clear IT. */
1284 bzero (it, sizeof *it);
1285 it->current.overlay_string_index = -1;
1286 it->current.dpvec_index = -1;
1287 it->base_face_id = base_face_id;
1289 /* The window in which we iterate over current_buffer: */
1290 XSETWINDOW (it->window, w);
1291 it->w = w;
1292 it->f = XFRAME (w->frame);
1294 /* Extra space between lines (on window systems only). */
1295 if (base_face_id == DEFAULT_FACE_ID
1296 && FRAME_WINDOW_P (it->f))
1298 if (NATNUMP (current_buffer->extra_line_spacing))
1299 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1300 else if (it->f->extra_line_spacing > 0)
1301 it->extra_line_spacing = it->f->extra_line_spacing;
1304 /* If realized faces have been removed, e.g. because of face
1305 attribute changes of named faces, recompute them. */
1306 if (FRAME_FACE_CACHE (it->f)->used == 0)
1307 recompute_basic_faces (it->f);
1309 /* Current value of the `space-width', and 'height' properties. */
1310 it->space_width = Qnil;
1311 it->font_height = Qnil;
1313 /* Are control characters displayed as `^C'? */
1314 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1316 /* -1 means everything between a CR and the following line end
1317 is invisible. >0 means lines indented more than this value are
1318 invisible. */
1319 it->selective = (INTEGERP (current_buffer->selective_display)
1320 ? XFASTINT (current_buffer->selective_display)
1321 : (!NILP (current_buffer->selective_display)
1322 ? -1 : 0));
1323 it->selective_display_ellipsis_p
1324 = !NILP (current_buffer->selective_display_ellipses);
1326 /* Display table to use. */
1327 it->dp = window_display_table (w);
1329 /* Are multibyte characters enabled in current_buffer? */
1330 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1332 /* Non-zero if we should highlight the region. */
1333 highlight_region_p
1334 = (!NILP (Vtransient_mark_mode)
1335 && !NILP (current_buffer->mark_active)
1336 && XMARKER (current_buffer->mark)->buffer != 0);
1338 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1339 start and end of a visible region in window IT->w. Set both to
1340 -1 to indicate no region. */
1341 if (highlight_region_p
1342 /* Maybe highlight only in selected window. */
1343 && (/* Either show region everywhere. */
1344 highlight_nonselected_windows
1345 /* Or show region in the selected window. */
1346 || w == XWINDOW (selected_window)
1347 /* Or show the region if we are in the mini-buffer and W is
1348 the window the mini-buffer refers to. */
1349 || (MINI_WINDOW_P (XWINDOW (selected_window))
1350 && w == XWINDOW (Vminibuf_scroll_window))))
1352 int charpos = marker_position (current_buffer->mark);
1353 it->region_beg_charpos = min (PT, charpos);
1354 it->region_end_charpos = max (PT, charpos);
1356 else
1357 it->region_beg_charpos = it->region_end_charpos = -1;
1359 /* Get the position at which the redisplay_end_trigger hook should
1360 be run, if it is to be run at all. */
1361 if (MARKERP (w->redisplay_end_trigger)
1362 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1363 it->redisplay_end_trigger_charpos
1364 = marker_position (w->redisplay_end_trigger);
1365 else if (INTEGERP (w->redisplay_end_trigger))
1366 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1368 /* Correct bogus values of tab_width. */
1369 it->tab_width = XINT (current_buffer->tab_width);
1370 if (it->tab_width <= 0 || it->tab_width > 1000)
1371 it->tab_width = 8;
1373 /* Are lines in the display truncated? */
1374 it->truncate_lines_p
1375 = (base_face_id != DEFAULT_FACE_ID
1376 || XINT (it->w->hscroll)
1377 || (truncate_partial_width_windows
1378 && !WINDOW_FULL_WIDTH_P (it->w))
1379 || !NILP (current_buffer->truncate_lines));
1381 /* Get dimensions of truncation and continuation glyphs. These are
1382 displayed as bitmaps under X, so we don't need them for such
1383 frames. */
1384 if (!FRAME_WINDOW_P (it->f))
1386 if (it->truncate_lines_p)
1388 /* We will need the truncation glyph. */
1389 xassert (it->glyph_row == NULL);
1390 produce_special_glyphs (it, IT_TRUNCATION);
1391 it->truncation_pixel_width = it->pixel_width;
1393 else
1395 /* We will need the continuation glyph. */
1396 xassert (it->glyph_row == NULL);
1397 produce_special_glyphs (it, IT_CONTINUATION);
1398 it->continuation_pixel_width = it->pixel_width;
1401 /* Reset these values to zero becaue the produce_special_glyphs
1402 above has changed them. */
1403 it->pixel_width = it->ascent = it->descent = 0;
1404 it->phys_ascent = it->phys_descent = 0;
1407 /* Set this after getting the dimensions of truncation and
1408 continuation glyphs, so that we don't produce glyphs when calling
1409 produce_special_glyphs, above. */
1410 it->glyph_row = row;
1411 it->area = TEXT_AREA;
1413 /* Get the dimensions of the display area. The display area
1414 consists of the visible window area plus a horizontally scrolled
1415 part to the left of the window. All x-values are relative to the
1416 start of this total display area. */
1417 if (base_face_id != DEFAULT_FACE_ID)
1419 /* Mode lines, menu bar in terminal frames. */
1420 it->first_visible_x = 0;
1421 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1423 else
1425 it->first_visible_x
1426 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1427 it->last_visible_x = (it->first_visible_x
1428 + window_box_width (w, TEXT_AREA));
1430 /* If we truncate lines, leave room for the truncator glyph(s) at
1431 the right margin. Otherwise, leave room for the continuation
1432 glyph(s). Truncation and continuation glyphs are not inserted
1433 for window-based redisplay. */
1434 if (!FRAME_WINDOW_P (it->f))
1436 if (it->truncate_lines_p)
1437 it->last_visible_x -= it->truncation_pixel_width;
1438 else
1439 it->last_visible_x -= it->continuation_pixel_width;
1442 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1443 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1446 /* Leave room for a border glyph. */
1447 if (!FRAME_WINDOW_P (it->f)
1448 && !WINDOW_RIGHTMOST_P (it->w))
1449 it->last_visible_x -= 1;
1451 it->last_visible_y = window_text_bottom_y (w);
1453 /* For mode lines and alike, arrange for the first glyph having a
1454 left box line if the face specifies a box. */
1455 if (base_face_id != DEFAULT_FACE_ID)
1457 struct face *face;
1459 it->face_id = base_face_id;
1461 /* If we have a boxed mode line, make the first character appear
1462 with a left box line. */
1463 face = FACE_FROM_ID (it->f, base_face_id);
1464 if (face->box != FACE_NO_BOX)
1465 it->start_of_box_run_p = 1;
1468 /* If a buffer position was specified, set the iterator there,
1469 getting overlays and face properties from that position. */
1470 if (charpos > 0)
1472 it->end_charpos = ZV;
1473 it->face_id = -1;
1474 IT_CHARPOS (*it) = charpos;
1476 /* Compute byte position if not specified. */
1477 if (bytepos <= 0)
1478 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1479 else
1480 IT_BYTEPOS (*it) = bytepos;
1482 /* Compute faces etc. */
1483 reseat (it, it->current.pos, 1);
1486 CHECK_IT (it);
1490 /* Initialize IT for the display of window W with window start POS. */
1492 void
1493 start_display (it, w, pos)
1494 struct it *it;
1495 struct window *w;
1496 struct text_pos pos;
1498 int start_at_line_beg_p;
1499 struct glyph_row *row;
1500 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1501 int first_y;
1503 row = w->desired_matrix->rows + first_vpos;
1504 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1505 first_y = it->current_y;
1507 /* If window start is not at a line start, move back to the line
1508 start. This makes sure that we take continuation lines into
1509 account. */
1510 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1511 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1512 if (!start_at_line_beg_p)
1513 reseat_at_previous_visible_line_start (it);
1515 /* If window start is not at a line start, skip forward to POS to
1516 get the correct continuation_lines_width and current_x. */
1517 if (!start_at_line_beg_p)
1519 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1521 /* If lines are continued, this line may end in the middle of a
1522 multi-glyph character (e.g. a control character displayed as
1523 \003, or in the middle of an overlay string). In this case
1524 move_it_to above will not have taken us to the start of
1525 the continuation line but to the end of the continued line. */
1526 if (!it->truncate_lines_p)
1528 if (it->current_x > 0)
1530 if (it->current.dpvec_index >= 0
1531 || it->current.overlay_string_index >= 0)
1533 set_iterator_to_next (it, 1);
1534 move_it_in_display_line_to (it, -1, -1, 0);
1537 it->continuation_lines_width += it->current_x;
1540 /* We're starting a new display line, not affected by the
1541 height of the continued line, so clear the appropriate
1542 fields in the iterator structure. */
1543 it->max_ascent = it->max_descent = 0;
1544 it->max_phys_ascent = it->max_phys_descent = 0;
1547 it->current_y = first_y;
1548 it->vpos = 0;
1549 it->current_x = it->hpos = 0;
1552 #if 0 /* Don't assert the following because start_display is sometimes
1553 called intentionally with a window start that is not at a
1554 line start. Please leave this code in as a comment. */
1556 /* Window start should be on a line start, now. */
1557 xassert (it->continuation_lines_width
1558 || IT_CHARPOS (it) == BEGV
1559 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1560 #endif /* 0 */
1564 /* Initialize IT for stepping through current_buffer in window W,
1565 starting at position POS that includes overlay string and display
1566 vector/ control character translation position information. */
1568 static void
1569 init_from_display_pos (it, w, pos)
1570 struct it *it;
1571 struct window *w;
1572 struct display_pos *pos;
1574 /* Keep in mind: the call to reseat in init_iterator skips invisible
1575 text, so we might end up at a position different from POS. This
1576 is only a problem when POS is a row start after a newline and an
1577 overlay starts there with an after-string, and the overlay has an
1578 invisible property. Since we don't skip invisible text in
1579 display_line and elsewhere immediately after consuming the
1580 newline before the row start, such a POS will not be in a string,
1581 but the call to init_iterator below will move us to the
1582 after-string. */
1583 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1584 NULL, DEFAULT_FACE_ID);
1586 /* If position is within an overlay string, set up IT to
1587 the right overlay string. */
1588 if (pos->overlay_string_index >= 0)
1590 int relative_index;
1592 /* We already have the first chunk of overlay strings in
1593 IT->overlay_strings. Load more until the one for
1594 pos->overlay_string_index is in IT->overlay_strings. */
1595 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1597 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1598 it->current.overlay_string_index = 0;
1599 while (n--)
1601 load_overlay_strings (it);
1602 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1606 it->current.overlay_string_index = pos->overlay_string_index;
1607 relative_index = (it->current.overlay_string_index
1608 % OVERLAY_STRING_CHUNK_SIZE);
1609 it->string = it->overlay_strings[relative_index];
1610 xassert (STRINGP (it->string));
1611 it->current.string_pos = pos->string_pos;
1612 it->method = next_element_from_string;
1614 else if (CHARPOS (pos->string_pos) >= 0)
1616 /* Recorded position is not in an overlay string, but in another
1617 string. This can only be a string from a `display' property.
1618 IT should already be filled with that string. */
1619 it->current.string_pos = pos->string_pos;
1620 xassert (STRINGP (it->string));
1623 /* Restore position in display vector translations or control
1624 character translations. */
1625 if (pos->dpvec_index >= 0)
1627 /* This fills IT->dpvec. */
1628 get_next_display_element (it);
1629 xassert (it->dpvec && it->current.dpvec_index == 0);
1630 it->current.dpvec_index = pos->dpvec_index;
1633 CHECK_IT (it);
1637 /* Initialize IT for stepping through current_buffer in window W
1638 starting at ROW->start. */
1640 static void
1641 init_to_row_start (it, w, row)
1642 struct it *it;
1643 struct window *w;
1644 struct glyph_row *row;
1646 init_from_display_pos (it, w, &row->start);
1647 it->continuation_lines_width = row->continuation_lines_width;
1648 CHECK_IT (it);
1652 /* Initialize IT for stepping through current_buffer in window W
1653 starting in the line following ROW, i.e. starting at ROW->end. */
1655 static void
1656 init_to_row_end (it, w, row)
1657 struct it *it;
1658 struct window *w;
1659 struct glyph_row *row;
1661 init_from_display_pos (it, w, &row->end);
1663 if (row->continued_p)
1664 it->continuation_lines_width = (row->continuation_lines_width
1665 + row->pixel_width);
1666 CHECK_IT (it);
1672 /***********************************************************************
1673 Text properties
1674 ***********************************************************************/
1676 /* Called when IT reaches IT->stop_charpos. Handle text property and
1677 overlay changes. Set IT->stop_charpos to the next position where
1678 to stop. */
1680 static void
1681 handle_stop (it)
1682 struct it *it;
1684 enum prop_handled handled;
1685 int handle_overlay_change_p = 1;
1686 struct props *p;
1688 it->dpvec = NULL;
1689 it->current.dpvec_index = -1;
1693 handled = HANDLED_NORMALLY;
1695 /* Call text property handlers. */
1696 for (p = it_props; p->handler; ++p)
1698 handled = p->handler (it);
1700 if (handled == HANDLED_RECOMPUTE_PROPS)
1701 break;
1702 else if (handled == HANDLED_RETURN)
1703 return;
1704 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1705 handle_overlay_change_p = 0;
1708 if (handled != HANDLED_RECOMPUTE_PROPS)
1710 /* Don't check for overlay strings below when set to deliver
1711 characters from a display vector. */
1712 if (it->method == next_element_from_display_vector)
1713 handle_overlay_change_p = 0;
1715 /* Handle overlay changes. */
1716 if (handle_overlay_change_p)
1717 handled = handle_overlay_change (it);
1719 /* Determine where to stop next. */
1720 if (handled == HANDLED_NORMALLY)
1721 compute_stop_pos (it);
1724 while (handled == HANDLED_RECOMPUTE_PROPS);
1728 /* Compute IT->stop_charpos from text property and overlay change
1729 information for IT's current position. */
1731 static void
1732 compute_stop_pos (it)
1733 struct it *it;
1735 register INTERVAL iv, next_iv;
1736 Lisp_Object object, limit, position;
1738 /* If nowhere else, stop at the end. */
1739 it->stop_charpos = it->end_charpos;
1741 if (STRINGP (it->string))
1743 /* Strings are usually short, so don't limit the search for
1744 properties. */
1745 object = it->string;
1746 limit = Qnil;
1747 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1749 else
1751 int charpos;
1753 /* If next overlay change is in front of the current stop pos
1754 (which is IT->end_charpos), stop there. Note: value of
1755 next_overlay_change is point-max if no overlay change
1756 follows. */
1757 charpos = next_overlay_change (IT_CHARPOS (*it));
1758 if (charpos < it->stop_charpos)
1759 it->stop_charpos = charpos;
1761 /* If showing the region, we have to stop at the region
1762 start or end because the face might change there. */
1763 if (it->region_beg_charpos > 0)
1765 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1766 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1767 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1768 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1771 /* Set up variables for computing the stop position from text
1772 property changes. */
1773 XSETBUFFER (object, current_buffer);
1774 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1775 XSETFASTINT (position, IT_CHARPOS (*it));
1779 /* Get the interval containing IT's position. Value is a null
1780 interval if there isn't such an interval. */
1781 iv = validate_interval_range (object, &position, &position, 0);
1782 if (!NULL_INTERVAL_P (iv))
1784 Lisp_Object values_here[LAST_PROP_IDX];
1785 struct props *p;
1787 /* Get properties here. */
1788 for (p = it_props; p->handler; ++p)
1789 values_here[p->idx] = textget (iv->plist, *p->name);
1791 /* Look for an interval following iv that has different
1792 properties. */
1793 for (next_iv = next_interval (iv);
1794 (!NULL_INTERVAL_P (next_iv)
1795 && (NILP (limit)
1796 || XFASTINT (limit) > next_iv->position));
1797 next_iv = next_interval (next_iv))
1799 for (p = it_props; p->handler; ++p)
1801 Lisp_Object new_value;
1803 new_value = textget (next_iv->plist, *p->name);
1804 if (!EQ (values_here[p->idx], new_value))
1805 break;
1808 if (p->handler)
1809 break;
1812 if (!NULL_INTERVAL_P (next_iv))
1814 if (INTEGERP (limit)
1815 && next_iv->position >= XFASTINT (limit))
1816 /* No text property change up to limit. */
1817 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1818 else
1819 /* Text properties change in next_iv. */
1820 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1824 xassert (STRINGP (it->string)
1825 || (it->stop_charpos >= BEGV
1826 && it->stop_charpos >= IT_CHARPOS (*it)));
1830 /* Return the position of the next overlay change after POS in
1831 current_buffer. Value is point-max if no overlay change
1832 follows. This is like `next-overlay-change' but doesn't use
1833 xmalloc. */
1835 static int
1836 next_overlay_change (pos)
1837 int pos;
1839 int noverlays;
1840 int endpos;
1841 Lisp_Object *overlays;
1842 int len;
1843 int i;
1845 /* Get all overlays at the given position. */
1846 len = 10;
1847 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1848 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1849 if (noverlays > len)
1851 len = noverlays;
1852 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1853 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1856 /* If any of these overlays ends before endpos,
1857 use its ending point instead. */
1858 for (i = 0; i < noverlays; ++i)
1860 Lisp_Object oend;
1861 int oendpos;
1863 oend = OVERLAY_END (overlays[i]);
1864 oendpos = OVERLAY_POSITION (oend);
1865 endpos = min (endpos, oendpos);
1868 return endpos;
1873 /***********************************************************************
1874 Fontification
1875 ***********************************************************************/
1877 /* Handle changes in the `fontified' property of the current buffer by
1878 calling hook functions from Qfontification_functions to fontify
1879 regions of text. */
1881 static enum prop_handled
1882 handle_fontified_prop (it)
1883 struct it *it;
1885 Lisp_Object prop, pos;
1886 enum prop_handled handled = HANDLED_NORMALLY;
1888 /* Get the value of the `fontified' property at IT's current buffer
1889 position. (The `fontified' property doesn't have a special
1890 meaning in strings.) If the value is nil, call functions from
1891 Qfontification_functions. */
1892 if (!STRINGP (it->string)
1893 && it->s == NULL
1894 && !NILP (Vfontification_functions)
1895 && !NILP (Vrun_hooks)
1896 && (pos = make_number (IT_CHARPOS (*it)),
1897 prop = Fget_char_property (pos, Qfontified, Qnil),
1898 NILP (prop)))
1900 int count = specpdl_ptr - specpdl;
1901 Lisp_Object val;
1903 val = Vfontification_functions;
1904 specbind (Qfontification_functions, Qnil);
1905 specbind (Qafter_change_functions, Qnil);
1907 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
1908 safe_call1 (val, pos);
1909 else
1911 Lisp_Object globals, fn;
1912 struct gcpro gcpro1, gcpro2;
1914 globals = Qnil;
1915 GCPRO2 (val, globals);
1917 for (; CONSP (val); val = XCDR (val))
1919 fn = XCAR (val);
1921 if (EQ (fn, Qt))
1923 /* A value of t indicates this hook has a local
1924 binding; it means to run the global binding too.
1925 In a global value, t should not occur. If it
1926 does, we must ignore it to avoid an endless
1927 loop. */
1928 for (globals = Fdefault_value (Qfontification_functions);
1929 CONSP (globals);
1930 globals = XCDR (globals))
1932 fn = XCAR (globals);
1933 if (!EQ (fn, Qt))
1934 safe_call1 (fn, pos);
1937 else
1938 safe_call1 (fn, pos);
1941 UNGCPRO;
1944 unbind_to (count, Qnil);
1946 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1947 something. This avoids an endless loop if they failed to
1948 fontify the text for which reason ever. */
1949 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1950 handled = HANDLED_RECOMPUTE_PROPS;
1953 return handled;
1958 /***********************************************************************
1959 Faces
1960 ***********************************************************************/
1962 /* Set up iterator IT from face properties at its current position.
1963 Called from handle_stop. */
1965 static enum prop_handled
1966 handle_face_prop (it)
1967 struct it *it;
1969 int new_face_id, next_stop;
1971 if (!STRINGP (it->string))
1973 new_face_id
1974 = face_at_buffer_position (it->w,
1975 IT_CHARPOS (*it),
1976 it->region_beg_charpos,
1977 it->region_end_charpos,
1978 &next_stop,
1979 (IT_CHARPOS (*it)
1980 + TEXT_PROP_DISTANCE_LIMIT),
1983 /* Is this a start of a run of characters with box face?
1984 Caveat: this can be called for a freshly initialized
1985 iterator; face_id is -1 is this case. We know that the new
1986 face will not change until limit, i.e. if the new face has a
1987 box, all characters up to limit will have one. But, as
1988 usual, we don't know whether limit is really the end. */
1989 if (new_face_id != it->face_id)
1991 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1993 /* If new face has a box but old face has not, this is
1994 the start of a run of characters with box, i.e. it has
1995 a shadow on the left side. The value of face_id of the
1996 iterator will be -1 if this is the initial call that gets
1997 the face. In this case, we have to look in front of IT's
1998 position and see whether there is a face != new_face_id. */
1999 it->start_of_box_run_p
2000 = (new_face->box != FACE_NO_BOX
2001 && (it->face_id >= 0
2002 || IT_CHARPOS (*it) == BEG
2003 || new_face_id != face_before_it_pos (it)));
2004 it->face_box_p = new_face->box != FACE_NO_BOX;
2007 else
2009 new_face_id
2010 = face_at_string_position (it->w,
2011 it->string,
2012 IT_STRING_CHARPOS (*it),
2013 (it->current.overlay_string_index >= 0
2014 ? IT_CHARPOS (*it)
2015 : 0),
2016 it->region_beg_charpos,
2017 it->region_end_charpos,
2018 &next_stop,
2019 it->base_face_id);
2021 #if 0 /* This shouldn't be neccessary. Let's check it. */
2022 /* If IT is used to display a mode line we would really like to
2023 use the mode line face instead of the frame's default face. */
2024 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2025 && new_face_id == DEFAULT_FACE_ID)
2026 new_face_id = MODE_LINE_FACE_ID;
2027 #endif
2029 /* Is this a start of a run of characters with box? Caveat:
2030 this can be called for a freshly allocated iterator; face_id
2031 is -1 is this case. We know that the new face will not
2032 change until the next check pos, i.e. if the new face has a
2033 box, all characters up to that position will have a
2034 box. But, as usual, we don't know whether that position
2035 is really the end. */
2036 if (new_face_id != it->face_id)
2038 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2039 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2041 /* If new face has a box but old face hasn't, this is the
2042 start of a run of characters with box, i.e. it has a
2043 shadow on the left side. */
2044 it->start_of_box_run_p
2045 = new_face->box && (old_face == NULL || !old_face->box);
2046 it->face_box_p = new_face->box != FACE_NO_BOX;
2050 it->face_id = new_face_id;
2051 return HANDLED_NORMALLY;
2055 /* Compute the face one character before or after the current position
2056 of IT. BEFORE_P non-zero means get the face in front of IT's
2057 position. Value is the id of the face. */
2059 static int
2060 face_before_or_after_it_pos (it, before_p)
2061 struct it *it;
2062 int before_p;
2064 int face_id, limit;
2065 int next_check_charpos;
2066 struct text_pos pos;
2068 xassert (it->s == NULL);
2070 if (STRINGP (it->string))
2072 /* No face change past the end of the string (for the case
2073 we are padding with spaces). No face change before the
2074 string start. */
2075 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2076 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2077 return it->face_id;
2079 /* Set pos to the position before or after IT's current position. */
2080 if (before_p)
2081 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2082 else
2083 /* For composition, we must check the character after the
2084 composition. */
2085 pos = (it->what == IT_COMPOSITION
2086 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2087 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2089 /* Get the face for ASCII, or unibyte. */
2090 face_id
2091 = face_at_string_position (it->w,
2092 it->string,
2093 CHARPOS (pos),
2094 (it->current.overlay_string_index >= 0
2095 ? IT_CHARPOS (*it)
2096 : 0),
2097 it->region_beg_charpos,
2098 it->region_end_charpos,
2099 &next_check_charpos,
2100 it->base_face_id);
2102 /* Correct the face for charsets different from ASCII. Do it
2103 for the multibyte case only. The face returned above is
2104 suitable for unibyte text if IT->string is unibyte. */
2105 if (STRING_MULTIBYTE (it->string))
2107 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2108 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2109 int c, len;
2110 struct face *face = FACE_FROM_ID (it->f, face_id);
2112 c = string_char_and_length (p, rest, &len);
2113 face_id = FACE_FOR_CHAR (it->f, face, c);
2116 else
2118 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2119 || (IT_CHARPOS (*it) <= BEGV && before_p))
2120 return it->face_id;
2122 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2123 pos = it->current.pos;
2125 if (before_p)
2126 DEC_TEXT_POS (pos, it->multibyte_p);
2127 else
2129 if (it->what == IT_COMPOSITION)
2130 /* For composition, we must check the position after the
2131 composition. */
2132 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2133 else
2134 INC_TEXT_POS (pos, it->multibyte_p);
2136 /* Determine face for CHARSET_ASCII, or unibyte. */
2137 face_id = face_at_buffer_position (it->w,
2138 CHARPOS (pos),
2139 it->region_beg_charpos,
2140 it->region_end_charpos,
2141 &next_check_charpos,
2142 limit, 0);
2144 /* Correct the face for charsets different from ASCII. Do it
2145 for the multibyte case only. The face returned above is
2146 suitable for unibyte text if current_buffer is unibyte. */
2147 if (it->multibyte_p)
2149 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2150 struct face *face = FACE_FROM_ID (it->f, face_id);
2151 face_id = FACE_FOR_CHAR (it->f, face, c);
2155 return face_id;
2160 /***********************************************************************
2161 Invisible text
2162 ***********************************************************************/
2164 /* Set up iterator IT from invisible properties at its current
2165 position. Called from handle_stop. */
2167 static enum prop_handled
2168 handle_invisible_prop (it)
2169 struct it *it;
2171 enum prop_handled handled = HANDLED_NORMALLY;
2173 if (STRINGP (it->string))
2175 extern Lisp_Object Qinvisible;
2176 Lisp_Object prop, end_charpos, limit, charpos;
2178 /* Get the value of the invisible text property at the
2179 current position. Value will be nil if there is no such
2180 property. */
2181 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2182 prop = Fget_text_property (charpos, Qinvisible, it->string);
2184 if (!NILP (prop)
2185 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2187 handled = HANDLED_RECOMPUTE_PROPS;
2189 /* Get the position at which the next change of the
2190 invisible text property can be found in IT->string.
2191 Value will be nil if the property value is the same for
2192 all the rest of IT->string. */
2193 XSETINT (limit, XSTRING (it->string)->size);
2194 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2195 it->string, limit);
2197 /* Text at current position is invisible. The next
2198 change in the property is at position end_charpos.
2199 Move IT's current position to that position. */
2200 if (INTEGERP (end_charpos)
2201 && XFASTINT (end_charpos) < XFASTINT (limit))
2203 struct text_pos old;
2204 old = it->current.string_pos;
2205 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2206 compute_string_pos (&it->current.string_pos, old, it->string);
2208 else
2210 /* The rest of the string is invisible. If this is an
2211 overlay string, proceed with the next overlay string
2212 or whatever comes and return a character from there. */
2213 if (it->current.overlay_string_index >= 0)
2215 next_overlay_string (it);
2216 /* Don't check for overlay strings when we just
2217 finished processing them. */
2218 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2220 else
2222 struct Lisp_String *s = XSTRING (it->string);
2223 IT_STRING_CHARPOS (*it) = s->size;
2224 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2229 else
2231 int visible_p, newpos, next_stop;
2232 Lisp_Object pos, prop;
2234 /* First of all, is there invisible text at this position? */
2235 XSETFASTINT (pos, IT_CHARPOS (*it));
2236 prop = Fget_char_property (pos, Qinvisible, it->window);
2238 /* If we are on invisible text, skip over it. */
2239 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2240 && IT_CHARPOS (*it) < it->end_charpos)
2242 /* Record whether we have to display an ellipsis for the
2243 invisible text. */
2244 int display_ellipsis_p
2245 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2247 handled = HANDLED_RECOMPUTE_PROPS;
2249 /* Loop skipping over invisible text. The loop is left at
2250 ZV or with IT on the first char being visible again. */
2253 /* Try to skip some invisible text. Return value is the
2254 position reached which can be equal to IT's position
2255 if there is nothing invisible here. This skips both
2256 over invisible text properties and overlays with
2257 invisible property. */
2258 newpos = skip_invisible (IT_CHARPOS (*it),
2259 &next_stop, ZV, it->window);
2261 /* If we skipped nothing at all we weren't at invisible
2262 text in the first place. If everything to the end of
2263 the buffer was skipped, end the loop. */
2264 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2265 visible_p = 1;
2266 else
2268 /* We skipped some characters but not necessarily
2269 all there are. Check if we ended up on visible
2270 text. Fget_char_property returns the property of
2271 the char before the given position, i.e. if we
2272 get visible_p = 1, this means that the char at
2273 newpos is visible. */
2274 XSETFASTINT (pos, newpos);
2275 prop = Fget_char_property (pos, Qinvisible, it->window);
2276 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2279 /* If we ended up on invisible text, proceed to
2280 skip starting with next_stop. */
2281 if (!visible_p)
2282 IT_CHARPOS (*it) = next_stop;
2284 while (!visible_p);
2286 /* The position newpos is now either ZV or on visible text. */
2287 IT_CHARPOS (*it) = newpos;
2288 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2290 /* Maybe return `...' next for the end of the invisible text. */
2291 if (display_ellipsis_p)
2293 if (it->dp
2294 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2296 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2297 it->dpvec = v->contents;
2298 it->dpend = v->contents + v->size;
2300 else
2302 /* Default `...'. */
2303 it->dpvec = default_invis_vector;
2304 it->dpend = default_invis_vector + 3;
2307 /* The ellipsis display does not replace the display of
2308 the character at the new position. Indicate this by
2309 setting IT->dpvec_char_len to zero. */
2310 it->dpvec_char_len = 0;
2312 it->current.dpvec_index = 0;
2313 it->method = next_element_from_display_vector;
2318 return handled;
2323 /***********************************************************************
2324 'display' property
2325 ***********************************************************************/
2327 /* Set up iterator IT from `display' property at its current position.
2328 Called from handle_stop. */
2330 static enum prop_handled
2331 handle_display_prop (it)
2332 struct it *it;
2334 Lisp_Object prop, object;
2335 struct text_pos *position;
2336 int space_or_image_found_p;
2338 if (STRINGP (it->string))
2340 object = it->string;
2341 position = &it->current.string_pos;
2343 else
2345 object = Qnil;
2346 position = &it->current.pos;
2349 /* Reset those iterator values set from display property values. */
2350 it->font_height = Qnil;
2351 it->space_width = Qnil;
2352 it->voffset = 0;
2354 /* We don't support recursive `display' properties, i.e. string
2355 values that have a string `display' property, that have a string
2356 `display' property etc. */
2357 if (!it->string_from_display_prop_p)
2358 it->area = TEXT_AREA;
2360 prop = Fget_char_property (make_number (position->charpos),
2361 Qdisplay, object);
2362 if (NILP (prop))
2363 return HANDLED_NORMALLY;
2365 space_or_image_found_p = 0;
2366 if (CONSP (prop)
2367 && CONSP (XCAR (prop))
2368 && !EQ (Qmargin, XCAR (XCAR (prop))))
2370 /* A list of sub-properties. */
2371 while (CONSP (prop))
2373 if (handle_single_display_prop (it, XCAR (prop), object, position))
2374 space_or_image_found_p = 1;
2375 prop = XCDR (prop);
2378 else if (VECTORP (prop))
2380 int i;
2381 for (i = 0; i < XVECTOR (prop)->size; ++i)
2382 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2383 object, position))
2384 space_or_image_found_p = 1;
2386 else
2388 if (handle_single_display_prop (it, prop, object, position))
2389 space_or_image_found_p = 1;
2392 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2396 /* Value is the position of the end of the `display' property starting
2397 at START_POS in OBJECT. */
2399 static struct text_pos
2400 display_prop_end (it, object, start_pos)
2401 struct it *it;
2402 Lisp_Object object;
2403 struct text_pos start_pos;
2405 Lisp_Object end;
2406 struct text_pos end_pos;
2408 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2409 Qdisplay, object, Qnil);
2410 CHARPOS (end_pos) = XFASTINT (end);
2411 if (STRINGP (object))
2412 compute_string_pos (&end_pos, start_pos, it->string);
2413 else
2414 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2416 return end_pos;
2420 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2421 is the object in which the `display' property was found. *POSITION
2422 is the position at which it was found.
2424 If PROP is a `space' or `image' sub-property, set *POSITION to the
2425 end position of the `display' property.
2427 Value is non-zero if a `space' or `image' property value was found. */
2429 static int
2430 handle_single_display_prop (it, prop, object, position)
2431 struct it *it;
2432 Lisp_Object prop;
2433 Lisp_Object object;
2434 struct text_pos *position;
2436 Lisp_Object value;
2437 int space_or_image_found_p = 0;
2438 Lisp_Object form;
2440 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2441 evaluated. If the result is nil, VALUE is ignored. */
2442 form = Qt;
2443 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2445 prop = XCDR (prop);
2446 if (!CONSP (prop))
2447 return 0;
2448 form = XCAR (prop);
2449 prop = XCDR (prop);
2452 if (!NILP (form) && !EQ (form, Qt))
2454 struct gcpro gcpro1;
2455 struct text_pos end_pos, pt;
2457 GCPRO1 (form);
2458 end_pos = display_prop_end (it, object, *position);
2460 /* Temporarily set point to the end position, and then evaluate
2461 the form. This makes `(eolp)' work as FORM. */
2462 if (BUFFERP (object))
2464 CHARPOS (pt) = PT;
2465 BYTEPOS (pt) = PT_BYTE;
2466 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2469 form = safe_eval (form);
2471 if (BUFFERP (object))
2472 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2473 UNGCPRO;
2476 if (NILP (form))
2477 return 0;
2479 if (CONSP (prop)
2480 && EQ (XCAR (prop), Qheight)
2481 && CONSP (XCDR (prop)))
2483 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2484 return 0;
2486 /* `(height HEIGHT)'. */
2487 it->font_height = XCAR (XCDR (prop));
2488 if (!NILP (it->font_height))
2490 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2491 int new_height = -1;
2493 if (CONSP (it->font_height)
2494 && (EQ (XCAR (it->font_height), Qplus)
2495 || EQ (XCAR (it->font_height), Qminus))
2496 && CONSP (XCDR (it->font_height))
2497 && INTEGERP (XCAR (XCDR (it->font_height))))
2499 /* `(+ N)' or `(- N)' where N is an integer. */
2500 int steps = XINT (XCAR (XCDR (it->font_height)));
2501 if (EQ (XCAR (it->font_height), Qplus))
2502 steps = - steps;
2503 it->face_id = smaller_face (it->f, it->face_id, steps);
2505 else if (FUNCTIONP (it->font_height))
2507 /* Call function with current height as argument.
2508 Value is the new height. */
2509 Lisp_Object height;
2510 height = safe_call1 (it->font_height,
2511 face->lface[LFACE_HEIGHT_INDEX]);
2512 if (NUMBERP (height))
2513 new_height = XFLOATINT (height);
2515 else if (NUMBERP (it->font_height))
2517 /* Value is a multiple of the canonical char height. */
2518 struct face *face;
2520 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2521 new_height = (XFLOATINT (it->font_height)
2522 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2524 else
2526 /* Evaluate IT->font_height with `height' bound to the
2527 current specified height to get the new height. */
2528 Lisp_Object value;
2529 int count = specpdl_ptr - specpdl;
2531 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2532 value = safe_eval (it->font_height);
2533 unbind_to (count, Qnil);
2535 if (NUMBERP (value))
2536 new_height = XFLOATINT (value);
2539 if (new_height > 0)
2540 it->face_id = face_with_height (it->f, it->face_id, new_height);
2543 else if (CONSP (prop)
2544 && EQ (XCAR (prop), Qspace_width)
2545 && CONSP (XCDR (prop)))
2547 /* `(space_width WIDTH)'. */
2548 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2549 return 0;
2551 value = XCAR (XCDR (prop));
2552 if (NUMBERP (value) && XFLOATINT (value) > 0)
2553 it->space_width = value;
2555 else if (CONSP (prop)
2556 && EQ (XCAR (prop), Qraise)
2557 && CONSP (XCDR (prop)))
2559 /* `(raise FACTOR)'. */
2560 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2561 return 0;
2563 #ifdef HAVE_WINDOW_SYSTEM
2564 value = XCAR (XCDR (prop));
2565 if (NUMBERP (value))
2567 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2568 it->voffset = - (XFLOATINT (value)
2569 * (FONT_HEIGHT (face->font)));
2571 #endif /* HAVE_WINDOW_SYSTEM */
2573 else if (!it->string_from_display_prop_p)
2575 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2576 VALUE) or `((margin nil) VALUE)' or VALUE. */
2577 Lisp_Object location, value;
2578 struct text_pos start_pos;
2579 int valid_p;
2581 /* Characters having this form of property are not displayed, so
2582 we have to find the end of the property. */
2583 start_pos = *position;
2584 *position = display_prop_end (it, object, start_pos);
2585 value = Qnil;
2587 /* Let's stop at the new position and assume that all
2588 text properties change there. */
2589 it->stop_charpos = position->charpos;
2591 location = Qunbound;
2592 if (CONSP (prop) && CONSP (XCAR (prop)))
2594 Lisp_Object tem;
2596 value = XCDR (prop);
2597 if (CONSP (value))
2598 value = XCAR (value);
2600 tem = XCAR (prop);
2601 if (EQ (XCAR (tem), Qmargin)
2602 && (tem = XCDR (tem),
2603 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2604 (NILP (tem)
2605 || EQ (tem, Qleft_margin)
2606 || EQ (tem, Qright_margin))))
2607 location = tem;
2610 if (EQ (location, Qunbound))
2612 location = Qnil;
2613 value = prop;
2616 #ifdef HAVE_WINDOW_SYSTEM
2617 if (FRAME_TERMCAP_P (it->f))
2618 valid_p = STRINGP (value);
2619 else
2620 valid_p = (STRINGP (value)
2621 || (CONSP (value) && EQ (XCAR (value), Qspace))
2622 || valid_image_p (value));
2623 #else /* not HAVE_WINDOW_SYSTEM */
2624 valid_p = STRINGP (value);
2625 #endif /* not HAVE_WINDOW_SYSTEM */
2627 if ((EQ (location, Qleft_margin)
2628 || EQ (location, Qright_margin)
2629 || NILP (location))
2630 && valid_p)
2632 space_or_image_found_p = 1;
2634 /* Save current settings of IT so that we can restore them
2635 when we are finished with the glyph property value. */
2636 push_it (it);
2638 if (NILP (location))
2639 it->area = TEXT_AREA;
2640 else if (EQ (location, Qleft_margin))
2641 it->area = LEFT_MARGIN_AREA;
2642 else
2643 it->area = RIGHT_MARGIN_AREA;
2645 if (STRINGP (value))
2647 it->string = value;
2648 it->multibyte_p = STRING_MULTIBYTE (it->string);
2649 it->current.overlay_string_index = -1;
2650 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2651 it->end_charpos = it->string_nchars
2652 = XSTRING (it->string)->size;
2653 it->method = next_element_from_string;
2654 it->stop_charpos = 0;
2655 it->string_from_display_prop_p = 1;
2657 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2659 it->method = next_element_from_stretch;
2660 it->object = value;
2661 it->current.pos = it->position = start_pos;
2663 #ifdef HAVE_WINDOW_SYSTEM
2664 else
2666 it->what = IT_IMAGE;
2667 it->image_id = lookup_image (it->f, value);
2668 it->position = start_pos;
2669 it->object = NILP (object) ? it->w->buffer : object;
2670 it->method = next_element_from_image;
2672 /* Say that we haven't consumed the characters with
2673 `display' property yet. The call to pop_it in
2674 set_iterator_to_next will clean this up. */
2675 *position = start_pos;
2677 #endif /* HAVE_WINDOW_SYSTEM */
2679 else
2680 /* Invalid property or property not supported. Restore
2681 the position to what it was before. */
2682 *position = start_pos;
2685 return space_or_image_found_p;
2689 /* Check if PROP is a display sub-property value whose text should be
2690 treated as intangible. */
2692 static int
2693 single_display_prop_intangible_p (prop)
2694 Lisp_Object prop;
2696 /* Skip over `when FORM'. */
2697 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2699 prop = XCDR (prop);
2700 if (!CONSP (prop))
2701 return 0;
2702 prop = XCDR (prop);
2705 if (!CONSP (prop))
2706 return 0;
2708 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2709 we don't need to treat text as intangible. */
2710 if (EQ (XCAR (prop), Qmargin))
2712 prop = XCDR (prop);
2713 if (!CONSP (prop))
2714 return 0;
2716 prop = XCDR (prop);
2717 if (!CONSP (prop)
2718 || EQ (XCAR (prop), Qleft_margin)
2719 || EQ (XCAR (prop), Qright_margin))
2720 return 0;
2723 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2727 /* Check if PROP is a display property value whose text should be
2728 treated as intangible. */
2731 display_prop_intangible_p (prop)
2732 Lisp_Object prop;
2734 if (CONSP (prop)
2735 && CONSP (XCAR (prop))
2736 && !EQ (Qmargin, XCAR (XCAR (prop))))
2738 /* A list of sub-properties. */
2739 while (CONSP (prop))
2741 if (single_display_prop_intangible_p (XCAR (prop)))
2742 return 1;
2743 prop = XCDR (prop);
2746 else if (VECTORP (prop))
2748 /* A vector of sub-properties. */
2749 int i;
2750 for (i = 0; i < XVECTOR (prop)->size; ++i)
2751 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2752 return 1;
2754 else
2755 return single_display_prop_intangible_p (prop);
2757 return 0;
2761 /***********************************************************************
2762 `composition' property
2763 ***********************************************************************/
2765 /* Set up iterator IT from `composition' property at its current
2766 position. Called from handle_stop. */
2768 static enum prop_handled
2769 handle_composition_prop (it)
2770 struct it *it;
2772 Lisp_Object prop, string;
2773 int pos, pos_byte, end;
2774 enum prop_handled handled = HANDLED_NORMALLY;
2776 if (STRINGP (it->string))
2778 pos = IT_STRING_CHARPOS (*it);
2779 pos_byte = IT_STRING_BYTEPOS (*it);
2780 string = it->string;
2782 else
2784 pos = IT_CHARPOS (*it);
2785 pos_byte = IT_BYTEPOS (*it);
2786 string = Qnil;
2789 /* If there's a valid composition and point is not inside of the
2790 composition (in the case that the composition is from the current
2791 buffer), draw a glyph composed from the composition components. */
2792 if (find_composition (pos, -1, &pos, &end, &prop, string)
2793 && COMPOSITION_VALID_P (pos, end, prop)
2794 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2796 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2798 if (id >= 0)
2800 it->method = next_element_from_composition;
2801 it->cmp_id = id;
2802 it->cmp_len = COMPOSITION_LENGTH (prop);
2803 /* For a terminal, draw only the first character of the
2804 components. */
2805 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2806 it->len = (STRINGP (it->string)
2807 ? string_char_to_byte (it->string, end)
2808 : CHAR_TO_BYTE (end)) - pos_byte;
2809 it->stop_charpos = end;
2810 handled = HANDLED_RETURN;
2814 return handled;
2819 /***********************************************************************
2820 Overlay strings
2821 ***********************************************************************/
2823 /* The following structure is used to record overlay strings for
2824 later sorting in load_overlay_strings. */
2826 struct overlay_entry
2828 Lisp_Object overlay;
2829 Lisp_Object string;
2830 int priority;
2831 int after_string_p;
2835 /* Set up iterator IT from overlay strings at its current position.
2836 Called from handle_stop. */
2838 static enum prop_handled
2839 handle_overlay_change (it)
2840 struct it *it;
2842 if (!STRINGP (it->string) && get_overlay_strings (it))
2843 return HANDLED_RECOMPUTE_PROPS;
2844 else
2845 return HANDLED_NORMALLY;
2849 /* Set up the next overlay string for delivery by IT, if there is an
2850 overlay string to deliver. Called by set_iterator_to_next when the
2851 end of the current overlay string is reached. If there are more
2852 overlay strings to display, IT->string and
2853 IT->current.overlay_string_index are set appropriately here.
2854 Otherwise IT->string is set to nil. */
2856 static void
2857 next_overlay_string (it)
2858 struct it *it;
2860 ++it->current.overlay_string_index;
2861 if (it->current.overlay_string_index == it->n_overlay_strings)
2863 /* No more overlay strings. Restore IT's settings to what
2864 they were before overlay strings were processed, and
2865 continue to deliver from current_buffer. */
2866 pop_it (it);
2867 xassert (it->stop_charpos >= BEGV
2868 && it->stop_charpos <= it->end_charpos);
2869 it->string = Qnil;
2870 it->current.overlay_string_index = -1;
2871 SET_TEXT_POS (it->current.string_pos, -1, -1);
2872 it->n_overlay_strings = 0;
2873 it->method = next_element_from_buffer;
2875 /* If we're at the end of the buffer, record that we have
2876 processed the overlay strings there already, so that
2877 next_element_from_buffer doesn't try it again. */
2878 if (IT_CHARPOS (*it) >= it->end_charpos)
2879 it->overlay_strings_at_end_processed_p = 1;
2881 else
2883 /* There are more overlay strings to process. If
2884 IT->current.overlay_string_index has advanced to a position
2885 where we must load IT->overlay_strings with more strings, do
2886 it. */
2887 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2889 if (it->current.overlay_string_index && i == 0)
2890 load_overlay_strings (it);
2892 /* Initialize IT to deliver display elements from the overlay
2893 string. */
2894 it->string = it->overlay_strings[i];
2895 it->multibyte_p = STRING_MULTIBYTE (it->string);
2896 SET_TEXT_POS (it->current.string_pos, 0, 0);
2897 it->method = next_element_from_string;
2898 it->stop_charpos = 0;
2901 CHECK_IT (it);
2905 /* Compare two overlay_entry structures E1 and E2. Used as a
2906 comparison function for qsort in load_overlay_strings. Overlay
2907 strings for the same position are sorted so that
2909 1. All after-strings come in front of before-strings, except
2910 when they come from the same overlay.
2912 2. Within after-strings, strings are sorted so that overlay strings
2913 from overlays with higher priorities come first.
2915 2. Within before-strings, strings are sorted so that overlay
2916 strings from overlays with higher priorities come last.
2918 Value is analogous to strcmp. */
2921 static int
2922 compare_overlay_entries (e1, e2)
2923 void *e1, *e2;
2925 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2926 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2927 int result;
2929 if (entry1->after_string_p != entry2->after_string_p)
2931 /* Let after-strings appear in front of before-strings if
2932 they come from different overlays. */
2933 if (EQ (entry1->overlay, entry2->overlay))
2934 result = entry1->after_string_p ? 1 : -1;
2935 else
2936 result = entry1->after_string_p ? -1 : 1;
2938 else if (entry1->after_string_p)
2939 /* After-strings sorted in order of decreasing priority. */
2940 result = entry2->priority - entry1->priority;
2941 else
2942 /* Before-strings sorted in order of increasing priority. */
2943 result = entry1->priority - entry2->priority;
2945 return result;
2949 /* Load the vector IT->overlay_strings with overlay strings from IT's
2950 current buffer position. Set IT->n_overlays to the total number of
2951 overlay strings found.
2953 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2954 a time. On entry into load_overlay_strings,
2955 IT->current.overlay_string_index gives the number of overlay
2956 strings that have already been loaded by previous calls to this
2957 function.
2959 IT->add_overlay_start contains an additional overlay start
2960 position to consider for taking overlay strings from, if non-zero.
2961 This position comes into play when the overlay has an `invisible'
2962 property, and both before and after-strings. When we've skipped to
2963 the end of the overlay, because of its `invisible' property, we
2964 nevertheless want its before-string to appear.
2965 IT->add_overlay_start will contain the overlay start position
2966 in this case.
2968 Overlay strings are sorted so that after-string strings come in
2969 front of before-string strings. Within before and after-strings,
2970 strings are sorted by overlay priority. See also function
2971 compare_overlay_entries. */
2973 static void
2974 load_overlay_strings (it)
2975 struct it *it;
2977 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2978 Lisp_Object ov, overlay, window, str, invisible;
2979 int start, end;
2980 int size = 20;
2981 int n = 0, i, j, invis_p;
2982 struct overlay_entry *entries
2983 = (struct overlay_entry *) alloca (size * sizeof *entries);
2984 int charpos = IT_CHARPOS (*it);
2986 /* Append the overlay string STRING of overlay OVERLAY to vector
2987 `entries' which has size `size' and currently contains `n'
2988 elements. AFTER_P non-zero means STRING is an after-string of
2989 OVERLAY. */
2990 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2991 do \
2993 Lisp_Object priority; \
2995 if (n == size) \
2997 int new_size = 2 * size; \
2998 struct overlay_entry *old = entries; \
2999 entries = \
3000 (struct overlay_entry *) alloca (new_size \
3001 * sizeof *entries); \
3002 bcopy (old, entries, size * sizeof *entries); \
3003 size = new_size; \
3006 entries[n].string = (STRING); \
3007 entries[n].overlay = (OVERLAY); \
3008 priority = Foverlay_get ((OVERLAY), Qpriority); \
3009 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3010 entries[n].after_string_p = (AFTER_P); \
3011 ++n; \
3013 while (0)
3015 /* Process overlay before the overlay center. */
3016 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3018 overlay = XCAR (ov);
3019 xassert (OVERLAYP (overlay));
3020 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3021 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3023 if (end < charpos)
3024 break;
3026 /* Skip this overlay if it doesn't start or end at IT's current
3027 position. */
3028 if (end != charpos && start != charpos)
3029 continue;
3031 /* Skip this overlay if it doesn't apply to IT->w. */
3032 window = Foverlay_get (overlay, Qwindow);
3033 if (WINDOWP (window) && XWINDOW (window) != it->w)
3034 continue;
3036 /* If the text ``under'' the overlay is invisible, both before-
3037 and after-strings from this overlay are visible; start and
3038 end position are indistinguishable. */
3039 invisible = Foverlay_get (overlay, Qinvisible);
3040 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3042 /* If overlay has a non-empty before-string, record it. */
3043 if ((start == charpos || (end == charpos && invis_p))
3044 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3045 && XSTRING (str)->size)
3046 RECORD_OVERLAY_STRING (overlay, str, 0);
3048 /* If overlay has a non-empty after-string, record it. */
3049 if ((end == charpos || (start == charpos && invis_p))
3050 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3051 && XSTRING (str)->size)
3052 RECORD_OVERLAY_STRING (overlay, str, 1);
3055 /* Process overlays after the overlay center. */
3056 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3058 overlay = XCAR (ov);
3059 xassert (OVERLAYP (overlay));
3060 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3061 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3063 if (start > charpos)
3064 break;
3066 /* Skip this overlay if it doesn't start or end at IT's current
3067 position. */
3068 if (end != charpos && start != charpos)
3069 continue;
3071 /* Skip this overlay if it doesn't apply to IT->w. */
3072 window = Foverlay_get (overlay, Qwindow);
3073 if (WINDOWP (window) && XWINDOW (window) != it->w)
3074 continue;
3076 /* If the text ``under'' the overlay is invisible, it has a zero
3077 dimension, and both before- and after-strings apply. */
3078 invisible = Foverlay_get (overlay, Qinvisible);
3079 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3081 /* If overlay has a non-empty before-string, record it. */
3082 if ((start == charpos || (end == charpos && invis_p))
3083 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3084 && XSTRING (str)->size)
3085 RECORD_OVERLAY_STRING (overlay, str, 0);
3087 /* If overlay has a non-empty after-string, record it. */
3088 if ((end == charpos || (start == charpos && invis_p))
3089 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3090 && XSTRING (str)->size)
3091 RECORD_OVERLAY_STRING (overlay, str, 1);
3094 #undef RECORD_OVERLAY_STRING
3096 /* Sort entries. */
3097 if (n > 1)
3098 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3100 /* Record the total number of strings to process. */
3101 it->n_overlay_strings = n;
3103 /* IT->current.overlay_string_index is the number of overlay strings
3104 that have already been consumed by IT. Copy some of the
3105 remaining overlay strings to IT->overlay_strings. */
3106 i = 0;
3107 j = it->current.overlay_string_index;
3108 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3109 it->overlay_strings[i++] = entries[j++].string;
3111 CHECK_IT (it);
3115 /* Get the first chunk of overlay strings at IT's current buffer
3116 position. Value is non-zero if at least one overlay string was
3117 found. */
3119 static int
3120 get_overlay_strings (it)
3121 struct it *it;
3123 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3124 process. This fills IT->overlay_strings with strings, and sets
3125 IT->n_overlay_strings to the total number of strings to process.
3126 IT->pos.overlay_string_index has to be set temporarily to zero
3127 because load_overlay_strings needs this; it must be set to -1
3128 when no overlay strings are found because a zero value would
3129 indicate a position in the first overlay string. */
3130 it->current.overlay_string_index = 0;
3131 load_overlay_strings (it);
3133 /* If we found overlay strings, set up IT to deliver display
3134 elements from the first one. Otherwise set up IT to deliver
3135 from current_buffer. */
3136 if (it->n_overlay_strings)
3138 /* Make sure we know settings in current_buffer, so that we can
3139 restore meaningful values when we're done with the overlay
3140 strings. */
3141 compute_stop_pos (it);
3142 xassert (it->face_id >= 0);
3144 /* Save IT's settings. They are restored after all overlay
3145 strings have been processed. */
3146 xassert (it->sp == 0);
3147 push_it (it);
3149 /* Set up IT to deliver display elements from the first overlay
3150 string. */
3151 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3152 it->stop_charpos = 0;
3153 it->string = it->overlay_strings[0];
3154 it->multibyte_p = STRING_MULTIBYTE (it->string);
3155 xassert (STRINGP (it->string));
3156 it->method = next_element_from_string;
3158 else
3160 it->string = Qnil;
3161 it->current.overlay_string_index = -1;
3162 it->method = next_element_from_buffer;
3165 CHECK_IT (it);
3167 /* Value is non-zero if we found at least one overlay string. */
3168 return STRINGP (it->string);
3173 /***********************************************************************
3174 Saving and restoring state
3175 ***********************************************************************/
3177 /* Save current settings of IT on IT->stack. Called, for example,
3178 before setting up IT for an overlay string, to be able to restore
3179 IT's settings to what they were after the overlay string has been
3180 processed. */
3182 static void
3183 push_it (it)
3184 struct it *it;
3186 struct iterator_stack_entry *p;
3188 xassert (it->sp < 2);
3189 p = it->stack + it->sp;
3191 p->stop_charpos = it->stop_charpos;
3192 xassert (it->face_id >= 0);
3193 p->face_id = it->face_id;
3194 p->string = it->string;
3195 p->pos = it->current;
3196 p->end_charpos = it->end_charpos;
3197 p->string_nchars = it->string_nchars;
3198 p->area = it->area;
3199 p->multibyte_p = it->multibyte_p;
3200 p->space_width = it->space_width;
3201 p->font_height = it->font_height;
3202 p->voffset = it->voffset;
3203 p->string_from_display_prop_p = it->string_from_display_prop_p;
3204 ++it->sp;
3208 /* Restore IT's settings from IT->stack. Called, for example, when no
3209 more overlay strings must be processed, and we return to delivering
3210 display elements from a buffer, or when the end of a string from a
3211 `display' property is reached and we return to delivering display
3212 elements from an overlay string, or from a buffer. */
3214 static void
3215 pop_it (it)
3216 struct it *it;
3218 struct iterator_stack_entry *p;
3220 xassert (it->sp > 0);
3221 --it->sp;
3222 p = it->stack + it->sp;
3223 it->stop_charpos = p->stop_charpos;
3224 it->face_id = p->face_id;
3225 it->string = p->string;
3226 it->current = p->pos;
3227 it->end_charpos = p->end_charpos;
3228 it->string_nchars = p->string_nchars;
3229 it->area = p->area;
3230 it->multibyte_p = p->multibyte_p;
3231 it->space_width = p->space_width;
3232 it->font_height = p->font_height;
3233 it->voffset = p->voffset;
3234 it->string_from_display_prop_p = p->string_from_display_prop_p;
3239 /***********************************************************************
3240 Moving over lines
3241 ***********************************************************************/
3243 /* Set IT's current position to the previous line start. */
3245 static void
3246 back_to_previous_line_start (it)
3247 struct it *it;
3249 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3250 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3254 /* Move IT to the next line start.
3256 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3257 we skipped over part of the text (as opposed to moving the iterator
3258 continuously over the text). Otherwise, don't change the value
3259 of *SKIPPED_P.
3261 Newlines may come from buffer text, overlay strings, or strings
3262 displayed via the `display' property. That's the reason we can't
3263 simply use find_next_newline_no_quit. */
3265 static int
3266 forward_to_next_line_start (it, skipped_p)
3267 struct it *it;
3268 int *skipped_p;
3270 int newline_found_p, n;
3271 const int MAX_NEWLINE_DISTANCE = 500;
3273 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3274 from buffer text. */
3275 n = newline_found_p = 0;
3276 while (n < MAX_NEWLINE_DISTANCE
3277 && get_next_display_element (it)
3278 && !newline_found_p)
3280 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3281 set_iterator_to_next (it, 0);
3282 if (!STRINGP (it->string))
3283 ++n;
3286 /* If we didn't find a newline near enough, see if we can use a
3287 short-cut. */
3288 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3290 int start = IT_CHARPOS (*it);
3291 int limit = find_next_newline_no_quit (start, 1);
3292 Lisp_Object pos;
3294 xassert (!STRINGP (it->string));
3296 /* If there isn't any `display' property in sight, and no
3297 overlays, we can just use the position of the newline in
3298 buffer text. */
3299 if (it->stop_charpos >= limit
3300 || ((pos = Fnext_single_property_change (make_number (start),
3301 Qdisplay,
3302 Qnil, make_number (limit)),
3303 NILP (pos))
3304 && next_overlay_change (start) == ZV))
3306 IT_CHARPOS (*it) = limit;
3307 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3308 *skipped_p = newline_found_p = 1;
3310 else
3312 while (get_next_display_element (it)
3313 && !newline_found_p)
3315 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3316 set_iterator_to_next (it, 0);
3321 return newline_found_p;
3325 /* Set IT's current position to the previous visible line start. Skip
3326 invisible text that is so either due to text properties or due to
3327 selective display. Caution: this does not change IT->current_x and
3328 IT->hpos. */
3330 static void
3331 back_to_previous_visible_line_start (it)
3332 struct it *it;
3334 int visible_p = 0;
3336 /* Go back one newline if not on BEGV already. */
3337 if (IT_CHARPOS (*it) > BEGV)
3338 back_to_previous_line_start (it);
3340 /* Move over lines that are invisible because of selective display
3341 or text properties. */
3342 while (IT_CHARPOS (*it) > BEGV
3343 && !visible_p)
3345 visible_p = 1;
3347 /* If selective > 0, then lines indented more than that values
3348 are invisible. */
3349 if (it->selective > 0
3350 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3351 it->selective))
3352 visible_p = 0;
3353 else
3355 Lisp_Object prop;
3357 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3358 Qinvisible, it->window);
3359 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3360 visible_p = 0;
3363 /* Back one more newline if the current one is invisible. */
3364 if (!visible_p)
3365 back_to_previous_line_start (it);
3368 xassert (IT_CHARPOS (*it) >= BEGV);
3369 xassert (IT_CHARPOS (*it) == BEGV
3370 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3371 CHECK_IT (it);
3375 /* Reseat iterator IT at the previous visible line start. Skip
3376 invisible text that is so either due to text properties or due to
3377 selective display. At the end, update IT's overlay information,
3378 face information etc. */
3380 static void
3381 reseat_at_previous_visible_line_start (it)
3382 struct it *it;
3384 back_to_previous_visible_line_start (it);
3385 reseat (it, it->current.pos, 1);
3386 CHECK_IT (it);
3390 /* Reseat iterator IT on the next visible line start in the current
3391 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3392 preceding the line start. Skip over invisible text that is so
3393 because of selective display. Compute faces, overlays etc at the
3394 new position. Note that this function does not skip over text that
3395 is invisible because of text properties. */
3397 static void
3398 reseat_at_next_visible_line_start (it, on_newline_p)
3399 struct it *it;
3400 int on_newline_p;
3402 int newline_found_p, skipped_p = 0;
3404 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3406 /* Skip over lines that are invisible because they are indented
3407 more than the value of IT->selective. */
3408 if (it->selective > 0)
3409 while (IT_CHARPOS (*it) < ZV
3410 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3411 it->selective))
3412 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3414 /* Position on the newline if that's what's requested. */
3415 if (on_newline_p && newline_found_p)
3417 if (STRINGP (it->string))
3419 if (IT_STRING_CHARPOS (*it) > 0)
3421 --IT_STRING_CHARPOS (*it);
3422 --IT_STRING_BYTEPOS (*it);
3425 else if (IT_CHARPOS (*it) > BEGV)
3427 --IT_CHARPOS (*it);
3428 --IT_BYTEPOS (*it);
3429 reseat (it, it->current.pos, 0);
3432 else if (skipped_p)
3433 reseat (it, it->current.pos, 0);
3435 CHECK_IT (it);
3440 /***********************************************************************
3441 Changing an iterator's position
3442 ***********************************************************************/
3444 /* Change IT's current position to POS in current_buffer. If FORCE_P
3445 is non-zero, always check for text properties at the new position.
3446 Otherwise, text properties are only looked up if POS >=
3447 IT->check_charpos of a property. */
3449 static void
3450 reseat (it, pos, force_p)
3451 struct it *it;
3452 struct text_pos pos;
3453 int force_p;
3455 int original_pos = IT_CHARPOS (*it);
3457 reseat_1 (it, pos, 0);
3459 /* Determine where to check text properties. Avoid doing it
3460 where possible because text property lookup is very expensive. */
3461 if (force_p
3462 || CHARPOS (pos) > it->stop_charpos
3463 || CHARPOS (pos) < original_pos)
3464 handle_stop (it);
3466 CHECK_IT (it);
3470 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3471 IT->stop_pos to POS, also. */
3473 static void
3474 reseat_1 (it, pos, set_stop_p)
3475 struct it *it;
3476 struct text_pos pos;
3477 int set_stop_p;
3479 /* Don't call this function when scanning a C string. */
3480 xassert (it->s == NULL);
3482 /* POS must be a reasonable value. */
3483 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3485 it->current.pos = it->position = pos;
3486 XSETBUFFER (it->object, current_buffer);
3487 it->dpvec = NULL;
3488 it->current.dpvec_index = -1;
3489 it->current.overlay_string_index = -1;
3490 IT_STRING_CHARPOS (*it) = -1;
3491 IT_STRING_BYTEPOS (*it) = -1;
3492 it->string = Qnil;
3493 it->method = next_element_from_buffer;
3494 it->sp = 0;
3496 if (set_stop_p)
3497 it->stop_charpos = CHARPOS (pos);
3501 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3502 If S is non-null, it is a C string to iterate over. Otherwise,
3503 STRING gives a Lisp string to iterate over.
3505 If PRECISION > 0, don't return more then PRECISION number of
3506 characters from the string.
3508 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3509 characters have been returned. FIELD_WIDTH < 0 means an infinite
3510 field width.
3512 MULTIBYTE = 0 means disable processing of multibyte characters,
3513 MULTIBYTE > 0 means enable it,
3514 MULTIBYTE < 0 means use IT->multibyte_p.
3516 IT must be initialized via a prior call to init_iterator before
3517 calling this function. */
3519 static void
3520 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3521 struct it *it;
3522 unsigned char *s;
3523 Lisp_Object string;
3524 int charpos;
3525 int precision, field_width, multibyte;
3527 /* No region in strings. */
3528 it->region_beg_charpos = it->region_end_charpos = -1;
3530 /* No text property checks performed by default, but see below. */
3531 it->stop_charpos = -1;
3533 /* Set iterator position and end position. */
3534 bzero (&it->current, sizeof it->current);
3535 it->current.overlay_string_index = -1;
3536 it->current.dpvec_index = -1;
3537 xassert (charpos >= 0);
3539 /* Use the setting of MULTIBYTE if specified. */
3540 if (multibyte >= 0)
3541 it->multibyte_p = multibyte > 0;
3543 if (s == NULL)
3545 xassert (STRINGP (string));
3546 it->string = string;
3547 it->s = NULL;
3548 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3549 it->method = next_element_from_string;
3550 it->current.string_pos = string_pos (charpos, string);
3552 else
3554 it->s = s;
3555 it->string = Qnil;
3557 /* Note that we use IT->current.pos, not it->current.string_pos,
3558 for displaying C strings. */
3559 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3560 if (it->multibyte_p)
3562 it->current.pos = c_string_pos (charpos, s, 1);
3563 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3565 else
3567 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3568 it->end_charpos = it->string_nchars = strlen (s);
3571 it->method = next_element_from_c_string;
3574 /* PRECISION > 0 means don't return more than PRECISION characters
3575 from the string. */
3576 if (precision > 0 && it->end_charpos - charpos > precision)
3577 it->end_charpos = it->string_nchars = charpos + precision;
3579 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3580 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3581 FIELD_WIDTH < 0 means infinite field width. This is useful for
3582 padding with `-' at the end of a mode line. */
3583 if (field_width < 0)
3584 field_width = INFINITY;
3585 if (field_width > it->end_charpos - charpos)
3586 it->end_charpos = charpos + field_width;
3588 /* Use the standard display table for displaying strings. */
3589 if (DISP_TABLE_P (Vstandard_display_table))
3590 it->dp = XCHAR_TABLE (Vstandard_display_table);
3592 it->stop_charpos = charpos;
3593 CHECK_IT (it);
3598 /***********************************************************************
3599 Iteration
3600 ***********************************************************************/
3602 /* Load IT's display element fields with information about the next
3603 display element from the current position of IT. Value is zero if
3604 end of buffer (or C string) is reached. */
3607 get_next_display_element (it)
3608 struct it *it;
3610 /* Non-zero means that we found an display element. Zero means that
3611 we hit the end of what we iterate over. Performance note: the
3612 function pointer `method' used here turns out to be faster than
3613 using a sequence of if-statements. */
3614 int success_p = (*it->method) (it);
3616 if (it->what == IT_CHARACTER)
3618 /* Map via display table or translate control characters.
3619 IT->c, IT->len etc. have been set to the next character by
3620 the function call above. If we have a display table, and it
3621 contains an entry for IT->c, translate it. Don't do this if
3622 IT->c itself comes from a display table, otherwise we could
3623 end up in an infinite recursion. (An alternative could be to
3624 count the recursion depth of this function and signal an
3625 error when a certain maximum depth is reached.) Is it worth
3626 it? */
3627 if (success_p && it->dpvec == NULL)
3629 Lisp_Object dv;
3631 if (it->dp
3632 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3633 VECTORP (dv)))
3635 struct Lisp_Vector *v = XVECTOR (dv);
3637 /* Return the first character from the display table
3638 entry, if not empty. If empty, don't display the
3639 current character. */
3640 if (v->size)
3642 it->dpvec_char_len = it->len;
3643 it->dpvec = v->contents;
3644 it->dpend = v->contents + v->size;
3645 it->current.dpvec_index = 0;
3646 it->method = next_element_from_display_vector;
3649 success_p = get_next_display_element (it);
3652 /* Translate control characters into `\003' or `^C' form.
3653 Control characters coming from a display table entry are
3654 currently not translated because we use IT->dpvec to hold
3655 the translation. This could easily be changed but I
3656 don't believe that it is worth doing.
3658 Non-printable multibyte characters are also translated
3659 octal form. */
3660 else if ((it->c < ' '
3661 && (it->area != TEXT_AREA
3662 || (it->c != '\n' && it->c != '\t')))
3663 || (it->c >= 127
3664 && it->len == 1)
3665 || !CHAR_PRINTABLE_P (it->c))
3667 /* IT->c is a control character which must be displayed
3668 either as '\003' or as `^C' where the '\\' and '^'
3669 can be defined in the display table. Fill
3670 IT->ctl_chars with glyphs for what we have to
3671 display. Then, set IT->dpvec to these glyphs. */
3672 GLYPH g;
3674 if (it->c < 128 && it->ctl_arrow_p)
3676 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3677 if (it->dp
3678 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3679 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3680 g = XINT (DISP_CTRL_GLYPH (it->dp));
3681 else
3682 g = FAST_MAKE_GLYPH ('^', 0);
3683 XSETINT (it->ctl_chars[0], g);
3685 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3686 XSETINT (it->ctl_chars[1], g);
3688 /* Set up IT->dpvec and return first character from it. */
3689 it->dpvec_char_len = it->len;
3690 it->dpvec = it->ctl_chars;
3691 it->dpend = it->dpvec + 2;
3692 it->current.dpvec_index = 0;
3693 it->method = next_element_from_display_vector;
3694 get_next_display_element (it);
3696 else
3698 unsigned char str[MAX_MULTIBYTE_LENGTH];
3699 int len;
3700 int i;
3701 GLYPH escape_glyph;
3703 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3704 if (it->dp
3705 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3706 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3707 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3708 else
3709 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3711 if (SINGLE_BYTE_CHAR_P (it->c))
3712 str[0] = it->c, len = 1;
3713 else
3714 len = CHAR_STRING (it->c, str);
3716 for (i = 0; i < len; i++)
3718 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3719 /* Insert three more glyphs into IT->ctl_chars for
3720 the octal display of the character. */
3721 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3722 XSETINT (it->ctl_chars[i * 4 + 1], g);
3723 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3724 XSETINT (it->ctl_chars[i * 4 + 2], g);
3725 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3726 XSETINT (it->ctl_chars[i * 4 + 3], g);
3729 /* Set up IT->dpvec and return the first character
3730 from it. */
3731 it->dpvec_char_len = it->len;
3732 it->dpvec = it->ctl_chars;
3733 it->dpend = it->dpvec + len * 4;
3734 it->current.dpvec_index = 0;
3735 it->method = next_element_from_display_vector;
3736 get_next_display_element (it);
3741 /* Adjust face id for a multibyte character. There are no
3742 multibyte character in unibyte text. */
3743 if (it->multibyte_p
3744 && success_p
3745 && FRAME_WINDOW_P (it->f))
3747 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3748 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3752 /* Is this character the last one of a run of characters with
3753 box? If yes, set IT->end_of_box_run_p to 1. */
3754 if (it->face_box_p
3755 && it->s == NULL)
3757 int face_id;
3758 struct face *face;
3760 it->end_of_box_run_p
3761 = ((face_id = face_after_it_pos (it),
3762 face_id != it->face_id)
3763 && (face = FACE_FROM_ID (it->f, face_id),
3764 face->box == FACE_NO_BOX));
3767 /* Value is 0 if end of buffer or string reached. */
3768 return success_p;
3772 /* Move IT to the next display element.
3774 RESEAT_P non-zero means if called on a newline in buffer text,
3775 skip to the next visible line start.
3777 Functions get_next_display_element and set_iterator_to_next are
3778 separate because I find this arrangement easier to handle than a
3779 get_next_display_element function that also increments IT's
3780 position. The way it is we can first look at an iterator's current
3781 display element, decide whether it fits on a line, and if it does,
3782 increment the iterator position. The other way around we probably
3783 would either need a flag indicating whether the iterator has to be
3784 incremented the next time, or we would have to implement a
3785 decrement position function which would not be easy to write. */
3787 void
3788 set_iterator_to_next (it, reseat_p)
3789 struct it *it;
3790 int reseat_p;
3792 if (it->method == next_element_from_buffer)
3794 /* The current display element of IT is a character from
3795 current_buffer. Advance in the buffer, and maybe skip over
3796 invisible lines that are so because of selective display. */
3797 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3798 reseat_at_next_visible_line_start (it, 0);
3799 else
3801 xassert (it->len != 0);
3802 IT_BYTEPOS (*it) += it->len;
3803 IT_CHARPOS (*it) += 1;
3804 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3807 else if (it->method == next_element_from_composition)
3809 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3810 if (STRINGP (it->string))
3812 IT_STRING_BYTEPOS (*it) += it->len;
3813 IT_STRING_CHARPOS (*it) += it->cmp_len;
3814 it->method = next_element_from_string;
3815 goto consider_string_end;
3817 else
3819 IT_BYTEPOS (*it) += it->len;
3820 IT_CHARPOS (*it) += it->cmp_len;
3821 it->method = next_element_from_buffer;
3824 else if (it->method == next_element_from_c_string)
3826 /* Current display element of IT is from a C string. */
3827 IT_BYTEPOS (*it) += it->len;
3828 IT_CHARPOS (*it) += 1;
3830 else if (it->method == next_element_from_display_vector)
3832 /* Current display element of IT is from a display table entry.
3833 Advance in the display table definition. Reset it to null if
3834 end reached, and continue with characters from buffers/
3835 strings. */
3836 ++it->current.dpvec_index;
3838 /* Restore face of the iterator to what they were before the
3839 display vector entry (these entries may contain faces). */
3840 it->face_id = it->saved_face_id;
3842 if (it->dpvec + it->current.dpvec_index == it->dpend)
3844 if (it->s)
3845 it->method = next_element_from_c_string;
3846 else if (STRINGP (it->string))
3847 it->method = next_element_from_string;
3848 else
3849 it->method = next_element_from_buffer;
3851 it->dpvec = NULL;
3852 it->current.dpvec_index = -1;
3854 /* Skip over characters which were displayed via IT->dpvec. */
3855 if (it->dpvec_char_len < 0)
3856 reseat_at_next_visible_line_start (it, 1);
3857 else if (it->dpvec_char_len > 0)
3859 it->len = it->dpvec_char_len;
3860 set_iterator_to_next (it, reseat_p);
3864 else if (it->method == next_element_from_string)
3866 /* Current display element is a character from a Lisp string. */
3867 xassert (it->s == NULL && STRINGP (it->string));
3868 IT_STRING_BYTEPOS (*it) += it->len;
3869 IT_STRING_CHARPOS (*it) += 1;
3871 consider_string_end:
3873 if (it->current.overlay_string_index >= 0)
3875 /* IT->string is an overlay string. Advance to the
3876 next, if there is one. */
3877 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3878 next_overlay_string (it);
3880 else
3882 /* IT->string is not an overlay string. If we reached
3883 its end, and there is something on IT->stack, proceed
3884 with what is on the stack. This can be either another
3885 string, this time an overlay string, or a buffer. */
3886 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3887 && it->sp > 0)
3889 pop_it (it);
3890 if (!STRINGP (it->string))
3891 it->method = next_element_from_buffer;
3895 else if (it->method == next_element_from_image
3896 || it->method == next_element_from_stretch)
3898 /* The position etc with which we have to proceed are on
3899 the stack. The position may be at the end of a string,
3900 if the `display' property takes up the whole string. */
3901 pop_it (it);
3902 it->image_id = 0;
3903 if (STRINGP (it->string))
3905 it->method = next_element_from_string;
3906 goto consider_string_end;
3908 else
3909 it->method = next_element_from_buffer;
3911 else
3912 /* There are no other methods defined, so this should be a bug. */
3913 abort ();
3915 /* Reset flags indicating start and end of a sequence of
3916 characters with box. */
3917 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3919 xassert (it->method != next_element_from_string
3920 || (STRINGP (it->string)
3921 && IT_STRING_CHARPOS (*it) >= 0));
3925 /* Load IT's display element fields with information about the next
3926 display element which comes from a display table entry or from the
3927 result of translating a control character to one of the forms `^C'
3928 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3930 static int
3931 next_element_from_display_vector (it)
3932 struct it *it;
3934 /* Precondition. */
3935 xassert (it->dpvec && it->current.dpvec_index >= 0);
3937 /* Remember the current face id in case glyphs specify faces.
3938 IT's face is restored in set_iterator_to_next. */
3939 it->saved_face_id = it->face_id;
3941 if (INTEGERP (*it->dpvec)
3942 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3944 int lface_id;
3945 GLYPH g;
3947 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3948 it->c = FAST_GLYPH_CHAR (g);
3949 it->len = CHAR_BYTES (it->c);
3951 /* The entry may contain a face id to use. Such a face id is
3952 the id of a Lisp face, not a realized face. A face id of
3953 zero means no face is specified. */
3954 lface_id = FAST_GLYPH_FACE (g);
3955 if (lface_id)
3957 /* The function returns -1 if lface_id is invalid. */
3958 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3959 if (face_id >= 0)
3960 it->face_id = face_id;
3963 else
3964 /* Display table entry is invalid. Return a space. */
3965 it->c = ' ', it->len = 1;
3967 /* Don't change position and object of the iterator here. They are
3968 still the values of the character that had this display table
3969 entry or was translated, and that's what we want. */
3970 it->what = IT_CHARACTER;
3971 return 1;
3975 /* Load IT with the next display element from Lisp string IT->string.
3976 IT->current.string_pos is the current position within the string.
3977 If IT->current.overlay_string_index >= 0, the Lisp string is an
3978 overlay string. */
3980 static int
3981 next_element_from_string (it)
3982 struct it *it;
3984 struct text_pos position;
3986 xassert (STRINGP (it->string));
3987 xassert (IT_STRING_CHARPOS (*it) >= 0);
3988 position = it->current.string_pos;
3990 /* Time to check for invisible text? */
3991 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3992 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3994 handle_stop (it);
3996 /* Since a handler may have changed IT->method, we must
3997 recurse here. */
3998 return get_next_display_element (it);
4001 if (it->current.overlay_string_index >= 0)
4003 /* Get the next character from an overlay string. In overlay
4004 strings, There is no field width or padding with spaces to
4005 do. */
4006 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4008 it->what = IT_EOB;
4009 return 0;
4011 else if (STRING_MULTIBYTE (it->string))
4013 int remaining = (STRING_BYTES (XSTRING (it->string))
4014 - IT_STRING_BYTEPOS (*it));
4015 unsigned char *s = (XSTRING (it->string)->data
4016 + IT_STRING_BYTEPOS (*it));
4017 it->c = string_char_and_length (s, remaining, &it->len);
4019 else
4021 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4022 it->len = 1;
4025 else
4027 /* Get the next character from a Lisp string that is not an
4028 overlay string. Such strings come from the mode line, for
4029 example. We may have to pad with spaces, or truncate the
4030 string. See also next_element_from_c_string. */
4031 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4033 it->what = IT_EOB;
4034 return 0;
4036 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4038 /* Pad with spaces. */
4039 it->c = ' ', it->len = 1;
4040 CHARPOS (position) = BYTEPOS (position) = -1;
4042 else if (STRING_MULTIBYTE (it->string))
4044 int maxlen = (STRING_BYTES (XSTRING (it->string))
4045 - IT_STRING_BYTEPOS (*it));
4046 unsigned char *s = (XSTRING (it->string)->data
4047 + IT_STRING_BYTEPOS (*it));
4048 it->c = string_char_and_length (s, maxlen, &it->len);
4050 else
4052 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4053 it->len = 1;
4057 /* Record what we have and where it came from. Note that we store a
4058 buffer position in IT->position although it could arguably be a
4059 string position. */
4060 it->what = IT_CHARACTER;
4061 it->object = it->string;
4062 it->position = position;
4063 return 1;
4067 /* Load IT with next display element from C string IT->s.
4068 IT->string_nchars is the maximum number of characters to return
4069 from the string. IT->end_charpos may be greater than
4070 IT->string_nchars when this function is called, in which case we
4071 may have to return padding spaces. Value is zero if end of string
4072 reached, including padding spaces. */
4074 static int
4075 next_element_from_c_string (it)
4076 struct it *it;
4078 int success_p = 1;
4080 xassert (it->s);
4081 it->what = IT_CHARACTER;
4082 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4083 it->object = Qnil;
4085 /* IT's position can be greater IT->string_nchars in case a field
4086 width or precision has been specified when the iterator was
4087 initialized. */
4088 if (IT_CHARPOS (*it) >= it->end_charpos)
4090 /* End of the game. */
4091 it->what = IT_EOB;
4092 success_p = 0;
4094 else if (IT_CHARPOS (*it) >= it->string_nchars)
4096 /* Pad with spaces. */
4097 it->c = ' ', it->len = 1;
4098 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4100 else if (it->multibyte_p)
4102 /* Implementation note: The calls to strlen apparently aren't a
4103 performance problem because there is no noticeable performance
4104 difference between Emacs running in unibyte or multibyte mode. */
4105 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4106 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4107 maxlen, &it->len);
4109 else
4110 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4112 return success_p;
4116 /* Set up IT to return characters from an ellipsis, if appropriate.
4117 The definition of the ellipsis glyphs may come from a display table
4118 entry. This function Fills IT with the first glyph from the
4119 ellipsis if an ellipsis is to be displayed. */
4121 static int
4122 next_element_from_ellipsis (it)
4123 struct it *it;
4125 if (it->selective_display_ellipsis_p)
4127 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4129 /* Use the display table definition for `...'. Invalid glyphs
4130 will be handled by the method returning elements from dpvec. */
4131 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4132 it->dpvec_char_len = it->len;
4133 it->dpvec = v->contents;
4134 it->dpend = v->contents + v->size;
4135 it->current.dpvec_index = 0;
4136 it->method = next_element_from_display_vector;
4138 else
4140 /* Use default `...' which is stored in default_invis_vector. */
4141 it->dpvec_char_len = it->len;
4142 it->dpvec = default_invis_vector;
4143 it->dpend = default_invis_vector + 3;
4144 it->current.dpvec_index = 0;
4145 it->method = next_element_from_display_vector;
4148 else
4149 reseat_at_next_visible_line_start (it, 1);
4151 return get_next_display_element (it);
4155 /* Deliver an image display element. The iterator IT is already
4156 filled with image information (done in handle_display_prop). Value
4157 is always 1. */
4160 static int
4161 next_element_from_image (it)
4162 struct it *it;
4164 it->what = IT_IMAGE;
4165 return 1;
4169 /* Fill iterator IT with next display element from a stretch glyph
4170 property. IT->object is the value of the text property. Value is
4171 always 1. */
4173 static int
4174 next_element_from_stretch (it)
4175 struct it *it;
4177 it->what = IT_STRETCH;
4178 return 1;
4182 /* Load IT with the next display element from current_buffer. Value
4183 is zero if end of buffer reached. IT->stop_charpos is the next
4184 position at which to stop and check for text properties or buffer
4185 end. */
4187 static int
4188 next_element_from_buffer (it)
4189 struct it *it;
4191 int success_p = 1;
4193 /* Check this assumption, otherwise, we would never enter the
4194 if-statement, below. */
4195 xassert (IT_CHARPOS (*it) >= BEGV
4196 && IT_CHARPOS (*it) <= it->stop_charpos);
4198 if (IT_CHARPOS (*it) >= it->stop_charpos)
4200 if (IT_CHARPOS (*it) >= it->end_charpos)
4202 int overlay_strings_follow_p;
4204 /* End of the game, except when overlay strings follow that
4205 haven't been returned yet. */
4206 if (it->overlay_strings_at_end_processed_p)
4207 overlay_strings_follow_p = 0;
4208 else
4210 it->overlay_strings_at_end_processed_p = 1;
4211 overlay_strings_follow_p = get_overlay_strings (it);
4214 if (overlay_strings_follow_p)
4215 success_p = get_next_display_element (it);
4216 else
4218 it->what = IT_EOB;
4219 it->position = it->current.pos;
4220 success_p = 0;
4223 else
4225 handle_stop (it);
4226 return get_next_display_element (it);
4229 else
4231 /* No face changes, overlays etc. in sight, so just return a
4232 character from current_buffer. */
4233 unsigned char *p;
4235 /* Maybe run the redisplay end trigger hook. Performance note:
4236 This doesn't seem to cost measurable time. */
4237 if (it->redisplay_end_trigger_charpos
4238 && it->glyph_row
4239 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4240 run_redisplay_end_trigger_hook (it);
4242 /* Get the next character, maybe multibyte. */
4243 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4244 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4246 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4247 - IT_BYTEPOS (*it));
4248 it->c = string_char_and_length (p, maxlen, &it->len);
4250 else
4251 it->c = *p, it->len = 1;
4253 /* Record what we have and where it came from. */
4254 it->what = IT_CHARACTER;;
4255 it->object = it->w->buffer;
4256 it->position = it->current.pos;
4258 /* Normally we return the character found above, except when we
4259 really want to return an ellipsis for selective display. */
4260 if (it->selective)
4262 if (it->c == '\n')
4264 /* A value of selective > 0 means hide lines indented more
4265 than that number of columns. */
4266 if (it->selective > 0
4267 && IT_CHARPOS (*it) + 1 < ZV
4268 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4269 IT_BYTEPOS (*it) + 1,
4270 it->selective))
4272 success_p = next_element_from_ellipsis (it);
4273 it->dpvec_char_len = -1;
4276 else if (it->c == '\r' && it->selective == -1)
4278 /* A value of selective == -1 means that everything from the
4279 CR to the end of the line is invisible, with maybe an
4280 ellipsis displayed for it. */
4281 success_p = next_element_from_ellipsis (it);
4282 it->dpvec_char_len = -1;
4287 /* Value is zero if end of buffer reached. */
4288 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4289 return success_p;
4293 /* Run the redisplay end trigger hook for IT. */
4295 static void
4296 run_redisplay_end_trigger_hook (it)
4297 struct it *it;
4299 Lisp_Object args[3];
4301 /* IT->glyph_row should be non-null, i.e. we should be actually
4302 displaying something, or otherwise we should not run the hook. */
4303 xassert (it->glyph_row);
4305 /* Set up hook arguments. */
4306 args[0] = Qredisplay_end_trigger_functions;
4307 args[1] = it->window;
4308 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4309 it->redisplay_end_trigger_charpos = 0;
4311 /* Since we are *trying* to run these functions, don't try to run
4312 them again, even if they get an error. */
4313 it->w->redisplay_end_trigger = Qnil;
4314 Frun_hook_with_args (3, args);
4316 /* Notice if it changed the face of the character we are on. */
4317 handle_face_prop (it);
4321 /* Deliver a composition display element. The iterator IT is already
4322 filled with composition information (done in
4323 handle_composition_prop). Value is always 1. */
4325 static int
4326 next_element_from_composition (it)
4327 struct it *it;
4329 it->what = IT_COMPOSITION;
4330 it->position = (STRINGP (it->string)
4331 ? it->current.string_pos
4332 : it->current.pos);
4333 return 1;
4338 /***********************************************************************
4339 Moving an iterator without producing glyphs
4340 ***********************************************************************/
4342 /* Move iterator IT to a specified buffer or X position within one
4343 line on the display without producing glyphs.
4345 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4346 whichever is reached first.
4348 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4350 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4351 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4352 TO_X includes the amount by which a window is horizontally
4353 scrolled.
4355 Value is
4357 MOVE_POS_MATCH_OR_ZV
4358 - when TO_POS or ZV was reached.
4360 MOVE_X_REACHED
4361 -when TO_X was reached before TO_POS or ZV were reached.
4363 MOVE_LINE_CONTINUED
4364 - when we reached the end of the display area and the line must
4365 be continued.
4367 MOVE_LINE_TRUNCATED
4368 - when we reached the end of the display area and the line is
4369 truncated.
4371 MOVE_NEWLINE_OR_CR
4372 - when we stopped at a line end, i.e. a newline or a CR and selective
4373 display is on. */
4375 static enum move_it_result
4376 move_it_in_display_line_to (it, to_charpos, to_x, op)
4377 struct it *it;
4378 int to_charpos, to_x, op;
4380 enum move_it_result result = MOVE_UNDEFINED;
4381 struct glyph_row *saved_glyph_row;
4383 /* Don't produce glyphs in produce_glyphs. */
4384 saved_glyph_row = it->glyph_row;
4385 it->glyph_row = NULL;
4387 while (1)
4389 int x, i, ascent = 0, descent = 0;
4391 /* Stop when ZV or TO_CHARPOS reached. */
4392 if (!get_next_display_element (it)
4393 || ((op & MOVE_TO_POS) != 0
4394 && BUFFERP (it->object)
4395 && IT_CHARPOS (*it) >= to_charpos))
4397 result = MOVE_POS_MATCH_OR_ZV;
4398 break;
4401 /* The call to produce_glyphs will get the metrics of the
4402 display element IT is loaded with. We record in x the
4403 x-position before this display element in case it does not
4404 fit on the line. */
4405 x = it->current_x;
4407 /* Remember the line height so far in case the next element doesn't
4408 fit on the line. */
4409 if (!it->truncate_lines_p)
4411 ascent = it->max_ascent;
4412 descent = it->max_descent;
4415 PRODUCE_GLYPHS (it);
4417 if (it->area != TEXT_AREA)
4419 set_iterator_to_next (it, 1);
4420 continue;
4423 /* The number of glyphs we get back in IT->nglyphs will normally
4424 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4425 character on a terminal frame, or (iii) a line end. For the
4426 second case, IT->nglyphs - 1 padding glyphs will be present
4427 (on X frames, there is only one glyph produced for a
4428 composite character.
4430 The behavior implemented below means, for continuation lines,
4431 that as many spaces of a TAB as fit on the current line are
4432 displayed there. For terminal frames, as many glyphs of a
4433 multi-glyph character are displayed in the current line, too.
4434 This is what the old redisplay code did, and we keep it that
4435 way. Under X, the whole shape of a complex character must
4436 fit on the line or it will be completely displayed in the
4437 next line.
4439 Note that both for tabs and padding glyphs, all glyphs have
4440 the same width. */
4441 if (it->nglyphs)
4443 /* More than one glyph or glyph doesn't fit on line. All
4444 glyphs have the same width. */
4445 int single_glyph_width = it->pixel_width / it->nglyphs;
4446 int new_x;
4448 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4450 new_x = x + single_glyph_width;
4452 /* We want to leave anything reaching TO_X to the caller. */
4453 if ((op & MOVE_TO_X) && new_x > to_x)
4455 it->current_x = x;
4456 result = MOVE_X_REACHED;
4457 break;
4459 else if (/* Lines are continued. */
4460 !it->truncate_lines_p
4461 && (/* And glyph doesn't fit on the line. */
4462 new_x > it->last_visible_x
4463 /* Or it fits exactly and we're on a window
4464 system frame. */
4465 || (new_x == it->last_visible_x
4466 && FRAME_WINDOW_P (it->f))))
4468 if (/* IT->hpos == 0 means the very first glyph
4469 doesn't fit on the line, e.g. a wide image. */
4470 it->hpos == 0
4471 || (new_x == it->last_visible_x
4472 && FRAME_WINDOW_P (it->f)))
4474 ++it->hpos;
4475 it->current_x = new_x;
4476 if (i == it->nglyphs - 1)
4477 set_iterator_to_next (it, 1);
4479 else
4481 it->current_x = x;
4482 it->max_ascent = ascent;
4483 it->max_descent = descent;
4486 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4487 IT_CHARPOS (*it)));
4488 result = MOVE_LINE_CONTINUED;
4489 break;
4491 else if (new_x > it->first_visible_x)
4493 /* Glyph is visible. Increment number of glyphs that
4494 would be displayed. */
4495 ++it->hpos;
4497 else
4499 /* Glyph is completely off the left margin of the display
4500 area. Nothing to do. */
4504 if (result != MOVE_UNDEFINED)
4505 break;
4507 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4509 /* Stop when TO_X specified and reached. This check is
4510 necessary here because of lines consisting of a line end,
4511 only. The line end will not produce any glyphs and we
4512 would never get MOVE_X_REACHED. */
4513 xassert (it->nglyphs == 0);
4514 result = MOVE_X_REACHED;
4515 break;
4518 /* Is this a line end? If yes, we're done. */
4519 if (ITERATOR_AT_END_OF_LINE_P (it))
4521 result = MOVE_NEWLINE_OR_CR;
4522 break;
4525 /* The current display element has been consumed. Advance
4526 to the next. */
4527 set_iterator_to_next (it, 1);
4529 /* Stop if lines are truncated and IT's current x-position is
4530 past the right edge of the window now. */
4531 if (it->truncate_lines_p
4532 && it->current_x >= it->last_visible_x)
4534 result = MOVE_LINE_TRUNCATED;
4535 break;
4539 /* Restore the iterator settings altered at the beginning of this
4540 function. */
4541 it->glyph_row = saved_glyph_row;
4542 return result;
4546 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4547 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4548 the description of enum move_operation_enum.
4550 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4551 screen line, this function will set IT to the next position >
4552 TO_CHARPOS. */
4554 void
4555 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4556 struct it *it;
4557 int to_charpos, to_x, to_y, to_vpos;
4558 int op;
4560 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4561 int line_height;
4562 int reached = 0;
4564 for (;;)
4566 if (op & MOVE_TO_VPOS)
4568 /* If no TO_CHARPOS and no TO_X specified, stop at the
4569 start of the line TO_VPOS. */
4570 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4572 if (it->vpos == to_vpos)
4574 reached = 1;
4575 break;
4577 else
4578 skip = move_it_in_display_line_to (it, -1, -1, 0);
4580 else
4582 /* TO_VPOS >= 0 means stop at TO_X in the line at
4583 TO_VPOS, or at TO_POS, whichever comes first. */
4584 if (it->vpos == to_vpos)
4586 reached = 2;
4587 break;
4590 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4592 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4594 reached = 3;
4595 break;
4597 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4599 /* We have reached TO_X but not in the line we want. */
4600 skip = move_it_in_display_line_to (it, to_charpos,
4601 -1, MOVE_TO_POS);
4602 if (skip == MOVE_POS_MATCH_OR_ZV)
4604 reached = 4;
4605 break;
4610 else if (op & MOVE_TO_Y)
4612 struct it it_backup;
4614 /* TO_Y specified means stop at TO_X in the line containing
4615 TO_Y---or at TO_CHARPOS if this is reached first. The
4616 problem is that we can't really tell whether the line
4617 contains TO_Y before we have completely scanned it, and
4618 this may skip past TO_X. What we do is to first scan to
4619 TO_X.
4621 If TO_X is not specified, use a TO_X of zero. The reason
4622 is to make the outcome of this function more predictable.
4623 If we didn't use TO_X == 0, we would stop at the end of
4624 the line which is probably not what a caller would expect
4625 to happen. */
4626 skip = move_it_in_display_line_to (it, to_charpos,
4627 ((op & MOVE_TO_X)
4628 ? to_x : 0),
4629 (MOVE_TO_X
4630 | (op & MOVE_TO_POS)));
4632 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4633 if (skip == MOVE_POS_MATCH_OR_ZV)
4635 reached = 5;
4636 break;
4639 /* If TO_X was reached, we would like to know whether TO_Y
4640 is in the line. This can only be said if we know the
4641 total line height which requires us to scan the rest of
4642 the line. */
4643 if (skip == MOVE_X_REACHED)
4645 it_backup = *it;
4646 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4647 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4648 op & MOVE_TO_POS);
4649 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4652 /* Now, decide whether TO_Y is in this line. */
4653 line_height = it->max_ascent + it->max_descent;
4654 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4656 if (to_y >= it->current_y
4657 && to_y < it->current_y + line_height)
4659 if (skip == MOVE_X_REACHED)
4660 /* If TO_Y is in this line and TO_X was reached above,
4661 we scanned too far. We have to restore IT's settings
4662 to the ones before skipping. */
4663 *it = it_backup;
4664 reached = 6;
4666 else if (skip == MOVE_X_REACHED)
4668 skip = skip2;
4669 if (skip == MOVE_POS_MATCH_OR_ZV)
4670 reached = 7;
4673 if (reached)
4674 break;
4676 else
4677 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4679 switch (skip)
4681 case MOVE_POS_MATCH_OR_ZV:
4682 reached = 8;
4683 goto out;
4685 case MOVE_NEWLINE_OR_CR:
4686 set_iterator_to_next (it, 1);
4687 it->continuation_lines_width = 0;
4688 break;
4690 case MOVE_LINE_TRUNCATED:
4691 it->continuation_lines_width = 0;
4692 reseat_at_next_visible_line_start (it, 0);
4693 if ((op & MOVE_TO_POS) != 0
4694 && IT_CHARPOS (*it) > to_charpos)
4696 reached = 9;
4697 goto out;
4699 break;
4701 case MOVE_LINE_CONTINUED:
4702 it->continuation_lines_width += it->current_x;
4703 break;
4705 default:
4706 abort ();
4709 /* Reset/increment for the next run. */
4710 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4711 it->current_x = it->hpos = 0;
4712 it->current_y += it->max_ascent + it->max_descent;
4713 ++it->vpos;
4714 last_height = it->max_ascent + it->max_descent;
4715 last_max_ascent = it->max_ascent;
4716 it->max_ascent = it->max_descent = 0;
4719 out:
4721 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4725 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4727 If DY > 0, move IT backward at least that many pixels. DY = 0
4728 means move IT backward to the preceding line start or BEGV. This
4729 function may move over more than DY pixels if IT->current_y - DY
4730 ends up in the middle of a line; in this case IT->current_y will be
4731 set to the top of the line moved to. */
4733 void
4734 move_it_vertically_backward (it, dy)
4735 struct it *it;
4736 int dy;
4738 int nlines, h, line_height;
4739 struct it it2;
4740 int start_pos = IT_CHARPOS (*it);
4742 xassert (dy >= 0);
4744 /* Estimate how many newlines we must move back. */
4745 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4747 /* Set the iterator's position that many lines back. */
4748 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4749 back_to_previous_visible_line_start (it);
4751 /* Reseat the iterator here. When moving backward, we don't want
4752 reseat to skip forward over invisible text, set up the iterator
4753 to deliver from overlay strings at the new position etc. So,
4754 use reseat_1 here. */
4755 reseat_1 (it, it->current.pos, 1);
4757 /* We are now surely at a line start. */
4758 it->current_x = it->hpos = 0;
4760 /* Move forward and see what y-distance we moved. First move to the
4761 start of the next line so that we get its height. We need this
4762 height to be able to tell whether we reached the specified
4763 y-distance. */
4764 it2 = *it;
4765 it2.max_ascent = it2.max_descent = 0;
4766 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4767 MOVE_TO_POS | MOVE_TO_VPOS);
4768 xassert (IT_CHARPOS (*it) >= BEGV);
4769 line_height = it2.max_ascent + it2.max_descent;
4771 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4772 xassert (IT_CHARPOS (*it) >= BEGV);
4773 h = it2.current_y - it->current_y;
4774 nlines = it2.vpos - it->vpos;
4776 /* Correct IT's y and vpos position. */
4777 it->vpos -= nlines;
4778 it->current_y -= h;
4780 if (dy == 0)
4782 /* DY == 0 means move to the start of the screen line. The
4783 value of nlines is > 0 if continuation lines were involved. */
4784 if (nlines > 0)
4785 move_it_by_lines (it, nlines, 1);
4786 xassert (IT_CHARPOS (*it) <= start_pos);
4788 else if (nlines)
4790 /* The y-position we try to reach. Note that h has been
4791 subtracted in front of the if-statement. */
4792 int target_y = it->current_y + h - dy;
4794 /* If we did not reach target_y, try to move further backward if
4795 we can. If we moved too far backward, try to move forward. */
4796 if (target_y < it->current_y
4797 && IT_CHARPOS (*it) > BEGV)
4799 move_it_vertically (it, target_y - it->current_y);
4800 xassert (IT_CHARPOS (*it) >= BEGV);
4802 else if (target_y >= it->current_y + line_height
4803 && IT_CHARPOS (*it) < ZV)
4805 move_it_vertically (it, target_y - (it->current_y + line_height));
4806 xassert (IT_CHARPOS (*it) >= BEGV);
4812 /* Move IT by a specified amount of pixel lines DY. DY negative means
4813 move backwards. DY = 0 means move to start of screen line. At the
4814 end, IT will be on the start of a screen line. */
4816 void
4817 move_it_vertically (it, dy)
4818 struct it *it;
4819 int dy;
4821 if (dy <= 0)
4822 move_it_vertically_backward (it, -dy);
4823 else if (dy > 0)
4825 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4826 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4827 MOVE_TO_POS | MOVE_TO_Y);
4828 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4830 /* If buffer ends in ZV without a newline, move to the start of
4831 the line to satisfy the post-condition. */
4832 if (IT_CHARPOS (*it) == ZV
4833 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4834 move_it_by_lines (it, 0, 0);
4839 /* Return non-zero if some text between buffer positions START_CHARPOS
4840 and END_CHARPOS is invisible. IT->window is the window for text
4841 property lookup. */
4843 static int
4844 invisible_text_between_p (it, start_charpos, end_charpos)
4845 struct it *it;
4846 int start_charpos, end_charpos;
4848 Lisp_Object prop, limit;
4849 int invisible_found_p;
4851 xassert (it != NULL && start_charpos <= end_charpos);
4853 /* Is text at START invisible? */
4854 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4855 it->window);
4856 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4857 invisible_found_p = 1;
4858 else
4860 limit = Fnext_single_char_property_change (make_number (start_charpos),
4861 Qinvisible, Qnil,
4862 make_number (end_charpos));
4863 invisible_found_p = XFASTINT (limit) < end_charpos;
4866 return invisible_found_p;
4870 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4871 negative means move up. DVPOS == 0 means move to the start of the
4872 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4873 NEED_Y_P is zero, IT->current_y will be left unchanged.
4875 Further optimization ideas: If we would know that IT->f doesn't use
4876 a face with proportional font, we could be faster for
4877 truncate-lines nil. */
4879 void
4880 move_it_by_lines (it, dvpos, need_y_p)
4881 struct it *it;
4882 int dvpos, need_y_p;
4884 struct position pos;
4886 if (!FRAME_WINDOW_P (it->f))
4888 struct text_pos textpos;
4890 /* We can use vmotion on frames without proportional fonts. */
4891 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4892 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4893 reseat (it, textpos, 1);
4894 it->vpos += pos.vpos;
4895 it->current_y += pos.vpos;
4897 else if (dvpos == 0)
4899 /* DVPOS == 0 means move to the start of the screen line. */
4900 move_it_vertically_backward (it, 0);
4901 xassert (it->current_x == 0 && it->hpos == 0);
4903 else if (dvpos > 0)
4905 /* If there are no continuation lines, and if there is no
4906 selective display, try the simple method of moving forward
4907 DVPOS newlines, then see where we are. */
4908 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4910 int shortage = 0, charpos;
4912 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4913 charpos = IT_CHARPOS (*it) + 1;
4914 else
4915 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4916 &shortage, 0);
4918 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4920 struct text_pos pos;
4921 CHARPOS (pos) = charpos;
4922 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4923 reseat (it, pos, 1);
4924 it->vpos += dvpos - shortage;
4925 it->hpos = it->current_x = 0;
4926 return;
4930 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4932 else
4934 struct it it2;
4935 int start_charpos, i;
4937 /* If there are no continuation lines, and if there is no
4938 selective display, try the simple method of moving backward
4939 -DVPOS newlines. */
4940 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4942 int shortage;
4943 int charpos = IT_CHARPOS (*it);
4944 int bytepos = IT_BYTEPOS (*it);
4946 /* If in the middle of a line, go to its start. */
4947 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4949 charpos = find_next_newline_no_quit (charpos, -1);
4950 bytepos = CHAR_TO_BYTE (charpos);
4953 if (charpos == BEGV)
4955 struct text_pos pos;
4956 CHARPOS (pos) = charpos;
4957 BYTEPOS (pos) = bytepos;
4958 reseat (it, pos, 1);
4959 it->hpos = it->current_x = 0;
4960 return;
4962 else
4964 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4965 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4967 struct text_pos pos;
4968 CHARPOS (pos) = charpos;
4969 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4970 reseat (it, pos, 1);
4971 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4972 it->hpos = it->current_x = 0;
4973 return;
4978 /* Go back -DVPOS visible lines and reseat the iterator there. */
4979 start_charpos = IT_CHARPOS (*it);
4980 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4981 back_to_previous_visible_line_start (it);
4982 reseat (it, it->current.pos, 1);
4983 it->current_x = it->hpos = 0;
4985 /* Above call may have moved too far if continuation lines
4986 are involved. Scan forward and see if it did. */
4987 it2 = *it;
4988 it2.vpos = it2.current_y = 0;
4989 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4990 it->vpos -= it2.vpos;
4991 it->current_y -= it2.current_y;
4992 it->current_x = it->hpos = 0;
4994 /* If we moved too far, move IT some lines forward. */
4995 if (it2.vpos > -dvpos)
4997 int delta = it2.vpos + dvpos;
4998 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5005 /***********************************************************************
5006 Messages
5007 ***********************************************************************/
5010 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5011 to *Messages*. */
5013 void
5014 add_to_log (format, arg1, arg2)
5015 char *format;
5016 Lisp_Object arg1, arg2;
5018 Lisp_Object args[3];
5019 Lisp_Object msg, fmt;
5020 char *buffer;
5021 int len;
5022 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5024 fmt = msg = Qnil;
5025 GCPRO4 (fmt, msg, arg1, arg2);
5027 args[0] = fmt = build_string (format);
5028 args[1] = arg1;
5029 args[2] = arg2;
5030 msg = Fformat (3, args);
5032 len = STRING_BYTES (XSTRING (msg)) + 1;
5033 buffer = (char *) alloca (len);
5034 strcpy (buffer, XSTRING (msg)->data);
5036 message_dolog (buffer, len - 1, 1, 0);
5037 UNGCPRO;
5041 /* Output a newline in the *Messages* buffer if "needs" one. */
5043 void
5044 message_log_maybe_newline ()
5046 if (message_log_need_newline)
5047 message_dolog ("", 0, 1, 0);
5051 /* Add a string M of length LEN to the message log, optionally
5052 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5053 nonzero, means interpret the contents of M as multibyte. This
5054 function calls low-level routines in order to bypass text property
5055 hooks, etc. which might not be safe to run. */
5057 void
5058 message_dolog (m, len, nlflag, multibyte)
5059 char *m;
5060 int len, nlflag, multibyte;
5062 if (!NILP (Vmessage_log_max))
5064 struct buffer *oldbuf;
5065 Lisp_Object oldpoint, oldbegv, oldzv;
5066 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5067 int point_at_end = 0;
5068 int zv_at_end = 0;
5069 Lisp_Object old_deactivate_mark, tem;
5070 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5072 old_deactivate_mark = Vdeactivate_mark;
5073 oldbuf = current_buffer;
5074 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5075 current_buffer->undo_list = Qt;
5077 oldpoint = Fpoint_marker ();
5078 oldbegv = Fpoint_min_marker ();
5079 oldzv = Fpoint_max_marker ();
5080 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5082 if (PT == Z)
5083 point_at_end = 1;
5084 if (ZV == Z)
5085 zv_at_end = 1;
5087 BEGV = BEG;
5088 BEGV_BYTE = BEG_BYTE;
5089 ZV = Z;
5090 ZV_BYTE = Z_BYTE;
5091 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5093 /* Insert the string--maybe converting multibyte to single byte
5094 or vice versa, so that all the text fits the buffer. */
5095 if (multibyte
5096 && NILP (current_buffer->enable_multibyte_characters))
5098 int i, c, nbytes;
5099 unsigned char work[1];
5101 /* Convert a multibyte string to single-byte
5102 for the *Message* buffer. */
5103 for (i = 0; i < len; i += nbytes)
5105 c = string_char_and_length (m + i, len - i, &nbytes);
5106 work[0] = (SINGLE_BYTE_CHAR_P (c)
5108 : multibyte_char_to_unibyte (c, Qnil));
5109 insert_1_both (work, 1, 1, 1, 0, 0);
5112 else if (! multibyte
5113 && ! NILP (current_buffer->enable_multibyte_characters))
5115 int i, c, nbytes;
5116 unsigned char *msg = (unsigned char *) m;
5117 unsigned char str[MAX_MULTIBYTE_LENGTH];
5118 /* Convert a single-byte string to multibyte
5119 for the *Message* buffer. */
5120 for (i = 0; i < len; i++)
5122 c = unibyte_char_to_multibyte (msg[i]);
5123 nbytes = CHAR_STRING (c, str);
5124 insert_1_both (str, 1, nbytes, 1, 0, 0);
5127 else if (len)
5128 insert_1 (m, len, 1, 0, 0);
5130 if (nlflag)
5132 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5133 insert_1 ("\n", 1, 1, 0, 0);
5135 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5136 this_bol = PT;
5137 this_bol_byte = PT_BYTE;
5139 if (this_bol > BEG)
5141 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5142 prev_bol = PT;
5143 prev_bol_byte = PT_BYTE;
5145 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5146 this_bol, this_bol_byte);
5147 if (dup)
5149 del_range_both (prev_bol, prev_bol_byte,
5150 this_bol, this_bol_byte, 0);
5151 if (dup > 1)
5153 char dupstr[40];
5154 int duplen;
5156 /* If you change this format, don't forget to also
5157 change message_log_check_duplicate. */
5158 sprintf (dupstr, " [%d times]", dup);
5159 duplen = strlen (dupstr);
5160 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5161 insert_1 (dupstr, duplen, 1, 0, 1);
5166 if (NATNUMP (Vmessage_log_max))
5168 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5169 -XFASTINT (Vmessage_log_max) - 1, 0);
5170 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5173 BEGV = XMARKER (oldbegv)->charpos;
5174 BEGV_BYTE = marker_byte_position (oldbegv);
5176 if (zv_at_end)
5178 ZV = Z;
5179 ZV_BYTE = Z_BYTE;
5181 else
5183 ZV = XMARKER (oldzv)->charpos;
5184 ZV_BYTE = marker_byte_position (oldzv);
5187 if (point_at_end)
5188 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5189 else
5190 /* We can't do Fgoto_char (oldpoint) because it will run some
5191 Lisp code. */
5192 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5193 XMARKER (oldpoint)->bytepos);
5195 UNGCPRO;
5196 free_marker (oldpoint);
5197 free_marker (oldbegv);
5198 free_marker (oldzv);
5200 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5201 set_buffer_internal (oldbuf);
5202 if (NILP (tem))
5203 windows_or_buffers_changed = old_windows_or_buffers_changed;
5204 message_log_need_newline = !nlflag;
5205 Vdeactivate_mark = old_deactivate_mark;
5210 /* We are at the end of the buffer after just having inserted a newline.
5211 (Note: We depend on the fact we won't be crossing the gap.)
5212 Check to see if the most recent message looks a lot like the previous one.
5213 Return 0 if different, 1 if the new one should just replace it, or a
5214 value N > 1 if we should also append " [N times]". */
5216 static int
5217 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5218 int prev_bol, this_bol;
5219 int prev_bol_byte, this_bol_byte;
5221 int i;
5222 int len = Z_BYTE - 1 - this_bol_byte;
5223 int seen_dots = 0;
5224 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5225 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5227 for (i = 0; i < len; i++)
5229 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5230 && p1[i] != '\n')
5231 seen_dots = 1;
5232 if (p1[i] != p2[i])
5233 return seen_dots;
5235 p1 += len;
5236 if (*p1 == '\n')
5237 return 2;
5238 if (*p1++ == ' ' && *p1++ == '[')
5240 int n = 0;
5241 while (*p1 >= '0' && *p1 <= '9')
5242 n = n * 10 + *p1++ - '0';
5243 if (strncmp (p1, " times]\n", 8) == 0)
5244 return n+1;
5246 return 0;
5250 /* Display an echo area message M with a specified length of LEN
5251 chars. The string may include null characters. If M is 0, clear
5252 out any existing message, and let the mini-buffer text show through.
5254 The buffer M must continue to exist until after the echo area gets
5255 cleared or some other message gets displayed there. This means do
5256 not pass text that is stored in a Lisp string; do not pass text in
5257 a buffer that was alloca'd. */
5259 void
5260 message2 (m, len, multibyte)
5261 char *m;
5262 int len;
5263 int multibyte;
5265 /* First flush out any partial line written with print. */
5266 message_log_maybe_newline ();
5267 if (m)
5268 message_dolog (m, len, 1, multibyte);
5269 message2_nolog (m, len, multibyte);
5273 /* The non-logging counterpart of message2. */
5275 void
5276 message2_nolog (m, len, multibyte)
5277 char *m;
5278 int len;
5280 struct frame *sf = SELECTED_FRAME ();
5281 message_enable_multibyte = multibyte;
5283 if (noninteractive)
5285 if (noninteractive_need_newline)
5286 putc ('\n', stderr);
5287 noninteractive_need_newline = 0;
5288 if (m)
5289 fwrite (m, len, 1, stderr);
5290 if (cursor_in_echo_area == 0)
5291 fprintf (stderr, "\n");
5292 fflush (stderr);
5294 /* A null message buffer means that the frame hasn't really been
5295 initialized yet. Error messages get reported properly by
5296 cmd_error, so this must be just an informative message; toss it. */
5297 else if (INTERACTIVE
5298 && sf->glyphs_initialized_p
5299 && FRAME_MESSAGE_BUF (sf))
5301 Lisp_Object mini_window;
5302 struct frame *f;
5304 /* Get the frame containing the mini-buffer
5305 that the selected frame is using. */
5306 mini_window = FRAME_MINIBUF_WINDOW (sf);
5307 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5309 FRAME_SAMPLE_VISIBILITY (f);
5310 if (FRAME_VISIBLE_P (sf)
5311 && ! FRAME_VISIBLE_P (f))
5312 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5314 if (m)
5316 set_message (m, Qnil, len, multibyte);
5317 if (minibuffer_auto_raise)
5318 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5320 else
5321 clear_message (1, 1);
5323 do_pending_window_change (0);
5324 echo_area_display (1);
5325 do_pending_window_change (0);
5326 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5327 (*frame_up_to_date_hook) (f);
5332 /* Display an echo area message M with a specified length of NBYTES
5333 bytes. The string may include null characters. If M is not a
5334 string, clear out any existing message, and let the mini-buffer
5335 text show through. */
5337 void
5338 message3 (m, nbytes, multibyte)
5339 Lisp_Object m;
5340 int nbytes;
5341 int multibyte;
5343 struct gcpro gcpro1;
5345 GCPRO1 (m);
5347 /* First flush out any partial line written with print. */
5348 message_log_maybe_newline ();
5349 if (STRINGP (m))
5350 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5351 message3_nolog (m, nbytes, multibyte);
5353 UNGCPRO;
5357 /* The non-logging version of message3. */
5359 void
5360 message3_nolog (m, nbytes, multibyte)
5361 Lisp_Object m;
5362 int nbytes, multibyte;
5364 struct frame *sf = SELECTED_FRAME ();
5365 message_enable_multibyte = multibyte;
5367 if (noninteractive)
5369 if (noninteractive_need_newline)
5370 putc ('\n', stderr);
5371 noninteractive_need_newline = 0;
5372 if (STRINGP (m))
5373 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5374 if (cursor_in_echo_area == 0)
5375 fprintf (stderr, "\n");
5376 fflush (stderr);
5378 /* A null message buffer means that the frame hasn't really been
5379 initialized yet. Error messages get reported properly by
5380 cmd_error, so this must be just an informative message; toss it. */
5381 else if (INTERACTIVE
5382 && sf->glyphs_initialized_p
5383 && FRAME_MESSAGE_BUF (sf))
5385 Lisp_Object mini_window;
5386 Lisp_Object frame;
5387 struct frame *f;
5389 /* Get the frame containing the mini-buffer
5390 that the selected frame is using. */
5391 mini_window = FRAME_MINIBUF_WINDOW (sf);
5392 frame = XWINDOW (mini_window)->frame;
5393 f = XFRAME (frame);
5395 FRAME_SAMPLE_VISIBILITY (f);
5396 if (FRAME_VISIBLE_P (sf)
5397 && !FRAME_VISIBLE_P (f))
5398 Fmake_frame_visible (frame);
5400 if (STRINGP (m) && XSTRING (m)->size)
5402 set_message (NULL, m, nbytes, multibyte);
5403 if (minibuffer_auto_raise)
5404 Fraise_frame (frame);
5406 else
5407 clear_message (1, 1);
5409 do_pending_window_change (0);
5410 echo_area_display (1);
5411 do_pending_window_change (0);
5412 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5413 (*frame_up_to_date_hook) (f);
5418 /* Display a null-terminated echo area message M. If M is 0, clear
5419 out any existing message, and let the mini-buffer text show through.
5421 The buffer M must continue to exist until after the echo area gets
5422 cleared or some other message gets displayed there. Do not pass
5423 text that is stored in a Lisp string. Do not pass text in a buffer
5424 that was alloca'd. */
5426 void
5427 message1 (m)
5428 char *m;
5430 message2 (m, (m ? strlen (m) : 0), 0);
5434 /* The non-logging counterpart of message1. */
5436 void
5437 message1_nolog (m)
5438 char *m;
5440 message2_nolog (m, (m ? strlen (m) : 0), 0);
5443 /* Display a message M which contains a single %s
5444 which gets replaced with STRING. */
5446 void
5447 message_with_string (m, string, log)
5448 char *m;
5449 Lisp_Object string;
5450 int log;
5452 if (noninteractive)
5454 if (m)
5456 if (noninteractive_need_newline)
5457 putc ('\n', stderr);
5458 noninteractive_need_newline = 0;
5459 fprintf (stderr, m, XSTRING (string)->data);
5460 if (cursor_in_echo_area == 0)
5461 fprintf (stderr, "\n");
5462 fflush (stderr);
5465 else if (INTERACTIVE)
5467 /* The frame whose minibuffer we're going to display the message on.
5468 It may be larger than the selected frame, so we need
5469 to use its buffer, not the selected frame's buffer. */
5470 Lisp_Object mini_window;
5471 struct frame *f, *sf = SELECTED_FRAME ();
5473 /* Get the frame containing the minibuffer
5474 that the selected frame is using. */
5475 mini_window = FRAME_MINIBUF_WINDOW (sf);
5476 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5478 /* A null message buffer means that the frame hasn't really been
5479 initialized yet. Error messages get reported properly by
5480 cmd_error, so this must be just an informative message; toss it. */
5481 if (FRAME_MESSAGE_BUF (f))
5483 int len;
5484 char *a[1];
5485 a[0] = (char *) XSTRING (string)->data;
5487 len = doprnt (FRAME_MESSAGE_BUF (f),
5488 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5490 if (log)
5491 message2 (FRAME_MESSAGE_BUF (f), len,
5492 STRING_MULTIBYTE (string));
5493 else
5494 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5495 STRING_MULTIBYTE (string));
5497 /* Print should start at the beginning of the message
5498 buffer next time. */
5499 message_buf_print = 0;
5505 /* Dump an informative message to the minibuf. If M is 0, clear out
5506 any existing message, and let the mini-buffer text show through. */
5508 /* VARARGS 1 */
5509 void
5510 message (m, a1, a2, a3)
5511 char *m;
5512 EMACS_INT a1, a2, a3;
5514 if (noninteractive)
5516 if (m)
5518 if (noninteractive_need_newline)
5519 putc ('\n', stderr);
5520 noninteractive_need_newline = 0;
5521 fprintf (stderr, m, a1, a2, a3);
5522 if (cursor_in_echo_area == 0)
5523 fprintf (stderr, "\n");
5524 fflush (stderr);
5527 else if (INTERACTIVE)
5529 /* The frame whose mini-buffer we're going to display the message
5530 on. It may be larger than the selected frame, so we need to
5531 use its buffer, not the selected frame's buffer. */
5532 Lisp_Object mini_window;
5533 struct frame *f, *sf = SELECTED_FRAME ();
5535 /* Get the frame containing the mini-buffer
5536 that the selected frame is using. */
5537 mini_window = FRAME_MINIBUF_WINDOW (sf);
5538 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5540 /* A null message buffer means that the frame hasn't really been
5541 initialized yet. Error messages get reported properly by
5542 cmd_error, so this must be just an informative message; toss
5543 it. */
5544 if (FRAME_MESSAGE_BUF (f))
5546 if (m)
5548 int len;
5549 #ifdef NO_ARG_ARRAY
5550 char *a[3];
5551 a[0] = (char *) a1;
5552 a[1] = (char *) a2;
5553 a[2] = (char *) a3;
5555 len = doprnt (FRAME_MESSAGE_BUF (f),
5556 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5557 #else
5558 len = doprnt (FRAME_MESSAGE_BUF (f),
5559 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5560 (char **) &a1);
5561 #endif /* NO_ARG_ARRAY */
5563 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5565 else
5566 message1 (0);
5568 /* Print should start at the beginning of the message
5569 buffer next time. */
5570 message_buf_print = 0;
5576 /* The non-logging version of message. */
5578 void
5579 message_nolog (m, a1, a2, a3)
5580 char *m;
5581 EMACS_INT a1, a2, a3;
5583 Lisp_Object old_log_max;
5584 old_log_max = Vmessage_log_max;
5585 Vmessage_log_max = Qnil;
5586 message (m, a1, a2, a3);
5587 Vmessage_log_max = old_log_max;
5591 /* Display the current message in the current mini-buffer. This is
5592 only called from error handlers in process.c, and is not time
5593 critical. */
5595 void
5596 update_echo_area ()
5598 if (!NILP (echo_area_buffer[0]))
5600 Lisp_Object string;
5601 string = Fcurrent_message ();
5602 message3 (string, XSTRING (string)->size,
5603 !NILP (current_buffer->enable_multibyte_characters));
5608 /* Make sure echo area buffers in echo_buffers[] are life. If they
5609 aren't, make new ones. */
5611 static void
5612 ensure_echo_area_buffers ()
5614 int i;
5616 for (i = 0; i < 2; ++i)
5617 if (!BUFFERP (echo_buffer[i])
5618 || NILP (XBUFFER (echo_buffer[i])->name))
5620 char name[30];
5621 Lisp_Object old_buffer;
5622 int j;
5624 old_buffer = echo_buffer[i];
5625 sprintf (name, " *Echo Area %d*", i);
5626 echo_buffer[i] = Fget_buffer_create (build_string (name));
5627 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5629 for (j = 0; j < 2; ++j)
5630 if (EQ (old_buffer, echo_area_buffer[j]))
5631 echo_area_buffer[j] = echo_buffer[i];
5636 /* Call FN with args A1..A4 with either the current or last displayed
5637 echo_area_buffer as current buffer.
5639 WHICH zero means use the current message buffer
5640 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5641 from echo_buffer[] and clear it.
5643 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5644 suitable buffer from echo_buffer[] and clear it.
5646 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5647 that the current message becomes the last displayed one, make
5648 choose a suitable buffer for echo_area_buffer[0], and clear it.
5650 Value is what FN returns. */
5652 static int
5653 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5654 struct window *w;
5655 int which;
5656 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5657 EMACS_INT a1;
5658 Lisp_Object a2;
5659 EMACS_INT a3, a4;
5661 Lisp_Object buffer;
5662 int this_one, the_other, clear_buffer_p, rc;
5663 int count = specpdl_ptr - specpdl;
5665 /* If buffers aren't life, make new ones. */
5666 ensure_echo_area_buffers ();
5668 clear_buffer_p = 0;
5670 if (which == 0)
5671 this_one = 0, the_other = 1;
5672 else if (which > 0)
5673 this_one = 1, the_other = 0;
5674 else
5676 this_one = 0, the_other = 1;
5677 clear_buffer_p = 1;
5679 /* We need a fresh one in case the current echo buffer equals
5680 the one containing the last displayed echo area message. */
5681 if (!NILP (echo_area_buffer[this_one])
5682 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5683 echo_area_buffer[this_one] = Qnil;
5686 /* Choose a suitable buffer from echo_buffer[] is we don't
5687 have one. */
5688 if (NILP (echo_area_buffer[this_one]))
5690 echo_area_buffer[this_one]
5691 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5692 ? echo_buffer[the_other]
5693 : echo_buffer[this_one]);
5694 clear_buffer_p = 1;
5697 buffer = echo_area_buffer[this_one];
5699 record_unwind_protect (unwind_with_echo_area_buffer,
5700 with_echo_area_buffer_unwind_data (w));
5702 /* Make the echo area buffer current. Note that for display
5703 purposes, it is not necessary that the displayed window's buffer
5704 == current_buffer, except for text property lookup. So, let's
5705 only set that buffer temporarily here without doing a full
5706 Fset_window_buffer. We must also change w->pointm, though,
5707 because otherwise an assertions in unshow_buffer fails, and Emacs
5708 aborts. */
5709 set_buffer_internal_1 (XBUFFER (buffer));
5710 if (w)
5712 w->buffer = buffer;
5713 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5716 current_buffer->undo_list = Qt;
5717 current_buffer->read_only = Qnil;
5719 if (clear_buffer_p && Z > BEG)
5720 del_range (BEG, Z);
5722 xassert (BEGV >= BEG);
5723 xassert (ZV <= Z && ZV >= BEGV);
5725 rc = fn (a1, a2, a3, a4);
5727 xassert (BEGV >= BEG);
5728 xassert (ZV <= Z && ZV >= BEGV);
5730 unbind_to (count, Qnil);
5731 return rc;
5735 /* Save state that should be preserved around the call to the function
5736 FN called in with_echo_area_buffer. */
5738 static Lisp_Object
5739 with_echo_area_buffer_unwind_data (w)
5740 struct window *w;
5742 int i = 0;
5743 Lisp_Object vector;
5745 /* Reduce consing by keeping one vector in
5746 Vwith_echo_area_save_vector. */
5747 vector = Vwith_echo_area_save_vector;
5748 Vwith_echo_area_save_vector = Qnil;
5750 if (NILP (vector))
5751 vector = Fmake_vector (make_number (7), Qnil);
5753 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5754 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5755 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5757 if (w)
5759 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5760 XVECTOR (vector)->contents[i++] = w->buffer;
5761 XVECTOR (vector)->contents[i++]
5762 = make_number (XMARKER (w->pointm)->charpos);
5763 XVECTOR (vector)->contents[i++]
5764 = make_number (XMARKER (w->pointm)->bytepos);
5766 else
5768 int end = i + 4;
5769 while (i < end)
5770 XVECTOR (vector)->contents[i++] = Qnil;
5773 xassert (i == XVECTOR (vector)->size);
5774 return vector;
5778 /* Restore global state from VECTOR which was created by
5779 with_echo_area_buffer_unwind_data. */
5781 static Lisp_Object
5782 unwind_with_echo_area_buffer (vector)
5783 Lisp_Object vector;
5785 int i = 0;
5787 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5788 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5789 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5791 if (WINDOWP (XVECTOR (vector)->contents[i]))
5793 struct window *w;
5794 Lisp_Object buffer, charpos, bytepos;
5796 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5797 buffer = XVECTOR (vector)->contents[i]; ++i;
5798 charpos = XVECTOR (vector)->contents[i]; ++i;
5799 bytepos = XVECTOR (vector)->contents[i]; ++i;
5801 w->buffer = buffer;
5802 set_marker_both (w->pointm, buffer,
5803 XFASTINT (charpos), XFASTINT (bytepos));
5806 Vwith_echo_area_save_vector = vector;
5807 return Qnil;
5811 /* Set up the echo area for use by print functions. MULTIBYTE_P
5812 non-zero means we will print multibyte. */
5814 void
5815 setup_echo_area_for_printing (multibyte_p)
5816 int multibyte_p;
5818 ensure_echo_area_buffers ();
5820 if (!message_buf_print)
5822 /* A message has been output since the last time we printed.
5823 Choose a fresh echo area buffer. */
5824 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5825 echo_area_buffer[0] = echo_buffer[1];
5826 else
5827 echo_area_buffer[0] = echo_buffer[0];
5829 /* Switch to that buffer and clear it. */
5830 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5831 if (Z > BEG)
5832 del_range (BEG, Z);
5833 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5835 /* Set up the buffer for the multibyteness we need. */
5836 if (multibyte_p
5837 != !NILP (current_buffer->enable_multibyte_characters))
5838 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5840 /* Raise the frame containing the echo area. */
5841 if (minibuffer_auto_raise)
5843 struct frame *sf = SELECTED_FRAME ();
5844 Lisp_Object mini_window;
5845 mini_window = FRAME_MINIBUF_WINDOW (sf);
5846 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5849 message_log_maybe_newline ();
5850 message_buf_print = 1;
5852 else
5854 if (NILP (echo_area_buffer[0]))
5856 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5857 echo_area_buffer[0] = echo_buffer[1];
5858 else
5859 echo_area_buffer[0] = echo_buffer[0];
5862 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5863 /* Someone switched buffers between print requests. */
5864 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5869 /* Display an echo area message in window W. Value is non-zero if W's
5870 height is changed. If display_last_displayed_message_p is
5871 non-zero, display the message that was last displayed, otherwise
5872 display the current message. */
5874 static int
5875 display_echo_area (w)
5876 struct window *w;
5878 int i, no_message_p, window_height_changed_p, count;
5880 /* Temporarily disable garbage collections while displaying the echo
5881 area. This is done because a GC can print a message itself.
5882 That message would modify the echo area buffer's contents while a
5883 redisplay of the buffer is going on, and seriously confuse
5884 redisplay. */
5885 count = inhibit_garbage_collection ();
5887 /* If there is no message, we must call display_echo_area_1
5888 nevertheless because it resizes the window. But we will have to
5889 reset the echo_area_buffer in question to nil at the end because
5890 with_echo_area_buffer will sets it to an empty buffer. */
5891 i = display_last_displayed_message_p ? 1 : 0;
5892 no_message_p = NILP (echo_area_buffer[i]);
5894 window_height_changed_p
5895 = with_echo_area_buffer (w, display_last_displayed_message_p,
5896 display_echo_area_1,
5897 (EMACS_INT) w, Qnil, 0, 0);
5899 if (no_message_p)
5900 echo_area_buffer[i] = Qnil;
5902 unbind_to (count, Qnil);
5903 return window_height_changed_p;
5907 /* Helper for display_echo_area. Display the current buffer which
5908 contains the current echo area message in window W, a mini-window,
5909 a pointer to which is passed in A1. A2..A4 are currently not used.
5910 Change the height of W so that all of the message is displayed.
5911 Value is non-zero if height of W was changed. */
5913 static int
5914 display_echo_area_1 (a1, a2, a3, a4)
5915 EMACS_INT a1;
5916 Lisp_Object a2;
5917 EMACS_INT a3, a4;
5919 struct window *w = (struct window *) a1;
5920 Lisp_Object window;
5921 struct text_pos start;
5922 int window_height_changed_p = 0;
5924 /* Do this before displaying, so that we have a large enough glyph
5925 matrix for the display. */
5926 window_height_changed_p = resize_mini_window (w, 0);
5928 /* Display. */
5929 clear_glyph_matrix (w->desired_matrix);
5930 XSETWINDOW (window, w);
5931 SET_TEXT_POS (start, BEG, BEG_BYTE);
5932 try_window (window, start);
5934 return window_height_changed_p;
5938 /* Resize the echo area window to exactly the size needed for the
5939 currently displayed message, if there is one. */
5941 void
5942 resize_echo_area_axactly ()
5944 if (BUFFERP (echo_area_buffer[0])
5945 && WINDOWP (echo_area_window))
5947 struct window *w = XWINDOW (echo_area_window);
5948 int resized_p;
5950 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
5951 (EMACS_INT) w, Qnil, 0, 0);
5952 if (resized_p)
5954 ++windows_or_buffers_changed;
5955 ++update_mode_lines;
5956 redisplay_internal (0);
5962 /* Callback function for with_echo_area_buffer, when used from
5963 resize_echo_area_axactly. A1 contains a pointer to the window to
5964 resize, A2 to A4 are not used. Value is what resize_mini_window
5965 returns. */
5967 static int
5968 resize_mini_window_1 (a1, a2, a3, a4)
5969 EMACS_INT a1;
5970 Lisp_Object a2;
5971 EMACS_INT a3, a4;
5973 return resize_mini_window ((struct window *) a1, 1);
5977 /* Resize mini-window W to fit the size of its contents. EXACT:P
5978 means size the window exactly to the size needed. Otherwise, it's
5979 only enlarged until W's buffer is empty. Value is non-zero if
5980 the window height has been changed. */
5983 resize_mini_window (w, exact_p)
5984 struct window *w;
5985 int exact_p;
5987 struct frame *f = XFRAME (w->frame);
5988 int window_height_changed_p = 0;
5990 xassert (MINI_WINDOW_P (w));
5992 /* Nil means don't try to resize. */
5993 if (NILP (Vmax_mini_window_height)
5994 || (FRAME_X_P (f) && f->output_data.x == NULL))
5995 return 0;
5997 if (!FRAME_MINIBUF_ONLY_P (f))
5999 struct it it;
6000 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6001 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6002 int height, max_height;
6003 int unit = CANON_Y_UNIT (f);
6004 struct text_pos start;
6006 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6008 /* Compute the max. number of lines specified by the user. */
6009 if (FLOATP (Vmax_mini_window_height))
6010 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6011 else if (INTEGERP (Vmax_mini_window_height))
6012 max_height = XINT (Vmax_mini_window_height);
6013 else
6014 max_height = total_height / 4;
6016 /* Correct that max. height if it's bogus. */
6017 max_height = max (1, max_height);
6018 max_height = min (total_height, max_height);
6020 /* Find out the height of the text in the window. */
6021 if (it.truncate_lines_p)
6022 height = 1;
6023 else
6025 last_height = 0;
6026 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6027 if (it.max_ascent == 0 && it.max_descent == 0)
6028 height = it.current_y + last_height;
6029 else
6030 height = it.current_y + it.max_ascent + it.max_descent;
6031 height -= it.extra_line_spacing;
6032 height = (height + unit - 1) / unit;
6035 /* Compute a suitable window start. */
6036 if (height > max_height)
6038 height = max_height;
6039 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6040 move_it_vertically_backward (&it, (height - 1) * unit);
6041 start = it.current.pos;
6043 else
6044 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6045 SET_MARKER_FROM_TEXT_POS (w->start, start);
6047 /* Let it grow only, until we display an empty message, in which
6048 case the window shrinks again. */
6049 if (height > XFASTINT (w->height))
6051 int old_height = XFASTINT (w->height);
6052 freeze_window_starts (f, 1);
6053 grow_mini_window (w, height - XFASTINT (w->height));
6054 window_height_changed_p = XFASTINT (w->height) != old_height;
6056 else if (height < XFASTINT (w->height)
6057 && (exact_p || BEGV == ZV))
6059 int old_height = XFASTINT (w->height);
6060 freeze_window_starts (f, 0);
6061 shrink_mini_window (w);
6062 window_height_changed_p = XFASTINT (w->height) != old_height;
6066 return window_height_changed_p;
6070 /* Value is the current message, a string, or nil if there is no
6071 current message. */
6073 Lisp_Object
6074 current_message ()
6076 Lisp_Object msg;
6078 if (NILP (echo_area_buffer[0]))
6079 msg = Qnil;
6080 else
6082 with_echo_area_buffer (0, 0, current_message_1,
6083 (EMACS_INT) &msg, Qnil, 0, 0);
6084 if (NILP (msg))
6085 echo_area_buffer[0] = Qnil;
6088 return msg;
6092 static int
6093 current_message_1 (a1, a2, a3, a4)
6094 EMACS_INT a1;
6095 Lisp_Object a2;
6096 EMACS_INT a3, a4;
6098 Lisp_Object *msg = (Lisp_Object *) a1;
6100 if (Z > BEG)
6101 *msg = make_buffer_string (BEG, Z, 1);
6102 else
6103 *msg = Qnil;
6104 return 0;
6108 /* Push the current message on Vmessage_stack for later restauration
6109 by restore_message. Value is non-zero if the current message isn't
6110 empty. This is a relatively infrequent operation, so it's not
6111 worth optimizing. */
6114 push_message ()
6116 Lisp_Object msg;
6117 msg = current_message ();
6118 Vmessage_stack = Fcons (msg, Vmessage_stack);
6119 return STRINGP (msg);
6123 /* Restore message display from the top of Vmessage_stack. */
6125 void
6126 restore_message ()
6128 Lisp_Object msg;
6130 xassert (CONSP (Vmessage_stack));
6131 msg = XCAR (Vmessage_stack);
6132 if (STRINGP (msg))
6133 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6134 else
6135 message3_nolog (msg, 0, 0);
6139 /* Pop the top-most entry off Vmessage_stack. */
6141 void
6142 pop_message ()
6144 xassert (CONSP (Vmessage_stack));
6145 Vmessage_stack = XCDR (Vmessage_stack);
6149 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6150 exits. If the stack is not empty, we have a missing pop_message
6151 somewhere. */
6153 void
6154 check_message_stack ()
6156 if (!NILP (Vmessage_stack))
6157 abort ();
6161 /* Truncate to NCHARS what will be displayed in the echo area the next
6162 time we display it---but don't redisplay it now. */
6164 void
6165 truncate_echo_area (nchars)
6166 int nchars;
6168 if (nchars == 0)
6169 echo_area_buffer[0] = Qnil;
6170 /* A null message buffer means that the frame hasn't really been
6171 initialized yet. Error messages get reported properly by
6172 cmd_error, so this must be just an informative message; toss it. */
6173 else if (!noninteractive
6174 && INTERACTIVE
6175 && !NILP (echo_area_buffer[0]))
6177 struct frame *sf = SELECTED_FRAME ();
6178 if (FRAME_MESSAGE_BUF (sf))
6179 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6184 /* Helper function for truncate_echo_area. Truncate the current
6185 message to at most NCHARS characters. */
6187 static int
6188 truncate_message_1 (nchars, a2, a3, a4)
6189 EMACS_INT nchars;
6190 Lisp_Object a2;
6191 EMACS_INT a3, a4;
6193 if (BEG + nchars < Z)
6194 del_range (BEG + nchars, Z);
6195 if (Z == BEG)
6196 echo_area_buffer[0] = Qnil;
6197 return 0;
6201 /* Set the current message to a substring of S or STRING.
6203 If STRING is a Lisp string, set the message to the first NBYTES
6204 bytes from STRING. NBYTES zero means use the whole string. If
6205 STRING is multibyte, the message will be displayed multibyte.
6207 If S is not null, set the message to the first LEN bytes of S. LEN
6208 zero means use the whole string. MULTIBYTE_P non-zero means S is
6209 multibyte. Display the message multibyte in that case. */
6211 void
6212 set_message (s, string, nbytes, multibyte_p)
6213 char *s;
6214 Lisp_Object string;
6215 int nbytes;
6217 message_enable_multibyte
6218 = ((s && multibyte_p)
6219 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6221 with_echo_area_buffer (0, -1, set_message_1,
6222 (EMACS_INT) s, string, nbytes, multibyte_p);
6223 message_buf_print = 0;
6224 help_echo_showing_p = 0;
6228 /* Helper function for set_message. Arguments have the same meaning
6229 as there, with A1 corresponding to S and A2 corresponding to STRING
6230 This function is called with the echo area buffer being
6231 current. */
6233 static int
6234 set_message_1 (a1, a2, nbytes, multibyte_p)
6235 EMACS_INT a1;
6236 Lisp_Object a2;
6237 EMACS_INT nbytes, multibyte_p;
6239 char *s = (char *) a1;
6240 Lisp_Object string = a2;
6242 xassert (BEG == Z);
6244 /* Change multibyteness of the echo buffer appropriately. */
6245 if (message_enable_multibyte
6246 != !NILP (current_buffer->enable_multibyte_characters))
6247 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6249 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6251 /* Insert new message at BEG. */
6252 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6254 if (STRINGP (string))
6256 int nchars;
6258 if (nbytes == 0)
6259 nbytes = XSTRING (string)->size_byte;
6260 nchars = string_byte_to_char (string, nbytes);
6262 /* This function takes care of single/multibyte conversion. We
6263 just have to ensure that the echo area buffer has the right
6264 setting of enable_multibyte_characters. */
6265 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6267 else if (s)
6269 if (nbytes == 0)
6270 nbytes = strlen (s);
6272 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6274 /* Convert from multi-byte to single-byte. */
6275 int i, c, n;
6276 unsigned char work[1];
6278 /* Convert a multibyte string to single-byte. */
6279 for (i = 0; i < nbytes; i += n)
6281 c = string_char_and_length (s + i, nbytes - i, &n);
6282 work[0] = (SINGLE_BYTE_CHAR_P (c)
6284 : multibyte_char_to_unibyte (c, Qnil));
6285 insert_1_both (work, 1, 1, 1, 0, 0);
6288 else if (!multibyte_p
6289 && !NILP (current_buffer->enable_multibyte_characters))
6291 /* Convert from single-byte to multi-byte. */
6292 int i, c, n;
6293 unsigned char *msg = (unsigned char *) s;
6294 unsigned char str[MAX_MULTIBYTE_LENGTH];
6296 /* Convert a single-byte string to multibyte. */
6297 for (i = 0; i < nbytes; i++)
6299 c = unibyte_char_to_multibyte (msg[i]);
6300 n = CHAR_STRING (c, str);
6301 insert_1_both (str, 1, n, 1, 0, 0);
6304 else
6305 insert_1 (s, nbytes, 1, 0, 0);
6308 return 0;
6312 /* Clear messages. CURRENT_P non-zero means clear the current
6313 message. LAST_DISPLAYED_P non-zero means clear the message
6314 last displayed. */
6316 void
6317 clear_message (current_p, last_displayed_p)
6318 int current_p, last_displayed_p;
6320 if (current_p)
6321 echo_area_buffer[0] = Qnil;
6323 if (last_displayed_p)
6324 echo_area_buffer[1] = Qnil;
6326 message_buf_print = 0;
6329 /* Clear garbaged frames.
6331 This function is used where the old redisplay called
6332 redraw_garbaged_frames which in turn called redraw_frame which in
6333 turn called clear_frame. The call to clear_frame was a source of
6334 flickering. I believe a clear_frame is not necessary. It should
6335 suffice in the new redisplay to invalidate all current matrices,
6336 and ensure a complete redisplay of all windows. */
6338 static void
6339 clear_garbaged_frames ()
6341 if (frame_garbaged)
6343 Lisp_Object tail, frame;
6345 FOR_EACH_FRAME (tail, frame)
6347 struct frame *f = XFRAME (frame);
6349 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6351 clear_current_matrices (f);
6352 f->garbaged = 0;
6356 frame_garbaged = 0;
6357 ++windows_or_buffers_changed;
6362 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6363 is non-zero update selected_frame. Value is non-zero if the
6364 mini-windows height has been changed. */
6366 static int
6367 echo_area_display (update_frame_p)
6368 int update_frame_p;
6370 Lisp_Object mini_window;
6371 struct window *w;
6372 struct frame *f;
6373 int window_height_changed_p = 0;
6374 struct frame *sf = SELECTED_FRAME ();
6376 mini_window = FRAME_MINIBUF_WINDOW (sf);
6377 w = XWINDOW (mini_window);
6378 f = XFRAME (WINDOW_FRAME (w));
6380 /* Don't display if frame is invisible or not yet initialized. */
6381 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6382 return 0;
6384 #ifdef HAVE_WINDOW_SYSTEM
6385 /* When Emacs starts, selected_frame may be a visible terminal
6386 frame, even if we run under a window system. If we let this
6387 through, a message would be displayed on the terminal. */
6388 if (EQ (selected_frame, Vterminal_frame)
6389 && !NILP (Vwindow_system))
6390 return 0;
6391 #endif /* HAVE_WINDOW_SYSTEM */
6393 /* Redraw garbaged frames. */
6394 if (frame_garbaged)
6395 clear_garbaged_frames ();
6397 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6399 echo_area_window = mini_window;
6400 window_height_changed_p = display_echo_area (w);
6401 w->must_be_updated_p = 1;
6403 /* Update the display, unless called from redisplay_internal. */
6404 if (update_frame_p)
6406 int n = 0;
6408 /* If the display update has been interrupted by pending
6409 input, update mode lines in the frame. Due to the
6410 pending input, it might have been that redisplay hasn't
6411 been called, so that mode lines above the echo area are
6412 garbaged. This looks odd, so we prevent it here. */
6413 if (!display_completed)
6414 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6416 if (window_height_changed_p)
6418 /* Must update other windows. */
6419 windows_or_buffers_changed = 1;
6420 redisplay_internal (0);
6422 else if (FRAME_WINDOW_P (f) && n == 0)
6424 /* Window configuration is the same as before.
6425 Can do with a display update of the echo area,
6426 unless we displayed some mode lines. */
6427 update_single_window (w, 1);
6428 rif->flush_display (f);
6430 else
6431 update_frame (f, 1, 1);
6434 else if (!EQ (mini_window, selected_window))
6435 windows_or_buffers_changed++;
6437 /* Last displayed message is now the current message. */
6438 echo_area_buffer[1] = echo_area_buffer[0];
6440 /* Prevent redisplay optimization in redisplay_internal by resetting
6441 this_line_start_pos. This is done because the mini-buffer now
6442 displays the message instead of its buffer text. */
6443 if (EQ (mini_window, selected_window))
6444 CHARPOS (this_line_start_pos) = 0;
6446 return window_height_changed_p;
6451 /***********************************************************************
6452 Frame Titles
6453 ***********************************************************************/
6456 #ifdef HAVE_WINDOW_SYSTEM
6458 /* A buffer for constructing frame titles in it; allocated from the
6459 heap in init_xdisp and resized as needed in store_frame_title_char. */
6461 static char *frame_title_buf;
6463 /* The buffer's end, and a current output position in it. */
6465 static char *frame_title_buf_end;
6466 static char *frame_title_ptr;
6469 /* Store a single character C for the frame title in frame_title_buf.
6470 Re-allocate frame_title_buf if necessary. */
6472 static void
6473 store_frame_title_char (c)
6474 char c;
6476 /* If output position has reached the end of the allocated buffer,
6477 double the buffer's size. */
6478 if (frame_title_ptr == frame_title_buf_end)
6480 int len = frame_title_ptr - frame_title_buf;
6481 int new_size = 2 * len * sizeof *frame_title_buf;
6482 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6483 frame_title_buf_end = frame_title_buf + new_size;
6484 frame_title_ptr = frame_title_buf + len;
6487 *frame_title_ptr++ = c;
6491 /* Store part of a frame title in frame_title_buf, beginning at
6492 frame_title_ptr. STR is the string to store. Do not copy more
6493 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6494 the whole string. Pad with spaces until FIELD_WIDTH number of
6495 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6496 Called from display_mode_element when it is used to build a frame
6497 title. */
6499 static int
6500 store_frame_title (str, field_width, precision)
6501 unsigned char *str;
6502 int field_width, precision;
6504 int n = 0;
6506 /* Copy at most PRECISION chars from STR. */
6507 while ((precision <= 0 || n < precision)
6508 && *str)
6510 store_frame_title_char (*str++);
6511 ++n;
6514 /* Fill up with spaces until FIELD_WIDTH reached. */
6515 while (field_width > 0
6516 && n < field_width)
6518 store_frame_title_char (' ');
6519 ++n;
6522 return n;
6526 /* Set the title of FRAME, if it has changed. The title format is
6527 Vicon_title_format if FRAME is iconified, otherwise it is
6528 frame_title_format. */
6530 static void
6531 x_consider_frame_title (frame)
6532 Lisp_Object frame;
6534 struct frame *f = XFRAME (frame);
6536 if (FRAME_WINDOW_P (f)
6537 || FRAME_MINIBUF_ONLY_P (f)
6538 || f->explicit_name)
6540 /* Do we have more than one visible frame on this X display? */
6541 Lisp_Object tail;
6542 Lisp_Object fmt;
6543 struct buffer *obuf;
6544 int len;
6545 struct it it;
6547 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6549 struct frame *tf = XFRAME (XCAR (tail));
6551 if (tf != f
6552 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6553 && !FRAME_MINIBUF_ONLY_P (tf)
6554 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6555 break;
6558 /* Set global variable indicating that multiple frames exist. */
6559 multiple_frames = CONSP (tail);
6561 /* Switch to the buffer of selected window of the frame. Set up
6562 frame_title_ptr so that display_mode_element will output into it;
6563 then display the title. */
6564 obuf = current_buffer;
6565 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6566 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6567 frame_title_ptr = frame_title_buf;
6568 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6569 NULL, DEFAULT_FACE_ID);
6570 len = display_mode_element (&it, 0, -1, -1, fmt);
6571 frame_title_ptr = NULL;
6572 set_buffer_internal (obuf);
6574 /* Set the title only if it's changed. This avoids consing in
6575 the common case where it hasn't. (If it turns out that we've
6576 already wasted too much time by walking through the list with
6577 display_mode_element, then we might need to optimize at a
6578 higher level than this.) */
6579 if (! STRINGP (f->name)
6580 || STRING_BYTES (XSTRING (f->name)) != len
6581 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6582 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6586 #else /* not HAVE_WINDOW_SYSTEM */
6588 #define frame_title_ptr ((char *)0)
6589 #define store_frame_title(str, mincol, maxcol) 0
6591 #endif /* not HAVE_WINDOW_SYSTEM */
6596 /***********************************************************************
6597 Menu Bars
6598 ***********************************************************************/
6601 /* Prepare for redisplay by updating menu-bar item lists when
6602 appropriate. This can call eval. */
6604 void
6605 prepare_menu_bars ()
6607 int all_windows;
6608 struct gcpro gcpro1, gcpro2;
6609 struct frame *f;
6610 struct frame *tooltip_frame;
6612 #ifdef HAVE_X_WINDOWS
6613 tooltip_frame = tip_frame;
6614 #else
6615 tooltip_frame = NULL;
6616 #endif
6618 /* Update all frame titles based on their buffer names, etc. We do
6619 this before the menu bars so that the buffer-menu will show the
6620 up-to-date frame titles. */
6621 #ifdef HAVE_WINDOW_SYSTEM
6622 if (windows_or_buffers_changed || update_mode_lines)
6624 Lisp_Object tail, frame;
6626 FOR_EACH_FRAME (tail, frame)
6628 f = XFRAME (frame);
6629 if (f != tooltip_frame
6630 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6631 x_consider_frame_title (frame);
6634 #endif /* HAVE_WINDOW_SYSTEM */
6636 /* Update the menu bar item lists, if appropriate. This has to be
6637 done before any actual redisplay or generation of display lines. */
6638 all_windows = (update_mode_lines
6639 || buffer_shared > 1
6640 || windows_or_buffers_changed);
6641 if (all_windows)
6643 Lisp_Object tail, frame;
6644 int count = specpdl_ptr - specpdl;
6646 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6648 FOR_EACH_FRAME (tail, frame)
6650 f = XFRAME (frame);
6652 /* Ignore tooltip frame. */
6653 if (f == tooltip_frame)
6654 continue;
6656 /* If a window on this frame changed size, report that to
6657 the user and clear the size-change flag. */
6658 if (FRAME_WINDOW_SIZES_CHANGED (f))
6660 Lisp_Object functions;
6662 /* Clear flag first in case we get an error below. */
6663 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6664 functions = Vwindow_size_change_functions;
6665 GCPRO2 (tail, functions);
6667 while (CONSP (functions))
6669 call1 (XCAR (functions), frame);
6670 functions = XCDR (functions);
6672 UNGCPRO;
6675 GCPRO1 (tail);
6676 update_menu_bar (f, 0);
6677 #ifdef HAVE_WINDOW_SYSTEM
6678 update_tool_bar (f, 0);
6679 #endif
6680 UNGCPRO;
6683 unbind_to (count, Qnil);
6685 else
6687 struct frame *sf = SELECTED_FRAME ();
6688 update_menu_bar (sf, 1);
6689 #ifdef HAVE_WINDOW_SYSTEM
6690 update_tool_bar (sf, 1);
6691 #endif
6694 /* Motif needs this. See comment in xmenu.c. Turn it off when
6695 pending_menu_activation is not defined. */
6696 #ifdef USE_X_TOOLKIT
6697 pending_menu_activation = 0;
6698 #endif
6702 /* Update the menu bar item list for frame F. This has to be done
6703 before we start to fill in any display lines, because it can call
6704 eval.
6706 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6708 static void
6709 update_menu_bar (f, save_match_data)
6710 struct frame *f;
6711 int save_match_data;
6713 Lisp_Object window;
6714 register struct window *w;
6716 window = FRAME_SELECTED_WINDOW (f);
6717 w = XWINDOW (window);
6719 if (update_mode_lines)
6720 w->update_mode_line = Qt;
6722 if (FRAME_WINDOW_P (f)
6724 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6725 FRAME_EXTERNAL_MENU_BAR (f)
6726 #else
6727 FRAME_MENU_BAR_LINES (f) > 0
6728 #endif
6729 : FRAME_MENU_BAR_LINES (f) > 0)
6731 /* If the user has switched buffers or windows, we need to
6732 recompute to reflect the new bindings. But we'll
6733 recompute when update_mode_lines is set too; that means
6734 that people can use force-mode-line-update to request
6735 that the menu bar be recomputed. The adverse effect on
6736 the rest of the redisplay algorithm is about the same as
6737 windows_or_buffers_changed anyway. */
6738 if (windows_or_buffers_changed
6739 || !NILP (w->update_mode_line)
6740 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6741 < BUF_MODIFF (XBUFFER (w->buffer)))
6742 != !NILP (w->last_had_star))
6743 || ((!NILP (Vtransient_mark_mode)
6744 && !NILP (XBUFFER (w->buffer)->mark_active))
6745 != !NILP (w->region_showing)))
6747 struct buffer *prev = current_buffer;
6748 int count = specpdl_ptr - specpdl;
6750 set_buffer_internal_1 (XBUFFER (w->buffer));
6751 if (save_match_data)
6752 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6753 if (NILP (Voverriding_local_map_menu_flag))
6755 specbind (Qoverriding_terminal_local_map, Qnil);
6756 specbind (Qoverriding_local_map, Qnil);
6759 /* Run the Lucid hook. */
6760 call1 (Vrun_hooks, Qactivate_menubar_hook);
6762 /* If it has changed current-menubar from previous value,
6763 really recompute the menu-bar from the value. */
6764 if (! NILP (Vlucid_menu_bar_dirty_flag))
6765 call0 (Qrecompute_lucid_menubar);
6767 safe_run_hooks (Qmenu_bar_update_hook);
6768 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6770 /* Redisplay the menu bar in case we changed it. */
6771 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6772 if (FRAME_WINDOW_P (f))
6773 set_frame_menubar (f, 0, 0);
6774 else
6775 /* On a terminal screen, the menu bar is an ordinary screen
6776 line, and this makes it get updated. */
6777 w->update_mode_line = Qt;
6778 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6779 /* In the non-toolkit version, the menu bar is an ordinary screen
6780 line, and this makes it get updated. */
6781 w->update_mode_line = Qt;
6782 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6784 unbind_to (count, Qnil);
6785 set_buffer_internal_1 (prev);
6792 /***********************************************************************
6793 Tool-bars
6794 ***********************************************************************/
6796 #ifdef HAVE_WINDOW_SYSTEM
6798 /* Update the tool-bar item list for frame F. This has to be done
6799 before we start to fill in any display lines. Called from
6800 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6801 and restore it here. */
6803 static void
6804 update_tool_bar (f, save_match_data)
6805 struct frame *f;
6806 int save_match_data;
6808 if (WINDOWP (f->tool_bar_window)
6809 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6811 Lisp_Object window;
6812 struct window *w;
6814 window = FRAME_SELECTED_WINDOW (f);
6815 w = XWINDOW (window);
6817 /* If the user has switched buffers or windows, we need to
6818 recompute to reflect the new bindings. But we'll
6819 recompute when update_mode_lines is set too; that means
6820 that people can use force-mode-line-update to request
6821 that the menu bar be recomputed. The adverse effect on
6822 the rest of the redisplay algorithm is about the same as
6823 windows_or_buffers_changed anyway. */
6824 if (windows_or_buffers_changed
6825 || !NILP (w->update_mode_line)
6826 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6827 < BUF_MODIFF (XBUFFER (w->buffer)))
6828 != !NILP (w->last_had_star))
6829 || ((!NILP (Vtransient_mark_mode)
6830 && !NILP (XBUFFER (w->buffer)->mark_active))
6831 != !NILP (w->region_showing)))
6833 struct buffer *prev = current_buffer;
6834 int count = specpdl_ptr - specpdl;
6836 /* Set current_buffer to the buffer of the selected
6837 window of the frame, so that we get the right local
6838 keymaps. */
6839 set_buffer_internal_1 (XBUFFER (w->buffer));
6841 /* Save match data, if we must. */
6842 if (save_match_data)
6843 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6845 /* Make sure that we don't accidentally use bogus keymaps. */
6846 if (NILP (Voverriding_local_map_menu_flag))
6848 specbind (Qoverriding_terminal_local_map, Qnil);
6849 specbind (Qoverriding_local_map, Qnil);
6852 /* Build desired tool-bar items from keymaps. */
6853 f->desired_tool_bar_items
6854 = tool_bar_items (f->desired_tool_bar_items,
6855 &f->n_desired_tool_bar_items);
6857 /* Redisplay the tool-bar in case we changed it. */
6858 w->update_mode_line = Qt;
6860 unbind_to (count, Qnil);
6861 set_buffer_internal_1 (prev);
6867 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6868 F's desired tool-bar contents. F->desired_tool_bar_items must have
6869 been set up previously by calling prepare_menu_bars. */
6871 static void
6872 build_desired_tool_bar_string (f)
6873 struct frame *f;
6875 int i, size, size_needed, string_idx;
6876 struct gcpro gcpro1, gcpro2, gcpro3;
6877 Lisp_Object image, plist, props;
6879 image = plist = props = Qnil;
6880 GCPRO3 (image, plist, props);
6882 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6883 Otherwise, make a new string. */
6885 /* The size of the string we might be able to reuse. */
6886 size = (STRINGP (f->desired_tool_bar_string)
6887 ? XSTRING (f->desired_tool_bar_string)->size
6888 : 0);
6890 /* Each image in the string we build is preceded by a space,
6891 and there is a space at the end. */
6892 size_needed = f->n_desired_tool_bar_items + 1;
6894 /* Reuse f->desired_tool_bar_string, if possible. */
6895 if (size < size_needed)
6896 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6897 make_number (' '));
6898 else
6900 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6901 Fremove_text_properties (make_number (0), make_number (size),
6902 props, f->desired_tool_bar_string);
6905 /* Put a `display' property on the string for the images to display,
6906 put a `menu_item' property on tool-bar items with a value that
6907 is the index of the item in F's tool-bar item vector. */
6908 for (i = 0, string_idx = 0;
6909 i < f->n_desired_tool_bar_items;
6910 ++i, string_idx += 1)
6912 #define PROP(IDX) \
6913 (XVECTOR (f->desired_tool_bar_items) \
6914 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6916 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6917 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6918 int margin, relief, idx;
6919 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6920 extern Lisp_Object Qlaplace;
6922 /* If image is a vector, choose the image according to the
6923 button state. */
6924 image = PROP (TOOL_BAR_ITEM_IMAGES);
6925 if (VECTORP (image))
6927 if (enabled_p)
6928 idx = (selected_p
6929 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6930 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6931 else
6932 idx = (selected_p
6933 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6934 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6936 xassert (ASIZE (image) >= idx);
6937 image = AREF (image, idx);
6939 else
6940 idx = -1;
6942 /* Ignore invalid image specifications. */
6943 if (!valid_image_p (image))
6944 continue;
6946 /* Display the tool-bar button pressed, or depressed. */
6947 plist = Fcopy_sequence (XCDR (image));
6949 /* Compute margin and relief to draw. */
6950 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6951 margin = relief + max (0, tool_bar_button_margin);
6953 if (auto_raise_tool_bar_buttons_p)
6955 /* Add a `:relief' property to the image spec if the item is
6956 selected. */
6957 if (selected_p)
6959 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6960 margin -= relief;
6963 else
6965 /* If image is selected, display it pressed, i.e. with a
6966 negative relief. If it's not selected, display it with a
6967 raised relief. */
6968 plist = Fplist_put (plist, QCrelief,
6969 (selected_p
6970 ? make_number (-relief)
6971 : make_number (relief)));
6972 margin -= relief;
6975 /* Put a margin around the image. */
6976 if (margin)
6977 plist = Fplist_put (plist, QCmargin, make_number (margin));
6979 /* If button is not enabled, and we don't have special images
6980 for the disabled state, make the image appear disabled by
6981 applying an appropriate algorithm to it. */
6982 if (!enabled_p && idx < 0)
6983 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
6985 /* Put a `display' text property on the string for the image to
6986 display. Put a `menu-item' property on the string that gives
6987 the start of this item's properties in the tool-bar items
6988 vector. */
6989 image = Fcons (Qimage, plist);
6990 props = list4 (Qdisplay, image,
6991 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6992 Fadd_text_properties (make_number (string_idx),
6993 make_number (string_idx + 1),
6994 props, f->desired_tool_bar_string);
6995 #undef PROP
6998 UNGCPRO;
7002 /* Display one line of the tool-bar of frame IT->f. */
7004 static void
7005 display_tool_bar_line (it)
7006 struct it *it;
7008 struct glyph_row *row = it->glyph_row;
7009 int max_x = it->last_visible_x;
7010 struct glyph *last;
7012 prepare_desired_row (row);
7013 row->y = it->current_y;
7015 while (it->current_x < max_x)
7017 int x_before, x, n_glyphs_before, i, nglyphs;
7019 /* Get the next display element. */
7020 if (!get_next_display_element (it))
7021 break;
7023 /* Produce glyphs. */
7024 x_before = it->current_x;
7025 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7026 PRODUCE_GLYPHS (it);
7028 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7029 i = 0;
7030 x = x_before;
7031 while (i < nglyphs)
7033 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7035 if (x + glyph->pixel_width > max_x)
7037 /* Glyph doesn't fit on line. */
7038 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7039 it->current_x = x;
7040 goto out;
7043 ++it->hpos;
7044 x += glyph->pixel_width;
7045 ++i;
7048 /* Stop at line ends. */
7049 if (ITERATOR_AT_END_OF_LINE_P (it))
7050 break;
7052 set_iterator_to_next (it, 1);
7055 out:;
7057 row->displays_text_p = row->used[TEXT_AREA] != 0;
7058 extend_face_to_end_of_line (it);
7059 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7060 last->right_box_line_p = 1;
7061 compute_line_metrics (it);
7063 /* If line is empty, make it occupy the rest of the tool-bar. */
7064 if (!row->displays_text_p)
7066 row->height = row->phys_height = it->last_visible_y - row->y;
7067 row->ascent = row->phys_ascent = 0;
7070 row->full_width_p = 1;
7071 row->continued_p = 0;
7072 row->truncated_on_left_p = 0;
7073 row->truncated_on_right_p = 0;
7075 it->current_x = it->hpos = 0;
7076 it->current_y += row->height;
7077 ++it->vpos;
7078 ++it->glyph_row;
7082 /* Value is the number of screen lines needed to make all tool-bar
7083 items of frame F visible. */
7085 static int
7086 tool_bar_lines_needed (f)
7087 struct frame *f;
7089 struct window *w = XWINDOW (f->tool_bar_window);
7090 struct it it;
7092 /* Initialize an iterator for iteration over
7093 F->desired_tool_bar_string in the tool-bar window of frame F. */
7094 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7095 it.first_visible_x = 0;
7096 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7097 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7099 while (!ITERATOR_AT_END_P (&it))
7101 it.glyph_row = w->desired_matrix->rows;
7102 clear_glyph_row (it.glyph_row);
7103 display_tool_bar_line (&it);
7106 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7110 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7111 height should be changed. */
7113 static int
7114 redisplay_tool_bar (f)
7115 struct frame *f;
7117 struct window *w;
7118 struct it it;
7119 struct glyph_row *row;
7120 int change_height_p = 0;
7122 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7123 do anything. This means you must start with tool-bar-lines
7124 non-zero to get the auto-sizing effect. Or in other words, you
7125 can turn off tool-bars by specifying tool-bar-lines zero. */
7126 if (!WINDOWP (f->tool_bar_window)
7127 || (w = XWINDOW (f->tool_bar_window),
7128 XFASTINT (w->height) == 0))
7129 return 0;
7131 /* Set up an iterator for the tool-bar window. */
7132 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7133 it.first_visible_x = 0;
7134 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7135 row = it.glyph_row;
7137 /* Build a string that represents the contents of the tool-bar. */
7138 build_desired_tool_bar_string (f);
7139 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7141 /* Display as many lines as needed to display all tool-bar items. */
7142 while (it.current_y < it.last_visible_y)
7143 display_tool_bar_line (&it);
7145 /* It doesn't make much sense to try scrolling in the tool-bar
7146 window, so don't do it. */
7147 w->desired_matrix->no_scrolling_p = 1;
7148 w->must_be_updated_p = 1;
7150 if (auto_resize_tool_bars_p)
7152 int nlines;
7154 /* If there are blank lines at the end, except for a partially
7155 visible blank line at the end that is smaller than
7156 CANON_Y_UNIT, change the tool-bar's height. */
7157 row = it.glyph_row - 1;
7158 if (!row->displays_text_p
7159 && row->height >= CANON_Y_UNIT (f))
7160 change_height_p = 1;
7162 /* If row displays tool-bar items, but is partially visible,
7163 change the tool-bar's height. */
7164 if (row->displays_text_p
7165 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7166 change_height_p = 1;
7168 /* Resize windows as needed by changing the `tool-bar-lines'
7169 frame parameter. */
7170 if (change_height_p
7171 && (nlines = tool_bar_lines_needed (f),
7172 nlines != XFASTINT (w->height)))
7174 extern Lisp_Object Qtool_bar_lines;
7175 Lisp_Object frame;
7177 XSETFRAME (frame, f);
7178 clear_glyph_matrix (w->desired_matrix);
7179 Fmodify_frame_parameters (frame,
7180 Fcons (Fcons (Qtool_bar_lines,
7181 make_number (nlines)),
7182 Qnil));
7183 fonts_changed_p = 1;
7187 return change_height_p;
7191 /* Get information about the tool-bar item which is displayed in GLYPH
7192 on frame F. Return in *PROP_IDX the index where tool-bar item
7193 properties start in F->current_tool_bar_items. Value is zero if
7194 GLYPH doesn't display a tool-bar item. */
7197 tool_bar_item_info (f, glyph, prop_idx)
7198 struct frame *f;
7199 struct glyph *glyph;
7200 int *prop_idx;
7202 Lisp_Object prop;
7203 int success_p;
7205 /* Get the text property `menu-item' at pos. The value of that
7206 property is the start index of this item's properties in
7207 F->current_tool_bar_items. */
7208 prop = Fget_text_property (make_number (glyph->charpos),
7209 Qmenu_item, f->current_tool_bar_string);
7210 if (INTEGERP (prop))
7212 *prop_idx = XINT (prop);
7213 success_p = 1;
7215 else
7216 success_p = 0;
7218 return success_p;
7221 #endif /* HAVE_WINDOW_SYSTEM */
7225 /************************************************************************
7226 Horizontal scrolling
7227 ************************************************************************/
7229 static int hscroll_window_tree P_ ((Lisp_Object));
7230 static int hscroll_windows P_ ((Lisp_Object));
7232 /* For all leaf windows in the window tree rooted at WINDOW, set their
7233 hscroll value so that PT is (i) visible in the window, and (ii) so
7234 that it is not within a certain margin at the window's left and
7235 right border. Value is non-zero if any window's hscroll has been
7236 changed. */
7238 static int
7239 hscroll_window_tree (window)
7240 Lisp_Object window;
7242 int hscrolled_p = 0;
7244 while (WINDOWP (window))
7246 struct window *w = XWINDOW (window);
7248 if (WINDOWP (w->hchild))
7249 hscrolled_p |= hscroll_window_tree (w->hchild);
7250 else if (WINDOWP (w->vchild))
7251 hscrolled_p |= hscroll_window_tree (w->vchild);
7252 else if (w->cursor.vpos >= 0)
7254 int hscroll_margin, text_area_x, text_area_y;
7255 int text_area_width, text_area_height;
7256 struct glyph_row *current_cursor_row
7257 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7258 struct glyph_row *desired_cursor_row
7259 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7260 struct glyph_row *cursor_row
7261 = (desired_cursor_row->enabled_p
7262 ? desired_cursor_row
7263 : current_cursor_row);
7265 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7266 &text_area_width, &text_area_height);
7268 /* Scroll when cursor is inside this scroll margin. */
7269 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7271 if ((XFASTINT (w->hscroll)
7272 && w->cursor.x < hscroll_margin)
7273 || (cursor_row->enabled_p
7274 && cursor_row->truncated_on_right_p
7275 && (w->cursor.x > text_area_width - hscroll_margin)))
7277 struct it it;
7278 int hscroll;
7279 struct buffer *saved_current_buffer;
7280 int pt;
7282 /* Find point in a display of infinite width. */
7283 saved_current_buffer = current_buffer;
7284 current_buffer = XBUFFER (w->buffer);
7286 if (w == XWINDOW (selected_window))
7287 pt = BUF_PT (current_buffer);
7288 else
7290 pt = marker_position (w->pointm);
7291 pt = max (BEGV, pt);
7292 pt = min (ZV, pt);
7295 /* Move iterator to pt starting at cursor_row->start in
7296 a line with infinite width. */
7297 init_to_row_start (&it, w, cursor_row);
7298 it.last_visible_x = INFINITY;
7299 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7300 current_buffer = saved_current_buffer;
7302 /* Center cursor in window. */
7303 hscroll = (max (0, it.current_x - text_area_width / 2)
7304 / CANON_X_UNIT (it.f));
7306 /* Don't call Fset_window_hscroll if value hasn't
7307 changed because it will prevent redisplay
7308 optimizations. */
7309 if (XFASTINT (w->hscroll) != hscroll)
7311 Fset_window_hscroll (window, make_number (hscroll));
7312 hscrolled_p = 1;
7317 window = w->next;
7320 /* Value is non-zero if hscroll of any leaf window has been changed. */
7321 return hscrolled_p;
7325 /* Set hscroll so that cursor is visible and not inside horizontal
7326 scroll margins for all windows in the tree rooted at WINDOW. See
7327 also hscroll_window_tree above. Value is non-zero if any window's
7328 hscroll has been changed. If it has, desired matrices on the frame
7329 of WINDOW are cleared. */
7331 static int
7332 hscroll_windows (window)
7333 Lisp_Object window;
7335 int hscrolled_p;
7337 if (automatic_hscrolling_p)
7339 hscrolled_p = hscroll_window_tree (window);
7340 if (hscrolled_p)
7341 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7343 else
7344 hscrolled_p = 0;
7345 return hscrolled_p;
7350 /************************************************************************
7351 Redisplay
7352 ************************************************************************/
7354 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7355 to a non-zero value. This is sometimes handy to have in a debugger
7356 session. */
7358 #if GLYPH_DEBUG
7360 /* First and last unchanged row for try_window_id. */
7362 int debug_first_unchanged_at_end_vpos;
7363 int debug_last_unchanged_at_beg_vpos;
7365 /* Delta vpos and y. */
7367 int debug_dvpos, debug_dy;
7369 /* Delta in characters and bytes for try_window_id. */
7371 int debug_delta, debug_delta_bytes;
7373 /* Values of window_end_pos and window_end_vpos at the end of
7374 try_window_id. */
7376 int debug_end_pos, debug_end_vpos;
7378 /* Append a string to W->desired_matrix->method. FMT is a printf
7379 format string. A1...A9 are a supplement for a variable-length
7380 argument list. If trace_redisplay_p is non-zero also printf the
7381 resulting string to stderr. */
7383 static void
7384 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7385 struct window *w;
7386 char *fmt;
7387 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7389 char buffer[512];
7390 char *method = w->desired_matrix->method;
7391 int len = strlen (method);
7392 int size = sizeof w->desired_matrix->method;
7393 int remaining = size - len - 1;
7395 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7396 if (len && remaining)
7398 method[len] = '|';
7399 --remaining, ++len;
7402 strncpy (method + len, buffer, remaining);
7404 if (trace_redisplay_p)
7405 fprintf (stderr, "%p (%s): %s\n",
7407 ((BUFFERP (w->buffer)
7408 && STRINGP (XBUFFER (w->buffer)->name))
7409 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7410 : "no buffer"),
7411 buffer);
7414 #endif /* GLYPH_DEBUG */
7417 /* This counter is used to clear the face cache every once in a while
7418 in redisplay_internal. It is incremented for each redisplay.
7419 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7420 cleared. */
7422 #define CLEAR_FACE_CACHE_COUNT 10000
7423 static int clear_face_cache_count;
7425 /* Record the previous terminal frame we displayed. */
7427 static struct frame *previous_terminal_frame;
7429 /* Non-zero while redisplay_internal is in progress. */
7431 int redisplaying_p;
7434 /* Value is non-zero if all changes in window W, which displays
7435 current_buffer, are in the text between START and END. START is a
7436 buffer position, END is given as a distance from Z. Used in
7437 redisplay_internal for display optimization. */
7439 static INLINE int
7440 text_outside_line_unchanged_p (w, start, end)
7441 struct window *w;
7442 int start, end;
7444 int unchanged_p = 1;
7446 /* If text or overlays have changed, see where. */
7447 if (XFASTINT (w->last_modified) < MODIFF
7448 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7450 /* Gap in the line? */
7451 if (GPT < start || Z - GPT < end)
7452 unchanged_p = 0;
7454 /* Changes start in front of the line, or end after it? */
7455 if (unchanged_p
7456 && (BEG_UNCHANGED < start - 1
7457 || END_UNCHANGED < end))
7458 unchanged_p = 0;
7460 /* If selective display, can't optimize if changes start at the
7461 beginning of the line. */
7462 if (unchanged_p
7463 && INTEGERP (current_buffer->selective_display)
7464 && XINT (current_buffer->selective_display) > 0
7465 && (BEG_UNCHANGED < start || GPT <= start))
7466 unchanged_p = 0;
7469 return unchanged_p;
7473 /* Do a frame update, taking possible shortcuts into account. This is
7474 the main external entry point for redisplay.
7476 If the last redisplay displayed an echo area message and that message
7477 is no longer requested, we clear the echo area or bring back the
7478 mini-buffer if that is in use. */
7480 void
7481 redisplay ()
7483 redisplay_internal (0);
7486 /* Return 1 if point moved out of or into a composition. Otherwise
7487 return 0. PREV_BUF and PREV_PT are the last point buffer and
7488 position. BUF and PT are the current point buffer and position. */
7491 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7492 struct buffer *prev_buf, *buf;
7493 int prev_pt, pt;
7495 int start, end;
7496 Lisp_Object prop;
7497 Lisp_Object buffer;
7499 XSETBUFFER (buffer, buf);
7500 /* Check a composition at the last point if point moved within the
7501 same buffer. */
7502 if (prev_buf == buf)
7504 if (prev_pt == pt)
7505 /* Point didn't move. */
7506 return 0;
7508 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7509 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7510 && COMPOSITION_VALID_P (start, end, prop)
7511 && start < prev_pt && end > prev_pt)
7512 /* The last point was within the composition. Return 1 iff
7513 point moved out of the composition. */
7514 return (pt <= start || pt >= end);
7517 /* Check a composition at the current point. */
7518 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7519 && find_composition (pt, -1, &start, &end, &prop, buffer)
7520 && COMPOSITION_VALID_P (start, end, prop)
7521 && start < pt && end > pt);
7524 /* Reconsider the setting of B->clip_changed which is displayed
7525 in window W. */
7527 static INLINE void
7528 reconsider_clip_changes (w, b)
7529 struct window *w;
7530 struct buffer *b;
7532 if (b->prevent_redisplay_optimizations_p)
7533 b->clip_changed = 1;
7534 else if (b->clip_changed
7535 && !NILP (w->window_end_valid)
7536 && w->current_matrix->buffer == b
7537 && w->current_matrix->zv == BUF_ZV (b)
7538 && w->current_matrix->begv == BUF_BEGV (b))
7539 b->clip_changed = 0;
7541 /* If display wasn't paused, and W is not a tool bar window, see if
7542 point has been moved into or out of a composition. In that case,
7543 we set b->clip_changed to 1 to force updating the screen. If
7544 b->clip_changed has already been set to 1, we can skip this
7545 check. */
7546 if (!b->clip_changed
7547 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7549 int pt;
7551 if (w == XWINDOW (selected_window))
7552 pt = BUF_PT (current_buffer);
7553 else
7554 pt = marker_position (w->pointm);
7556 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7557 || pt != XINT (w->last_point))
7558 && check_point_in_composition (w->current_matrix->buffer,
7559 XINT (w->last_point),
7560 XBUFFER (w->buffer), pt))
7561 b->clip_changed = 1;
7566 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7567 response to any user action; therefore, we should preserve the echo
7568 area. (Actually, our caller does that job.) Perhaps in the future
7569 avoid recentering windows if it is not necessary; currently that
7570 causes some problems. */
7572 static void
7573 redisplay_internal (preserve_echo_area)
7574 int preserve_echo_area;
7576 struct window *w = XWINDOW (selected_window);
7577 struct frame *f = XFRAME (w->frame);
7578 int pause;
7579 int must_finish = 0;
7580 struct text_pos tlbufpos, tlendpos;
7581 int number_of_visible_frames;
7582 int count;
7583 struct frame *sf = SELECTED_FRAME ();
7585 /* Non-zero means redisplay has to consider all windows on all
7586 frames. Zero means, only selected_window is considered. */
7587 int consider_all_windows_p;
7589 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7591 /* No redisplay if running in batch mode or frame is not yet fully
7592 initialized, or redisplay is explicitly turned off by setting
7593 Vinhibit_redisplay. */
7594 if (noninteractive
7595 || !NILP (Vinhibit_redisplay)
7596 || !f->glyphs_initialized_p)
7597 return;
7599 /* The flag redisplay_performed_directly_p is set by
7600 direct_output_for_insert when it already did the whole screen
7601 update necessary. */
7602 if (redisplay_performed_directly_p)
7604 redisplay_performed_directly_p = 0;
7605 if (!hscroll_windows (selected_window))
7606 return;
7609 #ifdef USE_X_TOOLKIT
7610 if (popup_activated ())
7611 return;
7612 #endif
7614 /* I don't think this happens but let's be paranoid. */
7615 if (redisplaying_p)
7616 return;
7618 /* Record a function that resets redisplaying_p to its old value
7619 when we leave this function. */
7620 count = specpdl_ptr - specpdl;
7621 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7622 ++redisplaying_p;
7624 retry:
7625 pause = 0;
7626 reconsider_clip_changes (w, current_buffer);
7628 /* If new fonts have been loaded that make a glyph matrix adjustment
7629 necessary, do it. */
7630 if (fonts_changed_p)
7632 adjust_glyphs (NULL);
7633 ++windows_or_buffers_changed;
7634 fonts_changed_p = 0;
7637 if (! FRAME_WINDOW_P (sf)
7638 && previous_terminal_frame != sf)
7640 /* Since frames on an ASCII terminal share the same display
7641 area, displaying a different frame means redisplay the whole
7642 thing. */
7643 windows_or_buffers_changed++;
7644 SET_FRAME_GARBAGED (sf);
7645 XSETFRAME (Vterminal_frame, sf);
7647 previous_terminal_frame = sf;
7649 /* Set the visible flags for all frames. Do this before checking
7650 for resized or garbaged frames; they want to know if their frames
7651 are visible. See the comment in frame.h for
7652 FRAME_SAMPLE_VISIBILITY. */
7654 Lisp_Object tail, frame;
7656 number_of_visible_frames = 0;
7658 FOR_EACH_FRAME (tail, frame)
7660 struct frame *f = XFRAME (frame);
7662 FRAME_SAMPLE_VISIBILITY (f);
7663 if (FRAME_VISIBLE_P (f))
7664 ++number_of_visible_frames;
7665 clear_desired_matrices (f);
7669 /* Notice any pending interrupt request to change frame size. */
7670 do_pending_window_change (1);
7672 /* Clear frames marked as garbaged. */
7673 if (frame_garbaged)
7674 clear_garbaged_frames ();
7676 /* Build menubar and tool-bar items. */
7677 prepare_menu_bars ();
7679 if (windows_or_buffers_changed)
7680 update_mode_lines++;
7682 /* Detect case that we need to write or remove a star in the mode line. */
7683 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7685 w->update_mode_line = Qt;
7686 if (buffer_shared > 1)
7687 update_mode_lines++;
7690 /* If %c is in the mode line, update it if needed. */
7691 if (!NILP (w->column_number_displayed)
7692 /* This alternative quickly identifies a common case
7693 where no change is needed. */
7694 && !(PT == XFASTINT (w->last_point)
7695 && XFASTINT (w->last_modified) >= MODIFF
7696 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7697 && XFASTINT (w->column_number_displayed) != current_column ())
7698 w->update_mode_line = Qt;
7700 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7702 /* The variable buffer_shared is set in redisplay_window and
7703 indicates that we redisplay a buffer in different windows. See
7704 there. */
7705 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7707 /* If specs for an arrow have changed, do thorough redisplay
7708 to ensure we remove any arrow that should no longer exist. */
7709 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7710 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7711 consider_all_windows_p = windows_or_buffers_changed = 1;
7713 /* Normally the message* functions will have already displayed and
7714 updated the echo area, but the frame may have been trashed, or
7715 the update may have been preempted, so display the echo area
7716 again here. Checking both message buffers captures the case that
7717 the echo area should be cleared. */
7718 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7720 int window_height_changed_p = echo_area_display (0);
7721 must_finish = 1;
7723 if (fonts_changed_p)
7724 goto retry;
7725 else if (window_height_changed_p)
7727 consider_all_windows_p = 1;
7728 ++update_mode_lines;
7729 ++windows_or_buffers_changed;
7731 /* If window configuration was changed, frames may have been
7732 marked garbaged. Clear them or we will experience
7733 surprises wrt scrolling. */
7734 if (frame_garbaged)
7735 clear_garbaged_frames ();
7738 else if (EQ (selected_window, minibuf_window)
7739 && (current_buffer->clip_changed
7740 || XFASTINT (w->last_modified) < MODIFF
7741 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7742 && resize_mini_window (w, 0))
7744 /* Resized active mini-window to fit the size of what it is
7745 showing if its contents might have changed. */
7746 must_finish = 1;
7747 consider_all_windows_p = 1;
7748 ++windows_or_buffers_changed;
7749 ++update_mode_lines;
7751 /* If window configuration was changed, frames may have been
7752 marked garbaged. Clear them or we will experience
7753 surprises wrt scrolling. */
7754 if (frame_garbaged)
7755 clear_garbaged_frames ();
7759 /* If showing the region, and mark has changed, we must redisplay
7760 the whole window. The assignment to this_line_start_pos prevents
7761 the optimization directly below this if-statement. */
7762 if (((!NILP (Vtransient_mark_mode)
7763 && !NILP (XBUFFER (w->buffer)->mark_active))
7764 != !NILP (w->region_showing))
7765 || (!NILP (w->region_showing)
7766 && !EQ (w->region_showing,
7767 Fmarker_position (XBUFFER (w->buffer)->mark))))
7768 CHARPOS (this_line_start_pos) = 0;
7770 /* Optimize the case that only the line containing the cursor in the
7771 selected window has changed. Variables starting with this_ are
7772 set in display_line and record information about the line
7773 containing the cursor. */
7774 tlbufpos = this_line_start_pos;
7775 tlendpos = this_line_end_pos;
7776 if (!consider_all_windows_p
7777 && CHARPOS (tlbufpos) > 0
7778 && NILP (w->update_mode_line)
7779 && !current_buffer->clip_changed
7780 && FRAME_VISIBLE_P (XFRAME (w->frame))
7781 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7782 /* Make sure recorded data applies to current buffer, etc. */
7783 && this_line_buffer == current_buffer
7784 && current_buffer == XBUFFER (w->buffer)
7785 && NILP (w->force_start)
7786 /* Point must be on the line that we have info recorded about. */
7787 && PT >= CHARPOS (tlbufpos)
7788 && PT <= Z - CHARPOS (tlendpos)
7789 /* All text outside that line, including its final newline,
7790 must be unchanged */
7791 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7792 CHARPOS (tlendpos)))
7794 if (CHARPOS (tlbufpos) > BEGV
7795 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7796 && (CHARPOS (tlbufpos) == ZV
7797 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7798 /* Former continuation line has disappeared by becoming empty */
7799 goto cancel;
7800 else if (XFASTINT (w->last_modified) < MODIFF
7801 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7802 || MINI_WINDOW_P (w))
7804 /* We have to handle the case of continuation around a
7805 wide-column character (See the comment in indent.c around
7806 line 885).
7808 For instance, in the following case:
7810 -------- Insert --------
7811 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7812 J_I_ ==> J_I_ `^^' are cursors.
7813 ^^ ^^
7814 -------- --------
7816 As we have to redraw the line above, we should goto cancel. */
7818 struct it it;
7819 int line_height_before = this_line_pixel_height;
7821 /* Note that start_display will handle the case that the
7822 line starting at tlbufpos is a continuation lines. */
7823 start_display (&it, w, tlbufpos);
7825 /* Implementation note: It this still necessary? */
7826 if (it.current_x != this_line_start_x)
7827 goto cancel;
7829 TRACE ((stderr, "trying display optimization 1\n"));
7830 w->cursor.vpos = -1;
7831 overlay_arrow_seen = 0;
7832 it.vpos = this_line_vpos;
7833 it.current_y = this_line_y;
7834 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7835 display_line (&it);
7837 /* If line contains point, is not continued,
7838 and ends at same distance from eob as before, we win */
7839 if (w->cursor.vpos >= 0
7840 /* Line is not continued, otherwise this_line_start_pos
7841 would have been set to 0 in display_line. */
7842 && CHARPOS (this_line_start_pos)
7843 /* Line ends as before. */
7844 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7845 /* Line has same height as before. Otherwise other lines
7846 would have to be shifted up or down. */
7847 && this_line_pixel_height == line_height_before)
7849 /* If this is not the window's last line, we must adjust
7850 the charstarts of the lines below. */
7851 if (it.current_y < it.last_visible_y)
7853 struct glyph_row *row
7854 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7855 int delta, delta_bytes;
7857 if (Z - CHARPOS (tlendpos) == ZV)
7859 /* This line ends at end of (accessible part of)
7860 buffer. There is no newline to count. */
7861 delta = (Z
7862 - CHARPOS (tlendpos)
7863 - MATRIX_ROW_START_CHARPOS (row));
7864 delta_bytes = (Z_BYTE
7865 - BYTEPOS (tlendpos)
7866 - MATRIX_ROW_START_BYTEPOS (row));
7868 else
7870 /* This line ends in a newline. Must take
7871 account of the newline and the rest of the
7872 text that follows. */
7873 delta = (Z
7874 - CHARPOS (tlendpos)
7875 - MATRIX_ROW_START_CHARPOS (row));
7876 delta_bytes = (Z_BYTE
7877 - BYTEPOS (tlendpos)
7878 - MATRIX_ROW_START_BYTEPOS (row));
7881 increment_matrix_positions (w->current_matrix,
7882 this_line_vpos + 1,
7883 w->current_matrix->nrows,
7884 delta, delta_bytes);
7887 /* If this row displays text now but previously didn't,
7888 or vice versa, w->window_end_vpos may have to be
7889 adjusted. */
7890 if ((it.glyph_row - 1)->displays_text_p)
7892 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7893 XSETINT (w->window_end_vpos, this_line_vpos);
7895 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7896 && this_line_vpos > 0)
7897 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7898 w->window_end_valid = Qnil;
7900 /* Update hint: No need to try to scroll in update_window. */
7901 w->desired_matrix->no_scrolling_p = 1;
7903 #if GLYPH_DEBUG
7904 *w->desired_matrix->method = 0;
7905 debug_method_add (w, "optimization 1");
7906 #endif
7907 goto update;
7909 else
7910 goto cancel;
7912 else if (/* Cursor position hasn't changed. */
7913 PT == XFASTINT (w->last_point)
7914 /* Make sure the cursor was last displayed
7915 in this window. Otherwise we have to reposition it. */
7916 && 0 <= w->cursor.vpos
7917 && XINT (w->height) > w->cursor.vpos)
7919 if (!must_finish)
7921 do_pending_window_change (1);
7923 /* We used to always goto end_of_redisplay here, but this
7924 isn't enough if we have a blinking cursor. */
7925 if (w->cursor_off_p == w->last_cursor_off_p)
7926 goto end_of_redisplay;
7928 goto update;
7930 /* If highlighting the region, or if the cursor is in the echo area,
7931 then we can't just move the cursor. */
7932 else if (! (!NILP (Vtransient_mark_mode)
7933 && !NILP (current_buffer->mark_active))
7934 && (EQ (selected_window, current_buffer->last_selected_window)
7935 || highlight_nonselected_windows)
7936 && NILP (w->region_showing)
7937 && NILP (Vshow_trailing_whitespace)
7938 && !cursor_in_echo_area)
7940 struct it it;
7941 struct glyph_row *row;
7943 /* Skip from tlbufpos to PT and see where it is. Note that
7944 PT may be in invisible text. If so, we will end at the
7945 next visible position. */
7946 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7947 NULL, DEFAULT_FACE_ID);
7948 it.current_x = this_line_start_x;
7949 it.current_y = this_line_y;
7950 it.vpos = this_line_vpos;
7952 /* The call to move_it_to stops in front of PT, but
7953 moves over before-strings. */
7954 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7956 if (it.vpos == this_line_vpos
7957 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7958 row->enabled_p))
7960 xassert (this_line_vpos == it.vpos);
7961 xassert (this_line_y == it.current_y);
7962 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7963 goto update;
7965 else
7966 goto cancel;
7969 cancel:
7970 /* Text changed drastically or point moved off of line. */
7971 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7974 CHARPOS (this_line_start_pos) = 0;
7975 consider_all_windows_p |= buffer_shared > 1;
7976 ++clear_face_cache_count;
7979 /* Build desired matrices, and update the display. If
7980 consider_all_windows_p is non-zero, do it for all windows on all
7981 frames. Otherwise do it for selected_window, only. */
7983 if (consider_all_windows_p)
7985 Lisp_Object tail, frame;
7987 /* Clear the face cache eventually. */
7988 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7990 clear_face_cache (0);
7991 clear_face_cache_count = 0;
7994 /* Recompute # windows showing selected buffer. This will be
7995 incremented each time such a window is displayed. */
7996 buffer_shared = 0;
7998 FOR_EACH_FRAME (tail, frame)
8000 struct frame *f = XFRAME (frame);
8002 if (FRAME_WINDOW_P (f) || f == sf)
8004 /* Mark all the scroll bars to be removed; we'll redeem
8005 the ones we want when we redisplay their windows. */
8006 if (condemn_scroll_bars_hook)
8007 (*condemn_scroll_bars_hook) (f);
8009 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8010 redisplay_windows (FRAME_ROOT_WINDOW (f));
8012 /* Any scroll bars which redisplay_windows should have
8013 nuked should now go away. */
8014 if (judge_scroll_bars_hook)
8015 (*judge_scroll_bars_hook) (f);
8017 /* If fonts changed, display again. */
8018 if (fonts_changed_p)
8019 goto retry;
8021 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8023 /* See if we have to hscroll. */
8024 if (hscroll_windows (f->root_window))
8025 goto retry;
8027 /* Prevent various kinds of signals during display
8028 update. stdio is not robust about handling
8029 signals, which can cause an apparent I/O
8030 error. */
8031 if (interrupt_input)
8032 unrequest_sigio ();
8033 stop_polling ();
8035 /* Update the display. */
8036 set_window_update_flags (XWINDOW (f->root_window), 1);
8037 pause |= update_frame (f, 0, 0);
8038 if (pause)
8039 break;
8041 mark_window_display_accurate (f->root_window, 1);
8042 if (frame_up_to_date_hook)
8043 frame_up_to_date_hook (f);
8048 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8050 Lisp_Object mini_window;
8051 struct frame *mini_frame;
8053 redisplay_window (selected_window, 1);
8055 /* Compare desired and current matrices, perform output. */
8056 update:
8058 /* If fonts changed, display again. */
8059 if (fonts_changed_p)
8060 goto retry;
8062 /* Prevent various kinds of signals during display update.
8063 stdio is not robust about handling signals,
8064 which can cause an apparent I/O error. */
8065 if (interrupt_input)
8066 unrequest_sigio ();
8067 stop_polling ();
8069 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8071 if (hscroll_windows (selected_window))
8072 goto retry;
8074 XWINDOW (selected_window)->must_be_updated_p = 1;
8075 pause = update_frame (sf, 0, 0);
8078 /* We may have called echo_area_display at the top of this
8079 function. If the echo area is on another frame, that may
8080 have put text on a frame other than the selected one, so the
8081 above call to update_frame would not have caught it. Catch
8082 it here. */
8083 mini_window = FRAME_MINIBUF_WINDOW (sf);
8084 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8086 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8088 XWINDOW (mini_window)->must_be_updated_p = 1;
8089 pause |= update_frame (mini_frame, 0, 0);
8090 if (!pause && hscroll_windows (mini_window))
8091 goto retry;
8095 /* If display was paused because of pending input, make sure we do a
8096 thorough update the next time. */
8097 if (pause)
8099 /* Prevent the optimization at the beginning of
8100 redisplay_internal that tries a single-line update of the
8101 line containing the cursor in the selected window. */
8102 CHARPOS (this_line_start_pos) = 0;
8104 /* Let the overlay arrow be updated the next time. */
8105 if (!NILP (last_arrow_position))
8107 last_arrow_position = Qt;
8108 last_arrow_string = Qt;
8111 /* If we pause after scrolling, some rows in the current
8112 matrices of some windows are not valid. */
8113 if (!WINDOW_FULL_WIDTH_P (w)
8114 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8115 update_mode_lines = 1;
8118 /* Now text on frame agrees with windows, so put info into the
8119 windows for partial redisplay to follow. */
8120 if (!pause)
8122 register struct buffer *b = XBUFFER (w->buffer);
8124 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8125 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8126 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8127 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8129 if (consider_all_windows_p)
8130 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8131 else
8133 XSETFASTINT (w->last_point, BUF_PT (b));
8134 w->last_cursor = w->cursor;
8135 w->last_cursor_off_p = w->cursor_off_p;
8137 b->clip_changed = 0;
8138 b->prevent_redisplay_optimizations_p = 0;
8139 w->update_mode_line = Qnil;
8140 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8141 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8142 w->last_had_star
8143 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8144 ? Qt : Qnil);
8146 /* Record if we are showing a region, so can make sure to
8147 update it fully at next redisplay. */
8148 w->region_showing = (!NILP (Vtransient_mark_mode)
8149 && (EQ (selected_window,
8150 current_buffer->last_selected_window)
8151 || highlight_nonselected_windows)
8152 && !NILP (XBUFFER (w->buffer)->mark_active)
8153 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8154 : Qnil);
8156 w->window_end_valid = w->buffer;
8157 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8158 last_arrow_string = Voverlay_arrow_string;
8159 if (frame_up_to_date_hook != 0)
8160 (*frame_up_to_date_hook) (sf);
8162 w->current_matrix->buffer = b;
8163 w->current_matrix->begv = BUF_BEGV (b);
8164 w->current_matrix->zv = BUF_ZV (b);
8167 update_mode_lines = 0;
8168 windows_or_buffers_changed = 0;
8171 /* Start SIGIO interrupts coming again. Having them off during the
8172 code above makes it less likely one will discard output, but not
8173 impossible, since there might be stuff in the system buffer here.
8174 But it is much hairier to try to do anything about that. */
8175 if (interrupt_input)
8176 request_sigio ();
8177 start_polling ();
8179 /* If a frame has become visible which was not before, redisplay
8180 again, so that we display it. Expose events for such a frame
8181 (which it gets when becoming visible) don't call the parts of
8182 redisplay constructing glyphs, so simply exposing a frame won't
8183 display anything in this case. So, we have to display these
8184 frames here explicitly. */
8185 if (!pause)
8187 Lisp_Object tail, frame;
8188 int new_count = 0;
8190 FOR_EACH_FRAME (tail, frame)
8192 int this_is_visible = 0;
8194 if (XFRAME (frame)->visible)
8195 this_is_visible = 1;
8196 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8197 if (XFRAME (frame)->visible)
8198 this_is_visible = 1;
8200 if (this_is_visible)
8201 new_count++;
8204 if (new_count != number_of_visible_frames)
8205 windows_or_buffers_changed++;
8208 /* Change frame size now if a change is pending. */
8209 do_pending_window_change (1);
8211 /* If we just did a pending size change, or have additional
8212 visible frames, redisplay again. */
8213 if (windows_or_buffers_changed && !pause)
8214 goto retry;
8216 end_of_redisplay:;
8218 unbind_to (count, Qnil);
8222 /* Redisplay, but leave alone any recent echo area message unless
8223 another message has been requested in its place.
8225 This is useful in situations where you need to redisplay but no
8226 user action has occurred, making it inappropriate for the message
8227 area to be cleared. See tracking_off and
8228 wait_reading_process_input for examples of these situations. */
8230 void
8231 redisplay_preserve_echo_area ()
8233 if (!NILP (echo_area_buffer[1]))
8235 /* We have a previously displayed message, but no current
8236 message. Redisplay the previous message. */
8237 display_last_displayed_message_p = 1;
8238 redisplay_internal (1);
8239 display_last_displayed_message_p = 0;
8241 else
8242 redisplay_internal (1);
8246 /* Function registered with record_unwind_protect in
8247 redisplay_internal. Clears the flag indicating that a redisplay is
8248 in progress. */
8250 static Lisp_Object
8251 unwind_redisplay (old_redisplaying_p)
8252 Lisp_Object old_redisplaying_p;
8254 redisplaying_p = XFASTINT (old_redisplaying_p);
8255 return Qnil;
8259 /* Mark the display of windows in the window tree rooted at WINDOW as
8260 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8261 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8262 the next time redisplay_internal is called. */
8264 void
8265 mark_window_display_accurate (window, accurate_p)
8266 Lisp_Object window;
8267 int accurate_p;
8269 struct window *w;
8271 for (; !NILP (window); window = w->next)
8273 w = XWINDOW (window);
8275 if (BUFFERP (w->buffer))
8277 struct buffer *b = XBUFFER (w->buffer);
8279 XSETFASTINT (w->last_modified,
8280 accurate_p ? BUF_MODIFF (b) : 0);
8281 XSETFASTINT (w->last_overlay_modified,
8282 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8283 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8284 ? Qt : Qnil);
8286 #if 0 /* I don't think this is necessary because display_line does it.
8287 Let's check it. */
8288 /* Record if we are showing a region, so can make sure to
8289 update it fully at next redisplay. */
8290 w->region_showing
8291 = (!NILP (Vtransient_mark_mode)
8292 && (w == XWINDOW (current_buffer->last_selected_window)
8293 || highlight_nonselected_windows)
8294 && (!NILP (b->mark_active)
8295 ? Fmarker_position (b->mark)
8296 : Qnil));
8297 #endif
8299 if (accurate_p)
8301 b->clip_changed = 0;
8302 b->prevent_redisplay_optimizations_p = 0;
8303 w->current_matrix->buffer = b;
8304 w->current_matrix->begv = BUF_BEGV (b);
8305 w->current_matrix->zv = BUF_ZV (b);
8306 w->last_cursor = w->cursor;
8307 w->last_cursor_off_p = w->cursor_off_p;
8308 if (w == XWINDOW (selected_window))
8309 w->last_point = make_number (BUF_PT (b));
8310 else
8311 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8315 w->window_end_valid = w->buffer;
8316 w->update_mode_line = Qnil;
8318 if (!NILP (w->vchild))
8319 mark_window_display_accurate (w->vchild, accurate_p);
8320 if (!NILP (w->hchild))
8321 mark_window_display_accurate (w->hchild, accurate_p);
8324 if (accurate_p)
8326 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8327 last_arrow_string = Voverlay_arrow_string;
8329 else
8331 /* Force a thorough redisplay the next time by setting
8332 last_arrow_position and last_arrow_string to t, which is
8333 unequal to any useful value of Voverlay_arrow_... */
8334 last_arrow_position = Qt;
8335 last_arrow_string = Qt;
8340 /* Return value in display table DP (Lisp_Char_Table *) for character
8341 C. Since a display table doesn't have any parent, we don't have to
8342 follow parent. Do not call this function directly but use the
8343 macro DISP_CHAR_VECTOR. */
8345 Lisp_Object
8346 disp_char_vector (dp, c)
8347 struct Lisp_Char_Table *dp;
8348 int c;
8350 int code[4], i;
8351 Lisp_Object val;
8353 if (SINGLE_BYTE_CHAR_P (c))
8354 return (dp->contents[c]);
8356 SPLIT_CHAR (c, code[0], code[1], code[2]);
8357 if (code[1] < 32)
8358 code[1] = -1;
8359 else if (code[2] < 32)
8360 code[2] = -1;
8362 /* Here, the possible range of code[0] (== charset ID) is
8363 128..max_charset. Since the top level char table contains data
8364 for multibyte characters after 256th element, we must increment
8365 code[0] by 128 to get a correct index. */
8366 code[0] += 128;
8367 code[3] = -1; /* anchor */
8369 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8371 val = dp->contents[code[i]];
8372 if (!SUB_CHAR_TABLE_P (val))
8373 return (NILP (val) ? dp->defalt : val);
8376 /* Here, val is a sub char table. We return the default value of
8377 it. */
8378 return (dp->defalt);
8383 /***********************************************************************
8384 Window Redisplay
8385 ***********************************************************************/
8387 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8389 static void
8390 redisplay_windows (window)
8391 Lisp_Object window;
8393 while (!NILP (window))
8395 struct window *w = XWINDOW (window);
8397 if (!NILP (w->hchild))
8398 redisplay_windows (w->hchild);
8399 else if (!NILP (w->vchild))
8400 redisplay_windows (w->vchild);
8401 else
8402 redisplay_window (window, 0);
8404 window = w->next;
8409 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8410 DELTA is the number of bytes by which positions recorded in ROW
8411 differ from current buffer positions. */
8413 void
8414 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8415 struct window *w;
8416 struct glyph_row *row;
8417 struct glyph_matrix *matrix;
8418 int delta, delta_bytes, dy, dvpos;
8420 struct glyph *glyph = row->glyphs[TEXT_AREA];
8421 struct glyph *end = glyph + row->used[TEXT_AREA];
8422 int x = row->x;
8423 int pt_old = PT - delta;
8425 /* Skip over glyphs not having an object at the start of the row.
8426 These are special glyphs like truncation marks on terminal
8427 frames. */
8428 if (row->displays_text_p)
8429 while (glyph < end
8430 && INTEGERP (glyph->object)
8431 && glyph->charpos < 0)
8433 x += glyph->pixel_width;
8434 ++glyph;
8437 while (glyph < end
8438 && !INTEGERP (glyph->object)
8439 && (!BUFFERP (glyph->object)
8440 || glyph->charpos < pt_old))
8442 x += glyph->pixel_width;
8443 ++glyph;
8446 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8447 w->cursor.x = x;
8448 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8449 w->cursor.y = row->y + dy;
8451 if (w == XWINDOW (selected_window))
8453 if (!row->continued_p
8454 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8455 && row->x == 0)
8457 this_line_buffer = XBUFFER (w->buffer);
8459 CHARPOS (this_line_start_pos)
8460 = MATRIX_ROW_START_CHARPOS (row) + delta;
8461 BYTEPOS (this_line_start_pos)
8462 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8464 CHARPOS (this_line_end_pos)
8465 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8466 BYTEPOS (this_line_end_pos)
8467 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8469 this_line_y = w->cursor.y;
8470 this_line_pixel_height = row->height;
8471 this_line_vpos = w->cursor.vpos;
8472 this_line_start_x = row->x;
8474 else
8475 CHARPOS (this_line_start_pos) = 0;
8480 /* Run window scroll functions, if any, for WINDOW with new window
8481 start STARTP. Sets the window start of WINDOW to that position.
8483 We assume that the window's buffer is really current. */
8485 static INLINE struct text_pos
8486 run_window_scroll_functions (window, startp)
8487 Lisp_Object window;
8488 struct text_pos startp;
8490 struct window *w = XWINDOW (window);
8491 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8493 if (current_buffer != XBUFFER (w->buffer))
8494 abort ();
8496 if (!NILP (Vwindow_scroll_functions))
8498 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8499 make_number (CHARPOS (startp)));
8500 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8501 /* In case the hook functions switch buffers. */
8502 if (current_buffer != XBUFFER (w->buffer))
8503 set_buffer_internal_1 (XBUFFER (w->buffer));
8506 return startp;
8510 /* Modify the desired matrix of window W and W->vscroll so that the
8511 line containing the cursor is fully visible. */
8513 static void
8514 make_cursor_line_fully_visible (w)
8515 struct window *w;
8517 struct glyph_matrix *matrix;
8518 struct glyph_row *row;
8519 int window_height, header_line_height;
8521 /* It's not always possible to find the cursor, e.g, when a window
8522 is full of overlay strings. Don't do anything in that case. */
8523 if (w->cursor.vpos < 0)
8524 return;
8526 matrix = w->desired_matrix;
8527 row = MATRIX_ROW (matrix, w->cursor.vpos);
8529 /* If the cursor row is not partially visible, there's nothing
8530 to do. */
8531 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8532 return;
8534 /* If the row the cursor is in is taller than the window's height,
8535 it's not clear what to do, so do nothing. */
8536 window_height = window_box_height (w);
8537 if (row->height >= window_height)
8538 return;
8540 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8542 int dy = row->height - row->visible_height;
8543 w->vscroll = 0;
8544 w->cursor.y += dy;
8545 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8547 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8549 int dy = - (row->height - row->visible_height);
8550 w->vscroll = dy;
8551 w->cursor.y += dy;
8552 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8555 /* When we change the cursor y-position of the selected window,
8556 change this_line_y as well so that the display optimization for
8557 the cursor line of the selected window in redisplay_internal uses
8558 the correct y-position. */
8559 if (w == XWINDOW (selected_window))
8560 this_line_y = w->cursor.y;
8564 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8565 non-zero means only WINDOW is redisplayed in redisplay_internal.
8566 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8567 in redisplay_window to bring a partially visible line into view in
8568 the case that only the cursor has moved.
8570 Value is
8572 1 if scrolling succeeded
8574 0 if scrolling didn't find point.
8576 -1 if new fonts have been loaded so that we must interrupt
8577 redisplay, adjust glyph matrices, and try again. */
8579 static int
8580 try_scrolling (window, just_this_one_p, scroll_conservatively,
8581 scroll_step, temp_scroll_step)
8582 Lisp_Object window;
8583 int just_this_one_p;
8584 int scroll_conservatively, scroll_step;
8585 int temp_scroll_step;
8587 struct window *w = XWINDOW (window);
8588 struct frame *f = XFRAME (w->frame);
8589 struct text_pos scroll_margin_pos;
8590 struct text_pos pos;
8591 struct text_pos startp;
8592 struct it it;
8593 Lisp_Object window_end;
8594 int this_scroll_margin;
8595 int dy = 0;
8596 int scroll_max;
8597 int line_height, rc;
8598 int amount_to_scroll = 0;
8599 Lisp_Object aggressive;
8600 int height;
8602 #if GLYPH_DEBUG
8603 debug_method_add (w, "try_scrolling");
8604 #endif
8606 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8608 /* Compute scroll margin height in pixels. We scroll when point is
8609 within this distance from the top or bottom of the window. */
8610 if (scroll_margin > 0)
8612 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8613 this_scroll_margin *= CANON_Y_UNIT (f);
8615 else
8616 this_scroll_margin = 0;
8618 /* Compute how much we should try to scroll maximally to bring point
8619 into view. */
8620 if (scroll_step)
8621 scroll_max = scroll_step;
8622 else if (scroll_conservatively)
8623 scroll_max = scroll_conservatively;
8624 else if (temp_scroll_step)
8625 scroll_max = temp_scroll_step;
8626 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8627 || NUMBERP (current_buffer->scroll_up_aggressively))
8628 /* We're trying to scroll because of aggressive scrolling
8629 but no scroll_step is set. Choose an arbitrary one. Maybe
8630 there should be a variable for this. */
8631 scroll_max = 10;
8632 else
8633 scroll_max = 0;
8634 scroll_max *= CANON_Y_UNIT (f);
8636 /* Decide whether we have to scroll down. Start at the window end
8637 and move this_scroll_margin up to find the position of the scroll
8638 margin. */
8639 window_end = Fwindow_end (window, Qt);
8640 CHARPOS (scroll_margin_pos) = XINT (window_end);
8641 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8642 if (this_scroll_margin)
8644 start_display (&it, w, scroll_margin_pos);
8645 move_it_vertically (&it, - this_scroll_margin);
8646 scroll_margin_pos = it.current.pos;
8649 if (PT >= CHARPOS (scroll_margin_pos))
8651 int y0;
8653 /* Point is in the scroll margin at the bottom of the window, or
8654 below. Compute a new window start that makes point visible. */
8656 /* Compute the distance from the scroll margin to PT.
8657 Give up if the distance is greater than scroll_max. */
8658 start_display (&it, w, scroll_margin_pos);
8659 y0 = it.current_y;
8660 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8661 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8662 line_height = (it.max_ascent + it.max_descent
8663 ? it.max_ascent + it.max_descent
8664 : last_height);
8665 dy = it.current_y + line_height - y0;
8667 if (dy > scroll_max)
8668 return 0;
8670 /* Move the window start down. If scrolling conservatively,
8671 move it just enough down to make point visible. If
8672 scroll_step is set, move it down by scroll_step. */
8673 start_display (&it, w, startp);
8675 if (scroll_conservatively)
8676 amount_to_scroll = dy;
8677 else if (scroll_step || temp_scroll_step)
8678 amount_to_scroll = scroll_max;
8679 else
8681 aggressive = current_buffer->scroll_down_aggressively;
8682 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8683 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8684 if (NUMBERP (aggressive))
8685 amount_to_scroll = XFLOATINT (aggressive) * height;
8688 if (amount_to_scroll <= 0)
8689 return 0;
8691 move_it_vertically (&it, amount_to_scroll);
8692 startp = it.current.pos;
8694 else
8696 /* See if point is inside the scroll margin at the top of the
8697 window. */
8698 scroll_margin_pos = startp;
8699 if (this_scroll_margin)
8701 start_display (&it, w, startp);
8702 move_it_vertically (&it, this_scroll_margin);
8703 scroll_margin_pos = it.current.pos;
8706 if (PT < CHARPOS (scroll_margin_pos))
8708 /* Point is in the scroll margin at the top of the window or
8709 above what is displayed in the window. */
8710 int y0;
8712 /* Compute the vertical distance from PT to the scroll
8713 margin position. Give up if distance is greater than
8714 scroll_max. */
8715 SET_TEXT_POS (pos, PT, PT_BYTE);
8716 start_display (&it, w, pos);
8717 y0 = it.current_y;
8718 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8719 it.last_visible_y, -1,
8720 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8721 dy = it.current_y - y0;
8722 if (dy > scroll_max)
8723 return 0;
8725 /* Compute new window start. */
8726 start_display (&it, w, startp);
8728 if (scroll_conservatively)
8729 amount_to_scroll = dy;
8730 else if (scroll_step || temp_scroll_step)
8731 amount_to_scroll = scroll_max;
8732 else
8734 aggressive = current_buffer->scroll_up_aggressively;
8735 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8736 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8737 if (NUMBERP (aggressive))
8738 amount_to_scroll = XFLOATINT (aggressive) * height;
8741 if (amount_to_scroll <= 0)
8742 return 0;
8744 move_it_vertically (&it, - amount_to_scroll);
8745 startp = it.current.pos;
8749 /* Run window scroll functions. */
8750 startp = run_window_scroll_functions (window, startp);
8752 /* Display the window. Give up if new fonts are loaded, or if point
8753 doesn't appear. */
8754 if (!try_window (window, startp))
8755 rc = -1;
8756 else if (w->cursor.vpos < 0)
8758 clear_glyph_matrix (w->desired_matrix);
8759 rc = 0;
8761 else
8763 /* Maybe forget recorded base line for line number display. */
8764 if (!just_this_one_p
8765 || current_buffer->clip_changed
8766 || BEG_UNCHANGED < CHARPOS (startp))
8767 w->base_line_number = Qnil;
8769 /* If cursor ends up on a partially visible line, shift display
8770 lines up or down. */
8771 make_cursor_line_fully_visible (w);
8772 rc = 1;
8775 return rc;
8779 /* Compute a suitable window start for window W if display of W starts
8780 on a continuation line. Value is non-zero if a new window start
8781 was computed.
8783 The new window start will be computed, based on W's width, starting
8784 from the start of the continued line. It is the start of the
8785 screen line with the minimum distance from the old start W->start. */
8787 static int
8788 compute_window_start_on_continuation_line (w)
8789 struct window *w;
8791 struct text_pos pos, start_pos;
8792 int window_start_changed_p = 0;
8794 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8796 /* If window start is on a continuation line... Window start may be
8797 < BEGV in case there's invisible text at the start of the
8798 buffer (M-x rmail, for example). */
8799 if (CHARPOS (start_pos) > BEGV
8800 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8802 struct it it;
8803 struct glyph_row *row;
8805 /* Handle the case that the window start is out of range. */
8806 if (CHARPOS (start_pos) < BEGV)
8807 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8808 else if (CHARPOS (start_pos) > ZV)
8809 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8811 /* Find the start of the continued line. This should be fast
8812 because scan_buffer is fast (newline cache). */
8813 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8814 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8815 row, DEFAULT_FACE_ID);
8816 reseat_at_previous_visible_line_start (&it);
8818 /* If the line start is "too far" away from the window start,
8819 say it takes too much time to compute a new window start. */
8820 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8821 < XFASTINT (w->height) * XFASTINT (w->width))
8823 int min_distance, distance;
8825 /* Move forward by display lines to find the new window
8826 start. If window width was enlarged, the new start can
8827 be expected to be > the old start. If window width was
8828 decreased, the new window start will be < the old start.
8829 So, we're looking for the display line start with the
8830 minimum distance from the old window start. */
8831 pos = it.current.pos;
8832 min_distance = INFINITY;
8833 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8834 distance < min_distance)
8836 min_distance = distance;
8837 pos = it.current.pos;
8838 move_it_by_lines (&it, 1, 0);
8841 /* Set the window start there. */
8842 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8843 window_start_changed_p = 1;
8847 return window_start_changed_p;
8851 /* Try cursor movement in case text has not changes in window WINDOW,
8852 with window start STARTP. Value is
8854 1 if successful
8856 0 if this method cannot be used
8858 -1 if we know we have to scroll the display. *SCROLL_STEP is
8859 set to 1, under certain circumstances, if we want to scroll as
8860 if scroll-step were set to 1. See the code. */
8862 static int
8863 try_cursor_movement (window, startp, scroll_step)
8864 Lisp_Object window;
8865 struct text_pos startp;
8866 int *scroll_step;
8868 struct window *w = XWINDOW (window);
8869 struct frame *f = XFRAME (w->frame);
8870 int rc = 0;
8872 /* Handle case where text has not changed, only point, and it has
8873 not moved off the frame. */
8874 if (/* Point may be in this window. */
8875 PT >= CHARPOS (startp)
8876 /* If we don't check this, we are called to move the cursor in a
8877 horizontally split window with a current matrix that doesn't
8878 fit the display. */
8879 && !windows_or_buffers_changed
8880 /* Selective display hasn't changed. */
8881 && !current_buffer->clip_changed
8882 /* If force-mode-line-update was called, really redisplay;
8883 that's how redisplay is forced after e.g. changing
8884 buffer-invisibility-spec. */
8885 && NILP (w->update_mode_line)
8886 /* Can't use this case if highlighting a region. When a
8887 region exists, cursor movement has to do more than just
8888 set the cursor. */
8889 && !(!NILP (Vtransient_mark_mode)
8890 && !NILP (current_buffer->mark_active))
8891 && NILP (w->region_showing)
8892 && NILP (Vshow_trailing_whitespace)
8893 /* Right after splitting windows, last_point may be nil. */
8894 && INTEGERP (w->last_point)
8895 /* This code is not used for mini-buffer for the sake of the case
8896 of redisplaying to replace an echo area message; since in
8897 that case the mini-buffer contents per se are usually
8898 unchanged. This code is of no real use in the mini-buffer
8899 since the handling of this_line_start_pos, etc., in redisplay
8900 handles the same cases. */
8901 && !EQ (window, minibuf_window)
8902 /* When splitting windows or for new windows, it happens that
8903 redisplay is called with a nil window_end_vpos or one being
8904 larger than the window. This should really be fixed in
8905 window.c. I don't have this on my list, now, so we do
8906 approximately the same as the old redisplay code. --gerd. */
8907 && INTEGERP (w->window_end_vpos)
8908 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8909 && (FRAME_WINDOW_P (f)
8910 || !MARKERP (Voverlay_arrow_position)
8911 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8913 int this_scroll_margin;
8914 struct glyph_row *row;
8916 #if GLYPH_DEBUG
8917 debug_method_add (w, "cursor movement");
8918 #endif
8920 /* Scroll if point within this distance from the top or bottom
8921 of the window. This is a pixel value. */
8922 this_scroll_margin = max (0, scroll_margin);
8923 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8924 this_scroll_margin *= CANON_Y_UNIT (f);
8926 /* Start with the row the cursor was displayed during the last
8927 not paused redisplay. Give up if that row is not valid. */
8928 if (w->last_cursor.vpos < 0
8929 || w->last_cursor.vpos >= w->current_matrix->nrows)
8930 rc = -1;
8931 else
8933 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8934 if (row->mode_line_p)
8935 ++row;
8936 if (!row->enabled_p)
8937 rc = -1;
8940 if (rc == 0)
8942 int scroll_p = 0;
8944 if (PT > XFASTINT (w->last_point))
8946 /* Point has moved forward. */
8947 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8949 while (MATRIX_ROW_END_CHARPOS (row) < PT
8950 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8952 xassert (row->enabled_p);
8953 ++row;
8956 /* The end position of a row equals the start position
8957 of the next row. If PT is there, we would rather
8958 display it in the next line. */
8959 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
8960 && MATRIX_ROW_END_CHARPOS (row) == PT
8961 && !cursor_row_p (w, row))
8962 ++row;
8964 /* If within the scroll margin, scroll. Note that
8965 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
8966 the next line would be drawn, and that
8967 this_scroll_margin can be zero. */
8968 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8969 || PT > MATRIX_ROW_END_CHARPOS (row)
8970 /* Line is completely visible last line in window
8971 and PT is to be set in the next line. */
8972 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8973 && PT == MATRIX_ROW_END_CHARPOS (row)
8974 && !row->ends_at_zv_p
8975 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8976 scroll_p = 1;
8978 else if (PT < XFASTINT (w->last_point))
8980 /* Cursor has to be moved backward. Note that PT >=
8981 CHARPOS (startp) because of the outer
8982 if-statement. */
8983 while (!row->mode_line_p
8984 && (MATRIX_ROW_START_CHARPOS (row) > PT
8985 || (MATRIX_ROW_START_CHARPOS (row) == PT
8986 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8987 && (row->y > this_scroll_margin
8988 || CHARPOS (startp) == BEGV))
8990 xassert (row->enabled_p);
8991 --row;
8994 /* Consider the following case: Window starts at BEGV,
8995 there is invisible, intangible text at BEGV, so that
8996 display starts at some point START > BEGV. It can
8997 happen that we are called with PT somewhere between
8998 BEGV and START. Try to handle that case. */
8999 if (row < w->current_matrix->rows
9000 || row->mode_line_p)
9002 row = w->current_matrix->rows;
9003 if (row->mode_line_p)
9004 ++row;
9007 /* Due to newlines in overlay strings, we may have to
9008 skip forward over overlay strings. */
9009 while (MATRIX_ROW_END_CHARPOS (row) == PT
9010 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
9011 && !row->ends_at_zv_p)
9012 ++row;
9014 /* If within the scroll margin, scroll. */
9015 if (row->y < this_scroll_margin
9016 && CHARPOS (startp) != BEGV)
9017 scroll_p = 1;
9020 if (PT < MATRIX_ROW_START_CHARPOS (row)
9021 || PT > MATRIX_ROW_END_CHARPOS (row))
9023 /* if PT is not in the glyph row, give up. */
9024 rc = -1;
9026 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9028 /* If we end up in a partially visible line, let's make it
9029 fully visible, except when it's taller than the window,
9030 in which case we can't do much about it. */
9031 if (row->height > window_box_height (w))
9033 *scroll_step = 1;
9034 rc = -1;
9036 else
9038 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9039 try_window (window, startp);
9040 make_cursor_line_fully_visible (w);
9041 rc = 1;
9044 else if (scroll_p)
9045 rc = -1;
9046 else
9048 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9049 rc = 1;
9054 return rc;
9058 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9059 selected_window is redisplayed. */
9061 static void
9062 redisplay_window (window, just_this_one_p)
9063 Lisp_Object window;
9064 int just_this_one_p;
9066 struct window *w = XWINDOW (window);
9067 struct frame *f = XFRAME (w->frame);
9068 struct buffer *buffer = XBUFFER (w->buffer);
9069 struct buffer *old = current_buffer;
9070 struct text_pos lpoint, opoint, startp;
9071 int update_mode_line;
9072 int tem;
9073 struct it it;
9074 /* Record it now because it's overwritten. */
9075 int current_matrix_up_to_date_p = 0;
9076 int temp_scroll_step = 0;
9077 int count = specpdl_ptr - specpdl;
9078 int rc;
9080 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9081 opoint = lpoint;
9083 /* W must be a leaf window here. */
9084 xassert (!NILP (w->buffer));
9085 #if GLYPH_DEBUG
9086 *w->desired_matrix->method = 0;
9087 #endif
9089 specbind (Qinhibit_point_motion_hooks, Qt);
9091 reconsider_clip_changes (w, buffer);
9093 /* Has the mode line to be updated? */
9094 update_mode_line = (!NILP (w->update_mode_line)
9095 || update_mode_lines
9096 || buffer->clip_changed);
9098 if (MINI_WINDOW_P (w))
9100 if (w == XWINDOW (echo_area_window)
9101 && !NILP (echo_area_buffer[0]))
9103 if (update_mode_line)
9104 /* We may have to update a tty frame's menu bar or a
9105 tool-bar. Example `M-x C-h C-h C-g'. */
9106 goto finish_menu_bars;
9107 else
9108 /* We've already displayed the echo area glyphs in this window. */
9109 goto finish_scroll_bars;
9111 else if (w != XWINDOW (minibuf_window))
9113 /* W is a mini-buffer window, but it's not the currently
9114 active one, so clear it. */
9115 int yb = window_text_bottom_y (w);
9116 struct glyph_row *row;
9117 int y;
9119 for (y = 0, row = w->desired_matrix->rows;
9120 y < yb;
9121 y += row->height, ++row)
9122 blank_row (w, row, y);
9123 goto finish_scroll_bars;
9127 /* Otherwise set up data on this window; select its buffer and point
9128 value. */
9129 /* Really select the buffer, for the sake of buffer-local
9130 variables. */
9131 set_buffer_internal_1 (XBUFFER (w->buffer));
9132 SET_TEXT_POS (opoint, PT, PT_BYTE);
9134 current_matrix_up_to_date_p
9135 = (!NILP (w->window_end_valid)
9136 && !current_buffer->clip_changed
9137 && XFASTINT (w->last_modified) >= MODIFF
9138 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9140 /* When windows_or_buffers_changed is non-zero, we can't rely on
9141 the window end being valid, so set it to nil there. */
9142 if (windows_or_buffers_changed)
9144 /* If window starts on a continuation line, maybe adjust the
9145 window start in case the window's width changed. */
9146 if (XMARKER (w->start)->buffer == current_buffer)
9147 compute_window_start_on_continuation_line (w);
9149 w->window_end_valid = Qnil;
9152 /* Some sanity checks. */
9153 CHECK_WINDOW_END (w);
9154 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9155 abort ();
9156 if (BYTEPOS (opoint) < CHARPOS (opoint))
9157 abort ();
9159 /* If %c is in mode line, update it if needed. */
9160 if (!NILP (w->column_number_displayed)
9161 /* This alternative quickly identifies a common case
9162 where no change is needed. */
9163 && !(PT == XFASTINT (w->last_point)
9164 && XFASTINT (w->last_modified) >= MODIFF
9165 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9166 && XFASTINT (w->column_number_displayed) != current_column ())
9167 update_mode_line = 1;
9169 /* Count number of windows showing the selected buffer. An indirect
9170 buffer counts as its base buffer. */
9171 if (!just_this_one_p)
9173 struct buffer *current_base, *window_base;
9174 current_base = current_buffer;
9175 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9176 if (current_base->base_buffer)
9177 current_base = current_base->base_buffer;
9178 if (window_base->base_buffer)
9179 window_base = window_base->base_buffer;
9180 if (current_base == window_base)
9181 buffer_shared++;
9184 /* Point refers normally to the selected window. For any other
9185 window, set up appropriate value. */
9186 if (!EQ (window, selected_window))
9188 int new_pt = XMARKER (w->pointm)->charpos;
9189 int new_pt_byte = marker_byte_position (w->pointm);
9190 if (new_pt < BEGV)
9192 new_pt = BEGV;
9193 new_pt_byte = BEGV_BYTE;
9194 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9196 else if (new_pt > (ZV - 1))
9198 new_pt = ZV;
9199 new_pt_byte = ZV_BYTE;
9200 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9203 /* We don't use SET_PT so that the point-motion hooks don't run. */
9204 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9207 /* If any of the character widths specified in the display table
9208 have changed, invalidate the width run cache. It's true that
9209 this may be a bit late to catch such changes, but the rest of
9210 redisplay goes (non-fatally) haywire when the display table is
9211 changed, so why should we worry about doing any better? */
9212 if (current_buffer->width_run_cache)
9214 struct Lisp_Char_Table *disptab = buffer_display_table ();
9216 if (! disptab_matches_widthtab (disptab,
9217 XVECTOR (current_buffer->width_table)))
9219 invalidate_region_cache (current_buffer,
9220 current_buffer->width_run_cache,
9221 BEG, Z);
9222 recompute_width_table (current_buffer, disptab);
9226 /* If window-start is screwed up, choose a new one. */
9227 if (XMARKER (w->start)->buffer != current_buffer)
9228 goto recenter;
9230 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9232 /* If someone specified a new starting point but did not insist,
9233 check whether it can be used. */
9234 if (!NILP (w->optional_new_start)
9235 && CHARPOS (startp) >= BEGV
9236 && CHARPOS (startp) <= ZV)
9238 w->optional_new_start = Qnil;
9239 start_display (&it, w, startp);
9240 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9241 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9242 if (IT_CHARPOS (it) == PT)
9243 w->force_start = Qt;
9246 /* Handle case where place to start displaying has been specified,
9247 unless the specified location is outside the accessible range. */
9248 if (!NILP (w->force_start)
9249 || w->frozen_window_start_p)
9251 w->force_start = Qnil;
9252 w->vscroll = 0;
9253 w->window_end_valid = Qnil;
9255 /* Forget any recorded base line for line number display. */
9256 if (!current_matrix_up_to_date_p
9257 || current_buffer->clip_changed)
9258 w->base_line_number = Qnil;
9260 /* Redisplay the mode line. Select the buffer properly for that.
9261 Also, run the hook window-scroll-functions
9262 because we have scrolled. */
9263 /* Note, we do this after clearing force_start because
9264 if there's an error, it is better to forget about force_start
9265 than to get into an infinite loop calling the hook functions
9266 and having them get more errors. */
9267 if (!update_mode_line
9268 || ! NILP (Vwindow_scroll_functions))
9270 update_mode_line = 1;
9271 w->update_mode_line = Qt;
9272 startp = run_window_scroll_functions (window, startp);
9275 XSETFASTINT (w->last_modified, 0);
9276 XSETFASTINT (w->last_overlay_modified, 0);
9277 if (CHARPOS (startp) < BEGV)
9278 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9279 else if (CHARPOS (startp) > ZV)
9280 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9282 /* Redisplay, then check if cursor has been set during the
9283 redisplay. Give up if new fonts were loaded. */
9284 if (!try_window (window, startp))
9286 w->force_start = Qt;
9287 clear_glyph_matrix (w->desired_matrix);
9288 goto restore_buffers;
9291 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9293 /* If point does not appear, try to move point so it does
9294 appear. The desired matrix has been built above, so we
9295 can use it here. */
9296 int window_height;
9297 struct glyph_row *row;
9299 window_height = window_box_height (w) / 2;
9300 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9301 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9302 ++row;
9304 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9305 MATRIX_ROW_START_BYTEPOS (row));
9307 if (w != XWINDOW (selected_window))
9308 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9309 else if (current_buffer == old)
9310 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9312 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9314 /* If we are highlighting the region, then we just changed
9315 the region, so redisplay to show it. */
9316 if (!NILP (Vtransient_mark_mode)
9317 && !NILP (current_buffer->mark_active))
9319 clear_glyph_matrix (w->desired_matrix);
9320 if (!try_window (window, startp))
9321 goto restore_buffers;
9325 make_cursor_line_fully_visible (w);
9326 #if GLYPH_DEBUG
9327 debug_method_add (w, "forced window start");
9328 #endif
9329 goto done;
9332 /* Handle case where text has not changed, only point, and it has
9333 not moved off the frame. */
9334 if (current_matrix_up_to_date_p
9335 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9336 rc != 0))
9338 if (rc == -1)
9339 goto try_to_scroll;
9340 else
9341 goto done;
9343 /* If current starting point was originally the beginning of a line
9344 but no longer is, find a new starting point. */
9345 else if (!NILP (w->start_at_line_beg)
9346 && !(CHARPOS (startp) <= BEGV
9347 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9349 #if GLYPH_DEBUG
9350 debug_method_add (w, "recenter 1");
9351 #endif
9352 goto recenter;
9355 /* Try scrolling with try_window_id. */
9356 else if (/* Windows and buffers haven't changed. */
9357 !windows_or_buffers_changed
9358 /* Window must be either use window-based redisplay or
9359 be full width. */
9360 && (FRAME_WINDOW_P (f)
9361 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9362 && !MINI_WINDOW_P (w)
9363 /* Point is not known NOT to appear in window. */
9364 && PT >= CHARPOS (startp)
9365 && XFASTINT (w->last_modified)
9366 /* Window is not hscrolled. */
9367 && XFASTINT (w->hscroll) == 0
9368 /* Selective display has not changed. */
9369 && !current_buffer->clip_changed
9370 /* Current matrix is up to date. */
9371 && !NILP (w->window_end_valid)
9372 /* Can't use this case if highlighting a region because
9373 a cursor movement will do more than just set the cursor. */
9374 && !(!NILP (Vtransient_mark_mode)
9375 && !NILP (current_buffer->mark_active))
9376 && NILP (w->region_showing)
9377 && NILP (Vshow_trailing_whitespace)
9378 /* Overlay arrow position and string not changed. */
9379 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9380 && EQ (last_arrow_string, Voverlay_arrow_string)
9381 /* Value is > 0 if update has been done, it is -1 if we
9382 know that the same window start will not work. It is 0
9383 if unsuccessful for some other reason. */
9384 && (tem = try_window_id (w)) != 0)
9386 #if GLYPH_DEBUG
9387 debug_method_add (w, "try_window_id %d", tem);
9388 #endif
9390 if (fonts_changed_p)
9391 goto restore_buffers;
9392 if (tem > 0)
9393 goto done;
9395 /* Otherwise try_window_id has returned -1 which means that we
9396 don't want the alternative below this comment to execute. */
9398 else if (CHARPOS (startp) >= BEGV
9399 && CHARPOS (startp) <= ZV
9400 && PT >= CHARPOS (startp)
9401 && (CHARPOS (startp) < ZV
9402 /* Avoid starting at end of buffer. */
9403 || CHARPOS (startp) == BEGV
9404 || (XFASTINT (w->last_modified) >= MODIFF
9405 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9407 #if GLYPH_DEBUG
9408 debug_method_add (w, "same window start");
9409 #endif
9411 /* Try to redisplay starting at same place as before.
9412 If point has not moved off frame, accept the results. */
9413 if (!current_matrix_up_to_date_p
9414 /* Don't use try_window_reusing_current_matrix in this case
9415 because a window scroll function can have changed the
9416 buffer. */
9417 || !NILP (Vwindow_scroll_functions)
9418 || MINI_WINDOW_P (w)
9419 || !try_window_reusing_current_matrix (w))
9421 IF_DEBUG (debug_method_add (w, "1"));
9422 try_window (window, startp);
9425 if (fonts_changed_p)
9426 goto restore_buffers;
9428 if (w->cursor.vpos >= 0)
9430 if (!just_this_one_p
9431 || current_buffer->clip_changed
9432 || BEG_UNCHANGED < CHARPOS (startp))
9433 /* Forget any recorded base line for line number display. */
9434 w->base_line_number = Qnil;
9436 make_cursor_line_fully_visible (w);
9437 goto done;
9439 else
9440 clear_glyph_matrix (w->desired_matrix);
9443 try_to_scroll:
9445 XSETFASTINT (w->last_modified, 0);
9446 XSETFASTINT (w->last_overlay_modified, 0);
9448 /* Redisplay the mode line. Select the buffer properly for that. */
9449 if (!update_mode_line)
9451 update_mode_line = 1;
9452 w->update_mode_line = Qt;
9455 /* Try to scroll by specified few lines. */
9456 if ((scroll_conservatively
9457 || scroll_step
9458 || temp_scroll_step
9459 || NUMBERP (current_buffer->scroll_up_aggressively)
9460 || NUMBERP (current_buffer->scroll_down_aggressively))
9461 && !current_buffer->clip_changed
9462 && CHARPOS (startp) >= BEGV
9463 && CHARPOS (startp) <= ZV)
9465 /* The function returns -1 if new fonts were loaded, 1 if
9466 successful, 0 if not successful. */
9467 int rc = try_scrolling (window, just_this_one_p,
9468 scroll_conservatively,
9469 scroll_step,
9470 temp_scroll_step);
9471 if (rc > 0)
9472 goto done;
9473 else if (rc < 0)
9474 goto restore_buffers;
9477 /* Finally, just choose place to start which centers point */
9479 recenter:
9481 #if GLYPH_DEBUG
9482 debug_method_add (w, "recenter");
9483 #endif
9485 /* w->vscroll = 0; */
9487 /* Forget any previously recorded base line for line number display. */
9488 if (!current_matrix_up_to_date_p
9489 || current_buffer->clip_changed)
9490 w->base_line_number = Qnil;
9492 /* Move backward half the height of the window. */
9493 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9494 it.current_y = it.last_visible_y;
9495 move_it_vertically_backward (&it, it.last_visible_y / 2);
9496 xassert (IT_CHARPOS (it) >= BEGV);
9498 /* The function move_it_vertically_backward may move over more
9499 than the specified y-distance. If it->w is small, e.g. a
9500 mini-buffer window, we may end up in front of the window's
9501 display area. Start displaying at the start of the line
9502 containing PT in this case. */
9503 if (it.current_y <= 0)
9505 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9506 move_it_vertically (&it, 0);
9507 xassert (IT_CHARPOS (it) <= PT);
9508 it.current_y = 0;
9511 it.current_x = it.hpos = 0;
9513 /* Set startp here explicitly in case that helps avoid an infinite loop
9514 in case the window-scroll-functions functions get errors. */
9515 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9517 /* Run scroll hooks. */
9518 startp = run_window_scroll_functions (window, it.current.pos);
9520 /* Redisplay the window. */
9521 if (!current_matrix_up_to_date_p
9522 || windows_or_buffers_changed
9523 /* Don't use try_window_reusing_current_matrix in this case
9524 because it can have changed the buffer. */
9525 || !NILP (Vwindow_scroll_functions)
9526 || !just_this_one_p
9527 || MINI_WINDOW_P (w)
9528 || !try_window_reusing_current_matrix (w))
9529 try_window (window, startp);
9531 /* If new fonts have been loaded (due to fontsets), give up. We
9532 have to start a new redisplay since we need to re-adjust glyph
9533 matrices. */
9534 if (fonts_changed_p)
9535 goto restore_buffers;
9537 /* If cursor did not appear assume that the middle of the window is
9538 in the first line of the window. Do it again with the next line.
9539 (Imagine a window of height 100, displaying two lines of height
9540 60. Moving back 50 from it->last_visible_y will end in the first
9541 line.) */
9542 if (w->cursor.vpos < 0)
9544 if (!NILP (w->window_end_valid)
9545 && PT >= Z - XFASTINT (w->window_end_pos))
9547 clear_glyph_matrix (w->desired_matrix);
9548 move_it_by_lines (&it, 1, 0);
9549 try_window (window, it.current.pos);
9551 else if (PT < IT_CHARPOS (it))
9553 clear_glyph_matrix (w->desired_matrix);
9554 move_it_by_lines (&it, -1, 0);
9555 try_window (window, it.current.pos);
9557 else
9559 /* Not much we can do about it. */
9563 /* Consider the following case: Window starts at BEGV, there is
9564 invisible, intangible text at BEGV, so that display starts at
9565 some point START > BEGV. It can happen that we are called with
9566 PT somewhere between BEGV and START. Try to handle that case. */
9567 if (w->cursor.vpos < 0)
9569 struct glyph_row *row = w->current_matrix->rows;
9570 if (row->mode_line_p)
9571 ++row;
9572 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9575 make_cursor_line_fully_visible (w);
9577 done:
9579 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9580 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9581 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9582 ? Qt : Qnil);
9584 /* Display the mode line, if we must. */
9585 if ((update_mode_line
9586 /* If window not full width, must redo its mode line
9587 if (a) the window to its side is being redone and
9588 (b) we do a frame-based redisplay. This is a consequence
9589 of how inverted lines are drawn in frame-based redisplay. */
9590 || (!just_this_one_p
9591 && !FRAME_WINDOW_P (f)
9592 && !WINDOW_FULL_WIDTH_P (w))
9593 /* Line number to display. */
9594 || INTEGERP (w->base_line_pos)
9595 /* Column number is displayed and different from the one displayed. */
9596 || (!NILP (w->column_number_displayed)
9597 && XFASTINT (w->column_number_displayed) != current_column ()))
9598 /* This means that the window has a mode line. */
9599 && (WINDOW_WANTS_MODELINE_P (w)
9600 || WINDOW_WANTS_HEADER_LINE_P (w)))
9602 Lisp_Object old_selected_frame;
9604 old_selected_frame = selected_frame;
9606 XSETFRAME (selected_frame, f);
9607 display_mode_lines (w);
9608 selected_frame = old_selected_frame;
9610 /* If mode line height has changed, arrange for a thorough
9611 immediate redisplay using the correct mode line height. */
9612 if (WINDOW_WANTS_MODELINE_P (w)
9613 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9615 fonts_changed_p = 1;
9616 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9617 = DESIRED_MODE_LINE_HEIGHT (w);
9620 /* If top line height has changed, arrange for a thorough
9621 immediate redisplay using the correct mode line height. */
9622 if (WINDOW_WANTS_HEADER_LINE_P (w)
9623 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9625 fonts_changed_p = 1;
9626 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9627 = DESIRED_HEADER_LINE_HEIGHT (w);
9630 if (fonts_changed_p)
9631 goto restore_buffers;
9634 if (!line_number_displayed
9635 && !BUFFERP (w->base_line_pos))
9637 w->base_line_pos = Qnil;
9638 w->base_line_number = Qnil;
9641 finish_menu_bars:
9643 /* When we reach a frame's selected window, redo the frame's menu bar. */
9644 if (update_mode_line
9645 && EQ (FRAME_SELECTED_WINDOW (f), window))
9647 int redisplay_menu_p = 0;
9649 if (FRAME_WINDOW_P (f))
9651 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9652 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9653 #else
9654 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9655 #endif
9657 else
9658 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9660 if (redisplay_menu_p)
9661 display_menu_bar (w);
9663 #ifdef HAVE_WINDOW_SYSTEM
9664 if (WINDOWP (f->tool_bar_window)
9665 && (FRAME_TOOL_BAR_LINES (f) > 0
9666 || auto_resize_tool_bars_p))
9667 redisplay_tool_bar (f);
9668 #endif
9671 finish_scroll_bars:
9673 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9675 int start, end, whole;
9677 /* Calculate the start and end positions for the current window.
9678 At some point, it would be nice to choose between scrollbars
9679 which reflect the whole buffer size, with special markers
9680 indicating narrowing, and scrollbars which reflect only the
9681 visible region.
9683 Note that mini-buffers sometimes aren't displaying any text. */
9684 if (!MINI_WINDOW_P (w)
9685 || (w == XWINDOW (minibuf_window)
9686 && NILP (echo_area_buffer[0])))
9688 whole = ZV - BEGV;
9689 start = marker_position (w->start) - BEGV;
9690 /* I don't think this is guaranteed to be right. For the
9691 moment, we'll pretend it is. */
9692 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9694 if (end < start)
9695 end = start;
9696 if (whole < (end - start))
9697 whole = end - start;
9699 else
9700 start = end = whole = 0;
9702 /* Indicate what this scroll bar ought to be displaying now. */
9703 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9705 /* Note that we actually used the scroll bar attached to this
9706 window, so it shouldn't be deleted at the end of redisplay. */
9707 (*redeem_scroll_bar_hook) (w);
9710 restore_buffers:
9712 /* Restore current_buffer and value of point in it. */
9713 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9714 set_buffer_internal_1 (old);
9715 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9717 unbind_to (count, Qnil);
9721 /* Build the complete desired matrix of WINDOW with a window start
9722 buffer position POS. Value is non-zero if successful. It is zero
9723 if fonts were loaded during redisplay which makes re-adjusting
9724 glyph matrices necessary. */
9727 try_window (window, pos)
9728 Lisp_Object window;
9729 struct text_pos pos;
9731 struct window *w = XWINDOW (window);
9732 struct it it;
9733 struct glyph_row *last_text_row = NULL;
9735 /* Make POS the new window start. */
9736 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9738 /* Mark cursor position as unknown. No overlay arrow seen. */
9739 w->cursor.vpos = -1;
9740 overlay_arrow_seen = 0;
9742 /* Initialize iterator and info to start at POS. */
9743 start_display (&it, w, pos);
9745 /* Display all lines of W. */
9746 while (it.current_y < it.last_visible_y)
9748 if (display_line (&it))
9749 last_text_row = it.glyph_row - 1;
9750 if (fonts_changed_p)
9751 return 0;
9754 /* If bottom moved off end of frame, change mode line percentage. */
9755 if (XFASTINT (w->window_end_pos) <= 0
9756 && Z != IT_CHARPOS (it))
9757 w->update_mode_line = Qt;
9759 /* Set window_end_pos to the offset of the last character displayed
9760 on the window from the end of current_buffer. Set
9761 window_end_vpos to its row number. */
9762 if (last_text_row)
9764 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9765 w->window_end_bytepos
9766 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9767 XSETFASTINT (w->window_end_pos,
9768 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9769 XSETFASTINT (w->window_end_vpos,
9770 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9771 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9772 ->displays_text_p);
9774 else
9776 w->window_end_bytepos = 0;
9777 XSETFASTINT (w->window_end_pos, 0);
9778 XSETFASTINT (w->window_end_vpos, 0);
9781 /* But that is not valid info until redisplay finishes. */
9782 w->window_end_valid = Qnil;
9783 return 1;
9788 /************************************************************************
9789 Window redisplay reusing current matrix when buffer has not changed
9790 ************************************************************************/
9792 /* Try redisplay of window W showing an unchanged buffer with a
9793 different window start than the last time it was displayed by
9794 reusing its current matrix. Value is non-zero if successful.
9795 W->start is the new window start. */
9797 static int
9798 try_window_reusing_current_matrix (w)
9799 struct window *w;
9801 struct frame *f = XFRAME (w->frame);
9802 struct glyph_row *row, *bottom_row;
9803 struct it it;
9804 struct run run;
9805 struct text_pos start, new_start;
9806 int nrows_scrolled, i;
9807 struct glyph_row *last_text_row;
9808 struct glyph_row *last_reused_text_row;
9809 struct glyph_row *start_row;
9810 int start_vpos, min_y, max_y;
9812 if (/* This function doesn't handle terminal frames. */
9813 !FRAME_WINDOW_P (f)
9814 /* Don't try to reuse the display if windows have been split
9815 or such. */
9816 || windows_or_buffers_changed)
9817 return 0;
9819 /* Can't do this if region may have changed. */
9820 if ((!NILP (Vtransient_mark_mode)
9821 && !NILP (current_buffer->mark_active))
9822 || !NILP (w->region_showing)
9823 || !NILP (Vshow_trailing_whitespace))
9824 return 0;
9826 /* If top-line visibility has changed, give up. */
9827 if (WINDOW_WANTS_HEADER_LINE_P (w)
9828 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9829 return 0;
9831 /* Give up if old or new display is scrolled vertically. We could
9832 make this function handle this, but right now it doesn't. */
9833 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9834 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9835 return 0;
9837 /* The variable new_start now holds the new window start. The old
9838 start `start' can be determined from the current matrix. */
9839 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9840 start = start_row->start.pos;
9841 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9843 /* Clear the desired matrix for the display below. */
9844 clear_glyph_matrix (w->desired_matrix);
9846 if (CHARPOS (new_start) <= CHARPOS (start))
9848 int first_row_y;
9850 IF_DEBUG (debug_method_add (w, "twu1"));
9852 /* Display up to a row that can be reused. The variable
9853 last_text_row is set to the last row displayed that displays
9854 text. Note that it.vpos == 0 if or if not there is a
9855 header-line; it's not the same as the MATRIX_ROW_VPOS! */
9856 start_display (&it, w, new_start);
9857 first_row_y = it.current_y;
9858 w->cursor.vpos = -1;
9859 last_text_row = last_reused_text_row = NULL;
9861 while (it.current_y < it.last_visible_y
9862 && IT_CHARPOS (it) < CHARPOS (start)
9863 && !fonts_changed_p)
9864 if (display_line (&it))
9865 last_text_row = it.glyph_row - 1;
9867 /* A value of current_y < last_visible_y means that we stopped
9868 at the previous window start, which in turn means that we
9869 have at least one reusable row. */
9870 if (it.current_y < it.last_visible_y)
9872 /* IT.vpos always starts from 0; it counts text lines. */
9873 nrows_scrolled = it.vpos;
9875 /* Find PT if not already found in the lines displayed. */
9876 if (w->cursor.vpos < 0)
9878 int dy = it.current_y - first_row_y;
9880 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9881 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9883 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9884 && PT < MATRIX_ROW_END_CHARPOS (row))
9886 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9887 dy, nrows_scrolled);
9888 break;
9891 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9892 break;
9894 ++row;
9897 /* Give up if point was not found. This shouldn't
9898 happen often; not more often than with try_window
9899 itself. */
9900 if (w->cursor.vpos < 0)
9902 clear_glyph_matrix (w->desired_matrix);
9903 return 0;
9907 /* Scroll the display. Do it before the current matrix is
9908 changed. The problem here is that update has not yet
9909 run, i.e. part of the current matrix is not up to date.
9910 scroll_run_hook will clear the cursor, and use the
9911 current matrix to get the height of the row the cursor is
9912 in. */
9913 run.current_y = first_row_y;
9914 run.desired_y = it.current_y;
9915 run.height = it.last_visible_y - it.current_y;
9917 if (run.height > 0 && run.current_y != run.desired_y)
9919 update_begin (f);
9920 rif->update_window_begin_hook (w);
9921 rif->clear_mouse_face (w);
9922 rif->scroll_run_hook (w, &run);
9923 rif->update_window_end_hook (w, 0, 0);
9924 update_end (f);
9927 /* Shift current matrix down by nrows_scrolled lines. */
9928 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9929 rotate_matrix (w->current_matrix,
9930 start_vpos,
9931 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9932 nrows_scrolled);
9934 /* Disable lines not reused. */
9935 for (i = 0; i < it.vpos; ++i)
9936 (start_row + i)->enabled_p = 0;
9938 /* Re-compute Y positions. */
9939 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9940 max_y = it.last_visible_y;
9941 for (row = start_row + nrows_scrolled;
9942 row < bottom_row;
9943 ++row)
9945 row->y = it.current_y;
9947 if (row->y < min_y)
9948 row->visible_height = row->height - (min_y - row->y);
9949 else if (row->y + row->height > max_y)
9950 row->visible_height
9951 = row->height - (row->y + row->height - max_y);
9952 else
9953 row->visible_height = row->height;
9955 it.current_y += row->height;
9957 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9958 last_reused_text_row = row;
9959 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9960 break;
9964 /* Update window_end_pos etc.; last_reused_text_row is the last
9965 reused row from the current matrix containing text, if any.
9966 The value of last_text_row is the last displayed line
9967 containing text. */
9968 if (last_reused_text_row)
9970 w->window_end_bytepos
9971 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9972 XSETFASTINT (w->window_end_pos,
9973 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9974 XSETFASTINT (w->window_end_vpos,
9975 MATRIX_ROW_VPOS (last_reused_text_row,
9976 w->current_matrix));
9978 else if (last_text_row)
9980 w->window_end_bytepos
9981 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9982 XSETFASTINT (w->window_end_pos,
9983 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9984 XSETFASTINT (w->window_end_vpos,
9985 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9987 else
9989 /* This window must be completely empty. */
9990 w->window_end_bytepos = 0;
9991 XSETFASTINT (w->window_end_pos, 0);
9992 XSETFASTINT (w->window_end_vpos, 0);
9994 w->window_end_valid = Qnil;
9996 /* Update hint: don't try scrolling again in update_window. */
9997 w->desired_matrix->no_scrolling_p = 1;
9999 #if GLYPH_DEBUG
10000 debug_method_add (w, "try_window_reusing_current_matrix 1");
10001 #endif
10002 return 1;
10004 else if (CHARPOS (new_start) > CHARPOS (start))
10006 struct glyph_row *pt_row, *row;
10007 struct glyph_row *first_reusable_row;
10008 struct glyph_row *first_row_to_display;
10009 int dy;
10010 int yb = window_text_bottom_y (w);
10012 IF_DEBUG (debug_method_add (w, "twu2"));
10014 /* Find the row starting at new_start, if there is one. Don't
10015 reuse a partially visible line at the end. */
10016 first_reusable_row = start_row;
10017 while (first_reusable_row->enabled_p
10018 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10019 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10020 < CHARPOS (new_start)))
10021 ++first_reusable_row;
10023 /* Give up if there is no row to reuse. */
10024 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10025 || !first_reusable_row->enabled_p
10026 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10027 != CHARPOS (new_start)))
10028 return 0;
10030 /* We can reuse fully visible rows beginning with
10031 first_reusable_row to the end of the window. Set
10032 first_row_to_display to the first row that cannot be reused.
10033 Set pt_row to the row containing point, if there is any. */
10034 first_row_to_display = first_reusable_row;
10035 pt_row = NULL;
10036 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10038 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10039 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10040 pt_row = first_row_to_display;
10042 ++first_row_to_display;
10045 /* Start displaying at the start of first_row_to_display. */
10046 xassert (first_row_to_display->y < yb);
10047 init_to_row_start (&it, w, first_row_to_display);
10048 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10049 - start_vpos);
10050 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10051 - nrows_scrolled);
10052 it.current_y = (first_row_to_display->y - first_reusable_row->y
10053 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10055 /* Display lines beginning with first_row_to_display in the
10056 desired matrix. Set last_text_row to the last row displayed
10057 that displays text. */
10058 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10059 if (pt_row == NULL)
10060 w->cursor.vpos = -1;
10061 last_text_row = NULL;
10062 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10063 if (display_line (&it))
10064 last_text_row = it.glyph_row - 1;
10066 /* Give up If point isn't in a row displayed or reused. */
10067 if (w->cursor.vpos < 0)
10069 clear_glyph_matrix (w->desired_matrix);
10070 return 0;
10073 /* If point is in a reused row, adjust y and vpos of the cursor
10074 position. */
10075 if (pt_row)
10077 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10078 w->current_matrix);
10079 w->cursor.y -= first_reusable_row->y;
10082 /* Scroll the display. */
10083 run.current_y = first_reusable_row->y;
10084 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10085 run.height = it.last_visible_y - run.current_y;
10086 dy = run.current_y - run.desired_y;
10088 if (run.height)
10090 struct frame *f = XFRAME (WINDOW_FRAME (w));
10091 update_begin (f);
10092 rif->update_window_begin_hook (w);
10093 rif->clear_mouse_face (w);
10094 rif->scroll_run_hook (w, &run);
10095 rif->update_window_end_hook (w, 0, 0);
10096 update_end (f);
10099 /* Adjust Y positions of reused rows. */
10100 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10101 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10102 max_y = it.last_visible_y;
10103 for (row = first_reusable_row; row < first_row_to_display; ++row)
10105 row->y -= dy;
10106 if (row->y < min_y)
10107 row->visible_height = row->height - (min_y - row->y);
10108 else if (row->y + row->height > max_y)
10109 row->visible_height
10110 = row->height - (row->y + row->height - max_y);
10111 else
10112 row->visible_height = row->height;
10115 /* Disable rows not reused. */
10116 while (row < bottom_row)
10118 row->enabled_p = 0;
10119 ++row;
10122 /* Scroll the current matrix. */
10123 xassert (nrows_scrolled > 0);
10124 rotate_matrix (w->current_matrix,
10125 start_vpos,
10126 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10127 -nrows_scrolled);
10129 /* Adjust window end. A null value of last_text_row means that
10130 the window end is in reused rows which in turn means that
10131 only its vpos can have changed. */
10132 if (last_text_row)
10134 w->window_end_bytepos
10135 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10136 XSETFASTINT (w->window_end_pos,
10137 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10138 XSETFASTINT (w->window_end_vpos,
10139 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10141 else
10143 XSETFASTINT (w->window_end_vpos,
10144 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10147 w->window_end_valid = Qnil;
10148 w->desired_matrix->no_scrolling_p = 1;
10150 #if GLYPH_DEBUG
10151 debug_method_add (w, "try_window_reusing_current_matrix 2");
10152 #endif
10153 return 1;
10156 return 0;
10161 /************************************************************************
10162 Window redisplay reusing current matrix when buffer has changed
10163 ************************************************************************/
10165 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
10166 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
10167 int *, int *));
10168 static struct glyph_row *
10169 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10170 struct glyph_row *));
10173 /* Return the last row in MATRIX displaying text. If row START is
10174 non-null, start searching with that row. IT gives the dimensions
10175 of the display. Value is null if matrix is empty; otherwise it is
10176 a pointer to the row found. */
10178 static struct glyph_row *
10179 find_last_row_displaying_text (matrix, it, start)
10180 struct glyph_matrix *matrix;
10181 struct it *it;
10182 struct glyph_row *start;
10184 struct glyph_row *row, *row_found;
10186 /* Set row_found to the last row in IT->w's current matrix
10187 displaying text. The loop looks funny but think of partially
10188 visible lines. */
10189 row_found = NULL;
10190 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10191 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10193 xassert (row->enabled_p);
10194 row_found = row;
10195 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10196 break;
10197 ++row;
10200 return row_found;
10204 /* Return the last row in the current matrix of W that is not affected
10205 by changes at the start of current_buffer that occurred since the
10206 last time W was redisplayed. Value is null if no such row exists.
10208 The global variable beg_unchanged has to contain the number of
10209 bytes unchanged at the start of current_buffer. BEG +
10210 beg_unchanged is the buffer position of the first changed byte in
10211 current_buffer. Characters at positions < BEG + beg_unchanged are
10212 at the same buffer positions as they were when the current matrix
10213 was built. */
10215 static struct glyph_row *
10216 get_last_unchanged_at_beg_row (w)
10217 struct window *w;
10219 int first_changed_pos = BEG + BEG_UNCHANGED;
10220 struct glyph_row *row;
10221 struct glyph_row *row_found = NULL;
10222 int yb = window_text_bottom_y (w);
10224 /* Find the last row displaying unchanged text. */
10225 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10226 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10227 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10229 if (/* If row ends before first_changed_pos, it is unchanged,
10230 except in some case. */
10231 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10232 /* When row ends in ZV and we write at ZV it is not
10233 unchanged. */
10234 && !row->ends_at_zv_p
10235 /* When first_changed_pos is the end of a continued line,
10236 row is not unchanged because it may be no longer
10237 continued. */
10238 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10239 && row->continued_p))
10240 row_found = row;
10242 /* Stop if last visible row. */
10243 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10244 break;
10246 ++row;
10249 return row_found;
10253 /* Find the first glyph row in the current matrix of W that is not
10254 affected by changes at the end of current_buffer since the last
10255 time the window was redisplayed. Return in *DELTA the number of
10256 chars by which buffer positions in unchanged text at the end of
10257 current_buffer must be adjusted. Return in *DELTA_BYTES the
10258 corresponding number of bytes. Value is null if no such row
10259 exists, i.e. all rows are affected by changes. */
10261 static struct glyph_row *
10262 get_first_unchanged_at_end_row (w, delta, delta_bytes)
10263 struct window *w;
10264 int *delta, *delta_bytes;
10266 struct glyph_row *row;
10267 struct glyph_row *row_found = NULL;
10269 *delta = *delta_bytes = 0;
10271 /* A value of window_end_pos >= end_unchanged means that the window
10272 end is in the range of changed text. If so, there is no
10273 unchanged row at the end of W's current matrix. */
10274 xassert (!NILP (w->window_end_valid));
10275 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10276 return NULL;
10278 /* Set row to the last row in W's current matrix displaying text. */
10279 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10281 /* If matrix is entirely empty, no unchanged row exists. */
10282 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10284 /* The value of row is the last glyph row in the matrix having a
10285 meaningful buffer position in it. The end position of row
10286 corresponds to window_end_pos. This allows us to translate
10287 buffer positions in the current matrix to current buffer
10288 positions for characters not in changed text. */
10289 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10290 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10291 int last_unchanged_pos, last_unchanged_pos_old;
10292 struct glyph_row *first_text_row
10293 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10295 *delta = Z - Z_old;
10296 *delta_bytes = Z_BYTE - Z_BYTE_old;
10298 /* Set last_unchanged_pos to the buffer position of the last
10299 character in the buffer that has not been changed. Z is the
10300 index + 1 of the last byte in current_buffer, i.e. by
10301 subtracting end_unchanged we get the index of the last
10302 unchanged character, and we have to add BEG to get its buffer
10303 position. */
10304 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10305 last_unchanged_pos_old = last_unchanged_pos - *delta;
10307 /* Search backward from ROW for a row displaying a line that
10308 starts at a minimum position >= last_unchanged_pos_old. */
10309 while (row >= first_text_row)
10311 xassert (row->enabled_p);
10312 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
10314 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10315 row_found = row;
10316 --row;
10320 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
10321 return row_found;
10325 /* Make sure that glyph rows in the current matrix of window W
10326 reference the same glyph memory as corresponding rows in the
10327 frame's frame matrix. This function is called after scrolling W's
10328 current matrix on a terminal frame in try_window_id and
10329 try_window_reusing_current_matrix. */
10331 static void
10332 sync_frame_with_window_matrix_rows (w)
10333 struct window *w;
10335 struct frame *f = XFRAME (w->frame);
10336 struct glyph_row *window_row, *window_row_end, *frame_row;
10338 /* Preconditions: W must be a leaf window and full-width. Its frame
10339 must have a frame matrix. */
10340 xassert (NILP (w->hchild) && NILP (w->vchild));
10341 xassert (WINDOW_FULL_WIDTH_P (w));
10342 xassert (!FRAME_WINDOW_P (f));
10344 /* If W is a full-width window, glyph pointers in W's current matrix
10345 have, by definition, to be the same as glyph pointers in the
10346 corresponding frame matrix. */
10347 window_row = w->current_matrix->rows;
10348 window_row_end = window_row + w->current_matrix->nrows;
10349 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10350 while (window_row < window_row_end)
10352 int area;
10354 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10355 frame_row->glyphs[area] = window_row->glyphs[area];
10357 /* Disable frame rows whose corresponding window rows have
10358 been disabled in try_window_id. */
10359 if (!window_row->enabled_p)
10360 frame_row->enabled_p = 0;
10362 ++window_row, ++frame_row;
10367 /* Find the glyph row in window W containing CHARPOS. Consider all
10368 rows between START and END (not inclusive). END null means search
10369 all rows to the end of the display area of W. Value is the row
10370 containing CHARPOS or null. */
10372 static struct glyph_row *
10373 row_containing_pos (w, charpos, start, end)
10374 struct window *w;
10375 int charpos;
10376 struct glyph_row *start, *end;
10378 struct glyph_row *row = start;
10379 int last_y;
10381 /* If we happen to start on a header-line, skip that. */
10382 if (row->mode_line_p)
10383 ++row;
10385 if ((end && row >= end) || !row->enabled_p)
10386 return NULL;
10388 last_y = window_text_bottom_y (w);
10390 while ((end == NULL || row < end)
10391 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10392 /* The end position of a row equals the start
10393 position of the next row. If CHARPOS is there, we
10394 would rather display it in the next line, except
10395 when this line ends in ZV. */
10396 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10397 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10398 || !row->ends_at_zv_p)))
10399 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10400 ++row;
10402 /* Give up if CHARPOS not found. */
10403 if ((end && row >= end)
10404 || charpos < MATRIX_ROW_START_CHARPOS (row)
10405 || charpos > MATRIX_ROW_END_CHARPOS (row))
10406 row = NULL;
10408 return row;
10412 /* Try to redisplay window W by reusing its existing display. W's
10413 current matrix must be up to date when this function is called,
10414 i.e. window_end_valid must not be nil.
10416 Value is
10418 1 if display has been updated
10419 0 if otherwise unsuccessful
10420 -1 if redisplay with same window start is known not to succeed
10422 The following steps are performed:
10424 1. Find the last row in the current matrix of W that is not
10425 affected by changes at the start of current_buffer. If no such row
10426 is found, give up.
10428 2. Find the first row in W's current matrix that is not affected by
10429 changes at the end of current_buffer. Maybe there is no such row.
10431 3. Display lines beginning with the row + 1 found in step 1 to the
10432 row found in step 2 or, if step 2 didn't find a row, to the end of
10433 the window.
10435 4. If cursor is not known to appear on the window, give up.
10437 5. If display stopped at the row found in step 2, scroll the
10438 display and current matrix as needed.
10440 6. Maybe display some lines at the end of W, if we must. This can
10441 happen under various circumstances, like a partially visible line
10442 becoming fully visible, or because newly displayed lines are displayed
10443 in smaller font sizes.
10445 7. Update W's window end information. */
10447 /* Check that window end is what we expect it to be. */
10449 static int
10450 try_window_id (w)
10451 struct window *w;
10453 struct frame *f = XFRAME (w->frame);
10454 struct glyph_matrix *current_matrix = w->current_matrix;
10455 struct glyph_matrix *desired_matrix = w->desired_matrix;
10456 struct glyph_row *last_unchanged_at_beg_row;
10457 struct glyph_row *first_unchanged_at_end_row;
10458 struct glyph_row *row;
10459 struct glyph_row *bottom_row;
10460 int bottom_vpos;
10461 struct it it;
10462 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10463 struct text_pos start_pos;
10464 struct run run;
10465 int first_unchanged_at_end_vpos = 0;
10466 struct glyph_row *last_text_row, *last_text_row_at_end;
10467 struct text_pos start;
10469 SET_TEXT_POS_FROM_MARKER (start, w->start);
10471 /* Check pre-conditions. Window end must be valid, otherwise
10472 the current matrix would not be up to date. */
10473 xassert (!NILP (w->window_end_valid));
10474 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10475 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10477 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10478 only if buffer has really changed. The reason is that the gap is
10479 initially at Z for freshly visited files. The code below would
10480 set end_unchanged to 0 in that case. */
10481 if (MODIFF > SAVE_MODIFF
10482 /* This seems to happen sometimes after saving a buffer. */
10483 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10485 if (GPT - BEG < BEG_UNCHANGED)
10486 BEG_UNCHANGED = GPT - BEG;
10487 if (Z - GPT < END_UNCHANGED)
10488 END_UNCHANGED = Z - GPT;
10491 /* If window starts after a line end, and the last change is in
10492 front of that newline, then changes don't affect the display.
10493 This case happens with stealth-fontification. Note that although
10494 the display is unchanged, glyph positions in the matrix have to
10495 be adjusted, of course. */
10496 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10497 if (CHARPOS (start) > BEGV
10498 && Z - END_UNCHANGED < CHARPOS (start) - 1
10499 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10500 && PT < MATRIX_ROW_END_CHARPOS (row))
10502 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10503 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10505 if (delta)
10507 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10508 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10510 increment_matrix_positions (w->current_matrix,
10511 MATRIX_ROW_VPOS (r0, current_matrix),
10512 MATRIX_ROW_VPOS (r1, current_matrix),
10513 delta, delta_bytes);
10516 #if 0 /* If changes are all in front of the window start, the
10517 distance of the last displayed glyph from Z hasn't
10518 changed. */
10519 w->window_end_pos
10520 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10521 w->window_end_bytepos
10522 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10523 #endif
10525 return 1;
10528 /* Return quickly if changes are all below what is displayed in the
10529 window, and if PT is in the window. */
10530 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10531 && PT < MATRIX_ROW_END_CHARPOS (row))
10533 /* We have to update window end positions because the buffer's
10534 size has changed. */
10535 w->window_end_pos
10536 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10537 w->window_end_bytepos
10538 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10540 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10541 row = row_containing_pos (w, PT, row, NULL);
10542 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10543 return 2;
10546 /* Check that window start agrees with the start of the first glyph
10547 row in its current matrix. Check this after we know the window
10548 start is not in changed text, otherwise positions would not be
10549 comparable. */
10550 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10551 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10552 return 0;
10554 /* Compute the position at which we have to start displaying new
10555 lines. Some of the lines at the top of the window might be
10556 reusable because they are not displaying changed text. Find the
10557 last row in W's current matrix not affected by changes at the
10558 start of current_buffer. Value is null if changes start in the
10559 first line of window. */
10560 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10561 if (last_unchanged_at_beg_row)
10563 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10564 start_pos = it.current.pos;
10566 /* Start displaying new lines in the desired matrix at the same
10567 vpos we would use in the current matrix, i.e. below
10568 last_unchanged_at_beg_row. */
10569 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10570 current_matrix);
10571 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10572 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10574 xassert (it.hpos == 0 && it.current_x == 0);
10576 else
10578 /* There are no reusable lines at the start of the window.
10579 Start displaying in the first line. */
10580 start_display (&it, w, start);
10581 start_pos = it.current.pos;
10584 /* Find the first row that is not affected by changes at the end of
10585 the buffer. Value will be null if there is no unchanged row, in
10586 which case we must redisplay to the end of the window. delta
10587 will be set to the value by which buffer positions beginning with
10588 first_unchanged_at_end_row have to be adjusted due to text
10589 changes. */
10590 first_unchanged_at_end_row
10591 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10592 IF_DEBUG (debug_delta = delta);
10593 IF_DEBUG (debug_delta_bytes = delta_bytes);
10595 /* Set stop_pos to the buffer position up to which we will have to
10596 display new lines. If first_unchanged_at_end_row != NULL, this
10597 is the buffer position of the start of the line displayed in that
10598 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10599 that we don't stop at a buffer position. */
10600 stop_pos = 0;
10601 if (first_unchanged_at_end_row)
10603 xassert (last_unchanged_at_beg_row == NULL
10604 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10606 /* If this is a continuation line, move forward to the next one
10607 that isn't. Changes in lines above affect this line.
10608 Caution: this may move first_unchanged_at_end_row to a row
10609 not displaying text. */
10610 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10611 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10612 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10613 < it.last_visible_y))
10614 ++first_unchanged_at_end_row;
10616 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10617 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10618 >= it.last_visible_y))
10619 first_unchanged_at_end_row = NULL;
10620 else
10622 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10623 + delta);
10624 first_unchanged_at_end_vpos
10625 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10626 xassert (stop_pos >= Z - END_UNCHANGED);
10629 else if (last_unchanged_at_beg_row == NULL)
10630 return 0;
10633 #if GLYPH_DEBUG
10635 /* Either there is no unchanged row at the end, or the one we have
10636 now displays text. This is a necessary condition for the window
10637 end pos calculation at the end of this function. */
10638 xassert (first_unchanged_at_end_row == NULL
10639 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10641 debug_last_unchanged_at_beg_vpos
10642 = (last_unchanged_at_beg_row
10643 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10644 : -1);
10645 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10647 #endif /* GLYPH_DEBUG != 0 */
10650 /* Display new lines. Set last_text_row to the last new line
10651 displayed which has text on it, i.e. might end up as being the
10652 line where the window_end_vpos is. */
10653 w->cursor.vpos = -1;
10654 last_text_row = NULL;
10655 overlay_arrow_seen = 0;
10656 while (it.current_y < it.last_visible_y
10657 && !fonts_changed_p
10658 && (first_unchanged_at_end_row == NULL
10659 || IT_CHARPOS (it) < stop_pos))
10661 if (display_line (&it))
10662 last_text_row = it.glyph_row - 1;
10665 if (fonts_changed_p)
10666 return -1;
10669 /* Compute differences in buffer positions, y-positions etc. for
10670 lines reused at the bottom of the window. Compute what we can
10671 scroll. */
10672 if (first_unchanged_at_end_row
10673 /* No lines reused because we displayed everything up to the
10674 bottom of the window. */
10675 && it.current_y < it.last_visible_y)
10677 dvpos = (it.vpos
10678 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10679 current_matrix));
10680 dy = it.current_y - first_unchanged_at_end_row->y;
10681 run.current_y = first_unchanged_at_end_row->y;
10682 run.desired_y = run.current_y + dy;
10683 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10685 else
10687 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10688 first_unchanged_at_end_row = NULL;
10690 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10693 /* Find the cursor if not already found. We have to decide whether
10694 PT will appear on this window (it sometimes doesn't, but this is
10695 not a very frequent case.) This decision has to be made before
10696 the current matrix is altered. A value of cursor.vpos < 0 means
10697 that PT is either in one of the lines beginning at
10698 first_unchanged_at_end_row or below the window. Don't care for
10699 lines that might be displayed later at the window end; as
10700 mentioned, this is not a frequent case. */
10701 if (w->cursor.vpos < 0)
10703 /* Cursor in unchanged rows at the top? */
10704 if (PT < CHARPOS (start_pos)
10705 && last_unchanged_at_beg_row)
10707 row = row_containing_pos (w, PT,
10708 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10709 last_unchanged_at_beg_row + 1);
10710 if (row)
10711 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10714 /* Start from first_unchanged_at_end_row looking for PT. */
10715 else if (first_unchanged_at_end_row)
10717 row = row_containing_pos (w, PT - delta,
10718 first_unchanged_at_end_row, NULL);
10719 if (row)
10720 set_cursor_from_row (w, row, w->current_matrix, delta,
10721 delta_bytes, dy, dvpos);
10724 /* Give up if cursor was not found. */
10725 if (w->cursor.vpos < 0)
10727 clear_glyph_matrix (w->desired_matrix);
10728 return -1;
10732 /* Don't let the cursor end in the scroll margins. */
10734 int this_scroll_margin, cursor_height;
10736 this_scroll_margin = max (0, scroll_margin);
10737 this_scroll_margin = min (this_scroll_margin,
10738 XFASTINT (w->height) / 4);
10739 this_scroll_margin *= CANON_Y_UNIT (it.f);
10740 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10742 if ((w->cursor.y < this_scroll_margin
10743 && CHARPOS (start) > BEGV)
10744 /* Don't take scroll margin into account at the bottom because
10745 old redisplay didn't do it either. */
10746 || w->cursor.y + cursor_height > it.last_visible_y)
10748 w->cursor.vpos = -1;
10749 clear_glyph_matrix (w->desired_matrix);
10750 return -1;
10754 /* Scroll the display. Do it before changing the current matrix so
10755 that xterm.c doesn't get confused about where the cursor glyph is
10756 found. */
10757 if (dy && run.height)
10759 update_begin (f);
10761 if (FRAME_WINDOW_P (f))
10763 rif->update_window_begin_hook (w);
10764 rif->clear_mouse_face (w);
10765 rif->scroll_run_hook (w, &run);
10766 rif->update_window_end_hook (w, 0, 0);
10768 else
10770 /* Terminal frame. In this case, dvpos gives the number of
10771 lines to scroll by; dvpos < 0 means scroll up. */
10772 int first_unchanged_at_end_vpos
10773 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10774 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10775 int end = XFASTINT (w->top) + window_internal_height (w);
10777 /* Perform the operation on the screen. */
10778 if (dvpos > 0)
10780 /* Scroll last_unchanged_at_beg_row to the end of the
10781 window down dvpos lines. */
10782 set_terminal_window (end);
10784 /* On dumb terminals delete dvpos lines at the end
10785 before inserting dvpos empty lines. */
10786 if (!scroll_region_ok)
10787 ins_del_lines (end - dvpos, -dvpos);
10789 /* Insert dvpos empty lines in front of
10790 last_unchanged_at_beg_row. */
10791 ins_del_lines (from, dvpos);
10793 else if (dvpos < 0)
10795 /* Scroll up last_unchanged_at_beg_vpos to the end of
10796 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10797 set_terminal_window (end);
10799 /* Delete dvpos lines in front of
10800 last_unchanged_at_beg_vpos. ins_del_lines will set
10801 the cursor to the given vpos and emit |dvpos| delete
10802 line sequences. */
10803 ins_del_lines (from + dvpos, dvpos);
10805 /* On a dumb terminal insert dvpos empty lines at the
10806 end. */
10807 if (!scroll_region_ok)
10808 ins_del_lines (end + dvpos, -dvpos);
10811 set_terminal_window (0);
10814 update_end (f);
10817 /* Shift reused rows of the current matrix to the right position.
10818 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10819 text. */
10820 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10821 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10822 if (dvpos < 0)
10824 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10825 bottom_vpos, dvpos);
10826 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10827 bottom_vpos, 0);
10829 else if (dvpos > 0)
10831 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10832 bottom_vpos, dvpos);
10833 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10834 first_unchanged_at_end_vpos + dvpos, 0);
10837 /* For frame-based redisplay, make sure that current frame and window
10838 matrix are in sync with respect to glyph memory. */
10839 if (!FRAME_WINDOW_P (f))
10840 sync_frame_with_window_matrix_rows (w);
10842 /* Adjust buffer positions in reused rows. */
10843 if (delta)
10844 increment_matrix_positions (current_matrix,
10845 first_unchanged_at_end_vpos + dvpos,
10846 bottom_vpos, delta, delta_bytes);
10848 /* Adjust Y positions. */
10849 if (dy)
10850 shift_glyph_matrix (w, current_matrix,
10851 first_unchanged_at_end_vpos + dvpos,
10852 bottom_vpos, dy);
10854 if (first_unchanged_at_end_row)
10855 first_unchanged_at_end_row += dvpos;
10857 /* If scrolling up, there may be some lines to display at the end of
10858 the window. */
10859 last_text_row_at_end = NULL;
10860 if (dy < 0)
10862 /* Set last_row to the glyph row in the current matrix where the
10863 window end line is found. It has been moved up or down in
10864 the matrix by dvpos. */
10865 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10866 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10868 /* If last_row is the window end line, it should display text. */
10869 xassert (last_row->displays_text_p);
10871 /* If window end line was partially visible before, begin
10872 displaying at that line. Otherwise begin displaying with the
10873 line following it. */
10874 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10876 init_to_row_start (&it, w, last_row);
10877 it.vpos = last_vpos;
10878 it.current_y = last_row->y;
10880 else
10882 init_to_row_end (&it, w, last_row);
10883 it.vpos = 1 + last_vpos;
10884 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10885 ++last_row;
10888 /* We may start in a continuation line. If so, we have to get
10889 the right continuation_lines_width and current_x. */
10890 it.continuation_lines_width = last_row->continuation_lines_width;
10891 it.hpos = it.current_x = 0;
10893 /* Display the rest of the lines at the window end. */
10894 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10895 while (it.current_y < it.last_visible_y
10896 && !fonts_changed_p)
10898 /* Is it always sure that the display agrees with lines in
10899 the current matrix? I don't think so, so we mark rows
10900 displayed invalid in the current matrix by setting their
10901 enabled_p flag to zero. */
10902 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10903 if (display_line (&it))
10904 last_text_row_at_end = it.glyph_row - 1;
10908 /* Update window_end_pos and window_end_vpos. */
10909 if (first_unchanged_at_end_row
10910 && first_unchanged_at_end_row->y < it.last_visible_y
10911 && !last_text_row_at_end)
10913 /* Window end line if one of the preserved rows from the current
10914 matrix. Set row to the last row displaying text in current
10915 matrix starting at first_unchanged_at_end_row, after
10916 scrolling. */
10917 xassert (first_unchanged_at_end_row->displays_text_p);
10918 row = find_last_row_displaying_text (w->current_matrix, &it,
10919 first_unchanged_at_end_row);
10920 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10922 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10923 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10924 XSETFASTINT (w->window_end_vpos,
10925 MATRIX_ROW_VPOS (row, w->current_matrix));
10927 else if (last_text_row_at_end)
10929 XSETFASTINT (w->window_end_pos,
10930 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10931 w->window_end_bytepos
10932 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10933 XSETFASTINT (w->window_end_vpos,
10934 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10936 else if (last_text_row)
10938 /* We have displayed either to the end of the window or at the
10939 end of the window, i.e. the last row with text is to be found
10940 in the desired matrix. */
10941 XSETFASTINT (w->window_end_pos,
10942 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10943 w->window_end_bytepos
10944 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10945 XSETFASTINT (w->window_end_vpos,
10946 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10948 else if (first_unchanged_at_end_row == NULL
10949 && last_text_row == NULL
10950 && last_text_row_at_end == NULL)
10952 /* Displayed to end of window, but no line containing text was
10953 displayed. Lines were deleted at the end of the window. */
10954 int vpos;
10955 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10957 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10958 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10959 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10960 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10961 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10962 break;
10964 w->window_end_vpos = make_number (vpos);
10966 else
10967 abort ();
10969 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10970 debug_end_vpos = XFASTINT (w->window_end_vpos));
10972 /* Record that display has not been completed. */
10973 w->window_end_valid = Qnil;
10974 w->desired_matrix->no_scrolling_p = 1;
10975 return 3;
10980 /***********************************************************************
10981 More debugging support
10982 ***********************************************************************/
10984 #if GLYPH_DEBUG
10986 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10987 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10990 /* Dump the contents of glyph matrix MATRIX on stderr. If
10991 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10993 static void
10994 dump_glyph_matrix (matrix, with_glyphs_p)
10995 struct glyph_matrix *matrix;
10996 int with_glyphs_p;
10998 int i;
10999 for (i = 0; i < matrix->nrows; ++i)
11000 dump_glyph_row (matrix, i, with_glyphs_p);
11004 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11005 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11007 void
11008 dump_glyph_row (matrix, vpos, with_glyphs_p)
11009 struct glyph_matrix *matrix;
11010 int vpos, with_glyphs_p;
11012 struct glyph_row *row;
11014 if (vpos < 0 || vpos >= matrix->nrows)
11015 return;
11017 row = MATRIX_ROW (matrix, vpos);
11019 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11020 fprintf (stderr, "=======================================================================\n");
11022 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11023 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11024 row - matrix->rows,
11025 MATRIX_ROW_START_CHARPOS (row),
11026 MATRIX_ROW_END_CHARPOS (row),
11027 row->used[TEXT_AREA],
11028 row->contains_overlapping_glyphs_p,
11029 row->enabled_p,
11030 row->inverse_p,
11031 row->truncated_on_left_p,
11032 row->truncated_on_right_p,
11033 row->overlay_arrow_p,
11034 row->continued_p,
11035 MATRIX_ROW_CONTINUATION_LINE_P (row),
11036 row->displays_text_p,
11037 row->ends_at_zv_p,
11038 row->fill_line_p,
11039 row->ends_in_middle_of_char_p,
11040 row->starts_in_middle_of_char_p,
11041 row->x,
11042 row->y,
11043 row->pixel_width,
11044 row->height,
11045 row->visible_height,
11046 row->ascent,
11047 row->phys_ascent);
11048 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11049 row->end.overlay_string_index);
11050 fprintf (stderr, "%9d %5d\n",
11051 CHARPOS (row->start.string_pos),
11052 CHARPOS (row->end.string_pos));
11053 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11054 row->end.dpvec_index);
11056 if (with_glyphs_p)
11058 struct glyph *glyph, *glyph_end;
11059 int prev_had_glyphs_p;
11061 glyph = row->glyphs[TEXT_AREA];
11062 glyph_end = glyph + row->used[TEXT_AREA];
11064 /* Glyph for a line end in text. */
11065 if (glyph == glyph_end && glyph->charpos > 0)
11066 ++glyph_end;
11068 if (glyph < glyph_end)
11070 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11071 prev_had_glyphs_p = 1;
11073 else
11074 prev_had_glyphs_p = 0;
11076 while (glyph < glyph_end)
11078 if (glyph->type == CHAR_GLYPH)
11080 fprintf (stderr,
11081 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11082 glyph - row->glyphs[TEXT_AREA],
11083 'C',
11084 glyph->charpos,
11085 (BUFFERP (glyph->object)
11086 ? 'B'
11087 : (STRINGP (glyph->object)
11088 ? 'S'
11089 : '-')),
11090 glyph->pixel_width,
11091 glyph->u.ch,
11092 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11093 ? glyph->u.ch
11094 : '.'),
11095 glyph->face_id,
11096 glyph->left_box_line_p,
11097 glyph->right_box_line_p);
11099 else if (glyph->type == STRETCH_GLYPH)
11101 fprintf (stderr,
11102 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11103 glyph - row->glyphs[TEXT_AREA],
11104 'S',
11105 glyph->charpos,
11106 (BUFFERP (glyph->object)
11107 ? 'B'
11108 : (STRINGP (glyph->object)
11109 ? 'S'
11110 : '-')),
11111 glyph->pixel_width,
11113 '.',
11114 glyph->face_id,
11115 glyph->left_box_line_p,
11116 glyph->right_box_line_p);
11118 else if (glyph->type == IMAGE_GLYPH)
11120 fprintf (stderr,
11121 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11122 glyph - row->glyphs[TEXT_AREA],
11123 'I',
11124 glyph->charpos,
11125 (BUFFERP (glyph->object)
11126 ? 'B'
11127 : (STRINGP (glyph->object)
11128 ? 'S'
11129 : '-')),
11130 glyph->pixel_width,
11131 glyph->u.img_id,
11132 '.',
11133 glyph->face_id,
11134 glyph->left_box_line_p,
11135 glyph->right_box_line_p);
11137 ++glyph;
11143 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11144 Sdump_glyph_matrix, 0, 1, "p",
11145 "Dump the current matrix of the selected window to stderr.\n\
11146 Shows contents of glyph row structures. With non-nil optional\n\
11147 parameter WITH-GLYPHS-P, dump glyphs as well.")
11148 (with_glyphs_p)
11149 Lisp_Object with_glyphs_p;
11151 struct window *w = XWINDOW (selected_window);
11152 struct buffer *buffer = XBUFFER (w->buffer);
11154 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11155 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11156 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11157 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11158 fprintf (stderr, "=============================================\n");
11159 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11160 return Qnil;
11164 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11165 "Dump glyph row ROW to stderr.")
11166 (row)
11167 Lisp_Object row;
11169 CHECK_NUMBER (row, 0);
11170 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11171 return Qnil;
11175 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11176 0, 0, "", "")
11179 struct frame *sf = SELECTED_FRAME ();
11180 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11181 ->current_matrix);
11182 dump_glyph_row (m, 0, 1);
11183 return Qnil;
11187 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11188 Strace_redisplay_toggle, 0, 0, "",
11189 "Toggle tracing of redisplay.")
11192 trace_redisplay_p = !trace_redisplay_p;
11193 return Qnil;
11197 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11198 "Print STRING to stderr.")
11199 (string)
11200 Lisp_Object string;
11202 CHECK_STRING (string, 0);
11203 fprintf (stderr, "%s", XSTRING (string)->data);
11204 return Qnil;
11207 #endif /* GLYPH_DEBUG */
11211 /***********************************************************************
11212 Building Desired Matrix Rows
11213 ***********************************************************************/
11215 /* Return a temporary glyph row holding the glyphs of an overlay
11216 arrow. Only used for non-window-redisplay windows. */
11218 static struct glyph_row *
11219 get_overlay_arrow_glyph_row (w)
11220 struct window *w;
11222 struct frame *f = XFRAME (WINDOW_FRAME (w));
11223 struct buffer *buffer = XBUFFER (w->buffer);
11224 struct buffer *old = current_buffer;
11225 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11226 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11227 unsigned char *arrow_end = arrow_string + arrow_len;
11228 unsigned char *p;
11229 struct it it;
11230 int multibyte_p;
11231 int n_glyphs_before;
11233 set_buffer_temp (buffer);
11234 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11235 it.glyph_row->used[TEXT_AREA] = 0;
11236 SET_TEXT_POS (it.position, 0, 0);
11238 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11239 p = arrow_string;
11240 while (p < arrow_end)
11242 Lisp_Object face, ilisp;
11244 /* Get the next character. */
11245 if (multibyte_p)
11246 it.c = string_char_and_length (p, arrow_len, &it.len);
11247 else
11248 it.c = *p, it.len = 1;
11249 p += it.len;
11251 /* Get its face. */
11252 XSETFASTINT (ilisp, p - arrow_string);
11253 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11254 it.face_id = compute_char_face (f, it.c, face);
11256 /* Compute its width, get its glyphs. */
11257 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11258 SET_TEXT_POS (it.position, -1, -1);
11259 PRODUCE_GLYPHS (&it);
11261 /* If this character doesn't fit any more in the line, we have
11262 to remove some glyphs. */
11263 if (it.current_x > it.last_visible_x)
11265 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11266 break;
11270 set_buffer_temp (old);
11271 return it.glyph_row;
11275 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11276 glyphs are only inserted for terminal frames since we can't really
11277 win with truncation glyphs when partially visible glyphs are
11278 involved. Which glyphs to insert is determined by
11279 produce_special_glyphs. */
11281 static void
11282 insert_left_trunc_glyphs (it)
11283 struct it *it;
11285 struct it truncate_it;
11286 struct glyph *from, *end, *to, *toend;
11288 xassert (!FRAME_WINDOW_P (it->f));
11290 /* Get the truncation glyphs. */
11291 truncate_it = *it;
11292 truncate_it.current_x = 0;
11293 truncate_it.face_id = DEFAULT_FACE_ID;
11294 truncate_it.glyph_row = &scratch_glyph_row;
11295 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11296 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11297 truncate_it.object = make_number (0);
11298 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11300 /* Overwrite glyphs from IT with truncation glyphs. */
11301 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11302 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11303 to = it->glyph_row->glyphs[TEXT_AREA];
11304 toend = to + it->glyph_row->used[TEXT_AREA];
11306 while (from < end)
11307 *to++ = *from++;
11309 /* There may be padding glyphs left over. Remove them. */
11310 from = to;
11311 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11312 ++from;
11313 while (from < toend)
11314 *to++ = *from++;
11316 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11320 /* Compute the pixel height and width of IT->glyph_row.
11322 Most of the time, ascent and height of a display line will be equal
11323 to the max_ascent and max_height values of the display iterator
11324 structure. This is not the case if
11326 1. We hit ZV without displaying anything. In this case, max_ascent
11327 and max_height will be zero.
11329 2. We have some glyphs that don't contribute to the line height.
11330 (The glyph row flag contributes_to_line_height_p is for future
11331 pixmap extensions).
11333 The first case is easily covered by using default values because in
11334 these cases, the line height does not really matter, except that it
11335 must not be zero. */
11337 static void
11338 compute_line_metrics (it)
11339 struct it *it;
11341 struct glyph_row *row = it->glyph_row;
11342 int area, i;
11344 if (FRAME_WINDOW_P (it->f))
11346 int i, header_line_height;
11348 /* The line may consist of one space only, that was added to
11349 place the cursor on it. If so, the row's height hasn't been
11350 computed yet. */
11351 if (row->height == 0)
11353 if (it->max_ascent + it->max_descent == 0)
11354 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11355 row->ascent = it->max_ascent;
11356 row->height = it->max_ascent + it->max_descent;
11357 row->phys_ascent = it->max_phys_ascent;
11358 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11361 /* Compute the width of this line. */
11362 row->pixel_width = row->x;
11363 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11364 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11366 xassert (row->pixel_width >= 0);
11367 xassert (row->ascent >= 0 && row->height > 0);
11369 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11370 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11372 /* If first line's physical ascent is larger than its logical
11373 ascent, use the physical ascent, and make the row taller.
11374 This makes accented characters fully visible. */
11375 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11376 && row->phys_ascent > row->ascent)
11378 row->height += row->phys_ascent - row->ascent;
11379 row->ascent = row->phys_ascent;
11382 /* Compute how much of the line is visible. */
11383 row->visible_height = row->height;
11385 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11386 if (row->y < header_line_height)
11387 row->visible_height -= header_line_height - row->y;
11388 else
11390 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11391 if (row->y + row->height > max_y)
11392 row->visible_height -= row->y + row->height - max_y;
11395 else
11397 row->pixel_width = row->used[TEXT_AREA];
11398 row->ascent = row->phys_ascent = 0;
11399 row->height = row->phys_height = row->visible_height = 1;
11402 /* Compute a hash code for this row. */
11403 row->hash = 0;
11404 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11405 for (i = 0; i < row->used[area]; ++i)
11406 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11407 + row->glyphs[area][i].u.val
11408 + row->glyphs[area][i].face_id
11409 + row->glyphs[area][i].padding_p
11410 + (row->glyphs[area][i].type << 2));
11412 it->max_ascent = it->max_descent = 0;
11413 it->max_phys_ascent = it->max_phys_descent = 0;
11417 /* Append one space to the glyph row of iterator IT if doing a
11418 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11419 space have the default face, otherwise let it have the same face as
11420 IT->face_id. Value is non-zero if a space was added.
11422 This function is called to make sure that there is always one glyph
11423 at the end of a glyph row that the cursor can be set on under
11424 window-systems. (If there weren't such a glyph we would not know
11425 how wide and tall a box cursor should be displayed).
11427 At the same time this space let's a nicely handle clearing to the
11428 end of the line if the row ends in italic text. */
11430 static int
11431 append_space (it, default_face_p)
11432 struct it *it;
11433 int default_face_p;
11435 if (FRAME_WINDOW_P (it->f))
11437 int n = it->glyph_row->used[TEXT_AREA];
11439 if (it->glyph_row->glyphs[TEXT_AREA] + n
11440 < it->glyph_row->glyphs[1 + TEXT_AREA])
11442 /* Save some values that must not be changed.
11443 Must save IT->c and IT->len because otherwise
11444 ITERATOR_AT_END_P wouldn't work anymore after
11445 append_space has been called. */
11446 int saved_what = it->what;
11447 int saved_c = it->c, saved_len = it->len;
11448 int saved_x = it->current_x;
11449 int saved_face_id = it->face_id;
11450 struct text_pos saved_pos;
11451 Lisp_Object saved_object;
11452 struct face *face;
11454 saved_object = it->object;
11455 saved_pos = it->position;
11457 it->what = IT_CHARACTER;
11458 bzero (&it->position, sizeof it->position);
11459 it->object = make_number (0);
11460 it->c = ' ';
11461 it->len = 1;
11463 if (default_face_p)
11464 it->face_id = DEFAULT_FACE_ID;
11465 face = FACE_FROM_ID (it->f, it->face_id);
11466 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11468 PRODUCE_GLYPHS (it);
11470 it->current_x = saved_x;
11471 it->object = saved_object;
11472 it->position = saved_pos;
11473 it->what = saved_what;
11474 it->face_id = saved_face_id;
11475 it->len = saved_len;
11476 it->c = saved_c;
11477 return 1;
11481 return 0;
11485 /* Extend the face of the last glyph in the text area of IT->glyph_row
11486 to the end of the display line. Called from display_line.
11487 If the glyph row is empty, add a space glyph to it so that we
11488 know the face to draw. Set the glyph row flag fill_line_p. */
11490 static void
11491 extend_face_to_end_of_line (it)
11492 struct it *it;
11494 struct face *face;
11495 struct frame *f = it->f;
11497 /* If line is already filled, do nothing. */
11498 if (it->current_x >= it->last_visible_x)
11499 return;
11501 /* Face extension extends the background and box of IT->face_id
11502 to the end of the line. If the background equals the background
11503 of the frame, we haven't to do anything. */
11504 face = FACE_FROM_ID (f, it->face_id);
11505 if (FRAME_WINDOW_P (f)
11506 && face->box == FACE_NO_BOX
11507 && face->background == FRAME_BACKGROUND_PIXEL (f)
11508 && !face->stipple)
11509 return;
11511 /* Set the glyph row flag indicating that the face of the last glyph
11512 in the text area has to be drawn to the end of the text area. */
11513 it->glyph_row->fill_line_p = 1;
11515 /* If current character of IT is not ASCII, make sure we have the
11516 ASCII face. This will be automatically undone the next time
11517 get_next_display_element returns a multibyte character. Note
11518 that the character will always be single byte in unibyte text. */
11519 if (!SINGLE_BYTE_CHAR_P (it->c))
11521 it->face_id = FACE_FOR_CHAR (f, face, 0);
11524 if (FRAME_WINDOW_P (f))
11526 /* If the row is empty, add a space with the current face of IT,
11527 so that we know which face to draw. */
11528 if (it->glyph_row->used[TEXT_AREA] == 0)
11530 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11531 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11532 it->glyph_row->used[TEXT_AREA] = 1;
11535 else
11537 /* Save some values that must not be changed. */
11538 int saved_x = it->current_x;
11539 struct text_pos saved_pos;
11540 Lisp_Object saved_object;
11541 int saved_what = it->what;
11543 saved_object = it->object;
11544 saved_pos = it->position;
11546 it->what = IT_CHARACTER;
11547 bzero (&it->position, sizeof it->position);
11548 it->object = make_number (0);
11549 it->c = ' ';
11550 it->len = 1;
11552 PRODUCE_GLYPHS (it);
11554 while (it->current_x <= it->last_visible_x)
11555 PRODUCE_GLYPHS (it);
11557 /* Don't count these blanks really. It would let us insert a left
11558 truncation glyph below and make us set the cursor on them, maybe. */
11559 it->current_x = saved_x;
11560 it->object = saved_object;
11561 it->position = saved_pos;
11562 it->what = saved_what;
11567 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11568 trailing whitespace. */
11570 static int
11571 trailing_whitespace_p (charpos)
11572 int charpos;
11574 int bytepos = CHAR_TO_BYTE (charpos);
11575 int c = 0;
11577 while (bytepos < ZV_BYTE
11578 && (c = FETCH_CHAR (bytepos),
11579 c == ' ' || c == '\t'))
11580 ++bytepos;
11582 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11584 if (bytepos != PT_BYTE)
11585 return 1;
11587 return 0;
11591 /* Highlight trailing whitespace, if any, in ROW. */
11593 void
11594 highlight_trailing_whitespace (f, row)
11595 struct frame *f;
11596 struct glyph_row *row;
11598 int used = row->used[TEXT_AREA];
11600 if (used)
11602 struct glyph *start = row->glyphs[TEXT_AREA];
11603 struct glyph *glyph = start + used - 1;
11605 /* Skip over the space glyph inserted to display the
11606 cursor at the end of a line. */
11607 if (glyph->type == CHAR_GLYPH
11608 && glyph->u.ch == ' '
11609 && INTEGERP (glyph->object))
11610 --glyph;
11612 /* If last glyph is a space or stretch, and it's trailing
11613 whitespace, set the face of all trailing whitespace glyphs in
11614 IT->glyph_row to `trailing-whitespace'. */
11615 if (glyph >= start
11616 && BUFFERP (glyph->object)
11617 && (glyph->type == STRETCH_GLYPH
11618 || (glyph->type == CHAR_GLYPH
11619 && glyph->u.ch == ' '))
11620 && trailing_whitespace_p (glyph->charpos))
11622 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11624 while (glyph >= start
11625 && BUFFERP (glyph->object)
11626 && (glyph->type == STRETCH_GLYPH
11627 || (glyph->type == CHAR_GLYPH
11628 && glyph->u.ch == ' ')))
11629 (glyph--)->face_id = face_id;
11635 /* Value is non-zero if glyph row ROW in window W should be
11636 used to put the cursor on. */
11638 static int
11639 cursor_row_p (w, row)
11640 struct window *w;
11641 struct glyph_row *row;
11643 if (PT == MATRIX_ROW_END_CHARPOS (row))
11645 /* If PT is at the end of ROW, and that row ends with a
11646 newline from a string, we don't want the cursor there. */
11647 if (row->end.overlay_string_index >= 0
11648 && ((truncate_partial_width_windows
11649 && !WINDOW_FULL_WIDTH_P (w))
11650 || !NILP (current_buffer->truncate_lines)))
11651 return 0;
11653 /* If PT is at the end of ROW, we normally want the cursor
11654 at the start of the row below, except when ROW ends
11655 at ZV or in the middle of a character. */
11656 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11657 || row->ends_at_zv_p)
11658 return 1;
11660 return 0;
11663 return 1;
11667 /* Construct the glyph row IT->glyph_row in the desired matrix of
11668 IT->w from text at the current position of IT. See dispextern.h
11669 for an overview of struct it. Value is non-zero if
11670 IT->glyph_row displays text, as opposed to a line displaying ZV
11671 only. */
11673 static int
11674 display_line (it)
11675 struct it *it;
11677 struct glyph_row *row = it->glyph_row;
11679 /* We always start displaying at hpos zero even if hscrolled. */
11680 xassert (it->hpos == 0 && it->current_x == 0);
11682 /* We must not display in a row that's not a text row. */
11683 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11684 < it->w->desired_matrix->nrows);
11686 /* Is IT->w showing the region? */
11687 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11689 /* Clear the result glyph row and enable it. */
11690 prepare_desired_row (row);
11692 row->y = it->current_y;
11693 row->start = it->current;
11694 row->continuation_lines_width = it->continuation_lines_width;
11695 row->displays_text_p = 1;
11696 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11697 it->starts_in_middle_of_char_p = 0;
11699 /* Arrange the overlays nicely for our purposes. Usually, we call
11700 display_line on only one line at a time, in which case this
11701 can't really hurt too much, or we call it on lines which appear
11702 one after another in the buffer, in which case all calls to
11703 recenter_overlay_lists but the first will be pretty cheap. */
11704 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11706 /* Move over display elements that are not visible because we are
11707 hscrolled. This may stop at an x-position < IT->first_visible_x
11708 if the first glyph is partially visible or if we hit a line end. */
11709 if (it->current_x < it->first_visible_x)
11710 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11711 MOVE_TO_POS | MOVE_TO_X);
11713 /* Get the initial row height. This is either the height of the
11714 text hscrolled, if there is any, or zero. */
11715 row->ascent = it->max_ascent;
11716 row->height = it->max_ascent + it->max_descent;
11717 row->phys_ascent = it->max_phys_ascent;
11718 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11720 /* Loop generating characters. The loop is left with IT on the next
11721 character to display. */
11722 while (1)
11724 int n_glyphs_before, hpos_before, x_before;
11725 int x, i, nglyphs;
11726 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11728 /* Retrieve the next thing to display. Value is zero if end of
11729 buffer reached. */
11730 if (!get_next_display_element (it))
11732 /* Maybe add a space at the end of this line that is used to
11733 display the cursor there under X. Set the charpos of the
11734 first glyph of blank lines not corresponding to any text
11735 to -1. */
11736 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11737 || row->used[TEXT_AREA] == 0)
11739 row->glyphs[TEXT_AREA]->charpos = -1;
11740 row->displays_text_p = 0;
11742 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11743 row->indicate_empty_line_p = 1;
11746 it->continuation_lines_width = 0;
11747 row->ends_at_zv_p = 1;
11748 break;
11751 /* Now, get the metrics of what we want to display. This also
11752 generates glyphs in `row' (which is IT->glyph_row). */
11753 n_glyphs_before = row->used[TEXT_AREA];
11754 x = it->current_x;
11756 /* Remember the line height so far in case the next element doesn't
11757 fit on the line. */
11758 if (!it->truncate_lines_p)
11760 ascent = it->max_ascent;
11761 descent = it->max_descent;
11762 phys_ascent = it->max_phys_ascent;
11763 phys_descent = it->max_phys_descent;
11766 PRODUCE_GLYPHS (it);
11768 /* If this display element was in marginal areas, continue with
11769 the next one. */
11770 if (it->area != TEXT_AREA)
11772 row->ascent = max (row->ascent, it->max_ascent);
11773 row->height = max (row->height, it->max_ascent + it->max_descent);
11774 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11775 row->phys_height = max (row->phys_height,
11776 it->max_phys_ascent + it->max_phys_descent);
11777 set_iterator_to_next (it, 1);
11778 continue;
11781 /* Does the display element fit on the line? If we truncate
11782 lines, we should draw past the right edge of the window. If
11783 we don't truncate, we want to stop so that we can display the
11784 continuation glyph before the right margin. If lines are
11785 continued, there are two possible strategies for characters
11786 resulting in more than 1 glyph (e.g. tabs): Display as many
11787 glyphs as possible in this line and leave the rest for the
11788 continuation line, or display the whole element in the next
11789 line. Original redisplay did the former, so we do it also. */
11790 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11791 hpos_before = it->hpos;
11792 x_before = x;
11794 if (nglyphs == 1
11795 && it->current_x < it->last_visible_x)
11797 ++it->hpos;
11798 row->ascent = max (row->ascent, it->max_ascent);
11799 row->height = max (row->height, it->max_ascent + it->max_descent);
11800 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11801 row->phys_height = max (row->phys_height,
11802 it->max_phys_ascent + it->max_phys_descent);
11803 if (it->current_x - it->pixel_width < it->first_visible_x)
11804 row->x = x - it->first_visible_x;
11806 else
11808 int new_x;
11809 struct glyph *glyph;
11811 for (i = 0; i < nglyphs; ++i, x = new_x)
11813 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11814 new_x = x + glyph->pixel_width;
11816 if (/* Lines are continued. */
11817 !it->truncate_lines_p
11818 && (/* Glyph doesn't fit on the line. */
11819 new_x > it->last_visible_x
11820 /* Or it fits exactly on a window system frame. */
11821 || (new_x == it->last_visible_x
11822 && FRAME_WINDOW_P (it->f))))
11824 /* End of a continued line. */
11826 if (it->hpos == 0
11827 || (new_x == it->last_visible_x
11828 && FRAME_WINDOW_P (it->f)))
11830 /* Current glyph is the only one on the line or
11831 fits exactly on the line. We must continue
11832 the line because we can't draw the cursor
11833 after the glyph. */
11834 row->continued_p = 1;
11835 it->current_x = new_x;
11836 it->continuation_lines_width += new_x;
11837 ++it->hpos;
11838 if (i == nglyphs - 1)
11839 set_iterator_to_next (it, 1);
11841 else if (CHAR_GLYPH_PADDING_P (*glyph)
11842 && !FRAME_WINDOW_P (it->f))
11844 /* A padding glyph that doesn't fit on this line.
11845 This means the whole character doesn't fit
11846 on the line. */
11847 row->used[TEXT_AREA] = n_glyphs_before;
11849 /* Fill the rest of the row with continuation
11850 glyphs like in 20.x. */
11851 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11852 < row->glyphs[1 + TEXT_AREA])
11853 produce_special_glyphs (it, IT_CONTINUATION);
11855 row->continued_p = 1;
11856 it->current_x = x_before;
11857 it->continuation_lines_width += x_before;
11859 /* Restore the height to what it was before the
11860 element not fitting on the line. */
11861 it->max_ascent = ascent;
11862 it->max_descent = descent;
11863 it->max_phys_ascent = phys_ascent;
11864 it->max_phys_descent = phys_descent;
11866 else
11868 /* Display element draws past the right edge of
11869 the window. Restore positions to values
11870 before the element. The next line starts
11871 with current_x before the glyph that could
11872 not be displayed, so that TAB works right. */
11873 row->used[TEXT_AREA] = n_glyphs_before + i;
11875 /* Display continuation glyphs. */
11876 if (!FRAME_WINDOW_P (it->f))
11877 produce_special_glyphs (it, IT_CONTINUATION);
11878 row->continued_p = 1;
11880 it->current_x = x;
11881 it->continuation_lines_width += x;
11882 if (nglyphs > 1 && i > 0)
11884 row->ends_in_middle_of_char_p = 1;
11885 it->starts_in_middle_of_char_p = 1;
11888 /* Restore the height to what it was before the
11889 element not fitting on the line. */
11890 it->max_ascent = ascent;
11891 it->max_descent = descent;
11892 it->max_phys_ascent = phys_ascent;
11893 it->max_phys_descent = phys_descent;
11896 break;
11898 else if (new_x > it->first_visible_x)
11900 /* Increment number of glyphs actually displayed. */
11901 ++it->hpos;
11903 if (x < it->first_visible_x)
11904 /* Glyph is partially visible, i.e. row starts at
11905 negative X position. */
11906 row->x = x - it->first_visible_x;
11908 else
11910 /* Glyph is completely off the left margin of the
11911 window. This should not happen because of the
11912 move_it_in_display_line at the start of
11913 this function. */
11914 abort ();
11918 row->ascent = max (row->ascent, it->max_ascent);
11919 row->height = max (row->height, it->max_ascent + it->max_descent);
11920 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11921 row->phys_height = max (row->phys_height,
11922 it->max_phys_ascent + it->max_phys_descent);
11924 /* End of this display line if row is continued. */
11925 if (row->continued_p)
11926 break;
11929 /* Is this a line end? If yes, we're also done, after making
11930 sure that a non-default face is extended up to the right
11931 margin of the window. */
11932 if (ITERATOR_AT_END_OF_LINE_P (it))
11934 int used_before = row->used[TEXT_AREA];
11936 /* Add a space at the end of the line that is used to
11937 display the cursor there. */
11938 append_space (it, 0);
11940 /* Extend the face to the end of the line. */
11941 extend_face_to_end_of_line (it);
11943 /* Make sure we have the position. */
11944 if (used_before == 0)
11945 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11947 /* Consume the line end. This skips over invisible lines. */
11948 set_iterator_to_next (it, 1);
11949 it->continuation_lines_width = 0;
11950 break;
11953 /* Proceed with next display element. Note that this skips
11954 over lines invisible because of selective display. */
11955 set_iterator_to_next (it, 1);
11957 /* If we truncate lines, we are done when the last displayed
11958 glyphs reach past the right margin of the window. */
11959 if (it->truncate_lines_p
11960 && (FRAME_WINDOW_P (it->f)
11961 ? (it->current_x >= it->last_visible_x)
11962 : (it->current_x > it->last_visible_x)))
11964 /* Maybe add truncation glyphs. */
11965 if (!FRAME_WINDOW_P (it->f))
11967 --it->glyph_row->used[TEXT_AREA];
11968 produce_special_glyphs (it, IT_TRUNCATION);
11971 row->truncated_on_right_p = 1;
11972 it->continuation_lines_width = 0;
11973 reseat_at_next_visible_line_start (it, 0);
11974 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11975 it->hpos = hpos_before;
11976 it->current_x = x_before;
11977 break;
11981 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11982 at the left window margin. */
11983 if (it->first_visible_x
11984 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11986 if (!FRAME_WINDOW_P (it->f))
11987 insert_left_trunc_glyphs (it);
11988 row->truncated_on_left_p = 1;
11991 /* If the start of this line is the overlay arrow-position, then
11992 mark this glyph row as the one containing the overlay arrow.
11993 This is clearly a mess with variable size fonts. It would be
11994 better to let it be displayed like cursors under X. */
11995 if (MARKERP (Voverlay_arrow_position)
11996 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11997 && (MATRIX_ROW_START_CHARPOS (row)
11998 == marker_position (Voverlay_arrow_position))
11999 && STRINGP (Voverlay_arrow_string)
12000 && ! overlay_arrow_seen)
12002 /* Overlay arrow in window redisplay is a bitmap. */
12003 if (!FRAME_WINDOW_P (it->f))
12005 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12006 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12007 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12008 struct glyph *p = row->glyphs[TEXT_AREA];
12009 struct glyph *p2, *end;
12011 /* Copy the arrow glyphs. */
12012 while (glyph < arrow_end)
12013 *p++ = *glyph++;
12015 /* Throw away padding glyphs. */
12016 p2 = p;
12017 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12018 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12019 ++p2;
12020 if (p2 > p)
12022 while (p2 < end)
12023 *p++ = *p2++;
12024 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12028 overlay_arrow_seen = 1;
12029 row->overlay_arrow_p = 1;
12032 /* Compute pixel dimensions of this line. */
12033 compute_line_metrics (it);
12035 /* Remember the position at which this line ends. */
12036 row->end = it->current;
12038 /* Maybe set the cursor. */
12039 if (it->w->cursor.vpos < 0
12040 && PT >= MATRIX_ROW_START_CHARPOS (row)
12041 && PT <= MATRIX_ROW_END_CHARPOS (row)
12042 && cursor_row_p (it->w, row))
12043 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12045 /* Highlight trailing whitespace. */
12046 if (!NILP (Vshow_trailing_whitespace))
12047 highlight_trailing_whitespace (it->f, it->glyph_row);
12049 /* Prepare for the next line. This line starts horizontally at (X
12050 HPOS) = (0 0). Vertical positions are incremented. As a
12051 convenience for the caller, IT->glyph_row is set to the next
12052 row to be used. */
12053 it->current_x = it->hpos = 0;
12054 it->current_y += row->height;
12055 ++it->vpos;
12056 ++it->glyph_row;
12057 return row->displays_text_p;
12062 /***********************************************************************
12063 Menu Bar
12064 ***********************************************************************/
12066 /* Redisplay the menu bar in the frame for window W.
12068 The menu bar of X frames that don't have X toolkit support is
12069 displayed in a special window W->frame->menu_bar_window.
12071 The menu bar of terminal frames is treated specially as far as
12072 glyph matrices are concerned. Menu bar lines are not part of
12073 windows, so the update is done directly on the frame matrix rows
12074 for the menu bar. */
12076 static void
12077 display_menu_bar (w)
12078 struct window *w;
12080 struct frame *f = XFRAME (WINDOW_FRAME (w));
12081 struct it it;
12082 Lisp_Object items;
12083 int i;
12085 /* Don't do all this for graphical frames. */
12086 #ifdef HAVE_NTGUI
12087 if (!NILP (Vwindow_system))
12088 return;
12089 #endif
12090 #ifdef USE_X_TOOLKIT
12091 if (FRAME_X_P (f))
12092 return;
12093 #endif
12095 #ifdef USE_X_TOOLKIT
12096 xassert (!FRAME_WINDOW_P (f));
12097 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12098 it.first_visible_x = 0;
12099 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12100 #else /* not USE_X_TOOLKIT */
12101 if (FRAME_WINDOW_P (f))
12103 /* Menu bar lines are displayed in the desired matrix of the
12104 dummy window menu_bar_window. */
12105 struct window *menu_w;
12106 xassert (WINDOWP (f->menu_bar_window));
12107 menu_w = XWINDOW (f->menu_bar_window);
12108 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12109 MENU_FACE_ID);
12110 it.first_visible_x = 0;
12111 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12113 else
12115 /* This is a TTY frame, i.e. character hpos/vpos are used as
12116 pixel x/y. */
12117 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12118 MENU_FACE_ID);
12119 it.first_visible_x = 0;
12120 it.last_visible_x = FRAME_WIDTH (f);
12122 #endif /* not USE_X_TOOLKIT */
12124 /* Clear all rows of the menu bar. */
12125 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12127 struct glyph_row *row = it.glyph_row + i;
12128 clear_glyph_row (row);
12129 row->enabled_p = 1;
12130 row->full_width_p = 1;
12133 /* Make the first line of the menu bar appear in reverse video. */
12134 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12136 /* Display all items of the menu bar. */
12137 items = FRAME_MENU_BAR_ITEMS (it.f);
12138 for (i = 0; i < XVECTOR (items)->size; i += 4)
12140 Lisp_Object string;
12142 /* Stop at nil string. */
12143 string = XVECTOR (items)->contents[i + 1];
12144 if (NILP (string))
12145 break;
12147 /* Remember where item was displayed. */
12148 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12150 /* Display the item, pad with one space. */
12151 if (it.current_x < it.last_visible_x)
12152 display_string (NULL, string, Qnil, 0, 0, &it,
12153 XSTRING (string)->size + 1, 0, 0, -1);
12156 /* Fill out the line with spaces. */
12157 if (it.current_x < it.last_visible_x)
12158 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12160 /* Compute the total height of the lines. */
12161 compute_line_metrics (&it);
12166 /***********************************************************************
12167 Mode Line
12168 ***********************************************************************/
12170 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12171 FORCE is non-zero, redisplay mode lines unconditionally.
12172 Otherwise, redisplay only mode lines that are garbaged. Value is
12173 the number of windows whose mode lines were redisplayed. */
12175 static int
12176 redisplay_mode_lines (window, force)
12177 Lisp_Object window;
12178 int force;
12180 int nwindows = 0;
12182 while (!NILP (window))
12184 struct window *w = XWINDOW (window);
12186 if (WINDOWP (w->hchild))
12187 nwindows += redisplay_mode_lines (w->hchild, force);
12188 else if (WINDOWP (w->vchild))
12189 nwindows += redisplay_mode_lines (w->vchild, force);
12190 else if (force
12191 || FRAME_GARBAGED_P (XFRAME (w->frame))
12192 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12194 Lisp_Object old_selected_frame;
12195 struct text_pos lpoint;
12196 struct buffer *old = current_buffer;
12198 /* Set the window's buffer for the mode line display. */
12199 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12200 set_buffer_internal_1 (XBUFFER (w->buffer));
12202 /* Point refers normally to the selected window. For any
12203 other window, set up appropriate value. */
12204 if (!EQ (window, selected_window))
12206 struct text_pos pt;
12208 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12209 if (CHARPOS (pt) < BEGV)
12210 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12211 else if (CHARPOS (pt) > (ZV - 1))
12212 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12213 else
12214 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12217 /* Temporarily set up the selected frame. */
12218 old_selected_frame = selected_frame;
12219 selected_frame = w->frame;
12221 /* Display mode lines. */
12222 clear_glyph_matrix (w->desired_matrix);
12223 if (display_mode_lines (w))
12225 ++nwindows;
12226 w->must_be_updated_p = 1;
12229 /* Restore old settings. */
12230 selected_frame = old_selected_frame;
12231 set_buffer_internal_1 (old);
12232 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12235 window = w->next;
12238 return nwindows;
12242 /* Display the mode and/or top line of window W. Value is the number
12243 of mode lines displayed. */
12245 static int
12246 display_mode_lines (w)
12247 struct window *w;
12249 int n = 0;
12251 /* These will be set while the mode line specs are processed. */
12252 line_number_displayed = 0;
12253 w->column_number_displayed = Qnil;
12255 if (WINDOW_WANTS_MODELINE_P (w))
12257 display_mode_line (w, MODE_LINE_FACE_ID,
12258 current_buffer->mode_line_format);
12259 ++n;
12262 if (WINDOW_WANTS_HEADER_LINE_P (w))
12264 display_mode_line (w, HEADER_LINE_FACE_ID,
12265 current_buffer->header_line_format);
12266 ++n;
12269 return n;
12273 /* Display mode or top line of window W. FACE_ID specifies which line
12274 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12275 FORMAT is the mode line format to display. */
12277 static void
12278 display_mode_line (w, face_id, format)
12279 struct window *w;
12280 enum face_id face_id;
12281 Lisp_Object format;
12283 struct it it;
12284 struct face *face;
12286 init_iterator (&it, w, -1, -1, NULL, face_id);
12287 prepare_desired_row (it.glyph_row);
12289 /* Temporarily make frame's keyboard the current kboard so that
12290 kboard-local variables in the mode_line_format will get the right
12291 values. */
12292 push_frame_kboard (it.f);
12293 display_mode_element (&it, 0, 0, 0, format);
12294 pop_frame_kboard ();
12296 /* Fill up with spaces. */
12297 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12299 compute_line_metrics (&it);
12300 it.glyph_row->full_width_p = 1;
12301 it.glyph_row->mode_line_p = 1;
12302 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12303 it.glyph_row->continued_p = 0;
12304 it.glyph_row->truncated_on_left_p = 0;
12305 it.glyph_row->truncated_on_right_p = 0;
12307 /* Make a 3D mode-line have a shadow at its right end. */
12308 face = FACE_FROM_ID (it.f, face_id);
12309 extend_face_to_end_of_line (&it);
12310 if (face->box != FACE_NO_BOX)
12312 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12313 + it.glyph_row->used[TEXT_AREA] - 1);
12314 last->right_box_line_p = 1;
12319 /* Contribute ELT to the mode line for window IT->w. How it
12320 translates into text depends on its data type.
12322 IT describes the display environment in which we display, as usual.
12324 DEPTH is the depth in recursion. It is used to prevent
12325 infinite recursion here.
12327 FIELD_WIDTH is the number of characters the display of ELT should
12328 occupy in the mode line, and PRECISION is the maximum number of
12329 characters to display from ELT's representation. See
12330 display_string for details. *
12332 Returns the hpos of the end of the text generated by ELT. */
12334 static int
12335 display_mode_element (it, depth, field_width, precision, elt)
12336 struct it *it;
12337 int depth;
12338 int field_width, precision;
12339 Lisp_Object elt;
12341 int n = 0, field, prec;
12343 tail_recurse:
12344 if (depth > 10)
12345 goto invalid;
12347 depth++;
12349 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12351 case Lisp_String:
12353 /* A string: output it and check for %-constructs within it. */
12354 unsigned char c;
12355 unsigned char *this = XSTRING (elt)->data;
12356 unsigned char *lisp_string = this;
12358 while ((precision <= 0 || n < precision)
12359 && *this
12360 && (frame_title_ptr
12361 || it->current_x < it->last_visible_x))
12363 unsigned char *last = this;
12365 /* Advance to end of string or next format specifier. */
12366 while ((c = *this++) != '\0' && c != '%')
12369 if (this - 1 != last)
12371 /* Output to end of string or up to '%'. Field width
12372 is length of string. Don't output more than
12373 PRECISION allows us. */
12374 prec = --this - last;
12375 if (precision > 0 && prec > precision - n)
12376 prec = precision - n;
12378 if (frame_title_ptr)
12379 n += store_frame_title (last, prec, prec);
12380 else
12381 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12382 it, 0, prec, 0, -1);
12384 else /* c == '%' */
12386 unsigned char *percent_position = this;
12388 /* Get the specified minimum width. Zero means
12389 don't pad. */
12390 field = 0;
12391 while ((c = *this++) >= '0' && c <= '9')
12392 field = field * 10 + c - '0';
12394 /* Don't pad beyond the total padding allowed. */
12395 if (field_width - n > 0 && field > field_width - n)
12396 field = field_width - n;
12398 /* Note that either PRECISION <= 0 or N < PRECISION. */
12399 prec = precision - n;
12401 if (c == 'M')
12402 n += display_mode_element (it, depth, field, prec,
12403 Vglobal_mode_string);
12404 else if (c != 0)
12406 unsigned char *spec
12407 = decode_mode_spec (it->w, c, field, prec);
12409 if (frame_title_ptr)
12410 n += store_frame_title (spec, field, prec);
12411 else
12413 int nglyphs_before
12414 = it->glyph_row->used[TEXT_AREA];
12415 int charpos
12416 = percent_position - XSTRING (elt)->data;
12417 int nwritten
12418 = display_string (spec, Qnil, elt, charpos, 0, it,
12419 field, prec, 0, -1);
12421 /* Assign to the glyphs written above the
12422 string where the `%x' came from, position
12423 of the `%'. */
12424 if (nwritten > 0)
12426 struct glyph *glyph
12427 = (it->glyph_row->glyphs[TEXT_AREA]
12428 + nglyphs_before);
12429 int i;
12431 for (i = 0; i < nwritten; ++i)
12433 glyph[i].object = elt;
12434 glyph[i].charpos = charpos;
12437 n += nwritten;
12444 break;
12446 case Lisp_Symbol:
12447 /* A symbol: process the value of the symbol recursively
12448 as if it appeared here directly. Avoid error if symbol void.
12449 Special case: if value of symbol is a string, output the string
12450 literally. */
12452 register Lisp_Object tem;
12453 tem = Fboundp (elt);
12454 if (!NILP (tem))
12456 tem = Fsymbol_value (elt);
12457 /* If value is a string, output that string literally:
12458 don't check for % within it. */
12459 if (STRINGP (tem))
12461 prec = XSTRING (tem)->size;
12462 if (precision > 0 && prec > precision - n)
12463 prec = precision - n;
12464 if (frame_title_ptr)
12465 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12466 else
12467 n += display_string (NULL, tem, Qnil, 0, 0, it,
12468 0, prec, 0, -1);
12470 else if (!EQ (tem, elt))
12472 /* Give up right away for nil or t. */
12473 elt = tem;
12474 goto tail_recurse;
12478 break;
12480 case Lisp_Cons:
12482 register Lisp_Object car, tem;
12484 /* A cons cell: three distinct cases.
12485 If first element is a string or a cons, process all the elements
12486 and effectively concatenate them.
12487 If first element is a negative number, truncate displaying cdr to
12488 at most that many characters. If positive, pad (with spaces)
12489 to at least that many characters.
12490 If first element is a symbol, process the cadr or caddr recursively
12491 according to whether the symbol's value is non-nil or nil. */
12492 car = XCAR (elt);
12493 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12495 /* An element of the form (:eval FORM) means evaluate FORM
12496 and use the result as mode line elements. */
12497 struct gcpro gcpro1;
12498 Lisp_Object spec;
12500 spec = safe_eval (XCAR (XCDR (elt)));
12501 GCPRO1 (spec);
12502 n += display_mode_element (it, depth, field_width - n,
12503 precision - n, spec);
12504 UNGCPRO;
12506 else if (SYMBOLP (car))
12508 tem = Fboundp (car);
12509 elt = XCDR (elt);
12510 if (!CONSP (elt))
12511 goto invalid;
12512 /* elt is now the cdr, and we know it is a cons cell.
12513 Use its car if CAR has a non-nil value. */
12514 if (!NILP (tem))
12516 tem = Fsymbol_value (car);
12517 if (!NILP (tem))
12519 elt = XCAR (elt);
12520 goto tail_recurse;
12523 /* Symbol's value is nil (or symbol is unbound)
12524 Get the cddr of the original list
12525 and if possible find the caddr and use that. */
12526 elt = XCDR (elt);
12527 if (NILP (elt))
12528 break;
12529 else if (!CONSP (elt))
12530 goto invalid;
12531 elt = XCAR (elt);
12532 goto tail_recurse;
12534 else if (INTEGERP (car))
12536 register int lim = XINT (car);
12537 elt = XCDR (elt);
12538 if (lim < 0)
12540 /* Negative int means reduce maximum width. */
12541 if (precision <= 0)
12542 precision = -lim;
12543 else
12544 precision = min (precision, -lim);
12546 else if (lim > 0)
12548 /* Padding specified. Don't let it be more than
12549 current maximum. */
12550 if (precision > 0)
12551 lim = min (precision, lim);
12553 /* If that's more padding than already wanted, queue it.
12554 But don't reduce padding already specified even if
12555 that is beyond the current truncation point. */
12556 field_width = max (lim, field_width);
12558 goto tail_recurse;
12560 else if (STRINGP (car) || CONSP (car))
12562 register int limit = 50;
12563 /* Limit is to protect against circular lists. */
12564 while (CONSP (elt)
12565 && --limit > 0
12566 && (precision <= 0 || n < precision))
12568 n += display_mode_element (it, depth, field_width - n,
12569 precision - n, XCAR (elt));
12570 elt = XCDR (elt);
12574 break;
12576 default:
12577 invalid:
12578 if (frame_title_ptr)
12579 n += store_frame_title ("*invalid*", 0, precision - n);
12580 else
12581 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12582 precision - n, 0, 0);
12583 return n;
12586 /* Pad to FIELD_WIDTH. */
12587 if (field_width > 0 && n < field_width)
12589 if (frame_title_ptr)
12590 n += store_frame_title ("", field_width - n, 0);
12591 else
12592 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12593 0, 0, 0);
12596 return n;
12600 /* Write a null-terminated, right justified decimal representation of
12601 the positive integer D to BUF using a minimal field width WIDTH. */
12603 static void
12604 pint2str (buf, width, d)
12605 register char *buf;
12606 register int width;
12607 register int d;
12609 register char *p = buf;
12611 if (d <= 0)
12612 *p++ = '0';
12613 else
12615 while (d > 0)
12617 *p++ = d % 10 + '0';
12618 d /= 10;
12622 for (width -= (int) (p - buf); width > 0; --width)
12623 *p++ = ' ';
12624 *p-- = '\0';
12625 while (p > buf)
12627 d = *buf;
12628 *buf++ = *p;
12629 *p-- = d;
12633 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12634 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12635 type of CODING_SYSTEM. Return updated pointer into BUF. */
12637 static unsigned char invalid_eol_type[] = "(*invalid*)";
12639 static char *
12640 decode_mode_spec_coding (coding_system, buf, eol_flag)
12641 Lisp_Object coding_system;
12642 register char *buf;
12643 int eol_flag;
12645 Lisp_Object val;
12646 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12647 unsigned char *eol_str;
12648 int eol_str_len;
12649 /* The EOL conversion we are using. */
12650 Lisp_Object eoltype;
12652 val = Fget (coding_system, Qcoding_system);
12653 eoltype = Qnil;
12655 if (!VECTORP (val)) /* Not yet decided. */
12657 if (multibyte)
12658 *buf++ = '-';
12659 if (eol_flag)
12660 eoltype = eol_mnemonic_undecided;
12661 /* Don't mention EOL conversion if it isn't decided. */
12663 else
12665 Lisp_Object eolvalue;
12667 eolvalue = Fget (coding_system, Qeol_type);
12669 if (multibyte)
12670 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12672 if (eol_flag)
12674 /* The EOL conversion that is normal on this system. */
12676 if (NILP (eolvalue)) /* Not yet decided. */
12677 eoltype = eol_mnemonic_undecided;
12678 else if (VECTORP (eolvalue)) /* Not yet decided. */
12679 eoltype = eol_mnemonic_undecided;
12680 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12681 eoltype = (XFASTINT (eolvalue) == 0
12682 ? eol_mnemonic_unix
12683 : (XFASTINT (eolvalue) == 1
12684 ? eol_mnemonic_dos : eol_mnemonic_mac));
12688 if (eol_flag)
12690 /* Mention the EOL conversion if it is not the usual one. */
12691 if (STRINGP (eoltype))
12693 eol_str = XSTRING (eoltype)->data;
12694 eol_str_len = XSTRING (eoltype)->size;
12696 else if (INTEGERP (eoltype)
12697 && CHAR_VALID_P (XINT (eoltype), 0))
12699 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12700 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12702 else
12704 eol_str = invalid_eol_type;
12705 eol_str_len = sizeof (invalid_eol_type) - 1;
12707 bcopy (eol_str, buf, eol_str_len);
12708 buf += eol_str_len;
12711 return buf;
12714 /* Return a string for the output of a mode line %-spec for window W,
12715 generated by character C. PRECISION >= 0 means don't return a
12716 string longer than that value. FIELD_WIDTH > 0 means pad the
12717 string returned with spaces to that value. */
12719 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12721 static char *
12722 decode_mode_spec (w, c, field_width, precision)
12723 struct window *w;
12724 register int c;
12725 int field_width, precision;
12727 Lisp_Object obj;
12728 struct frame *f = XFRAME (WINDOW_FRAME (w));
12729 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12730 struct buffer *b = XBUFFER (w->buffer);
12732 obj = Qnil;
12734 switch (c)
12736 case '*':
12737 if (!NILP (b->read_only))
12738 return "%";
12739 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12740 return "*";
12741 return "-";
12743 case '+':
12744 /* This differs from %* only for a modified read-only buffer. */
12745 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12746 return "*";
12747 if (!NILP (b->read_only))
12748 return "%";
12749 return "-";
12751 case '&':
12752 /* This differs from %* in ignoring read-only-ness. */
12753 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12754 return "*";
12755 return "-";
12757 case '%':
12758 return "%";
12760 case '[':
12762 int i;
12763 char *p;
12765 if (command_loop_level > 5)
12766 return "[[[... ";
12767 p = decode_mode_spec_buf;
12768 for (i = 0; i < command_loop_level; i++)
12769 *p++ = '[';
12770 *p = 0;
12771 return decode_mode_spec_buf;
12774 case ']':
12776 int i;
12777 char *p;
12779 if (command_loop_level > 5)
12780 return " ...]]]";
12781 p = decode_mode_spec_buf;
12782 for (i = 0; i < command_loop_level; i++)
12783 *p++ = ']';
12784 *p = 0;
12785 return decode_mode_spec_buf;
12788 case '-':
12790 register int i;
12792 /* Let lots_of_dashes be a string of infinite length. */
12793 if (field_width <= 0
12794 || field_width > sizeof (lots_of_dashes))
12796 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12797 decode_mode_spec_buf[i] = '-';
12798 decode_mode_spec_buf[i] = '\0';
12799 return decode_mode_spec_buf;
12801 else
12802 return lots_of_dashes;
12805 case 'b':
12806 obj = b->name;
12807 break;
12809 case 'c':
12811 int col = current_column ();
12812 XSETFASTINT (w->column_number_displayed, col);
12813 pint2str (decode_mode_spec_buf, field_width, col);
12814 return decode_mode_spec_buf;
12817 case 'F':
12818 /* %F displays the frame name. */
12819 if (!NILP (f->title))
12820 return (char *) XSTRING (f->title)->data;
12821 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12822 return (char *) XSTRING (f->name)->data;
12823 return "Emacs";
12825 case 'f':
12826 obj = b->filename;
12827 break;
12829 case 'l':
12831 int startpos = XMARKER (w->start)->charpos;
12832 int startpos_byte = marker_byte_position (w->start);
12833 int line, linepos, linepos_byte, topline;
12834 int nlines, junk;
12835 int height = XFASTINT (w->height);
12837 /* If we decided that this buffer isn't suitable for line numbers,
12838 don't forget that too fast. */
12839 if (EQ (w->base_line_pos, w->buffer))
12840 goto no_value;
12841 /* But do forget it, if the window shows a different buffer now. */
12842 else if (BUFFERP (w->base_line_pos))
12843 w->base_line_pos = Qnil;
12845 /* If the buffer is very big, don't waste time. */
12846 if (INTEGERP (Vline_number_display_limit)
12847 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
12849 w->base_line_pos = Qnil;
12850 w->base_line_number = Qnil;
12851 goto no_value;
12854 if (!NILP (w->base_line_number)
12855 && !NILP (w->base_line_pos)
12856 && XFASTINT (w->base_line_pos) <= startpos)
12858 line = XFASTINT (w->base_line_number);
12859 linepos = XFASTINT (w->base_line_pos);
12860 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12862 else
12864 line = 1;
12865 linepos = BUF_BEGV (b);
12866 linepos_byte = BUF_BEGV_BYTE (b);
12869 /* Count lines from base line to window start position. */
12870 nlines = display_count_lines (linepos, linepos_byte,
12871 startpos_byte,
12872 startpos, &junk);
12874 topline = nlines + line;
12876 /* Determine a new base line, if the old one is too close
12877 or too far away, or if we did not have one.
12878 "Too close" means it's plausible a scroll-down would
12879 go back past it. */
12880 if (startpos == BUF_BEGV (b))
12882 XSETFASTINT (w->base_line_number, topline);
12883 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12885 else if (nlines < height + 25 || nlines > height * 3 + 50
12886 || linepos == BUF_BEGV (b))
12888 int limit = BUF_BEGV (b);
12889 int limit_byte = BUF_BEGV_BYTE (b);
12890 int position;
12891 int distance = (height * 2 + 30) * line_number_display_limit_width;
12893 if (startpos - distance > limit)
12895 limit = startpos - distance;
12896 limit_byte = CHAR_TO_BYTE (limit);
12899 nlines = display_count_lines (startpos, startpos_byte,
12900 limit_byte,
12901 - (height * 2 + 30),
12902 &position);
12903 /* If we couldn't find the lines we wanted within
12904 line_number_display_limit_width chars per line,
12905 give up on line numbers for this window. */
12906 if (position == limit_byte && limit == startpos - distance)
12908 w->base_line_pos = w->buffer;
12909 w->base_line_number = Qnil;
12910 goto no_value;
12913 XSETFASTINT (w->base_line_number, topline - nlines);
12914 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12917 /* Now count lines from the start pos to point. */
12918 nlines = display_count_lines (startpos, startpos_byte,
12919 PT_BYTE, PT, &junk);
12921 /* Record that we did display the line number. */
12922 line_number_displayed = 1;
12924 /* Make the string to show. */
12925 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12926 return decode_mode_spec_buf;
12927 no_value:
12929 char* p = decode_mode_spec_buf;
12930 int pad = field_width - 2;
12931 while (pad-- > 0)
12932 *p++ = ' ';
12933 *p++ = '?';
12934 *p++ = '?';
12935 *p = '\0';
12936 return decode_mode_spec_buf;
12939 break;
12941 case 'm':
12942 obj = b->mode_name;
12943 break;
12945 case 'n':
12946 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12947 return " Narrow";
12948 break;
12950 case 'p':
12952 int pos = marker_position (w->start);
12953 int total = BUF_ZV (b) - BUF_BEGV (b);
12955 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12957 if (pos <= BUF_BEGV (b))
12958 return "All";
12959 else
12960 return "Bottom";
12962 else if (pos <= BUF_BEGV (b))
12963 return "Top";
12964 else
12966 if (total > 1000000)
12967 /* Do it differently for a large value, to avoid overflow. */
12968 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12969 else
12970 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12971 /* We can't normally display a 3-digit number,
12972 so get us a 2-digit number that is close. */
12973 if (total == 100)
12974 total = 99;
12975 sprintf (decode_mode_spec_buf, "%2d%%", total);
12976 return decode_mode_spec_buf;
12980 /* Display percentage of size above the bottom of the screen. */
12981 case 'P':
12983 int toppos = marker_position (w->start);
12984 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12985 int total = BUF_ZV (b) - BUF_BEGV (b);
12987 if (botpos >= BUF_ZV (b))
12989 if (toppos <= BUF_BEGV (b))
12990 return "All";
12991 else
12992 return "Bottom";
12994 else
12996 if (total > 1000000)
12997 /* Do it differently for a large value, to avoid overflow. */
12998 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12999 else
13000 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13001 /* We can't normally display a 3-digit number,
13002 so get us a 2-digit number that is close. */
13003 if (total == 100)
13004 total = 99;
13005 if (toppos <= BUF_BEGV (b))
13006 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13007 else
13008 sprintf (decode_mode_spec_buf, "%2d%%", total);
13009 return decode_mode_spec_buf;
13013 case 's':
13014 /* status of process */
13015 obj = Fget_buffer_process (w->buffer);
13016 if (NILP (obj))
13017 return "no process";
13018 #ifdef subprocesses
13019 obj = Fsymbol_name (Fprocess_status (obj));
13020 #endif
13021 break;
13023 case 't': /* indicate TEXT or BINARY */
13024 #ifdef MODE_LINE_BINARY_TEXT
13025 return MODE_LINE_BINARY_TEXT (b);
13026 #else
13027 return "T";
13028 #endif
13030 case 'z':
13031 /* coding-system (not including end-of-line format) */
13032 case 'Z':
13033 /* coding-system (including end-of-line type) */
13035 int eol_flag = (c == 'Z');
13036 char *p = decode_mode_spec_buf;
13038 if (! FRAME_WINDOW_P (f))
13040 /* No need to mention EOL here--the terminal never needs
13041 to do EOL conversion. */
13042 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13043 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13045 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13046 p, eol_flag);
13048 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13049 #ifdef subprocesses
13050 obj = Fget_buffer_process (Fcurrent_buffer ());
13051 if (PROCESSP (obj))
13053 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13054 p, eol_flag);
13055 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13056 p, eol_flag);
13058 #endif /* subprocesses */
13059 #endif /* 0 */
13060 *p = 0;
13061 return decode_mode_spec_buf;
13065 if (STRINGP (obj))
13066 return (char *) XSTRING (obj)->data;
13067 else
13068 return "";
13072 /* Count up to COUNT lines starting from START / START_BYTE.
13073 But don't go beyond LIMIT_BYTE.
13074 Return the number of lines thus found (always nonnegative).
13076 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13078 static int
13079 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13080 int start, start_byte, limit_byte, count;
13081 int *byte_pos_ptr;
13083 register unsigned char *cursor;
13084 unsigned char *base;
13086 register int ceiling;
13087 register unsigned char *ceiling_addr;
13088 int orig_count = count;
13090 /* If we are not in selective display mode,
13091 check only for newlines. */
13092 int selective_display = (!NILP (current_buffer->selective_display)
13093 && !INTEGERP (current_buffer->selective_display));
13095 if (count > 0)
13097 while (start_byte < limit_byte)
13099 ceiling = BUFFER_CEILING_OF (start_byte);
13100 ceiling = min (limit_byte - 1, ceiling);
13101 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13102 base = (cursor = BYTE_POS_ADDR (start_byte));
13103 while (1)
13105 if (selective_display)
13106 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13108 else
13109 while (*cursor != '\n' && ++cursor != ceiling_addr)
13112 if (cursor != ceiling_addr)
13114 if (--count == 0)
13116 start_byte += cursor - base + 1;
13117 *byte_pos_ptr = start_byte;
13118 return orig_count;
13120 else
13121 if (++cursor == ceiling_addr)
13122 break;
13124 else
13125 break;
13127 start_byte += cursor - base;
13130 else
13132 while (start_byte > limit_byte)
13134 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13135 ceiling = max (limit_byte, ceiling);
13136 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13137 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13138 while (1)
13140 if (selective_display)
13141 while (--cursor != ceiling_addr
13142 && *cursor != '\n' && *cursor != 015)
13144 else
13145 while (--cursor != ceiling_addr && *cursor != '\n')
13148 if (cursor != ceiling_addr)
13150 if (++count == 0)
13152 start_byte += cursor - base + 1;
13153 *byte_pos_ptr = start_byte;
13154 /* When scanning backwards, we should
13155 not count the newline posterior to which we stop. */
13156 return - orig_count - 1;
13159 else
13160 break;
13162 /* Here we add 1 to compensate for the last decrement
13163 of CURSOR, which took it past the valid range. */
13164 start_byte += cursor - base + 1;
13168 *byte_pos_ptr = limit_byte;
13170 if (count < 0)
13171 return - orig_count + count;
13172 return orig_count - count;
13178 /***********************************************************************
13179 Displaying strings
13180 ***********************************************************************/
13182 /* Display a NUL-terminated string, starting with index START.
13184 If STRING is non-null, display that C string. Otherwise, the Lisp
13185 string LISP_STRING is displayed.
13187 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13188 FACE_STRING. Display STRING or LISP_STRING with the face at
13189 FACE_STRING_POS in FACE_STRING:
13191 Display the string in the environment given by IT, but use the
13192 standard display table, temporarily.
13194 FIELD_WIDTH is the minimum number of output glyphs to produce.
13195 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13196 with spaces. If STRING has more characters, more than FIELD_WIDTH
13197 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13199 PRECISION is the maximum number of characters to output from
13200 STRING. PRECISION < 0 means don't truncate the string.
13202 This is roughly equivalent to printf format specifiers:
13204 FIELD_WIDTH PRECISION PRINTF
13205 ----------------------------------------
13206 -1 -1 %s
13207 -1 10 %.10s
13208 10 -1 %10s
13209 20 10 %20.10s
13211 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13212 display them, and < 0 means obey the current buffer's value of
13213 enable_multibyte_characters.
13215 Value is the number of glyphs produced. */
13217 static int
13218 display_string (string, lisp_string, face_string, face_string_pos,
13219 start, it, field_width, precision, max_x, multibyte)
13220 unsigned char *string;
13221 Lisp_Object lisp_string;
13222 Lisp_Object face_string;
13223 int face_string_pos;
13224 int start;
13225 struct it *it;
13226 int field_width, precision, max_x;
13227 int multibyte;
13229 int hpos_at_start = it->hpos;
13230 int saved_face_id = it->face_id;
13231 struct glyph_row *row = it->glyph_row;
13233 /* Initialize the iterator IT for iteration over STRING beginning
13234 with index START. We assume that IT may be modified here (which
13235 means that display_line has to do something when displaying a
13236 mini-buffer prompt, which it does). */
13237 reseat_to_string (it, string, lisp_string, start,
13238 precision, field_width, multibyte);
13240 /* If displaying STRING, set up the face of the iterator
13241 from LISP_STRING, if that's given. */
13242 if (STRINGP (face_string))
13244 int endptr;
13245 struct face *face;
13247 it->face_id
13248 = face_at_string_position (it->w, face_string, face_string_pos,
13249 0, it->region_beg_charpos,
13250 it->region_end_charpos,
13251 &endptr, it->base_face_id);
13252 face = FACE_FROM_ID (it->f, it->face_id);
13253 it->face_box_p = face->box != FACE_NO_BOX;
13256 /* Set max_x to the maximum allowed X position. Don't let it go
13257 beyond the right edge of the window. */
13258 if (max_x <= 0)
13259 max_x = it->last_visible_x;
13260 else
13261 max_x = min (max_x, it->last_visible_x);
13263 /* Skip over display elements that are not visible. because IT->w is
13264 hscrolled. */
13265 if (it->current_x < it->first_visible_x)
13266 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13267 MOVE_TO_POS | MOVE_TO_X);
13269 row->ascent = it->max_ascent;
13270 row->height = it->max_ascent + it->max_descent;
13271 row->phys_ascent = it->max_phys_ascent;
13272 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13274 /* This condition is for the case that we are called with current_x
13275 past last_visible_x. */
13276 while (it->current_x < max_x)
13278 int x_before, x, n_glyphs_before, i, nglyphs;
13280 /* Get the next display element. */
13281 if (!get_next_display_element (it))
13282 break;
13284 /* Produce glyphs. */
13285 x_before = it->current_x;
13286 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13287 PRODUCE_GLYPHS (it);
13289 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13290 i = 0;
13291 x = x_before;
13292 while (i < nglyphs)
13294 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13296 if (!it->truncate_lines_p
13297 && x + glyph->pixel_width > max_x)
13299 /* End of continued line or max_x reached. */
13300 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13301 it->current_x = x;
13302 break;
13304 else if (x + glyph->pixel_width > it->first_visible_x)
13306 /* Glyph is at least partially visible. */
13307 ++it->hpos;
13308 if (x < it->first_visible_x)
13309 it->glyph_row->x = x - it->first_visible_x;
13311 else
13313 /* Glyph is off the left margin of the display area.
13314 Should not happen. */
13315 abort ();
13318 row->ascent = max (row->ascent, it->max_ascent);
13319 row->height = max (row->height, it->max_ascent + it->max_descent);
13320 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13321 row->phys_height = max (row->phys_height,
13322 it->max_phys_ascent + it->max_phys_descent);
13323 x += glyph->pixel_width;
13324 ++i;
13327 /* Stop if max_x reached. */
13328 if (i < nglyphs)
13329 break;
13331 /* Stop at line ends. */
13332 if (ITERATOR_AT_END_OF_LINE_P (it))
13334 it->continuation_lines_width = 0;
13335 break;
13338 set_iterator_to_next (it, 1);
13340 /* Stop if truncating at the right edge. */
13341 if (it->truncate_lines_p
13342 && it->current_x >= it->last_visible_x)
13344 /* Add truncation mark, but don't do it if the line is
13345 truncated at a padding space. */
13346 if (IT_CHARPOS (*it) < it->string_nchars)
13348 if (!FRAME_WINDOW_P (it->f))
13349 produce_special_glyphs (it, IT_TRUNCATION);
13350 it->glyph_row->truncated_on_right_p = 1;
13352 break;
13356 /* Maybe insert a truncation at the left. */
13357 if (it->first_visible_x
13358 && IT_CHARPOS (*it) > 0)
13360 if (!FRAME_WINDOW_P (it->f))
13361 insert_left_trunc_glyphs (it);
13362 it->glyph_row->truncated_on_left_p = 1;
13365 it->face_id = saved_face_id;
13367 /* Value is number of columns displayed. */
13368 return it->hpos - hpos_at_start;
13373 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13374 appears as an element of LIST or as the car of an element of LIST.
13375 If PROPVAL is a list, compare each element against LIST in that
13376 way, and return 1 if any element of PROPVAL is found in LIST.
13377 Otherwise return 0. This function cannot quit. */
13380 invisible_p (propval, list)
13381 register Lisp_Object propval;
13382 Lisp_Object list;
13384 register Lisp_Object tail, proptail;
13386 for (tail = list; CONSP (tail); tail = XCDR (tail))
13388 register Lisp_Object tem;
13389 tem = XCAR (tail);
13390 if (EQ (propval, tem))
13391 return 1;
13392 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13393 return 1;
13396 if (CONSP (propval))
13398 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13400 Lisp_Object propelt;
13401 propelt = XCAR (proptail);
13402 for (tail = list; CONSP (tail); tail = XCDR (tail))
13404 register Lisp_Object tem;
13405 tem = XCAR (tail);
13406 if (EQ (propelt, tem))
13407 return 1;
13408 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13409 return 1;
13414 return 0;
13418 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13419 the cdr of that element is non-nil. If PROPVAL is a list, check
13420 each element of PROPVAL in that way, and the first time some
13421 element is found, return 1 if the cdr of that element is non-nil.
13422 Otherwise return 0. This function cannot quit. */
13425 invisible_ellipsis_p (propval, list)
13426 register Lisp_Object propval;
13427 Lisp_Object list;
13429 register Lisp_Object tail, proptail;
13431 for (tail = list; CONSP (tail); tail = XCDR (tail))
13433 register Lisp_Object tem;
13434 tem = XCAR (tail);
13435 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13436 return ! NILP (XCDR (tem));
13439 if (CONSP (propval))
13440 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13442 Lisp_Object propelt;
13443 propelt = XCAR (proptail);
13444 for (tail = list; CONSP (tail); tail = XCDR (tail))
13446 register Lisp_Object tem;
13447 tem = XCAR (tail);
13448 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13449 return ! NILP (XCDR (tem));
13453 return 0;
13458 /***********************************************************************
13459 Initialization
13460 ***********************************************************************/
13462 void
13463 syms_of_xdisp ()
13465 Vwith_echo_area_save_vector = Qnil;
13466 staticpro (&Vwith_echo_area_save_vector);
13468 Vmessage_stack = Qnil;
13469 staticpro (&Vmessage_stack);
13471 Qinhibit_redisplay = intern ("inhibit-redisplay");
13472 staticpro (&Qinhibit_redisplay);
13474 #if GLYPH_DEBUG
13475 defsubr (&Sdump_glyph_matrix);
13476 defsubr (&Sdump_glyph_row);
13477 defsubr (&Sdump_tool_bar_row);
13478 defsubr (&Strace_redisplay_toggle);
13479 defsubr (&Strace_to_stderr);
13480 #endif
13482 staticpro (&Qmenu_bar_update_hook);
13483 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13485 staticpro (&Qoverriding_terminal_local_map);
13486 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13488 staticpro (&Qoverriding_local_map);
13489 Qoverriding_local_map = intern ("overriding-local-map");
13491 staticpro (&Qwindow_scroll_functions);
13492 Qwindow_scroll_functions = intern ("window-scroll-functions");
13494 staticpro (&Qredisplay_end_trigger_functions);
13495 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13497 staticpro (&Qinhibit_point_motion_hooks);
13498 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13500 QCdata = intern (":data");
13501 staticpro (&QCdata);
13502 Qdisplay = intern ("display");
13503 staticpro (&Qdisplay);
13504 Qspace_width = intern ("space-width");
13505 staticpro (&Qspace_width);
13506 Qraise = intern ("raise");
13507 staticpro (&Qraise);
13508 Qspace = intern ("space");
13509 staticpro (&Qspace);
13510 Qmargin = intern ("margin");
13511 staticpro (&Qmargin);
13512 Qleft_margin = intern ("left-margin");
13513 staticpro (&Qleft_margin);
13514 Qright_margin = intern ("right-margin");
13515 staticpro (&Qright_margin);
13516 Qalign_to = intern ("align-to");
13517 staticpro (&Qalign_to);
13518 QCalign_to = intern (":align-to");
13519 staticpro (&QCalign_to);
13520 Qrelative_width = intern ("relative-width");
13521 staticpro (&Qrelative_width);
13522 QCrelative_width = intern (":relative-width");
13523 staticpro (&QCrelative_width);
13524 QCrelative_height = intern (":relative-height");
13525 staticpro (&QCrelative_height);
13526 QCeval = intern (":eval");
13527 staticpro (&QCeval);
13528 Qwhen = intern ("when");
13529 staticpro (&Qwhen);
13530 QCfile = intern (":file");
13531 staticpro (&QCfile);
13532 Qfontified = intern ("fontified");
13533 staticpro (&Qfontified);
13534 Qfontification_functions = intern ("fontification-functions");
13535 staticpro (&Qfontification_functions);
13536 Qtrailing_whitespace = intern ("trailing-whitespace");
13537 staticpro (&Qtrailing_whitespace);
13538 Qimage = intern ("image");
13539 staticpro (&Qimage);
13540 Qmessage_truncate_lines = intern ("message-truncate-lines");
13541 staticpro (&Qmessage_truncate_lines);
13543 last_arrow_position = Qnil;
13544 last_arrow_string = Qnil;
13545 staticpro (&last_arrow_position);
13546 staticpro (&last_arrow_string);
13548 echo_buffer[0] = echo_buffer[1] = Qnil;
13549 staticpro (&echo_buffer[0]);
13550 staticpro (&echo_buffer[1]);
13552 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13553 staticpro (&echo_area_buffer[0]);
13554 staticpro (&echo_area_buffer[1]);
13556 Vmessages_buffer_name = build_string ("*Messages*");
13557 staticpro (&Vmessages_buffer_name);
13559 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13560 "Non-nil means highlight trailing whitespace.\n\
13561 The face used for trailing whitespace is `trailing-whitespace'.");
13562 Vshow_trailing_whitespace = Qnil;
13564 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13565 "Non-nil means don't actually do any redisplay.\n\
13566 This is used for internal purposes.");
13567 Vinhibit_redisplay = Qnil;
13569 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13570 "String (or mode line construct) included (normally) in `mode-line-format'.");
13571 Vglobal_mode_string = Qnil;
13573 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13574 "Marker for where to display an arrow on top of the buffer text.\n\
13575 This must be the beginning of a line in order to work.\n\
13576 See also `overlay-arrow-string'.");
13577 Voverlay_arrow_position = Qnil;
13579 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13580 "String to display as an arrow. See also `overlay-arrow-position'.");
13581 Voverlay_arrow_string = Qnil;
13583 DEFVAR_INT ("scroll-step", &scroll_step,
13584 "*The number of lines to try scrolling a window by when point moves out.\n\
13585 If that fails to bring point back on frame, point is centered instead.\n\
13586 If this is zero, point is always centered after it moves off frame.");
13588 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13589 "*Scroll up to this many lines, to bring point back on screen.\n\
13590 A value of zero means to scroll the text to center point vertically\n\
13591 in the window.");
13592 scroll_conservatively = 0;
13594 DEFVAR_INT ("scroll-margin", &scroll_margin,
13595 "*Number of lines of margin at the top and bottom of a window.\n\
13596 Recenter the window whenever point gets within this many lines\n\
13597 of the top or bottom of the window.");
13598 scroll_margin = 0;
13600 #if GLYPH_DEBUG
13601 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13602 #endif
13604 DEFVAR_BOOL ("truncate-partial-width-windows",
13605 &truncate_partial_width_windows,
13606 "*Non-nil means truncate lines in all windows less than full frame wide.");
13607 truncate_partial_width_windows = 1;
13609 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13610 "*Non-nil means use inverse video for the mode line.");
13611 mode_line_inverse_video = 1;
13613 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13614 "*Maximum buffer size for which line number should be displayed.\n\
13615 If the buffer is bigger than this, the line number does not appear\n\
13616 in the mode line. A value of nil means no limit.");
13617 Vline_number_display_limit = Qnil;
13619 DEFVAR_INT ("line-number-display-limit-width",
13620 &line_number_display_limit_width,
13621 "*Maximum line width (in characters) for line number display.\n\
13622 If the average length of the lines near point is bigger than this, then the\n\
13623 line number may be omitted from the mode line.");
13624 line_number_display_limit_width = 200;
13626 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13627 "*Non-nil means highlight region even in nonselected windows.");
13628 highlight_nonselected_windows = 0;
13630 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13631 "Non-nil if more than one frame is visible on this display.\n\
13632 Minibuffer-only frames don't count, but iconified frames do.\n\
13633 This variable is not guaranteed to be accurate except while processing\n\
13634 `frame-title-format' and `icon-title-format'.");
13636 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13637 "Template for displaying the title bar of visible frames.\n\
13638 \(Assuming the window manager supports this feature.)\n\
13639 This variable has the same structure as `mode-line-format' (which see),\n\
13640 and is used only on frames for which no explicit name has been set\n\
13641 \(see `modify-frame-parameters').");
13642 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13643 "Template for displaying the title bar of an iconified frame.\n\
13644 \(Assuming the window manager supports this feature.)\n\
13645 This variable has the same structure as `mode-line-format' (which see),\n\
13646 and is used only on frames for which no explicit name has been set\n\
13647 \(see `modify-frame-parameters').");
13648 Vicon_title_format
13649 = Vframe_title_format
13650 = Fcons (intern ("multiple-frames"),
13651 Fcons (build_string ("%b"),
13652 Fcons (Fcons (build_string (""),
13653 Fcons (intern ("invocation-name"),
13654 Fcons (build_string ("@"),
13655 Fcons (intern ("system-name"),
13656 Qnil)))),
13657 Qnil)));
13659 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13660 "Maximum number of lines to keep in the message log buffer.\n\
13661 If nil, disable message logging. If t, log messages but don't truncate\n\
13662 the buffer when it becomes large.");
13663 XSETFASTINT (Vmessage_log_max, 50);
13665 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13666 "Functions called before redisplay, if window sizes have changed.\n\
13667 The value should be a list of functions that take one argument.\n\
13668 Just before redisplay, for each frame, if any of its windows have changed\n\
13669 size since the last redisplay, or have been split or deleted,\n\
13670 all the functions in the list are called, with the frame as argument.");
13671 Vwindow_size_change_functions = Qnil;
13673 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13674 "List of Functions to call before redisplaying a window with scrolling.\n\
13675 Each function is called with two arguments, the window\n\
13676 and its new display-start position. Note that the value of `window-end'\n\
13677 is not valid when these functions are called.");
13678 Vwindow_scroll_functions = Qnil;
13680 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13681 "*Non-nil means automatically resize tool-bars.\n\
13682 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13683 It decreases a tool-bar's height when it would display blank lines\n\
13684 otherwise.");
13685 auto_resize_tool_bars_p = 1;
13687 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13688 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13689 auto_raise_tool_bar_buttons_p = 1;
13691 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13692 "*Margin around tool-bar buttons in pixels.");
13693 tool_bar_button_margin = 1;
13695 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13696 "Relief thickness of tool-bar buttons.");
13697 tool_bar_button_relief = 3;
13699 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13700 "List of functions to call to fontify regions of text.\n\
13701 Each function is called with one argument POS. Functions must\n\
13702 fontify a region starting at POS in the current buffer, and give\n\
13703 fontified regions the property `fontified'.\n\
13704 This variable automatically becomes buffer-local when set.");
13705 Vfontification_functions = Qnil;
13706 Fmake_local_variable (Qfontification_functions);
13708 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13709 &unibyte_display_via_language_environment,
13710 "*Non-nil means display unibyte text according to language environment.\n\
13711 Specifically this means that unibyte non-ASCII characters\n\
13712 are displayed by converting them to the equivalent multibyte characters\n\
13713 according to the current language environment. As a result, they are\n\
13714 displayed according to the current fontset.");
13715 unibyte_display_via_language_environment = 0;
13717 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13718 "*Maximum height for resizing mini-windows.\n\
13719 If a float, it specifies a fraction of the mini-window frame's height.\n\
13720 If an integer, it specifies a number of lines.\n\
13721 If nil, don't resize.");
13722 Vmax_mini_window_height = make_float (0.25);
13724 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13725 &cursor_in_non_selected_windows,
13726 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13727 Nil means don't display a cursor there.");
13728 cursor_in_non_selected_windows = 1;
13730 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13731 "*Non-nil means scroll the display automatically to make point visible.");
13732 automatic_hscrolling_p = 1;
13734 DEFVAR_LISP ("image-types", &Vimage_types,
13735 "List of supported image types.\n\
13736 Each element of the list is a symbol for a supported image type.");
13737 Vimage_types = Qnil;
13739 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13740 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13741 Bind this around calls to `message' to let it take effect.");
13742 message_truncate_lines = 0;
13744 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13745 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13746 Can be used to update submenus whose contents should vary.");
13751 /* Initialize this module when Emacs starts. */
13753 void
13754 init_xdisp ()
13756 Lisp_Object root_window;
13757 struct window *mini_w;
13759 CHARPOS (this_line_start_pos) = 0;
13761 mini_w = XWINDOW (minibuf_window);
13762 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13764 if (!noninteractive)
13766 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13767 int i;
13769 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13770 set_window_height (root_window,
13771 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13773 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13774 set_window_height (minibuf_window, 1, 0);
13776 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13777 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13779 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13780 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13781 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13783 /* The default ellipsis glyphs `...'. */
13784 for (i = 0; i < 3; ++i)
13785 XSETFASTINT (default_invis_vector[i], '.');
13788 #ifdef HAVE_WINDOW_SYSTEM
13790 /* Allocate the buffer for frame titles. */
13791 int size = 100;
13792 frame_title_buf = (char *) xmalloc (size);
13793 frame_title_buf_end = frame_title_buf + size;
13794 frame_title_ptr = NULL;
13796 #endif /* HAVE_WINDOW_SYSTEM */
13798 help_echo_showing_p = 0;