*** empty log message ***
[emacs.git] / src / xdisp.c
blobcc875e59e7cb456d6410b6a36c39ddb858301483
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
204 #define INFINITY 10000000
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
211 extern int interrupt_input;
212 extern int command_loop_level;
214 extern int minibuffer_auto_raise;
216 extern Lisp_Object Qface;
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
230 /* Functions called to fontify regions of text. */
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
238 int auto_raise_tool_bar_buttons_p;
240 /* Margin around tool bar buttons in pixels. */
242 int tool_bar_button_margin;
244 /* Thickness of shadow to draw around tool bar buttons. */
246 int tool_bar_button_relief;
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
251 int auto_resize_tool_bars_p;
253 /* Non-nil means don't actually do any redisplay. */
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257 /* Names of text properties relevant for redisplay. */
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
262 /* Symbols used in text property values. */
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
269 /* Non-nil means highlight trailing whitespace. */
271 Lisp_Object Vshow_trailing_whitespace;
273 /* Name of the face used to highlight trailing whitespace. */
275 Lisp_Object Qtrailing_whitespace;
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
280 Lisp_Object Qimage;
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
285 int noninteractive_need_newline;
287 /* Non-zero means print newline to message log before next message. */
289 static int message_log_need_newline;
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
297 static struct text_pos this_line_start_pos;
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
302 static struct text_pos this_line_end_pos;
304 /* The vertical positions and the height of this line. */
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
313 static int this_line_start_x;
315 /* Buffer that this_line_.* variables are referring to. */
317 static struct buffer *this_line_buffer;
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
322 int truncate_partial_width_windows;
324 /* A flag to control how to display unibyte 8-bit character. */
326 int unibyte_display_via_language_environment;
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
332 int multiple_frames;
334 Lisp_Object Vglobal_mode_string;
336 /* Marker for where to display an arrow on top of the buffer text. */
338 Lisp_Object Voverlay_arrow_position;
340 /* String to display for the arrow. Only used on terminal frames. */
342 Lisp_Object Voverlay_arrow_string;
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
348 static Lisp_Object last_arrow_position, last_arrow_string;
350 /* Like mode-line-format, but for the title bar on a visible frame. */
352 Lisp_Object Vframe_title_format;
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
356 Lisp_Object Vicon_title_format;
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
362 static Lisp_Object Vwindow_size_change_functions;
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
366 /* Nonzero if overlay arrow has been displayed once in this window. */
368 static int overlay_arrow_seen;
370 /* Nonzero means highlight the region even in nonselected windows. */
372 int highlight_nonselected_windows;
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
377 static int scroll_step;
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
382 static int scroll_conservatively;
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
389 int scroll_margin;
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
395 int buffer_shared;
397 /* Vector containing glyphs for an ellipsis `...'. */
399 static Lisp_Object default_invis_vector[3];
401 /* Nonzero means display mode line highlighted. */
403 int mode_line_inverse_video;
405 /* Prompt to display in front of the mini-buffer contents. */
407 Lisp_Object minibuf_prompt;
409 /* Width of current mini-buffer prompt. Only set after display_line
410 of the line that contains the prompt. */
412 int minibuf_prompt_width;
413 int minibuf_prompt_pixel_width;
415 /* This is the window where the echo area message was displayed. It
416 is always a mini-buffer window, but it may not be the same window
417 currently active as a mini-buffer. */
419 Lisp_Object echo_area_window;
421 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
422 pushes the current message and the value of
423 message_enable_multibyte on the stack, the function restore_message
424 pops the stack and displays MESSAGE again. */
426 Lisp_Object Vmessage_stack;
428 /* Nonzero means multibyte characters were enabled when the echo area
429 message was specified. */
431 int message_enable_multibyte;
433 /* True if we should redraw the mode lines on the next redisplay. */
435 int update_mode_lines;
437 /* Nonzero if window sizes or contents have changed since last
438 redisplay that finished */
440 int windows_or_buffers_changed;
442 /* Nonzero after display_mode_line if %l was used and it displayed a
443 line number. */
445 int line_number_displayed;
447 /* Maximum buffer size for which to display line numbers. */
449 Lisp_Object Vline_number_display_limit;
451 /* line width to consider when repostioning for line number display */
453 static int line_number_display_limit_width;
455 /* Number of lines to keep in the message log buffer. t means
456 infinite. nil means don't log at all. */
458 Lisp_Object Vmessage_log_max;
460 /* The name of the *Messages* buffer, a string. */
462 static Lisp_Object Vmessages_buffer_name;
464 /* Current, index 0, and last displayed echo area message. Either
465 buffers from echo_buffers, or nil to indicate no message. */
467 Lisp_Object echo_area_buffer[2];
469 /* The buffers referenced from echo_area_buffer. */
471 static Lisp_Object echo_buffer[2];
473 /* A vector saved used in with_area_buffer to reduce consing. */
475 static Lisp_Object Vwith_echo_area_save_vector;
477 /* Non-zero means display_echo_area should display the last echo area
478 message again. Set by redisplay_preserve_echo_area. */
480 static int display_last_displayed_message_p;
482 /* Nonzero if echo area is being used by print; zero if being used by
483 message. */
485 int message_buf_print;
487 /* Maximum height for resizing mini-windows. Either a float
488 specifying a fraction of the available height, or an integer
489 specifying a number of lines. */
491 Lisp_Object Vmax_mini_window_height;
493 /* Non-zero means messages should be displayed with truncated
494 lines instead of being continued. */
496 int message_truncate_lines;
497 Lisp_Object Qmessage_truncate_lines;
499 /* Non-zero means we want a hollow cursor in windows that are not
500 selected. Zero means there's no cursor in such windows. */
502 int cursor_in_non_selected_windows;
504 /* A scratch glyph row with contents used for generating truncation
505 glyphs. Also used in direct_output_for_insert. */
507 #define MAX_SCRATCH_GLYPHS 100
508 struct glyph_row scratch_glyph_row;
509 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
511 /* Ascent and height of the last line processed by move_it_to. */
513 static int last_max_ascent, last_height;
515 /* Non-zero if there's a help-echo in the echo area. */
517 int help_echo_showing_p;
519 /* If >= 0, computed, exact values of mode-line and header-line height
520 to use in the macros CURRENT_MODE_LINE_HEIGHT and
521 CURRENT_HEADER_LINE_HEIGHT. */
523 int current_mode_line_height, current_header_line_height;
525 /* The maximum distance to look ahead for text properties. Values
526 that are too small let us call compute_char_face and similar
527 functions too often which is expensive. Values that are too large
528 let us call compute_char_face and alike too often because we
529 might not be interested in text properties that far away. */
531 #define TEXT_PROP_DISTANCE_LIMIT 100
533 #if GLYPH_DEBUG
535 /* Non-zero means print traces of redisplay if compiled with
536 GLYPH_DEBUG != 0. */
538 int trace_redisplay_p;
540 #endif /* GLYPH_DEBUG */
542 #ifdef DEBUG_TRACE_MOVE
543 /* Non-zero means trace with TRACE_MOVE to stderr. */
544 int trace_move;
546 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
547 #else
548 #define TRACE_MOVE(x) (void) 0
549 #endif
551 /* Non-zero means automatically scroll windows horizontally to make
552 point visible. */
554 int automatic_hscrolling_p;
556 /* A list of symbols, one for each supported image type. */
558 Lisp_Object Vimage_types;
560 /* The variable `resize-mini-windows'. If nil, don't resize
561 mini-windows. If t, always resize them to fit the text they
562 display. If `grow-only', let mini-windows grow only until they
563 become empty. */
565 Lisp_Object Vresize_mini_windows;
567 /* Value returned from text property handlers (see below). */
569 enum prop_handled
571 HANDLED_NORMALLY,
572 HANDLED_RECOMPUTE_PROPS,
573 HANDLED_OVERLAY_STRING_CONSUMED,
574 HANDLED_RETURN
577 /* A description of text properties that redisplay is interested
578 in. */
580 struct props
582 /* The name of the property. */
583 Lisp_Object *name;
585 /* A unique index for the property. */
586 enum prop_idx idx;
588 /* A handler function called to set up iterator IT from the property
589 at IT's current position. Value is used to steer handle_stop. */
590 enum prop_handled (*handler) P_ ((struct it *it));
593 static enum prop_handled handle_face_prop P_ ((struct it *));
594 static enum prop_handled handle_invisible_prop P_ ((struct it *));
595 static enum prop_handled handle_display_prop P_ ((struct it *));
596 static enum prop_handled handle_composition_prop P_ ((struct it *));
597 static enum prop_handled handle_overlay_change P_ ((struct it *));
598 static enum prop_handled handle_fontified_prop P_ ((struct it *));
600 /* Properties handled by iterators. */
602 static struct props it_props[] =
604 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
605 /* Handle `face' before `display' because some sub-properties of
606 `display' need to know the face. */
607 {&Qface, FACE_PROP_IDX, handle_face_prop},
608 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
609 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
610 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
611 {NULL, 0, NULL}
614 /* Value is the position described by X. If X is a marker, value is
615 the marker_position of X. Otherwise, value is X. */
617 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
619 /* Enumeration returned by some move_it_.* functions internally. */
621 enum move_it_result
623 /* Not used. Undefined value. */
624 MOVE_UNDEFINED,
626 /* Move ended at the requested buffer position or ZV. */
627 MOVE_POS_MATCH_OR_ZV,
629 /* Move ended at the requested X pixel position. */
630 MOVE_X_REACHED,
632 /* Move within a line ended at the end of a line that must be
633 continued. */
634 MOVE_LINE_CONTINUED,
636 /* Move within a line ended at the end of a line that would
637 be displayed truncated. */
638 MOVE_LINE_TRUNCATED,
640 /* Move within a line ended at a line end. */
641 MOVE_NEWLINE_OR_CR
646 /* Function prototypes. */
648 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
649 static int redisplay_mode_lines P_ ((Lisp_Object, int));
650 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
651 static int invisible_text_between_p P_ ((struct it *, int, int));
652 static int next_element_from_ellipsis P_ ((struct it *));
653 static void pint2str P_ ((char *, int, int));
654 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
655 struct text_pos));
656 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
657 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
658 static void store_frame_title_char P_ ((char));
659 static int store_frame_title P_ ((unsigned char *, int, int));
660 static void x_consider_frame_title P_ ((Lisp_Object));
661 static void handle_stop P_ ((struct it *));
662 static int tool_bar_lines_needed P_ ((struct frame *));
663 static int single_display_prop_intangible_p P_ ((Lisp_Object));
664 static void ensure_echo_area_buffers P_ ((void));
665 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
666 struct glyph_row *,
667 struct glyph_row *));
668 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
669 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
670 static int with_echo_area_buffer P_ ((struct window *, int,
671 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
672 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
673 static void clear_garbaged_frames P_ ((void));
674 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
675 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
676 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
677 static int display_echo_area P_ ((struct window *));
678 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
679 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
680 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
681 static int string_char_and_length P_ ((unsigned char *, int, int *));
682 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
683 struct text_pos));
684 static int compute_window_start_on_continuation_line P_ ((struct window *));
685 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
686 static void insert_left_trunc_glyphs P_ ((struct it *));
687 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
688 static void extend_face_to_end_of_line P_ ((struct it *));
689 static int append_space P_ ((struct it *, int));
690 static void make_cursor_line_fully_visible P_ ((struct window *));
691 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
692 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
693 static int trailing_whitespace_p P_ ((int));
694 static int message_log_check_duplicate P_ ((int, int, int, int));
695 int invisible_p P_ ((Lisp_Object, Lisp_Object));
696 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
697 static void push_it P_ ((struct it *));
698 static void pop_it P_ ((struct it *));
699 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
700 static void redisplay_internal P_ ((int));
701 static int echo_area_display P_ ((int));
702 static void redisplay_windows P_ ((Lisp_Object));
703 static void redisplay_window P_ ((Lisp_Object, int));
704 static void update_menu_bar P_ ((struct frame *, int));
705 static int try_window_reusing_current_matrix P_ ((struct window *));
706 static int try_window_id P_ ((struct window *));
707 static int display_line P_ ((struct it *));
708 static int display_mode_lines P_ ((struct window *));
709 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
710 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
711 static char *decode_mode_spec P_ ((struct window *, int, int, int));
712 static void display_menu_bar P_ ((struct window *));
713 static int display_count_lines P_ ((int, int, int, int, int *));
714 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
715 int, int, struct it *, int, int, int, int));
716 static void compute_line_metrics P_ ((struct it *));
717 static void run_redisplay_end_trigger_hook P_ ((struct it *));
718 static int get_overlay_strings P_ ((struct it *));
719 static void next_overlay_string P_ ((struct it *));
720 static void reseat P_ ((struct it *, struct text_pos, int));
721 static void reseat_1 P_ ((struct it *, struct text_pos, int));
722 static void back_to_previous_visible_line_start P_ ((struct it *));
723 static void reseat_at_previous_visible_line_start P_ ((struct it *));
724 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
725 static int next_element_from_display_vector P_ ((struct it *));
726 static int next_element_from_string P_ ((struct it *));
727 static int next_element_from_c_string P_ ((struct it *));
728 static int next_element_from_buffer P_ ((struct it *));
729 static int next_element_from_composition P_ ((struct it *));
730 static int next_element_from_image P_ ((struct it *));
731 static int next_element_from_stretch P_ ((struct it *));
732 static void load_overlay_strings P_ ((struct it *));
733 static void init_from_display_pos P_ ((struct it *, struct window *,
734 struct display_pos *));
735 static void reseat_to_string P_ ((struct it *, unsigned char *,
736 Lisp_Object, int, int, int, int));
737 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
738 int, int, int));
739 void move_it_vertically_backward P_ ((struct it *, int));
740 static void init_to_row_start P_ ((struct it *, struct window *,
741 struct glyph_row *));
742 static void init_to_row_end P_ ((struct it *, struct window *,
743 struct glyph_row *));
744 static void back_to_previous_line_start P_ ((struct it *));
745 static int forward_to_next_line_start P_ ((struct it *, int *));
746 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
747 Lisp_Object, int));
748 static struct text_pos string_pos P_ ((int, Lisp_Object));
749 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
750 static int number_of_chars P_ ((unsigned char *, int));
751 static void compute_stop_pos P_ ((struct it *));
752 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
753 Lisp_Object));
754 static int face_before_or_after_it_pos P_ ((struct it *, int));
755 static int next_overlay_change P_ ((int));
756 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
757 Lisp_Object, struct text_pos *));
759 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
760 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
762 #ifdef HAVE_WINDOW_SYSTEM
764 static void update_tool_bar P_ ((struct frame *, int));
765 static void build_desired_tool_bar_string P_ ((struct frame *f));
766 static int redisplay_tool_bar P_ ((struct frame *));
767 static void display_tool_bar_line P_ ((struct it *));
769 #endif /* HAVE_WINDOW_SYSTEM */
772 /***********************************************************************
773 Window display dimensions
774 ***********************************************************************/
776 /* Return the window-relative maximum y + 1 for glyph rows displaying
777 text in window W. This is the height of W minus the height of a
778 mode line, if any. */
780 INLINE int
781 window_text_bottom_y (w)
782 struct window *w;
784 struct frame *f = XFRAME (w->frame);
785 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
787 if (WINDOW_WANTS_MODELINE_P (w))
788 height -= CURRENT_MODE_LINE_HEIGHT (w);
789 return height;
793 /* Return the pixel width of display area AREA of window W. AREA < 0
794 means return the total width of W, not including bitmap areas to
795 the left and right of the window. */
797 INLINE int
798 window_box_width (w, area)
799 struct window *w;
800 int area;
802 struct frame *f = XFRAME (w->frame);
803 int width = XFASTINT (w->width);
805 if (!w->pseudo_window_p)
807 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
809 if (area == TEXT_AREA)
811 if (INTEGERP (w->left_margin_width))
812 width -= XFASTINT (w->left_margin_width);
813 if (INTEGERP (w->right_margin_width))
814 width -= XFASTINT (w->right_margin_width);
816 else if (area == LEFT_MARGIN_AREA)
817 width = (INTEGERP (w->left_margin_width)
818 ? XFASTINT (w->left_margin_width) : 0);
819 else if (area == RIGHT_MARGIN_AREA)
820 width = (INTEGERP (w->right_margin_width)
821 ? XFASTINT (w->right_margin_width) : 0);
824 return width * CANON_X_UNIT (f);
828 /* Return the pixel height of the display area of window W, not
829 including mode lines of W, if any.. */
831 INLINE int
832 window_box_height (w)
833 struct window *w;
835 struct frame *f = XFRAME (w->frame);
836 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
838 xassert (height >= 0);
840 if (WINDOW_WANTS_MODELINE_P (w))
841 height -= CURRENT_MODE_LINE_HEIGHT (w);
843 if (WINDOW_WANTS_HEADER_LINE_P (w))
844 height -= CURRENT_HEADER_LINE_HEIGHT (w);
846 return height;
850 /* Return the frame-relative coordinate of the left edge of display
851 area AREA of window W. AREA < 0 means return the left edge of the
852 whole window, to the right of any bitmap area at the left side of
853 W. */
855 INLINE int
856 window_box_left (w, area)
857 struct window *w;
858 int area;
860 struct frame *f = XFRAME (w->frame);
861 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
863 if (!w->pseudo_window_p)
865 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
866 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
868 if (area == TEXT_AREA)
869 x += window_box_width (w, LEFT_MARGIN_AREA);
870 else if (area == RIGHT_MARGIN_AREA)
871 x += (window_box_width (w, LEFT_MARGIN_AREA)
872 + window_box_width (w, TEXT_AREA));
875 return x;
879 /* Return the frame-relative coordinate of the right edge of display
880 area AREA of window W. AREA < 0 means return the left edge of the
881 whole window, to the left of any bitmap area at the right side of
882 W. */
884 INLINE int
885 window_box_right (w, area)
886 struct window *w;
887 int area;
889 return window_box_left (w, area) + window_box_width (w, area);
893 /* Get the bounding box of the display area AREA of window W, without
894 mode lines, in frame-relative coordinates. AREA < 0 means the
895 whole window, not including bitmap areas to the left and right of
896 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
897 coordinates of the upper-left corner of the box. Return in
898 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
900 INLINE void
901 window_box (w, area, box_x, box_y, box_width, box_height)
902 struct window *w;
903 int area;
904 int *box_x, *box_y, *box_width, *box_height;
906 struct frame *f = XFRAME (w->frame);
908 *box_width = window_box_width (w, area);
909 *box_height = window_box_height (w);
910 *box_x = window_box_left (w, area);
911 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
912 + XFASTINT (w->top) * CANON_Y_UNIT (f));
913 if (WINDOW_WANTS_HEADER_LINE_P (w))
914 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
918 /* Get the bounding box of the display area AREA of window W, without
919 mode lines. AREA < 0 means the whole window, not including bitmap
920 areas to the left and right of the window. Return in *TOP_LEFT_X
921 and TOP_LEFT_Y the frame-relative pixel coordinates of the
922 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
923 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
924 box. */
926 INLINE void
927 window_box_edges (w, area, top_left_x, top_left_y,
928 bottom_right_x, bottom_right_y)
929 struct window *w;
930 int area;
931 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
933 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
934 bottom_right_y);
935 *bottom_right_x += *top_left_x;
936 *bottom_right_y += *top_left_y;
941 /***********************************************************************
942 Utilities
943 ***********************************************************************/
945 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
946 1 if POS is visible and the line containing POS is fully visible.
947 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
948 and header-lines heights. */
951 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
952 struct window *w;
953 int charpos, *fully, exact_mode_line_heights_p;
955 struct it it;
956 struct text_pos top;
957 int visible_p;
958 struct buffer *old_buffer = NULL;
960 if (XBUFFER (w->buffer) != current_buffer)
962 old_buffer = current_buffer;
963 set_buffer_internal_1 (XBUFFER (w->buffer));
966 *fully = visible_p = 0;
967 SET_TEXT_POS_FROM_MARKER (top, w->start);
969 /* Compute exact mode line heights, if requested. */
970 if (exact_mode_line_heights_p)
972 if (WINDOW_WANTS_MODELINE_P (w))
973 current_mode_line_height
974 = display_mode_line (w, MODE_LINE_FACE_ID,
975 current_buffer->mode_line_format);
977 if (WINDOW_WANTS_HEADER_LINE_P (w))
978 current_header_line_height
979 = display_mode_line (w, HEADER_LINE_FACE_ID,
980 current_buffer->header_line_format);
983 start_display (&it, w, top);
984 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
985 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
987 if (IT_CHARPOS (it) == charpos)
989 int line_height, line_bottom_y;
990 int line_top_y = it.current_y;
991 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
993 line_height = it.max_ascent + it.max_descent;
994 if (line_height == 0)
996 if (last_height)
997 line_height = last_height;
998 else if (charpos < ZV)
1000 move_it_by_lines (&it, 1, 1);
1001 line_height = (it.max_ascent || it.max_descent
1002 ? it.max_ascent + it.max_descent
1003 : last_height);
1005 else
1007 /* Use the default character height. */
1008 it.what = IT_CHARACTER;
1009 it.c = ' ';
1010 it.len = 1;
1011 PRODUCE_GLYPHS (&it);
1012 line_height = it.ascent + it.descent;
1015 line_bottom_y = line_top_y + line_height;
1017 if (line_top_y < window_top_y)
1018 visible_p = line_bottom_y > window_top_y;
1019 else if (line_top_y < it.last_visible_y)
1021 visible_p = 1;
1022 *fully = line_bottom_y <= it.last_visible_y;
1025 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1027 move_it_by_lines (&it, 1, 0);
1028 if (charpos < IT_CHARPOS (it))
1030 visible_p = 1;
1031 *fully = 0;
1035 if (old_buffer)
1036 set_buffer_internal_1 (old_buffer);
1038 current_header_line_height = current_mode_line_height = -1;
1039 return visible_p;
1043 /* Return the next character from STR which is MAXLEN bytes long.
1044 Return in *LEN the length of the character. This is like
1045 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1046 we find one, we return a `?', but with the length of the invalid
1047 character. */
1049 static INLINE int
1050 string_char_and_length (str, maxlen, len)
1051 unsigned char *str;
1052 int maxlen, *len;
1054 int c;
1056 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1057 if (!CHAR_VALID_P (c, 1))
1058 /* We may not change the length here because other places in Emacs
1059 don't use this function, i.e. they silently accept invalid
1060 characters. */
1061 c = '?';
1063 return c;
1068 /* Given a position POS containing a valid character and byte position
1069 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1071 static struct text_pos
1072 string_pos_nchars_ahead (pos, string, nchars)
1073 struct text_pos pos;
1074 Lisp_Object string;
1075 int nchars;
1077 xassert (STRINGP (string) && nchars >= 0);
1079 if (STRING_MULTIBYTE (string))
1081 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1082 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1083 int len;
1085 while (nchars--)
1087 string_char_and_length (p, rest, &len);
1088 p += len, rest -= len;
1089 xassert (rest >= 0);
1090 CHARPOS (pos) += 1;
1091 BYTEPOS (pos) += len;
1094 else
1095 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1097 return pos;
1101 /* Value is the text position, i.e. character and byte position,
1102 for character position CHARPOS in STRING. */
1104 static INLINE struct text_pos
1105 string_pos (charpos, string)
1106 int charpos;
1107 Lisp_Object string;
1109 struct text_pos pos;
1110 xassert (STRINGP (string));
1111 xassert (charpos >= 0);
1112 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1113 return pos;
1117 /* Value is a text position, i.e. character and byte position, for
1118 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1119 means recognize multibyte characters. */
1121 static struct text_pos
1122 c_string_pos (charpos, s, multibyte_p)
1123 int charpos;
1124 unsigned char *s;
1125 int multibyte_p;
1127 struct text_pos pos;
1129 xassert (s != NULL);
1130 xassert (charpos >= 0);
1132 if (multibyte_p)
1134 int rest = strlen (s), len;
1136 SET_TEXT_POS (pos, 0, 0);
1137 while (charpos--)
1139 string_char_and_length (s, rest, &len);
1140 s += len, rest -= len;
1141 xassert (rest >= 0);
1142 CHARPOS (pos) += 1;
1143 BYTEPOS (pos) += len;
1146 else
1147 SET_TEXT_POS (pos, charpos, charpos);
1149 return pos;
1153 /* Value is the number of characters in C string S. MULTIBYTE_P
1154 non-zero means recognize multibyte characters. */
1156 static int
1157 number_of_chars (s, multibyte_p)
1158 unsigned char *s;
1159 int multibyte_p;
1161 int nchars;
1163 if (multibyte_p)
1165 int rest = strlen (s), len;
1166 unsigned char *p = (unsigned char *) s;
1168 for (nchars = 0; rest > 0; ++nchars)
1170 string_char_and_length (p, rest, &len);
1171 rest -= len, p += len;
1174 else
1175 nchars = strlen (s);
1177 return nchars;
1181 /* Compute byte position NEWPOS->bytepos corresponding to
1182 NEWPOS->charpos. POS is a known position in string STRING.
1183 NEWPOS->charpos must be >= POS.charpos. */
1185 static void
1186 compute_string_pos (newpos, pos, string)
1187 struct text_pos *newpos, pos;
1188 Lisp_Object string;
1190 xassert (STRINGP (string));
1191 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1193 if (STRING_MULTIBYTE (string))
1194 *newpos = string_pos_nchars_ahead (pos, string,
1195 CHARPOS (*newpos) - CHARPOS (pos));
1196 else
1197 BYTEPOS (*newpos) = CHARPOS (*newpos);
1202 /***********************************************************************
1203 Lisp form evaluation
1204 ***********************************************************************/
1206 /* Error handler for safe_eval and safe_call. */
1208 static Lisp_Object
1209 safe_eval_handler (arg)
1210 Lisp_Object arg;
1212 add_to_log ("Error during redisplay: %s", arg, Qnil);
1213 return Qnil;
1217 /* Evaluate SEXPR and return the result, or nil if something went
1218 wrong. */
1220 Lisp_Object
1221 safe_eval (sexpr)
1222 Lisp_Object sexpr;
1224 int count = BINDING_STACK_SIZE ();
1225 struct gcpro gcpro1;
1226 Lisp_Object val;
1228 GCPRO1 (sexpr);
1229 specbind (Qinhibit_redisplay, Qt);
1230 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1231 UNGCPRO;
1232 return unbind_to (count, val);
1236 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1237 Return the result, or nil if something went wrong. */
1239 Lisp_Object
1240 safe_call (nargs, args)
1241 int nargs;
1242 Lisp_Object *args;
1244 int count = BINDING_STACK_SIZE ();
1245 Lisp_Object val;
1246 struct gcpro gcpro1;
1248 GCPRO1 (args[0]);
1249 gcpro1.nvars = nargs;
1250 specbind (Qinhibit_redisplay, Qt);
1251 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1252 safe_eval_handler);
1253 UNGCPRO;
1254 return unbind_to (count, val);
1258 /* Call function FN with one argument ARG.
1259 Return the result, or nil if something went wrong. */
1261 Lisp_Object
1262 safe_call1 (fn, arg)
1263 Lisp_Object fn, arg;
1265 Lisp_Object args[2];
1266 args[0] = fn;
1267 args[1] = arg;
1268 return safe_call (2, args);
1273 /***********************************************************************
1274 Debugging
1275 ***********************************************************************/
1277 #if 0
1279 /* Define CHECK_IT to perform sanity checks on iterators.
1280 This is for debugging. It is too slow to do unconditionally. */
1282 static void
1283 check_it (it)
1284 struct it *it;
1286 if (it->method == next_element_from_string)
1288 xassert (STRINGP (it->string));
1289 xassert (IT_STRING_CHARPOS (*it) >= 0);
1291 else if (it->method == next_element_from_buffer)
1293 /* Check that character and byte positions agree. */
1294 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1297 if (it->dpvec)
1298 xassert (it->current.dpvec_index >= 0);
1299 else
1300 xassert (it->current.dpvec_index < 0);
1303 #define CHECK_IT(IT) check_it ((IT))
1305 #else /* not 0 */
1307 #define CHECK_IT(IT) (void) 0
1309 #endif /* not 0 */
1312 #if GLYPH_DEBUG
1314 /* Check that the window end of window W is what we expect it
1315 to be---the last row in the current matrix displaying text. */
1317 static void
1318 check_window_end (w)
1319 struct window *w;
1321 if (!MINI_WINDOW_P (w)
1322 && !NILP (w->window_end_valid))
1324 struct glyph_row *row;
1325 xassert ((row = MATRIX_ROW (w->current_matrix,
1326 XFASTINT (w->window_end_vpos)),
1327 !row->enabled_p
1328 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1329 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1333 #define CHECK_WINDOW_END(W) check_window_end ((W))
1335 #else /* not GLYPH_DEBUG */
1337 #define CHECK_WINDOW_END(W) (void) 0
1339 #endif /* not GLYPH_DEBUG */
1343 /***********************************************************************
1344 Iterator initialization
1345 ***********************************************************************/
1347 /* Initialize IT for displaying current_buffer in window W, starting
1348 at character position CHARPOS. CHARPOS < 0 means that no buffer
1349 position is specified which is useful when the iterator is assigned
1350 a position later. BYTEPOS is the byte position corresponding to
1351 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1353 If ROW is not null, calls to produce_glyphs with IT as parameter
1354 will produce glyphs in that row.
1356 BASE_FACE_ID is the id of a base face to use. It must be one of
1357 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1358 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1359 displaying the tool-bar.
1361 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1362 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1363 corresponding mode line glyph row of the desired matrix of W. */
1365 void
1366 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1367 struct it *it;
1368 struct window *w;
1369 int charpos, bytepos;
1370 struct glyph_row *row;
1371 enum face_id base_face_id;
1373 int highlight_region_p;
1375 /* Some precondition checks. */
1376 xassert (w != NULL && it != NULL);
1377 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1379 /* If face attributes have been changed since the last redisplay,
1380 free realized faces now because they depend on face definitions
1381 that might have changed. */
1382 if (face_change_count)
1384 face_change_count = 0;
1385 free_all_realized_faces (Qnil);
1388 /* Use one of the mode line rows of W's desired matrix if
1389 appropriate. */
1390 if (row == NULL)
1392 if (base_face_id == MODE_LINE_FACE_ID)
1393 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1394 else if (base_face_id == HEADER_LINE_FACE_ID)
1395 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1398 /* Clear IT. */
1399 bzero (it, sizeof *it);
1400 it->current.overlay_string_index = -1;
1401 it->current.dpvec_index = -1;
1402 it->base_face_id = base_face_id;
1404 /* The window in which we iterate over current_buffer: */
1405 XSETWINDOW (it->window, w);
1406 it->w = w;
1407 it->f = XFRAME (w->frame);
1409 /* Extra space between lines (on window systems only). */
1410 if (base_face_id == DEFAULT_FACE_ID
1411 && FRAME_WINDOW_P (it->f))
1413 if (NATNUMP (current_buffer->extra_line_spacing))
1414 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1415 else if (it->f->extra_line_spacing > 0)
1416 it->extra_line_spacing = it->f->extra_line_spacing;
1419 /* If realized faces have been removed, e.g. because of face
1420 attribute changes of named faces, recompute them. */
1421 if (FRAME_FACE_CACHE (it->f)->used == 0)
1422 recompute_basic_faces (it->f);
1424 /* Current value of the `space-width', and 'height' properties. */
1425 it->space_width = Qnil;
1426 it->font_height = Qnil;
1428 /* Are control characters displayed as `^C'? */
1429 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1431 /* -1 means everything between a CR and the following line end
1432 is invisible. >0 means lines indented more than this value are
1433 invisible. */
1434 it->selective = (INTEGERP (current_buffer->selective_display)
1435 ? XFASTINT (current_buffer->selective_display)
1436 : (!NILP (current_buffer->selective_display)
1437 ? -1 : 0));
1438 it->selective_display_ellipsis_p
1439 = !NILP (current_buffer->selective_display_ellipses);
1441 /* Display table to use. */
1442 it->dp = window_display_table (w);
1444 /* Are multibyte characters enabled in current_buffer? */
1445 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1447 /* Non-zero if we should highlight the region. */
1448 highlight_region_p
1449 = (!NILP (Vtransient_mark_mode)
1450 && !NILP (current_buffer->mark_active)
1451 && XMARKER (current_buffer->mark)->buffer != 0);
1453 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1454 start and end of a visible region in window IT->w. Set both to
1455 -1 to indicate no region. */
1456 if (highlight_region_p
1457 /* Maybe highlight only in selected window. */
1458 && (/* Either show region everywhere. */
1459 highlight_nonselected_windows
1460 /* Or show region in the selected window. */
1461 || w == XWINDOW (selected_window)
1462 /* Or show the region if we are in the mini-buffer and W is
1463 the window the mini-buffer refers to. */
1464 || (MINI_WINDOW_P (XWINDOW (selected_window))
1465 && w == XWINDOW (Vminibuf_scroll_window))))
1467 int charpos = marker_position (current_buffer->mark);
1468 it->region_beg_charpos = min (PT, charpos);
1469 it->region_end_charpos = max (PT, charpos);
1471 else
1472 it->region_beg_charpos = it->region_end_charpos = -1;
1474 /* Get the position at which the redisplay_end_trigger hook should
1475 be run, if it is to be run at all. */
1476 if (MARKERP (w->redisplay_end_trigger)
1477 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1478 it->redisplay_end_trigger_charpos
1479 = marker_position (w->redisplay_end_trigger);
1480 else if (INTEGERP (w->redisplay_end_trigger))
1481 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1483 /* Correct bogus values of tab_width. */
1484 it->tab_width = XINT (current_buffer->tab_width);
1485 if (it->tab_width <= 0 || it->tab_width > 1000)
1486 it->tab_width = 8;
1488 /* Are lines in the display truncated? */
1489 it->truncate_lines_p
1490 = (base_face_id != DEFAULT_FACE_ID
1491 || XINT (it->w->hscroll)
1492 || (truncate_partial_width_windows
1493 && !WINDOW_FULL_WIDTH_P (it->w))
1494 || !NILP (current_buffer->truncate_lines));
1496 /* Get dimensions of truncation and continuation glyphs. These are
1497 displayed as bitmaps under X, so we don't need them for such
1498 frames. */
1499 if (!FRAME_WINDOW_P (it->f))
1501 if (it->truncate_lines_p)
1503 /* We will need the truncation glyph. */
1504 xassert (it->glyph_row == NULL);
1505 produce_special_glyphs (it, IT_TRUNCATION);
1506 it->truncation_pixel_width = it->pixel_width;
1508 else
1510 /* We will need the continuation glyph. */
1511 xassert (it->glyph_row == NULL);
1512 produce_special_glyphs (it, IT_CONTINUATION);
1513 it->continuation_pixel_width = it->pixel_width;
1516 /* Reset these values to zero becaue the produce_special_glyphs
1517 above has changed them. */
1518 it->pixel_width = it->ascent = it->descent = 0;
1519 it->phys_ascent = it->phys_descent = 0;
1522 /* Set this after getting the dimensions of truncation and
1523 continuation glyphs, so that we don't produce glyphs when calling
1524 produce_special_glyphs, above. */
1525 it->glyph_row = row;
1526 it->area = TEXT_AREA;
1528 /* Get the dimensions of the display area. The display area
1529 consists of the visible window area plus a horizontally scrolled
1530 part to the left of the window. All x-values are relative to the
1531 start of this total display area. */
1532 if (base_face_id != DEFAULT_FACE_ID)
1534 /* Mode lines, menu bar in terminal frames. */
1535 it->first_visible_x = 0;
1536 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1538 else
1540 it->first_visible_x
1541 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1542 it->last_visible_x = (it->first_visible_x
1543 + window_box_width (w, TEXT_AREA));
1545 /* If we truncate lines, leave room for the truncator glyph(s) at
1546 the right margin. Otherwise, leave room for the continuation
1547 glyph(s). Truncation and continuation glyphs are not inserted
1548 for window-based redisplay. */
1549 if (!FRAME_WINDOW_P (it->f))
1551 if (it->truncate_lines_p)
1552 it->last_visible_x -= it->truncation_pixel_width;
1553 else
1554 it->last_visible_x -= it->continuation_pixel_width;
1557 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1558 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1561 /* Leave room for a border glyph. */
1562 if (!FRAME_WINDOW_P (it->f)
1563 && !WINDOW_RIGHTMOST_P (it->w))
1564 it->last_visible_x -= 1;
1566 it->last_visible_y = window_text_bottom_y (w);
1568 /* For mode lines and alike, arrange for the first glyph having a
1569 left box line if the face specifies a box. */
1570 if (base_face_id != DEFAULT_FACE_ID)
1572 struct face *face;
1574 it->face_id = base_face_id;
1576 /* If we have a boxed mode line, make the first character appear
1577 with a left box line. */
1578 face = FACE_FROM_ID (it->f, base_face_id);
1579 if (face->box != FACE_NO_BOX)
1580 it->start_of_box_run_p = 1;
1583 /* If a buffer position was specified, set the iterator there,
1584 getting overlays and face properties from that position. */
1585 if (charpos > 0)
1587 it->end_charpos = ZV;
1588 it->face_id = -1;
1589 IT_CHARPOS (*it) = charpos;
1591 /* Compute byte position if not specified. */
1592 if (bytepos <= 0)
1593 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1594 else
1595 IT_BYTEPOS (*it) = bytepos;
1597 /* Compute faces etc. */
1598 reseat (it, it->current.pos, 1);
1601 CHECK_IT (it);
1605 /* Initialize IT for the display of window W with window start POS. */
1607 void
1608 start_display (it, w, pos)
1609 struct it *it;
1610 struct window *w;
1611 struct text_pos pos;
1613 int start_at_line_beg_p;
1614 struct glyph_row *row;
1615 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1616 int first_y;
1618 row = w->desired_matrix->rows + first_vpos;
1619 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1620 first_y = it->current_y;
1622 /* If window start is not at a line start, move back to the line
1623 start. This makes sure that we take continuation lines into
1624 account. */
1625 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1626 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1627 if (!start_at_line_beg_p)
1628 reseat_at_previous_visible_line_start (it);
1630 /* If window start is not at a line start, skip forward to POS to
1631 get the correct continuation_lines_width and current_x. */
1632 if (!start_at_line_beg_p)
1634 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1636 /* If lines are continued, this line may end in the middle of a
1637 multi-glyph character (e.g. a control character displayed as
1638 \003, or in the middle of an overlay string). In this case
1639 move_it_to above will not have taken us to the start of
1640 the continuation line but to the end of the continued line. */
1641 if (!it->truncate_lines_p)
1643 if (it->current_x > 0)
1645 if (it->current.dpvec_index >= 0
1646 || it->current.overlay_string_index >= 0)
1648 set_iterator_to_next (it, 1);
1649 move_it_in_display_line_to (it, -1, -1, 0);
1652 it->continuation_lines_width += it->current_x;
1655 /* We're starting a new display line, not affected by the
1656 height of the continued line, so clear the appropriate
1657 fields in the iterator structure. */
1658 it->max_ascent = it->max_descent = 0;
1659 it->max_phys_ascent = it->max_phys_descent = 0;
1662 it->current_y = first_y;
1663 it->vpos = 0;
1664 it->current_x = it->hpos = 0;
1667 #if 0 /* Don't assert the following because start_display is sometimes
1668 called intentionally with a window start that is not at a
1669 line start. Please leave this code in as a comment. */
1671 /* Window start should be on a line start, now. */
1672 xassert (it->continuation_lines_width
1673 || IT_CHARPOS (it) == BEGV
1674 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1675 #endif /* 0 */
1679 /* Initialize IT for stepping through current_buffer in window W,
1680 starting at position POS that includes overlay string and display
1681 vector/ control character translation position information. */
1683 static void
1684 init_from_display_pos (it, w, pos)
1685 struct it *it;
1686 struct window *w;
1687 struct display_pos *pos;
1689 /* Keep in mind: the call to reseat in init_iterator skips invisible
1690 text, so we might end up at a position different from POS. This
1691 is only a problem when POS is a row start after a newline and an
1692 overlay starts there with an after-string, and the overlay has an
1693 invisible property. Since we don't skip invisible text in
1694 display_line and elsewhere immediately after consuming the
1695 newline before the row start, such a POS will not be in a string,
1696 but the call to init_iterator below will move us to the
1697 after-string. */
1698 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1699 NULL, DEFAULT_FACE_ID);
1701 /* If position is within an overlay string, set up IT to
1702 the right overlay string. */
1703 if (pos->overlay_string_index >= 0)
1705 int relative_index;
1707 /* We already have the first chunk of overlay strings in
1708 IT->overlay_strings. Load more until the one for
1709 pos->overlay_string_index is in IT->overlay_strings. */
1710 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1712 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1713 it->current.overlay_string_index = 0;
1714 while (n--)
1716 load_overlay_strings (it);
1717 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1721 it->current.overlay_string_index = pos->overlay_string_index;
1722 relative_index = (it->current.overlay_string_index
1723 % OVERLAY_STRING_CHUNK_SIZE);
1724 it->string = it->overlay_strings[relative_index];
1725 xassert (STRINGP (it->string));
1726 it->current.string_pos = pos->string_pos;
1727 it->method = next_element_from_string;
1729 else if (CHARPOS (pos->string_pos) >= 0)
1731 /* Recorded position is not in an overlay string, but in another
1732 string. This can only be a string from a `display' property.
1733 IT should already be filled with that string. */
1734 it->current.string_pos = pos->string_pos;
1735 xassert (STRINGP (it->string));
1738 /* Restore position in display vector translations or control
1739 character translations. */
1740 if (pos->dpvec_index >= 0)
1742 /* This fills IT->dpvec. */
1743 get_next_display_element (it);
1744 xassert (it->dpvec && it->current.dpvec_index == 0);
1745 it->current.dpvec_index = pos->dpvec_index;
1748 CHECK_IT (it);
1752 /* Initialize IT for stepping through current_buffer in window W
1753 starting at ROW->start. */
1755 static void
1756 init_to_row_start (it, w, row)
1757 struct it *it;
1758 struct window *w;
1759 struct glyph_row *row;
1761 init_from_display_pos (it, w, &row->start);
1762 it->continuation_lines_width = row->continuation_lines_width;
1763 CHECK_IT (it);
1767 /* Initialize IT for stepping through current_buffer in window W
1768 starting in the line following ROW, i.e. starting at ROW->end. */
1770 static void
1771 init_to_row_end (it, w, row)
1772 struct it *it;
1773 struct window *w;
1774 struct glyph_row *row;
1776 init_from_display_pos (it, w, &row->end);
1778 if (row->continued_p)
1779 it->continuation_lines_width = (row->continuation_lines_width
1780 + row->pixel_width);
1781 CHECK_IT (it);
1787 /***********************************************************************
1788 Text properties
1789 ***********************************************************************/
1791 /* Called when IT reaches IT->stop_charpos. Handle text property and
1792 overlay changes. Set IT->stop_charpos to the next position where
1793 to stop. */
1795 static void
1796 handle_stop (it)
1797 struct it *it;
1799 enum prop_handled handled;
1800 int handle_overlay_change_p = 1;
1801 struct props *p;
1803 it->dpvec = NULL;
1804 it->current.dpvec_index = -1;
1808 handled = HANDLED_NORMALLY;
1810 /* Call text property handlers. */
1811 for (p = it_props; p->handler; ++p)
1813 handled = p->handler (it);
1815 if (handled == HANDLED_RECOMPUTE_PROPS)
1816 break;
1817 else if (handled == HANDLED_RETURN)
1818 return;
1819 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1820 handle_overlay_change_p = 0;
1823 if (handled != HANDLED_RECOMPUTE_PROPS)
1825 /* Don't check for overlay strings below when set to deliver
1826 characters from a display vector. */
1827 if (it->method == next_element_from_display_vector)
1828 handle_overlay_change_p = 0;
1830 /* Handle overlay changes. */
1831 if (handle_overlay_change_p)
1832 handled = handle_overlay_change (it);
1834 /* Determine where to stop next. */
1835 if (handled == HANDLED_NORMALLY)
1836 compute_stop_pos (it);
1839 while (handled == HANDLED_RECOMPUTE_PROPS);
1843 /* Compute IT->stop_charpos from text property and overlay change
1844 information for IT's current position. */
1846 static void
1847 compute_stop_pos (it)
1848 struct it *it;
1850 register INTERVAL iv, next_iv;
1851 Lisp_Object object, limit, position;
1853 /* If nowhere else, stop at the end. */
1854 it->stop_charpos = it->end_charpos;
1856 if (STRINGP (it->string))
1858 /* Strings are usually short, so don't limit the search for
1859 properties. */
1860 object = it->string;
1861 limit = Qnil;
1862 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1864 else
1866 int charpos;
1868 /* If next overlay change is in front of the current stop pos
1869 (which is IT->end_charpos), stop there. Note: value of
1870 next_overlay_change is point-max if no overlay change
1871 follows. */
1872 charpos = next_overlay_change (IT_CHARPOS (*it));
1873 if (charpos < it->stop_charpos)
1874 it->stop_charpos = charpos;
1876 /* If showing the region, we have to stop at the region
1877 start or end because the face might change there. */
1878 if (it->region_beg_charpos > 0)
1880 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1881 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1882 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1883 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1886 /* Set up variables for computing the stop position from text
1887 property changes. */
1888 XSETBUFFER (object, current_buffer);
1889 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1890 XSETFASTINT (position, IT_CHARPOS (*it));
1894 /* Get the interval containing IT's position. Value is a null
1895 interval if there isn't such an interval. */
1896 iv = validate_interval_range (object, &position, &position, 0);
1897 if (!NULL_INTERVAL_P (iv))
1899 Lisp_Object values_here[LAST_PROP_IDX];
1900 struct props *p;
1902 /* Get properties here. */
1903 for (p = it_props; p->handler; ++p)
1904 values_here[p->idx] = textget (iv->plist, *p->name);
1906 /* Look for an interval following iv that has different
1907 properties. */
1908 for (next_iv = next_interval (iv);
1909 (!NULL_INTERVAL_P (next_iv)
1910 && (NILP (limit)
1911 || XFASTINT (limit) > next_iv->position));
1912 next_iv = next_interval (next_iv))
1914 for (p = it_props; p->handler; ++p)
1916 Lisp_Object new_value;
1918 new_value = textget (next_iv->plist, *p->name);
1919 if (!EQ (values_here[p->idx], new_value))
1920 break;
1923 if (p->handler)
1924 break;
1927 if (!NULL_INTERVAL_P (next_iv))
1929 if (INTEGERP (limit)
1930 && next_iv->position >= XFASTINT (limit))
1931 /* No text property change up to limit. */
1932 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1933 else
1934 /* Text properties change in next_iv. */
1935 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1939 xassert (STRINGP (it->string)
1940 || (it->stop_charpos >= BEGV
1941 && it->stop_charpos >= IT_CHARPOS (*it)));
1945 /* Return the position of the next overlay change after POS in
1946 current_buffer. Value is point-max if no overlay change
1947 follows. This is like `next-overlay-change' but doesn't use
1948 xmalloc. */
1950 static int
1951 next_overlay_change (pos)
1952 int pos;
1954 int noverlays;
1955 int endpos;
1956 Lisp_Object *overlays;
1957 int len;
1958 int i;
1960 /* Get all overlays at the given position. */
1961 len = 10;
1962 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1963 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1964 if (noverlays > len)
1966 len = noverlays;
1967 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1968 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1971 /* If any of these overlays ends before endpos,
1972 use its ending point instead. */
1973 for (i = 0; i < noverlays; ++i)
1975 Lisp_Object oend;
1976 int oendpos;
1978 oend = OVERLAY_END (overlays[i]);
1979 oendpos = OVERLAY_POSITION (oend);
1980 endpos = min (endpos, oendpos);
1983 return endpos;
1988 /***********************************************************************
1989 Fontification
1990 ***********************************************************************/
1992 /* Handle changes in the `fontified' property of the current buffer by
1993 calling hook functions from Qfontification_functions to fontify
1994 regions of text. */
1996 static enum prop_handled
1997 handle_fontified_prop (it)
1998 struct it *it;
2000 Lisp_Object prop, pos;
2001 enum prop_handled handled = HANDLED_NORMALLY;
2003 /* Get the value of the `fontified' property at IT's current buffer
2004 position. (The `fontified' property doesn't have a special
2005 meaning in strings.) If the value is nil, call functions from
2006 Qfontification_functions. */
2007 if (!STRINGP (it->string)
2008 && it->s == NULL
2009 && !NILP (Vfontification_functions)
2010 && !NILP (Vrun_hooks)
2011 && (pos = make_number (IT_CHARPOS (*it)),
2012 prop = Fget_char_property (pos, Qfontified, Qnil),
2013 NILP (prop)))
2015 int count = BINDING_STACK_SIZE ();
2016 Lisp_Object val;
2018 val = Vfontification_functions;
2019 specbind (Qfontification_functions, Qnil);
2020 specbind (Qafter_change_functions, Qnil);
2022 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2023 safe_call1 (val, pos);
2024 else
2026 Lisp_Object globals, fn;
2027 struct gcpro gcpro1, gcpro2;
2029 globals = Qnil;
2030 GCPRO2 (val, globals);
2032 for (; CONSP (val); val = XCDR (val))
2034 fn = XCAR (val);
2036 if (EQ (fn, Qt))
2038 /* A value of t indicates this hook has a local
2039 binding; it means to run the global binding too.
2040 In a global value, t should not occur. If it
2041 does, we must ignore it to avoid an endless
2042 loop. */
2043 for (globals = Fdefault_value (Qfontification_functions);
2044 CONSP (globals);
2045 globals = XCDR (globals))
2047 fn = XCAR (globals);
2048 if (!EQ (fn, Qt))
2049 safe_call1 (fn, pos);
2052 else
2053 safe_call1 (fn, pos);
2056 UNGCPRO;
2059 unbind_to (count, Qnil);
2061 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2062 something. This avoids an endless loop if they failed to
2063 fontify the text for which reason ever. */
2064 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2065 handled = HANDLED_RECOMPUTE_PROPS;
2068 return handled;
2073 /***********************************************************************
2074 Faces
2075 ***********************************************************************/
2077 /* Set up iterator IT from face properties at its current position.
2078 Called from handle_stop. */
2080 static enum prop_handled
2081 handle_face_prop (it)
2082 struct it *it;
2084 int new_face_id, next_stop;
2086 if (!STRINGP (it->string))
2088 new_face_id
2089 = face_at_buffer_position (it->w,
2090 IT_CHARPOS (*it),
2091 it->region_beg_charpos,
2092 it->region_end_charpos,
2093 &next_stop,
2094 (IT_CHARPOS (*it)
2095 + TEXT_PROP_DISTANCE_LIMIT),
2098 /* Is this a start of a run of characters with box face?
2099 Caveat: this can be called for a freshly initialized
2100 iterator; face_id is -1 is this case. We know that the new
2101 face will not change until limit, i.e. if the new face has a
2102 box, all characters up to limit will have one. But, as
2103 usual, we don't know whether limit is really the end. */
2104 if (new_face_id != it->face_id)
2106 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2108 /* If new face has a box but old face has not, this is
2109 the start of a run of characters with box, i.e. it has
2110 a shadow on the left side. The value of face_id of the
2111 iterator will be -1 if this is the initial call that gets
2112 the face. In this case, we have to look in front of IT's
2113 position and see whether there is a face != new_face_id. */
2114 it->start_of_box_run_p
2115 = (new_face->box != FACE_NO_BOX
2116 && (it->face_id >= 0
2117 || IT_CHARPOS (*it) == BEG
2118 || new_face_id != face_before_it_pos (it)));
2119 it->face_box_p = new_face->box != FACE_NO_BOX;
2122 else
2124 new_face_id
2125 = face_at_string_position (it->w,
2126 it->string,
2127 IT_STRING_CHARPOS (*it),
2128 (it->current.overlay_string_index >= 0
2129 ? IT_CHARPOS (*it)
2130 : 0),
2131 it->region_beg_charpos,
2132 it->region_end_charpos,
2133 &next_stop,
2134 it->base_face_id);
2136 #if 0 /* This shouldn't be neccessary. Let's check it. */
2137 /* If IT is used to display a mode line we would really like to
2138 use the mode line face instead of the frame's default face. */
2139 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2140 && new_face_id == DEFAULT_FACE_ID)
2141 new_face_id = MODE_LINE_FACE_ID;
2142 #endif
2144 /* Is this a start of a run of characters with box? Caveat:
2145 this can be called for a freshly allocated iterator; face_id
2146 is -1 is this case. We know that the new face will not
2147 change until the next check pos, i.e. if the new face has a
2148 box, all characters up to that position will have a
2149 box. But, as usual, we don't know whether that position
2150 is really the end. */
2151 if (new_face_id != it->face_id)
2153 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2154 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2156 /* If new face has a box but old face hasn't, this is the
2157 start of a run of characters with box, i.e. it has a
2158 shadow on the left side. */
2159 it->start_of_box_run_p
2160 = new_face->box && (old_face == NULL || !old_face->box);
2161 it->face_box_p = new_face->box != FACE_NO_BOX;
2165 it->face_id = new_face_id;
2166 return HANDLED_NORMALLY;
2170 /* Compute the face one character before or after the current position
2171 of IT. BEFORE_P non-zero means get the face in front of IT's
2172 position. Value is the id of the face. */
2174 static int
2175 face_before_or_after_it_pos (it, before_p)
2176 struct it *it;
2177 int before_p;
2179 int face_id, limit;
2180 int next_check_charpos;
2181 struct text_pos pos;
2183 xassert (it->s == NULL);
2185 if (STRINGP (it->string))
2187 /* No face change past the end of the string (for the case
2188 we are padding with spaces). No face change before the
2189 string start. */
2190 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2191 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2192 return it->face_id;
2194 /* Set pos to the position before or after IT's current position. */
2195 if (before_p)
2196 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2197 else
2198 /* For composition, we must check the character after the
2199 composition. */
2200 pos = (it->what == IT_COMPOSITION
2201 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2202 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2204 /* Get the face for ASCII, or unibyte. */
2205 face_id
2206 = face_at_string_position (it->w,
2207 it->string,
2208 CHARPOS (pos),
2209 (it->current.overlay_string_index >= 0
2210 ? IT_CHARPOS (*it)
2211 : 0),
2212 it->region_beg_charpos,
2213 it->region_end_charpos,
2214 &next_check_charpos,
2215 it->base_face_id);
2217 /* Correct the face for charsets different from ASCII. Do it
2218 for the multibyte case only. The face returned above is
2219 suitable for unibyte text if IT->string is unibyte. */
2220 if (STRING_MULTIBYTE (it->string))
2222 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2223 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2224 int c, len;
2225 struct face *face = FACE_FROM_ID (it->f, face_id);
2227 c = string_char_and_length (p, rest, &len);
2228 face_id = FACE_FOR_CHAR (it->f, face, c);
2231 else
2233 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2234 || (IT_CHARPOS (*it) <= BEGV && before_p))
2235 return it->face_id;
2237 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2238 pos = it->current.pos;
2240 if (before_p)
2241 DEC_TEXT_POS (pos, it->multibyte_p);
2242 else
2244 if (it->what == IT_COMPOSITION)
2245 /* For composition, we must check the position after the
2246 composition. */
2247 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2248 else
2249 INC_TEXT_POS (pos, it->multibyte_p);
2251 /* Determine face for CHARSET_ASCII, or unibyte. */
2252 face_id = face_at_buffer_position (it->w,
2253 CHARPOS (pos),
2254 it->region_beg_charpos,
2255 it->region_end_charpos,
2256 &next_check_charpos,
2257 limit, 0);
2259 /* Correct the face for charsets different from ASCII. Do it
2260 for the multibyte case only. The face returned above is
2261 suitable for unibyte text if current_buffer is unibyte. */
2262 if (it->multibyte_p)
2264 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2265 struct face *face = FACE_FROM_ID (it->f, face_id);
2266 face_id = FACE_FOR_CHAR (it->f, face, c);
2270 return face_id;
2275 /***********************************************************************
2276 Invisible text
2277 ***********************************************************************/
2279 /* Set up iterator IT from invisible properties at its current
2280 position. Called from handle_stop. */
2282 static enum prop_handled
2283 handle_invisible_prop (it)
2284 struct it *it;
2286 enum prop_handled handled = HANDLED_NORMALLY;
2288 if (STRINGP (it->string))
2290 extern Lisp_Object Qinvisible;
2291 Lisp_Object prop, end_charpos, limit, charpos;
2293 /* Get the value of the invisible text property at the
2294 current position. Value will be nil if there is no such
2295 property. */
2296 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2297 prop = Fget_text_property (charpos, Qinvisible, it->string);
2299 if (!NILP (prop)
2300 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2302 handled = HANDLED_RECOMPUTE_PROPS;
2304 /* Get the position at which the next change of the
2305 invisible text property can be found in IT->string.
2306 Value will be nil if the property value is the same for
2307 all the rest of IT->string. */
2308 XSETINT (limit, XSTRING (it->string)->size);
2309 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2310 it->string, limit);
2312 /* Text at current position is invisible. The next
2313 change in the property is at position end_charpos.
2314 Move IT's current position to that position. */
2315 if (INTEGERP (end_charpos)
2316 && XFASTINT (end_charpos) < XFASTINT (limit))
2318 struct text_pos old;
2319 old = it->current.string_pos;
2320 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2321 compute_string_pos (&it->current.string_pos, old, it->string);
2323 else
2325 /* The rest of the string is invisible. If this is an
2326 overlay string, proceed with the next overlay string
2327 or whatever comes and return a character from there. */
2328 if (it->current.overlay_string_index >= 0)
2330 next_overlay_string (it);
2331 /* Don't check for overlay strings when we just
2332 finished processing them. */
2333 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2335 else
2337 struct Lisp_String *s = XSTRING (it->string);
2338 IT_STRING_CHARPOS (*it) = s->size;
2339 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2344 else
2346 int visible_p, newpos, next_stop;
2347 Lisp_Object pos, prop;
2349 /* First of all, is there invisible text at this position? */
2350 XSETFASTINT (pos, IT_CHARPOS (*it));
2351 prop = Fget_char_property (pos, Qinvisible, it->window);
2353 /* If we are on invisible text, skip over it. */
2354 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2355 && IT_CHARPOS (*it) < it->end_charpos)
2357 /* Record whether we have to display an ellipsis for the
2358 invisible text. */
2359 int display_ellipsis_p
2360 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2362 handled = HANDLED_RECOMPUTE_PROPS;
2364 /* Loop skipping over invisible text. The loop is left at
2365 ZV or with IT on the first char being visible again. */
2368 /* Try to skip some invisible text. Return value is the
2369 position reached which can be equal to IT's position
2370 if there is nothing invisible here. This skips both
2371 over invisible text properties and overlays with
2372 invisible property. */
2373 newpos = skip_invisible (IT_CHARPOS (*it),
2374 &next_stop, ZV, it->window);
2376 /* If we skipped nothing at all we weren't at invisible
2377 text in the first place. If everything to the end of
2378 the buffer was skipped, end the loop. */
2379 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2380 visible_p = 1;
2381 else
2383 /* We skipped some characters but not necessarily
2384 all there are. Check if we ended up on visible
2385 text. Fget_char_property returns the property of
2386 the char before the given position, i.e. if we
2387 get visible_p = 1, this means that the char at
2388 newpos is visible. */
2389 XSETFASTINT (pos, newpos);
2390 prop = Fget_char_property (pos, Qinvisible, it->window);
2391 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2394 /* If we ended up on invisible text, proceed to
2395 skip starting with next_stop. */
2396 if (!visible_p)
2397 IT_CHARPOS (*it) = next_stop;
2399 while (!visible_p);
2401 /* The position newpos is now either ZV or on visible text. */
2402 IT_CHARPOS (*it) = newpos;
2403 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2405 /* Maybe return `...' next for the end of the invisible text. */
2406 if (display_ellipsis_p)
2408 if (it->dp
2409 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2411 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2412 it->dpvec = v->contents;
2413 it->dpend = v->contents + v->size;
2415 else
2417 /* Default `...'. */
2418 it->dpvec = default_invis_vector;
2419 it->dpend = default_invis_vector + 3;
2422 /* The ellipsis display does not replace the display of
2423 the character at the new position. Indicate this by
2424 setting IT->dpvec_char_len to zero. */
2425 it->dpvec_char_len = 0;
2427 it->current.dpvec_index = 0;
2428 it->method = next_element_from_display_vector;
2433 return handled;
2438 /***********************************************************************
2439 'display' property
2440 ***********************************************************************/
2442 /* Set up iterator IT from `display' property at its current position.
2443 Called from handle_stop. */
2445 static enum prop_handled
2446 handle_display_prop (it)
2447 struct it *it;
2449 Lisp_Object prop, object;
2450 struct text_pos *position;
2451 int space_or_image_found_p;
2453 if (STRINGP (it->string))
2455 object = it->string;
2456 position = &it->current.string_pos;
2458 else
2460 object = Qnil;
2461 position = &it->current.pos;
2464 /* Reset those iterator values set from display property values. */
2465 it->font_height = Qnil;
2466 it->space_width = Qnil;
2467 it->voffset = 0;
2469 /* We don't support recursive `display' properties, i.e. string
2470 values that have a string `display' property, that have a string
2471 `display' property etc. */
2472 if (!it->string_from_display_prop_p)
2473 it->area = TEXT_AREA;
2475 prop = Fget_char_property (make_number (position->charpos),
2476 Qdisplay, object);
2477 if (NILP (prop))
2478 return HANDLED_NORMALLY;
2480 space_or_image_found_p = 0;
2481 if (CONSP (prop)
2482 && CONSP (XCAR (prop))
2483 && !EQ (Qmargin, XCAR (XCAR (prop))))
2485 /* A list of sub-properties. */
2486 while (CONSP (prop))
2488 if (handle_single_display_prop (it, XCAR (prop), object, position))
2489 space_or_image_found_p = 1;
2490 prop = XCDR (prop);
2493 else if (VECTORP (prop))
2495 int i;
2496 for (i = 0; i < XVECTOR (prop)->size; ++i)
2497 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2498 object, position))
2499 space_or_image_found_p = 1;
2501 else
2503 if (handle_single_display_prop (it, prop, object, position))
2504 space_or_image_found_p = 1;
2507 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2511 /* Value is the position of the end of the `display' property starting
2512 at START_POS in OBJECT. */
2514 static struct text_pos
2515 display_prop_end (it, object, start_pos)
2516 struct it *it;
2517 Lisp_Object object;
2518 struct text_pos start_pos;
2520 Lisp_Object end;
2521 struct text_pos end_pos;
2523 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2524 Qdisplay, object, Qnil);
2525 CHARPOS (end_pos) = XFASTINT (end);
2526 if (STRINGP (object))
2527 compute_string_pos (&end_pos, start_pos, it->string);
2528 else
2529 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2531 return end_pos;
2535 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2536 is the object in which the `display' property was found. *POSITION
2537 is the position at which it was found.
2539 If PROP is a `space' or `image' sub-property, set *POSITION to the
2540 end position of the `display' property.
2542 Value is non-zero if a `space' or `image' property value was found. */
2544 static int
2545 handle_single_display_prop (it, prop, object, position)
2546 struct it *it;
2547 Lisp_Object prop;
2548 Lisp_Object object;
2549 struct text_pos *position;
2551 Lisp_Object value;
2552 int space_or_image_found_p = 0;
2553 Lisp_Object form;
2555 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2556 evaluated. If the result is nil, VALUE is ignored. */
2557 form = Qt;
2558 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2560 prop = XCDR (prop);
2561 if (!CONSP (prop))
2562 return 0;
2563 form = XCAR (prop);
2564 prop = XCDR (prop);
2567 if (!NILP (form) && !EQ (form, Qt))
2569 struct gcpro gcpro1;
2570 struct text_pos end_pos, pt;
2572 GCPRO1 (form);
2573 end_pos = display_prop_end (it, object, *position);
2575 /* Temporarily set point to the end position, and then evaluate
2576 the form. This makes `(eolp)' work as FORM. */
2577 if (BUFFERP (object))
2579 CHARPOS (pt) = PT;
2580 BYTEPOS (pt) = PT_BYTE;
2581 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2584 form = safe_eval (form);
2586 if (BUFFERP (object))
2587 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2588 UNGCPRO;
2591 if (NILP (form))
2592 return 0;
2594 if (CONSP (prop)
2595 && EQ (XCAR (prop), Qheight)
2596 && CONSP (XCDR (prop)))
2598 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2599 return 0;
2601 /* `(height HEIGHT)'. */
2602 it->font_height = XCAR (XCDR (prop));
2603 if (!NILP (it->font_height))
2605 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2606 int new_height = -1;
2608 if (CONSP (it->font_height)
2609 && (EQ (XCAR (it->font_height), Qplus)
2610 || EQ (XCAR (it->font_height), Qminus))
2611 && CONSP (XCDR (it->font_height))
2612 && INTEGERP (XCAR (XCDR (it->font_height))))
2614 /* `(+ N)' or `(- N)' where N is an integer. */
2615 int steps = XINT (XCAR (XCDR (it->font_height)));
2616 if (EQ (XCAR (it->font_height), Qplus))
2617 steps = - steps;
2618 it->face_id = smaller_face (it->f, it->face_id, steps);
2620 else if (FUNCTIONP (it->font_height))
2622 /* Call function with current height as argument.
2623 Value is the new height. */
2624 Lisp_Object height;
2625 height = safe_call1 (it->font_height,
2626 face->lface[LFACE_HEIGHT_INDEX]);
2627 if (NUMBERP (height))
2628 new_height = XFLOATINT (height);
2630 else if (NUMBERP (it->font_height))
2632 /* Value is a multiple of the canonical char height. */
2633 struct face *face;
2635 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2636 new_height = (XFLOATINT (it->font_height)
2637 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2639 else
2641 /* Evaluate IT->font_height with `height' bound to the
2642 current specified height to get the new height. */
2643 Lisp_Object value;
2644 int count = BINDING_STACK_SIZE ();
2646 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2647 value = safe_eval (it->font_height);
2648 unbind_to (count, Qnil);
2650 if (NUMBERP (value))
2651 new_height = XFLOATINT (value);
2654 if (new_height > 0)
2655 it->face_id = face_with_height (it->f, it->face_id, new_height);
2658 else if (CONSP (prop)
2659 && EQ (XCAR (prop), Qspace_width)
2660 && CONSP (XCDR (prop)))
2662 /* `(space_width WIDTH)'. */
2663 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2664 return 0;
2666 value = XCAR (XCDR (prop));
2667 if (NUMBERP (value) && XFLOATINT (value) > 0)
2668 it->space_width = value;
2670 else if (CONSP (prop)
2671 && EQ (XCAR (prop), Qraise)
2672 && CONSP (XCDR (prop)))
2674 /* `(raise FACTOR)'. */
2675 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2676 return 0;
2678 #ifdef HAVE_WINDOW_SYSTEM
2679 value = XCAR (XCDR (prop));
2680 if (NUMBERP (value))
2682 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2683 it->voffset = - (XFLOATINT (value)
2684 * (FONT_HEIGHT (face->font)));
2686 #endif /* HAVE_WINDOW_SYSTEM */
2688 else if (!it->string_from_display_prop_p)
2690 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2691 VALUE) or `((margin nil) VALUE)' or VALUE. */
2692 Lisp_Object location, value;
2693 struct text_pos start_pos;
2694 int valid_p;
2696 /* Characters having this form of property are not displayed, so
2697 we have to find the end of the property. */
2698 start_pos = *position;
2699 *position = display_prop_end (it, object, start_pos);
2700 value = Qnil;
2702 /* Let's stop at the new position and assume that all
2703 text properties change there. */
2704 it->stop_charpos = position->charpos;
2706 location = Qunbound;
2707 if (CONSP (prop) && CONSP (XCAR (prop)))
2709 Lisp_Object tem;
2711 value = XCDR (prop);
2712 if (CONSP (value))
2713 value = XCAR (value);
2715 tem = XCAR (prop);
2716 if (EQ (XCAR (tem), Qmargin)
2717 && (tem = XCDR (tem),
2718 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2719 (NILP (tem)
2720 || EQ (tem, Qleft_margin)
2721 || EQ (tem, Qright_margin))))
2722 location = tem;
2725 if (EQ (location, Qunbound))
2727 location = Qnil;
2728 value = prop;
2731 #ifdef HAVE_WINDOW_SYSTEM
2732 if (FRAME_TERMCAP_P (it->f))
2733 valid_p = STRINGP (value);
2734 else
2735 valid_p = (STRINGP (value)
2736 || (CONSP (value) && EQ (XCAR (value), Qspace))
2737 || valid_image_p (value));
2738 #else /* not HAVE_WINDOW_SYSTEM */
2739 valid_p = STRINGP (value);
2740 #endif /* not HAVE_WINDOW_SYSTEM */
2742 if ((EQ (location, Qleft_margin)
2743 || EQ (location, Qright_margin)
2744 || NILP (location))
2745 && valid_p)
2747 space_or_image_found_p = 1;
2749 /* Save current settings of IT so that we can restore them
2750 when we are finished with the glyph property value. */
2751 push_it (it);
2753 if (NILP (location))
2754 it->area = TEXT_AREA;
2755 else if (EQ (location, Qleft_margin))
2756 it->area = LEFT_MARGIN_AREA;
2757 else
2758 it->area = RIGHT_MARGIN_AREA;
2760 if (STRINGP (value))
2762 it->string = value;
2763 it->multibyte_p = STRING_MULTIBYTE (it->string);
2764 it->current.overlay_string_index = -1;
2765 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2766 it->end_charpos = it->string_nchars
2767 = XSTRING (it->string)->size;
2768 it->method = next_element_from_string;
2769 it->stop_charpos = 0;
2770 it->string_from_display_prop_p = 1;
2772 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2774 it->method = next_element_from_stretch;
2775 it->object = value;
2776 it->current.pos = it->position = start_pos;
2778 #ifdef HAVE_WINDOW_SYSTEM
2779 else
2781 it->what = IT_IMAGE;
2782 it->image_id = lookup_image (it->f, value);
2783 it->position = start_pos;
2784 it->object = NILP (object) ? it->w->buffer : object;
2785 it->method = next_element_from_image;
2787 /* Say that we haven't consumed the characters with
2788 `display' property yet. The call to pop_it in
2789 set_iterator_to_next will clean this up. */
2790 *position = start_pos;
2792 #endif /* HAVE_WINDOW_SYSTEM */
2794 else
2795 /* Invalid property or property not supported. Restore
2796 the position to what it was before. */
2797 *position = start_pos;
2800 return space_or_image_found_p;
2804 /* Check if PROP is a display sub-property value whose text should be
2805 treated as intangible. */
2807 static int
2808 single_display_prop_intangible_p (prop)
2809 Lisp_Object prop;
2811 /* Skip over `when FORM'. */
2812 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2814 prop = XCDR (prop);
2815 if (!CONSP (prop))
2816 return 0;
2817 prop = XCDR (prop);
2820 if (!CONSP (prop))
2821 return 0;
2823 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2824 we don't need to treat text as intangible. */
2825 if (EQ (XCAR (prop), Qmargin))
2827 prop = XCDR (prop);
2828 if (!CONSP (prop))
2829 return 0;
2831 prop = XCDR (prop);
2832 if (!CONSP (prop)
2833 || EQ (XCAR (prop), Qleft_margin)
2834 || EQ (XCAR (prop), Qright_margin))
2835 return 0;
2838 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2842 /* Check if PROP is a display property value whose text should be
2843 treated as intangible. */
2846 display_prop_intangible_p (prop)
2847 Lisp_Object prop;
2849 if (CONSP (prop)
2850 && CONSP (XCAR (prop))
2851 && !EQ (Qmargin, XCAR (XCAR (prop))))
2853 /* A list of sub-properties. */
2854 while (CONSP (prop))
2856 if (single_display_prop_intangible_p (XCAR (prop)))
2857 return 1;
2858 prop = XCDR (prop);
2861 else if (VECTORP (prop))
2863 /* A vector of sub-properties. */
2864 int i;
2865 for (i = 0; i < XVECTOR (prop)->size; ++i)
2866 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2867 return 1;
2869 else
2870 return single_display_prop_intangible_p (prop);
2872 return 0;
2876 /***********************************************************************
2877 `composition' property
2878 ***********************************************************************/
2880 /* Set up iterator IT from `composition' property at its current
2881 position. Called from handle_stop. */
2883 static enum prop_handled
2884 handle_composition_prop (it)
2885 struct it *it;
2887 Lisp_Object prop, string;
2888 int pos, pos_byte, end;
2889 enum prop_handled handled = HANDLED_NORMALLY;
2891 if (STRINGP (it->string))
2893 pos = IT_STRING_CHARPOS (*it);
2894 pos_byte = IT_STRING_BYTEPOS (*it);
2895 string = it->string;
2897 else
2899 pos = IT_CHARPOS (*it);
2900 pos_byte = IT_BYTEPOS (*it);
2901 string = Qnil;
2904 /* If there's a valid composition and point is not inside of the
2905 composition (in the case that the composition is from the current
2906 buffer), draw a glyph composed from the composition components. */
2907 if (find_composition (pos, -1, &pos, &end, &prop, string)
2908 && COMPOSITION_VALID_P (pos, end, prop)
2909 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2911 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2913 if (id >= 0)
2915 it->method = next_element_from_composition;
2916 it->cmp_id = id;
2917 it->cmp_len = COMPOSITION_LENGTH (prop);
2918 /* For a terminal, draw only the first character of the
2919 components. */
2920 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2921 it->len = (STRINGP (it->string)
2922 ? string_char_to_byte (it->string, end)
2923 : CHAR_TO_BYTE (end)) - pos_byte;
2924 it->stop_charpos = end;
2925 handled = HANDLED_RETURN;
2929 return handled;
2934 /***********************************************************************
2935 Overlay strings
2936 ***********************************************************************/
2938 /* The following structure is used to record overlay strings for
2939 later sorting in load_overlay_strings. */
2941 struct overlay_entry
2943 Lisp_Object overlay;
2944 Lisp_Object string;
2945 int priority;
2946 int after_string_p;
2950 /* Set up iterator IT from overlay strings at its current position.
2951 Called from handle_stop. */
2953 static enum prop_handled
2954 handle_overlay_change (it)
2955 struct it *it;
2957 if (!STRINGP (it->string) && get_overlay_strings (it))
2958 return HANDLED_RECOMPUTE_PROPS;
2959 else
2960 return HANDLED_NORMALLY;
2964 /* Set up the next overlay string for delivery by IT, if there is an
2965 overlay string to deliver. Called by set_iterator_to_next when the
2966 end of the current overlay string is reached. If there are more
2967 overlay strings to display, IT->string and
2968 IT->current.overlay_string_index are set appropriately here.
2969 Otherwise IT->string is set to nil. */
2971 static void
2972 next_overlay_string (it)
2973 struct it *it;
2975 ++it->current.overlay_string_index;
2976 if (it->current.overlay_string_index == it->n_overlay_strings)
2978 /* No more overlay strings. Restore IT's settings to what
2979 they were before overlay strings were processed, and
2980 continue to deliver from current_buffer. */
2981 pop_it (it);
2982 xassert (it->stop_charpos >= BEGV
2983 && it->stop_charpos <= it->end_charpos);
2984 it->string = Qnil;
2985 it->current.overlay_string_index = -1;
2986 SET_TEXT_POS (it->current.string_pos, -1, -1);
2987 it->n_overlay_strings = 0;
2988 it->method = next_element_from_buffer;
2990 /* If we're at the end of the buffer, record that we have
2991 processed the overlay strings there already, so that
2992 next_element_from_buffer doesn't try it again. */
2993 if (IT_CHARPOS (*it) >= it->end_charpos)
2994 it->overlay_strings_at_end_processed_p = 1;
2996 else
2998 /* There are more overlay strings to process. If
2999 IT->current.overlay_string_index has advanced to a position
3000 where we must load IT->overlay_strings with more strings, do
3001 it. */
3002 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3004 if (it->current.overlay_string_index && i == 0)
3005 load_overlay_strings (it);
3007 /* Initialize IT to deliver display elements from the overlay
3008 string. */
3009 it->string = it->overlay_strings[i];
3010 it->multibyte_p = STRING_MULTIBYTE (it->string);
3011 SET_TEXT_POS (it->current.string_pos, 0, 0);
3012 it->method = next_element_from_string;
3013 it->stop_charpos = 0;
3016 CHECK_IT (it);
3020 /* Compare two overlay_entry structures E1 and E2. Used as a
3021 comparison function for qsort in load_overlay_strings. Overlay
3022 strings for the same position are sorted so that
3024 1. All after-strings come in front of before-strings, except
3025 when they come from the same overlay.
3027 2. Within after-strings, strings are sorted so that overlay strings
3028 from overlays with higher priorities come first.
3030 2. Within before-strings, strings are sorted so that overlay
3031 strings from overlays with higher priorities come last.
3033 Value is analogous to strcmp. */
3036 static int
3037 compare_overlay_entries (e1, e2)
3038 void *e1, *e2;
3040 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3041 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3042 int result;
3044 if (entry1->after_string_p != entry2->after_string_p)
3046 /* Let after-strings appear in front of before-strings if
3047 they come from different overlays. */
3048 if (EQ (entry1->overlay, entry2->overlay))
3049 result = entry1->after_string_p ? 1 : -1;
3050 else
3051 result = entry1->after_string_p ? -1 : 1;
3053 else if (entry1->after_string_p)
3054 /* After-strings sorted in order of decreasing priority. */
3055 result = entry2->priority - entry1->priority;
3056 else
3057 /* Before-strings sorted in order of increasing priority. */
3058 result = entry1->priority - entry2->priority;
3060 return result;
3064 /* Load the vector IT->overlay_strings with overlay strings from IT's
3065 current buffer position. Set IT->n_overlays to the total number of
3066 overlay strings found.
3068 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3069 a time. On entry into load_overlay_strings,
3070 IT->current.overlay_string_index gives the number of overlay
3071 strings that have already been loaded by previous calls to this
3072 function.
3074 IT->add_overlay_start contains an additional overlay start
3075 position to consider for taking overlay strings from, if non-zero.
3076 This position comes into play when the overlay has an `invisible'
3077 property, and both before and after-strings. When we've skipped to
3078 the end of the overlay, because of its `invisible' property, we
3079 nevertheless want its before-string to appear.
3080 IT->add_overlay_start will contain the overlay start position
3081 in this case.
3083 Overlay strings are sorted so that after-string strings come in
3084 front of before-string strings. Within before and after-strings,
3085 strings are sorted by overlay priority. See also function
3086 compare_overlay_entries. */
3088 static void
3089 load_overlay_strings (it)
3090 struct it *it;
3092 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3093 Lisp_Object ov, overlay, window, str, invisible;
3094 int start, end;
3095 int size = 20;
3096 int n = 0, i, j, invis_p;
3097 struct overlay_entry *entries
3098 = (struct overlay_entry *) alloca (size * sizeof *entries);
3099 int charpos = IT_CHARPOS (*it);
3101 /* Append the overlay string STRING of overlay OVERLAY to vector
3102 `entries' which has size `size' and currently contains `n'
3103 elements. AFTER_P non-zero means STRING is an after-string of
3104 OVERLAY. */
3105 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3106 do \
3108 Lisp_Object priority; \
3110 if (n == size) \
3112 int new_size = 2 * size; \
3113 struct overlay_entry *old = entries; \
3114 entries = \
3115 (struct overlay_entry *) alloca (new_size \
3116 * sizeof *entries); \
3117 bcopy (old, entries, size * sizeof *entries); \
3118 size = new_size; \
3121 entries[n].string = (STRING); \
3122 entries[n].overlay = (OVERLAY); \
3123 priority = Foverlay_get ((OVERLAY), Qpriority); \
3124 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3125 entries[n].after_string_p = (AFTER_P); \
3126 ++n; \
3128 while (0)
3130 /* Process overlay before the overlay center. */
3131 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3133 overlay = XCAR (ov);
3134 xassert (OVERLAYP (overlay));
3135 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3136 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3138 if (end < charpos)
3139 break;
3141 /* Skip this overlay if it doesn't start or end at IT's current
3142 position. */
3143 if (end != charpos && start != charpos)
3144 continue;
3146 /* Skip this overlay if it doesn't apply to IT->w. */
3147 window = Foverlay_get (overlay, Qwindow);
3148 if (WINDOWP (window) && XWINDOW (window) != it->w)
3149 continue;
3151 /* If the text ``under'' the overlay is invisible, both before-
3152 and after-strings from this overlay are visible; start and
3153 end position are indistinguishable. */
3154 invisible = Foverlay_get (overlay, Qinvisible);
3155 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3157 /* If overlay has a non-empty before-string, record it. */
3158 if ((start == charpos || (end == charpos && invis_p))
3159 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3160 && XSTRING (str)->size)
3161 RECORD_OVERLAY_STRING (overlay, str, 0);
3163 /* If overlay has a non-empty after-string, record it. */
3164 if ((end == charpos || (start == charpos && invis_p))
3165 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3166 && XSTRING (str)->size)
3167 RECORD_OVERLAY_STRING (overlay, str, 1);
3170 /* Process overlays after the overlay center. */
3171 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3173 overlay = XCAR (ov);
3174 xassert (OVERLAYP (overlay));
3175 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3176 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3178 if (start > charpos)
3179 break;
3181 /* Skip this overlay if it doesn't start or end at IT's current
3182 position. */
3183 if (end != charpos && start != charpos)
3184 continue;
3186 /* Skip this overlay if it doesn't apply to IT->w. */
3187 window = Foverlay_get (overlay, Qwindow);
3188 if (WINDOWP (window) && XWINDOW (window) != it->w)
3189 continue;
3191 /* If the text ``under'' the overlay is invisible, it has a zero
3192 dimension, and both before- and after-strings apply. */
3193 invisible = Foverlay_get (overlay, Qinvisible);
3194 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3196 /* If overlay has a non-empty before-string, record it. */
3197 if ((start == charpos || (end == charpos && invis_p))
3198 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3199 && XSTRING (str)->size)
3200 RECORD_OVERLAY_STRING (overlay, str, 0);
3202 /* If overlay has a non-empty after-string, record it. */
3203 if ((end == charpos || (start == charpos && invis_p))
3204 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3205 && XSTRING (str)->size)
3206 RECORD_OVERLAY_STRING (overlay, str, 1);
3209 #undef RECORD_OVERLAY_STRING
3211 /* Sort entries. */
3212 if (n > 1)
3213 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3215 /* Record the total number of strings to process. */
3216 it->n_overlay_strings = n;
3218 /* IT->current.overlay_string_index is the number of overlay strings
3219 that have already been consumed by IT. Copy some of the
3220 remaining overlay strings to IT->overlay_strings. */
3221 i = 0;
3222 j = it->current.overlay_string_index;
3223 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3224 it->overlay_strings[i++] = entries[j++].string;
3226 CHECK_IT (it);
3230 /* Get the first chunk of overlay strings at IT's current buffer
3231 position. Value is non-zero if at least one overlay string was
3232 found. */
3234 static int
3235 get_overlay_strings (it)
3236 struct it *it;
3238 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3239 process. This fills IT->overlay_strings with strings, and sets
3240 IT->n_overlay_strings to the total number of strings to process.
3241 IT->pos.overlay_string_index has to be set temporarily to zero
3242 because load_overlay_strings needs this; it must be set to -1
3243 when no overlay strings are found because a zero value would
3244 indicate a position in the first overlay string. */
3245 it->current.overlay_string_index = 0;
3246 load_overlay_strings (it);
3248 /* If we found overlay strings, set up IT to deliver display
3249 elements from the first one. Otherwise set up IT to deliver
3250 from current_buffer. */
3251 if (it->n_overlay_strings)
3253 /* Make sure we know settings in current_buffer, so that we can
3254 restore meaningful values when we're done with the overlay
3255 strings. */
3256 compute_stop_pos (it);
3257 xassert (it->face_id >= 0);
3259 /* Save IT's settings. They are restored after all overlay
3260 strings have been processed. */
3261 xassert (it->sp == 0);
3262 push_it (it);
3264 /* Set up IT to deliver display elements from the first overlay
3265 string. */
3266 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3267 it->stop_charpos = 0;
3268 it->string = it->overlay_strings[0];
3269 it->multibyte_p = STRING_MULTIBYTE (it->string);
3270 xassert (STRINGP (it->string));
3271 it->method = next_element_from_string;
3273 else
3275 it->string = Qnil;
3276 it->current.overlay_string_index = -1;
3277 it->method = next_element_from_buffer;
3280 CHECK_IT (it);
3282 /* Value is non-zero if we found at least one overlay string. */
3283 return STRINGP (it->string);
3288 /***********************************************************************
3289 Saving and restoring state
3290 ***********************************************************************/
3292 /* Save current settings of IT on IT->stack. Called, for example,
3293 before setting up IT for an overlay string, to be able to restore
3294 IT's settings to what they were after the overlay string has been
3295 processed. */
3297 static void
3298 push_it (it)
3299 struct it *it;
3301 struct iterator_stack_entry *p;
3303 xassert (it->sp < 2);
3304 p = it->stack + it->sp;
3306 p->stop_charpos = it->stop_charpos;
3307 xassert (it->face_id >= 0);
3308 p->face_id = it->face_id;
3309 p->string = it->string;
3310 p->pos = it->current;
3311 p->end_charpos = it->end_charpos;
3312 p->string_nchars = it->string_nchars;
3313 p->area = it->area;
3314 p->multibyte_p = it->multibyte_p;
3315 p->space_width = it->space_width;
3316 p->font_height = it->font_height;
3317 p->voffset = it->voffset;
3318 p->string_from_display_prop_p = it->string_from_display_prop_p;
3319 ++it->sp;
3323 /* Restore IT's settings from IT->stack. Called, for example, when no
3324 more overlay strings must be processed, and we return to delivering
3325 display elements from a buffer, or when the end of a string from a
3326 `display' property is reached and we return to delivering display
3327 elements from an overlay string, or from a buffer. */
3329 static void
3330 pop_it (it)
3331 struct it *it;
3333 struct iterator_stack_entry *p;
3335 xassert (it->sp > 0);
3336 --it->sp;
3337 p = it->stack + it->sp;
3338 it->stop_charpos = p->stop_charpos;
3339 it->face_id = p->face_id;
3340 it->string = p->string;
3341 it->current = p->pos;
3342 it->end_charpos = p->end_charpos;
3343 it->string_nchars = p->string_nchars;
3344 it->area = p->area;
3345 it->multibyte_p = p->multibyte_p;
3346 it->space_width = p->space_width;
3347 it->font_height = p->font_height;
3348 it->voffset = p->voffset;
3349 it->string_from_display_prop_p = p->string_from_display_prop_p;
3354 /***********************************************************************
3355 Moving over lines
3356 ***********************************************************************/
3358 /* Set IT's current position to the previous line start. */
3360 static void
3361 back_to_previous_line_start (it)
3362 struct it *it;
3364 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3365 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3369 /* Move IT to the next line start.
3371 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3372 we skipped over part of the text (as opposed to moving the iterator
3373 continuously over the text). Otherwise, don't change the value
3374 of *SKIPPED_P.
3376 Newlines may come from buffer text, overlay strings, or strings
3377 displayed via the `display' property. That's the reason we can't
3378 simply use find_next_newline_no_quit. */
3380 static int
3381 forward_to_next_line_start (it, skipped_p)
3382 struct it *it;
3383 int *skipped_p;
3385 int old_selective, newline_found_p, n;
3386 const int MAX_NEWLINE_DISTANCE = 500;
3388 /* Don't handle selective display in the following. It's (a)
3389 unnecessary and (b) leads to an infinite recursion because
3390 next_element_from_ellipsis indirectly calls this function. */
3391 old_selective = it->selective;
3392 it->selective = 0;
3394 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3395 from buffer text. */
3396 n = newline_found_p = 0;
3397 while (n < MAX_NEWLINE_DISTANCE
3398 && get_next_display_element (it)
3399 && !newline_found_p)
3401 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3402 set_iterator_to_next (it, 0);
3403 if (!STRINGP (it->string))
3404 ++n;
3407 /* If we didn't find a newline near enough, see if we can use a
3408 short-cut. */
3409 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3411 int start = IT_CHARPOS (*it);
3412 int limit = find_next_newline_no_quit (start, 1);
3413 Lisp_Object pos;
3415 xassert (!STRINGP (it->string));
3417 /* If there isn't any `display' property in sight, and no
3418 overlays, we can just use the position of the newline in
3419 buffer text. */
3420 if (it->stop_charpos >= limit
3421 || ((pos = Fnext_single_property_change (make_number (start),
3422 Qdisplay,
3423 Qnil, make_number (limit)),
3424 NILP (pos))
3425 && next_overlay_change (start) == ZV))
3427 IT_CHARPOS (*it) = limit;
3428 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3429 *skipped_p = newline_found_p = 1;
3431 else
3433 while (get_next_display_element (it)
3434 && !newline_found_p)
3436 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3437 set_iterator_to_next (it, 0);
3442 it->selective = old_selective;
3443 return newline_found_p;
3447 /* Set IT's current position to the previous visible line start. Skip
3448 invisible text that is so either due to text properties or due to
3449 selective display. Caution: this does not change IT->current_x and
3450 IT->hpos. */
3452 static void
3453 back_to_previous_visible_line_start (it)
3454 struct it *it;
3456 int visible_p = 0;
3458 /* Go back one newline if not on BEGV already. */
3459 if (IT_CHARPOS (*it) > BEGV)
3460 back_to_previous_line_start (it);
3462 /* Move over lines that are invisible because of selective display
3463 or text properties. */
3464 while (IT_CHARPOS (*it) > BEGV
3465 && !visible_p)
3467 visible_p = 1;
3469 /* If selective > 0, then lines indented more than that values
3470 are invisible. */
3471 if (it->selective > 0
3472 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3473 it->selective))
3474 visible_p = 0;
3475 else
3477 Lisp_Object prop;
3479 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3480 Qinvisible, it->window);
3481 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3482 visible_p = 0;
3485 /* Back one more newline if the current one is invisible. */
3486 if (!visible_p)
3487 back_to_previous_line_start (it);
3490 xassert (IT_CHARPOS (*it) >= BEGV);
3491 xassert (IT_CHARPOS (*it) == BEGV
3492 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3493 CHECK_IT (it);
3497 /* Reseat iterator IT at the previous visible line start. Skip
3498 invisible text that is so either due to text properties or due to
3499 selective display. At the end, update IT's overlay information,
3500 face information etc. */
3502 static void
3503 reseat_at_previous_visible_line_start (it)
3504 struct it *it;
3506 back_to_previous_visible_line_start (it);
3507 reseat (it, it->current.pos, 1);
3508 CHECK_IT (it);
3512 /* Reseat iterator IT on the next visible line start in the current
3513 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3514 preceding the line start. Skip over invisible text that is so
3515 because of selective display. Compute faces, overlays etc at the
3516 new position. Note that this function does not skip over text that
3517 is invisible because of text properties. */
3519 static void
3520 reseat_at_next_visible_line_start (it, on_newline_p)
3521 struct it *it;
3522 int on_newline_p;
3524 int newline_found_p, skipped_p = 0;
3526 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3528 /* Skip over lines that are invisible because they are indented
3529 more than the value of IT->selective. */
3530 if (it->selective > 0)
3531 while (IT_CHARPOS (*it) < ZV
3532 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3533 it->selective))
3534 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3536 /* Position on the newline if that's what's requested. */
3537 if (on_newline_p && newline_found_p)
3539 if (STRINGP (it->string))
3541 if (IT_STRING_CHARPOS (*it) > 0)
3543 --IT_STRING_CHARPOS (*it);
3544 --IT_STRING_BYTEPOS (*it);
3547 else if (IT_CHARPOS (*it) > BEGV)
3549 --IT_CHARPOS (*it);
3550 --IT_BYTEPOS (*it);
3551 reseat (it, it->current.pos, 0);
3554 else if (skipped_p)
3555 reseat (it, it->current.pos, 0);
3557 CHECK_IT (it);
3562 /***********************************************************************
3563 Changing an iterator's position
3564 ***********************************************************************/
3566 /* Change IT's current position to POS in current_buffer. If FORCE_P
3567 is non-zero, always check for text properties at the new position.
3568 Otherwise, text properties are only looked up if POS >=
3569 IT->check_charpos of a property. */
3571 static void
3572 reseat (it, pos, force_p)
3573 struct it *it;
3574 struct text_pos pos;
3575 int force_p;
3577 int original_pos = IT_CHARPOS (*it);
3579 reseat_1 (it, pos, 0);
3581 /* Determine where to check text properties. Avoid doing it
3582 where possible because text property lookup is very expensive. */
3583 if (force_p
3584 || CHARPOS (pos) > it->stop_charpos
3585 || CHARPOS (pos) < original_pos)
3586 handle_stop (it);
3588 CHECK_IT (it);
3592 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3593 IT->stop_pos to POS, also. */
3595 static void
3596 reseat_1 (it, pos, set_stop_p)
3597 struct it *it;
3598 struct text_pos pos;
3599 int set_stop_p;
3601 /* Don't call this function when scanning a C string. */
3602 xassert (it->s == NULL);
3604 /* POS must be a reasonable value. */
3605 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3607 it->current.pos = it->position = pos;
3608 XSETBUFFER (it->object, current_buffer);
3609 it->dpvec = NULL;
3610 it->current.dpvec_index = -1;
3611 it->current.overlay_string_index = -1;
3612 IT_STRING_CHARPOS (*it) = -1;
3613 IT_STRING_BYTEPOS (*it) = -1;
3614 it->string = Qnil;
3615 it->method = next_element_from_buffer;
3616 it->sp = 0;
3618 if (set_stop_p)
3619 it->stop_charpos = CHARPOS (pos);
3623 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3624 If S is non-null, it is a C string to iterate over. Otherwise,
3625 STRING gives a Lisp string to iterate over.
3627 If PRECISION > 0, don't return more then PRECISION number of
3628 characters from the string.
3630 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3631 characters have been returned. FIELD_WIDTH < 0 means an infinite
3632 field width.
3634 MULTIBYTE = 0 means disable processing of multibyte characters,
3635 MULTIBYTE > 0 means enable it,
3636 MULTIBYTE < 0 means use IT->multibyte_p.
3638 IT must be initialized via a prior call to init_iterator before
3639 calling this function. */
3641 static void
3642 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3643 struct it *it;
3644 unsigned char *s;
3645 Lisp_Object string;
3646 int charpos;
3647 int precision, field_width, multibyte;
3649 /* No region in strings. */
3650 it->region_beg_charpos = it->region_end_charpos = -1;
3652 /* No text property checks performed by default, but see below. */
3653 it->stop_charpos = -1;
3655 /* Set iterator position and end position. */
3656 bzero (&it->current, sizeof it->current);
3657 it->current.overlay_string_index = -1;
3658 it->current.dpvec_index = -1;
3659 xassert (charpos >= 0);
3661 /* Use the setting of MULTIBYTE if specified. */
3662 if (multibyte >= 0)
3663 it->multibyte_p = multibyte > 0;
3665 if (s == NULL)
3667 xassert (STRINGP (string));
3668 it->string = string;
3669 it->s = NULL;
3670 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3671 it->method = next_element_from_string;
3672 it->current.string_pos = string_pos (charpos, string);
3674 else
3676 it->s = s;
3677 it->string = Qnil;
3679 /* Note that we use IT->current.pos, not it->current.string_pos,
3680 for displaying C strings. */
3681 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3682 if (it->multibyte_p)
3684 it->current.pos = c_string_pos (charpos, s, 1);
3685 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3687 else
3689 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3690 it->end_charpos = it->string_nchars = strlen (s);
3693 it->method = next_element_from_c_string;
3696 /* PRECISION > 0 means don't return more than PRECISION characters
3697 from the string. */
3698 if (precision > 0 && it->end_charpos - charpos > precision)
3699 it->end_charpos = it->string_nchars = charpos + precision;
3701 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3702 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3703 FIELD_WIDTH < 0 means infinite field width. This is useful for
3704 padding with `-' at the end of a mode line. */
3705 if (field_width < 0)
3706 field_width = INFINITY;
3707 if (field_width > it->end_charpos - charpos)
3708 it->end_charpos = charpos + field_width;
3710 /* Use the standard display table for displaying strings. */
3711 if (DISP_TABLE_P (Vstandard_display_table))
3712 it->dp = XCHAR_TABLE (Vstandard_display_table);
3714 it->stop_charpos = charpos;
3715 CHECK_IT (it);
3720 /***********************************************************************
3721 Iteration
3722 ***********************************************************************/
3724 /* Load IT's display element fields with information about the next
3725 display element from the current position of IT. Value is zero if
3726 end of buffer (or C string) is reached. */
3729 get_next_display_element (it)
3730 struct it *it;
3732 /* Non-zero means that we found an display element. Zero means that
3733 we hit the end of what we iterate over. Performance note: the
3734 function pointer `method' used here turns out to be faster than
3735 using a sequence of if-statements. */
3736 int success_p = (*it->method) (it);
3738 if (it->what == IT_CHARACTER)
3740 /* Map via display table or translate control characters.
3741 IT->c, IT->len etc. have been set to the next character by
3742 the function call above. If we have a display table, and it
3743 contains an entry for IT->c, translate it. Don't do this if
3744 IT->c itself comes from a display table, otherwise we could
3745 end up in an infinite recursion. (An alternative could be to
3746 count the recursion depth of this function and signal an
3747 error when a certain maximum depth is reached.) Is it worth
3748 it? */
3749 if (success_p && it->dpvec == NULL)
3751 Lisp_Object dv;
3753 if (it->dp
3754 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3755 VECTORP (dv)))
3757 struct Lisp_Vector *v = XVECTOR (dv);
3759 /* Return the first character from the display table
3760 entry, if not empty. If empty, don't display the
3761 current character. */
3762 if (v->size)
3764 it->dpvec_char_len = it->len;
3765 it->dpvec = v->contents;
3766 it->dpend = v->contents + v->size;
3767 it->current.dpvec_index = 0;
3768 it->method = next_element_from_display_vector;
3771 success_p = get_next_display_element (it);
3774 /* Translate control characters into `\003' or `^C' form.
3775 Control characters coming from a display table entry are
3776 currently not translated because we use IT->dpvec to hold
3777 the translation. This could easily be changed but I
3778 don't believe that it is worth doing.
3780 Non-printable multibyte characters are also translated
3781 octal form. */
3782 else if ((it->c < ' '
3783 && (it->area != TEXT_AREA
3784 || (it->c != '\n' && it->c != '\t')))
3785 || (it->c >= 127
3786 && it->len == 1)
3787 || !CHAR_PRINTABLE_P (it->c))
3789 /* IT->c is a control character which must be displayed
3790 either as '\003' or as `^C' where the '\\' and '^'
3791 can be defined in the display table. Fill
3792 IT->ctl_chars with glyphs for what we have to
3793 display. Then, set IT->dpvec to these glyphs. */
3794 GLYPH g;
3796 if (it->c < 128 && it->ctl_arrow_p)
3798 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3799 if (it->dp
3800 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3801 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3802 g = XINT (DISP_CTRL_GLYPH (it->dp));
3803 else
3804 g = FAST_MAKE_GLYPH ('^', 0);
3805 XSETINT (it->ctl_chars[0], g);
3807 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3808 XSETINT (it->ctl_chars[1], g);
3810 /* Set up IT->dpvec and return first character from it. */
3811 it->dpvec_char_len = it->len;
3812 it->dpvec = it->ctl_chars;
3813 it->dpend = it->dpvec + 2;
3814 it->current.dpvec_index = 0;
3815 it->method = next_element_from_display_vector;
3816 get_next_display_element (it);
3818 else
3820 unsigned char str[MAX_MULTIBYTE_LENGTH];
3821 int len;
3822 int i;
3823 GLYPH escape_glyph;
3825 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3826 if (it->dp
3827 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3828 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3829 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3830 else
3831 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3833 if (SINGLE_BYTE_CHAR_P (it->c))
3834 str[0] = it->c, len = 1;
3835 else
3836 len = CHAR_STRING (it->c, str);
3838 for (i = 0; i < len; i++)
3840 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3841 /* Insert three more glyphs into IT->ctl_chars for
3842 the octal display of the character. */
3843 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3844 XSETINT (it->ctl_chars[i * 4 + 1], g);
3845 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3846 XSETINT (it->ctl_chars[i * 4 + 2], g);
3847 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3848 XSETINT (it->ctl_chars[i * 4 + 3], g);
3851 /* Set up IT->dpvec and return the first character
3852 from it. */
3853 it->dpvec_char_len = it->len;
3854 it->dpvec = it->ctl_chars;
3855 it->dpend = it->dpvec + len * 4;
3856 it->current.dpvec_index = 0;
3857 it->method = next_element_from_display_vector;
3858 get_next_display_element (it);
3863 /* Adjust face id for a multibyte character. There are no
3864 multibyte character in unibyte text. */
3865 if (it->multibyte_p
3866 && success_p
3867 && FRAME_WINDOW_P (it->f))
3869 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3870 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3874 /* Is this character the last one of a run of characters with
3875 box? If yes, set IT->end_of_box_run_p to 1. */
3876 if (it->face_box_p
3877 && it->s == NULL)
3879 int face_id;
3880 struct face *face;
3882 it->end_of_box_run_p
3883 = ((face_id = face_after_it_pos (it),
3884 face_id != it->face_id)
3885 && (face = FACE_FROM_ID (it->f, face_id),
3886 face->box == FACE_NO_BOX));
3889 /* Value is 0 if end of buffer or string reached. */
3890 return success_p;
3894 /* Move IT to the next display element.
3896 RESEAT_P non-zero means if called on a newline in buffer text,
3897 skip to the next visible line start.
3899 Functions get_next_display_element and set_iterator_to_next are
3900 separate because I find this arrangement easier to handle than a
3901 get_next_display_element function that also increments IT's
3902 position. The way it is we can first look at an iterator's current
3903 display element, decide whether it fits on a line, and if it does,
3904 increment the iterator position. The other way around we probably
3905 would either need a flag indicating whether the iterator has to be
3906 incremented the next time, or we would have to implement a
3907 decrement position function which would not be easy to write. */
3909 void
3910 set_iterator_to_next (it, reseat_p)
3911 struct it *it;
3912 int reseat_p;
3914 /* Reset flags indicating start and end of a sequence of characters
3915 with box. Reset them at the start of this function because
3916 moving the iterator to a new position might set them. */
3917 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3919 if (it->method == next_element_from_buffer)
3921 /* The current display element of IT is a character from
3922 current_buffer. Advance in the buffer, and maybe skip over
3923 invisible lines that are so because of selective display. */
3924 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3925 reseat_at_next_visible_line_start (it, 0);
3926 else
3928 xassert (it->len != 0);
3929 IT_BYTEPOS (*it) += it->len;
3930 IT_CHARPOS (*it) += 1;
3931 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3934 else if (it->method == next_element_from_composition)
3936 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3937 if (STRINGP (it->string))
3939 IT_STRING_BYTEPOS (*it) += it->len;
3940 IT_STRING_CHARPOS (*it) += it->cmp_len;
3941 it->method = next_element_from_string;
3942 goto consider_string_end;
3944 else
3946 IT_BYTEPOS (*it) += it->len;
3947 IT_CHARPOS (*it) += it->cmp_len;
3948 it->method = next_element_from_buffer;
3951 else if (it->method == next_element_from_c_string)
3953 /* Current display element of IT is from a C string. */
3954 IT_BYTEPOS (*it) += it->len;
3955 IT_CHARPOS (*it) += 1;
3957 else if (it->method == next_element_from_display_vector)
3959 /* Current display element of IT is from a display table entry.
3960 Advance in the display table definition. Reset it to null if
3961 end reached, and continue with characters from buffers/
3962 strings. */
3963 ++it->current.dpvec_index;
3965 /* Restore face of the iterator to what they were before the
3966 display vector entry (these entries may contain faces). */
3967 it->face_id = it->saved_face_id;
3969 if (it->dpvec + it->current.dpvec_index == it->dpend)
3971 if (it->s)
3972 it->method = next_element_from_c_string;
3973 else if (STRINGP (it->string))
3974 it->method = next_element_from_string;
3975 else
3976 it->method = next_element_from_buffer;
3978 it->dpvec = NULL;
3979 it->current.dpvec_index = -1;
3981 /* Skip over characters which were displayed via IT->dpvec. */
3982 if (it->dpvec_char_len < 0)
3983 reseat_at_next_visible_line_start (it, 1);
3984 else if (it->dpvec_char_len > 0)
3986 it->len = it->dpvec_char_len;
3987 set_iterator_to_next (it, reseat_p);
3991 else if (it->method == next_element_from_string)
3993 /* Current display element is a character from a Lisp string. */
3994 xassert (it->s == NULL && STRINGP (it->string));
3995 IT_STRING_BYTEPOS (*it) += it->len;
3996 IT_STRING_CHARPOS (*it) += 1;
3998 consider_string_end:
4000 if (it->current.overlay_string_index >= 0)
4002 /* IT->string is an overlay string. Advance to the
4003 next, if there is one. */
4004 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4005 next_overlay_string (it);
4007 else
4009 /* IT->string is not an overlay string. If we reached
4010 its end, and there is something on IT->stack, proceed
4011 with what is on the stack. This can be either another
4012 string, this time an overlay string, or a buffer. */
4013 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4014 && it->sp > 0)
4016 pop_it (it);
4017 if (!STRINGP (it->string))
4018 it->method = next_element_from_buffer;
4022 else if (it->method == next_element_from_image
4023 || it->method == next_element_from_stretch)
4025 /* The position etc with which we have to proceed are on
4026 the stack. The position may be at the end of a string,
4027 if the `display' property takes up the whole string. */
4028 pop_it (it);
4029 it->image_id = 0;
4030 if (STRINGP (it->string))
4032 it->method = next_element_from_string;
4033 goto consider_string_end;
4035 else
4036 it->method = next_element_from_buffer;
4038 else
4039 /* There are no other methods defined, so this should be a bug. */
4040 abort ();
4042 xassert (it->method != next_element_from_string
4043 || (STRINGP (it->string)
4044 && IT_STRING_CHARPOS (*it) >= 0));
4048 /* Load IT's display element fields with information about the next
4049 display element which comes from a display table entry or from the
4050 result of translating a control character to one of the forms `^C'
4051 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4053 static int
4054 next_element_from_display_vector (it)
4055 struct it *it;
4057 /* Precondition. */
4058 xassert (it->dpvec && it->current.dpvec_index >= 0);
4060 /* Remember the current face id in case glyphs specify faces.
4061 IT's face is restored in set_iterator_to_next. */
4062 it->saved_face_id = it->face_id;
4064 if (INTEGERP (*it->dpvec)
4065 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4067 int lface_id;
4068 GLYPH g;
4070 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4071 it->c = FAST_GLYPH_CHAR (g);
4072 it->len = CHAR_BYTES (it->c);
4074 /* The entry may contain a face id to use. Such a face id is
4075 the id of a Lisp face, not a realized face. A face id of
4076 zero means no face is specified. */
4077 lface_id = FAST_GLYPH_FACE (g);
4078 if (lface_id)
4080 /* The function returns -1 if lface_id is invalid. */
4081 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4082 if (face_id >= 0)
4083 it->face_id = face_id;
4086 else
4087 /* Display table entry is invalid. Return a space. */
4088 it->c = ' ', it->len = 1;
4090 /* Don't change position and object of the iterator here. They are
4091 still the values of the character that had this display table
4092 entry or was translated, and that's what we want. */
4093 it->what = IT_CHARACTER;
4094 return 1;
4098 /* Load IT with the next display element from Lisp string IT->string.
4099 IT->current.string_pos is the current position within the string.
4100 If IT->current.overlay_string_index >= 0, the Lisp string is an
4101 overlay string. */
4103 static int
4104 next_element_from_string (it)
4105 struct it *it;
4107 struct text_pos position;
4109 xassert (STRINGP (it->string));
4110 xassert (IT_STRING_CHARPOS (*it) >= 0);
4111 position = it->current.string_pos;
4113 /* Time to check for invisible text? */
4114 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4115 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4117 handle_stop (it);
4119 /* Since a handler may have changed IT->method, we must
4120 recurse here. */
4121 return get_next_display_element (it);
4124 if (it->current.overlay_string_index >= 0)
4126 /* Get the next character from an overlay string. In overlay
4127 strings, There is no field width or padding with spaces to
4128 do. */
4129 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4131 it->what = IT_EOB;
4132 return 0;
4134 else if (STRING_MULTIBYTE (it->string))
4136 int remaining = (STRING_BYTES (XSTRING (it->string))
4137 - IT_STRING_BYTEPOS (*it));
4138 unsigned char *s = (XSTRING (it->string)->data
4139 + IT_STRING_BYTEPOS (*it));
4140 it->c = string_char_and_length (s, remaining, &it->len);
4142 else
4144 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4145 it->len = 1;
4148 else
4150 /* Get the next character from a Lisp string that is not an
4151 overlay string. Such strings come from the mode line, for
4152 example. We may have to pad with spaces, or truncate the
4153 string. See also next_element_from_c_string. */
4154 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4156 it->what = IT_EOB;
4157 return 0;
4159 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4161 /* Pad with spaces. */
4162 it->c = ' ', it->len = 1;
4163 CHARPOS (position) = BYTEPOS (position) = -1;
4165 else if (STRING_MULTIBYTE (it->string))
4167 int maxlen = (STRING_BYTES (XSTRING (it->string))
4168 - IT_STRING_BYTEPOS (*it));
4169 unsigned char *s = (XSTRING (it->string)->data
4170 + IT_STRING_BYTEPOS (*it));
4171 it->c = string_char_and_length (s, maxlen, &it->len);
4173 else
4175 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4176 it->len = 1;
4180 /* Record what we have and where it came from. Note that we store a
4181 buffer position in IT->position although it could arguably be a
4182 string position. */
4183 it->what = IT_CHARACTER;
4184 it->object = it->string;
4185 it->position = position;
4186 return 1;
4190 /* Load IT with next display element from C string IT->s.
4191 IT->string_nchars is the maximum number of characters to return
4192 from the string. IT->end_charpos may be greater than
4193 IT->string_nchars when this function is called, in which case we
4194 may have to return padding spaces. Value is zero if end of string
4195 reached, including padding spaces. */
4197 static int
4198 next_element_from_c_string (it)
4199 struct it *it;
4201 int success_p = 1;
4203 xassert (it->s);
4204 it->what = IT_CHARACTER;
4205 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4206 it->object = Qnil;
4208 /* IT's position can be greater IT->string_nchars in case a field
4209 width or precision has been specified when the iterator was
4210 initialized. */
4211 if (IT_CHARPOS (*it) >= it->end_charpos)
4213 /* End of the game. */
4214 it->what = IT_EOB;
4215 success_p = 0;
4217 else if (IT_CHARPOS (*it) >= it->string_nchars)
4219 /* Pad with spaces. */
4220 it->c = ' ', it->len = 1;
4221 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4223 else if (it->multibyte_p)
4225 /* Implementation note: The calls to strlen apparently aren't a
4226 performance problem because there is no noticeable performance
4227 difference between Emacs running in unibyte or multibyte mode. */
4228 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4229 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4230 maxlen, &it->len);
4232 else
4233 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4235 return success_p;
4239 /* Set up IT to return characters from an ellipsis, if appropriate.
4240 The definition of the ellipsis glyphs may come from a display table
4241 entry. This function Fills IT with the first glyph from the
4242 ellipsis if an ellipsis is to be displayed. */
4244 static int
4245 next_element_from_ellipsis (it)
4246 struct it *it;
4248 if (it->selective_display_ellipsis_p)
4250 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4252 /* Use the display table definition for `...'. Invalid glyphs
4253 will be handled by the method returning elements from dpvec. */
4254 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4255 it->dpvec_char_len = it->len;
4256 it->dpvec = v->contents;
4257 it->dpend = v->contents + v->size;
4258 it->current.dpvec_index = 0;
4259 it->method = next_element_from_display_vector;
4261 else
4263 /* Use default `...' which is stored in default_invis_vector. */
4264 it->dpvec_char_len = it->len;
4265 it->dpvec = default_invis_vector;
4266 it->dpend = default_invis_vector + 3;
4267 it->current.dpvec_index = 0;
4268 it->method = next_element_from_display_vector;
4271 else
4273 it->method = next_element_from_buffer;
4274 reseat_at_next_visible_line_start (it, 1);
4277 return get_next_display_element (it);
4281 /* Deliver an image display element. The iterator IT is already
4282 filled with image information (done in handle_display_prop). Value
4283 is always 1. */
4286 static int
4287 next_element_from_image (it)
4288 struct it *it;
4290 it->what = IT_IMAGE;
4291 return 1;
4295 /* Fill iterator IT with next display element from a stretch glyph
4296 property. IT->object is the value of the text property. Value is
4297 always 1. */
4299 static int
4300 next_element_from_stretch (it)
4301 struct it *it;
4303 it->what = IT_STRETCH;
4304 return 1;
4308 /* Load IT with the next display element from current_buffer. Value
4309 is zero if end of buffer reached. IT->stop_charpos is the next
4310 position at which to stop and check for text properties or buffer
4311 end. */
4313 static int
4314 next_element_from_buffer (it)
4315 struct it *it;
4317 int success_p = 1;
4319 /* Check this assumption, otherwise, we would never enter the
4320 if-statement, below. */
4321 xassert (IT_CHARPOS (*it) >= BEGV
4322 && IT_CHARPOS (*it) <= it->stop_charpos);
4324 if (IT_CHARPOS (*it) >= it->stop_charpos)
4326 if (IT_CHARPOS (*it) >= it->end_charpos)
4328 int overlay_strings_follow_p;
4330 /* End of the game, except when overlay strings follow that
4331 haven't been returned yet. */
4332 if (it->overlay_strings_at_end_processed_p)
4333 overlay_strings_follow_p = 0;
4334 else
4336 it->overlay_strings_at_end_processed_p = 1;
4337 overlay_strings_follow_p = get_overlay_strings (it);
4340 if (overlay_strings_follow_p)
4341 success_p = get_next_display_element (it);
4342 else
4344 it->what = IT_EOB;
4345 it->position = it->current.pos;
4346 success_p = 0;
4349 else
4351 handle_stop (it);
4352 return get_next_display_element (it);
4355 else
4357 /* No face changes, overlays etc. in sight, so just return a
4358 character from current_buffer. */
4359 unsigned char *p;
4361 /* Maybe run the redisplay end trigger hook. Performance note:
4362 This doesn't seem to cost measurable time. */
4363 if (it->redisplay_end_trigger_charpos
4364 && it->glyph_row
4365 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4366 run_redisplay_end_trigger_hook (it);
4368 /* Get the next character, maybe multibyte. */
4369 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4370 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4372 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4373 - IT_BYTEPOS (*it));
4374 it->c = string_char_and_length (p, maxlen, &it->len);
4376 else
4377 it->c = *p, it->len = 1;
4379 /* Record what we have and where it came from. */
4380 it->what = IT_CHARACTER;;
4381 it->object = it->w->buffer;
4382 it->position = it->current.pos;
4384 /* Normally we return the character found above, except when we
4385 really want to return an ellipsis for selective display. */
4386 if (it->selective)
4388 if (it->c == '\n')
4390 /* A value of selective > 0 means hide lines indented more
4391 than that number of columns. */
4392 if (it->selective > 0
4393 && IT_CHARPOS (*it) + 1 < ZV
4394 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4395 IT_BYTEPOS (*it) + 1,
4396 it->selective))
4398 success_p = next_element_from_ellipsis (it);
4399 it->dpvec_char_len = -1;
4402 else if (it->c == '\r' && it->selective == -1)
4404 /* A value of selective == -1 means that everything from the
4405 CR to the end of the line is invisible, with maybe an
4406 ellipsis displayed for it. */
4407 success_p = next_element_from_ellipsis (it);
4408 it->dpvec_char_len = -1;
4413 /* Value is zero if end of buffer reached. */
4414 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4415 return success_p;
4419 /* Run the redisplay end trigger hook for IT. */
4421 static void
4422 run_redisplay_end_trigger_hook (it)
4423 struct it *it;
4425 Lisp_Object args[3];
4427 /* IT->glyph_row should be non-null, i.e. we should be actually
4428 displaying something, or otherwise we should not run the hook. */
4429 xassert (it->glyph_row);
4431 /* Set up hook arguments. */
4432 args[0] = Qredisplay_end_trigger_functions;
4433 args[1] = it->window;
4434 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4435 it->redisplay_end_trigger_charpos = 0;
4437 /* Since we are *trying* to run these functions, don't try to run
4438 them again, even if they get an error. */
4439 it->w->redisplay_end_trigger = Qnil;
4440 Frun_hook_with_args (3, args);
4442 /* Notice if it changed the face of the character we are on. */
4443 handle_face_prop (it);
4447 /* Deliver a composition display element. The iterator IT is already
4448 filled with composition information (done in
4449 handle_composition_prop). Value is always 1. */
4451 static int
4452 next_element_from_composition (it)
4453 struct it *it;
4455 it->what = IT_COMPOSITION;
4456 it->position = (STRINGP (it->string)
4457 ? it->current.string_pos
4458 : it->current.pos);
4459 return 1;
4464 /***********************************************************************
4465 Moving an iterator without producing glyphs
4466 ***********************************************************************/
4468 /* Move iterator IT to a specified buffer or X position within one
4469 line on the display without producing glyphs.
4471 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4472 whichever is reached first.
4474 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4476 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4477 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4478 TO_X includes the amount by which a window is horizontally
4479 scrolled.
4481 Value is
4483 MOVE_POS_MATCH_OR_ZV
4484 - when TO_POS or ZV was reached.
4486 MOVE_X_REACHED
4487 -when TO_X was reached before TO_POS or ZV were reached.
4489 MOVE_LINE_CONTINUED
4490 - when we reached the end of the display area and the line must
4491 be continued.
4493 MOVE_LINE_TRUNCATED
4494 - when we reached the end of the display area and the line is
4495 truncated.
4497 MOVE_NEWLINE_OR_CR
4498 - when we stopped at a line end, i.e. a newline or a CR and selective
4499 display is on. */
4501 static enum move_it_result
4502 move_it_in_display_line_to (it, to_charpos, to_x, op)
4503 struct it *it;
4504 int to_charpos, to_x, op;
4506 enum move_it_result result = MOVE_UNDEFINED;
4507 struct glyph_row *saved_glyph_row;
4509 /* Don't produce glyphs in produce_glyphs. */
4510 saved_glyph_row = it->glyph_row;
4511 it->glyph_row = NULL;
4513 while (1)
4515 int x, i, ascent = 0, descent = 0;
4517 /* Stop when ZV or TO_CHARPOS reached. */
4518 if (!get_next_display_element (it)
4519 || ((op & MOVE_TO_POS) != 0
4520 && BUFFERP (it->object)
4521 && IT_CHARPOS (*it) >= to_charpos))
4523 result = MOVE_POS_MATCH_OR_ZV;
4524 break;
4527 /* The call to produce_glyphs will get the metrics of the
4528 display element IT is loaded with. We record in x the
4529 x-position before this display element in case it does not
4530 fit on the line. */
4531 x = it->current_x;
4533 /* Remember the line height so far in case the next element doesn't
4534 fit on the line. */
4535 if (!it->truncate_lines_p)
4537 ascent = it->max_ascent;
4538 descent = it->max_descent;
4541 PRODUCE_GLYPHS (it);
4543 if (it->area != TEXT_AREA)
4545 set_iterator_to_next (it, 1);
4546 continue;
4549 /* The number of glyphs we get back in IT->nglyphs will normally
4550 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4551 character on a terminal frame, or (iii) a line end. For the
4552 second case, IT->nglyphs - 1 padding glyphs will be present
4553 (on X frames, there is only one glyph produced for a
4554 composite character.
4556 The behavior implemented below means, for continuation lines,
4557 that as many spaces of a TAB as fit on the current line are
4558 displayed there. For terminal frames, as many glyphs of a
4559 multi-glyph character are displayed in the current line, too.
4560 This is what the old redisplay code did, and we keep it that
4561 way. Under X, the whole shape of a complex character must
4562 fit on the line or it will be completely displayed in the
4563 next line.
4565 Note that both for tabs and padding glyphs, all glyphs have
4566 the same width. */
4567 if (it->nglyphs)
4569 /* More than one glyph or glyph doesn't fit on line. All
4570 glyphs have the same width. */
4571 int single_glyph_width = it->pixel_width / it->nglyphs;
4572 int new_x;
4574 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4576 new_x = x + single_glyph_width;
4578 /* We want to leave anything reaching TO_X to the caller. */
4579 if ((op & MOVE_TO_X) && new_x > to_x)
4581 it->current_x = x;
4582 result = MOVE_X_REACHED;
4583 break;
4585 else if (/* Lines are continued. */
4586 !it->truncate_lines_p
4587 && (/* And glyph doesn't fit on the line. */
4588 new_x > it->last_visible_x
4589 /* Or it fits exactly and we're on a window
4590 system frame. */
4591 || (new_x == it->last_visible_x
4592 && FRAME_WINDOW_P (it->f))))
4594 if (/* IT->hpos == 0 means the very first glyph
4595 doesn't fit on the line, e.g. a wide image. */
4596 it->hpos == 0
4597 || (new_x == it->last_visible_x
4598 && FRAME_WINDOW_P (it->f)))
4600 ++it->hpos;
4601 it->current_x = new_x;
4602 if (i == it->nglyphs - 1)
4603 set_iterator_to_next (it, 1);
4605 else
4607 it->current_x = x;
4608 it->max_ascent = ascent;
4609 it->max_descent = descent;
4612 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4613 IT_CHARPOS (*it)));
4614 result = MOVE_LINE_CONTINUED;
4615 break;
4617 else if (new_x > it->first_visible_x)
4619 /* Glyph is visible. Increment number of glyphs that
4620 would be displayed. */
4621 ++it->hpos;
4623 else
4625 /* Glyph is completely off the left margin of the display
4626 area. Nothing to do. */
4630 if (result != MOVE_UNDEFINED)
4631 break;
4633 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4635 /* Stop when TO_X specified and reached. This check is
4636 necessary here because of lines consisting of a line end,
4637 only. The line end will not produce any glyphs and we
4638 would never get MOVE_X_REACHED. */
4639 xassert (it->nglyphs == 0);
4640 result = MOVE_X_REACHED;
4641 break;
4644 /* Is this a line end? If yes, we're done. */
4645 if (ITERATOR_AT_END_OF_LINE_P (it))
4647 result = MOVE_NEWLINE_OR_CR;
4648 break;
4651 /* The current display element has been consumed. Advance
4652 to the next. */
4653 set_iterator_to_next (it, 1);
4655 /* Stop if lines are truncated and IT's current x-position is
4656 past the right edge of the window now. */
4657 if (it->truncate_lines_p
4658 && it->current_x >= it->last_visible_x)
4660 result = MOVE_LINE_TRUNCATED;
4661 break;
4665 /* Restore the iterator settings altered at the beginning of this
4666 function. */
4667 it->glyph_row = saved_glyph_row;
4668 return result;
4672 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4673 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4674 the description of enum move_operation_enum.
4676 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4677 screen line, this function will set IT to the next position >
4678 TO_CHARPOS. */
4680 void
4681 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4682 struct it *it;
4683 int to_charpos, to_x, to_y, to_vpos;
4684 int op;
4686 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4687 int line_height;
4688 int reached = 0;
4690 for (;;)
4692 if (op & MOVE_TO_VPOS)
4694 /* If no TO_CHARPOS and no TO_X specified, stop at the
4695 start of the line TO_VPOS. */
4696 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4698 if (it->vpos == to_vpos)
4700 reached = 1;
4701 break;
4703 else
4704 skip = move_it_in_display_line_to (it, -1, -1, 0);
4706 else
4708 /* TO_VPOS >= 0 means stop at TO_X in the line at
4709 TO_VPOS, or at TO_POS, whichever comes first. */
4710 if (it->vpos == to_vpos)
4712 reached = 2;
4713 break;
4716 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4718 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4720 reached = 3;
4721 break;
4723 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4725 /* We have reached TO_X but not in the line we want. */
4726 skip = move_it_in_display_line_to (it, to_charpos,
4727 -1, MOVE_TO_POS);
4728 if (skip == MOVE_POS_MATCH_OR_ZV)
4730 reached = 4;
4731 break;
4736 else if (op & MOVE_TO_Y)
4738 struct it it_backup;
4740 /* TO_Y specified means stop at TO_X in the line containing
4741 TO_Y---or at TO_CHARPOS if this is reached first. The
4742 problem is that we can't really tell whether the line
4743 contains TO_Y before we have completely scanned it, and
4744 this may skip past TO_X. What we do is to first scan to
4745 TO_X.
4747 If TO_X is not specified, use a TO_X of zero. The reason
4748 is to make the outcome of this function more predictable.
4749 If we didn't use TO_X == 0, we would stop at the end of
4750 the line which is probably not what a caller would expect
4751 to happen. */
4752 skip = move_it_in_display_line_to (it, to_charpos,
4753 ((op & MOVE_TO_X)
4754 ? to_x : 0),
4755 (MOVE_TO_X
4756 | (op & MOVE_TO_POS)));
4758 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4759 if (skip == MOVE_POS_MATCH_OR_ZV)
4761 reached = 5;
4762 break;
4765 /* If TO_X was reached, we would like to know whether TO_Y
4766 is in the line. This can only be said if we know the
4767 total line height which requires us to scan the rest of
4768 the line. */
4769 if (skip == MOVE_X_REACHED)
4771 it_backup = *it;
4772 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4773 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4774 op & MOVE_TO_POS);
4775 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4778 /* Now, decide whether TO_Y is in this line. */
4779 line_height = it->max_ascent + it->max_descent;
4780 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4782 if (to_y >= it->current_y
4783 && to_y < it->current_y + line_height)
4785 if (skip == MOVE_X_REACHED)
4786 /* If TO_Y is in this line and TO_X was reached above,
4787 we scanned too far. We have to restore IT's settings
4788 to the ones before skipping. */
4789 *it = it_backup;
4790 reached = 6;
4792 else if (skip == MOVE_X_REACHED)
4794 skip = skip2;
4795 if (skip == MOVE_POS_MATCH_OR_ZV)
4796 reached = 7;
4799 if (reached)
4800 break;
4802 else
4803 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4805 switch (skip)
4807 case MOVE_POS_MATCH_OR_ZV:
4808 reached = 8;
4809 goto out;
4811 case MOVE_NEWLINE_OR_CR:
4812 set_iterator_to_next (it, 1);
4813 it->continuation_lines_width = 0;
4814 break;
4816 case MOVE_LINE_TRUNCATED:
4817 it->continuation_lines_width = 0;
4818 reseat_at_next_visible_line_start (it, 0);
4819 if ((op & MOVE_TO_POS) != 0
4820 && IT_CHARPOS (*it) > to_charpos)
4822 reached = 9;
4823 goto out;
4825 break;
4827 case MOVE_LINE_CONTINUED:
4828 it->continuation_lines_width += it->current_x;
4829 break;
4831 default:
4832 abort ();
4835 /* Reset/increment for the next run. */
4836 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4837 it->current_x = it->hpos = 0;
4838 it->current_y += it->max_ascent + it->max_descent;
4839 ++it->vpos;
4840 last_height = it->max_ascent + it->max_descent;
4841 last_max_ascent = it->max_ascent;
4842 it->max_ascent = it->max_descent = 0;
4845 out:
4847 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4851 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4853 If DY > 0, move IT backward at least that many pixels. DY = 0
4854 means move IT backward to the preceding line start or BEGV. This
4855 function may move over more than DY pixels if IT->current_y - DY
4856 ends up in the middle of a line; in this case IT->current_y will be
4857 set to the top of the line moved to. */
4859 void
4860 move_it_vertically_backward (it, dy)
4861 struct it *it;
4862 int dy;
4864 int nlines, h, line_height;
4865 struct it it2;
4866 int start_pos = IT_CHARPOS (*it);
4868 xassert (dy >= 0);
4870 /* Estimate how many newlines we must move back. */
4871 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4873 /* Set the iterator's position that many lines back. */
4874 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4875 back_to_previous_visible_line_start (it);
4877 /* Reseat the iterator here. When moving backward, we don't want
4878 reseat to skip forward over invisible text, set up the iterator
4879 to deliver from overlay strings at the new position etc. So,
4880 use reseat_1 here. */
4881 reseat_1 (it, it->current.pos, 1);
4883 /* We are now surely at a line start. */
4884 it->current_x = it->hpos = 0;
4886 /* Move forward and see what y-distance we moved. First move to the
4887 start of the next line so that we get its height. We need this
4888 height to be able to tell whether we reached the specified
4889 y-distance. */
4890 it2 = *it;
4891 it2.max_ascent = it2.max_descent = 0;
4892 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4893 MOVE_TO_POS | MOVE_TO_VPOS);
4894 xassert (IT_CHARPOS (*it) >= BEGV);
4895 line_height = it2.max_ascent + it2.max_descent;
4897 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4898 xassert (IT_CHARPOS (*it) >= BEGV);
4899 h = it2.current_y - it->current_y;
4900 nlines = it2.vpos - it->vpos;
4902 /* Correct IT's y and vpos position. */
4903 it->vpos -= nlines;
4904 it->current_y -= h;
4906 if (dy == 0)
4908 /* DY == 0 means move to the start of the screen line. The
4909 value of nlines is > 0 if continuation lines were involved. */
4910 if (nlines > 0)
4911 move_it_by_lines (it, nlines, 1);
4912 xassert (IT_CHARPOS (*it) <= start_pos);
4914 else if (nlines)
4916 /* The y-position we try to reach. Note that h has been
4917 subtracted in front of the if-statement. */
4918 int target_y = it->current_y + h - dy;
4920 /* If we did not reach target_y, try to move further backward if
4921 we can. If we moved too far backward, try to move forward. */
4922 if (target_y < it->current_y
4923 && IT_CHARPOS (*it) > BEGV)
4925 move_it_vertically (it, target_y - it->current_y);
4926 xassert (IT_CHARPOS (*it) >= BEGV);
4928 else if (target_y >= it->current_y + line_height
4929 && IT_CHARPOS (*it) < ZV)
4931 move_it_vertically (it, target_y - (it->current_y + line_height));
4932 xassert (IT_CHARPOS (*it) >= BEGV);
4938 /* Move IT by a specified amount of pixel lines DY. DY negative means
4939 move backwards. DY = 0 means move to start of screen line. At the
4940 end, IT will be on the start of a screen line. */
4942 void
4943 move_it_vertically (it, dy)
4944 struct it *it;
4945 int dy;
4947 if (dy <= 0)
4948 move_it_vertically_backward (it, -dy);
4949 else if (dy > 0)
4951 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4952 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4953 MOVE_TO_POS | MOVE_TO_Y);
4954 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4956 /* If buffer ends in ZV without a newline, move to the start of
4957 the line to satisfy the post-condition. */
4958 if (IT_CHARPOS (*it) == ZV
4959 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4960 move_it_by_lines (it, 0, 0);
4965 /* Return non-zero if some text between buffer positions START_CHARPOS
4966 and END_CHARPOS is invisible. IT->window is the window for text
4967 property lookup. */
4969 static int
4970 invisible_text_between_p (it, start_charpos, end_charpos)
4971 struct it *it;
4972 int start_charpos, end_charpos;
4974 Lisp_Object prop, limit;
4975 int invisible_found_p;
4977 xassert (it != NULL && start_charpos <= end_charpos);
4979 /* Is text at START invisible? */
4980 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4981 it->window);
4982 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4983 invisible_found_p = 1;
4984 else
4986 limit = Fnext_single_char_property_change (make_number (start_charpos),
4987 Qinvisible, Qnil,
4988 make_number (end_charpos));
4989 invisible_found_p = XFASTINT (limit) < end_charpos;
4992 return invisible_found_p;
4996 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4997 negative means move up. DVPOS == 0 means move to the start of the
4998 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4999 NEED_Y_P is zero, IT->current_y will be left unchanged.
5001 Further optimization ideas: If we would know that IT->f doesn't use
5002 a face with proportional font, we could be faster for
5003 truncate-lines nil. */
5005 void
5006 move_it_by_lines (it, dvpos, need_y_p)
5007 struct it *it;
5008 int dvpos, need_y_p;
5010 struct position pos;
5012 if (!FRAME_WINDOW_P (it->f))
5014 struct text_pos textpos;
5016 /* We can use vmotion on frames without proportional fonts. */
5017 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5018 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5019 reseat (it, textpos, 1);
5020 it->vpos += pos.vpos;
5021 it->current_y += pos.vpos;
5023 else if (dvpos == 0)
5025 /* DVPOS == 0 means move to the start of the screen line. */
5026 move_it_vertically_backward (it, 0);
5027 xassert (it->current_x == 0 && it->hpos == 0);
5029 else if (dvpos > 0)
5031 /* If there are no continuation lines, and if there is no
5032 selective display, try the simple method of moving forward
5033 DVPOS newlines, then see where we are. */
5034 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5036 int shortage = 0, charpos;
5038 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
5039 charpos = IT_CHARPOS (*it) + 1;
5040 else
5041 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5042 &shortage, 0);
5044 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5046 struct text_pos pos;
5047 CHARPOS (pos) = charpos;
5048 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5049 reseat (it, pos, 1);
5050 it->vpos += dvpos - shortage;
5051 it->hpos = it->current_x = 0;
5052 return;
5056 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5058 else
5060 struct it it2;
5061 int start_charpos, i;
5063 /* If there are no continuation lines, and if there is no
5064 selective display, try the simple method of moving backward
5065 -DVPOS newlines. */
5066 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5068 int shortage;
5069 int charpos = IT_CHARPOS (*it);
5070 int bytepos = IT_BYTEPOS (*it);
5072 /* If in the middle of a line, go to its start. */
5073 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5075 charpos = find_next_newline_no_quit (charpos, -1);
5076 bytepos = CHAR_TO_BYTE (charpos);
5079 if (charpos == BEGV)
5081 struct text_pos pos;
5082 CHARPOS (pos) = charpos;
5083 BYTEPOS (pos) = bytepos;
5084 reseat (it, pos, 1);
5085 it->hpos = it->current_x = 0;
5086 return;
5088 else
5090 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5091 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5093 struct text_pos pos;
5094 CHARPOS (pos) = charpos;
5095 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5096 reseat (it, pos, 1);
5097 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5098 it->hpos = it->current_x = 0;
5099 return;
5104 /* Go back -DVPOS visible lines and reseat the iterator there. */
5105 start_charpos = IT_CHARPOS (*it);
5106 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5107 back_to_previous_visible_line_start (it);
5108 reseat (it, it->current.pos, 1);
5109 it->current_x = it->hpos = 0;
5111 /* Above call may have moved too far if continuation lines
5112 are involved. Scan forward and see if it did. */
5113 it2 = *it;
5114 it2.vpos = it2.current_y = 0;
5115 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5116 it->vpos -= it2.vpos;
5117 it->current_y -= it2.current_y;
5118 it->current_x = it->hpos = 0;
5120 /* If we moved too far, move IT some lines forward. */
5121 if (it2.vpos > -dvpos)
5123 int delta = it2.vpos + dvpos;
5124 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5131 /***********************************************************************
5132 Messages
5133 ***********************************************************************/
5136 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5137 to *Messages*. */
5139 void
5140 add_to_log (format, arg1, arg2)
5141 char *format;
5142 Lisp_Object arg1, arg2;
5144 Lisp_Object args[3];
5145 Lisp_Object msg, fmt;
5146 char *buffer;
5147 int len;
5148 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5150 fmt = msg = Qnil;
5151 GCPRO4 (fmt, msg, arg1, arg2);
5153 args[0] = fmt = build_string (format);
5154 args[1] = arg1;
5155 args[2] = arg2;
5156 msg = Fformat (3, args);
5158 len = STRING_BYTES (XSTRING (msg)) + 1;
5159 buffer = (char *) alloca (len);
5160 strcpy (buffer, XSTRING (msg)->data);
5162 message_dolog (buffer, len - 1, 1, 0);
5163 UNGCPRO;
5167 /* Output a newline in the *Messages* buffer if "needs" one. */
5169 void
5170 message_log_maybe_newline ()
5172 if (message_log_need_newline)
5173 message_dolog ("", 0, 1, 0);
5177 /* Add a string M of length LEN to the message log, optionally
5178 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5179 nonzero, means interpret the contents of M as multibyte. This
5180 function calls low-level routines in order to bypass text property
5181 hooks, etc. which might not be safe to run. */
5183 void
5184 message_dolog (m, len, nlflag, multibyte)
5185 char *m;
5186 int len, nlflag, multibyte;
5188 if (!NILP (Vmessage_log_max))
5190 struct buffer *oldbuf;
5191 Lisp_Object oldpoint, oldbegv, oldzv;
5192 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5193 int point_at_end = 0;
5194 int zv_at_end = 0;
5195 Lisp_Object old_deactivate_mark, tem;
5196 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5198 old_deactivate_mark = Vdeactivate_mark;
5199 oldbuf = current_buffer;
5200 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5201 current_buffer->undo_list = Qt;
5203 oldpoint = Fpoint_marker ();
5204 oldbegv = Fpoint_min_marker ();
5205 oldzv = Fpoint_max_marker ();
5206 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5208 if (PT == Z)
5209 point_at_end = 1;
5210 if (ZV == Z)
5211 zv_at_end = 1;
5213 BEGV = BEG;
5214 BEGV_BYTE = BEG_BYTE;
5215 ZV = Z;
5216 ZV_BYTE = Z_BYTE;
5217 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5219 /* Insert the string--maybe converting multibyte to single byte
5220 or vice versa, so that all the text fits the buffer. */
5221 if (multibyte
5222 && NILP (current_buffer->enable_multibyte_characters))
5224 int i, c, nbytes;
5225 unsigned char work[1];
5227 /* Convert a multibyte string to single-byte
5228 for the *Message* buffer. */
5229 for (i = 0; i < len; i += nbytes)
5231 c = string_char_and_length (m + i, len - i, &nbytes);
5232 work[0] = (SINGLE_BYTE_CHAR_P (c)
5234 : multibyte_char_to_unibyte (c, Qnil));
5235 insert_1_both (work, 1, 1, 1, 0, 0);
5238 else if (! multibyte
5239 && ! NILP (current_buffer->enable_multibyte_characters))
5241 int i, c, nbytes;
5242 unsigned char *msg = (unsigned char *) m;
5243 unsigned char str[MAX_MULTIBYTE_LENGTH];
5244 /* Convert a single-byte string to multibyte
5245 for the *Message* buffer. */
5246 for (i = 0; i < len; i++)
5248 c = unibyte_char_to_multibyte (msg[i]);
5249 nbytes = CHAR_STRING (c, str);
5250 insert_1_both (str, 1, nbytes, 1, 0, 0);
5253 else if (len)
5254 insert_1 (m, len, 1, 0, 0);
5256 if (nlflag)
5258 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5259 insert_1 ("\n", 1, 1, 0, 0);
5261 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5262 this_bol = PT;
5263 this_bol_byte = PT_BYTE;
5265 if (this_bol > BEG)
5267 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5268 prev_bol = PT;
5269 prev_bol_byte = PT_BYTE;
5271 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5272 this_bol, this_bol_byte);
5273 if (dup)
5275 del_range_both (prev_bol, prev_bol_byte,
5276 this_bol, this_bol_byte, 0);
5277 if (dup > 1)
5279 char dupstr[40];
5280 int duplen;
5282 /* If you change this format, don't forget to also
5283 change message_log_check_duplicate. */
5284 sprintf (dupstr, " [%d times]", dup);
5285 duplen = strlen (dupstr);
5286 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5287 insert_1 (dupstr, duplen, 1, 0, 1);
5292 if (NATNUMP (Vmessage_log_max))
5294 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5295 -XFASTINT (Vmessage_log_max) - 1, 0);
5296 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5299 BEGV = XMARKER (oldbegv)->charpos;
5300 BEGV_BYTE = marker_byte_position (oldbegv);
5302 if (zv_at_end)
5304 ZV = Z;
5305 ZV_BYTE = Z_BYTE;
5307 else
5309 ZV = XMARKER (oldzv)->charpos;
5310 ZV_BYTE = marker_byte_position (oldzv);
5313 if (point_at_end)
5314 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5315 else
5316 /* We can't do Fgoto_char (oldpoint) because it will run some
5317 Lisp code. */
5318 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5319 XMARKER (oldpoint)->bytepos);
5321 UNGCPRO;
5322 free_marker (oldpoint);
5323 free_marker (oldbegv);
5324 free_marker (oldzv);
5326 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5327 set_buffer_internal (oldbuf);
5328 if (NILP (tem))
5329 windows_or_buffers_changed = old_windows_or_buffers_changed;
5330 message_log_need_newline = !nlflag;
5331 Vdeactivate_mark = old_deactivate_mark;
5336 /* We are at the end of the buffer after just having inserted a newline.
5337 (Note: We depend on the fact we won't be crossing the gap.)
5338 Check to see if the most recent message looks a lot like the previous one.
5339 Return 0 if different, 1 if the new one should just replace it, or a
5340 value N > 1 if we should also append " [N times]". */
5342 static int
5343 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5344 int prev_bol, this_bol;
5345 int prev_bol_byte, this_bol_byte;
5347 int i;
5348 int len = Z_BYTE - 1 - this_bol_byte;
5349 int seen_dots = 0;
5350 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5351 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5353 for (i = 0; i < len; i++)
5355 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5356 seen_dots = 1;
5357 if (p1[i] != p2[i])
5358 return seen_dots;
5360 p1 += len;
5361 if (*p1 == '\n')
5362 return 2;
5363 if (*p1++ == ' ' && *p1++ == '[')
5365 int n = 0;
5366 while (*p1 >= '0' && *p1 <= '9')
5367 n = n * 10 + *p1++ - '0';
5368 if (strncmp (p1, " times]\n", 8) == 0)
5369 return n+1;
5371 return 0;
5375 /* Display an echo area message M with a specified length of LEN
5376 chars. The string may include null characters. If M is 0, clear
5377 out any existing message, and let the mini-buffer text show through.
5379 The buffer M must continue to exist until after the echo area gets
5380 cleared or some other message gets displayed there. This means do
5381 not pass text that is stored in a Lisp string; do not pass text in
5382 a buffer that was alloca'd. */
5384 void
5385 message2 (m, len, multibyte)
5386 char *m;
5387 int len;
5388 int multibyte;
5390 /* First flush out any partial line written with print. */
5391 message_log_maybe_newline ();
5392 if (m)
5393 message_dolog (m, len, 1, multibyte);
5394 message2_nolog (m, len, multibyte);
5398 /* The non-logging counterpart of message2. */
5400 void
5401 message2_nolog (m, len, multibyte)
5402 char *m;
5403 int len;
5405 struct frame *sf = SELECTED_FRAME ();
5406 message_enable_multibyte = multibyte;
5408 if (noninteractive)
5410 if (noninteractive_need_newline)
5411 putc ('\n', stderr);
5412 noninteractive_need_newline = 0;
5413 if (m)
5414 fwrite (m, len, 1, stderr);
5415 if (cursor_in_echo_area == 0)
5416 fprintf (stderr, "\n");
5417 fflush (stderr);
5419 /* A null message buffer means that the frame hasn't really been
5420 initialized yet. Error messages get reported properly by
5421 cmd_error, so this must be just an informative message; toss it. */
5422 else if (INTERACTIVE
5423 && sf->glyphs_initialized_p
5424 && FRAME_MESSAGE_BUF (sf))
5426 Lisp_Object mini_window;
5427 struct frame *f;
5429 /* Get the frame containing the mini-buffer
5430 that the selected frame is using. */
5431 mini_window = FRAME_MINIBUF_WINDOW (sf);
5432 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5434 FRAME_SAMPLE_VISIBILITY (f);
5435 if (FRAME_VISIBLE_P (sf)
5436 && ! FRAME_VISIBLE_P (f))
5437 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5439 if (m)
5441 set_message (m, Qnil, len, multibyte);
5442 if (minibuffer_auto_raise)
5443 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5445 else
5446 clear_message (1, 1);
5448 do_pending_window_change (0);
5449 echo_area_display (1);
5450 do_pending_window_change (0);
5451 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5452 (*frame_up_to_date_hook) (f);
5457 /* Display an echo area message M with a specified length of NBYTES
5458 bytes. The string may include null characters. If M is not a
5459 string, clear out any existing message, and let the mini-buffer
5460 text show through. */
5462 void
5463 message3 (m, nbytes, multibyte)
5464 Lisp_Object m;
5465 int nbytes;
5466 int multibyte;
5468 struct gcpro gcpro1;
5470 GCPRO1 (m);
5472 /* First flush out any partial line written with print. */
5473 message_log_maybe_newline ();
5474 if (STRINGP (m))
5475 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5476 message3_nolog (m, nbytes, multibyte);
5478 UNGCPRO;
5482 /* The non-logging version of message3. */
5484 void
5485 message3_nolog (m, nbytes, multibyte)
5486 Lisp_Object m;
5487 int nbytes, multibyte;
5489 struct frame *sf = SELECTED_FRAME ();
5490 message_enable_multibyte = multibyte;
5492 if (noninteractive)
5494 if (noninteractive_need_newline)
5495 putc ('\n', stderr);
5496 noninteractive_need_newline = 0;
5497 if (STRINGP (m))
5498 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5499 if (cursor_in_echo_area == 0)
5500 fprintf (stderr, "\n");
5501 fflush (stderr);
5503 /* A null message buffer means that the frame hasn't really been
5504 initialized yet. Error messages get reported properly by
5505 cmd_error, so this must be just an informative message; toss it. */
5506 else if (INTERACTIVE
5507 && sf->glyphs_initialized_p
5508 && FRAME_MESSAGE_BUF (sf))
5510 Lisp_Object mini_window;
5511 Lisp_Object frame;
5512 struct frame *f;
5514 /* Get the frame containing the mini-buffer
5515 that the selected frame is using. */
5516 mini_window = FRAME_MINIBUF_WINDOW (sf);
5517 frame = XWINDOW (mini_window)->frame;
5518 f = XFRAME (frame);
5520 FRAME_SAMPLE_VISIBILITY (f);
5521 if (FRAME_VISIBLE_P (sf)
5522 && !FRAME_VISIBLE_P (f))
5523 Fmake_frame_visible (frame);
5525 if (STRINGP (m) && XSTRING (m)->size)
5527 set_message (NULL, m, nbytes, multibyte);
5528 if (minibuffer_auto_raise)
5529 Fraise_frame (frame);
5531 else
5532 clear_message (1, 1);
5534 do_pending_window_change (0);
5535 echo_area_display (1);
5536 do_pending_window_change (0);
5537 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5538 (*frame_up_to_date_hook) (f);
5543 /* Display a null-terminated echo area message M. If M is 0, clear
5544 out any existing message, and let the mini-buffer text show through.
5546 The buffer M must continue to exist until after the echo area gets
5547 cleared or some other message gets displayed there. Do not pass
5548 text that is stored in a Lisp string. Do not pass text in a buffer
5549 that was alloca'd. */
5551 void
5552 message1 (m)
5553 char *m;
5555 message2 (m, (m ? strlen (m) : 0), 0);
5559 /* The non-logging counterpart of message1. */
5561 void
5562 message1_nolog (m)
5563 char *m;
5565 message2_nolog (m, (m ? strlen (m) : 0), 0);
5568 /* Display a message M which contains a single %s
5569 which gets replaced with STRING. */
5571 void
5572 message_with_string (m, string, log)
5573 char *m;
5574 Lisp_Object string;
5575 int log;
5577 if (noninteractive)
5579 if (m)
5581 if (noninteractive_need_newline)
5582 putc ('\n', stderr);
5583 noninteractive_need_newline = 0;
5584 fprintf (stderr, m, XSTRING (string)->data);
5585 if (cursor_in_echo_area == 0)
5586 fprintf (stderr, "\n");
5587 fflush (stderr);
5590 else if (INTERACTIVE)
5592 /* The frame whose minibuffer we're going to display the message on.
5593 It may be larger than the selected frame, so we need
5594 to use its buffer, not the selected frame's buffer. */
5595 Lisp_Object mini_window;
5596 struct frame *f, *sf = SELECTED_FRAME ();
5598 /* Get the frame containing the minibuffer
5599 that the selected frame is using. */
5600 mini_window = FRAME_MINIBUF_WINDOW (sf);
5601 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5603 /* A null message buffer means that the frame hasn't really been
5604 initialized yet. Error messages get reported properly by
5605 cmd_error, so this must be just an informative message; toss it. */
5606 if (FRAME_MESSAGE_BUF (f))
5608 int len;
5609 char *a[1];
5610 a[0] = (char *) XSTRING (string)->data;
5612 len = doprnt (FRAME_MESSAGE_BUF (f),
5613 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5615 if (log)
5616 message2 (FRAME_MESSAGE_BUF (f), len,
5617 STRING_MULTIBYTE (string));
5618 else
5619 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5620 STRING_MULTIBYTE (string));
5622 /* Print should start at the beginning of the message
5623 buffer next time. */
5624 message_buf_print = 0;
5630 /* Dump an informative message to the minibuf. If M is 0, clear out
5631 any existing message, and let the mini-buffer text show through. */
5633 /* VARARGS 1 */
5634 void
5635 message (m, a1, a2, a3)
5636 char *m;
5637 EMACS_INT a1, a2, a3;
5639 if (noninteractive)
5641 if (m)
5643 if (noninteractive_need_newline)
5644 putc ('\n', stderr);
5645 noninteractive_need_newline = 0;
5646 fprintf (stderr, m, a1, a2, a3);
5647 if (cursor_in_echo_area == 0)
5648 fprintf (stderr, "\n");
5649 fflush (stderr);
5652 else if (INTERACTIVE)
5654 /* The frame whose mini-buffer we're going to display the message
5655 on. It may be larger than the selected frame, so we need to
5656 use its buffer, not the selected frame's buffer. */
5657 Lisp_Object mini_window;
5658 struct frame *f, *sf = SELECTED_FRAME ();
5660 /* Get the frame containing the mini-buffer
5661 that the selected frame is using. */
5662 mini_window = FRAME_MINIBUF_WINDOW (sf);
5663 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5665 /* A null message buffer means that the frame hasn't really been
5666 initialized yet. Error messages get reported properly by
5667 cmd_error, so this must be just an informative message; toss
5668 it. */
5669 if (FRAME_MESSAGE_BUF (f))
5671 if (m)
5673 int len;
5674 #ifdef NO_ARG_ARRAY
5675 char *a[3];
5676 a[0] = (char *) a1;
5677 a[1] = (char *) a2;
5678 a[2] = (char *) a3;
5680 len = doprnt (FRAME_MESSAGE_BUF (f),
5681 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5682 #else
5683 len = doprnt (FRAME_MESSAGE_BUF (f),
5684 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5685 (char **) &a1);
5686 #endif /* NO_ARG_ARRAY */
5688 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5690 else
5691 message1 (0);
5693 /* Print should start at the beginning of the message
5694 buffer next time. */
5695 message_buf_print = 0;
5701 /* The non-logging version of message. */
5703 void
5704 message_nolog (m, a1, a2, a3)
5705 char *m;
5706 EMACS_INT a1, a2, a3;
5708 Lisp_Object old_log_max;
5709 old_log_max = Vmessage_log_max;
5710 Vmessage_log_max = Qnil;
5711 message (m, a1, a2, a3);
5712 Vmessage_log_max = old_log_max;
5716 /* Display the current message in the current mini-buffer. This is
5717 only called from error handlers in process.c, and is not time
5718 critical. */
5720 void
5721 update_echo_area ()
5723 if (!NILP (echo_area_buffer[0]))
5725 Lisp_Object string;
5726 string = Fcurrent_message ();
5727 message3 (string, XSTRING (string)->size,
5728 !NILP (current_buffer->enable_multibyte_characters));
5733 /* Make sure echo area buffers in echo_buffers[] are life. If they
5734 aren't, make new ones. */
5736 static void
5737 ensure_echo_area_buffers ()
5739 int i;
5741 for (i = 0; i < 2; ++i)
5742 if (!BUFFERP (echo_buffer[i])
5743 || NILP (XBUFFER (echo_buffer[i])->name))
5745 char name[30];
5746 Lisp_Object old_buffer;
5747 int j;
5749 old_buffer = echo_buffer[i];
5750 sprintf (name, " *Echo Area %d*", i);
5751 echo_buffer[i] = Fget_buffer_create (build_string (name));
5752 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5754 for (j = 0; j < 2; ++j)
5755 if (EQ (old_buffer, echo_area_buffer[j]))
5756 echo_area_buffer[j] = echo_buffer[i];
5761 /* Call FN with args A1..A4 with either the current or last displayed
5762 echo_area_buffer as current buffer.
5764 WHICH zero means use the current message buffer
5765 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5766 from echo_buffer[] and clear it.
5768 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5769 suitable buffer from echo_buffer[] and clear it.
5771 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5772 that the current message becomes the last displayed one, make
5773 choose a suitable buffer for echo_area_buffer[0], and clear it.
5775 Value is what FN returns. */
5777 static int
5778 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5779 struct window *w;
5780 int which;
5781 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5782 EMACS_INT a1;
5783 Lisp_Object a2;
5784 EMACS_INT a3, a4;
5786 Lisp_Object buffer;
5787 int this_one, the_other, clear_buffer_p, rc;
5788 int count = BINDING_STACK_SIZE ();
5790 /* If buffers aren't life, make new ones. */
5791 ensure_echo_area_buffers ();
5793 clear_buffer_p = 0;
5795 if (which == 0)
5796 this_one = 0, the_other = 1;
5797 else if (which > 0)
5798 this_one = 1, the_other = 0;
5799 else
5801 this_one = 0, the_other = 1;
5802 clear_buffer_p = 1;
5804 /* We need a fresh one in case the current echo buffer equals
5805 the one containing the last displayed echo area message. */
5806 if (!NILP (echo_area_buffer[this_one])
5807 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5808 echo_area_buffer[this_one] = Qnil;
5811 /* Choose a suitable buffer from echo_buffer[] is we don't
5812 have one. */
5813 if (NILP (echo_area_buffer[this_one]))
5815 echo_area_buffer[this_one]
5816 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5817 ? echo_buffer[the_other]
5818 : echo_buffer[this_one]);
5819 clear_buffer_p = 1;
5822 buffer = echo_area_buffer[this_one];
5824 record_unwind_protect (unwind_with_echo_area_buffer,
5825 with_echo_area_buffer_unwind_data (w));
5827 /* Make the echo area buffer current. Note that for display
5828 purposes, it is not necessary that the displayed window's buffer
5829 == current_buffer, except for text property lookup. So, let's
5830 only set that buffer temporarily here without doing a full
5831 Fset_window_buffer. We must also change w->pointm, though,
5832 because otherwise an assertions in unshow_buffer fails, and Emacs
5833 aborts. */
5834 set_buffer_internal_1 (XBUFFER (buffer));
5835 if (w)
5837 w->buffer = buffer;
5838 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5841 current_buffer->undo_list = Qt;
5842 current_buffer->read_only = Qnil;
5844 if (clear_buffer_p && Z > BEG)
5845 del_range (BEG, Z);
5847 xassert (BEGV >= BEG);
5848 xassert (ZV <= Z && ZV >= BEGV);
5850 rc = fn (a1, a2, a3, a4);
5852 xassert (BEGV >= BEG);
5853 xassert (ZV <= Z && ZV >= BEGV);
5855 unbind_to (count, Qnil);
5856 return rc;
5860 /* Save state that should be preserved around the call to the function
5861 FN called in with_echo_area_buffer. */
5863 static Lisp_Object
5864 with_echo_area_buffer_unwind_data (w)
5865 struct window *w;
5867 int i = 0;
5868 Lisp_Object vector;
5870 /* Reduce consing by keeping one vector in
5871 Vwith_echo_area_save_vector. */
5872 vector = Vwith_echo_area_save_vector;
5873 Vwith_echo_area_save_vector = Qnil;
5875 if (NILP (vector))
5876 vector = Fmake_vector (make_number (7), Qnil);
5878 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5879 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5880 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5882 if (w)
5884 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5885 XVECTOR (vector)->contents[i++] = w->buffer;
5886 XVECTOR (vector)->contents[i++]
5887 = make_number (XMARKER (w->pointm)->charpos);
5888 XVECTOR (vector)->contents[i++]
5889 = make_number (XMARKER (w->pointm)->bytepos);
5891 else
5893 int end = i + 4;
5894 while (i < end)
5895 XVECTOR (vector)->contents[i++] = Qnil;
5898 xassert (i == XVECTOR (vector)->size);
5899 return vector;
5903 /* Restore global state from VECTOR which was created by
5904 with_echo_area_buffer_unwind_data. */
5906 static Lisp_Object
5907 unwind_with_echo_area_buffer (vector)
5908 Lisp_Object vector;
5910 int i = 0;
5912 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5913 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5914 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5916 if (WINDOWP (XVECTOR (vector)->contents[i]))
5918 struct window *w;
5919 Lisp_Object buffer, charpos, bytepos;
5921 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5922 buffer = XVECTOR (vector)->contents[i]; ++i;
5923 charpos = XVECTOR (vector)->contents[i]; ++i;
5924 bytepos = XVECTOR (vector)->contents[i]; ++i;
5926 w->buffer = buffer;
5927 set_marker_both (w->pointm, buffer,
5928 XFASTINT (charpos), XFASTINT (bytepos));
5931 Vwith_echo_area_save_vector = vector;
5932 return Qnil;
5936 /* Set up the echo area for use by print functions. MULTIBYTE_P
5937 non-zero means we will print multibyte. */
5939 void
5940 setup_echo_area_for_printing (multibyte_p)
5941 int multibyte_p;
5943 ensure_echo_area_buffers ();
5945 if (!message_buf_print)
5947 /* A message has been output since the last time we printed.
5948 Choose a fresh echo area buffer. */
5949 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5950 echo_area_buffer[0] = echo_buffer[1];
5951 else
5952 echo_area_buffer[0] = echo_buffer[0];
5954 /* Switch to that buffer and clear it. */
5955 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5956 if (Z > BEG)
5957 del_range (BEG, Z);
5958 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5960 /* Set up the buffer for the multibyteness we need. */
5961 if (multibyte_p
5962 != !NILP (current_buffer->enable_multibyte_characters))
5963 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5965 /* Raise the frame containing the echo area. */
5966 if (minibuffer_auto_raise)
5968 struct frame *sf = SELECTED_FRAME ();
5969 Lisp_Object mini_window;
5970 mini_window = FRAME_MINIBUF_WINDOW (sf);
5971 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5974 message_log_maybe_newline ();
5975 message_buf_print = 1;
5977 else
5979 if (NILP (echo_area_buffer[0]))
5981 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5982 echo_area_buffer[0] = echo_buffer[1];
5983 else
5984 echo_area_buffer[0] = echo_buffer[0];
5987 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5988 /* Someone switched buffers between print requests. */
5989 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5994 /* Display an echo area message in window W. Value is non-zero if W's
5995 height is changed. If display_last_displayed_message_p is
5996 non-zero, display the message that was last displayed, otherwise
5997 display the current message. */
5999 static int
6000 display_echo_area (w)
6001 struct window *w;
6003 int i, no_message_p, window_height_changed_p, count;
6005 /* Temporarily disable garbage collections while displaying the echo
6006 area. This is done because a GC can print a message itself.
6007 That message would modify the echo area buffer's contents while a
6008 redisplay of the buffer is going on, and seriously confuse
6009 redisplay. */
6010 count = inhibit_garbage_collection ();
6012 /* If there is no message, we must call display_echo_area_1
6013 nevertheless because it resizes the window. But we will have to
6014 reset the echo_area_buffer in question to nil at the end because
6015 with_echo_area_buffer will sets it to an empty buffer. */
6016 i = display_last_displayed_message_p ? 1 : 0;
6017 no_message_p = NILP (echo_area_buffer[i]);
6019 window_height_changed_p
6020 = with_echo_area_buffer (w, display_last_displayed_message_p,
6021 display_echo_area_1,
6022 (EMACS_INT) w, Qnil, 0, 0);
6024 if (no_message_p)
6025 echo_area_buffer[i] = Qnil;
6027 unbind_to (count, Qnil);
6028 return window_height_changed_p;
6032 /* Helper for display_echo_area. Display the current buffer which
6033 contains the current echo area message in window W, a mini-window,
6034 a pointer to which is passed in A1. A2..A4 are currently not used.
6035 Change the height of W so that all of the message is displayed.
6036 Value is non-zero if height of W was changed. */
6038 static int
6039 display_echo_area_1 (a1, a2, a3, a4)
6040 EMACS_INT a1;
6041 Lisp_Object a2;
6042 EMACS_INT a3, a4;
6044 struct window *w = (struct window *) a1;
6045 Lisp_Object window;
6046 struct text_pos start;
6047 int window_height_changed_p = 0;
6049 /* Do this before displaying, so that we have a large enough glyph
6050 matrix for the display. */
6051 window_height_changed_p = resize_mini_window (w, 0);
6053 /* Display. */
6054 clear_glyph_matrix (w->desired_matrix);
6055 XSETWINDOW (window, w);
6056 SET_TEXT_POS (start, BEG, BEG_BYTE);
6057 try_window (window, start);
6059 return window_height_changed_p;
6063 /* Resize the echo area window to exactly the size needed for the
6064 currently displayed message, if there is one. */
6066 void
6067 resize_echo_area_axactly ()
6069 if (BUFFERP (echo_area_buffer[0])
6070 && WINDOWP (echo_area_window))
6072 struct window *w = XWINDOW (echo_area_window);
6073 int resized_p;
6075 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6076 (EMACS_INT) w, Qnil, 0, 0);
6077 if (resized_p)
6079 ++windows_or_buffers_changed;
6080 ++update_mode_lines;
6081 redisplay_internal (0);
6087 /* Callback function for with_echo_area_buffer, when used from
6088 resize_echo_area_axactly. A1 contains a pointer to the window to
6089 resize, A2 to A4 are not used. Value is what resize_mini_window
6090 returns. */
6092 static int
6093 resize_mini_window_1 (a1, a2, a3, a4)
6094 EMACS_INT a1;
6095 Lisp_Object a2;
6096 EMACS_INT a3, a4;
6098 return resize_mini_window ((struct window *) a1, 1);
6102 /* Resize mini-window W to fit the size of its contents. EXACT:P
6103 means size the window exactly to the size needed. Otherwise, it's
6104 only enlarged until W's buffer is empty. Value is non-zero if
6105 the window height has been changed. */
6108 resize_mini_window (w, exact_p)
6109 struct window *w;
6110 int exact_p;
6112 struct frame *f = XFRAME (w->frame);
6113 int window_height_changed_p = 0;
6115 xassert (MINI_WINDOW_P (w));
6117 /* Nil means don't try to resize. */
6118 if (NILP (Vresize_mini_windows)
6119 || (FRAME_X_P (f) && f->output_data.x == NULL))
6120 return 0;
6122 if (!FRAME_MINIBUF_ONLY_P (f))
6124 struct it it;
6125 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6126 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6127 int height, max_height;
6128 int unit = CANON_Y_UNIT (f);
6129 struct text_pos start;
6130 struct buffer *old_current_buffer = NULL;
6132 if (current_buffer != XBUFFER (w->buffer))
6134 old_current_buffer = current_buffer;
6135 set_buffer_internal (XBUFFER (w->buffer));
6138 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6140 /* Compute the max. number of lines specified by the user. */
6141 if (FLOATP (Vmax_mini_window_height))
6142 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6143 else if (INTEGERP (Vmax_mini_window_height))
6144 max_height = XINT (Vmax_mini_window_height);
6145 else
6146 max_height = total_height / 4;
6148 /* Correct that max. height if it's bogus. */
6149 max_height = max (1, max_height);
6150 max_height = min (total_height, max_height);
6152 /* Find out the height of the text in the window. */
6153 if (it.truncate_lines_p)
6154 height = 1;
6155 else
6157 last_height = 0;
6158 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6159 if (it.max_ascent == 0 && it.max_descent == 0)
6160 height = it.current_y + last_height;
6161 else
6162 height = it.current_y + it.max_ascent + it.max_descent;
6163 height -= it.extra_line_spacing;
6164 height = (height + unit - 1) / unit;
6167 /* Compute a suitable window start. */
6168 if (height > max_height)
6170 height = max_height;
6171 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6172 move_it_vertically_backward (&it, (height - 1) * unit);
6173 start = it.current.pos;
6175 else
6176 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6177 SET_MARKER_FROM_TEXT_POS (w->start, start);
6179 if (EQ (Vresize_mini_windows, Qgrow_only))
6181 /* Let it grow only, until we display an empty message, in which
6182 case the window shrinks again. */
6183 if (height > XFASTINT (w->height))
6185 int old_height = XFASTINT (w->height);
6186 freeze_window_starts (f, 1);
6187 grow_mini_window (w, height - XFASTINT (w->height));
6188 window_height_changed_p = XFASTINT (w->height) != old_height;
6190 else if (height < XFASTINT (w->height)
6191 && (exact_p || BEGV == ZV))
6193 int old_height = XFASTINT (w->height);
6194 freeze_window_starts (f, 0);
6195 shrink_mini_window (w);
6196 window_height_changed_p = XFASTINT (w->height) != old_height;
6199 else
6201 /* Always resize to exact size needed. */
6202 if (height > XFASTINT (w->height))
6204 int old_height = XFASTINT (w->height);
6205 freeze_window_starts (f, 1);
6206 grow_mini_window (w, height - XFASTINT (w->height));
6207 window_height_changed_p = XFASTINT (w->height) != old_height;
6209 else if (height < XFASTINT (w->height))
6211 int old_height = XFASTINT (w->height);
6212 freeze_window_starts (f, 0);
6213 shrink_mini_window (w);
6215 if (height)
6217 freeze_window_starts (f, 1);
6218 grow_mini_window (w, height - XFASTINT (w->height));
6221 window_height_changed_p = XFASTINT (w->height) != old_height;
6225 if (old_current_buffer)
6226 set_buffer_internal (old_current_buffer);
6229 return window_height_changed_p;
6233 /* Value is the current message, a string, or nil if there is no
6234 current message. */
6236 Lisp_Object
6237 current_message ()
6239 Lisp_Object msg;
6241 if (NILP (echo_area_buffer[0]))
6242 msg = Qnil;
6243 else
6245 with_echo_area_buffer (0, 0, current_message_1,
6246 (EMACS_INT) &msg, Qnil, 0, 0);
6247 if (NILP (msg))
6248 echo_area_buffer[0] = Qnil;
6251 return msg;
6255 static int
6256 current_message_1 (a1, a2, a3, a4)
6257 EMACS_INT a1;
6258 Lisp_Object a2;
6259 EMACS_INT a3, a4;
6261 Lisp_Object *msg = (Lisp_Object *) a1;
6263 if (Z > BEG)
6264 *msg = make_buffer_string (BEG, Z, 1);
6265 else
6266 *msg = Qnil;
6267 return 0;
6271 /* Push the current message on Vmessage_stack for later restauration
6272 by restore_message. Value is non-zero if the current message isn't
6273 empty. This is a relatively infrequent operation, so it's not
6274 worth optimizing. */
6277 push_message ()
6279 Lisp_Object msg;
6280 msg = current_message ();
6281 Vmessage_stack = Fcons (msg, Vmessage_stack);
6282 return STRINGP (msg);
6286 /* Restore message display from the top of Vmessage_stack. */
6288 void
6289 restore_message ()
6291 Lisp_Object msg;
6293 xassert (CONSP (Vmessage_stack));
6294 msg = XCAR (Vmessage_stack);
6295 if (STRINGP (msg))
6296 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6297 else
6298 message3_nolog (msg, 0, 0);
6302 /* Pop the top-most entry off Vmessage_stack. */
6304 void
6305 pop_message ()
6307 xassert (CONSP (Vmessage_stack));
6308 Vmessage_stack = XCDR (Vmessage_stack);
6312 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6313 exits. If the stack is not empty, we have a missing pop_message
6314 somewhere. */
6316 void
6317 check_message_stack ()
6319 if (!NILP (Vmessage_stack))
6320 abort ();
6324 /* Truncate to NCHARS what will be displayed in the echo area the next
6325 time we display it---but don't redisplay it now. */
6327 void
6328 truncate_echo_area (nchars)
6329 int nchars;
6331 if (nchars == 0)
6332 echo_area_buffer[0] = Qnil;
6333 /* A null message buffer means that the frame hasn't really been
6334 initialized yet. Error messages get reported properly by
6335 cmd_error, so this must be just an informative message; toss it. */
6336 else if (!noninteractive
6337 && INTERACTIVE
6338 && !NILP (echo_area_buffer[0]))
6340 struct frame *sf = SELECTED_FRAME ();
6341 if (FRAME_MESSAGE_BUF (sf))
6342 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6347 /* Helper function for truncate_echo_area. Truncate the current
6348 message to at most NCHARS characters. */
6350 static int
6351 truncate_message_1 (nchars, a2, a3, a4)
6352 EMACS_INT nchars;
6353 Lisp_Object a2;
6354 EMACS_INT a3, a4;
6356 if (BEG + nchars < Z)
6357 del_range (BEG + nchars, Z);
6358 if (Z == BEG)
6359 echo_area_buffer[0] = Qnil;
6360 return 0;
6364 /* Set the current message to a substring of S or STRING.
6366 If STRING is a Lisp string, set the message to the first NBYTES
6367 bytes from STRING. NBYTES zero means use the whole string. If
6368 STRING is multibyte, the message will be displayed multibyte.
6370 If S is not null, set the message to the first LEN bytes of S. LEN
6371 zero means use the whole string. MULTIBYTE_P non-zero means S is
6372 multibyte. Display the message multibyte in that case. */
6374 void
6375 set_message (s, string, nbytes, multibyte_p)
6376 char *s;
6377 Lisp_Object string;
6378 int nbytes;
6380 message_enable_multibyte
6381 = ((s && multibyte_p)
6382 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6384 with_echo_area_buffer (0, -1, set_message_1,
6385 (EMACS_INT) s, string, nbytes, multibyte_p);
6386 message_buf_print = 0;
6387 help_echo_showing_p = 0;
6391 /* Helper function for set_message. Arguments have the same meaning
6392 as there, with A1 corresponding to S and A2 corresponding to STRING
6393 This function is called with the echo area buffer being
6394 current. */
6396 static int
6397 set_message_1 (a1, a2, nbytes, multibyte_p)
6398 EMACS_INT a1;
6399 Lisp_Object a2;
6400 EMACS_INT nbytes, multibyte_p;
6402 char *s = (char *) a1;
6403 Lisp_Object string = a2;
6405 xassert (BEG == Z);
6407 /* Change multibyteness of the echo buffer appropriately. */
6408 if (message_enable_multibyte
6409 != !NILP (current_buffer->enable_multibyte_characters))
6410 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6412 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6414 /* Insert new message at BEG. */
6415 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6417 if (STRINGP (string))
6419 int nchars;
6421 if (nbytes == 0)
6422 nbytes = XSTRING (string)->size_byte;
6423 nchars = string_byte_to_char (string, nbytes);
6425 /* This function takes care of single/multibyte conversion. We
6426 just have to ensure that the echo area buffer has the right
6427 setting of enable_multibyte_characters. */
6428 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6430 else if (s)
6432 if (nbytes == 0)
6433 nbytes = strlen (s);
6435 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6437 /* Convert from multi-byte to single-byte. */
6438 int i, c, n;
6439 unsigned char work[1];
6441 /* Convert a multibyte string to single-byte. */
6442 for (i = 0; i < nbytes; i += n)
6444 c = string_char_and_length (s + i, nbytes - i, &n);
6445 work[0] = (SINGLE_BYTE_CHAR_P (c)
6447 : multibyte_char_to_unibyte (c, Qnil));
6448 insert_1_both (work, 1, 1, 1, 0, 0);
6451 else if (!multibyte_p
6452 && !NILP (current_buffer->enable_multibyte_characters))
6454 /* Convert from single-byte to multi-byte. */
6455 int i, c, n;
6456 unsigned char *msg = (unsigned char *) s;
6457 unsigned char str[MAX_MULTIBYTE_LENGTH];
6459 /* Convert a single-byte string to multibyte. */
6460 for (i = 0; i < nbytes; i++)
6462 c = unibyte_char_to_multibyte (msg[i]);
6463 n = CHAR_STRING (c, str);
6464 insert_1_both (str, 1, n, 1, 0, 0);
6467 else
6468 insert_1 (s, nbytes, 1, 0, 0);
6471 return 0;
6475 /* Clear messages. CURRENT_P non-zero means clear the current
6476 message. LAST_DISPLAYED_P non-zero means clear the message
6477 last displayed. */
6479 void
6480 clear_message (current_p, last_displayed_p)
6481 int current_p, last_displayed_p;
6483 if (current_p)
6484 echo_area_buffer[0] = Qnil;
6486 if (last_displayed_p)
6487 echo_area_buffer[1] = Qnil;
6489 message_buf_print = 0;
6492 /* Clear garbaged frames.
6494 This function is used where the old redisplay called
6495 redraw_garbaged_frames which in turn called redraw_frame which in
6496 turn called clear_frame. The call to clear_frame was a source of
6497 flickering. I believe a clear_frame is not necessary. It should
6498 suffice in the new redisplay to invalidate all current matrices,
6499 and ensure a complete redisplay of all windows. */
6501 static void
6502 clear_garbaged_frames ()
6504 if (frame_garbaged)
6506 Lisp_Object tail, frame;
6508 FOR_EACH_FRAME (tail, frame)
6510 struct frame *f = XFRAME (frame);
6512 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6514 clear_current_matrices (f);
6515 f->garbaged = 0;
6519 frame_garbaged = 0;
6520 ++windows_or_buffers_changed;
6525 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6526 is non-zero update selected_frame. Value is non-zero if the
6527 mini-windows height has been changed. */
6529 static int
6530 echo_area_display (update_frame_p)
6531 int update_frame_p;
6533 Lisp_Object mini_window;
6534 struct window *w;
6535 struct frame *f;
6536 int window_height_changed_p = 0;
6537 struct frame *sf = SELECTED_FRAME ();
6539 mini_window = FRAME_MINIBUF_WINDOW (sf);
6540 w = XWINDOW (mini_window);
6541 f = XFRAME (WINDOW_FRAME (w));
6543 /* Don't display if frame is invisible or not yet initialized. */
6544 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6545 return 0;
6547 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6548 #ifndef macintosh
6549 #ifdef HAVE_WINDOW_SYSTEM
6550 /* When Emacs starts, selected_frame may be a visible terminal
6551 frame, even if we run under a window system. If we let this
6552 through, a message would be displayed on the terminal. */
6553 if (EQ (selected_frame, Vterminal_frame)
6554 && !NILP (Vwindow_system))
6555 return 0;
6556 #endif /* HAVE_WINDOW_SYSTEM */
6557 #endif
6559 /* Redraw garbaged frames. */
6560 if (frame_garbaged)
6561 clear_garbaged_frames ();
6563 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6565 echo_area_window = mini_window;
6566 window_height_changed_p = display_echo_area (w);
6567 w->must_be_updated_p = 1;
6569 /* Update the display, unless called from redisplay_internal.
6570 Also don't update the screen during redisplay itself. The
6571 update will happen at the end of redisplay, and an update
6572 here could cause confusion. */
6573 if (update_frame_p && !redisplaying_p)
6575 int n = 0;
6577 /* If the display update has been interrupted by pending
6578 input, update mode lines in the frame. Due to the
6579 pending input, it might have been that redisplay hasn't
6580 been called, so that mode lines above the echo area are
6581 garbaged. This looks odd, so we prevent it here. */
6582 if (!display_completed)
6583 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6585 if (window_height_changed_p)
6587 /* Must update other windows. */
6588 windows_or_buffers_changed = 1;
6589 redisplay_internal (0);
6591 else if (FRAME_WINDOW_P (f) && n == 0)
6593 /* Window configuration is the same as before.
6594 Can do with a display update of the echo area,
6595 unless we displayed some mode lines. */
6596 update_single_window (w, 1);
6597 rif->flush_display (f);
6599 else
6600 update_frame (f, 1, 1);
6603 else if (!EQ (mini_window, selected_window))
6604 windows_or_buffers_changed++;
6606 /* Last displayed message is now the current message. */
6607 echo_area_buffer[1] = echo_area_buffer[0];
6609 /* Prevent redisplay optimization in redisplay_internal by resetting
6610 this_line_start_pos. This is done because the mini-buffer now
6611 displays the message instead of its buffer text. */
6612 if (EQ (mini_window, selected_window))
6613 CHARPOS (this_line_start_pos) = 0;
6615 return window_height_changed_p;
6620 /***********************************************************************
6621 Frame Titles
6622 ***********************************************************************/
6625 #ifdef HAVE_WINDOW_SYSTEM
6627 /* A buffer for constructing frame titles in it; allocated from the
6628 heap in init_xdisp and resized as needed in store_frame_title_char. */
6630 static char *frame_title_buf;
6632 /* The buffer's end, and a current output position in it. */
6634 static char *frame_title_buf_end;
6635 static char *frame_title_ptr;
6638 /* Store a single character C for the frame title in frame_title_buf.
6639 Re-allocate frame_title_buf if necessary. */
6641 static void
6642 store_frame_title_char (c)
6643 char c;
6645 /* If output position has reached the end of the allocated buffer,
6646 double the buffer's size. */
6647 if (frame_title_ptr == frame_title_buf_end)
6649 int len = frame_title_ptr - frame_title_buf;
6650 int new_size = 2 * len * sizeof *frame_title_buf;
6651 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6652 frame_title_buf_end = frame_title_buf + new_size;
6653 frame_title_ptr = frame_title_buf + len;
6656 *frame_title_ptr++ = c;
6660 /* Store part of a frame title in frame_title_buf, beginning at
6661 frame_title_ptr. STR is the string to store. Do not copy more
6662 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6663 the whole string. Pad with spaces until FIELD_WIDTH number of
6664 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6665 Called from display_mode_element when it is used to build a frame
6666 title. */
6668 static int
6669 store_frame_title (str, field_width, precision)
6670 unsigned char *str;
6671 int field_width, precision;
6673 int n = 0;
6675 /* Copy at most PRECISION chars from STR. */
6676 while ((precision <= 0 || n < precision)
6677 && *str)
6679 store_frame_title_char (*str++);
6680 ++n;
6683 /* Fill up with spaces until FIELD_WIDTH reached. */
6684 while (field_width > 0
6685 && n < field_width)
6687 store_frame_title_char (' ');
6688 ++n;
6691 return n;
6695 /* Set the title of FRAME, if it has changed. The title format is
6696 Vicon_title_format if FRAME is iconified, otherwise it is
6697 frame_title_format. */
6699 static void
6700 x_consider_frame_title (frame)
6701 Lisp_Object frame;
6703 struct frame *f = XFRAME (frame);
6705 if (FRAME_WINDOW_P (f)
6706 || FRAME_MINIBUF_ONLY_P (f)
6707 || f->explicit_name)
6709 /* Do we have more than one visible frame on this X display? */
6710 Lisp_Object tail;
6711 Lisp_Object fmt;
6712 struct buffer *obuf;
6713 int len;
6714 struct it it;
6716 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6718 struct frame *tf = XFRAME (XCAR (tail));
6720 if (tf != f
6721 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6722 && !FRAME_MINIBUF_ONLY_P (tf)
6723 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6724 break;
6727 /* Set global variable indicating that multiple frames exist. */
6728 multiple_frames = CONSP (tail);
6730 /* Switch to the buffer of selected window of the frame. Set up
6731 frame_title_ptr so that display_mode_element will output into it;
6732 then display the title. */
6733 obuf = current_buffer;
6734 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6735 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6736 frame_title_ptr = frame_title_buf;
6737 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6738 NULL, DEFAULT_FACE_ID);
6739 len = display_mode_element (&it, 0, -1, -1, fmt);
6740 frame_title_ptr = NULL;
6741 set_buffer_internal (obuf);
6743 /* Set the title only if it's changed. This avoids consing in
6744 the common case where it hasn't. (If it turns out that we've
6745 already wasted too much time by walking through the list with
6746 display_mode_element, then we might need to optimize at a
6747 higher level than this.) */
6748 if (! STRINGP (f->name)
6749 || STRING_BYTES (XSTRING (f->name)) != len
6750 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6751 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6755 #else /* not HAVE_WINDOW_SYSTEM */
6757 #define frame_title_ptr ((char *)0)
6758 #define store_frame_title(str, mincol, maxcol) 0
6760 #endif /* not HAVE_WINDOW_SYSTEM */
6765 /***********************************************************************
6766 Menu Bars
6767 ***********************************************************************/
6770 /* Prepare for redisplay by updating menu-bar item lists when
6771 appropriate. This can call eval. */
6773 void
6774 prepare_menu_bars ()
6776 int all_windows;
6777 struct gcpro gcpro1, gcpro2;
6778 struct frame *f;
6779 struct frame *tooltip_frame;
6781 #ifdef HAVE_X_WINDOWS
6782 tooltip_frame = tip_frame;
6783 #else
6784 tooltip_frame = NULL;
6785 #endif
6787 /* Update all frame titles based on their buffer names, etc. We do
6788 this before the menu bars so that the buffer-menu will show the
6789 up-to-date frame titles. */
6790 #ifdef HAVE_WINDOW_SYSTEM
6791 if (windows_or_buffers_changed || update_mode_lines)
6793 Lisp_Object tail, frame;
6795 FOR_EACH_FRAME (tail, frame)
6797 f = XFRAME (frame);
6798 if (f != tooltip_frame
6799 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6800 x_consider_frame_title (frame);
6803 #endif /* HAVE_WINDOW_SYSTEM */
6805 /* Update the menu bar item lists, if appropriate. This has to be
6806 done before any actual redisplay or generation of display lines. */
6807 all_windows = (update_mode_lines
6808 || buffer_shared > 1
6809 || windows_or_buffers_changed);
6810 if (all_windows)
6812 Lisp_Object tail, frame;
6813 int count = BINDING_STACK_SIZE ();
6815 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6817 FOR_EACH_FRAME (tail, frame)
6819 f = XFRAME (frame);
6821 /* Ignore tooltip frame. */
6822 if (f == tooltip_frame)
6823 continue;
6825 /* If a window on this frame changed size, report that to
6826 the user and clear the size-change flag. */
6827 if (FRAME_WINDOW_SIZES_CHANGED (f))
6829 Lisp_Object functions;
6831 /* Clear flag first in case we get an error below. */
6832 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6833 functions = Vwindow_size_change_functions;
6834 GCPRO2 (tail, functions);
6836 while (CONSP (functions))
6838 call1 (XCAR (functions), frame);
6839 functions = XCDR (functions);
6841 UNGCPRO;
6844 GCPRO1 (tail);
6845 update_menu_bar (f, 0);
6846 #ifdef HAVE_WINDOW_SYSTEM
6847 update_tool_bar (f, 0);
6848 #endif
6849 UNGCPRO;
6852 unbind_to (count, Qnil);
6854 else
6856 struct frame *sf = SELECTED_FRAME ();
6857 update_menu_bar (sf, 1);
6858 #ifdef HAVE_WINDOW_SYSTEM
6859 update_tool_bar (sf, 1);
6860 #endif
6863 /* Motif needs this. See comment in xmenu.c. Turn it off when
6864 pending_menu_activation is not defined. */
6865 #ifdef USE_X_TOOLKIT
6866 pending_menu_activation = 0;
6867 #endif
6871 /* Update the menu bar item list for frame F. This has to be done
6872 before we start to fill in any display lines, because it can call
6873 eval.
6875 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6877 static void
6878 update_menu_bar (f, save_match_data)
6879 struct frame *f;
6880 int save_match_data;
6882 Lisp_Object window;
6883 register struct window *w;
6885 window = FRAME_SELECTED_WINDOW (f);
6886 w = XWINDOW (window);
6888 if (update_mode_lines)
6889 w->update_mode_line = Qt;
6891 if (FRAME_WINDOW_P (f)
6893 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6894 FRAME_EXTERNAL_MENU_BAR (f)
6895 #else
6896 FRAME_MENU_BAR_LINES (f) > 0
6897 #endif
6898 : FRAME_MENU_BAR_LINES (f) > 0)
6900 /* If the user has switched buffers or windows, we need to
6901 recompute to reflect the new bindings. But we'll
6902 recompute when update_mode_lines is set too; that means
6903 that people can use force-mode-line-update to request
6904 that the menu bar be recomputed. The adverse effect on
6905 the rest of the redisplay algorithm is about the same as
6906 windows_or_buffers_changed anyway. */
6907 if (windows_or_buffers_changed
6908 || !NILP (w->update_mode_line)
6909 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6910 < BUF_MODIFF (XBUFFER (w->buffer)))
6911 != !NILP (w->last_had_star))
6912 || ((!NILP (Vtransient_mark_mode)
6913 && !NILP (XBUFFER (w->buffer)->mark_active))
6914 != !NILP (w->region_showing)))
6916 struct buffer *prev = current_buffer;
6917 int count = BINDING_STACK_SIZE ();
6919 set_buffer_internal_1 (XBUFFER (w->buffer));
6920 if (save_match_data)
6921 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6922 if (NILP (Voverriding_local_map_menu_flag))
6924 specbind (Qoverriding_terminal_local_map, Qnil);
6925 specbind (Qoverriding_local_map, Qnil);
6928 /* Run the Lucid hook. */
6929 call1 (Vrun_hooks, Qactivate_menubar_hook);
6931 /* If it has changed current-menubar from previous value,
6932 really recompute the menu-bar from the value. */
6933 if (! NILP (Vlucid_menu_bar_dirty_flag))
6934 call0 (Qrecompute_lucid_menubar);
6936 safe_run_hooks (Qmenu_bar_update_hook);
6937 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6939 /* Redisplay the menu bar in case we changed it. */
6940 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6941 if (FRAME_WINDOW_P (f)
6942 #if defined (macintosh)
6943 /* All frames on Mac OS share the same menubar. So only the
6944 selected frame should be allowed to set it. */
6945 && f == SELECTED_FRAME ()
6946 #endif
6948 set_frame_menubar (f, 0, 0);
6949 else
6950 /* On a terminal screen, the menu bar is an ordinary screen
6951 line, and this makes it get updated. */
6952 w->update_mode_line = Qt;
6953 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6954 /* In the non-toolkit version, the menu bar is an ordinary screen
6955 line, and this makes it get updated. */
6956 w->update_mode_line = Qt;
6957 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6959 unbind_to (count, Qnil);
6960 set_buffer_internal_1 (prev);
6967 /***********************************************************************
6968 Tool-bars
6969 ***********************************************************************/
6971 #ifdef HAVE_WINDOW_SYSTEM
6973 /* Update the tool-bar item list for frame F. This has to be done
6974 before we start to fill in any display lines. Called from
6975 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6976 and restore it here. */
6978 static void
6979 update_tool_bar (f, save_match_data)
6980 struct frame *f;
6981 int save_match_data;
6983 if (WINDOWP (f->tool_bar_window)
6984 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6986 Lisp_Object window;
6987 struct window *w;
6989 window = FRAME_SELECTED_WINDOW (f);
6990 w = XWINDOW (window);
6992 /* If the user has switched buffers or windows, we need to
6993 recompute to reflect the new bindings. But we'll
6994 recompute when update_mode_lines is set too; that means
6995 that people can use force-mode-line-update to request
6996 that the menu bar be recomputed. The adverse effect on
6997 the rest of the redisplay algorithm is about the same as
6998 windows_or_buffers_changed anyway. */
6999 if (windows_or_buffers_changed
7000 || !NILP (w->update_mode_line)
7001 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7002 < BUF_MODIFF (XBUFFER (w->buffer)))
7003 != !NILP (w->last_had_star))
7004 || ((!NILP (Vtransient_mark_mode)
7005 && !NILP (XBUFFER (w->buffer)->mark_active))
7006 != !NILP (w->region_showing)))
7008 struct buffer *prev = current_buffer;
7009 int count = BINDING_STACK_SIZE ();
7011 /* Set current_buffer to the buffer of the selected
7012 window of the frame, so that we get the right local
7013 keymaps. */
7014 set_buffer_internal_1 (XBUFFER (w->buffer));
7016 /* Save match data, if we must. */
7017 if (save_match_data)
7018 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7020 /* Make sure that we don't accidentally use bogus keymaps. */
7021 if (NILP (Voverriding_local_map_menu_flag))
7023 specbind (Qoverriding_terminal_local_map, Qnil);
7024 specbind (Qoverriding_local_map, Qnil);
7027 /* Build desired tool-bar items from keymaps. */
7028 f->desired_tool_bar_items
7029 = tool_bar_items (f->desired_tool_bar_items,
7030 &f->n_desired_tool_bar_items);
7032 /* Redisplay the tool-bar in case we changed it. */
7033 w->update_mode_line = Qt;
7035 unbind_to (count, Qnil);
7036 set_buffer_internal_1 (prev);
7042 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7043 F's desired tool-bar contents. F->desired_tool_bar_items must have
7044 been set up previously by calling prepare_menu_bars. */
7046 static void
7047 build_desired_tool_bar_string (f)
7048 struct frame *f;
7050 int i, size, size_needed, string_idx;
7051 struct gcpro gcpro1, gcpro2, gcpro3;
7052 Lisp_Object image, plist, props;
7054 image = plist = props = Qnil;
7055 GCPRO3 (image, plist, props);
7057 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7058 Otherwise, make a new string. */
7060 /* The size of the string we might be able to reuse. */
7061 size = (STRINGP (f->desired_tool_bar_string)
7062 ? XSTRING (f->desired_tool_bar_string)->size
7063 : 0);
7065 /* Each image in the string we build is preceded by a space,
7066 and there is a space at the end. */
7067 size_needed = f->n_desired_tool_bar_items + 1;
7069 /* Reuse f->desired_tool_bar_string, if possible. */
7070 if (size < size_needed)
7071 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7072 make_number (' '));
7073 else
7075 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7076 Fremove_text_properties (make_number (0), make_number (size),
7077 props, f->desired_tool_bar_string);
7080 /* Put a `display' property on the string for the images to display,
7081 put a `menu_item' property on tool-bar items with a value that
7082 is the index of the item in F's tool-bar item vector. */
7083 for (i = 0, string_idx = 0;
7084 i < f->n_desired_tool_bar_items;
7085 ++i, string_idx += 1)
7087 #define PROP(IDX) \
7088 (XVECTOR (f->desired_tool_bar_items) \
7089 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
7091 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7092 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7093 int margin, relief, idx;
7094 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7095 extern Lisp_Object Qlaplace;
7097 /* If image is a vector, choose the image according to the
7098 button state. */
7099 image = PROP (TOOL_BAR_ITEM_IMAGES);
7100 if (VECTORP (image))
7102 if (enabled_p)
7103 idx = (selected_p
7104 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7105 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7106 else
7107 idx = (selected_p
7108 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7109 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7111 xassert (ASIZE (image) >= idx);
7112 image = AREF (image, idx);
7114 else
7115 idx = -1;
7117 /* Ignore invalid image specifications. */
7118 if (!valid_image_p (image))
7119 continue;
7121 /* Display the tool-bar button pressed, or depressed. */
7122 plist = Fcopy_sequence (XCDR (image));
7124 /* Compute margin and relief to draw. */
7125 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7126 margin = relief + max (0, tool_bar_button_margin);
7128 if (auto_raise_tool_bar_buttons_p)
7130 /* Add a `:relief' property to the image spec if the item is
7131 selected. */
7132 if (selected_p)
7134 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7135 margin -= relief;
7138 else
7140 /* If image is selected, display it pressed, i.e. with a
7141 negative relief. If it's not selected, display it with a
7142 raised relief. */
7143 plist = Fplist_put (plist, QCrelief,
7144 (selected_p
7145 ? make_number (-relief)
7146 : make_number (relief)));
7147 margin -= relief;
7150 /* Put a margin around the image. */
7151 if (margin)
7152 plist = Fplist_put (plist, QCmargin, make_number (margin));
7154 /* If button is not enabled, and we don't have special images
7155 for the disabled state, make the image appear disabled by
7156 applying an appropriate algorithm to it. */
7157 if (!enabled_p && idx < 0)
7158 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7160 /* Put a `display' text property on the string for the image to
7161 display. Put a `menu-item' property on the string that gives
7162 the start of this item's properties in the tool-bar items
7163 vector. */
7164 image = Fcons (Qimage, plist);
7165 props = list4 (Qdisplay, image,
7166 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7167 Fadd_text_properties (make_number (string_idx),
7168 make_number (string_idx + 1),
7169 props, f->desired_tool_bar_string);
7170 #undef PROP
7173 UNGCPRO;
7177 /* Display one line of the tool-bar of frame IT->f. */
7179 static void
7180 display_tool_bar_line (it)
7181 struct it *it;
7183 struct glyph_row *row = it->glyph_row;
7184 int max_x = it->last_visible_x;
7185 struct glyph *last;
7187 prepare_desired_row (row);
7188 row->y = it->current_y;
7190 while (it->current_x < max_x)
7192 int x_before, x, n_glyphs_before, i, nglyphs;
7194 /* Get the next display element. */
7195 if (!get_next_display_element (it))
7196 break;
7198 /* Produce glyphs. */
7199 x_before = it->current_x;
7200 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7201 PRODUCE_GLYPHS (it);
7203 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7204 i = 0;
7205 x = x_before;
7206 while (i < nglyphs)
7208 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7210 if (x + glyph->pixel_width > max_x)
7212 /* Glyph doesn't fit on line. */
7213 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7214 it->current_x = x;
7215 goto out;
7218 ++it->hpos;
7219 x += glyph->pixel_width;
7220 ++i;
7223 /* Stop at line ends. */
7224 if (ITERATOR_AT_END_OF_LINE_P (it))
7225 break;
7227 set_iterator_to_next (it, 1);
7230 out:;
7232 row->displays_text_p = row->used[TEXT_AREA] != 0;
7233 extend_face_to_end_of_line (it);
7234 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7235 last->right_box_line_p = 1;
7236 compute_line_metrics (it);
7238 /* If line is empty, make it occupy the rest of the tool-bar. */
7239 if (!row->displays_text_p)
7241 row->height = row->phys_height = it->last_visible_y - row->y;
7242 row->ascent = row->phys_ascent = 0;
7245 row->full_width_p = 1;
7246 row->continued_p = 0;
7247 row->truncated_on_left_p = 0;
7248 row->truncated_on_right_p = 0;
7250 it->current_x = it->hpos = 0;
7251 it->current_y += row->height;
7252 ++it->vpos;
7253 ++it->glyph_row;
7257 /* Value is the number of screen lines needed to make all tool-bar
7258 items of frame F visible. */
7260 static int
7261 tool_bar_lines_needed (f)
7262 struct frame *f;
7264 struct window *w = XWINDOW (f->tool_bar_window);
7265 struct it it;
7267 /* Initialize an iterator for iteration over
7268 F->desired_tool_bar_string in the tool-bar window of frame F. */
7269 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7270 it.first_visible_x = 0;
7271 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7272 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7274 while (!ITERATOR_AT_END_P (&it))
7276 it.glyph_row = w->desired_matrix->rows;
7277 clear_glyph_row (it.glyph_row);
7278 display_tool_bar_line (&it);
7281 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7285 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7286 height should be changed. */
7288 static int
7289 redisplay_tool_bar (f)
7290 struct frame *f;
7292 struct window *w;
7293 struct it it;
7294 struct glyph_row *row;
7295 int change_height_p = 0;
7297 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7298 do anything. This means you must start with tool-bar-lines
7299 non-zero to get the auto-sizing effect. Or in other words, you
7300 can turn off tool-bars by specifying tool-bar-lines zero. */
7301 if (!WINDOWP (f->tool_bar_window)
7302 || (w = XWINDOW (f->tool_bar_window),
7303 XFASTINT (w->height) == 0))
7304 return 0;
7306 /* Set up an iterator for the tool-bar window. */
7307 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7308 it.first_visible_x = 0;
7309 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7310 row = it.glyph_row;
7312 /* Build a string that represents the contents of the tool-bar. */
7313 build_desired_tool_bar_string (f);
7314 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7316 /* Display as many lines as needed to display all tool-bar items. */
7317 while (it.current_y < it.last_visible_y)
7318 display_tool_bar_line (&it);
7320 /* It doesn't make much sense to try scrolling in the tool-bar
7321 window, so don't do it. */
7322 w->desired_matrix->no_scrolling_p = 1;
7323 w->must_be_updated_p = 1;
7325 if (auto_resize_tool_bars_p)
7327 int nlines;
7329 /* If there are blank lines at the end, except for a partially
7330 visible blank line at the end that is smaller than
7331 CANON_Y_UNIT, change the tool-bar's height. */
7332 row = it.glyph_row - 1;
7333 if (!row->displays_text_p
7334 && row->height >= CANON_Y_UNIT (f))
7335 change_height_p = 1;
7337 /* If row displays tool-bar items, but is partially visible,
7338 change the tool-bar's height. */
7339 if (row->displays_text_p
7340 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7341 change_height_p = 1;
7343 /* Resize windows as needed by changing the `tool-bar-lines'
7344 frame parameter. */
7345 if (change_height_p
7346 && (nlines = tool_bar_lines_needed (f),
7347 nlines != XFASTINT (w->height)))
7349 extern Lisp_Object Qtool_bar_lines;
7350 Lisp_Object frame;
7351 int old_height = XFASTINT (w->height);
7353 XSETFRAME (frame, f);
7354 clear_glyph_matrix (w->desired_matrix);
7355 Fmodify_frame_parameters (frame,
7356 Fcons (Fcons (Qtool_bar_lines,
7357 make_number (nlines)),
7358 Qnil));
7359 if (XFASTINT (w->height) != old_height)
7360 fonts_changed_p = 1;
7364 return change_height_p;
7368 /* Get information about the tool-bar item which is displayed in GLYPH
7369 on frame F. Return in *PROP_IDX the index where tool-bar item
7370 properties start in F->current_tool_bar_items. Value is zero if
7371 GLYPH doesn't display a tool-bar item. */
7374 tool_bar_item_info (f, glyph, prop_idx)
7375 struct frame *f;
7376 struct glyph *glyph;
7377 int *prop_idx;
7379 Lisp_Object prop;
7380 int success_p;
7382 /* Get the text property `menu-item' at pos. The value of that
7383 property is the start index of this item's properties in
7384 F->current_tool_bar_items. */
7385 prop = Fget_text_property (make_number (glyph->charpos),
7386 Qmenu_item, f->current_tool_bar_string);
7387 if (INTEGERP (prop))
7389 *prop_idx = XINT (prop);
7390 success_p = 1;
7392 else
7393 success_p = 0;
7395 return success_p;
7398 #endif /* HAVE_WINDOW_SYSTEM */
7402 /************************************************************************
7403 Horizontal scrolling
7404 ************************************************************************/
7406 static int hscroll_window_tree P_ ((Lisp_Object));
7407 static int hscroll_windows P_ ((Lisp_Object));
7409 /* For all leaf windows in the window tree rooted at WINDOW, set their
7410 hscroll value so that PT is (i) visible in the window, and (ii) so
7411 that it is not within a certain margin at the window's left and
7412 right border. Value is non-zero if any window's hscroll has been
7413 changed. */
7415 static int
7416 hscroll_window_tree (window)
7417 Lisp_Object window;
7419 int hscrolled_p = 0;
7421 while (WINDOWP (window))
7423 struct window *w = XWINDOW (window);
7425 if (WINDOWP (w->hchild))
7426 hscrolled_p |= hscroll_window_tree (w->hchild);
7427 else if (WINDOWP (w->vchild))
7428 hscrolled_p |= hscroll_window_tree (w->vchild);
7429 else if (w->cursor.vpos >= 0)
7431 int hscroll_margin, text_area_x, text_area_y;
7432 int text_area_width, text_area_height;
7433 struct glyph_row *current_cursor_row
7434 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7435 struct glyph_row *desired_cursor_row
7436 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7437 struct glyph_row *cursor_row
7438 = (desired_cursor_row->enabled_p
7439 ? desired_cursor_row
7440 : current_cursor_row);
7442 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7443 &text_area_width, &text_area_height);
7445 /* Scroll when cursor is inside this scroll margin. */
7446 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7448 if ((XFASTINT (w->hscroll)
7449 && w->cursor.x < hscroll_margin)
7450 || (cursor_row->enabled_p
7451 && cursor_row->truncated_on_right_p
7452 && (w->cursor.x > text_area_width - hscroll_margin)))
7454 struct it it;
7455 int hscroll;
7456 struct buffer *saved_current_buffer;
7457 int pt;
7459 /* Find point in a display of infinite width. */
7460 saved_current_buffer = current_buffer;
7461 current_buffer = XBUFFER (w->buffer);
7463 if (w == XWINDOW (selected_window))
7464 pt = BUF_PT (current_buffer);
7465 else
7467 pt = marker_position (w->pointm);
7468 pt = max (BEGV, pt);
7469 pt = min (ZV, pt);
7472 /* Move iterator to pt starting at cursor_row->start in
7473 a line with infinite width. */
7474 init_to_row_start (&it, w, cursor_row);
7475 it.last_visible_x = INFINITY;
7476 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7477 current_buffer = saved_current_buffer;
7479 /* Center cursor in window. */
7480 hscroll = (max (0, it.current_x - text_area_width / 2)
7481 / CANON_X_UNIT (it.f));
7483 /* Don't call Fset_window_hscroll if value hasn't
7484 changed because it will prevent redisplay
7485 optimizations. */
7486 if (XFASTINT (w->hscroll) != hscroll)
7488 Fset_window_hscroll (window, make_number (hscroll));
7489 hscrolled_p = 1;
7494 window = w->next;
7497 /* Value is non-zero if hscroll of any leaf window has been changed. */
7498 return hscrolled_p;
7502 /* Set hscroll so that cursor is visible and not inside horizontal
7503 scroll margins for all windows in the tree rooted at WINDOW. See
7504 also hscroll_window_tree above. Value is non-zero if any window's
7505 hscroll has been changed. If it has, desired matrices on the frame
7506 of WINDOW are cleared. */
7508 static int
7509 hscroll_windows (window)
7510 Lisp_Object window;
7512 int hscrolled_p;
7514 if (automatic_hscrolling_p)
7516 hscrolled_p = hscroll_window_tree (window);
7517 if (hscrolled_p)
7518 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7520 else
7521 hscrolled_p = 0;
7522 return hscrolled_p;
7527 /************************************************************************
7528 Redisplay
7529 ************************************************************************/
7531 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7532 to a non-zero value. This is sometimes handy to have in a debugger
7533 session. */
7535 #if GLYPH_DEBUG
7537 /* First and last unchanged row for try_window_id. */
7539 int debug_first_unchanged_at_end_vpos;
7540 int debug_last_unchanged_at_beg_vpos;
7542 /* Delta vpos and y. */
7544 int debug_dvpos, debug_dy;
7546 /* Delta in characters and bytes for try_window_id. */
7548 int debug_delta, debug_delta_bytes;
7550 /* Values of window_end_pos and window_end_vpos at the end of
7551 try_window_id. */
7553 int debug_end_pos, debug_end_vpos;
7555 /* Append a string to W->desired_matrix->method. FMT is a printf
7556 format string. A1...A9 are a supplement for a variable-length
7557 argument list. If trace_redisplay_p is non-zero also printf the
7558 resulting string to stderr. */
7560 static void
7561 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7562 struct window *w;
7563 char *fmt;
7564 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7566 char buffer[512];
7567 char *method = w->desired_matrix->method;
7568 int len = strlen (method);
7569 int size = sizeof w->desired_matrix->method;
7570 int remaining = size - len - 1;
7572 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7573 if (len && remaining)
7575 method[len] = '|';
7576 --remaining, ++len;
7579 strncpy (method + len, buffer, remaining);
7581 if (trace_redisplay_p)
7582 fprintf (stderr, "%p (%s): %s\n",
7584 ((BUFFERP (w->buffer)
7585 && STRINGP (XBUFFER (w->buffer)->name))
7586 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7587 : "no buffer"),
7588 buffer);
7591 #endif /* GLYPH_DEBUG */
7594 /* This counter is used to clear the face cache every once in a while
7595 in redisplay_internal. It is incremented for each redisplay.
7596 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7597 cleared. */
7599 #define CLEAR_FACE_CACHE_COUNT 10000
7600 static int clear_face_cache_count;
7602 /* Record the previous terminal frame we displayed. */
7604 static struct frame *previous_terminal_frame;
7606 /* Non-zero while redisplay_internal is in progress. */
7608 int redisplaying_p;
7611 /* Value is non-zero if all changes in window W, which displays
7612 current_buffer, are in the text between START and END. START is a
7613 buffer position, END is given as a distance from Z. Used in
7614 redisplay_internal for display optimization. */
7616 static INLINE int
7617 text_outside_line_unchanged_p (w, start, end)
7618 struct window *w;
7619 int start, end;
7621 int unchanged_p = 1;
7623 /* If text or overlays have changed, see where. */
7624 if (XFASTINT (w->last_modified) < MODIFF
7625 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7627 /* Gap in the line? */
7628 if (GPT < start || Z - GPT < end)
7629 unchanged_p = 0;
7631 /* Changes start in front of the line, or end after it? */
7632 if (unchanged_p
7633 && (BEG_UNCHANGED < start - 1
7634 || END_UNCHANGED < end))
7635 unchanged_p = 0;
7637 /* If selective display, can't optimize if changes start at the
7638 beginning of the line. */
7639 if (unchanged_p
7640 && INTEGERP (current_buffer->selective_display)
7641 && XINT (current_buffer->selective_display) > 0
7642 && (BEG_UNCHANGED < start || GPT <= start))
7643 unchanged_p = 0;
7646 return unchanged_p;
7650 /* Do a frame update, taking possible shortcuts into account. This is
7651 the main external entry point for redisplay.
7653 If the last redisplay displayed an echo area message and that message
7654 is no longer requested, we clear the echo area or bring back the
7655 mini-buffer if that is in use. */
7657 void
7658 redisplay ()
7660 redisplay_internal (0);
7663 /* Return 1 if point moved out of or into a composition. Otherwise
7664 return 0. PREV_BUF and PREV_PT are the last point buffer and
7665 position. BUF and PT are the current point buffer and position. */
7668 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7669 struct buffer *prev_buf, *buf;
7670 int prev_pt, pt;
7672 int start, end;
7673 Lisp_Object prop;
7674 Lisp_Object buffer;
7676 XSETBUFFER (buffer, buf);
7677 /* Check a composition at the last point if point moved within the
7678 same buffer. */
7679 if (prev_buf == buf)
7681 if (prev_pt == pt)
7682 /* Point didn't move. */
7683 return 0;
7685 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7686 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7687 && COMPOSITION_VALID_P (start, end, prop)
7688 && start < prev_pt && end > prev_pt)
7689 /* The last point was within the composition. Return 1 iff
7690 point moved out of the composition. */
7691 return (pt <= start || pt >= end);
7694 /* Check a composition at the current point. */
7695 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7696 && find_composition (pt, -1, &start, &end, &prop, buffer)
7697 && COMPOSITION_VALID_P (start, end, prop)
7698 && start < pt && end > pt);
7701 /* Reconsider the setting of B->clip_changed which is displayed
7702 in window W. */
7704 static INLINE void
7705 reconsider_clip_changes (w, b)
7706 struct window *w;
7707 struct buffer *b;
7709 if (b->prevent_redisplay_optimizations_p)
7710 b->clip_changed = 1;
7711 else if (b->clip_changed
7712 && !NILP (w->window_end_valid)
7713 && w->current_matrix->buffer == b
7714 && w->current_matrix->zv == BUF_ZV (b)
7715 && w->current_matrix->begv == BUF_BEGV (b))
7716 b->clip_changed = 0;
7718 /* If display wasn't paused, and W is not a tool bar window, see if
7719 point has been moved into or out of a composition. In that case,
7720 we set b->clip_changed to 1 to force updating the screen. If
7721 b->clip_changed has already been set to 1, we can skip this
7722 check. */
7723 if (!b->clip_changed
7724 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7726 int pt;
7728 if (w == XWINDOW (selected_window))
7729 pt = BUF_PT (current_buffer);
7730 else
7731 pt = marker_position (w->pointm);
7733 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7734 || pt != XINT (w->last_point))
7735 && check_point_in_composition (w->current_matrix->buffer,
7736 XINT (w->last_point),
7737 XBUFFER (w->buffer), pt))
7738 b->clip_changed = 1;
7743 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7744 response to any user action; therefore, we should preserve the echo
7745 area. (Actually, our caller does that job.) Perhaps in the future
7746 avoid recentering windows if it is not necessary; currently that
7747 causes some problems. */
7749 static void
7750 redisplay_internal (preserve_echo_area)
7751 int preserve_echo_area;
7753 struct window *w = XWINDOW (selected_window);
7754 struct frame *f = XFRAME (w->frame);
7755 int pause;
7756 int must_finish = 0;
7757 struct text_pos tlbufpos, tlendpos;
7758 int number_of_visible_frames;
7759 int count;
7760 struct frame *sf = SELECTED_FRAME ();
7762 /* Non-zero means redisplay has to consider all windows on all
7763 frames. Zero means, only selected_window is considered. */
7764 int consider_all_windows_p;
7766 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7768 /* No redisplay if running in batch mode or frame is not yet fully
7769 initialized, or redisplay is explicitly turned off by setting
7770 Vinhibit_redisplay. */
7771 if (noninteractive
7772 || !NILP (Vinhibit_redisplay)
7773 || !f->glyphs_initialized_p)
7774 return;
7776 /* The flag redisplay_performed_directly_p is set by
7777 direct_output_for_insert when it already did the whole screen
7778 update necessary. */
7779 if (redisplay_performed_directly_p)
7781 redisplay_performed_directly_p = 0;
7782 if (!hscroll_windows (selected_window))
7783 return;
7786 #ifdef USE_X_TOOLKIT
7787 if (popup_activated ())
7788 return;
7789 #endif
7791 /* I don't think this happens but let's be paranoid. */
7792 if (redisplaying_p)
7793 return;
7795 /* Record a function that resets redisplaying_p to its old value
7796 when we leave this function. */
7797 count = BINDING_STACK_SIZE ();
7798 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7799 ++redisplaying_p;
7801 retry:
7802 pause = 0;
7803 reconsider_clip_changes (w, current_buffer);
7805 /* If new fonts have been loaded that make a glyph matrix adjustment
7806 necessary, do it. */
7807 if (fonts_changed_p)
7809 adjust_glyphs (NULL);
7810 ++windows_or_buffers_changed;
7811 fonts_changed_p = 0;
7814 if (! FRAME_WINDOW_P (sf)
7815 && previous_terminal_frame != sf)
7817 /* Since frames on an ASCII terminal share the same display
7818 area, displaying a different frame means redisplay the whole
7819 thing. */
7820 windows_or_buffers_changed++;
7821 SET_FRAME_GARBAGED (sf);
7822 XSETFRAME (Vterminal_frame, sf);
7824 previous_terminal_frame = sf;
7826 /* Set the visible flags for all frames. Do this before checking
7827 for resized or garbaged frames; they want to know if their frames
7828 are visible. See the comment in frame.h for
7829 FRAME_SAMPLE_VISIBILITY. */
7831 Lisp_Object tail, frame;
7833 number_of_visible_frames = 0;
7835 FOR_EACH_FRAME (tail, frame)
7837 struct frame *f = XFRAME (frame);
7839 FRAME_SAMPLE_VISIBILITY (f);
7840 if (FRAME_VISIBLE_P (f))
7841 ++number_of_visible_frames;
7842 clear_desired_matrices (f);
7846 /* Notice any pending interrupt request to change frame size. */
7847 do_pending_window_change (1);
7849 /* Clear frames marked as garbaged. */
7850 if (frame_garbaged)
7851 clear_garbaged_frames ();
7853 /* Build menubar and tool-bar items. */
7854 prepare_menu_bars ();
7856 if (windows_or_buffers_changed)
7857 update_mode_lines++;
7859 /* Detect case that we need to write or remove a star in the mode line. */
7860 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7862 w->update_mode_line = Qt;
7863 if (buffer_shared > 1)
7864 update_mode_lines++;
7867 /* If %c is in the mode line, update it if needed. */
7868 if (!NILP (w->column_number_displayed)
7869 /* This alternative quickly identifies a common case
7870 where no change is needed. */
7871 && !(PT == XFASTINT (w->last_point)
7872 && XFASTINT (w->last_modified) >= MODIFF
7873 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7874 && XFASTINT (w->column_number_displayed) != current_column ())
7875 w->update_mode_line = Qt;
7877 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7879 /* The variable buffer_shared is set in redisplay_window and
7880 indicates that we redisplay a buffer in different windows. See
7881 there. */
7882 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7884 /* If specs for an arrow have changed, do thorough redisplay
7885 to ensure we remove any arrow that should no longer exist. */
7886 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7887 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7888 consider_all_windows_p = windows_or_buffers_changed = 1;
7890 /* Normally the message* functions will have already displayed and
7891 updated the echo area, but the frame may have been trashed, or
7892 the update may have been preempted, so display the echo area
7893 again here. Checking both message buffers captures the case that
7894 the echo area should be cleared. */
7895 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7897 int window_height_changed_p = echo_area_display (0);
7898 must_finish = 1;
7900 if (fonts_changed_p)
7901 goto retry;
7902 else if (window_height_changed_p)
7904 consider_all_windows_p = 1;
7905 ++update_mode_lines;
7906 ++windows_or_buffers_changed;
7908 /* If window configuration was changed, frames may have been
7909 marked garbaged. Clear them or we will experience
7910 surprises wrt scrolling. */
7911 if (frame_garbaged)
7912 clear_garbaged_frames ();
7915 else if (EQ (selected_window, minibuf_window)
7916 && (current_buffer->clip_changed
7917 || XFASTINT (w->last_modified) < MODIFF
7918 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7919 && resize_mini_window (w, 0))
7921 /* Resized active mini-window to fit the size of what it is
7922 showing if its contents might have changed. */
7923 must_finish = 1;
7924 consider_all_windows_p = 1;
7925 ++windows_or_buffers_changed;
7926 ++update_mode_lines;
7928 /* If window configuration was changed, frames may have been
7929 marked garbaged. Clear them or we will experience
7930 surprises wrt scrolling. */
7931 if (frame_garbaged)
7932 clear_garbaged_frames ();
7936 /* If showing the region, and mark has changed, we must redisplay
7937 the whole window. The assignment to this_line_start_pos prevents
7938 the optimization directly below this if-statement. */
7939 if (((!NILP (Vtransient_mark_mode)
7940 && !NILP (XBUFFER (w->buffer)->mark_active))
7941 != !NILP (w->region_showing))
7942 || (!NILP (w->region_showing)
7943 && !EQ (w->region_showing,
7944 Fmarker_position (XBUFFER (w->buffer)->mark))))
7945 CHARPOS (this_line_start_pos) = 0;
7947 /* Optimize the case that only the line containing the cursor in the
7948 selected window has changed. Variables starting with this_ are
7949 set in display_line and record information about the line
7950 containing the cursor. */
7951 tlbufpos = this_line_start_pos;
7952 tlendpos = this_line_end_pos;
7953 if (!consider_all_windows_p
7954 && CHARPOS (tlbufpos) > 0
7955 && NILP (w->update_mode_line)
7956 && !current_buffer->clip_changed
7957 && FRAME_VISIBLE_P (XFRAME (w->frame))
7958 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7959 /* Make sure recorded data applies to current buffer, etc. */
7960 && this_line_buffer == current_buffer
7961 && current_buffer == XBUFFER (w->buffer)
7962 && NILP (w->force_start)
7963 /* Point must be on the line that we have info recorded about. */
7964 && PT >= CHARPOS (tlbufpos)
7965 && PT <= Z - CHARPOS (tlendpos)
7966 /* All text outside that line, including its final newline,
7967 must be unchanged */
7968 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7969 CHARPOS (tlendpos)))
7971 if (CHARPOS (tlbufpos) > BEGV
7972 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7973 && (CHARPOS (tlbufpos) == ZV
7974 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7975 /* Former continuation line has disappeared by becoming empty */
7976 goto cancel;
7977 else if (XFASTINT (w->last_modified) < MODIFF
7978 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7979 || MINI_WINDOW_P (w))
7981 /* We have to handle the case of continuation around a
7982 wide-column character (See the comment in indent.c around
7983 line 885).
7985 For instance, in the following case:
7987 -------- Insert --------
7988 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7989 J_I_ ==> J_I_ `^^' are cursors.
7990 ^^ ^^
7991 -------- --------
7993 As we have to redraw the line above, we should goto cancel. */
7995 struct it it;
7996 int line_height_before = this_line_pixel_height;
7998 /* Note that start_display will handle the case that the
7999 line starting at tlbufpos is a continuation lines. */
8000 start_display (&it, w, tlbufpos);
8002 /* Implementation note: It this still necessary? */
8003 if (it.current_x != this_line_start_x)
8004 goto cancel;
8006 TRACE ((stderr, "trying display optimization 1\n"));
8007 w->cursor.vpos = -1;
8008 overlay_arrow_seen = 0;
8009 it.vpos = this_line_vpos;
8010 it.current_y = this_line_y;
8011 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8012 display_line (&it);
8014 /* If line contains point, is not continued,
8015 and ends at same distance from eob as before, we win */
8016 if (w->cursor.vpos >= 0
8017 /* Line is not continued, otherwise this_line_start_pos
8018 would have been set to 0 in display_line. */
8019 && CHARPOS (this_line_start_pos)
8020 /* Line ends as before. */
8021 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8022 /* Line has same height as before. Otherwise other lines
8023 would have to be shifted up or down. */
8024 && this_line_pixel_height == line_height_before)
8026 /* If this is not the window's last line, we must adjust
8027 the charstarts of the lines below. */
8028 if (it.current_y < it.last_visible_y)
8030 struct glyph_row *row
8031 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8032 int delta, delta_bytes;
8034 if (Z - CHARPOS (tlendpos) == ZV)
8036 /* This line ends at end of (accessible part of)
8037 buffer. There is no newline to count. */
8038 delta = (Z
8039 - CHARPOS (tlendpos)
8040 - MATRIX_ROW_START_CHARPOS (row));
8041 delta_bytes = (Z_BYTE
8042 - BYTEPOS (tlendpos)
8043 - MATRIX_ROW_START_BYTEPOS (row));
8045 else
8047 /* This line ends in a newline. Must take
8048 account of the newline and the rest of the
8049 text that follows. */
8050 delta = (Z
8051 - CHARPOS (tlendpos)
8052 - MATRIX_ROW_START_CHARPOS (row));
8053 delta_bytes = (Z_BYTE
8054 - BYTEPOS (tlendpos)
8055 - MATRIX_ROW_START_BYTEPOS (row));
8058 increment_matrix_positions (w->current_matrix,
8059 this_line_vpos + 1,
8060 w->current_matrix->nrows,
8061 delta, delta_bytes);
8064 /* If this row displays text now but previously didn't,
8065 or vice versa, w->window_end_vpos may have to be
8066 adjusted. */
8067 if ((it.glyph_row - 1)->displays_text_p)
8069 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8070 XSETINT (w->window_end_vpos, this_line_vpos);
8072 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8073 && this_line_vpos > 0)
8074 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8075 w->window_end_valid = Qnil;
8077 /* Update hint: No need to try to scroll in update_window. */
8078 w->desired_matrix->no_scrolling_p = 1;
8080 #if GLYPH_DEBUG
8081 *w->desired_matrix->method = 0;
8082 debug_method_add (w, "optimization 1");
8083 #endif
8084 goto update;
8086 else
8087 goto cancel;
8089 else if (/* Cursor position hasn't changed. */
8090 PT == XFASTINT (w->last_point)
8091 /* Make sure the cursor was last displayed
8092 in this window. Otherwise we have to reposition it. */
8093 && 0 <= w->cursor.vpos
8094 && XINT (w->height) > w->cursor.vpos)
8096 if (!must_finish)
8098 do_pending_window_change (1);
8100 /* We used to always goto end_of_redisplay here, but this
8101 isn't enough if we have a blinking cursor. */
8102 if (w->cursor_off_p == w->last_cursor_off_p)
8103 goto end_of_redisplay;
8105 goto update;
8107 /* If highlighting the region, or if the cursor is in the echo area,
8108 then we can't just move the cursor. */
8109 else if (! (!NILP (Vtransient_mark_mode)
8110 && !NILP (current_buffer->mark_active))
8111 && (EQ (selected_window, current_buffer->last_selected_window)
8112 || highlight_nonselected_windows)
8113 && NILP (w->region_showing)
8114 && NILP (Vshow_trailing_whitespace)
8115 && !cursor_in_echo_area)
8117 struct it it;
8118 struct glyph_row *row;
8120 /* Skip from tlbufpos to PT and see where it is. Note that
8121 PT may be in invisible text. If so, we will end at the
8122 next visible position. */
8123 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8124 NULL, DEFAULT_FACE_ID);
8125 it.current_x = this_line_start_x;
8126 it.current_y = this_line_y;
8127 it.vpos = this_line_vpos;
8129 /* The call to move_it_to stops in front of PT, but
8130 moves over before-strings. */
8131 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8133 if (it.vpos == this_line_vpos
8134 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8135 row->enabled_p))
8137 xassert (this_line_vpos == it.vpos);
8138 xassert (this_line_y == it.current_y);
8139 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8140 goto update;
8142 else
8143 goto cancel;
8146 cancel:
8147 /* Text changed drastically or point moved off of line. */
8148 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8151 CHARPOS (this_line_start_pos) = 0;
8152 consider_all_windows_p |= buffer_shared > 1;
8153 ++clear_face_cache_count;
8156 /* Build desired matrices, and update the display. If
8157 consider_all_windows_p is non-zero, do it for all windows on all
8158 frames. Otherwise do it for selected_window, only. */
8160 if (consider_all_windows_p)
8162 Lisp_Object tail, frame;
8164 /* Clear the face cache eventually. */
8165 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8167 clear_face_cache (0);
8168 clear_face_cache_count = 0;
8171 /* Recompute # windows showing selected buffer. This will be
8172 incremented each time such a window is displayed. */
8173 buffer_shared = 0;
8175 FOR_EACH_FRAME (tail, frame)
8177 struct frame *f = XFRAME (frame);
8179 if (FRAME_WINDOW_P (f) || f == sf)
8181 /* Mark all the scroll bars to be removed; we'll redeem
8182 the ones we want when we redisplay their windows. */
8183 if (condemn_scroll_bars_hook)
8184 (*condemn_scroll_bars_hook) (f);
8186 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8187 redisplay_windows (FRAME_ROOT_WINDOW (f));
8189 /* Any scroll bars which redisplay_windows should have
8190 nuked should now go away. */
8191 if (judge_scroll_bars_hook)
8192 (*judge_scroll_bars_hook) (f);
8194 /* If fonts changed, display again. */
8195 if (fonts_changed_p)
8196 goto retry;
8198 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8200 /* See if we have to hscroll. */
8201 if (hscroll_windows (f->root_window))
8202 goto retry;
8204 /* Prevent various kinds of signals during display
8205 update. stdio is not robust about handling
8206 signals, which can cause an apparent I/O
8207 error. */
8208 if (interrupt_input)
8209 unrequest_sigio ();
8210 stop_polling ();
8212 /* Update the display. */
8213 set_window_update_flags (XWINDOW (f->root_window), 1);
8214 pause |= update_frame (f, 0, 0);
8215 if (pause)
8216 break;
8218 mark_window_display_accurate (f->root_window, 1);
8219 if (frame_up_to_date_hook)
8220 frame_up_to_date_hook (f);
8225 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8227 Lisp_Object mini_window;
8228 struct frame *mini_frame;
8230 redisplay_window (selected_window, 1);
8232 /* Compare desired and current matrices, perform output. */
8233 update:
8235 /* If fonts changed, display again. */
8236 if (fonts_changed_p)
8237 goto retry;
8239 /* Prevent various kinds of signals during display update.
8240 stdio is not robust about handling signals,
8241 which can cause an apparent I/O error. */
8242 if (interrupt_input)
8243 unrequest_sigio ();
8244 stop_polling ();
8246 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8248 if (hscroll_windows (selected_window))
8249 goto retry;
8251 XWINDOW (selected_window)->must_be_updated_p = 1;
8252 pause = update_frame (sf, 0, 0);
8255 /* We may have called echo_area_display at the top of this
8256 function. If the echo area is on another frame, that may
8257 have put text on a frame other than the selected one, so the
8258 above call to update_frame would not have caught it. Catch
8259 it here. */
8260 mini_window = FRAME_MINIBUF_WINDOW (sf);
8261 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8263 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8265 XWINDOW (mini_window)->must_be_updated_p = 1;
8266 pause |= update_frame (mini_frame, 0, 0);
8267 if (!pause && hscroll_windows (mini_window))
8268 goto retry;
8272 /* If display was paused because of pending input, make sure we do a
8273 thorough update the next time. */
8274 if (pause)
8276 /* Prevent the optimization at the beginning of
8277 redisplay_internal that tries a single-line update of the
8278 line containing the cursor in the selected window. */
8279 CHARPOS (this_line_start_pos) = 0;
8281 /* Let the overlay arrow be updated the next time. */
8282 if (!NILP (last_arrow_position))
8284 last_arrow_position = Qt;
8285 last_arrow_string = Qt;
8288 /* If we pause after scrolling, some rows in the current
8289 matrices of some windows are not valid. */
8290 if (!WINDOW_FULL_WIDTH_P (w)
8291 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8292 update_mode_lines = 1;
8295 /* Now text on frame agrees with windows, so put info into the
8296 windows for partial redisplay to follow. */
8297 if (!pause)
8299 register struct buffer *b = XBUFFER (w->buffer);
8301 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8302 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8303 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8304 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8306 if (consider_all_windows_p)
8307 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8308 else
8310 XSETFASTINT (w->last_point, BUF_PT (b));
8311 w->last_cursor = w->cursor;
8312 w->last_cursor_off_p = w->cursor_off_p;
8314 b->clip_changed = 0;
8315 b->prevent_redisplay_optimizations_p = 0;
8316 w->update_mode_line = Qnil;
8317 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8318 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8319 w->last_had_star
8320 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8321 ? Qt : Qnil);
8323 /* Record if we are showing a region, so can make sure to
8324 update it fully at next redisplay. */
8325 w->region_showing = (!NILP (Vtransient_mark_mode)
8326 && (EQ (selected_window,
8327 current_buffer->last_selected_window)
8328 || highlight_nonselected_windows)
8329 && !NILP (XBUFFER (w->buffer)->mark_active)
8330 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8331 : Qnil);
8333 w->window_end_valid = w->buffer;
8334 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8335 last_arrow_string = Voverlay_arrow_string;
8336 if (frame_up_to_date_hook != 0)
8337 (*frame_up_to_date_hook) (sf);
8339 w->current_matrix->buffer = b;
8340 w->current_matrix->begv = BUF_BEGV (b);
8341 w->current_matrix->zv = BUF_ZV (b);
8344 update_mode_lines = 0;
8345 windows_or_buffers_changed = 0;
8348 /* Start SIGIO interrupts coming again. Having them off during the
8349 code above makes it less likely one will discard output, but not
8350 impossible, since there might be stuff in the system buffer here.
8351 But it is much hairier to try to do anything about that. */
8352 if (interrupt_input)
8353 request_sigio ();
8354 start_polling ();
8356 /* If a frame has become visible which was not before, redisplay
8357 again, so that we display it. Expose events for such a frame
8358 (which it gets when becoming visible) don't call the parts of
8359 redisplay constructing glyphs, so simply exposing a frame won't
8360 display anything in this case. So, we have to display these
8361 frames here explicitly. */
8362 if (!pause)
8364 Lisp_Object tail, frame;
8365 int new_count = 0;
8367 FOR_EACH_FRAME (tail, frame)
8369 int this_is_visible = 0;
8371 if (XFRAME (frame)->visible)
8372 this_is_visible = 1;
8373 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8374 if (XFRAME (frame)->visible)
8375 this_is_visible = 1;
8377 if (this_is_visible)
8378 new_count++;
8381 if (new_count != number_of_visible_frames)
8382 windows_or_buffers_changed++;
8385 /* Change frame size now if a change is pending. */
8386 do_pending_window_change (1);
8388 /* If we just did a pending size change, or have additional
8389 visible frames, redisplay again. */
8390 if (windows_or_buffers_changed && !pause)
8391 goto retry;
8393 end_of_redisplay:;
8395 unbind_to (count, Qnil);
8399 /* Redisplay, but leave alone any recent echo area message unless
8400 another message has been requested in its place.
8402 This is useful in situations where you need to redisplay but no
8403 user action has occurred, making it inappropriate for the message
8404 area to be cleared. See tracking_off and
8405 wait_reading_process_input for examples of these situations. */
8407 void
8408 redisplay_preserve_echo_area ()
8410 if (!NILP (echo_area_buffer[1]))
8412 /* We have a previously displayed message, but no current
8413 message. Redisplay the previous message. */
8414 display_last_displayed_message_p = 1;
8415 redisplay_internal (1);
8416 display_last_displayed_message_p = 0;
8418 else
8419 redisplay_internal (1);
8423 /* Function registered with record_unwind_protect in
8424 redisplay_internal. Clears the flag indicating that a redisplay is
8425 in progress. */
8427 static Lisp_Object
8428 unwind_redisplay (old_redisplaying_p)
8429 Lisp_Object old_redisplaying_p;
8431 redisplaying_p = XFASTINT (old_redisplaying_p);
8432 return Qnil;
8436 /* Mark the display of windows in the window tree rooted at WINDOW as
8437 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8438 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8439 the next time redisplay_internal is called. */
8441 void
8442 mark_window_display_accurate (window, accurate_p)
8443 Lisp_Object window;
8444 int accurate_p;
8446 struct window *w;
8448 for (; !NILP (window); window = w->next)
8450 w = XWINDOW (window);
8452 if (BUFFERP (w->buffer))
8454 struct buffer *b = XBUFFER (w->buffer);
8456 XSETFASTINT (w->last_modified,
8457 accurate_p ? BUF_MODIFF (b) : 0);
8458 XSETFASTINT (w->last_overlay_modified,
8459 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8460 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8461 ? Qt : Qnil);
8463 #if 0 /* I don't think this is necessary because display_line does it.
8464 Let's check it. */
8465 /* Record if we are showing a region, so can make sure to
8466 update it fully at next redisplay. */
8467 w->region_showing
8468 = (!NILP (Vtransient_mark_mode)
8469 && (w == XWINDOW (current_buffer->last_selected_window)
8470 || highlight_nonselected_windows)
8471 && (!NILP (b->mark_active)
8472 ? Fmarker_position (b->mark)
8473 : Qnil));
8474 #endif
8476 if (accurate_p)
8478 b->clip_changed = 0;
8479 b->prevent_redisplay_optimizations_p = 0;
8480 w->current_matrix->buffer = b;
8481 w->current_matrix->begv = BUF_BEGV (b);
8482 w->current_matrix->zv = BUF_ZV (b);
8483 w->last_cursor = w->cursor;
8484 w->last_cursor_off_p = w->cursor_off_p;
8485 if (w == XWINDOW (selected_window))
8486 w->last_point = make_number (BUF_PT (b));
8487 else
8488 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8492 w->window_end_valid = w->buffer;
8493 w->update_mode_line = Qnil;
8495 if (!NILP (w->vchild))
8496 mark_window_display_accurate (w->vchild, accurate_p);
8497 if (!NILP (w->hchild))
8498 mark_window_display_accurate (w->hchild, accurate_p);
8501 if (accurate_p)
8503 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8504 last_arrow_string = Voverlay_arrow_string;
8506 else
8508 /* Force a thorough redisplay the next time by setting
8509 last_arrow_position and last_arrow_string to t, which is
8510 unequal to any useful value of Voverlay_arrow_... */
8511 last_arrow_position = Qt;
8512 last_arrow_string = Qt;
8517 /* Return value in display table DP (Lisp_Char_Table *) for character
8518 C. Since a display table doesn't have any parent, we don't have to
8519 follow parent. Do not call this function directly but use the
8520 macro DISP_CHAR_VECTOR. */
8522 Lisp_Object
8523 disp_char_vector (dp, c)
8524 struct Lisp_Char_Table *dp;
8525 int c;
8527 int code[4], i;
8528 Lisp_Object val;
8530 if (SINGLE_BYTE_CHAR_P (c))
8531 return (dp->contents[c]);
8533 SPLIT_CHAR (c, code[0], code[1], code[2]);
8534 if (code[1] < 32)
8535 code[1] = -1;
8536 else if (code[2] < 32)
8537 code[2] = -1;
8539 /* Here, the possible range of code[0] (== charset ID) is
8540 128..max_charset. Since the top level char table contains data
8541 for multibyte characters after 256th element, we must increment
8542 code[0] by 128 to get a correct index. */
8543 code[0] += 128;
8544 code[3] = -1; /* anchor */
8546 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8548 val = dp->contents[code[i]];
8549 if (!SUB_CHAR_TABLE_P (val))
8550 return (NILP (val) ? dp->defalt : val);
8553 /* Here, val is a sub char table. We return the default value of
8554 it. */
8555 return (dp->defalt);
8560 /***********************************************************************
8561 Window Redisplay
8562 ***********************************************************************/
8564 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8566 static void
8567 redisplay_windows (window)
8568 Lisp_Object window;
8570 while (!NILP (window))
8572 struct window *w = XWINDOW (window);
8574 if (!NILP (w->hchild))
8575 redisplay_windows (w->hchild);
8576 else if (!NILP (w->vchild))
8577 redisplay_windows (w->vchild);
8578 else
8579 redisplay_window (window, 0);
8581 window = w->next;
8586 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8587 DELTA is the number of bytes by which positions recorded in ROW
8588 differ from current buffer positions. */
8590 void
8591 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8592 struct window *w;
8593 struct glyph_row *row;
8594 struct glyph_matrix *matrix;
8595 int delta, delta_bytes, dy, dvpos;
8597 struct glyph *glyph = row->glyphs[TEXT_AREA];
8598 struct glyph *end = glyph + row->used[TEXT_AREA];
8599 int x = row->x;
8600 int pt_old = PT - delta;
8602 /* Skip over glyphs not having an object at the start of the row.
8603 These are special glyphs like truncation marks on terminal
8604 frames. */
8605 if (row->displays_text_p)
8606 while (glyph < end
8607 && INTEGERP (glyph->object)
8608 && glyph->charpos < 0)
8610 x += glyph->pixel_width;
8611 ++glyph;
8614 while (glyph < end
8615 && !INTEGERP (glyph->object)
8616 && (!BUFFERP (glyph->object)
8617 || glyph->charpos < pt_old))
8619 x += glyph->pixel_width;
8620 ++glyph;
8623 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8624 w->cursor.x = x;
8625 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8626 w->cursor.y = row->y + dy;
8628 if (w == XWINDOW (selected_window))
8630 if (!row->continued_p
8631 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8632 && row->x == 0)
8634 this_line_buffer = XBUFFER (w->buffer);
8636 CHARPOS (this_line_start_pos)
8637 = MATRIX_ROW_START_CHARPOS (row) + delta;
8638 BYTEPOS (this_line_start_pos)
8639 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8641 CHARPOS (this_line_end_pos)
8642 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8643 BYTEPOS (this_line_end_pos)
8644 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8646 this_line_y = w->cursor.y;
8647 this_line_pixel_height = row->height;
8648 this_line_vpos = w->cursor.vpos;
8649 this_line_start_x = row->x;
8651 else
8652 CHARPOS (this_line_start_pos) = 0;
8657 /* Run window scroll functions, if any, for WINDOW with new window
8658 start STARTP. Sets the window start of WINDOW to that position.
8660 We assume that the window's buffer is really current. */
8662 static INLINE struct text_pos
8663 run_window_scroll_functions (window, startp)
8664 Lisp_Object window;
8665 struct text_pos startp;
8667 struct window *w = XWINDOW (window);
8668 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8670 if (current_buffer != XBUFFER (w->buffer))
8671 abort ();
8673 if (!NILP (Vwindow_scroll_functions))
8675 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8676 make_number (CHARPOS (startp)));
8677 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8678 /* In case the hook functions switch buffers. */
8679 if (current_buffer != XBUFFER (w->buffer))
8680 set_buffer_internal_1 (XBUFFER (w->buffer));
8683 return startp;
8687 /* Modify the desired matrix of window W and W->vscroll so that the
8688 line containing the cursor is fully visible. */
8690 static void
8691 make_cursor_line_fully_visible (w)
8692 struct window *w;
8694 struct glyph_matrix *matrix;
8695 struct glyph_row *row;
8696 int window_height, header_line_height;
8698 /* It's not always possible to find the cursor, e.g, when a window
8699 is full of overlay strings. Don't do anything in that case. */
8700 if (w->cursor.vpos < 0)
8701 return;
8703 matrix = w->desired_matrix;
8704 row = MATRIX_ROW (matrix, w->cursor.vpos);
8706 /* If the cursor row is not partially visible, there's nothing
8707 to do. */
8708 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8709 return;
8711 /* If the row the cursor is in is taller than the window's height,
8712 it's not clear what to do, so do nothing. */
8713 window_height = window_box_height (w);
8714 if (row->height >= window_height)
8715 return;
8717 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8719 int dy = row->height - row->visible_height;
8720 w->vscroll = 0;
8721 w->cursor.y += dy;
8722 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8724 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8726 int dy = - (row->height - row->visible_height);
8727 w->vscroll = dy;
8728 w->cursor.y += dy;
8729 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8732 /* When we change the cursor y-position of the selected window,
8733 change this_line_y as well so that the display optimization for
8734 the cursor line of the selected window in redisplay_internal uses
8735 the correct y-position. */
8736 if (w == XWINDOW (selected_window))
8737 this_line_y = w->cursor.y;
8741 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8742 non-zero means only WINDOW is redisplayed in redisplay_internal.
8743 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8744 in redisplay_window to bring a partially visible line into view in
8745 the case that only the cursor has moved.
8747 Value is
8749 1 if scrolling succeeded
8751 0 if scrolling didn't find point.
8753 -1 if new fonts have been loaded so that we must interrupt
8754 redisplay, adjust glyph matrices, and try again. */
8756 static int
8757 try_scrolling (window, just_this_one_p, scroll_conservatively,
8758 scroll_step, temp_scroll_step)
8759 Lisp_Object window;
8760 int just_this_one_p;
8761 int scroll_conservatively, scroll_step;
8762 int temp_scroll_step;
8764 struct window *w = XWINDOW (window);
8765 struct frame *f = XFRAME (w->frame);
8766 struct text_pos scroll_margin_pos;
8767 struct text_pos pos;
8768 struct text_pos startp;
8769 struct it it;
8770 Lisp_Object window_end;
8771 int this_scroll_margin;
8772 int dy = 0;
8773 int scroll_max;
8774 int rc;
8775 int amount_to_scroll = 0;
8776 Lisp_Object aggressive;
8777 int height;
8779 #if GLYPH_DEBUG
8780 debug_method_add (w, "try_scrolling");
8781 #endif
8783 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8785 /* Compute scroll margin height in pixels. We scroll when point is
8786 within this distance from the top or bottom of the window. */
8787 if (scroll_margin > 0)
8789 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8790 this_scroll_margin *= CANON_Y_UNIT (f);
8792 else
8793 this_scroll_margin = 0;
8795 /* Compute how much we should try to scroll maximally to bring point
8796 into view. */
8797 if (scroll_step || scroll_conservatively || temp_scroll_step)
8798 scroll_max = max (scroll_step,
8799 max (scroll_conservatively, temp_scroll_step));
8800 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8801 || NUMBERP (current_buffer->scroll_up_aggressively))
8802 /* We're trying to scroll because of aggressive scrolling
8803 but no scroll_step is set. Choose an arbitrary one. Maybe
8804 there should be a variable for this. */
8805 scroll_max = 10;
8806 else
8807 scroll_max = 0;
8808 scroll_max *= CANON_Y_UNIT (f);
8810 /* Decide whether we have to scroll down. Start at the window end
8811 and move this_scroll_margin up to find the position of the scroll
8812 margin. */
8813 window_end = Fwindow_end (window, Qt);
8814 CHARPOS (scroll_margin_pos) = XINT (window_end);
8815 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8816 if (this_scroll_margin)
8818 start_display (&it, w, scroll_margin_pos);
8819 move_it_vertically (&it, - this_scroll_margin);
8820 scroll_margin_pos = it.current.pos;
8823 if (PT >= CHARPOS (scroll_margin_pos))
8825 int y0;
8826 #if 0
8827 int line_height;
8828 #endif
8830 /* Point is in the scroll margin at the bottom of the window, or
8831 below. Compute a new window start that makes point visible. */
8833 /* Compute the distance from the scroll margin to PT.
8834 Give up if the distance is greater than scroll_max. */
8835 start_display (&it, w, scroll_margin_pos);
8836 y0 = it.current_y;
8837 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8838 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8839 #if 0 /* Taking the line's height into account here looks wrong. */
8840 line_height = (it.max_ascent + it.max_descent
8841 ? it.max_ascent + it.max_descent
8842 : last_height);
8843 dy = it.current_y + line_height - y0;
8844 #else
8845 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8846 end, which is one line below the window. The iterator's
8847 current_y will be same as y0 in that case, but we have to
8848 scroll a line to make PT visible. That's the reason why 1 is
8849 added below. */
8850 dy = 1 + it.current_y - y0;
8851 #endif
8853 if (dy > scroll_max)
8854 return 0;
8856 /* Move the window start down. If scrolling conservatively,
8857 move it just enough down to make point visible. If
8858 scroll_step is set, move it down by scroll_step. */
8859 start_display (&it, w, startp);
8861 if (scroll_conservatively)
8862 amount_to_scroll =
8863 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8864 else if (scroll_step || temp_scroll_step)
8865 amount_to_scroll = scroll_max;
8866 else
8868 aggressive = current_buffer->scroll_down_aggressively;
8869 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8870 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8871 if (NUMBERP (aggressive))
8872 amount_to_scroll = XFLOATINT (aggressive) * height;
8875 if (amount_to_scroll <= 0)
8876 return 0;
8878 move_it_vertically (&it, amount_to_scroll);
8879 startp = it.current.pos;
8881 else
8883 /* See if point is inside the scroll margin at the top of the
8884 window. */
8885 scroll_margin_pos = startp;
8886 if (this_scroll_margin)
8888 start_display (&it, w, startp);
8889 move_it_vertically (&it, this_scroll_margin);
8890 scroll_margin_pos = it.current.pos;
8893 if (PT < CHARPOS (scroll_margin_pos))
8895 /* Point is in the scroll margin at the top of the window or
8896 above what is displayed in the window. */
8897 int y0;
8899 /* Compute the vertical distance from PT to the scroll
8900 margin position. Give up if distance is greater than
8901 scroll_max. */
8902 SET_TEXT_POS (pos, PT, PT_BYTE);
8903 start_display (&it, w, pos);
8904 y0 = it.current_y;
8905 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8906 it.last_visible_y, -1,
8907 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8908 dy = it.current_y - y0;
8909 if (dy > scroll_max)
8910 return 0;
8912 /* Compute new window start. */
8913 start_display (&it, w, startp);
8915 if (scroll_conservatively)
8916 amount_to_scroll =
8917 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8918 else if (scroll_step || temp_scroll_step)
8919 amount_to_scroll = scroll_max;
8920 else
8922 aggressive = current_buffer->scroll_up_aggressively;
8923 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8924 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8925 if (NUMBERP (aggressive))
8926 amount_to_scroll = XFLOATINT (aggressive) * height;
8929 if (amount_to_scroll <= 0)
8930 return 0;
8932 move_it_vertically (&it, - amount_to_scroll);
8933 startp = it.current.pos;
8937 /* Run window scroll functions. */
8938 startp = run_window_scroll_functions (window, startp);
8940 /* Display the window. Give up if new fonts are loaded, or if point
8941 doesn't appear. */
8942 if (!try_window (window, startp))
8943 rc = -1;
8944 else if (w->cursor.vpos < 0)
8946 clear_glyph_matrix (w->desired_matrix);
8947 rc = 0;
8949 else
8951 /* Maybe forget recorded base line for line number display. */
8952 if (!just_this_one_p
8953 || current_buffer->clip_changed
8954 || BEG_UNCHANGED < CHARPOS (startp))
8955 w->base_line_number = Qnil;
8957 /* If cursor ends up on a partially visible line, shift display
8958 lines up or down. */
8959 make_cursor_line_fully_visible (w);
8960 rc = 1;
8963 return rc;
8967 /* Compute a suitable window start for window W if display of W starts
8968 on a continuation line. Value is non-zero if a new window start
8969 was computed.
8971 The new window start will be computed, based on W's width, starting
8972 from the start of the continued line. It is the start of the
8973 screen line with the minimum distance from the old start W->start. */
8975 static int
8976 compute_window_start_on_continuation_line (w)
8977 struct window *w;
8979 struct text_pos pos, start_pos;
8980 int window_start_changed_p = 0;
8982 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8984 /* If window start is on a continuation line... Window start may be
8985 < BEGV in case there's invisible text at the start of the
8986 buffer (M-x rmail, for example). */
8987 if (CHARPOS (start_pos) > BEGV
8988 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8990 struct it it;
8991 struct glyph_row *row;
8993 /* Handle the case that the window start is out of range. */
8994 if (CHARPOS (start_pos) < BEGV)
8995 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8996 else if (CHARPOS (start_pos) > ZV)
8997 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8999 /* Find the start of the continued line. This should be fast
9000 because scan_buffer is fast (newline cache). */
9001 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9002 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9003 row, DEFAULT_FACE_ID);
9004 reseat_at_previous_visible_line_start (&it);
9006 /* If the line start is "too far" away from the window start,
9007 say it takes too much time to compute a new window start. */
9008 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9009 < XFASTINT (w->height) * XFASTINT (w->width))
9011 int min_distance, distance;
9013 /* Move forward by display lines to find the new window
9014 start. If window width was enlarged, the new start can
9015 be expected to be > the old start. If window width was
9016 decreased, the new window start will be < the old start.
9017 So, we're looking for the display line start with the
9018 minimum distance from the old window start. */
9019 pos = it.current.pos;
9020 min_distance = INFINITY;
9021 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9022 distance < min_distance)
9024 min_distance = distance;
9025 pos = it.current.pos;
9026 move_it_by_lines (&it, 1, 0);
9029 /* Set the window start there. */
9030 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9031 window_start_changed_p = 1;
9035 return window_start_changed_p;
9039 /* Try cursor movement in case text has not changes in window WINDOW,
9040 with window start STARTP. Value is
9042 1 if successful
9044 0 if this method cannot be used
9046 -1 if we know we have to scroll the display. *SCROLL_STEP is
9047 set to 1, under certain circumstances, if we want to scroll as
9048 if scroll-step were set to 1. See the code. */
9050 static int
9051 try_cursor_movement (window, startp, scroll_step)
9052 Lisp_Object window;
9053 struct text_pos startp;
9054 int *scroll_step;
9056 struct window *w = XWINDOW (window);
9057 struct frame *f = XFRAME (w->frame);
9058 int rc = 0;
9060 /* Handle case where text has not changed, only point, and it has
9061 not moved off the frame. */
9062 if (/* Point may be in this window. */
9063 PT >= CHARPOS (startp)
9064 /* If we don't check this, we are called to move the cursor in a
9065 horizontally split window with a current matrix that doesn't
9066 fit the display. */
9067 && !windows_or_buffers_changed
9068 /* Selective display hasn't changed. */
9069 && !current_buffer->clip_changed
9070 /* If force-mode-line-update was called, really redisplay;
9071 that's how redisplay is forced after e.g. changing
9072 buffer-invisibility-spec. */
9073 && NILP (w->update_mode_line)
9074 /* Can't use this case if highlighting a region. When a
9075 region exists, cursor movement has to do more than just
9076 set the cursor. */
9077 && !(!NILP (Vtransient_mark_mode)
9078 && !NILP (current_buffer->mark_active))
9079 && NILP (w->region_showing)
9080 && NILP (Vshow_trailing_whitespace)
9081 /* Right after splitting windows, last_point may be nil. */
9082 && INTEGERP (w->last_point)
9083 /* This code is not used for mini-buffer for the sake of the case
9084 of redisplaying to replace an echo area message; since in
9085 that case the mini-buffer contents per se are usually
9086 unchanged. This code is of no real use in the mini-buffer
9087 since the handling of this_line_start_pos, etc., in redisplay
9088 handles the same cases. */
9089 && !EQ (window, minibuf_window)
9090 /* When splitting windows or for new windows, it happens that
9091 redisplay is called with a nil window_end_vpos or one being
9092 larger than the window. This should really be fixed in
9093 window.c. I don't have this on my list, now, so we do
9094 approximately the same as the old redisplay code. --gerd. */
9095 && INTEGERP (w->window_end_vpos)
9096 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9097 && (FRAME_WINDOW_P (f)
9098 || !MARKERP (Voverlay_arrow_position)
9099 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9101 int this_scroll_margin;
9102 struct glyph_row *row;
9104 #if GLYPH_DEBUG
9105 debug_method_add (w, "cursor movement");
9106 #endif
9108 /* Scroll if point within this distance from the top or bottom
9109 of the window. This is a pixel value. */
9110 this_scroll_margin = max (0, scroll_margin);
9111 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9112 this_scroll_margin *= CANON_Y_UNIT (f);
9114 /* Start with the row the cursor was displayed during the last
9115 not paused redisplay. Give up if that row is not valid. */
9116 if (w->last_cursor.vpos < 0
9117 || w->last_cursor.vpos >= w->current_matrix->nrows)
9118 rc = -1;
9119 else
9121 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9122 if (row->mode_line_p)
9123 ++row;
9124 if (!row->enabled_p)
9125 rc = -1;
9128 if (rc == 0)
9130 int scroll_p = 0;
9131 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9134 if (PT > XFASTINT (w->last_point))
9136 /* Point has moved forward. */
9137 while (MATRIX_ROW_END_CHARPOS (row) < PT
9138 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9140 xassert (row->enabled_p);
9141 ++row;
9144 /* The end position of a row equals the start position
9145 of the next row. If PT is there, we would rather
9146 display it in the next line. */
9147 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9148 && MATRIX_ROW_END_CHARPOS (row) == PT
9149 && !cursor_row_p (w, row))
9150 ++row;
9152 /* If within the scroll margin, scroll. Note that
9153 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9154 the next line would be drawn, and that
9155 this_scroll_margin can be zero. */
9156 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9157 || PT > MATRIX_ROW_END_CHARPOS (row)
9158 /* Line is completely visible last line in window
9159 and PT is to be set in the next line. */
9160 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9161 && PT == MATRIX_ROW_END_CHARPOS (row)
9162 && !row->ends_at_zv_p
9163 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9164 scroll_p = 1;
9166 else if (PT < XFASTINT (w->last_point))
9168 /* Cursor has to be moved backward. Note that PT >=
9169 CHARPOS (startp) because of the outer
9170 if-statement. */
9171 while (!row->mode_line_p
9172 && (MATRIX_ROW_START_CHARPOS (row) > PT
9173 || (MATRIX_ROW_START_CHARPOS (row) == PT
9174 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9175 && (row->y > this_scroll_margin
9176 || CHARPOS (startp) == BEGV))
9178 xassert (row->enabled_p);
9179 --row;
9182 /* Consider the following case: Window starts at BEGV,
9183 there is invisible, intangible text at BEGV, so that
9184 display starts at some point START > BEGV. It can
9185 happen that we are called with PT somewhere between
9186 BEGV and START. Try to handle that case. */
9187 if (row < w->current_matrix->rows
9188 || row->mode_line_p)
9190 row = w->current_matrix->rows;
9191 if (row->mode_line_p)
9192 ++row;
9195 /* Due to newlines in overlay strings, we may have to
9196 skip forward over overlay strings. */
9197 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9198 && MATRIX_ROW_END_CHARPOS (row) == PT
9199 && !cursor_row_p (w, row))
9200 ++row;
9202 /* If within the scroll margin, scroll. */
9203 if (row->y < this_scroll_margin
9204 && CHARPOS (startp) != BEGV)
9205 scroll_p = 1;
9208 if (PT < MATRIX_ROW_START_CHARPOS (row)
9209 || PT > MATRIX_ROW_END_CHARPOS (row))
9211 /* if PT is not in the glyph row, give up. */
9212 rc = -1;
9214 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9216 /* If we end up in a partially visible line, let's make it
9217 fully visible, except when it's taller than the window,
9218 in which case we can't do much about it. */
9219 if (row->height > window_box_height (w))
9221 *scroll_step = 1;
9222 rc = -1;
9224 else
9226 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9227 try_window (window, startp);
9228 make_cursor_line_fully_visible (w);
9229 rc = 1;
9232 else if (scroll_p)
9233 rc = -1;
9234 else
9236 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9237 rc = 1;
9242 return rc;
9246 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9247 selected_window is redisplayed. */
9249 static void
9250 redisplay_window (window, just_this_one_p)
9251 Lisp_Object window;
9252 int just_this_one_p;
9254 struct window *w = XWINDOW (window);
9255 struct frame *f = XFRAME (w->frame);
9256 struct buffer *buffer = XBUFFER (w->buffer);
9257 struct buffer *old = current_buffer;
9258 struct text_pos lpoint, opoint, startp;
9259 int update_mode_line;
9260 int tem;
9261 struct it it;
9262 /* Record it now because it's overwritten. */
9263 int current_matrix_up_to_date_p = 0;
9264 int temp_scroll_step = 0;
9265 int count = BINDING_STACK_SIZE ();
9266 int rc;
9268 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9269 opoint = lpoint;
9271 /* W must be a leaf window here. */
9272 xassert (!NILP (w->buffer));
9273 #if GLYPH_DEBUG
9274 *w->desired_matrix->method = 0;
9275 #endif
9277 specbind (Qinhibit_point_motion_hooks, Qt);
9279 reconsider_clip_changes (w, buffer);
9281 /* Has the mode line to be updated? */
9282 update_mode_line = (!NILP (w->update_mode_line)
9283 || update_mode_lines
9284 || buffer->clip_changed);
9286 if (MINI_WINDOW_P (w))
9288 if (w == XWINDOW (echo_area_window)
9289 && !NILP (echo_area_buffer[0]))
9291 if (update_mode_line)
9292 /* We may have to update a tty frame's menu bar or a
9293 tool-bar. Example `M-x C-h C-h C-g'. */
9294 goto finish_menu_bars;
9295 else
9296 /* We've already displayed the echo area glyphs in this window. */
9297 goto finish_scroll_bars;
9299 else if (w != XWINDOW (minibuf_window))
9301 /* W is a mini-buffer window, but it's not the currently
9302 active one, so clear it. */
9303 int yb = window_text_bottom_y (w);
9304 struct glyph_row *row;
9305 int y;
9307 for (y = 0, row = w->desired_matrix->rows;
9308 y < yb;
9309 y += row->height, ++row)
9310 blank_row (w, row, y);
9311 goto finish_scroll_bars;
9315 /* Otherwise set up data on this window; select its buffer and point
9316 value. */
9317 /* Really select the buffer, for the sake of buffer-local
9318 variables. */
9319 set_buffer_internal_1 (XBUFFER (w->buffer));
9320 SET_TEXT_POS (opoint, PT, PT_BYTE);
9322 current_matrix_up_to_date_p
9323 = (!NILP (w->window_end_valid)
9324 && !current_buffer->clip_changed
9325 && XFASTINT (w->last_modified) >= MODIFF
9326 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9328 /* When windows_or_buffers_changed is non-zero, we can't rely on
9329 the window end being valid, so set it to nil there. */
9330 if (windows_or_buffers_changed)
9332 /* If window starts on a continuation line, maybe adjust the
9333 window start in case the window's width changed. */
9334 if (XMARKER (w->start)->buffer == current_buffer)
9335 compute_window_start_on_continuation_line (w);
9337 w->window_end_valid = Qnil;
9340 /* Some sanity checks. */
9341 CHECK_WINDOW_END (w);
9342 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9343 abort ();
9344 if (BYTEPOS (opoint) < CHARPOS (opoint))
9345 abort ();
9347 /* If %c is in mode line, update it if needed. */
9348 if (!NILP (w->column_number_displayed)
9349 /* This alternative quickly identifies a common case
9350 where no change is needed. */
9351 && !(PT == XFASTINT (w->last_point)
9352 && XFASTINT (w->last_modified) >= MODIFF
9353 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9354 && XFASTINT (w->column_number_displayed) != current_column ())
9355 update_mode_line = 1;
9357 /* Count number of windows showing the selected buffer. An indirect
9358 buffer counts as its base buffer. */
9359 if (!just_this_one_p)
9361 struct buffer *current_base, *window_base;
9362 current_base = current_buffer;
9363 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9364 if (current_base->base_buffer)
9365 current_base = current_base->base_buffer;
9366 if (window_base->base_buffer)
9367 window_base = window_base->base_buffer;
9368 if (current_base == window_base)
9369 buffer_shared++;
9372 /* Point refers normally to the selected window. For any other
9373 window, set up appropriate value. */
9374 if (!EQ (window, selected_window))
9376 int new_pt = XMARKER (w->pointm)->charpos;
9377 int new_pt_byte = marker_byte_position (w->pointm);
9378 if (new_pt < BEGV)
9380 new_pt = BEGV;
9381 new_pt_byte = BEGV_BYTE;
9382 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9384 else if (new_pt > (ZV - 1))
9386 new_pt = ZV;
9387 new_pt_byte = ZV_BYTE;
9388 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9391 /* We don't use SET_PT so that the point-motion hooks don't run. */
9392 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9395 /* If any of the character widths specified in the display table
9396 have changed, invalidate the width run cache. It's true that
9397 this may be a bit late to catch such changes, but the rest of
9398 redisplay goes (non-fatally) haywire when the display table is
9399 changed, so why should we worry about doing any better? */
9400 if (current_buffer->width_run_cache)
9402 struct Lisp_Char_Table *disptab = buffer_display_table ();
9404 if (! disptab_matches_widthtab (disptab,
9405 XVECTOR (current_buffer->width_table)))
9407 invalidate_region_cache (current_buffer,
9408 current_buffer->width_run_cache,
9409 BEG, Z);
9410 recompute_width_table (current_buffer, disptab);
9414 /* If window-start is screwed up, choose a new one. */
9415 if (XMARKER (w->start)->buffer != current_buffer)
9416 goto recenter;
9418 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9420 /* If someone specified a new starting point but did not insist,
9421 check whether it can be used. */
9422 if (!NILP (w->optional_new_start)
9423 && CHARPOS (startp) >= BEGV
9424 && CHARPOS (startp) <= ZV)
9426 w->optional_new_start = Qnil;
9427 start_display (&it, w, startp);
9428 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9429 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9430 if (IT_CHARPOS (it) == PT)
9431 w->force_start = Qt;
9434 /* Handle case where place to start displaying has been specified,
9435 unless the specified location is outside the accessible range. */
9436 if (!NILP (w->force_start)
9437 || w->frozen_window_start_p)
9439 w->force_start = Qnil;
9440 w->vscroll = 0;
9441 w->window_end_valid = Qnil;
9443 /* Forget any recorded base line for line number display. */
9444 if (!current_matrix_up_to_date_p
9445 || current_buffer->clip_changed)
9446 w->base_line_number = Qnil;
9448 /* Redisplay the mode line. Select the buffer properly for that.
9449 Also, run the hook window-scroll-functions
9450 because we have scrolled. */
9451 /* Note, we do this after clearing force_start because
9452 if there's an error, it is better to forget about force_start
9453 than to get into an infinite loop calling the hook functions
9454 and having them get more errors. */
9455 if (!update_mode_line
9456 || ! NILP (Vwindow_scroll_functions))
9458 update_mode_line = 1;
9459 w->update_mode_line = Qt;
9460 startp = run_window_scroll_functions (window, startp);
9463 XSETFASTINT (w->last_modified, 0);
9464 XSETFASTINT (w->last_overlay_modified, 0);
9465 if (CHARPOS (startp) < BEGV)
9466 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9467 else if (CHARPOS (startp) > ZV)
9468 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9470 /* Redisplay, then check if cursor has been set during the
9471 redisplay. Give up if new fonts were loaded. */
9472 if (!try_window (window, startp))
9474 w->force_start = Qt;
9475 clear_glyph_matrix (w->desired_matrix);
9476 goto restore_buffers;
9479 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9481 /* If point does not appear, try to move point so it does
9482 appear. The desired matrix has been built above, so we
9483 can use it here. */
9484 int window_height;
9485 struct glyph_row *row;
9487 window_height = window_box_height (w) / 2;
9488 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9489 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9490 ++row;
9492 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9493 MATRIX_ROW_START_BYTEPOS (row));
9495 if (w != XWINDOW (selected_window))
9496 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9497 else if (current_buffer == old)
9498 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9500 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9502 /* If we are highlighting the region, then we just changed
9503 the region, so redisplay to show it. */
9504 if (!NILP (Vtransient_mark_mode)
9505 && !NILP (current_buffer->mark_active))
9507 clear_glyph_matrix (w->desired_matrix);
9508 if (!try_window (window, startp))
9509 goto restore_buffers;
9513 make_cursor_line_fully_visible (w);
9514 #if GLYPH_DEBUG
9515 debug_method_add (w, "forced window start");
9516 #endif
9517 goto done;
9520 /* Handle case where text has not changed, only point, and it has
9521 not moved off the frame. */
9522 if (current_matrix_up_to_date_p
9523 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9524 rc != 0))
9526 if (rc == -1)
9527 goto try_to_scroll;
9528 else
9529 goto done;
9531 /* If current starting point was originally the beginning of a line
9532 but no longer is, find a new starting point. */
9533 else if (!NILP (w->start_at_line_beg)
9534 && !(CHARPOS (startp) <= BEGV
9535 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9537 #if GLYPH_DEBUG
9538 debug_method_add (w, "recenter 1");
9539 #endif
9540 goto recenter;
9543 /* Try scrolling with try_window_id. */
9544 else if (/* Windows and buffers haven't changed. */
9545 !windows_or_buffers_changed
9546 /* Window must be either use window-based redisplay or
9547 be full width. */
9548 && (FRAME_WINDOW_P (f)
9549 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9550 && !MINI_WINDOW_P (w)
9551 /* Point is not known NOT to appear in window. */
9552 && PT >= CHARPOS (startp)
9553 && XFASTINT (w->last_modified)
9554 /* Window is not hscrolled. */
9555 && XFASTINT (w->hscroll) == 0
9556 /* Selective display has not changed. */
9557 && !current_buffer->clip_changed
9558 /* Current matrix is up to date. */
9559 && !NILP (w->window_end_valid)
9560 /* Can't use this case if highlighting a region because
9561 a cursor movement will do more than just set the cursor. */
9562 && !(!NILP (Vtransient_mark_mode)
9563 && !NILP (current_buffer->mark_active))
9564 && NILP (w->region_showing)
9565 && NILP (Vshow_trailing_whitespace)
9566 /* Overlay arrow position and string not changed. */
9567 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9568 && EQ (last_arrow_string, Voverlay_arrow_string)
9569 /* Value is > 0 if update has been done, it is -1 if we
9570 know that the same window start will not work. It is 0
9571 if unsuccessful for some other reason. */
9572 && (tem = try_window_id (w)) != 0)
9574 #if GLYPH_DEBUG
9575 debug_method_add (w, "try_window_id %d", tem);
9576 #endif
9578 if (fonts_changed_p)
9579 goto restore_buffers;
9580 if (tem > 0)
9581 goto done;
9583 /* Otherwise try_window_id has returned -1 which means that we
9584 don't want the alternative below this comment to execute. */
9586 else if (CHARPOS (startp) >= BEGV
9587 && CHARPOS (startp) <= ZV
9588 && PT >= CHARPOS (startp)
9589 && (CHARPOS (startp) < ZV
9590 /* Avoid starting at end of buffer. */
9591 || CHARPOS (startp) == BEGV
9592 || (XFASTINT (w->last_modified) >= MODIFF
9593 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9595 #if GLYPH_DEBUG
9596 debug_method_add (w, "same window start");
9597 #endif
9599 /* Try to redisplay starting at same place as before.
9600 If point has not moved off frame, accept the results. */
9601 if (!current_matrix_up_to_date_p
9602 /* Don't use try_window_reusing_current_matrix in this case
9603 because a window scroll function can have changed the
9604 buffer. */
9605 || !NILP (Vwindow_scroll_functions)
9606 || MINI_WINDOW_P (w)
9607 || !try_window_reusing_current_matrix (w))
9609 IF_DEBUG (debug_method_add (w, "1"));
9610 try_window (window, startp);
9613 if (fonts_changed_p)
9614 goto restore_buffers;
9616 if (w->cursor.vpos >= 0)
9618 if (!just_this_one_p
9619 || current_buffer->clip_changed
9620 || BEG_UNCHANGED < CHARPOS (startp))
9621 /* Forget any recorded base line for line number display. */
9622 w->base_line_number = Qnil;
9624 make_cursor_line_fully_visible (w);
9625 goto done;
9627 else
9628 clear_glyph_matrix (w->desired_matrix);
9631 try_to_scroll:
9633 XSETFASTINT (w->last_modified, 0);
9634 XSETFASTINT (w->last_overlay_modified, 0);
9636 /* Redisplay the mode line. Select the buffer properly for that. */
9637 if (!update_mode_line)
9639 update_mode_line = 1;
9640 w->update_mode_line = Qt;
9643 /* Try to scroll by specified few lines. */
9644 if ((scroll_conservatively
9645 || scroll_step
9646 || temp_scroll_step
9647 || NUMBERP (current_buffer->scroll_up_aggressively)
9648 || NUMBERP (current_buffer->scroll_down_aggressively))
9649 && !current_buffer->clip_changed
9650 && CHARPOS (startp) >= BEGV
9651 && CHARPOS (startp) <= ZV)
9653 /* The function returns -1 if new fonts were loaded, 1 if
9654 successful, 0 if not successful. */
9655 int rc = try_scrolling (window, just_this_one_p,
9656 scroll_conservatively,
9657 scroll_step,
9658 temp_scroll_step);
9659 if (rc > 0)
9660 goto done;
9661 else if (rc < 0)
9662 goto restore_buffers;
9665 /* Finally, just choose place to start which centers point */
9667 recenter:
9669 #if GLYPH_DEBUG
9670 debug_method_add (w, "recenter");
9671 #endif
9673 /* w->vscroll = 0; */
9675 /* Forget any previously recorded base line for line number display. */
9676 if (!current_matrix_up_to_date_p
9677 || current_buffer->clip_changed)
9678 w->base_line_number = Qnil;
9680 /* Move backward half the height of the window. */
9681 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9682 it.current_y = it.last_visible_y;
9683 move_it_vertically_backward (&it, it.last_visible_y / 2);
9684 xassert (IT_CHARPOS (it) >= BEGV);
9686 /* The function move_it_vertically_backward may move over more
9687 than the specified y-distance. If it->w is small, e.g. a
9688 mini-buffer window, we may end up in front of the window's
9689 display area. Start displaying at the start of the line
9690 containing PT in this case. */
9691 if (it.current_y <= 0)
9693 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9694 move_it_vertically (&it, 0);
9695 xassert (IT_CHARPOS (it) <= PT);
9696 it.current_y = 0;
9699 it.current_x = it.hpos = 0;
9701 /* Set startp here explicitly in case that helps avoid an infinite loop
9702 in case the window-scroll-functions functions get errors. */
9703 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9705 /* Run scroll hooks. */
9706 startp = run_window_scroll_functions (window, it.current.pos);
9708 /* Redisplay the window. */
9709 if (!current_matrix_up_to_date_p
9710 || windows_or_buffers_changed
9711 /* Don't use try_window_reusing_current_matrix in this case
9712 because it can have changed the buffer. */
9713 || !NILP (Vwindow_scroll_functions)
9714 || !just_this_one_p
9715 || MINI_WINDOW_P (w)
9716 || !try_window_reusing_current_matrix (w))
9717 try_window (window, startp);
9719 /* If new fonts have been loaded (due to fontsets), give up. We
9720 have to start a new redisplay since we need to re-adjust glyph
9721 matrices. */
9722 if (fonts_changed_p)
9723 goto restore_buffers;
9725 /* If cursor did not appear assume that the middle of the window is
9726 in the first line of the window. Do it again with the next line.
9727 (Imagine a window of height 100, displaying two lines of height
9728 60. Moving back 50 from it->last_visible_y will end in the first
9729 line.) */
9730 if (w->cursor.vpos < 0)
9732 if (!NILP (w->window_end_valid)
9733 && PT >= Z - XFASTINT (w->window_end_pos))
9735 clear_glyph_matrix (w->desired_matrix);
9736 move_it_by_lines (&it, 1, 0);
9737 try_window (window, it.current.pos);
9739 else if (PT < IT_CHARPOS (it))
9741 clear_glyph_matrix (w->desired_matrix);
9742 move_it_by_lines (&it, -1, 0);
9743 try_window (window, it.current.pos);
9745 else
9747 /* Not much we can do about it. */
9751 /* Consider the following case: Window starts at BEGV, there is
9752 invisible, intangible text at BEGV, so that display starts at
9753 some point START > BEGV. It can happen that we are called with
9754 PT somewhere between BEGV and START. Try to handle that case. */
9755 if (w->cursor.vpos < 0)
9757 struct glyph_row *row = w->current_matrix->rows;
9758 if (row->mode_line_p)
9759 ++row;
9760 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9763 make_cursor_line_fully_visible (w);
9765 done:
9767 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9768 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9769 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9770 ? Qt : Qnil);
9772 /* Display the mode line, if we must. */
9773 if ((update_mode_line
9774 /* If window not full width, must redo its mode line
9775 if (a) the window to its side is being redone and
9776 (b) we do a frame-based redisplay. This is a consequence
9777 of how inverted lines are drawn in frame-based redisplay. */
9778 || (!just_this_one_p
9779 && !FRAME_WINDOW_P (f)
9780 && !WINDOW_FULL_WIDTH_P (w))
9781 /* Line number to display. */
9782 || INTEGERP (w->base_line_pos)
9783 /* Column number is displayed and different from the one displayed. */
9784 || (!NILP (w->column_number_displayed)
9785 && XFASTINT (w->column_number_displayed) != current_column ()))
9786 /* This means that the window has a mode line. */
9787 && (WINDOW_WANTS_MODELINE_P (w)
9788 || WINDOW_WANTS_HEADER_LINE_P (w)))
9790 Lisp_Object old_selected_frame;
9792 old_selected_frame = selected_frame;
9794 XSETFRAME (selected_frame, f);
9795 display_mode_lines (w);
9796 selected_frame = old_selected_frame;
9798 /* If mode line height has changed, arrange for a thorough
9799 immediate redisplay using the correct mode line height. */
9800 if (WINDOW_WANTS_MODELINE_P (w)
9801 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9803 fonts_changed_p = 1;
9804 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9805 = DESIRED_MODE_LINE_HEIGHT (w);
9808 /* If top line height has changed, arrange for a thorough
9809 immediate redisplay using the correct mode line height. */
9810 if (WINDOW_WANTS_HEADER_LINE_P (w)
9811 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9813 fonts_changed_p = 1;
9814 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9815 = DESIRED_HEADER_LINE_HEIGHT (w);
9818 if (fonts_changed_p)
9819 goto restore_buffers;
9822 if (!line_number_displayed
9823 && !BUFFERP (w->base_line_pos))
9825 w->base_line_pos = Qnil;
9826 w->base_line_number = Qnil;
9829 finish_menu_bars:
9831 /* When we reach a frame's selected window, redo the frame's menu bar. */
9832 if (update_mode_line
9833 && EQ (FRAME_SELECTED_WINDOW (f), window))
9835 int redisplay_menu_p = 0;
9837 if (FRAME_WINDOW_P (f))
9839 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9840 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9841 #else
9842 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9843 #endif
9845 else
9846 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9848 if (redisplay_menu_p)
9849 display_menu_bar (w);
9851 #ifdef HAVE_WINDOW_SYSTEM
9852 if (WINDOWP (f->tool_bar_window)
9853 && (FRAME_TOOL_BAR_LINES (f) > 0
9854 || auto_resize_tool_bars_p))
9855 redisplay_tool_bar (f);
9856 #endif
9859 finish_scroll_bars:
9861 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9863 int start, end, whole;
9865 /* Calculate the start and end positions for the current window.
9866 At some point, it would be nice to choose between scrollbars
9867 which reflect the whole buffer size, with special markers
9868 indicating narrowing, and scrollbars which reflect only the
9869 visible region.
9871 Note that mini-buffers sometimes aren't displaying any text. */
9872 if (!MINI_WINDOW_P (w)
9873 || (w == XWINDOW (minibuf_window)
9874 && NILP (echo_area_buffer[0])))
9876 whole = ZV - BEGV;
9877 start = marker_position (w->start) - BEGV;
9878 /* I don't think this is guaranteed to be right. For the
9879 moment, we'll pretend it is. */
9880 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9882 if (end < start)
9883 end = start;
9884 if (whole < (end - start))
9885 whole = end - start;
9887 else
9888 start = end = whole = 0;
9890 /* Indicate what this scroll bar ought to be displaying now. */
9891 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9893 /* Note that we actually used the scroll bar attached to this
9894 window, so it shouldn't be deleted at the end of redisplay. */
9895 (*redeem_scroll_bar_hook) (w);
9898 restore_buffers:
9900 /* Restore current_buffer and value of point in it. */
9901 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9902 set_buffer_internal_1 (old);
9903 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9905 unbind_to (count, Qnil);
9909 /* Build the complete desired matrix of WINDOW with a window start
9910 buffer position POS. Value is non-zero if successful. It is zero
9911 if fonts were loaded during redisplay which makes re-adjusting
9912 glyph matrices necessary. */
9915 try_window (window, pos)
9916 Lisp_Object window;
9917 struct text_pos pos;
9919 struct window *w = XWINDOW (window);
9920 struct it it;
9921 struct glyph_row *last_text_row = NULL;
9923 /* Make POS the new window start. */
9924 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9926 /* Mark cursor position as unknown. No overlay arrow seen. */
9927 w->cursor.vpos = -1;
9928 overlay_arrow_seen = 0;
9930 /* Initialize iterator and info to start at POS. */
9931 start_display (&it, w, pos);
9933 /* Display all lines of W. */
9934 while (it.current_y < it.last_visible_y)
9936 if (display_line (&it))
9937 last_text_row = it.glyph_row - 1;
9938 if (fonts_changed_p)
9939 return 0;
9942 /* If bottom moved off end of frame, change mode line percentage. */
9943 if (XFASTINT (w->window_end_pos) <= 0
9944 && Z != IT_CHARPOS (it))
9945 w->update_mode_line = Qt;
9947 /* Set window_end_pos to the offset of the last character displayed
9948 on the window from the end of current_buffer. Set
9949 window_end_vpos to its row number. */
9950 if (last_text_row)
9952 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9953 w->window_end_bytepos
9954 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9955 XSETFASTINT (w->window_end_pos,
9956 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9957 XSETFASTINT (w->window_end_vpos,
9958 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9959 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9960 ->displays_text_p);
9962 else
9964 w->window_end_bytepos = 0;
9965 XSETFASTINT (w->window_end_pos, 0);
9966 XSETFASTINT (w->window_end_vpos, 0);
9969 /* But that is not valid info until redisplay finishes. */
9970 w->window_end_valid = Qnil;
9971 return 1;
9976 /************************************************************************
9977 Window redisplay reusing current matrix when buffer has not changed
9978 ************************************************************************/
9980 /* Try redisplay of window W showing an unchanged buffer with a
9981 different window start than the last time it was displayed by
9982 reusing its current matrix. Value is non-zero if successful.
9983 W->start is the new window start. */
9985 static int
9986 try_window_reusing_current_matrix (w)
9987 struct window *w;
9989 struct frame *f = XFRAME (w->frame);
9990 struct glyph_row *row, *bottom_row;
9991 struct it it;
9992 struct run run;
9993 struct text_pos start, new_start;
9994 int nrows_scrolled, i;
9995 struct glyph_row *last_text_row;
9996 struct glyph_row *last_reused_text_row;
9997 struct glyph_row *start_row;
9998 int start_vpos, min_y, max_y;
10000 if (/* This function doesn't handle terminal frames. */
10001 !FRAME_WINDOW_P (f)
10002 /* Don't try to reuse the display if windows have been split
10003 or such. */
10004 || windows_or_buffers_changed)
10005 return 0;
10007 /* Can't do this if region may have changed. */
10008 if ((!NILP (Vtransient_mark_mode)
10009 && !NILP (current_buffer->mark_active))
10010 || !NILP (w->region_showing)
10011 || !NILP (Vshow_trailing_whitespace))
10012 return 0;
10014 /* If top-line visibility has changed, give up. */
10015 if (WINDOW_WANTS_HEADER_LINE_P (w)
10016 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10017 return 0;
10019 /* Give up if old or new display is scrolled vertically. We could
10020 make this function handle this, but right now it doesn't. */
10021 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10022 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10023 return 0;
10025 /* The variable new_start now holds the new window start. The old
10026 start `start' can be determined from the current matrix. */
10027 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10028 start = start_row->start.pos;
10029 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10031 /* Clear the desired matrix for the display below. */
10032 clear_glyph_matrix (w->desired_matrix);
10034 if (CHARPOS (new_start) <= CHARPOS (start))
10036 int first_row_y;
10038 IF_DEBUG (debug_method_add (w, "twu1"));
10040 /* Display up to a row that can be reused. The variable
10041 last_text_row is set to the last row displayed that displays
10042 text. Note that it.vpos == 0 if or if not there is a
10043 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10044 start_display (&it, w, new_start);
10045 first_row_y = it.current_y;
10046 w->cursor.vpos = -1;
10047 last_text_row = last_reused_text_row = NULL;
10049 while (it.current_y < it.last_visible_y
10050 && IT_CHARPOS (it) < CHARPOS (start)
10051 && !fonts_changed_p)
10052 if (display_line (&it))
10053 last_text_row = it.glyph_row - 1;
10055 /* A value of current_y < last_visible_y means that we stopped
10056 at the previous window start, which in turn means that we
10057 have at least one reusable row. */
10058 if (it.current_y < it.last_visible_y)
10060 /* IT.vpos always starts from 0; it counts text lines. */
10061 nrows_scrolled = it.vpos;
10063 /* Find PT if not already found in the lines displayed. */
10064 if (w->cursor.vpos < 0)
10066 int dy = it.current_y - first_row_y;
10068 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10069 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10071 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10072 && PT < MATRIX_ROW_END_CHARPOS (row))
10074 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10075 dy, nrows_scrolled);
10076 break;
10079 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10080 break;
10082 ++row;
10085 /* Give up if point was not found. This shouldn't
10086 happen often; not more often than with try_window
10087 itself. */
10088 if (w->cursor.vpos < 0)
10090 clear_glyph_matrix (w->desired_matrix);
10091 return 0;
10095 /* Scroll the display. Do it before the current matrix is
10096 changed. The problem here is that update has not yet
10097 run, i.e. part of the current matrix is not up to date.
10098 scroll_run_hook will clear the cursor, and use the
10099 current matrix to get the height of the row the cursor is
10100 in. */
10101 run.current_y = first_row_y;
10102 run.desired_y = it.current_y;
10103 run.height = it.last_visible_y - it.current_y;
10105 if (run.height > 0 && run.current_y != run.desired_y)
10107 update_begin (f);
10108 rif->update_window_begin_hook (w);
10109 rif->clear_mouse_face (w);
10110 rif->scroll_run_hook (w, &run);
10111 rif->update_window_end_hook (w, 0, 0);
10112 update_end (f);
10115 /* Shift current matrix down by nrows_scrolled lines. */
10116 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10117 rotate_matrix (w->current_matrix,
10118 start_vpos,
10119 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10120 nrows_scrolled);
10122 /* Disable lines not reused. */
10123 for (i = 0; i < it.vpos; ++i)
10124 (start_row + i)->enabled_p = 0;
10126 /* Re-compute Y positions. */
10127 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10128 max_y = it.last_visible_y;
10129 for (row = start_row + nrows_scrolled;
10130 row < bottom_row;
10131 ++row)
10133 row->y = it.current_y;
10135 if (row->y < min_y)
10136 row->visible_height = row->height - (min_y - row->y);
10137 else if (row->y + row->height > max_y)
10138 row->visible_height
10139 = row->height - (row->y + row->height - max_y);
10140 else
10141 row->visible_height = row->height;
10143 it.current_y += row->height;
10145 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10146 last_reused_text_row = row;
10147 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10148 break;
10152 /* Update window_end_pos etc.; last_reused_text_row is the last
10153 reused row from the current matrix containing text, if any.
10154 The value of last_text_row is the last displayed line
10155 containing text. */
10156 if (last_reused_text_row)
10158 w->window_end_bytepos
10159 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10160 XSETFASTINT (w->window_end_pos,
10161 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10162 XSETFASTINT (w->window_end_vpos,
10163 MATRIX_ROW_VPOS (last_reused_text_row,
10164 w->current_matrix));
10166 else if (last_text_row)
10168 w->window_end_bytepos
10169 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10170 XSETFASTINT (w->window_end_pos,
10171 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10172 XSETFASTINT (w->window_end_vpos,
10173 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10175 else
10177 /* This window must be completely empty. */
10178 w->window_end_bytepos = 0;
10179 XSETFASTINT (w->window_end_pos, 0);
10180 XSETFASTINT (w->window_end_vpos, 0);
10182 w->window_end_valid = Qnil;
10184 /* Update hint: don't try scrolling again in update_window. */
10185 w->desired_matrix->no_scrolling_p = 1;
10187 #if GLYPH_DEBUG
10188 debug_method_add (w, "try_window_reusing_current_matrix 1");
10189 #endif
10190 return 1;
10192 else if (CHARPOS (new_start) > CHARPOS (start))
10194 struct glyph_row *pt_row, *row;
10195 struct glyph_row *first_reusable_row;
10196 struct glyph_row *first_row_to_display;
10197 int dy;
10198 int yb = window_text_bottom_y (w);
10200 IF_DEBUG (debug_method_add (w, "twu2"));
10202 /* Find the row starting at new_start, if there is one. Don't
10203 reuse a partially visible line at the end. */
10204 first_reusable_row = start_row;
10205 while (first_reusable_row->enabled_p
10206 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10207 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10208 < CHARPOS (new_start)))
10209 ++first_reusable_row;
10211 /* Give up if there is no row to reuse. */
10212 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10213 || !first_reusable_row->enabled_p
10214 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10215 != CHARPOS (new_start)))
10216 return 0;
10218 /* We can reuse fully visible rows beginning with
10219 first_reusable_row to the end of the window. Set
10220 first_row_to_display to the first row that cannot be reused.
10221 Set pt_row to the row containing point, if there is any. */
10222 first_row_to_display = first_reusable_row;
10223 pt_row = NULL;
10224 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10226 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10227 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10228 pt_row = first_row_to_display;
10230 ++first_row_to_display;
10233 /* Start displaying at the start of first_row_to_display. */
10234 xassert (first_row_to_display->y < yb);
10235 init_to_row_start (&it, w, first_row_to_display);
10236 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10237 - start_vpos);
10238 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10239 - nrows_scrolled);
10240 it.current_y = (first_row_to_display->y - first_reusable_row->y
10241 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10243 /* Display lines beginning with first_row_to_display in the
10244 desired matrix. Set last_text_row to the last row displayed
10245 that displays text. */
10246 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10247 if (pt_row == NULL)
10248 w->cursor.vpos = -1;
10249 last_text_row = NULL;
10250 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10251 if (display_line (&it))
10252 last_text_row = it.glyph_row - 1;
10254 /* Give up If point isn't in a row displayed or reused. */
10255 if (w->cursor.vpos < 0)
10257 clear_glyph_matrix (w->desired_matrix);
10258 return 0;
10261 /* If point is in a reused row, adjust y and vpos of the cursor
10262 position. */
10263 if (pt_row)
10265 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10266 w->current_matrix);
10267 w->cursor.y -= first_reusable_row->y;
10270 /* Scroll the display. */
10271 run.current_y = first_reusable_row->y;
10272 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10273 run.height = it.last_visible_y - run.current_y;
10274 dy = run.current_y - run.desired_y;
10276 if (run.height)
10278 struct frame *f = XFRAME (WINDOW_FRAME (w));
10279 update_begin (f);
10280 rif->update_window_begin_hook (w);
10281 rif->clear_mouse_face (w);
10282 rif->scroll_run_hook (w, &run);
10283 rif->update_window_end_hook (w, 0, 0);
10284 update_end (f);
10287 /* Adjust Y positions of reused rows. */
10288 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10289 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10290 max_y = it.last_visible_y;
10291 for (row = first_reusable_row; row < first_row_to_display; ++row)
10293 row->y -= dy;
10294 if (row->y < min_y)
10295 row->visible_height = row->height - (min_y - row->y);
10296 else if (row->y + row->height > max_y)
10297 row->visible_height
10298 = row->height - (row->y + row->height - max_y);
10299 else
10300 row->visible_height = row->height;
10303 /* Disable rows not reused. */
10304 while (row < bottom_row)
10306 row->enabled_p = 0;
10307 ++row;
10310 /* Scroll the current matrix. */
10311 xassert (nrows_scrolled > 0);
10312 rotate_matrix (w->current_matrix,
10313 start_vpos,
10314 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10315 -nrows_scrolled);
10317 /* Adjust window end. A null value of last_text_row means that
10318 the window end is in reused rows which in turn means that
10319 only its vpos can have changed. */
10320 if (last_text_row)
10322 w->window_end_bytepos
10323 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10324 XSETFASTINT (w->window_end_pos,
10325 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10326 XSETFASTINT (w->window_end_vpos,
10327 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10329 else
10331 XSETFASTINT (w->window_end_vpos,
10332 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10335 w->window_end_valid = Qnil;
10336 w->desired_matrix->no_scrolling_p = 1;
10338 #if GLYPH_DEBUG
10339 debug_method_add (w, "try_window_reusing_current_matrix 2");
10340 #endif
10341 return 1;
10344 return 0;
10349 /************************************************************************
10350 Window redisplay reusing current matrix when buffer has changed
10351 ************************************************************************/
10353 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10354 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10355 int *, int *));
10356 static struct glyph_row *
10357 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10358 struct glyph_row *));
10361 /* Return the last row in MATRIX displaying text. If row START is
10362 non-null, start searching with that row. IT gives the dimensions
10363 of the display. Value is null if matrix is empty; otherwise it is
10364 a pointer to the row found. */
10366 static struct glyph_row *
10367 find_last_row_displaying_text (matrix, it, start)
10368 struct glyph_matrix *matrix;
10369 struct it *it;
10370 struct glyph_row *start;
10372 struct glyph_row *row, *row_found;
10374 /* Set row_found to the last row in IT->w's current matrix
10375 displaying text. The loop looks funny but think of partially
10376 visible lines. */
10377 row_found = NULL;
10378 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10379 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10381 xassert (row->enabled_p);
10382 row_found = row;
10383 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10384 break;
10385 ++row;
10388 return row_found;
10392 /* Return the last row in the current matrix of W that is not affected
10393 by changes at the start of current_buffer that occurred since the
10394 last time W was redisplayed. Value is null if no such row exists.
10396 The global variable beg_unchanged has to contain the number of
10397 bytes unchanged at the start of current_buffer. BEG +
10398 beg_unchanged is the buffer position of the first changed byte in
10399 current_buffer. Characters at positions < BEG + beg_unchanged are
10400 at the same buffer positions as they were when the current matrix
10401 was built. */
10403 static struct glyph_row *
10404 find_last_unchanged_at_beg_row (w)
10405 struct window *w;
10407 int first_changed_pos = BEG + BEG_UNCHANGED;
10408 struct glyph_row *row;
10409 struct glyph_row *row_found = NULL;
10410 int yb = window_text_bottom_y (w);
10412 /* Find the last row displaying unchanged text. */
10413 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10414 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10415 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10417 if (/* If row ends before first_changed_pos, it is unchanged,
10418 except in some case. */
10419 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10420 /* When row ends in ZV and we write at ZV it is not
10421 unchanged. */
10422 && !row->ends_at_zv_p
10423 /* When first_changed_pos is the end of a continued line,
10424 row is not unchanged because it may be no longer
10425 continued. */
10426 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10427 && row->continued_p))
10428 row_found = row;
10430 /* Stop if last visible row. */
10431 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10432 break;
10434 ++row;
10437 return row_found;
10441 /* Find the first glyph row in the current matrix of W that is not
10442 affected by changes at the end of current_buffer since the last
10443 time the window was redisplayed. Return in *DELTA the number of
10444 chars by which buffer positions in unchanged text at the end of
10445 current_buffer must be adjusted. Return in *DELTA_BYTES the
10446 corresponding number of bytes. Value is null if no such row
10447 exists, i.e. all rows are affected by changes. */
10449 static struct glyph_row *
10450 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10451 struct window *w;
10452 int *delta, *delta_bytes;
10454 struct glyph_row *row;
10455 struct glyph_row *row_found = NULL;
10457 *delta = *delta_bytes = 0;
10459 /* Display must not have been paused, otherwise the current matrix
10460 is not up to date. */
10461 if (NILP (w->window_end_valid))
10462 abort ();
10464 /* A value of window_end_pos >= END_UNCHANGED means that the window
10465 end is in the range of changed text. If so, there is no
10466 unchanged row at the end of W's current matrix. */
10467 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10468 return NULL;
10470 /* Set row to the last row in W's current matrix displaying text. */
10471 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10473 /* If matrix is entirely empty, no unchanged row exists. */
10474 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10476 /* The value of row is the last glyph row in the matrix having a
10477 meaningful buffer position in it. The end position of row
10478 corresponds to window_end_pos. This allows us to translate
10479 buffer positions in the current matrix to current buffer
10480 positions for characters not in changed text. */
10481 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10482 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10483 int last_unchanged_pos, last_unchanged_pos_old;
10484 struct glyph_row *first_text_row
10485 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10487 *delta = Z - Z_old;
10488 *delta_bytes = Z_BYTE - Z_BYTE_old;
10490 /* Set last_unchanged_pos to the buffer position of the last
10491 character in the buffer that has not been changed. Z is the
10492 index + 1 of the last byte in current_buffer, i.e. by
10493 subtracting end_unchanged we get the index of the last
10494 unchanged character, and we have to add BEG to get its buffer
10495 position. */
10496 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10497 last_unchanged_pos_old = last_unchanged_pos - *delta;
10499 /* Search backward from ROW for a row displaying a line that
10500 starts at a minimum position >= last_unchanged_pos_old. */
10501 for (; row > first_text_row; --row)
10503 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10504 abort ();
10506 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10507 row_found = row;
10511 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10512 abort ();
10514 return row_found;
10518 /* Make sure that glyph rows in the current matrix of window W
10519 reference the same glyph memory as corresponding rows in the
10520 frame's frame matrix. This function is called after scrolling W's
10521 current matrix on a terminal frame in try_window_id and
10522 try_window_reusing_current_matrix. */
10524 static void
10525 sync_frame_with_window_matrix_rows (w)
10526 struct window *w;
10528 struct frame *f = XFRAME (w->frame);
10529 struct glyph_row *window_row, *window_row_end, *frame_row;
10531 /* Preconditions: W must be a leaf window and full-width. Its frame
10532 must have a frame matrix. */
10533 xassert (NILP (w->hchild) && NILP (w->vchild));
10534 xassert (WINDOW_FULL_WIDTH_P (w));
10535 xassert (!FRAME_WINDOW_P (f));
10537 /* If W is a full-width window, glyph pointers in W's current matrix
10538 have, by definition, to be the same as glyph pointers in the
10539 corresponding frame matrix. */
10540 window_row = w->current_matrix->rows;
10541 window_row_end = window_row + w->current_matrix->nrows;
10542 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10543 while (window_row < window_row_end)
10545 int area;
10547 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10548 frame_row->glyphs[area] = window_row->glyphs[area];
10550 /* Disable frame rows whose corresponding window rows have
10551 been disabled in try_window_id. */
10552 if (!window_row->enabled_p)
10553 frame_row->enabled_p = 0;
10555 ++window_row, ++frame_row;
10560 /* Find the glyph row in window W containing CHARPOS. Consider all
10561 rows between START and END (not inclusive). END null means search
10562 all rows to the end of the display area of W. Value is the row
10563 containing CHARPOS or null. */
10565 static struct glyph_row *
10566 row_containing_pos (w, charpos, start, end)
10567 struct window *w;
10568 int charpos;
10569 struct glyph_row *start, *end;
10571 struct glyph_row *row = start;
10572 int last_y;
10574 /* If we happen to start on a header-line, skip that. */
10575 if (row->mode_line_p)
10576 ++row;
10578 if ((end && row >= end) || !row->enabled_p)
10579 return NULL;
10581 last_y = window_text_bottom_y (w);
10583 while ((end == NULL || row < end)
10584 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10585 /* The end position of a row equals the start
10586 position of the next row. If CHARPOS is there, we
10587 would rather display it in the next line, except
10588 when this line ends in ZV. */
10589 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10590 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10591 || !row->ends_at_zv_p)))
10592 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10593 ++row;
10595 /* Give up if CHARPOS not found. */
10596 if ((end && row >= end)
10597 || charpos < MATRIX_ROW_START_CHARPOS (row)
10598 || charpos > MATRIX_ROW_END_CHARPOS (row))
10599 row = NULL;
10601 return row;
10605 /* Try to redisplay window W by reusing its existing display. W's
10606 current matrix must be up to date when this function is called,
10607 i.e. window_end_valid must not be nil.
10609 Value is
10611 1 if display has been updated
10612 0 if otherwise unsuccessful
10613 -1 if redisplay with same window start is known not to succeed
10615 The following steps are performed:
10617 1. Find the last row in the current matrix of W that is not
10618 affected by changes at the start of current_buffer. If no such row
10619 is found, give up.
10621 2. Find the first row in W's current matrix that is not affected by
10622 changes at the end of current_buffer. Maybe there is no such row.
10624 3. Display lines beginning with the row + 1 found in step 1 to the
10625 row found in step 2 or, if step 2 didn't find a row, to the end of
10626 the window.
10628 4. If cursor is not known to appear on the window, give up.
10630 5. If display stopped at the row found in step 2, scroll the
10631 display and current matrix as needed.
10633 6. Maybe display some lines at the end of W, if we must. This can
10634 happen under various circumstances, like a partially visible line
10635 becoming fully visible, or because newly displayed lines are displayed
10636 in smaller font sizes.
10638 7. Update W's window end information. */
10640 /* Check that window end is what we expect it to be. */
10642 static int
10643 try_window_id (w)
10644 struct window *w;
10646 struct frame *f = XFRAME (w->frame);
10647 struct glyph_matrix *current_matrix = w->current_matrix;
10648 struct glyph_matrix *desired_matrix = w->desired_matrix;
10649 struct glyph_row *last_unchanged_at_beg_row;
10650 struct glyph_row *first_unchanged_at_end_row;
10651 struct glyph_row *row;
10652 struct glyph_row *bottom_row;
10653 int bottom_vpos;
10654 struct it it;
10655 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10656 struct text_pos start_pos;
10657 struct run run;
10658 int first_unchanged_at_end_vpos = 0;
10659 struct glyph_row *last_text_row, *last_text_row_at_end;
10660 struct text_pos start;
10662 SET_TEXT_POS_FROM_MARKER (start, w->start);
10664 /* Check pre-conditions. Window end must be valid, otherwise
10665 the current matrix would not be up to date. */
10666 xassert (!NILP (w->window_end_valid));
10667 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10668 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10670 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10671 only if buffer has really changed. The reason is that the gap is
10672 initially at Z for freshly visited files. The code below would
10673 set end_unchanged to 0 in that case. */
10674 if (MODIFF > SAVE_MODIFF
10675 /* This seems to happen sometimes after saving a buffer. */
10676 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10678 if (GPT - BEG < BEG_UNCHANGED)
10679 BEG_UNCHANGED = GPT - BEG;
10680 if (Z - GPT < END_UNCHANGED)
10681 END_UNCHANGED = Z - GPT;
10684 /* If window starts after a line end, and the last change is in
10685 front of that newline, then changes don't affect the display.
10686 This case happens with stealth-fontification. Note that although
10687 the display is unchanged, glyph positions in the matrix have to
10688 be adjusted, of course. */
10689 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10690 if (CHARPOS (start) > BEGV
10691 && Z - END_UNCHANGED < CHARPOS (start) - 1
10692 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10693 && PT < MATRIX_ROW_END_CHARPOS (row))
10695 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10696 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10698 if (delta)
10700 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10701 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10703 increment_matrix_positions (w->current_matrix,
10704 MATRIX_ROW_VPOS (r0, current_matrix),
10705 MATRIX_ROW_VPOS (r1, current_matrix),
10706 delta, delta_bytes);
10709 #if 0 /* If changes are all in front of the window start, the
10710 distance of the last displayed glyph from Z hasn't
10711 changed. */
10712 w->window_end_pos
10713 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10714 w->window_end_bytepos
10715 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10716 #endif
10718 return 1;
10721 /* Return quickly if changes are all below what is displayed in the
10722 window, and if PT is in the window. */
10723 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10724 && PT < MATRIX_ROW_END_CHARPOS (row))
10726 /* We have to update window end positions because the buffer's
10727 size has changed. */
10728 w->window_end_pos
10729 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10730 w->window_end_bytepos
10731 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10733 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10734 row = row_containing_pos (w, PT, row, NULL);
10735 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10736 return 2;
10739 /* Check that window start agrees with the start of the first glyph
10740 row in its current matrix. Check this after we know the window
10741 start is not in changed text, otherwise positions would not be
10742 comparable. */
10743 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10744 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10745 return 0;
10747 /* Compute the position at which we have to start displaying new
10748 lines. Some of the lines at the top of the window might be
10749 reusable because they are not displaying changed text. Find the
10750 last row in W's current matrix not affected by changes at the
10751 start of current_buffer. Value is null if changes start in the
10752 first line of window. */
10753 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10754 if (last_unchanged_at_beg_row)
10756 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10757 start_pos = it.current.pos;
10759 /* Start displaying new lines in the desired matrix at the same
10760 vpos we would use in the current matrix, i.e. below
10761 last_unchanged_at_beg_row. */
10762 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10763 current_matrix);
10764 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10765 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10767 xassert (it.hpos == 0 && it.current_x == 0);
10769 else
10771 /* There are no reusable lines at the start of the window.
10772 Start displaying in the first line. */
10773 start_display (&it, w, start);
10774 start_pos = it.current.pos;
10777 /* Find the first row that is not affected by changes at the end of
10778 the buffer. Value will be null if there is no unchanged row, in
10779 which case we must redisplay to the end of the window. delta
10780 will be set to the value by which buffer positions beginning with
10781 first_unchanged_at_end_row have to be adjusted due to text
10782 changes. */
10783 first_unchanged_at_end_row
10784 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10785 IF_DEBUG (debug_delta = delta);
10786 IF_DEBUG (debug_delta_bytes = delta_bytes);
10788 /* Set stop_pos to the buffer position up to which we will have to
10789 display new lines. If first_unchanged_at_end_row != NULL, this
10790 is the buffer position of the start of the line displayed in that
10791 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10792 that we don't stop at a buffer position. */
10793 stop_pos = 0;
10794 if (first_unchanged_at_end_row)
10796 xassert (last_unchanged_at_beg_row == NULL
10797 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10799 /* If this is a continuation line, move forward to the next one
10800 that isn't. Changes in lines above affect this line.
10801 Caution: this may move first_unchanged_at_end_row to a row
10802 not displaying text. */
10803 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10804 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10805 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10806 < it.last_visible_y))
10807 ++first_unchanged_at_end_row;
10809 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10810 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10811 >= it.last_visible_y))
10812 first_unchanged_at_end_row = NULL;
10813 else
10815 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10816 + delta);
10817 first_unchanged_at_end_vpos
10818 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10819 xassert (stop_pos >= Z - END_UNCHANGED);
10822 else if (last_unchanged_at_beg_row == NULL)
10823 return 0;
10826 #if GLYPH_DEBUG
10828 /* Either there is no unchanged row at the end, or the one we have
10829 now displays text. This is a necessary condition for the window
10830 end pos calculation at the end of this function. */
10831 xassert (first_unchanged_at_end_row == NULL
10832 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10834 debug_last_unchanged_at_beg_vpos
10835 = (last_unchanged_at_beg_row
10836 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10837 : -1);
10838 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10840 #endif /* GLYPH_DEBUG != 0 */
10843 /* Display new lines. Set last_text_row to the last new line
10844 displayed which has text on it, i.e. might end up as being the
10845 line where the window_end_vpos is. */
10846 w->cursor.vpos = -1;
10847 last_text_row = NULL;
10848 overlay_arrow_seen = 0;
10849 while (it.current_y < it.last_visible_y
10850 && !fonts_changed_p
10851 && (first_unchanged_at_end_row == NULL
10852 || IT_CHARPOS (it) < stop_pos))
10854 if (display_line (&it))
10855 last_text_row = it.glyph_row - 1;
10858 if (fonts_changed_p)
10859 return -1;
10862 /* Compute differences in buffer positions, y-positions etc. for
10863 lines reused at the bottom of the window. Compute what we can
10864 scroll. */
10865 if (first_unchanged_at_end_row
10866 /* No lines reused because we displayed everything up to the
10867 bottom of the window. */
10868 && it.current_y < it.last_visible_y)
10870 dvpos = (it.vpos
10871 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10872 current_matrix));
10873 dy = it.current_y - first_unchanged_at_end_row->y;
10874 run.current_y = first_unchanged_at_end_row->y;
10875 run.desired_y = run.current_y + dy;
10876 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10878 else
10880 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10881 first_unchanged_at_end_row = NULL;
10883 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10886 /* Find the cursor if not already found. We have to decide whether
10887 PT will appear on this window (it sometimes doesn't, but this is
10888 not a very frequent case.) This decision has to be made before
10889 the current matrix is altered. A value of cursor.vpos < 0 means
10890 that PT is either in one of the lines beginning at
10891 first_unchanged_at_end_row or below the window. Don't care for
10892 lines that might be displayed later at the window end; as
10893 mentioned, this is not a frequent case. */
10894 if (w->cursor.vpos < 0)
10896 /* Cursor in unchanged rows at the top? */
10897 if (PT < CHARPOS (start_pos)
10898 && last_unchanged_at_beg_row)
10900 row = row_containing_pos (w, PT,
10901 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10902 last_unchanged_at_beg_row + 1);
10903 if (row)
10904 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10907 /* Start from first_unchanged_at_end_row looking for PT. */
10908 else if (first_unchanged_at_end_row)
10910 row = row_containing_pos (w, PT - delta,
10911 first_unchanged_at_end_row, NULL);
10912 if (row)
10913 set_cursor_from_row (w, row, w->current_matrix, delta,
10914 delta_bytes, dy, dvpos);
10917 /* Give up if cursor was not found. */
10918 if (w->cursor.vpos < 0)
10920 clear_glyph_matrix (w->desired_matrix);
10921 return -1;
10925 /* Don't let the cursor end in the scroll margins. */
10927 int this_scroll_margin, cursor_height;
10929 this_scroll_margin = max (0, scroll_margin);
10930 this_scroll_margin = min (this_scroll_margin,
10931 XFASTINT (w->height) / 4);
10932 this_scroll_margin *= CANON_Y_UNIT (it.f);
10933 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10935 if ((w->cursor.y < this_scroll_margin
10936 && CHARPOS (start) > BEGV)
10937 /* Don't take scroll margin into account at the bottom because
10938 old redisplay didn't do it either. */
10939 || w->cursor.y + cursor_height > it.last_visible_y)
10941 w->cursor.vpos = -1;
10942 clear_glyph_matrix (w->desired_matrix);
10943 return -1;
10947 /* Scroll the display. Do it before changing the current matrix so
10948 that xterm.c doesn't get confused about where the cursor glyph is
10949 found. */
10950 if (dy && run.height)
10952 update_begin (f);
10954 if (FRAME_WINDOW_P (f))
10956 rif->update_window_begin_hook (w);
10957 rif->clear_mouse_face (w);
10958 rif->scroll_run_hook (w, &run);
10959 rif->update_window_end_hook (w, 0, 0);
10961 else
10963 /* Terminal frame. In this case, dvpos gives the number of
10964 lines to scroll by; dvpos < 0 means scroll up. */
10965 int first_unchanged_at_end_vpos
10966 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10967 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10968 int end = XFASTINT (w->top) + window_internal_height (w);
10970 /* Perform the operation on the screen. */
10971 if (dvpos > 0)
10973 /* Scroll last_unchanged_at_beg_row to the end of the
10974 window down dvpos lines. */
10975 set_terminal_window (end);
10977 /* On dumb terminals delete dvpos lines at the end
10978 before inserting dvpos empty lines. */
10979 if (!scroll_region_ok)
10980 ins_del_lines (end - dvpos, -dvpos);
10982 /* Insert dvpos empty lines in front of
10983 last_unchanged_at_beg_row. */
10984 ins_del_lines (from, dvpos);
10986 else if (dvpos < 0)
10988 /* Scroll up last_unchanged_at_beg_vpos to the end of
10989 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10990 set_terminal_window (end);
10992 /* Delete dvpos lines in front of
10993 last_unchanged_at_beg_vpos. ins_del_lines will set
10994 the cursor to the given vpos and emit |dvpos| delete
10995 line sequences. */
10996 ins_del_lines (from + dvpos, dvpos);
10998 /* On a dumb terminal insert dvpos empty lines at the
10999 end. */
11000 if (!scroll_region_ok)
11001 ins_del_lines (end + dvpos, -dvpos);
11004 set_terminal_window (0);
11007 update_end (f);
11010 /* Shift reused rows of the current matrix to the right position.
11011 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11012 text. */
11013 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11014 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11015 if (dvpos < 0)
11017 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11018 bottom_vpos, dvpos);
11019 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11020 bottom_vpos, 0);
11022 else if (dvpos > 0)
11024 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11025 bottom_vpos, dvpos);
11026 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11027 first_unchanged_at_end_vpos + dvpos, 0);
11030 /* For frame-based redisplay, make sure that current frame and window
11031 matrix are in sync with respect to glyph memory. */
11032 if (!FRAME_WINDOW_P (f))
11033 sync_frame_with_window_matrix_rows (w);
11035 /* Adjust buffer positions in reused rows. */
11036 if (delta)
11037 increment_matrix_positions (current_matrix,
11038 first_unchanged_at_end_vpos + dvpos,
11039 bottom_vpos, delta, delta_bytes);
11041 /* Adjust Y positions. */
11042 if (dy)
11043 shift_glyph_matrix (w, current_matrix,
11044 first_unchanged_at_end_vpos + dvpos,
11045 bottom_vpos, dy);
11047 if (first_unchanged_at_end_row)
11048 first_unchanged_at_end_row += dvpos;
11050 /* If scrolling up, there may be some lines to display at the end of
11051 the window. */
11052 last_text_row_at_end = NULL;
11053 if (dy < 0)
11055 /* Set last_row to the glyph row in the current matrix where the
11056 window end line is found. It has been moved up or down in
11057 the matrix by dvpos. */
11058 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11059 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11061 /* If last_row is the window end line, it should display text. */
11062 xassert (last_row->displays_text_p);
11064 /* If window end line was partially visible before, begin
11065 displaying at that line. Otherwise begin displaying with the
11066 line following it. */
11067 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11069 init_to_row_start (&it, w, last_row);
11070 it.vpos = last_vpos;
11071 it.current_y = last_row->y;
11073 else
11075 init_to_row_end (&it, w, last_row);
11076 it.vpos = 1 + last_vpos;
11077 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11078 ++last_row;
11081 /* We may start in a continuation line. If so, we have to get
11082 the right continuation_lines_width and current_x. */
11083 it.continuation_lines_width = last_row->continuation_lines_width;
11084 it.hpos = it.current_x = 0;
11086 /* Display the rest of the lines at the window end. */
11087 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11088 while (it.current_y < it.last_visible_y
11089 && !fonts_changed_p)
11091 /* Is it always sure that the display agrees with lines in
11092 the current matrix? I don't think so, so we mark rows
11093 displayed invalid in the current matrix by setting their
11094 enabled_p flag to zero. */
11095 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11096 if (display_line (&it))
11097 last_text_row_at_end = it.glyph_row - 1;
11101 /* Update window_end_pos and window_end_vpos. */
11102 if (first_unchanged_at_end_row
11103 && first_unchanged_at_end_row->y < it.last_visible_y
11104 && !last_text_row_at_end)
11106 /* Window end line if one of the preserved rows from the current
11107 matrix. Set row to the last row displaying text in current
11108 matrix starting at first_unchanged_at_end_row, after
11109 scrolling. */
11110 xassert (first_unchanged_at_end_row->displays_text_p);
11111 row = find_last_row_displaying_text (w->current_matrix, &it,
11112 first_unchanged_at_end_row);
11113 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11115 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11116 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11117 XSETFASTINT (w->window_end_vpos,
11118 MATRIX_ROW_VPOS (row, w->current_matrix));
11120 else if (last_text_row_at_end)
11122 XSETFASTINT (w->window_end_pos,
11123 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11124 w->window_end_bytepos
11125 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11126 XSETFASTINT (w->window_end_vpos,
11127 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11129 else if (last_text_row)
11131 /* We have displayed either to the end of the window or at the
11132 end of the window, i.e. the last row with text is to be found
11133 in the desired matrix. */
11134 XSETFASTINT (w->window_end_pos,
11135 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11136 w->window_end_bytepos
11137 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11138 XSETFASTINT (w->window_end_vpos,
11139 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11141 else if (first_unchanged_at_end_row == NULL
11142 && last_text_row == NULL
11143 && last_text_row_at_end == NULL)
11145 /* Displayed to end of window, but no line containing text was
11146 displayed. Lines were deleted at the end of the window. */
11147 int vpos;
11148 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11150 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11151 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11152 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11153 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11154 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11155 break;
11157 w->window_end_vpos = make_number (vpos);
11159 else
11160 abort ();
11162 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11163 debug_end_vpos = XFASTINT (w->window_end_vpos));
11165 /* Record that display has not been completed. */
11166 w->window_end_valid = Qnil;
11167 w->desired_matrix->no_scrolling_p = 1;
11168 return 3;
11173 /***********************************************************************
11174 More debugging support
11175 ***********************************************************************/
11177 #if GLYPH_DEBUG
11179 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11180 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11183 /* Dump the contents of glyph matrix MATRIX on stderr. If
11184 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11186 static void
11187 dump_glyph_matrix (matrix, with_glyphs_p)
11188 struct glyph_matrix *matrix;
11189 int with_glyphs_p;
11191 int i;
11192 for (i = 0; i < matrix->nrows; ++i)
11193 dump_glyph_row (matrix, i, with_glyphs_p);
11197 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11198 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11200 void
11201 dump_glyph_row (matrix, vpos, with_glyphs_p)
11202 struct glyph_matrix *matrix;
11203 int vpos, with_glyphs_p;
11205 struct glyph_row *row;
11207 if (vpos < 0 || vpos >= matrix->nrows)
11208 return;
11210 row = MATRIX_ROW (matrix, vpos);
11212 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11213 fprintf (stderr, "=======================================================================\n");
11215 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11216 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11217 row - matrix->rows,
11218 MATRIX_ROW_START_CHARPOS (row),
11219 MATRIX_ROW_END_CHARPOS (row),
11220 row->used[TEXT_AREA],
11221 row->contains_overlapping_glyphs_p,
11222 row->enabled_p,
11223 row->inverse_p,
11224 row->truncated_on_left_p,
11225 row->truncated_on_right_p,
11226 row->overlay_arrow_p,
11227 row->continued_p,
11228 MATRIX_ROW_CONTINUATION_LINE_P (row),
11229 row->displays_text_p,
11230 row->ends_at_zv_p,
11231 row->fill_line_p,
11232 row->ends_in_middle_of_char_p,
11233 row->starts_in_middle_of_char_p,
11234 row->x,
11235 row->y,
11236 row->pixel_width,
11237 row->height,
11238 row->visible_height,
11239 row->ascent,
11240 row->phys_ascent);
11241 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11242 row->end.overlay_string_index);
11243 fprintf (stderr, "%9d %5d\n",
11244 CHARPOS (row->start.string_pos),
11245 CHARPOS (row->end.string_pos));
11246 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11247 row->end.dpvec_index);
11249 if (with_glyphs_p)
11251 struct glyph *glyph, *glyph_end;
11252 int prev_had_glyphs_p;
11254 glyph = row->glyphs[TEXT_AREA];
11255 glyph_end = glyph + row->used[TEXT_AREA];
11257 /* Glyph for a line end in text. */
11258 if (glyph == glyph_end && glyph->charpos > 0)
11259 ++glyph_end;
11261 if (glyph < glyph_end)
11263 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11264 prev_had_glyphs_p = 1;
11266 else
11267 prev_had_glyphs_p = 0;
11269 while (glyph < glyph_end)
11271 if (glyph->type == CHAR_GLYPH)
11273 fprintf (stderr,
11274 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11275 glyph - row->glyphs[TEXT_AREA],
11276 'C',
11277 glyph->charpos,
11278 (BUFFERP (glyph->object)
11279 ? 'B'
11280 : (STRINGP (glyph->object)
11281 ? 'S'
11282 : '-')),
11283 glyph->pixel_width,
11284 glyph->u.ch,
11285 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11286 ? glyph->u.ch
11287 : '.'),
11288 glyph->face_id,
11289 glyph->left_box_line_p,
11290 glyph->right_box_line_p);
11292 else if (glyph->type == STRETCH_GLYPH)
11294 fprintf (stderr,
11295 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11296 glyph - row->glyphs[TEXT_AREA],
11297 'S',
11298 glyph->charpos,
11299 (BUFFERP (glyph->object)
11300 ? 'B'
11301 : (STRINGP (glyph->object)
11302 ? 'S'
11303 : '-')),
11304 glyph->pixel_width,
11306 '.',
11307 glyph->face_id,
11308 glyph->left_box_line_p,
11309 glyph->right_box_line_p);
11311 else if (glyph->type == IMAGE_GLYPH)
11313 fprintf (stderr,
11314 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11315 glyph - row->glyphs[TEXT_AREA],
11316 'I',
11317 glyph->charpos,
11318 (BUFFERP (glyph->object)
11319 ? 'B'
11320 : (STRINGP (glyph->object)
11321 ? 'S'
11322 : '-')),
11323 glyph->pixel_width,
11324 glyph->u.img_id,
11325 '.',
11326 glyph->face_id,
11327 glyph->left_box_line_p,
11328 glyph->right_box_line_p);
11330 ++glyph;
11336 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11337 Sdump_glyph_matrix, 0, 1, "p",
11338 "Dump the current matrix of the selected window to stderr.\n\
11339 Shows contents of glyph row structures. With non-nil optional\n\
11340 parameter WITH-GLYPHS-P, dump glyphs as well.")
11341 (with_glyphs_p)
11342 Lisp_Object with_glyphs_p;
11344 struct window *w = XWINDOW (selected_window);
11345 struct buffer *buffer = XBUFFER (w->buffer);
11347 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11348 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11349 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11350 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11351 fprintf (stderr, "=============================================\n");
11352 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11353 return Qnil;
11357 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11358 "Dump glyph row ROW to stderr.")
11359 (row)
11360 Lisp_Object row;
11362 CHECK_NUMBER (row, 0);
11363 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11364 return Qnil;
11368 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11369 0, 0, "", "")
11372 struct frame *sf = SELECTED_FRAME ();
11373 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11374 ->current_matrix);
11375 dump_glyph_row (m, 0, 1);
11376 return Qnil;
11380 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11381 Strace_redisplay_toggle, 0, 0, "",
11382 "Toggle tracing of redisplay.")
11385 trace_redisplay_p = !trace_redisplay_p;
11386 return Qnil;
11390 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11391 "Print STRING to stderr.")
11392 (string)
11393 Lisp_Object string;
11395 CHECK_STRING (string, 0);
11396 fprintf (stderr, "%s", XSTRING (string)->data);
11397 return Qnil;
11400 #endif /* GLYPH_DEBUG */
11404 /***********************************************************************
11405 Building Desired Matrix Rows
11406 ***********************************************************************/
11408 /* Return a temporary glyph row holding the glyphs of an overlay
11409 arrow. Only used for non-window-redisplay windows. */
11411 static struct glyph_row *
11412 get_overlay_arrow_glyph_row (w)
11413 struct window *w;
11415 struct frame *f = XFRAME (WINDOW_FRAME (w));
11416 struct buffer *buffer = XBUFFER (w->buffer);
11417 struct buffer *old = current_buffer;
11418 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11419 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11420 unsigned char *arrow_end = arrow_string + arrow_len;
11421 unsigned char *p;
11422 struct it it;
11423 int multibyte_p;
11424 int n_glyphs_before;
11426 set_buffer_temp (buffer);
11427 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11428 it.glyph_row->used[TEXT_AREA] = 0;
11429 SET_TEXT_POS (it.position, 0, 0);
11431 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11432 p = arrow_string;
11433 while (p < arrow_end)
11435 Lisp_Object face, ilisp;
11437 /* Get the next character. */
11438 if (multibyte_p)
11439 it.c = string_char_and_length (p, arrow_len, &it.len);
11440 else
11441 it.c = *p, it.len = 1;
11442 p += it.len;
11444 /* Get its face. */
11445 XSETFASTINT (ilisp, p - arrow_string);
11446 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11447 it.face_id = compute_char_face (f, it.c, face);
11449 /* Compute its width, get its glyphs. */
11450 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11451 SET_TEXT_POS (it.position, -1, -1);
11452 PRODUCE_GLYPHS (&it);
11454 /* If this character doesn't fit any more in the line, we have
11455 to remove some glyphs. */
11456 if (it.current_x > it.last_visible_x)
11458 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11459 break;
11463 set_buffer_temp (old);
11464 return it.glyph_row;
11468 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11469 glyphs are only inserted for terminal frames since we can't really
11470 win with truncation glyphs when partially visible glyphs are
11471 involved. Which glyphs to insert is determined by
11472 produce_special_glyphs. */
11474 static void
11475 insert_left_trunc_glyphs (it)
11476 struct it *it;
11478 struct it truncate_it;
11479 struct glyph *from, *end, *to, *toend;
11481 xassert (!FRAME_WINDOW_P (it->f));
11483 /* Get the truncation glyphs. */
11484 truncate_it = *it;
11485 truncate_it.current_x = 0;
11486 truncate_it.face_id = DEFAULT_FACE_ID;
11487 truncate_it.glyph_row = &scratch_glyph_row;
11488 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11489 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11490 truncate_it.object = make_number (0);
11491 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11493 /* Overwrite glyphs from IT with truncation glyphs. */
11494 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11495 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11496 to = it->glyph_row->glyphs[TEXT_AREA];
11497 toend = to + it->glyph_row->used[TEXT_AREA];
11499 while (from < end)
11500 *to++ = *from++;
11502 /* There may be padding glyphs left over. Remove them. */
11503 from = to;
11504 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11505 ++from;
11506 while (from < toend)
11507 *to++ = *from++;
11509 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11513 /* Compute the pixel height and width of IT->glyph_row.
11515 Most of the time, ascent and height of a display line will be equal
11516 to the max_ascent and max_height values of the display iterator
11517 structure. This is not the case if
11519 1. We hit ZV without displaying anything. In this case, max_ascent
11520 and max_height will be zero.
11522 2. We have some glyphs that don't contribute to the line height.
11523 (The glyph row flag contributes_to_line_height_p is for future
11524 pixmap extensions).
11526 The first case is easily covered by using default values because in
11527 these cases, the line height does not really matter, except that it
11528 must not be zero. */
11530 static void
11531 compute_line_metrics (it)
11532 struct it *it;
11534 struct glyph_row *row = it->glyph_row;
11535 int area, i;
11537 if (FRAME_WINDOW_P (it->f))
11539 int i, header_line_height;
11541 /* The line may consist of one space only, that was added to
11542 place the cursor on it. If so, the row's height hasn't been
11543 computed yet. */
11544 if (row->height == 0)
11546 if (it->max_ascent + it->max_descent == 0)
11547 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11548 row->ascent = it->max_ascent;
11549 row->height = it->max_ascent + it->max_descent;
11550 row->phys_ascent = it->max_phys_ascent;
11551 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11554 /* Compute the width of this line. */
11555 row->pixel_width = row->x;
11556 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11557 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11559 xassert (row->pixel_width >= 0);
11560 xassert (row->ascent >= 0 && row->height > 0);
11562 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11563 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11565 /* If first line's physical ascent is larger than its logical
11566 ascent, use the physical ascent, and make the row taller.
11567 This makes accented characters fully visible. */
11568 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11569 && row->phys_ascent > row->ascent)
11571 row->height += row->phys_ascent - row->ascent;
11572 row->ascent = row->phys_ascent;
11575 /* Compute how much of the line is visible. */
11576 row->visible_height = row->height;
11578 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11579 if (row->y < header_line_height)
11580 row->visible_height -= header_line_height - row->y;
11581 else
11583 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11584 if (row->y + row->height > max_y)
11585 row->visible_height -= row->y + row->height - max_y;
11588 else
11590 row->pixel_width = row->used[TEXT_AREA];
11591 row->ascent = row->phys_ascent = 0;
11592 row->height = row->phys_height = row->visible_height = 1;
11595 /* Compute a hash code for this row. */
11596 row->hash = 0;
11597 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11598 for (i = 0; i < row->used[area]; ++i)
11599 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11600 + row->glyphs[area][i].u.val
11601 + row->glyphs[area][i].face_id
11602 + row->glyphs[area][i].padding_p
11603 + (row->glyphs[area][i].type << 2));
11605 it->max_ascent = it->max_descent = 0;
11606 it->max_phys_ascent = it->max_phys_descent = 0;
11610 /* Append one space to the glyph row of iterator IT if doing a
11611 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11612 space have the default face, otherwise let it have the same face as
11613 IT->face_id. Value is non-zero if a space was added.
11615 This function is called to make sure that there is always one glyph
11616 at the end of a glyph row that the cursor can be set on under
11617 window-systems. (If there weren't such a glyph we would not know
11618 how wide and tall a box cursor should be displayed).
11620 At the same time this space let's a nicely handle clearing to the
11621 end of the line if the row ends in italic text. */
11623 static int
11624 append_space (it, default_face_p)
11625 struct it *it;
11626 int default_face_p;
11628 if (FRAME_WINDOW_P (it->f))
11630 int n = it->glyph_row->used[TEXT_AREA];
11632 if (it->glyph_row->glyphs[TEXT_AREA] + n
11633 < it->glyph_row->glyphs[1 + TEXT_AREA])
11635 /* Save some values that must not be changed.
11636 Must save IT->c and IT->len because otherwise
11637 ITERATOR_AT_END_P wouldn't work anymore after
11638 append_space has been called. */
11639 int saved_what = it->what;
11640 int saved_c = it->c, saved_len = it->len;
11641 int saved_x = it->current_x;
11642 int saved_face_id = it->face_id;
11643 struct text_pos saved_pos;
11644 Lisp_Object saved_object;
11645 struct face *face;
11647 saved_object = it->object;
11648 saved_pos = it->position;
11650 it->what = IT_CHARACTER;
11651 bzero (&it->position, sizeof it->position);
11652 it->object = make_number (0);
11653 it->c = ' ';
11654 it->len = 1;
11656 if (default_face_p)
11657 it->face_id = DEFAULT_FACE_ID;
11658 face = FACE_FROM_ID (it->f, it->face_id);
11659 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11661 PRODUCE_GLYPHS (it);
11663 it->current_x = saved_x;
11664 it->object = saved_object;
11665 it->position = saved_pos;
11666 it->what = saved_what;
11667 it->face_id = saved_face_id;
11668 it->len = saved_len;
11669 it->c = saved_c;
11670 return 1;
11674 return 0;
11678 /* Extend the face of the last glyph in the text area of IT->glyph_row
11679 to the end of the display line. Called from display_line.
11680 If the glyph row is empty, add a space glyph to it so that we
11681 know the face to draw. Set the glyph row flag fill_line_p. */
11683 static void
11684 extend_face_to_end_of_line (it)
11685 struct it *it;
11687 struct face *face;
11688 struct frame *f = it->f;
11690 /* If line is already filled, do nothing. */
11691 if (it->current_x >= it->last_visible_x)
11692 return;
11694 /* Face extension extends the background and box of IT->face_id
11695 to the end of the line. If the background equals the background
11696 of the frame, we haven't to do anything. */
11697 face = FACE_FROM_ID (f, it->face_id);
11698 if (FRAME_WINDOW_P (f)
11699 && face->box == FACE_NO_BOX
11700 && face->background == FRAME_BACKGROUND_PIXEL (f)
11701 && !face->stipple)
11702 return;
11704 /* Set the glyph row flag indicating that the face of the last glyph
11705 in the text area has to be drawn to the end of the text area. */
11706 it->glyph_row->fill_line_p = 1;
11708 /* If current character of IT is not ASCII, make sure we have the
11709 ASCII face. This will be automatically undone the next time
11710 get_next_display_element returns a multibyte character. Note
11711 that the character will always be single byte in unibyte text. */
11712 if (!SINGLE_BYTE_CHAR_P (it->c))
11714 it->face_id = FACE_FOR_CHAR (f, face, 0);
11717 if (FRAME_WINDOW_P (f))
11719 /* If the row is empty, add a space with the current face of IT,
11720 so that we know which face to draw. */
11721 if (it->glyph_row->used[TEXT_AREA] == 0)
11723 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11724 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11725 it->glyph_row->used[TEXT_AREA] = 1;
11728 else
11730 /* Save some values that must not be changed. */
11731 int saved_x = it->current_x;
11732 struct text_pos saved_pos;
11733 Lisp_Object saved_object;
11734 int saved_what = it->what;
11736 saved_object = it->object;
11737 saved_pos = it->position;
11739 it->what = IT_CHARACTER;
11740 bzero (&it->position, sizeof it->position);
11741 it->object = make_number (0);
11742 it->c = ' ';
11743 it->len = 1;
11745 PRODUCE_GLYPHS (it);
11747 while (it->current_x <= it->last_visible_x)
11748 PRODUCE_GLYPHS (it);
11750 /* Don't count these blanks really. It would let us insert a left
11751 truncation glyph below and make us set the cursor on them, maybe. */
11752 it->current_x = saved_x;
11753 it->object = saved_object;
11754 it->position = saved_pos;
11755 it->what = saved_what;
11760 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11761 trailing whitespace. */
11763 static int
11764 trailing_whitespace_p (charpos)
11765 int charpos;
11767 int bytepos = CHAR_TO_BYTE (charpos);
11768 int c = 0;
11770 while (bytepos < ZV_BYTE
11771 && (c = FETCH_CHAR (bytepos),
11772 c == ' ' || c == '\t'))
11773 ++bytepos;
11775 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11777 if (bytepos != PT_BYTE)
11778 return 1;
11780 return 0;
11784 /* Highlight trailing whitespace, if any, in ROW. */
11786 void
11787 highlight_trailing_whitespace (f, row)
11788 struct frame *f;
11789 struct glyph_row *row;
11791 int used = row->used[TEXT_AREA];
11793 if (used)
11795 struct glyph *start = row->glyphs[TEXT_AREA];
11796 struct glyph *glyph = start + used - 1;
11798 /* Skip over the space glyph inserted to display the
11799 cursor at the end of a line. */
11800 if (glyph->type == CHAR_GLYPH
11801 && glyph->u.ch == ' '
11802 && INTEGERP (glyph->object))
11803 --glyph;
11805 /* If last glyph is a space or stretch, and it's trailing
11806 whitespace, set the face of all trailing whitespace glyphs in
11807 IT->glyph_row to `trailing-whitespace'. */
11808 if (glyph >= start
11809 && BUFFERP (glyph->object)
11810 && (glyph->type == STRETCH_GLYPH
11811 || (glyph->type == CHAR_GLYPH
11812 && glyph->u.ch == ' '))
11813 && trailing_whitespace_p (glyph->charpos))
11815 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11817 while (glyph >= start
11818 && BUFFERP (glyph->object)
11819 && (glyph->type == STRETCH_GLYPH
11820 || (glyph->type == CHAR_GLYPH
11821 && glyph->u.ch == ' ')))
11822 (glyph--)->face_id = face_id;
11828 /* Value is non-zero if glyph row ROW in window W should be
11829 used to put the cursor on. */
11831 static int
11832 cursor_row_p (w, row)
11833 struct window *w;
11834 struct glyph_row *row;
11836 int cursor_row_p = 1;
11838 if (PT == MATRIX_ROW_END_CHARPOS (row))
11840 /* If the row ends with a newline from a string, we don't want
11841 the cursor there (if the row is continued it doesn't end in a
11842 newline). */
11843 if (CHARPOS (row->end.string_pos) >= 0
11844 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11845 cursor_row_p = row->continued_p;
11847 /* If the row ends at ZV, display the cursor at the end of that
11848 row instead of at the start of the row below. */
11849 else if (row->ends_at_zv_p)
11850 cursor_row_p = 1;
11851 else
11852 cursor_row_p = 0;
11855 return cursor_row_p;
11859 /* Construct the glyph row IT->glyph_row in the desired matrix of
11860 IT->w from text at the current position of IT. See dispextern.h
11861 for an overview of struct it. Value is non-zero if
11862 IT->glyph_row displays text, as opposed to a line displaying ZV
11863 only. */
11865 static int
11866 display_line (it)
11867 struct it *it;
11869 struct glyph_row *row = it->glyph_row;
11871 /* We always start displaying at hpos zero even if hscrolled. */
11872 xassert (it->hpos == 0 && it->current_x == 0);
11874 /* We must not display in a row that's not a text row. */
11875 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11876 < it->w->desired_matrix->nrows);
11878 /* Is IT->w showing the region? */
11879 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11881 /* Clear the result glyph row and enable it. */
11882 prepare_desired_row (row);
11884 row->y = it->current_y;
11885 row->start = it->current;
11886 row->continuation_lines_width = it->continuation_lines_width;
11887 row->displays_text_p = 1;
11888 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11889 it->starts_in_middle_of_char_p = 0;
11891 /* Arrange the overlays nicely for our purposes. Usually, we call
11892 display_line on only one line at a time, in which case this
11893 can't really hurt too much, or we call it on lines which appear
11894 one after another in the buffer, in which case all calls to
11895 recenter_overlay_lists but the first will be pretty cheap. */
11896 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11898 /* Move over display elements that are not visible because we are
11899 hscrolled. This may stop at an x-position < IT->first_visible_x
11900 if the first glyph is partially visible or if we hit a line end. */
11901 if (it->current_x < it->first_visible_x)
11902 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11903 MOVE_TO_POS | MOVE_TO_X);
11905 /* Get the initial row height. This is either the height of the
11906 text hscrolled, if there is any, or zero. */
11907 row->ascent = it->max_ascent;
11908 row->height = it->max_ascent + it->max_descent;
11909 row->phys_ascent = it->max_phys_ascent;
11910 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11912 /* Loop generating characters. The loop is left with IT on the next
11913 character to display. */
11914 while (1)
11916 int n_glyphs_before, hpos_before, x_before;
11917 int x, i, nglyphs;
11918 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11920 /* Retrieve the next thing to display. Value is zero if end of
11921 buffer reached. */
11922 if (!get_next_display_element (it))
11924 /* Maybe add a space at the end of this line that is used to
11925 display the cursor there under X. Set the charpos of the
11926 first glyph of blank lines not corresponding to any text
11927 to -1. */
11928 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11929 || row->used[TEXT_AREA] == 0)
11931 row->glyphs[TEXT_AREA]->charpos = -1;
11932 row->displays_text_p = 0;
11934 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11935 row->indicate_empty_line_p = 1;
11938 it->continuation_lines_width = 0;
11939 row->ends_at_zv_p = 1;
11940 break;
11943 /* Now, get the metrics of what we want to display. This also
11944 generates glyphs in `row' (which is IT->glyph_row). */
11945 n_glyphs_before = row->used[TEXT_AREA];
11946 x = it->current_x;
11948 /* Remember the line height so far in case the next element doesn't
11949 fit on the line. */
11950 if (!it->truncate_lines_p)
11952 ascent = it->max_ascent;
11953 descent = it->max_descent;
11954 phys_ascent = it->max_phys_ascent;
11955 phys_descent = it->max_phys_descent;
11958 PRODUCE_GLYPHS (it);
11960 /* If this display element was in marginal areas, continue with
11961 the next one. */
11962 if (it->area != TEXT_AREA)
11964 row->ascent = max (row->ascent, it->max_ascent);
11965 row->height = max (row->height, it->max_ascent + it->max_descent);
11966 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11967 row->phys_height = max (row->phys_height,
11968 it->max_phys_ascent + it->max_phys_descent);
11969 set_iterator_to_next (it, 1);
11970 continue;
11973 /* Does the display element fit on the line? If we truncate
11974 lines, we should draw past the right edge of the window. If
11975 we don't truncate, we want to stop so that we can display the
11976 continuation glyph before the right margin. If lines are
11977 continued, there are two possible strategies for characters
11978 resulting in more than 1 glyph (e.g. tabs): Display as many
11979 glyphs as possible in this line and leave the rest for the
11980 continuation line, or display the whole element in the next
11981 line. Original redisplay did the former, so we do it also. */
11982 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11983 hpos_before = it->hpos;
11984 x_before = x;
11986 if (nglyphs == 1
11987 && it->current_x < it->last_visible_x)
11989 ++it->hpos;
11990 row->ascent = max (row->ascent, it->max_ascent);
11991 row->height = max (row->height, it->max_ascent + it->max_descent);
11992 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11993 row->phys_height = max (row->phys_height,
11994 it->max_phys_ascent + it->max_phys_descent);
11995 if (it->current_x - it->pixel_width < it->first_visible_x)
11996 row->x = x - it->first_visible_x;
11998 else
12000 int new_x;
12001 struct glyph *glyph;
12003 for (i = 0; i < nglyphs; ++i, x = new_x)
12005 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12006 new_x = x + glyph->pixel_width;
12008 if (/* Lines are continued. */
12009 !it->truncate_lines_p
12010 && (/* Glyph doesn't fit on the line. */
12011 new_x > it->last_visible_x
12012 /* Or it fits exactly on a window system frame. */
12013 || (new_x == it->last_visible_x
12014 && FRAME_WINDOW_P (it->f))))
12016 /* End of a continued line. */
12018 if (it->hpos == 0
12019 || (new_x == it->last_visible_x
12020 && FRAME_WINDOW_P (it->f)))
12022 /* Current glyph is the only one on the line or
12023 fits exactly on the line. We must continue
12024 the line because we can't draw the cursor
12025 after the glyph. */
12026 row->continued_p = 1;
12027 it->current_x = new_x;
12028 it->continuation_lines_width += new_x;
12029 ++it->hpos;
12030 if (i == nglyphs - 1)
12031 set_iterator_to_next (it, 1);
12033 else if (CHAR_GLYPH_PADDING_P (*glyph)
12034 && !FRAME_WINDOW_P (it->f))
12036 /* A padding glyph that doesn't fit on this line.
12037 This means the whole character doesn't fit
12038 on the line. */
12039 row->used[TEXT_AREA] = n_glyphs_before;
12041 /* Fill the rest of the row with continuation
12042 glyphs like in 20.x. */
12043 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12044 < row->glyphs[1 + TEXT_AREA])
12045 produce_special_glyphs (it, IT_CONTINUATION);
12047 row->continued_p = 1;
12048 it->current_x = x_before;
12049 it->continuation_lines_width += x_before;
12051 /* Restore the height to what it was before the
12052 element not fitting on the line. */
12053 it->max_ascent = ascent;
12054 it->max_descent = descent;
12055 it->max_phys_ascent = phys_ascent;
12056 it->max_phys_descent = phys_descent;
12058 else
12060 /* Display element draws past the right edge of
12061 the window. Restore positions to values
12062 before the element. The next line starts
12063 with current_x before the glyph that could
12064 not be displayed, so that TAB works right. */
12065 row->used[TEXT_AREA] = n_glyphs_before + i;
12067 /* Display continuation glyphs. */
12068 if (!FRAME_WINDOW_P (it->f))
12069 produce_special_glyphs (it, IT_CONTINUATION);
12070 row->continued_p = 1;
12072 it->current_x = x;
12073 it->continuation_lines_width += x;
12074 if (nglyphs > 1 && i > 0)
12076 row->ends_in_middle_of_char_p = 1;
12077 it->starts_in_middle_of_char_p = 1;
12080 /* Restore the height to what it was before the
12081 element not fitting on the line. */
12082 it->max_ascent = ascent;
12083 it->max_descent = descent;
12084 it->max_phys_ascent = phys_ascent;
12085 it->max_phys_descent = phys_descent;
12088 break;
12090 else if (new_x > it->first_visible_x)
12092 /* Increment number of glyphs actually displayed. */
12093 ++it->hpos;
12095 if (x < it->first_visible_x)
12096 /* Glyph is partially visible, i.e. row starts at
12097 negative X position. */
12098 row->x = x - it->first_visible_x;
12100 else
12102 /* Glyph is completely off the left margin of the
12103 window. This should not happen because of the
12104 move_it_in_display_line at the start of
12105 this function. */
12106 abort ();
12110 row->ascent = max (row->ascent, it->max_ascent);
12111 row->height = max (row->height, it->max_ascent + it->max_descent);
12112 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12113 row->phys_height = max (row->phys_height,
12114 it->max_phys_ascent + it->max_phys_descent);
12116 /* End of this display line if row is continued. */
12117 if (row->continued_p)
12118 break;
12121 /* Is this a line end? If yes, we're also done, after making
12122 sure that a non-default face is extended up to the right
12123 margin of the window. */
12124 if (ITERATOR_AT_END_OF_LINE_P (it))
12126 int used_before = row->used[TEXT_AREA];
12128 /* Add a space at the end of the line that is used to
12129 display the cursor there. */
12130 append_space (it, 0);
12132 /* Extend the face to the end of the line. */
12133 extend_face_to_end_of_line (it);
12135 /* Make sure we have the position. */
12136 if (used_before == 0)
12137 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12139 /* Consume the line end. This skips over invisible lines. */
12140 set_iterator_to_next (it, 1);
12141 it->continuation_lines_width = 0;
12142 break;
12145 /* Proceed with next display element. Note that this skips
12146 over lines invisible because of selective display. */
12147 set_iterator_to_next (it, 1);
12149 /* If we truncate lines, we are done when the last displayed
12150 glyphs reach past the right margin of the window. */
12151 if (it->truncate_lines_p
12152 && (FRAME_WINDOW_P (it->f)
12153 ? (it->current_x >= it->last_visible_x)
12154 : (it->current_x > it->last_visible_x)))
12156 /* Maybe add truncation glyphs. */
12157 if (!FRAME_WINDOW_P (it->f))
12159 --it->glyph_row->used[TEXT_AREA];
12160 produce_special_glyphs (it, IT_TRUNCATION);
12163 row->truncated_on_right_p = 1;
12164 it->continuation_lines_width = 0;
12165 reseat_at_next_visible_line_start (it, 0);
12166 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12167 it->hpos = hpos_before;
12168 it->current_x = x_before;
12169 break;
12173 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12174 at the left window margin. */
12175 if (it->first_visible_x
12176 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12178 if (!FRAME_WINDOW_P (it->f))
12179 insert_left_trunc_glyphs (it);
12180 row->truncated_on_left_p = 1;
12183 /* If the start of this line is the overlay arrow-position, then
12184 mark this glyph row as the one containing the overlay arrow.
12185 This is clearly a mess with variable size fonts. It would be
12186 better to let it be displayed like cursors under X. */
12187 if (MARKERP (Voverlay_arrow_position)
12188 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12189 && (MATRIX_ROW_START_CHARPOS (row)
12190 == marker_position (Voverlay_arrow_position))
12191 && STRINGP (Voverlay_arrow_string)
12192 && ! overlay_arrow_seen)
12194 /* Overlay arrow in window redisplay is a bitmap. */
12195 if (!FRAME_WINDOW_P (it->f))
12197 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12198 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12199 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12200 struct glyph *p = row->glyphs[TEXT_AREA];
12201 struct glyph *p2, *end;
12203 /* Copy the arrow glyphs. */
12204 while (glyph < arrow_end)
12205 *p++ = *glyph++;
12207 /* Throw away padding glyphs. */
12208 p2 = p;
12209 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12210 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12211 ++p2;
12212 if (p2 > p)
12214 while (p2 < end)
12215 *p++ = *p2++;
12216 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12220 overlay_arrow_seen = 1;
12221 row->overlay_arrow_p = 1;
12224 /* Compute pixel dimensions of this line. */
12225 compute_line_metrics (it);
12227 /* Remember the position at which this line ends. */
12228 row->end = it->current;
12230 /* Maybe set the cursor. */
12231 if (it->w->cursor.vpos < 0
12232 && PT >= MATRIX_ROW_START_CHARPOS (row)
12233 && PT <= MATRIX_ROW_END_CHARPOS (row)
12234 && cursor_row_p (it->w, row))
12235 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12237 /* Highlight trailing whitespace. */
12238 if (!NILP (Vshow_trailing_whitespace))
12239 highlight_trailing_whitespace (it->f, it->glyph_row);
12241 /* Prepare for the next line. This line starts horizontally at (X
12242 HPOS) = (0 0). Vertical positions are incremented. As a
12243 convenience for the caller, IT->glyph_row is set to the next
12244 row to be used. */
12245 it->current_x = it->hpos = 0;
12246 it->current_y += row->height;
12247 ++it->vpos;
12248 ++it->glyph_row;
12249 return row->displays_text_p;
12254 /***********************************************************************
12255 Menu Bar
12256 ***********************************************************************/
12258 /* Redisplay the menu bar in the frame for window W.
12260 The menu bar of X frames that don't have X toolkit support is
12261 displayed in a special window W->frame->menu_bar_window.
12263 The menu bar of terminal frames is treated specially as far as
12264 glyph matrices are concerned. Menu bar lines are not part of
12265 windows, so the update is done directly on the frame matrix rows
12266 for the menu bar. */
12268 static void
12269 display_menu_bar (w)
12270 struct window *w;
12272 struct frame *f = XFRAME (WINDOW_FRAME (w));
12273 struct it it;
12274 Lisp_Object items;
12275 int i;
12277 /* Don't do all this for graphical frames. */
12278 #ifdef HAVE_NTGUI
12279 if (!NILP (Vwindow_system))
12280 return;
12281 #endif
12282 #ifdef USE_X_TOOLKIT
12283 if (FRAME_X_P (f))
12284 return;
12285 #endif
12286 #ifdef macintosh
12287 if (FRAME_MAC_P (f))
12288 return;
12289 #endif
12291 #ifdef USE_X_TOOLKIT
12292 xassert (!FRAME_WINDOW_P (f));
12293 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12294 it.first_visible_x = 0;
12295 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12296 #else /* not USE_X_TOOLKIT */
12297 if (FRAME_WINDOW_P (f))
12299 /* Menu bar lines are displayed in the desired matrix of the
12300 dummy window menu_bar_window. */
12301 struct window *menu_w;
12302 xassert (WINDOWP (f->menu_bar_window));
12303 menu_w = XWINDOW (f->menu_bar_window);
12304 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12305 MENU_FACE_ID);
12306 it.first_visible_x = 0;
12307 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12309 else
12311 /* This is a TTY frame, i.e. character hpos/vpos are used as
12312 pixel x/y. */
12313 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12314 MENU_FACE_ID);
12315 it.first_visible_x = 0;
12316 it.last_visible_x = FRAME_WIDTH (f);
12318 #endif /* not USE_X_TOOLKIT */
12320 /* Clear all rows of the menu bar. */
12321 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12323 struct glyph_row *row = it.glyph_row + i;
12324 clear_glyph_row (row);
12325 row->enabled_p = 1;
12326 row->full_width_p = 1;
12329 /* Make the first line of the menu bar appear in reverse video. */
12330 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12332 /* Display all items of the menu bar. */
12333 items = FRAME_MENU_BAR_ITEMS (it.f);
12334 for (i = 0; i < XVECTOR (items)->size; i += 4)
12336 Lisp_Object string;
12338 /* Stop at nil string. */
12339 string = XVECTOR (items)->contents[i + 1];
12340 if (NILP (string))
12341 break;
12343 /* Remember where item was displayed. */
12344 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12346 /* Display the item, pad with one space. */
12347 if (it.current_x < it.last_visible_x)
12348 display_string (NULL, string, Qnil, 0, 0, &it,
12349 XSTRING (string)->size + 1, 0, 0, -1);
12352 /* Fill out the line with spaces. */
12353 if (it.current_x < it.last_visible_x)
12354 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12356 /* Compute the total height of the lines. */
12357 compute_line_metrics (&it);
12362 /***********************************************************************
12363 Mode Line
12364 ***********************************************************************/
12366 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12367 FORCE is non-zero, redisplay mode lines unconditionally.
12368 Otherwise, redisplay only mode lines that are garbaged. Value is
12369 the number of windows whose mode lines were redisplayed. */
12371 static int
12372 redisplay_mode_lines (window, force)
12373 Lisp_Object window;
12374 int force;
12376 int nwindows = 0;
12378 while (!NILP (window))
12380 struct window *w = XWINDOW (window);
12382 if (WINDOWP (w->hchild))
12383 nwindows += redisplay_mode_lines (w->hchild, force);
12384 else if (WINDOWP (w->vchild))
12385 nwindows += redisplay_mode_lines (w->vchild, force);
12386 else if (force
12387 || FRAME_GARBAGED_P (XFRAME (w->frame))
12388 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12390 Lisp_Object old_selected_frame;
12391 struct text_pos lpoint;
12392 struct buffer *old = current_buffer;
12394 /* Set the window's buffer for the mode line display. */
12395 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12396 set_buffer_internal_1 (XBUFFER (w->buffer));
12398 /* Point refers normally to the selected window. For any
12399 other window, set up appropriate value. */
12400 if (!EQ (window, selected_window))
12402 struct text_pos pt;
12404 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12405 if (CHARPOS (pt) < BEGV)
12406 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12407 else if (CHARPOS (pt) > (ZV - 1))
12408 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12409 else
12410 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12413 /* Temporarily set up the selected frame. */
12414 old_selected_frame = selected_frame;
12415 selected_frame = w->frame;
12417 /* Display mode lines. */
12418 clear_glyph_matrix (w->desired_matrix);
12419 if (display_mode_lines (w))
12421 ++nwindows;
12422 w->must_be_updated_p = 1;
12425 /* Restore old settings. */
12426 selected_frame = old_selected_frame;
12427 set_buffer_internal_1 (old);
12428 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12431 window = w->next;
12434 return nwindows;
12438 /* Display the mode and/or top line of window W. Value is the number
12439 of mode lines displayed. */
12441 static int
12442 display_mode_lines (w)
12443 struct window *w;
12445 int n = 0;
12447 /* These will be set while the mode line specs are processed. */
12448 line_number_displayed = 0;
12449 w->column_number_displayed = Qnil;
12451 if (WINDOW_WANTS_MODELINE_P (w))
12453 display_mode_line (w, MODE_LINE_FACE_ID,
12454 current_buffer->mode_line_format);
12455 ++n;
12458 if (WINDOW_WANTS_HEADER_LINE_P (w))
12460 display_mode_line (w, HEADER_LINE_FACE_ID,
12461 current_buffer->header_line_format);
12462 ++n;
12465 return n;
12469 /* Display mode or top line of window W. FACE_ID specifies which line
12470 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12471 FORMAT is the mode line format to display. Value is the pixel
12472 height of the mode line displayed. */
12474 static int
12475 display_mode_line (w, face_id, format)
12476 struct window *w;
12477 enum face_id face_id;
12478 Lisp_Object format;
12480 struct it it;
12481 struct face *face;
12483 init_iterator (&it, w, -1, -1, NULL, face_id);
12484 prepare_desired_row (it.glyph_row);
12486 /* Temporarily make frame's keyboard the current kboard so that
12487 kboard-local variables in the mode_line_format will get the right
12488 values. */
12489 push_frame_kboard (it.f);
12490 display_mode_element (&it, 0, 0, 0, format);
12491 pop_frame_kboard ();
12493 /* Fill up with spaces. */
12494 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12496 compute_line_metrics (&it);
12497 it.glyph_row->full_width_p = 1;
12498 it.glyph_row->mode_line_p = 1;
12499 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12500 it.glyph_row->continued_p = 0;
12501 it.glyph_row->truncated_on_left_p = 0;
12502 it.glyph_row->truncated_on_right_p = 0;
12504 /* Make a 3D mode-line have a shadow at its right end. */
12505 face = FACE_FROM_ID (it.f, face_id);
12506 extend_face_to_end_of_line (&it);
12507 if (face->box != FACE_NO_BOX)
12509 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12510 + it.glyph_row->used[TEXT_AREA] - 1);
12511 last->right_box_line_p = 1;
12514 return it.glyph_row->height;
12518 /* Contribute ELT to the mode line for window IT->w. How it
12519 translates into text depends on its data type.
12521 IT describes the display environment in which we display, as usual.
12523 DEPTH is the depth in recursion. It is used to prevent
12524 infinite recursion here.
12526 FIELD_WIDTH is the number of characters the display of ELT should
12527 occupy in the mode line, and PRECISION is the maximum number of
12528 characters to display from ELT's representation. See
12529 display_string for details. *
12531 Returns the hpos of the end of the text generated by ELT. */
12533 static int
12534 display_mode_element (it, depth, field_width, precision, elt)
12535 struct it *it;
12536 int depth;
12537 int field_width, precision;
12538 Lisp_Object elt;
12540 int n = 0, field, prec;
12542 tail_recurse:
12543 if (depth > 10)
12544 goto invalid;
12546 depth++;
12548 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12550 case Lisp_String:
12552 /* A string: output it and check for %-constructs within it. */
12553 unsigned char c;
12554 unsigned char *this = XSTRING (elt)->data;
12555 unsigned char *lisp_string = this;
12557 while ((precision <= 0 || n < precision)
12558 && *this
12559 && (frame_title_ptr
12560 || it->current_x < it->last_visible_x))
12562 unsigned char *last = this;
12564 /* Advance to end of string or next format specifier. */
12565 while ((c = *this++) != '\0' && c != '%')
12568 if (this - 1 != last)
12570 /* Output to end of string or up to '%'. Field width
12571 is length of string. Don't output more than
12572 PRECISION allows us. */
12573 prec = --this - last;
12574 if (precision > 0 && prec > precision - n)
12575 prec = precision - n;
12577 if (frame_title_ptr)
12578 n += store_frame_title (last, prec, prec);
12579 else
12580 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12581 it, 0, prec, 0, -1);
12583 else /* c == '%' */
12585 unsigned char *percent_position = this;
12587 /* Get the specified minimum width. Zero means
12588 don't pad. */
12589 field = 0;
12590 while ((c = *this++) >= '0' && c <= '9')
12591 field = field * 10 + c - '0';
12593 /* Don't pad beyond the total padding allowed. */
12594 if (field_width - n > 0 && field > field_width - n)
12595 field = field_width - n;
12597 /* Note that either PRECISION <= 0 or N < PRECISION. */
12598 prec = precision - n;
12600 if (c == 'M')
12601 n += display_mode_element (it, depth, field, prec,
12602 Vglobal_mode_string);
12603 else if (c != 0)
12605 unsigned char *spec
12606 = decode_mode_spec (it->w, c, field, prec);
12608 if (frame_title_ptr)
12609 n += store_frame_title (spec, field, prec);
12610 else
12612 int nglyphs_before
12613 = it->glyph_row->used[TEXT_AREA];
12614 int charpos
12615 = percent_position - XSTRING (elt)->data;
12616 int nwritten
12617 = display_string (spec, Qnil, elt, charpos, 0, it,
12618 field, prec, 0, -1);
12620 /* Assign to the glyphs written above the
12621 string where the `%x' came from, position
12622 of the `%'. */
12623 if (nwritten > 0)
12625 struct glyph *glyph
12626 = (it->glyph_row->glyphs[TEXT_AREA]
12627 + nglyphs_before);
12628 int i;
12630 for (i = 0; i < nwritten; ++i)
12632 glyph[i].object = elt;
12633 glyph[i].charpos = charpos;
12636 n += nwritten;
12643 break;
12645 case Lisp_Symbol:
12646 /* A symbol: process the value of the symbol recursively
12647 as if it appeared here directly. Avoid error if symbol void.
12648 Special case: if value of symbol is a string, output the string
12649 literally. */
12651 register Lisp_Object tem;
12652 tem = Fboundp (elt);
12653 if (!NILP (tem))
12655 tem = Fsymbol_value (elt);
12656 /* If value is a string, output that string literally:
12657 don't check for % within it. */
12658 if (STRINGP (tem))
12660 prec = XSTRING (tem)->size;
12661 if (precision > 0 && prec > precision - n)
12662 prec = precision - n;
12663 if (frame_title_ptr)
12664 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12665 else
12666 n += display_string (NULL, tem, Qnil, 0, 0, it,
12667 0, prec, 0, -1);
12669 else if (!EQ (tem, elt))
12671 /* Give up right away for nil or t. */
12672 elt = tem;
12673 goto tail_recurse;
12677 break;
12679 case Lisp_Cons:
12681 register Lisp_Object car, tem;
12683 /* A cons cell: three distinct cases.
12684 If first element is a string or a cons, process all the elements
12685 and effectively concatenate them.
12686 If first element is a negative number, truncate displaying cdr to
12687 at most that many characters. If positive, pad (with spaces)
12688 to at least that many characters.
12689 If first element is a symbol, process the cadr or caddr recursively
12690 according to whether the symbol's value is non-nil or nil. */
12691 car = XCAR (elt);
12692 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12694 /* An element of the form (:eval FORM) means evaluate FORM
12695 and use the result as mode line elements. */
12696 struct gcpro gcpro1;
12697 Lisp_Object spec;
12699 spec = safe_eval (XCAR (XCDR (elt)));
12700 GCPRO1 (spec);
12701 n += display_mode_element (it, depth, field_width - n,
12702 precision - n, spec);
12703 UNGCPRO;
12705 else if (SYMBOLP (car))
12707 tem = Fboundp (car);
12708 elt = XCDR (elt);
12709 if (!CONSP (elt))
12710 goto invalid;
12711 /* elt is now the cdr, and we know it is a cons cell.
12712 Use its car if CAR has a non-nil value. */
12713 if (!NILP (tem))
12715 tem = Fsymbol_value (car);
12716 if (!NILP (tem))
12718 elt = XCAR (elt);
12719 goto tail_recurse;
12722 /* Symbol's value is nil (or symbol is unbound)
12723 Get the cddr of the original list
12724 and if possible find the caddr and use that. */
12725 elt = XCDR (elt);
12726 if (NILP (elt))
12727 break;
12728 else if (!CONSP (elt))
12729 goto invalid;
12730 elt = XCAR (elt);
12731 goto tail_recurse;
12733 else if (INTEGERP (car))
12735 register int lim = XINT (car);
12736 elt = XCDR (elt);
12737 if (lim < 0)
12739 /* Negative int means reduce maximum width. */
12740 if (precision <= 0)
12741 precision = -lim;
12742 else
12743 precision = min (precision, -lim);
12745 else if (lim > 0)
12747 /* Padding specified. Don't let it be more than
12748 current maximum. */
12749 if (precision > 0)
12750 lim = min (precision, lim);
12752 /* If that's more padding than already wanted, queue it.
12753 But don't reduce padding already specified even if
12754 that is beyond the current truncation point. */
12755 field_width = max (lim, field_width);
12757 goto tail_recurse;
12759 else if (STRINGP (car) || CONSP (car))
12761 register int limit = 50;
12762 /* Limit is to protect against circular lists. */
12763 while (CONSP (elt)
12764 && --limit > 0
12765 && (precision <= 0 || n < precision))
12767 n += display_mode_element (it, depth, field_width - n,
12768 precision - n, XCAR (elt));
12769 elt = XCDR (elt);
12773 break;
12775 default:
12776 invalid:
12777 if (frame_title_ptr)
12778 n += store_frame_title ("*invalid*", 0, precision - n);
12779 else
12780 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12781 precision - n, 0, 0);
12782 return n;
12785 /* Pad to FIELD_WIDTH. */
12786 if (field_width > 0 && n < field_width)
12788 if (frame_title_ptr)
12789 n += store_frame_title ("", field_width - n, 0);
12790 else
12791 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12792 0, 0, 0);
12795 return n;
12799 /* Write a null-terminated, right justified decimal representation of
12800 the positive integer D to BUF using a minimal field width WIDTH. */
12802 static void
12803 pint2str (buf, width, d)
12804 register char *buf;
12805 register int width;
12806 register int d;
12808 register char *p = buf;
12810 if (d <= 0)
12811 *p++ = '0';
12812 else
12814 while (d > 0)
12816 *p++ = d % 10 + '0';
12817 d /= 10;
12821 for (width -= (int) (p - buf); width > 0; --width)
12822 *p++ = ' ';
12823 *p-- = '\0';
12824 while (p > buf)
12826 d = *buf;
12827 *buf++ = *p;
12828 *p-- = d;
12832 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12833 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12834 type of CODING_SYSTEM. Return updated pointer into BUF. */
12836 static unsigned char invalid_eol_type[] = "(*invalid*)";
12838 static char *
12839 decode_mode_spec_coding (coding_system, buf, eol_flag)
12840 Lisp_Object coding_system;
12841 register char *buf;
12842 int eol_flag;
12844 Lisp_Object val;
12845 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12846 unsigned char *eol_str;
12847 int eol_str_len;
12848 /* The EOL conversion we are using. */
12849 Lisp_Object eoltype;
12851 val = Fget (coding_system, Qcoding_system);
12852 eoltype = Qnil;
12854 if (!VECTORP (val)) /* Not yet decided. */
12856 if (multibyte)
12857 *buf++ = '-';
12858 if (eol_flag)
12859 eoltype = eol_mnemonic_undecided;
12860 /* Don't mention EOL conversion if it isn't decided. */
12862 else
12864 Lisp_Object eolvalue;
12866 eolvalue = Fget (coding_system, Qeol_type);
12868 if (multibyte)
12869 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12871 if (eol_flag)
12873 /* The EOL conversion that is normal on this system. */
12875 if (NILP (eolvalue)) /* Not yet decided. */
12876 eoltype = eol_mnemonic_undecided;
12877 else if (VECTORP (eolvalue)) /* Not yet decided. */
12878 eoltype = eol_mnemonic_undecided;
12879 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12880 eoltype = (XFASTINT (eolvalue) == 0
12881 ? eol_mnemonic_unix
12882 : (XFASTINT (eolvalue) == 1
12883 ? eol_mnemonic_dos : eol_mnemonic_mac));
12887 if (eol_flag)
12889 /* Mention the EOL conversion if it is not the usual one. */
12890 if (STRINGP (eoltype))
12892 eol_str = XSTRING (eoltype)->data;
12893 eol_str_len = XSTRING (eoltype)->size;
12895 else if (INTEGERP (eoltype)
12896 && CHAR_VALID_P (XINT (eoltype), 0))
12898 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12899 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12901 else
12903 eol_str = invalid_eol_type;
12904 eol_str_len = sizeof (invalid_eol_type) - 1;
12906 bcopy (eol_str, buf, eol_str_len);
12907 buf += eol_str_len;
12910 return buf;
12913 /* Return a string for the output of a mode line %-spec for window W,
12914 generated by character C. PRECISION >= 0 means don't return a
12915 string longer than that value. FIELD_WIDTH > 0 means pad the
12916 string returned with spaces to that value. */
12918 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12920 static char *
12921 decode_mode_spec (w, c, field_width, precision)
12922 struct window *w;
12923 register int c;
12924 int field_width, precision;
12926 Lisp_Object obj;
12927 struct frame *f = XFRAME (WINDOW_FRAME (w));
12928 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12929 struct buffer *b = XBUFFER (w->buffer);
12931 obj = Qnil;
12933 switch (c)
12935 case '*':
12936 if (!NILP (b->read_only))
12937 return "%";
12938 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12939 return "*";
12940 return "-";
12942 case '+':
12943 /* This differs from %* only for a modified read-only buffer. */
12944 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12945 return "*";
12946 if (!NILP (b->read_only))
12947 return "%";
12948 return "-";
12950 case '&':
12951 /* This differs from %* in ignoring read-only-ness. */
12952 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12953 return "*";
12954 return "-";
12956 case '%':
12957 return "%";
12959 case '[':
12961 int i;
12962 char *p;
12964 if (command_loop_level > 5)
12965 return "[[[... ";
12966 p = decode_mode_spec_buf;
12967 for (i = 0; i < command_loop_level; i++)
12968 *p++ = '[';
12969 *p = 0;
12970 return decode_mode_spec_buf;
12973 case ']':
12975 int i;
12976 char *p;
12978 if (command_loop_level > 5)
12979 return " ...]]]";
12980 p = decode_mode_spec_buf;
12981 for (i = 0; i < command_loop_level; i++)
12982 *p++ = ']';
12983 *p = 0;
12984 return decode_mode_spec_buf;
12987 case '-':
12989 register int i;
12991 /* Let lots_of_dashes be a string of infinite length. */
12992 if (field_width <= 0
12993 || field_width > sizeof (lots_of_dashes))
12995 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12996 decode_mode_spec_buf[i] = '-';
12997 decode_mode_spec_buf[i] = '\0';
12998 return decode_mode_spec_buf;
13000 else
13001 return lots_of_dashes;
13004 case 'b':
13005 obj = b->name;
13006 break;
13008 case 'c':
13010 int col = current_column ();
13011 XSETFASTINT (w->column_number_displayed, col);
13012 pint2str (decode_mode_spec_buf, field_width, col);
13013 return decode_mode_spec_buf;
13016 case 'F':
13017 /* %F displays the frame name. */
13018 if (!NILP (f->title))
13019 return (char *) XSTRING (f->title)->data;
13020 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13021 return (char *) XSTRING (f->name)->data;
13022 return "Emacs";
13024 case 'f':
13025 obj = b->filename;
13026 break;
13028 case 'l':
13030 int startpos = XMARKER (w->start)->charpos;
13031 int startpos_byte = marker_byte_position (w->start);
13032 int line, linepos, linepos_byte, topline;
13033 int nlines, junk;
13034 int height = XFASTINT (w->height);
13036 /* If we decided that this buffer isn't suitable for line numbers,
13037 don't forget that too fast. */
13038 if (EQ (w->base_line_pos, w->buffer))
13039 goto no_value;
13040 /* But do forget it, if the window shows a different buffer now. */
13041 else if (BUFFERP (w->base_line_pos))
13042 w->base_line_pos = Qnil;
13044 /* If the buffer is very big, don't waste time. */
13045 if (INTEGERP (Vline_number_display_limit)
13046 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13048 w->base_line_pos = Qnil;
13049 w->base_line_number = Qnil;
13050 goto no_value;
13053 if (!NILP (w->base_line_number)
13054 && !NILP (w->base_line_pos)
13055 && XFASTINT (w->base_line_pos) <= startpos)
13057 line = XFASTINT (w->base_line_number);
13058 linepos = XFASTINT (w->base_line_pos);
13059 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13061 else
13063 line = 1;
13064 linepos = BUF_BEGV (b);
13065 linepos_byte = BUF_BEGV_BYTE (b);
13068 /* Count lines from base line to window start position. */
13069 nlines = display_count_lines (linepos, linepos_byte,
13070 startpos_byte,
13071 startpos, &junk);
13073 topline = nlines + line;
13075 /* Determine a new base line, if the old one is too close
13076 or too far away, or if we did not have one.
13077 "Too close" means it's plausible a scroll-down would
13078 go back past it. */
13079 if (startpos == BUF_BEGV (b))
13081 XSETFASTINT (w->base_line_number, topline);
13082 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13084 else if (nlines < height + 25 || nlines > height * 3 + 50
13085 || linepos == BUF_BEGV (b))
13087 int limit = BUF_BEGV (b);
13088 int limit_byte = BUF_BEGV_BYTE (b);
13089 int position;
13090 int distance = (height * 2 + 30) * line_number_display_limit_width;
13092 if (startpos - distance > limit)
13094 limit = startpos - distance;
13095 limit_byte = CHAR_TO_BYTE (limit);
13098 nlines = display_count_lines (startpos, startpos_byte,
13099 limit_byte,
13100 - (height * 2 + 30),
13101 &position);
13102 /* If we couldn't find the lines we wanted within
13103 line_number_display_limit_width chars per line,
13104 give up on line numbers for this window. */
13105 if (position == limit_byte && limit == startpos - distance)
13107 w->base_line_pos = w->buffer;
13108 w->base_line_number = Qnil;
13109 goto no_value;
13112 XSETFASTINT (w->base_line_number, topline - nlines);
13113 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13116 /* Now count lines from the start pos to point. */
13117 nlines = display_count_lines (startpos, startpos_byte,
13118 PT_BYTE, PT, &junk);
13120 /* Record that we did display the line number. */
13121 line_number_displayed = 1;
13123 /* Make the string to show. */
13124 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13125 return decode_mode_spec_buf;
13126 no_value:
13128 char* p = decode_mode_spec_buf;
13129 int pad = field_width - 2;
13130 while (pad-- > 0)
13131 *p++ = ' ';
13132 *p++ = '?';
13133 *p++ = '?';
13134 *p = '\0';
13135 return decode_mode_spec_buf;
13138 break;
13140 case 'm':
13141 obj = b->mode_name;
13142 break;
13144 case 'n':
13145 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13146 return " Narrow";
13147 break;
13149 case 'p':
13151 int pos = marker_position (w->start);
13152 int total = BUF_ZV (b) - BUF_BEGV (b);
13154 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13156 if (pos <= BUF_BEGV (b))
13157 return "All";
13158 else
13159 return "Bottom";
13161 else if (pos <= BUF_BEGV (b))
13162 return "Top";
13163 else
13165 if (total > 1000000)
13166 /* Do it differently for a large value, to avoid overflow. */
13167 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13168 else
13169 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13170 /* We can't normally display a 3-digit number,
13171 so get us a 2-digit number that is close. */
13172 if (total == 100)
13173 total = 99;
13174 sprintf (decode_mode_spec_buf, "%2d%%", total);
13175 return decode_mode_spec_buf;
13179 /* Display percentage of size above the bottom of the screen. */
13180 case 'P':
13182 int toppos = marker_position (w->start);
13183 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13184 int total = BUF_ZV (b) - BUF_BEGV (b);
13186 if (botpos >= BUF_ZV (b))
13188 if (toppos <= BUF_BEGV (b))
13189 return "All";
13190 else
13191 return "Bottom";
13193 else
13195 if (total > 1000000)
13196 /* Do it differently for a large value, to avoid overflow. */
13197 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13198 else
13199 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13200 /* We can't normally display a 3-digit number,
13201 so get us a 2-digit number that is close. */
13202 if (total == 100)
13203 total = 99;
13204 if (toppos <= BUF_BEGV (b))
13205 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13206 else
13207 sprintf (decode_mode_spec_buf, "%2d%%", total);
13208 return decode_mode_spec_buf;
13212 case 's':
13213 /* status of process */
13214 obj = Fget_buffer_process (w->buffer);
13215 if (NILP (obj))
13216 return "no process";
13217 #ifdef subprocesses
13218 obj = Fsymbol_name (Fprocess_status (obj));
13219 #endif
13220 break;
13222 case 't': /* indicate TEXT or BINARY */
13223 #ifdef MODE_LINE_BINARY_TEXT
13224 return MODE_LINE_BINARY_TEXT (b);
13225 #else
13226 return "T";
13227 #endif
13229 case 'z':
13230 /* coding-system (not including end-of-line format) */
13231 case 'Z':
13232 /* coding-system (including end-of-line type) */
13234 int eol_flag = (c == 'Z');
13235 char *p = decode_mode_spec_buf;
13237 if (! FRAME_WINDOW_P (f))
13239 /* No need to mention EOL here--the terminal never needs
13240 to do EOL conversion. */
13241 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13242 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13244 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13245 p, eol_flag);
13247 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13248 #ifdef subprocesses
13249 obj = Fget_buffer_process (Fcurrent_buffer ());
13250 if (PROCESSP (obj))
13252 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13253 p, eol_flag);
13254 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13255 p, eol_flag);
13257 #endif /* subprocesses */
13258 #endif /* 0 */
13259 *p = 0;
13260 return decode_mode_spec_buf;
13264 if (STRINGP (obj))
13265 return (char *) XSTRING (obj)->data;
13266 else
13267 return "";
13271 /* Count up to COUNT lines starting from START / START_BYTE.
13272 But don't go beyond LIMIT_BYTE.
13273 Return the number of lines thus found (always nonnegative).
13275 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13277 static int
13278 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13279 int start, start_byte, limit_byte, count;
13280 int *byte_pos_ptr;
13282 register unsigned char *cursor;
13283 unsigned char *base;
13285 register int ceiling;
13286 register unsigned char *ceiling_addr;
13287 int orig_count = count;
13289 /* If we are not in selective display mode,
13290 check only for newlines. */
13291 int selective_display = (!NILP (current_buffer->selective_display)
13292 && !INTEGERP (current_buffer->selective_display));
13294 if (count > 0)
13296 while (start_byte < limit_byte)
13298 ceiling = BUFFER_CEILING_OF (start_byte);
13299 ceiling = min (limit_byte - 1, ceiling);
13300 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13301 base = (cursor = BYTE_POS_ADDR (start_byte));
13302 while (1)
13304 if (selective_display)
13305 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13307 else
13308 while (*cursor != '\n' && ++cursor != ceiling_addr)
13311 if (cursor != ceiling_addr)
13313 if (--count == 0)
13315 start_byte += cursor - base + 1;
13316 *byte_pos_ptr = start_byte;
13317 return orig_count;
13319 else
13320 if (++cursor == ceiling_addr)
13321 break;
13323 else
13324 break;
13326 start_byte += cursor - base;
13329 else
13331 while (start_byte > limit_byte)
13333 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13334 ceiling = max (limit_byte, ceiling);
13335 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13336 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13337 while (1)
13339 if (selective_display)
13340 while (--cursor != ceiling_addr
13341 && *cursor != '\n' && *cursor != 015)
13343 else
13344 while (--cursor != ceiling_addr && *cursor != '\n')
13347 if (cursor != ceiling_addr)
13349 if (++count == 0)
13351 start_byte += cursor - base + 1;
13352 *byte_pos_ptr = start_byte;
13353 /* When scanning backwards, we should
13354 not count the newline posterior to which we stop. */
13355 return - orig_count - 1;
13358 else
13359 break;
13361 /* Here we add 1 to compensate for the last decrement
13362 of CURSOR, which took it past the valid range. */
13363 start_byte += cursor - base + 1;
13367 *byte_pos_ptr = limit_byte;
13369 if (count < 0)
13370 return - orig_count + count;
13371 return orig_count - count;
13377 /***********************************************************************
13378 Displaying strings
13379 ***********************************************************************/
13381 /* Display a NUL-terminated string, starting with index START.
13383 If STRING is non-null, display that C string. Otherwise, the Lisp
13384 string LISP_STRING is displayed.
13386 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13387 FACE_STRING. Display STRING or LISP_STRING with the face at
13388 FACE_STRING_POS in FACE_STRING:
13390 Display the string in the environment given by IT, but use the
13391 standard display table, temporarily.
13393 FIELD_WIDTH is the minimum number of output glyphs to produce.
13394 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13395 with spaces. If STRING has more characters, more than FIELD_WIDTH
13396 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13398 PRECISION is the maximum number of characters to output from
13399 STRING. PRECISION < 0 means don't truncate the string.
13401 This is roughly equivalent to printf format specifiers:
13403 FIELD_WIDTH PRECISION PRINTF
13404 ----------------------------------------
13405 -1 -1 %s
13406 -1 10 %.10s
13407 10 -1 %10s
13408 20 10 %20.10s
13410 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13411 display them, and < 0 means obey the current buffer's value of
13412 enable_multibyte_characters.
13414 Value is the number of glyphs produced. */
13416 static int
13417 display_string (string, lisp_string, face_string, face_string_pos,
13418 start, it, field_width, precision, max_x, multibyte)
13419 unsigned char *string;
13420 Lisp_Object lisp_string;
13421 Lisp_Object face_string;
13422 int face_string_pos;
13423 int start;
13424 struct it *it;
13425 int field_width, precision, max_x;
13426 int multibyte;
13428 int hpos_at_start = it->hpos;
13429 int saved_face_id = it->face_id;
13430 struct glyph_row *row = it->glyph_row;
13432 /* Initialize the iterator IT for iteration over STRING beginning
13433 with index START. We assume that IT may be modified here (which
13434 means that display_line has to do something when displaying a
13435 mini-buffer prompt, which it does). */
13436 reseat_to_string (it, string, lisp_string, start,
13437 precision, field_width, multibyte);
13439 /* If displaying STRING, set up the face of the iterator
13440 from LISP_STRING, if that's given. */
13441 if (STRINGP (face_string))
13443 int endptr;
13444 struct face *face;
13446 it->face_id
13447 = face_at_string_position (it->w, face_string, face_string_pos,
13448 0, it->region_beg_charpos,
13449 it->region_end_charpos,
13450 &endptr, it->base_face_id);
13451 face = FACE_FROM_ID (it->f, it->face_id);
13452 it->face_box_p = face->box != FACE_NO_BOX;
13455 /* Set max_x to the maximum allowed X position. Don't let it go
13456 beyond the right edge of the window. */
13457 if (max_x <= 0)
13458 max_x = it->last_visible_x;
13459 else
13460 max_x = min (max_x, it->last_visible_x);
13462 /* Skip over display elements that are not visible. because IT->w is
13463 hscrolled. */
13464 if (it->current_x < it->first_visible_x)
13465 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13466 MOVE_TO_POS | MOVE_TO_X);
13468 row->ascent = it->max_ascent;
13469 row->height = it->max_ascent + it->max_descent;
13470 row->phys_ascent = it->max_phys_ascent;
13471 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13473 /* This condition is for the case that we are called with current_x
13474 past last_visible_x. */
13475 while (it->current_x < max_x)
13477 int x_before, x, n_glyphs_before, i, nglyphs;
13479 /* Get the next display element. */
13480 if (!get_next_display_element (it))
13481 break;
13483 /* Produce glyphs. */
13484 x_before = it->current_x;
13485 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13486 PRODUCE_GLYPHS (it);
13488 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13489 i = 0;
13490 x = x_before;
13491 while (i < nglyphs)
13493 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13495 if (!it->truncate_lines_p
13496 && x + glyph->pixel_width > max_x)
13498 /* End of continued line or max_x reached. */
13499 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13500 it->current_x = x;
13501 break;
13503 else if (x + glyph->pixel_width > it->first_visible_x)
13505 /* Glyph is at least partially visible. */
13506 ++it->hpos;
13507 if (x < it->first_visible_x)
13508 it->glyph_row->x = x - it->first_visible_x;
13510 else
13512 /* Glyph is off the left margin of the display area.
13513 Should not happen. */
13514 abort ();
13517 row->ascent = max (row->ascent, it->max_ascent);
13518 row->height = max (row->height, it->max_ascent + it->max_descent);
13519 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13520 row->phys_height = max (row->phys_height,
13521 it->max_phys_ascent + it->max_phys_descent);
13522 x += glyph->pixel_width;
13523 ++i;
13526 /* Stop if max_x reached. */
13527 if (i < nglyphs)
13528 break;
13530 /* Stop at line ends. */
13531 if (ITERATOR_AT_END_OF_LINE_P (it))
13533 it->continuation_lines_width = 0;
13534 break;
13537 set_iterator_to_next (it, 1);
13539 /* Stop if truncating at the right edge. */
13540 if (it->truncate_lines_p
13541 && it->current_x >= it->last_visible_x)
13543 /* Add truncation mark, but don't do it if the line is
13544 truncated at a padding space. */
13545 if (IT_CHARPOS (*it) < it->string_nchars)
13547 if (!FRAME_WINDOW_P (it->f))
13548 produce_special_glyphs (it, IT_TRUNCATION);
13549 it->glyph_row->truncated_on_right_p = 1;
13551 break;
13555 /* Maybe insert a truncation at the left. */
13556 if (it->first_visible_x
13557 && IT_CHARPOS (*it) > 0)
13559 if (!FRAME_WINDOW_P (it->f))
13560 insert_left_trunc_glyphs (it);
13561 it->glyph_row->truncated_on_left_p = 1;
13564 it->face_id = saved_face_id;
13566 /* Value is number of columns displayed. */
13567 return it->hpos - hpos_at_start;
13572 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13573 appears as an element of LIST or as the car of an element of LIST.
13574 If PROPVAL is a list, compare each element against LIST in that
13575 way, and return 1 if any element of PROPVAL is found in LIST.
13576 Otherwise return 0. This function cannot quit. */
13579 invisible_p (propval, list)
13580 register Lisp_Object propval;
13581 Lisp_Object list;
13583 register Lisp_Object tail, proptail;
13585 for (tail = list; CONSP (tail); tail = XCDR (tail))
13587 register Lisp_Object tem;
13588 tem = XCAR (tail);
13589 if (EQ (propval, tem))
13590 return 1;
13591 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13592 return 1;
13595 if (CONSP (propval))
13597 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13599 Lisp_Object propelt;
13600 propelt = XCAR (proptail);
13601 for (tail = list; CONSP (tail); tail = XCDR (tail))
13603 register Lisp_Object tem;
13604 tem = XCAR (tail);
13605 if (EQ (propelt, tem))
13606 return 1;
13607 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13608 return 1;
13613 return 0;
13617 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13618 the cdr of that element is non-nil. If PROPVAL is a list, check
13619 each element of PROPVAL in that way, and the first time some
13620 element is found, return 1 if the cdr of that element is non-nil.
13621 Otherwise return 0. This function cannot quit. */
13624 invisible_ellipsis_p (propval, list)
13625 register Lisp_Object propval;
13626 Lisp_Object list;
13628 register Lisp_Object tail, proptail;
13630 for (tail = list; CONSP (tail); tail = XCDR (tail))
13632 register Lisp_Object tem;
13633 tem = XCAR (tail);
13634 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13635 return ! NILP (XCDR (tem));
13638 if (CONSP (propval))
13639 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13641 Lisp_Object propelt;
13642 propelt = XCAR (proptail);
13643 for (tail = list; CONSP (tail); tail = XCDR (tail))
13645 register Lisp_Object tem;
13646 tem = XCAR (tail);
13647 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13648 return ! NILP (XCDR (tem));
13652 return 0;
13657 /***********************************************************************
13658 Initialization
13659 ***********************************************************************/
13661 void
13662 syms_of_xdisp ()
13664 Vwith_echo_area_save_vector = Qnil;
13665 staticpro (&Vwith_echo_area_save_vector);
13667 Vmessage_stack = Qnil;
13668 staticpro (&Vmessage_stack);
13670 Qinhibit_redisplay = intern ("inhibit-redisplay");
13671 staticpro (&Qinhibit_redisplay);
13673 #if GLYPH_DEBUG
13674 defsubr (&Sdump_glyph_matrix);
13675 defsubr (&Sdump_glyph_row);
13676 defsubr (&Sdump_tool_bar_row);
13677 defsubr (&Strace_redisplay_toggle);
13678 defsubr (&Strace_to_stderr);
13679 #endif
13681 staticpro (&Qmenu_bar_update_hook);
13682 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13684 staticpro (&Qoverriding_terminal_local_map);
13685 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13687 staticpro (&Qoverriding_local_map);
13688 Qoverriding_local_map = intern ("overriding-local-map");
13690 staticpro (&Qwindow_scroll_functions);
13691 Qwindow_scroll_functions = intern ("window-scroll-functions");
13693 staticpro (&Qredisplay_end_trigger_functions);
13694 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13696 staticpro (&Qinhibit_point_motion_hooks);
13697 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13699 QCdata = intern (":data");
13700 staticpro (&QCdata);
13701 Qdisplay = intern ("display");
13702 staticpro (&Qdisplay);
13703 Qspace_width = intern ("space-width");
13704 staticpro (&Qspace_width);
13705 Qraise = intern ("raise");
13706 staticpro (&Qraise);
13707 Qspace = intern ("space");
13708 staticpro (&Qspace);
13709 Qmargin = intern ("margin");
13710 staticpro (&Qmargin);
13711 Qleft_margin = intern ("left-margin");
13712 staticpro (&Qleft_margin);
13713 Qright_margin = intern ("right-margin");
13714 staticpro (&Qright_margin);
13715 Qalign_to = intern ("align-to");
13716 staticpro (&Qalign_to);
13717 QCalign_to = intern (":align-to");
13718 staticpro (&QCalign_to);
13719 Qrelative_width = intern ("relative-width");
13720 staticpro (&Qrelative_width);
13721 QCrelative_width = intern (":relative-width");
13722 staticpro (&QCrelative_width);
13723 QCrelative_height = intern (":relative-height");
13724 staticpro (&QCrelative_height);
13725 QCeval = intern (":eval");
13726 staticpro (&QCeval);
13727 Qwhen = intern ("when");
13728 staticpro (&Qwhen);
13729 QCfile = intern (":file");
13730 staticpro (&QCfile);
13731 Qfontified = intern ("fontified");
13732 staticpro (&Qfontified);
13733 Qfontification_functions = intern ("fontification-functions");
13734 staticpro (&Qfontification_functions);
13735 Qtrailing_whitespace = intern ("trailing-whitespace");
13736 staticpro (&Qtrailing_whitespace);
13737 Qimage = intern ("image");
13738 staticpro (&Qimage);
13739 Qmessage_truncate_lines = intern ("message-truncate-lines");
13740 staticpro (&Qmessage_truncate_lines);
13741 Qgrow_only = intern ("grow-only");
13742 staticpro (&Qgrow_only);
13744 last_arrow_position = Qnil;
13745 last_arrow_string = Qnil;
13746 staticpro (&last_arrow_position);
13747 staticpro (&last_arrow_string);
13749 echo_buffer[0] = echo_buffer[1] = Qnil;
13750 staticpro (&echo_buffer[0]);
13751 staticpro (&echo_buffer[1]);
13753 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13754 staticpro (&echo_area_buffer[0]);
13755 staticpro (&echo_area_buffer[1]);
13757 Vmessages_buffer_name = build_string ("*Messages*");
13758 staticpro (&Vmessages_buffer_name);
13760 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13761 "Non-nil means highlight trailing whitespace.\n\
13762 The face used for trailing whitespace is `trailing-whitespace'.");
13763 Vshow_trailing_whitespace = Qnil;
13765 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13766 "Non-nil means don't actually do any redisplay.\n\
13767 This is used for internal purposes.");
13768 Vinhibit_redisplay = Qnil;
13770 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13771 "String (or mode line construct) included (normally) in `mode-line-format'.");
13772 Vglobal_mode_string = Qnil;
13774 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13775 "Marker for where to display an arrow on top of the buffer text.\n\
13776 This must be the beginning of a line in order to work.\n\
13777 See also `overlay-arrow-string'.");
13778 Voverlay_arrow_position = Qnil;
13780 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13781 "String to display as an arrow. See also `overlay-arrow-position'.");
13782 Voverlay_arrow_string = Qnil;
13784 DEFVAR_INT ("scroll-step", &scroll_step,
13785 "*The number of lines to try scrolling a window by when point moves out.\n\
13786 If that fails to bring point back on frame, point is centered instead.\n\
13787 If this is zero, point is always centered after it moves off frame.\n\
13788 If you want scrolling to always be a line at a time, you should set\n\
13789 `scroll-conservatively' to a large value rather than set this to 1.");
13791 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13792 "*Scroll up to this many lines, to bring point back on screen.\n\
13793 A value of zero means to scroll the text to center point vertically\n\
13794 in the window.");
13795 scroll_conservatively = 0;
13797 DEFVAR_INT ("scroll-margin", &scroll_margin,
13798 "*Number of lines of margin at the top and bottom of a window.\n\
13799 Recenter the window whenever point gets within this many lines\n\
13800 of the top or bottom of the window.");
13801 scroll_margin = 0;
13803 #if GLYPH_DEBUG
13804 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13805 #endif
13807 DEFVAR_BOOL ("truncate-partial-width-windows",
13808 &truncate_partial_width_windows,
13809 "*Non-nil means truncate lines in all windows less than full frame wide.");
13810 truncate_partial_width_windows = 1;
13812 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13813 "*Non-nil means use inverse video for the mode line.");
13814 mode_line_inverse_video = 1;
13816 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13817 "*Maximum buffer size for which line number should be displayed.\n\
13818 If the buffer is bigger than this, the line number does not appear\n\
13819 in the mode line. A value of nil means no limit.");
13820 Vline_number_display_limit = Qnil;
13822 DEFVAR_INT ("line-number-display-limit-width",
13823 &line_number_display_limit_width,
13824 "*Maximum line width (in characters) for line number display.\n\
13825 If the average length of the lines near point is bigger than this, then the\n\
13826 line number may be omitted from the mode line.");
13827 line_number_display_limit_width = 200;
13829 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13830 "*Non-nil means highlight region even in nonselected windows.");
13831 highlight_nonselected_windows = 0;
13833 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13834 "Non-nil if more than one frame is visible on this display.\n\
13835 Minibuffer-only frames don't count, but iconified frames do.\n\
13836 This variable is not guaranteed to be accurate except while processing\n\
13837 `frame-title-format' and `icon-title-format'.");
13839 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13840 "Template for displaying the title bar of visible frames.\n\
13841 \(Assuming the window manager supports this feature.)\n\
13842 This variable has the same structure as `mode-line-format' (which see),\n\
13843 and is used only on frames for which no explicit name has been set\n\
13844 \(see `modify-frame-parameters').");
13845 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13846 "Template for displaying the title bar of an iconified frame.\n\
13847 \(Assuming the window manager supports this feature.)\n\
13848 This variable has the same structure as `mode-line-format' (which see),\n\
13849 and is used only on frames for which no explicit name has been set\n\
13850 \(see `modify-frame-parameters').");
13851 Vicon_title_format
13852 = Vframe_title_format
13853 = Fcons (intern ("multiple-frames"),
13854 Fcons (build_string ("%b"),
13855 Fcons (Fcons (build_string (""),
13856 Fcons (intern ("invocation-name"),
13857 Fcons (build_string ("@"),
13858 Fcons (intern ("system-name"),
13859 Qnil)))),
13860 Qnil)));
13862 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13863 "Maximum number of lines to keep in the message log buffer.\n\
13864 If nil, disable message logging. If t, log messages but don't truncate\n\
13865 the buffer when it becomes large.");
13866 XSETFASTINT (Vmessage_log_max, 50);
13868 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13869 "Functions called before redisplay, if window sizes have changed.\n\
13870 The value should be a list of functions that take one argument.\n\
13871 Just before redisplay, for each frame, if any of its windows have changed\n\
13872 size since the last redisplay, or have been split or deleted,\n\
13873 all the functions in the list are called, with the frame as argument.");
13874 Vwindow_size_change_functions = Qnil;
13876 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13877 "List of Functions to call before redisplaying a window with scrolling.\n\
13878 Each function is called with two arguments, the window\n\
13879 and its new display-start position. Note that the value of `window-end'\n\
13880 is not valid when these functions are called.");
13881 Vwindow_scroll_functions = Qnil;
13883 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13884 "*Non-nil means automatically resize tool-bars.\n\
13885 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13886 It decreases a tool-bar's height when it would display blank lines\n\
13887 otherwise.");
13888 auto_resize_tool_bars_p = 1;
13890 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13891 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13892 auto_raise_tool_bar_buttons_p = 1;
13894 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13895 "*Margin around tool-bar buttons in pixels.");
13896 tool_bar_button_margin = 1;
13898 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13899 "Relief thickness of tool-bar buttons.");
13900 tool_bar_button_relief = 3;
13902 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13903 "List of functions to call to fontify regions of text.\n\
13904 Each function is called with one argument POS. Functions must\n\
13905 fontify a region starting at POS in the current buffer, and give\n\
13906 fontified regions the property `fontified'.\n\
13907 This variable automatically becomes buffer-local when set.");
13908 Vfontification_functions = Qnil;
13909 Fmake_local_variable (Qfontification_functions);
13911 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13912 &unibyte_display_via_language_environment,
13913 "*Non-nil means display unibyte text according to language environment.\n\
13914 Specifically this means that unibyte non-ASCII characters\n\
13915 are displayed by converting them to the equivalent multibyte characters\n\
13916 according to the current language environment. As a result, they are\n\
13917 displayed according to the current fontset.");
13918 unibyte_display_via_language_environment = 0;
13920 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13921 "*Maximum height for resizing mini-windows.\n\
13922 If a float, it specifies a fraction of the mini-window frame's height.\n\
13923 If an integer, it specifies a number of lines.");
13924 Vmax_mini_window_height = make_float (0.25);
13926 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
13927 "*How to resize the mini-window.\n\
13928 A value of nil means don't automatically resize mini-windows.\n\
13929 A value of t means resize it to fit the text displayed in it.\n\
13930 A value of `grow-only', the default, means let mini-windows grow\n\
13931 only, until the its display becomes empty, at which point the mini-window\n\
13932 goes back to its normal size.");
13933 Vresize_mini_windows = Qgrow_only;
13935 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13936 &cursor_in_non_selected_windows,
13937 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13938 Nil means don't display a cursor there.");
13939 cursor_in_non_selected_windows = 1;
13941 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13942 "*Non-nil means scroll the display automatically to make point visible.");
13943 automatic_hscrolling_p = 1;
13945 DEFVAR_LISP ("image-types", &Vimage_types,
13946 "List of supported image types.\n\
13947 Each element of the list is a symbol for a supported image type.");
13948 Vimage_types = Qnil;
13950 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13951 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13952 Bind this around calls to `message' to let it take effect.");
13953 message_truncate_lines = 0;
13955 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13956 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13957 Can be used to update submenus whose contents should vary.");
13958 Vmenu_bar_update_hook = Qnil;
13962 /* Initialize this module when Emacs starts. */
13964 void
13965 init_xdisp ()
13967 Lisp_Object root_window;
13968 struct window *mini_w;
13970 current_header_line_height = current_mode_line_height = -1;
13972 CHARPOS (this_line_start_pos) = 0;
13974 mini_w = XWINDOW (minibuf_window);
13975 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13977 if (!noninteractive)
13979 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13980 int i;
13982 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13983 set_window_height (root_window,
13984 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13986 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13987 set_window_height (minibuf_window, 1, 0);
13989 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13990 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13992 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13993 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13994 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13996 /* The default ellipsis glyphs `...'. */
13997 for (i = 0; i < 3; ++i)
13998 XSETFASTINT (default_invis_vector[i], '.');
14001 #ifdef HAVE_WINDOW_SYSTEM
14003 /* Allocate the buffer for frame titles. */
14004 int size = 100;
14005 frame_title_buf = (char *) xmalloc (size);
14006 frame_title_buf_end = frame_title_buf + size;
14007 frame_title_ptr = NULL;
14009 #endif /* HAVE_WINDOW_SYSTEM */
14011 help_echo_showing_p = 0;