(try_window_reusing_current_matrix) <scrolling up>:
[emacs/old-mirror.git] / src / xdisp.c
blob669b0d77ebf4496fb34aa778359fd93a13c22f58
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
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 Lisp_Object Vtool_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 /* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
405 This variable is deprecated. */
407 int mode_line_inverse_video;
409 /* Prompt to display in front of the mini-buffer contents. */
411 Lisp_Object minibuf_prompt;
413 /* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
416 int minibuf_prompt_width;
417 int minibuf_prompt_pixel_width;
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
423 Lisp_Object echo_area_window;
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
430 Lisp_Object Vmessage_stack;
432 /* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
435 int message_enable_multibyte;
437 /* True if we should redraw the mode lines on the next redisplay. */
439 int update_mode_lines;
441 /* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
444 int windows_or_buffers_changed;
446 /* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
449 int line_number_displayed;
451 /* Maximum buffer size for which to display line numbers. */
453 Lisp_Object Vline_number_display_limit;
455 /* line width to consider when repostioning for line number display */
457 static int line_number_display_limit_width;
459 /* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
462 Lisp_Object Vmessage_log_max;
464 /* The name of the *Messages* buffer, a string. */
466 static Lisp_Object Vmessages_buffer_name;
468 /* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
471 Lisp_Object echo_area_buffer[2];
473 /* The buffers referenced from echo_area_buffer. */
475 static Lisp_Object echo_buffer[2];
477 /* A vector saved used in with_area_buffer to reduce consing. */
479 static Lisp_Object Vwith_echo_area_save_vector;
481 /* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
484 static int display_last_displayed_message_p;
486 /* Nonzero if echo area is being used by print; zero if being used by
487 message. */
489 int message_buf_print;
491 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
493 Lisp_Object Qinhibit_menubar_update;
494 int inhibit_menubar_update;
496 /* Maximum height for resizing mini-windows. Either a float
497 specifying a fraction of the available height, or an integer
498 specifying a number of lines. */
500 Lisp_Object Vmax_mini_window_height;
502 /* Non-zero means messages should be displayed with truncated
503 lines instead of being continued. */
505 int message_truncate_lines;
506 Lisp_Object Qmessage_truncate_lines;
508 /* Non-zero means we want a hollow cursor in windows that are not
509 selected. Zero means there's no cursor in such windows. */
511 int cursor_in_non_selected_windows;
513 /* A scratch glyph row with contents used for generating truncation
514 glyphs. Also used in direct_output_for_insert. */
516 #define MAX_SCRATCH_GLYPHS 100
517 struct glyph_row scratch_glyph_row;
518 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
520 /* Ascent and height of the last line processed by move_it_to. */
522 static int last_max_ascent, last_height;
524 /* Non-zero if there's a help-echo in the echo area. */
526 int help_echo_showing_p;
528 /* If >= 0, computed, exact values of mode-line and header-line height
529 to use in the macros CURRENT_MODE_LINE_HEIGHT and
530 CURRENT_HEADER_LINE_HEIGHT. */
532 int current_mode_line_height, current_header_line_height;
534 /* The maximum distance to look ahead for text properties. Values
535 that are too small let us call compute_char_face and similar
536 functions too often which is expensive. Values that are too large
537 let us call compute_char_face and alike too often because we
538 might not be interested in text properties that far away. */
540 #define TEXT_PROP_DISTANCE_LIMIT 100
542 #if GLYPH_DEBUG
544 /* Non-zero means print traces of redisplay if compiled with
545 GLYPH_DEBUG != 0. */
547 int trace_redisplay_p;
549 #endif /* GLYPH_DEBUG */
551 #ifdef DEBUG_TRACE_MOVE
552 /* Non-zero means trace with TRACE_MOVE to stderr. */
553 int trace_move;
555 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
556 #else
557 #define TRACE_MOVE(x) (void) 0
558 #endif
560 /* Non-zero means automatically scroll windows horizontally to make
561 point visible. */
563 int automatic_hscrolling_p;
565 /* A list of symbols, one for each supported image type. */
567 Lisp_Object Vimage_types;
569 /* The variable `resize-mini-windows'. If nil, don't resize
570 mini-windows. If t, always resize them to fit the text they
571 display. If `grow-only', let mini-windows grow only until they
572 become empty. */
574 Lisp_Object Vresize_mini_windows;
576 /* Value returned from text property handlers (see below). */
578 enum prop_handled
580 HANDLED_NORMALLY,
581 HANDLED_RECOMPUTE_PROPS,
582 HANDLED_OVERLAY_STRING_CONSUMED,
583 HANDLED_RETURN
586 /* A description of text properties that redisplay is interested
587 in. */
589 struct props
591 /* The name of the property. */
592 Lisp_Object *name;
594 /* A unique index for the property. */
595 enum prop_idx idx;
597 /* A handler function called to set up iterator IT from the property
598 at IT's current position. Value is used to steer handle_stop. */
599 enum prop_handled (*handler) P_ ((struct it *it));
602 static enum prop_handled handle_face_prop P_ ((struct it *));
603 static enum prop_handled handle_invisible_prop P_ ((struct it *));
604 static enum prop_handled handle_display_prop P_ ((struct it *));
605 static enum prop_handled handle_composition_prop P_ ((struct it *));
606 static enum prop_handled handle_overlay_change P_ ((struct it *));
607 static enum prop_handled handle_fontified_prop P_ ((struct it *));
609 /* Properties handled by iterators. */
611 static struct props it_props[] =
613 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
614 /* Handle `face' before `display' because some sub-properties of
615 `display' need to know the face. */
616 {&Qface, FACE_PROP_IDX, handle_face_prop},
617 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
618 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
619 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
620 {NULL, 0, NULL}
623 /* Value is the position described by X. If X is a marker, value is
624 the marker_position of X. Otherwise, value is X. */
626 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
628 /* Enumeration returned by some move_it_.* functions internally. */
630 enum move_it_result
632 /* Not used. Undefined value. */
633 MOVE_UNDEFINED,
635 /* Move ended at the requested buffer position or ZV. */
636 MOVE_POS_MATCH_OR_ZV,
638 /* Move ended at the requested X pixel position. */
639 MOVE_X_REACHED,
641 /* Move within a line ended at the end of a line that must be
642 continued. */
643 MOVE_LINE_CONTINUED,
645 /* Move within a line ended at the end of a line that would
646 be displayed truncated. */
647 MOVE_LINE_TRUNCATED,
649 /* Move within a line ended at a line end. */
650 MOVE_NEWLINE_OR_CR
655 /* Function prototypes. */
657 static void mark_window_display_accurate_1 P_ ((struct window *, int));
658 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
659 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
660 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
661 static int redisplay_mode_lines P_ ((Lisp_Object, int));
662 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
663 static int invisible_text_between_p P_ ((struct it *, int, int));
664 static int next_element_from_ellipsis P_ ((struct it *));
665 static void pint2str P_ ((char *, int, int));
666 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
667 struct text_pos));
668 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
669 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
670 static void store_frame_title_char P_ ((char));
671 static int store_frame_title P_ ((unsigned char *, int, int));
672 static void x_consider_frame_title P_ ((Lisp_Object));
673 static void handle_stop P_ ((struct it *));
674 static int tool_bar_lines_needed P_ ((struct frame *));
675 static int single_display_prop_intangible_p P_ ((Lisp_Object));
676 static void ensure_echo_area_buffers P_ ((void));
677 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
678 struct glyph_row *,
679 struct glyph_row *));
680 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
681 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
682 static int with_echo_area_buffer P_ ((struct window *, int,
683 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
684 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
685 static void clear_garbaged_frames P_ ((void));
686 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
687 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
688 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
689 static int display_echo_area P_ ((struct window *));
690 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
693 static int string_char_and_length P_ ((unsigned char *, int, int *));
694 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
695 struct text_pos));
696 static int compute_window_start_on_continuation_line P_ ((struct window *));
697 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
698 static void insert_left_trunc_glyphs P_ ((struct it *));
699 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
700 static void extend_face_to_end_of_line P_ ((struct it *));
701 static int append_space P_ ((struct it *, int));
702 static void make_cursor_line_fully_visible P_ ((struct window *));
703 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
704 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
705 static int trailing_whitespace_p P_ ((int));
706 static int message_log_check_duplicate P_ ((int, int, int, int));
707 int invisible_p P_ ((Lisp_Object, Lisp_Object));
708 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
709 static void push_it P_ ((struct it *));
710 static void pop_it P_ ((struct it *));
711 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
712 static void redisplay_internal P_ ((int));
713 static int echo_area_display P_ ((int));
714 static void redisplay_windows P_ ((Lisp_Object));
715 static void redisplay_window P_ ((Lisp_Object, int));
716 static void update_menu_bar P_ ((struct frame *, int));
717 static int try_window_reusing_current_matrix P_ ((struct window *));
718 static int try_window_id P_ ((struct window *));
719 static int display_line P_ ((struct it *));
720 static int display_mode_lines P_ ((struct window *));
721 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
722 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
723 static char *decode_mode_spec P_ ((struct window *, int, int, int));
724 static void display_menu_bar P_ ((struct window *));
725 static int display_count_lines P_ ((int, int, int, int, int *));
726 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
727 int, int, struct it *, int, int, int, int));
728 static void compute_line_metrics P_ ((struct it *));
729 static void run_redisplay_end_trigger_hook P_ ((struct it *));
730 static int get_overlay_strings P_ ((struct it *));
731 static void next_overlay_string P_ ((struct it *));
732 static void reseat P_ ((struct it *, struct text_pos, int));
733 static void reseat_1 P_ ((struct it *, struct text_pos, int));
734 static void back_to_previous_visible_line_start P_ ((struct it *));
735 static void reseat_at_previous_visible_line_start P_ ((struct it *));
736 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
737 static int next_element_from_display_vector P_ ((struct it *));
738 static int next_element_from_string P_ ((struct it *));
739 static int next_element_from_c_string P_ ((struct it *));
740 static int next_element_from_buffer P_ ((struct it *));
741 static int next_element_from_composition P_ ((struct it *));
742 static int next_element_from_image P_ ((struct it *));
743 static int next_element_from_stretch P_ ((struct it *));
744 static void load_overlay_strings P_ ((struct it *));
745 static void init_from_display_pos P_ ((struct it *, struct window *,
746 struct display_pos *));
747 static void reseat_to_string P_ ((struct it *, unsigned char *,
748 Lisp_Object, int, int, int, int));
749 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
750 int, int, int));
751 void move_it_vertically_backward P_ ((struct it *, int));
752 static void init_to_row_start P_ ((struct it *, struct window *,
753 struct glyph_row *));
754 static void init_to_row_end P_ ((struct it *, struct window *,
755 struct glyph_row *));
756 static void back_to_previous_line_start P_ ((struct it *));
757 static int forward_to_next_line_start P_ ((struct it *, int *));
758 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
759 Lisp_Object, int));
760 static struct text_pos string_pos P_ ((int, Lisp_Object));
761 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
762 static int number_of_chars P_ ((unsigned char *, int));
763 static void compute_stop_pos P_ ((struct it *));
764 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
765 Lisp_Object));
766 static int face_before_or_after_it_pos P_ ((struct it *, int));
767 static int next_overlay_change P_ ((int));
768 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
769 Lisp_Object, struct text_pos *,
770 int));
771 static int underlying_face_id P_ ((struct it *));
772 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
773 struct window *));
775 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
776 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
778 #ifdef HAVE_WINDOW_SYSTEM
780 static void update_tool_bar P_ ((struct frame *, int));
781 static void build_desired_tool_bar_string P_ ((struct frame *f));
782 static int redisplay_tool_bar P_ ((struct frame *));
783 static void display_tool_bar_line P_ ((struct it *));
785 #endif /* HAVE_WINDOW_SYSTEM */
788 /***********************************************************************
789 Window display dimensions
790 ***********************************************************************/
792 /* Return the window-relative maximum y + 1 for glyph rows displaying
793 text in window W. This is the height of W minus the height of a
794 mode line, if any. */
796 INLINE int
797 window_text_bottom_y (w)
798 struct window *w;
800 struct frame *f = XFRAME (w->frame);
801 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
803 if (WINDOW_WANTS_MODELINE_P (w))
804 height -= CURRENT_MODE_LINE_HEIGHT (w);
805 return height;
809 /* Return the pixel width of display area AREA of window W. AREA < 0
810 means return the total width of W, not including bitmap areas to
811 the left and right of the window. */
813 INLINE int
814 window_box_width (w, area)
815 struct window *w;
816 int area;
818 struct frame *f = XFRAME (w->frame);
819 int width = XFASTINT (w->width);
821 if (!w->pseudo_window_p)
823 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
825 if (area == TEXT_AREA)
827 if (INTEGERP (w->left_margin_width))
828 width -= XFASTINT (w->left_margin_width);
829 if (INTEGERP (w->right_margin_width))
830 width -= XFASTINT (w->right_margin_width);
832 else if (area == LEFT_MARGIN_AREA)
833 width = (INTEGERP (w->left_margin_width)
834 ? XFASTINT (w->left_margin_width) : 0);
835 else if (area == RIGHT_MARGIN_AREA)
836 width = (INTEGERP (w->right_margin_width)
837 ? XFASTINT (w->right_margin_width) : 0);
840 return width * CANON_X_UNIT (f);
844 /* Return the pixel height of the display area of window W, not
845 including mode lines of W, if any.. */
847 INLINE int
848 window_box_height (w)
849 struct window *w;
851 struct frame *f = XFRAME (w->frame);
852 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
854 xassert (height >= 0);
856 /* Note: the code below that determines the mode-line/header-line
857 height is essentially the same as that contained in the macro
858 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
859 the appropriate glyph row has its `mode_line_p' flag set,
860 and if it doesn't, uses estimate_mode_line_height instead. */
862 if (WINDOW_WANTS_MODELINE_P (w))
864 struct glyph_row *ml_row
865 = (w->current_matrix && w->current_matrix->rows
866 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
867 : 0);
868 if (ml_row && ml_row->mode_line_p)
869 height -= ml_row->height;
870 else
871 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
874 if (WINDOW_WANTS_HEADER_LINE_P (w))
876 struct glyph_row *hl_row
877 = (w->current_matrix && w->current_matrix->rows
878 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
879 : 0);
880 if (hl_row && hl_row->mode_line_p)
881 height -= hl_row->height;
882 else
883 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
886 return height;
890 /* Return the frame-relative coordinate of the left edge of display
891 area AREA of window W. AREA < 0 means return the left edge of the
892 whole window, to the right of any bitmap area at the left side of
893 W. */
895 INLINE int
896 window_box_left (w, area)
897 struct window *w;
898 int area;
900 struct frame *f = XFRAME (w->frame);
901 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
903 if (!w->pseudo_window_p)
905 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
906 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
908 if (area == TEXT_AREA)
909 x += window_box_width (w, LEFT_MARGIN_AREA);
910 else if (area == RIGHT_MARGIN_AREA)
911 x += (window_box_width (w, LEFT_MARGIN_AREA)
912 + window_box_width (w, TEXT_AREA));
915 return x;
919 /* Return the frame-relative coordinate of the right edge of display
920 area AREA of window W. AREA < 0 means return the left edge of the
921 whole window, to the left of any bitmap area at the right side of
922 W. */
924 INLINE int
925 window_box_right (w, area)
926 struct window *w;
927 int area;
929 return window_box_left (w, area) + window_box_width (w, area);
933 /* Get the bounding box of the display area AREA of window W, without
934 mode lines, in frame-relative coordinates. AREA < 0 means the
935 whole window, not including bitmap areas to the left and right of
936 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
937 coordinates of the upper-left corner of the box. Return in
938 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
940 INLINE void
941 window_box (w, area, box_x, box_y, box_width, box_height)
942 struct window *w;
943 int area;
944 int *box_x, *box_y, *box_width, *box_height;
946 struct frame *f = XFRAME (w->frame);
948 *box_width = window_box_width (w, area);
949 *box_height = window_box_height (w);
950 *box_x = window_box_left (w, area);
951 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
952 + XFASTINT (w->top) * CANON_Y_UNIT (f));
953 if (WINDOW_WANTS_HEADER_LINE_P (w))
954 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
958 /* Get the bounding box of the display area AREA of window W, without
959 mode lines. AREA < 0 means the whole window, not including bitmap
960 areas to the left and right of the window. Return in *TOP_LEFT_X
961 and TOP_LEFT_Y the frame-relative pixel coordinates of the
962 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
963 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
964 box. */
966 INLINE void
967 window_box_edges (w, area, top_left_x, top_left_y,
968 bottom_right_x, bottom_right_y)
969 struct window *w;
970 int area;
971 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
973 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
974 bottom_right_y);
975 *bottom_right_x += *top_left_x;
976 *bottom_right_y += *top_left_y;
981 /***********************************************************************
982 Utilities
983 ***********************************************************************/
985 /* Return the bottom y-position of the line the iterator IT is in.
986 This can modify IT's settings. */
989 line_bottom_y (it)
990 struct it *it;
992 int line_height = it->max_ascent + it->max_descent;
993 int line_top_y = it->current_y;
995 if (line_height == 0)
997 if (last_height)
998 line_height = last_height;
999 else if (IT_CHARPOS (*it) < ZV)
1001 move_it_by_lines (it, 1, 1);
1002 line_height = (it->max_ascent || it->max_descent
1003 ? it->max_ascent + it->max_descent
1004 : last_height);
1006 else
1008 struct glyph_row *row = it->glyph_row;
1010 /* Use the default character height. */
1011 it->glyph_row = NULL;
1012 it->what = IT_CHARACTER;
1013 it->c = ' ';
1014 it->len = 1;
1015 PRODUCE_GLYPHS (it);
1016 line_height = it->ascent + it->descent;
1017 it->glyph_row = row;
1021 return line_top_y + line_height;
1025 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1026 1 if POS is visible and the line containing POS is fully visible.
1027 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1028 and header-lines heights. */
1031 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1032 struct window *w;
1033 int charpos, *fully, exact_mode_line_heights_p;
1035 struct it it;
1036 struct text_pos top;
1037 int visible_p;
1038 struct buffer *old_buffer = NULL;
1040 if (XBUFFER (w->buffer) != current_buffer)
1042 old_buffer = current_buffer;
1043 set_buffer_internal_1 (XBUFFER (w->buffer));
1046 *fully = visible_p = 0;
1047 SET_TEXT_POS_FROM_MARKER (top, w->start);
1049 /* Compute exact mode line heights, if requested. */
1050 if (exact_mode_line_heights_p)
1052 if (WINDOW_WANTS_MODELINE_P (w))
1053 current_mode_line_height
1054 = display_mode_line (w, MODE_LINE_FACE_ID,
1055 current_buffer->mode_line_format);
1057 if (WINDOW_WANTS_HEADER_LINE_P (w))
1058 current_header_line_height
1059 = display_mode_line (w, HEADER_LINE_FACE_ID,
1060 current_buffer->header_line_format);
1063 start_display (&it, w, top);
1064 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1065 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1067 /* Note that we may overshoot because of invisible text. */
1068 if (IT_CHARPOS (it) >= charpos)
1070 int top_y = it.current_y;
1071 int bottom_y = line_bottom_y (&it);
1072 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1074 if (top_y < window_top_y)
1075 visible_p = bottom_y > window_top_y;
1076 else if (top_y < it.last_visible_y)
1078 visible_p = 1;
1079 *fully = bottom_y <= it.last_visible_y;
1082 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1084 move_it_by_lines (&it, 1, 0);
1085 if (charpos < IT_CHARPOS (it))
1087 visible_p = 1;
1088 *fully = 0;
1092 if (old_buffer)
1093 set_buffer_internal_1 (old_buffer);
1095 current_header_line_height = current_mode_line_height = -1;
1096 return visible_p;
1100 /* Return the next character from STR which is MAXLEN bytes long.
1101 Return in *LEN the length of the character. This is like
1102 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1103 we find one, we return a `?', but with the length of the invalid
1104 character. */
1106 static INLINE int
1107 string_char_and_length (str, maxlen, len)
1108 unsigned char *str;
1109 int maxlen, *len;
1111 int c;
1113 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1114 if (!CHAR_VALID_P (c, 1))
1115 /* We may not change the length here because other places in Emacs
1116 don't use this function, i.e. they silently accept invalid
1117 characters. */
1118 c = '?';
1120 return c;
1125 /* Given a position POS containing a valid character and byte position
1126 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1128 static struct text_pos
1129 string_pos_nchars_ahead (pos, string, nchars)
1130 struct text_pos pos;
1131 Lisp_Object string;
1132 int nchars;
1134 xassert (STRINGP (string) && nchars >= 0);
1136 if (STRING_MULTIBYTE (string))
1138 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1139 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1140 int len;
1142 while (nchars--)
1144 string_char_and_length (p, rest, &len);
1145 p += len, rest -= len;
1146 xassert (rest >= 0);
1147 CHARPOS (pos) += 1;
1148 BYTEPOS (pos) += len;
1151 else
1152 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1154 return pos;
1158 /* Value is the text position, i.e. character and byte position,
1159 for character position CHARPOS in STRING. */
1161 static INLINE struct text_pos
1162 string_pos (charpos, string)
1163 int charpos;
1164 Lisp_Object string;
1166 struct text_pos pos;
1167 xassert (STRINGP (string));
1168 xassert (charpos >= 0);
1169 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1170 return pos;
1174 /* Value is a text position, i.e. character and byte position, for
1175 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1176 means recognize multibyte characters. */
1178 static struct text_pos
1179 c_string_pos (charpos, s, multibyte_p)
1180 int charpos;
1181 unsigned char *s;
1182 int multibyte_p;
1184 struct text_pos pos;
1186 xassert (s != NULL);
1187 xassert (charpos >= 0);
1189 if (multibyte_p)
1191 int rest = strlen (s), len;
1193 SET_TEXT_POS (pos, 0, 0);
1194 while (charpos--)
1196 string_char_and_length (s, rest, &len);
1197 s += len, rest -= len;
1198 xassert (rest >= 0);
1199 CHARPOS (pos) += 1;
1200 BYTEPOS (pos) += len;
1203 else
1204 SET_TEXT_POS (pos, charpos, charpos);
1206 return pos;
1210 /* Value is the number of characters in C string S. MULTIBYTE_P
1211 non-zero means recognize multibyte characters. */
1213 static int
1214 number_of_chars (s, multibyte_p)
1215 unsigned char *s;
1216 int multibyte_p;
1218 int nchars;
1220 if (multibyte_p)
1222 int rest = strlen (s), len;
1223 unsigned char *p = (unsigned char *) s;
1225 for (nchars = 0; rest > 0; ++nchars)
1227 string_char_and_length (p, rest, &len);
1228 rest -= len, p += len;
1231 else
1232 nchars = strlen (s);
1234 return nchars;
1238 /* Compute byte position NEWPOS->bytepos corresponding to
1239 NEWPOS->charpos. POS is a known position in string STRING.
1240 NEWPOS->charpos must be >= POS.charpos. */
1242 static void
1243 compute_string_pos (newpos, pos, string)
1244 struct text_pos *newpos, pos;
1245 Lisp_Object string;
1247 xassert (STRINGP (string));
1248 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1250 if (STRING_MULTIBYTE (string))
1251 *newpos = string_pos_nchars_ahead (pos, string,
1252 CHARPOS (*newpos) - CHARPOS (pos));
1253 else
1254 BYTEPOS (*newpos) = CHARPOS (*newpos);
1259 /***********************************************************************
1260 Lisp form evaluation
1261 ***********************************************************************/
1263 /* Error handler for safe_eval and safe_call. */
1265 static Lisp_Object
1266 safe_eval_handler (arg)
1267 Lisp_Object arg;
1269 add_to_log ("Error during redisplay: %s", arg, Qnil);
1270 return Qnil;
1274 /* Evaluate SEXPR and return the result, or nil if something went
1275 wrong. */
1277 Lisp_Object
1278 safe_eval (sexpr)
1279 Lisp_Object sexpr;
1281 int count = BINDING_STACK_SIZE ();
1282 struct gcpro gcpro1;
1283 Lisp_Object val;
1285 GCPRO1 (sexpr);
1286 specbind (Qinhibit_redisplay, Qt);
1287 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1288 UNGCPRO;
1289 return unbind_to (count, val);
1293 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1294 Return the result, or nil if something went wrong. */
1296 Lisp_Object
1297 safe_call (nargs, args)
1298 int nargs;
1299 Lisp_Object *args;
1301 int count = BINDING_STACK_SIZE ();
1302 Lisp_Object val;
1303 struct gcpro gcpro1;
1305 GCPRO1 (args[0]);
1306 gcpro1.nvars = nargs;
1307 specbind (Qinhibit_redisplay, Qt);
1308 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1309 safe_eval_handler);
1310 UNGCPRO;
1311 return unbind_to (count, val);
1315 /* Call function FN with one argument ARG.
1316 Return the result, or nil if something went wrong. */
1318 Lisp_Object
1319 safe_call1 (fn, arg)
1320 Lisp_Object fn, arg;
1322 Lisp_Object args[2];
1323 args[0] = fn;
1324 args[1] = arg;
1325 return safe_call (2, args);
1330 /***********************************************************************
1331 Debugging
1332 ***********************************************************************/
1334 #if 0
1336 /* Define CHECK_IT to perform sanity checks on iterators.
1337 This is for debugging. It is too slow to do unconditionally. */
1339 static void
1340 check_it (it)
1341 struct it *it;
1343 if (it->method == next_element_from_string)
1345 xassert (STRINGP (it->string));
1346 xassert (IT_STRING_CHARPOS (*it) >= 0);
1348 else if (it->method == next_element_from_buffer)
1350 /* Check that character and byte positions agree. */
1351 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1354 if (it->dpvec)
1355 xassert (it->current.dpvec_index >= 0);
1356 else
1357 xassert (it->current.dpvec_index < 0);
1360 #define CHECK_IT(IT) check_it ((IT))
1362 #else /* not 0 */
1364 #define CHECK_IT(IT) (void) 0
1366 #endif /* not 0 */
1369 #if GLYPH_DEBUG
1371 /* Check that the window end of window W is what we expect it
1372 to be---the last row in the current matrix displaying text. */
1374 static void
1375 check_window_end (w)
1376 struct window *w;
1378 if (!MINI_WINDOW_P (w)
1379 && !NILP (w->window_end_valid))
1381 struct glyph_row *row;
1382 xassert ((row = MATRIX_ROW (w->current_matrix,
1383 XFASTINT (w->window_end_vpos)),
1384 !row->enabled_p
1385 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1386 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1390 #define CHECK_WINDOW_END(W) check_window_end ((W))
1392 #else /* not GLYPH_DEBUG */
1394 #define CHECK_WINDOW_END(W) (void) 0
1396 #endif /* not GLYPH_DEBUG */
1400 /***********************************************************************
1401 Iterator initialization
1402 ***********************************************************************/
1404 /* Initialize IT for displaying current_buffer in window W, starting
1405 at character position CHARPOS. CHARPOS < 0 means that no buffer
1406 position is specified which is useful when the iterator is assigned
1407 a position later. BYTEPOS is the byte position corresponding to
1408 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1410 If ROW is not null, calls to produce_glyphs with IT as parameter
1411 will produce glyphs in that row.
1413 BASE_FACE_ID is the id of a base face to use. It must be one of
1414 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1415 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1416 displaying the tool-bar.
1418 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1419 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1420 corresponding mode line glyph row of the desired matrix of W. */
1422 void
1423 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1424 struct it *it;
1425 struct window *w;
1426 int charpos, bytepos;
1427 struct glyph_row *row;
1428 enum face_id base_face_id;
1430 int highlight_region_p;
1432 /* Some precondition checks. */
1433 xassert (w != NULL && it != NULL);
1434 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1436 /* If face attributes have been changed since the last redisplay,
1437 free realized faces now because they depend on face definitions
1438 that might have changed. */
1439 if (face_change_count)
1441 face_change_count = 0;
1442 free_all_realized_faces (Qnil);
1445 /* Use one of the mode line rows of W's desired matrix if
1446 appropriate. */
1447 if (row == NULL)
1449 if (base_face_id == MODE_LINE_FACE_ID)
1450 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1451 else if (base_face_id == HEADER_LINE_FACE_ID)
1452 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1455 /* Clear IT. */
1456 bzero (it, sizeof *it);
1457 it->current.overlay_string_index = -1;
1458 it->current.dpvec_index = -1;
1459 it->base_face_id = base_face_id;
1461 /* The window in which we iterate over current_buffer: */
1462 XSETWINDOW (it->window, w);
1463 it->w = w;
1464 it->f = XFRAME (w->frame);
1466 /* Extra space between lines (on window systems only). */
1467 if (base_face_id == DEFAULT_FACE_ID
1468 && FRAME_WINDOW_P (it->f))
1470 if (NATNUMP (current_buffer->extra_line_spacing))
1471 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1472 else if (it->f->extra_line_spacing > 0)
1473 it->extra_line_spacing = it->f->extra_line_spacing;
1476 /* If realized faces have been removed, e.g. because of face
1477 attribute changes of named faces, recompute them. When running
1478 in batch mode, the face cache of Vterminal_frame is null. If
1479 we happen to get called, make a dummy face cache. */
1480 if (
1481 #ifndef WINDOWSNT
1482 noninteractive &&
1483 #endif
1484 FRAME_FACE_CACHE (it->f) == NULL)
1485 init_frame_faces (it->f);
1486 if (FRAME_FACE_CACHE (it->f)->used == 0)
1487 recompute_basic_faces (it->f);
1489 /* Current value of the `space-width', and 'height' properties. */
1490 it->space_width = Qnil;
1491 it->font_height = Qnil;
1493 /* Are control characters displayed as `^C'? */
1494 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1496 /* -1 means everything between a CR and the following line end
1497 is invisible. >0 means lines indented more than this value are
1498 invisible. */
1499 it->selective = (INTEGERP (current_buffer->selective_display)
1500 ? XFASTINT (current_buffer->selective_display)
1501 : (!NILP (current_buffer->selective_display)
1502 ? -1 : 0));
1503 it->selective_display_ellipsis_p
1504 = !NILP (current_buffer->selective_display_ellipses);
1506 /* Display table to use. */
1507 it->dp = window_display_table (w);
1509 /* Are multibyte characters enabled in current_buffer? */
1510 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1512 /* Non-zero if we should highlight the region. */
1513 highlight_region_p
1514 = (!NILP (Vtransient_mark_mode)
1515 && !NILP (current_buffer->mark_active)
1516 && XMARKER (current_buffer->mark)->buffer != 0);
1518 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1519 start and end of a visible region in window IT->w. Set both to
1520 -1 to indicate no region. */
1521 if (highlight_region_p
1522 /* Maybe highlight only in selected window. */
1523 && (/* Either show region everywhere. */
1524 highlight_nonselected_windows
1525 /* Or show region in the selected window. */
1526 || w == XWINDOW (selected_window)
1527 /* Or show the region if we are in the mini-buffer and W is
1528 the window the mini-buffer refers to. */
1529 || (MINI_WINDOW_P (XWINDOW (selected_window))
1530 && WINDOWP (Vminibuf_scroll_window)
1531 && w == XWINDOW (Vminibuf_scroll_window))))
1533 int charpos = marker_position (current_buffer->mark);
1534 it->region_beg_charpos = min (PT, charpos);
1535 it->region_end_charpos = max (PT, charpos);
1537 else
1538 it->region_beg_charpos = it->region_end_charpos = -1;
1540 /* Get the position at which the redisplay_end_trigger hook should
1541 be run, if it is to be run at all. */
1542 if (MARKERP (w->redisplay_end_trigger)
1543 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1544 it->redisplay_end_trigger_charpos
1545 = marker_position (w->redisplay_end_trigger);
1546 else if (INTEGERP (w->redisplay_end_trigger))
1547 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1549 /* Correct bogus values of tab_width. */
1550 it->tab_width = XINT (current_buffer->tab_width);
1551 if (it->tab_width <= 0 || it->tab_width > 1000)
1552 it->tab_width = 8;
1554 /* Are lines in the display truncated? */
1555 it->truncate_lines_p
1556 = (base_face_id != DEFAULT_FACE_ID
1557 || XINT (it->w->hscroll)
1558 || (truncate_partial_width_windows
1559 && !WINDOW_FULL_WIDTH_P (it->w))
1560 || !NILP (current_buffer->truncate_lines));
1562 /* Get dimensions of truncation and continuation glyphs. These are
1563 displayed as bitmaps under X, so we don't need them for such
1564 frames. */
1565 if (!FRAME_WINDOW_P (it->f))
1567 if (it->truncate_lines_p)
1569 /* We will need the truncation glyph. */
1570 xassert (it->glyph_row == NULL);
1571 produce_special_glyphs (it, IT_TRUNCATION);
1572 it->truncation_pixel_width = it->pixel_width;
1574 else
1576 /* We will need the continuation glyph. */
1577 xassert (it->glyph_row == NULL);
1578 produce_special_glyphs (it, IT_CONTINUATION);
1579 it->continuation_pixel_width = it->pixel_width;
1582 /* Reset these values to zero becaue the produce_special_glyphs
1583 above has changed them. */
1584 it->pixel_width = it->ascent = it->descent = 0;
1585 it->phys_ascent = it->phys_descent = 0;
1588 /* Set this after getting the dimensions of truncation and
1589 continuation glyphs, so that we don't produce glyphs when calling
1590 produce_special_glyphs, above. */
1591 it->glyph_row = row;
1592 it->area = TEXT_AREA;
1594 /* Get the dimensions of the display area. The display area
1595 consists of the visible window area plus a horizontally scrolled
1596 part to the left of the window. All x-values are relative to the
1597 start of this total display area. */
1598 if (base_face_id != DEFAULT_FACE_ID)
1600 /* Mode lines, menu bar in terminal frames. */
1601 it->first_visible_x = 0;
1602 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1604 else
1606 it->first_visible_x
1607 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1608 it->last_visible_x = (it->first_visible_x
1609 + window_box_width (w, TEXT_AREA));
1611 /* If we truncate lines, leave room for the truncator glyph(s) at
1612 the right margin. Otherwise, leave room for the continuation
1613 glyph(s). Truncation and continuation glyphs are not inserted
1614 for window-based redisplay. */
1615 if (!FRAME_WINDOW_P (it->f))
1617 if (it->truncate_lines_p)
1618 it->last_visible_x -= it->truncation_pixel_width;
1619 else
1620 it->last_visible_x -= it->continuation_pixel_width;
1623 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1624 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1627 /* Leave room for a border glyph. */
1628 if (!FRAME_WINDOW_P (it->f)
1629 && !WINDOW_RIGHTMOST_P (it->w))
1630 it->last_visible_x -= 1;
1632 it->last_visible_y = window_text_bottom_y (w);
1634 /* For mode lines and alike, arrange for the first glyph having a
1635 left box line if the face specifies a box. */
1636 if (base_face_id != DEFAULT_FACE_ID)
1638 struct face *face;
1640 it->face_id = base_face_id;
1642 /* If we have a boxed mode line, make the first character appear
1643 with a left box line. */
1644 face = FACE_FROM_ID (it->f, base_face_id);
1645 if (face->box != FACE_NO_BOX)
1646 it->start_of_box_run_p = 1;
1649 /* If a buffer position was specified, set the iterator there,
1650 getting overlays and face properties from that position. */
1651 if (charpos > 0)
1653 it->end_charpos = ZV;
1654 it->face_id = -1;
1655 IT_CHARPOS (*it) = charpos;
1657 /* Compute byte position if not specified. */
1658 if (bytepos <= 0)
1659 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1660 else
1661 IT_BYTEPOS (*it) = bytepos;
1663 /* Compute faces etc. */
1664 reseat (it, it->current.pos, 1);
1667 CHECK_IT (it);
1671 /* Initialize IT for the display of window W with window start POS. */
1673 void
1674 start_display (it, w, pos)
1675 struct it *it;
1676 struct window *w;
1677 struct text_pos pos;
1679 int start_at_line_beg_p;
1680 struct glyph_row *row;
1681 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1682 int first_y;
1684 row = w->desired_matrix->rows + first_vpos;
1685 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1686 first_y = it->current_y;
1688 /* If window start is not at a line start, move back to the line
1689 start. This makes sure that we take continuation lines into
1690 account. */
1691 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1692 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1693 if (!start_at_line_beg_p)
1694 reseat_at_previous_visible_line_start (it);
1696 /* If window start is not at a line start, skip forward to POS to
1697 get the correct continuation_lines_width and current_x. */
1698 if (!start_at_line_beg_p)
1700 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1702 /* If lines are continued, this line may end in the middle of a
1703 multi-glyph character (e.g. a control character displayed as
1704 \003, or in the middle of an overlay string). In this case
1705 move_it_to above will not have taken us to the start of
1706 the continuation line but to the end of the continued line. */
1707 if (!it->truncate_lines_p)
1709 if (it->current_x > 0)
1711 if (it->current.dpvec_index >= 0
1712 || it->current.overlay_string_index >= 0)
1714 set_iterator_to_next (it, 1);
1715 move_it_in_display_line_to (it, -1, -1, 0);
1718 it->continuation_lines_width += it->current_x;
1721 /* We're starting a new display line, not affected by the
1722 height of the continued line, so clear the appropriate
1723 fields in the iterator structure. */
1724 it->max_ascent = it->max_descent = 0;
1725 it->max_phys_ascent = it->max_phys_descent = 0;
1728 it->current_y = first_y;
1729 it->vpos = 0;
1730 it->current_x = it->hpos = 0;
1733 #if 0 /* Don't assert the following because start_display is sometimes
1734 called intentionally with a window start that is not at a
1735 line start. Please leave this code in as a comment. */
1737 /* Window start should be on a line start, now. */
1738 xassert (it->continuation_lines_width
1739 || IT_CHARPOS (it) == BEGV
1740 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1741 #endif /* 0 */
1745 /* Return 1 if POS is a position in ellipses displayed for invisible
1746 text. W is the window we display, for text property lookup. */
1748 static int
1749 in_ellipses_for_invisible_text_p (pos, w)
1750 struct display_pos *pos;
1751 struct window *w;
1753 Lisp_Object prop, window;
1754 int ellipses_p = 0;
1755 int charpos = CHARPOS (pos->pos);
1757 /* If POS specifies a position in a display vector, this might
1758 be for an ellipsis displayed for invisible text. We won't
1759 get the iterator set up for delivering that ellipsis unless
1760 we make sure that it gets aware of the invisible text. */
1761 if (pos->dpvec_index >= 0
1762 && pos->overlay_string_index < 0
1763 && CHARPOS (pos->string_pos) < 0
1764 && charpos > BEGV
1765 && (XSETWINDOW (window, w),
1766 prop = Fget_char_property (make_number (charpos),
1767 Qinvisible, window),
1768 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1770 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1771 window);
1772 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1773 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1774 ellipses_p = 1;
1777 return ellipses_p;
1781 /* Initialize IT for stepping through current_buffer in window W,
1782 starting at position POS that includes overlay string and display
1783 vector/ control character translation position information. */
1785 static void
1786 init_from_display_pos (it, w, pos)
1787 struct it *it;
1788 struct window *w;
1789 struct display_pos *pos;
1791 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1793 /* If POS specifies a position in a display vector, this might
1794 be for an ellipsis displayed for invisible text. We won't
1795 get the iterator set up for delivering that ellipsis unless
1796 we make sure that it gets aware of the invisible text. */
1797 if (in_ellipses_for_invisible_text_p (pos, w))
1799 --charpos;
1800 bytepos = 0;
1803 /* Keep in mind: the call to reseat in init_iterator skips invisible
1804 text, so we might end up at a position different from POS. This
1805 is only a problem when POS is a row start after a newline and an
1806 overlay starts there with an after-string, and the overlay has an
1807 invisible property. Since we don't skip invisible text in
1808 display_line and elsewhere immediately after consuming the
1809 newline before the row start, such a POS will not be in a string,
1810 but the call to init_iterator below will move us to the
1811 after-string. */
1812 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1814 /* If position is within an overlay string, set up IT to
1815 the right overlay string. */
1816 if (pos->overlay_string_index >= 0)
1818 int relative_index;
1820 /* We already have the first chunk of overlay strings in
1821 IT->overlay_strings. Load more until the one for
1822 pos->overlay_string_index is in IT->overlay_strings. */
1823 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1825 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1826 it->current.overlay_string_index = 0;
1827 while (n--)
1829 load_overlay_strings (it);
1830 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1834 it->current.overlay_string_index = pos->overlay_string_index;
1835 relative_index = (it->current.overlay_string_index
1836 % OVERLAY_STRING_CHUNK_SIZE);
1837 it->string = it->overlay_strings[relative_index];
1838 xassert (STRINGP (it->string));
1839 it->current.string_pos = pos->string_pos;
1840 it->method = next_element_from_string;
1842 else if (it->current.overlay_string_index >= 0)
1844 /* If POS says we're already after an overlay string ending at
1845 POS, make sure to pop the iterator because it will be in
1846 front of that overlay string. When POS is ZV, we've thereby
1847 also ``processed'' overlay strings at ZV. */
1848 while (it->sp)
1849 pop_it (it);
1850 it->current.overlay_string_index = -1;
1851 it->method = next_element_from_buffer;
1852 if (CHARPOS (pos->pos) == ZV)
1853 it->overlay_strings_at_end_processed_p = 1;
1856 if (CHARPOS (pos->string_pos) >= 0)
1858 /* Recorded position is not in an overlay string, but in another
1859 string. This can only be a string from a `display' property.
1860 IT should already be filled with that string. */
1861 it->current.string_pos = pos->string_pos;
1862 xassert (STRINGP (it->string));
1865 /* Restore position in display vector translations, control
1866 character translations or ellipses. */
1867 if (pos->dpvec_index >= 0)
1869 if (it->dpvec == NULL)
1870 get_next_display_element (it);
1871 xassert (it->dpvec && it->current.dpvec_index == 0);
1872 it->current.dpvec_index = pos->dpvec_index;
1875 CHECK_IT (it);
1879 /* Initialize IT for stepping through current_buffer in window W
1880 starting at ROW->start. */
1882 static void
1883 init_to_row_start (it, w, row)
1884 struct it *it;
1885 struct window *w;
1886 struct glyph_row *row;
1888 init_from_display_pos (it, w, &row->start);
1889 it->continuation_lines_width = row->continuation_lines_width;
1890 CHECK_IT (it);
1894 /* Initialize IT for stepping through current_buffer in window W
1895 starting in the line following ROW, i.e. starting at ROW->end. */
1897 static void
1898 init_to_row_end (it, w, row)
1899 struct it *it;
1900 struct window *w;
1901 struct glyph_row *row;
1903 init_from_display_pos (it, w, &row->end);
1905 if (row->continued_p)
1906 it->continuation_lines_width = (row->continuation_lines_width
1907 + row->pixel_width);
1908 CHECK_IT (it);
1914 /***********************************************************************
1915 Text properties
1916 ***********************************************************************/
1918 /* Called when IT reaches IT->stop_charpos. Handle text property and
1919 overlay changes. Set IT->stop_charpos to the next position where
1920 to stop. */
1922 static void
1923 handle_stop (it)
1924 struct it *it;
1926 enum prop_handled handled;
1927 int handle_overlay_change_p = 1;
1928 struct props *p;
1930 it->dpvec = NULL;
1931 it->current.dpvec_index = -1;
1935 handled = HANDLED_NORMALLY;
1937 /* Call text property handlers. */
1938 for (p = it_props; p->handler; ++p)
1940 handled = p->handler (it);
1942 if (handled == HANDLED_RECOMPUTE_PROPS)
1943 break;
1944 else if (handled == HANDLED_RETURN)
1945 return;
1946 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1947 handle_overlay_change_p = 0;
1950 if (handled != HANDLED_RECOMPUTE_PROPS)
1952 /* Don't check for overlay strings below when set to deliver
1953 characters from a display vector. */
1954 if (it->method == next_element_from_display_vector)
1955 handle_overlay_change_p = 0;
1957 /* Handle overlay changes. */
1958 if (handle_overlay_change_p)
1959 handled = handle_overlay_change (it);
1961 /* Determine where to stop next. */
1962 if (handled == HANDLED_NORMALLY)
1963 compute_stop_pos (it);
1966 while (handled == HANDLED_RECOMPUTE_PROPS);
1970 /* Compute IT->stop_charpos from text property and overlay change
1971 information for IT's current position. */
1973 static void
1974 compute_stop_pos (it)
1975 struct it *it;
1977 register INTERVAL iv, next_iv;
1978 Lisp_Object object, limit, position;
1980 /* If nowhere else, stop at the end. */
1981 it->stop_charpos = it->end_charpos;
1983 if (STRINGP (it->string))
1985 /* Strings are usually short, so don't limit the search for
1986 properties. */
1987 object = it->string;
1988 limit = Qnil;
1989 position = make_number (IT_STRING_CHARPOS (*it));
1991 else
1993 int charpos;
1995 /* If next overlay change is in front of the current stop pos
1996 (which is IT->end_charpos), stop there. Note: value of
1997 next_overlay_change is point-max if no overlay change
1998 follows. */
1999 charpos = next_overlay_change (IT_CHARPOS (*it));
2000 if (charpos < it->stop_charpos)
2001 it->stop_charpos = charpos;
2003 /* If showing the region, we have to stop at the region
2004 start or end because the face might change there. */
2005 if (it->region_beg_charpos > 0)
2007 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2008 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2009 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2010 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2013 /* Set up variables for computing the stop position from text
2014 property changes. */
2015 XSETBUFFER (object, current_buffer);
2016 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2017 position = make_number (IT_CHARPOS (*it));
2021 /* Get the interval containing IT's position. Value is a null
2022 interval if there isn't such an interval. */
2023 iv = validate_interval_range (object, &position, &position, 0);
2024 if (!NULL_INTERVAL_P (iv))
2026 Lisp_Object values_here[LAST_PROP_IDX];
2027 struct props *p;
2029 /* Get properties here. */
2030 for (p = it_props; p->handler; ++p)
2031 values_here[p->idx] = textget (iv->plist, *p->name);
2033 /* Look for an interval following iv that has different
2034 properties. */
2035 for (next_iv = next_interval (iv);
2036 (!NULL_INTERVAL_P (next_iv)
2037 && (NILP (limit)
2038 || XFASTINT (limit) > next_iv->position));
2039 next_iv = next_interval (next_iv))
2041 for (p = it_props; p->handler; ++p)
2043 Lisp_Object new_value;
2045 new_value = textget (next_iv->plist, *p->name);
2046 if (!EQ (values_here[p->idx], new_value))
2047 break;
2050 if (p->handler)
2051 break;
2054 if (!NULL_INTERVAL_P (next_iv))
2056 if (INTEGERP (limit)
2057 && next_iv->position >= XFASTINT (limit))
2058 /* No text property change up to limit. */
2059 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2060 else
2061 /* Text properties change in next_iv. */
2062 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2066 xassert (STRINGP (it->string)
2067 || (it->stop_charpos >= BEGV
2068 && it->stop_charpos >= IT_CHARPOS (*it)));
2072 /* Return the position of the next overlay change after POS in
2073 current_buffer. Value is point-max if no overlay change
2074 follows. This is like `next-overlay-change' but doesn't use
2075 xmalloc. */
2077 static int
2078 next_overlay_change (pos)
2079 int pos;
2081 int noverlays;
2082 int endpos;
2083 Lisp_Object *overlays;
2084 int len;
2085 int i;
2087 /* Get all overlays at the given position. */
2088 len = 10;
2089 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2090 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2091 if (noverlays > len)
2093 len = noverlays;
2094 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2095 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2098 /* If any of these overlays ends before endpos,
2099 use its ending point instead. */
2100 for (i = 0; i < noverlays; ++i)
2102 Lisp_Object oend;
2103 int oendpos;
2105 oend = OVERLAY_END (overlays[i]);
2106 oendpos = OVERLAY_POSITION (oend);
2107 endpos = min (endpos, oendpos);
2110 return endpos;
2115 /***********************************************************************
2116 Fontification
2117 ***********************************************************************/
2119 /* Handle changes in the `fontified' property of the current buffer by
2120 calling hook functions from Qfontification_functions to fontify
2121 regions of text. */
2123 static enum prop_handled
2124 handle_fontified_prop (it)
2125 struct it *it;
2127 Lisp_Object prop, pos;
2128 enum prop_handled handled = HANDLED_NORMALLY;
2130 /* Get the value of the `fontified' property at IT's current buffer
2131 position. (The `fontified' property doesn't have a special
2132 meaning in strings.) If the value is nil, call functions from
2133 Qfontification_functions. */
2134 if (!STRINGP (it->string)
2135 && it->s == NULL
2136 && !NILP (Vfontification_functions)
2137 && !NILP (Vrun_hooks)
2138 && (pos = make_number (IT_CHARPOS (*it)),
2139 prop = Fget_char_property (pos, Qfontified, Qnil),
2140 NILP (prop)))
2142 int count = BINDING_STACK_SIZE ();
2143 Lisp_Object val;
2145 val = Vfontification_functions;
2146 specbind (Qfontification_functions, Qnil);
2147 specbind (Qafter_change_functions, Qnil);
2149 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2150 safe_call1 (val, pos);
2151 else
2153 Lisp_Object globals, fn;
2154 struct gcpro gcpro1, gcpro2;
2156 globals = Qnil;
2157 GCPRO2 (val, globals);
2159 for (; CONSP (val); val = XCDR (val))
2161 fn = XCAR (val);
2163 if (EQ (fn, Qt))
2165 /* A value of t indicates this hook has a local
2166 binding; it means to run the global binding too.
2167 In a global value, t should not occur. If it
2168 does, we must ignore it to avoid an endless
2169 loop. */
2170 for (globals = Fdefault_value (Qfontification_functions);
2171 CONSP (globals);
2172 globals = XCDR (globals))
2174 fn = XCAR (globals);
2175 if (!EQ (fn, Qt))
2176 safe_call1 (fn, pos);
2179 else
2180 safe_call1 (fn, pos);
2183 UNGCPRO;
2186 unbind_to (count, Qnil);
2188 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2189 something. This avoids an endless loop if they failed to
2190 fontify the text for which reason ever. */
2191 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2192 handled = HANDLED_RECOMPUTE_PROPS;
2195 return handled;
2200 /***********************************************************************
2201 Faces
2202 ***********************************************************************/
2204 /* Set up iterator IT from face properties at its current position.
2205 Called from handle_stop. */
2207 static enum prop_handled
2208 handle_face_prop (it)
2209 struct it *it;
2211 int new_face_id, next_stop;
2213 if (!STRINGP (it->string))
2215 new_face_id
2216 = face_at_buffer_position (it->w,
2217 IT_CHARPOS (*it),
2218 it->region_beg_charpos,
2219 it->region_end_charpos,
2220 &next_stop,
2221 (IT_CHARPOS (*it)
2222 + TEXT_PROP_DISTANCE_LIMIT),
2225 /* Is this a start of a run of characters with box face?
2226 Caveat: this can be called for a freshly initialized
2227 iterator; face_id is -1 is this case. We know that the new
2228 face will not change until limit, i.e. if the new face has a
2229 box, all characters up to limit will have one. But, as
2230 usual, we don't know whether limit is really the end. */
2231 if (new_face_id != it->face_id)
2233 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2235 /* If new face has a box but old face has not, this is
2236 the start of a run of characters with box, i.e. it has
2237 a shadow on the left side. The value of face_id of the
2238 iterator will be -1 if this is the initial call that gets
2239 the face. In this case, we have to look in front of IT's
2240 position and see whether there is a face != new_face_id. */
2241 it->start_of_box_run_p
2242 = (new_face->box != FACE_NO_BOX
2243 && (it->face_id >= 0
2244 || IT_CHARPOS (*it) == BEG
2245 || new_face_id != face_before_it_pos (it)));
2246 it->face_box_p = new_face->box != FACE_NO_BOX;
2249 else
2251 int base_face_id, bufpos;
2253 if (it->current.overlay_string_index >= 0)
2254 bufpos = IT_CHARPOS (*it);
2255 else
2256 bufpos = 0;
2258 /* For strings from a buffer, i.e. overlay strings or strings
2259 from a `display' property, use the face at IT's current
2260 buffer position as the base face to merge with, so that
2261 overlay strings appear in the same face as surrounding
2262 text, unless they specify their own faces. */
2263 base_face_id = underlying_face_id (it);
2265 new_face_id = face_at_string_position (it->w,
2266 it->string,
2267 IT_STRING_CHARPOS (*it),
2268 bufpos,
2269 it->region_beg_charpos,
2270 it->region_end_charpos,
2271 &next_stop,
2272 base_face_id, 0);
2274 #if 0 /* This shouldn't be neccessary. Let's check it. */
2275 /* If IT is used to display a mode line we would really like to
2276 use the mode line face instead of the frame's default face. */
2277 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2278 && new_face_id == DEFAULT_FACE_ID)
2279 new_face_id = MODE_LINE_FACE_ID;
2280 #endif
2282 /* Is this a start of a run of characters with box? Caveat:
2283 this can be called for a freshly allocated iterator; face_id
2284 is -1 is this case. We know that the new face will not
2285 change until the next check pos, i.e. if the new face has a
2286 box, all characters up to that position will have a
2287 box. But, as usual, we don't know whether that position
2288 is really the end. */
2289 if (new_face_id != it->face_id)
2291 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2292 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2294 /* If new face has a box but old face hasn't, this is the
2295 start of a run of characters with box, i.e. it has a
2296 shadow on the left side. */
2297 it->start_of_box_run_p
2298 = new_face->box && (old_face == NULL || !old_face->box);
2299 it->face_box_p = new_face->box != FACE_NO_BOX;
2303 it->face_id = new_face_id;
2304 return HANDLED_NORMALLY;
2308 /* Return the ID of the face ``underlying'' IT's current position,
2309 which is in a string. If the iterator is associated with a
2310 buffer, return the face at IT's current buffer position.
2311 Otherwise, use the iterator's base_face_id. */
2313 static int
2314 underlying_face_id (it)
2315 struct it *it;
2317 int face_id = it->base_face_id, i;
2319 xassert (STRINGP (it->string));
2321 for (i = it->sp - 1; i >= 0; --i)
2322 if (NILP (it->stack[i].string))
2323 face_id = it->stack[i].face_id;
2325 return face_id;
2329 /* Compute the face one character before or after the current position
2330 of IT. BEFORE_P non-zero means get the face in front of IT's
2331 position. Value is the id of the face. */
2333 static int
2334 face_before_or_after_it_pos (it, before_p)
2335 struct it *it;
2336 int before_p;
2338 int face_id, limit;
2339 int next_check_charpos;
2340 struct text_pos pos;
2342 xassert (it->s == NULL);
2344 if (STRINGP (it->string))
2346 int bufpos, base_face_id;
2348 /* No face change past the end of the string (for the case
2349 we are padding with spaces). No face change before the
2350 string start. */
2351 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2352 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2353 return it->face_id;
2355 /* Set pos to the position before or after IT's current position. */
2356 if (before_p)
2357 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2358 else
2359 /* For composition, we must check the character after the
2360 composition. */
2361 pos = (it->what == IT_COMPOSITION
2362 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2363 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2365 if (it->current.overlay_string_index >= 0)
2366 bufpos = IT_CHARPOS (*it);
2367 else
2368 bufpos = 0;
2370 base_face_id = underlying_face_id (it);
2372 /* Get the face for ASCII, or unibyte. */
2373 face_id = face_at_string_position (it->w,
2374 it->string,
2375 CHARPOS (pos),
2376 bufpos,
2377 it->region_beg_charpos,
2378 it->region_end_charpos,
2379 &next_check_charpos,
2380 base_face_id, 0);
2382 /* Correct the face for charsets different from ASCII. Do it
2383 for the multibyte case only. The face returned above is
2384 suitable for unibyte text if IT->string is unibyte. */
2385 if (STRING_MULTIBYTE (it->string))
2387 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2388 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2389 int c, len;
2390 struct face *face = FACE_FROM_ID (it->f, face_id);
2392 c = string_char_and_length (p, rest, &len);
2393 face_id = FACE_FOR_CHAR (it->f, face, c);
2396 else
2398 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2399 || (IT_CHARPOS (*it) <= BEGV && before_p))
2400 return it->face_id;
2402 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2403 pos = it->current.pos;
2405 if (before_p)
2406 DEC_TEXT_POS (pos, it->multibyte_p);
2407 else
2409 if (it->what == IT_COMPOSITION)
2410 /* For composition, we must check the position after the
2411 composition. */
2412 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2413 else
2414 INC_TEXT_POS (pos, it->multibyte_p);
2417 /* Determine face for CHARSET_ASCII, or unibyte. */
2418 face_id = face_at_buffer_position (it->w,
2419 CHARPOS (pos),
2420 it->region_beg_charpos,
2421 it->region_end_charpos,
2422 &next_check_charpos,
2423 limit, 0);
2425 /* Correct the face for charsets different from ASCII. Do it
2426 for the multibyte case only. The face returned above is
2427 suitable for unibyte text if current_buffer is unibyte. */
2428 if (it->multibyte_p)
2430 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2431 struct face *face = FACE_FROM_ID (it->f, face_id);
2432 face_id = FACE_FOR_CHAR (it->f, face, c);
2436 return face_id;
2441 /***********************************************************************
2442 Invisible text
2443 ***********************************************************************/
2445 /* Set up iterator IT from invisible properties at its current
2446 position. Called from handle_stop. */
2448 static enum prop_handled
2449 handle_invisible_prop (it)
2450 struct it *it;
2452 enum prop_handled handled = HANDLED_NORMALLY;
2454 if (STRINGP (it->string))
2456 extern Lisp_Object Qinvisible;
2457 Lisp_Object prop, end_charpos, limit, charpos;
2459 /* Get the value of the invisible text property at the
2460 current position. Value will be nil if there is no such
2461 property. */
2462 charpos = make_number (IT_STRING_CHARPOS (*it));
2463 prop = Fget_text_property (charpos, Qinvisible, it->string);
2465 if (!NILP (prop)
2466 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2468 handled = HANDLED_RECOMPUTE_PROPS;
2470 /* Get the position at which the next change of the
2471 invisible text property can be found in IT->string.
2472 Value will be nil if the property value is the same for
2473 all the rest of IT->string. */
2474 XSETINT (limit, XSTRING (it->string)->size);
2475 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2476 it->string, limit);
2478 /* Text at current position is invisible. The next
2479 change in the property is at position end_charpos.
2480 Move IT's current position to that position. */
2481 if (INTEGERP (end_charpos)
2482 && XFASTINT (end_charpos) < XFASTINT (limit))
2484 struct text_pos old;
2485 old = it->current.string_pos;
2486 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2487 compute_string_pos (&it->current.string_pos, old, it->string);
2489 else
2491 /* The rest of the string is invisible. If this is an
2492 overlay string, proceed with the next overlay string
2493 or whatever comes and return a character from there. */
2494 if (it->current.overlay_string_index >= 0)
2496 next_overlay_string (it);
2497 /* Don't check for overlay strings when we just
2498 finished processing them. */
2499 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2501 else
2503 struct Lisp_String *s = XSTRING (it->string);
2504 IT_STRING_CHARPOS (*it) = s->size;
2505 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2510 else
2512 int visible_p, newpos, next_stop;
2513 Lisp_Object pos, prop;
2515 /* First of all, is there invisible text at this position? */
2516 pos = make_number (IT_CHARPOS (*it));
2517 prop = Fget_char_property (pos, Qinvisible, it->window);
2519 /* If we are on invisible text, skip over it. */
2520 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2521 && IT_CHARPOS (*it) < it->end_charpos)
2523 /* Record whether we have to display an ellipsis for the
2524 invisible text. */
2525 int display_ellipsis_p
2526 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2528 handled = HANDLED_RECOMPUTE_PROPS;
2530 /* Loop skipping over invisible text. The loop is left at
2531 ZV or with IT on the first char being visible again. */
2534 /* Try to skip some invisible text. Return value is the
2535 position reached which can be equal to IT's position
2536 if there is nothing invisible here. This skips both
2537 over invisible text properties and overlays with
2538 invisible property. */
2539 newpos = skip_invisible (IT_CHARPOS (*it),
2540 &next_stop, ZV, it->window);
2542 /* If we skipped nothing at all we weren't at invisible
2543 text in the first place. If everything to the end of
2544 the buffer was skipped, end the loop. */
2545 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2546 visible_p = 1;
2547 else
2549 /* We skipped some characters but not necessarily
2550 all there are. Check if we ended up on visible
2551 text. Fget_char_property returns the property of
2552 the char before the given position, i.e. if we
2553 get visible_p = 1, this means that the char at
2554 newpos is visible. */
2555 pos = make_number (newpos);
2556 prop = Fget_char_property (pos, Qinvisible, it->window);
2557 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2560 /* If we ended up on invisible text, proceed to
2561 skip starting with next_stop. */
2562 if (!visible_p)
2563 IT_CHARPOS (*it) = next_stop;
2565 while (!visible_p);
2567 /* The position newpos is now either ZV or on visible text. */
2568 IT_CHARPOS (*it) = newpos;
2569 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2571 /* Maybe return `...' next for the end of the invisible text. */
2572 if (display_ellipsis_p)
2574 if (it->dp
2575 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2577 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2578 it->dpvec = v->contents;
2579 it->dpend = v->contents + v->size;
2581 else
2583 /* Default `...'. */
2584 it->dpvec = default_invis_vector;
2585 it->dpend = default_invis_vector + 3;
2588 /* The ellipsis display does not replace the display of
2589 the character at the new position. Indicate this by
2590 setting IT->dpvec_char_len to zero. */
2591 it->dpvec_char_len = 0;
2593 it->current.dpvec_index = 0;
2594 it->method = next_element_from_display_vector;
2599 return handled;
2604 /***********************************************************************
2605 'display' property
2606 ***********************************************************************/
2608 /* Set up iterator IT from `display' property at its current position.
2609 Called from handle_stop. */
2611 static enum prop_handled
2612 handle_display_prop (it)
2613 struct it *it;
2615 Lisp_Object prop, object;
2616 struct text_pos *position;
2617 int display_replaced_p = 0;
2619 if (STRINGP (it->string))
2621 object = it->string;
2622 position = &it->current.string_pos;
2624 else
2626 object = it->w->buffer;
2627 position = &it->current.pos;
2630 /* Reset those iterator values set from display property values. */
2631 it->font_height = Qnil;
2632 it->space_width = Qnil;
2633 it->voffset = 0;
2635 /* We don't support recursive `display' properties, i.e. string
2636 values that have a string `display' property, that have a string
2637 `display' property etc. */
2638 if (!it->string_from_display_prop_p)
2639 it->area = TEXT_AREA;
2641 prop = Fget_char_property (make_number (position->charpos),
2642 Qdisplay, object);
2643 if (NILP (prop))
2644 return HANDLED_NORMALLY;
2646 if (CONSP (prop)
2647 && CONSP (XCAR (prop))
2648 && !EQ (Qmargin, XCAR (XCAR (prop))))
2650 /* A list of sub-properties. */
2651 for (; CONSP (prop); prop = XCDR (prop))
2653 if (handle_single_display_prop (it, XCAR (prop), object,
2654 position, display_replaced_p))
2655 display_replaced_p = 1;
2658 else if (VECTORP (prop))
2660 int i;
2661 for (i = 0; i < ASIZE (prop); ++i)
2662 if (handle_single_display_prop (it, AREF (prop, i), object,
2663 position, display_replaced_p))
2664 display_replaced_p = 1;
2666 else
2668 if (handle_single_display_prop (it, prop, object, position, 0))
2669 display_replaced_p = 1;
2672 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2676 /* Value is the position of the end of the `display' property starting
2677 at START_POS in OBJECT. */
2679 static struct text_pos
2680 display_prop_end (it, object, start_pos)
2681 struct it *it;
2682 Lisp_Object object;
2683 struct text_pos start_pos;
2685 Lisp_Object end;
2686 struct text_pos end_pos;
2688 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2689 Qdisplay, object, Qnil);
2690 CHARPOS (end_pos) = XFASTINT (end);
2691 if (STRINGP (object))
2692 compute_string_pos (&end_pos, start_pos, it->string);
2693 else
2694 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2696 return end_pos;
2700 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2701 is the object in which the `display' property was found. *POSITION
2702 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2703 means that we previously saw a display sub-property which already
2704 replaced text display with something else, for example an image;
2705 ignore such properties after the first one has been processed.
2707 If PROP is a `space' or `image' sub-property, set *POSITION to the
2708 end position of the `display' property.
2710 Value is non-zero something was found which replaces the display
2711 of buffer or string text. */
2713 static int
2714 handle_single_display_prop (it, prop, object, position,
2715 display_replaced_before_p)
2716 struct it *it;
2717 Lisp_Object prop;
2718 Lisp_Object object;
2719 struct text_pos *position;
2720 int display_replaced_before_p;
2722 Lisp_Object value;
2723 int replaces_text_display_p = 0;
2724 Lisp_Object form;
2726 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2727 evaluated. If the result is nil, VALUE is ignored. */
2728 form = Qt;
2729 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2731 prop = XCDR (prop);
2732 if (!CONSP (prop))
2733 return 0;
2734 form = XCAR (prop);
2735 prop = XCDR (prop);
2738 if (!NILP (form) && !EQ (form, Qt))
2740 struct gcpro gcpro1;
2741 struct text_pos end_pos, pt;
2743 GCPRO1 (form);
2744 end_pos = display_prop_end (it, object, *position);
2746 /* Temporarily set point to the end position, and then evaluate
2747 the form. This makes `(eolp)' work as FORM. */
2748 if (BUFFERP (object))
2750 CHARPOS (pt) = PT;
2751 BYTEPOS (pt) = PT_BYTE;
2752 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2755 form = safe_eval (form);
2757 if (BUFFERP (object))
2758 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2759 UNGCPRO;
2762 if (NILP (form))
2763 return 0;
2765 if (CONSP (prop)
2766 && EQ (XCAR (prop), Qheight)
2767 && CONSP (XCDR (prop)))
2769 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2770 return 0;
2772 /* `(height HEIGHT)'. */
2773 it->font_height = XCAR (XCDR (prop));
2774 if (!NILP (it->font_height))
2776 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2777 int new_height = -1;
2779 if (CONSP (it->font_height)
2780 && (EQ (XCAR (it->font_height), Qplus)
2781 || EQ (XCAR (it->font_height), Qminus))
2782 && CONSP (XCDR (it->font_height))
2783 && INTEGERP (XCAR (XCDR (it->font_height))))
2785 /* `(+ N)' or `(- N)' where N is an integer. */
2786 int steps = XINT (XCAR (XCDR (it->font_height)));
2787 if (EQ (XCAR (it->font_height), Qplus))
2788 steps = - steps;
2789 it->face_id = smaller_face (it->f, it->face_id, steps);
2791 else if (FUNCTIONP (it->font_height))
2793 /* Call function with current height as argument.
2794 Value is the new height. */
2795 Lisp_Object height;
2796 height = safe_call1 (it->font_height,
2797 face->lface[LFACE_HEIGHT_INDEX]);
2798 if (NUMBERP (height))
2799 new_height = XFLOATINT (height);
2801 else if (NUMBERP (it->font_height))
2803 /* Value is a multiple of the canonical char height. */
2804 struct face *face;
2806 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2807 new_height = (XFLOATINT (it->font_height)
2808 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2810 else
2812 /* Evaluate IT->font_height with `height' bound to the
2813 current specified height to get the new height. */
2814 Lisp_Object value;
2815 int count = BINDING_STACK_SIZE ();
2817 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2818 value = safe_eval (it->font_height);
2819 unbind_to (count, Qnil);
2821 if (NUMBERP (value))
2822 new_height = XFLOATINT (value);
2825 if (new_height > 0)
2826 it->face_id = face_with_height (it->f, it->face_id, new_height);
2829 else if (CONSP (prop)
2830 && EQ (XCAR (prop), Qspace_width)
2831 && CONSP (XCDR (prop)))
2833 /* `(space_width WIDTH)'. */
2834 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2835 return 0;
2837 value = XCAR (XCDR (prop));
2838 if (NUMBERP (value) && XFLOATINT (value) > 0)
2839 it->space_width = value;
2841 else if (CONSP (prop)
2842 && EQ (XCAR (prop), Qraise)
2843 && CONSP (XCDR (prop)))
2845 /* `(raise FACTOR)'. */
2846 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2847 return 0;
2849 #ifdef HAVE_WINDOW_SYSTEM
2850 value = XCAR (XCDR (prop));
2851 if (NUMBERP (value))
2853 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2854 it->voffset = - (XFLOATINT (value)
2855 * (FONT_HEIGHT (face->font)));
2857 #endif /* HAVE_WINDOW_SYSTEM */
2859 else if (!it->string_from_display_prop_p)
2861 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2862 VALUE) or `((margin nil) VALUE)' or VALUE. */
2863 Lisp_Object location, value;
2864 struct text_pos start_pos;
2865 int valid_p;
2867 /* Characters having this form of property are not displayed, so
2868 we have to find the end of the property. */
2869 start_pos = *position;
2870 *position = display_prop_end (it, object, start_pos);
2871 value = Qnil;
2873 /* Let's stop at the new position and assume that all
2874 text properties change there. */
2875 it->stop_charpos = position->charpos;
2877 location = Qunbound;
2878 if (CONSP (prop) && CONSP (XCAR (prop)))
2880 Lisp_Object tem;
2882 value = XCDR (prop);
2883 if (CONSP (value))
2884 value = XCAR (value);
2886 tem = XCAR (prop);
2887 if (EQ (XCAR (tem), Qmargin)
2888 && (tem = XCDR (tem),
2889 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2890 (NILP (tem)
2891 || EQ (tem, Qleft_margin)
2892 || EQ (tem, Qright_margin))))
2893 location = tem;
2896 if (EQ (location, Qunbound))
2898 location = Qnil;
2899 value = prop;
2902 #ifdef HAVE_WINDOW_SYSTEM
2903 if (FRAME_TERMCAP_P (it->f))
2904 valid_p = STRINGP (value);
2905 else
2906 valid_p = (STRINGP (value)
2907 || (CONSP (value) && EQ (XCAR (value), Qspace))
2908 || valid_image_p (value));
2909 #else /* not HAVE_WINDOW_SYSTEM */
2910 valid_p = STRINGP (value);
2911 #endif /* not HAVE_WINDOW_SYSTEM */
2913 if ((EQ (location, Qleft_margin)
2914 || EQ (location, Qright_margin)
2915 || NILP (location))
2916 && valid_p
2917 && !display_replaced_before_p)
2919 replaces_text_display_p = 1;
2921 /* Save current settings of IT so that we can restore them
2922 when we are finished with the glyph property value. */
2923 push_it (it);
2925 if (NILP (location))
2926 it->area = TEXT_AREA;
2927 else if (EQ (location, Qleft_margin))
2928 it->area = LEFT_MARGIN_AREA;
2929 else
2930 it->area = RIGHT_MARGIN_AREA;
2932 if (STRINGP (value))
2934 it->string = value;
2935 it->multibyte_p = STRING_MULTIBYTE (it->string);
2936 it->current.overlay_string_index = -1;
2937 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2938 it->end_charpos = it->string_nchars
2939 = XSTRING (it->string)->size;
2940 it->method = next_element_from_string;
2941 it->stop_charpos = 0;
2942 it->string_from_display_prop_p = 1;
2943 /* Say that we haven't consumed the characters with
2944 `display' property yet. The call to pop_it in
2945 set_iterator_to_next will clean this up. */
2946 *position = start_pos;
2948 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2950 it->method = next_element_from_stretch;
2951 it->object = value;
2952 it->current.pos = it->position = start_pos;
2954 #ifdef HAVE_WINDOW_SYSTEM
2955 else
2957 it->what = IT_IMAGE;
2958 it->image_id = lookup_image (it->f, value);
2959 it->position = start_pos;
2960 it->object = NILP (object) ? it->w->buffer : object;
2961 it->method = next_element_from_image;
2963 /* Say that we haven't consumed the characters with
2964 `display' property yet. The call to pop_it in
2965 set_iterator_to_next will clean this up. */
2966 *position = start_pos;
2968 #endif /* HAVE_WINDOW_SYSTEM */
2970 else
2971 /* Invalid property or property not supported. Restore
2972 the position to what it was before. */
2973 *position = start_pos;
2976 return replaces_text_display_p;
2980 /* Check if PROP is a display sub-property value whose text should be
2981 treated as intangible. */
2983 static int
2984 single_display_prop_intangible_p (prop)
2985 Lisp_Object prop;
2987 /* Skip over `when FORM'. */
2988 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2990 prop = XCDR (prop);
2991 if (!CONSP (prop))
2992 return 0;
2993 prop = XCDR (prop);
2996 if (!CONSP (prop))
2997 return 0;
2999 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3000 we don't need to treat text as intangible. */
3001 if (EQ (XCAR (prop), Qmargin))
3003 prop = XCDR (prop);
3004 if (!CONSP (prop))
3005 return 0;
3007 prop = XCDR (prop);
3008 if (!CONSP (prop)
3009 || EQ (XCAR (prop), Qleft_margin)
3010 || EQ (XCAR (prop), Qright_margin))
3011 return 0;
3014 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3018 /* Check if PROP is a display property value whose text should be
3019 treated as intangible. */
3022 display_prop_intangible_p (prop)
3023 Lisp_Object prop;
3025 if (CONSP (prop)
3026 && CONSP (XCAR (prop))
3027 && !EQ (Qmargin, XCAR (XCAR (prop))))
3029 /* A list of sub-properties. */
3030 while (CONSP (prop))
3032 if (single_display_prop_intangible_p (XCAR (prop)))
3033 return 1;
3034 prop = XCDR (prop);
3037 else if (VECTORP (prop))
3039 /* A vector of sub-properties. */
3040 int i;
3041 for (i = 0; i < ASIZE (prop); ++i)
3042 if (single_display_prop_intangible_p (AREF (prop, i)))
3043 return 1;
3045 else
3046 return single_display_prop_intangible_p (prop);
3048 return 0;
3052 /* Return 1 if PROP is a display sub-property value containing STRING. */
3054 static int
3055 single_display_prop_string_p (prop, string)
3056 Lisp_Object prop, string;
3058 extern Lisp_Object Qwhen, Qmargin;
3060 if (EQ (string, prop))
3061 return 1;
3063 /* Skip over `when FORM'. */
3064 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3066 prop = XCDR (prop);
3067 if (!CONSP (prop))
3068 return 0;
3069 prop = XCDR (prop);
3072 if (CONSP (prop))
3073 /* Skip over `margin LOCATION'. */
3074 if (EQ (XCAR (prop), Qmargin))
3076 prop = XCDR (prop);
3077 if (!CONSP (prop))
3078 return 0;
3080 prop = XCDR (prop);
3081 if (!CONSP (prop))
3082 return 0;
3085 return CONSP (prop) && EQ (XCAR (prop), string);
3089 /* Return 1 if STRING appears in the `display' property PROP. */
3091 static int
3092 display_prop_string_p (prop, string)
3093 Lisp_Object prop, string;
3095 extern Lisp_Object Qwhen, Qmargin;
3097 if (CONSP (prop)
3098 && CONSP (XCAR (prop))
3099 && !EQ (Qmargin, XCAR (XCAR (prop))))
3101 /* A list of sub-properties. */
3102 while (CONSP (prop))
3104 if (single_display_prop_string_p (XCAR (prop), string))
3105 return 1;
3106 prop = XCDR (prop);
3109 else if (VECTORP (prop))
3111 /* A vector of sub-properties. */
3112 int i;
3113 for (i = 0; i < ASIZE (prop); ++i)
3114 if (single_display_prop_string_p (AREF (prop, i), string))
3115 return 1;
3117 else
3118 return single_display_prop_string_p (prop, string);
3120 return 0;
3124 /* Determine from which buffer position in W's buffer STRING comes
3125 from. AROUND_CHARPOS is an approximate position where it could
3126 be from. Value is the buffer position or 0 if it couldn't be
3127 determined.
3129 W's buffer must be current.
3131 This function is necessary because we don't record buffer positions
3132 in glyphs generated from strings (to keep struct glyph small).
3133 This function may only use code that doesn't eval because it is
3134 called asynchronously from note_mouse_highlight. */
3137 string_buffer_position (w, string, around_charpos)
3138 struct window *w;
3139 Lisp_Object string;
3140 int around_charpos;
3142 Lisp_Object around = make_number (around_charpos);
3143 Lisp_Object limit, prop, pos;
3144 const int MAX_DISTANCE = 1000;
3145 int found = 0;
3147 pos = make_number (around_charpos);
3148 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3149 while (!found && !EQ (pos, limit))
3151 prop = Fget_char_property (pos, Qdisplay, Qnil);
3152 if (!NILP (prop) && display_prop_string_p (prop, string))
3153 found = 1;
3154 else
3155 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3158 if (!found)
3160 pos = make_number (around_charpos);
3161 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3162 while (!found && !EQ (pos, limit))
3164 prop = Fget_char_property (pos, Qdisplay, Qnil);
3165 if (!NILP (prop) && display_prop_string_p (prop, string))
3166 found = 1;
3167 else
3168 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3169 limit);
3173 return found ? XINT (pos) : 0;
3178 /***********************************************************************
3179 `composition' property
3180 ***********************************************************************/
3182 /* Set up iterator IT from `composition' property at its current
3183 position. Called from handle_stop. */
3185 static enum prop_handled
3186 handle_composition_prop (it)
3187 struct it *it;
3189 Lisp_Object prop, string;
3190 int pos, pos_byte, end;
3191 enum prop_handled handled = HANDLED_NORMALLY;
3193 if (STRINGP (it->string))
3195 pos = IT_STRING_CHARPOS (*it);
3196 pos_byte = IT_STRING_BYTEPOS (*it);
3197 string = it->string;
3199 else
3201 pos = IT_CHARPOS (*it);
3202 pos_byte = IT_BYTEPOS (*it);
3203 string = Qnil;
3206 /* If there's a valid composition and point is not inside of the
3207 composition (in the case that the composition is from the current
3208 buffer), draw a glyph composed from the composition components. */
3209 if (find_composition (pos, -1, &pos, &end, &prop, string)
3210 && COMPOSITION_VALID_P (pos, end, prop)
3211 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3213 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3215 if (id >= 0)
3217 it->method = next_element_from_composition;
3218 it->cmp_id = id;
3219 it->cmp_len = COMPOSITION_LENGTH (prop);
3220 /* For a terminal, draw only the first character of the
3221 components. */
3222 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3223 it->len = (STRINGP (it->string)
3224 ? string_char_to_byte (it->string, end)
3225 : CHAR_TO_BYTE (end)) - pos_byte;
3226 it->stop_charpos = end;
3227 handled = HANDLED_RETURN;
3231 return handled;
3236 /***********************************************************************
3237 Overlay strings
3238 ***********************************************************************/
3240 /* The following structure is used to record overlay strings for
3241 later sorting in load_overlay_strings. */
3243 struct overlay_entry
3245 Lisp_Object overlay;
3246 Lisp_Object string;
3247 int priority;
3248 int after_string_p;
3252 /* Set up iterator IT from overlay strings at its current position.
3253 Called from handle_stop. */
3255 static enum prop_handled
3256 handle_overlay_change (it)
3257 struct it *it;
3259 if (!STRINGP (it->string) && get_overlay_strings (it))
3260 return HANDLED_RECOMPUTE_PROPS;
3261 else
3262 return HANDLED_NORMALLY;
3266 /* Set up the next overlay string for delivery by IT, if there is an
3267 overlay string to deliver. Called by set_iterator_to_next when the
3268 end of the current overlay string is reached. If there are more
3269 overlay strings to display, IT->string and
3270 IT->current.overlay_string_index are set appropriately here.
3271 Otherwise IT->string is set to nil. */
3273 static void
3274 next_overlay_string (it)
3275 struct it *it;
3277 ++it->current.overlay_string_index;
3278 if (it->current.overlay_string_index == it->n_overlay_strings)
3280 /* No more overlay strings. Restore IT's settings to what
3281 they were before overlay strings were processed, and
3282 continue to deliver from current_buffer. */
3283 pop_it (it);
3284 xassert (it->stop_charpos >= BEGV
3285 && it->stop_charpos <= it->end_charpos);
3286 it->string = Qnil;
3287 it->current.overlay_string_index = -1;
3288 SET_TEXT_POS (it->current.string_pos, -1, -1);
3289 it->n_overlay_strings = 0;
3290 it->method = next_element_from_buffer;
3292 /* If we're at the end of the buffer, record that we have
3293 processed the overlay strings there already, so that
3294 next_element_from_buffer doesn't try it again. */
3295 if (IT_CHARPOS (*it) >= it->end_charpos)
3296 it->overlay_strings_at_end_processed_p = 1;
3298 else
3300 /* There are more overlay strings to process. If
3301 IT->current.overlay_string_index has advanced to a position
3302 where we must load IT->overlay_strings with more strings, do
3303 it. */
3304 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3306 if (it->current.overlay_string_index && i == 0)
3307 load_overlay_strings (it);
3309 /* Initialize IT to deliver display elements from the overlay
3310 string. */
3311 it->string = it->overlay_strings[i];
3312 it->multibyte_p = STRING_MULTIBYTE (it->string);
3313 SET_TEXT_POS (it->current.string_pos, 0, 0);
3314 it->method = next_element_from_string;
3315 it->stop_charpos = 0;
3318 CHECK_IT (it);
3322 /* Compare two overlay_entry structures E1 and E2. Used as a
3323 comparison function for qsort in load_overlay_strings. Overlay
3324 strings for the same position are sorted so that
3326 1. All after-strings come in front of before-strings, except
3327 when they come from the same overlay.
3329 2. Within after-strings, strings are sorted so that overlay strings
3330 from overlays with higher priorities come first.
3332 2. Within before-strings, strings are sorted so that overlay
3333 strings from overlays with higher priorities come last.
3335 Value is analogous to strcmp. */
3338 static int
3339 compare_overlay_entries (e1, e2)
3340 void *e1, *e2;
3342 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3343 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3344 int result;
3346 if (entry1->after_string_p != entry2->after_string_p)
3348 /* Let after-strings appear in front of before-strings if
3349 they come from different overlays. */
3350 if (EQ (entry1->overlay, entry2->overlay))
3351 result = entry1->after_string_p ? 1 : -1;
3352 else
3353 result = entry1->after_string_p ? -1 : 1;
3355 else if (entry1->after_string_p)
3356 /* After-strings sorted in order of decreasing priority. */
3357 result = entry2->priority - entry1->priority;
3358 else
3359 /* Before-strings sorted in order of increasing priority. */
3360 result = entry1->priority - entry2->priority;
3362 return result;
3366 /* Load the vector IT->overlay_strings with overlay strings from IT's
3367 current buffer position. Set IT->n_overlays to the total number of
3368 overlay strings found.
3370 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3371 a time. On entry into load_overlay_strings,
3372 IT->current.overlay_string_index gives the number of overlay
3373 strings that have already been loaded by previous calls to this
3374 function.
3376 IT->add_overlay_start contains an additional overlay start
3377 position to consider for taking overlay strings from, if non-zero.
3378 This position comes into play when the overlay has an `invisible'
3379 property, and both before and after-strings. When we've skipped to
3380 the end of the overlay, because of its `invisible' property, we
3381 nevertheless want its before-string to appear.
3382 IT->add_overlay_start will contain the overlay start position
3383 in this case.
3385 Overlay strings are sorted so that after-string strings come in
3386 front of before-string strings. Within before and after-strings,
3387 strings are sorted by overlay priority. See also function
3388 compare_overlay_entries. */
3390 static void
3391 load_overlay_strings (it)
3392 struct it *it;
3394 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3395 Lisp_Object ov, overlay, window, str, invisible;
3396 int start, end;
3397 int size = 20;
3398 int n = 0, i, j, invis_p;
3399 struct overlay_entry *entries
3400 = (struct overlay_entry *) alloca (size * sizeof *entries);
3401 int charpos = IT_CHARPOS (*it);
3403 /* Append the overlay string STRING of overlay OVERLAY to vector
3404 `entries' which has size `size' and currently contains `n'
3405 elements. AFTER_P non-zero means STRING is an after-string of
3406 OVERLAY. */
3407 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3408 do \
3410 Lisp_Object priority; \
3412 if (n == size) \
3414 int new_size = 2 * size; \
3415 struct overlay_entry *old = entries; \
3416 entries = \
3417 (struct overlay_entry *) alloca (new_size \
3418 * sizeof *entries); \
3419 bcopy (old, entries, size * sizeof *entries); \
3420 size = new_size; \
3423 entries[n].string = (STRING); \
3424 entries[n].overlay = (OVERLAY); \
3425 priority = Foverlay_get ((OVERLAY), Qpriority); \
3426 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3427 entries[n].after_string_p = (AFTER_P); \
3428 ++n; \
3430 while (0)
3432 /* Process overlay before the overlay center. */
3433 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3435 overlay = XCAR (ov);
3436 xassert (OVERLAYP (overlay));
3437 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3438 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3440 if (end < charpos)
3441 break;
3443 /* Skip this overlay if it doesn't start or end at IT's current
3444 position. */
3445 if (end != charpos && start != charpos)
3446 continue;
3448 /* Skip this overlay if it doesn't apply to IT->w. */
3449 window = Foverlay_get (overlay, Qwindow);
3450 if (WINDOWP (window) && XWINDOW (window) != it->w)
3451 continue;
3453 /* If the text ``under'' the overlay is invisible, both before-
3454 and after-strings from this overlay are visible; start and
3455 end position are indistinguishable. */
3456 invisible = Foverlay_get (overlay, Qinvisible);
3457 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3459 /* If overlay has a non-empty before-string, record it. */
3460 if ((start == charpos || (end == charpos && invis_p))
3461 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3462 && XSTRING (str)->size)
3463 RECORD_OVERLAY_STRING (overlay, str, 0);
3465 /* If overlay has a non-empty after-string, record it. */
3466 if ((end == charpos || (start == charpos && invis_p))
3467 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3468 && XSTRING (str)->size)
3469 RECORD_OVERLAY_STRING (overlay, str, 1);
3472 /* Process overlays after the overlay center. */
3473 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3475 overlay = XCAR (ov);
3476 xassert (OVERLAYP (overlay));
3477 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3478 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3480 if (start > charpos)
3481 break;
3483 /* Skip this overlay if it doesn't start or end at IT's current
3484 position. */
3485 if (end != charpos && start != charpos)
3486 continue;
3488 /* Skip this overlay if it doesn't apply to IT->w. */
3489 window = Foverlay_get (overlay, Qwindow);
3490 if (WINDOWP (window) && XWINDOW (window) != it->w)
3491 continue;
3493 /* If the text ``under'' the overlay is invisible, it has a zero
3494 dimension, and both before- and after-strings apply. */
3495 invisible = Foverlay_get (overlay, Qinvisible);
3496 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3498 /* If overlay has a non-empty before-string, record it. */
3499 if ((start == charpos || (end == charpos && invis_p))
3500 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3501 && XSTRING (str)->size)
3502 RECORD_OVERLAY_STRING (overlay, str, 0);
3504 /* If overlay has a non-empty after-string, record it. */
3505 if ((end == charpos || (start == charpos && invis_p))
3506 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3507 && XSTRING (str)->size)
3508 RECORD_OVERLAY_STRING (overlay, str, 1);
3511 #undef RECORD_OVERLAY_STRING
3513 /* Sort entries. */
3514 if (n > 1)
3515 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3517 /* Record the total number of strings to process. */
3518 it->n_overlay_strings = n;
3520 /* IT->current.overlay_string_index is the number of overlay strings
3521 that have already been consumed by IT. Copy some of the
3522 remaining overlay strings to IT->overlay_strings. */
3523 i = 0;
3524 j = it->current.overlay_string_index;
3525 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3526 it->overlay_strings[i++] = entries[j++].string;
3528 CHECK_IT (it);
3532 /* Get the first chunk of overlay strings at IT's current buffer
3533 position. Value is non-zero if at least one overlay string was
3534 found. */
3536 static int
3537 get_overlay_strings (it)
3538 struct it *it;
3540 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3541 process. This fills IT->overlay_strings with strings, and sets
3542 IT->n_overlay_strings to the total number of strings to process.
3543 IT->pos.overlay_string_index has to be set temporarily to zero
3544 because load_overlay_strings needs this; it must be set to -1
3545 when no overlay strings are found because a zero value would
3546 indicate a position in the first overlay string. */
3547 it->current.overlay_string_index = 0;
3548 load_overlay_strings (it);
3550 /* If we found overlay strings, set up IT to deliver display
3551 elements from the first one. Otherwise set up IT to deliver
3552 from current_buffer. */
3553 if (it->n_overlay_strings)
3555 /* Make sure we know settings in current_buffer, so that we can
3556 restore meaningful values when we're done with the overlay
3557 strings. */
3558 compute_stop_pos (it);
3559 xassert (it->face_id >= 0);
3561 /* Save IT's settings. They are restored after all overlay
3562 strings have been processed. */
3563 xassert (it->sp == 0);
3564 push_it (it);
3566 /* Set up IT to deliver display elements from the first overlay
3567 string. */
3568 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3569 it->stop_charpos = 0;
3570 it->string = it->overlay_strings[0];
3571 it->multibyte_p = STRING_MULTIBYTE (it->string);
3572 xassert (STRINGP (it->string));
3573 it->method = next_element_from_string;
3575 else
3577 it->string = Qnil;
3578 it->current.overlay_string_index = -1;
3579 it->method = next_element_from_buffer;
3582 CHECK_IT (it);
3584 /* Value is non-zero if we found at least one overlay string. */
3585 return STRINGP (it->string);
3590 /***********************************************************************
3591 Saving and restoring state
3592 ***********************************************************************/
3594 /* Save current settings of IT on IT->stack. Called, for example,
3595 before setting up IT for an overlay string, to be able to restore
3596 IT's settings to what they were after the overlay string has been
3597 processed. */
3599 static void
3600 push_it (it)
3601 struct it *it;
3603 struct iterator_stack_entry *p;
3605 xassert (it->sp < 2);
3606 p = it->stack + it->sp;
3608 p->stop_charpos = it->stop_charpos;
3609 xassert (it->face_id >= 0);
3610 p->face_id = it->face_id;
3611 p->string = it->string;
3612 p->pos = it->current;
3613 p->end_charpos = it->end_charpos;
3614 p->string_nchars = it->string_nchars;
3615 p->area = it->area;
3616 p->multibyte_p = it->multibyte_p;
3617 p->space_width = it->space_width;
3618 p->font_height = it->font_height;
3619 p->voffset = it->voffset;
3620 p->string_from_display_prop_p = it->string_from_display_prop_p;
3621 ++it->sp;
3625 /* Restore IT's settings from IT->stack. Called, for example, when no
3626 more overlay strings must be processed, and we return to delivering
3627 display elements from a buffer, or when the end of a string from a
3628 `display' property is reached and we return to delivering display
3629 elements from an overlay string, or from a buffer. */
3631 static void
3632 pop_it (it)
3633 struct it *it;
3635 struct iterator_stack_entry *p;
3637 xassert (it->sp > 0);
3638 --it->sp;
3639 p = it->stack + it->sp;
3640 it->stop_charpos = p->stop_charpos;
3641 it->face_id = p->face_id;
3642 it->string = p->string;
3643 it->current = p->pos;
3644 it->end_charpos = p->end_charpos;
3645 it->string_nchars = p->string_nchars;
3646 it->area = p->area;
3647 it->multibyte_p = p->multibyte_p;
3648 it->space_width = p->space_width;
3649 it->font_height = p->font_height;
3650 it->voffset = p->voffset;
3651 it->string_from_display_prop_p = p->string_from_display_prop_p;
3656 /***********************************************************************
3657 Moving over lines
3658 ***********************************************************************/
3660 /* Set IT's current position to the previous line start. */
3662 static void
3663 back_to_previous_line_start (it)
3664 struct it *it;
3666 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3667 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3671 /* Move IT to the next line start.
3673 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3674 we skipped over part of the text (as opposed to moving the iterator
3675 continuously over the text). Otherwise, don't change the value
3676 of *SKIPPED_P.
3678 Newlines may come from buffer text, overlay strings, or strings
3679 displayed via the `display' property. That's the reason we can't
3680 simply use find_next_newline_no_quit.
3682 Note that this function may not skip over invisible text that is so
3683 because of text properties and immediately follows a newline. If
3684 it would, function reseat_at_next_visible_line_start, when called
3685 from set_iterator_to_next, would effectively make invisible
3686 characters following a newline part of the wrong glyph row, which
3687 leads to wrong cursor motion. */
3689 static int
3690 forward_to_next_line_start (it, skipped_p)
3691 struct it *it;
3692 int *skipped_p;
3694 int old_selective, newline_found_p, n;
3695 const int MAX_NEWLINE_DISTANCE = 500;
3697 /* If already on a newline, just consume it to avoid unintended
3698 skipping over invisible text below. */
3699 if (it->what == IT_CHARACTER
3700 && it->c == '\n'
3701 && CHARPOS (it->position) == IT_CHARPOS (*it))
3703 set_iterator_to_next (it, 0);
3704 it->c = 0;
3705 return 1;
3708 /* Don't handle selective display in the following. It's (a)
3709 unnecessary because it's done by the caller, and (b) leads to an
3710 infinite recursion because next_element_from_ellipsis indirectly
3711 calls this function. */
3712 old_selective = it->selective;
3713 it->selective = 0;
3715 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3716 from buffer text. */
3717 for (n = newline_found_p = 0;
3718 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3719 n += STRINGP (it->string) ? 0 : 1)
3721 if (!get_next_display_element (it))
3722 break;
3723 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3724 set_iterator_to_next (it, 0);
3727 /* If we didn't find a newline near enough, see if we can use a
3728 short-cut. */
3729 if (n == MAX_NEWLINE_DISTANCE)
3731 int start = IT_CHARPOS (*it);
3732 int limit = find_next_newline_no_quit (start, 1);
3733 Lisp_Object pos;
3735 xassert (!STRINGP (it->string));
3737 /* If there isn't any `display' property in sight, and no
3738 overlays, we can just use the position of the newline in
3739 buffer text. */
3740 if (it->stop_charpos >= limit
3741 || ((pos = Fnext_single_property_change (make_number (start),
3742 Qdisplay,
3743 Qnil, make_number (limit)),
3744 NILP (pos))
3745 && next_overlay_change (start) == ZV))
3747 IT_CHARPOS (*it) = limit;
3748 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3749 *skipped_p = newline_found_p = 1;
3751 else
3753 while (get_next_display_element (it)
3754 && !newline_found_p)
3756 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3757 set_iterator_to_next (it, 0);
3762 it->selective = old_selective;
3763 return newline_found_p;
3767 /* Set IT's current position to the previous visible line start. Skip
3768 invisible text that is so either due to text properties or due to
3769 selective display. Caution: this does not change IT->current_x and
3770 IT->hpos. */
3772 static void
3773 back_to_previous_visible_line_start (it)
3774 struct it *it;
3776 int visible_p = 0;
3778 /* Go back one newline if not on BEGV already. */
3779 if (IT_CHARPOS (*it) > BEGV)
3780 back_to_previous_line_start (it);
3782 /* Move over lines that are invisible because of selective display
3783 or text properties. */
3784 while (IT_CHARPOS (*it) > BEGV
3785 && !visible_p)
3787 visible_p = 1;
3789 /* If selective > 0, then lines indented more than that values
3790 are invisible. */
3791 if (it->selective > 0
3792 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3793 it->selective))
3794 visible_p = 0;
3795 else
3797 Lisp_Object prop;
3799 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3800 Qinvisible, it->window);
3801 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3802 visible_p = 0;
3805 /* Back one more newline if the current one is invisible. */
3806 if (!visible_p)
3807 back_to_previous_line_start (it);
3810 xassert (IT_CHARPOS (*it) >= BEGV);
3811 xassert (IT_CHARPOS (*it) == BEGV
3812 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3813 CHECK_IT (it);
3817 /* Reseat iterator IT at the previous visible line start. Skip
3818 invisible text that is so either due to text properties or due to
3819 selective display. At the end, update IT's overlay information,
3820 face information etc. */
3822 static void
3823 reseat_at_previous_visible_line_start (it)
3824 struct it *it;
3826 back_to_previous_visible_line_start (it);
3827 reseat (it, it->current.pos, 1);
3828 CHECK_IT (it);
3832 /* Reseat iterator IT on the next visible line start in the current
3833 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3834 preceding the line start. Skip over invisible text that is so
3835 because of selective display. Compute faces, overlays etc at the
3836 new position. Note that this function does not skip over text that
3837 is invisible because of text properties. */
3839 static void
3840 reseat_at_next_visible_line_start (it, on_newline_p)
3841 struct it *it;
3842 int on_newline_p;
3844 int newline_found_p, skipped_p = 0;
3846 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3848 /* Skip over lines that are invisible because they are indented
3849 more than the value of IT->selective. */
3850 if (it->selective > 0)
3851 while (IT_CHARPOS (*it) < ZV
3852 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3853 it->selective))
3855 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3856 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3859 /* Position on the newline if that's what's requested. */
3860 if (on_newline_p && newline_found_p)
3862 if (STRINGP (it->string))
3864 if (IT_STRING_CHARPOS (*it) > 0)
3866 --IT_STRING_CHARPOS (*it);
3867 --IT_STRING_BYTEPOS (*it);
3870 else if (IT_CHARPOS (*it) > BEGV)
3872 --IT_CHARPOS (*it);
3873 --IT_BYTEPOS (*it);
3874 reseat (it, it->current.pos, 0);
3877 else if (skipped_p)
3878 reseat (it, it->current.pos, 0);
3880 CHECK_IT (it);
3885 /***********************************************************************
3886 Changing an iterator's position
3887 ***********************************************************************/
3889 /* Change IT's current position to POS in current_buffer. If FORCE_P
3890 is non-zero, always check for text properties at the new position.
3891 Otherwise, text properties are only looked up if POS >=
3892 IT->check_charpos of a property. */
3894 static void
3895 reseat (it, pos, force_p)
3896 struct it *it;
3897 struct text_pos pos;
3898 int force_p;
3900 int original_pos = IT_CHARPOS (*it);
3902 reseat_1 (it, pos, 0);
3904 /* Determine where to check text properties. Avoid doing it
3905 where possible because text property lookup is very expensive. */
3906 if (force_p
3907 || CHARPOS (pos) > it->stop_charpos
3908 || CHARPOS (pos) < original_pos)
3909 handle_stop (it);
3911 CHECK_IT (it);
3915 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3916 IT->stop_pos to POS, also. */
3918 static void
3919 reseat_1 (it, pos, set_stop_p)
3920 struct it *it;
3921 struct text_pos pos;
3922 int set_stop_p;
3924 /* Don't call this function when scanning a C string. */
3925 xassert (it->s == NULL);
3927 /* POS must be a reasonable value. */
3928 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3930 it->current.pos = it->position = pos;
3931 XSETBUFFER (it->object, current_buffer);
3932 it->end_charpos = ZV;
3933 it->dpvec = NULL;
3934 it->current.dpvec_index = -1;
3935 it->current.overlay_string_index = -1;
3936 IT_STRING_CHARPOS (*it) = -1;
3937 IT_STRING_BYTEPOS (*it) = -1;
3938 it->string = Qnil;
3939 it->method = next_element_from_buffer;
3940 it->sp = 0;
3941 it->face_before_selective_p = 0;
3943 if (set_stop_p)
3944 it->stop_charpos = CHARPOS (pos);
3948 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3949 If S is non-null, it is a C string to iterate over. Otherwise,
3950 STRING gives a Lisp string to iterate over.
3952 If PRECISION > 0, don't return more then PRECISION number of
3953 characters from the string.
3955 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3956 characters have been returned. FIELD_WIDTH < 0 means an infinite
3957 field width.
3959 MULTIBYTE = 0 means disable processing of multibyte characters,
3960 MULTIBYTE > 0 means enable it,
3961 MULTIBYTE < 0 means use IT->multibyte_p.
3963 IT must be initialized via a prior call to init_iterator before
3964 calling this function. */
3966 static void
3967 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3968 struct it *it;
3969 unsigned char *s;
3970 Lisp_Object string;
3971 int charpos;
3972 int precision, field_width, multibyte;
3974 /* No region in strings. */
3975 it->region_beg_charpos = it->region_end_charpos = -1;
3977 /* No text property checks performed by default, but see below. */
3978 it->stop_charpos = -1;
3980 /* Set iterator position and end position. */
3981 bzero (&it->current, sizeof it->current);
3982 it->current.overlay_string_index = -1;
3983 it->current.dpvec_index = -1;
3984 xassert (charpos >= 0);
3986 /* Use the setting of MULTIBYTE if specified. */
3987 if (multibyte >= 0)
3988 it->multibyte_p = multibyte > 0;
3990 if (s == NULL)
3992 xassert (STRINGP (string));
3993 it->string = string;
3994 it->s = NULL;
3995 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3996 it->method = next_element_from_string;
3997 it->current.string_pos = string_pos (charpos, string);
3999 else
4001 it->s = s;
4002 it->string = Qnil;
4004 /* Note that we use IT->current.pos, not it->current.string_pos,
4005 for displaying C strings. */
4006 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4007 if (it->multibyte_p)
4009 it->current.pos = c_string_pos (charpos, s, 1);
4010 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4012 else
4014 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4015 it->end_charpos = it->string_nchars = strlen (s);
4018 it->method = next_element_from_c_string;
4021 /* PRECISION > 0 means don't return more than PRECISION characters
4022 from the string. */
4023 if (precision > 0 && it->end_charpos - charpos > precision)
4024 it->end_charpos = it->string_nchars = charpos + precision;
4026 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4027 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4028 FIELD_WIDTH < 0 means infinite field width. This is useful for
4029 padding with `-' at the end of a mode line. */
4030 if (field_width < 0)
4031 field_width = INFINITY;
4032 if (field_width > it->end_charpos - charpos)
4033 it->end_charpos = charpos + field_width;
4035 /* Use the standard display table for displaying strings. */
4036 if (DISP_TABLE_P (Vstandard_display_table))
4037 it->dp = XCHAR_TABLE (Vstandard_display_table);
4039 it->stop_charpos = charpos;
4040 CHECK_IT (it);
4045 /***********************************************************************
4046 Iteration
4047 ***********************************************************************/
4049 /* Load IT's display element fields with information about the next
4050 display element from the current position of IT. Value is zero if
4051 end of buffer (or C string) is reached. */
4054 get_next_display_element (it)
4055 struct it *it;
4057 /* Non-zero means that we found an display element. Zero means that
4058 we hit the end of what we iterate over. Performance note: the
4059 function pointer `method' used here turns out to be faster than
4060 using a sequence of if-statements. */
4061 int success_p = (*it->method) (it);
4063 if (it->what == IT_CHARACTER)
4065 /* Map via display table or translate control characters.
4066 IT->c, IT->len etc. have been set to the next character by
4067 the function call above. If we have a display table, and it
4068 contains an entry for IT->c, translate it. Don't do this if
4069 IT->c itself comes from a display table, otherwise we could
4070 end up in an infinite recursion. (An alternative could be to
4071 count the recursion depth of this function and signal an
4072 error when a certain maximum depth is reached.) Is it worth
4073 it? */
4074 if (success_p && it->dpvec == NULL)
4076 Lisp_Object dv;
4078 if (it->dp
4079 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4080 VECTORP (dv)))
4082 struct Lisp_Vector *v = XVECTOR (dv);
4084 /* Return the first character from the display table
4085 entry, if not empty. If empty, don't display the
4086 current character. */
4087 if (v->size)
4089 it->dpvec_char_len = it->len;
4090 it->dpvec = v->contents;
4091 it->dpend = v->contents + v->size;
4092 it->current.dpvec_index = 0;
4093 it->method = next_element_from_display_vector;
4094 success_p = get_next_display_element (it);
4096 else
4098 set_iterator_to_next (it, 0);
4099 success_p = get_next_display_element (it);
4103 /* Translate control characters into `\003' or `^C' form.
4104 Control characters coming from a display table entry are
4105 currently not translated because we use IT->dpvec to hold
4106 the translation. This could easily be changed but I
4107 don't believe that it is worth doing.
4109 Non-printable multibyte characters are also translated
4110 octal form. */
4111 else if ((it->c < ' '
4112 && (it->area != TEXT_AREA
4113 || (it->c != '\n' && it->c != '\t')))
4114 || (it->c >= 127
4115 && it->len == 1)
4116 || !CHAR_PRINTABLE_P (it->c))
4118 /* IT->c is a control character which must be displayed
4119 either as '\003' or as `^C' where the '\\' and '^'
4120 can be defined in the display table. Fill
4121 IT->ctl_chars with glyphs for what we have to
4122 display. Then, set IT->dpvec to these glyphs. */
4123 GLYPH g;
4125 if (it->c < 128 && it->ctl_arrow_p)
4127 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4128 if (it->dp
4129 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4130 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4131 g = XINT (DISP_CTRL_GLYPH (it->dp));
4132 else
4133 g = FAST_MAKE_GLYPH ('^', 0);
4134 XSETINT (it->ctl_chars[0], g);
4136 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4137 XSETINT (it->ctl_chars[1], g);
4139 /* Set up IT->dpvec and return first character from it. */
4140 it->dpvec_char_len = it->len;
4141 it->dpvec = it->ctl_chars;
4142 it->dpend = it->dpvec + 2;
4143 it->current.dpvec_index = 0;
4144 it->method = next_element_from_display_vector;
4145 get_next_display_element (it);
4147 else
4149 unsigned char str[MAX_MULTIBYTE_LENGTH];
4150 int len;
4151 int i;
4152 GLYPH escape_glyph;
4154 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4155 if (it->dp
4156 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4157 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4158 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4159 else
4160 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4162 if (SINGLE_BYTE_CHAR_P (it->c))
4163 str[0] = it->c, len = 1;
4164 else
4165 len = CHAR_STRING (it->c, str);
4167 for (i = 0; i < len; i++)
4169 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4170 /* Insert three more glyphs into IT->ctl_chars for
4171 the octal display of the character. */
4172 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4173 XSETINT (it->ctl_chars[i * 4 + 1], g);
4174 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4175 XSETINT (it->ctl_chars[i * 4 + 2], g);
4176 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4177 XSETINT (it->ctl_chars[i * 4 + 3], g);
4180 /* Set up IT->dpvec and return the first character
4181 from it. */
4182 it->dpvec_char_len = it->len;
4183 it->dpvec = it->ctl_chars;
4184 it->dpend = it->dpvec + len * 4;
4185 it->current.dpvec_index = 0;
4186 it->method = next_element_from_display_vector;
4187 get_next_display_element (it);
4192 /* Adjust face id for a multibyte character. There are no
4193 multibyte character in unibyte text. */
4194 if (it->multibyte_p
4195 && success_p
4196 && FRAME_WINDOW_P (it->f))
4198 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4199 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4203 /* Is this character the last one of a run of characters with
4204 box? If yes, set IT->end_of_box_run_p to 1. */
4205 if (it->face_box_p
4206 && it->s == NULL)
4208 int face_id;
4209 struct face *face;
4211 it->end_of_box_run_p
4212 = ((face_id = face_after_it_pos (it),
4213 face_id != it->face_id)
4214 && (face = FACE_FROM_ID (it->f, face_id),
4215 face->box == FACE_NO_BOX));
4218 /* Value is 0 if end of buffer or string reached. */
4219 return success_p;
4223 /* Move IT to the next display element.
4225 RESEAT_P non-zero means if called on a newline in buffer text,
4226 skip to the next visible line start.
4228 Functions get_next_display_element and set_iterator_to_next are
4229 separate because I find this arrangement easier to handle than a
4230 get_next_display_element function that also increments IT's
4231 position. The way it is we can first look at an iterator's current
4232 display element, decide whether it fits on a line, and if it does,
4233 increment the iterator position. The other way around we probably
4234 would either need a flag indicating whether the iterator has to be
4235 incremented the next time, or we would have to implement a
4236 decrement position function which would not be easy to write. */
4238 void
4239 set_iterator_to_next (it, reseat_p)
4240 struct it *it;
4241 int reseat_p;
4243 /* Reset flags indicating start and end of a sequence of characters
4244 with box. Reset them at the start of this function because
4245 moving the iterator to a new position might set them. */
4246 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4248 if (it->method == next_element_from_buffer)
4250 /* The current display element of IT is a character from
4251 current_buffer. Advance in the buffer, and maybe skip over
4252 invisible lines that are so because of selective display. */
4253 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4254 reseat_at_next_visible_line_start (it, 0);
4255 else
4257 xassert (it->len != 0);
4258 IT_BYTEPOS (*it) += it->len;
4259 IT_CHARPOS (*it) += 1;
4260 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4263 else if (it->method == next_element_from_composition)
4265 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4266 if (STRINGP (it->string))
4268 IT_STRING_BYTEPOS (*it) += it->len;
4269 IT_STRING_CHARPOS (*it) += it->cmp_len;
4270 it->method = next_element_from_string;
4271 goto consider_string_end;
4273 else
4275 IT_BYTEPOS (*it) += it->len;
4276 IT_CHARPOS (*it) += it->cmp_len;
4277 it->method = next_element_from_buffer;
4280 else if (it->method == next_element_from_c_string)
4282 /* Current display element of IT is from a C string. */
4283 IT_BYTEPOS (*it) += it->len;
4284 IT_CHARPOS (*it) += 1;
4286 else if (it->method == next_element_from_display_vector)
4288 /* Current display element of IT is from a display table entry.
4289 Advance in the display table definition. Reset it to null if
4290 end reached, and continue with characters from buffers/
4291 strings. */
4292 ++it->current.dpvec_index;
4294 /* Restore face of the iterator to what they were before the
4295 display vector entry (these entries may contain faces). */
4296 it->face_id = it->saved_face_id;
4298 if (it->dpvec + it->current.dpvec_index == it->dpend)
4300 if (it->s)
4301 it->method = next_element_from_c_string;
4302 else if (STRINGP (it->string))
4303 it->method = next_element_from_string;
4304 else
4305 it->method = next_element_from_buffer;
4307 it->dpvec = NULL;
4308 it->current.dpvec_index = -1;
4310 /* Skip over characters which were displayed via IT->dpvec. */
4311 if (it->dpvec_char_len < 0)
4312 reseat_at_next_visible_line_start (it, 1);
4313 else if (it->dpvec_char_len > 0)
4315 it->len = it->dpvec_char_len;
4316 set_iterator_to_next (it, reseat_p);
4320 else if (it->method == next_element_from_string)
4322 /* Current display element is a character from a Lisp string. */
4323 xassert (it->s == NULL && STRINGP (it->string));
4324 IT_STRING_BYTEPOS (*it) += it->len;
4325 IT_STRING_CHARPOS (*it) += 1;
4327 consider_string_end:
4329 if (it->current.overlay_string_index >= 0)
4331 /* IT->string is an overlay string. Advance to the
4332 next, if there is one. */
4333 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4334 next_overlay_string (it);
4336 else
4338 /* IT->string is not an overlay string. If we reached
4339 its end, and there is something on IT->stack, proceed
4340 with what is on the stack. This can be either another
4341 string, this time an overlay string, or a buffer. */
4342 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4343 && it->sp > 0)
4345 pop_it (it);
4346 if (!STRINGP (it->string))
4347 it->method = next_element_from_buffer;
4351 else if (it->method == next_element_from_image
4352 || it->method == next_element_from_stretch)
4354 /* The position etc with which we have to proceed are on
4355 the stack. The position may be at the end of a string,
4356 if the `display' property takes up the whole string. */
4357 pop_it (it);
4358 it->image_id = 0;
4359 if (STRINGP (it->string))
4361 it->method = next_element_from_string;
4362 goto consider_string_end;
4364 else
4365 it->method = next_element_from_buffer;
4367 else
4368 /* There are no other methods defined, so this should be a bug. */
4369 abort ();
4371 xassert (it->method != next_element_from_string
4372 || (STRINGP (it->string)
4373 && IT_STRING_CHARPOS (*it) >= 0));
4377 /* Load IT's display element fields with information about the next
4378 display element which comes from a display table entry or from the
4379 result of translating a control character to one of the forms `^C'
4380 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4382 static int
4383 next_element_from_display_vector (it)
4384 struct it *it;
4386 /* Precondition. */
4387 xassert (it->dpvec && it->current.dpvec_index >= 0);
4389 /* Remember the current face id in case glyphs specify faces.
4390 IT's face is restored in set_iterator_to_next. */
4391 it->saved_face_id = it->face_id;
4393 if (INTEGERP (*it->dpvec)
4394 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4396 int lface_id;
4397 GLYPH g;
4399 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4400 it->c = FAST_GLYPH_CHAR (g);
4401 it->len = CHAR_BYTES (it->c);
4403 /* The entry may contain a face id to use. Such a face id is
4404 the id of a Lisp face, not a realized face. A face id of
4405 zero means no face is specified. */
4406 lface_id = FAST_GLYPH_FACE (g);
4407 if (lface_id)
4409 /* The function returns -1 if lface_id is invalid. */
4410 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4411 if (face_id >= 0)
4412 it->face_id = face_id;
4415 else
4416 /* Display table entry is invalid. Return a space. */
4417 it->c = ' ', it->len = 1;
4419 /* Don't change position and object of the iterator here. They are
4420 still the values of the character that had this display table
4421 entry or was translated, and that's what we want. */
4422 it->what = IT_CHARACTER;
4423 return 1;
4427 /* Load IT with the next display element from Lisp string IT->string.
4428 IT->current.string_pos is the current position within the string.
4429 If IT->current.overlay_string_index >= 0, the Lisp string is an
4430 overlay string. */
4432 static int
4433 next_element_from_string (it)
4434 struct it *it;
4436 struct text_pos position;
4438 xassert (STRINGP (it->string));
4439 xassert (IT_STRING_CHARPOS (*it) >= 0);
4440 position = it->current.string_pos;
4442 /* Time to check for invisible text? */
4443 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4444 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4446 handle_stop (it);
4448 /* Since a handler may have changed IT->method, we must
4449 recurse here. */
4450 return get_next_display_element (it);
4453 if (it->current.overlay_string_index >= 0)
4455 /* Get the next character from an overlay string. In overlay
4456 strings, There is no field width or padding with spaces to
4457 do. */
4458 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4460 it->what = IT_EOB;
4461 return 0;
4463 else if (STRING_MULTIBYTE (it->string))
4465 int remaining = (STRING_BYTES (XSTRING (it->string))
4466 - IT_STRING_BYTEPOS (*it));
4467 unsigned char *s = (XSTRING (it->string)->data
4468 + IT_STRING_BYTEPOS (*it));
4469 it->c = string_char_and_length (s, remaining, &it->len);
4471 else
4473 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4474 it->len = 1;
4477 else
4479 /* Get the next character from a Lisp string that is not an
4480 overlay string. Such strings come from the mode line, for
4481 example. We may have to pad with spaces, or truncate the
4482 string. See also next_element_from_c_string. */
4483 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4485 it->what = IT_EOB;
4486 return 0;
4488 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4490 /* Pad with spaces. */
4491 it->c = ' ', it->len = 1;
4492 CHARPOS (position) = BYTEPOS (position) = -1;
4494 else if (STRING_MULTIBYTE (it->string))
4496 int maxlen = (STRING_BYTES (XSTRING (it->string))
4497 - IT_STRING_BYTEPOS (*it));
4498 unsigned char *s = (XSTRING (it->string)->data
4499 + IT_STRING_BYTEPOS (*it));
4500 it->c = string_char_and_length (s, maxlen, &it->len);
4502 else
4504 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4505 it->len = 1;
4509 /* Record what we have and where it came from. Note that we store a
4510 buffer position in IT->position although it could arguably be a
4511 string position. */
4512 it->what = IT_CHARACTER;
4513 it->object = it->string;
4514 it->position = position;
4515 return 1;
4519 /* Load IT with next display element from C string IT->s.
4520 IT->string_nchars is the maximum number of characters to return
4521 from the string. IT->end_charpos may be greater than
4522 IT->string_nchars when this function is called, in which case we
4523 may have to return padding spaces. Value is zero if end of string
4524 reached, including padding spaces. */
4526 static int
4527 next_element_from_c_string (it)
4528 struct it *it;
4530 int success_p = 1;
4532 xassert (it->s);
4533 it->what = IT_CHARACTER;
4534 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4535 it->object = Qnil;
4537 /* IT's position can be greater IT->string_nchars in case a field
4538 width or precision has been specified when the iterator was
4539 initialized. */
4540 if (IT_CHARPOS (*it) >= it->end_charpos)
4542 /* End of the game. */
4543 it->what = IT_EOB;
4544 success_p = 0;
4546 else if (IT_CHARPOS (*it) >= it->string_nchars)
4548 /* Pad with spaces. */
4549 it->c = ' ', it->len = 1;
4550 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4552 else if (it->multibyte_p)
4554 /* Implementation note: The calls to strlen apparently aren't a
4555 performance problem because there is no noticeable performance
4556 difference between Emacs running in unibyte or multibyte mode. */
4557 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4558 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4559 maxlen, &it->len);
4561 else
4562 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4564 return success_p;
4568 /* Set up IT to return characters from an ellipsis, if appropriate.
4569 The definition of the ellipsis glyphs may come from a display table
4570 entry. This function Fills IT with the first glyph from the
4571 ellipsis if an ellipsis is to be displayed. */
4573 static int
4574 next_element_from_ellipsis (it)
4575 struct it *it;
4577 if (it->selective_display_ellipsis_p)
4579 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4581 /* Use the display table definition for `...'. Invalid glyphs
4582 will be handled by the method returning elements from dpvec. */
4583 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4584 it->dpvec_char_len = it->len;
4585 it->dpvec = v->contents;
4586 it->dpend = v->contents + v->size;
4587 it->current.dpvec_index = 0;
4588 it->method = next_element_from_display_vector;
4590 else
4592 /* Use default `...' which is stored in default_invis_vector. */
4593 it->dpvec_char_len = it->len;
4594 it->dpvec = default_invis_vector;
4595 it->dpend = default_invis_vector + 3;
4596 it->current.dpvec_index = 0;
4597 it->method = next_element_from_display_vector;
4600 else
4602 /* The face at the current position may be different from the
4603 face we find after the invisible text. Remember what it
4604 was in IT->saved_face_id, and signal that it's there by
4605 setting face_before_selective_p. */
4606 it->saved_face_id = it->face_id;
4607 it->method = next_element_from_buffer;
4608 reseat_at_next_visible_line_start (it, 1);
4609 it->face_before_selective_p = 1;
4612 return get_next_display_element (it);
4616 /* Deliver an image display element. The iterator IT is already
4617 filled with image information (done in handle_display_prop). Value
4618 is always 1. */
4621 static int
4622 next_element_from_image (it)
4623 struct it *it;
4625 it->what = IT_IMAGE;
4626 return 1;
4630 /* Fill iterator IT with next display element from a stretch glyph
4631 property. IT->object is the value of the text property. Value is
4632 always 1. */
4634 static int
4635 next_element_from_stretch (it)
4636 struct it *it;
4638 it->what = IT_STRETCH;
4639 return 1;
4643 /* Load IT with the next display element from current_buffer. Value
4644 is zero if end of buffer reached. IT->stop_charpos is the next
4645 position at which to stop and check for text properties or buffer
4646 end. */
4648 static int
4649 next_element_from_buffer (it)
4650 struct it *it;
4652 int success_p = 1;
4654 /* Check this assumption, otherwise, we would never enter the
4655 if-statement, below. */
4656 xassert (IT_CHARPOS (*it) >= BEGV
4657 && IT_CHARPOS (*it) <= it->stop_charpos);
4659 if (IT_CHARPOS (*it) >= it->stop_charpos)
4661 if (IT_CHARPOS (*it) >= it->end_charpos)
4663 int overlay_strings_follow_p;
4665 /* End of the game, except when overlay strings follow that
4666 haven't been returned yet. */
4667 if (it->overlay_strings_at_end_processed_p)
4668 overlay_strings_follow_p = 0;
4669 else
4671 it->overlay_strings_at_end_processed_p = 1;
4672 overlay_strings_follow_p = get_overlay_strings (it);
4675 if (overlay_strings_follow_p)
4676 success_p = get_next_display_element (it);
4677 else
4679 it->what = IT_EOB;
4680 it->position = it->current.pos;
4681 success_p = 0;
4684 else
4686 handle_stop (it);
4687 return get_next_display_element (it);
4690 else
4692 /* No face changes, overlays etc. in sight, so just return a
4693 character from current_buffer. */
4694 unsigned char *p;
4696 /* Maybe run the redisplay end trigger hook. Performance note:
4697 This doesn't seem to cost measurable time. */
4698 if (it->redisplay_end_trigger_charpos
4699 && it->glyph_row
4700 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4701 run_redisplay_end_trigger_hook (it);
4703 /* Get the next character, maybe multibyte. */
4704 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4705 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4707 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4708 - IT_BYTEPOS (*it));
4709 it->c = string_char_and_length (p, maxlen, &it->len);
4711 else
4712 it->c = *p, it->len = 1;
4714 /* Record what we have and where it came from. */
4715 it->what = IT_CHARACTER;;
4716 it->object = it->w->buffer;
4717 it->position = it->current.pos;
4719 /* Normally we return the character found above, except when we
4720 really want to return an ellipsis for selective display. */
4721 if (it->selective)
4723 if (it->c == '\n')
4725 /* A value of selective > 0 means hide lines indented more
4726 than that number of columns. */
4727 if (it->selective > 0
4728 && IT_CHARPOS (*it) + 1 < ZV
4729 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4730 IT_BYTEPOS (*it) + 1,
4731 it->selective))
4733 success_p = next_element_from_ellipsis (it);
4734 it->dpvec_char_len = -1;
4737 else if (it->c == '\r' && it->selective == -1)
4739 /* A value of selective == -1 means that everything from the
4740 CR to the end of the line is invisible, with maybe an
4741 ellipsis displayed for it. */
4742 success_p = next_element_from_ellipsis (it);
4743 it->dpvec_char_len = -1;
4748 /* Value is zero if end of buffer reached. */
4749 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4750 return success_p;
4754 /* Run the redisplay end trigger hook for IT. */
4756 static void
4757 run_redisplay_end_trigger_hook (it)
4758 struct it *it;
4760 Lisp_Object args[3];
4762 /* IT->glyph_row should be non-null, i.e. we should be actually
4763 displaying something, or otherwise we should not run the hook. */
4764 xassert (it->glyph_row);
4766 /* Set up hook arguments. */
4767 args[0] = Qredisplay_end_trigger_functions;
4768 args[1] = it->window;
4769 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4770 it->redisplay_end_trigger_charpos = 0;
4772 /* Since we are *trying* to run these functions, don't try to run
4773 them again, even if they get an error. */
4774 it->w->redisplay_end_trigger = Qnil;
4775 Frun_hook_with_args (3, args);
4777 /* Notice if it changed the face of the character we are on. */
4778 handle_face_prop (it);
4782 /* Deliver a composition display element. The iterator IT is already
4783 filled with composition information (done in
4784 handle_composition_prop). Value is always 1. */
4786 static int
4787 next_element_from_composition (it)
4788 struct it *it;
4790 it->what = IT_COMPOSITION;
4791 it->position = (STRINGP (it->string)
4792 ? it->current.string_pos
4793 : it->current.pos);
4794 return 1;
4799 /***********************************************************************
4800 Moving an iterator without producing glyphs
4801 ***********************************************************************/
4803 /* Move iterator IT to a specified buffer or X position within one
4804 line on the display without producing glyphs.
4806 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4807 whichever is reached first.
4809 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4811 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4812 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4813 TO_X includes the amount by which a window is horizontally
4814 scrolled.
4816 Value is
4818 MOVE_POS_MATCH_OR_ZV
4819 - when TO_POS or ZV was reached.
4821 MOVE_X_REACHED
4822 -when TO_X was reached before TO_POS or ZV were reached.
4824 MOVE_LINE_CONTINUED
4825 - when we reached the end of the display area and the line must
4826 be continued.
4828 MOVE_LINE_TRUNCATED
4829 - when we reached the end of the display area and the line is
4830 truncated.
4832 MOVE_NEWLINE_OR_CR
4833 - when we stopped at a line end, i.e. a newline or a CR and selective
4834 display is on. */
4836 static enum move_it_result
4837 move_it_in_display_line_to (it, to_charpos, to_x, op)
4838 struct it *it;
4839 int to_charpos, to_x, op;
4841 enum move_it_result result = MOVE_UNDEFINED;
4842 struct glyph_row *saved_glyph_row;
4844 /* Don't produce glyphs in produce_glyphs. */
4845 saved_glyph_row = it->glyph_row;
4846 it->glyph_row = NULL;
4848 while (1)
4850 int x, i, ascent = 0, descent = 0;
4852 /* Stop when ZV or TO_CHARPOS reached. */
4853 if (!get_next_display_element (it)
4854 || ((op & MOVE_TO_POS) != 0
4855 && BUFFERP (it->object)
4856 && IT_CHARPOS (*it) >= to_charpos))
4858 result = MOVE_POS_MATCH_OR_ZV;
4859 break;
4862 /* The call to produce_glyphs will get the metrics of the
4863 display element IT is loaded with. We record in x the
4864 x-position before this display element in case it does not
4865 fit on the line. */
4866 x = it->current_x;
4868 /* Remember the line height so far in case the next element doesn't
4869 fit on the line. */
4870 if (!it->truncate_lines_p)
4872 ascent = it->max_ascent;
4873 descent = it->max_descent;
4876 PRODUCE_GLYPHS (it);
4878 if (it->area != TEXT_AREA)
4880 set_iterator_to_next (it, 1);
4881 continue;
4884 /* The number of glyphs we get back in IT->nglyphs will normally
4885 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4886 character on a terminal frame, or (iii) a line end. For the
4887 second case, IT->nglyphs - 1 padding glyphs will be present
4888 (on X frames, there is only one glyph produced for a
4889 composite character.
4891 The behavior implemented below means, for continuation lines,
4892 that as many spaces of a TAB as fit on the current line are
4893 displayed there. For terminal frames, as many glyphs of a
4894 multi-glyph character are displayed in the current line, too.
4895 This is what the old redisplay code did, and we keep it that
4896 way. Under X, the whole shape of a complex character must
4897 fit on the line or it will be completely displayed in the
4898 next line.
4900 Note that both for tabs and padding glyphs, all glyphs have
4901 the same width. */
4902 if (it->nglyphs)
4904 /* More than one glyph or glyph doesn't fit on line. All
4905 glyphs have the same width. */
4906 int single_glyph_width = it->pixel_width / it->nglyphs;
4907 int new_x;
4909 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4911 new_x = x + single_glyph_width;
4913 /* We want to leave anything reaching TO_X to the caller. */
4914 if ((op & MOVE_TO_X) && new_x > to_x)
4916 it->current_x = x;
4917 result = MOVE_X_REACHED;
4918 break;
4920 else if (/* Lines are continued. */
4921 !it->truncate_lines_p
4922 && (/* And glyph doesn't fit on the line. */
4923 new_x > it->last_visible_x
4924 /* Or it fits exactly and we're on a window
4925 system frame. */
4926 || (new_x == it->last_visible_x
4927 && FRAME_WINDOW_P (it->f))))
4929 if (/* IT->hpos == 0 means the very first glyph
4930 doesn't fit on the line, e.g. a wide image. */
4931 it->hpos == 0
4932 || (new_x == it->last_visible_x
4933 && FRAME_WINDOW_P (it->f)))
4935 ++it->hpos;
4936 it->current_x = new_x;
4937 if (i == it->nglyphs - 1)
4938 set_iterator_to_next (it, 1);
4940 else
4942 it->current_x = x;
4943 it->max_ascent = ascent;
4944 it->max_descent = descent;
4947 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4948 IT_CHARPOS (*it)));
4949 result = MOVE_LINE_CONTINUED;
4950 break;
4952 else if (new_x > it->first_visible_x)
4954 /* Glyph is visible. Increment number of glyphs that
4955 would be displayed. */
4956 ++it->hpos;
4958 else
4960 /* Glyph is completely off the left margin of the display
4961 area. Nothing to do. */
4965 if (result != MOVE_UNDEFINED)
4966 break;
4968 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4970 /* Stop when TO_X specified and reached. This check is
4971 necessary here because of lines consisting of a line end,
4972 only. The line end will not produce any glyphs and we
4973 would never get MOVE_X_REACHED. */
4974 xassert (it->nglyphs == 0);
4975 result = MOVE_X_REACHED;
4976 break;
4979 /* Is this a line end? If yes, we're done. */
4980 if (ITERATOR_AT_END_OF_LINE_P (it))
4982 result = MOVE_NEWLINE_OR_CR;
4983 break;
4986 /* The current display element has been consumed. Advance
4987 to the next. */
4988 set_iterator_to_next (it, 1);
4990 /* Stop if lines are truncated and IT's current x-position is
4991 past the right edge of the window now. */
4992 if (it->truncate_lines_p
4993 && it->current_x >= it->last_visible_x)
4995 result = MOVE_LINE_TRUNCATED;
4996 break;
5000 /* Restore the iterator settings altered at the beginning of this
5001 function. */
5002 it->glyph_row = saved_glyph_row;
5003 return result;
5007 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5008 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5009 the description of enum move_operation_enum.
5011 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5012 screen line, this function will set IT to the next position >
5013 TO_CHARPOS. */
5015 void
5016 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5017 struct it *it;
5018 int to_charpos, to_x, to_y, to_vpos;
5019 int op;
5021 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5022 int line_height;
5023 int reached = 0;
5025 for (;;)
5027 if (op & MOVE_TO_VPOS)
5029 /* If no TO_CHARPOS and no TO_X specified, stop at the
5030 start of the line TO_VPOS. */
5031 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5033 if (it->vpos == to_vpos)
5035 reached = 1;
5036 break;
5038 else
5039 skip = move_it_in_display_line_to (it, -1, -1, 0);
5041 else
5043 /* TO_VPOS >= 0 means stop at TO_X in the line at
5044 TO_VPOS, or at TO_POS, whichever comes first. */
5045 if (it->vpos == to_vpos)
5047 reached = 2;
5048 break;
5051 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5053 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5055 reached = 3;
5056 break;
5058 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5060 /* We have reached TO_X but not in the line we want. */
5061 skip = move_it_in_display_line_to (it, to_charpos,
5062 -1, MOVE_TO_POS);
5063 if (skip == MOVE_POS_MATCH_OR_ZV)
5065 reached = 4;
5066 break;
5071 else if (op & MOVE_TO_Y)
5073 struct it it_backup;
5075 /* TO_Y specified means stop at TO_X in the line containing
5076 TO_Y---or at TO_CHARPOS if this is reached first. The
5077 problem is that we can't really tell whether the line
5078 contains TO_Y before we have completely scanned it, and
5079 this may skip past TO_X. What we do is to first scan to
5080 TO_X.
5082 If TO_X is not specified, use a TO_X of zero. The reason
5083 is to make the outcome of this function more predictable.
5084 If we didn't use TO_X == 0, we would stop at the end of
5085 the line which is probably not what a caller would expect
5086 to happen. */
5087 skip = move_it_in_display_line_to (it, to_charpos,
5088 ((op & MOVE_TO_X)
5089 ? to_x : 0),
5090 (MOVE_TO_X
5091 | (op & MOVE_TO_POS)));
5093 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5094 if (skip == MOVE_POS_MATCH_OR_ZV)
5096 reached = 5;
5097 break;
5100 /* If TO_X was reached, we would like to know whether TO_Y
5101 is in the line. This can only be said if we know the
5102 total line height which requires us to scan the rest of
5103 the line. */
5104 if (skip == MOVE_X_REACHED)
5106 it_backup = *it;
5107 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5108 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5109 op & MOVE_TO_POS);
5110 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5113 /* Now, decide whether TO_Y is in this line. */
5114 line_height = it->max_ascent + it->max_descent;
5115 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5117 if (to_y >= it->current_y
5118 && to_y < it->current_y + line_height)
5120 if (skip == MOVE_X_REACHED)
5121 /* If TO_Y is in this line and TO_X was reached above,
5122 we scanned too far. We have to restore IT's settings
5123 to the ones before skipping. */
5124 *it = it_backup;
5125 reached = 6;
5127 else if (skip == MOVE_X_REACHED)
5129 skip = skip2;
5130 if (skip == MOVE_POS_MATCH_OR_ZV)
5131 reached = 7;
5134 if (reached)
5135 break;
5137 else
5138 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5140 switch (skip)
5142 case MOVE_POS_MATCH_OR_ZV:
5143 reached = 8;
5144 goto out;
5146 case MOVE_NEWLINE_OR_CR:
5147 set_iterator_to_next (it, 1);
5148 it->continuation_lines_width = 0;
5149 break;
5151 case MOVE_LINE_TRUNCATED:
5152 it->continuation_lines_width = 0;
5153 reseat_at_next_visible_line_start (it, 0);
5154 if ((op & MOVE_TO_POS) != 0
5155 && IT_CHARPOS (*it) > to_charpos)
5157 reached = 9;
5158 goto out;
5160 break;
5162 case MOVE_LINE_CONTINUED:
5163 it->continuation_lines_width += it->current_x;
5164 break;
5166 default:
5167 abort ();
5170 /* Reset/increment for the next run. */
5171 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5172 it->current_x = it->hpos = 0;
5173 it->current_y += it->max_ascent + it->max_descent;
5174 ++it->vpos;
5175 last_height = it->max_ascent + it->max_descent;
5176 last_max_ascent = it->max_ascent;
5177 it->max_ascent = it->max_descent = 0;
5180 out:
5182 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5186 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5188 If DY > 0, move IT backward at least that many pixels. DY = 0
5189 means move IT backward to the preceding line start or BEGV. This
5190 function may move over more than DY pixels if IT->current_y - DY
5191 ends up in the middle of a line; in this case IT->current_y will be
5192 set to the top of the line moved to. */
5194 void
5195 move_it_vertically_backward (it, dy)
5196 struct it *it;
5197 int dy;
5199 int nlines, h, line_height;
5200 struct it it2;
5201 int start_pos = IT_CHARPOS (*it);
5203 xassert (dy >= 0);
5205 /* Estimate how many newlines we must move back. */
5206 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5208 /* Set the iterator's position that many lines back. */
5209 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5210 back_to_previous_visible_line_start (it);
5212 /* Reseat the iterator here. When moving backward, we don't want
5213 reseat to skip forward over invisible text, set up the iterator
5214 to deliver from overlay strings at the new position etc. So,
5215 use reseat_1 here. */
5216 reseat_1 (it, it->current.pos, 1);
5218 /* We are now surely at a line start. */
5219 it->current_x = it->hpos = 0;
5221 /* Move forward and see what y-distance we moved. First move to the
5222 start of the next line so that we get its height. We need this
5223 height to be able to tell whether we reached the specified
5224 y-distance. */
5225 it2 = *it;
5226 it2.max_ascent = it2.max_descent = 0;
5227 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5228 MOVE_TO_POS | MOVE_TO_VPOS);
5229 xassert (IT_CHARPOS (*it) >= BEGV);
5230 line_height = it2.max_ascent + it2.max_descent;
5232 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5233 xassert (IT_CHARPOS (*it) >= BEGV);
5234 h = it2.current_y - it->current_y;
5235 nlines = it2.vpos - it->vpos;
5237 /* Correct IT's y and vpos position. */
5238 it->vpos -= nlines;
5239 it->current_y -= h;
5241 if (dy == 0)
5243 /* DY == 0 means move to the start of the screen line. The
5244 value of nlines is > 0 if continuation lines were involved. */
5245 if (nlines > 0)
5246 move_it_by_lines (it, nlines, 1);
5247 xassert (IT_CHARPOS (*it) <= start_pos);
5249 else if (nlines)
5251 /* The y-position we try to reach. Note that h has been
5252 subtracted in front of the if-statement. */
5253 int target_y = it->current_y + h - dy;
5255 /* If we did not reach target_y, try to move further backward if
5256 we can. If we moved too far backward, try to move forward. */
5257 if (target_y < it->current_y
5258 && IT_CHARPOS (*it) > BEGV)
5260 move_it_vertically (it, target_y - it->current_y);
5261 xassert (IT_CHARPOS (*it) >= BEGV);
5263 else if (target_y >= it->current_y + line_height
5264 && IT_CHARPOS (*it) < ZV)
5266 move_it_vertically (it, target_y - (it->current_y + line_height));
5267 xassert (IT_CHARPOS (*it) >= BEGV);
5273 /* Move IT by a specified amount of pixel lines DY. DY negative means
5274 move backwards. DY = 0 means move to start of screen line. At the
5275 end, IT will be on the start of a screen line. */
5277 void
5278 move_it_vertically (it, dy)
5279 struct it *it;
5280 int dy;
5282 if (dy <= 0)
5283 move_it_vertically_backward (it, -dy);
5284 else if (dy > 0)
5286 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5287 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5288 MOVE_TO_POS | MOVE_TO_Y);
5289 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5291 /* If buffer ends in ZV without a newline, move to the start of
5292 the line to satisfy the post-condition. */
5293 if (IT_CHARPOS (*it) == ZV
5294 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5295 move_it_by_lines (it, 0, 0);
5300 /* Move iterator IT past the end of the text line it is in. */
5302 void
5303 move_it_past_eol (it)
5304 struct it *it;
5306 enum move_it_result rc;
5308 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5309 if (rc == MOVE_NEWLINE_OR_CR)
5310 set_iterator_to_next (it, 0);
5314 #if 0 /* Currently not used. */
5316 /* Return non-zero if some text between buffer positions START_CHARPOS
5317 and END_CHARPOS is invisible. IT->window is the window for text
5318 property lookup. */
5320 static int
5321 invisible_text_between_p (it, start_charpos, end_charpos)
5322 struct it *it;
5323 int start_charpos, end_charpos;
5325 Lisp_Object prop, limit;
5326 int invisible_found_p;
5328 xassert (it != NULL && start_charpos <= end_charpos);
5330 /* Is text at START invisible? */
5331 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5332 it->window);
5333 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5334 invisible_found_p = 1;
5335 else
5337 limit = Fnext_single_char_property_change (make_number (start_charpos),
5338 Qinvisible, Qnil,
5339 make_number (end_charpos));
5340 invisible_found_p = XFASTINT (limit) < end_charpos;
5343 return invisible_found_p;
5346 #endif /* 0 */
5349 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5350 negative means move up. DVPOS == 0 means move to the start of the
5351 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5352 NEED_Y_P is zero, IT->current_y will be left unchanged.
5354 Further optimization ideas: If we would know that IT->f doesn't use
5355 a face with proportional font, we could be faster for
5356 truncate-lines nil. */
5358 void
5359 move_it_by_lines (it, dvpos, need_y_p)
5360 struct it *it;
5361 int dvpos, need_y_p;
5363 struct position pos;
5365 if (!FRAME_WINDOW_P (it->f))
5367 struct text_pos textpos;
5369 /* We can use vmotion on frames without proportional fonts. */
5370 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5371 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5372 reseat (it, textpos, 1);
5373 it->vpos += pos.vpos;
5374 it->current_y += pos.vpos;
5376 else if (dvpos == 0)
5378 /* DVPOS == 0 means move to the start of the screen line. */
5379 move_it_vertically_backward (it, 0);
5380 xassert (it->current_x == 0 && it->hpos == 0);
5382 else if (dvpos > 0)
5383 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5384 else
5386 struct it it2;
5387 int start_charpos, i;
5389 /* Go back -DVPOS visible lines and reseat the iterator there. */
5390 start_charpos = IT_CHARPOS (*it);
5391 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5392 back_to_previous_visible_line_start (it);
5393 reseat (it, it->current.pos, 1);
5394 it->current_x = it->hpos = 0;
5396 /* Above call may have moved too far if continuation lines
5397 are involved. Scan forward and see if it did. */
5398 it2 = *it;
5399 it2.vpos = it2.current_y = 0;
5400 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5401 it->vpos -= it2.vpos;
5402 it->current_y -= it2.current_y;
5403 it->current_x = it->hpos = 0;
5405 /* If we moved too far, move IT some lines forward. */
5406 if (it2.vpos > -dvpos)
5408 int delta = it2.vpos + dvpos;
5409 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5416 /***********************************************************************
5417 Messages
5418 ***********************************************************************/
5421 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5422 to *Messages*. */
5424 void
5425 add_to_log (format, arg1, arg2)
5426 char *format;
5427 Lisp_Object arg1, arg2;
5429 Lisp_Object args[3];
5430 Lisp_Object msg, fmt;
5431 char *buffer;
5432 int len;
5433 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5435 fmt = msg = Qnil;
5436 GCPRO4 (fmt, msg, arg1, arg2);
5438 args[0] = fmt = build_string (format);
5439 args[1] = arg1;
5440 args[2] = arg2;
5441 msg = Fformat (3, args);
5443 len = STRING_BYTES (XSTRING (msg)) + 1;
5444 buffer = (char *) alloca (len);
5445 strcpy (buffer, XSTRING (msg)->data);
5447 message_dolog (buffer, len - 1, 1, 0);
5448 UNGCPRO;
5452 /* Output a newline in the *Messages* buffer if "needs" one. */
5454 void
5455 message_log_maybe_newline ()
5457 if (message_log_need_newline)
5458 message_dolog ("", 0, 1, 0);
5462 /* Add a string M of length NBYTES to the message log, optionally
5463 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5464 nonzero, means interpret the contents of M as multibyte. This
5465 function calls low-level routines in order to bypass text property
5466 hooks, etc. which might not be safe to run. */
5468 void
5469 message_dolog (m, nbytes, nlflag, multibyte)
5470 char *m;
5471 int nbytes, nlflag, multibyte;
5473 if (!NILP (Vmessage_log_max))
5475 struct buffer *oldbuf;
5476 Lisp_Object oldpoint, oldbegv, oldzv;
5477 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5478 int point_at_end = 0;
5479 int zv_at_end = 0;
5480 Lisp_Object old_deactivate_mark, tem;
5481 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5483 old_deactivate_mark = Vdeactivate_mark;
5484 oldbuf = current_buffer;
5485 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5486 current_buffer->undo_list = Qt;
5488 oldpoint = Fpoint_marker ();
5489 oldbegv = Fpoint_min_marker ();
5490 oldzv = Fpoint_max_marker ();
5491 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5493 if (PT == Z)
5494 point_at_end = 1;
5495 if (ZV == Z)
5496 zv_at_end = 1;
5498 BEGV = BEG;
5499 BEGV_BYTE = BEG_BYTE;
5500 ZV = Z;
5501 ZV_BYTE = Z_BYTE;
5502 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5504 /* Insert the string--maybe converting multibyte to single byte
5505 or vice versa, so that all the text fits the buffer. */
5506 if (multibyte
5507 && NILP (current_buffer->enable_multibyte_characters))
5509 int i, c, char_bytes;
5510 unsigned char work[1];
5512 /* Convert a multibyte string to single-byte
5513 for the *Message* buffer. */
5514 for (i = 0; i < nbytes; i += nbytes)
5516 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5517 work[0] = (SINGLE_BYTE_CHAR_P (c)
5519 : multibyte_char_to_unibyte (c, Qnil));
5520 insert_1_both (work, 1, 1, 1, 0, 0);
5523 else if (! multibyte
5524 && ! NILP (current_buffer->enable_multibyte_characters))
5526 int i, c, char_bytes;
5527 unsigned char *msg = (unsigned char *) m;
5528 unsigned char str[MAX_MULTIBYTE_LENGTH];
5529 /* Convert a single-byte string to multibyte
5530 for the *Message* buffer. */
5531 for (i = 0; i < nbytes; i++)
5533 c = unibyte_char_to_multibyte (msg[i]);
5534 char_bytes = CHAR_STRING (c, str);
5535 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5538 else if (nbytes)
5539 insert_1 (m, nbytes, 1, 0, 0);
5541 if (nlflag)
5543 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5544 insert_1 ("\n", 1, 1, 0, 0);
5546 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5547 this_bol = PT;
5548 this_bol_byte = PT_BYTE;
5550 if (this_bol > BEG)
5552 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5553 prev_bol = PT;
5554 prev_bol_byte = PT_BYTE;
5556 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5557 this_bol, this_bol_byte);
5558 if (dup)
5560 del_range_both (prev_bol, prev_bol_byte,
5561 this_bol, this_bol_byte, 0);
5562 if (dup > 1)
5564 char dupstr[40];
5565 int duplen;
5567 /* If you change this format, don't forget to also
5568 change message_log_check_duplicate. */
5569 sprintf (dupstr, " [%d times]", dup);
5570 duplen = strlen (dupstr);
5571 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5572 insert_1 (dupstr, duplen, 1, 0, 1);
5577 if (NATNUMP (Vmessage_log_max))
5579 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5580 -XFASTINT (Vmessage_log_max) - 1, 0);
5581 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5584 BEGV = XMARKER (oldbegv)->charpos;
5585 BEGV_BYTE = marker_byte_position (oldbegv);
5587 if (zv_at_end)
5589 ZV = Z;
5590 ZV_BYTE = Z_BYTE;
5592 else
5594 ZV = XMARKER (oldzv)->charpos;
5595 ZV_BYTE = marker_byte_position (oldzv);
5598 if (point_at_end)
5599 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5600 else
5601 /* We can't do Fgoto_char (oldpoint) because it will run some
5602 Lisp code. */
5603 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5604 XMARKER (oldpoint)->bytepos);
5606 UNGCPRO;
5607 free_marker (oldpoint);
5608 free_marker (oldbegv);
5609 free_marker (oldzv);
5611 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5612 set_buffer_internal (oldbuf);
5613 if (NILP (tem))
5614 windows_or_buffers_changed = old_windows_or_buffers_changed;
5615 message_log_need_newline = !nlflag;
5616 Vdeactivate_mark = old_deactivate_mark;
5621 /* We are at the end of the buffer after just having inserted a newline.
5622 (Note: We depend on the fact we won't be crossing the gap.)
5623 Check to see if the most recent message looks a lot like the previous one.
5624 Return 0 if different, 1 if the new one should just replace it, or a
5625 value N > 1 if we should also append " [N times]". */
5627 static int
5628 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5629 int prev_bol, this_bol;
5630 int prev_bol_byte, this_bol_byte;
5632 int i;
5633 int len = Z_BYTE - 1 - this_bol_byte;
5634 int seen_dots = 0;
5635 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5636 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5638 for (i = 0; i < len; i++)
5640 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5641 seen_dots = 1;
5642 if (p1[i] != p2[i])
5643 return seen_dots;
5645 p1 += len;
5646 if (*p1 == '\n')
5647 return 2;
5648 if (*p1++ == ' ' && *p1++ == '[')
5650 int n = 0;
5651 while (*p1 >= '0' && *p1 <= '9')
5652 n = n * 10 + *p1++ - '0';
5653 if (strncmp (p1, " times]\n", 8) == 0)
5654 return n+1;
5656 return 0;
5660 /* Display an echo area message M with a specified length of NBYTES
5661 bytes. The string may include null characters. If M is 0, clear
5662 out any existing message, and let the mini-buffer text show
5663 through.
5665 The buffer M must continue to exist until after the echo area gets
5666 cleared or some other message gets displayed there. This means do
5667 not pass text that is stored in a Lisp string; do not pass text in
5668 a buffer that was alloca'd. */
5670 void
5671 message2 (m, nbytes, multibyte)
5672 char *m;
5673 int nbytes;
5674 int multibyte;
5676 /* First flush out any partial line written with print. */
5677 message_log_maybe_newline ();
5678 if (m)
5679 message_dolog (m, nbytes, 1, multibyte);
5680 message2_nolog (m, nbytes, multibyte);
5684 /* The non-logging counterpart of message2. */
5686 void
5687 message2_nolog (m, nbytes, multibyte)
5688 char *m;
5689 int nbytes;
5691 struct frame *sf = SELECTED_FRAME ();
5692 message_enable_multibyte = multibyte;
5694 if (noninteractive)
5696 if (noninteractive_need_newline)
5697 putc ('\n', stderr);
5698 noninteractive_need_newline = 0;
5699 if (m)
5700 fwrite (m, nbytes, 1, stderr);
5701 if (cursor_in_echo_area == 0)
5702 fprintf (stderr, "\n");
5703 fflush (stderr);
5705 /* A null message buffer means that the frame hasn't really been
5706 initialized yet. Error messages get reported properly by
5707 cmd_error, so this must be just an informative message; toss it. */
5708 else if (INTERACTIVE
5709 && sf->glyphs_initialized_p
5710 && FRAME_MESSAGE_BUF (sf))
5712 Lisp_Object mini_window;
5713 struct frame *f;
5715 /* Get the frame containing the mini-buffer
5716 that the selected frame is using. */
5717 mini_window = FRAME_MINIBUF_WINDOW (sf);
5718 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5720 FRAME_SAMPLE_VISIBILITY (f);
5721 if (FRAME_VISIBLE_P (sf)
5722 && ! FRAME_VISIBLE_P (f))
5723 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5725 if (m)
5727 set_message (m, Qnil, nbytes, multibyte);
5728 if (minibuffer_auto_raise)
5729 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5731 else
5732 clear_message (1, 1);
5734 do_pending_window_change (0);
5735 echo_area_display (1);
5736 do_pending_window_change (0);
5737 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5738 (*frame_up_to_date_hook) (f);
5743 /* Display an echo area message M with a specified length of NBYTES
5744 bytes. The string may include null characters. If M is not a
5745 string, clear out any existing message, and let the mini-buffer
5746 text show through. */
5748 void
5749 message3 (m, nbytes, multibyte)
5750 Lisp_Object m;
5751 int nbytes;
5752 int multibyte;
5754 struct gcpro gcpro1;
5756 GCPRO1 (m);
5758 /* First flush out any partial line written with print. */
5759 message_log_maybe_newline ();
5760 if (STRINGP (m))
5761 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5762 message3_nolog (m, nbytes, multibyte);
5764 UNGCPRO;
5768 /* The non-logging version of message3. */
5770 void
5771 message3_nolog (m, nbytes, multibyte)
5772 Lisp_Object m;
5773 int nbytes, multibyte;
5775 struct frame *sf = SELECTED_FRAME ();
5776 message_enable_multibyte = multibyte;
5778 if (noninteractive)
5780 if (noninteractive_need_newline)
5781 putc ('\n', stderr);
5782 noninteractive_need_newline = 0;
5783 if (STRINGP (m))
5784 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5785 if (cursor_in_echo_area == 0)
5786 fprintf (stderr, "\n");
5787 fflush (stderr);
5789 /* A null message buffer means that the frame hasn't really been
5790 initialized yet. Error messages get reported properly by
5791 cmd_error, so this must be just an informative message; toss it. */
5792 else if (INTERACTIVE
5793 && sf->glyphs_initialized_p
5794 && FRAME_MESSAGE_BUF (sf))
5796 Lisp_Object mini_window;
5797 Lisp_Object frame;
5798 struct frame *f;
5800 /* Get the frame containing the mini-buffer
5801 that the selected frame is using. */
5802 mini_window = FRAME_MINIBUF_WINDOW (sf);
5803 frame = XWINDOW (mini_window)->frame;
5804 f = XFRAME (frame);
5806 FRAME_SAMPLE_VISIBILITY (f);
5807 if (FRAME_VISIBLE_P (sf)
5808 && !FRAME_VISIBLE_P (f))
5809 Fmake_frame_visible (frame);
5811 if (STRINGP (m) && XSTRING (m)->size)
5813 set_message (NULL, m, nbytes, multibyte);
5814 if (minibuffer_auto_raise)
5815 Fraise_frame (frame);
5817 else
5818 clear_message (1, 1);
5820 do_pending_window_change (0);
5821 echo_area_display (1);
5822 do_pending_window_change (0);
5823 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5824 (*frame_up_to_date_hook) (f);
5829 /* Display a null-terminated echo area message M. If M is 0, clear
5830 out any existing message, and let the mini-buffer text show through.
5832 The buffer M must continue to exist until after the echo area gets
5833 cleared or some other message gets displayed there. Do not pass
5834 text that is stored in a Lisp string. Do not pass text in a buffer
5835 that was alloca'd. */
5837 void
5838 message1 (m)
5839 char *m;
5841 message2 (m, (m ? strlen (m) : 0), 0);
5845 /* The non-logging counterpart of message1. */
5847 void
5848 message1_nolog (m)
5849 char *m;
5851 message2_nolog (m, (m ? strlen (m) : 0), 0);
5854 /* Display a message M which contains a single %s
5855 which gets replaced with STRING. */
5857 void
5858 message_with_string (m, string, log)
5859 char *m;
5860 Lisp_Object string;
5861 int log;
5863 if (noninteractive)
5865 if (m)
5867 if (noninteractive_need_newline)
5868 putc ('\n', stderr);
5869 noninteractive_need_newline = 0;
5870 fprintf (stderr, m, XSTRING (string)->data);
5871 if (cursor_in_echo_area == 0)
5872 fprintf (stderr, "\n");
5873 fflush (stderr);
5876 else if (INTERACTIVE)
5878 /* The frame whose minibuffer we're going to display the message on.
5879 It may be larger than the selected frame, so we need
5880 to use its buffer, not the selected frame's buffer. */
5881 Lisp_Object mini_window;
5882 struct frame *f, *sf = SELECTED_FRAME ();
5884 /* Get the frame containing the minibuffer
5885 that the selected frame is using. */
5886 mini_window = FRAME_MINIBUF_WINDOW (sf);
5887 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5889 /* A null message buffer means that the frame hasn't really been
5890 initialized yet. Error messages get reported properly by
5891 cmd_error, so this must be just an informative message; toss it. */
5892 if (FRAME_MESSAGE_BUF (f))
5894 int len;
5895 char *a[1];
5896 a[0] = (char *) XSTRING (string)->data;
5898 len = doprnt (FRAME_MESSAGE_BUF (f),
5899 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5901 if (log)
5902 message2 (FRAME_MESSAGE_BUF (f), len,
5903 STRING_MULTIBYTE (string));
5904 else
5905 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5906 STRING_MULTIBYTE (string));
5908 /* Print should start at the beginning of the message
5909 buffer next time. */
5910 message_buf_print = 0;
5916 /* Dump an informative message to the minibuf. If M is 0, clear out
5917 any existing message, and let the mini-buffer text show through. */
5919 /* VARARGS 1 */
5920 void
5921 message (m, a1, a2, a3)
5922 char *m;
5923 EMACS_INT a1, a2, a3;
5925 if (noninteractive)
5927 if (m)
5929 if (noninteractive_need_newline)
5930 putc ('\n', stderr);
5931 noninteractive_need_newline = 0;
5932 fprintf (stderr, m, a1, a2, a3);
5933 if (cursor_in_echo_area == 0)
5934 fprintf (stderr, "\n");
5935 fflush (stderr);
5938 else if (INTERACTIVE)
5940 /* The frame whose mini-buffer we're going to display the message
5941 on. It may be larger than the selected frame, so we need to
5942 use its buffer, not the selected frame's buffer. */
5943 Lisp_Object mini_window;
5944 struct frame *f, *sf = SELECTED_FRAME ();
5946 /* Get the frame containing the mini-buffer
5947 that the selected frame is using. */
5948 mini_window = FRAME_MINIBUF_WINDOW (sf);
5949 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5951 /* A null message buffer means that the frame hasn't really been
5952 initialized yet. Error messages get reported properly by
5953 cmd_error, so this must be just an informative message; toss
5954 it. */
5955 if (FRAME_MESSAGE_BUF (f))
5957 if (m)
5959 int len;
5960 #ifdef NO_ARG_ARRAY
5961 char *a[3];
5962 a[0] = (char *) a1;
5963 a[1] = (char *) a2;
5964 a[2] = (char *) a3;
5966 len = doprnt (FRAME_MESSAGE_BUF (f),
5967 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5968 #else
5969 len = doprnt (FRAME_MESSAGE_BUF (f),
5970 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5971 (char **) &a1);
5972 #endif /* NO_ARG_ARRAY */
5974 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5976 else
5977 message1 (0);
5979 /* Print should start at the beginning of the message
5980 buffer next time. */
5981 message_buf_print = 0;
5987 /* The non-logging version of message. */
5989 void
5990 message_nolog (m, a1, a2, a3)
5991 char *m;
5992 EMACS_INT a1, a2, a3;
5994 Lisp_Object old_log_max;
5995 old_log_max = Vmessage_log_max;
5996 Vmessage_log_max = Qnil;
5997 message (m, a1, a2, a3);
5998 Vmessage_log_max = old_log_max;
6002 /* Display the current message in the current mini-buffer. This is
6003 only called from error handlers in process.c, and is not time
6004 critical. */
6006 void
6007 update_echo_area ()
6009 if (!NILP (echo_area_buffer[0]))
6011 Lisp_Object string;
6012 string = Fcurrent_message ();
6013 message3 (string, XSTRING (string)->size,
6014 !NILP (current_buffer->enable_multibyte_characters));
6019 /* Make sure echo area buffers in echo_buffers[] are life. If they
6020 aren't, make new ones. */
6022 static void
6023 ensure_echo_area_buffers ()
6025 int i;
6027 for (i = 0; i < 2; ++i)
6028 if (!BUFFERP (echo_buffer[i])
6029 || NILP (XBUFFER (echo_buffer[i])->name))
6031 char name[30];
6032 Lisp_Object old_buffer;
6033 int j;
6035 old_buffer = echo_buffer[i];
6036 sprintf (name, " *Echo Area %d*", i);
6037 echo_buffer[i] = Fget_buffer_create (build_string (name));
6038 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6040 for (j = 0; j < 2; ++j)
6041 if (EQ (old_buffer, echo_area_buffer[j]))
6042 echo_area_buffer[j] = echo_buffer[i];
6047 /* Call FN with args A1..A4 with either the current or last displayed
6048 echo_area_buffer as current buffer.
6050 WHICH zero means use the current message buffer
6051 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6052 from echo_buffer[] and clear it.
6054 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6055 suitable buffer from echo_buffer[] and clear it.
6057 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6058 that the current message becomes the last displayed one, make
6059 choose a suitable buffer for echo_area_buffer[0], and clear it.
6061 Value is what FN returns. */
6063 static int
6064 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6065 struct window *w;
6066 int which;
6067 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6068 EMACS_INT a1;
6069 Lisp_Object a2;
6070 EMACS_INT a3, a4;
6072 Lisp_Object buffer;
6073 int this_one, the_other, clear_buffer_p, rc;
6074 int count = BINDING_STACK_SIZE ();
6076 /* If buffers aren't life, make new ones. */
6077 ensure_echo_area_buffers ();
6079 clear_buffer_p = 0;
6081 if (which == 0)
6082 this_one = 0, the_other = 1;
6083 else if (which > 0)
6084 this_one = 1, the_other = 0;
6085 else
6087 this_one = 0, the_other = 1;
6088 clear_buffer_p = 1;
6090 /* We need a fresh one in case the current echo buffer equals
6091 the one containing the last displayed echo area message. */
6092 if (!NILP (echo_area_buffer[this_one])
6093 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6094 echo_area_buffer[this_one] = Qnil;
6097 /* Choose a suitable buffer from echo_buffer[] is we don't
6098 have one. */
6099 if (NILP (echo_area_buffer[this_one]))
6101 echo_area_buffer[this_one]
6102 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6103 ? echo_buffer[the_other]
6104 : echo_buffer[this_one]);
6105 clear_buffer_p = 1;
6108 buffer = echo_area_buffer[this_one];
6110 record_unwind_protect (unwind_with_echo_area_buffer,
6111 with_echo_area_buffer_unwind_data (w));
6113 /* Make the echo area buffer current. Note that for display
6114 purposes, it is not necessary that the displayed window's buffer
6115 == current_buffer, except for text property lookup. So, let's
6116 only set that buffer temporarily here without doing a full
6117 Fset_window_buffer. We must also change w->pointm, though,
6118 because otherwise an assertions in unshow_buffer fails, and Emacs
6119 aborts. */
6120 set_buffer_internal_1 (XBUFFER (buffer));
6121 if (w)
6123 w->buffer = buffer;
6124 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6127 current_buffer->undo_list = Qt;
6128 current_buffer->read_only = Qnil;
6129 specbind (Qinhibit_read_only, Qt);
6131 if (clear_buffer_p && Z > BEG)
6132 del_range (BEG, Z);
6134 xassert (BEGV >= BEG);
6135 xassert (ZV <= Z && ZV >= BEGV);
6137 rc = fn (a1, a2, a3, a4);
6139 xassert (BEGV >= BEG);
6140 xassert (ZV <= Z && ZV >= BEGV);
6142 unbind_to (count, Qnil);
6143 return rc;
6147 /* Save state that should be preserved around the call to the function
6148 FN called in with_echo_area_buffer. */
6150 static Lisp_Object
6151 with_echo_area_buffer_unwind_data (w)
6152 struct window *w;
6154 int i = 0;
6155 Lisp_Object vector;
6157 /* Reduce consing by keeping one vector in
6158 Vwith_echo_area_save_vector. */
6159 vector = Vwith_echo_area_save_vector;
6160 Vwith_echo_area_save_vector = Qnil;
6162 if (NILP (vector))
6163 vector = Fmake_vector (make_number (7), Qnil);
6165 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6166 AREF (vector, i) = Vdeactivate_mark, ++i;
6167 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6169 if (w)
6171 XSETWINDOW (AREF (vector, i), w); ++i;
6172 AREF (vector, i) = w->buffer; ++i;
6173 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6174 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6176 else
6178 int end = i + 4;
6179 for (; i < end; ++i)
6180 AREF (vector, i) = Qnil;
6183 xassert (i == ASIZE (vector));
6184 return vector;
6188 /* Restore global state from VECTOR which was created by
6189 with_echo_area_buffer_unwind_data. */
6191 static Lisp_Object
6192 unwind_with_echo_area_buffer (vector)
6193 Lisp_Object vector;
6195 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6196 Vdeactivate_mark = AREF (vector, 1);
6197 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6199 if (WINDOWP (AREF (vector, 3)))
6201 struct window *w;
6202 Lisp_Object buffer, charpos, bytepos;
6204 w = XWINDOW (AREF (vector, 3));
6205 buffer = AREF (vector, 4);
6206 charpos = AREF (vector, 5);
6207 bytepos = AREF (vector, 6);
6209 w->buffer = buffer;
6210 set_marker_both (w->pointm, buffer,
6211 XFASTINT (charpos), XFASTINT (bytepos));
6214 Vwith_echo_area_save_vector = vector;
6215 return Qnil;
6219 /* Set up the echo area for use by print functions. MULTIBYTE_P
6220 non-zero means we will print multibyte. */
6222 void
6223 setup_echo_area_for_printing (multibyte_p)
6224 int multibyte_p;
6226 ensure_echo_area_buffers ();
6228 if (!message_buf_print)
6230 /* A message has been output since the last time we printed.
6231 Choose a fresh echo area buffer. */
6232 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6233 echo_area_buffer[0] = echo_buffer[1];
6234 else
6235 echo_area_buffer[0] = echo_buffer[0];
6237 /* Switch to that buffer and clear it. */
6238 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6239 current_buffer->truncate_lines = Qnil;
6241 if (Z > BEG)
6243 int count = BINDING_STACK_SIZE ();
6244 specbind (Qinhibit_read_only, Qt);
6245 del_range (BEG, Z);
6246 unbind_to (count, Qnil);
6248 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6250 /* Set up the buffer for the multibyteness we need. */
6251 if (multibyte_p
6252 != !NILP (current_buffer->enable_multibyte_characters))
6253 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6255 /* Raise the frame containing the echo area. */
6256 if (minibuffer_auto_raise)
6258 struct frame *sf = SELECTED_FRAME ();
6259 Lisp_Object mini_window;
6260 mini_window = FRAME_MINIBUF_WINDOW (sf);
6261 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6264 message_log_maybe_newline ();
6265 message_buf_print = 1;
6267 else
6269 if (NILP (echo_area_buffer[0]))
6271 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6272 echo_area_buffer[0] = echo_buffer[1];
6273 else
6274 echo_area_buffer[0] = echo_buffer[0];
6277 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6279 /* Someone switched buffers between print requests. */
6280 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6281 current_buffer->truncate_lines = Qnil;
6287 /* Display an echo area message in window W. Value is non-zero if W's
6288 height is changed. If display_last_displayed_message_p is
6289 non-zero, display the message that was last displayed, otherwise
6290 display the current message. */
6292 static int
6293 display_echo_area (w)
6294 struct window *w;
6296 int i, no_message_p, window_height_changed_p, count;
6298 /* Temporarily disable garbage collections while displaying the echo
6299 area. This is done because a GC can print a message itself.
6300 That message would modify the echo area buffer's contents while a
6301 redisplay of the buffer is going on, and seriously confuse
6302 redisplay. */
6303 count = inhibit_garbage_collection ();
6305 /* If there is no message, we must call display_echo_area_1
6306 nevertheless because it resizes the window. But we will have to
6307 reset the echo_area_buffer in question to nil at the end because
6308 with_echo_area_buffer will sets it to an empty buffer. */
6309 i = display_last_displayed_message_p ? 1 : 0;
6310 no_message_p = NILP (echo_area_buffer[i]);
6312 window_height_changed_p
6313 = with_echo_area_buffer (w, display_last_displayed_message_p,
6314 display_echo_area_1,
6315 (EMACS_INT) w, Qnil, 0, 0);
6317 if (no_message_p)
6318 echo_area_buffer[i] = Qnil;
6320 unbind_to (count, Qnil);
6321 return window_height_changed_p;
6325 /* Helper for display_echo_area. Display the current buffer which
6326 contains the current echo area message in window W, a mini-window,
6327 a pointer to which is passed in A1. A2..A4 are currently not used.
6328 Change the height of W so that all of the message is displayed.
6329 Value is non-zero if height of W was changed. */
6331 static int
6332 display_echo_area_1 (a1, a2, a3, a4)
6333 EMACS_INT a1;
6334 Lisp_Object a2;
6335 EMACS_INT a3, a4;
6337 struct window *w = (struct window *) a1;
6338 Lisp_Object window;
6339 struct text_pos start;
6340 int window_height_changed_p = 0;
6342 /* Do this before displaying, so that we have a large enough glyph
6343 matrix for the display. */
6344 window_height_changed_p = resize_mini_window (w, 0);
6346 /* Display. */
6347 clear_glyph_matrix (w->desired_matrix);
6348 XSETWINDOW (window, w);
6349 SET_TEXT_POS (start, BEG, BEG_BYTE);
6350 try_window (window, start);
6352 return window_height_changed_p;
6356 /* Resize the echo area window to exactly the size needed for the
6357 currently displayed message, if there is one. */
6359 void
6360 resize_echo_area_axactly ()
6362 if (BUFFERP (echo_area_buffer[0])
6363 && WINDOWP (echo_area_window))
6365 struct window *w = XWINDOW (echo_area_window);
6366 int resized_p;
6368 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6369 (EMACS_INT) w, Qnil, 0, 0);
6370 if (resized_p)
6372 ++windows_or_buffers_changed;
6373 ++update_mode_lines;
6374 redisplay_internal (0);
6380 /* Callback function for with_echo_area_buffer, when used from
6381 resize_echo_area_axactly. A1 contains a pointer to the window to
6382 resize, A2 to A4 are not used. Value is what resize_mini_window
6383 returns. */
6385 static int
6386 resize_mini_window_1 (a1, a2, a3, a4)
6387 EMACS_INT a1;
6388 Lisp_Object a2;
6389 EMACS_INT a3, a4;
6391 return resize_mini_window ((struct window *) a1, 1);
6395 /* Resize mini-window W to fit the size of its contents. EXACT:P
6396 means size the window exactly to the size needed. Otherwise, it's
6397 only enlarged until W's buffer is empty. Value is non-zero if
6398 the window height has been changed. */
6401 resize_mini_window (w, exact_p)
6402 struct window *w;
6403 int exact_p;
6405 struct frame *f = XFRAME (w->frame);
6406 int window_height_changed_p = 0;
6408 xassert (MINI_WINDOW_P (w));
6410 /* Nil means don't try to resize. */
6411 if (NILP (Vresize_mini_windows)
6412 || (FRAME_X_P (f) && f->output_data.x == NULL))
6413 return 0;
6415 if (!FRAME_MINIBUF_ONLY_P (f))
6417 struct it it;
6418 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6419 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6420 int height, max_height;
6421 int unit = CANON_Y_UNIT (f);
6422 struct text_pos start;
6423 struct buffer *old_current_buffer = NULL;
6425 if (current_buffer != XBUFFER (w->buffer))
6427 old_current_buffer = current_buffer;
6428 set_buffer_internal (XBUFFER (w->buffer));
6431 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6433 /* Compute the max. number of lines specified by the user. */
6434 if (FLOATP (Vmax_mini_window_height))
6435 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6436 else if (INTEGERP (Vmax_mini_window_height))
6437 max_height = XINT (Vmax_mini_window_height);
6438 else
6439 max_height = total_height / 4;
6441 /* Correct that max. height if it's bogus. */
6442 max_height = max (1, max_height);
6443 max_height = min (total_height, max_height);
6445 /* Find out the height of the text in the window. */
6446 if (it.truncate_lines_p)
6447 height = 1;
6448 else
6450 last_height = 0;
6451 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6452 if (it.max_ascent == 0 && it.max_descent == 0)
6453 height = it.current_y + last_height;
6454 else
6455 height = it.current_y + it.max_ascent + it.max_descent;
6456 height -= it.extra_line_spacing;
6457 height = (height + unit - 1) / unit;
6460 /* Compute a suitable window start. */
6461 if (height > max_height)
6463 height = max_height;
6464 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6465 move_it_vertically_backward (&it, (height - 1) * unit);
6466 start = it.current.pos;
6468 else
6469 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6470 SET_MARKER_FROM_TEXT_POS (w->start, start);
6472 if (EQ (Vresize_mini_windows, Qgrow_only))
6474 /* Let it grow only, until we display an empty message, in which
6475 case the window shrinks again. */
6476 if (height > XFASTINT (w->height))
6478 int old_height = XFASTINT (w->height);
6479 freeze_window_starts (f, 1);
6480 grow_mini_window (w, height - XFASTINT (w->height));
6481 window_height_changed_p = XFASTINT (w->height) != old_height;
6483 else if (height < XFASTINT (w->height)
6484 && (exact_p || BEGV == ZV))
6486 int old_height = XFASTINT (w->height);
6487 freeze_window_starts (f, 0);
6488 shrink_mini_window (w);
6489 window_height_changed_p = XFASTINT (w->height) != old_height;
6492 else
6494 /* Always resize to exact size needed. */
6495 if (height > XFASTINT (w->height))
6497 int old_height = XFASTINT (w->height);
6498 freeze_window_starts (f, 1);
6499 grow_mini_window (w, height - XFASTINT (w->height));
6500 window_height_changed_p = XFASTINT (w->height) != old_height;
6502 else if (height < XFASTINT (w->height))
6504 int old_height = XFASTINT (w->height);
6505 freeze_window_starts (f, 0);
6506 shrink_mini_window (w);
6508 if (height)
6510 freeze_window_starts (f, 1);
6511 grow_mini_window (w, height - XFASTINT (w->height));
6514 window_height_changed_p = XFASTINT (w->height) != old_height;
6518 if (old_current_buffer)
6519 set_buffer_internal (old_current_buffer);
6522 return window_height_changed_p;
6526 /* Value is the current message, a string, or nil if there is no
6527 current message. */
6529 Lisp_Object
6530 current_message ()
6532 Lisp_Object msg;
6534 if (NILP (echo_area_buffer[0]))
6535 msg = Qnil;
6536 else
6538 with_echo_area_buffer (0, 0, current_message_1,
6539 (EMACS_INT) &msg, Qnil, 0, 0);
6540 if (NILP (msg))
6541 echo_area_buffer[0] = Qnil;
6544 return msg;
6548 static int
6549 current_message_1 (a1, a2, a3, a4)
6550 EMACS_INT a1;
6551 Lisp_Object a2;
6552 EMACS_INT a3, a4;
6554 Lisp_Object *msg = (Lisp_Object *) a1;
6556 if (Z > BEG)
6557 *msg = make_buffer_string (BEG, Z, 1);
6558 else
6559 *msg = Qnil;
6560 return 0;
6564 /* Push the current message on Vmessage_stack for later restauration
6565 by restore_message. Value is non-zero if the current message isn't
6566 empty. This is a relatively infrequent operation, so it's not
6567 worth optimizing. */
6570 push_message ()
6572 Lisp_Object msg;
6573 msg = current_message ();
6574 Vmessage_stack = Fcons (msg, Vmessage_stack);
6575 return STRINGP (msg);
6579 /* Handler for record_unwind_protect calling pop_message. */
6581 Lisp_Object
6582 push_message_unwind (dummy)
6583 Lisp_Object dummy;
6585 pop_message ();
6586 return Qnil;
6590 /* Restore message display from the top of Vmessage_stack. */
6592 void
6593 restore_message ()
6595 Lisp_Object msg;
6597 xassert (CONSP (Vmessage_stack));
6598 msg = XCAR (Vmessage_stack);
6599 if (STRINGP (msg))
6600 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6601 else
6602 message3_nolog (msg, 0, 0);
6606 /* Pop the top-most entry off Vmessage_stack. */
6608 void
6609 pop_message ()
6611 xassert (CONSP (Vmessage_stack));
6612 Vmessage_stack = XCDR (Vmessage_stack);
6616 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6617 exits. If the stack is not empty, we have a missing pop_message
6618 somewhere. */
6620 void
6621 check_message_stack ()
6623 if (!NILP (Vmessage_stack))
6624 abort ();
6628 /* Truncate to NCHARS what will be displayed in the echo area the next
6629 time we display it---but don't redisplay it now. */
6631 void
6632 truncate_echo_area (nchars)
6633 int nchars;
6635 if (nchars == 0)
6636 echo_area_buffer[0] = Qnil;
6637 /* A null message buffer means that the frame hasn't really been
6638 initialized yet. Error messages get reported properly by
6639 cmd_error, so this must be just an informative message; toss it. */
6640 else if (!noninteractive
6641 && INTERACTIVE
6642 && !NILP (echo_area_buffer[0]))
6644 struct frame *sf = SELECTED_FRAME ();
6645 if (FRAME_MESSAGE_BUF (sf))
6646 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6651 /* Helper function for truncate_echo_area. Truncate the current
6652 message to at most NCHARS characters. */
6654 static int
6655 truncate_message_1 (nchars, a2, a3, a4)
6656 EMACS_INT nchars;
6657 Lisp_Object a2;
6658 EMACS_INT a3, a4;
6660 if (BEG + nchars < Z)
6661 del_range (BEG + nchars, Z);
6662 if (Z == BEG)
6663 echo_area_buffer[0] = Qnil;
6664 return 0;
6668 /* Set the current message to a substring of S or STRING.
6670 If STRING is a Lisp string, set the message to the first NBYTES
6671 bytes from STRING. NBYTES zero means use the whole string. If
6672 STRING is multibyte, the message will be displayed multibyte.
6674 If S is not null, set the message to the first LEN bytes of S. LEN
6675 zero means use the whole string. MULTIBYTE_P non-zero means S is
6676 multibyte. Display the message multibyte in that case. */
6678 void
6679 set_message (s, string, nbytes, multibyte_p)
6680 char *s;
6681 Lisp_Object string;
6682 int nbytes;
6684 message_enable_multibyte
6685 = ((s && multibyte_p)
6686 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6688 with_echo_area_buffer (0, -1, set_message_1,
6689 (EMACS_INT) s, string, nbytes, multibyte_p);
6690 message_buf_print = 0;
6691 help_echo_showing_p = 0;
6695 /* Helper function for set_message. Arguments have the same meaning
6696 as there, with A1 corresponding to S and A2 corresponding to STRING
6697 This function is called with the echo area buffer being
6698 current. */
6700 static int
6701 set_message_1 (a1, a2, nbytes, multibyte_p)
6702 EMACS_INT a1;
6703 Lisp_Object a2;
6704 EMACS_INT nbytes, multibyte_p;
6706 char *s = (char *) a1;
6707 Lisp_Object string = a2;
6709 xassert (BEG == Z);
6711 /* Change multibyteness of the echo buffer appropriately. */
6712 if (message_enable_multibyte
6713 != !NILP (current_buffer->enable_multibyte_characters))
6714 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6716 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6718 /* Insert new message at BEG. */
6719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6721 if (STRINGP (string))
6723 int nchars;
6725 if (nbytes == 0)
6726 nbytes = XSTRING (string)->size_byte;
6727 nchars = string_byte_to_char (string, nbytes);
6729 /* This function takes care of single/multibyte conversion. We
6730 just have to ensure that the echo area buffer has the right
6731 setting of enable_multibyte_characters. */
6732 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6734 else if (s)
6736 if (nbytes == 0)
6737 nbytes = strlen (s);
6739 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6741 /* Convert from multi-byte to single-byte. */
6742 int i, c, n;
6743 unsigned char work[1];
6745 /* Convert a multibyte string to single-byte. */
6746 for (i = 0; i < nbytes; i += n)
6748 c = string_char_and_length (s + i, nbytes - i, &n);
6749 work[0] = (SINGLE_BYTE_CHAR_P (c)
6751 : multibyte_char_to_unibyte (c, Qnil));
6752 insert_1_both (work, 1, 1, 1, 0, 0);
6755 else if (!multibyte_p
6756 && !NILP (current_buffer->enable_multibyte_characters))
6758 /* Convert from single-byte to multi-byte. */
6759 int i, c, n;
6760 unsigned char *msg = (unsigned char *) s;
6761 unsigned char str[MAX_MULTIBYTE_LENGTH];
6763 /* Convert a single-byte string to multibyte. */
6764 for (i = 0; i < nbytes; i++)
6766 c = unibyte_char_to_multibyte (msg[i]);
6767 n = CHAR_STRING (c, str);
6768 insert_1_both (str, 1, n, 1, 0, 0);
6771 else
6772 insert_1 (s, nbytes, 1, 0, 0);
6775 return 0;
6779 /* Clear messages. CURRENT_P non-zero means clear the current
6780 message. LAST_DISPLAYED_P non-zero means clear the message
6781 last displayed. */
6783 void
6784 clear_message (current_p, last_displayed_p)
6785 int current_p, last_displayed_p;
6787 if (current_p)
6788 echo_area_buffer[0] = Qnil;
6790 if (last_displayed_p)
6791 echo_area_buffer[1] = Qnil;
6793 message_buf_print = 0;
6796 /* Clear garbaged frames.
6798 This function is used where the old redisplay called
6799 redraw_garbaged_frames which in turn called redraw_frame which in
6800 turn called clear_frame. The call to clear_frame was a source of
6801 flickering. I believe a clear_frame is not necessary. It should
6802 suffice in the new redisplay to invalidate all current matrices,
6803 and ensure a complete redisplay of all windows. */
6805 static void
6806 clear_garbaged_frames ()
6808 if (frame_garbaged)
6810 Lisp_Object tail, frame;
6812 FOR_EACH_FRAME (tail, frame)
6814 struct frame *f = XFRAME (frame);
6816 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6818 clear_current_matrices (f);
6819 f->garbaged = 0;
6823 frame_garbaged = 0;
6824 ++windows_or_buffers_changed;
6829 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6830 is non-zero update selected_frame. Value is non-zero if the
6831 mini-windows height has been changed. */
6833 static int
6834 echo_area_display (update_frame_p)
6835 int update_frame_p;
6837 Lisp_Object mini_window;
6838 struct window *w;
6839 struct frame *f;
6840 int window_height_changed_p = 0;
6841 struct frame *sf = SELECTED_FRAME ();
6843 mini_window = FRAME_MINIBUF_WINDOW (sf);
6844 w = XWINDOW (mini_window);
6845 f = XFRAME (WINDOW_FRAME (w));
6847 /* Don't display if frame is invisible or not yet initialized. */
6848 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6849 return 0;
6851 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6852 #ifndef macintosh
6853 #ifdef HAVE_WINDOW_SYSTEM
6854 /* When Emacs starts, selected_frame may be a visible terminal
6855 frame, even if we run under a window system. If we let this
6856 through, a message would be displayed on the terminal. */
6857 if (EQ (selected_frame, Vterminal_frame)
6858 && !NILP (Vwindow_system))
6859 return 0;
6860 #endif /* HAVE_WINDOW_SYSTEM */
6861 #endif
6863 /* Redraw garbaged frames. */
6864 if (frame_garbaged)
6865 clear_garbaged_frames ();
6867 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6869 echo_area_window = mini_window;
6870 window_height_changed_p = display_echo_area (w);
6871 w->must_be_updated_p = 1;
6873 /* Update the display, unless called from redisplay_internal.
6874 Also don't update the screen during redisplay itself. The
6875 update will happen at the end of redisplay, and an update
6876 here could cause confusion. */
6877 if (update_frame_p && !redisplaying_p)
6879 int n = 0;
6881 /* If the display update has been interrupted by pending
6882 input, update mode lines in the frame. Due to the
6883 pending input, it might have been that redisplay hasn't
6884 been called, so that mode lines above the echo area are
6885 garbaged. This looks odd, so we prevent it here. */
6886 if (!display_completed)
6887 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6889 if (window_height_changed_p
6890 /* Don't do this if Emacs is shutting down. Redisplay
6891 needs to run hooks. */
6892 && !NILP (Vrun_hooks))
6894 /* Must update other windows. Likewise as in other
6895 cases, don't let this update be interrupted by
6896 pending input. */
6897 int count = BINDING_STACK_SIZE ();
6898 specbind (Qredisplay_dont_pause, Qt);
6899 windows_or_buffers_changed = 1;
6900 redisplay_internal (0);
6901 unbind_to (count, Qnil);
6903 else if (FRAME_WINDOW_P (f) && n == 0)
6905 /* Window configuration is the same as before.
6906 Can do with a display update of the echo area,
6907 unless we displayed some mode lines. */
6908 update_single_window (w, 1);
6909 rif->flush_display (f);
6911 else
6912 update_frame (f, 1, 1);
6914 /* If cursor is in the echo area, make sure that the next
6915 redisplay displays the minibuffer, so that the cursor will
6916 be replaced with what the minibuffer wants. */
6917 if (cursor_in_echo_area)
6918 ++windows_or_buffers_changed;
6921 else if (!EQ (mini_window, selected_window))
6922 windows_or_buffers_changed++;
6924 /* Last displayed message is now the current message. */
6925 echo_area_buffer[1] = echo_area_buffer[0];
6927 /* Prevent redisplay optimization in redisplay_internal by resetting
6928 this_line_start_pos. This is done because the mini-buffer now
6929 displays the message instead of its buffer text. */
6930 if (EQ (mini_window, selected_window))
6931 CHARPOS (this_line_start_pos) = 0;
6933 return window_height_changed_p;
6938 /***********************************************************************
6939 Frame Titles
6940 ***********************************************************************/
6943 #ifdef HAVE_WINDOW_SYSTEM
6945 /* A buffer for constructing frame titles in it; allocated from the
6946 heap in init_xdisp and resized as needed in store_frame_title_char. */
6948 static char *frame_title_buf;
6950 /* The buffer's end, and a current output position in it. */
6952 static char *frame_title_buf_end;
6953 static char *frame_title_ptr;
6956 /* Store a single character C for the frame title in frame_title_buf.
6957 Re-allocate frame_title_buf if necessary. */
6959 static void
6960 store_frame_title_char (c)
6961 char c;
6963 /* If output position has reached the end of the allocated buffer,
6964 double the buffer's size. */
6965 if (frame_title_ptr == frame_title_buf_end)
6967 int len = frame_title_ptr - frame_title_buf;
6968 int new_size = 2 * len * sizeof *frame_title_buf;
6969 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6970 frame_title_buf_end = frame_title_buf + new_size;
6971 frame_title_ptr = frame_title_buf + len;
6974 *frame_title_ptr++ = c;
6978 /* Store part of a frame title in frame_title_buf, beginning at
6979 frame_title_ptr. STR is the string to store. Do not copy
6980 characters that yield more columns than PRECISION; PRECISION <= 0
6981 means copy the whole string. Pad with spaces until FIELD_WIDTH
6982 number of characters have been copied; FIELD_WIDTH <= 0 means don't
6983 pad. Called from display_mode_element when it is used to build a
6984 frame title. */
6986 static int
6987 store_frame_title (str, field_width, precision)
6988 unsigned char *str;
6989 int field_width, precision;
6991 int n = 0;
6992 int dummy, nbytes, width;
6994 /* Copy at most PRECISION chars from STR. */
6995 nbytes = strlen (str);
6996 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
6997 while (nbytes--)
6998 store_frame_title_char (*str++);
7000 /* Fill up with spaces until FIELD_WIDTH reached. */
7001 while (field_width > 0
7002 && n < field_width)
7004 store_frame_title_char (' ');
7005 ++n;
7008 return n;
7012 /* Set the title of FRAME, if it has changed. The title format is
7013 Vicon_title_format if FRAME is iconified, otherwise it is
7014 frame_title_format. */
7016 static void
7017 x_consider_frame_title (frame)
7018 Lisp_Object frame;
7020 struct frame *f = XFRAME (frame);
7022 if (FRAME_WINDOW_P (f)
7023 || FRAME_MINIBUF_ONLY_P (f)
7024 || f->explicit_name)
7026 /* Do we have more than one visible frame on this X display? */
7027 Lisp_Object tail;
7028 Lisp_Object fmt;
7029 struct buffer *obuf;
7030 int len;
7031 struct it it;
7033 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7035 struct frame *tf = XFRAME (XCAR (tail));
7037 if (tf != f
7038 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7039 && !FRAME_MINIBUF_ONLY_P (tf)
7040 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7041 break;
7044 /* Set global variable indicating that multiple frames exist. */
7045 multiple_frames = CONSP (tail);
7047 /* Switch to the buffer of selected window of the frame. Set up
7048 frame_title_ptr so that display_mode_element will output into it;
7049 then display the title. */
7050 obuf = current_buffer;
7051 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7052 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7053 frame_title_ptr = frame_title_buf;
7054 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7055 NULL, DEFAULT_FACE_ID);
7056 display_mode_element (&it, 0, -1, -1, fmt);
7057 len = frame_title_ptr - frame_title_buf;
7058 frame_title_ptr = NULL;
7059 set_buffer_internal (obuf);
7061 /* Set the title only if it's changed. This avoids consing in
7062 the common case where it hasn't. (If it turns out that we've
7063 already wasted too much time by walking through the list with
7064 display_mode_element, then we might need to optimize at a
7065 higher level than this.) */
7066 if (! STRINGP (f->name)
7067 || STRING_BYTES (XSTRING (f->name)) != len
7068 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7069 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7073 #else /* not HAVE_WINDOW_SYSTEM */
7075 #define frame_title_ptr ((char *)0)
7076 #define store_frame_title(str, mincol, maxcol) 0
7078 #endif /* not HAVE_WINDOW_SYSTEM */
7083 /***********************************************************************
7084 Menu Bars
7085 ***********************************************************************/
7088 /* Prepare for redisplay by updating menu-bar item lists when
7089 appropriate. This can call eval. */
7091 void
7092 prepare_menu_bars ()
7094 int all_windows;
7095 struct gcpro gcpro1, gcpro2;
7096 struct frame *f;
7097 Lisp_Object tooltip_frame;
7099 #ifdef HAVE_X_WINDOWS
7100 tooltip_frame = tip_frame;
7101 #else
7102 tooltip_frame = Qnil;
7103 #endif
7105 /* Update all frame titles based on their buffer names, etc. We do
7106 this before the menu bars so that the buffer-menu will show the
7107 up-to-date frame titles. */
7108 #ifdef HAVE_WINDOW_SYSTEM
7109 if (windows_or_buffers_changed || update_mode_lines)
7111 Lisp_Object tail, frame;
7113 FOR_EACH_FRAME (tail, frame)
7115 f = XFRAME (frame);
7116 if (!EQ (frame, tooltip_frame)
7117 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7118 x_consider_frame_title (frame);
7121 #endif /* HAVE_WINDOW_SYSTEM */
7123 /* Update the menu bar item lists, if appropriate. This has to be
7124 done before any actual redisplay or generation of display lines. */
7125 all_windows = (update_mode_lines
7126 || buffer_shared > 1
7127 || windows_or_buffers_changed);
7128 if (all_windows)
7130 Lisp_Object tail, frame;
7131 int count = BINDING_STACK_SIZE ();
7133 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7135 FOR_EACH_FRAME (tail, frame)
7137 f = XFRAME (frame);
7139 /* Ignore tooltip frame. */
7140 if (EQ (frame, tooltip_frame))
7141 continue;
7143 /* If a window on this frame changed size, report that to
7144 the user and clear the size-change flag. */
7145 if (FRAME_WINDOW_SIZES_CHANGED (f))
7147 Lisp_Object functions;
7149 /* Clear flag first in case we get an error below. */
7150 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7151 functions = Vwindow_size_change_functions;
7152 GCPRO2 (tail, functions);
7154 while (CONSP (functions))
7156 call1 (XCAR (functions), frame);
7157 functions = XCDR (functions);
7159 UNGCPRO;
7162 GCPRO1 (tail);
7163 update_menu_bar (f, 0);
7164 #ifdef HAVE_WINDOW_SYSTEM
7165 update_tool_bar (f, 0);
7166 #endif
7167 UNGCPRO;
7170 unbind_to (count, Qnil);
7172 else
7174 struct frame *sf = SELECTED_FRAME ();
7175 update_menu_bar (sf, 1);
7176 #ifdef HAVE_WINDOW_SYSTEM
7177 update_tool_bar (sf, 1);
7178 #endif
7181 /* Motif needs this. See comment in xmenu.c. Turn it off when
7182 pending_menu_activation is not defined. */
7183 #ifdef USE_X_TOOLKIT
7184 pending_menu_activation = 0;
7185 #endif
7189 /* Update the menu bar item list for frame F. This has to be done
7190 before we start to fill in any display lines, because it can call
7191 eval.
7193 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7195 static void
7196 update_menu_bar (f, save_match_data)
7197 struct frame *f;
7198 int save_match_data;
7200 Lisp_Object window;
7201 register struct window *w;
7203 /* If called recursively during a menu update, do nothing. This can
7204 happen when, for instance, an activate-menubar-hook causes a
7205 redisplay. */
7206 if (inhibit_menubar_update)
7207 return;
7209 window = FRAME_SELECTED_WINDOW (f);
7210 w = XWINDOW (window);
7212 if (update_mode_lines)
7213 w->update_mode_line = Qt;
7215 if (FRAME_WINDOW_P (f)
7217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7218 FRAME_EXTERNAL_MENU_BAR (f)
7219 #else
7220 FRAME_MENU_BAR_LINES (f) > 0
7221 #endif
7222 : FRAME_MENU_BAR_LINES (f) > 0)
7224 /* If the user has switched buffers or windows, we need to
7225 recompute to reflect the new bindings. But we'll
7226 recompute when update_mode_lines is set too; that means
7227 that people can use force-mode-line-update to request
7228 that the menu bar be recomputed. The adverse effect on
7229 the rest of the redisplay algorithm is about the same as
7230 windows_or_buffers_changed anyway. */
7231 if (windows_or_buffers_changed
7232 || !NILP (w->update_mode_line)
7233 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7234 < BUF_MODIFF (XBUFFER (w->buffer)))
7235 != !NILP (w->last_had_star))
7236 || ((!NILP (Vtransient_mark_mode)
7237 && !NILP (XBUFFER (w->buffer)->mark_active))
7238 != !NILP (w->region_showing)))
7240 struct buffer *prev = current_buffer;
7241 int count = BINDING_STACK_SIZE ();
7243 specbind (Qinhibit_menubar_update, Qt);
7245 set_buffer_internal_1 (XBUFFER (w->buffer));
7246 if (save_match_data)
7247 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7248 if (NILP (Voverriding_local_map_menu_flag))
7250 specbind (Qoverriding_terminal_local_map, Qnil);
7251 specbind (Qoverriding_local_map, Qnil);
7254 /* Run the Lucid hook. */
7255 safe_run_hooks (Qactivate_menubar_hook);
7257 /* If it has changed current-menubar from previous value,
7258 really recompute the menu-bar from the value. */
7259 if (! NILP (Vlucid_menu_bar_dirty_flag))
7260 call0 (Qrecompute_lucid_menubar);
7262 safe_run_hooks (Qmenu_bar_update_hook);
7263 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7265 /* Redisplay the menu bar in case we changed it. */
7266 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7267 if (FRAME_WINDOW_P (f)
7268 #if defined (macintosh)
7269 /* All frames on Mac OS share the same menubar. So only the
7270 selected frame should be allowed to set it. */
7271 && f == SELECTED_FRAME ()
7272 #endif
7274 set_frame_menubar (f, 0, 0);
7275 else
7276 /* On a terminal screen, the menu bar is an ordinary screen
7277 line, and this makes it get updated. */
7278 w->update_mode_line = Qt;
7279 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7280 /* In the non-toolkit version, the menu bar is an ordinary screen
7281 line, and this makes it get updated. */
7282 w->update_mode_line = Qt;
7283 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7285 unbind_to (count, Qnil);
7286 set_buffer_internal_1 (prev);
7293 /***********************************************************************
7294 Tool-bars
7295 ***********************************************************************/
7297 #ifdef HAVE_WINDOW_SYSTEM
7299 /* Update the tool-bar item list for frame F. This has to be done
7300 before we start to fill in any display lines. Called from
7301 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7302 and restore it here. */
7304 static void
7305 update_tool_bar (f, save_match_data)
7306 struct frame *f;
7307 int save_match_data;
7309 if (WINDOWP (f->tool_bar_window)
7310 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7312 Lisp_Object window;
7313 struct window *w;
7315 window = FRAME_SELECTED_WINDOW (f);
7316 w = XWINDOW (window);
7318 /* If the user has switched buffers or windows, we need to
7319 recompute to reflect the new bindings. But we'll
7320 recompute when update_mode_lines is set too; that means
7321 that people can use force-mode-line-update to request
7322 that the menu bar be recomputed. The adverse effect on
7323 the rest of the redisplay algorithm is about the same as
7324 windows_or_buffers_changed anyway. */
7325 if (windows_or_buffers_changed
7326 || !NILP (w->update_mode_line)
7327 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7328 < BUF_MODIFF (XBUFFER (w->buffer)))
7329 != !NILP (w->last_had_star))
7330 || ((!NILP (Vtransient_mark_mode)
7331 && !NILP (XBUFFER (w->buffer)->mark_active))
7332 != !NILP (w->region_showing)))
7334 struct buffer *prev = current_buffer;
7335 int count = BINDING_STACK_SIZE ();
7337 /* Set current_buffer to the buffer of the selected
7338 window of the frame, so that we get the right local
7339 keymaps. */
7340 set_buffer_internal_1 (XBUFFER (w->buffer));
7342 /* Save match data, if we must. */
7343 if (save_match_data)
7344 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7346 /* Make sure that we don't accidentally use bogus keymaps. */
7347 if (NILP (Voverriding_local_map_menu_flag))
7349 specbind (Qoverriding_terminal_local_map, Qnil);
7350 specbind (Qoverriding_local_map, Qnil);
7353 /* Build desired tool-bar items from keymaps. */
7354 f->tool_bar_items
7355 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7357 /* Redisplay the tool-bar in case we changed it. */
7358 w->update_mode_line = Qt;
7360 unbind_to (count, Qnil);
7361 set_buffer_internal_1 (prev);
7367 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7368 F's desired tool-bar contents. F->tool_bar_items must have
7369 been set up previously by calling prepare_menu_bars. */
7371 static void
7372 build_desired_tool_bar_string (f)
7373 struct frame *f;
7375 int i, size, size_needed;
7376 struct gcpro gcpro1, gcpro2, gcpro3;
7377 Lisp_Object image, plist, props;
7379 image = plist = props = Qnil;
7380 GCPRO3 (image, plist, props);
7382 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7383 Otherwise, make a new string. */
7385 /* The size of the string we might be able to reuse. */
7386 size = (STRINGP (f->desired_tool_bar_string)
7387 ? XSTRING (f->desired_tool_bar_string)->size
7388 : 0);
7390 /* We need one space in the string for each image. */
7391 size_needed = f->n_tool_bar_items;
7393 /* Reuse f->desired_tool_bar_string, if possible. */
7394 if (size < size_needed || NILP (f->desired_tool_bar_string))
7395 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7396 make_number (' '));
7397 else
7399 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7400 Fremove_text_properties (make_number (0), make_number (size),
7401 props, f->desired_tool_bar_string);
7404 /* Put a `display' property on the string for the images to display,
7405 put a `menu_item' property on tool-bar items with a value that
7406 is the index of the item in F's tool-bar item vector. */
7407 for (i = 0; i < f->n_tool_bar_items; ++i)
7409 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7411 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7412 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7413 int hmargin, vmargin, relief, idx, end;
7414 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7415 extern Lisp_Object Qlaplace;
7417 /* If image is a vector, choose the image according to the
7418 button state. */
7419 image = PROP (TOOL_BAR_ITEM_IMAGES);
7420 if (VECTORP (image))
7422 if (enabled_p)
7423 idx = (selected_p
7424 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7425 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7426 else
7427 idx = (selected_p
7428 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7429 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7431 xassert (ASIZE (image) >= idx);
7432 image = AREF (image, idx);
7434 else
7435 idx = -1;
7437 /* Ignore invalid image specifications. */
7438 if (!valid_image_p (image))
7439 continue;
7441 /* Display the tool-bar button pressed, or depressed. */
7442 plist = Fcopy_sequence (XCDR (image));
7444 /* Compute margin and relief to draw. */
7445 relief = (tool_bar_button_relief > 0
7446 ? tool_bar_button_relief
7447 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7448 hmargin = vmargin = relief;
7450 if (INTEGERP (Vtool_bar_button_margin)
7451 && XINT (Vtool_bar_button_margin) > 0)
7453 hmargin += XFASTINT (Vtool_bar_button_margin);
7454 vmargin += XFASTINT (Vtool_bar_button_margin);
7456 else if (CONSP (Vtool_bar_button_margin))
7458 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7459 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7460 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7462 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7463 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7464 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7467 if (auto_raise_tool_bar_buttons_p)
7469 /* Add a `:relief' property to the image spec if the item is
7470 selected. */
7471 if (selected_p)
7473 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7474 hmargin -= relief;
7475 vmargin -= relief;
7478 else
7480 /* If image is selected, display it pressed, i.e. with a
7481 negative relief. If it's not selected, display it with a
7482 raised relief. */
7483 plist = Fplist_put (plist, QCrelief,
7484 (selected_p
7485 ? make_number (-relief)
7486 : make_number (relief)));
7487 hmargin -= relief;
7488 vmargin -= relief;
7491 /* Put a margin around the image. */
7492 if (hmargin || vmargin)
7494 if (hmargin == vmargin)
7495 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7496 else
7497 plist = Fplist_put (plist, QCmargin,
7498 Fcons (make_number (hmargin),
7499 make_number (vmargin)));
7502 /* If button is not enabled, and we don't have special images
7503 for the disabled state, make the image appear disabled by
7504 applying an appropriate algorithm to it. */
7505 if (!enabled_p && idx < 0)
7506 plist = Fplist_put (plist, QCconversion, Qdisabled);
7508 /* Put a `display' text property on the string for the image to
7509 display. Put a `menu-item' property on the string that gives
7510 the start of this item's properties in the tool-bar items
7511 vector. */
7512 image = Fcons (Qimage, plist);
7513 props = list4 (Qdisplay, image,
7514 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7516 /* Let the last image hide all remaining spaces in the tool bar
7517 string. The string can be longer than needed when we reuse a
7518 previous string. */
7519 if (i + 1 == f->n_tool_bar_items)
7520 end = XSTRING (f->desired_tool_bar_string)->size;
7521 else
7522 end = i + 1;
7523 Fadd_text_properties (make_number (i), make_number (end),
7524 props, f->desired_tool_bar_string);
7525 #undef PROP
7528 UNGCPRO;
7532 /* Display one line of the tool-bar of frame IT->f. */
7534 static void
7535 display_tool_bar_line (it)
7536 struct it *it;
7538 struct glyph_row *row = it->glyph_row;
7539 int max_x = it->last_visible_x;
7540 struct glyph *last;
7542 prepare_desired_row (row);
7543 row->y = it->current_y;
7545 /* Note that this isn't made use of if the face hasn't a box,
7546 so there's no need to check the face here. */
7547 it->start_of_box_run_p = 1;
7549 while (it->current_x < max_x)
7551 int x_before, x, n_glyphs_before, i, nglyphs;
7553 /* Get the next display element. */
7554 if (!get_next_display_element (it))
7555 break;
7557 /* Produce glyphs. */
7558 x_before = it->current_x;
7559 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7560 PRODUCE_GLYPHS (it);
7562 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7563 i = 0;
7564 x = x_before;
7565 while (i < nglyphs)
7567 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7569 if (x + glyph->pixel_width > max_x)
7571 /* Glyph doesn't fit on line. */
7572 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7573 it->current_x = x;
7574 goto out;
7577 ++it->hpos;
7578 x += glyph->pixel_width;
7579 ++i;
7582 /* Stop at line ends. */
7583 if (ITERATOR_AT_END_OF_LINE_P (it))
7584 break;
7586 set_iterator_to_next (it, 1);
7589 out:;
7591 row->displays_text_p = row->used[TEXT_AREA] != 0;
7592 extend_face_to_end_of_line (it);
7593 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7594 last->right_box_line_p = 1;
7595 if (last == row->glyphs[TEXT_AREA])
7596 last->left_box_line_p = 1;
7597 compute_line_metrics (it);
7599 /* If line is empty, make it occupy the rest of the tool-bar. */
7600 if (!row->displays_text_p)
7602 row->height = row->phys_height = it->last_visible_y - row->y;
7603 row->ascent = row->phys_ascent = 0;
7606 row->full_width_p = 1;
7607 row->continued_p = 0;
7608 row->truncated_on_left_p = 0;
7609 row->truncated_on_right_p = 0;
7611 it->current_x = it->hpos = 0;
7612 it->current_y += row->height;
7613 ++it->vpos;
7614 ++it->glyph_row;
7618 /* Value is the number of screen lines needed to make all tool-bar
7619 items of frame F visible. */
7621 static int
7622 tool_bar_lines_needed (f)
7623 struct frame *f;
7625 struct window *w = XWINDOW (f->tool_bar_window);
7626 struct it it;
7628 /* Initialize an iterator for iteration over
7629 F->desired_tool_bar_string in the tool-bar window of frame F. */
7630 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7631 it.first_visible_x = 0;
7632 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7633 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7635 while (!ITERATOR_AT_END_P (&it))
7637 it.glyph_row = w->desired_matrix->rows;
7638 clear_glyph_row (it.glyph_row);
7639 display_tool_bar_line (&it);
7642 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7646 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7647 0, 1, 0,
7648 "Return the number of lines occupied by the tool bar of FRAME.")
7649 (frame)
7650 Lisp_Object frame;
7652 struct frame *f;
7653 struct window *w;
7654 int nlines = 0;
7656 if (NILP (frame))
7657 frame = selected_frame;
7658 else
7659 CHECK_FRAME (frame, 0);
7660 f = XFRAME (frame);
7662 if (WINDOWP (f->tool_bar_window)
7663 || (w = XWINDOW (f->tool_bar_window),
7664 XFASTINT (w->height) > 0))
7666 update_tool_bar (f, 1);
7667 if (f->n_tool_bar_items)
7669 build_desired_tool_bar_string (f);
7670 nlines = tool_bar_lines_needed (f);
7674 return make_number (nlines);
7678 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7679 height should be changed. */
7681 static int
7682 redisplay_tool_bar (f)
7683 struct frame *f;
7685 struct window *w;
7686 struct it it;
7687 struct glyph_row *row;
7688 int change_height_p = 0;
7690 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7691 do anything. This means you must start with tool-bar-lines
7692 non-zero to get the auto-sizing effect. Or in other words, you
7693 can turn off tool-bars by specifying tool-bar-lines zero. */
7694 if (!WINDOWP (f->tool_bar_window)
7695 || (w = XWINDOW (f->tool_bar_window),
7696 XFASTINT (w->height) == 0))
7697 return 0;
7699 /* Set up an iterator for the tool-bar window. */
7700 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7701 it.first_visible_x = 0;
7702 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7703 row = it.glyph_row;
7705 /* Build a string that represents the contents of the tool-bar. */
7706 build_desired_tool_bar_string (f);
7707 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7709 /* Display as many lines as needed to display all tool-bar items. */
7710 while (it.current_y < it.last_visible_y)
7711 display_tool_bar_line (&it);
7713 /* It doesn't make much sense to try scrolling in the tool-bar
7714 window, so don't do it. */
7715 w->desired_matrix->no_scrolling_p = 1;
7716 w->must_be_updated_p = 1;
7718 if (auto_resize_tool_bars_p)
7720 int nlines;
7722 /* If we couldn't display everything, change the tool-bar's
7723 height. */
7724 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7725 change_height_p = 1;
7727 /* If there are blank lines at the end, except for a partially
7728 visible blank line at the end that is smaller than
7729 CANON_Y_UNIT, change the tool-bar's height. */
7730 row = it.glyph_row - 1;
7731 if (!row->displays_text_p
7732 && row->height >= CANON_Y_UNIT (f))
7733 change_height_p = 1;
7735 /* If row displays tool-bar items, but is partially visible,
7736 change the tool-bar's height. */
7737 if (row->displays_text_p
7738 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7739 change_height_p = 1;
7741 /* Resize windows as needed by changing the `tool-bar-lines'
7742 frame parameter. */
7743 if (change_height_p
7744 && (nlines = tool_bar_lines_needed (f),
7745 nlines != XFASTINT (w->height)))
7747 extern Lisp_Object Qtool_bar_lines;
7748 Lisp_Object frame;
7749 int old_height = XFASTINT (w->height);
7751 XSETFRAME (frame, f);
7752 clear_glyph_matrix (w->desired_matrix);
7753 Fmodify_frame_parameters (frame,
7754 Fcons (Fcons (Qtool_bar_lines,
7755 make_number (nlines)),
7756 Qnil));
7757 if (XFASTINT (w->height) != old_height)
7758 fonts_changed_p = 1;
7762 return change_height_p;
7766 /* Get information about the tool-bar item which is displayed in GLYPH
7767 on frame F. Return in *PROP_IDX the index where tool-bar item
7768 properties start in F->tool_bar_items. Value is zero if
7769 GLYPH doesn't display a tool-bar item. */
7772 tool_bar_item_info (f, glyph, prop_idx)
7773 struct frame *f;
7774 struct glyph *glyph;
7775 int *prop_idx;
7777 Lisp_Object prop;
7778 int success_p;
7780 /* Get the text property `menu-item' at pos. The value of that
7781 property is the start index of this item's properties in
7782 F->tool_bar_items. */
7783 prop = Fget_text_property (make_number (glyph->charpos),
7784 Qmenu_item, f->current_tool_bar_string);
7785 if (INTEGERP (prop))
7787 *prop_idx = XINT (prop);
7788 success_p = 1;
7790 else
7791 success_p = 0;
7793 return success_p;
7796 #endif /* HAVE_WINDOW_SYSTEM */
7800 /************************************************************************
7801 Horizontal scrolling
7802 ************************************************************************/
7804 static int hscroll_window_tree P_ ((Lisp_Object));
7805 static int hscroll_windows P_ ((Lisp_Object));
7807 /* For all leaf windows in the window tree rooted at WINDOW, set their
7808 hscroll value so that PT is (i) visible in the window, and (ii) so
7809 that it is not within a certain margin at the window's left and
7810 right border. Value is non-zero if any window's hscroll has been
7811 changed. */
7813 static int
7814 hscroll_window_tree (window)
7815 Lisp_Object window;
7817 int hscrolled_p = 0;
7819 while (WINDOWP (window))
7821 struct window *w = XWINDOW (window);
7823 if (WINDOWP (w->hchild))
7824 hscrolled_p |= hscroll_window_tree (w->hchild);
7825 else if (WINDOWP (w->vchild))
7826 hscrolled_p |= hscroll_window_tree (w->vchild);
7827 else if (w->cursor.vpos >= 0)
7829 int hscroll_margin, text_area_x, text_area_y;
7830 int text_area_width, text_area_height;
7831 struct glyph_row *current_cursor_row
7832 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7833 struct glyph_row *desired_cursor_row
7834 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7835 struct glyph_row *cursor_row
7836 = (desired_cursor_row->enabled_p
7837 ? desired_cursor_row
7838 : current_cursor_row);
7840 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7841 &text_area_width, &text_area_height);
7843 /* Scroll when cursor is inside this scroll margin. */
7844 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7846 if ((XFASTINT (w->hscroll)
7847 && w->cursor.x < hscroll_margin)
7848 || (cursor_row->enabled_p
7849 && cursor_row->truncated_on_right_p
7850 && (w->cursor.x > text_area_width - hscroll_margin)))
7852 struct it it;
7853 int hscroll;
7854 struct buffer *saved_current_buffer;
7855 int pt;
7857 /* Find point in a display of infinite width. */
7858 saved_current_buffer = current_buffer;
7859 current_buffer = XBUFFER (w->buffer);
7861 if (w == XWINDOW (selected_window))
7862 pt = BUF_PT (current_buffer);
7863 else
7865 pt = marker_position (w->pointm);
7866 pt = max (BEGV, pt);
7867 pt = min (ZV, pt);
7870 /* Move iterator to pt starting at cursor_row->start in
7871 a line with infinite width. */
7872 init_to_row_start (&it, w, cursor_row);
7873 it.last_visible_x = INFINITY;
7874 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7875 current_buffer = saved_current_buffer;
7877 /* Center cursor in window. */
7878 hscroll = (max (0, it.current_x - text_area_width / 2)
7879 / CANON_X_UNIT (it.f));
7880 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7882 /* Don't call Fset_window_hscroll if value hasn't
7883 changed because it will prevent redisplay
7884 optimizations. */
7885 if (XFASTINT (w->hscroll) != hscroll)
7887 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7888 w->hscroll = make_number (hscroll);
7889 hscrolled_p = 1;
7894 window = w->next;
7897 /* Value is non-zero if hscroll of any leaf window has been changed. */
7898 return hscrolled_p;
7902 /* Set hscroll so that cursor is visible and not inside horizontal
7903 scroll margins for all windows in the tree rooted at WINDOW. See
7904 also hscroll_window_tree above. Value is non-zero if any window's
7905 hscroll has been changed. If it has, desired matrices on the frame
7906 of WINDOW are cleared. */
7908 static int
7909 hscroll_windows (window)
7910 Lisp_Object window;
7912 int hscrolled_p;
7914 if (automatic_hscrolling_p)
7916 hscrolled_p = hscroll_window_tree (window);
7917 if (hscrolled_p)
7918 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7920 else
7921 hscrolled_p = 0;
7922 return hscrolled_p;
7927 /************************************************************************
7928 Redisplay
7929 ************************************************************************/
7931 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7932 to a non-zero value. This is sometimes handy to have in a debugger
7933 session. */
7935 #if GLYPH_DEBUG
7937 /* First and last unchanged row for try_window_id. */
7939 int debug_first_unchanged_at_end_vpos;
7940 int debug_last_unchanged_at_beg_vpos;
7942 /* Delta vpos and y. */
7944 int debug_dvpos, debug_dy;
7946 /* Delta in characters and bytes for try_window_id. */
7948 int debug_delta, debug_delta_bytes;
7950 /* Values of window_end_pos and window_end_vpos at the end of
7951 try_window_id. */
7953 int debug_end_pos, debug_end_vpos;
7955 /* Append a string to W->desired_matrix->method. FMT is a printf
7956 format string. A1...A9 are a supplement for a variable-length
7957 argument list. If trace_redisplay_p is non-zero also printf the
7958 resulting string to stderr. */
7960 static void
7961 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7962 struct window *w;
7963 char *fmt;
7964 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7966 char buffer[512];
7967 char *method = w->desired_matrix->method;
7968 int len = strlen (method);
7969 int size = sizeof w->desired_matrix->method;
7970 int remaining = size - len - 1;
7972 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7973 if (len && remaining)
7975 method[len] = '|';
7976 --remaining, ++len;
7979 strncpy (method + len, buffer, remaining);
7981 if (trace_redisplay_p)
7982 fprintf (stderr, "%p (%s): %s\n",
7984 ((BUFFERP (w->buffer)
7985 && STRINGP (XBUFFER (w->buffer)->name))
7986 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7987 : "no buffer"),
7988 buffer);
7991 #endif /* GLYPH_DEBUG */
7994 /* This counter is used to clear the face cache every once in a while
7995 in redisplay_internal. It is incremented for each redisplay.
7996 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7997 cleared. */
7999 #define CLEAR_FACE_CACHE_COUNT 10000
8000 static int clear_face_cache_count;
8002 /* Record the previous terminal frame we displayed. */
8004 static struct frame *previous_terminal_frame;
8006 /* Non-zero while redisplay_internal is in progress. */
8008 int redisplaying_p;
8011 /* Value is non-zero if all changes in window W, which displays
8012 current_buffer, are in the text between START and END. START is a
8013 buffer position, END is given as a distance from Z. Used in
8014 redisplay_internal for display optimization. */
8016 static INLINE int
8017 text_outside_line_unchanged_p (w, start, end)
8018 struct window *w;
8019 int start, end;
8021 int unchanged_p = 1;
8023 /* If text or overlays have changed, see where. */
8024 if (XFASTINT (w->last_modified) < MODIFF
8025 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8027 /* Gap in the line? */
8028 if (GPT < start || Z - GPT < end)
8029 unchanged_p = 0;
8031 /* Changes start in front of the line, or end after it? */
8032 if (unchanged_p
8033 && (BEG_UNCHANGED < start - 1
8034 || END_UNCHANGED < end))
8035 unchanged_p = 0;
8037 /* If selective display, can't optimize if changes start at the
8038 beginning of the line. */
8039 if (unchanged_p
8040 && INTEGERP (current_buffer->selective_display)
8041 && XINT (current_buffer->selective_display) > 0
8042 && (BEG_UNCHANGED < start || GPT <= start))
8043 unchanged_p = 0;
8046 return unchanged_p;
8050 /* Do a frame update, taking possible shortcuts into account. This is
8051 the main external entry point for redisplay.
8053 If the last redisplay displayed an echo area message and that message
8054 is no longer requested, we clear the echo area or bring back the
8055 mini-buffer if that is in use. */
8057 void
8058 redisplay ()
8060 redisplay_internal (0);
8063 /* Return 1 if point moved out of or into a composition. Otherwise
8064 return 0. PREV_BUF and PREV_PT are the last point buffer and
8065 position. BUF and PT are the current point buffer and position. */
8068 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8069 struct buffer *prev_buf, *buf;
8070 int prev_pt, pt;
8072 int start, end;
8073 Lisp_Object prop;
8074 Lisp_Object buffer;
8076 XSETBUFFER (buffer, buf);
8077 /* Check a composition at the last point if point moved within the
8078 same buffer. */
8079 if (prev_buf == buf)
8081 if (prev_pt == pt)
8082 /* Point didn't move. */
8083 return 0;
8085 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8086 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8087 && COMPOSITION_VALID_P (start, end, prop)
8088 && start < prev_pt && end > prev_pt)
8089 /* The last point was within the composition. Return 1 iff
8090 point moved out of the composition. */
8091 return (pt <= start || pt >= end);
8094 /* Check a composition at the current point. */
8095 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8096 && find_composition (pt, -1, &start, &end, &prop, buffer)
8097 && COMPOSITION_VALID_P (start, end, prop)
8098 && start < pt && end > pt);
8101 /* Reconsider the setting of B->clip_changed which is displayed
8102 in window W. */
8104 static INLINE void
8105 reconsider_clip_changes (w, b)
8106 struct window *w;
8107 struct buffer *b;
8109 if (b->prevent_redisplay_optimizations_p)
8110 b->clip_changed = 1;
8111 else if (b->clip_changed
8112 && !NILP (w->window_end_valid)
8113 && w->current_matrix->buffer == b
8114 && w->current_matrix->zv == BUF_ZV (b)
8115 && w->current_matrix->begv == BUF_BEGV (b))
8116 b->clip_changed = 0;
8118 /* If display wasn't paused, and W is not a tool bar window, see if
8119 point has been moved into or out of a composition. In that case,
8120 we set b->clip_changed to 1 to force updating the screen. If
8121 b->clip_changed has already been set to 1, we can skip this
8122 check. */
8123 if (!b->clip_changed
8124 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8126 int pt;
8128 if (w == XWINDOW (selected_window))
8129 pt = BUF_PT (current_buffer);
8130 else
8131 pt = marker_position (w->pointm);
8133 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8134 || pt != XINT (w->last_point))
8135 && check_point_in_composition (w->current_matrix->buffer,
8136 XINT (w->last_point),
8137 XBUFFER (w->buffer), pt))
8138 b->clip_changed = 1;
8143 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8144 response to any user action; therefore, we should preserve the echo
8145 area. (Actually, our caller does that job.) Perhaps in the future
8146 avoid recentering windows if it is not necessary; currently that
8147 causes some problems. */
8149 static void
8150 redisplay_internal (preserve_echo_area)
8151 int preserve_echo_area;
8153 struct window *w = XWINDOW (selected_window);
8154 struct frame *f = XFRAME (w->frame);
8155 int pause;
8156 int must_finish = 0;
8157 struct text_pos tlbufpos, tlendpos;
8158 int number_of_visible_frames;
8159 int count;
8160 struct frame *sf = SELECTED_FRAME ();
8162 /* Non-zero means redisplay has to consider all windows on all
8163 frames. Zero means, only selected_window is considered. */
8164 int consider_all_windows_p;
8166 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8168 /* No redisplay if running in batch mode or frame is not yet fully
8169 initialized, or redisplay is explicitly turned off by setting
8170 Vinhibit_redisplay. */
8171 if (noninteractive
8172 || !NILP (Vinhibit_redisplay)
8173 || !f->glyphs_initialized_p)
8174 return;
8176 /* The flag redisplay_performed_directly_p is set by
8177 direct_output_for_insert when it already did the whole screen
8178 update necessary. */
8179 if (redisplay_performed_directly_p)
8181 redisplay_performed_directly_p = 0;
8182 if (!hscroll_windows (selected_window))
8183 return;
8186 #ifdef USE_X_TOOLKIT
8187 if (popup_activated ())
8188 return;
8189 #endif
8191 /* I don't think this happens but let's be paranoid. */
8192 if (redisplaying_p)
8193 return;
8195 /* Record a function that resets redisplaying_p to its old value
8196 when we leave this function. */
8197 count = BINDING_STACK_SIZE ();
8198 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8199 ++redisplaying_p;
8201 retry:
8202 pause = 0;
8203 reconsider_clip_changes (w, current_buffer);
8205 /* If new fonts have been loaded that make a glyph matrix adjustment
8206 necessary, do it. */
8207 if (fonts_changed_p)
8209 adjust_glyphs (NULL);
8210 ++windows_or_buffers_changed;
8211 fonts_changed_p = 0;
8214 /* If face_change_count is non-zero, init_iterator will free all
8215 realized faces, which includes the faces referenced from current
8216 matrices. So, we can't reuse current matrices in this case. */
8217 if (face_change_count)
8218 ++windows_or_buffers_changed;
8220 if (! FRAME_WINDOW_P (sf)
8221 && previous_terminal_frame != sf)
8223 /* Since frames on an ASCII terminal share the same display
8224 area, displaying a different frame means redisplay the whole
8225 thing. */
8226 windows_or_buffers_changed++;
8227 SET_FRAME_GARBAGED (sf);
8228 XSETFRAME (Vterminal_frame, sf);
8230 previous_terminal_frame = sf;
8232 /* Set the visible flags for all frames. Do this before checking
8233 for resized or garbaged frames; they want to know if their frames
8234 are visible. See the comment in frame.h for
8235 FRAME_SAMPLE_VISIBILITY. */
8237 Lisp_Object tail, frame;
8239 number_of_visible_frames = 0;
8241 FOR_EACH_FRAME (tail, frame)
8243 struct frame *f = XFRAME (frame);
8245 FRAME_SAMPLE_VISIBILITY (f);
8246 if (FRAME_VISIBLE_P (f))
8247 ++number_of_visible_frames;
8248 clear_desired_matrices (f);
8252 /* Notice any pending interrupt request to change frame size. */
8253 do_pending_window_change (1);
8255 /* Clear frames marked as garbaged. */
8256 if (frame_garbaged)
8257 clear_garbaged_frames ();
8259 /* Build menubar and tool-bar items. */
8260 prepare_menu_bars ();
8262 if (windows_or_buffers_changed)
8263 update_mode_lines++;
8265 /* Detect case that we need to write or remove a star in the mode line. */
8266 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8268 w->update_mode_line = Qt;
8269 if (buffer_shared > 1)
8270 update_mode_lines++;
8273 /* If %c is in the mode line, update it if needed. */
8274 if (!NILP (w->column_number_displayed)
8275 /* This alternative quickly identifies a common case
8276 where no change is needed. */
8277 && !(PT == XFASTINT (w->last_point)
8278 && XFASTINT (w->last_modified) >= MODIFF
8279 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8280 && XFASTINT (w->column_number_displayed) != current_column ())
8281 w->update_mode_line = Qt;
8283 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8285 /* The variable buffer_shared is set in redisplay_window and
8286 indicates that we redisplay a buffer in different windows. See
8287 there. */
8288 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8290 /* If specs for an arrow have changed, do thorough redisplay
8291 to ensure we remove any arrow that should no longer exist. */
8292 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8293 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8294 consider_all_windows_p = windows_or_buffers_changed = 1;
8296 /* Normally the message* functions will have already displayed and
8297 updated the echo area, but the frame may have been trashed, or
8298 the update may have been preempted, so display the echo area
8299 again here. Checking both message buffers captures the case that
8300 the echo area should be cleared. */
8301 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8303 int window_height_changed_p = echo_area_display (0);
8304 must_finish = 1;
8306 if (fonts_changed_p)
8307 goto retry;
8308 else if (window_height_changed_p)
8310 consider_all_windows_p = 1;
8311 ++update_mode_lines;
8312 ++windows_or_buffers_changed;
8314 /* If window configuration was changed, frames may have been
8315 marked garbaged. Clear them or we will experience
8316 surprises wrt scrolling. */
8317 if (frame_garbaged)
8318 clear_garbaged_frames ();
8321 else if (EQ (selected_window, minibuf_window)
8322 && (current_buffer->clip_changed
8323 || XFASTINT (w->last_modified) < MODIFF
8324 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8325 && resize_mini_window (w, 0))
8327 /* Resized active mini-window to fit the size of what it is
8328 showing if its contents might have changed. */
8329 must_finish = 1;
8330 consider_all_windows_p = 1;
8331 ++windows_or_buffers_changed;
8332 ++update_mode_lines;
8334 /* If window configuration was changed, frames may have been
8335 marked garbaged. Clear them or we will experience
8336 surprises wrt scrolling. */
8337 if (frame_garbaged)
8338 clear_garbaged_frames ();
8342 /* If showing the region, and mark has changed, we must redisplay
8343 the whole window. The assignment to this_line_start_pos prevents
8344 the optimization directly below this if-statement. */
8345 if (((!NILP (Vtransient_mark_mode)
8346 && !NILP (XBUFFER (w->buffer)->mark_active))
8347 != !NILP (w->region_showing))
8348 || (!NILP (w->region_showing)
8349 && !EQ (w->region_showing,
8350 Fmarker_position (XBUFFER (w->buffer)->mark))))
8351 CHARPOS (this_line_start_pos) = 0;
8353 /* Optimize the case that only the line containing the cursor in the
8354 selected window has changed. Variables starting with this_ are
8355 set in display_line and record information about the line
8356 containing the cursor. */
8357 tlbufpos = this_line_start_pos;
8358 tlendpos = this_line_end_pos;
8359 if (!consider_all_windows_p
8360 && CHARPOS (tlbufpos) > 0
8361 && NILP (w->update_mode_line)
8362 && !current_buffer->clip_changed
8363 && FRAME_VISIBLE_P (XFRAME (w->frame))
8364 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8365 /* Make sure recorded data applies to current buffer, etc. */
8366 && this_line_buffer == current_buffer
8367 && current_buffer == XBUFFER (w->buffer)
8368 && NILP (w->force_start)
8369 /* Point must be on the line that we have info recorded about. */
8370 && PT >= CHARPOS (tlbufpos)
8371 && PT <= Z - CHARPOS (tlendpos)
8372 /* All text outside that line, including its final newline,
8373 must be unchanged */
8374 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8375 CHARPOS (tlendpos)))
8377 if (CHARPOS (tlbufpos) > BEGV
8378 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8379 && (CHARPOS (tlbufpos) == ZV
8380 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8381 /* Former continuation line has disappeared by becoming empty */
8382 goto cancel;
8383 else if (XFASTINT (w->last_modified) < MODIFF
8384 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8385 || MINI_WINDOW_P (w))
8387 /* We have to handle the case of continuation around a
8388 wide-column character (See the comment in indent.c around
8389 line 885).
8391 For instance, in the following case:
8393 -------- Insert --------
8394 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8395 J_I_ ==> J_I_ `^^' are cursors.
8396 ^^ ^^
8397 -------- --------
8399 As we have to redraw the line above, we should goto cancel. */
8401 struct it it;
8402 int line_height_before = this_line_pixel_height;
8404 /* Note that start_display will handle the case that the
8405 line starting at tlbufpos is a continuation lines. */
8406 start_display (&it, w, tlbufpos);
8408 /* Implementation note: It this still necessary? */
8409 if (it.current_x != this_line_start_x)
8410 goto cancel;
8412 TRACE ((stderr, "trying display optimization 1\n"));
8413 w->cursor.vpos = -1;
8414 overlay_arrow_seen = 0;
8415 it.vpos = this_line_vpos;
8416 it.current_y = this_line_y;
8417 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8418 display_line (&it);
8420 /* If line contains point, is not continued,
8421 and ends at same distance from eob as before, we win */
8422 if (w->cursor.vpos >= 0
8423 /* Line is not continued, otherwise this_line_start_pos
8424 would have been set to 0 in display_line. */
8425 && CHARPOS (this_line_start_pos)
8426 /* Line ends as before. */
8427 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8428 /* Line has same height as before. Otherwise other lines
8429 would have to be shifted up or down. */
8430 && this_line_pixel_height == line_height_before)
8432 /* If this is not the window's last line, we must adjust
8433 the charstarts of the lines below. */
8434 if (it.current_y < it.last_visible_y)
8436 struct glyph_row *row
8437 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8438 int delta, delta_bytes;
8440 if (Z - CHARPOS (tlendpos) == ZV)
8442 /* This line ends at end of (accessible part of)
8443 buffer. There is no newline to count. */
8444 delta = (Z
8445 - CHARPOS (tlendpos)
8446 - MATRIX_ROW_START_CHARPOS (row));
8447 delta_bytes = (Z_BYTE
8448 - BYTEPOS (tlendpos)
8449 - MATRIX_ROW_START_BYTEPOS (row));
8451 else
8453 /* This line ends in a newline. Must take
8454 account of the newline and the rest of the
8455 text that follows. */
8456 delta = (Z
8457 - CHARPOS (tlendpos)
8458 - MATRIX_ROW_START_CHARPOS (row));
8459 delta_bytes = (Z_BYTE
8460 - BYTEPOS (tlendpos)
8461 - MATRIX_ROW_START_BYTEPOS (row));
8464 increment_matrix_positions (w->current_matrix,
8465 this_line_vpos + 1,
8466 w->current_matrix->nrows,
8467 delta, delta_bytes);
8470 /* If this row displays text now but previously didn't,
8471 or vice versa, w->window_end_vpos may have to be
8472 adjusted. */
8473 if ((it.glyph_row - 1)->displays_text_p)
8475 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8476 XSETINT (w->window_end_vpos, this_line_vpos);
8478 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8479 && this_line_vpos > 0)
8480 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8481 w->window_end_valid = Qnil;
8483 /* Update hint: No need to try to scroll in update_window. */
8484 w->desired_matrix->no_scrolling_p = 1;
8486 #if GLYPH_DEBUG
8487 *w->desired_matrix->method = 0;
8488 debug_method_add (w, "optimization 1");
8489 #endif
8490 goto update;
8492 else
8493 goto cancel;
8495 else if (/* Cursor position hasn't changed. */
8496 PT == XFASTINT (w->last_point)
8497 /* Make sure the cursor was last displayed
8498 in this window. Otherwise we have to reposition it. */
8499 && 0 <= w->cursor.vpos
8500 && XINT (w->height) > w->cursor.vpos)
8502 if (!must_finish)
8504 do_pending_window_change (1);
8506 /* We used to always goto end_of_redisplay here, but this
8507 isn't enough if we have a blinking cursor. */
8508 if (w->cursor_off_p == w->last_cursor_off_p)
8509 goto end_of_redisplay;
8511 goto update;
8513 /* If highlighting the region, or if the cursor is in the echo area,
8514 then we can't just move the cursor. */
8515 else if (! (!NILP (Vtransient_mark_mode)
8516 && !NILP (current_buffer->mark_active))
8517 && (EQ (selected_window, current_buffer->last_selected_window)
8518 || highlight_nonselected_windows)
8519 && NILP (w->region_showing)
8520 && NILP (Vshow_trailing_whitespace)
8521 && !cursor_in_echo_area)
8523 struct it it;
8524 struct glyph_row *row;
8526 /* Skip from tlbufpos to PT and see where it is. Note that
8527 PT may be in invisible text. If so, we will end at the
8528 next visible position. */
8529 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8530 NULL, DEFAULT_FACE_ID);
8531 it.current_x = this_line_start_x;
8532 it.current_y = this_line_y;
8533 it.vpos = this_line_vpos;
8535 /* The call to move_it_to stops in front of PT, but
8536 moves over before-strings. */
8537 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8539 if (it.vpos == this_line_vpos
8540 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8541 row->enabled_p))
8543 xassert (this_line_vpos == it.vpos);
8544 xassert (this_line_y == it.current_y);
8545 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8546 #if GLYPH_DEBUG
8547 *w->desired_matrix->method = 0;
8548 debug_method_add (w, "optimization 3");
8549 #endif
8550 goto update;
8552 else
8553 goto cancel;
8556 cancel:
8557 /* Text changed drastically or point moved off of line. */
8558 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8561 CHARPOS (this_line_start_pos) = 0;
8562 consider_all_windows_p |= buffer_shared > 1;
8563 ++clear_face_cache_count;
8566 /* Build desired matrices, and update the display. If
8567 consider_all_windows_p is non-zero, do it for all windows on all
8568 frames. Otherwise do it for selected_window, only. */
8570 if (consider_all_windows_p)
8572 Lisp_Object tail, frame;
8573 int i, n = 0, size = 50;
8574 struct frame **updated
8575 = (struct frame **) alloca (size * sizeof *updated);
8577 /* Clear the face cache eventually. */
8578 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8580 clear_face_cache (0);
8581 clear_face_cache_count = 0;
8584 /* Recompute # windows showing selected buffer. This will be
8585 incremented each time such a window is displayed. */
8586 buffer_shared = 0;
8588 FOR_EACH_FRAME (tail, frame)
8590 struct frame *f = XFRAME (frame);
8592 if (FRAME_WINDOW_P (f) || f == sf)
8594 /* Mark all the scroll bars to be removed; we'll redeem
8595 the ones we want when we redisplay their windows. */
8596 if (condemn_scroll_bars_hook)
8597 condemn_scroll_bars_hook (f);
8599 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8600 redisplay_windows (FRAME_ROOT_WINDOW (f));
8602 /* Any scroll bars which redisplay_windows should have
8603 nuked should now go away. */
8604 if (judge_scroll_bars_hook)
8605 judge_scroll_bars_hook (f);
8607 /* If fonts changed, display again. */
8608 if (fonts_changed_p)
8609 goto retry;
8611 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8613 /* See if we have to hscroll. */
8614 if (hscroll_windows (f->root_window))
8615 goto retry;
8617 /* Prevent various kinds of signals during display
8618 update. stdio is not robust about handling
8619 signals, which can cause an apparent I/O
8620 error. */
8621 if (interrupt_input)
8622 unrequest_sigio ();
8623 stop_polling ();
8625 /* Update the display. */
8626 set_window_update_flags (XWINDOW (f->root_window), 1);
8627 pause |= update_frame (f, 0, 0);
8628 if (pause)
8629 break;
8631 if (n == size)
8633 int nbytes = size * sizeof *updated;
8634 struct frame **p = (struct frame **) alloca (2 * nbytes);
8635 bcopy (updated, p, nbytes);
8636 size *= 2;
8639 updated[n++] = f;
8644 /* Do the mark_window_display_accurate after all windows have
8645 been redisplayed because this call resets flags in buffers
8646 which are needed for proper redisplay. */
8647 for (i = 0; i < n; ++i)
8649 struct frame *f = updated[i];
8650 mark_window_display_accurate (f->root_window, 1);
8651 if (frame_up_to_date_hook)
8652 frame_up_to_date_hook (f);
8655 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8657 Lisp_Object mini_window;
8658 struct frame *mini_frame;
8660 redisplay_window (selected_window, 1);
8662 /* Compare desired and current matrices, perform output. */
8663 update:
8665 /* If fonts changed, display again. */
8666 if (fonts_changed_p)
8667 goto retry;
8669 /* Prevent various kinds of signals during display update.
8670 stdio is not robust about handling signals,
8671 which can cause an apparent I/O error. */
8672 if (interrupt_input)
8673 unrequest_sigio ();
8674 stop_polling ();
8676 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8678 if (hscroll_windows (selected_window))
8679 goto retry;
8681 XWINDOW (selected_window)->must_be_updated_p = 1;
8682 pause = update_frame (sf, 0, 0);
8685 /* We may have called echo_area_display at the top of this
8686 function. If the echo area is on another frame, that may
8687 have put text on a frame other than the selected one, so the
8688 above call to update_frame would not have caught it. Catch
8689 it here. */
8690 mini_window = FRAME_MINIBUF_WINDOW (sf);
8691 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8693 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8695 XWINDOW (mini_window)->must_be_updated_p = 1;
8696 pause |= update_frame (mini_frame, 0, 0);
8697 if (!pause && hscroll_windows (mini_window))
8698 goto retry;
8702 /* If display was paused because of pending input, make sure we do a
8703 thorough update the next time. */
8704 if (pause)
8706 /* Prevent the optimization at the beginning of
8707 redisplay_internal that tries a single-line update of the
8708 line containing the cursor in the selected window. */
8709 CHARPOS (this_line_start_pos) = 0;
8711 /* Let the overlay arrow be updated the next time. */
8712 if (!NILP (last_arrow_position))
8714 last_arrow_position = Qt;
8715 last_arrow_string = Qt;
8718 /* If we pause after scrolling, some rows in the current
8719 matrices of some windows are not valid. */
8720 if (!WINDOW_FULL_WIDTH_P (w)
8721 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8722 update_mode_lines = 1;
8724 else
8726 if (!consider_all_windows_p)
8728 /* This has already been done above if
8729 consider_all_windows_p is set. */
8730 mark_window_display_accurate_1 (w, 1);
8732 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8733 last_arrow_string = Voverlay_arrow_string;
8735 if (frame_up_to_date_hook != 0)
8736 frame_up_to_date_hook (sf);
8739 update_mode_lines = 0;
8740 windows_or_buffers_changed = 0;
8743 /* Start SIGIO interrupts coming again. Having them off during the
8744 code above makes it less likely one will discard output, but not
8745 impossible, since there might be stuff in the system buffer here.
8746 But it is much hairier to try to do anything about that. */
8747 if (interrupt_input)
8748 request_sigio ();
8749 start_polling ();
8751 /* If a frame has become visible which was not before, redisplay
8752 again, so that we display it. Expose events for such a frame
8753 (which it gets when becoming visible) don't call the parts of
8754 redisplay constructing glyphs, so simply exposing a frame won't
8755 display anything in this case. So, we have to display these
8756 frames here explicitly. */
8757 if (!pause)
8759 Lisp_Object tail, frame;
8760 int new_count = 0;
8762 FOR_EACH_FRAME (tail, frame)
8764 int this_is_visible = 0;
8766 if (XFRAME (frame)->visible)
8767 this_is_visible = 1;
8768 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8769 if (XFRAME (frame)->visible)
8770 this_is_visible = 1;
8772 if (this_is_visible)
8773 new_count++;
8776 if (new_count != number_of_visible_frames)
8777 windows_or_buffers_changed++;
8780 /* Change frame size now if a change is pending. */
8781 do_pending_window_change (1);
8783 /* If we just did a pending size change, or have additional
8784 visible frames, redisplay again. */
8785 if (windows_or_buffers_changed && !pause)
8786 goto retry;
8788 end_of_redisplay:;
8790 unbind_to (count, Qnil);
8794 /* Redisplay, but leave alone any recent echo area message unless
8795 another message has been requested in its place.
8797 This is useful in situations where you need to redisplay but no
8798 user action has occurred, making it inappropriate for the message
8799 area to be cleared. See tracking_off and
8800 wait_reading_process_input for examples of these situations.
8802 FROM_WHERE is an integer saying from where this function was
8803 called. This is useful for debugging. */
8805 void
8806 redisplay_preserve_echo_area (from_where)
8807 int from_where;
8809 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8811 if (!NILP (echo_area_buffer[1]))
8813 /* We have a previously displayed message, but no current
8814 message. Redisplay the previous message. */
8815 display_last_displayed_message_p = 1;
8816 redisplay_internal (1);
8817 display_last_displayed_message_p = 0;
8819 else
8820 redisplay_internal (1);
8824 /* Function registered with record_unwind_protect in
8825 redisplay_internal. Clears the flag indicating that a redisplay is
8826 in progress. */
8828 static Lisp_Object
8829 unwind_redisplay (old_redisplaying_p)
8830 Lisp_Object old_redisplaying_p;
8832 redisplaying_p = XFASTINT (old_redisplaying_p);
8833 return Qnil;
8837 /* Mark the display of window W as accurate or inaccurate. If
8838 ACCURATE_P is non-zero mark display of W as accurate. If
8839 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8840 redisplay_internal is called. */
8842 static void
8843 mark_window_display_accurate_1 (w, accurate_p)
8844 struct window *w;
8845 int accurate_p;
8847 if (BUFFERP (w->buffer))
8849 struct buffer *b = XBUFFER (w->buffer);
8851 w->last_modified
8852 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8853 w->last_overlay_modified
8854 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8855 w->last_had_star
8856 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8858 if (accurate_p)
8860 b->clip_changed = 0;
8861 b->prevent_redisplay_optimizations_p = 0;
8863 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8864 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8865 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8866 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8868 w->current_matrix->buffer = b;
8869 w->current_matrix->begv = BUF_BEGV (b);
8870 w->current_matrix->zv = BUF_ZV (b);
8872 w->last_cursor = w->cursor;
8873 w->last_cursor_off_p = w->cursor_off_p;
8875 if (w == XWINDOW (selected_window))
8876 w->last_point = make_number (BUF_PT (b));
8877 else
8878 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8882 if (accurate_p)
8884 w->window_end_valid = w->buffer;
8885 w->update_mode_line = Qnil;
8890 /* Mark the display of windows in the window tree rooted at WINDOW as
8891 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8892 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8893 be redisplayed the next time redisplay_internal is called. */
8895 void
8896 mark_window_display_accurate (window, accurate_p)
8897 Lisp_Object window;
8898 int accurate_p;
8900 struct window *w;
8902 for (; !NILP (window); window = w->next)
8904 w = XWINDOW (window);
8905 mark_window_display_accurate_1 (w, accurate_p);
8907 if (!NILP (w->vchild))
8908 mark_window_display_accurate (w->vchild, accurate_p);
8909 if (!NILP (w->hchild))
8910 mark_window_display_accurate (w->hchild, accurate_p);
8913 if (accurate_p)
8915 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8916 last_arrow_string = Voverlay_arrow_string;
8918 else
8920 /* Force a thorough redisplay the next time by setting
8921 last_arrow_position and last_arrow_string to t, which is
8922 unequal to any useful value of Voverlay_arrow_... */
8923 last_arrow_position = Qt;
8924 last_arrow_string = Qt;
8929 /* Return value in display table DP (Lisp_Char_Table *) for character
8930 C. Since a display table doesn't have any parent, we don't have to
8931 follow parent. Do not call this function directly but use the
8932 macro DISP_CHAR_VECTOR. */
8934 Lisp_Object
8935 disp_char_vector (dp, c)
8936 struct Lisp_Char_Table *dp;
8937 int c;
8939 int code[4], i;
8940 Lisp_Object val;
8942 if (SINGLE_BYTE_CHAR_P (c))
8943 return (dp->contents[c]);
8945 SPLIT_CHAR (c, code[0], code[1], code[2]);
8946 if (code[1] < 32)
8947 code[1] = -1;
8948 else if (code[2] < 32)
8949 code[2] = -1;
8951 /* Here, the possible range of code[0] (== charset ID) is
8952 128..max_charset. Since the top level char table contains data
8953 for multibyte characters after 256th element, we must increment
8954 code[0] by 128 to get a correct index. */
8955 code[0] += 128;
8956 code[3] = -1; /* anchor */
8958 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8960 val = dp->contents[code[i]];
8961 if (!SUB_CHAR_TABLE_P (val))
8962 return (NILP (val) ? dp->defalt : val);
8965 /* Here, val is a sub char table. We return the default value of
8966 it. */
8967 return (dp->defalt);
8972 /***********************************************************************
8973 Window Redisplay
8974 ***********************************************************************/
8976 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8978 static void
8979 redisplay_windows (window)
8980 Lisp_Object window;
8982 while (!NILP (window))
8984 struct window *w = XWINDOW (window);
8986 if (!NILP (w->hchild))
8987 redisplay_windows (w->hchild);
8988 else if (!NILP (w->vchild))
8989 redisplay_windows (w->vchild);
8990 else
8991 redisplay_window (window, 0);
8993 window = w->next;
8998 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8999 DELTA is the number of bytes by which positions recorded in ROW
9000 differ from current buffer positions. */
9002 void
9003 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9004 struct window *w;
9005 struct glyph_row *row;
9006 struct glyph_matrix *matrix;
9007 int delta, delta_bytes, dy, dvpos;
9009 struct glyph *glyph = row->glyphs[TEXT_AREA];
9010 struct glyph *end = glyph + row->used[TEXT_AREA];
9011 int x = row->x;
9012 int pt_old = PT - delta;
9014 /* Skip over glyphs not having an object at the start of the row.
9015 These are special glyphs like truncation marks on terminal
9016 frames. */
9017 if (row->displays_text_p)
9018 while (glyph < end
9019 && INTEGERP (glyph->object)
9020 && glyph->charpos < 0)
9022 x += glyph->pixel_width;
9023 ++glyph;
9026 while (glyph < end
9027 && !INTEGERP (glyph->object)
9028 && (!BUFFERP (glyph->object)
9029 || glyph->charpos < pt_old))
9031 x += glyph->pixel_width;
9032 ++glyph;
9035 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9036 w->cursor.x = x;
9037 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9038 w->cursor.y = row->y + dy;
9040 if (w == XWINDOW (selected_window))
9042 if (!row->continued_p
9043 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9044 && row->x == 0)
9046 this_line_buffer = XBUFFER (w->buffer);
9048 CHARPOS (this_line_start_pos)
9049 = MATRIX_ROW_START_CHARPOS (row) + delta;
9050 BYTEPOS (this_line_start_pos)
9051 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9053 CHARPOS (this_line_end_pos)
9054 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9055 BYTEPOS (this_line_end_pos)
9056 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9058 this_line_y = w->cursor.y;
9059 this_line_pixel_height = row->height;
9060 this_line_vpos = w->cursor.vpos;
9061 this_line_start_x = row->x;
9063 else
9064 CHARPOS (this_line_start_pos) = 0;
9069 /* Run window scroll functions, if any, for WINDOW with new window
9070 start STARTP. Sets the window start of WINDOW to that position.
9072 We assume that the window's buffer is really current. */
9074 static INLINE struct text_pos
9075 run_window_scroll_functions (window, startp)
9076 Lisp_Object window;
9077 struct text_pos startp;
9079 struct window *w = XWINDOW (window);
9080 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9082 if (current_buffer != XBUFFER (w->buffer))
9083 abort ();
9085 if (!NILP (Vwindow_scroll_functions))
9087 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9088 make_number (CHARPOS (startp)));
9089 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9090 /* In case the hook functions switch buffers. */
9091 if (current_buffer != XBUFFER (w->buffer))
9092 set_buffer_internal_1 (XBUFFER (w->buffer));
9095 return startp;
9099 /* Modify the desired matrix of window W and W->vscroll so that the
9100 line containing the cursor is fully visible. */
9102 static void
9103 make_cursor_line_fully_visible (w)
9104 struct window *w;
9106 struct glyph_matrix *matrix;
9107 struct glyph_row *row;
9108 int window_height;
9110 /* It's not always possible to find the cursor, e.g, when a window
9111 is full of overlay strings. Don't do anything in that case. */
9112 if (w->cursor.vpos < 0)
9113 return;
9115 matrix = w->desired_matrix;
9116 row = MATRIX_ROW (matrix, w->cursor.vpos);
9118 /* If the cursor row is not partially visible, there's nothing
9119 to do. */
9120 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9121 return;
9123 /* If the row the cursor is in is taller than the window's height,
9124 it's not clear what to do, so do nothing. */
9125 window_height = window_box_height (w);
9126 if (row->height >= window_height)
9127 return;
9129 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9131 int dy = row->height - row->visible_height;
9132 w->vscroll = 0;
9133 w->cursor.y += dy;
9134 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9136 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9138 int dy = - (row->height - row->visible_height);
9139 w->vscroll = dy;
9140 w->cursor.y += dy;
9141 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9144 /* When we change the cursor y-position of the selected window,
9145 change this_line_y as well so that the display optimization for
9146 the cursor line of the selected window in redisplay_internal uses
9147 the correct y-position. */
9148 if (w == XWINDOW (selected_window))
9149 this_line_y = w->cursor.y;
9153 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9154 non-zero means only WINDOW is redisplayed in redisplay_internal.
9155 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9156 in redisplay_window to bring a partially visible line into view in
9157 the case that only the cursor has moved.
9159 Value is
9161 1 if scrolling succeeded
9163 0 if scrolling didn't find point.
9165 -1 if new fonts have been loaded so that we must interrupt
9166 redisplay, adjust glyph matrices, and try again. */
9168 static int
9169 try_scrolling (window, just_this_one_p, scroll_conservatively,
9170 scroll_step, temp_scroll_step)
9171 Lisp_Object window;
9172 int just_this_one_p;
9173 int scroll_conservatively, scroll_step;
9174 int temp_scroll_step;
9176 struct window *w = XWINDOW (window);
9177 struct frame *f = XFRAME (w->frame);
9178 struct text_pos scroll_margin_pos;
9179 struct text_pos pos;
9180 struct text_pos startp;
9181 struct it it;
9182 Lisp_Object window_end;
9183 int this_scroll_margin;
9184 int dy = 0;
9185 int scroll_max;
9186 int rc;
9187 int amount_to_scroll = 0;
9188 Lisp_Object aggressive;
9189 int height;
9191 #if GLYPH_DEBUG
9192 debug_method_add (w, "try_scrolling");
9193 #endif
9195 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9197 /* Compute scroll margin height in pixels. We scroll when point is
9198 within this distance from the top or bottom of the window. */
9199 if (scroll_margin > 0)
9201 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9202 this_scroll_margin *= CANON_Y_UNIT (f);
9204 else
9205 this_scroll_margin = 0;
9207 /* Compute how much we should try to scroll maximally to bring point
9208 into view. */
9209 if (scroll_step || scroll_conservatively || temp_scroll_step)
9210 scroll_max = max (scroll_step,
9211 max (scroll_conservatively, temp_scroll_step));
9212 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9213 || NUMBERP (current_buffer->scroll_up_aggressively))
9214 /* We're trying to scroll because of aggressive scrolling
9215 but no scroll_step is set. Choose an arbitrary one. Maybe
9216 there should be a variable for this. */
9217 scroll_max = 10;
9218 else
9219 scroll_max = 0;
9220 scroll_max *= CANON_Y_UNIT (f);
9222 /* Decide whether we have to scroll down. Start at the window end
9223 and move this_scroll_margin up to find the position of the scroll
9224 margin. */
9225 window_end = Fwindow_end (window, Qt);
9226 CHARPOS (scroll_margin_pos) = XINT (window_end);
9227 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9228 if (this_scroll_margin)
9230 start_display (&it, w, scroll_margin_pos);
9231 move_it_vertically (&it, - this_scroll_margin);
9232 scroll_margin_pos = it.current.pos;
9235 if (PT >= CHARPOS (scroll_margin_pos))
9237 int y0;
9239 /* Point is in the scroll margin at the bottom of the window, or
9240 below. Compute a new window start that makes point visible. */
9242 /* Compute the distance from the scroll margin to PT.
9243 Give up if the distance is greater than scroll_max. */
9244 start_display (&it, w, scroll_margin_pos);
9245 y0 = it.current_y;
9246 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9247 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9249 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9250 end, which is one line below the window. The iterator's
9251 current_y will be same as y0 in that case, but we have to
9252 scroll a line to make PT visible. That's the reason why 1 is
9253 added below. */
9254 dy = 1 + it.current_y - y0;
9256 if (dy > scroll_max)
9257 return 0;
9259 /* Move the window start down. If scrolling conservatively,
9260 move it just enough down to make point visible. If
9261 scroll_step is set, move it down by scroll_step. */
9262 start_display (&it, w, startp);
9264 if (scroll_conservatively)
9265 amount_to_scroll
9266 = max (max (dy, CANON_Y_UNIT (f)),
9267 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9268 else if (scroll_step || temp_scroll_step)
9269 amount_to_scroll = scroll_max;
9270 else
9272 aggressive = current_buffer->scroll_down_aggressively;
9273 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9274 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9275 if (NUMBERP (aggressive))
9276 amount_to_scroll = XFLOATINT (aggressive) * height;
9279 if (amount_to_scroll <= 0)
9280 return 0;
9282 move_it_vertically (&it, amount_to_scroll);
9283 startp = it.current.pos;
9285 else
9287 /* See if point is inside the scroll margin at the top of the
9288 window. */
9289 scroll_margin_pos = startp;
9290 if (this_scroll_margin)
9292 start_display (&it, w, startp);
9293 move_it_vertically (&it, this_scroll_margin);
9294 scroll_margin_pos = it.current.pos;
9297 if (PT < CHARPOS (scroll_margin_pos))
9299 /* Point is in the scroll margin at the top of the window or
9300 above what is displayed in the window. */
9301 int y0;
9303 /* Compute the vertical distance from PT to the scroll
9304 margin position. Give up if distance is greater than
9305 scroll_max. */
9306 SET_TEXT_POS (pos, PT, PT_BYTE);
9307 start_display (&it, w, pos);
9308 y0 = it.current_y;
9309 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9310 it.last_visible_y, -1,
9311 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9312 dy = it.current_y - y0;
9313 if (dy > scroll_max)
9314 return 0;
9316 /* Compute new window start. */
9317 start_display (&it, w, startp);
9319 if (scroll_conservatively)
9320 amount_to_scroll =
9321 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9322 else if (scroll_step || temp_scroll_step)
9323 amount_to_scroll = scroll_max;
9324 else
9326 aggressive = current_buffer->scroll_up_aggressively;
9327 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9328 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9329 if (NUMBERP (aggressive))
9330 amount_to_scroll = XFLOATINT (aggressive) * height;
9333 if (amount_to_scroll <= 0)
9334 return 0;
9336 move_it_vertically (&it, - amount_to_scroll);
9337 startp = it.current.pos;
9341 /* Run window scroll functions. */
9342 startp = run_window_scroll_functions (window, startp);
9344 /* Display the window. Give up if new fonts are loaded, or if point
9345 doesn't appear. */
9346 if (!try_window (window, startp))
9347 rc = -1;
9348 else if (w->cursor.vpos < 0)
9350 clear_glyph_matrix (w->desired_matrix);
9351 rc = 0;
9353 else
9355 /* Maybe forget recorded base line for line number display. */
9356 if (!just_this_one_p
9357 || current_buffer->clip_changed
9358 || BEG_UNCHANGED < CHARPOS (startp))
9359 w->base_line_number = Qnil;
9361 /* If cursor ends up on a partially visible line, shift display
9362 lines up or down. */
9363 make_cursor_line_fully_visible (w);
9364 rc = 1;
9367 return rc;
9371 /* Compute a suitable window start for window W if display of W starts
9372 on a continuation line. Value is non-zero if a new window start
9373 was computed.
9375 The new window start will be computed, based on W's width, starting
9376 from the start of the continued line. It is the start of the
9377 screen line with the minimum distance from the old start W->start. */
9379 static int
9380 compute_window_start_on_continuation_line (w)
9381 struct window *w;
9383 struct text_pos pos, start_pos;
9384 int window_start_changed_p = 0;
9386 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9388 /* If window start is on a continuation line... Window start may be
9389 < BEGV in case there's invisible text at the start of the
9390 buffer (M-x rmail, for example). */
9391 if (CHARPOS (start_pos) > BEGV
9392 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9394 struct it it;
9395 struct glyph_row *row;
9397 /* Handle the case that the window start is out of range. */
9398 if (CHARPOS (start_pos) < BEGV)
9399 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9400 else if (CHARPOS (start_pos) > ZV)
9401 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9403 /* Find the start of the continued line. This should be fast
9404 because scan_buffer is fast (newline cache). */
9405 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9406 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9407 row, DEFAULT_FACE_ID);
9408 reseat_at_previous_visible_line_start (&it);
9410 /* If the line start is "too far" away from the window start,
9411 say it takes too much time to compute a new window start. */
9412 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9413 < XFASTINT (w->height) * XFASTINT (w->width))
9415 int min_distance, distance;
9417 /* Move forward by display lines to find the new window
9418 start. If window width was enlarged, the new start can
9419 be expected to be > the old start. If window width was
9420 decreased, the new window start will be < the old start.
9421 So, we're looking for the display line start with the
9422 minimum distance from the old window start. */
9423 pos = it.current.pos;
9424 min_distance = INFINITY;
9425 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9426 distance < min_distance)
9428 min_distance = distance;
9429 pos = it.current.pos;
9430 move_it_by_lines (&it, 1, 0);
9433 /* Set the window start there. */
9434 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9435 window_start_changed_p = 1;
9439 return window_start_changed_p;
9443 /* Try cursor movement in case text has not changes in window WINDOW,
9444 with window start STARTP. Value is
9446 1 if successful
9448 0 if this method cannot be used
9450 -1 if we know we have to scroll the display. *SCROLL_STEP is
9451 set to 1, under certain circumstances, if we want to scroll as
9452 if scroll-step were set to 1. See the code. */
9454 static int
9455 try_cursor_movement (window, startp, scroll_step)
9456 Lisp_Object window;
9457 struct text_pos startp;
9458 int *scroll_step;
9460 struct window *w = XWINDOW (window);
9461 struct frame *f = XFRAME (w->frame);
9462 int rc = 0;
9464 /* Handle case where text has not changed, only point, and it has
9465 not moved off the frame. */
9466 if (/* Point may be in this window. */
9467 PT >= CHARPOS (startp)
9468 /* Selective display hasn't changed. */
9469 && !current_buffer->clip_changed
9470 /* Function force-mode-line-update is used to force a thorough
9471 redisplay. It sets either windows_or_buffers_changed or
9472 update_mode_lines. So don't take a shortcut here for these
9473 cases. */
9474 && !update_mode_lines
9475 && !windows_or_buffers_changed
9476 /* Can't use this case if highlighting a region. When a
9477 region exists, cursor movement has to do more than just
9478 set the cursor. */
9479 && !(!NILP (Vtransient_mark_mode)
9480 && !NILP (current_buffer->mark_active))
9481 && NILP (w->region_showing)
9482 && NILP (Vshow_trailing_whitespace)
9483 /* Right after splitting windows, last_point may be nil. */
9484 && INTEGERP (w->last_point)
9485 /* This code is not used for mini-buffer for the sake of the case
9486 of redisplaying to replace an echo area message; since in
9487 that case the mini-buffer contents per se are usually
9488 unchanged. This code is of no real use in the mini-buffer
9489 since the handling of this_line_start_pos, etc., in redisplay
9490 handles the same cases. */
9491 && !EQ (window, minibuf_window)
9492 /* When splitting windows or for new windows, it happens that
9493 redisplay is called with a nil window_end_vpos or one being
9494 larger than the window. This should really be fixed in
9495 window.c. I don't have this on my list, now, so we do
9496 approximately the same as the old redisplay code. --gerd. */
9497 && INTEGERP (w->window_end_vpos)
9498 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9499 && (FRAME_WINDOW_P (f)
9500 || !MARKERP (Voverlay_arrow_position)
9501 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9503 int this_scroll_margin;
9504 struct glyph_row *row;
9506 #if GLYPH_DEBUG
9507 debug_method_add (w, "cursor movement");
9508 #endif
9510 /* Scroll if point within this distance from the top or bottom
9511 of the window. This is a pixel value. */
9512 this_scroll_margin = max (0, scroll_margin);
9513 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9514 this_scroll_margin *= CANON_Y_UNIT (f);
9516 /* Start with the row the cursor was displayed during the last
9517 not paused redisplay. Give up if that row is not valid. */
9518 if (w->last_cursor.vpos < 0
9519 || w->last_cursor.vpos >= w->current_matrix->nrows)
9520 rc = -1;
9521 else
9523 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9524 if (row->mode_line_p)
9525 ++row;
9526 if (!row->enabled_p)
9527 rc = -1;
9530 if (rc == 0)
9532 int scroll_p = 0;
9533 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9535 if (PT > XFASTINT (w->last_point))
9537 /* Point has moved forward. */
9538 while (MATRIX_ROW_END_CHARPOS (row) < PT
9539 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9541 xassert (row->enabled_p);
9542 ++row;
9545 /* The end position of a row equals the start position
9546 of the next row. If PT is there, we would rather
9547 display it in the next line. */
9548 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9549 && MATRIX_ROW_END_CHARPOS (row) == PT
9550 && !cursor_row_p (w, row))
9551 ++row;
9553 /* If within the scroll margin, scroll. Note that
9554 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9555 the next line would be drawn, and that
9556 this_scroll_margin can be zero. */
9557 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9558 || PT > MATRIX_ROW_END_CHARPOS (row)
9559 /* Line is completely visible last line in window
9560 and PT is to be set in the next line. */
9561 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9562 && PT == MATRIX_ROW_END_CHARPOS (row)
9563 && !row->ends_at_zv_p
9564 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9565 scroll_p = 1;
9567 else if (PT < XFASTINT (w->last_point))
9569 /* Cursor has to be moved backward. Note that PT >=
9570 CHARPOS (startp) because of the outer
9571 if-statement. */
9572 while (!row->mode_line_p
9573 && (MATRIX_ROW_START_CHARPOS (row) > PT
9574 || (MATRIX_ROW_START_CHARPOS (row) == PT
9575 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9576 && (row->y > this_scroll_margin
9577 || CHARPOS (startp) == BEGV))
9579 xassert (row->enabled_p);
9580 --row;
9583 /* Consider the following case: Window starts at BEGV,
9584 there is invisible, intangible text at BEGV, so that
9585 display starts at some point START > BEGV. It can
9586 happen that we are called with PT somewhere between
9587 BEGV and START. Try to handle that case. */
9588 if (row < w->current_matrix->rows
9589 || row->mode_line_p)
9591 row = w->current_matrix->rows;
9592 if (row->mode_line_p)
9593 ++row;
9596 /* Due to newlines in overlay strings, we may have to
9597 skip forward over overlay strings. */
9598 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9599 && MATRIX_ROW_END_CHARPOS (row) == PT
9600 && !cursor_row_p (w, row))
9601 ++row;
9603 /* If within the scroll margin, scroll. */
9604 if (row->y < this_scroll_margin
9605 && CHARPOS (startp) != BEGV)
9606 scroll_p = 1;
9609 if (PT < MATRIX_ROW_START_CHARPOS (row)
9610 || PT > MATRIX_ROW_END_CHARPOS (row))
9612 /* if PT is not in the glyph row, give up. */
9613 rc = -1;
9615 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9617 if (PT == MATRIX_ROW_END_CHARPOS (row)
9618 && !row->ends_at_zv_p
9619 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9620 rc = -1;
9621 else if (row->height > window_box_height (w))
9623 /* If we end up in a partially visible line, let's
9624 make it fully visible, except when it's taller
9625 than the window, in which case we can't do much
9626 about it. */
9627 *scroll_step = 1;
9628 rc = -1;
9630 else
9632 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9633 try_window (window, startp);
9634 make_cursor_line_fully_visible (w);
9635 rc = 1;
9638 else if (scroll_p)
9639 rc = -1;
9640 else
9642 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9643 rc = 1;
9648 return rc;
9652 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9653 selected_window is redisplayed. */
9655 static void
9656 redisplay_window (window, just_this_one_p)
9657 Lisp_Object window;
9658 int just_this_one_p;
9660 struct window *w = XWINDOW (window);
9661 struct frame *f = XFRAME (w->frame);
9662 struct buffer *buffer = XBUFFER (w->buffer);
9663 struct buffer *old = current_buffer;
9664 struct text_pos lpoint, opoint, startp;
9665 int update_mode_line;
9666 int tem;
9667 struct it it;
9668 /* Record it now because it's overwritten. */
9669 int current_matrix_up_to_date_p = 0;
9670 int temp_scroll_step = 0;
9671 int count = BINDING_STACK_SIZE ();
9672 int rc;
9674 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9675 opoint = lpoint;
9677 /* W must be a leaf window here. */
9678 xassert (!NILP (w->buffer));
9679 #if GLYPH_DEBUG
9680 *w->desired_matrix->method = 0;
9681 #endif
9683 specbind (Qinhibit_point_motion_hooks, Qt);
9685 reconsider_clip_changes (w, buffer);
9687 /* Has the mode line to be updated? */
9688 update_mode_line = (!NILP (w->update_mode_line)
9689 || update_mode_lines
9690 || buffer->clip_changed);
9692 if (MINI_WINDOW_P (w))
9694 if (w == XWINDOW (echo_area_window)
9695 && !NILP (echo_area_buffer[0]))
9697 if (update_mode_line)
9698 /* We may have to update a tty frame's menu bar or a
9699 tool-bar. Example `M-x C-h C-h C-g'. */
9700 goto finish_menu_bars;
9701 else
9702 /* We've already displayed the echo area glyphs in this window. */
9703 goto finish_scroll_bars;
9705 else if (w != XWINDOW (minibuf_window))
9707 /* W is a mini-buffer window, but it's not the currently
9708 active one, so clear it. */
9709 int yb = window_text_bottom_y (w);
9710 struct glyph_row *row;
9711 int y;
9713 for (y = 0, row = w->desired_matrix->rows;
9714 y < yb;
9715 y += row->height, ++row)
9716 blank_row (w, row, y);
9717 goto finish_scroll_bars;
9721 /* Otherwise set up data on this window; select its buffer and point
9722 value. */
9723 /* Really select the buffer, for the sake of buffer-local
9724 variables. */
9725 set_buffer_internal_1 (XBUFFER (w->buffer));
9726 SET_TEXT_POS (opoint, PT, PT_BYTE);
9728 current_matrix_up_to_date_p
9729 = (!NILP (w->window_end_valid)
9730 && !current_buffer->clip_changed
9731 && XFASTINT (w->last_modified) >= MODIFF
9732 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9734 /* When windows_or_buffers_changed is non-zero, we can't rely on
9735 the window end being valid, so set it to nil there. */
9736 if (windows_or_buffers_changed)
9738 /* If window starts on a continuation line, maybe adjust the
9739 window start in case the window's width changed. */
9740 if (XMARKER (w->start)->buffer == current_buffer)
9741 compute_window_start_on_continuation_line (w);
9743 w->window_end_valid = Qnil;
9746 /* Some sanity checks. */
9747 CHECK_WINDOW_END (w);
9748 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9749 abort ();
9750 if (BYTEPOS (opoint) < CHARPOS (opoint))
9751 abort ();
9753 /* If %c is in mode line, update it if needed. */
9754 if (!NILP (w->column_number_displayed)
9755 /* This alternative quickly identifies a common case
9756 where no change is needed. */
9757 && !(PT == XFASTINT (w->last_point)
9758 && XFASTINT (w->last_modified) >= MODIFF
9759 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9760 && XFASTINT (w->column_number_displayed) != current_column ())
9761 update_mode_line = 1;
9763 /* Count number of windows showing the selected buffer. An indirect
9764 buffer counts as its base buffer. */
9765 if (!just_this_one_p)
9767 struct buffer *current_base, *window_base;
9768 current_base = current_buffer;
9769 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9770 if (current_base->base_buffer)
9771 current_base = current_base->base_buffer;
9772 if (window_base->base_buffer)
9773 window_base = window_base->base_buffer;
9774 if (current_base == window_base)
9775 buffer_shared++;
9778 /* Point refers normally to the selected window. For any other
9779 window, set up appropriate value. */
9780 if (!EQ (window, selected_window))
9782 int new_pt = XMARKER (w->pointm)->charpos;
9783 int new_pt_byte = marker_byte_position (w->pointm);
9784 if (new_pt < BEGV)
9786 new_pt = BEGV;
9787 new_pt_byte = BEGV_BYTE;
9788 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9790 else if (new_pt > (ZV - 1))
9792 new_pt = ZV;
9793 new_pt_byte = ZV_BYTE;
9794 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9797 /* We don't use SET_PT so that the point-motion hooks don't run. */
9798 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9801 /* If any of the character widths specified in the display table
9802 have changed, invalidate the width run cache. It's true that
9803 this may be a bit late to catch such changes, but the rest of
9804 redisplay goes (non-fatally) haywire when the display table is
9805 changed, so why should we worry about doing any better? */
9806 if (current_buffer->width_run_cache)
9808 struct Lisp_Char_Table *disptab = buffer_display_table ();
9810 if (! disptab_matches_widthtab (disptab,
9811 XVECTOR (current_buffer->width_table)))
9813 invalidate_region_cache (current_buffer,
9814 current_buffer->width_run_cache,
9815 BEG, Z);
9816 recompute_width_table (current_buffer, disptab);
9820 /* If window-start is screwed up, choose a new one. */
9821 if (XMARKER (w->start)->buffer != current_buffer)
9822 goto recenter;
9824 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9826 /* If someone specified a new starting point but did not insist,
9827 check whether it can be used. */
9828 if (!NILP (w->optional_new_start)
9829 && CHARPOS (startp) >= BEGV
9830 && CHARPOS (startp) <= ZV)
9832 w->optional_new_start = Qnil;
9833 start_display (&it, w, startp);
9834 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9835 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9836 if (IT_CHARPOS (it) == PT)
9837 w->force_start = Qt;
9840 /* Handle case where place to start displaying has been specified,
9841 unless the specified location is outside the accessible range. */
9842 if (!NILP (w->force_start)
9843 || w->frozen_window_start_p)
9845 w->force_start = Qnil;
9846 w->vscroll = 0;
9847 w->window_end_valid = Qnil;
9849 /* Forget any recorded base line for line number display. */
9850 if (!current_matrix_up_to_date_p
9851 || current_buffer->clip_changed)
9852 w->base_line_number = Qnil;
9854 /* Redisplay the mode line. Select the buffer properly for that.
9855 Also, run the hook window-scroll-functions
9856 because we have scrolled. */
9857 /* Note, we do this after clearing force_start because
9858 if there's an error, it is better to forget about force_start
9859 than to get into an infinite loop calling the hook functions
9860 and having them get more errors. */
9861 if (!update_mode_line
9862 || ! NILP (Vwindow_scroll_functions))
9864 update_mode_line = 1;
9865 w->update_mode_line = Qt;
9866 startp = run_window_scroll_functions (window, startp);
9869 w->last_modified = make_number (0);
9870 w->last_overlay_modified = make_number (0);
9871 if (CHARPOS (startp) < BEGV)
9872 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9873 else if (CHARPOS (startp) > ZV)
9874 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9876 /* Redisplay, then check if cursor has been set during the
9877 redisplay. Give up if new fonts were loaded. */
9878 if (!try_window (window, startp))
9880 w->force_start = Qt;
9881 clear_glyph_matrix (w->desired_matrix);
9882 goto finish_scroll_bars;
9885 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9887 /* If point does not appear, try to move point so it does
9888 appear. The desired matrix has been built above, so we
9889 can use it here. */
9890 int window_height;
9891 struct glyph_row *row;
9893 window_height = window_box_height (w) / 2;
9894 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9895 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9896 ++row;
9898 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9899 MATRIX_ROW_START_BYTEPOS (row));
9901 if (w != XWINDOW (selected_window))
9902 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9903 else if (current_buffer == old)
9904 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9906 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9908 /* If we are highlighting the region, then we just changed
9909 the region, so redisplay to show it. */
9910 if (!NILP (Vtransient_mark_mode)
9911 && !NILP (current_buffer->mark_active))
9913 clear_glyph_matrix (w->desired_matrix);
9914 if (!try_window (window, startp))
9915 goto finish_scroll_bars;
9919 make_cursor_line_fully_visible (w);
9920 #if GLYPH_DEBUG
9921 debug_method_add (w, "forced window start");
9922 #endif
9923 goto done;
9926 /* Handle case where text has not changed, only point, and it has
9927 not moved off the frame. */
9928 if (current_matrix_up_to_date_p
9929 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9930 rc != 0))
9932 if (rc == -1)
9933 goto try_to_scroll;
9934 else
9935 goto done;
9937 /* If current starting point was originally the beginning of a line
9938 but no longer is, find a new starting point. */
9939 else if (!NILP (w->start_at_line_beg)
9940 && !(CHARPOS (startp) <= BEGV
9941 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9943 #if GLYPH_DEBUG
9944 debug_method_add (w, "recenter 1");
9945 #endif
9946 goto recenter;
9949 /* Try scrolling with try_window_id. */
9950 else if (/* Windows and buffers haven't changed. */
9951 !windows_or_buffers_changed
9952 /* Window must be either use window-based redisplay or
9953 be full width. */
9954 && (FRAME_WINDOW_P (f)
9955 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9956 && !MINI_WINDOW_P (w)
9957 /* Point is not known NOT to appear in window. */
9958 && PT >= CHARPOS (startp)
9959 && XFASTINT (w->last_modified)
9960 /* Window is not hscrolled. */
9961 && XFASTINT (w->hscroll) == 0
9962 /* Selective display has not changed. */
9963 && !current_buffer->clip_changed
9964 /* Current matrix is up to date. */
9965 && !NILP (w->window_end_valid)
9966 /* Can't use this case if highlighting a region because
9967 a cursor movement will do more than just set the cursor. */
9968 && !(!NILP (Vtransient_mark_mode)
9969 && !NILP (current_buffer->mark_active))
9970 && NILP (w->region_showing)
9971 && NILP (Vshow_trailing_whitespace)
9972 /* Overlay arrow position and string not changed. */
9973 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9974 && EQ (last_arrow_string, Voverlay_arrow_string)
9975 /* Value is > 0 if update has been done, it is -1 if we
9976 know that the same window start will not work. It is 0
9977 if unsuccessful for some other reason. */
9978 && (tem = try_window_id (w)) != 0)
9980 #if GLYPH_DEBUG
9981 debug_method_add (w, "try_window_id %d", tem);
9982 #endif
9984 if (fonts_changed_p)
9985 goto finish_scroll_bars;
9986 if (tem > 0)
9987 goto done;
9989 /* Otherwise try_window_id has returned -1 which means that we
9990 don't want the alternative below this comment to execute. */
9992 else if (CHARPOS (startp) >= BEGV
9993 && CHARPOS (startp) <= ZV
9994 && PT >= CHARPOS (startp)
9995 && (CHARPOS (startp) < ZV
9996 /* Avoid starting at end of buffer. */
9997 || CHARPOS (startp) == BEGV
9998 || (XFASTINT (w->last_modified) >= MODIFF
9999 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10001 #if GLYPH_DEBUG
10002 debug_method_add (w, "same window start");
10003 #endif
10005 /* Try to redisplay starting at same place as before.
10006 If point has not moved off frame, accept the results. */
10007 if (!current_matrix_up_to_date_p
10008 /* Don't use try_window_reusing_current_matrix in this case
10009 because a window scroll function can have changed the
10010 buffer. */
10011 || !NILP (Vwindow_scroll_functions)
10012 || MINI_WINDOW_P (w)
10013 || !try_window_reusing_current_matrix (w))
10015 IF_DEBUG (debug_method_add (w, "1"));
10016 try_window (window, startp);
10019 if (fonts_changed_p)
10020 goto finish_scroll_bars;
10022 if (w->cursor.vpos >= 0)
10024 if (!just_this_one_p
10025 || current_buffer->clip_changed
10026 || BEG_UNCHANGED < CHARPOS (startp))
10027 /* Forget any recorded base line for line number display. */
10028 w->base_line_number = Qnil;
10030 make_cursor_line_fully_visible (w);
10031 goto done;
10033 else
10034 clear_glyph_matrix (w->desired_matrix);
10037 try_to_scroll:
10039 w->last_modified = make_number (0);
10040 w->last_overlay_modified = make_number (0);
10042 /* Redisplay the mode line. Select the buffer properly for that. */
10043 if (!update_mode_line)
10045 update_mode_line = 1;
10046 w->update_mode_line = Qt;
10049 /* Try to scroll by specified few lines. */
10050 if ((scroll_conservatively
10051 || scroll_step
10052 || temp_scroll_step
10053 || NUMBERP (current_buffer->scroll_up_aggressively)
10054 || NUMBERP (current_buffer->scroll_down_aggressively))
10055 && !current_buffer->clip_changed
10056 && CHARPOS (startp) >= BEGV
10057 && CHARPOS (startp) <= ZV)
10059 /* The function returns -1 if new fonts were loaded, 1 if
10060 successful, 0 if not successful. */
10061 int rc = try_scrolling (window, just_this_one_p,
10062 scroll_conservatively,
10063 scroll_step,
10064 temp_scroll_step);
10065 if (rc > 0)
10066 goto done;
10067 else if (rc < 0)
10068 goto finish_scroll_bars;
10071 /* Finally, just choose place to start which centers point */
10073 recenter:
10075 #if GLYPH_DEBUG
10076 debug_method_add (w, "recenter");
10077 #endif
10079 /* w->vscroll = 0; */
10081 /* Forget any previously recorded base line for line number display. */
10082 if (!current_matrix_up_to_date_p
10083 || current_buffer->clip_changed)
10084 w->base_line_number = Qnil;
10086 /* Move backward half the height of the window. */
10087 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10088 it.current_y = it.last_visible_y;
10089 move_it_vertically_backward (&it, it.last_visible_y / 2);
10090 xassert (IT_CHARPOS (it) >= BEGV);
10092 /* The function move_it_vertically_backward may move over more
10093 than the specified y-distance. If it->w is small, e.g. a
10094 mini-buffer window, we may end up in front of the window's
10095 display area. Start displaying at the start of the line
10096 containing PT in this case. */
10097 if (it.current_y <= 0)
10099 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10100 move_it_vertically (&it, 0);
10101 xassert (IT_CHARPOS (it) <= PT);
10102 it.current_y = 0;
10105 it.current_x = it.hpos = 0;
10107 /* Set startp here explicitly in case that helps avoid an infinite loop
10108 in case the window-scroll-functions functions get errors. */
10109 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10111 /* Run scroll hooks. */
10112 startp = run_window_scroll_functions (window, it.current.pos);
10114 /* Redisplay the window. */
10115 if (!current_matrix_up_to_date_p
10116 || windows_or_buffers_changed
10117 /* Don't use try_window_reusing_current_matrix in this case
10118 because it can have changed the buffer. */
10119 || !NILP (Vwindow_scroll_functions)
10120 || !just_this_one_p
10121 || MINI_WINDOW_P (w)
10122 || !try_window_reusing_current_matrix (w))
10123 try_window (window, startp);
10125 /* If new fonts have been loaded (due to fontsets), give up. We
10126 have to start a new redisplay since we need to re-adjust glyph
10127 matrices. */
10128 if (fonts_changed_p)
10129 goto finish_scroll_bars;
10131 /* If cursor did not appear assume that the middle of the window is
10132 in the first line of the window. Do it again with the next line.
10133 (Imagine a window of height 100, displaying two lines of height
10134 60. Moving back 50 from it->last_visible_y will end in the first
10135 line.) */
10136 if (w->cursor.vpos < 0)
10138 if (!NILP (w->window_end_valid)
10139 && PT >= Z - XFASTINT (w->window_end_pos))
10141 clear_glyph_matrix (w->desired_matrix);
10142 move_it_by_lines (&it, 1, 0);
10143 try_window (window, it.current.pos);
10145 else if (PT < IT_CHARPOS (it))
10147 clear_glyph_matrix (w->desired_matrix);
10148 move_it_by_lines (&it, -1, 0);
10149 try_window (window, it.current.pos);
10151 else
10153 /* Not much we can do about it. */
10157 /* Consider the following case: Window starts at BEGV, there is
10158 invisible, intangible text at BEGV, so that display starts at
10159 some point START > BEGV. It can happen that we are called with
10160 PT somewhere between BEGV and START. Try to handle that case. */
10161 if (w->cursor.vpos < 0)
10163 struct glyph_row *row = w->current_matrix->rows;
10164 if (row->mode_line_p)
10165 ++row;
10166 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10169 make_cursor_line_fully_visible (w);
10171 done:
10173 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10174 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10175 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10176 ? Qt : Qnil);
10178 /* Display the mode line, if we must. */
10179 if ((update_mode_line
10180 /* If window not full width, must redo its mode line
10181 if (a) the window to its side is being redone and
10182 (b) we do a frame-based redisplay. This is a consequence
10183 of how inverted lines are drawn in frame-based redisplay. */
10184 || (!just_this_one_p
10185 && !FRAME_WINDOW_P (f)
10186 && !WINDOW_FULL_WIDTH_P (w))
10187 /* Line number to display. */
10188 || INTEGERP (w->base_line_pos)
10189 /* Column number is displayed and different from the one displayed. */
10190 || (!NILP (w->column_number_displayed)
10191 && XFASTINT (w->column_number_displayed) != current_column ()))
10192 /* This means that the window has a mode line. */
10193 && (WINDOW_WANTS_MODELINE_P (w)
10194 || WINDOW_WANTS_HEADER_LINE_P (w)))
10196 Lisp_Object old_selected_frame;
10198 old_selected_frame = selected_frame;
10200 XSETFRAME (selected_frame, f);
10201 display_mode_lines (w);
10202 selected_frame = old_selected_frame;
10204 /* If mode line height has changed, arrange for a thorough
10205 immediate redisplay using the correct mode line height. */
10206 if (WINDOW_WANTS_MODELINE_P (w)
10207 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10209 fonts_changed_p = 1;
10210 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10211 = DESIRED_MODE_LINE_HEIGHT (w);
10214 /* If top line height has changed, arrange for a thorough
10215 immediate redisplay using the correct mode line height. */
10216 if (WINDOW_WANTS_HEADER_LINE_P (w)
10217 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10219 fonts_changed_p = 1;
10220 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10221 = DESIRED_HEADER_LINE_HEIGHT (w);
10224 if (fonts_changed_p)
10225 goto finish_scroll_bars;
10228 if (!line_number_displayed
10229 && !BUFFERP (w->base_line_pos))
10231 w->base_line_pos = Qnil;
10232 w->base_line_number = Qnil;
10235 finish_menu_bars:
10237 /* When we reach a frame's selected window, redo the frame's menu bar. */
10238 if (update_mode_line
10239 && EQ (FRAME_SELECTED_WINDOW (f), window))
10241 int redisplay_menu_p = 0;
10243 if (FRAME_WINDOW_P (f))
10245 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10246 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10247 #else
10248 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10249 #endif
10251 else
10252 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10254 if (redisplay_menu_p)
10255 display_menu_bar (w);
10257 #ifdef HAVE_WINDOW_SYSTEM
10258 if (WINDOWP (f->tool_bar_window)
10259 && (FRAME_TOOL_BAR_LINES (f) > 0
10260 || auto_resize_tool_bars_p))
10261 redisplay_tool_bar (f);
10262 #endif
10265 finish_scroll_bars:
10267 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10269 int start, end, whole;
10271 /* Calculate the start and end positions for the current window.
10272 At some point, it would be nice to choose between scrollbars
10273 which reflect the whole buffer size, with special markers
10274 indicating narrowing, and scrollbars which reflect only the
10275 visible region.
10277 Note that mini-buffers sometimes aren't displaying any text. */
10278 if (!MINI_WINDOW_P (w)
10279 || (w == XWINDOW (minibuf_window)
10280 && NILP (echo_area_buffer[0])))
10282 whole = ZV - BEGV;
10283 start = marker_position (w->start) - BEGV;
10284 /* I don't think this is guaranteed to be right. For the
10285 moment, we'll pretend it is. */
10286 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10288 if (end < start)
10289 end = start;
10290 if (whole < (end - start))
10291 whole = end - start;
10293 else
10294 start = end = whole = 0;
10296 /* Indicate what this scroll bar ought to be displaying now. */
10297 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10299 /* Note that we actually used the scroll bar attached to this
10300 window, so it shouldn't be deleted at the end of redisplay. */
10301 redeem_scroll_bar_hook (w);
10304 /* Restore current_buffer and value of point in it. */
10305 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10306 set_buffer_internal_1 (old);
10307 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10309 unbind_to (count, Qnil);
10313 /* Build the complete desired matrix of WINDOW with a window start
10314 buffer position POS. Value is non-zero if successful. It is zero
10315 if fonts were loaded during redisplay which makes re-adjusting
10316 glyph matrices necessary. */
10319 try_window (window, pos)
10320 Lisp_Object window;
10321 struct text_pos pos;
10323 struct window *w = XWINDOW (window);
10324 struct it it;
10325 struct glyph_row *last_text_row = NULL;
10327 /* Make POS the new window start. */
10328 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10330 /* Mark cursor position as unknown. No overlay arrow seen. */
10331 w->cursor.vpos = -1;
10332 overlay_arrow_seen = 0;
10334 /* Initialize iterator and info to start at POS. */
10335 start_display (&it, w, pos);
10337 /* Display all lines of W. */
10338 while (it.current_y < it.last_visible_y)
10340 if (display_line (&it))
10341 last_text_row = it.glyph_row - 1;
10342 if (fonts_changed_p)
10343 return 0;
10346 /* If bottom moved off end of frame, change mode line percentage. */
10347 if (XFASTINT (w->window_end_pos) <= 0
10348 && Z != IT_CHARPOS (it))
10349 w->update_mode_line = Qt;
10351 /* Set window_end_pos to the offset of the last character displayed
10352 on the window from the end of current_buffer. Set
10353 window_end_vpos to its row number. */
10354 if (last_text_row)
10356 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10357 w->window_end_bytepos
10358 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10359 w->window_end_pos
10360 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10361 w->window_end_vpos
10362 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10363 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10364 ->displays_text_p);
10366 else
10368 w->window_end_bytepos = 0;
10369 w->window_end_pos = w->window_end_vpos = make_number (0);
10372 /* But that is not valid info until redisplay finishes. */
10373 w->window_end_valid = Qnil;
10374 return 1;
10379 /************************************************************************
10380 Window redisplay reusing current matrix when buffer has not changed
10381 ************************************************************************/
10383 /* Try redisplay of window W showing an unchanged buffer with a
10384 different window start than the last time it was displayed by
10385 reusing its current matrix. Value is non-zero if successful.
10386 W->start is the new window start. */
10388 static int
10389 try_window_reusing_current_matrix (w)
10390 struct window *w;
10392 struct frame *f = XFRAME (w->frame);
10393 struct glyph_row *row, *bottom_row;
10394 struct it it;
10395 struct run run;
10396 struct text_pos start, new_start;
10397 int nrows_scrolled, i;
10398 struct glyph_row *last_text_row;
10399 struct glyph_row *last_reused_text_row;
10400 struct glyph_row *start_row;
10401 int start_vpos, min_y, max_y;
10403 if (/* This function doesn't handle terminal frames. */
10404 !FRAME_WINDOW_P (f)
10405 /* Don't try to reuse the display if windows have been split
10406 or such. */
10407 || windows_or_buffers_changed)
10408 return 0;
10410 /* Can't do this if region may have changed. */
10411 if ((!NILP (Vtransient_mark_mode)
10412 && !NILP (current_buffer->mark_active))
10413 || !NILP (w->region_showing)
10414 || !NILP (Vshow_trailing_whitespace))
10415 return 0;
10417 /* If top-line visibility has changed, give up. */
10418 if (WINDOW_WANTS_HEADER_LINE_P (w)
10419 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10420 return 0;
10422 /* Give up if old or new display is scrolled vertically. We could
10423 make this function handle this, but right now it doesn't. */
10424 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10425 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10426 return 0;
10428 /* The variable new_start now holds the new window start. The old
10429 start `start' can be determined from the current matrix. */
10430 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10431 start = start_row->start.pos;
10432 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10434 /* Clear the desired matrix for the display below. */
10435 clear_glyph_matrix (w->desired_matrix);
10437 if (CHARPOS (new_start) <= CHARPOS (start))
10439 int first_row_y;
10441 /* Don't use this method if the display starts with an ellipsis
10442 displayed for invisible text. It's not easy to handle that case
10443 below, and it's certainly not worth the effort since this is
10444 not a frequent case. */
10445 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10446 return 0;
10448 IF_DEBUG (debug_method_add (w, "twu1"));
10450 /* Display up to a row that can be reused. The variable
10451 last_text_row is set to the last row displayed that displays
10452 text. Note that it.vpos == 0 if or if not there is a
10453 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10454 start_display (&it, w, new_start);
10455 first_row_y = it.current_y;
10456 w->cursor.vpos = -1;
10457 last_text_row = last_reused_text_row = NULL;
10459 while (it.current_y < it.last_visible_y
10460 && IT_CHARPOS (it) < CHARPOS (start)
10461 && !fonts_changed_p)
10462 if (display_line (&it))
10463 last_text_row = it.glyph_row - 1;
10465 /* A value of current_y < last_visible_y means that we stopped
10466 at the previous window start, which in turn means that we
10467 have at least one reusable row. */
10468 if (it.current_y < it.last_visible_y)
10470 /* IT.vpos always starts from 0; it counts text lines. */
10471 nrows_scrolled = it.vpos;
10473 /* Find PT if not already found in the lines displayed. */
10474 if (w->cursor.vpos < 0)
10476 int dy = it.current_y - first_row_y;
10478 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10479 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10481 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10482 && PT < MATRIX_ROW_END_CHARPOS (row))
10484 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10485 dy, nrows_scrolled);
10486 break;
10489 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10490 break;
10492 ++row;
10495 /* Give up if point was not found. This shouldn't
10496 happen often; not more often than with try_window
10497 itself. */
10498 if (w->cursor.vpos < 0)
10500 clear_glyph_matrix (w->desired_matrix);
10501 return 0;
10505 /* Scroll the display. Do it before the current matrix is
10506 changed. The problem here is that update has not yet
10507 run, i.e. part of the current matrix is not up to date.
10508 scroll_run_hook will clear the cursor, and use the
10509 current matrix to get the height of the row the cursor is
10510 in. */
10511 run.current_y = first_row_y;
10512 run.desired_y = it.current_y;
10513 run.height = it.last_visible_y - it.current_y;
10515 if (run.height > 0 && run.current_y != run.desired_y)
10517 update_begin (f);
10518 rif->update_window_begin_hook (w);
10519 rif->clear_mouse_face (w);
10520 rif->scroll_run_hook (w, &run);
10521 rif->update_window_end_hook (w, 0, 0);
10522 update_end (f);
10525 /* Shift current matrix down by nrows_scrolled lines. */
10526 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10527 rotate_matrix (w->current_matrix,
10528 start_vpos,
10529 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10530 nrows_scrolled);
10532 /* Disable lines that must be updated. */
10533 for (i = 0; i < it.vpos; ++i)
10534 (start_row + i)->enabled_p = 0;
10536 /* Re-compute Y positions. */
10537 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10538 max_y = it.last_visible_y;
10539 for (row = start_row + nrows_scrolled;
10540 row < bottom_row;
10541 ++row)
10543 row->y = it.current_y;
10545 if (row->y < min_y)
10546 row->visible_height = row->height - (min_y - row->y);
10547 else if (row->y + row->height > max_y)
10548 row->visible_height
10549 = row->height - (row->y + row->height - max_y);
10550 else
10551 row->visible_height = row->height;
10553 it.current_y += row->height;
10555 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10556 last_reused_text_row = row;
10557 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10558 break;
10561 /* Disable lines in the current matrix which are now
10562 below the window. */
10563 for (++row; row < bottom_row; ++row)
10564 row->enabled_p = 0;
10567 /* Update window_end_pos etc.; last_reused_text_row is the last
10568 reused row from the current matrix containing text, if any.
10569 The value of last_text_row is the last displayed line
10570 containing text. */
10571 if (last_reused_text_row)
10573 w->window_end_bytepos
10574 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10575 w->window_end_pos
10576 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10577 w->window_end_vpos
10578 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10579 w->current_matrix));
10581 else if (last_text_row)
10583 w->window_end_bytepos
10584 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10585 w->window_end_pos
10586 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10587 w->window_end_vpos
10588 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10590 else
10592 /* This window must be completely empty. */
10593 w->window_end_bytepos = 0;
10594 w->window_end_pos = w->window_end_vpos = make_number (0);
10596 w->window_end_valid = Qnil;
10598 /* Update hint: don't try scrolling again in update_window. */
10599 w->desired_matrix->no_scrolling_p = 1;
10601 #if GLYPH_DEBUG
10602 debug_method_add (w, "try_window_reusing_current_matrix 1");
10603 #endif
10604 return 1;
10606 else if (CHARPOS (new_start) > CHARPOS (start))
10608 struct glyph_row *pt_row, *row;
10609 struct glyph_row *first_reusable_row;
10610 struct glyph_row *first_row_to_display;
10611 int dy;
10612 int yb = window_text_bottom_y (w);
10614 /* Find the row starting at new_start, if there is one. Don't
10615 reuse a partially visible line at the end. */
10616 first_reusable_row = start_row;
10617 while (first_reusable_row->enabled_p
10618 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10619 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10620 < CHARPOS (new_start)))
10621 ++first_reusable_row;
10623 /* Give up if there is no row to reuse. */
10624 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10625 || !first_reusable_row->enabled_p
10626 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10627 != CHARPOS (new_start)))
10628 return 0;
10630 /* We can reuse fully visible rows beginning with
10631 first_reusable_row to the end of the window. Set
10632 first_row_to_display to the first row that cannot be reused.
10633 Set pt_row to the row containing point, if there is any. */
10634 pt_row = NULL;
10635 for (first_row_to_display = first_reusable_row;
10636 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10637 ++first_row_to_display)
10639 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10640 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10641 pt_row = first_row_to_display;
10644 /* Start displaying at the start of first_row_to_display. */
10645 xassert (first_row_to_display->y < yb);
10646 init_to_row_start (&it, w, first_row_to_display);
10648 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10649 - start_vpos);
10650 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10651 - nrows_scrolled);
10652 it.current_y = (first_row_to_display->y - first_reusable_row->y
10653 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10655 /* Display lines beginning with first_row_to_display in the
10656 desired matrix. Set last_text_row to the last row displayed
10657 that displays text. */
10658 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10659 if (pt_row == NULL)
10660 w->cursor.vpos = -1;
10661 last_text_row = NULL;
10662 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10663 if (display_line (&it))
10664 last_text_row = it.glyph_row - 1;
10666 /* Give up If point isn't in a row displayed or reused. */
10667 if (w->cursor.vpos < 0)
10669 clear_glyph_matrix (w->desired_matrix);
10670 return 0;
10673 /* If point is in a reused row, adjust y and vpos of the cursor
10674 position. */
10675 if (pt_row)
10677 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10678 w->current_matrix);
10679 w->cursor.y -= first_reusable_row->y;
10682 /* Scroll the display. */
10683 run.current_y = first_reusable_row->y;
10684 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10685 run.height = it.last_visible_y - run.current_y;
10686 dy = run.current_y - run.desired_y;
10688 if (run.height)
10690 struct frame *f = XFRAME (WINDOW_FRAME (w));
10691 update_begin (f);
10692 rif->update_window_begin_hook (w);
10693 rif->clear_mouse_face (w);
10694 rif->scroll_run_hook (w, &run);
10695 rif->update_window_end_hook (w, 0, 0);
10696 update_end (f);
10699 /* Adjust Y positions of reused rows. */
10700 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10701 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10702 max_y = it.last_visible_y;
10703 for (row = first_reusable_row; row < first_row_to_display; ++row)
10705 row->y -= dy;
10706 if (row->y < min_y)
10707 row->visible_height = row->height - (min_y - row->y);
10708 else if (row->y + row->height > max_y)
10709 row->visible_height
10710 = row->height - (row->y + row->height - max_y);
10711 else
10712 row->visible_height = row->height;
10715 /* Scroll the current matrix. */
10716 xassert (nrows_scrolled > 0);
10717 rotate_matrix (w->current_matrix,
10718 start_vpos,
10719 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10720 -nrows_scrolled);
10722 /* Disable rows not reused. */
10723 for (row -= nrows_scrolled; row < bottom_row; ++row)
10724 row->enabled_p = 0;
10726 /* Adjust window end. A null value of last_text_row means that
10727 the window end is in reused rows which in turn means that
10728 only its vpos can have changed. */
10729 if (last_text_row)
10731 w->window_end_bytepos
10732 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10733 w->window_end_pos
10734 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10735 w->window_end_vpos
10736 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10738 else
10740 w->window_end_vpos
10741 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10744 w->window_end_valid = Qnil;
10745 w->desired_matrix->no_scrolling_p = 1;
10747 #if GLYPH_DEBUG
10748 debug_method_add (w, "try_window_reusing_current_matrix 2");
10749 #endif
10750 return 1;
10753 return 0;
10758 /************************************************************************
10759 Window redisplay reusing current matrix when buffer has changed
10760 ************************************************************************/
10762 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10763 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10764 int *, int *));
10765 static struct glyph_row *
10766 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10767 struct glyph_row *));
10770 /* Return the last row in MATRIX displaying text. If row START is
10771 non-null, start searching with that row. IT gives the dimensions
10772 of the display. Value is null if matrix is empty; otherwise it is
10773 a pointer to the row found. */
10775 static struct glyph_row *
10776 find_last_row_displaying_text (matrix, it, start)
10777 struct glyph_matrix *matrix;
10778 struct it *it;
10779 struct glyph_row *start;
10781 struct glyph_row *row, *row_found;
10783 /* Set row_found to the last row in IT->w's current matrix
10784 displaying text. The loop looks funny but think of partially
10785 visible lines. */
10786 row_found = NULL;
10787 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10788 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10790 xassert (row->enabled_p);
10791 row_found = row;
10792 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10793 break;
10794 ++row;
10797 return row_found;
10801 /* Return the last row in the current matrix of W that is not affected
10802 by changes at the start of current_buffer that occurred since the
10803 last time W was redisplayed. Value is null if no such row exists.
10805 The global variable beg_unchanged has to contain the number of
10806 bytes unchanged at the start of current_buffer. BEG +
10807 beg_unchanged is the buffer position of the first changed byte in
10808 current_buffer. Characters at positions < BEG + beg_unchanged are
10809 at the same buffer positions as they were when the current matrix
10810 was built. */
10812 static struct glyph_row *
10813 find_last_unchanged_at_beg_row (w)
10814 struct window *w;
10816 int first_changed_pos = BEG + BEG_UNCHANGED;
10817 struct glyph_row *row;
10818 struct glyph_row *row_found = NULL;
10819 int yb = window_text_bottom_y (w);
10821 /* Find the last row displaying unchanged text. */
10822 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10823 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10824 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10826 if (/* If row ends before first_changed_pos, it is unchanged,
10827 except in some case. */
10828 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10829 /* When row ends in ZV and we write at ZV it is not
10830 unchanged. */
10831 && !row->ends_at_zv_p
10832 /* When first_changed_pos is the end of a continued line,
10833 row is not unchanged because it may be no longer
10834 continued. */
10835 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10836 && row->continued_p))
10837 row_found = row;
10839 /* Stop if last visible row. */
10840 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10841 break;
10843 ++row;
10846 return row_found;
10850 /* Find the first glyph row in the current matrix of W that is not
10851 affected by changes at the end of current_buffer since the last
10852 time the window was redisplayed. Return in *DELTA the number of
10853 chars by which buffer positions in unchanged text at the end of
10854 current_buffer must be adjusted. Return in *DELTA_BYTES the
10855 corresponding number of bytes. Value is null if no such row
10856 exists, i.e. all rows are affected by changes. */
10858 static struct glyph_row *
10859 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10860 struct window *w;
10861 int *delta, *delta_bytes;
10863 struct glyph_row *row;
10864 struct glyph_row *row_found = NULL;
10866 *delta = *delta_bytes = 0;
10868 /* Display must not have been paused, otherwise the current matrix
10869 is not up to date. */
10870 if (NILP (w->window_end_valid))
10871 abort ();
10873 /* A value of window_end_pos >= END_UNCHANGED means that the window
10874 end is in the range of changed text. If so, there is no
10875 unchanged row at the end of W's current matrix. */
10876 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10877 return NULL;
10879 /* Set row to the last row in W's current matrix displaying text. */
10880 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10882 /* If matrix is entirely empty, no unchanged row exists. */
10883 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10885 /* The value of row is the last glyph row in the matrix having a
10886 meaningful buffer position in it. The end position of row
10887 corresponds to window_end_pos. This allows us to translate
10888 buffer positions in the current matrix to current buffer
10889 positions for characters not in changed text. */
10890 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10891 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10892 int last_unchanged_pos, last_unchanged_pos_old;
10893 struct glyph_row *first_text_row
10894 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10896 *delta = Z - Z_old;
10897 *delta_bytes = Z_BYTE - Z_BYTE_old;
10899 /* Set last_unchanged_pos to the buffer position of the last
10900 character in the buffer that has not been changed. Z is the
10901 index + 1 of the last byte in current_buffer, i.e. by
10902 subtracting end_unchanged we get the index of the last
10903 unchanged character, and we have to add BEG to get its buffer
10904 position. */
10905 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10906 last_unchanged_pos_old = last_unchanged_pos - *delta;
10908 /* Search backward from ROW for a row displaying a line that
10909 starts at a minimum position >= last_unchanged_pos_old. */
10910 for (; row > first_text_row; --row)
10912 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10913 abort ();
10915 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10916 row_found = row;
10920 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10921 abort ();
10923 return row_found;
10927 /* Make sure that glyph rows in the current matrix of window W
10928 reference the same glyph memory as corresponding rows in the
10929 frame's frame matrix. This function is called after scrolling W's
10930 current matrix on a terminal frame in try_window_id and
10931 try_window_reusing_current_matrix. */
10933 static void
10934 sync_frame_with_window_matrix_rows (w)
10935 struct window *w;
10937 struct frame *f = XFRAME (w->frame);
10938 struct glyph_row *window_row, *window_row_end, *frame_row;
10940 /* Preconditions: W must be a leaf window and full-width. Its frame
10941 must have a frame matrix. */
10942 xassert (NILP (w->hchild) && NILP (w->vchild));
10943 xassert (WINDOW_FULL_WIDTH_P (w));
10944 xassert (!FRAME_WINDOW_P (f));
10946 /* If W is a full-width window, glyph pointers in W's current matrix
10947 have, by definition, to be the same as glyph pointers in the
10948 corresponding frame matrix. */
10949 window_row = w->current_matrix->rows;
10950 window_row_end = window_row + w->current_matrix->nrows;
10951 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10952 while (window_row < window_row_end)
10954 int area;
10956 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10957 frame_row->glyphs[area] = window_row->glyphs[area];
10959 /* Disable frame rows whose corresponding window rows have
10960 been disabled in try_window_id. */
10961 if (!window_row->enabled_p)
10962 frame_row->enabled_p = 0;
10964 ++window_row, ++frame_row;
10969 /* Find the glyph row in window W containing CHARPOS. Consider all
10970 rows between START and END (not inclusive). END null means search
10971 all rows to the end of the display area of W. Value is the row
10972 containing CHARPOS or null. */
10974 static struct glyph_row *
10975 row_containing_pos (w, charpos, start, end)
10976 struct window *w;
10977 int charpos;
10978 struct glyph_row *start, *end;
10980 struct glyph_row *row = start;
10981 int last_y;
10983 /* If we happen to start on a header-line, skip that. */
10984 if (row->mode_line_p)
10985 ++row;
10987 if ((end && row >= end) || !row->enabled_p)
10988 return NULL;
10990 last_y = window_text_bottom_y (w);
10992 while ((end == NULL || row < end)
10993 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10994 /* The end position of a row equals the start
10995 position of the next row. If CHARPOS is there, we
10996 would rather display it in the next line, except
10997 when this line ends in ZV. */
10998 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10999 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11000 || !row->ends_at_zv_p)))
11001 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11002 ++row;
11004 /* Give up if CHARPOS not found. */
11005 if ((end && row >= end)
11006 || charpos < MATRIX_ROW_START_CHARPOS (row)
11007 || charpos > MATRIX_ROW_END_CHARPOS (row))
11008 row = NULL;
11010 return row;
11014 /* Try to redisplay window W by reusing its existing display. W's
11015 current matrix must be up to date when this function is called,
11016 i.e. window_end_valid must not be nil.
11018 Value is
11020 1 if display has been updated
11021 0 if otherwise unsuccessful
11022 -1 if redisplay with same window start is known not to succeed
11024 The following steps are performed:
11026 1. Find the last row in the current matrix of W that is not
11027 affected by changes at the start of current_buffer. If no such row
11028 is found, give up.
11030 2. Find the first row in W's current matrix that is not affected by
11031 changes at the end of current_buffer. Maybe there is no such row.
11033 3. Display lines beginning with the row + 1 found in step 1 to the
11034 row found in step 2 or, if step 2 didn't find a row, to the end of
11035 the window.
11037 4. If cursor is not known to appear on the window, give up.
11039 5. If display stopped at the row found in step 2, scroll the
11040 display and current matrix as needed.
11042 6. Maybe display some lines at the end of W, if we must. This can
11043 happen under various circumstances, like a partially visible line
11044 becoming fully visible, or because newly displayed lines are displayed
11045 in smaller font sizes.
11047 7. Update W's window end information. */
11049 /* Check that window end is what we expect it to be. */
11051 static int
11052 try_window_id (w)
11053 struct window *w;
11055 struct frame *f = XFRAME (w->frame);
11056 struct glyph_matrix *current_matrix = w->current_matrix;
11057 struct glyph_matrix *desired_matrix = w->desired_matrix;
11058 struct glyph_row *last_unchanged_at_beg_row;
11059 struct glyph_row *first_unchanged_at_end_row;
11060 struct glyph_row *row;
11061 struct glyph_row *bottom_row;
11062 int bottom_vpos;
11063 struct it it;
11064 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11065 struct text_pos start_pos;
11066 struct run run;
11067 int first_unchanged_at_end_vpos = 0;
11068 struct glyph_row *last_text_row, *last_text_row_at_end;
11069 struct text_pos start;
11071 SET_TEXT_POS_FROM_MARKER (start, w->start);
11073 /* Check pre-conditions. Window end must be valid, otherwise
11074 the current matrix would not be up to date. */
11075 xassert (!NILP (w->window_end_valid));
11076 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11077 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11079 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11080 only if buffer has really changed. The reason is that the gap is
11081 initially at Z for freshly visited files. The code below would
11082 set end_unchanged to 0 in that case. */
11083 if (MODIFF > SAVE_MODIFF
11084 /* This seems to happen sometimes after saving a buffer. */
11085 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11087 if (GPT - BEG < BEG_UNCHANGED)
11088 BEG_UNCHANGED = GPT - BEG;
11089 if (Z - GPT < END_UNCHANGED)
11090 END_UNCHANGED = Z - GPT;
11093 /* If window starts after a line end, and the last change is in
11094 front of that newline, then changes don't affect the display.
11095 This case happens with stealth-fontification. Note that although
11096 the display is unchanged, glyph positions in the matrix have to
11097 be adjusted, of course. */
11098 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11099 if (CHARPOS (start) > BEGV
11100 && Z - END_UNCHANGED < CHARPOS (start) - 1
11101 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11102 && PT < MATRIX_ROW_END_CHARPOS (row))
11104 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11105 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11107 if (delta)
11109 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11110 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11112 increment_matrix_positions (w->current_matrix,
11113 MATRIX_ROW_VPOS (r0, current_matrix),
11114 MATRIX_ROW_VPOS (r1, current_matrix),
11115 delta, delta_bytes);
11118 #if 0 /* If changes are all in front of the window start, the
11119 distance of the last displayed glyph from Z hasn't
11120 changed. */
11121 w->window_end_pos
11122 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11123 w->window_end_bytepos
11124 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11125 #endif
11127 row = row_containing_pos (w, PT, r0, NULL);
11128 if (row == NULL)
11129 return 0;
11131 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11132 return 1;
11135 /* Return quickly if changes are all below what is displayed in the
11136 window, and if PT is in the window. */
11137 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
11138 && PT < MATRIX_ROW_END_CHARPOS (row))
11140 /* We have to update window end positions because the buffer's
11141 size has changed. */
11142 w->window_end_pos
11143 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11144 w->window_end_bytepos
11145 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11147 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11148 row = row_containing_pos (w, PT, row, NULL);
11149 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11150 return 2;
11153 /* Check that window start agrees with the start of the first glyph
11154 row in its current matrix. Check this after we know the window
11155 start is not in changed text, otherwise positions would not be
11156 comparable. */
11157 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11158 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11159 return 0;
11161 /* Compute the position at which we have to start displaying new
11162 lines. Some of the lines at the top of the window might be
11163 reusable because they are not displaying changed text. Find the
11164 last row in W's current matrix not affected by changes at the
11165 start of current_buffer. Value is null if changes start in the
11166 first line of window. */
11167 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11168 if (last_unchanged_at_beg_row)
11170 /* Avoid starting to display in the moddle of a character, a TAB
11171 for instance. This is easier than to set up the iterator
11172 exactly, and it's not a frequent case, so the additional
11173 effort wouldn't really pay off. */
11174 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11175 && last_unchanged_at_beg_row > w->current_matrix->rows)
11176 --last_unchanged_at_beg_row;
11178 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11179 return 0;
11181 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11182 start_pos = it.current.pos;
11184 /* Start displaying new lines in the desired matrix at the same
11185 vpos we would use in the current matrix, i.e. below
11186 last_unchanged_at_beg_row. */
11187 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11188 current_matrix);
11189 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11190 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11192 xassert (it.hpos == 0 && it.current_x == 0);
11194 else
11196 /* There are no reusable lines at the start of the window.
11197 Start displaying in the first line. */
11198 start_display (&it, w, start);
11199 start_pos = it.current.pos;
11202 /* Find the first row that is not affected by changes at the end of
11203 the buffer. Value will be null if there is no unchanged row, in
11204 which case we must redisplay to the end of the window. delta
11205 will be set to the value by which buffer positions beginning with
11206 first_unchanged_at_end_row have to be adjusted due to text
11207 changes. */
11208 first_unchanged_at_end_row
11209 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11210 IF_DEBUG (debug_delta = delta);
11211 IF_DEBUG (debug_delta_bytes = delta_bytes);
11213 /* Set stop_pos to the buffer position up to which we will have to
11214 display new lines. If first_unchanged_at_end_row != NULL, this
11215 is the buffer position of the start of the line displayed in that
11216 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11217 that we don't stop at a buffer position. */
11218 stop_pos = 0;
11219 if (first_unchanged_at_end_row)
11221 xassert (last_unchanged_at_beg_row == NULL
11222 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11224 /* If this is a continuation line, move forward to the next one
11225 that isn't. Changes in lines above affect this line.
11226 Caution: this may move first_unchanged_at_end_row to a row
11227 not displaying text. */
11228 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11229 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11230 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11231 < it.last_visible_y))
11232 ++first_unchanged_at_end_row;
11234 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11235 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11236 >= it.last_visible_y))
11237 first_unchanged_at_end_row = NULL;
11238 else
11240 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11241 + delta);
11242 first_unchanged_at_end_vpos
11243 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11244 xassert (stop_pos >= Z - END_UNCHANGED);
11247 else if (last_unchanged_at_beg_row == NULL)
11248 return 0;
11251 #if GLYPH_DEBUG
11253 /* Either there is no unchanged row at the end, or the one we have
11254 now displays text. This is a necessary condition for the window
11255 end pos calculation at the end of this function. */
11256 xassert (first_unchanged_at_end_row == NULL
11257 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11259 debug_last_unchanged_at_beg_vpos
11260 = (last_unchanged_at_beg_row
11261 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11262 : -1);
11263 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11265 #endif /* GLYPH_DEBUG != 0 */
11268 /* Display new lines. Set last_text_row to the last new line
11269 displayed which has text on it, i.e. might end up as being the
11270 line where the window_end_vpos is. */
11271 w->cursor.vpos = -1;
11272 last_text_row = NULL;
11273 overlay_arrow_seen = 0;
11274 while (it.current_y < it.last_visible_y
11275 && !fonts_changed_p
11276 && (first_unchanged_at_end_row == NULL
11277 || IT_CHARPOS (it) < stop_pos))
11279 if (display_line (&it))
11280 last_text_row = it.glyph_row - 1;
11283 if (fonts_changed_p)
11284 return -1;
11287 /* Compute differences in buffer positions, y-positions etc. for
11288 lines reused at the bottom of the window. Compute what we can
11289 scroll. */
11290 if (first_unchanged_at_end_row
11291 /* No lines reused because we displayed everything up to the
11292 bottom of the window. */
11293 && it.current_y < it.last_visible_y)
11295 dvpos = (it.vpos
11296 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11297 current_matrix));
11298 dy = it.current_y - first_unchanged_at_end_row->y;
11299 run.current_y = first_unchanged_at_end_row->y;
11300 run.desired_y = run.current_y + dy;
11301 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11303 else
11305 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11306 first_unchanged_at_end_row = NULL;
11308 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11311 /* Find the cursor if not already found. We have to decide whether
11312 PT will appear on this window (it sometimes doesn't, but this is
11313 not a very frequent case.) This decision has to be made before
11314 the current matrix is altered. A value of cursor.vpos < 0 means
11315 that PT is either in one of the lines beginning at
11316 first_unchanged_at_end_row or below the window. Don't care for
11317 lines that might be displayed later at the window end; as
11318 mentioned, this is not a frequent case. */
11319 if (w->cursor.vpos < 0)
11321 /* Cursor in unchanged rows at the top? */
11322 if (PT < CHARPOS (start_pos)
11323 && last_unchanged_at_beg_row)
11325 row = row_containing_pos (w, PT,
11326 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11327 last_unchanged_at_beg_row + 1);
11328 if (row)
11329 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11332 /* Start from first_unchanged_at_end_row looking for PT. */
11333 else if (first_unchanged_at_end_row)
11335 row = row_containing_pos (w, PT - delta,
11336 first_unchanged_at_end_row, NULL);
11337 if (row)
11338 set_cursor_from_row (w, row, w->current_matrix, delta,
11339 delta_bytes, dy, dvpos);
11342 /* Give up if cursor was not found. */
11343 if (w->cursor.vpos < 0)
11345 clear_glyph_matrix (w->desired_matrix);
11346 return -1;
11350 /* Don't let the cursor end in the scroll margins. */
11352 int this_scroll_margin, cursor_height;
11354 this_scroll_margin = max (0, scroll_margin);
11355 this_scroll_margin = min (this_scroll_margin,
11356 XFASTINT (w->height) / 4);
11357 this_scroll_margin *= CANON_Y_UNIT (it.f);
11358 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11360 if ((w->cursor.y < this_scroll_margin
11361 && CHARPOS (start) > BEGV)
11362 /* Don't take scroll margin into account at the bottom because
11363 old redisplay didn't do it either. */
11364 || w->cursor.y + cursor_height > it.last_visible_y)
11366 w->cursor.vpos = -1;
11367 clear_glyph_matrix (w->desired_matrix);
11368 return -1;
11372 /* Scroll the display. Do it before changing the current matrix so
11373 that xterm.c doesn't get confused about where the cursor glyph is
11374 found. */
11375 if (dy && run.height)
11377 update_begin (f);
11379 if (FRAME_WINDOW_P (f))
11381 rif->update_window_begin_hook (w);
11382 rif->clear_mouse_face (w);
11383 rif->scroll_run_hook (w, &run);
11384 rif->update_window_end_hook (w, 0, 0);
11386 else
11388 /* Terminal frame. In this case, dvpos gives the number of
11389 lines to scroll by; dvpos < 0 means scroll up. */
11390 int first_unchanged_at_end_vpos
11391 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11392 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11393 int end = XFASTINT (w->top) + window_internal_height (w);
11395 /* Perform the operation on the screen. */
11396 if (dvpos > 0)
11398 /* Scroll last_unchanged_at_beg_row to the end of the
11399 window down dvpos lines. */
11400 set_terminal_window (end);
11402 /* On dumb terminals delete dvpos lines at the end
11403 before inserting dvpos empty lines. */
11404 if (!scroll_region_ok)
11405 ins_del_lines (end - dvpos, -dvpos);
11407 /* Insert dvpos empty lines in front of
11408 last_unchanged_at_beg_row. */
11409 ins_del_lines (from, dvpos);
11411 else if (dvpos < 0)
11413 /* Scroll up last_unchanged_at_beg_vpos to the end of
11414 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11415 set_terminal_window (end);
11417 /* Delete dvpos lines in front of
11418 last_unchanged_at_beg_vpos. ins_del_lines will set
11419 the cursor to the given vpos and emit |dvpos| delete
11420 line sequences. */
11421 ins_del_lines (from + dvpos, dvpos);
11423 /* On a dumb terminal insert dvpos empty lines at the
11424 end. */
11425 if (!scroll_region_ok)
11426 ins_del_lines (end + dvpos, -dvpos);
11429 set_terminal_window (0);
11432 update_end (f);
11435 /* Shift reused rows of the current matrix to the right position.
11436 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11437 text. */
11438 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11439 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11440 if (dvpos < 0)
11442 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11443 bottom_vpos, dvpos);
11444 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11445 bottom_vpos, 0);
11447 else if (dvpos > 0)
11449 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11450 bottom_vpos, dvpos);
11451 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11452 first_unchanged_at_end_vpos + dvpos, 0);
11455 /* For frame-based redisplay, make sure that current frame and window
11456 matrix are in sync with respect to glyph memory. */
11457 if (!FRAME_WINDOW_P (f))
11458 sync_frame_with_window_matrix_rows (w);
11460 /* Adjust buffer positions in reused rows. */
11461 if (delta)
11462 increment_matrix_positions (current_matrix,
11463 first_unchanged_at_end_vpos + dvpos,
11464 bottom_vpos, delta, delta_bytes);
11466 /* Adjust Y positions. */
11467 if (dy)
11468 shift_glyph_matrix (w, current_matrix,
11469 first_unchanged_at_end_vpos + dvpos,
11470 bottom_vpos, dy);
11472 if (first_unchanged_at_end_row)
11473 first_unchanged_at_end_row += dvpos;
11475 /* If scrolling up, there may be some lines to display at the end of
11476 the window. */
11477 last_text_row_at_end = NULL;
11478 if (dy < 0)
11480 /* Set last_row to the glyph row in the current matrix where the
11481 window end line is found. It has been moved up or down in
11482 the matrix by dvpos. */
11483 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11484 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11486 /* If last_row is the window end line, it should display text. */
11487 xassert (last_row->displays_text_p);
11489 /* If window end line was partially visible before, begin
11490 displaying at that line. Otherwise begin displaying with the
11491 line following it. */
11492 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11494 init_to_row_start (&it, w, last_row);
11495 it.vpos = last_vpos;
11496 it.current_y = last_row->y;
11498 else
11500 init_to_row_end (&it, w, last_row);
11501 it.vpos = 1 + last_vpos;
11502 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11503 ++last_row;
11506 /* We may start in a continuation line. If so, we have to get
11507 the right continuation_lines_width and current_x. */
11508 it.continuation_lines_width = last_row->continuation_lines_width;
11509 it.hpos = it.current_x = 0;
11511 /* Display the rest of the lines at the window end. */
11512 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11513 while (it.current_y < it.last_visible_y
11514 && !fonts_changed_p)
11516 /* Is it always sure that the display agrees with lines in
11517 the current matrix? I don't think so, so we mark rows
11518 displayed invalid in the current matrix by setting their
11519 enabled_p flag to zero. */
11520 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11521 if (display_line (&it))
11522 last_text_row_at_end = it.glyph_row - 1;
11526 /* Update window_end_pos and window_end_vpos. */
11527 if (first_unchanged_at_end_row
11528 && first_unchanged_at_end_row->y < it.last_visible_y
11529 && !last_text_row_at_end)
11531 /* Window end line if one of the preserved rows from the current
11532 matrix. Set row to the last row displaying text in current
11533 matrix starting at first_unchanged_at_end_row, after
11534 scrolling. */
11535 xassert (first_unchanged_at_end_row->displays_text_p);
11536 row = find_last_row_displaying_text (w->current_matrix, &it,
11537 first_unchanged_at_end_row);
11538 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11540 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11541 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11542 w->window_end_vpos
11543 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11545 else if (last_text_row_at_end)
11547 w->window_end_pos
11548 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11549 w->window_end_bytepos
11550 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11551 w->window_end_vpos
11552 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11554 else if (last_text_row)
11556 /* We have displayed either to the end of the window or at the
11557 end of the window, i.e. the last row with text is to be found
11558 in the desired matrix. */
11559 w->window_end_pos
11560 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11561 w->window_end_bytepos
11562 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11563 w->window_end_vpos
11564 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11566 else if (first_unchanged_at_end_row == NULL
11567 && last_text_row == NULL
11568 && last_text_row_at_end == NULL)
11570 /* Displayed to end of window, but no line containing text was
11571 displayed. Lines were deleted at the end of the window. */
11572 int vpos;
11573 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11575 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11576 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11577 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11578 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11579 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11580 break;
11582 w->window_end_vpos = make_number (vpos);
11584 else
11585 abort ();
11587 #if 0 /* This leads to problems, for instance when the cursor is
11588 at ZV, and the cursor line displays no text. */
11589 /* Disable rows below what's displayed in the window. This makes
11590 debugging easier. */
11591 enable_glyph_matrix_rows (current_matrix,
11592 XFASTINT (w->window_end_vpos) + 1,
11593 bottom_vpos, 0);
11594 #endif
11596 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11597 debug_end_vpos = XFASTINT (w->window_end_vpos));
11599 /* Record that display has not been completed. */
11600 w->window_end_valid = Qnil;
11601 w->desired_matrix->no_scrolling_p = 1;
11602 return 3;
11607 /***********************************************************************
11608 More debugging support
11609 ***********************************************************************/
11611 #if GLYPH_DEBUG
11613 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11614 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11615 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11618 /* Dump the contents of glyph matrix MATRIX on stderr.
11620 GLYPHS 0 means don't show glyph contents.
11621 GLYPHS 1 means show glyphs in short form
11622 GLYPHS > 1 means show glyphs in long form. */
11624 void
11625 dump_glyph_matrix (matrix, glyphs)
11626 struct glyph_matrix *matrix;
11627 int glyphs;
11629 int i;
11630 for (i = 0; i < matrix->nrows; ++i)
11631 dump_glyph_row (matrix, i, glyphs);
11635 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11636 the glyph row and area where the glyph comes from. */
11638 void
11639 dump_glyph (row, glyph, area)
11640 struct glyph_row *row;
11641 struct glyph *glyph;
11642 int area;
11644 if (glyph->type == CHAR_GLYPH)
11646 fprintf (stderr,
11647 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11648 glyph - row->glyphs[TEXT_AREA],
11649 'C',
11650 glyph->charpos,
11651 (BUFFERP (glyph->object)
11652 ? 'B'
11653 : (STRINGP (glyph->object)
11654 ? 'S'
11655 : '-')),
11656 glyph->pixel_width,
11657 glyph->u.ch,
11658 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11659 ? glyph->u.ch
11660 : '.'),
11661 glyph->face_id,
11662 glyph->left_box_line_p,
11663 glyph->right_box_line_p);
11665 else if (glyph->type == STRETCH_GLYPH)
11667 fprintf (stderr,
11668 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11669 glyph - row->glyphs[TEXT_AREA],
11670 'S',
11671 glyph->charpos,
11672 (BUFFERP (glyph->object)
11673 ? 'B'
11674 : (STRINGP (glyph->object)
11675 ? 'S'
11676 : '-')),
11677 glyph->pixel_width,
11679 '.',
11680 glyph->face_id,
11681 glyph->left_box_line_p,
11682 glyph->right_box_line_p);
11684 else if (glyph->type == IMAGE_GLYPH)
11686 fprintf (stderr,
11687 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11688 glyph - row->glyphs[TEXT_AREA],
11689 'I',
11690 glyph->charpos,
11691 (BUFFERP (glyph->object)
11692 ? 'B'
11693 : (STRINGP (glyph->object)
11694 ? 'S'
11695 : '-')),
11696 glyph->pixel_width,
11697 glyph->u.img_id,
11698 '.',
11699 glyph->face_id,
11700 glyph->left_box_line_p,
11701 glyph->right_box_line_p);
11706 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11707 GLYPHS 0 means don't show glyph contents.
11708 GLYPHS 1 means show glyphs in short form
11709 GLYPHS > 1 means show glyphs in long form. */
11711 void
11712 dump_glyph_row (matrix, vpos, glyphs)
11713 struct glyph_matrix *matrix;
11714 int vpos, glyphs;
11716 struct glyph_row *row;
11718 if (vpos < 0 || vpos >= matrix->nrows)
11719 return;
11721 row = MATRIX_ROW (matrix, vpos);
11723 if (glyphs != 1)
11725 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11726 fprintf (stderr, "=======================================================================\n");
11728 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11729 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11730 row - matrix->rows,
11731 MATRIX_ROW_START_CHARPOS (row),
11732 MATRIX_ROW_END_CHARPOS (row),
11733 row->used[TEXT_AREA],
11734 row->contains_overlapping_glyphs_p,
11735 row->enabled_p,
11736 row->inverse_p,
11737 row->truncated_on_left_p,
11738 row->truncated_on_right_p,
11739 row->overlay_arrow_p,
11740 row->continued_p,
11741 MATRIX_ROW_CONTINUATION_LINE_P (row),
11742 row->displays_text_p,
11743 row->ends_at_zv_p,
11744 row->fill_line_p,
11745 row->ends_in_middle_of_char_p,
11746 row->starts_in_middle_of_char_p,
11747 row->mouse_face_p,
11748 row->x,
11749 row->y,
11750 row->pixel_width,
11751 row->height,
11752 row->visible_height,
11753 row->ascent,
11754 row->phys_ascent);
11755 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11756 row->end.overlay_string_index);
11757 fprintf (stderr, "%9d %5d\n",
11758 CHARPOS (row->start.string_pos),
11759 CHARPOS (row->end.string_pos));
11760 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11761 row->end.dpvec_index);
11764 if (glyphs > 1)
11766 int area;
11768 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11770 struct glyph *glyph = row->glyphs[area];
11771 struct glyph *glyph_end = glyph + row->used[area];
11773 /* Glyph for a line end in text. */
11774 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11775 ++glyph_end;
11777 if (glyph < glyph_end)
11778 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11780 for (; glyph < glyph_end; ++glyph)
11781 dump_glyph (row, glyph, area);
11784 else if (glyphs == 1)
11786 int area;
11788 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11790 char *s = (char *) alloca (row->used[area] + 1);
11791 int i;
11793 for (i = 0; i < row->used[area]; ++i)
11795 struct glyph *glyph = row->glyphs[area] + i;
11796 if (glyph->type == CHAR_GLYPH
11797 && glyph->u.ch < 0x80
11798 && glyph->u.ch >= ' ')
11799 s[i] = glyph->u.ch;
11800 else
11801 s[i] = '.';
11804 s[i] = '\0';
11805 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11811 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11812 Sdump_glyph_matrix, 0, 1, "p",
11813 "Dump the current matrix of the selected window to stderr.\n\
11814 Shows contents of glyph row structures. With non-nil\n\
11815 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11816 glyphs in short form, otherwise show glyphs in long form.")
11817 (glyphs)
11818 Lisp_Object glyphs;
11820 struct window *w = XWINDOW (selected_window);
11821 struct buffer *buffer = XBUFFER (w->buffer);
11823 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11824 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11825 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11826 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11827 fprintf (stderr, "=============================================\n");
11828 dump_glyph_matrix (w->current_matrix,
11829 NILP (glyphs) ? 0 : XINT (glyphs));
11830 return Qnil;
11834 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11835 "Dump glyph row ROW to stderr.\n\
11836 GLYPH 0 means don't dump glyphs.\n\
11837 GLYPH 1 means dump glyphs in short form.\n\
11838 GLYPH > 1 or omitted means dump glyphs in long form.")
11839 (row, glyphs)
11840 Lisp_Object row, glyphs;
11842 CHECK_NUMBER (row, 0);
11843 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11844 XINT (row),
11845 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11846 return Qnil;
11850 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11851 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11852 GLYPH 0 means don't dump glyphs.\n\
11853 GLYPH 1 means dump glyphs in short form.\n\
11854 GLYPH > 1 or omitted means dump glyphs in long form.")
11855 (row, glyphs)
11856 Lisp_Object row, glyphs;
11858 struct frame *sf = SELECTED_FRAME ();
11859 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11860 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11861 return Qnil;
11865 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11866 Strace_redisplay_toggle, 0, 0, "",
11867 "Toggle tracing of redisplay.")
11870 trace_redisplay_p = !trace_redisplay_p;
11871 return Qnil;
11875 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11876 "Print STRING to stderr.")
11877 (string)
11878 Lisp_Object string;
11880 CHECK_STRING (string, 0);
11881 fprintf (stderr, "%s", XSTRING (string)->data);
11882 return Qnil;
11885 #endif /* GLYPH_DEBUG */
11889 /***********************************************************************
11890 Building Desired Matrix Rows
11891 ***********************************************************************/
11893 /* Return a temporary glyph row holding the glyphs of an overlay
11894 arrow. Only used for non-window-redisplay windows. */
11896 static struct glyph_row *
11897 get_overlay_arrow_glyph_row (w)
11898 struct window *w;
11900 struct frame *f = XFRAME (WINDOW_FRAME (w));
11901 struct buffer *buffer = XBUFFER (w->buffer);
11902 struct buffer *old = current_buffer;
11903 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11904 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11905 unsigned char *arrow_end = arrow_string + arrow_len;
11906 unsigned char *p;
11907 struct it it;
11908 int multibyte_p;
11909 int n_glyphs_before;
11911 set_buffer_temp (buffer);
11912 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11913 it.glyph_row->used[TEXT_AREA] = 0;
11914 SET_TEXT_POS (it.position, 0, 0);
11916 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11917 p = arrow_string;
11918 while (p < arrow_end)
11920 Lisp_Object face, ilisp;
11922 /* Get the next character. */
11923 if (multibyte_p)
11924 it.c = string_char_and_length (p, arrow_len, &it.len);
11925 else
11926 it.c = *p, it.len = 1;
11927 p += it.len;
11929 /* Get its face. */
11930 ilisp = make_number (p - arrow_string);
11931 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11932 it.face_id = compute_char_face (f, it.c, face);
11934 /* Compute its width, get its glyphs. */
11935 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11936 SET_TEXT_POS (it.position, -1, -1);
11937 PRODUCE_GLYPHS (&it);
11939 /* If this character doesn't fit any more in the line, we have
11940 to remove some glyphs. */
11941 if (it.current_x > it.last_visible_x)
11943 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11944 break;
11948 set_buffer_temp (old);
11949 return it.glyph_row;
11953 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11954 glyphs are only inserted for terminal frames since we can't really
11955 win with truncation glyphs when partially visible glyphs are
11956 involved. Which glyphs to insert is determined by
11957 produce_special_glyphs. */
11959 static void
11960 insert_left_trunc_glyphs (it)
11961 struct it *it;
11963 struct it truncate_it;
11964 struct glyph *from, *end, *to, *toend;
11966 xassert (!FRAME_WINDOW_P (it->f));
11968 /* Get the truncation glyphs. */
11969 truncate_it = *it;
11970 truncate_it.current_x = 0;
11971 truncate_it.face_id = DEFAULT_FACE_ID;
11972 truncate_it.glyph_row = &scratch_glyph_row;
11973 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11974 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11975 truncate_it.object = make_number (0);
11976 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11978 /* Overwrite glyphs from IT with truncation glyphs. */
11979 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11980 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11981 to = it->glyph_row->glyphs[TEXT_AREA];
11982 toend = to + it->glyph_row->used[TEXT_AREA];
11984 while (from < end)
11985 *to++ = *from++;
11987 /* There may be padding glyphs left over. Overwrite them too. */
11988 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
11990 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11991 while (from < end)
11992 *to++ = *from++;
11995 if (to > toend)
11996 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12000 /* Compute the pixel height and width of IT->glyph_row.
12002 Most of the time, ascent and height of a display line will be equal
12003 to the max_ascent and max_height values of the display iterator
12004 structure. This is not the case if
12006 1. We hit ZV without displaying anything. In this case, max_ascent
12007 and max_height will be zero.
12009 2. We have some glyphs that don't contribute to the line height.
12010 (The glyph row flag contributes_to_line_height_p is for future
12011 pixmap extensions).
12013 The first case is easily covered by using default values because in
12014 these cases, the line height does not really matter, except that it
12015 must not be zero. */
12017 static void
12018 compute_line_metrics (it)
12019 struct it *it;
12021 struct glyph_row *row = it->glyph_row;
12022 int area, i;
12024 if (FRAME_WINDOW_P (it->f))
12026 int i, header_line_height;
12028 /* The line may consist of one space only, that was added to
12029 place the cursor on it. If so, the row's height hasn't been
12030 computed yet. */
12031 if (row->height == 0)
12033 if (it->max_ascent + it->max_descent == 0)
12034 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12035 row->ascent = it->max_ascent;
12036 row->height = it->max_ascent + it->max_descent;
12037 row->phys_ascent = it->max_phys_ascent;
12038 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12041 /* Compute the width of this line. */
12042 row->pixel_width = row->x;
12043 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12044 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12046 xassert (row->pixel_width >= 0);
12047 xassert (row->ascent >= 0 && row->height > 0);
12049 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12050 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12052 /* If first line's physical ascent is larger than its logical
12053 ascent, use the physical ascent, and make the row taller.
12054 This makes accented characters fully visible. */
12055 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12056 && row->phys_ascent > row->ascent)
12058 row->height += row->phys_ascent - row->ascent;
12059 row->ascent = row->phys_ascent;
12062 /* Compute how much of the line is visible. */
12063 row->visible_height = row->height;
12065 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12066 if (row->y < header_line_height)
12067 row->visible_height -= header_line_height - row->y;
12068 else
12070 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12071 if (row->y + row->height > max_y)
12072 row->visible_height -= row->y + row->height - max_y;
12075 else
12077 row->pixel_width = row->used[TEXT_AREA];
12078 row->ascent = row->phys_ascent = 0;
12079 row->height = row->phys_height = row->visible_height = 1;
12082 /* Compute a hash code for this row. */
12083 row->hash = 0;
12084 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12085 for (i = 0; i < row->used[area]; ++i)
12086 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12087 + row->glyphs[area][i].u.val
12088 + row->glyphs[area][i].face_id
12089 + row->glyphs[area][i].padding_p
12090 + (row->glyphs[area][i].type << 2));
12092 it->max_ascent = it->max_descent = 0;
12093 it->max_phys_ascent = it->max_phys_descent = 0;
12097 /* Append one space to the glyph row of iterator IT if doing a
12098 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12099 space have the default face, otherwise let it have the same face as
12100 IT->face_id. Value is non-zero if a space was added.
12102 This function is called to make sure that there is always one glyph
12103 at the end of a glyph row that the cursor can be set on under
12104 window-systems. (If there weren't such a glyph we would not know
12105 how wide and tall a box cursor should be displayed).
12107 At the same time this space let's a nicely handle clearing to the
12108 end of the line if the row ends in italic text. */
12110 static int
12111 append_space (it, default_face_p)
12112 struct it *it;
12113 int default_face_p;
12115 if (FRAME_WINDOW_P (it->f))
12117 int n = it->glyph_row->used[TEXT_AREA];
12119 if (it->glyph_row->glyphs[TEXT_AREA] + n
12120 < it->glyph_row->glyphs[1 + TEXT_AREA])
12122 /* Save some values that must not be changed.
12123 Must save IT->c and IT->len because otherwise
12124 ITERATOR_AT_END_P wouldn't work anymore after
12125 append_space has been called. */
12126 enum display_element_type saved_what = it->what;
12127 int saved_c = it->c, saved_len = it->len;
12128 int saved_x = it->current_x;
12129 int saved_face_id = it->face_id;
12130 struct text_pos saved_pos;
12131 Lisp_Object saved_object;
12132 struct face *face;
12134 saved_object = it->object;
12135 saved_pos = it->position;
12137 it->what = IT_CHARACTER;
12138 bzero (&it->position, sizeof it->position);
12139 it->object = make_number (0);
12140 it->c = ' ';
12141 it->len = 1;
12143 if (default_face_p)
12144 it->face_id = DEFAULT_FACE_ID;
12145 else if (it->face_before_selective_p)
12146 it->face_id = it->saved_face_id;
12147 face = FACE_FROM_ID (it->f, it->face_id);
12148 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12150 PRODUCE_GLYPHS (it);
12152 it->current_x = saved_x;
12153 it->object = saved_object;
12154 it->position = saved_pos;
12155 it->what = saved_what;
12156 it->face_id = saved_face_id;
12157 it->len = saved_len;
12158 it->c = saved_c;
12159 return 1;
12163 return 0;
12167 /* Extend the face of the last glyph in the text area of IT->glyph_row
12168 to the end of the display line. Called from display_line.
12169 If the glyph row is empty, add a space glyph to it so that we
12170 know the face to draw. Set the glyph row flag fill_line_p. */
12172 static void
12173 extend_face_to_end_of_line (it)
12174 struct it *it;
12176 struct face *face;
12177 struct frame *f = it->f;
12179 /* If line is already filled, do nothing. */
12180 if (it->current_x >= it->last_visible_x)
12181 return;
12183 /* Face extension extends the background and box of IT->face_id
12184 to the end of the line. If the background equals the background
12185 of the frame, we don't have to do anything. */
12186 if (it->face_before_selective_p)
12187 face = FACE_FROM_ID (it->f, it->saved_face_id);
12188 else
12189 face = FACE_FROM_ID (f, it->face_id);
12191 if (FRAME_WINDOW_P (f)
12192 && face->box == FACE_NO_BOX
12193 && face->background == FRAME_BACKGROUND_PIXEL (f)
12194 && !face->stipple)
12195 return;
12197 /* Set the glyph row flag indicating that the face of the last glyph
12198 in the text area has to be drawn to the end of the text area. */
12199 it->glyph_row->fill_line_p = 1;
12201 /* If current character of IT is not ASCII, make sure we have the
12202 ASCII face. This will be automatically undone the next time
12203 get_next_display_element returns a multibyte character. Note
12204 that the character will always be single byte in unibyte text. */
12205 if (!SINGLE_BYTE_CHAR_P (it->c))
12207 it->face_id = FACE_FOR_CHAR (f, face, 0);
12210 if (FRAME_WINDOW_P (f))
12212 /* If the row is empty, add a space with the current face of IT,
12213 so that we know which face to draw. */
12214 if (it->glyph_row->used[TEXT_AREA] == 0)
12216 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12217 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12218 it->glyph_row->used[TEXT_AREA] = 1;
12221 else
12223 /* Save some values that must not be changed. */
12224 int saved_x = it->current_x;
12225 struct text_pos saved_pos;
12226 Lisp_Object saved_object;
12227 enum display_element_type saved_what = it->what;
12228 int saved_face_id = it->face_id;
12230 saved_object = it->object;
12231 saved_pos = it->position;
12233 it->what = IT_CHARACTER;
12234 bzero (&it->position, sizeof it->position);
12235 it->object = make_number (0);
12236 it->c = ' ';
12237 it->len = 1;
12238 it->face_id = face->id;
12240 PRODUCE_GLYPHS (it);
12242 while (it->current_x <= it->last_visible_x)
12243 PRODUCE_GLYPHS (it);
12245 /* Don't count these blanks really. It would let us insert a left
12246 truncation glyph below and make us set the cursor on them, maybe. */
12247 it->current_x = saved_x;
12248 it->object = saved_object;
12249 it->position = saved_pos;
12250 it->what = saved_what;
12251 it->face_id = saved_face_id;
12256 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12257 trailing whitespace. */
12259 static int
12260 trailing_whitespace_p (charpos)
12261 int charpos;
12263 int bytepos = CHAR_TO_BYTE (charpos);
12264 int c = 0;
12266 while (bytepos < ZV_BYTE
12267 && (c = FETCH_CHAR (bytepos),
12268 c == ' ' || c == '\t'))
12269 ++bytepos;
12271 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12273 if (bytepos != PT_BYTE)
12274 return 1;
12276 return 0;
12280 /* Highlight trailing whitespace, if any, in ROW. */
12282 void
12283 highlight_trailing_whitespace (f, row)
12284 struct frame *f;
12285 struct glyph_row *row;
12287 int used = row->used[TEXT_AREA];
12289 if (used)
12291 struct glyph *start = row->glyphs[TEXT_AREA];
12292 struct glyph *glyph = start + used - 1;
12294 /* Skip over glyphs inserted to display the cursor at the
12295 end of a line, for extending the face of the last glyph
12296 to the end of the line on terminals, and for truncation
12297 and continuation glyphs. */
12298 while (glyph >= start
12299 && glyph->type == CHAR_GLYPH
12300 && INTEGERP (glyph->object))
12301 --glyph;
12303 /* If last glyph is a space or stretch, and it's trailing
12304 whitespace, set the face of all trailing whitespace glyphs in
12305 IT->glyph_row to `trailing-whitespace'. */
12306 if (glyph >= start
12307 && BUFFERP (glyph->object)
12308 && (glyph->type == STRETCH_GLYPH
12309 || (glyph->type == CHAR_GLYPH
12310 && glyph->u.ch == ' '))
12311 && trailing_whitespace_p (glyph->charpos))
12313 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12315 while (glyph >= start
12316 && BUFFERP (glyph->object)
12317 && (glyph->type == STRETCH_GLYPH
12318 || (glyph->type == CHAR_GLYPH
12319 && glyph->u.ch == ' ')))
12320 (glyph--)->face_id = face_id;
12326 /* Value is non-zero if glyph row ROW in window W should be
12327 used to hold the cursor. */
12329 static int
12330 cursor_row_p (w, row)
12331 struct window *w;
12332 struct glyph_row *row;
12334 int cursor_row_p = 1;
12336 if (PT == MATRIX_ROW_END_CHARPOS (row))
12338 /* If the row ends with a newline from a string, we don't want
12339 the cursor there (if the row is continued it doesn't end in a
12340 newline). */
12341 if (CHARPOS (row->end.string_pos) >= 0
12342 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12343 cursor_row_p = row->continued_p;
12345 /* If the row ends at ZV, display the cursor at the end of that
12346 row instead of at the start of the row below. */
12347 else if (row->ends_at_zv_p)
12348 cursor_row_p = 1;
12349 else
12350 cursor_row_p = 0;
12353 return cursor_row_p;
12357 /* Construct the glyph row IT->glyph_row in the desired matrix of
12358 IT->w from text at the current position of IT. See dispextern.h
12359 for an overview of struct it. Value is non-zero if
12360 IT->glyph_row displays text, as opposed to a line displaying ZV
12361 only. */
12363 static int
12364 display_line (it)
12365 struct it *it;
12367 struct glyph_row *row = it->glyph_row;
12369 /* We always start displaying at hpos zero even if hscrolled. */
12370 xassert (it->hpos == 0 && it->current_x == 0);
12372 /* We must not display in a row that's not a text row. */
12373 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12374 < it->w->desired_matrix->nrows);
12376 /* Is IT->w showing the region? */
12377 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12379 /* Clear the result glyph row and enable it. */
12380 prepare_desired_row (row);
12382 row->y = it->current_y;
12383 row->start = it->current;
12384 row->continuation_lines_width = it->continuation_lines_width;
12385 row->displays_text_p = 1;
12386 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12387 it->starts_in_middle_of_char_p = 0;
12389 /* Arrange the overlays nicely for our purposes. Usually, we call
12390 display_line on only one line at a time, in which case this
12391 can't really hurt too much, or we call it on lines which appear
12392 one after another in the buffer, in which case all calls to
12393 recenter_overlay_lists but the first will be pretty cheap. */
12394 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12396 /* Move over display elements that are not visible because we are
12397 hscrolled. This may stop at an x-position < IT->first_visible_x
12398 if the first glyph is partially visible or if we hit a line end. */
12399 if (it->current_x < it->first_visible_x)
12400 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12401 MOVE_TO_POS | MOVE_TO_X);
12403 /* Get the initial row height. This is either the height of the
12404 text hscrolled, if there is any, or zero. */
12405 row->ascent = it->max_ascent;
12406 row->height = it->max_ascent + it->max_descent;
12407 row->phys_ascent = it->max_phys_ascent;
12408 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12410 /* Loop generating characters. The loop is left with IT on the next
12411 character to display. */
12412 while (1)
12414 int n_glyphs_before, hpos_before, x_before;
12415 int x, i, nglyphs;
12416 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12418 /* Retrieve the next thing to display. Value is zero if end of
12419 buffer reached. */
12420 if (!get_next_display_element (it))
12422 /* Maybe add a space at the end of this line that is used to
12423 display the cursor there under X. Set the charpos of the
12424 first glyph of blank lines not corresponding to any text
12425 to -1. */
12426 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12427 || row->used[TEXT_AREA] == 0)
12429 row->glyphs[TEXT_AREA]->charpos = -1;
12430 row->displays_text_p = 0;
12432 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12433 row->indicate_empty_line_p = 1;
12436 it->continuation_lines_width = 0;
12437 row->ends_at_zv_p = 1;
12438 break;
12441 /* Now, get the metrics of what we want to display. This also
12442 generates glyphs in `row' (which is IT->glyph_row). */
12443 n_glyphs_before = row->used[TEXT_AREA];
12444 x = it->current_x;
12446 /* Remember the line height so far in case the next element doesn't
12447 fit on the line. */
12448 if (!it->truncate_lines_p)
12450 ascent = it->max_ascent;
12451 descent = it->max_descent;
12452 phys_ascent = it->max_phys_ascent;
12453 phys_descent = it->max_phys_descent;
12456 PRODUCE_GLYPHS (it);
12458 /* If this display element was in marginal areas, continue with
12459 the next one. */
12460 if (it->area != TEXT_AREA)
12462 row->ascent = max (row->ascent, it->max_ascent);
12463 row->height = max (row->height, it->max_ascent + it->max_descent);
12464 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12465 row->phys_height = max (row->phys_height,
12466 it->max_phys_ascent + it->max_phys_descent);
12467 set_iterator_to_next (it, 1);
12468 continue;
12471 /* Does the display element fit on the line? If we truncate
12472 lines, we should draw past the right edge of the window. If
12473 we don't truncate, we want to stop so that we can display the
12474 continuation glyph before the right margin. If lines are
12475 continued, there are two possible strategies for characters
12476 resulting in more than 1 glyph (e.g. tabs): Display as many
12477 glyphs as possible in this line and leave the rest for the
12478 continuation line, or display the whole element in the next
12479 line. Original redisplay did the former, so we do it also. */
12480 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12481 hpos_before = it->hpos;
12482 x_before = x;
12484 if (/* Not a newline. */
12485 nglyphs > 0
12486 /* Glyphs produced fit entirely in the line. */
12487 && it->current_x < it->last_visible_x)
12489 it->hpos += nglyphs;
12490 row->ascent = max (row->ascent, it->max_ascent);
12491 row->height = max (row->height, it->max_ascent + it->max_descent);
12492 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12493 row->phys_height = max (row->phys_height,
12494 it->max_phys_ascent + it->max_phys_descent);
12495 if (it->current_x - it->pixel_width < it->first_visible_x)
12496 row->x = x - it->first_visible_x;
12498 else
12500 int new_x;
12501 struct glyph *glyph;
12503 for (i = 0; i < nglyphs; ++i, x = new_x)
12505 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12506 new_x = x + glyph->pixel_width;
12508 if (/* Lines are continued. */
12509 !it->truncate_lines_p
12510 && (/* Glyph doesn't fit on the line. */
12511 new_x > it->last_visible_x
12512 /* Or it fits exactly on a window system frame. */
12513 || (new_x == it->last_visible_x
12514 && FRAME_WINDOW_P (it->f))))
12516 /* End of a continued line. */
12518 if (it->hpos == 0
12519 || (new_x == it->last_visible_x
12520 && FRAME_WINDOW_P (it->f)))
12522 /* Current glyph is the only one on the line or
12523 fits exactly on the line. We must continue
12524 the line because we can't draw the cursor
12525 after the glyph. */
12526 row->continued_p = 1;
12527 it->current_x = new_x;
12528 it->continuation_lines_width += new_x;
12529 ++it->hpos;
12530 if (i == nglyphs - 1)
12531 set_iterator_to_next (it, 1);
12533 else if (CHAR_GLYPH_PADDING_P (*glyph)
12534 && !FRAME_WINDOW_P (it->f))
12536 /* A padding glyph that doesn't fit on this line.
12537 This means the whole character doesn't fit
12538 on the line. */
12539 row->used[TEXT_AREA] = n_glyphs_before;
12541 /* Fill the rest of the row with continuation
12542 glyphs like in 20.x. */
12543 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12544 < row->glyphs[1 + TEXT_AREA])
12545 produce_special_glyphs (it, IT_CONTINUATION);
12547 row->continued_p = 1;
12548 it->current_x = x_before;
12549 it->continuation_lines_width += x_before;
12551 /* Restore the height to what it was before the
12552 element not fitting on the line. */
12553 it->max_ascent = ascent;
12554 it->max_descent = descent;
12555 it->max_phys_ascent = phys_ascent;
12556 it->max_phys_descent = phys_descent;
12558 else
12560 /* Display element draws past the right edge of
12561 the window. Restore positions to values
12562 before the element. The next line starts
12563 with current_x before the glyph that could
12564 not be displayed, so that TAB works right. */
12565 row->used[TEXT_AREA] = n_glyphs_before + i;
12567 /* Display continuation glyphs. */
12568 if (!FRAME_WINDOW_P (it->f))
12569 produce_special_glyphs (it, IT_CONTINUATION);
12570 row->continued_p = 1;
12572 it->current_x = x;
12573 it->continuation_lines_width += x;
12574 if (nglyphs > 1 && i > 0)
12576 row->ends_in_middle_of_char_p = 1;
12577 it->starts_in_middle_of_char_p = 1;
12580 /* Restore the height to what it was before the
12581 element not fitting on the line. */
12582 it->max_ascent = ascent;
12583 it->max_descent = descent;
12584 it->max_phys_ascent = phys_ascent;
12585 it->max_phys_descent = phys_descent;
12588 break;
12590 else if (new_x > it->first_visible_x)
12592 /* Increment number of glyphs actually displayed. */
12593 ++it->hpos;
12595 if (x < it->first_visible_x)
12596 /* Glyph is partially visible, i.e. row starts at
12597 negative X position. */
12598 row->x = x - it->first_visible_x;
12600 else
12602 /* Glyph is completely off the left margin of the
12603 window. This should not happen because of the
12604 move_it_in_display_line at the start of
12605 this function. */
12606 abort ();
12610 row->ascent = max (row->ascent, it->max_ascent);
12611 row->height = max (row->height, it->max_ascent + it->max_descent);
12612 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12613 row->phys_height = max (row->phys_height,
12614 it->max_phys_ascent + it->max_phys_descent);
12616 /* End of this display line if row is continued. */
12617 if (row->continued_p)
12618 break;
12621 /* Is this a line end? If yes, we're also done, after making
12622 sure that a non-default face is extended up to the right
12623 margin of the window. */
12624 if (ITERATOR_AT_END_OF_LINE_P (it))
12626 int used_before = row->used[TEXT_AREA];
12628 /* Add a space at the end of the line that is used to
12629 display the cursor there. */
12630 append_space (it, 0);
12632 /* Extend the face to the end of the line. */
12633 extend_face_to_end_of_line (it);
12635 /* Make sure we have the position. */
12636 if (used_before == 0)
12637 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12639 /* Consume the line end. This skips over invisible lines. */
12640 set_iterator_to_next (it, 1);
12641 it->continuation_lines_width = 0;
12642 break;
12645 /* Proceed with next display element. Note that this skips
12646 over lines invisible because of selective display. */
12647 set_iterator_to_next (it, 1);
12649 /* If we truncate lines, we are done when the last displayed
12650 glyphs reach past the right margin of the window. */
12651 if (it->truncate_lines_p
12652 && (FRAME_WINDOW_P (it->f)
12653 ? (it->current_x >= it->last_visible_x)
12654 : (it->current_x > it->last_visible_x)))
12656 /* Maybe add truncation glyphs. */
12657 if (!FRAME_WINDOW_P (it->f))
12659 int i, n;
12661 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12662 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12663 break;
12665 for (n = row->used[TEXT_AREA]; i < n; ++i)
12667 row->used[TEXT_AREA] = i;
12668 produce_special_glyphs (it, IT_TRUNCATION);
12672 row->truncated_on_right_p = 1;
12673 it->continuation_lines_width = 0;
12674 reseat_at_next_visible_line_start (it, 0);
12675 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12676 it->hpos = hpos_before;
12677 it->current_x = x_before;
12678 break;
12682 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12683 at the left window margin. */
12684 if (it->first_visible_x
12685 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12687 if (!FRAME_WINDOW_P (it->f))
12688 insert_left_trunc_glyphs (it);
12689 row->truncated_on_left_p = 1;
12692 /* If the start of this line is the overlay arrow-position, then
12693 mark this glyph row as the one containing the overlay arrow.
12694 This is clearly a mess with variable size fonts. It would be
12695 better to let it be displayed like cursors under X. */
12696 if (MARKERP (Voverlay_arrow_position)
12697 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12698 && (MATRIX_ROW_START_CHARPOS (row)
12699 == marker_position (Voverlay_arrow_position))
12700 && STRINGP (Voverlay_arrow_string)
12701 && ! overlay_arrow_seen)
12703 /* Overlay arrow in window redisplay is a bitmap. */
12704 if (!FRAME_WINDOW_P (it->f))
12706 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12707 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12708 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12709 struct glyph *p = row->glyphs[TEXT_AREA];
12710 struct glyph *p2, *end;
12712 /* Copy the arrow glyphs. */
12713 while (glyph < arrow_end)
12714 *p++ = *glyph++;
12716 /* Throw away padding glyphs. */
12717 p2 = p;
12718 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12719 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12720 ++p2;
12721 if (p2 > p)
12723 while (p2 < end)
12724 *p++ = *p2++;
12725 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12729 overlay_arrow_seen = 1;
12730 row->overlay_arrow_p = 1;
12733 /* Compute pixel dimensions of this line. */
12734 compute_line_metrics (it);
12736 /* Remember the position at which this line ends. */
12737 row->end = it->current;
12739 /* Maybe set the cursor. */
12740 if (it->w->cursor.vpos < 0
12741 && PT >= MATRIX_ROW_START_CHARPOS (row)
12742 && PT <= MATRIX_ROW_END_CHARPOS (row)
12743 && cursor_row_p (it->w, row))
12744 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12746 /* Highlight trailing whitespace. */
12747 if (!NILP (Vshow_trailing_whitespace))
12748 highlight_trailing_whitespace (it->f, it->glyph_row);
12750 /* Prepare for the next line. This line starts horizontally at (X
12751 HPOS) = (0 0). Vertical positions are incremented. As a
12752 convenience for the caller, IT->glyph_row is set to the next
12753 row to be used. */
12754 it->current_x = it->hpos = 0;
12755 it->current_y += row->height;
12756 ++it->vpos;
12757 ++it->glyph_row;
12758 return row->displays_text_p;
12763 /***********************************************************************
12764 Menu Bar
12765 ***********************************************************************/
12767 /* Redisplay the menu bar in the frame for window W.
12769 The menu bar of X frames that don't have X toolkit support is
12770 displayed in a special window W->frame->menu_bar_window.
12772 The menu bar of terminal frames is treated specially as far as
12773 glyph matrices are concerned. Menu bar lines are not part of
12774 windows, so the update is done directly on the frame matrix rows
12775 for the menu bar. */
12777 static void
12778 display_menu_bar (w)
12779 struct window *w;
12781 struct frame *f = XFRAME (WINDOW_FRAME (w));
12782 struct it it;
12783 Lisp_Object items;
12784 int i;
12786 /* Don't do all this for graphical frames. */
12787 #ifdef HAVE_NTGUI
12788 if (!NILP (Vwindow_system))
12789 return;
12790 #endif
12791 #ifdef USE_X_TOOLKIT
12792 if (FRAME_X_P (f))
12793 return;
12794 #endif
12795 #ifdef macintosh
12796 if (FRAME_MAC_P (f))
12797 return;
12798 #endif
12800 #ifdef USE_X_TOOLKIT
12801 xassert (!FRAME_WINDOW_P (f));
12802 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12803 it.first_visible_x = 0;
12804 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12805 #else /* not USE_X_TOOLKIT */
12806 if (FRAME_WINDOW_P (f))
12808 /* Menu bar lines are displayed in the desired matrix of the
12809 dummy window menu_bar_window. */
12810 struct window *menu_w;
12811 xassert (WINDOWP (f->menu_bar_window));
12812 menu_w = XWINDOW (f->menu_bar_window);
12813 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12814 MENU_FACE_ID);
12815 it.first_visible_x = 0;
12816 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12818 else
12820 /* This is a TTY frame, i.e. character hpos/vpos are used as
12821 pixel x/y. */
12822 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12823 MENU_FACE_ID);
12824 it.first_visible_x = 0;
12825 it.last_visible_x = FRAME_WIDTH (f);
12827 #endif /* not USE_X_TOOLKIT */
12829 if (! mode_line_inverse_video)
12830 /* Force the menu-bar to be displayed in the default face. */
12831 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12833 /* Clear all rows of the menu bar. */
12834 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12836 struct glyph_row *row = it.glyph_row + i;
12837 clear_glyph_row (row);
12838 row->enabled_p = 1;
12839 row->full_width_p = 1;
12842 /* Display all items of the menu bar. */
12843 items = FRAME_MENU_BAR_ITEMS (it.f);
12844 for (i = 0; i < XVECTOR (items)->size; i += 4)
12846 Lisp_Object string;
12848 /* Stop at nil string. */
12849 string = AREF (items, i + 1);
12850 if (NILP (string))
12851 break;
12853 /* Remember where item was displayed. */
12854 AREF (items, i + 3) = make_number (it.hpos);
12856 /* Display the item, pad with one space. */
12857 if (it.current_x < it.last_visible_x)
12858 display_string (NULL, string, Qnil, 0, 0, &it,
12859 XSTRING (string)->size + 1, 0, 0, -1);
12862 /* Fill out the line with spaces. */
12863 if (it.current_x < it.last_visible_x)
12864 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12866 /* Compute the total height of the lines. */
12867 compute_line_metrics (&it);
12872 /***********************************************************************
12873 Mode Line
12874 ***********************************************************************/
12876 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12877 FORCE is non-zero, redisplay mode lines unconditionally.
12878 Otherwise, redisplay only mode lines that are garbaged. Value is
12879 the number of windows whose mode lines were redisplayed. */
12881 static int
12882 redisplay_mode_lines (window, force)
12883 Lisp_Object window;
12884 int force;
12886 int nwindows = 0;
12888 while (!NILP (window))
12890 struct window *w = XWINDOW (window);
12892 if (WINDOWP (w->hchild))
12893 nwindows += redisplay_mode_lines (w->hchild, force);
12894 else if (WINDOWP (w->vchild))
12895 nwindows += redisplay_mode_lines (w->vchild, force);
12896 else if (force
12897 || FRAME_GARBAGED_P (XFRAME (w->frame))
12898 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12900 Lisp_Object old_selected_frame;
12901 struct text_pos lpoint;
12902 struct buffer *old = current_buffer;
12904 /* Set the window's buffer for the mode line display. */
12905 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12906 set_buffer_internal_1 (XBUFFER (w->buffer));
12908 /* Point refers normally to the selected window. For any
12909 other window, set up appropriate value. */
12910 if (!EQ (window, selected_window))
12912 struct text_pos pt;
12914 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12915 if (CHARPOS (pt) < BEGV)
12916 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12917 else if (CHARPOS (pt) > (ZV - 1))
12918 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12919 else
12920 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12923 /* Temporarily set up the selected frame. */
12924 old_selected_frame = selected_frame;
12925 selected_frame = w->frame;
12927 /* Display mode lines. */
12928 clear_glyph_matrix (w->desired_matrix);
12929 if (display_mode_lines (w))
12931 ++nwindows;
12932 w->must_be_updated_p = 1;
12935 /* Restore old settings. */
12936 selected_frame = old_selected_frame;
12937 set_buffer_internal_1 (old);
12938 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12941 window = w->next;
12944 return nwindows;
12948 /* Display the mode and/or top line of window W. Value is the number
12949 of mode lines displayed. */
12951 static int
12952 display_mode_lines (w)
12953 struct window *w;
12955 int n = 0;
12957 /* These will be set while the mode line specs are processed. */
12958 line_number_displayed = 0;
12959 w->column_number_displayed = Qnil;
12961 if (WINDOW_WANTS_MODELINE_P (w))
12963 display_mode_line (w, MODE_LINE_FACE_ID,
12964 current_buffer->mode_line_format);
12965 ++n;
12968 if (WINDOW_WANTS_HEADER_LINE_P (w))
12970 display_mode_line (w, HEADER_LINE_FACE_ID,
12971 current_buffer->header_line_format);
12972 ++n;
12975 return n;
12979 /* Display mode or top line of window W. FACE_ID specifies which line
12980 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12981 FORMAT is the mode line format to display. Value is the pixel
12982 height of the mode line displayed. */
12984 static int
12985 display_mode_line (w, face_id, format)
12986 struct window *w;
12987 enum face_id face_id;
12988 Lisp_Object format;
12990 struct it it;
12991 struct face *face;
12993 init_iterator (&it, w, -1, -1, NULL, face_id);
12994 prepare_desired_row (it.glyph_row);
12996 if (! mode_line_inverse_video)
12997 /* Force the mode-line to be displayed in the default face. */
12998 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13000 /* Temporarily make frame's keyboard the current kboard so that
13001 kboard-local variables in the mode_line_format will get the right
13002 values. */
13003 push_frame_kboard (it.f);
13004 display_mode_element (&it, 0, 0, 0, format);
13005 pop_frame_kboard ();
13007 /* Fill up with spaces. */
13008 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13010 compute_line_metrics (&it);
13011 it.glyph_row->full_width_p = 1;
13012 it.glyph_row->mode_line_p = 1;
13013 it.glyph_row->inverse_p = 0;
13014 it.glyph_row->continued_p = 0;
13015 it.glyph_row->truncated_on_left_p = 0;
13016 it.glyph_row->truncated_on_right_p = 0;
13018 /* Make a 3D mode-line have a shadow at its right end. */
13019 face = FACE_FROM_ID (it.f, face_id);
13020 extend_face_to_end_of_line (&it);
13021 if (face->box != FACE_NO_BOX)
13023 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13024 + it.glyph_row->used[TEXT_AREA] - 1);
13025 last->right_box_line_p = 1;
13028 return it.glyph_row->height;
13032 /* Contribute ELT to the mode line for window IT->w. How it
13033 translates into text depends on its data type.
13035 IT describes the display environment in which we display, as usual.
13037 DEPTH is the depth in recursion. It is used to prevent
13038 infinite recursion here.
13040 FIELD_WIDTH is the number of characters the display of ELT should
13041 occupy in the mode line, and PRECISION is the maximum number of
13042 characters to display from ELT's representation. See
13043 display_string for details. *
13045 Returns the hpos of the end of the text generated by ELT. */
13047 static int
13048 display_mode_element (it, depth, field_width, precision, elt)
13049 struct it *it;
13050 int depth;
13051 int field_width, precision;
13052 Lisp_Object elt;
13054 int n = 0, field, prec;
13056 tail_recurse:
13057 if (depth > 10)
13058 goto invalid;
13060 depth++;
13062 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13064 case Lisp_String:
13066 /* A string: output it and check for %-constructs within it. */
13067 unsigned char c;
13068 unsigned char *this = XSTRING (elt)->data;
13069 unsigned char *lisp_string = this;
13071 while ((precision <= 0 || n < precision)
13072 && *this
13073 && (frame_title_ptr
13074 || it->current_x < it->last_visible_x))
13076 unsigned char *last = this;
13078 /* Advance to end of string or next format specifier. */
13079 while ((c = *this++) != '\0' && c != '%')
13082 if (this - 1 != last)
13084 /* Output to end of string or up to '%'. Field width
13085 is length of string. Don't output more than
13086 PRECISION allows us. */
13087 --this;
13088 prec = strwidth (last, this - last);
13089 if (precision > 0 && prec > precision - n)
13090 prec = precision - n;
13092 if (frame_title_ptr)
13093 n += store_frame_title (last, 0, prec);
13094 else
13095 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13096 it, 0, prec, 0, -1);
13098 else /* c == '%' */
13100 unsigned char *percent_position = this;
13102 /* Get the specified minimum width. Zero means
13103 don't pad. */
13104 field = 0;
13105 while ((c = *this++) >= '0' && c <= '9')
13106 field = field * 10 + c - '0';
13108 /* Don't pad beyond the total padding allowed. */
13109 if (field_width - n > 0 && field > field_width - n)
13110 field = field_width - n;
13112 /* Note that either PRECISION <= 0 or N < PRECISION. */
13113 prec = precision - n;
13115 if (c == 'M')
13116 n += display_mode_element (it, depth, field, prec,
13117 Vglobal_mode_string);
13118 else if (c != 0)
13120 unsigned char *spec
13121 = decode_mode_spec (it->w, c, field, prec);
13123 if (frame_title_ptr)
13124 n += store_frame_title (spec, field, prec);
13125 else
13127 int nglyphs_before
13128 = it->glyph_row->used[TEXT_AREA];
13129 int charpos
13130 = percent_position - XSTRING (elt)->data;
13131 int nwritten
13132 = display_string (spec, Qnil, elt, charpos, 0, it,
13133 field, prec, 0, -1);
13135 /* Assign to the glyphs written above the
13136 string where the `%x' came from, position
13137 of the `%'. */
13138 if (nwritten > 0)
13140 struct glyph *glyph
13141 = (it->glyph_row->glyphs[TEXT_AREA]
13142 + nglyphs_before);
13143 int i;
13145 for (i = 0; i < nwritten; ++i)
13147 glyph[i].object = elt;
13148 glyph[i].charpos = charpos;
13151 n += nwritten;
13158 break;
13160 case Lisp_Symbol:
13161 /* A symbol: process the value of the symbol recursively
13162 as if it appeared here directly. Avoid error if symbol void.
13163 Special case: if value of symbol is a string, output the string
13164 literally. */
13166 register Lisp_Object tem;
13167 tem = Fboundp (elt);
13168 if (!NILP (tem))
13170 tem = Fsymbol_value (elt);
13171 /* If value is a string, output that string literally:
13172 don't check for % within it. */
13173 if (STRINGP (tem))
13175 prec = precision - n;
13176 if (frame_title_ptr)
13177 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13178 else
13179 n += display_string (NULL, tem, Qnil, 0, 0, it,
13180 0, prec, 0, -1);
13182 else if (!EQ (tem, elt))
13184 /* Give up right away for nil or t. */
13185 elt = tem;
13186 goto tail_recurse;
13190 break;
13192 case Lisp_Cons:
13194 register Lisp_Object car, tem;
13196 /* A cons cell: three distinct cases.
13197 If first element is a string or a cons, process all the elements
13198 and effectively concatenate them.
13199 If first element is a negative number, truncate displaying cdr to
13200 at most that many characters. If positive, pad (with spaces)
13201 to at least that many characters.
13202 If first element is a symbol, process the cadr or caddr recursively
13203 according to whether the symbol's value is non-nil or nil. */
13204 car = XCAR (elt);
13205 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13207 /* An element of the form (:eval FORM) means evaluate FORM
13208 and use the result as mode line elements. */
13209 struct gcpro gcpro1;
13210 Lisp_Object spec;
13212 spec = safe_eval (XCAR (XCDR (elt)));
13213 GCPRO1 (spec);
13214 n += display_mode_element (it, depth, field_width - n,
13215 precision - n, spec);
13216 UNGCPRO;
13218 else if (SYMBOLP (car))
13220 tem = Fboundp (car);
13221 elt = XCDR (elt);
13222 if (!CONSP (elt))
13223 goto invalid;
13224 /* elt is now the cdr, and we know it is a cons cell.
13225 Use its car if CAR has a non-nil value. */
13226 if (!NILP (tem))
13228 tem = Fsymbol_value (car);
13229 if (!NILP (tem))
13231 elt = XCAR (elt);
13232 goto tail_recurse;
13235 /* Symbol's value is nil (or symbol is unbound)
13236 Get the cddr of the original list
13237 and if possible find the caddr and use that. */
13238 elt = XCDR (elt);
13239 if (NILP (elt))
13240 break;
13241 else if (!CONSP (elt))
13242 goto invalid;
13243 elt = XCAR (elt);
13244 goto tail_recurse;
13246 else if (INTEGERP (car))
13248 register int lim = XINT (car);
13249 elt = XCDR (elt);
13250 if (lim < 0)
13252 /* Negative int means reduce maximum width. */
13253 if (precision <= 0)
13254 precision = -lim;
13255 else
13256 precision = min (precision, -lim);
13258 else if (lim > 0)
13260 /* Padding specified. Don't let it be more than
13261 current maximum. */
13262 if (precision > 0)
13263 lim = min (precision, lim);
13265 /* If that's more padding than already wanted, queue it.
13266 But don't reduce padding already specified even if
13267 that is beyond the current truncation point. */
13268 field_width = max (lim, field_width);
13270 goto tail_recurse;
13272 else if (STRINGP (car) || CONSP (car))
13274 register int limit = 50;
13275 /* Limit is to protect against circular lists. */
13276 while (CONSP (elt)
13277 && --limit > 0
13278 && (precision <= 0 || n < precision))
13280 n += display_mode_element (it, depth, field_width - n,
13281 precision - n, XCAR (elt));
13282 elt = XCDR (elt);
13286 break;
13288 default:
13289 invalid:
13290 if (frame_title_ptr)
13291 n += store_frame_title ("*invalid*", 0, precision - n);
13292 else
13293 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13294 precision - n, 0, 0);
13295 return n;
13298 /* Pad to FIELD_WIDTH. */
13299 if (field_width > 0 && n < field_width)
13301 if (frame_title_ptr)
13302 n += store_frame_title ("", field_width - n, 0);
13303 else
13304 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13305 0, 0, 0);
13308 return n;
13312 /* Write a null-terminated, right justified decimal representation of
13313 the positive integer D to BUF using a minimal field width WIDTH. */
13315 static void
13316 pint2str (buf, width, d)
13317 register char *buf;
13318 register int width;
13319 register int d;
13321 register char *p = buf;
13323 if (d <= 0)
13324 *p++ = '0';
13325 else
13327 while (d > 0)
13329 *p++ = d % 10 + '0';
13330 d /= 10;
13334 for (width -= (int) (p - buf); width > 0; --width)
13335 *p++ = ' ';
13336 *p-- = '\0';
13337 while (p > buf)
13339 d = *buf;
13340 *buf++ = *p;
13341 *p-- = d;
13345 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13346 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13347 type of CODING_SYSTEM. Return updated pointer into BUF. */
13349 static unsigned char invalid_eol_type[] = "(*invalid*)";
13351 static char *
13352 decode_mode_spec_coding (coding_system, buf, eol_flag)
13353 Lisp_Object coding_system;
13354 register char *buf;
13355 int eol_flag;
13357 Lisp_Object val;
13358 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13359 unsigned char *eol_str;
13360 int eol_str_len;
13361 /* The EOL conversion we are using. */
13362 Lisp_Object eoltype;
13364 val = Fget (coding_system, Qcoding_system);
13365 eoltype = Qnil;
13367 if (!VECTORP (val)) /* Not yet decided. */
13369 if (multibyte)
13370 *buf++ = '-';
13371 if (eol_flag)
13372 eoltype = eol_mnemonic_undecided;
13373 /* Don't mention EOL conversion if it isn't decided. */
13375 else
13377 Lisp_Object eolvalue;
13379 eolvalue = Fget (coding_system, Qeol_type);
13381 if (multibyte)
13382 *buf++ = XFASTINT (AREF (val, 1));
13384 if (eol_flag)
13386 /* The EOL conversion that is normal on this system. */
13388 if (NILP (eolvalue)) /* Not yet decided. */
13389 eoltype = eol_mnemonic_undecided;
13390 else if (VECTORP (eolvalue)) /* Not yet decided. */
13391 eoltype = eol_mnemonic_undecided;
13392 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13393 eoltype = (XFASTINT (eolvalue) == 0
13394 ? eol_mnemonic_unix
13395 : (XFASTINT (eolvalue) == 1
13396 ? eol_mnemonic_dos : eol_mnemonic_mac));
13400 if (eol_flag)
13402 /* Mention the EOL conversion if it is not the usual one. */
13403 if (STRINGP (eoltype))
13405 eol_str = XSTRING (eoltype)->data;
13406 eol_str_len = XSTRING (eoltype)->size;
13408 else if (INTEGERP (eoltype)
13409 && CHAR_VALID_P (XINT (eoltype), 0))
13411 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13412 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13414 else
13416 eol_str = invalid_eol_type;
13417 eol_str_len = sizeof (invalid_eol_type) - 1;
13419 bcopy (eol_str, buf, eol_str_len);
13420 buf += eol_str_len;
13423 return buf;
13426 /* Return a string for the output of a mode line %-spec for window W,
13427 generated by character C. PRECISION >= 0 means don't return a
13428 string longer than that value. FIELD_WIDTH > 0 means pad the
13429 string returned with spaces to that value. */
13431 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13433 static char *
13434 decode_mode_spec (w, c, field_width, precision)
13435 struct window *w;
13436 register int c;
13437 int field_width, precision;
13439 Lisp_Object obj;
13440 struct frame *f = XFRAME (WINDOW_FRAME (w));
13441 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13442 struct buffer *b = XBUFFER (w->buffer);
13444 obj = Qnil;
13446 switch (c)
13448 case '*':
13449 if (!NILP (b->read_only))
13450 return "%";
13451 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13452 return "*";
13453 return "-";
13455 case '+':
13456 /* This differs from %* only for a modified read-only buffer. */
13457 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13458 return "*";
13459 if (!NILP (b->read_only))
13460 return "%";
13461 return "-";
13463 case '&':
13464 /* This differs from %* in ignoring read-only-ness. */
13465 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13466 return "*";
13467 return "-";
13469 case '%':
13470 return "%";
13472 case '[':
13474 int i;
13475 char *p;
13477 if (command_loop_level > 5)
13478 return "[[[... ";
13479 p = decode_mode_spec_buf;
13480 for (i = 0; i < command_loop_level; i++)
13481 *p++ = '[';
13482 *p = 0;
13483 return decode_mode_spec_buf;
13486 case ']':
13488 int i;
13489 char *p;
13491 if (command_loop_level > 5)
13492 return " ...]]]";
13493 p = decode_mode_spec_buf;
13494 for (i = 0; i < command_loop_level; i++)
13495 *p++ = ']';
13496 *p = 0;
13497 return decode_mode_spec_buf;
13500 case '-':
13502 register int i;
13504 /* Let lots_of_dashes be a string of infinite length. */
13505 if (field_width <= 0
13506 || field_width > sizeof (lots_of_dashes))
13508 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13509 decode_mode_spec_buf[i] = '-';
13510 decode_mode_spec_buf[i] = '\0';
13511 return decode_mode_spec_buf;
13513 else
13514 return lots_of_dashes;
13517 case 'b':
13518 obj = b->name;
13519 break;
13521 case 'c':
13523 int col = current_column ();
13524 w->column_number_displayed = make_number (col);
13525 pint2str (decode_mode_spec_buf, field_width, col);
13526 return decode_mode_spec_buf;
13529 case 'F':
13530 /* %F displays the frame name. */
13531 if (!NILP (f->title))
13532 return (char *) XSTRING (f->title)->data;
13533 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13534 return (char *) XSTRING (f->name)->data;
13535 return "Emacs";
13537 case 'f':
13538 obj = b->filename;
13539 break;
13541 case 'l':
13543 int startpos = XMARKER (w->start)->charpos;
13544 int startpos_byte = marker_byte_position (w->start);
13545 int line, linepos, linepos_byte, topline;
13546 int nlines, junk;
13547 int height = XFASTINT (w->height);
13549 /* If we decided that this buffer isn't suitable for line numbers,
13550 don't forget that too fast. */
13551 if (EQ (w->base_line_pos, w->buffer))
13552 goto no_value;
13553 /* But do forget it, if the window shows a different buffer now. */
13554 else if (BUFFERP (w->base_line_pos))
13555 w->base_line_pos = Qnil;
13557 /* If the buffer is very big, don't waste time. */
13558 if (INTEGERP (Vline_number_display_limit)
13559 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13561 w->base_line_pos = Qnil;
13562 w->base_line_number = Qnil;
13563 goto no_value;
13566 if (!NILP (w->base_line_number)
13567 && !NILP (w->base_line_pos)
13568 && XFASTINT (w->base_line_pos) <= startpos)
13570 line = XFASTINT (w->base_line_number);
13571 linepos = XFASTINT (w->base_line_pos);
13572 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13574 else
13576 line = 1;
13577 linepos = BUF_BEGV (b);
13578 linepos_byte = BUF_BEGV_BYTE (b);
13581 /* Count lines from base line to window start position. */
13582 nlines = display_count_lines (linepos, linepos_byte,
13583 startpos_byte,
13584 startpos, &junk);
13586 topline = nlines + line;
13588 /* Determine a new base line, if the old one is too close
13589 or too far away, or if we did not have one.
13590 "Too close" means it's plausible a scroll-down would
13591 go back past it. */
13592 if (startpos == BUF_BEGV (b))
13594 w->base_line_number = make_number (topline);
13595 w->base_line_pos = make_number (BUF_BEGV (b));
13597 else if (nlines < height + 25 || nlines > height * 3 + 50
13598 || linepos == BUF_BEGV (b))
13600 int limit = BUF_BEGV (b);
13601 int limit_byte = BUF_BEGV_BYTE (b);
13602 int position;
13603 int distance = (height * 2 + 30) * line_number_display_limit_width;
13605 if (startpos - distance > limit)
13607 limit = startpos - distance;
13608 limit_byte = CHAR_TO_BYTE (limit);
13611 nlines = display_count_lines (startpos, startpos_byte,
13612 limit_byte,
13613 - (height * 2 + 30),
13614 &position);
13615 /* If we couldn't find the lines we wanted within
13616 line_number_display_limit_width chars per line,
13617 give up on line numbers for this window. */
13618 if (position == limit_byte && limit == startpos - distance)
13620 w->base_line_pos = w->buffer;
13621 w->base_line_number = Qnil;
13622 goto no_value;
13625 w->base_line_number = make_number (topline - nlines);
13626 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13629 /* Now count lines from the start pos to point. */
13630 nlines = display_count_lines (startpos, startpos_byte,
13631 PT_BYTE, PT, &junk);
13633 /* Record that we did display the line number. */
13634 line_number_displayed = 1;
13636 /* Make the string to show. */
13637 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13638 return decode_mode_spec_buf;
13639 no_value:
13641 char* p = decode_mode_spec_buf;
13642 int pad = field_width - 2;
13643 while (pad-- > 0)
13644 *p++ = ' ';
13645 *p++ = '?';
13646 *p++ = '?';
13647 *p = '\0';
13648 return decode_mode_spec_buf;
13651 break;
13653 case 'm':
13654 obj = b->mode_name;
13655 break;
13657 case 'n':
13658 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13659 return " Narrow";
13660 break;
13662 case 'p':
13664 int pos = marker_position (w->start);
13665 int total = BUF_ZV (b) - BUF_BEGV (b);
13667 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13669 if (pos <= BUF_BEGV (b))
13670 return "All";
13671 else
13672 return "Bottom";
13674 else if (pos <= BUF_BEGV (b))
13675 return "Top";
13676 else
13678 if (total > 1000000)
13679 /* Do it differently for a large value, to avoid overflow. */
13680 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13681 else
13682 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13683 /* We can't normally display a 3-digit number,
13684 so get us a 2-digit number that is close. */
13685 if (total == 100)
13686 total = 99;
13687 sprintf (decode_mode_spec_buf, "%2d%%", total);
13688 return decode_mode_spec_buf;
13692 /* Display percentage of size above the bottom of the screen. */
13693 case 'P':
13695 int toppos = marker_position (w->start);
13696 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13697 int total = BUF_ZV (b) - BUF_BEGV (b);
13699 if (botpos >= BUF_ZV (b))
13701 if (toppos <= BUF_BEGV (b))
13702 return "All";
13703 else
13704 return "Bottom";
13706 else
13708 if (total > 1000000)
13709 /* Do it differently for a large value, to avoid overflow. */
13710 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13711 else
13712 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13713 /* We can't normally display a 3-digit number,
13714 so get us a 2-digit number that is close. */
13715 if (total == 100)
13716 total = 99;
13717 if (toppos <= BUF_BEGV (b))
13718 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13719 else
13720 sprintf (decode_mode_spec_buf, "%2d%%", total);
13721 return decode_mode_spec_buf;
13725 case 's':
13726 /* status of process */
13727 obj = Fget_buffer_process (w->buffer);
13728 if (NILP (obj))
13729 return "no process";
13730 #ifdef subprocesses
13731 obj = Fsymbol_name (Fprocess_status (obj));
13732 #endif
13733 break;
13735 case 't': /* indicate TEXT or BINARY */
13736 #ifdef MODE_LINE_BINARY_TEXT
13737 return MODE_LINE_BINARY_TEXT (b);
13738 #else
13739 return "T";
13740 #endif
13742 case 'z':
13743 /* coding-system (not including end-of-line format) */
13744 case 'Z':
13745 /* coding-system (including end-of-line type) */
13747 int eol_flag = (c == 'Z');
13748 char *p = decode_mode_spec_buf;
13750 if (! FRAME_WINDOW_P (f))
13752 /* No need to mention EOL here--the terminal never needs
13753 to do EOL conversion. */
13754 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13755 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13757 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13758 p, eol_flag);
13760 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13761 #ifdef subprocesses
13762 obj = Fget_buffer_process (Fcurrent_buffer ());
13763 if (PROCESSP (obj))
13765 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13766 p, eol_flag);
13767 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13768 p, eol_flag);
13770 #endif /* subprocesses */
13771 #endif /* 0 */
13772 *p = 0;
13773 return decode_mode_spec_buf;
13777 if (STRINGP (obj))
13778 return (char *) XSTRING (obj)->data;
13779 else
13780 return "";
13784 /* Count up to COUNT lines starting from START / START_BYTE.
13785 But don't go beyond LIMIT_BYTE.
13786 Return the number of lines thus found (always nonnegative).
13788 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13790 static int
13791 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13792 int start, start_byte, limit_byte, count;
13793 int *byte_pos_ptr;
13795 register unsigned char *cursor;
13796 unsigned char *base;
13798 register int ceiling;
13799 register unsigned char *ceiling_addr;
13800 int orig_count = count;
13802 /* If we are not in selective display mode,
13803 check only for newlines. */
13804 int selective_display = (!NILP (current_buffer->selective_display)
13805 && !INTEGERP (current_buffer->selective_display));
13807 if (count > 0)
13809 while (start_byte < limit_byte)
13811 ceiling = BUFFER_CEILING_OF (start_byte);
13812 ceiling = min (limit_byte - 1, ceiling);
13813 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13814 base = (cursor = BYTE_POS_ADDR (start_byte));
13815 while (1)
13817 if (selective_display)
13818 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13820 else
13821 while (*cursor != '\n' && ++cursor != ceiling_addr)
13824 if (cursor != ceiling_addr)
13826 if (--count == 0)
13828 start_byte += cursor - base + 1;
13829 *byte_pos_ptr = start_byte;
13830 return orig_count;
13832 else
13833 if (++cursor == ceiling_addr)
13834 break;
13836 else
13837 break;
13839 start_byte += cursor - base;
13842 else
13844 while (start_byte > limit_byte)
13846 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13847 ceiling = max (limit_byte, ceiling);
13848 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13849 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13850 while (1)
13852 if (selective_display)
13853 while (--cursor != ceiling_addr
13854 && *cursor != '\n' && *cursor != 015)
13856 else
13857 while (--cursor != ceiling_addr && *cursor != '\n')
13860 if (cursor != ceiling_addr)
13862 if (++count == 0)
13864 start_byte += cursor - base + 1;
13865 *byte_pos_ptr = start_byte;
13866 /* When scanning backwards, we should
13867 not count the newline posterior to which we stop. */
13868 return - orig_count - 1;
13871 else
13872 break;
13874 /* Here we add 1 to compensate for the last decrement
13875 of CURSOR, which took it past the valid range. */
13876 start_byte += cursor - base + 1;
13880 *byte_pos_ptr = limit_byte;
13882 if (count < 0)
13883 return - orig_count + count;
13884 return orig_count - count;
13890 /***********************************************************************
13891 Displaying strings
13892 ***********************************************************************/
13894 /* Display a NUL-terminated string, starting with index START.
13896 If STRING is non-null, display that C string. Otherwise, the Lisp
13897 string LISP_STRING is displayed.
13899 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13900 FACE_STRING. Display STRING or LISP_STRING with the face at
13901 FACE_STRING_POS in FACE_STRING:
13903 Display the string in the environment given by IT, but use the
13904 standard display table, temporarily.
13906 FIELD_WIDTH is the minimum number of output glyphs to produce.
13907 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13908 with spaces. If STRING has more characters, more than FIELD_WIDTH
13909 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13911 PRECISION is the maximum number of characters to output from
13912 STRING. PRECISION < 0 means don't truncate the string.
13914 This is roughly equivalent to printf format specifiers:
13916 FIELD_WIDTH PRECISION PRINTF
13917 ----------------------------------------
13918 -1 -1 %s
13919 -1 10 %.10s
13920 10 -1 %10s
13921 20 10 %20.10s
13923 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13924 display them, and < 0 means obey the current buffer's value of
13925 enable_multibyte_characters.
13927 Value is the number of glyphs produced. */
13929 static int
13930 display_string (string, lisp_string, face_string, face_string_pos,
13931 start, it, field_width, precision, max_x, multibyte)
13932 unsigned char *string;
13933 Lisp_Object lisp_string;
13934 Lisp_Object face_string;
13935 int face_string_pos;
13936 int start;
13937 struct it *it;
13938 int field_width, precision, max_x;
13939 int multibyte;
13941 int hpos_at_start = it->hpos;
13942 int saved_face_id = it->face_id;
13943 struct glyph_row *row = it->glyph_row;
13945 /* Initialize the iterator IT for iteration over STRING beginning
13946 with index START. We assume that IT may be modified here (which
13947 means that display_line has to do something when displaying a
13948 mini-buffer prompt, which it does). */
13949 reseat_to_string (it, string, lisp_string, start,
13950 precision, field_width, multibyte);
13952 /* If displaying STRING, set up the face of the iterator
13953 from LISP_STRING, if that's given. */
13954 if (STRINGP (face_string))
13956 int endptr;
13957 struct face *face;
13959 it->face_id
13960 = face_at_string_position (it->w, face_string, face_string_pos,
13961 0, it->region_beg_charpos,
13962 it->region_end_charpos,
13963 &endptr, it->base_face_id, 0);
13964 face = FACE_FROM_ID (it->f, it->face_id);
13965 it->face_box_p = face->box != FACE_NO_BOX;
13968 /* Set max_x to the maximum allowed X position. Don't let it go
13969 beyond the right edge of the window. */
13970 if (max_x <= 0)
13971 max_x = it->last_visible_x;
13972 else
13973 max_x = min (max_x, it->last_visible_x);
13975 /* Skip over display elements that are not visible. because IT->w is
13976 hscrolled. */
13977 if (it->current_x < it->first_visible_x)
13978 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13979 MOVE_TO_POS | MOVE_TO_X);
13981 row->ascent = it->max_ascent;
13982 row->height = it->max_ascent + it->max_descent;
13983 row->phys_ascent = it->max_phys_ascent;
13984 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13986 /* This condition is for the case that we are called with current_x
13987 past last_visible_x. */
13988 while (it->current_x < max_x)
13990 int x_before, x, n_glyphs_before, i, nglyphs;
13992 /* Get the next display element. */
13993 if (!get_next_display_element (it))
13994 break;
13996 /* Produce glyphs. */
13997 x_before = it->current_x;
13998 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13999 PRODUCE_GLYPHS (it);
14001 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14002 i = 0;
14003 x = x_before;
14004 while (i < nglyphs)
14006 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14008 if (!it->truncate_lines_p
14009 && x + glyph->pixel_width > max_x)
14011 /* End of continued line or max_x reached. */
14012 if (CHAR_GLYPH_PADDING_P (*glyph))
14014 /* A wide character is unbreakable. */
14015 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14016 it->current_x = x_before;
14018 else
14020 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14021 it->current_x = x;
14023 break;
14025 else if (x + glyph->pixel_width > it->first_visible_x)
14027 /* Glyph is at least partially visible. */
14028 ++it->hpos;
14029 if (x < it->first_visible_x)
14030 it->glyph_row->x = x - it->first_visible_x;
14032 else
14034 /* Glyph is off the left margin of the display area.
14035 Should not happen. */
14036 abort ();
14039 row->ascent = max (row->ascent, it->max_ascent);
14040 row->height = max (row->height, it->max_ascent + it->max_descent);
14041 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14042 row->phys_height = max (row->phys_height,
14043 it->max_phys_ascent + it->max_phys_descent);
14044 x += glyph->pixel_width;
14045 ++i;
14048 /* Stop if max_x reached. */
14049 if (i < nglyphs)
14050 break;
14052 /* Stop at line ends. */
14053 if (ITERATOR_AT_END_OF_LINE_P (it))
14055 it->continuation_lines_width = 0;
14056 break;
14059 set_iterator_to_next (it, 1);
14061 /* Stop if truncating at the right edge. */
14062 if (it->truncate_lines_p
14063 && it->current_x >= it->last_visible_x)
14065 /* Add truncation mark, but don't do it if the line is
14066 truncated at a padding space. */
14067 if (IT_CHARPOS (*it) < it->string_nchars)
14069 if (!FRAME_WINDOW_P (it->f))
14071 int i, n;
14073 if (it->current_x > it->last_visible_x)
14075 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14076 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14077 break;
14078 for (n = row->used[TEXT_AREA]; i < n; ++i)
14080 row->used[TEXT_AREA] = i;
14081 produce_special_glyphs (it, IT_TRUNCATION);
14084 produce_special_glyphs (it, IT_TRUNCATION);
14086 it->glyph_row->truncated_on_right_p = 1;
14088 break;
14092 /* Maybe insert a truncation at the left. */
14093 if (it->first_visible_x
14094 && IT_CHARPOS (*it) > 0)
14096 if (!FRAME_WINDOW_P (it->f))
14097 insert_left_trunc_glyphs (it);
14098 it->glyph_row->truncated_on_left_p = 1;
14101 it->face_id = saved_face_id;
14103 /* Value is number of columns displayed. */
14104 return it->hpos - hpos_at_start;
14109 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14110 appears as an element of LIST or as the car of an element of LIST.
14111 If PROPVAL is a list, compare each element against LIST in that
14112 way, and return 1 if any element of PROPVAL is found in LIST.
14113 Otherwise return 0. This function cannot quit. */
14116 invisible_p (propval, list)
14117 register Lisp_Object propval;
14118 Lisp_Object list;
14120 register Lisp_Object tail, proptail;
14122 for (tail = list; CONSP (tail); tail = XCDR (tail))
14124 register Lisp_Object tem;
14125 tem = XCAR (tail);
14126 if (EQ (propval, tem))
14127 return 1;
14128 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14129 return 1;
14132 if (CONSP (propval))
14134 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14136 Lisp_Object propelt;
14137 propelt = XCAR (proptail);
14138 for (tail = list; CONSP (tail); tail = XCDR (tail))
14140 register Lisp_Object tem;
14141 tem = XCAR (tail);
14142 if (EQ (propelt, tem))
14143 return 1;
14144 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14145 return 1;
14150 return 0;
14154 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14155 the cdr of that element is non-nil. If PROPVAL is a list, check
14156 each element of PROPVAL in that way, and the first time some
14157 element is found, return 1 if the cdr of that element is non-nil.
14158 Otherwise return 0. This function cannot quit. */
14161 invisible_ellipsis_p (propval, list)
14162 register Lisp_Object propval;
14163 Lisp_Object list;
14165 register Lisp_Object tail, proptail;
14167 for (tail = list; CONSP (tail); tail = XCDR (tail))
14169 register Lisp_Object tem;
14170 tem = XCAR (tail);
14171 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14172 return ! NILP (XCDR (tem));
14175 if (CONSP (propval))
14176 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14178 Lisp_Object propelt;
14179 propelt = XCAR (proptail);
14180 for (tail = list; CONSP (tail); tail = XCDR (tail))
14182 register Lisp_Object tem;
14183 tem = XCAR (tail);
14184 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14185 return ! NILP (XCDR (tem));
14189 return 0;
14194 /***********************************************************************
14195 Initialization
14196 ***********************************************************************/
14198 void
14199 syms_of_xdisp ()
14201 Vwith_echo_area_save_vector = Qnil;
14202 staticpro (&Vwith_echo_area_save_vector);
14204 Vmessage_stack = Qnil;
14205 staticpro (&Vmessage_stack);
14207 Qinhibit_redisplay = intern ("inhibit-redisplay");
14208 staticpro (&Qinhibit_redisplay);
14210 #if GLYPH_DEBUG
14211 defsubr (&Sdump_glyph_matrix);
14212 defsubr (&Sdump_glyph_row);
14213 defsubr (&Sdump_tool_bar_row);
14214 defsubr (&Strace_redisplay_toggle);
14215 defsubr (&Strace_to_stderr);
14216 #endif
14217 #ifdef HAVE_WINDOW_SYSTEM
14218 defsubr (&Stool_bar_lines_needed);
14219 #endif
14221 staticpro (&Qmenu_bar_update_hook);
14222 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14224 staticpro (&Qoverriding_terminal_local_map);
14225 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14227 staticpro (&Qoverriding_local_map);
14228 Qoverriding_local_map = intern ("overriding-local-map");
14230 staticpro (&Qwindow_scroll_functions);
14231 Qwindow_scroll_functions = intern ("window-scroll-functions");
14233 staticpro (&Qredisplay_end_trigger_functions);
14234 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14236 staticpro (&Qinhibit_point_motion_hooks);
14237 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14239 QCdata = intern (":data");
14240 staticpro (&QCdata);
14241 Qdisplay = intern ("display");
14242 staticpro (&Qdisplay);
14243 Qspace_width = intern ("space-width");
14244 staticpro (&Qspace_width);
14245 Qraise = intern ("raise");
14246 staticpro (&Qraise);
14247 Qspace = intern ("space");
14248 staticpro (&Qspace);
14249 Qmargin = intern ("margin");
14250 staticpro (&Qmargin);
14251 Qleft_margin = intern ("left-margin");
14252 staticpro (&Qleft_margin);
14253 Qright_margin = intern ("right-margin");
14254 staticpro (&Qright_margin);
14255 Qalign_to = intern ("align-to");
14256 staticpro (&Qalign_to);
14257 QCalign_to = intern (":align-to");
14258 staticpro (&QCalign_to);
14259 Qrelative_width = intern ("relative-width");
14260 staticpro (&Qrelative_width);
14261 QCrelative_width = intern (":relative-width");
14262 staticpro (&QCrelative_width);
14263 QCrelative_height = intern (":relative-height");
14264 staticpro (&QCrelative_height);
14265 QCeval = intern (":eval");
14266 staticpro (&QCeval);
14267 Qwhen = intern ("when");
14268 staticpro (&Qwhen);
14269 QCfile = intern (":file");
14270 staticpro (&QCfile);
14271 Qfontified = intern ("fontified");
14272 staticpro (&Qfontified);
14273 Qfontification_functions = intern ("fontification-functions");
14274 staticpro (&Qfontification_functions);
14275 Qtrailing_whitespace = intern ("trailing-whitespace");
14276 staticpro (&Qtrailing_whitespace);
14277 Qimage = intern ("image");
14278 staticpro (&Qimage);
14279 Qmessage_truncate_lines = intern ("message-truncate-lines");
14280 staticpro (&Qmessage_truncate_lines);
14281 Qgrow_only = intern ("grow-only");
14282 staticpro (&Qgrow_only);
14283 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14284 staticpro (&Qinhibit_menubar_update);
14286 last_arrow_position = Qnil;
14287 last_arrow_string = Qnil;
14288 staticpro (&last_arrow_position);
14289 staticpro (&last_arrow_string);
14291 echo_buffer[0] = echo_buffer[1] = Qnil;
14292 staticpro (&echo_buffer[0]);
14293 staticpro (&echo_buffer[1]);
14295 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14296 staticpro (&echo_area_buffer[0]);
14297 staticpro (&echo_area_buffer[1]);
14299 Vmessages_buffer_name = build_string ("*Messages*");
14300 staticpro (&Vmessages_buffer_name);
14302 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14303 "Non-nil means highlight trailing whitespace.\n\
14304 The face used for trailing whitespace is `trailing-whitespace'.");
14305 Vshow_trailing_whitespace = Qnil;
14307 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14308 "Non-nil means don't actually do any redisplay.\n\
14309 This is used for internal purposes.");
14310 Vinhibit_redisplay = Qnil;
14312 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14313 "String (or mode line construct) included (normally) in `mode-line-format'.");
14314 Vglobal_mode_string = Qnil;
14316 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14317 "Marker for where to display an arrow on top of the buffer text.\n\
14318 This must be the beginning of a line in order to work.\n\
14319 See also `overlay-arrow-string'.");
14320 Voverlay_arrow_position = Qnil;
14322 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14323 "String to display as an arrow. See also `overlay-arrow-position'.");
14324 Voverlay_arrow_string = Qnil;
14326 DEFVAR_INT ("scroll-step", &scroll_step,
14327 "*The number of lines to try scrolling a window by when point moves out.\n\
14328 If that fails to bring point back on frame, point is centered instead.\n\
14329 If this is zero, point is always centered after it moves off frame.\n\
14330 If you want scrolling to always be a line at a time, you should set\n\
14331 `scroll-conservatively' to a large value rather than set this to 1.");
14333 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14334 "*Scroll up to this many lines, to bring point back on screen.\n\
14335 A value of zero means to scroll the text to center point vertically\n\
14336 in the window.");
14337 scroll_conservatively = 0;
14339 DEFVAR_INT ("scroll-margin", &scroll_margin,
14340 "*Number of lines of margin at the top and bottom of a window.\n\
14341 Recenter the window whenever point gets within this many lines\n\
14342 of the top or bottom of the window.");
14343 scroll_margin = 0;
14345 #if GLYPH_DEBUG
14346 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14347 #endif
14349 DEFVAR_BOOL ("truncate-partial-width-windows",
14350 &truncate_partial_width_windows,
14351 "*Non-nil means truncate lines in all windows less than full frame wide.");
14352 truncate_partial_width_windows = 1;
14354 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14355 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14356 Any other value means to use the appropriate face, `mode-line',\n\
14357 `header-line', or `menu' respectively.\n\
14359 This variable is deprecated; please change the above faces instead.");
14360 mode_line_inverse_video = 1;
14362 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14363 "*Maximum buffer size for which line number should be displayed.\n\
14364 If the buffer is bigger than this, the line number does not appear\n\
14365 in the mode line. A value of nil means no limit.");
14366 Vline_number_display_limit = Qnil;
14368 DEFVAR_INT ("line-number-display-limit-width",
14369 &line_number_display_limit_width,
14370 "*Maximum line width (in characters) for line number display.\n\
14371 If the average length of the lines near point is bigger than this, then the\n\
14372 line number may be omitted from the mode line.");
14373 line_number_display_limit_width = 200;
14375 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14376 "*Non-nil means highlight region even in nonselected windows.");
14377 highlight_nonselected_windows = 0;
14379 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14380 "Non-nil if more than one frame is visible on this display.\n\
14381 Minibuffer-only frames don't count, but iconified frames do.\n\
14382 This variable is not guaranteed to be accurate except while processing\n\
14383 `frame-title-format' and `icon-title-format'.");
14385 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14386 "Template for displaying the title bar of visible frames.\n\
14387 \(Assuming the window manager supports this feature.)\n\
14388 This variable has the same structure as `mode-line-format' (which see),\n\
14389 and is used only on frames for which no explicit name has been set\n\
14390 \(see `modify-frame-parameters').");
14391 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14392 "Template for displaying the title bar of an iconified frame.\n\
14393 \(Assuming the window manager supports this feature.)\n\
14394 This variable has the same structure as `mode-line-format' (which see),\n\
14395 and is used only on frames for which no explicit name has been set\n\
14396 \(see `modify-frame-parameters').");
14397 Vicon_title_format
14398 = Vframe_title_format
14399 = Fcons (intern ("multiple-frames"),
14400 Fcons (build_string ("%b"),
14401 Fcons (Fcons (build_string (""),
14402 Fcons (intern ("invocation-name"),
14403 Fcons (build_string ("@"),
14404 Fcons (intern ("system-name"),
14405 Qnil)))),
14406 Qnil)));
14408 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14409 "Maximum number of lines to keep in the message log buffer.\n\
14410 If nil, disable message logging. If t, log messages but don't truncate\n\
14411 the buffer when it becomes large.");
14412 Vmessage_log_max = make_number (50);
14414 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14415 "Functions called before redisplay, if window sizes have changed.\n\
14416 The value should be a list of functions that take one argument.\n\
14417 Just before redisplay, for each frame, if any of its windows have changed\n\
14418 size since the last redisplay, or have been split or deleted,\n\
14419 all the functions in the list are called, with the frame as argument.");
14420 Vwindow_size_change_functions = Qnil;
14422 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14423 "List of Functions to call before redisplaying a window with scrolling.\n\
14424 Each function is called with two arguments, the window\n\
14425 and its new display-start position. Note that the value of `window-end'\n\
14426 is not valid when these functions are called.");
14427 Vwindow_scroll_functions = Qnil;
14429 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14430 "*Non-nil means automatically resize tool-bars.\n\
14431 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14432 It decreases a tool-bar's height when it would display blank lines\n\
14433 otherwise.");
14434 auto_resize_tool_bars_p = 1;
14436 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14437 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14438 auto_raise_tool_bar_buttons_p = 1;
14440 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14441 "*Margin around tool-bar buttons in pixels.\n\
14442 If an integer, use that for both horizontal and vertical margins.\n\
14443 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14444 HORZ specifying the horizontal margin, and VERT specifying the\n\
14445 vertical margin.");
14446 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14448 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14449 "Relief thickness of tool-bar buttons.");
14450 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14452 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14453 "List of functions to call to fontify regions of text.\n\
14454 Each function is called with one argument POS. Functions must\n\
14455 fontify a region starting at POS in the current buffer, and give\n\
14456 fontified regions the property `fontified'.\n\
14457 This variable automatically becomes buffer-local when set.");
14458 Vfontification_functions = Qnil;
14459 Fmake_variable_buffer_local (Qfontification_functions);
14461 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14462 &unibyte_display_via_language_environment,
14463 "*Non-nil means display unibyte text according to language environment.\n\
14464 Specifically this means that unibyte non-ASCII characters\n\
14465 are displayed by converting them to the equivalent multibyte characters\n\
14466 according to the current language environment. As a result, they are\n\
14467 displayed according to the current fontset.");
14468 unibyte_display_via_language_environment = 0;
14470 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14471 "*Maximum height for resizing mini-windows.\n\
14472 If a float, it specifies a fraction of the mini-window frame's height.\n\
14473 If an integer, it specifies a number of lines.");
14474 Vmax_mini_window_height = make_float (0.25);
14476 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14477 "*How to resize mini-windows.\n\
14478 A value of nil means don't automatically resize mini-windows.\n\
14479 A value of t means resize them to fit the text displayed in them.\n\
14480 A value of `grow-only', the default, means let mini-windows grow\n\
14481 only, until their display becomes empty, at which point the windows\n\
14482 go back to their normal size.");
14483 Vresize_mini_windows = Qgrow_only;
14485 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14486 &cursor_in_non_selected_windows,
14487 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14488 Nil means don't display a cursor there.");
14489 cursor_in_non_selected_windows = 1;
14491 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14492 "*Non-nil means scroll the display automatically to make point visible.");
14493 automatic_hscrolling_p = 1;
14495 DEFVAR_LISP ("image-types", &Vimage_types,
14496 "List of supported image types.\n\
14497 Each element of the list is a symbol for a supported image type.");
14498 Vimage_types = Qnil;
14500 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14501 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14502 Bind this around calls to `message' to let it take effect.");
14503 message_truncate_lines = 0;
14505 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14506 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14507 Can be used to update submenus whose contents should vary.");
14508 Vmenu_bar_update_hook = Qnil;
14510 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14511 "Non-nil means don't update menu bars. Internal use only.");
14512 inhibit_menubar_update = 0;
14516 /* Initialize this module when Emacs starts. */
14518 void
14519 init_xdisp ()
14521 Lisp_Object root_window;
14522 struct window *mini_w;
14524 current_header_line_height = current_mode_line_height = -1;
14526 CHARPOS (this_line_start_pos) = 0;
14528 mini_w = XWINDOW (minibuf_window);
14529 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14531 if (!noninteractive)
14533 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14534 int i;
14536 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14537 set_window_height (root_window,
14538 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14540 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14541 set_window_height (minibuf_window, 1, 0);
14543 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14544 mini_w->width = make_number (FRAME_WIDTH (f));
14546 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14547 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14548 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14550 /* The default ellipsis glyphs `...'. */
14551 for (i = 0; i < 3; ++i)
14552 default_invis_vector[i] = make_number ('.');
14555 #ifdef HAVE_WINDOW_SYSTEM
14557 /* Allocate the buffer for frame titles. */
14558 int size = 100;
14559 frame_title_buf = (char *) xmalloc (size);
14560 frame_title_buf_end = frame_title_buf + size;
14561 frame_title_ptr = NULL;
14563 #endif /* HAVE_WINDOW_SYSTEM */
14565 help_echo_showing_p = 0;